]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
OpenMP: Fixes for omp critical + hint
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45 #include "target.h"
46
47 /* The type of functions taking a tree, and some additional data, and
48 returning an int. */
49 typedef int (*tree_fn_t) (tree, void*);
50
51 /* The PENDING_TEMPLATES is a list of templates whose instantiations
52 have been deferred, either because their definitions were not yet
53 available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
55 {
56 struct pending_template *next;
57 struct tinst_level *tinst;
58 };
59
60 static GTY(()) struct pending_template *pending_templates;
61 static GTY(()) struct pending_template *last_pending_template;
62
63 int processing_template_parmlist;
64 static int template_header_count;
65
66 static GTY(()) tree saved_trees;
67 static vec<int> inline_parm_levels;
68
69 static GTY(()) struct tinst_level *current_tinst_level;
70
71 static GTY(()) vec<tree, va_gc> *saved_access_scope;
72
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
77
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
80 //
81 // Implementation of the RAII helper for creating new local
82 // specializations.
83 local_specialization_stack::local_specialization_stack (lss_policy policy)
84 : saved (local_specializations)
85 {
86 if (policy == lss_nop)
87 ;
88 else if (policy == lss_blank || !saved)
89 local_specializations = new hash_map<tree, tree>;
90 else
91 local_specializations = new hash_map<tree, tree>(*saved);
92 }
93
94 local_specialization_stack::~local_specialization_stack ()
95 {
96 if (local_specializations != saved)
97 {
98 delete local_specializations;
99 local_specializations = saved;
100 }
101 }
102
103 /* True if we've recursed into fn_type_unification too many times. */
104 static bool excessive_deduction_depth;
105
106 struct GTY((for_user)) spec_entry
107 {
108 tree tmpl;
109 tree args;
110 tree spec;
111 };
112
113 struct spec_hasher : ggc_ptr_hash<spec_entry>
114 {
115 static hashval_t hash (spec_entry *);
116 static bool equal (spec_entry *, spec_entry *);
117 };
118
119 /* The general template is not in these tables. */
120 typedef hash_table<spec_hasher> spec_hash_table;
121 static GTY (()) spec_hash_table *decl_specializations;
122 static GTY (()) spec_hash_table *type_specializations;
123
124 /* Contains canonical template parameter types. The vector is indexed by
125 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
126 TREE_LIST, whose TREE_VALUEs contain the canonical template
127 parameters of various types and levels. */
128 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
129
130 #define UNIFY_ALLOW_NONE 0
131 #define UNIFY_ALLOW_MORE_CV_QUAL 1
132 #define UNIFY_ALLOW_LESS_CV_QUAL 2
133 #define UNIFY_ALLOW_DERIVED 4
134 #define UNIFY_ALLOW_INTEGER 8
135 #define UNIFY_ALLOW_OUTER_LEVEL 16
136 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
137 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
138
139 enum template_base_result {
140 tbr_incomplete_type,
141 tbr_ambiguous_baseclass,
142 tbr_success
143 };
144
145 static bool resolve_overloaded_unification (tree, tree, tree, tree,
146 unification_kind_t, int,
147 bool);
148 static int try_one_overload (tree, tree, tree, tree, tree,
149 unification_kind_t, int, bool, bool);
150 static int unify (tree, tree, tree, tree, int, bool);
151 static void add_pending_template (tree);
152 static tree reopen_tinst_level (struct tinst_level *);
153 static tree tsubst_initializer_list (tree, tree);
154 static tree get_partial_spec_bindings (tree, tree, tree);
155 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
156 bool, bool);
157 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
158 bool, bool);
159 static void tsubst_enum (tree, tree, tree);
160 static tree add_to_template_args (tree, tree);
161 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
162 static int check_non_deducible_conversion (tree, tree, int, int,
163 struct conversion **, bool);
164 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
165 tree);
166 static int type_unification_real (tree, tree, tree, const tree *,
167 unsigned int, int, unification_kind_t,
168 vec<deferred_access_check, va_gc> **,
169 bool);
170 static void note_template_header (int);
171 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
172 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
173 static tree convert_template_argument (tree, tree, tree,
174 tsubst_flags_t, int, tree);
175 static tree for_each_template_parm (tree, tree_fn_t, void*,
176 hash_set<tree> *, bool, tree_fn_t = NULL);
177 static tree expand_template_argument_pack (tree);
178 static tree build_template_parm_index (int, int, int, tree, tree);
179 static bool inline_needs_template_parms (tree, bool);
180 static void push_inline_template_parms_recursive (tree, int);
181 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
182 static int mark_template_parm (tree, void *);
183 static int template_parm_this_level_p (tree, void *);
184 static tree tsubst_friend_function (tree, tree);
185 static tree tsubst_friend_class (tree, tree);
186 static int can_complete_type_without_circularity (tree);
187 static tree get_bindings (tree, tree, tree, bool);
188 static int template_decl_level (tree);
189 static int check_cv_quals_for_unify (int, tree, tree);
190 static int unify_pack_expansion (tree, tree, tree,
191 tree, unification_kind_t, bool, bool);
192 static tree copy_template_args (tree);
193 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
194 tree most_specialized_partial_spec (tree, tsubst_flags_t);
195 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
196 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
197 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
198 static bool check_specialization_scope (void);
199 static tree process_partial_specialization (tree);
200 static void set_current_access_from_decl (tree);
201 static enum template_base_result get_template_base (tree, tree, tree, tree,
202 bool , tree *);
203 static tree try_class_unification (tree, tree, tree, tree, bool);
204 static bool class_nttp_const_wrapper_p (tree t);
205 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
206 tree, tree);
207 static bool template_template_parm_bindings_ok_p (tree, tree);
208 static void tsubst_default_arguments (tree, tsubst_flags_t);
209 static tree for_each_template_parm_r (tree *, int *, void *);
210 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
211 static void copy_default_args_to_explicit_spec (tree);
212 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
213 static bool dependent_template_arg_p (tree);
214 static bool any_template_arguments_need_structural_equality_p (tree);
215 static bool dependent_type_p_r (tree);
216 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
217 static tree tsubst_decl (tree, tree, tsubst_flags_t);
218 static void perform_instantiation_time_access_checks (tree, tree);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
223 static bool complex_alias_template_p (const_tree tmpl);
224 static tree get_underlying_template (tree);
225 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
226 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
227 static tree make_argument_pack (tree);
228 static void register_parameter_specializations (tree, tree);
229 static tree enclosing_instantiation_of (tree tctx);
230
231 /* Make the current scope suitable for access checking when we are
232 processing T. T can be FUNCTION_DECL for instantiated function
233 template, VAR_DECL for static member variable, or TYPE_DECL for
234 alias template (needed by instantiate_decl). */
235
236 void
237 push_access_scope (tree t)
238 {
239 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
240 || TREE_CODE (t) == TYPE_DECL);
241
242 if (DECL_FRIEND_CONTEXT (t))
243 push_nested_class (DECL_FRIEND_CONTEXT (t));
244 else if (DECL_CLASS_SCOPE_P (t))
245 push_nested_class (DECL_CONTEXT (t));
246 else
247 push_to_top_level ();
248
249 if (TREE_CODE (t) == FUNCTION_DECL)
250 {
251 vec_safe_push (saved_access_scope, current_function_decl);
252 current_function_decl = t;
253 }
254 }
255
256 /* Restore the scope set up by push_access_scope. T is the node we
257 are processing. */
258
259 void
260 pop_access_scope (tree t)
261 {
262 if (TREE_CODE (t) == FUNCTION_DECL)
263 current_function_decl = saved_access_scope->pop();
264
265 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
266 pop_nested_class ();
267 else
268 pop_from_top_level ();
269 }
270
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
275
276 tree
277 finish_member_template_decl (tree decl)
278 {
279 if (decl == error_mark_node)
280 return error_mark_node;
281
282 gcc_assert (DECL_P (decl));
283
284 if (TREE_CODE (decl) == TYPE_DECL)
285 {
286 tree type;
287
288 type = TREE_TYPE (decl);
289 if (type == error_mark_node)
290 return error_mark_node;
291 if (MAYBE_CLASS_TYPE_P (type)
292 && CLASSTYPE_TEMPLATE_INFO (type)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
294 {
295 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
296 check_member_template (tmpl);
297 return tmpl;
298 }
299 return NULL_TREE;
300 }
301 else if (TREE_CODE (decl) == FIELD_DECL)
302 error_at (DECL_SOURCE_LOCATION (decl),
303 "data member %qD cannot be a member template", decl);
304 else if (DECL_TEMPLATE_INFO (decl))
305 {
306 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
307 {
308 check_member_template (DECL_TI_TEMPLATE (decl));
309 return DECL_TI_TEMPLATE (decl);
310 }
311 else
312 return decl;
313 }
314 else
315 error_at (DECL_SOURCE_LOCATION (decl),
316 "invalid member template declaration %qD", decl);
317
318 return error_mark_node;
319 }
320
321 /* Create a template info node. */
322
323 tree
324 build_template_info (tree template_decl, tree template_args)
325 {
326 tree result = make_node (TEMPLATE_INFO);
327 TI_TEMPLATE (result) = template_decl;
328 TI_ARGS (result) = template_args;
329 return result;
330 }
331
332 /* Return the template info node corresponding to T, whatever T is. */
333
334 tree
335 get_template_info (const_tree t)
336 {
337 tree tinfo = NULL_TREE;
338
339 if (!t || t == error_mark_node)
340 return NULL;
341
342 if (TREE_CODE (t) == NAMESPACE_DECL
343 || TREE_CODE (t) == PARM_DECL)
344 return NULL;
345
346 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
347 tinfo = DECL_TEMPLATE_INFO (t);
348
349 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
350 t = TREE_TYPE (t);
351
352 if (OVERLOAD_TYPE_P (t))
353 tinfo = TYPE_TEMPLATE_INFO (t);
354 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
355 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
356
357 return tinfo;
358 }
359
360 /* Returns the template nesting level of the indicated class TYPE.
361
362 For example, in:
363 template <class T>
364 struct A
365 {
366 template <class U>
367 struct B {};
368 };
369
370 A<T>::B<U> has depth two, while A<T> has depth one.
371 Both A<T>::B<int> and A<int>::B<U> have depth one, if
372 they are instantiations, not specializations.
373
374 This function is guaranteed to return 0 if passed NULL_TREE so
375 that, for example, `template_class_depth (current_class_type)' is
376 always safe. */
377
378 int
379 template_class_depth (tree type)
380 {
381 int depth;
382
383 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
384 {
385 tree tinfo = get_template_info (type);
386
387 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
388 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
389 ++depth;
390
391 if (DECL_P (type))
392 {
393 if (tree fctx = DECL_FRIEND_CONTEXT (type))
394 type = fctx;
395 else
396 type = CP_DECL_CONTEXT (type);
397 }
398 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
399 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
400 else
401 type = CP_TYPE_CONTEXT (type);
402 }
403
404 return depth;
405 }
406
407 /* Return TRUE if NODE instantiates a template that has arguments of
408 its own, be it directly a primary template or indirectly through a
409 partial specializations. */
410 static bool
411 instantiates_primary_template_p (tree node)
412 {
413 tree tinfo = get_template_info (node);
414 if (!tinfo)
415 return false;
416
417 tree tmpl = TI_TEMPLATE (tinfo);
418 if (PRIMARY_TEMPLATE_P (tmpl))
419 return true;
420
421 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
422 return false;
423
424 /* So now we know we have a specialization, but it could be a full
425 or a partial specialization. To tell which, compare the depth of
426 its template arguments with those of its context. */
427
428 tree ctxt = DECL_CONTEXT (tmpl);
429 tree ctinfo = get_template_info (ctxt);
430 if (!ctinfo)
431 return true;
432
433 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
434 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
435 }
436
437 /* Subroutine of maybe_begin_member_template_processing.
438 Returns true if processing DECL needs us to push template parms. */
439
440 static bool
441 inline_needs_template_parms (tree decl, bool nsdmi)
442 {
443 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
444 return false;
445
446 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
447 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
448 }
449
450 /* Subroutine of maybe_begin_member_template_processing.
451 Push the template parms in PARMS, starting from LEVELS steps into the
452 chain, and ending at the beginning, since template parms are listed
453 innermost first. */
454
455 static void
456 push_inline_template_parms_recursive (tree parmlist, int levels)
457 {
458 tree parms = TREE_VALUE (parmlist);
459 int i;
460
461 if (levels > 1)
462 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
463
464 ++processing_template_decl;
465 current_template_parms
466 = tree_cons (size_int (processing_template_decl),
467 parms, current_template_parms);
468 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
469
470 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
471 NULL);
472 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
473 {
474 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
475
476 if (error_operand_p (parm))
477 continue;
478
479 gcc_assert (DECL_P (parm));
480
481 switch (TREE_CODE (parm))
482 {
483 case TYPE_DECL:
484 case TEMPLATE_DECL:
485 pushdecl (parm);
486 break;
487
488 case PARM_DECL:
489 /* Push the CONST_DECL. */
490 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
491 break;
492
493 default:
494 gcc_unreachable ();
495 }
496 }
497 }
498
499 /* Restore the template parameter context for a member template, a
500 friend template defined in a class definition, or a non-template
501 member of template class. */
502
503 void
504 maybe_begin_member_template_processing (tree decl)
505 {
506 tree parms;
507 int levels = 0;
508 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
509
510 if (nsdmi)
511 {
512 tree ctx = DECL_CONTEXT (decl);
513 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
514 /* Disregard full specializations (c++/60999). */
515 && uses_template_parms (ctx)
516 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
517 }
518
519 if (inline_needs_template_parms (decl, nsdmi))
520 {
521 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
522 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
523
524 if (DECL_TEMPLATE_SPECIALIZATION (decl))
525 {
526 --levels;
527 parms = TREE_CHAIN (parms);
528 }
529
530 push_inline_template_parms_recursive (parms, levels);
531 }
532
533 /* Remember how many levels of template parameters we pushed so that
534 we can pop them later. */
535 inline_parm_levels.safe_push (levels);
536 }
537
538 /* Undo the effects of maybe_begin_member_template_processing. */
539
540 void
541 maybe_end_member_template_processing (void)
542 {
543 int i;
544 int last;
545
546 if (inline_parm_levels.length () == 0)
547 return;
548
549 last = inline_parm_levels.pop ();
550 for (i = 0; i < last; ++i)
551 {
552 --processing_template_decl;
553 current_template_parms = TREE_CHAIN (current_template_parms);
554 poplevel (0, 0, 0);
555 }
556 }
557
558 /* Return a new template argument vector which contains all of ARGS,
559 but has as its innermost set of arguments the EXTRA_ARGS. */
560
561 static tree
562 add_to_template_args (tree args, tree extra_args)
563 {
564 tree new_args;
565 int extra_depth;
566 int i;
567 int j;
568
569 if (args == NULL_TREE || extra_args == error_mark_node)
570 return extra_args;
571
572 extra_depth = TMPL_ARGS_DEPTH (extra_args);
573 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
574
575 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
576 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
577
578 for (j = 1; j <= extra_depth; ++j, ++i)
579 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
580
581 return new_args;
582 }
583
584 /* Like add_to_template_args, but only the outermost ARGS are added to
585 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
586 (EXTRA_ARGS) levels are added. This function is used to combine
587 the template arguments from a partial instantiation with the
588 template arguments used to attain the full instantiation from the
589 partial instantiation. */
590
591 tree
592 add_outermost_template_args (tree args, tree extra_args)
593 {
594 tree new_args;
595
596 /* If there are more levels of EXTRA_ARGS than there are ARGS,
597 something very fishy is going on. */
598 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
599
600 /* If *all* the new arguments will be the EXTRA_ARGS, just return
601 them. */
602 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
603 return extra_args;
604
605 /* For the moment, we make ARGS look like it contains fewer levels. */
606 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
607
608 new_args = add_to_template_args (args, extra_args);
609
610 /* Now, we restore ARGS to its full dimensions. */
611 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
612
613 return new_args;
614 }
615
616 /* Return the N levels of innermost template arguments from the ARGS. */
617
618 tree
619 get_innermost_template_args (tree args, int n)
620 {
621 tree new_args;
622 int extra_levels;
623 int i;
624
625 gcc_assert (n >= 0);
626
627 /* If N is 1, just return the innermost set of template arguments. */
628 if (n == 1)
629 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
630
631 /* If we're not removing anything, just return the arguments we were
632 given. */
633 extra_levels = TMPL_ARGS_DEPTH (args) - n;
634 gcc_assert (extra_levels >= 0);
635 if (extra_levels == 0)
636 return args;
637
638 /* Make a new set of arguments, not containing the outer arguments. */
639 new_args = make_tree_vec (n);
640 for (i = 1; i <= n; ++i)
641 SET_TMPL_ARGS_LEVEL (new_args, i,
642 TMPL_ARGS_LEVEL (args, i + extra_levels));
643
644 return new_args;
645 }
646
647 /* The inverse of get_innermost_template_args: Return all but the innermost
648 EXTRA_LEVELS levels of template arguments from the ARGS. */
649
650 static tree
651 strip_innermost_template_args (tree args, int extra_levels)
652 {
653 tree new_args;
654 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
655 int i;
656
657 gcc_assert (n >= 0);
658
659 /* If N is 1, just return the outermost set of template arguments. */
660 if (n == 1)
661 return TMPL_ARGS_LEVEL (args, 1);
662
663 /* If we're not removing anything, just return the arguments we were
664 given. */
665 gcc_assert (extra_levels >= 0);
666 if (extra_levels == 0)
667 return args;
668
669 /* Make a new set of arguments, not containing the inner arguments. */
670 new_args = make_tree_vec (n);
671 for (i = 1; i <= n; ++i)
672 SET_TMPL_ARGS_LEVEL (new_args, i,
673 TMPL_ARGS_LEVEL (args, i));
674
675 return new_args;
676 }
677
678 /* We've got a template header coming up; push to a new level for storing
679 the parms. */
680
681 void
682 begin_template_parm_list (void)
683 {
684 /* We use a non-tag-transparent scope here, which causes pushtag to
685 put tags in this scope, rather than in the enclosing class or
686 namespace scope. This is the right thing, since we want
687 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
688 global template class, push_template_decl handles putting the
689 TEMPLATE_DECL into top-level scope. For a nested template class,
690 e.g.:
691
692 template <class T> struct S1 {
693 template <class T> struct S2 {};
694 };
695
696 pushtag contains special code to insert the TEMPLATE_DECL for S2
697 at the right scope. */
698 begin_scope (sk_template_parms, NULL);
699 ++processing_template_decl;
700 ++processing_template_parmlist;
701 note_template_header (0);
702
703 /* Add a dummy parameter level while we process the parameter list. */
704 current_template_parms
705 = tree_cons (size_int (processing_template_decl),
706 make_tree_vec (0),
707 current_template_parms);
708 }
709
710 /* This routine is called when a specialization is declared. If it is
711 invalid to declare a specialization here, an error is reported and
712 false is returned, otherwise this routine will return true. */
713
714 static bool
715 check_specialization_scope (void)
716 {
717 tree scope = current_scope ();
718
719 /* [temp.expl.spec]
720
721 An explicit specialization shall be declared in the namespace of
722 which the template is a member, or, for member templates, in the
723 namespace of which the enclosing class or enclosing class
724 template is a member. An explicit specialization of a member
725 function, member class or static data member of a class template
726 shall be declared in the namespace of which the class template
727 is a member. */
728 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
729 {
730 error ("explicit specialization in non-namespace scope %qD", scope);
731 return false;
732 }
733
734 /* [temp.expl.spec]
735
736 In an explicit specialization declaration for a member of a class
737 template or a member template that appears in namespace scope,
738 the member template and some of its enclosing class templates may
739 remain unspecialized, except that the declaration shall not
740 explicitly specialize a class member template if its enclosing
741 class templates are not explicitly specialized as well. */
742 if (current_template_parms)
743 {
744 error ("enclosing class templates are not explicitly specialized");
745 return false;
746 }
747
748 return true;
749 }
750
751 /* We've just seen template <>. */
752
753 bool
754 begin_specialization (void)
755 {
756 begin_scope (sk_template_spec, NULL);
757 note_template_header (1);
758 return check_specialization_scope ();
759 }
760
761 /* Called at then end of processing a declaration preceded by
762 template<>. */
763
764 void
765 end_specialization (void)
766 {
767 finish_scope ();
768 reset_specialization ();
769 }
770
771 /* Any template <>'s that we have seen thus far are not referring to a
772 function specialization. */
773
774 void
775 reset_specialization (void)
776 {
777 processing_specialization = 0;
778 template_header_count = 0;
779 }
780
781 /* We've just seen a template header. If SPECIALIZATION is nonzero,
782 it was of the form template <>. */
783
784 static void
785 note_template_header (int specialization)
786 {
787 processing_specialization = specialization;
788 template_header_count++;
789 }
790
791 /* We're beginning an explicit instantiation. */
792
793 void
794 begin_explicit_instantiation (void)
795 {
796 gcc_assert (!processing_explicit_instantiation);
797 processing_explicit_instantiation = true;
798 }
799
800
801 void
802 end_explicit_instantiation (void)
803 {
804 gcc_assert (processing_explicit_instantiation);
805 processing_explicit_instantiation = false;
806 }
807
808 /* An explicit specialization or partial specialization of TMPL is being
809 declared. Check that the namespace in which the specialization is
810 occurring is permissible. Returns false iff it is invalid to
811 specialize TMPL in the current namespace. */
812
813 static bool
814 check_specialization_namespace (tree tmpl)
815 {
816 tree tpl_ns = decl_namespace_context (tmpl);
817
818 /* [tmpl.expl.spec]
819
820 An explicit specialization shall be declared in a namespace enclosing the
821 specialized template. An explicit specialization whose declarator-id is
822 not qualified shall be declared in the nearest enclosing namespace of the
823 template, or, if the namespace is inline (7.3.1), any namespace from its
824 enclosing namespace set. */
825 if (current_scope() != DECL_CONTEXT (tmpl)
826 && !at_namespace_scope_p ())
827 {
828 error ("specialization of %qD must appear at namespace scope", tmpl);
829 return false;
830 }
831
832 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
833 /* Same or enclosing namespace. */
834 return true;
835 else
836 {
837 auto_diagnostic_group d;
838 if (permerror (input_location,
839 "specialization of %qD in different namespace", tmpl))
840 inform (DECL_SOURCE_LOCATION (tmpl),
841 " from definition of %q#D", tmpl);
842 return false;
843 }
844 }
845
846 /* SPEC is an explicit instantiation. Check that it is valid to
847 perform this explicit instantiation in the current namespace. */
848
849 static void
850 check_explicit_instantiation_namespace (tree spec)
851 {
852 tree ns;
853
854 /* DR 275: An explicit instantiation shall appear in an enclosing
855 namespace of its template. */
856 ns = decl_namespace_context (spec);
857 if (!is_nested_namespace (current_namespace, ns))
858 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
859 "(which does not enclose namespace %qD)",
860 spec, current_namespace, ns);
861 }
862
863 /* Returns the type of a template specialization only if that
864 specialization needs to be defined. Otherwise (e.g., if the type has
865 already been defined), the function returns NULL_TREE. */
866
867 static tree
868 maybe_new_partial_specialization (tree type)
869 {
870 /* An implicit instantiation of an incomplete type implies
871 the definition of a new class template.
872
873 template<typename T>
874 struct S;
875
876 template<typename T>
877 struct S<T*>;
878
879 Here, S<T*> is an implicit instantiation of S whose type
880 is incomplete. */
881 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
882 return type;
883
884 /* It can also be the case that TYPE is a completed specialization.
885 Continuing the previous example, suppose we also declare:
886
887 template<typename T>
888 requires Integral<T>
889 struct S<T*>;
890
891 Here, S<T*> refers to the specialization S<T*> defined
892 above. However, we need to differentiate definitions because
893 we intend to define a new partial specialization. In this case,
894 we rely on the fact that the constraints are different for
895 this declaration than that above.
896
897 Note that we also get here for injected class names and
898 late-parsed template definitions. We must ensure that we
899 do not create new type declarations for those cases. */
900 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
901 {
902 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
903 tree args = CLASSTYPE_TI_ARGS (type);
904
905 /* If there are no template parameters, this cannot be a new
906 partial template specialization? */
907 if (!current_template_parms)
908 return NULL_TREE;
909
910 /* The injected-class-name is not a new partial specialization. */
911 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
912 return NULL_TREE;
913
914 /* If the constraints are not the same as those of the primary
915 then, we can probably create a new specialization. */
916 tree type_constr = current_template_constraints ();
917
918 if (type == TREE_TYPE (tmpl))
919 {
920 tree main_constr = get_constraints (tmpl);
921 if (equivalent_constraints (type_constr, main_constr))
922 return NULL_TREE;
923 }
924
925 /* Also, if there's a pre-existing specialization with matching
926 constraints, then this also isn't new. */
927 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
928 while (specs)
929 {
930 tree spec_tmpl = TREE_VALUE (specs);
931 tree spec_args = TREE_PURPOSE (specs);
932 tree spec_constr = get_constraints (spec_tmpl);
933 if (comp_template_args (args, spec_args)
934 && equivalent_constraints (type_constr, spec_constr))
935 return NULL_TREE;
936 specs = TREE_CHAIN (specs);
937 }
938
939 /* Create a new type node (and corresponding type decl)
940 for the newly declared specialization. */
941 tree t = make_class_type (TREE_CODE (type));
942 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
943 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
944
945 /* We only need a separate type node for storing the definition of this
946 partial specialization; uses of S<T*> are unconstrained, so all are
947 equivalent. So keep TYPE_CANONICAL the same. */
948 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
949
950 /* Build the corresponding type decl. */
951 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
952 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
953 DECL_SOURCE_LOCATION (d) = input_location;
954 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
955 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
956
957 return t;
958 }
959
960 return NULL_TREE;
961 }
962
963 /* The TYPE is being declared. If it is a template type, that means it
964 is a partial specialization. Do appropriate error-checking. */
965
966 tree
967 maybe_process_partial_specialization (tree type)
968 {
969 tree context;
970
971 if (type == error_mark_node)
972 return error_mark_node;
973
974 /* A lambda that appears in specialization context is not itself a
975 specialization. */
976 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
977 return type;
978
979 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
980 {
981 error ("name of class shadows template template parameter %qD",
982 TYPE_NAME (type));
983 return error_mark_node;
984 }
985
986 context = TYPE_CONTEXT (type);
987
988 if (TYPE_ALIAS_P (type))
989 {
990 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
991
992 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
993 error ("specialization of alias template %qD",
994 TI_TEMPLATE (tinfo));
995 else
996 error ("explicit specialization of non-template %qT", type);
997 return error_mark_node;
998 }
999 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1000 {
1001 /* This is for ordinary explicit specialization and partial
1002 specialization of a template class such as:
1003
1004 template <> class C<int>;
1005
1006 or:
1007
1008 template <class T> class C<T*>;
1009
1010 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1011
1012 if (tree t = maybe_new_partial_specialization (type))
1013 {
1014 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1015 && !at_namespace_scope_p ())
1016 return error_mark_node;
1017 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1018 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1019 if (processing_template_decl)
1020 {
1021 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1022 if (decl == error_mark_node)
1023 return error_mark_node;
1024 return TREE_TYPE (decl);
1025 }
1026 }
1027 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1028 error ("specialization of %qT after instantiation", type);
1029 else if (errorcount && !processing_specialization
1030 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1031 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1032 /* Trying to define a specialization either without a template<> header
1033 or in an inappropriate place. We've already given an error, so just
1034 bail now so we don't actually define the specialization. */
1035 return error_mark_node;
1036 }
1037 else if (CLASS_TYPE_P (type)
1038 && !CLASSTYPE_USE_TEMPLATE (type)
1039 && CLASSTYPE_TEMPLATE_INFO (type)
1040 && context && CLASS_TYPE_P (context)
1041 && CLASSTYPE_TEMPLATE_INFO (context))
1042 {
1043 /* This is for an explicit specialization of member class
1044 template according to [temp.expl.spec/18]:
1045
1046 template <> template <class U> class C<int>::D;
1047
1048 The context `C<int>' must be an implicit instantiation.
1049 Otherwise this is just a member class template declared
1050 earlier like:
1051
1052 template <> class C<int> { template <class U> class D; };
1053 template <> template <class U> class C<int>::D;
1054
1055 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1056 while in the second case, `C<int>::D' is a primary template
1057 and `C<T>::D' may not exist. */
1058
1059 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1060 && !COMPLETE_TYPE_P (type))
1061 {
1062 tree t;
1063 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1064
1065 if (current_namespace
1066 != decl_namespace_context (tmpl))
1067 {
1068 if (permerror (input_location,
1069 "specialization of %qD in different namespace",
1070 type))
1071 inform (DECL_SOURCE_LOCATION (tmpl),
1072 "from definition of %q#D", tmpl);
1073 }
1074
1075 /* Check for invalid specialization after instantiation:
1076
1077 template <> template <> class C<int>::D<int>;
1078 template <> template <class U> class C<int>::D; */
1079
1080 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1081 t; t = TREE_CHAIN (t))
1082 {
1083 tree inst = TREE_VALUE (t);
1084 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1085 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1086 {
1087 /* We already have a full specialization of this partial
1088 instantiation, or a full specialization has been
1089 looked up but not instantiated. Reassign it to the
1090 new member specialization template. */
1091 spec_entry elt;
1092 spec_entry *entry;
1093
1094 elt.tmpl = most_general_template (tmpl);
1095 elt.args = CLASSTYPE_TI_ARGS (inst);
1096 elt.spec = inst;
1097
1098 type_specializations->remove_elt (&elt);
1099
1100 elt.tmpl = tmpl;
1101 CLASSTYPE_TI_ARGS (inst)
1102 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1103
1104 spec_entry **slot
1105 = type_specializations->find_slot (&elt, INSERT);
1106 entry = ggc_alloc<spec_entry> ();
1107 *entry = elt;
1108 *slot = entry;
1109 }
1110 else
1111 /* But if we've had an implicit instantiation, that's a
1112 problem ([temp.expl.spec]/6). */
1113 error ("specialization %qT after instantiation %qT",
1114 type, inst);
1115 }
1116
1117 /* Mark TYPE as a specialization. And as a result, we only
1118 have one level of template argument for the innermost
1119 class template. */
1120 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1121 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1122 CLASSTYPE_TI_ARGS (type)
1123 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1124 }
1125 }
1126 else if (processing_specialization)
1127 {
1128 /* Someday C++0x may allow for enum template specialization. */
1129 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1130 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1131 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1132 "of %qD not allowed by ISO C++", type);
1133 else
1134 {
1135 error ("explicit specialization of non-template %qT", type);
1136 return error_mark_node;
1137 }
1138 }
1139
1140 return type;
1141 }
1142
1143 /* Returns nonzero if we can optimize the retrieval of specializations
1144 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1145 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1146
1147 static inline bool
1148 optimize_specialization_lookup_p (tree tmpl)
1149 {
1150 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1151 && DECL_CLASS_SCOPE_P (tmpl)
1152 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1153 parameter. */
1154 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1155 /* The optimized lookup depends on the fact that the
1156 template arguments for the member function template apply
1157 purely to the containing class, which is not true if the
1158 containing class is an explicit or partial
1159 specialization. */
1160 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1161 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1162 && !DECL_CONV_FN_P (tmpl)
1163 /* It is possible to have a template that is not a member
1164 template and is not a member of a template class:
1165
1166 template <typename T>
1167 struct S { friend A::f(); };
1168
1169 Here, the friend function is a template, but the context does
1170 not have template information. The optimized lookup relies
1171 on having ARGS be the template arguments for both the class
1172 and the function template. */
1173 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1174 }
1175
1176 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1177 gone through coerce_template_parms by now. */
1178
1179 static void
1180 verify_unstripped_args_1 (tree inner)
1181 {
1182 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1183 {
1184 tree arg = TREE_VEC_ELT (inner, i);
1185 if (TREE_CODE (arg) == TEMPLATE_DECL)
1186 /* OK */;
1187 else if (TYPE_P (arg))
1188 gcc_assert (strip_typedefs (arg, NULL) == arg);
1189 else if (ARGUMENT_PACK_P (arg))
1190 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1191 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1192 /* Allow typedefs on the type of a non-type argument, since a
1193 parameter can have them. */;
1194 else
1195 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1196 }
1197 }
1198
1199 static void
1200 verify_unstripped_args (tree args)
1201 {
1202 ++processing_template_decl;
1203 if (!any_dependent_template_arguments_p (args))
1204 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1205 --processing_template_decl;
1206 }
1207
1208 /* Retrieve the specialization (in the sense of [temp.spec] - a
1209 specialization is either an instantiation or an explicit
1210 specialization) of TMPL for the given template ARGS. If there is
1211 no such specialization, return NULL_TREE. The ARGS are a vector of
1212 arguments, or a vector of vectors of arguments, in the case of
1213 templates with more than one level of parameters.
1214
1215 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1216 then we search for a partial specialization matching ARGS. This
1217 parameter is ignored if TMPL is not a class template.
1218
1219 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1220 result is a NONTYPE_ARGUMENT_PACK. */
1221
1222 static tree
1223 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1224 {
1225 if (tmpl == NULL_TREE)
1226 return NULL_TREE;
1227
1228 if (args == error_mark_node)
1229 return NULL_TREE;
1230
1231 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1232 || TREE_CODE (tmpl) == FIELD_DECL);
1233
1234 /* There should be as many levels of arguments as there are
1235 levels of parameters. */
1236 gcc_assert (TMPL_ARGS_DEPTH (args)
1237 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1238 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1239 : template_class_depth (DECL_CONTEXT (tmpl))));
1240
1241 if (flag_checking)
1242 verify_unstripped_args (args);
1243
1244 /* Lambda functions in templates aren't instantiated normally, but through
1245 tsubst_lambda_expr. */
1246 if (lambda_fn_in_template_p (tmpl))
1247 return NULL_TREE;
1248
1249 if (optimize_specialization_lookup_p (tmpl))
1250 {
1251 /* The template arguments actually apply to the containing
1252 class. Find the class specialization with those
1253 arguments. */
1254 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1255 tree class_specialization
1256 = retrieve_specialization (class_template, args, 0);
1257 if (!class_specialization)
1258 return NULL_TREE;
1259
1260 /* Find the instance of TMPL. */
1261 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1262 for (ovl_iterator iter (fns); iter; ++iter)
1263 {
1264 tree fn = *iter;
1265 if (tree ti = get_template_info (fn))
1266 if (TI_TEMPLATE (ti) == tmpl
1267 /* using-declarations can bring in a different
1268 instantiation of tmpl as a member of a different
1269 instantiation of tmpl's class. We don't want those
1270 here. */
1271 && DECL_CONTEXT (fn) == class_specialization)
1272 return fn;
1273 }
1274 return NULL_TREE;
1275 }
1276 else
1277 {
1278 spec_entry *found;
1279 spec_entry elt;
1280 spec_hash_table *specializations;
1281
1282 elt.tmpl = tmpl;
1283 elt.args = args;
1284 elt.spec = NULL_TREE;
1285
1286 if (DECL_CLASS_TEMPLATE_P (tmpl))
1287 specializations = type_specializations;
1288 else
1289 specializations = decl_specializations;
1290
1291 if (hash == 0)
1292 hash = spec_hasher::hash (&elt);
1293 found = specializations->find_with_hash (&elt, hash);
1294 if (found)
1295 return found->spec;
1296 }
1297
1298 return NULL_TREE;
1299 }
1300
1301 /* Like retrieve_specialization, but for local declarations. */
1302
1303 tree
1304 retrieve_local_specialization (tree tmpl)
1305 {
1306 if (local_specializations == NULL)
1307 return NULL_TREE;
1308
1309 tree *slot = local_specializations->get (tmpl);
1310 return slot ? *slot : NULL_TREE;
1311 }
1312
1313 /* Returns nonzero iff DECL is a specialization of TMPL. */
1314
1315 int
1316 is_specialization_of (tree decl, tree tmpl)
1317 {
1318 tree t;
1319
1320 if (TREE_CODE (decl) == FUNCTION_DECL)
1321 {
1322 for (t = decl;
1323 t != NULL_TREE;
1324 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1325 if (t == tmpl)
1326 return 1;
1327 }
1328 else
1329 {
1330 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1331
1332 for (t = TREE_TYPE (decl);
1333 t != NULL_TREE;
1334 t = CLASSTYPE_USE_TEMPLATE (t)
1335 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1336 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1337 return 1;
1338 }
1339
1340 return 0;
1341 }
1342
1343 /* Returns nonzero iff DECL is a specialization of friend declaration
1344 FRIEND_DECL according to [temp.friend]. */
1345
1346 bool
1347 is_specialization_of_friend (tree decl, tree friend_decl)
1348 {
1349 bool need_template = true;
1350 int template_depth;
1351
1352 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1353 || TREE_CODE (decl) == TYPE_DECL);
1354
1355 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1356 of a template class, we want to check if DECL is a specialization
1357 if this. */
1358 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1359 && DECL_TEMPLATE_INFO (friend_decl)
1360 && !DECL_USE_TEMPLATE (friend_decl))
1361 {
1362 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1363 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1364 need_template = false;
1365 }
1366 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1367 && !PRIMARY_TEMPLATE_P (friend_decl))
1368 need_template = false;
1369
1370 /* There is nothing to do if this is not a template friend. */
1371 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1372 return false;
1373
1374 if (is_specialization_of (decl, friend_decl))
1375 return true;
1376
1377 /* [temp.friend/6]
1378 A member of a class template may be declared to be a friend of a
1379 non-template class. In this case, the corresponding member of
1380 every specialization of the class template is a friend of the
1381 class granting friendship.
1382
1383 For example, given a template friend declaration
1384
1385 template <class T> friend void A<T>::f();
1386
1387 the member function below is considered a friend
1388
1389 template <> struct A<int> {
1390 void f();
1391 };
1392
1393 For this type of template friend, TEMPLATE_DEPTH below will be
1394 nonzero. To determine if DECL is a friend of FRIEND, we first
1395 check if the enclosing class is a specialization of another. */
1396
1397 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1398 if (template_depth
1399 && DECL_CLASS_SCOPE_P (decl)
1400 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1401 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1402 {
1403 /* Next, we check the members themselves. In order to handle
1404 a few tricky cases, such as when FRIEND_DECL's are
1405
1406 template <class T> friend void A<T>::g(T t);
1407 template <class T> template <T t> friend void A<T>::h();
1408
1409 and DECL's are
1410
1411 void A<int>::g(int);
1412 template <int> void A<int>::h();
1413
1414 we need to figure out ARGS, the template arguments from
1415 the context of DECL. This is required for template substitution
1416 of `T' in the function parameter of `g' and template parameter
1417 of `h' in the above examples. Here ARGS corresponds to `int'. */
1418
1419 tree context = DECL_CONTEXT (decl);
1420 tree args = NULL_TREE;
1421 int current_depth = 0;
1422
1423 while (current_depth < template_depth)
1424 {
1425 if (CLASSTYPE_TEMPLATE_INFO (context))
1426 {
1427 if (current_depth == 0)
1428 args = TYPE_TI_ARGS (context);
1429 else
1430 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1431 current_depth++;
1432 }
1433 context = TYPE_CONTEXT (context);
1434 }
1435
1436 if (TREE_CODE (decl) == FUNCTION_DECL)
1437 {
1438 bool is_template;
1439 tree friend_type;
1440 tree decl_type;
1441 tree friend_args_type;
1442 tree decl_args_type;
1443
1444 /* Make sure that both DECL and FRIEND_DECL are templates or
1445 non-templates. */
1446 is_template = DECL_TEMPLATE_INFO (decl)
1447 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1448 if (need_template ^ is_template)
1449 return false;
1450 else if (is_template)
1451 {
1452 /* If both are templates, check template parameter list. */
1453 tree friend_parms
1454 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1455 args, tf_none);
1456 if (!comp_template_parms
1457 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1458 friend_parms))
1459 return false;
1460
1461 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1462 }
1463 else
1464 decl_type = TREE_TYPE (decl);
1465
1466 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1467 tf_none, NULL_TREE);
1468 if (friend_type == error_mark_node)
1469 return false;
1470
1471 /* Check if return types match. */
1472 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1473 return false;
1474
1475 /* Check if function parameter types match, ignoring the
1476 `this' parameter. */
1477 friend_args_type = TYPE_ARG_TYPES (friend_type);
1478 decl_args_type = TYPE_ARG_TYPES (decl_type);
1479 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1480 friend_args_type = TREE_CHAIN (friend_args_type);
1481 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1482 decl_args_type = TREE_CHAIN (decl_args_type);
1483
1484 return compparms (decl_args_type, friend_args_type);
1485 }
1486 else
1487 {
1488 /* DECL is a TYPE_DECL */
1489 bool is_template;
1490 tree decl_type = TREE_TYPE (decl);
1491
1492 /* Make sure that both DECL and FRIEND_DECL are templates or
1493 non-templates. */
1494 is_template
1495 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1496 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1497
1498 if (need_template ^ is_template)
1499 return false;
1500 else if (is_template)
1501 {
1502 tree friend_parms;
1503 /* If both are templates, check the name of the two
1504 TEMPLATE_DECL's first because is_friend didn't. */
1505 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1506 != DECL_NAME (friend_decl))
1507 return false;
1508
1509 /* Now check template parameter list. */
1510 friend_parms
1511 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1512 args, tf_none);
1513 return comp_template_parms
1514 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1515 friend_parms);
1516 }
1517 else
1518 return (DECL_NAME (decl)
1519 == DECL_NAME (friend_decl));
1520 }
1521 }
1522 return false;
1523 }
1524
1525 /* Register the specialization SPEC as a specialization of TMPL with
1526 the indicated ARGS. IS_FRIEND indicates whether the specialization
1527 is actually just a friend declaration. ATTRLIST is the list of
1528 attributes that the specialization is declared with or NULL when
1529 it isn't. Returns SPEC, or an equivalent prior declaration, if
1530 available.
1531
1532 We also store instantiations of field packs in the hash table, even
1533 though they are not themselves templates, to make lookup easier. */
1534
1535 static tree
1536 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1537 hashval_t hash)
1538 {
1539 tree fn;
1540 spec_entry **slot = NULL;
1541 spec_entry elt;
1542
1543 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1544 || (TREE_CODE (tmpl) == FIELD_DECL
1545 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1546
1547 if (TREE_CODE (spec) == FUNCTION_DECL
1548 && uses_template_parms (DECL_TI_ARGS (spec)))
1549 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1550 register it; we want the corresponding TEMPLATE_DECL instead.
1551 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1552 the more obvious `uses_template_parms (spec)' to avoid problems
1553 with default function arguments. In particular, given
1554 something like this:
1555
1556 template <class T> void f(T t1, T t = T())
1557
1558 the default argument expression is not substituted for in an
1559 instantiation unless and until it is actually needed. */
1560 return spec;
1561
1562 if (optimize_specialization_lookup_p (tmpl))
1563 /* We don't put these specializations in the hash table, but we might
1564 want to give an error about a mismatch. */
1565 fn = retrieve_specialization (tmpl, args, 0);
1566 else
1567 {
1568 elt.tmpl = tmpl;
1569 elt.args = args;
1570 elt.spec = spec;
1571
1572 if (hash == 0)
1573 hash = spec_hasher::hash (&elt);
1574
1575 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1576 if (*slot)
1577 fn = (*slot)->spec;
1578 else
1579 fn = NULL_TREE;
1580 }
1581
1582 /* We can sometimes try to re-register a specialization that we've
1583 already got. In particular, regenerate_decl_from_template calls
1584 duplicate_decls which will update the specialization list. But,
1585 we'll still get called again here anyhow. It's more convenient
1586 to simply allow this than to try to prevent it. */
1587 if (fn == spec)
1588 return spec;
1589 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1590 {
1591 if (DECL_TEMPLATE_INSTANTIATION (fn))
1592 {
1593 if (DECL_ODR_USED (fn)
1594 || DECL_EXPLICIT_INSTANTIATION (fn))
1595 {
1596 error ("specialization of %qD after instantiation",
1597 fn);
1598 return error_mark_node;
1599 }
1600 else
1601 {
1602 tree clone;
1603 /* This situation should occur only if the first
1604 specialization is an implicit instantiation, the
1605 second is an explicit specialization, and the
1606 implicit instantiation has not yet been used. That
1607 situation can occur if we have implicitly
1608 instantiated a member function and then specialized
1609 it later.
1610
1611 We can also wind up here if a friend declaration that
1612 looked like an instantiation turns out to be a
1613 specialization:
1614
1615 template <class T> void foo(T);
1616 class S { friend void foo<>(int) };
1617 template <> void foo(int);
1618
1619 We transform the existing DECL in place so that any
1620 pointers to it become pointers to the updated
1621 declaration.
1622
1623 If there was a definition for the template, but not
1624 for the specialization, we want this to look as if
1625 there were no definition, and vice versa. */
1626 DECL_INITIAL (fn) = NULL_TREE;
1627 duplicate_decls (spec, fn, is_friend);
1628 /* The call to duplicate_decls will have applied
1629 [temp.expl.spec]:
1630
1631 An explicit specialization of a function template
1632 is inline only if it is explicitly declared to be,
1633 and independently of whether its function template
1634 is.
1635
1636 to the primary function; now copy the inline bits to
1637 the various clones. */
1638 FOR_EACH_CLONE (clone, fn)
1639 {
1640 DECL_DECLARED_INLINE_P (clone)
1641 = DECL_DECLARED_INLINE_P (fn);
1642 DECL_SOURCE_LOCATION (clone)
1643 = DECL_SOURCE_LOCATION (fn);
1644 DECL_DELETED_FN (clone)
1645 = DECL_DELETED_FN (fn);
1646 }
1647 check_specialization_namespace (tmpl);
1648
1649 return fn;
1650 }
1651 }
1652 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1653 {
1654 tree dd = duplicate_decls (spec, fn, is_friend);
1655 if (dd == error_mark_node)
1656 /* We've already complained in duplicate_decls. */
1657 return error_mark_node;
1658
1659 if (dd == NULL_TREE && DECL_INITIAL (spec))
1660 /* Dup decl failed, but this is a new definition. Set the
1661 line number so any errors match this new
1662 definition. */
1663 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1664
1665 return fn;
1666 }
1667 }
1668 else if (fn)
1669 return duplicate_decls (spec, fn, is_friend);
1670
1671 /* A specialization must be declared in the same namespace as the
1672 template it is specializing. */
1673 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1674 && !check_specialization_namespace (tmpl))
1675 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1676
1677 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1678 {
1679 spec_entry *entry = ggc_alloc<spec_entry> ();
1680 gcc_assert (tmpl && args && spec);
1681 *entry = elt;
1682 *slot = entry;
1683 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1684 && PRIMARY_TEMPLATE_P (tmpl)
1685 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1686 || variable_template_p (tmpl))
1687 /* If TMPL is a forward declaration of a template function, keep a list
1688 of all specializations in case we need to reassign them to a friend
1689 template later in tsubst_friend_function.
1690
1691 Also keep a list of all variable template instantiations so that
1692 process_partial_specialization can check whether a later partial
1693 specialization would have used it. */
1694 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1695 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1696 }
1697
1698 return spec;
1699 }
1700
1701 /* Returns true iff two spec_entry nodes are equivalent. */
1702
1703 int comparing_specializations;
1704
1705 bool
1706 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1707 {
1708 int equal;
1709
1710 ++comparing_specializations;
1711 equal = (e1->tmpl == e2->tmpl
1712 && comp_template_args (e1->args, e2->args));
1713 if (equal && flag_concepts
1714 /* tmpl could be a FIELD_DECL for a capture pack. */
1715 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1716 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1717 && uses_template_parms (e1->args))
1718 {
1719 /* Partial specializations of a variable template can be distinguished by
1720 constraints. */
1721 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1722 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1723 equal = equivalent_constraints (c1, c2);
1724 }
1725 --comparing_specializations;
1726
1727 return equal;
1728 }
1729
1730 /* Returns a hash for a template TMPL and template arguments ARGS. */
1731
1732 static hashval_t
1733 hash_tmpl_and_args (tree tmpl, tree args)
1734 {
1735 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1736 return iterative_hash_template_arg (args, val);
1737 }
1738
1739 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1740 ignoring SPEC. */
1741
1742 hashval_t
1743 spec_hasher::hash (spec_entry *e)
1744 {
1745 return hash_tmpl_and_args (e->tmpl, e->args);
1746 }
1747
1748 /* Recursively calculate a hash value for a template argument ARG, for use
1749 in the hash tables of template specializations. We must be
1750 careful to (at least) skip the same entities template_args_equal
1751 does. */
1752
1753 hashval_t
1754 iterative_hash_template_arg (tree arg, hashval_t val)
1755 {
1756 if (arg == NULL_TREE)
1757 return iterative_hash_object (arg, val);
1758
1759 if (!TYPE_P (arg))
1760 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1761 while (CONVERT_EXPR_P (arg)
1762 || TREE_CODE (arg) == NON_LVALUE_EXPR
1763 || class_nttp_const_wrapper_p (arg))
1764 arg = TREE_OPERAND (arg, 0);
1765
1766 enum tree_code code = TREE_CODE (arg);
1767
1768 val = iterative_hash_object (code, val);
1769
1770 switch (code)
1771 {
1772 case ARGUMENT_PACK_SELECT:
1773 gcc_unreachable ();
1774
1775 case ERROR_MARK:
1776 return val;
1777
1778 case IDENTIFIER_NODE:
1779 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1780
1781 case TREE_VEC:
1782 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1783 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1784 return val;
1785
1786 case TYPE_PACK_EXPANSION:
1787 case EXPR_PACK_EXPANSION:
1788 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1789 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1790
1791 case TYPE_ARGUMENT_PACK:
1792 case NONTYPE_ARGUMENT_PACK:
1793 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1794
1795 case TREE_LIST:
1796 for (; arg; arg = TREE_CHAIN (arg))
1797 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1798 return val;
1799
1800 case OVERLOAD:
1801 for (lkp_iterator iter (arg); iter; ++iter)
1802 val = iterative_hash_template_arg (*iter, val);
1803 return val;
1804
1805 case CONSTRUCTOR:
1806 {
1807 tree field, value;
1808 unsigned i;
1809 iterative_hash_template_arg (TREE_TYPE (arg), val);
1810 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1811 {
1812 val = iterative_hash_template_arg (field, val);
1813 val = iterative_hash_template_arg (value, val);
1814 }
1815 return val;
1816 }
1817
1818 case PARM_DECL:
1819 if (!DECL_ARTIFICIAL (arg))
1820 {
1821 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1822 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1823 }
1824 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1825
1826 case TARGET_EXPR:
1827 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1828
1829 case PTRMEM_CST:
1830 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1831 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1832
1833 case TEMPLATE_PARM_INDEX:
1834 val = iterative_hash_template_arg
1835 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1836 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1837 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1838
1839 case TRAIT_EXPR:
1840 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1841 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1842 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1843
1844 case BASELINK:
1845 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1846 val);
1847 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1848 val);
1849
1850 case MODOP_EXPR:
1851 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1852 code = TREE_CODE (TREE_OPERAND (arg, 1));
1853 val = iterative_hash_object (code, val);
1854 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1855
1856 case LAMBDA_EXPR:
1857 /* [temp.over.link] Two lambda-expressions are never considered
1858 equivalent.
1859
1860 So just hash the closure type. */
1861 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1862
1863 case CAST_EXPR:
1864 case IMPLICIT_CONV_EXPR:
1865 case STATIC_CAST_EXPR:
1866 case REINTERPRET_CAST_EXPR:
1867 case CONST_CAST_EXPR:
1868 case DYNAMIC_CAST_EXPR:
1869 case NEW_EXPR:
1870 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1871 /* Now hash operands as usual. */
1872 break;
1873
1874 case CALL_EXPR:
1875 {
1876 tree fn = CALL_EXPR_FN (arg);
1877 if (tree name = dependent_name (fn))
1878 {
1879 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1880 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1881 fn = name;
1882 }
1883 val = iterative_hash_template_arg (fn, val);
1884 call_expr_arg_iterator ai;
1885 for (tree x = first_call_expr_arg (arg, &ai); x;
1886 x = next_call_expr_arg (&ai))
1887 val = iterative_hash_template_arg (x, val);
1888 return val;
1889 }
1890
1891 default:
1892 break;
1893 }
1894
1895 char tclass = TREE_CODE_CLASS (code);
1896 switch (tclass)
1897 {
1898 case tcc_type:
1899 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1900 {
1901 // We want an alias specialization that survived strip_typedefs
1902 // to hash differently from its TYPE_CANONICAL, to avoid hash
1903 // collisions that compare as different in template_args_equal.
1904 // These could be dependent specializations that strip_typedefs
1905 // left alone, or untouched specializations because
1906 // coerce_template_parms returns the unconverted template
1907 // arguments if it sees incomplete argument packs.
1908 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1909 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1910 }
1911
1912 switch (TREE_CODE (arg))
1913 {
1914 case TEMPLATE_TEMPLATE_PARM:
1915 {
1916 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1917
1918 /* Do not recurse with TPI directly, as that is unbounded
1919 recursion. */
1920 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1921 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1922 }
1923 break;
1924
1925 case DECLTYPE_TYPE:
1926 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1927 break;
1928
1929 default:
1930 if (tree canonical = TYPE_CANONICAL (arg))
1931 val = iterative_hash_object (TYPE_HASH (canonical), val);
1932 break;
1933 }
1934
1935 return val;
1936
1937 case tcc_declaration:
1938 case tcc_constant:
1939 return iterative_hash_expr (arg, val);
1940
1941 default:
1942 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1943 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1944 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1945 return val;
1946 }
1947
1948 gcc_unreachable ();
1949 return 0;
1950 }
1951
1952 /* Unregister the specialization SPEC as a specialization of TMPL.
1953 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1954 if the SPEC was listed as a specialization of TMPL.
1955
1956 Note that SPEC has been ggc_freed, so we can't look inside it. */
1957
1958 bool
1959 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1960 {
1961 spec_entry *entry;
1962 spec_entry elt;
1963
1964 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1965 elt.args = TI_ARGS (tinfo);
1966 elt.spec = NULL_TREE;
1967
1968 entry = decl_specializations->find (&elt);
1969 if (entry != NULL)
1970 {
1971 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1972 gcc_assert (new_spec != NULL_TREE);
1973 entry->spec = new_spec;
1974 return 1;
1975 }
1976
1977 return 0;
1978 }
1979
1980 /* Like register_specialization, but for local declarations. We are
1981 registering SPEC, an instantiation of TMPL. */
1982
1983 void
1984 register_local_specialization (tree spec, tree tmpl)
1985 {
1986 gcc_assert (tmpl != spec);
1987 local_specializations->put (tmpl, spec);
1988 }
1989
1990 /* TYPE is a class type. Returns true if TYPE is an explicitly
1991 specialized class. */
1992
1993 bool
1994 explicit_class_specialization_p (tree type)
1995 {
1996 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1997 return false;
1998 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1999 }
2000
2001 /* Print the list of functions at FNS, going through all the overloads
2002 for each element of the list. Alternatively, FNS cannot be a
2003 TREE_LIST, in which case it will be printed together with all the
2004 overloads.
2005
2006 MORE and *STR should respectively be FALSE and NULL when the function
2007 is called from the outside. They are used internally on recursive
2008 calls. print_candidates manages the two parameters and leaves NULL
2009 in *STR when it ends. */
2010
2011 static void
2012 print_candidates_1 (tree fns, char **str, bool more = false)
2013 {
2014 if (TREE_CODE (fns) == TREE_LIST)
2015 for (; fns; fns = TREE_CHAIN (fns))
2016 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2017 else
2018 for (lkp_iterator iter (fns); iter;)
2019 {
2020 tree cand = *iter;
2021 ++iter;
2022
2023 const char *pfx = *str;
2024 if (!pfx)
2025 {
2026 if (more || iter)
2027 pfx = _("candidates are:");
2028 else
2029 pfx = _("candidate is:");
2030 *str = get_spaces (pfx);
2031 }
2032 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2033 }
2034 }
2035
2036 /* Print the list of candidate FNS in an error message. FNS can also
2037 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2038
2039 void
2040 print_candidates (tree fns)
2041 {
2042 char *str = NULL;
2043 print_candidates_1 (fns, &str);
2044 free (str);
2045 }
2046
2047 /* Get a (possibly) constrained template declaration for the
2048 purpose of ordering candidates. */
2049 static tree
2050 get_template_for_ordering (tree list)
2051 {
2052 gcc_assert (TREE_CODE (list) == TREE_LIST);
2053 tree f = TREE_VALUE (list);
2054 if (tree ti = DECL_TEMPLATE_INFO (f))
2055 return TI_TEMPLATE (ti);
2056 return f;
2057 }
2058
2059 /* Among candidates having the same signature, return the
2060 most constrained or NULL_TREE if there is no best candidate.
2061 If the signatures of candidates vary (e.g., template
2062 specialization vs. member function), then there can be no
2063 most constrained.
2064
2065 Note that we don't compare constraints on the functions
2066 themselves, but rather those of their templates. */
2067 static tree
2068 most_constrained_function (tree candidates)
2069 {
2070 // Try to find the best candidate in a first pass.
2071 tree champ = candidates;
2072 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2073 {
2074 int winner = more_constrained (get_template_for_ordering (champ),
2075 get_template_for_ordering (c));
2076 if (winner == -1)
2077 champ = c; // The candidate is more constrained
2078 else if (winner == 0)
2079 return NULL_TREE; // Neither is more constrained
2080 }
2081
2082 // Verify that the champ is better than previous candidates.
2083 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2084 if (!more_constrained (get_template_for_ordering (champ),
2085 get_template_for_ordering (c)))
2086 return NULL_TREE;
2087 }
2088
2089 return champ;
2090 }
2091
2092
2093 /* Returns the template (one of the functions given by TEMPLATE_ID)
2094 which can be specialized to match the indicated DECL with the
2095 explicit template args given in TEMPLATE_ID. The DECL may be
2096 NULL_TREE if none is available. In that case, the functions in
2097 TEMPLATE_ID are non-members.
2098
2099 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2100 specialization of a member template.
2101
2102 The TEMPLATE_COUNT is the number of references to qualifying
2103 template classes that appeared in the name of the function. See
2104 check_explicit_specialization for a more accurate description.
2105
2106 TSK indicates what kind of template declaration (if any) is being
2107 declared. TSK_TEMPLATE indicates that the declaration given by
2108 DECL, though a FUNCTION_DECL, has template parameters, and is
2109 therefore a template function.
2110
2111 The template args (those explicitly specified and those deduced)
2112 are output in a newly created vector *TARGS_OUT.
2113
2114 If it is impossible to determine the result, an error message is
2115 issued. The error_mark_node is returned to indicate failure. */
2116
2117 static tree
2118 determine_specialization (tree template_id,
2119 tree decl,
2120 tree* targs_out,
2121 int need_member_template,
2122 int template_count,
2123 tmpl_spec_kind tsk)
2124 {
2125 tree fns;
2126 tree targs;
2127 tree explicit_targs;
2128 tree candidates = NULL_TREE;
2129
2130 /* A TREE_LIST of templates of which DECL may be a specialization.
2131 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2132 corresponding TREE_PURPOSE is the set of template arguments that,
2133 when used to instantiate the template, would produce a function
2134 with the signature of DECL. */
2135 tree templates = NULL_TREE;
2136 int header_count;
2137 cp_binding_level *b;
2138
2139 *targs_out = NULL_TREE;
2140
2141 if (template_id == error_mark_node || decl == error_mark_node)
2142 return error_mark_node;
2143
2144 /* We shouldn't be specializing a member template of an
2145 unspecialized class template; we already gave an error in
2146 check_specialization_scope, now avoid crashing. */
2147 if (!VAR_P (decl)
2148 && template_count && DECL_CLASS_SCOPE_P (decl)
2149 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2150 {
2151 gcc_assert (errorcount);
2152 return error_mark_node;
2153 }
2154
2155 fns = TREE_OPERAND (template_id, 0);
2156 explicit_targs = TREE_OPERAND (template_id, 1);
2157
2158 if (fns == error_mark_node)
2159 return error_mark_node;
2160
2161 /* Check for baselinks. */
2162 if (BASELINK_P (fns))
2163 fns = BASELINK_FUNCTIONS (fns);
2164
2165 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2166 {
2167 error_at (DECL_SOURCE_LOCATION (decl),
2168 "%qD is not a function template", fns);
2169 return error_mark_node;
2170 }
2171 else if (VAR_P (decl) && !variable_template_p (fns))
2172 {
2173 error ("%qD is not a variable template", fns);
2174 return error_mark_node;
2175 }
2176
2177 /* Count the number of template headers specified for this
2178 specialization. */
2179 header_count = 0;
2180 for (b = current_binding_level;
2181 b->kind == sk_template_parms;
2182 b = b->level_chain)
2183 ++header_count;
2184
2185 tree orig_fns = fns;
2186
2187 if (variable_template_p (fns))
2188 {
2189 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2190 targs = coerce_template_parms (parms, explicit_targs, fns,
2191 tf_warning_or_error,
2192 /*req_all*/true, /*use_defarg*/true);
2193 if (targs != error_mark_node)
2194 templates = tree_cons (targs, fns, templates);
2195 }
2196 else for (lkp_iterator iter (fns); iter; ++iter)
2197 {
2198 tree fn = *iter;
2199
2200 if (TREE_CODE (fn) == TEMPLATE_DECL)
2201 {
2202 tree decl_arg_types;
2203 tree fn_arg_types;
2204 tree insttype;
2205
2206 /* In case of explicit specialization, we need to check if
2207 the number of template headers appearing in the specialization
2208 is correct. This is usually done in check_explicit_specialization,
2209 but the check done there cannot be exhaustive when specializing
2210 member functions. Consider the following code:
2211
2212 template <> void A<int>::f(int);
2213 template <> template <> void A<int>::f(int);
2214
2215 Assuming that A<int> is not itself an explicit specialization
2216 already, the first line specializes "f" which is a non-template
2217 member function, whilst the second line specializes "f" which
2218 is a template member function. So both lines are syntactically
2219 correct, and check_explicit_specialization does not reject
2220 them.
2221
2222 Here, we can do better, as we are matching the specialization
2223 against the declarations. We count the number of template
2224 headers, and we check if they match TEMPLATE_COUNT + 1
2225 (TEMPLATE_COUNT is the number of qualifying template classes,
2226 plus there must be another header for the member template
2227 itself).
2228
2229 Notice that if header_count is zero, this is not a
2230 specialization but rather a template instantiation, so there
2231 is no check we can perform here. */
2232 if (header_count && header_count != template_count + 1)
2233 continue;
2234
2235 /* Check that the number of template arguments at the
2236 innermost level for DECL is the same as for FN. */
2237 if (current_binding_level->kind == sk_template_parms
2238 && !current_binding_level->explicit_spec_p
2239 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2240 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2241 (current_template_parms))))
2242 continue;
2243
2244 /* DECL might be a specialization of FN. */
2245 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2246 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2247
2248 /* For a non-static member function, we need to make sure
2249 that the const qualification is the same. Since
2250 get_bindings does not try to merge the "this" parameter,
2251 we must do the comparison explicitly. */
2252 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2253 {
2254 if (!same_type_p (TREE_VALUE (fn_arg_types),
2255 TREE_VALUE (decl_arg_types)))
2256 continue;
2257
2258 /* And the ref-qualification. */
2259 if (type_memfn_rqual (TREE_TYPE (decl))
2260 != type_memfn_rqual (TREE_TYPE (fn)))
2261 continue;
2262 }
2263
2264 /* Skip the "this" parameter and, for constructors of
2265 classes with virtual bases, the VTT parameter. A
2266 full specialization of a constructor will have a VTT
2267 parameter, but a template never will. */
2268 decl_arg_types
2269 = skip_artificial_parms_for (decl, decl_arg_types);
2270 fn_arg_types
2271 = skip_artificial_parms_for (fn, fn_arg_types);
2272
2273 /* Function templates cannot be specializations; there are
2274 no partial specializations of functions. Therefore, if
2275 the type of DECL does not match FN, there is no
2276 match.
2277
2278 Note that it should never be the case that we have both
2279 candidates added here, and for regular member functions
2280 below. */
2281 if (tsk == tsk_template)
2282 {
2283 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2284 current_template_parms))
2285 continue;
2286 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2287 TREE_TYPE (TREE_TYPE (fn))))
2288 continue;
2289 if (!compparms (fn_arg_types, decl_arg_types))
2290 continue;
2291
2292 tree freq = get_trailing_function_requirements (fn);
2293 tree dreq = get_trailing_function_requirements (decl);
2294 if (!freq != !dreq)
2295 continue;
2296 if (freq)
2297 {
2298 tree fargs = DECL_TI_ARGS (fn);
2299 tsubst_flags_t complain = tf_none;
2300 freq = tsubst_constraint (freq, fargs, complain, fn);
2301 if (!cp_tree_equal (freq, dreq))
2302 continue;
2303 }
2304
2305 candidates = tree_cons (NULL_TREE, fn, candidates);
2306 continue;
2307 }
2308
2309 /* See whether this function might be a specialization of this
2310 template. Suppress access control because we might be trying
2311 to make this specialization a friend, and we have already done
2312 access control for the declaration of the specialization. */
2313 push_deferring_access_checks (dk_no_check);
2314 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2315 pop_deferring_access_checks ();
2316
2317 if (!targs)
2318 /* We cannot deduce template arguments that when used to
2319 specialize TMPL will produce DECL. */
2320 continue;
2321
2322 if (uses_template_parms (targs))
2323 /* We deduced something involving 'auto', which isn't a valid
2324 template argument. */
2325 continue;
2326
2327 /* Remove, from the set of candidates, all those functions
2328 whose constraints are not satisfied. */
2329 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2330 continue;
2331
2332 // Then, try to form the new function type.
2333 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2334 if (insttype == error_mark_node)
2335 continue;
2336 fn_arg_types
2337 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2338 if (!compparms (fn_arg_types, decl_arg_types))
2339 continue;
2340
2341 /* Save this template, and the arguments deduced. */
2342 templates = tree_cons (targs, fn, templates);
2343 }
2344 else if (need_member_template)
2345 /* FN is an ordinary member function, and we need a
2346 specialization of a member template. */
2347 ;
2348 else if (TREE_CODE (fn) != FUNCTION_DECL)
2349 /* We can get IDENTIFIER_NODEs here in certain erroneous
2350 cases. */
2351 ;
2352 else if (!DECL_FUNCTION_MEMBER_P (fn))
2353 /* This is just an ordinary non-member function. Nothing can
2354 be a specialization of that. */
2355 ;
2356 else if (DECL_ARTIFICIAL (fn))
2357 /* Cannot specialize functions that are created implicitly. */
2358 ;
2359 else
2360 {
2361 tree decl_arg_types;
2362
2363 /* This is an ordinary member function. However, since
2364 we're here, we can assume its enclosing class is a
2365 template class. For example,
2366
2367 template <typename T> struct S { void f(); };
2368 template <> void S<int>::f() {}
2369
2370 Here, S<int>::f is a non-template, but S<int> is a
2371 template class. If FN has the same type as DECL, we
2372 might be in business. */
2373
2374 if (!DECL_TEMPLATE_INFO (fn))
2375 /* Its enclosing class is an explicit specialization
2376 of a template class. This is not a candidate. */
2377 continue;
2378
2379 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2380 TREE_TYPE (TREE_TYPE (fn))))
2381 /* The return types differ. */
2382 continue;
2383
2384 /* Adjust the type of DECL in case FN is a static member. */
2385 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2386 if (DECL_STATIC_FUNCTION_P (fn)
2387 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2388 decl_arg_types = TREE_CHAIN (decl_arg_types);
2389
2390 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2391 decl_arg_types))
2392 continue;
2393
2394 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2395 && (type_memfn_rqual (TREE_TYPE (decl))
2396 != type_memfn_rqual (TREE_TYPE (fn))))
2397 continue;
2398
2399 // If the deduced arguments do not satisfy the constraints,
2400 // this is not a candidate.
2401 if (flag_concepts && !constraints_satisfied_p (fn))
2402 continue;
2403
2404 // Add the candidate.
2405 candidates = tree_cons (NULL_TREE, fn, candidates);
2406 }
2407 }
2408
2409 if (templates && TREE_CHAIN (templates))
2410 {
2411 /* We have:
2412
2413 [temp.expl.spec]
2414
2415 It is possible for a specialization with a given function
2416 signature to be instantiated from more than one function
2417 template. In such cases, explicit specification of the
2418 template arguments must be used to uniquely identify the
2419 function template specialization being specialized.
2420
2421 Note that here, there's no suggestion that we're supposed to
2422 determine which of the candidate templates is most
2423 specialized. However, we, also have:
2424
2425 [temp.func.order]
2426
2427 Partial ordering of overloaded function template
2428 declarations is used in the following contexts to select
2429 the function template to which a function template
2430 specialization refers:
2431
2432 -- when an explicit specialization refers to a function
2433 template.
2434
2435 So, we do use the partial ordering rules, at least for now.
2436 This extension can only serve to make invalid programs valid,
2437 so it's safe. And, there is strong anecdotal evidence that
2438 the committee intended the partial ordering rules to apply;
2439 the EDG front end has that behavior, and John Spicer claims
2440 that the committee simply forgot to delete the wording in
2441 [temp.expl.spec]. */
2442 tree tmpl = most_specialized_instantiation (templates);
2443 if (tmpl != error_mark_node)
2444 {
2445 templates = tmpl;
2446 TREE_CHAIN (templates) = NULL_TREE;
2447 }
2448 }
2449
2450 // Concepts allows multiple declarations of member functions
2451 // with the same signature. Like above, we need to rely on
2452 // on the partial ordering of those candidates to determine which
2453 // is the best.
2454 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2455 {
2456 if (tree cand = most_constrained_function (candidates))
2457 {
2458 candidates = cand;
2459 TREE_CHAIN (cand) = NULL_TREE;
2460 }
2461 }
2462
2463 if (templates == NULL_TREE && candidates == NULL_TREE)
2464 {
2465 error ("template-id %qD for %q+D does not match any template "
2466 "declaration", template_id, decl);
2467 if (header_count && header_count != template_count + 1)
2468 inform (DECL_SOURCE_LOCATION (decl),
2469 "saw %d %<template<>%>, need %d for "
2470 "specializing a member function template",
2471 header_count, template_count + 1);
2472 else
2473 print_candidates (orig_fns);
2474 return error_mark_node;
2475 }
2476 else if ((templates && TREE_CHAIN (templates))
2477 || (candidates && TREE_CHAIN (candidates))
2478 || (templates && candidates))
2479 {
2480 error ("ambiguous template specialization %qD for %q+D",
2481 template_id, decl);
2482 candidates = chainon (candidates, templates);
2483 print_candidates (candidates);
2484 return error_mark_node;
2485 }
2486
2487 /* We have one, and exactly one, match. */
2488 if (candidates)
2489 {
2490 tree fn = TREE_VALUE (candidates);
2491 *targs_out = copy_node (DECL_TI_ARGS (fn));
2492
2493 /* Propagate the candidate's constraints to the declaration. */
2494 if (tsk != tsk_template)
2495 set_constraints (decl, get_constraints (fn));
2496
2497 /* DECL is a re-declaration or partial instantiation of a template
2498 function. */
2499 if (TREE_CODE (fn) == TEMPLATE_DECL)
2500 return fn;
2501 /* It was a specialization of an ordinary member function in a
2502 template class. */
2503 return DECL_TI_TEMPLATE (fn);
2504 }
2505
2506 /* It was a specialization of a template. */
2507 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2508 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2509 {
2510 *targs_out = copy_node (targs);
2511 SET_TMPL_ARGS_LEVEL (*targs_out,
2512 TMPL_ARGS_DEPTH (*targs_out),
2513 TREE_PURPOSE (templates));
2514 }
2515 else
2516 *targs_out = TREE_PURPOSE (templates);
2517 return TREE_VALUE (templates);
2518 }
2519
2520 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2521 but with the default argument values filled in from those in the
2522 TMPL_TYPES. */
2523
2524 static tree
2525 copy_default_args_to_explicit_spec_1 (tree spec_types,
2526 tree tmpl_types)
2527 {
2528 tree new_spec_types;
2529
2530 if (!spec_types)
2531 return NULL_TREE;
2532
2533 if (spec_types == void_list_node)
2534 return void_list_node;
2535
2536 /* Substitute into the rest of the list. */
2537 new_spec_types =
2538 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2539 TREE_CHAIN (tmpl_types));
2540
2541 /* Add the default argument for this parameter. */
2542 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2543 TREE_VALUE (spec_types),
2544 new_spec_types);
2545 }
2546
2547 /* DECL is an explicit specialization. Replicate default arguments
2548 from the template it specializes. (That way, code like:
2549
2550 template <class T> void f(T = 3);
2551 template <> void f(double);
2552 void g () { f (); }
2553
2554 works, as required.) An alternative approach would be to look up
2555 the correct default arguments at the call-site, but this approach
2556 is consistent with how implicit instantiations are handled. */
2557
2558 static void
2559 copy_default_args_to_explicit_spec (tree decl)
2560 {
2561 tree tmpl;
2562 tree spec_types;
2563 tree tmpl_types;
2564 tree new_spec_types;
2565 tree old_type;
2566 tree new_type;
2567 tree t;
2568 tree object_type = NULL_TREE;
2569 tree in_charge = NULL_TREE;
2570 tree vtt = NULL_TREE;
2571
2572 /* See if there's anything we need to do. */
2573 tmpl = DECL_TI_TEMPLATE (decl);
2574 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2575 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2576 if (TREE_PURPOSE (t))
2577 break;
2578 if (!t)
2579 return;
2580
2581 old_type = TREE_TYPE (decl);
2582 spec_types = TYPE_ARG_TYPES (old_type);
2583
2584 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2585 {
2586 /* Remove the this pointer, but remember the object's type for
2587 CV quals. */
2588 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2589 spec_types = TREE_CHAIN (spec_types);
2590 tmpl_types = TREE_CHAIN (tmpl_types);
2591
2592 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2593 {
2594 /* DECL may contain more parameters than TMPL due to the extra
2595 in-charge parameter in constructors and destructors. */
2596 in_charge = spec_types;
2597 spec_types = TREE_CHAIN (spec_types);
2598 }
2599 if (DECL_HAS_VTT_PARM_P (decl))
2600 {
2601 vtt = spec_types;
2602 spec_types = TREE_CHAIN (spec_types);
2603 }
2604 }
2605
2606 /* Compute the merged default arguments. */
2607 new_spec_types =
2608 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2609
2610 /* Compute the new FUNCTION_TYPE. */
2611 if (object_type)
2612 {
2613 if (vtt)
2614 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2615 TREE_VALUE (vtt),
2616 new_spec_types);
2617
2618 if (in_charge)
2619 /* Put the in-charge parameter back. */
2620 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2621 TREE_VALUE (in_charge),
2622 new_spec_types);
2623
2624 new_type = build_method_type_directly (object_type,
2625 TREE_TYPE (old_type),
2626 new_spec_types);
2627 }
2628 else
2629 new_type = build_function_type (TREE_TYPE (old_type),
2630 new_spec_types);
2631 new_type = cp_build_type_attribute_variant (new_type,
2632 TYPE_ATTRIBUTES (old_type));
2633 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2634
2635 TREE_TYPE (decl) = new_type;
2636 }
2637
2638 /* Return the number of template headers we expect to see for a definition
2639 or specialization of CTYPE or one of its non-template members. */
2640
2641 int
2642 num_template_headers_for_class (tree ctype)
2643 {
2644 int num_templates = 0;
2645
2646 while (ctype && CLASS_TYPE_P (ctype))
2647 {
2648 /* You're supposed to have one `template <...>' for every
2649 template class, but you don't need one for a full
2650 specialization. For example:
2651
2652 template <class T> struct S{};
2653 template <> struct S<int> { void f(); };
2654 void S<int>::f () {}
2655
2656 is correct; there shouldn't be a `template <>' for the
2657 definition of `S<int>::f'. */
2658 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2659 /* If CTYPE does not have template information of any
2660 kind, then it is not a template, nor is it nested
2661 within a template. */
2662 break;
2663 if (explicit_class_specialization_p (ctype))
2664 break;
2665 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2666 ++num_templates;
2667
2668 ctype = TYPE_CONTEXT (ctype);
2669 }
2670
2671 return num_templates;
2672 }
2673
2674 /* Do a simple sanity check on the template headers that precede the
2675 variable declaration DECL. */
2676
2677 void
2678 check_template_variable (tree decl)
2679 {
2680 tree ctx = CP_DECL_CONTEXT (decl);
2681 int wanted = num_template_headers_for_class (ctx);
2682 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2683 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2684 {
2685 if (cxx_dialect < cxx14)
2686 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2687 "variable templates only available with "
2688 "%<-std=c++14%> or %<-std=gnu++14%>");
2689
2690 // Namespace-scope variable templates should have a template header.
2691 ++wanted;
2692 }
2693 if (template_header_count > wanted)
2694 {
2695 auto_diagnostic_group d;
2696 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2697 "too many template headers for %qD "
2698 "(should be %d)",
2699 decl, wanted);
2700 if (warned && CLASS_TYPE_P (ctx)
2701 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2702 inform (DECL_SOURCE_LOCATION (decl),
2703 "members of an explicitly specialized class are defined "
2704 "without a template header");
2705 }
2706 }
2707
2708 /* An explicit specialization whose declarator-id or class-head-name is not
2709 qualified shall be declared in the nearest enclosing namespace of the
2710 template, or, if the namespace is inline (7.3.1), any namespace from its
2711 enclosing namespace set.
2712
2713 If the name declared in the explicit instantiation is an unqualified name,
2714 the explicit instantiation shall appear in the namespace where its template
2715 is declared or, if that namespace is inline (7.3.1), any namespace from its
2716 enclosing namespace set. */
2717
2718 void
2719 check_unqualified_spec_or_inst (tree t, location_t loc)
2720 {
2721 tree tmpl = most_general_template (t);
2722 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2723 && !is_nested_namespace (current_namespace,
2724 CP_DECL_CONTEXT (tmpl), true))
2725 {
2726 if (processing_specialization)
2727 permerror (loc, "explicit specialization of %qD outside its "
2728 "namespace must use a nested-name-specifier", tmpl);
2729 else if (processing_explicit_instantiation
2730 && cxx_dialect >= cxx11)
2731 /* This was allowed in C++98, so only pedwarn. */
2732 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2733 "outside its namespace must use a nested-name-"
2734 "specifier", tmpl);
2735 }
2736 }
2737
2738 /* Warn for a template specialization SPEC that is missing some of a set
2739 of function or type attributes that the template TEMPL is declared with.
2740 ATTRLIST is a list of additional attributes that SPEC should be taken
2741 to ultimately be declared with. */
2742
2743 static void
2744 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2745 {
2746 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2747 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2748
2749 /* Avoid warning if the difference between the primary and
2750 the specialization is not in one of the attributes below. */
2751 const char* const blacklist[] = {
2752 "alloc_align", "alloc_size", "assume_aligned", "format",
2753 "format_arg", "malloc", "nonnull", NULL
2754 };
2755
2756 /* Put together a list of the black listed attributes that the primary
2757 template is declared with that the specialization is not, in case
2758 it's not apparent from the most recent declaration of the primary. */
2759 pretty_printer str;
2760 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2761 blacklist, &str);
2762
2763 if (!nattrs)
2764 return;
2765
2766 auto_diagnostic_group d;
2767 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2768 "explicit specialization %q#D may be missing attributes",
2769 spec))
2770 inform (DECL_SOURCE_LOCATION (tmpl),
2771 nattrs > 1
2772 ? G_("missing primary template attributes %s")
2773 : G_("missing primary template attribute %s"),
2774 pp_formatted_text (&str));
2775 }
2776
2777 /* Check to see if the function just declared, as indicated in
2778 DECLARATOR, and in DECL, is a specialization of a function
2779 template. We may also discover that the declaration is an explicit
2780 instantiation at this point.
2781
2782 Returns DECL, or an equivalent declaration that should be used
2783 instead if all goes well. Issues an error message if something is
2784 amiss. Returns error_mark_node if the error is not easily
2785 recoverable.
2786
2787 FLAGS is a bitmask consisting of the following flags:
2788
2789 2: The function has a definition.
2790 4: The function is a friend.
2791
2792 The TEMPLATE_COUNT is the number of references to qualifying
2793 template classes that appeared in the name of the function. For
2794 example, in
2795
2796 template <class T> struct S { void f(); };
2797 void S<int>::f();
2798
2799 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2800 classes are not counted in the TEMPLATE_COUNT, so that in
2801
2802 template <class T> struct S {};
2803 template <> struct S<int> { void f(); }
2804 template <> void S<int>::f();
2805
2806 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2807 invalid; there should be no template <>.)
2808
2809 If the function is a specialization, it is marked as such via
2810 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2811 is set up correctly, and it is added to the list of specializations
2812 for that template. */
2813
2814 tree
2815 check_explicit_specialization (tree declarator,
2816 tree decl,
2817 int template_count,
2818 int flags,
2819 tree attrlist)
2820 {
2821 int have_def = flags & 2;
2822 int is_friend = flags & 4;
2823 bool is_concept = flags & 8;
2824 int specialization = 0;
2825 int explicit_instantiation = 0;
2826 int member_specialization = 0;
2827 tree ctype = DECL_CLASS_CONTEXT (decl);
2828 tree dname = DECL_NAME (decl);
2829 tmpl_spec_kind tsk;
2830
2831 if (is_friend)
2832 {
2833 if (!processing_specialization)
2834 tsk = tsk_none;
2835 else
2836 tsk = tsk_excessive_parms;
2837 }
2838 else
2839 tsk = current_tmpl_spec_kind (template_count);
2840
2841 switch (tsk)
2842 {
2843 case tsk_none:
2844 if (processing_specialization && !VAR_P (decl))
2845 {
2846 specialization = 1;
2847 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2848 }
2849 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2850 {
2851 if (is_friend)
2852 /* This could be something like:
2853
2854 template <class T> void f(T);
2855 class S { friend void f<>(int); } */
2856 specialization = 1;
2857 else
2858 {
2859 /* This case handles bogus declarations like template <>
2860 template <class T> void f<int>(); */
2861
2862 error_at (cp_expr_loc_or_input_loc (declarator),
2863 "template-id %qE in declaration of primary template",
2864 declarator);
2865 return decl;
2866 }
2867 }
2868 break;
2869
2870 case tsk_invalid_member_spec:
2871 /* The error has already been reported in
2872 check_specialization_scope. */
2873 return error_mark_node;
2874
2875 case tsk_invalid_expl_inst:
2876 error ("template parameter list used in explicit instantiation");
2877
2878 /* Fall through. */
2879
2880 case tsk_expl_inst:
2881 if (have_def)
2882 error ("definition provided for explicit instantiation");
2883
2884 explicit_instantiation = 1;
2885 break;
2886
2887 case tsk_excessive_parms:
2888 case tsk_insufficient_parms:
2889 if (tsk == tsk_excessive_parms)
2890 error ("too many template parameter lists in declaration of %qD",
2891 decl);
2892 else if (template_header_count)
2893 error("too few template parameter lists in declaration of %qD", decl);
2894 else
2895 error("explicit specialization of %qD must be introduced by "
2896 "%<template <>%>", decl);
2897
2898 /* Fall through. */
2899 case tsk_expl_spec:
2900 if (is_concept)
2901 error ("explicit specialization declared %<concept%>");
2902
2903 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2904 /* In cases like template<> constexpr bool v = true;
2905 We'll give an error in check_template_variable. */
2906 break;
2907
2908 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2909 if (ctype)
2910 member_specialization = 1;
2911 else
2912 specialization = 1;
2913 break;
2914
2915 case tsk_template:
2916 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2917 {
2918 /* This case handles bogus declarations like template <>
2919 template <class T> void f<int>(); */
2920
2921 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2922 error_at (cp_expr_loc_or_input_loc (declarator),
2923 "template-id %qE in declaration of primary template",
2924 declarator);
2925 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2926 {
2927 /* Partial specialization of variable template. */
2928 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2929 specialization = 1;
2930 goto ok;
2931 }
2932 else if (cxx_dialect < cxx14)
2933 error_at (cp_expr_loc_or_input_loc (declarator),
2934 "non-type partial specialization %qE "
2935 "is not allowed", declarator);
2936 else
2937 error_at (cp_expr_loc_or_input_loc (declarator),
2938 "non-class, non-variable partial specialization %qE "
2939 "is not allowed", declarator);
2940 return decl;
2941 ok:;
2942 }
2943
2944 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2945 /* This is a specialization of a member template, without
2946 specialization the containing class. Something like:
2947
2948 template <class T> struct S {
2949 template <class U> void f (U);
2950 };
2951 template <> template <class U> void S<int>::f(U) {}
2952
2953 That's a specialization -- but of the entire template. */
2954 specialization = 1;
2955 break;
2956
2957 default:
2958 gcc_unreachable ();
2959 }
2960
2961 if ((specialization || member_specialization)
2962 /* This doesn't apply to variable templates. */
2963 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2964 {
2965 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2966 for (; t; t = TREE_CHAIN (t))
2967 if (TREE_PURPOSE (t))
2968 {
2969 permerror (input_location,
2970 "default argument specified in explicit specialization");
2971 break;
2972 }
2973 }
2974
2975 if (specialization || member_specialization || explicit_instantiation)
2976 {
2977 tree tmpl = NULL_TREE;
2978 tree targs = NULL_TREE;
2979 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2980
2981 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2982 if (!was_template_id)
2983 {
2984 tree fns;
2985
2986 gcc_assert (identifier_p (declarator));
2987 if (ctype)
2988 fns = dname;
2989 else
2990 {
2991 /* If there is no class context, the explicit instantiation
2992 must be at namespace scope. */
2993 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2994
2995 /* Find the namespace binding, using the declaration
2996 context. */
2997 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2998 false, true);
2999 if (fns == error_mark_node)
3000 /* If lookup fails, look for a friend declaration so we can
3001 give a better diagnostic. */
3002 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3003 /*type*/false, /*complain*/true,
3004 /*hidden*/true);
3005
3006 if (fns == error_mark_node || !is_overloaded_fn (fns))
3007 {
3008 error ("%qD is not a template function", dname);
3009 fns = error_mark_node;
3010 }
3011 }
3012
3013 declarator = lookup_template_function (fns, NULL_TREE);
3014 }
3015
3016 if (declarator == error_mark_node)
3017 return error_mark_node;
3018
3019 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3020 {
3021 if (!explicit_instantiation)
3022 /* A specialization in class scope. This is invalid,
3023 but the error will already have been flagged by
3024 check_specialization_scope. */
3025 return error_mark_node;
3026 else
3027 {
3028 /* It's not valid to write an explicit instantiation in
3029 class scope, e.g.:
3030
3031 class C { template void f(); }
3032
3033 This case is caught by the parser. However, on
3034 something like:
3035
3036 template class C { void f(); };
3037
3038 (which is invalid) we can get here. The error will be
3039 issued later. */
3040 ;
3041 }
3042
3043 return decl;
3044 }
3045 else if (ctype != NULL_TREE
3046 && (identifier_p (TREE_OPERAND (declarator, 0))))
3047 {
3048 // We'll match variable templates in start_decl.
3049 if (VAR_P (decl))
3050 return decl;
3051
3052 /* Find the list of functions in ctype that have the same
3053 name as the declared function. */
3054 tree name = TREE_OPERAND (declarator, 0);
3055
3056 if (constructor_name_p (name, ctype))
3057 {
3058 if (DECL_CONSTRUCTOR_P (decl)
3059 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3060 : !CLASSTYPE_DESTRUCTOR (ctype))
3061 {
3062 /* From [temp.expl.spec]:
3063
3064 If such an explicit specialization for the member
3065 of a class template names an implicitly-declared
3066 special member function (clause _special_), the
3067 program is ill-formed.
3068
3069 Similar language is found in [temp.explicit]. */
3070 error ("specialization of implicitly-declared special member function");
3071 return error_mark_node;
3072 }
3073
3074 name = DECL_NAME (decl);
3075 }
3076
3077 /* For a type-conversion operator, We might be looking for
3078 `operator int' which will be a specialization of
3079 `operator T'. Grab all the conversion operators, and
3080 then select from them. */
3081 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3082 ? conv_op_identifier : name);
3083
3084 if (fns == NULL_TREE)
3085 {
3086 error ("no member function %qD declared in %qT", name, ctype);
3087 return error_mark_node;
3088 }
3089 else
3090 TREE_OPERAND (declarator, 0) = fns;
3091 }
3092
3093 /* Figure out what exactly is being specialized at this point.
3094 Note that for an explicit instantiation, even one for a
3095 member function, we cannot tell a priori whether the
3096 instantiation is for a member template, or just a member
3097 function of a template class. Even if a member template is
3098 being instantiated, the member template arguments may be
3099 elided if they can be deduced from the rest of the
3100 declaration. */
3101 tmpl = determine_specialization (declarator, decl,
3102 &targs,
3103 member_specialization,
3104 template_count,
3105 tsk);
3106
3107 if (!tmpl || tmpl == error_mark_node)
3108 /* We couldn't figure out what this declaration was
3109 specializing. */
3110 return error_mark_node;
3111 else
3112 {
3113 if (TREE_CODE (decl) == FUNCTION_DECL
3114 && DECL_HIDDEN_FRIEND_P (tmpl))
3115 {
3116 auto_diagnostic_group d;
3117 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3118 "friend declaration %qD is not visible to "
3119 "explicit specialization", tmpl))
3120 inform (DECL_SOURCE_LOCATION (tmpl),
3121 "friend declaration here");
3122 }
3123 else if (!ctype && !is_friend
3124 && CP_DECL_CONTEXT (decl) == current_namespace)
3125 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3126
3127 tree gen_tmpl = most_general_template (tmpl);
3128
3129 if (explicit_instantiation)
3130 {
3131 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3132 is done by do_decl_instantiation later. */
3133
3134 int arg_depth = TMPL_ARGS_DEPTH (targs);
3135 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3136
3137 if (arg_depth > parm_depth)
3138 {
3139 /* If TMPL is not the most general template (for
3140 example, if TMPL is a friend template that is
3141 injected into namespace scope), then there will
3142 be too many levels of TARGS. Remove some of them
3143 here. */
3144 int i;
3145 tree new_targs;
3146
3147 new_targs = make_tree_vec (parm_depth);
3148 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3149 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3150 = TREE_VEC_ELT (targs, i);
3151 targs = new_targs;
3152 }
3153
3154 return instantiate_template (tmpl, targs, tf_error);
3155 }
3156
3157 /* If we thought that the DECL was a member function, but it
3158 turns out to be specializing a static member function,
3159 make DECL a static member function as well. */
3160 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3161 && DECL_STATIC_FUNCTION_P (tmpl)
3162 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3163 revert_static_member_fn (decl);
3164
3165 /* If this is a specialization of a member template of a
3166 template class, we want to return the TEMPLATE_DECL, not
3167 the specialization of it. */
3168 if (tsk == tsk_template && !was_template_id)
3169 {
3170 tree result = DECL_TEMPLATE_RESULT (tmpl);
3171 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3172 DECL_INITIAL (result) = NULL_TREE;
3173 if (have_def)
3174 {
3175 tree parm;
3176 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3177 DECL_SOURCE_LOCATION (result)
3178 = DECL_SOURCE_LOCATION (decl);
3179 /* We want to use the argument list specified in the
3180 definition, not in the original declaration. */
3181 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3182 for (parm = DECL_ARGUMENTS (result); parm;
3183 parm = DECL_CHAIN (parm))
3184 DECL_CONTEXT (parm) = result;
3185 }
3186 return register_specialization (tmpl, gen_tmpl, targs,
3187 is_friend, 0);
3188 }
3189
3190 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3191 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3192
3193 if (was_template_id)
3194 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3195
3196 /* Inherit default function arguments from the template
3197 DECL is specializing. */
3198 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3199 copy_default_args_to_explicit_spec (decl);
3200
3201 /* This specialization has the same protection as the
3202 template it specializes. */
3203 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3204 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3205
3206 /* 7.1.1-1 [dcl.stc]
3207
3208 A storage-class-specifier shall not be specified in an
3209 explicit specialization...
3210
3211 The parser rejects these, so unless action is taken here,
3212 explicit function specializations will always appear with
3213 global linkage.
3214
3215 The action recommended by the C++ CWG in response to C++
3216 defect report 605 is to make the storage class and linkage
3217 of the explicit specialization match the templated function:
3218
3219 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3220 */
3221 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3222 {
3223 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3224 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3225
3226 /* A concept cannot be specialized. */
3227 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3228 {
3229 error ("explicit specialization of function concept %qD",
3230 gen_tmpl);
3231 return error_mark_node;
3232 }
3233
3234 /* This specialization has the same linkage and visibility as
3235 the function template it specializes. */
3236 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3237 if (! TREE_PUBLIC (decl))
3238 {
3239 DECL_INTERFACE_KNOWN (decl) = 1;
3240 DECL_NOT_REALLY_EXTERN (decl) = 1;
3241 }
3242 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3243 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3244 {
3245 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3246 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3247 }
3248 }
3249
3250 /* If DECL is a friend declaration, declared using an
3251 unqualified name, the namespace associated with DECL may
3252 have been set incorrectly. For example, in:
3253
3254 template <typename T> void f(T);
3255 namespace N {
3256 struct S { friend void f<int>(int); }
3257 }
3258
3259 we will have set the DECL_CONTEXT for the friend
3260 declaration to N, rather than to the global namespace. */
3261 if (DECL_NAMESPACE_SCOPE_P (decl))
3262 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3263
3264 if (is_friend && !have_def)
3265 /* This is not really a declaration of a specialization.
3266 It's just the name of an instantiation. But, it's not
3267 a request for an instantiation, either. */
3268 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3269 else if (TREE_CODE (decl) == FUNCTION_DECL)
3270 /* A specialization is not necessarily COMDAT. */
3271 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3272 && DECL_DECLARED_INLINE_P (decl));
3273 else if (VAR_P (decl))
3274 DECL_COMDAT (decl) = false;
3275
3276 /* If this is a full specialization, register it so that we can find
3277 it again. Partial specializations will be registered in
3278 process_partial_specialization. */
3279 if (!processing_template_decl)
3280 {
3281 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3282
3283 decl = register_specialization (decl, gen_tmpl, targs,
3284 is_friend, 0);
3285 }
3286
3287
3288 /* A 'structor should already have clones. */
3289 gcc_assert (decl == error_mark_node
3290 || variable_template_p (tmpl)
3291 || !(DECL_CONSTRUCTOR_P (decl)
3292 || DECL_DESTRUCTOR_P (decl))
3293 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3294 }
3295 }
3296
3297 return decl;
3298 }
3299
3300 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3301 parameters. These are represented in the same format used for
3302 DECL_TEMPLATE_PARMS. */
3303
3304 int
3305 comp_template_parms (const_tree parms1, const_tree parms2)
3306 {
3307 const_tree p1;
3308 const_tree p2;
3309
3310 if (parms1 == parms2)
3311 return 1;
3312
3313 for (p1 = parms1, p2 = parms2;
3314 p1 != NULL_TREE && p2 != NULL_TREE;
3315 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3316 {
3317 tree t1 = TREE_VALUE (p1);
3318 tree t2 = TREE_VALUE (p2);
3319 int i;
3320
3321 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3322 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3323
3324 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3325 return 0;
3326
3327 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3328 {
3329 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3330 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3331
3332 /* If either of the template parameters are invalid, assume
3333 they match for the sake of error recovery. */
3334 if (error_operand_p (parm1) || error_operand_p (parm2))
3335 return 1;
3336
3337 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3338 return 0;
3339
3340 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3341 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3342 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3343 continue;
3344 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3345 return 0;
3346 }
3347 }
3348
3349 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3350 /* One set of parameters has more parameters lists than the
3351 other. */
3352 return 0;
3353
3354 return 1;
3355 }
3356
3357 /* Returns true if two template parameters are declared with
3358 equivalent constraints. */
3359
3360 static bool
3361 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3362 {
3363 tree req1 = TREE_TYPE (parm1);
3364 tree req2 = TREE_TYPE (parm2);
3365 if (!req1 != !req2)
3366 return false;
3367 if (req1)
3368 return cp_tree_equal (req1, req2);
3369 return true;
3370 }
3371
3372 /* Returns true when two template parameters are equivalent. */
3373
3374 static bool
3375 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3376 {
3377 tree decl1 = TREE_VALUE (parm1);
3378 tree decl2 = TREE_VALUE (parm2);
3379
3380 /* If either of the template parameters are invalid, assume
3381 they match for the sake of error recovery. */
3382 if (error_operand_p (decl1) || error_operand_p (decl2))
3383 return true;
3384
3385 /* ... they declare parameters of the same kind. */
3386 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3387 return false;
3388
3389 /* ... one parameter was introduced by a parameter declaration, then
3390 both are. This case arises as a result of eagerly rewriting declarations
3391 during parsing. */
3392 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3393 return false;
3394
3395 /* ... if either declares a pack, they both do. */
3396 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3397 return false;
3398
3399 if (TREE_CODE (decl1) == PARM_DECL)
3400 {
3401 /* ... if they declare non-type parameters, the types are equivalent. */
3402 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3403 return false;
3404 }
3405 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3406 {
3407 /* ... if they declare template template parameters, their template
3408 parameter lists are equivalent. */
3409 if (!template_heads_equivalent_p (decl1, decl2))
3410 return false;
3411 }
3412
3413 /* ... if they are declared with a qualified-concept name, they both
3414 are, and those names are equivalent. */
3415 return template_parameter_constraints_equivalent_p (parm1, parm2);
3416 }
3417
3418 /* Returns true if two template parameters lists are equivalent.
3419 Two template parameter lists are equivalent if they have the
3420 same length and their corresponding parameters are equivalent.
3421
3422 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3423 data structure returned by DECL_TEMPLATE_PARMS.
3424
3425 This is generally the same implementation as comp_template_parms
3426 except that it also the concept names and arguments used to
3427 introduce parameters. */
3428
3429 static bool
3430 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3431 {
3432 if (parms1 == parms2)
3433 return true;
3434
3435 const_tree p1 = parms1;
3436 const_tree p2 = parms2;
3437 while (p1 != NULL_TREE && p2 != NULL_TREE)
3438 {
3439 tree list1 = TREE_VALUE (p1);
3440 tree list2 = TREE_VALUE (p2);
3441
3442 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3443 return 0;
3444
3445 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3446 {
3447 tree parm1 = TREE_VEC_ELT (list1, i);
3448 tree parm2 = TREE_VEC_ELT (list2, i);
3449 if (!template_parameters_equivalent_p (parm1, parm2))
3450 return false;
3451 }
3452
3453 p1 = TREE_CHAIN (p1);
3454 p2 = TREE_CHAIN (p2);
3455 }
3456
3457 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3458 return false;
3459
3460 return true;
3461 }
3462
3463 /* Return true if the requires-clause of the template parameter lists are
3464 equivalent and false otherwise. */
3465 static bool
3466 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3467 {
3468 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3469 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3470 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3471 return false;
3472 if (!cp_tree_equal (req1, req2))
3473 return false;
3474 return true;
3475 }
3476
3477 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3478 Two template heads are equivalent if their template parameter
3479 lists are equivalent and their requires clauses are equivalent.
3480
3481 In pre-C++20, this is equivalent to calling comp_template_parms
3482 for the template parameters of TMPL1 and TMPL2. */
3483
3484 bool
3485 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3486 {
3487 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3488 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3489
3490 /* Don't change the matching rules for pre-C++20. */
3491 if (cxx_dialect < cxx20)
3492 return comp_template_parms (parms1, parms2);
3493
3494 /* ... have the same number of template parameters, and their
3495 corresponding parameters are equivalent. */
3496 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3497 return false;
3498
3499 /* ... if either has a requires-clause, they both do and their
3500 corresponding constraint-expressions are equivalent. */
3501 return template_requirements_equivalent_p (parms1, parms2);
3502 }
3503
3504 /* Determine whether PARM is a parameter pack. */
3505
3506 bool
3507 template_parameter_pack_p (const_tree parm)
3508 {
3509 /* Determine if we have a non-type template parameter pack. */
3510 if (TREE_CODE (parm) == PARM_DECL)
3511 return (DECL_TEMPLATE_PARM_P (parm)
3512 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3513 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3514 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3515
3516 /* If this is a list of template parameters, we could get a
3517 TYPE_DECL or a TEMPLATE_DECL. */
3518 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3519 parm = TREE_TYPE (parm);
3520
3521 /* Otherwise it must be a type template parameter. */
3522 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3523 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3524 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3525 }
3526
3527 /* Determine if T is a function parameter pack. */
3528
3529 bool
3530 function_parameter_pack_p (const_tree t)
3531 {
3532 if (t && TREE_CODE (t) == PARM_DECL)
3533 return DECL_PACK_P (t);
3534 return false;
3535 }
3536
3537 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3538 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3539
3540 tree
3541 get_function_template_decl (const_tree primary_func_tmpl_inst)
3542 {
3543 if (! primary_func_tmpl_inst
3544 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3545 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3546 return NULL;
3547
3548 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3549 }
3550
3551 /* Return true iff the function parameter PARAM_DECL was expanded
3552 from the function parameter pack PACK. */
3553
3554 bool
3555 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3556 {
3557 if (DECL_ARTIFICIAL (param_decl)
3558 || !function_parameter_pack_p (pack))
3559 return false;
3560
3561 /* The parameter pack and its pack arguments have the same
3562 DECL_PARM_INDEX. */
3563 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3564 }
3565
3566 /* Determine whether ARGS describes a variadic template args list,
3567 i.e., one that is terminated by a template argument pack. */
3568
3569 static bool
3570 template_args_variadic_p (tree args)
3571 {
3572 int nargs;
3573 tree last_parm;
3574
3575 if (args == NULL_TREE)
3576 return false;
3577
3578 args = INNERMOST_TEMPLATE_ARGS (args);
3579 nargs = TREE_VEC_LENGTH (args);
3580
3581 if (nargs == 0)
3582 return false;
3583
3584 last_parm = TREE_VEC_ELT (args, nargs - 1);
3585
3586 return ARGUMENT_PACK_P (last_parm);
3587 }
3588
3589 /* Generate a new name for the parameter pack name NAME (an
3590 IDENTIFIER_NODE) that incorporates its */
3591
3592 static tree
3593 make_ith_pack_parameter_name (tree name, int i)
3594 {
3595 /* Munge the name to include the parameter index. */
3596 #define NUMBUF_LEN 128
3597 char numbuf[NUMBUF_LEN];
3598 char* newname;
3599 int newname_len;
3600
3601 if (name == NULL_TREE)
3602 return name;
3603 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3604 newname_len = IDENTIFIER_LENGTH (name)
3605 + strlen (numbuf) + 2;
3606 newname = (char*)alloca (newname_len);
3607 snprintf (newname, newname_len,
3608 "%s#%i", IDENTIFIER_POINTER (name), i);
3609 return get_identifier (newname);
3610 }
3611
3612 /* Return true if T is a primary function, class or alias template
3613 specialization, not including the template pattern. */
3614
3615 bool
3616 primary_template_specialization_p (const_tree t)
3617 {
3618 if (!t)
3619 return false;
3620
3621 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3622 return (DECL_LANG_SPECIFIC (t)
3623 && DECL_USE_TEMPLATE (t)
3624 && DECL_TEMPLATE_INFO (t)
3625 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3626 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3627 return (CLASSTYPE_TEMPLATE_INFO (t)
3628 && CLASSTYPE_USE_TEMPLATE (t)
3629 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3630 else if (alias_template_specialization_p (t, nt_transparent))
3631 return true;
3632 return false;
3633 }
3634
3635 /* Return true if PARM is a template template parameter. */
3636
3637 bool
3638 template_template_parameter_p (const_tree parm)
3639 {
3640 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3641 }
3642
3643 /* Return true iff PARM is a DECL representing a type template
3644 parameter. */
3645
3646 bool
3647 template_type_parameter_p (const_tree parm)
3648 {
3649 return (parm
3650 && (TREE_CODE (parm) == TYPE_DECL
3651 || TREE_CODE (parm) == TEMPLATE_DECL)
3652 && DECL_TEMPLATE_PARM_P (parm));
3653 }
3654
3655 /* Return the template parameters of T if T is a
3656 primary template instantiation, NULL otherwise. */
3657
3658 tree
3659 get_primary_template_innermost_parameters (const_tree t)
3660 {
3661 tree parms = NULL, template_info = NULL;
3662
3663 if ((template_info = get_template_info (t))
3664 && primary_template_specialization_p (t))
3665 parms = INNERMOST_TEMPLATE_PARMS
3666 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3667
3668 return parms;
3669 }
3670
3671 /* Return the template parameters of the LEVELth level from the full list
3672 of template parameters PARMS. */
3673
3674 tree
3675 get_template_parms_at_level (tree parms, int level)
3676 {
3677 tree p;
3678 if (!parms
3679 || TREE_CODE (parms) != TREE_LIST
3680 || level > TMPL_PARMS_DEPTH (parms))
3681 return NULL_TREE;
3682
3683 for (p = parms; p; p = TREE_CHAIN (p))
3684 if (TMPL_PARMS_DEPTH (p) == level)
3685 return p;
3686
3687 return NULL_TREE;
3688 }
3689
3690 /* Returns the template arguments of T if T is a template instantiation,
3691 NULL otherwise. */
3692
3693 tree
3694 get_template_innermost_arguments (const_tree t)
3695 {
3696 tree args = NULL, template_info = NULL;
3697
3698 if ((template_info = get_template_info (t))
3699 && TI_ARGS (template_info))
3700 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3701
3702 return args;
3703 }
3704
3705 /* Return the argument pack elements of T if T is a template argument pack,
3706 NULL otherwise. */
3707
3708 tree
3709 get_template_argument_pack_elems (const_tree t)
3710 {
3711 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3712 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3713 return NULL;
3714
3715 return ARGUMENT_PACK_ARGS (t);
3716 }
3717
3718 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3719 ARGUMENT_PACK_SELECT represents. */
3720
3721 static tree
3722 argument_pack_select_arg (tree t)
3723 {
3724 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3725 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3726
3727 /* If the selected argument is an expansion E, that most likely means we were
3728 called from gen_elem_of_pack_expansion_instantiation during the
3729 substituting of an argument pack (of which the Ith element is a pack
3730 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3731 In this case, the Ith element resulting from this substituting is going to
3732 be a pack expansion, which pattern is the pattern of E. Let's return the
3733 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3734 resulting pack expansion from it. */
3735 if (PACK_EXPANSION_P (arg))
3736 {
3737 /* Make sure we aren't throwing away arg info. */
3738 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3739 arg = PACK_EXPANSION_PATTERN (arg);
3740 }
3741
3742 return arg;
3743 }
3744
3745
3746 /* True iff FN is a function representing a built-in variadic parameter
3747 pack. */
3748
3749 bool
3750 builtin_pack_fn_p (tree fn)
3751 {
3752 if (!fn
3753 || TREE_CODE (fn) != FUNCTION_DECL
3754 || !DECL_IS_BUILTIN (fn))
3755 return false;
3756
3757 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3758 return true;
3759
3760 return false;
3761 }
3762
3763 /* True iff CALL is a call to a function representing a built-in variadic
3764 parameter pack. */
3765
3766 static bool
3767 builtin_pack_call_p (tree call)
3768 {
3769 if (TREE_CODE (call) != CALL_EXPR)
3770 return false;
3771 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3772 }
3773
3774 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3775
3776 static tree
3777 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3778 tree in_decl)
3779 {
3780 tree ohi = CALL_EXPR_ARG (call, 0);
3781 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3782 false/*fn*/, true/*int_cst*/);
3783
3784 if (value_dependent_expression_p (hi))
3785 {
3786 if (hi != ohi)
3787 {
3788 call = copy_node (call);
3789 CALL_EXPR_ARG (call, 0) = hi;
3790 }
3791 tree ex = make_pack_expansion (call, complain);
3792 tree vec = make_tree_vec (1);
3793 TREE_VEC_ELT (vec, 0) = ex;
3794 return vec;
3795 }
3796 else
3797 {
3798 hi = cxx_constant_value (hi);
3799 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3800
3801 /* Calculate the largest value of len that won't make the size of the vec
3802 overflow an int. The compiler will exceed resource limits long before
3803 this, but it seems a decent place to diagnose. */
3804 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3805
3806 if (len < 0 || len > max)
3807 {
3808 if ((complain & tf_error)
3809 && hi != error_mark_node)
3810 error ("argument to %<__integer_pack%> must be between 0 and %d",
3811 max);
3812 return error_mark_node;
3813 }
3814
3815 tree vec = make_tree_vec (len);
3816
3817 for (int i = 0; i < len; ++i)
3818 TREE_VEC_ELT (vec, i) = size_int (i);
3819
3820 return vec;
3821 }
3822 }
3823
3824 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3825 CALL. */
3826
3827 static tree
3828 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3829 tree in_decl)
3830 {
3831 if (!builtin_pack_call_p (call))
3832 return NULL_TREE;
3833
3834 tree fn = CALL_EXPR_FN (call);
3835
3836 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3837 return expand_integer_pack (call, args, complain, in_decl);
3838
3839 return NULL_TREE;
3840 }
3841
3842 /* Structure used to track the progress of find_parameter_packs_r. */
3843 struct find_parameter_pack_data
3844 {
3845 /* TREE_LIST that will contain all of the parameter packs found by
3846 the traversal. */
3847 tree* parameter_packs;
3848
3849 /* Set of AST nodes that have been visited by the traversal. */
3850 hash_set<tree> *visited;
3851
3852 /* True iff we're making a type pack expansion. */
3853 bool type_pack_expansion_p;
3854 };
3855
3856 /* Identifies all of the argument packs that occur in a template
3857 argument and appends them to the TREE_LIST inside DATA, which is a
3858 find_parameter_pack_data structure. This is a subroutine of
3859 make_pack_expansion and uses_parameter_packs. */
3860 static tree
3861 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3862 {
3863 tree t = *tp;
3864 struct find_parameter_pack_data* ppd =
3865 (struct find_parameter_pack_data*)data;
3866 bool parameter_pack_p = false;
3867
3868 /* Don't look through typedefs; we are interested in whether a
3869 parameter pack is actually written in the expression/type we're
3870 looking at, not the target type. */
3871 if (TYPE_P (t) && typedef_variant_p (t))
3872 {
3873 /* But do look at arguments for an alias template. */
3874 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3875 cp_walk_tree (&TI_ARGS (tinfo),
3876 &find_parameter_packs_r,
3877 ppd, ppd->visited);
3878 *walk_subtrees = 0;
3879 return NULL_TREE;
3880 }
3881
3882 /* Identify whether this is a parameter pack or not. */
3883 switch (TREE_CODE (t))
3884 {
3885 case TEMPLATE_PARM_INDEX:
3886 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3887 parameter_pack_p = true;
3888 break;
3889
3890 case TEMPLATE_TYPE_PARM:
3891 t = TYPE_MAIN_VARIANT (t);
3892 /* FALLTHRU */
3893 case TEMPLATE_TEMPLATE_PARM:
3894 /* If the placeholder appears in the decl-specifier-seq of a function
3895 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3896 is a pack expansion, the invented template parameter is a template
3897 parameter pack. */
3898 if (ppd->type_pack_expansion_p && is_auto (t))
3899 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3900 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3901 parameter_pack_p = true;
3902 break;
3903
3904 case FIELD_DECL:
3905 case PARM_DECL:
3906 if (DECL_PACK_P (t))
3907 {
3908 /* We don't want to walk into the type of a PARM_DECL,
3909 because we don't want to see the type parameter pack. */
3910 *walk_subtrees = 0;
3911 parameter_pack_p = true;
3912 }
3913 break;
3914
3915 case VAR_DECL:
3916 if (DECL_PACK_P (t))
3917 {
3918 /* We don't want to walk into the type of a variadic capture proxy,
3919 because we don't want to see the type parameter pack. */
3920 *walk_subtrees = 0;
3921 parameter_pack_p = true;
3922 }
3923 else if (variable_template_specialization_p (t))
3924 {
3925 cp_walk_tree (&DECL_TI_ARGS (t),
3926 find_parameter_packs_r,
3927 ppd, ppd->visited);
3928 *walk_subtrees = 0;
3929 }
3930 break;
3931
3932 case CALL_EXPR:
3933 if (builtin_pack_call_p (t))
3934 parameter_pack_p = true;
3935 break;
3936
3937 case BASES:
3938 parameter_pack_p = true;
3939 break;
3940 default:
3941 /* Not a parameter pack. */
3942 break;
3943 }
3944
3945 if (parameter_pack_p)
3946 {
3947 /* Add this parameter pack to the list. */
3948 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3949 }
3950
3951 if (TYPE_P (t))
3952 cp_walk_tree (&TYPE_CONTEXT (t),
3953 &find_parameter_packs_r, ppd, ppd->visited);
3954
3955 /* This switch statement will return immediately if we don't find a
3956 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3957 switch (TREE_CODE (t))
3958 {
3959 case BOUND_TEMPLATE_TEMPLATE_PARM:
3960 /* Check the template itself. */
3961 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3962 &find_parameter_packs_r, ppd, ppd->visited);
3963 return NULL_TREE;
3964
3965 case DECL_EXPR:
3966 {
3967 tree decl = DECL_EXPR_DECL (t);
3968 /* Ignore the declaration of a capture proxy for a parameter pack. */
3969 if (is_capture_proxy (decl))
3970 *walk_subtrees = 0;
3971 if (is_typedef_decl (decl))
3972 /* Since we stop at typedefs above, we need to look through them at
3973 the point of the DECL_EXPR. */
3974 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3975 &find_parameter_packs_r, ppd, ppd->visited);
3976 return NULL_TREE;
3977 }
3978
3979 case TEMPLATE_DECL:
3980 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3981 return NULL_TREE;
3982 cp_walk_tree (&TREE_TYPE (t),
3983 &find_parameter_packs_r, ppd, ppd->visited);
3984 return NULL_TREE;
3985
3986 case TYPE_PACK_EXPANSION:
3987 case EXPR_PACK_EXPANSION:
3988 *walk_subtrees = 0;
3989 return NULL_TREE;
3990
3991 case INTEGER_TYPE:
3992 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3993 ppd, ppd->visited);
3994 *walk_subtrees = 0;
3995 return NULL_TREE;
3996
3997 case IDENTIFIER_NODE:
3998 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3999 ppd->visited);
4000 *walk_subtrees = 0;
4001 return NULL_TREE;
4002
4003 case LAMBDA_EXPR:
4004 {
4005 /* Since we defer implicit capture, look in the parms and body. */
4006 tree fn = lambda_function (t);
4007 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4008 ppd->visited);
4009 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4010 ppd->visited);
4011 return NULL_TREE;
4012 }
4013
4014 case DECLTYPE_TYPE:
4015 {
4016 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4017 type_pack_expansion_p to false so that any placeholders
4018 within the expression don't get marked as parameter packs. */
4019 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4020 ppd->type_pack_expansion_p = false;
4021 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4022 ppd, ppd->visited);
4023 ppd->type_pack_expansion_p = type_pack_expansion_p;
4024 *walk_subtrees = 0;
4025 return NULL_TREE;
4026 }
4027
4028 case IF_STMT:
4029 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4030 ppd, ppd->visited);
4031 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4032 ppd, ppd->visited);
4033 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4034 ppd, ppd->visited);
4035 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4036 *walk_subtrees = 0;
4037 return NULL_TREE;
4038
4039 default:
4040 return NULL_TREE;
4041 }
4042
4043 return NULL_TREE;
4044 }
4045
4046 /* Determines if the expression or type T uses any parameter packs. */
4047 tree
4048 uses_parameter_packs (tree t)
4049 {
4050 tree parameter_packs = NULL_TREE;
4051 struct find_parameter_pack_data ppd;
4052 ppd.parameter_packs = &parameter_packs;
4053 ppd.visited = new hash_set<tree>;
4054 ppd.type_pack_expansion_p = false;
4055 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4056 delete ppd.visited;
4057 return parameter_packs;
4058 }
4059
4060 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4061 representation a base-class initializer into a parameter pack
4062 expansion. If all goes well, the resulting node will be an
4063 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4064 respectively. */
4065 tree
4066 make_pack_expansion (tree arg, tsubst_flags_t complain)
4067 {
4068 tree result;
4069 tree parameter_packs = NULL_TREE;
4070 bool for_types = false;
4071 struct find_parameter_pack_data ppd;
4072
4073 if (!arg || arg == error_mark_node)
4074 return arg;
4075
4076 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4077 {
4078 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4079 class initializer. In this case, the TREE_PURPOSE will be a
4080 _TYPE node (representing the base class expansion we're
4081 initializing) and the TREE_VALUE will be a TREE_LIST
4082 containing the initialization arguments.
4083
4084 The resulting expansion looks somewhat different from most
4085 expansions. Rather than returning just one _EXPANSION, we
4086 return a TREE_LIST whose TREE_PURPOSE is a
4087 TYPE_PACK_EXPANSION containing the bases that will be
4088 initialized. The TREE_VALUE will be identical to the
4089 original TREE_VALUE, which is a list of arguments that will
4090 be passed to each base. We do not introduce any new pack
4091 expansion nodes into the TREE_VALUE (although it is possible
4092 that some already exist), because the TREE_PURPOSE and
4093 TREE_VALUE all need to be expanded together with the same
4094 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4095 resulting TREE_PURPOSE will mention the parameter packs in
4096 both the bases and the arguments to the bases. */
4097 tree purpose;
4098 tree value;
4099 tree parameter_packs = NULL_TREE;
4100
4101 /* Determine which parameter packs will be used by the base
4102 class expansion. */
4103 ppd.visited = new hash_set<tree>;
4104 ppd.parameter_packs = &parameter_packs;
4105 ppd.type_pack_expansion_p = false;
4106 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4107 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4108 &ppd, ppd.visited);
4109
4110 if (parameter_packs == NULL_TREE)
4111 {
4112 if (complain & tf_error)
4113 error ("base initializer expansion %qT contains no parameter packs",
4114 arg);
4115 delete ppd.visited;
4116 return error_mark_node;
4117 }
4118
4119 if (TREE_VALUE (arg) != void_type_node)
4120 {
4121 /* Collect the sets of parameter packs used in each of the
4122 initialization arguments. */
4123 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4124 {
4125 /* Determine which parameter packs will be expanded in this
4126 argument. */
4127 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4128 &ppd, ppd.visited);
4129 }
4130 }
4131
4132 delete ppd.visited;
4133
4134 /* Create the pack expansion type for the base type. */
4135 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4136 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4137 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4138 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4139
4140 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4141 they will rarely be compared to anything. */
4142 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4143
4144 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4145 }
4146
4147 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4148 for_types = true;
4149
4150 /* Build the PACK_EXPANSION_* node. */
4151 result = for_types
4152 ? cxx_make_type (TYPE_PACK_EXPANSION)
4153 : make_node (EXPR_PACK_EXPANSION);
4154 SET_PACK_EXPANSION_PATTERN (result, arg);
4155 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4156 {
4157 /* Propagate type and const-expression information. */
4158 TREE_TYPE (result) = TREE_TYPE (arg);
4159 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4160 /* Mark this read now, since the expansion might be length 0. */
4161 mark_exp_read (arg);
4162 }
4163 else
4164 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4165 they will rarely be compared to anything. */
4166 SET_TYPE_STRUCTURAL_EQUALITY (result);
4167
4168 /* Determine which parameter packs will be expanded. */
4169 ppd.parameter_packs = &parameter_packs;
4170 ppd.visited = new hash_set<tree>;
4171 ppd.type_pack_expansion_p = TYPE_P (arg);
4172 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4173 delete ppd.visited;
4174
4175 /* Make sure we found some parameter packs. */
4176 if (parameter_packs == NULL_TREE)
4177 {
4178 if (complain & tf_error)
4179 {
4180 if (TYPE_P (arg))
4181 error ("expansion pattern %qT contains no parameter packs", arg);
4182 else
4183 error ("expansion pattern %qE contains no parameter packs", arg);
4184 }
4185 return error_mark_node;
4186 }
4187 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4188
4189 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4190
4191 return result;
4192 }
4193
4194 /* Checks T for any "bare" parameter packs, which have not yet been
4195 expanded, and issues an error if any are found. This operation can
4196 only be done on full expressions or types (e.g., an expression
4197 statement, "if" condition, etc.), because we could have expressions like:
4198
4199 foo(f(g(h(args)))...)
4200
4201 where "args" is a parameter pack. check_for_bare_parameter_packs
4202 should not be called for the subexpressions args, h(args),
4203 g(h(args)), or f(g(h(args))), because we would produce erroneous
4204 error messages.
4205
4206 Returns TRUE and emits an error if there were bare parameter packs,
4207 returns FALSE otherwise. */
4208 bool
4209 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4210 {
4211 tree parameter_packs = NULL_TREE;
4212 struct find_parameter_pack_data ppd;
4213
4214 if (!processing_template_decl || !t || t == error_mark_node)
4215 return false;
4216
4217 /* A lambda might use a parameter pack from the containing context. */
4218 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4219 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4220 return false;
4221
4222 if (TREE_CODE (t) == TYPE_DECL)
4223 t = TREE_TYPE (t);
4224
4225 ppd.parameter_packs = &parameter_packs;
4226 ppd.visited = new hash_set<tree>;
4227 ppd.type_pack_expansion_p = false;
4228 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4229 delete ppd.visited;
4230
4231 if (parameter_packs)
4232 {
4233 if (loc == UNKNOWN_LOCATION)
4234 loc = cp_expr_loc_or_input_loc (t);
4235 error_at (loc, "parameter packs not expanded with %<...%>:");
4236 while (parameter_packs)
4237 {
4238 tree pack = TREE_VALUE (parameter_packs);
4239 tree name = NULL_TREE;
4240
4241 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4242 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4243 name = TYPE_NAME (pack);
4244 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4245 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4246 else if (TREE_CODE (pack) == CALL_EXPR)
4247 name = DECL_NAME (CALL_EXPR_FN (pack));
4248 else
4249 name = DECL_NAME (pack);
4250
4251 if (name)
4252 inform (loc, " %qD", name);
4253 else
4254 inform (loc, " %s", "<anonymous>");
4255
4256 parameter_packs = TREE_CHAIN (parameter_packs);
4257 }
4258
4259 return true;
4260 }
4261
4262 return false;
4263 }
4264
4265 /* Expand any parameter packs that occur in the template arguments in
4266 ARGS. */
4267 tree
4268 expand_template_argument_pack (tree args)
4269 {
4270 if (args == error_mark_node)
4271 return error_mark_node;
4272
4273 tree result_args = NULL_TREE;
4274 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4275 int num_result_args = -1;
4276 int non_default_args_count = -1;
4277
4278 /* First, determine if we need to expand anything, and the number of
4279 slots we'll need. */
4280 for (in_arg = 0; in_arg < nargs; ++in_arg)
4281 {
4282 tree arg = TREE_VEC_ELT (args, in_arg);
4283 if (arg == NULL_TREE)
4284 return args;
4285 if (ARGUMENT_PACK_P (arg))
4286 {
4287 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4288 if (num_result_args < 0)
4289 num_result_args = in_arg + num_packed;
4290 else
4291 num_result_args += num_packed;
4292 }
4293 else
4294 {
4295 if (num_result_args >= 0)
4296 num_result_args++;
4297 }
4298 }
4299
4300 /* If no expansion is necessary, we're done. */
4301 if (num_result_args < 0)
4302 return args;
4303
4304 /* Expand arguments. */
4305 result_args = make_tree_vec (num_result_args);
4306 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4307 non_default_args_count =
4308 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4309 for (in_arg = 0; in_arg < nargs; ++in_arg)
4310 {
4311 tree arg = TREE_VEC_ELT (args, in_arg);
4312 if (ARGUMENT_PACK_P (arg))
4313 {
4314 tree packed = ARGUMENT_PACK_ARGS (arg);
4315 int i, num_packed = TREE_VEC_LENGTH (packed);
4316 for (i = 0; i < num_packed; ++i, ++out_arg)
4317 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4318 if (non_default_args_count > 0)
4319 non_default_args_count += num_packed - 1;
4320 }
4321 else
4322 {
4323 TREE_VEC_ELT (result_args, out_arg) = arg;
4324 ++out_arg;
4325 }
4326 }
4327 if (non_default_args_count >= 0)
4328 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4329 return result_args;
4330 }
4331
4332 /* Checks if DECL shadows a template parameter.
4333
4334 [temp.local]: A template-parameter shall not be redeclared within its
4335 scope (including nested scopes).
4336
4337 Emits an error and returns TRUE if the DECL shadows a parameter,
4338 returns FALSE otherwise. */
4339
4340 bool
4341 check_template_shadow (tree decl)
4342 {
4343 tree olddecl;
4344
4345 /* If we're not in a template, we can't possibly shadow a template
4346 parameter. */
4347 if (!current_template_parms)
4348 return true;
4349
4350 /* Figure out what we're shadowing. */
4351 decl = OVL_FIRST (decl);
4352 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4353
4354 /* If there's no previous binding for this name, we're not shadowing
4355 anything, let alone a template parameter. */
4356 if (!olddecl)
4357 return true;
4358
4359 /* If we're not shadowing a template parameter, we're done. Note
4360 that OLDDECL might be an OVERLOAD (or perhaps even an
4361 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4362 node. */
4363 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4364 return true;
4365
4366 /* We check for decl != olddecl to avoid bogus errors for using a
4367 name inside a class. We check TPFI to avoid duplicate errors for
4368 inline member templates. */
4369 if (decl == olddecl
4370 || (DECL_TEMPLATE_PARM_P (decl)
4371 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4372 return true;
4373
4374 /* Don't complain about the injected class name, as we've already
4375 complained about the class itself. */
4376 if (DECL_SELF_REFERENCE_P (decl))
4377 return false;
4378
4379 if (DECL_TEMPLATE_PARM_P (decl))
4380 error ("declaration of template parameter %q+D shadows "
4381 "template parameter", decl);
4382 else
4383 error ("declaration of %q+#D shadows template parameter", decl);
4384 inform (DECL_SOURCE_LOCATION (olddecl),
4385 "template parameter %qD declared here", olddecl);
4386 return false;
4387 }
4388
4389 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4390 ORIG_LEVEL, DECL, and TYPE. */
4391
4392 static tree
4393 build_template_parm_index (int index,
4394 int level,
4395 int orig_level,
4396 tree decl,
4397 tree type)
4398 {
4399 tree t = make_node (TEMPLATE_PARM_INDEX);
4400 TEMPLATE_PARM_IDX (t) = index;
4401 TEMPLATE_PARM_LEVEL (t) = level;
4402 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4403 TEMPLATE_PARM_DECL (t) = decl;
4404 TREE_TYPE (t) = type;
4405 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4406 TREE_READONLY (t) = TREE_READONLY (decl);
4407
4408 return t;
4409 }
4410
4411 /* Find the canonical type parameter for the given template type
4412 parameter. Returns the canonical type parameter, which may be TYPE
4413 if no such parameter existed. */
4414
4415 static tree
4416 canonical_type_parameter (tree type)
4417 {
4418 int idx = TEMPLATE_TYPE_IDX (type);
4419
4420 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4421
4422 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4423 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4424
4425 for (tree list = (*canonical_template_parms)[idx];
4426 list; list = TREE_CHAIN (list))
4427 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4428 return TREE_VALUE (list);
4429
4430 (*canonical_template_parms)[idx]
4431 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4432 return type;
4433 }
4434
4435 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4436 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4437 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4438 new one is created. */
4439
4440 static tree
4441 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4442 tsubst_flags_t complain)
4443 {
4444 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4445 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4446 != TEMPLATE_PARM_LEVEL (index) - levels)
4447 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4448 {
4449 tree orig_decl = TEMPLATE_PARM_DECL (index);
4450
4451 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4452 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4453 type);
4454 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4455 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4456 DECL_ARTIFICIAL (decl) = 1;
4457 SET_DECL_TEMPLATE_PARM_P (decl);
4458
4459 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4460 TEMPLATE_PARM_LEVEL (index) - levels,
4461 TEMPLATE_PARM_ORIG_LEVEL (index),
4462 decl, type);
4463 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4464 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4465 = TEMPLATE_PARM_PARAMETER_PACK (index);
4466
4467 /* Template template parameters need this. */
4468 tree inner = decl;
4469 if (TREE_CODE (decl) == TEMPLATE_DECL)
4470 {
4471 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4472 TYPE_DECL, DECL_NAME (decl), type);
4473 DECL_TEMPLATE_RESULT (decl) = inner;
4474 DECL_ARTIFICIAL (inner) = true;
4475 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4476 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4477 }
4478
4479 /* Attach the TPI to the decl. */
4480 if (TREE_CODE (inner) == TYPE_DECL)
4481 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4482 else
4483 DECL_INITIAL (decl) = tpi;
4484 }
4485
4486 return TEMPLATE_PARM_DESCENDANTS (index);
4487 }
4488
4489 /* Process information from new template parameter PARM and append it
4490 to the LIST being built. This new parameter is a non-type
4491 parameter iff IS_NON_TYPE is true. This new parameter is a
4492 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4493 is in PARM_LOC. */
4494
4495 tree
4496 process_template_parm (tree list, location_t parm_loc, tree parm,
4497 bool is_non_type, bool is_parameter_pack)
4498 {
4499 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4500 tree prev = NULL_TREE;
4501 int idx = 0;
4502
4503 if (list)
4504 {
4505 prev = tree_last (list);
4506
4507 tree p = TREE_VALUE (prev);
4508 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4509 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4510 else if (TREE_CODE (p) == PARM_DECL)
4511 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4512
4513 ++idx;
4514 }
4515
4516 tree decl = NULL_TREE;
4517 tree defval = TREE_PURPOSE (parm);
4518 tree constr = TREE_TYPE (parm);
4519
4520 if (is_non_type)
4521 {
4522 parm = TREE_VALUE (parm);
4523
4524 SET_DECL_TEMPLATE_PARM_P (parm);
4525
4526 if (TREE_TYPE (parm) != error_mark_node)
4527 {
4528 /* [temp.param]
4529
4530 The top-level cv-qualifiers on the template-parameter are
4531 ignored when determining its type. */
4532 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4533 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4534 TREE_TYPE (parm) = error_mark_node;
4535 else if (uses_parameter_packs (TREE_TYPE (parm))
4536 && !is_parameter_pack
4537 /* If we're in a nested template parameter list, the template
4538 template parameter could be a parameter pack. */
4539 && processing_template_parmlist == 1)
4540 {
4541 /* This template parameter is not a parameter pack, but it
4542 should be. Complain about "bare" parameter packs. */
4543 check_for_bare_parameter_packs (TREE_TYPE (parm));
4544
4545 /* Recover by calling this a parameter pack. */
4546 is_parameter_pack = true;
4547 }
4548 }
4549
4550 /* A template parameter is not modifiable. */
4551 TREE_CONSTANT (parm) = 1;
4552 TREE_READONLY (parm) = 1;
4553 decl = build_decl (parm_loc,
4554 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4555 TREE_CONSTANT (decl) = 1;
4556 TREE_READONLY (decl) = 1;
4557 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4558 = build_template_parm_index (idx, processing_template_decl,
4559 processing_template_decl,
4560 decl, TREE_TYPE (parm));
4561
4562 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4563 = is_parameter_pack;
4564 }
4565 else
4566 {
4567 tree t;
4568 parm = TREE_VALUE (TREE_VALUE (parm));
4569
4570 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4571 {
4572 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4573 /* This is for distinguishing between real templates and template
4574 template parameters */
4575 TREE_TYPE (parm) = t;
4576
4577 /* any_template_parm_r expects to be able to get the targs of a
4578 DECL_TEMPLATE_RESULT. */
4579 tree result = DECL_TEMPLATE_RESULT (parm);
4580 TREE_TYPE (result) = t;
4581 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4582 tree tinfo = build_template_info (parm, args);
4583 retrofit_lang_decl (result);
4584 DECL_TEMPLATE_INFO (result) = tinfo;
4585
4586 decl = parm;
4587 }
4588 else
4589 {
4590 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4591 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4592 decl = build_decl (parm_loc,
4593 TYPE_DECL, parm, t);
4594 }
4595
4596 TYPE_NAME (t) = decl;
4597 TYPE_STUB_DECL (t) = decl;
4598 parm = decl;
4599 TEMPLATE_TYPE_PARM_INDEX (t)
4600 = build_template_parm_index (idx, processing_template_decl,
4601 processing_template_decl,
4602 decl, TREE_TYPE (parm));
4603 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4604 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4605 SET_TYPE_STRUCTURAL_EQUALITY (t);
4606 else
4607 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4608 }
4609 DECL_ARTIFICIAL (decl) = 1;
4610 SET_DECL_TEMPLATE_PARM_P (decl);
4611
4612 /* Build requirements for the type/template parameter.
4613 This must be done after SET_DECL_TEMPLATE_PARM_P or
4614 process_template_parm could fail. */
4615 tree reqs = finish_shorthand_constraint (parm, constr);
4616
4617 decl = pushdecl (decl);
4618 if (!is_non_type)
4619 parm = decl;
4620
4621 /* Build the parameter node linking the parameter declaration,
4622 its default argument (if any), and its constraints (if any). */
4623 parm = build_tree_list (defval, parm);
4624 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4625
4626 if (prev)
4627 TREE_CHAIN (prev) = parm;
4628 else
4629 list = parm;
4630
4631 return list;
4632 }
4633
4634 /* The end of a template parameter list has been reached. Process the
4635 tree list into a parameter vector, converting each parameter into a more
4636 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4637 as PARM_DECLs. */
4638
4639 tree
4640 end_template_parm_list (tree parms)
4641 {
4642 tree saved_parmlist = make_tree_vec (list_length (parms));
4643
4644 /* Pop the dummy parameter level and add the real one. We do not
4645 morph the dummy parameter in place, as it might have been
4646 captured by a (nested) template-template-parm. */
4647 current_template_parms = TREE_CHAIN (current_template_parms);
4648
4649 current_template_parms
4650 = tree_cons (size_int (processing_template_decl),
4651 saved_parmlist, current_template_parms);
4652
4653 for (unsigned ix = 0; parms; ix++)
4654 {
4655 tree parm = parms;
4656 parms = TREE_CHAIN (parms);
4657 TREE_CHAIN (parm) = NULL_TREE;
4658
4659 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4660 }
4661
4662 --processing_template_parmlist;
4663
4664 return saved_parmlist;
4665 }
4666
4667 // Explicitly indicate the end of the template parameter list. We assume
4668 // that the current template parameters have been constructed and/or
4669 // managed explicitly, as when creating new template template parameters
4670 // from a shorthand constraint.
4671 void
4672 end_template_parm_list ()
4673 {
4674 --processing_template_parmlist;
4675 }
4676
4677 /* end_template_decl is called after a template declaration is seen. */
4678
4679 void
4680 end_template_decl (void)
4681 {
4682 reset_specialization ();
4683
4684 if (! processing_template_decl)
4685 return;
4686
4687 /* This matches the pushlevel in begin_template_parm_list. */
4688 finish_scope ();
4689
4690 --processing_template_decl;
4691 current_template_parms = TREE_CHAIN (current_template_parms);
4692 }
4693
4694 /* Takes a TREE_LIST representing a template parameter and convert it
4695 into an argument suitable to be passed to the type substitution
4696 functions. Note that If the TREE_LIST contains an error_mark
4697 node, the returned argument is error_mark_node. */
4698
4699 tree
4700 template_parm_to_arg (tree t)
4701 {
4702
4703 if (t == NULL_TREE
4704 || TREE_CODE (t) != TREE_LIST)
4705 return t;
4706
4707 if (error_operand_p (TREE_VALUE (t)))
4708 return error_mark_node;
4709
4710 t = TREE_VALUE (t);
4711
4712 if (TREE_CODE (t) == TYPE_DECL
4713 || TREE_CODE (t) == TEMPLATE_DECL)
4714 {
4715 t = TREE_TYPE (t);
4716
4717 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4718 {
4719 /* Turn this argument into a TYPE_ARGUMENT_PACK
4720 with a single element, which expands T. */
4721 tree vec = make_tree_vec (1);
4722 if (CHECKING_P)
4723 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4724
4725 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4726
4727 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4728 SET_ARGUMENT_PACK_ARGS (t, vec);
4729 }
4730 }
4731 else
4732 {
4733 t = DECL_INITIAL (t);
4734
4735 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4736 {
4737 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4738 with a single element, which expands T. */
4739 tree vec = make_tree_vec (1);
4740 if (CHECKING_P)
4741 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4742
4743 t = convert_from_reference (t);
4744 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4745
4746 t = make_node (NONTYPE_ARGUMENT_PACK);
4747 SET_ARGUMENT_PACK_ARGS (t, vec);
4748 }
4749 else
4750 t = convert_from_reference (t);
4751 }
4752 return t;
4753 }
4754
4755 /* Given a single level of template parameters (a TREE_VEC), return it
4756 as a set of template arguments. */
4757
4758 tree
4759 template_parms_level_to_args (tree parms)
4760 {
4761 tree a = copy_node (parms);
4762 TREE_TYPE (a) = NULL_TREE;
4763 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4764 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4765
4766 if (CHECKING_P)
4767 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4768
4769 return a;
4770 }
4771
4772 /* Given a set of template parameters, return them as a set of template
4773 arguments. The template parameters are represented as a TREE_VEC, in
4774 the form documented in cp-tree.h for template arguments. */
4775
4776 tree
4777 template_parms_to_args (tree parms)
4778 {
4779 tree header;
4780 tree args = NULL_TREE;
4781 int length = TMPL_PARMS_DEPTH (parms);
4782 int l = length;
4783
4784 /* If there is only one level of template parameters, we do not
4785 create a TREE_VEC of TREE_VECs. Instead, we return a single
4786 TREE_VEC containing the arguments. */
4787 if (length > 1)
4788 args = make_tree_vec (length);
4789
4790 for (header = parms; header; header = TREE_CHAIN (header))
4791 {
4792 tree a = template_parms_level_to_args (TREE_VALUE (header));
4793
4794 if (length > 1)
4795 TREE_VEC_ELT (args, --l) = a;
4796 else
4797 args = a;
4798 }
4799
4800 return args;
4801 }
4802
4803 /* Within the declaration of a template, return the currently active
4804 template parameters as an argument TREE_VEC. */
4805
4806 static tree
4807 current_template_args (void)
4808 {
4809 return template_parms_to_args (current_template_parms);
4810 }
4811
4812 /* Return the fully generic arguments for of TMPL, i.e. what
4813 current_template_args would be while parsing it. */
4814
4815 tree
4816 generic_targs_for (tree tmpl)
4817 {
4818 if (tmpl == NULL_TREE)
4819 return NULL_TREE;
4820 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4821 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4822 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4823 template parameter, it has no TEMPLATE_INFO; for a partial
4824 specialization, it has the arguments for the primary template, and we
4825 want the arguments for the partial specialization. */;
4826 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4827 if (tree ti = get_template_info (result))
4828 return TI_ARGS (ti);
4829 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4830 }
4831
4832 /* Update the declared TYPE by doing any lookups which were thought to be
4833 dependent, but are not now that we know the SCOPE of the declarator. */
4834
4835 tree
4836 maybe_update_decl_type (tree orig_type, tree scope)
4837 {
4838 tree type = orig_type;
4839
4840 if (type == NULL_TREE)
4841 return type;
4842
4843 if (TREE_CODE (orig_type) == TYPE_DECL)
4844 type = TREE_TYPE (type);
4845
4846 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4847 && dependent_type_p (type)
4848 /* Don't bother building up the args in this case. */
4849 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4850 {
4851 /* tsubst in the args corresponding to the template parameters,
4852 including auto if present. Most things will be unchanged, but
4853 make_typename_type and tsubst_qualified_id will resolve
4854 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4855 tree args = current_template_args ();
4856 tree auto_node = type_uses_auto (type);
4857 tree pushed;
4858 if (auto_node)
4859 {
4860 tree auto_vec = make_tree_vec (1);
4861 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4862 args = add_to_template_args (args, auto_vec);
4863 }
4864 pushed = push_scope (scope);
4865 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4866 if (pushed)
4867 pop_scope (scope);
4868 }
4869
4870 if (type == error_mark_node)
4871 return orig_type;
4872
4873 if (TREE_CODE (orig_type) == TYPE_DECL)
4874 {
4875 if (same_type_p (type, TREE_TYPE (orig_type)))
4876 type = orig_type;
4877 else
4878 type = TYPE_NAME (type);
4879 }
4880 return type;
4881 }
4882
4883 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4884 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4885 the new template is a member template. */
4886
4887 static tree
4888 build_template_decl (tree decl, tree parms, bool member_template_p)
4889 {
4890 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4891 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4892 DECL_TEMPLATE_PARMS (tmpl) = parms;
4893 DECL_TEMPLATE_RESULT (tmpl) = decl;
4894 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4895 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4896 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4897 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4898
4899 return tmpl;
4900 }
4901
4902 struct template_parm_data
4903 {
4904 /* The level of the template parameters we are currently
4905 processing. */
4906 int level;
4907
4908 /* The index of the specialization argument we are currently
4909 processing. */
4910 int current_arg;
4911
4912 /* An array whose size is the number of template parameters. The
4913 elements are nonzero if the parameter has been used in any one
4914 of the arguments processed so far. */
4915 int* parms;
4916
4917 /* An array whose size is the number of template arguments. The
4918 elements are nonzero if the argument makes use of template
4919 parameters of this level. */
4920 int* arg_uses_template_parms;
4921 };
4922
4923 /* Subroutine of push_template_decl used to see if each template
4924 parameter in a partial specialization is used in the explicit
4925 argument list. If T is of the LEVEL given in DATA (which is
4926 treated as a template_parm_data*), then DATA->PARMS is marked
4927 appropriately. */
4928
4929 static int
4930 mark_template_parm (tree t, void* data)
4931 {
4932 int level;
4933 int idx;
4934 struct template_parm_data* tpd = (struct template_parm_data*) data;
4935
4936 template_parm_level_and_index (t, &level, &idx);
4937
4938 if (level == tpd->level)
4939 {
4940 tpd->parms[idx] = 1;
4941 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4942 }
4943
4944 /* In C++17 the type of a non-type argument is a deduced context. */
4945 if (cxx_dialect >= cxx17
4946 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4947 for_each_template_parm (TREE_TYPE (t),
4948 &mark_template_parm,
4949 data,
4950 NULL,
4951 /*include_nondeduced_p=*/false);
4952
4953 /* Return zero so that for_each_template_parm will continue the
4954 traversal of the tree; we want to mark *every* template parm. */
4955 return 0;
4956 }
4957
4958 /* Process the partial specialization DECL. */
4959
4960 static tree
4961 process_partial_specialization (tree decl)
4962 {
4963 tree type = TREE_TYPE (decl);
4964 tree tinfo = get_template_info (decl);
4965 tree maintmpl = TI_TEMPLATE (tinfo);
4966 tree specargs = TI_ARGS (tinfo);
4967 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4968 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4969 tree inner_parms;
4970 tree inst;
4971 int nargs = TREE_VEC_LENGTH (inner_args);
4972 int ntparms;
4973 int i;
4974 bool did_error_intro = false;
4975 struct template_parm_data tpd;
4976 struct template_parm_data tpd2;
4977
4978 gcc_assert (current_template_parms);
4979
4980 /* A concept cannot be specialized. */
4981 if (flag_concepts && variable_concept_p (maintmpl))
4982 {
4983 error ("specialization of variable concept %q#D", maintmpl);
4984 return error_mark_node;
4985 }
4986
4987 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4988 ntparms = TREE_VEC_LENGTH (inner_parms);
4989
4990 /* We check that each of the template parameters given in the
4991 partial specialization is used in the argument list to the
4992 specialization. For example:
4993
4994 template <class T> struct S;
4995 template <class T> struct S<T*>;
4996
4997 The second declaration is OK because `T*' uses the template
4998 parameter T, whereas
4999
5000 template <class T> struct S<int>;
5001
5002 is no good. Even trickier is:
5003
5004 template <class T>
5005 struct S1
5006 {
5007 template <class U>
5008 struct S2;
5009 template <class U>
5010 struct S2<T>;
5011 };
5012
5013 The S2<T> declaration is actually invalid; it is a
5014 full-specialization. Of course,
5015
5016 template <class U>
5017 struct S2<T (*)(U)>;
5018
5019 or some such would have been OK. */
5020 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5021 tpd.parms = XALLOCAVEC (int, ntparms);
5022 memset (tpd.parms, 0, sizeof (int) * ntparms);
5023
5024 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5025 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5026 for (i = 0; i < nargs; ++i)
5027 {
5028 tpd.current_arg = i;
5029 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5030 &mark_template_parm,
5031 &tpd,
5032 NULL,
5033 /*include_nondeduced_p=*/false);
5034 }
5035 for (i = 0; i < ntparms; ++i)
5036 if (tpd.parms[i] == 0)
5037 {
5038 /* One of the template parms was not used in a deduced context in the
5039 specialization. */
5040 if (!did_error_intro)
5041 {
5042 error ("template parameters not deducible in "
5043 "partial specialization:");
5044 did_error_intro = true;
5045 }
5046
5047 inform (input_location, " %qD",
5048 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5049 }
5050
5051 if (did_error_intro)
5052 return error_mark_node;
5053
5054 /* [temp.class.spec]
5055
5056 The argument list of the specialization shall not be identical to
5057 the implicit argument list of the primary template. */
5058 tree main_args
5059 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5060 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5061 && (!flag_concepts
5062 || !strictly_subsumes (current_template_constraints (),
5063 main_args, maintmpl)))
5064 {
5065 if (!flag_concepts)
5066 error ("partial specialization %q+D does not specialize "
5067 "any template arguments; to define the primary template, "
5068 "remove the template argument list", decl);
5069 else
5070 error ("partial specialization %q+D does not specialize any "
5071 "template arguments and is not more constrained than "
5072 "the primary template; to define the primary template, "
5073 "remove the template argument list", decl);
5074 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5075 }
5076
5077 /* A partial specialization that replaces multiple parameters of the
5078 primary template with a pack expansion is less specialized for those
5079 parameters. */
5080 if (nargs < DECL_NTPARMS (maintmpl))
5081 {
5082 error ("partial specialization is not more specialized than the "
5083 "primary template because it replaces multiple parameters "
5084 "with a pack expansion");
5085 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5086 /* Avoid crash in process_partial_specialization. */
5087 return decl;
5088 }
5089
5090 else if (nargs > DECL_NTPARMS (maintmpl))
5091 {
5092 error ("too many arguments for partial specialization %qT", type);
5093 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5094 /* Avoid crash below. */
5095 return decl;
5096 }
5097
5098 /* If we aren't in a dependent class, we can actually try deduction. */
5099 else if (tpd.level == 1
5100 /* FIXME we should be able to handle a partial specialization of a
5101 partial instantiation, but currently we can't (c++/41727). */
5102 && TMPL_ARGS_DEPTH (specargs) == 1
5103 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5104 {
5105 auto_diagnostic_group d;
5106 if (permerror (input_location, "partial specialization %qD is not "
5107 "more specialized than", decl))
5108 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5109 maintmpl);
5110 }
5111
5112 /* [temp.class.spec]
5113
5114 A partially specialized non-type argument expression shall not
5115 involve template parameters of the partial specialization except
5116 when the argument expression is a simple identifier.
5117
5118 The type of a template parameter corresponding to a specialized
5119 non-type argument shall not be dependent on a parameter of the
5120 specialization.
5121
5122 Also, we verify that pack expansions only occur at the
5123 end of the argument list. */
5124 tpd2.parms = 0;
5125 for (i = 0; i < nargs; ++i)
5126 {
5127 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5128 tree arg = TREE_VEC_ELT (inner_args, i);
5129 tree packed_args = NULL_TREE;
5130 int j, len = 1;
5131
5132 if (ARGUMENT_PACK_P (arg))
5133 {
5134 /* Extract the arguments from the argument pack. We'll be
5135 iterating over these in the following loop. */
5136 packed_args = ARGUMENT_PACK_ARGS (arg);
5137 len = TREE_VEC_LENGTH (packed_args);
5138 }
5139
5140 for (j = 0; j < len; j++)
5141 {
5142 if (packed_args)
5143 /* Get the Jth argument in the parameter pack. */
5144 arg = TREE_VEC_ELT (packed_args, j);
5145
5146 if (PACK_EXPANSION_P (arg))
5147 {
5148 /* Pack expansions must come at the end of the
5149 argument list. */
5150 if ((packed_args && j < len - 1)
5151 || (!packed_args && i < nargs - 1))
5152 {
5153 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5154 error ("parameter pack argument %qE must be at the "
5155 "end of the template argument list", arg);
5156 else
5157 error ("parameter pack argument %qT must be at the "
5158 "end of the template argument list", arg);
5159 }
5160 }
5161
5162 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5163 /* We only care about the pattern. */
5164 arg = PACK_EXPANSION_PATTERN (arg);
5165
5166 if (/* These first two lines are the `non-type' bit. */
5167 !TYPE_P (arg)
5168 && TREE_CODE (arg) != TEMPLATE_DECL
5169 /* This next two lines are the `argument expression is not just a
5170 simple identifier' condition and also the `specialized
5171 non-type argument' bit. */
5172 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5173 && !((REFERENCE_REF_P (arg)
5174 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5175 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5176 {
5177 if ((!packed_args && tpd.arg_uses_template_parms[i])
5178 || (packed_args && uses_template_parms (arg)))
5179 error_at (cp_expr_loc_or_input_loc (arg),
5180 "template argument %qE involves template "
5181 "parameter(s)", arg);
5182 else
5183 {
5184 /* Look at the corresponding template parameter,
5185 marking which template parameters its type depends
5186 upon. */
5187 tree type = TREE_TYPE (parm);
5188
5189 if (!tpd2.parms)
5190 {
5191 /* We haven't yet initialized TPD2. Do so now. */
5192 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5193 /* The number of parameters here is the number in the
5194 main template, which, as checked in the assertion
5195 above, is NARGS. */
5196 tpd2.parms = XALLOCAVEC (int, nargs);
5197 tpd2.level =
5198 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5199 }
5200
5201 /* Mark the template parameters. But this time, we're
5202 looking for the template parameters of the main
5203 template, not in the specialization. */
5204 tpd2.current_arg = i;
5205 tpd2.arg_uses_template_parms[i] = 0;
5206 memset (tpd2.parms, 0, sizeof (int) * nargs);
5207 for_each_template_parm (type,
5208 &mark_template_parm,
5209 &tpd2,
5210 NULL,
5211 /*include_nondeduced_p=*/false);
5212
5213 if (tpd2.arg_uses_template_parms [i])
5214 {
5215 /* The type depended on some template parameters.
5216 If they are fully specialized in the
5217 specialization, that's OK. */
5218 int j;
5219 int count = 0;
5220 for (j = 0; j < nargs; ++j)
5221 if (tpd2.parms[j] != 0
5222 && tpd.arg_uses_template_parms [j])
5223 ++count;
5224 if (count != 0)
5225 error_n (input_location, count,
5226 "type %qT of template argument %qE depends "
5227 "on a template parameter",
5228 "type %qT of template argument %qE depends "
5229 "on template parameters",
5230 type,
5231 arg);
5232 }
5233 }
5234 }
5235 }
5236 }
5237
5238 /* We should only get here once. */
5239 if (TREE_CODE (decl) == TYPE_DECL)
5240 gcc_assert (!COMPLETE_TYPE_P (type));
5241
5242 // Build the template decl.
5243 tree tmpl = build_template_decl (decl, current_template_parms,
5244 DECL_MEMBER_TEMPLATE_P (maintmpl));
5245 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5246 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5247 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5248
5249 /* Give template template parms a DECL_CONTEXT of the template
5250 for which they are a parameter. */
5251 for (i = 0; i < ntparms; ++i)
5252 {
5253 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5254 if (TREE_CODE (parm) == TEMPLATE_DECL)
5255 DECL_CONTEXT (parm) = tmpl;
5256 }
5257
5258 if (VAR_P (decl))
5259 /* We didn't register this in check_explicit_specialization so we could
5260 wait until the constraints were set. */
5261 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5262 else
5263 associate_classtype_constraints (type);
5264
5265 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5266 = tree_cons (specargs, tmpl,
5267 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5268 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5269
5270 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5271 inst = TREE_CHAIN (inst))
5272 {
5273 tree instance = TREE_VALUE (inst);
5274 if (TYPE_P (instance)
5275 ? (COMPLETE_TYPE_P (instance)
5276 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5277 : DECL_TEMPLATE_INSTANTIATION (instance))
5278 {
5279 tree spec = most_specialized_partial_spec (instance, tf_none);
5280 tree inst_decl = (DECL_P (instance)
5281 ? instance : TYPE_NAME (instance));
5282 if (!spec)
5283 /* OK */;
5284 else if (spec == error_mark_node)
5285 permerror (input_location,
5286 "declaration of %qD ambiguates earlier template "
5287 "instantiation for %qD", decl, inst_decl);
5288 else if (TREE_VALUE (spec) == tmpl)
5289 permerror (input_location,
5290 "partial specialization of %qD after instantiation "
5291 "of %qD", decl, inst_decl);
5292 }
5293 }
5294
5295 return decl;
5296 }
5297
5298 /* PARM is a template parameter of some form; return the corresponding
5299 TEMPLATE_PARM_INDEX. */
5300
5301 static tree
5302 get_template_parm_index (tree parm)
5303 {
5304 if (TREE_CODE (parm) == PARM_DECL
5305 || TREE_CODE (parm) == CONST_DECL)
5306 parm = DECL_INITIAL (parm);
5307 else if (TREE_CODE (parm) == TYPE_DECL
5308 || TREE_CODE (parm) == TEMPLATE_DECL)
5309 parm = TREE_TYPE (parm);
5310 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5311 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5312 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5313 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5314 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5315 return parm;
5316 }
5317
5318 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5319 parameter packs used by the template parameter PARM. */
5320
5321 static void
5322 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5323 {
5324 /* A type parm can't refer to another parm. */
5325 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5326 return;
5327 else if (TREE_CODE (parm) == PARM_DECL)
5328 {
5329 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5330 ppd, ppd->visited);
5331 return;
5332 }
5333
5334 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5335
5336 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5337 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5338 {
5339 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5340 if (template_parameter_pack_p (p))
5341 /* Any packs in the type are expanded by this parameter. */;
5342 else
5343 fixed_parameter_pack_p_1 (p, ppd);
5344 }
5345 }
5346
5347 /* PARM is a template parameter pack. Return any parameter packs used in
5348 its type or the type of any of its template parameters. If there are
5349 any such packs, it will be instantiated into a fixed template parameter
5350 list by partial instantiation rather than be fully deduced. */
5351
5352 tree
5353 fixed_parameter_pack_p (tree parm)
5354 {
5355 /* This can only be true in a member template. */
5356 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5357 return NULL_TREE;
5358 /* This can only be true for a parameter pack. */
5359 if (!template_parameter_pack_p (parm))
5360 return NULL_TREE;
5361 /* A type parm can't refer to another parm. */
5362 if (TREE_CODE (parm) == TYPE_DECL)
5363 return NULL_TREE;
5364
5365 tree parameter_packs = NULL_TREE;
5366 struct find_parameter_pack_data ppd;
5367 ppd.parameter_packs = &parameter_packs;
5368 ppd.visited = new hash_set<tree>;
5369 ppd.type_pack_expansion_p = false;
5370
5371 fixed_parameter_pack_p_1 (parm, &ppd);
5372
5373 delete ppd.visited;
5374 return parameter_packs;
5375 }
5376
5377 /* Check that a template declaration's use of default arguments and
5378 parameter packs is not invalid. Here, PARMS are the template
5379 parameters. IS_PRIMARY is true if DECL is the thing declared by
5380 a primary template. IS_PARTIAL is true if DECL is a partial
5381 specialization.
5382
5383 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5384 function template declaration or a friend class template
5385 declaration. In the function case, 1 indicates a declaration, 2
5386 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5387 emitted for extraneous default arguments.
5388
5389 Returns TRUE if there were no errors found, FALSE otherwise. */
5390
5391 bool
5392 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5393 bool is_partial, int is_friend_decl)
5394 {
5395 const char *msg;
5396 int last_level_to_check;
5397 tree parm_level;
5398 bool no_errors = true;
5399
5400 /* [temp.param]
5401
5402 A default template-argument shall not be specified in a
5403 function template declaration or a function template definition, nor
5404 in the template-parameter-list of the definition of a member of a
5405 class template. */
5406
5407 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5408 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5409 /* You can't have a function template declaration in a local
5410 scope, nor you can you define a member of a class template in a
5411 local scope. */
5412 return true;
5413
5414 if ((TREE_CODE (decl) == TYPE_DECL
5415 && TREE_TYPE (decl)
5416 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5417 || (TREE_CODE (decl) == FUNCTION_DECL
5418 && LAMBDA_FUNCTION_P (decl)))
5419 /* A lambda doesn't have an explicit declaration; don't complain
5420 about the parms of the enclosing class. */
5421 return true;
5422
5423 if (current_class_type
5424 && !TYPE_BEING_DEFINED (current_class_type)
5425 && DECL_LANG_SPECIFIC (decl)
5426 && DECL_DECLARES_FUNCTION_P (decl)
5427 /* If this is either a friend defined in the scope of the class
5428 or a member function. */
5429 && (DECL_FUNCTION_MEMBER_P (decl)
5430 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5431 : DECL_FRIEND_CONTEXT (decl)
5432 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5433 : false)
5434 /* And, if it was a member function, it really was defined in
5435 the scope of the class. */
5436 && (!DECL_FUNCTION_MEMBER_P (decl)
5437 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5438 /* We already checked these parameters when the template was
5439 declared, so there's no need to do it again now. This function
5440 was defined in class scope, but we're processing its body now
5441 that the class is complete. */
5442 return true;
5443
5444 /* Core issue 226 (C++0x only): the following only applies to class
5445 templates. */
5446 if (is_primary
5447 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5448 {
5449 /* [temp.param]
5450
5451 If a template-parameter has a default template-argument, all
5452 subsequent template-parameters shall have a default
5453 template-argument supplied. */
5454 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5455 {
5456 tree inner_parms = TREE_VALUE (parm_level);
5457 int ntparms = TREE_VEC_LENGTH (inner_parms);
5458 int seen_def_arg_p = 0;
5459 int i;
5460
5461 for (i = 0; i < ntparms; ++i)
5462 {
5463 tree parm = TREE_VEC_ELT (inner_parms, i);
5464
5465 if (parm == error_mark_node)
5466 continue;
5467
5468 if (TREE_PURPOSE (parm))
5469 seen_def_arg_p = 1;
5470 else if (seen_def_arg_p
5471 && !template_parameter_pack_p (TREE_VALUE (parm)))
5472 {
5473 error ("no default argument for %qD", TREE_VALUE (parm));
5474 /* For better subsequent error-recovery, we indicate that
5475 there should have been a default argument. */
5476 TREE_PURPOSE (parm) = error_mark_node;
5477 no_errors = false;
5478 }
5479 else if (!is_partial
5480 && !is_friend_decl
5481 /* Don't complain about an enclosing partial
5482 specialization. */
5483 && parm_level == parms
5484 && TREE_CODE (decl) == TYPE_DECL
5485 && i < ntparms - 1
5486 && template_parameter_pack_p (TREE_VALUE (parm))
5487 /* A fixed parameter pack will be partially
5488 instantiated into a fixed length list. */
5489 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5490 {
5491 /* A primary class template can only have one
5492 parameter pack, at the end of the template
5493 parameter list. */
5494
5495 error ("parameter pack %q+D must be at the end of the"
5496 " template parameter list", TREE_VALUE (parm));
5497
5498 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5499 = error_mark_node;
5500 no_errors = false;
5501 }
5502 }
5503 }
5504 }
5505
5506 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5507 || is_partial
5508 || !is_primary
5509 || is_friend_decl)
5510 /* For an ordinary class template, default template arguments are
5511 allowed at the innermost level, e.g.:
5512 template <class T = int>
5513 struct S {};
5514 but, in a partial specialization, they're not allowed even
5515 there, as we have in [temp.class.spec]:
5516
5517 The template parameter list of a specialization shall not
5518 contain default template argument values.
5519
5520 So, for a partial specialization, or for a function template
5521 (in C++98/C++03), we look at all of them. */
5522 ;
5523 else
5524 /* But, for a primary class template that is not a partial
5525 specialization we look at all template parameters except the
5526 innermost ones. */
5527 parms = TREE_CHAIN (parms);
5528
5529 /* Figure out what error message to issue. */
5530 if (is_friend_decl == 2)
5531 msg = G_("default template arguments may not be used in function template "
5532 "friend re-declaration");
5533 else if (is_friend_decl)
5534 msg = G_("default template arguments may not be used in template "
5535 "friend declarations");
5536 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5537 msg = G_("default template arguments may not be used in function templates "
5538 "without %<-std=c++11%> or %<-std=gnu++11%>");
5539 else if (is_partial)
5540 msg = G_("default template arguments may not be used in "
5541 "partial specializations");
5542 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5543 msg = G_("default argument for template parameter for class enclosing %qD");
5544 else
5545 /* Per [temp.param]/9, "A default template-argument shall not be
5546 specified in the template-parameter-lists of the definition of
5547 a member of a class template that appears outside of the member's
5548 class.", thus if we aren't handling a member of a class template
5549 there is no need to examine the parameters. */
5550 return true;
5551
5552 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5553 /* If we're inside a class definition, there's no need to
5554 examine the parameters to the class itself. On the one
5555 hand, they will be checked when the class is defined, and,
5556 on the other, default arguments are valid in things like:
5557 template <class T = double>
5558 struct S { template <class U> void f(U); };
5559 Here the default argument for `S' has no bearing on the
5560 declaration of `f'. */
5561 last_level_to_check = template_class_depth (current_class_type) + 1;
5562 else
5563 /* Check everything. */
5564 last_level_to_check = 0;
5565
5566 for (parm_level = parms;
5567 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5568 parm_level = TREE_CHAIN (parm_level))
5569 {
5570 tree inner_parms = TREE_VALUE (parm_level);
5571 int i;
5572 int ntparms;
5573
5574 ntparms = TREE_VEC_LENGTH (inner_parms);
5575 for (i = 0; i < ntparms; ++i)
5576 {
5577 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5578 continue;
5579
5580 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5581 {
5582 if (msg)
5583 {
5584 no_errors = false;
5585 if (is_friend_decl == 2)
5586 return no_errors;
5587
5588 error (msg, decl);
5589 msg = 0;
5590 }
5591
5592 /* Clear out the default argument so that we are not
5593 confused later. */
5594 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5595 }
5596 }
5597
5598 /* At this point, if we're still interested in issuing messages,
5599 they must apply to classes surrounding the object declared. */
5600 if (msg)
5601 msg = G_("default argument for template parameter for class "
5602 "enclosing %qD");
5603 }
5604
5605 return no_errors;
5606 }
5607
5608 /* Worker for push_template_decl_real, called via
5609 for_each_template_parm. DATA is really an int, indicating the
5610 level of the parameters we are interested in. If T is a template
5611 parameter of that level, return nonzero. */
5612
5613 static int
5614 template_parm_this_level_p (tree t, void* data)
5615 {
5616 int this_level = *(int *)data;
5617 int level;
5618
5619 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5620 level = TEMPLATE_PARM_LEVEL (t);
5621 else
5622 level = TEMPLATE_TYPE_LEVEL (t);
5623 return level == this_level;
5624 }
5625
5626 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5627 DATA is really an int, indicating the innermost outer level of parameters.
5628 If T is a template parameter of that level or further out, return
5629 nonzero. */
5630
5631 static int
5632 template_parm_outer_level (tree t, void *data)
5633 {
5634 int this_level = *(int *)data;
5635 int level;
5636
5637 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5638 level = TEMPLATE_PARM_LEVEL (t);
5639 else
5640 level = TEMPLATE_TYPE_LEVEL (t);
5641 return level <= this_level;
5642 }
5643
5644 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5645 parameters given by current_template_args, or reuses a
5646 previously existing one, if appropriate. Returns the DECL, or an
5647 equivalent one, if it is replaced via a call to duplicate_decls.
5648
5649 If IS_FRIEND is true, DECL is a friend declaration. */
5650
5651 tree
5652 push_template_decl_real (tree decl, bool is_friend)
5653 {
5654 tree tmpl;
5655 tree args;
5656 tree info;
5657 tree ctx;
5658 bool is_primary;
5659 bool is_partial;
5660 int new_template_p = 0;
5661 /* True if the template is a member template, in the sense of
5662 [temp.mem]. */
5663 bool member_template_p = false;
5664
5665 if (decl == error_mark_node || !current_template_parms)
5666 return error_mark_node;
5667
5668 /* See if this is a partial specialization. */
5669 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5670 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5671 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5672 || (VAR_P (decl)
5673 && DECL_LANG_SPECIFIC (decl)
5674 && DECL_TEMPLATE_SPECIALIZATION (decl)
5675 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5676
5677 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5678 is_friend = true;
5679
5680 if (is_friend)
5681 /* For a friend, we want the context of the friend, not
5682 the type of which it is a friend. */
5683 ctx = CP_DECL_CONTEXT (decl);
5684 else if (CP_DECL_CONTEXT (decl)
5685 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5686 /* In the case of a virtual function, we want the class in which
5687 it is defined. */
5688 ctx = CP_DECL_CONTEXT (decl);
5689 else
5690 /* Otherwise, if we're currently defining some class, the DECL
5691 is assumed to be a member of the class. */
5692 ctx = current_scope ();
5693
5694 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5695 ctx = NULL_TREE;
5696
5697 if (!DECL_CONTEXT (decl))
5698 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5699
5700 /* See if this is a primary template. */
5701 if (is_friend && ctx
5702 && uses_template_parms_level (ctx, processing_template_decl))
5703 /* A friend template that specifies a class context, i.e.
5704 template <typename T> friend void A<T>::f();
5705 is not primary. */
5706 is_primary = false;
5707 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5708 is_primary = false;
5709 else
5710 is_primary = template_parm_scope_p ();
5711
5712 if (is_primary)
5713 {
5714 warning (OPT_Wtemplates, "template %qD declared", decl);
5715
5716 if (DECL_CLASS_SCOPE_P (decl))
5717 member_template_p = true;
5718 if (TREE_CODE (decl) == TYPE_DECL
5719 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5720 {
5721 error ("template class without a name");
5722 return error_mark_node;
5723 }
5724 else if (TREE_CODE (decl) == FUNCTION_DECL)
5725 {
5726 if (member_template_p)
5727 {
5728 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5729 error ("member template %qD may not have virt-specifiers", decl);
5730 }
5731 if (DECL_DESTRUCTOR_P (decl))
5732 {
5733 /* [temp.mem]
5734
5735 A destructor shall not be a member template. */
5736 error_at (DECL_SOURCE_LOCATION (decl),
5737 "destructor %qD declared as member template", decl);
5738 return error_mark_node;
5739 }
5740 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5741 && (!prototype_p (TREE_TYPE (decl))
5742 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5743 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5744 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5745 == void_list_node)))
5746 {
5747 /* [basic.stc.dynamic.allocation]
5748
5749 An allocation function can be a function
5750 template. ... Template allocation functions shall
5751 have two or more parameters. */
5752 error ("invalid template declaration of %qD", decl);
5753 return error_mark_node;
5754 }
5755 }
5756 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5757 && CLASS_TYPE_P (TREE_TYPE (decl)))
5758 {
5759 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5760 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5761 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5762 {
5763 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5764 if (TREE_CODE (t) == TYPE_DECL)
5765 t = TREE_TYPE (t);
5766 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5767 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5768 }
5769 }
5770 else if (TREE_CODE (decl) == TYPE_DECL
5771 && TYPE_DECL_ALIAS_P (decl))
5772 /* alias-declaration */
5773 gcc_assert (!DECL_ARTIFICIAL (decl));
5774 else if (VAR_P (decl))
5775 /* C++14 variable template. */;
5776 else if (TREE_CODE (decl) == CONCEPT_DECL)
5777 /* C++20 concept definitions. */;
5778 else
5779 {
5780 error ("template declaration of %q#D", decl);
5781 return error_mark_node;
5782 }
5783 }
5784
5785 /* Check to see that the rules regarding the use of default
5786 arguments are not being violated. We check args for a friend
5787 functions when we know whether it's a definition, introducing
5788 declaration or re-declaration. */
5789 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5790 check_default_tmpl_args (decl, current_template_parms,
5791 is_primary, is_partial, is_friend);
5792
5793 /* Ensure that there are no parameter packs in the type of this
5794 declaration that have not been expanded. */
5795 if (TREE_CODE (decl) == FUNCTION_DECL)
5796 {
5797 /* Check each of the arguments individually to see if there are
5798 any bare parameter packs. */
5799 tree type = TREE_TYPE (decl);
5800 tree arg = DECL_ARGUMENTS (decl);
5801 tree argtype = TYPE_ARG_TYPES (type);
5802
5803 while (arg && argtype)
5804 {
5805 if (!DECL_PACK_P (arg)
5806 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5807 {
5808 /* This is a PARM_DECL that contains unexpanded parameter
5809 packs. We have already complained about this in the
5810 check_for_bare_parameter_packs call, so just replace
5811 these types with ERROR_MARK_NODE. */
5812 TREE_TYPE (arg) = error_mark_node;
5813 TREE_VALUE (argtype) = error_mark_node;
5814 }
5815
5816 arg = DECL_CHAIN (arg);
5817 argtype = TREE_CHAIN (argtype);
5818 }
5819
5820 /* Check for bare parameter packs in the return type and the
5821 exception specifiers. */
5822 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5823 /* Errors were already issued, set return type to int
5824 as the frontend doesn't expect error_mark_node as
5825 the return type. */
5826 TREE_TYPE (type) = integer_type_node;
5827 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5828 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5829 }
5830 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5831 ? DECL_ORIGINAL_TYPE (decl)
5832 : TREE_TYPE (decl)))
5833 {
5834 TREE_TYPE (decl) = error_mark_node;
5835 return error_mark_node;
5836 }
5837
5838 if (is_partial)
5839 return process_partial_specialization (decl);
5840
5841 args = current_template_args ();
5842
5843 if (!ctx
5844 || TREE_CODE (ctx) == FUNCTION_DECL
5845 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5846 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5847 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5848 {
5849 if (DECL_LANG_SPECIFIC (decl)
5850 && DECL_TEMPLATE_INFO (decl)
5851 && DECL_TI_TEMPLATE (decl))
5852 tmpl = DECL_TI_TEMPLATE (decl);
5853 /* If DECL is a TYPE_DECL for a class-template, then there won't
5854 be DECL_LANG_SPECIFIC. The information equivalent to
5855 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5856 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5857 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5858 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5859 {
5860 /* Since a template declaration already existed for this
5861 class-type, we must be redeclaring it here. Make sure
5862 that the redeclaration is valid. */
5863 redeclare_class_template (TREE_TYPE (decl),
5864 current_template_parms,
5865 current_template_constraints ());
5866 /* We don't need to create a new TEMPLATE_DECL; just use the
5867 one we already had. */
5868 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5869 }
5870 else
5871 {
5872 tmpl = build_template_decl (decl, current_template_parms,
5873 member_template_p);
5874 new_template_p = 1;
5875
5876 if (DECL_LANG_SPECIFIC (decl)
5877 && DECL_TEMPLATE_SPECIALIZATION (decl))
5878 {
5879 /* A specialization of a member template of a template
5880 class. */
5881 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5882 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5883 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5884 }
5885 }
5886 }
5887 else
5888 {
5889 tree a, t, current, parms;
5890 int i;
5891 tree tinfo = get_template_info (decl);
5892
5893 if (!tinfo)
5894 {
5895 error ("template definition of non-template %q#D", decl);
5896 return error_mark_node;
5897 }
5898
5899 tmpl = TI_TEMPLATE (tinfo);
5900
5901 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5902 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5903 && DECL_TEMPLATE_SPECIALIZATION (decl)
5904 && DECL_MEMBER_TEMPLATE_P (tmpl))
5905 {
5906 tree new_tmpl;
5907
5908 /* The declaration is a specialization of a member
5909 template, declared outside the class. Therefore, the
5910 innermost template arguments will be NULL, so we
5911 replace them with the arguments determined by the
5912 earlier call to check_explicit_specialization. */
5913 args = DECL_TI_ARGS (decl);
5914
5915 new_tmpl
5916 = build_template_decl (decl, current_template_parms,
5917 member_template_p);
5918 DECL_TI_TEMPLATE (decl) = new_tmpl;
5919 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5920 DECL_TEMPLATE_INFO (new_tmpl)
5921 = build_template_info (tmpl, args);
5922
5923 register_specialization (new_tmpl,
5924 most_general_template (tmpl),
5925 args,
5926 is_friend, 0);
5927 return decl;
5928 }
5929
5930 /* Make sure the template headers we got make sense. */
5931
5932 parms = DECL_TEMPLATE_PARMS (tmpl);
5933 i = TMPL_PARMS_DEPTH (parms);
5934 if (TMPL_ARGS_DEPTH (args) != i)
5935 {
5936 error ("expected %d levels of template parms for %q#D, got %d",
5937 i, decl, TMPL_ARGS_DEPTH (args));
5938 DECL_INTERFACE_KNOWN (decl) = 1;
5939 return error_mark_node;
5940 }
5941 else
5942 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5943 {
5944 a = TMPL_ARGS_LEVEL (args, i);
5945 t = INNERMOST_TEMPLATE_PARMS (parms);
5946
5947 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5948 {
5949 if (current == decl)
5950 error ("got %d template parameters for %q#D",
5951 TREE_VEC_LENGTH (a), decl);
5952 else
5953 error ("got %d template parameters for %q#T",
5954 TREE_VEC_LENGTH (a), current);
5955 error (" but %d required", TREE_VEC_LENGTH (t));
5956 /* Avoid crash in import_export_decl. */
5957 DECL_INTERFACE_KNOWN (decl) = 1;
5958 return error_mark_node;
5959 }
5960
5961 if (current == decl)
5962 current = ctx;
5963 else if (current == NULL_TREE)
5964 /* Can happen in erroneous input. */
5965 break;
5966 else
5967 current = get_containing_scope (current);
5968 }
5969
5970 /* Check that the parms are used in the appropriate qualifying scopes
5971 in the declarator. */
5972 if (!comp_template_args
5973 (TI_ARGS (tinfo),
5974 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5975 {
5976 error ("template arguments to %qD do not match original "
5977 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5978 if (!uses_template_parms (TI_ARGS (tinfo)))
5979 inform (input_location, "use %<template<>%> for"
5980 " an explicit specialization");
5981 /* Avoid crash in import_export_decl. */
5982 DECL_INTERFACE_KNOWN (decl) = 1;
5983 return error_mark_node;
5984 }
5985 }
5986
5987 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl) == decl);
5988
5989 if (new_template_p)
5990 {
5991 /* Push template declarations for global functions and types.
5992 Note that we do not try to push a global template friend
5993 declared in a template class; such a thing may well depend on
5994 the template parameters of the class and we'll push it when
5995 instantiating the befriending class. */
5996 if (!ctx
5997 && !(is_friend && template_class_depth (current_class_type) > 0))
5998 {
5999 tmpl = pushdecl_namespace_level (tmpl, is_friend);
6000 if (tmpl == error_mark_node)
6001 return error_mark_node;
6002
6003 /* Hide template friend classes that haven't been declared yet. */
6004 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
6005 {
6006 DECL_ANTICIPATED (tmpl) = 1;
6007 DECL_FRIEND_P (tmpl) = 1;
6008 }
6009 }
6010 }
6011 else
6012 /* The type may have been completed, or (erroneously) changed. */
6013 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6014
6015 if (is_primary)
6016 {
6017 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6018
6019 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6020
6021 /* Give template template parms a DECL_CONTEXT of the template
6022 for which they are a parameter. */
6023 parms = INNERMOST_TEMPLATE_PARMS (parms);
6024 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6025 {
6026 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6027 if (TREE_CODE (parm) == TEMPLATE_DECL)
6028 DECL_CONTEXT (parm) = tmpl;
6029 }
6030
6031 if (TREE_CODE (decl) == TYPE_DECL
6032 && TYPE_DECL_ALIAS_P (decl))
6033 {
6034 if (tree constr
6035 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6036 {
6037 /* ??? Why don't we do this here for all templates? */
6038 constr = build_constraints (constr, NULL_TREE);
6039 set_constraints (decl, constr);
6040 }
6041 if (complex_alias_template_p (tmpl))
6042 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6043 }
6044 }
6045
6046 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6047 back to its most general template. If TMPL is a specialization,
6048 ARGS may only have the innermost set of arguments. Add the missing
6049 argument levels if necessary. */
6050 if (DECL_TEMPLATE_INFO (tmpl))
6051 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6052
6053 info = build_template_info (tmpl, args);
6054
6055 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6056 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6057 else
6058 {
6059 if (is_primary)
6060 retrofit_lang_decl (decl);
6061 if (DECL_LANG_SPECIFIC (decl))
6062 DECL_TEMPLATE_INFO (decl) = info;
6063 }
6064
6065 if (flag_implicit_templates
6066 && !is_friend
6067 && TREE_PUBLIC (decl)
6068 && VAR_OR_FUNCTION_DECL_P (decl))
6069 /* Set DECL_COMDAT on template instantiations; if we force
6070 them to be emitted by explicit instantiation,
6071 mark_needed will tell cgraph to do the right thing. */
6072 DECL_COMDAT (decl) = true;
6073
6074 return DECL_TEMPLATE_RESULT (tmpl);
6075 }
6076
6077 tree
6078 push_template_decl (tree decl)
6079 {
6080 return push_template_decl_real (decl, false);
6081 }
6082
6083 /* FN is an inheriting constructor that inherits from the constructor
6084 template INHERITED; turn FN into a constructor template with a matching
6085 template header. */
6086
6087 tree
6088 add_inherited_template_parms (tree fn, tree inherited)
6089 {
6090 tree inner_parms
6091 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6092 inner_parms = copy_node (inner_parms);
6093 tree parms
6094 = tree_cons (size_int (processing_template_decl + 1),
6095 inner_parms, current_template_parms);
6096 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6097 tree args = template_parms_to_args (parms);
6098 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6099 DECL_ARTIFICIAL (tmpl) = true;
6100 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6101 return tmpl;
6102 }
6103
6104 /* Called when a class template TYPE is redeclared with the indicated
6105 template PARMS, e.g.:
6106
6107 template <class T> struct S;
6108 template <class T> struct S {}; */
6109
6110 bool
6111 redeclare_class_template (tree type, tree parms, tree cons)
6112 {
6113 tree tmpl;
6114 tree tmpl_parms;
6115 int i;
6116
6117 if (!TYPE_TEMPLATE_INFO (type))
6118 {
6119 error ("%qT is not a template type", type);
6120 return false;
6121 }
6122
6123 tmpl = TYPE_TI_TEMPLATE (type);
6124 if (!PRIMARY_TEMPLATE_P (tmpl))
6125 /* The type is nested in some template class. Nothing to worry
6126 about here; there are no new template parameters for the nested
6127 type. */
6128 return true;
6129
6130 if (!parms)
6131 {
6132 error ("template specifiers not specified in declaration of %qD",
6133 tmpl);
6134 return false;
6135 }
6136
6137 parms = INNERMOST_TEMPLATE_PARMS (parms);
6138 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6139
6140 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6141 {
6142 error_n (input_location, TREE_VEC_LENGTH (parms),
6143 "redeclared with %d template parameter",
6144 "redeclared with %d template parameters",
6145 TREE_VEC_LENGTH (parms));
6146 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6147 "previous declaration %qD used %d template parameter",
6148 "previous declaration %qD used %d template parameters",
6149 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6150 return false;
6151 }
6152
6153 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6154 {
6155 tree tmpl_parm;
6156 tree parm;
6157 tree tmpl_default;
6158 tree parm_default;
6159
6160 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6161 || TREE_VEC_ELT (parms, i) == error_mark_node)
6162 continue;
6163
6164 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6165 if (error_operand_p (tmpl_parm))
6166 return false;
6167
6168 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6169 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6170 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6171
6172 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6173 TEMPLATE_DECL. */
6174 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6175 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6176 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6177 || (TREE_CODE (tmpl_parm) != PARM_DECL
6178 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6179 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6180 || (TREE_CODE (tmpl_parm) == PARM_DECL
6181 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6182 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6183 {
6184 auto_diagnostic_group d;
6185 error ("template parameter %q+#D", tmpl_parm);
6186 inform (input_location, "redeclared here as %q#D", parm);
6187 return false;
6188 }
6189
6190 /* The parameters can be declared to introduce different
6191 constraints. */
6192 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6193 tree p2 = TREE_VEC_ELT (parms, i);
6194 if (!template_parameter_constraints_equivalent_p (p1, p2))
6195 {
6196 auto_diagnostic_group d;
6197 error ("declaration of template parameter %q+#D with different "
6198 "constraints", parm);
6199 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6200 "original declaration appeared here");
6201 return false;
6202 }
6203
6204 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6205 {
6206 /* We have in [temp.param]:
6207
6208 A template-parameter may not be given default arguments
6209 by two different declarations in the same scope. */
6210 auto_diagnostic_group d;
6211 error_at (input_location, "redefinition of default argument for %q#D", parm);
6212 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6213 "original definition appeared here");
6214 return false;
6215 }
6216
6217 if (parm_default != NULL_TREE)
6218 /* Update the previous template parameters (which are the ones
6219 that will really count) with the new default value. */
6220 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6221 else if (tmpl_default != NULL_TREE)
6222 /* Update the new parameters, too; they'll be used as the
6223 parameters for any members. */
6224 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6225
6226 /* Give each template template parm in this redeclaration a
6227 DECL_CONTEXT of the template for which they are a parameter. */
6228 if (TREE_CODE (parm) == TEMPLATE_DECL)
6229 {
6230 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6231 DECL_CONTEXT (parm) = tmpl;
6232 }
6233
6234 if (TREE_CODE (parm) == TYPE_DECL)
6235 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6236 }
6237
6238 tree ci = get_constraints (tmpl);
6239 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6240 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6241
6242 /* Two classes with different constraints declare different entities. */
6243 if (!cp_tree_equal (req1, req2))
6244 {
6245 auto_diagnostic_group d;
6246 error_at (input_location, "redeclaration %q#D with different "
6247 "constraints", tmpl);
6248 inform (DECL_SOURCE_LOCATION (tmpl),
6249 "original declaration appeared here");
6250 return false;
6251 }
6252
6253 return true;
6254 }
6255
6256 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6257 to be used when the caller has already checked
6258 (processing_template_decl
6259 && !instantiation_dependent_expression_p (expr)
6260 && potential_constant_expression (expr))
6261 and cleared processing_template_decl. */
6262
6263 tree
6264 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6265 {
6266 return tsubst_copy_and_build (expr,
6267 /*args=*/NULL_TREE,
6268 complain,
6269 /*in_decl=*/NULL_TREE,
6270 /*function_p=*/false,
6271 /*integral_constant_expression_p=*/true);
6272 }
6273
6274 /* Simplify EXPR if it is a non-dependent expression. Returns the
6275 (possibly simplified) expression. */
6276
6277 tree
6278 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6279 {
6280 if (expr == NULL_TREE)
6281 return NULL_TREE;
6282
6283 /* If we're in a template, but EXPR isn't value dependent, simplify
6284 it. We're supposed to treat:
6285
6286 template <typename T> void f(T[1 + 1]);
6287 template <typename T> void f(T[2]);
6288
6289 as two declarations of the same function, for example. */
6290 if (processing_template_decl
6291 && is_nondependent_constant_expression (expr))
6292 {
6293 processing_template_decl_sentinel s;
6294 expr = instantiate_non_dependent_expr_internal (expr, complain);
6295 }
6296 return expr;
6297 }
6298
6299 tree
6300 instantiate_non_dependent_expr (tree expr)
6301 {
6302 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6303 }
6304
6305 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6306 an uninstantiated expression. */
6307
6308 tree
6309 instantiate_non_dependent_or_null (tree expr)
6310 {
6311 if (expr == NULL_TREE)
6312 return NULL_TREE;
6313 if (processing_template_decl)
6314 {
6315 if (!is_nondependent_constant_expression (expr))
6316 expr = NULL_TREE;
6317 else
6318 {
6319 processing_template_decl_sentinel s;
6320 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6321 }
6322 }
6323 return expr;
6324 }
6325
6326 /* True iff T is a specialization of a variable template. */
6327
6328 bool
6329 variable_template_specialization_p (tree t)
6330 {
6331 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6332 return false;
6333 tree tmpl = DECL_TI_TEMPLATE (t);
6334 return variable_template_p (tmpl);
6335 }
6336
6337 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6338 template declaration, or a TYPE_DECL for an alias declaration. */
6339
6340 bool
6341 alias_type_or_template_p (tree t)
6342 {
6343 if (t == NULL_TREE)
6344 return false;
6345 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6346 || (TYPE_P (t)
6347 && TYPE_NAME (t)
6348 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6349 || DECL_ALIAS_TEMPLATE_P (t));
6350 }
6351
6352 /* If T is a specialization of an alias template, return it; otherwise return
6353 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6354
6355 tree
6356 alias_template_specialization_p (const_tree t,
6357 bool transparent_typedefs)
6358 {
6359 if (!TYPE_P (t))
6360 return NULL_TREE;
6361
6362 /* It's an alias template specialization if it's an alias and its
6363 TYPE_NAME is a specialization of a primary template. */
6364 if (typedef_variant_p (t))
6365 {
6366 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6367 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6368 return CONST_CAST_TREE (t);
6369 if (transparent_typedefs)
6370 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6371 (TYPE_NAME (t)),
6372 transparent_typedefs);
6373 }
6374
6375 return NULL_TREE;
6376 }
6377
6378 /* An alias template is complex from a SFINAE perspective if a template-id
6379 using that alias can be ill-formed when the expansion is not, as with
6380 the void_t template. We determine this by checking whether the
6381 expansion for the alias template uses all its template parameters. */
6382
6383 struct uses_all_template_parms_data
6384 {
6385 int level;
6386 bool *seen;
6387 };
6388
6389 static int
6390 uses_all_template_parms_r (tree t, void *data_)
6391 {
6392 struct uses_all_template_parms_data &data
6393 = *(struct uses_all_template_parms_data*)data_;
6394 tree idx = get_template_parm_index (t);
6395
6396 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6397 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6398 return 0;
6399 }
6400
6401 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6402
6403 static int
6404 complex_pack_expansion_r (tree t, void *data_)
6405 {
6406 /* An alias template with a pack expansion that expands a pack from the
6407 enclosing class needs to be considered complex, to avoid confusion with
6408 the same pack being used as an argument to the alias's own template
6409 parameter (91966). */
6410 if (!PACK_EXPANSION_P (t))
6411 return 0;
6412 struct uses_all_template_parms_data &data
6413 = *(struct uses_all_template_parms_data*)data_;
6414 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6415 pack = TREE_CHAIN (pack))
6416 {
6417 tree parm_pack = TREE_VALUE (pack);
6418 if (!TEMPLATE_PARM_P (parm_pack))
6419 continue;
6420 int idx, level;
6421 template_parm_level_and_index (parm_pack, &level, &idx);
6422 if (level < data.level)
6423 return 1;
6424 }
6425 return 0;
6426 }
6427
6428 static bool
6429 complex_alias_template_p (const_tree tmpl)
6430 {
6431 /* A renaming alias isn't complex. */
6432 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6433 return false;
6434
6435 /* Any other constrained alias is complex. */
6436 if (get_constraints (tmpl))
6437 return true;
6438
6439 struct uses_all_template_parms_data data;
6440 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6441 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6442 data.level = TMPL_PARMS_DEPTH (parms);
6443 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6444 data.seen = XALLOCAVEC (bool, len);
6445 for (int i = 0; i < len; ++i)
6446 data.seen[i] = false;
6447
6448 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6449 NULL, true, complex_pack_expansion_r))
6450 return true;
6451 for (int i = 0; i < len; ++i)
6452 if (!data.seen[i])
6453 return true;
6454 return false;
6455 }
6456
6457 /* If T is a specialization of a complex alias template with dependent
6458 template-arguments, return it; otherwise return NULL_TREE. If T is a
6459 typedef to such a specialization, return the specialization. */
6460
6461 tree
6462 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6463 {
6464 if (!TYPE_P (t) || !typedef_variant_p (t))
6465 return NULL_TREE;
6466
6467 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6468 if (tinfo
6469 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6470 && (any_dependent_template_arguments_p
6471 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6472 return CONST_CAST_TREE (t);
6473
6474 if (transparent_typedefs)
6475 {
6476 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6477 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6478 }
6479
6480 return NULL_TREE;
6481 }
6482
6483 /* Return the number of innermost template parameters in TMPL. */
6484
6485 static int
6486 num_innermost_template_parms (const_tree tmpl)
6487 {
6488 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6489 return TREE_VEC_LENGTH (parms);
6490 }
6491
6492 /* Return either TMPL or another template that it is equivalent to under DR
6493 1286: An alias that just changes the name of a template is equivalent to
6494 the other template. */
6495
6496 static tree
6497 get_underlying_template (tree tmpl)
6498 {
6499 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6500 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6501 {
6502 /* Determine if the alias is equivalent to an underlying template. */
6503 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6504 /* The underlying type may have been ill-formed. Don't proceed. */
6505 if (!orig_type)
6506 break;
6507 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6508 if (!tinfo)
6509 break;
6510
6511 tree underlying = TI_TEMPLATE (tinfo);
6512 if (!PRIMARY_TEMPLATE_P (underlying)
6513 || (num_innermost_template_parms (tmpl)
6514 != num_innermost_template_parms (underlying)))
6515 break;
6516
6517 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6518 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6519 break;
6520
6521 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6522 it's appropriate to treat a less-constrained alias as equivalent. */
6523 if (!at_least_as_constrained (underlying, tmpl))
6524 break;
6525
6526 /* Alias is equivalent. Strip it and repeat. */
6527 tmpl = underlying;
6528 }
6529
6530 return tmpl;
6531 }
6532
6533 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6534 must be a reference-to-function or a pointer-to-function type, as specified
6535 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6536 and check that the resulting function has external linkage. */
6537
6538 static tree
6539 convert_nontype_argument_function (tree type, tree expr,
6540 tsubst_flags_t complain)
6541 {
6542 tree fns = expr;
6543 tree fn, fn_no_ptr;
6544 linkage_kind linkage;
6545
6546 fn = instantiate_type (type, fns, tf_none);
6547 if (fn == error_mark_node)
6548 return error_mark_node;
6549
6550 if (value_dependent_expression_p (fn))
6551 goto accept;
6552
6553 fn_no_ptr = strip_fnptr_conv (fn);
6554 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6555 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6556 if (BASELINK_P (fn_no_ptr))
6557 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6558
6559 /* [temp.arg.nontype]/1
6560
6561 A template-argument for a non-type, non-template template-parameter
6562 shall be one of:
6563 [...]
6564 -- the address of an object or function with external [C++11: or
6565 internal] linkage. */
6566
6567 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6568 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6569 {
6570 if (complain & tf_error)
6571 {
6572 location_t loc = cp_expr_loc_or_input_loc (expr);
6573 error_at (loc, "%qE is not a valid template argument for type %qT",
6574 expr, type);
6575 if (TYPE_PTR_P (type))
6576 inform (loc, "it must be the address of a function "
6577 "with external linkage");
6578 else
6579 inform (loc, "it must be the name of a function with "
6580 "external linkage");
6581 }
6582 return NULL_TREE;
6583 }
6584
6585 linkage = decl_linkage (fn_no_ptr);
6586 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6587 {
6588 if (complain & tf_error)
6589 {
6590 location_t loc = cp_expr_loc_or_input_loc (expr);
6591 if (cxx_dialect >= cxx11)
6592 error_at (loc, "%qE is not a valid template argument for type "
6593 "%qT because %qD has no linkage",
6594 expr, type, fn_no_ptr);
6595 else
6596 error_at (loc, "%qE is not a valid template argument for type "
6597 "%qT because %qD does not have external linkage",
6598 expr, type, fn_no_ptr);
6599 }
6600 return NULL_TREE;
6601 }
6602
6603 accept:
6604 if (TYPE_REF_P (type))
6605 {
6606 if (REFERENCE_REF_P (fn))
6607 fn = TREE_OPERAND (fn, 0);
6608 else
6609 fn = build_address (fn);
6610 }
6611 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6612 fn = build_nop (type, fn);
6613
6614 return fn;
6615 }
6616
6617 /* Subroutine of convert_nontype_argument.
6618 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6619 Emit an error otherwise. */
6620
6621 static bool
6622 check_valid_ptrmem_cst_expr (tree type, tree expr,
6623 tsubst_flags_t complain)
6624 {
6625 tree orig_expr = expr;
6626 STRIP_NOPS (expr);
6627 if (null_ptr_cst_p (expr))
6628 return true;
6629 if (TREE_CODE (expr) == PTRMEM_CST
6630 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6631 PTRMEM_CST_CLASS (expr)))
6632 return true;
6633 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6634 return true;
6635 if (processing_template_decl
6636 && TREE_CODE (expr) == ADDR_EXPR
6637 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6638 return true;
6639 if (complain & tf_error)
6640 {
6641 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6642 error_at (loc, "%qE is not a valid template argument for type %qT",
6643 orig_expr, type);
6644 if (TREE_CODE (expr) != PTRMEM_CST)
6645 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6646 else
6647 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6648 }
6649 return false;
6650 }
6651
6652 /* Returns TRUE iff the address of OP is value-dependent.
6653
6654 14.6.2.4 [temp.dep.temp]:
6655 A non-integral non-type template-argument is dependent if its type is
6656 dependent or it has either of the following forms
6657 qualified-id
6658 & qualified-id
6659 and contains a nested-name-specifier which specifies a class-name that
6660 names a dependent type.
6661
6662 We generalize this to just say that the address of a member of a
6663 dependent class is value-dependent; the above doesn't cover the
6664 address of a static data member named with an unqualified-id. */
6665
6666 static bool
6667 has_value_dependent_address (tree op)
6668 {
6669 STRIP_ANY_LOCATION_WRAPPER (op);
6670
6671 /* We could use get_inner_reference here, but there's no need;
6672 this is only relevant for template non-type arguments, which
6673 can only be expressed as &id-expression. */
6674 if (DECL_P (op))
6675 {
6676 tree ctx = CP_DECL_CONTEXT (op);
6677 if (TYPE_P (ctx) && dependent_type_p (ctx))
6678 return true;
6679 }
6680
6681 return false;
6682 }
6683
6684 /* The next set of functions are used for providing helpful explanatory
6685 diagnostics for failed overload resolution. Their messages should be
6686 indented by two spaces for consistency with the messages in
6687 call.c */
6688
6689 static int
6690 unify_success (bool /*explain_p*/)
6691 {
6692 return 0;
6693 }
6694
6695 /* Other failure functions should call this one, to provide a single function
6696 for setting a breakpoint on. */
6697
6698 static int
6699 unify_invalid (bool /*explain_p*/)
6700 {
6701 return 1;
6702 }
6703
6704 static int
6705 unify_parameter_deduction_failure (bool explain_p, tree parm)
6706 {
6707 if (explain_p)
6708 inform (input_location,
6709 " couldn%'t deduce template parameter %qD", parm);
6710 return unify_invalid (explain_p);
6711 }
6712
6713 static int
6714 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6715 {
6716 if (explain_p)
6717 inform (input_location,
6718 " types %qT and %qT have incompatible cv-qualifiers",
6719 parm, arg);
6720 return unify_invalid (explain_p);
6721 }
6722
6723 static int
6724 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6725 {
6726 if (explain_p)
6727 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6728 return unify_invalid (explain_p);
6729 }
6730
6731 static int
6732 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6733 {
6734 if (explain_p)
6735 inform (input_location,
6736 " template parameter %qD is not a parameter pack, but "
6737 "argument %qD is",
6738 parm, arg);
6739 return unify_invalid (explain_p);
6740 }
6741
6742 static int
6743 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6744 {
6745 if (explain_p)
6746 inform (input_location,
6747 " template argument %qE does not match "
6748 "pointer-to-member constant %qE",
6749 arg, parm);
6750 return unify_invalid (explain_p);
6751 }
6752
6753 static int
6754 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6755 {
6756 if (explain_p)
6757 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6758 return unify_invalid (explain_p);
6759 }
6760
6761 static int
6762 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6763 {
6764 if (explain_p)
6765 inform (input_location,
6766 " inconsistent parameter pack deduction with %qT and %qT",
6767 old_arg, new_arg);
6768 return unify_invalid (explain_p);
6769 }
6770
6771 static int
6772 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6773 {
6774 if (explain_p)
6775 {
6776 if (TYPE_P (parm))
6777 inform (input_location,
6778 " deduced conflicting types for parameter %qT (%qT and %qT)",
6779 parm, first, second);
6780 else
6781 inform (input_location,
6782 " deduced conflicting values for non-type parameter "
6783 "%qE (%qE and %qE)", parm, first, second);
6784 }
6785 return unify_invalid (explain_p);
6786 }
6787
6788 static int
6789 unify_vla_arg (bool explain_p, tree arg)
6790 {
6791 if (explain_p)
6792 inform (input_location,
6793 " variable-sized array type %qT is not "
6794 "a valid template argument",
6795 arg);
6796 return unify_invalid (explain_p);
6797 }
6798
6799 static int
6800 unify_method_type_error (bool explain_p, tree arg)
6801 {
6802 if (explain_p)
6803 inform (input_location,
6804 " member function type %qT is not a valid template argument",
6805 arg);
6806 return unify_invalid (explain_p);
6807 }
6808
6809 static int
6810 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6811 {
6812 if (explain_p)
6813 {
6814 if (least_p)
6815 inform_n (input_location, wanted,
6816 " candidate expects at least %d argument, %d provided",
6817 " candidate expects at least %d arguments, %d provided",
6818 wanted, have);
6819 else
6820 inform_n (input_location, wanted,
6821 " candidate expects %d argument, %d provided",
6822 " candidate expects %d arguments, %d provided",
6823 wanted, have);
6824 }
6825 return unify_invalid (explain_p);
6826 }
6827
6828 static int
6829 unify_too_many_arguments (bool explain_p, int have, int wanted)
6830 {
6831 return unify_arity (explain_p, have, wanted);
6832 }
6833
6834 static int
6835 unify_too_few_arguments (bool explain_p, int have, int wanted,
6836 bool least_p = false)
6837 {
6838 return unify_arity (explain_p, have, wanted, least_p);
6839 }
6840
6841 static int
6842 unify_arg_conversion (bool explain_p, tree to_type,
6843 tree from_type, tree arg)
6844 {
6845 if (explain_p)
6846 inform (cp_expr_loc_or_input_loc (arg),
6847 " cannot convert %qE (type %qT) to type %qT",
6848 arg, from_type, to_type);
6849 return unify_invalid (explain_p);
6850 }
6851
6852 static int
6853 unify_no_common_base (bool explain_p, enum template_base_result r,
6854 tree parm, tree arg)
6855 {
6856 if (explain_p)
6857 switch (r)
6858 {
6859 case tbr_ambiguous_baseclass:
6860 inform (input_location, " %qT is an ambiguous base class of %qT",
6861 parm, arg);
6862 break;
6863 default:
6864 inform (input_location, " %qT is not derived from %qT", arg, parm);
6865 break;
6866 }
6867 return unify_invalid (explain_p);
6868 }
6869
6870 static int
6871 unify_inconsistent_template_template_parameters (bool explain_p)
6872 {
6873 if (explain_p)
6874 inform (input_location,
6875 " template parameters of a template template argument are "
6876 "inconsistent with other deduced template arguments");
6877 return unify_invalid (explain_p);
6878 }
6879
6880 static int
6881 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6882 {
6883 if (explain_p)
6884 inform (input_location,
6885 " cannot deduce a template for %qT from non-template type %qT",
6886 parm, arg);
6887 return unify_invalid (explain_p);
6888 }
6889
6890 static int
6891 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6892 {
6893 if (explain_p)
6894 inform (input_location,
6895 " template argument %qE does not match %qE", arg, parm);
6896 return unify_invalid (explain_p);
6897 }
6898
6899 /* True if T is a C++20 template parameter object to store the argument for a
6900 template parameter of class type. */
6901
6902 bool
6903 template_parm_object_p (const_tree t)
6904 {
6905 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6906 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6907 }
6908
6909 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6910 argument for TYPE, points to an unsuitable object.
6911
6912 Also adjust the type of the index in C++20 array subobject references. */
6913
6914 static bool
6915 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6916 {
6917 switch (TREE_CODE (expr))
6918 {
6919 CASE_CONVERT:
6920 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6921 complain);
6922
6923 case TARGET_EXPR:
6924 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6925 complain);
6926
6927 case CONSTRUCTOR:
6928 {
6929 unsigned i; tree elt;
6930 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6931 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6932 return true;
6933 }
6934 break;
6935
6936 case ADDR_EXPR:
6937 {
6938 tree decl = TREE_OPERAND (expr, 0);
6939
6940 if (cxx_dialect >= cxx20)
6941 while (TREE_CODE (decl) == COMPONENT_REF
6942 || TREE_CODE (decl) == ARRAY_REF)
6943 {
6944 tree &op = TREE_OPERAND (decl, 1);
6945 if (TREE_CODE (decl) == ARRAY_REF
6946 && TREE_CODE (op) == INTEGER_CST)
6947 /* Canonicalize array offsets to ptrdiff_t; how they were
6948 written doesn't matter for subobject identity. */
6949 op = fold_convert (ptrdiff_type_node, op);
6950 decl = TREE_OPERAND (decl, 0);
6951 }
6952
6953 if (!VAR_P (decl))
6954 {
6955 if (complain & tf_error)
6956 error_at (cp_expr_loc_or_input_loc (expr),
6957 "%qE is not a valid template argument of type %qT "
6958 "because %qE is not a variable", expr, type, decl);
6959 return true;
6960 }
6961 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6962 {
6963 if (complain & tf_error)
6964 error_at (cp_expr_loc_or_input_loc (expr),
6965 "%qE is not a valid template argument of type %qT "
6966 "in C++98 because %qD does not have external linkage",
6967 expr, type, decl);
6968 return true;
6969 }
6970 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6971 && decl_linkage (decl) == lk_none)
6972 {
6973 if (complain & tf_error)
6974 error_at (cp_expr_loc_or_input_loc (expr),
6975 "%qE is not a valid template argument of type %qT "
6976 "because %qD has no linkage", expr, type, decl);
6977 return true;
6978 }
6979 /* C++17: For a non-type template-parameter of reference or pointer
6980 type, the value of the constant expression shall not refer to (or
6981 for a pointer type, shall not be the address of):
6982 * a subobject (4.5),
6983 * a temporary object (15.2),
6984 * a string literal (5.13.5),
6985 * the result of a typeid expression (8.2.8), or
6986 * a predefined __func__ variable (11.4.1). */
6987 else if (DECL_ARTIFICIAL (decl))
6988 {
6989 if (complain & tf_error)
6990 error ("the address of %qD is not a valid template argument",
6991 decl);
6992 return true;
6993 }
6994 else if (cxx_dialect < cxx20
6995 && !(same_type_ignoring_top_level_qualifiers_p
6996 (strip_array_types (TREE_TYPE (type)),
6997 strip_array_types (TREE_TYPE (decl)))))
6998 {
6999 if (complain & tf_error)
7000 error ("the address of the %qT subobject of %qD is not a "
7001 "valid template argument", TREE_TYPE (type), decl);
7002 return true;
7003 }
7004 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7005 {
7006 if (complain & tf_error)
7007 error ("the address of %qD is not a valid template argument "
7008 "because it does not have static storage duration",
7009 decl);
7010 return true;
7011 }
7012 }
7013 break;
7014
7015 default:
7016 if (!INDIRECT_TYPE_P (type))
7017 /* We're only concerned about pointers and references here. */;
7018 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7019 /* Null pointer values are OK in C++11. */;
7020 else
7021 {
7022 if (VAR_P (expr))
7023 {
7024 if (complain & tf_error)
7025 error ("%qD is not a valid template argument "
7026 "because %qD is a variable, not the address of "
7027 "a variable", expr, expr);
7028 return true;
7029 }
7030 else
7031 {
7032 if (complain & tf_error)
7033 error ("%qE is not a valid template argument for %qT "
7034 "because it is not the address of a variable",
7035 expr, type);
7036 return true;
7037 }
7038 }
7039 }
7040 return false;
7041
7042 }
7043
7044 /* The template arguments corresponding to template parameter objects of types
7045 that contain pointers to members. */
7046
7047 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7048
7049 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7050 template argument EXPR. */
7051
7052 static tree
7053 get_template_parm_object (tree expr, tsubst_flags_t complain)
7054 {
7055 if (TREE_CODE (expr) == TARGET_EXPR)
7056 expr = TARGET_EXPR_INITIAL (expr);
7057
7058 if (!TREE_CONSTANT (expr))
7059 {
7060 if ((complain & tf_error)
7061 && require_rvalue_constant_expression (expr))
7062 cxx_constant_value (expr);
7063 return error_mark_node;
7064 }
7065 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7066 return error_mark_node;
7067
7068 tree name = mangle_template_parm_object (expr);
7069 tree decl = get_global_binding (name);
7070 if (decl)
7071 return decl;
7072
7073 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7074 decl = create_temporary_var (type);
7075 TREE_STATIC (decl) = true;
7076 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7077 TREE_READONLY (decl) = true;
7078 DECL_NAME (decl) = name;
7079 SET_DECL_ASSEMBLER_NAME (decl, name);
7080 DECL_CONTEXT (decl) = global_namespace;
7081 comdat_linkage (decl);
7082
7083 if (!zero_init_p (type))
7084 {
7085 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7086 lower_var_init before we're done mangling. So store the original
7087 value elsewhere. */
7088 tree copy = unshare_constructor (expr);
7089 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7090 }
7091
7092 pushdecl_top_level_and_finish (decl, expr);
7093
7094 return decl;
7095 }
7096
7097 /* Return the actual template argument corresponding to template parameter
7098 object VAR. */
7099
7100 tree
7101 tparm_object_argument (tree var)
7102 {
7103 if (zero_init_p (TREE_TYPE (var)))
7104 return DECL_INITIAL (var);
7105 return *(tparm_obj_values->get (var));
7106 }
7107
7108 /* Attempt to convert the non-type template parameter EXPR to the
7109 indicated TYPE. If the conversion is successful, return the
7110 converted value. If the conversion is unsuccessful, return
7111 NULL_TREE if we issued an error message, or error_mark_node if we
7112 did not. We issue error messages for out-and-out bad template
7113 parameters, but not simply because the conversion failed, since we
7114 might be just trying to do argument deduction. Both TYPE and EXPR
7115 must be non-dependent.
7116
7117 The conversion follows the special rules described in
7118 [temp.arg.nontype], and it is much more strict than an implicit
7119 conversion.
7120
7121 This function is called twice for each template argument (see
7122 lookup_template_class for a more accurate description of this
7123 problem). This means that we need to handle expressions which
7124 are not valid in a C++ source, but can be created from the
7125 first call (for instance, casts to perform conversions). These
7126 hacks can go away after we fix the double coercion problem. */
7127
7128 static tree
7129 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7130 {
7131 tree expr_type;
7132 location_t loc = cp_expr_loc_or_input_loc (expr);
7133
7134 /* Detect immediately string literals as invalid non-type argument.
7135 This special-case is not needed for correctness (we would easily
7136 catch this later), but only to provide better diagnostic for this
7137 common user mistake. As suggested by DR 100, we do not mention
7138 linkage issues in the diagnostic as this is not the point. */
7139 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7140 {
7141 if (complain & tf_error)
7142 error ("%qE is not a valid template argument for type %qT "
7143 "because string literals can never be used in this context",
7144 expr, type);
7145 return NULL_TREE;
7146 }
7147
7148 /* Add the ADDR_EXPR now for the benefit of
7149 value_dependent_expression_p. */
7150 if (TYPE_PTROBV_P (type)
7151 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7152 {
7153 expr = decay_conversion (expr, complain);
7154 if (expr == error_mark_node)
7155 return error_mark_node;
7156 }
7157
7158 /* If we are in a template, EXPR may be non-dependent, but still
7159 have a syntactic, rather than semantic, form. For example, EXPR
7160 might be a SCOPE_REF, rather than the VAR_DECL to which the
7161 SCOPE_REF refers. Preserving the qualifying scope is necessary
7162 so that access checking can be performed when the template is
7163 instantiated -- but here we need the resolved form so that we can
7164 convert the argument. */
7165 bool non_dep = false;
7166 if (TYPE_REF_OBJ_P (type)
7167 && has_value_dependent_address (expr))
7168 /* If we want the address and it's value-dependent, don't fold. */;
7169 else if (processing_template_decl
7170 && is_nondependent_constant_expression (expr))
7171 non_dep = true;
7172 if (error_operand_p (expr))
7173 return error_mark_node;
7174 expr_type = TREE_TYPE (expr);
7175
7176 /* If the argument is non-dependent, perform any conversions in
7177 non-dependent context as well. */
7178 processing_template_decl_sentinel s (non_dep);
7179 if (non_dep)
7180 expr = instantiate_non_dependent_expr_internal (expr, complain);
7181
7182 const bool val_dep_p = value_dependent_expression_p (expr);
7183 if (val_dep_p)
7184 expr = canonicalize_expr_argument (expr, complain);
7185
7186 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7187 to a non-type argument of "nullptr". */
7188 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7189 expr = fold_simple (convert (type, expr));
7190
7191 /* In C++11, integral or enumeration non-type template arguments can be
7192 arbitrary constant expressions. Pointer and pointer to
7193 member arguments can be general constant expressions that evaluate
7194 to a null value, but otherwise still need to be of a specific form. */
7195 if (cxx_dialect >= cxx11)
7196 {
7197 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7198 /* A PTRMEM_CST is already constant, and a valid template
7199 argument for a parameter of pointer to member type, we just want
7200 to leave it in that form rather than lower it to a
7201 CONSTRUCTOR. */;
7202 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7203 || cxx_dialect >= cxx17)
7204 {
7205 /* C++17: A template-argument for a non-type template-parameter shall
7206 be a converted constant expression (8.20) of the type of the
7207 template-parameter. */
7208 expr = build_converted_constant_expr (type, expr, complain);
7209 if (expr == error_mark_node)
7210 /* Make sure we return NULL_TREE only if we have really issued
7211 an error, as described above. */
7212 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7213 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7214 {
7215 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7216 return expr;
7217 }
7218 expr = maybe_constant_value (expr, NULL_TREE,
7219 /*manifestly_const_eval=*/true);
7220 expr = convert_from_reference (expr);
7221 }
7222 else if (TYPE_PTR_OR_PTRMEM_P (type))
7223 {
7224 tree folded = maybe_constant_value (expr, NULL_TREE,
7225 /*manifestly_const_eval=*/true);
7226 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7227 : null_member_pointer_value_p (folded))
7228 expr = folded;
7229 }
7230 }
7231
7232 if (TYPE_REF_P (type))
7233 expr = mark_lvalue_use (expr);
7234 else
7235 expr = mark_rvalue_use (expr);
7236
7237 /* HACK: Due to double coercion, we can get a
7238 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7239 which is the tree that we built on the first call (see
7240 below when coercing to reference to object or to reference to
7241 function). We just strip everything and get to the arg.
7242 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7243 for examples. */
7244 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7245 {
7246 tree probe_type, probe = expr;
7247 if (REFERENCE_REF_P (probe))
7248 probe = TREE_OPERAND (probe, 0);
7249 probe_type = TREE_TYPE (probe);
7250 if (TREE_CODE (probe) == NOP_EXPR)
7251 {
7252 /* ??? Maybe we could use convert_from_reference here, but we
7253 would need to relax its constraints because the NOP_EXPR
7254 could actually change the type to something more cv-qualified,
7255 and this is not folded by convert_from_reference. */
7256 tree addr = TREE_OPERAND (probe, 0);
7257 if (TYPE_REF_P (probe_type)
7258 && TREE_CODE (addr) == ADDR_EXPR
7259 && TYPE_PTR_P (TREE_TYPE (addr))
7260 && (same_type_ignoring_top_level_qualifiers_p
7261 (TREE_TYPE (probe_type),
7262 TREE_TYPE (TREE_TYPE (addr)))))
7263 {
7264 expr = TREE_OPERAND (addr, 0);
7265 expr_type = TREE_TYPE (probe_type);
7266 }
7267 }
7268 }
7269
7270 /* [temp.arg.nontype]/5, bullet 1
7271
7272 For a non-type template-parameter of integral or enumeration type,
7273 integral promotions (_conv.prom_) and integral conversions
7274 (_conv.integral_) are applied. */
7275 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7276 || TREE_CODE (type) == REAL_TYPE)
7277 {
7278 if (cxx_dialect < cxx11)
7279 {
7280 tree t = build_converted_constant_expr (type, expr, complain);
7281 t = maybe_constant_value (t);
7282 if (t != error_mark_node)
7283 expr = t;
7284 }
7285
7286 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7287 return error_mark_node;
7288
7289 /* Notice that there are constant expressions like '4 % 0' which
7290 do not fold into integer constants. */
7291 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7292 {
7293 if (complain & tf_error)
7294 {
7295 int errs = errorcount, warns = warningcount + werrorcount;
7296 if (!require_potential_constant_expression (expr))
7297 expr = error_mark_node;
7298 else
7299 expr = cxx_constant_value (expr);
7300 if (errorcount > errs || warningcount + werrorcount > warns)
7301 inform (loc, "in template argument for type %qT", type);
7302 if (expr == error_mark_node)
7303 return NULL_TREE;
7304 /* else cxx_constant_value complained but gave us
7305 a real constant, so go ahead. */
7306 if (!CONSTANT_CLASS_P (expr))
7307 {
7308 /* Some assemble time constant expressions like
7309 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7310 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7311 as we can emit them into .rodata initializers of
7312 variables, yet they can't fold into an INTEGER_CST at
7313 compile time. Refuse them here. */
7314 gcc_checking_assert (reduced_constant_expression_p (expr));
7315 error_at (loc, "template argument %qE for type %qT not "
7316 "a compile-time constant", expr, type);
7317 return NULL_TREE;
7318 }
7319 }
7320 else
7321 return NULL_TREE;
7322 }
7323
7324 /* Avoid typedef problems. */
7325 if (TREE_TYPE (expr) != type)
7326 expr = fold_convert (type, expr);
7327 }
7328 /* [temp.arg.nontype]/5, bullet 2
7329
7330 For a non-type template-parameter of type pointer to object,
7331 qualification conversions (_conv.qual_) and the array-to-pointer
7332 conversion (_conv.array_) are applied. */
7333 else if (TYPE_PTROBV_P (type))
7334 {
7335 tree decayed = expr;
7336
7337 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7338 decay_conversion or an explicit cast. If it's a problematic cast,
7339 we'll complain about it below. */
7340 if (TREE_CODE (expr) == NOP_EXPR)
7341 {
7342 tree probe = expr;
7343 STRIP_NOPS (probe);
7344 if (TREE_CODE (probe) == ADDR_EXPR
7345 && TYPE_PTR_P (TREE_TYPE (probe)))
7346 {
7347 expr = probe;
7348 expr_type = TREE_TYPE (expr);
7349 }
7350 }
7351
7352 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7353
7354 A template-argument for a non-type, non-template template-parameter
7355 shall be one of: [...]
7356
7357 -- the name of a non-type template-parameter;
7358 -- the address of an object or function with external linkage, [...]
7359 expressed as "& id-expression" where the & is optional if the name
7360 refers to a function or array, or if the corresponding
7361 template-parameter is a reference.
7362
7363 Here, we do not care about functions, as they are invalid anyway
7364 for a parameter of type pointer-to-object. */
7365
7366 if (val_dep_p)
7367 /* Non-type template parameters are OK. */
7368 ;
7369 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7370 /* Null pointer values are OK in C++11. */;
7371 else if (TREE_CODE (expr) != ADDR_EXPR
7372 && !INDIRECT_TYPE_P (expr_type))
7373 /* Other values, like integer constants, might be valid
7374 non-type arguments of some other type. */
7375 return error_mark_node;
7376 else if (invalid_tparm_referent_p (type, expr, complain))
7377 return NULL_TREE;
7378
7379 expr = decayed;
7380
7381 expr = perform_qualification_conversions (type, expr);
7382 if (expr == error_mark_node)
7383 return error_mark_node;
7384 }
7385 /* [temp.arg.nontype]/5, bullet 3
7386
7387 For a non-type template-parameter of type reference to object, no
7388 conversions apply. The type referred to by the reference may be more
7389 cv-qualified than the (otherwise identical) type of the
7390 template-argument. The template-parameter is bound directly to the
7391 template-argument, which must be an lvalue. */
7392 else if (TYPE_REF_OBJ_P (type))
7393 {
7394 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7395 expr_type))
7396 return error_mark_node;
7397
7398 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7399 {
7400 if (complain & tf_error)
7401 error ("%qE is not a valid template argument for type %qT "
7402 "because of conflicts in cv-qualification", expr, type);
7403 return NULL_TREE;
7404 }
7405
7406 if (!lvalue_p (expr))
7407 {
7408 if (complain & tf_error)
7409 error ("%qE is not a valid template argument for type %qT "
7410 "because it is not an lvalue", expr, type);
7411 return NULL_TREE;
7412 }
7413
7414 /* [temp.arg.nontype]/1
7415
7416 A template-argument for a non-type, non-template template-parameter
7417 shall be one of: [...]
7418
7419 -- the address of an object or function with external linkage. */
7420 if (INDIRECT_REF_P (expr)
7421 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7422 {
7423 expr = TREE_OPERAND (expr, 0);
7424 if (DECL_P (expr))
7425 {
7426 if (complain & tf_error)
7427 error ("%q#D is not a valid template argument for type %qT "
7428 "because a reference variable does not have a constant "
7429 "address", expr, type);
7430 return NULL_TREE;
7431 }
7432 }
7433
7434 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7435 /* OK, dependent reference. We don't want to ask whether a DECL is
7436 itself value-dependent, since what we want here is its address. */;
7437 else
7438 {
7439 expr = build_address (expr);
7440
7441 if (invalid_tparm_referent_p (type, expr, complain))
7442 return NULL_TREE;
7443 }
7444
7445 if (!same_type_p (type, TREE_TYPE (expr)))
7446 expr = build_nop (type, expr);
7447 }
7448 /* [temp.arg.nontype]/5, bullet 4
7449
7450 For a non-type template-parameter of type pointer to function, only
7451 the function-to-pointer conversion (_conv.func_) is applied. If the
7452 template-argument represents a set of overloaded functions (or a
7453 pointer to such), the matching function is selected from the set
7454 (_over.over_). */
7455 else if (TYPE_PTRFN_P (type))
7456 {
7457 /* If the argument is a template-id, we might not have enough
7458 context information to decay the pointer. */
7459 if (!type_unknown_p (expr_type))
7460 {
7461 expr = decay_conversion (expr, complain);
7462 if (expr == error_mark_node)
7463 return error_mark_node;
7464 }
7465
7466 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7467 /* Null pointer values are OK in C++11. */
7468 return perform_qualification_conversions (type, expr);
7469
7470 expr = convert_nontype_argument_function (type, expr, complain);
7471 if (!expr || expr == error_mark_node)
7472 return expr;
7473 }
7474 /* [temp.arg.nontype]/5, bullet 5
7475
7476 For a non-type template-parameter of type reference to function, no
7477 conversions apply. If the template-argument represents a set of
7478 overloaded functions, the matching function is selected from the set
7479 (_over.over_). */
7480 else if (TYPE_REFFN_P (type))
7481 {
7482 if (TREE_CODE (expr) == ADDR_EXPR)
7483 {
7484 if (complain & tf_error)
7485 {
7486 error ("%qE is not a valid template argument for type %qT "
7487 "because it is a pointer", expr, type);
7488 inform (input_location, "try using %qE instead",
7489 TREE_OPERAND (expr, 0));
7490 }
7491 return NULL_TREE;
7492 }
7493
7494 expr = convert_nontype_argument_function (type, expr, complain);
7495 if (!expr || expr == error_mark_node)
7496 return expr;
7497 }
7498 /* [temp.arg.nontype]/5, bullet 6
7499
7500 For a non-type template-parameter of type pointer to member function,
7501 no conversions apply. If the template-argument represents a set of
7502 overloaded member functions, the matching member function is selected
7503 from the set (_over.over_). */
7504 else if (TYPE_PTRMEMFUNC_P (type))
7505 {
7506 expr = instantiate_type (type, expr, tf_none);
7507 if (expr == error_mark_node)
7508 return error_mark_node;
7509
7510 /* [temp.arg.nontype] bullet 1 says the pointer to member
7511 expression must be a pointer-to-member constant. */
7512 if (!val_dep_p
7513 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7514 return NULL_TREE;
7515
7516 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7517 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7518 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7519 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7520 }
7521 /* [temp.arg.nontype]/5, bullet 7
7522
7523 For a non-type template-parameter of type pointer to data member,
7524 qualification conversions (_conv.qual_) are applied. */
7525 else if (TYPE_PTRDATAMEM_P (type))
7526 {
7527 /* [temp.arg.nontype] bullet 1 says the pointer to member
7528 expression must be a pointer-to-member constant. */
7529 if (!val_dep_p
7530 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7531 return NULL_TREE;
7532
7533 expr = perform_qualification_conversions (type, expr);
7534 if (expr == error_mark_node)
7535 return expr;
7536 }
7537 else if (NULLPTR_TYPE_P (type))
7538 {
7539 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7540 {
7541 if (complain & tf_error)
7542 error ("%qE is not a valid template argument for type %qT "
7543 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7544 return NULL_TREE;
7545 }
7546 return expr;
7547 }
7548 else if (CLASS_TYPE_P (type))
7549 {
7550 /* Replace the argument with a reference to the corresponding template
7551 parameter object. */
7552 if (!val_dep_p)
7553 expr = get_template_parm_object (expr, complain);
7554 if (expr == error_mark_node)
7555 return NULL_TREE;
7556 }
7557 /* A template non-type parameter must be one of the above. */
7558 else
7559 gcc_unreachable ();
7560
7561 /* Sanity check: did we actually convert the argument to the
7562 right type? */
7563 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7564 (type, TREE_TYPE (expr)));
7565 return convert_from_reference (expr);
7566 }
7567
7568 /* Subroutine of coerce_template_template_parms, which returns 1 if
7569 PARM_PARM and ARG_PARM match using the rule for the template
7570 parameters of template template parameters. Both PARM and ARG are
7571 template parameters; the rest of the arguments are the same as for
7572 coerce_template_template_parms.
7573 */
7574 static int
7575 coerce_template_template_parm (tree parm,
7576 tree arg,
7577 tsubst_flags_t complain,
7578 tree in_decl,
7579 tree outer_args)
7580 {
7581 if (arg == NULL_TREE || error_operand_p (arg)
7582 || parm == NULL_TREE || error_operand_p (parm))
7583 return 0;
7584
7585 if (TREE_CODE (arg) != TREE_CODE (parm))
7586 return 0;
7587
7588 switch (TREE_CODE (parm))
7589 {
7590 case TEMPLATE_DECL:
7591 /* We encounter instantiations of templates like
7592 template <template <template <class> class> class TT>
7593 class C; */
7594 {
7595 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7596 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7597
7598 if (!coerce_template_template_parms
7599 (parmparm, argparm, complain, in_decl, outer_args))
7600 return 0;
7601 }
7602 /* Fall through. */
7603
7604 case TYPE_DECL:
7605 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7606 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7607 /* Argument is a parameter pack but parameter is not. */
7608 return 0;
7609 break;
7610
7611 case PARM_DECL:
7612 /* The tsubst call is used to handle cases such as
7613
7614 template <int> class C {};
7615 template <class T, template <T> class TT> class D {};
7616 D<int, C> d;
7617
7618 i.e. the parameter list of TT depends on earlier parameters. */
7619 if (!uses_template_parms (TREE_TYPE (arg)))
7620 {
7621 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7622 if (!uses_template_parms (t)
7623 && !same_type_p (t, TREE_TYPE (arg)))
7624 return 0;
7625 }
7626
7627 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7628 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7629 /* Argument is a parameter pack but parameter is not. */
7630 return 0;
7631
7632 break;
7633
7634 default:
7635 gcc_unreachable ();
7636 }
7637
7638 return 1;
7639 }
7640
7641 /* Coerce template argument list ARGLIST for use with template
7642 template-parameter TEMPL. */
7643
7644 static tree
7645 coerce_template_args_for_ttp (tree templ, tree arglist,
7646 tsubst_flags_t complain)
7647 {
7648 /* Consider an example where a template template parameter declared as
7649
7650 template <class T, class U = std::allocator<T> > class TT
7651
7652 The template parameter level of T and U are one level larger than
7653 of TT. To proper process the default argument of U, say when an
7654 instantiation `TT<int>' is seen, we need to build the full
7655 arguments containing {int} as the innermost level. Outer levels,
7656 available when not appearing as default template argument, can be
7657 obtained from the arguments of the enclosing template.
7658
7659 Suppose that TT is later substituted with std::vector. The above
7660 instantiation is `TT<int, std::allocator<T> >' with TT at
7661 level 1, and T at level 2, while the template arguments at level 1
7662 becomes {std::vector} and the inner level 2 is {int}. */
7663
7664 tree outer = DECL_CONTEXT (templ);
7665 if (outer)
7666 outer = generic_targs_for (outer);
7667 else if (current_template_parms)
7668 {
7669 /* This is an argument of the current template, so we haven't set
7670 DECL_CONTEXT yet. */
7671 tree relevant_template_parms;
7672
7673 /* Parameter levels that are greater than the level of the given
7674 template template parm are irrelevant. */
7675 relevant_template_parms = current_template_parms;
7676 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7677 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7678 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7679
7680 outer = template_parms_to_args (relevant_template_parms);
7681 }
7682
7683 if (outer)
7684 arglist = add_to_template_args (outer, arglist);
7685
7686 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7687 return coerce_template_parms (parmlist, arglist, templ,
7688 complain,
7689 /*require_all_args=*/true,
7690 /*use_default_args=*/true);
7691 }
7692
7693 /* A cache of template template parameters with match-all default
7694 arguments. */
7695 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7696
7697 /* T is a bound template template-parameter. Copy its arguments into default
7698 arguments of the template template-parameter's template parameters. */
7699
7700 static tree
7701 add_defaults_to_ttp (tree otmpl)
7702 {
7703 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7704 return *c;
7705
7706 tree ntmpl = copy_node (otmpl);
7707
7708 tree ntype = copy_node (TREE_TYPE (otmpl));
7709 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7710 TYPE_MAIN_VARIANT (ntype) = ntype;
7711 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7712 TYPE_NAME (ntype) = ntmpl;
7713 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7714
7715 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7716 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7717 TEMPLATE_PARM_DECL (idx) = ntmpl;
7718 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7719
7720 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7721 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7722 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7723 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7724 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7725 {
7726 tree o = TREE_VEC_ELT (vec, i);
7727 if (!template_parameter_pack_p (TREE_VALUE (o)))
7728 {
7729 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7730 TREE_PURPOSE (n) = any_targ_node;
7731 }
7732 }
7733
7734 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7735 return ntmpl;
7736 }
7737
7738 /* ARG is a bound potential template template-argument, and PARGS is a list
7739 of arguments for the corresponding template template-parameter. Adjust
7740 PARGS as appropriate for application to ARG's template, and if ARG is a
7741 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7742 arguments to the template template parameter. */
7743
7744 static tree
7745 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7746 {
7747 ++processing_template_decl;
7748 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7749 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7750 {
7751 /* When comparing two template template-parameters in partial ordering,
7752 rewrite the one currently being used as an argument to have default
7753 arguments for all parameters. */
7754 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7755 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7756 if (pargs != error_mark_node)
7757 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7758 TYPE_TI_ARGS (arg));
7759 }
7760 else
7761 {
7762 tree aparms
7763 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7764 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7765 /*require_all*/true,
7766 /*use_default*/true);
7767 }
7768 --processing_template_decl;
7769 return pargs;
7770 }
7771
7772 /* Subroutine of unify for the case when PARM is a
7773 BOUND_TEMPLATE_TEMPLATE_PARM. */
7774
7775 static int
7776 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7777 bool explain_p)
7778 {
7779 tree parmvec = TYPE_TI_ARGS (parm);
7780 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7781
7782 /* The template template parm might be variadic and the argument
7783 not, so flatten both argument lists. */
7784 parmvec = expand_template_argument_pack (parmvec);
7785 argvec = expand_template_argument_pack (argvec);
7786
7787 if (flag_new_ttp)
7788 {
7789 /* In keeping with P0522R0, adjust P's template arguments
7790 to apply to A's template; then flatten it again. */
7791 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7792 nparmvec = expand_template_argument_pack (nparmvec);
7793
7794 if (unify (tparms, targs, nparmvec, argvec,
7795 UNIFY_ALLOW_NONE, explain_p))
7796 return 1;
7797
7798 /* If the P0522 adjustment eliminated a pack expansion, deduce
7799 empty packs. */
7800 if (flag_new_ttp
7801 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7802 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7803 DEDUCE_EXACT, /*sub*/true, explain_p))
7804 return 1;
7805 }
7806 else
7807 {
7808 /* Deduce arguments T, i from TT<T> or TT<i>.
7809 We check each element of PARMVEC and ARGVEC individually
7810 rather than the whole TREE_VEC since they can have
7811 different number of elements, which is allowed under N2555. */
7812
7813 int len = TREE_VEC_LENGTH (parmvec);
7814
7815 /* Check if the parameters end in a pack, making them
7816 variadic. */
7817 int parm_variadic_p = 0;
7818 if (len > 0
7819 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7820 parm_variadic_p = 1;
7821
7822 for (int i = 0; i < len - parm_variadic_p; ++i)
7823 /* If the template argument list of P contains a pack
7824 expansion that is not the last template argument, the
7825 entire template argument list is a non-deduced
7826 context. */
7827 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7828 return unify_success (explain_p);
7829
7830 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7831 return unify_too_few_arguments (explain_p,
7832 TREE_VEC_LENGTH (argvec), len);
7833
7834 for (int i = 0; i < len - parm_variadic_p; ++i)
7835 if (unify (tparms, targs,
7836 TREE_VEC_ELT (parmvec, i),
7837 TREE_VEC_ELT (argvec, i),
7838 UNIFY_ALLOW_NONE, explain_p))
7839 return 1;
7840
7841 if (parm_variadic_p
7842 && unify_pack_expansion (tparms, targs,
7843 parmvec, argvec,
7844 DEDUCE_EXACT,
7845 /*subr=*/true, explain_p))
7846 return 1;
7847 }
7848
7849 return 0;
7850 }
7851
7852 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7853 template template parameters. Both PARM_PARMS and ARG_PARMS are
7854 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7855 or PARM_DECL.
7856
7857 Consider the example:
7858 template <class T> class A;
7859 template<template <class U> class TT> class B;
7860
7861 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7862 the parameters to A, and OUTER_ARGS contains A. */
7863
7864 static int
7865 coerce_template_template_parms (tree parm_parms,
7866 tree arg_parms,
7867 tsubst_flags_t complain,
7868 tree in_decl,
7869 tree outer_args)
7870 {
7871 int nparms, nargs, i;
7872 tree parm, arg;
7873 int variadic_p = 0;
7874
7875 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7876 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7877
7878 nparms = TREE_VEC_LENGTH (parm_parms);
7879 nargs = TREE_VEC_LENGTH (arg_parms);
7880
7881 if (flag_new_ttp)
7882 {
7883 /* P0522R0: A template template-parameter P is at least as specialized as
7884 a template template-argument A if, given the following rewrite to two
7885 function templates, the function template corresponding to P is at
7886 least as specialized as the function template corresponding to A
7887 according to the partial ordering rules for function templates
7888 ([temp.func.order]). Given an invented class template X with the
7889 template parameter list of A (including default arguments):
7890
7891 * Each of the two function templates has the same template parameters,
7892 respectively, as P or A.
7893
7894 * Each function template has a single function parameter whose type is
7895 a specialization of X with template arguments corresponding to the
7896 template parameters from the respective function template where, for
7897 each template parameter PP in the template parameter list of the
7898 function template, a corresponding template argument AA is formed. If
7899 PP declares a parameter pack, then AA is the pack expansion
7900 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7901
7902 If the rewrite produces an invalid type, then P is not at least as
7903 specialized as A. */
7904
7905 /* So coerce P's args to apply to A's parms, and then deduce between A's
7906 args and the converted args. If that succeeds, A is at least as
7907 specialized as P, so they match.*/
7908 tree pargs = template_parms_level_to_args (parm_parms);
7909 pargs = add_outermost_template_args (outer_args, pargs);
7910 ++processing_template_decl;
7911 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7912 /*require_all*/true, /*use_default*/true);
7913 --processing_template_decl;
7914 if (pargs != error_mark_node)
7915 {
7916 tree targs = make_tree_vec (nargs);
7917 tree aargs = template_parms_level_to_args (arg_parms);
7918 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7919 /*explain*/false))
7920 return 1;
7921 }
7922 }
7923
7924 /* Determine whether we have a parameter pack at the end of the
7925 template template parameter's template parameter list. */
7926 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7927 {
7928 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7929
7930 if (error_operand_p (parm))
7931 return 0;
7932
7933 switch (TREE_CODE (parm))
7934 {
7935 case TEMPLATE_DECL:
7936 case TYPE_DECL:
7937 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7938 variadic_p = 1;
7939 break;
7940
7941 case PARM_DECL:
7942 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7943 variadic_p = 1;
7944 break;
7945
7946 default:
7947 gcc_unreachable ();
7948 }
7949 }
7950
7951 if (nargs != nparms
7952 && !(variadic_p && nargs >= nparms - 1))
7953 return 0;
7954
7955 /* Check all of the template parameters except the parameter pack at
7956 the end (if any). */
7957 for (i = 0; i < nparms - variadic_p; ++i)
7958 {
7959 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7960 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7961 continue;
7962
7963 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7964 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7965
7966 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7967 outer_args))
7968 return 0;
7969
7970 }
7971
7972 if (variadic_p)
7973 {
7974 /* Check each of the template parameters in the template
7975 argument against the template parameter pack at the end of
7976 the template template parameter. */
7977 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7978 return 0;
7979
7980 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7981
7982 for (; i < nargs; ++i)
7983 {
7984 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7985 continue;
7986
7987 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7988
7989 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7990 outer_args))
7991 return 0;
7992 }
7993 }
7994
7995 return 1;
7996 }
7997
7998 /* Verifies that the deduced template arguments (in TARGS) for the
7999 template template parameters (in TPARMS) represent valid bindings,
8000 by comparing the template parameter list of each template argument
8001 to the template parameter list of its corresponding template
8002 template parameter, in accordance with DR150. This
8003 routine can only be called after all template arguments have been
8004 deduced. It will return TRUE if all of the template template
8005 parameter bindings are okay, FALSE otherwise. */
8006 bool
8007 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8008 {
8009 int i, ntparms = TREE_VEC_LENGTH (tparms);
8010 bool ret = true;
8011
8012 /* We're dealing with template parms in this process. */
8013 ++processing_template_decl;
8014
8015 targs = INNERMOST_TEMPLATE_ARGS (targs);
8016
8017 for (i = 0; i < ntparms; ++i)
8018 {
8019 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8020 tree targ = TREE_VEC_ELT (targs, i);
8021
8022 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8023 {
8024 tree packed_args = NULL_TREE;
8025 int idx, len = 1;
8026
8027 if (ARGUMENT_PACK_P (targ))
8028 {
8029 /* Look inside the argument pack. */
8030 packed_args = ARGUMENT_PACK_ARGS (targ);
8031 len = TREE_VEC_LENGTH (packed_args);
8032 }
8033
8034 for (idx = 0; idx < len; ++idx)
8035 {
8036 tree targ_parms = NULL_TREE;
8037
8038 if (packed_args)
8039 /* Extract the next argument from the argument
8040 pack. */
8041 targ = TREE_VEC_ELT (packed_args, idx);
8042
8043 if (PACK_EXPANSION_P (targ))
8044 /* Look at the pattern of the pack expansion. */
8045 targ = PACK_EXPANSION_PATTERN (targ);
8046
8047 /* Extract the template parameters from the template
8048 argument. */
8049 if (TREE_CODE (targ) == TEMPLATE_DECL)
8050 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8051 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8052 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8053
8054 /* Verify that we can coerce the template template
8055 parameters from the template argument to the template
8056 parameter. This requires an exact match. */
8057 if (targ_parms
8058 && !coerce_template_template_parms
8059 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8060 targ_parms,
8061 tf_none,
8062 tparm,
8063 targs))
8064 {
8065 ret = false;
8066 goto out;
8067 }
8068 }
8069 }
8070 }
8071
8072 out:
8073
8074 --processing_template_decl;
8075 return ret;
8076 }
8077
8078 /* Since type attributes aren't mangled, we need to strip them from
8079 template type arguments. */
8080
8081 tree
8082 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8083 {
8084 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8085 return arg;
8086 bool removed_attributes = false;
8087 tree canon = strip_typedefs (arg, &removed_attributes);
8088 if (removed_attributes
8089 && (complain & tf_warning))
8090 warning (OPT_Wignored_attributes,
8091 "ignoring attributes on template argument %qT", arg);
8092 return canon;
8093 }
8094
8095 /* And from inside dependent non-type arguments like sizeof(Type). */
8096
8097 static tree
8098 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8099 {
8100 if (!arg || arg == error_mark_node)
8101 return arg;
8102 bool removed_attributes = false;
8103 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8104 if (removed_attributes
8105 && (complain & tf_warning))
8106 warning (OPT_Wignored_attributes,
8107 "ignoring attributes in template argument %qE", arg);
8108 return canon;
8109 }
8110
8111 // A template declaration can be substituted for a constrained
8112 // template template parameter only when the argument is more
8113 // constrained than the parameter.
8114 static bool
8115 is_compatible_template_arg (tree parm, tree arg)
8116 {
8117 tree parm_cons = get_constraints (parm);
8118
8119 /* For now, allow constrained template template arguments
8120 and unconstrained template template parameters. */
8121 if (parm_cons == NULL_TREE)
8122 return true;
8123
8124 /* If the template parameter is constrained, we need to rewrite its
8125 constraints in terms of the ARG's template parameters. This ensures
8126 that all of the template parameter types will have the same depth.
8127
8128 Note that this is only valid when coerce_template_template_parm is
8129 true for the innermost template parameters of PARM and ARG. In other
8130 words, because coercion is successful, this conversion will be valid. */
8131 tree new_args = NULL_TREE;
8132 if (parm_cons)
8133 {
8134 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8135 new_args = template_parms_level_to_args (aparms);
8136 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8137 tf_none, NULL_TREE);
8138 if (parm_cons == error_mark_node)
8139 return false;
8140 }
8141
8142 return weakly_subsumes (parm_cons, new_args, arg);
8143 }
8144
8145 // Convert a placeholder argument into a binding to the original
8146 // parameter. The original parameter is saved as the TREE_TYPE of
8147 // ARG.
8148 static inline tree
8149 convert_wildcard_argument (tree parm, tree arg)
8150 {
8151 TREE_TYPE (arg) = parm;
8152 return arg;
8153 }
8154
8155 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8156 because one of them is dependent. But we need to represent the
8157 conversion for the benefit of cp_tree_equal. */
8158
8159 static tree
8160 maybe_convert_nontype_argument (tree type, tree arg)
8161 {
8162 /* Auto parms get no conversion. */
8163 if (type_uses_auto (type))
8164 return arg;
8165 /* We don't need or want to add this conversion now if we're going to use the
8166 argument for deduction. */
8167 if (value_dependent_expression_p (arg))
8168 return arg;
8169
8170 type = cv_unqualified (type);
8171 tree argtype = TREE_TYPE (arg);
8172 if (same_type_p (type, argtype))
8173 return arg;
8174
8175 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8176 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8177 return arg;
8178 }
8179
8180 /* Convert the indicated template ARG as necessary to match the
8181 indicated template PARM. Returns the converted ARG, or
8182 error_mark_node if the conversion was unsuccessful. Error and
8183 warning messages are issued under control of COMPLAIN. This
8184 conversion is for the Ith parameter in the parameter list. ARGS is
8185 the full set of template arguments deduced so far. */
8186
8187 static tree
8188 convert_template_argument (tree parm,
8189 tree arg,
8190 tree args,
8191 tsubst_flags_t complain,
8192 int i,
8193 tree in_decl)
8194 {
8195 tree orig_arg;
8196 tree val;
8197 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8198
8199 if (parm == error_mark_node || error_operand_p (arg))
8200 return error_mark_node;
8201
8202 /* Trivially convert placeholders. */
8203 if (TREE_CODE (arg) == WILDCARD_DECL)
8204 return convert_wildcard_argument (parm, arg);
8205
8206 if (arg == any_targ_node)
8207 return arg;
8208
8209 if (TREE_CODE (arg) == TREE_LIST
8210 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8211 {
8212 /* The template argument was the name of some
8213 member function. That's usually
8214 invalid, but static members are OK. In any
8215 case, grab the underlying fields/functions
8216 and issue an error later if required. */
8217 TREE_TYPE (arg) = unknown_type_node;
8218 }
8219
8220 orig_arg = arg;
8221
8222 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8223 requires_type = (TREE_CODE (parm) == TYPE_DECL
8224 || requires_tmpl_type);
8225
8226 /* When determining whether an argument pack expansion is a template,
8227 look at the pattern. */
8228 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8229 arg = PACK_EXPANSION_PATTERN (arg);
8230
8231 /* Deal with an injected-class-name used as a template template arg. */
8232 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8233 {
8234 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8235 if (TREE_CODE (t) == TEMPLATE_DECL)
8236 {
8237 if (cxx_dialect >= cxx11)
8238 /* OK under DR 1004. */;
8239 else if (complain & tf_warning_or_error)
8240 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8241 " used as template template argument", TYPE_NAME (arg));
8242 else if (flag_pedantic_errors)
8243 t = arg;
8244
8245 arg = t;
8246 }
8247 }
8248
8249 is_tmpl_type =
8250 ((TREE_CODE (arg) == TEMPLATE_DECL
8251 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8252 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8253 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8254 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8255
8256 if (is_tmpl_type
8257 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8258 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8259 arg = TYPE_STUB_DECL (arg);
8260
8261 is_type = TYPE_P (arg) || is_tmpl_type;
8262
8263 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8264 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8265 {
8266 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8267 {
8268 if (complain & tf_error)
8269 error ("invalid use of destructor %qE as a type", orig_arg);
8270 return error_mark_node;
8271 }
8272
8273 permerror (input_location,
8274 "to refer to a type member of a template parameter, "
8275 "use %<typename %E%>", orig_arg);
8276
8277 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8278 TREE_OPERAND (arg, 1),
8279 typename_type,
8280 complain);
8281 arg = orig_arg;
8282 is_type = 1;
8283 }
8284 if (is_type != requires_type)
8285 {
8286 if (in_decl)
8287 {
8288 if (complain & tf_error)
8289 {
8290 error ("type/value mismatch at argument %d in template "
8291 "parameter list for %qD",
8292 i + 1, in_decl);
8293 if (is_type)
8294 {
8295 /* The template argument is a type, but we're expecting
8296 an expression. */
8297 inform (input_location,
8298 " expected a constant of type %qT, got %qT",
8299 TREE_TYPE (parm),
8300 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8301 /* [temp.arg]/2: "In a template-argument, an ambiguity
8302 between a type-id and an expression is resolved to a
8303 type-id, regardless of the form of the corresponding
8304 template-parameter." So give the user a clue. */
8305 if (TREE_CODE (arg) == FUNCTION_TYPE)
8306 inform (input_location, " ambiguous template argument "
8307 "for non-type template parameter is treated as "
8308 "function type");
8309 }
8310 else if (requires_tmpl_type)
8311 inform (input_location,
8312 " expected a class template, got %qE", orig_arg);
8313 else
8314 inform (input_location,
8315 " expected a type, got %qE", orig_arg);
8316 }
8317 }
8318 return error_mark_node;
8319 }
8320 if (is_tmpl_type ^ requires_tmpl_type)
8321 {
8322 if (in_decl && (complain & tf_error))
8323 {
8324 error ("type/value mismatch at argument %d in template "
8325 "parameter list for %qD",
8326 i + 1, in_decl);
8327 if (is_tmpl_type)
8328 inform (input_location,
8329 " expected a type, got %qT", DECL_NAME (arg));
8330 else
8331 inform (input_location,
8332 " expected a class template, got %qT", orig_arg);
8333 }
8334 return error_mark_node;
8335 }
8336
8337 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8338 /* We already did the appropriate conversion when packing args. */
8339 val = orig_arg;
8340 else if (is_type)
8341 {
8342 if (requires_tmpl_type)
8343 {
8344 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8345 /* The number of argument required is not known yet.
8346 Just accept it for now. */
8347 val = orig_arg;
8348 else
8349 {
8350 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8351 tree argparm;
8352
8353 /* Strip alias templates that are equivalent to another
8354 template. */
8355 arg = get_underlying_template (arg);
8356 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8357
8358 if (coerce_template_template_parms (parmparm, argparm,
8359 complain, in_decl,
8360 args))
8361 {
8362 val = arg;
8363
8364 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8365 TEMPLATE_DECL. */
8366 if (val != error_mark_node)
8367 {
8368 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8369 val = TREE_TYPE (val);
8370 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8371 val = make_pack_expansion (val, complain);
8372 }
8373 }
8374 else
8375 {
8376 if (in_decl && (complain & tf_error))
8377 {
8378 error ("type/value mismatch at argument %d in "
8379 "template parameter list for %qD",
8380 i + 1, in_decl);
8381 inform (input_location,
8382 " expected a template of type %qD, got %qT",
8383 parm, orig_arg);
8384 }
8385
8386 val = error_mark_node;
8387 }
8388
8389 // Check that the constraints are compatible before allowing the
8390 // substitution.
8391 if (val != error_mark_node)
8392 if (!is_compatible_template_arg (parm, arg))
8393 {
8394 if (in_decl && (complain & tf_error))
8395 {
8396 error ("constraint mismatch at argument %d in "
8397 "template parameter list for %qD",
8398 i + 1, in_decl);
8399 inform (input_location, " expected %qD but got %qD",
8400 parm, arg);
8401 }
8402 val = error_mark_node;
8403 }
8404 }
8405 }
8406 else
8407 val = orig_arg;
8408 /* We only form one instance of each template specialization.
8409 Therefore, if we use a non-canonical variant (i.e., a
8410 typedef), any future messages referring to the type will use
8411 the typedef, which is confusing if those future uses do not
8412 themselves also use the typedef. */
8413 if (TYPE_P (val))
8414 val = canonicalize_type_argument (val, complain);
8415 }
8416 else
8417 {
8418 tree t = TREE_TYPE (parm);
8419
8420 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8421 > TMPL_ARGS_DEPTH (args))
8422 /* We don't have enough levels of args to do any substitution. This
8423 can happen in the context of -fnew-ttp-matching. */;
8424 else if (tree a = type_uses_auto (t))
8425 {
8426 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8427 if (t == error_mark_node)
8428 return error_mark_node;
8429 }
8430 else
8431 t = tsubst (t, args, complain, in_decl);
8432
8433 if (invalid_nontype_parm_type_p (t, complain))
8434 return error_mark_node;
8435
8436 if (t != TREE_TYPE (parm))
8437 t = canonicalize_type_argument (t, complain);
8438
8439 if (!type_dependent_expression_p (orig_arg)
8440 && !uses_template_parms (t))
8441 /* We used to call digest_init here. However, digest_init
8442 will report errors, which we don't want when complain
8443 is zero. More importantly, digest_init will try too
8444 hard to convert things: for example, `0' should not be
8445 converted to pointer type at this point according to
8446 the standard. Accepting this is not merely an
8447 extension, since deciding whether or not these
8448 conversions can occur is part of determining which
8449 function template to call, or whether a given explicit
8450 argument specification is valid. */
8451 val = convert_nontype_argument (t, orig_arg, complain);
8452 else
8453 {
8454 val = canonicalize_expr_argument (orig_arg, complain);
8455 val = maybe_convert_nontype_argument (t, val);
8456 }
8457
8458
8459 if (val == NULL_TREE)
8460 val = error_mark_node;
8461 else if (val == error_mark_node && (complain & tf_error))
8462 error_at (cp_expr_loc_or_input_loc (orig_arg),
8463 "could not convert template argument %qE from %qT to %qT",
8464 orig_arg, TREE_TYPE (orig_arg), t);
8465
8466 if (INDIRECT_REF_P (val))
8467 {
8468 /* Reject template arguments that are references to built-in
8469 functions with no library fallbacks. */
8470 const_tree inner = TREE_OPERAND (val, 0);
8471 const_tree innertype = TREE_TYPE (inner);
8472 if (innertype
8473 && TYPE_REF_P (innertype)
8474 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8475 && TREE_OPERAND_LENGTH (inner) > 0
8476 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8477 return error_mark_node;
8478 }
8479
8480 if (TREE_CODE (val) == SCOPE_REF)
8481 {
8482 /* Strip typedefs from the SCOPE_REF. */
8483 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8484 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8485 complain);
8486 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8487 QUALIFIED_NAME_IS_TEMPLATE (val));
8488 }
8489 }
8490
8491 return val;
8492 }
8493
8494 /* Coerces the remaining template arguments in INNER_ARGS (from
8495 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8496 Returns the coerced argument pack. PARM_IDX is the position of this
8497 parameter in the template parameter list. ARGS is the original
8498 template argument list. */
8499 static tree
8500 coerce_template_parameter_pack (tree parms,
8501 int parm_idx,
8502 tree args,
8503 tree inner_args,
8504 int arg_idx,
8505 tree new_args,
8506 int* lost,
8507 tree in_decl,
8508 tsubst_flags_t complain)
8509 {
8510 tree parm = TREE_VEC_ELT (parms, parm_idx);
8511 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8512 tree packed_args;
8513 tree argument_pack;
8514 tree packed_parms = NULL_TREE;
8515
8516 if (arg_idx > nargs)
8517 arg_idx = nargs;
8518
8519 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8520 {
8521 /* When the template parameter is a non-type template parameter pack
8522 or template template parameter pack whose type or template
8523 parameters use parameter packs, we know exactly how many arguments
8524 we are looking for. Build a vector of the instantiated decls for
8525 these template parameters in PACKED_PARMS. */
8526 /* We can't use make_pack_expansion here because it would interpret a
8527 _DECL as a use rather than a declaration. */
8528 tree decl = TREE_VALUE (parm);
8529 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8530 SET_PACK_EXPANSION_PATTERN (exp, decl);
8531 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8532 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8533
8534 TREE_VEC_LENGTH (args)--;
8535 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8536 TREE_VEC_LENGTH (args)++;
8537
8538 if (packed_parms == error_mark_node)
8539 return error_mark_node;
8540
8541 /* If we're doing a partial instantiation of a member template,
8542 verify that all of the types used for the non-type
8543 template parameter pack are, in fact, valid for non-type
8544 template parameters. */
8545 if (arg_idx < nargs
8546 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8547 {
8548 int j, len = TREE_VEC_LENGTH (packed_parms);
8549 for (j = 0; j < len; ++j)
8550 {
8551 tree t = TREE_VEC_ELT (packed_parms, j);
8552 if (TREE_CODE (t) == PARM_DECL
8553 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8554 return error_mark_node;
8555 }
8556 /* We don't know how many args we have yet, just
8557 use the unconverted ones for now. */
8558 return NULL_TREE;
8559 }
8560
8561 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8562 }
8563 /* Check if we have a placeholder pack, which indicates we're
8564 in the context of a introduction list. In that case we want
8565 to match this pack to the single placeholder. */
8566 else if (arg_idx < nargs
8567 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8568 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8569 {
8570 nargs = arg_idx + 1;
8571 packed_args = make_tree_vec (1);
8572 }
8573 else
8574 packed_args = make_tree_vec (nargs - arg_idx);
8575
8576 /* Convert the remaining arguments, which will be a part of the
8577 parameter pack "parm". */
8578 int first_pack_arg = arg_idx;
8579 for (; arg_idx < nargs; ++arg_idx)
8580 {
8581 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8582 tree actual_parm = TREE_VALUE (parm);
8583 int pack_idx = arg_idx - first_pack_arg;
8584
8585 if (packed_parms)
8586 {
8587 /* Once we've packed as many args as we have types, stop. */
8588 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8589 break;
8590 else if (PACK_EXPANSION_P (arg))
8591 /* We don't know how many args we have yet, just
8592 use the unconverted ones for now. */
8593 return NULL_TREE;
8594 else
8595 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8596 }
8597
8598 if (arg == error_mark_node)
8599 {
8600 if (complain & tf_error)
8601 error ("template argument %d is invalid", arg_idx + 1);
8602 }
8603 else
8604 arg = convert_template_argument (actual_parm,
8605 arg, new_args, complain, parm_idx,
8606 in_decl);
8607 if (arg == error_mark_node)
8608 (*lost)++;
8609 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8610 }
8611
8612 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8613 && TREE_VEC_LENGTH (packed_args) > 0)
8614 {
8615 if (complain & tf_error)
8616 error ("wrong number of template arguments (%d, should be %d)",
8617 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8618 return error_mark_node;
8619 }
8620
8621 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8622 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8623 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8624 else
8625 {
8626 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8627 TREE_CONSTANT (argument_pack) = 1;
8628 }
8629
8630 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8631 if (CHECKING_P)
8632 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8633 TREE_VEC_LENGTH (packed_args));
8634 return argument_pack;
8635 }
8636
8637 /* Returns the number of pack expansions in the template argument vector
8638 ARGS. */
8639
8640 static int
8641 pack_expansion_args_count (tree args)
8642 {
8643 int i;
8644 int count = 0;
8645 if (args)
8646 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8647 {
8648 tree elt = TREE_VEC_ELT (args, i);
8649 if (elt && PACK_EXPANSION_P (elt))
8650 ++count;
8651 }
8652 return count;
8653 }
8654
8655 /* Convert all template arguments to their appropriate types, and
8656 return a vector containing the innermost resulting template
8657 arguments. If any error occurs, return error_mark_node. Error and
8658 warning messages are issued under control of COMPLAIN.
8659
8660 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8661 for arguments not specified in ARGS. Otherwise, if
8662 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8663 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8664 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8665 ARGS. */
8666
8667 static tree
8668 coerce_template_parms (tree parms,
8669 tree args,
8670 tree in_decl,
8671 tsubst_flags_t complain,
8672 bool require_all_args,
8673 bool use_default_args)
8674 {
8675 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8676 tree orig_inner_args;
8677 tree inner_args;
8678 tree new_args;
8679 tree new_inner_args;
8680
8681 /* When used as a boolean value, indicates whether this is a
8682 variadic template parameter list. Since it's an int, we can also
8683 subtract it from nparms to get the number of non-variadic
8684 parameters. */
8685 int variadic_p = 0;
8686 int variadic_args_p = 0;
8687 int post_variadic_parms = 0;
8688
8689 /* Adjustment to nparms for fixed parameter packs. */
8690 int fixed_pack_adjust = 0;
8691 int fixed_packs = 0;
8692 int missing = 0;
8693
8694 /* Likewise for parameters with default arguments. */
8695 int default_p = 0;
8696
8697 if (args == error_mark_node)
8698 return error_mark_node;
8699
8700 nparms = TREE_VEC_LENGTH (parms);
8701
8702 /* Determine if there are any parameter packs or default arguments. */
8703 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8704 {
8705 tree parm = TREE_VEC_ELT (parms, parm_idx);
8706 if (variadic_p)
8707 ++post_variadic_parms;
8708 if (template_parameter_pack_p (TREE_VALUE (parm)))
8709 ++variadic_p;
8710 if (TREE_PURPOSE (parm))
8711 ++default_p;
8712 }
8713
8714 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8715 /* If there are no parameters that follow a parameter pack, we need to
8716 expand any argument packs so that we can deduce a parameter pack from
8717 some non-packed args followed by an argument pack, as in variadic85.C.
8718 If there are such parameters, we need to leave argument packs intact
8719 so the arguments are assigned properly. This can happen when dealing
8720 with a nested class inside a partial specialization of a class
8721 template, as in variadic92.C, or when deducing a template parameter pack
8722 from a sub-declarator, as in variadic114.C. */
8723 if (!post_variadic_parms)
8724 inner_args = expand_template_argument_pack (inner_args);
8725
8726 /* Count any pack expansion args. */
8727 variadic_args_p = pack_expansion_args_count (inner_args);
8728
8729 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8730 if ((nargs - variadic_args_p > nparms && !variadic_p)
8731 || (nargs < nparms - variadic_p
8732 && require_all_args
8733 && !variadic_args_p
8734 && (!use_default_args
8735 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8736 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8737 {
8738 bad_nargs:
8739 if (complain & tf_error)
8740 {
8741 if (variadic_p || default_p)
8742 {
8743 nparms -= variadic_p + default_p;
8744 error ("wrong number of template arguments "
8745 "(%d, should be at least %d)", nargs, nparms);
8746 }
8747 else
8748 error ("wrong number of template arguments "
8749 "(%d, should be %d)", nargs, nparms);
8750
8751 if (in_decl)
8752 inform (DECL_SOURCE_LOCATION (in_decl),
8753 "provided for %qD", in_decl);
8754 }
8755
8756 return error_mark_node;
8757 }
8758 /* We can't pass a pack expansion to a non-pack parameter of an alias
8759 template (DR 1430). */
8760 else if (in_decl
8761 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8762 || concept_definition_p (in_decl))
8763 && variadic_args_p
8764 && nargs - variadic_args_p < nparms - variadic_p)
8765 {
8766 if (complain & tf_error)
8767 {
8768 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8769 {
8770 tree arg = TREE_VEC_ELT (inner_args, i);
8771 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8772
8773 if (PACK_EXPANSION_P (arg)
8774 && !template_parameter_pack_p (parm))
8775 {
8776 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8777 error_at (location_of (arg),
8778 "pack expansion argument for non-pack parameter "
8779 "%qD of alias template %qD", parm, in_decl);
8780 else
8781 error_at (location_of (arg),
8782 "pack expansion argument for non-pack parameter "
8783 "%qD of concept %qD", parm, in_decl);
8784 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8785 goto found;
8786 }
8787 }
8788 gcc_unreachable ();
8789 found:;
8790 }
8791 return error_mark_node;
8792 }
8793
8794 /* We need to evaluate the template arguments, even though this
8795 template-id may be nested within a "sizeof". */
8796 cp_evaluated ev;
8797
8798 new_inner_args = make_tree_vec (nparms);
8799 new_args = add_outermost_template_args (args, new_inner_args);
8800 int pack_adjust = 0;
8801 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8802 {
8803 tree arg;
8804 tree parm;
8805
8806 /* Get the Ith template parameter. */
8807 parm = TREE_VEC_ELT (parms, parm_idx);
8808
8809 if (parm == error_mark_node)
8810 {
8811 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8812 continue;
8813 }
8814
8815 /* Calculate the next argument. */
8816 if (arg_idx < nargs)
8817 arg = TREE_VEC_ELT (inner_args, arg_idx);
8818 else
8819 arg = NULL_TREE;
8820
8821 if (template_parameter_pack_p (TREE_VALUE (parm))
8822 && (arg || require_all_args || !(complain & tf_partial))
8823 && !(arg && ARGUMENT_PACK_P (arg)))
8824 {
8825 /* Some arguments will be placed in the
8826 template parameter pack PARM. */
8827 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8828 inner_args, arg_idx,
8829 new_args, &lost,
8830 in_decl, complain);
8831
8832 if (arg == NULL_TREE)
8833 {
8834 /* We don't know how many args we have yet, just use the
8835 unconverted (and still packed) ones for now. */
8836 new_inner_args = orig_inner_args;
8837 arg_idx = nargs;
8838 break;
8839 }
8840
8841 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8842
8843 /* Store this argument. */
8844 if (arg == error_mark_node)
8845 {
8846 lost++;
8847 /* We are done with all of the arguments. */
8848 arg_idx = nargs;
8849 break;
8850 }
8851 else
8852 {
8853 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8854 arg_idx += pack_adjust;
8855 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8856 {
8857 ++fixed_packs;
8858 fixed_pack_adjust += pack_adjust;
8859 }
8860 }
8861
8862 continue;
8863 }
8864 else if (arg)
8865 {
8866 if (PACK_EXPANSION_P (arg))
8867 {
8868 /* "If every valid specialization of a variadic template
8869 requires an empty template parameter pack, the template is
8870 ill-formed, no diagnostic required." So check that the
8871 pattern works with this parameter. */
8872 tree pattern = PACK_EXPANSION_PATTERN (arg);
8873 tree conv = convert_template_argument (TREE_VALUE (parm),
8874 pattern, new_args,
8875 complain, parm_idx,
8876 in_decl);
8877 if (conv == error_mark_node)
8878 {
8879 if (complain & tf_error)
8880 inform (input_location, "so any instantiation with a "
8881 "non-empty parameter pack would be ill-formed");
8882 ++lost;
8883 }
8884 else if (TYPE_P (conv) && !TYPE_P (pattern))
8885 /* Recover from missing typename. */
8886 TREE_VEC_ELT (inner_args, arg_idx)
8887 = make_pack_expansion (conv, complain);
8888
8889 /* We don't know how many args we have yet, just
8890 use the unconverted ones for now. */
8891 new_inner_args = inner_args;
8892 arg_idx = nargs;
8893 break;
8894 }
8895 }
8896 else if (require_all_args)
8897 {
8898 /* There must be a default arg in this case. */
8899 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8900 complain, in_decl);
8901 /* The position of the first default template argument,
8902 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8903 Record that. */
8904 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8905 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8906 arg_idx - pack_adjust);
8907 }
8908 else
8909 break;
8910
8911 if (arg == error_mark_node)
8912 {
8913 if (complain & tf_error)
8914 error ("template argument %d is invalid", arg_idx + 1);
8915 }
8916 else if (!arg)
8917 {
8918 /* This can occur if there was an error in the template
8919 parameter list itself (which we would already have
8920 reported) that we are trying to recover from, e.g., a class
8921 template with a parameter list such as
8922 template<typename..., typename> (cpp0x/variadic150.C). */
8923 ++lost;
8924
8925 /* This can also happen with a fixed parameter pack (71834). */
8926 if (arg_idx >= nargs)
8927 ++missing;
8928 }
8929 else
8930 arg = convert_template_argument (TREE_VALUE (parm),
8931 arg, new_args, complain,
8932 parm_idx, in_decl);
8933
8934 if (arg == error_mark_node)
8935 lost++;
8936
8937 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8938 }
8939
8940 if (missing || arg_idx < nargs - variadic_args_p)
8941 {
8942 /* If we had fixed parameter packs, we didn't know how many arguments we
8943 actually needed earlier; now we do. */
8944 nparms += fixed_pack_adjust;
8945 variadic_p -= fixed_packs;
8946 goto bad_nargs;
8947 }
8948
8949 if (arg_idx < nargs)
8950 {
8951 /* We had some pack expansion arguments that will only work if the packs
8952 are empty, but wait until instantiation time to complain.
8953 See variadic-ttp3.C. */
8954
8955 /* Except that we can't provide empty packs to alias templates or
8956 concepts when there are no corresponding parameters. Basically,
8957 we can get here with this:
8958
8959 template<typename T> concept C = true;
8960
8961 template<typename... Args>
8962 requires C<Args...>
8963 void f();
8964
8965 When parsing C<Args...>, we try to form a concept check of
8966 C<?, Args...>. Without the extra check for substituting an empty
8967 pack past the last parameter, we can accept the check as valid.
8968
8969 FIXME: This may be valid for alias templates (but I doubt it).
8970
8971 FIXME: The error could be better also. */
8972 if (in_decl && concept_definition_p (in_decl))
8973 {
8974 if (complain & tf_error)
8975 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8976 "too many arguments");
8977 return error_mark_node;
8978 }
8979
8980 int len = nparms + (nargs - arg_idx);
8981 tree args = make_tree_vec (len);
8982 int i = 0;
8983 for (; i < nparms; ++i)
8984 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8985 for (; i < len; ++i, ++arg_idx)
8986 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8987 arg_idx - pack_adjust);
8988 new_inner_args = args;
8989 }
8990
8991 if (lost)
8992 {
8993 gcc_assert (!(complain & tf_error) || seen_error ());
8994 return error_mark_node;
8995 }
8996
8997 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8998 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8999 TREE_VEC_LENGTH (new_inner_args));
9000
9001 return new_inner_args;
9002 }
9003
9004 /* Convert all template arguments to their appropriate types, and
9005 return a vector containing the innermost resulting template
9006 arguments. If any error occurs, return error_mark_node. Error and
9007 warning messages are not issued.
9008
9009 Note that no function argument deduction is performed, and default
9010 arguments are used to fill in unspecified arguments. */
9011 tree
9012 coerce_template_parms (tree parms, tree args, tree in_decl)
9013 {
9014 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9015 }
9016
9017 /* Convert all template arguments to their appropriate type, and
9018 instantiate default arguments as needed. This returns a vector
9019 containing the innermost resulting template arguments, or
9020 error_mark_node if unsuccessful. */
9021 tree
9022 coerce_template_parms (tree parms, tree args, tree in_decl,
9023 tsubst_flags_t complain)
9024 {
9025 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9026 }
9027
9028 /* Like coerce_template_parms. If PARMS represents all template
9029 parameters levels, this function returns a vector of vectors
9030 representing all the resulting argument levels. Note that in this
9031 case, only the innermost arguments are coerced because the
9032 outermost ones are supposed to have been coerced already.
9033
9034 Otherwise, if PARMS represents only (the innermost) vector of
9035 parameters, this function returns a vector containing just the
9036 innermost resulting arguments. */
9037
9038 static tree
9039 coerce_innermost_template_parms (tree parms,
9040 tree args,
9041 tree in_decl,
9042 tsubst_flags_t complain,
9043 bool require_all_args,
9044 bool use_default_args)
9045 {
9046 int parms_depth = TMPL_PARMS_DEPTH (parms);
9047 int args_depth = TMPL_ARGS_DEPTH (args);
9048 tree coerced_args;
9049
9050 if (parms_depth > 1)
9051 {
9052 coerced_args = make_tree_vec (parms_depth);
9053 tree level;
9054 int cur_depth;
9055
9056 for (level = parms, cur_depth = parms_depth;
9057 parms_depth > 0 && level != NULL_TREE;
9058 level = TREE_CHAIN (level), --cur_depth)
9059 {
9060 tree l;
9061 if (cur_depth == args_depth)
9062 l = coerce_template_parms (TREE_VALUE (level),
9063 args, in_decl, complain,
9064 require_all_args,
9065 use_default_args);
9066 else
9067 l = TMPL_ARGS_LEVEL (args, cur_depth);
9068
9069 if (l == error_mark_node)
9070 return error_mark_node;
9071
9072 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9073 }
9074 }
9075 else
9076 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9077 args, in_decl, complain,
9078 require_all_args,
9079 use_default_args);
9080 return coerced_args;
9081 }
9082
9083 /* Returns true if T is a wrapper to make a C++20 template parameter
9084 object const. */
9085
9086 static bool
9087 class_nttp_const_wrapper_p (tree t)
9088 {
9089 if (cxx_dialect < cxx20)
9090 return false;
9091 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9092 && CP_TYPE_CONST_P (TREE_TYPE (t))
9093 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9094 }
9095
9096 /* Returns 1 if template args OT and NT are equivalent. */
9097
9098 int
9099 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9100 {
9101 if (nt == ot)
9102 return 1;
9103 if (nt == NULL_TREE || ot == NULL_TREE)
9104 return false;
9105 if (nt == any_targ_node || ot == any_targ_node)
9106 return true;
9107
9108 if (class_nttp_const_wrapper_p (nt))
9109 nt = TREE_OPERAND (nt, 0);
9110 if (class_nttp_const_wrapper_p (ot))
9111 ot = TREE_OPERAND (ot, 0);
9112
9113 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9114 /* For member templates */
9115 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9116 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9117 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9118 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9119 PACK_EXPANSION_PATTERN (nt))
9120 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9121 PACK_EXPANSION_EXTRA_ARGS (nt)));
9122 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9123 return cp_tree_equal (ot, nt);
9124 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9125 gcc_unreachable ();
9126 else if (TYPE_P (nt) || TYPE_P (ot))
9127 {
9128 if (!(TYPE_P (nt) && TYPE_P (ot)))
9129 return false;
9130 /* Don't treat an alias template specialization with dependent
9131 arguments as equivalent to its underlying type when used as a
9132 template argument; we need them to be distinct so that we
9133 substitute into the specialization arguments at instantiation
9134 time. And aliases can't be equivalent without being ==, so
9135 we don't need to look any deeper.
9136
9137 During partial ordering, however, we need to treat them normally so
9138 that we can order uses of the same alias with different
9139 cv-qualification (79960). */
9140 if (!partial_order
9141 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9142 return false;
9143 else
9144 return same_type_p (ot, nt);
9145 }
9146 else
9147 {
9148 /* Try to treat a template non-type argument that has been converted
9149 to the parameter type as equivalent to one that hasn't yet. */
9150 for (enum tree_code code1 = TREE_CODE (ot);
9151 CONVERT_EXPR_CODE_P (code1)
9152 || code1 == NON_LVALUE_EXPR;
9153 code1 = TREE_CODE (ot))
9154 ot = TREE_OPERAND (ot, 0);
9155
9156 for (enum tree_code code2 = TREE_CODE (nt);
9157 CONVERT_EXPR_CODE_P (code2)
9158 || code2 == NON_LVALUE_EXPR;
9159 code2 = TREE_CODE (nt))
9160 nt = TREE_OPERAND (nt, 0);
9161
9162 return cp_tree_equal (ot, nt);
9163 }
9164 }
9165
9166 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9167 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9168 NEWARG_PTR with the offending arguments if they are non-NULL. */
9169
9170 int
9171 comp_template_args (tree oldargs, tree newargs,
9172 tree *oldarg_ptr, tree *newarg_ptr,
9173 bool partial_order)
9174 {
9175 int i;
9176
9177 if (oldargs == newargs)
9178 return 1;
9179
9180 if (!oldargs || !newargs)
9181 return 0;
9182
9183 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9184 return 0;
9185
9186 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9187 {
9188 tree nt = TREE_VEC_ELT (newargs, i);
9189 tree ot = TREE_VEC_ELT (oldargs, i);
9190
9191 if (! template_args_equal (ot, nt, partial_order))
9192 {
9193 if (oldarg_ptr != NULL)
9194 *oldarg_ptr = ot;
9195 if (newarg_ptr != NULL)
9196 *newarg_ptr = nt;
9197 return 0;
9198 }
9199 }
9200 return 1;
9201 }
9202
9203 inline bool
9204 comp_template_args_porder (tree oargs, tree nargs)
9205 {
9206 return comp_template_args (oargs, nargs, NULL, NULL, true);
9207 }
9208
9209 /* Implement a freelist interface for objects of type T.
9210
9211 Head is a separate object, rather than a regular member, so that we
9212 can define it as a GTY deletable pointer, which is highly
9213 desirable. A data member could be declared that way, but then the
9214 containing object would implicitly get GTY((user)), which would
9215 prevent us from instantiating freelists as global objects.
9216 Although this way we can create freelist global objects, they're
9217 such thin wrappers that instantiating temporaries at every use
9218 loses nothing and saves permanent storage for the freelist object.
9219
9220 Member functions next, anew, poison and reinit have default
9221 implementations that work for most of the types we're interested
9222 in, but if they don't work for some type, they should be explicitly
9223 specialized. See the comments before them for requirements, and
9224 the example specializations for the tree_list_freelist. */
9225 template <typename T>
9226 class freelist
9227 {
9228 /* Return the next object in a chain. We could just do type
9229 punning, but if we access the object with its underlying type, we
9230 avoid strict-aliasing trouble. This needs only work between
9231 poison and reinit. */
9232 static T *&next (T *obj) { return obj->next; }
9233
9234 /* Return a newly allocated, uninitialized or minimally-initialized
9235 object of type T. Any initialization performed by anew should
9236 either remain across the life of the object and the execution of
9237 poison, or be redone by reinit. */
9238 static T *anew () { return ggc_alloc<T> (); }
9239
9240 /* Optionally scribble all over the bits holding the object, so that
9241 they become (mostly?) uninitialized memory. This is called while
9242 preparing to make the object part of the free list. */
9243 static void poison (T *obj) {
9244 T *p ATTRIBUTE_UNUSED = obj;
9245 T **q ATTRIBUTE_UNUSED = &next (obj);
9246
9247 #ifdef ENABLE_GC_CHECKING
9248 /* Poison the data, to indicate the data is garbage. */
9249 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9250 memset (p, 0xa5, sizeof (*p));
9251 #endif
9252 /* Let valgrind know the object is free. */
9253 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9254
9255 /* Let valgrind know the next portion of the object is available,
9256 but uninitialized. */
9257 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9258 }
9259
9260 /* Bring an object that underwent at least one lifecycle after anew
9261 and before the most recent free and poison, back to a usable
9262 state, reinitializing whatever is needed for it to be
9263 functionally equivalent to an object just allocated and returned
9264 by anew. This may poison or clear the next field, used by
9265 freelist housekeeping after poison was called. */
9266 static void reinit (T *obj) {
9267 T **q ATTRIBUTE_UNUSED = &next (obj);
9268
9269 #ifdef ENABLE_GC_CHECKING
9270 memset (q, 0xa5, sizeof (*q));
9271 #endif
9272 /* Let valgrind know the entire object is available, but
9273 uninitialized. */
9274 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9275 }
9276
9277 /* Reference a GTY-deletable pointer that points to the first object
9278 in the free list proper. */
9279 T *&head;
9280 public:
9281 /* Construct a freelist object chaining objects off of HEAD. */
9282 freelist (T *&head) : head(head) {}
9283
9284 /* Add OBJ to the free object list. The former head becomes OBJ's
9285 successor. */
9286 void free (T *obj)
9287 {
9288 poison (obj);
9289 next (obj) = head;
9290 head = obj;
9291 }
9292
9293 /* Take an object from the free list, if one is available, or
9294 allocate a new one. Objects taken from the free list should be
9295 regarded as filled with garbage, except for bits that are
9296 configured to be preserved across free and alloc. */
9297 T *alloc ()
9298 {
9299 if (head)
9300 {
9301 T *obj = head;
9302 head = next (head);
9303 reinit (obj);
9304 return obj;
9305 }
9306 else
9307 return anew ();
9308 }
9309 };
9310
9311 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9312 want to allocate a TREE_LIST using the usual interface, and ensure
9313 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9314 build_tree_list logic in reinit, so this could go out of sync. */
9315 template <>
9316 inline tree &
9317 freelist<tree_node>::next (tree obj)
9318 {
9319 return TREE_CHAIN (obj);
9320 }
9321 template <>
9322 inline tree
9323 freelist<tree_node>::anew ()
9324 {
9325 return build_tree_list (NULL, NULL);
9326 }
9327 template <>
9328 inline void
9329 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9330 {
9331 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9332 tree p ATTRIBUTE_UNUSED = obj;
9333 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9334 tree *q ATTRIBUTE_UNUSED = &next (obj);
9335
9336 #ifdef ENABLE_GC_CHECKING
9337 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9338
9339 /* Poison the data, to indicate the data is garbage. */
9340 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9341 memset (p, 0xa5, size);
9342 #endif
9343 /* Let valgrind know the object is free. */
9344 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9345 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9346 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9347 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9348
9349 #ifdef ENABLE_GC_CHECKING
9350 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9351 /* Keep TREE_CHAIN functional. */
9352 TREE_SET_CODE (obj, TREE_LIST);
9353 #else
9354 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9355 #endif
9356 }
9357 template <>
9358 inline void
9359 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9360 {
9361 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9362
9363 #ifdef ENABLE_GC_CHECKING
9364 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9365 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9366 memset (obj, 0, sizeof (tree_list));
9367 #endif
9368
9369 /* Let valgrind know the entire object is available, but
9370 uninitialized. */
9371 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9372
9373 #ifdef ENABLE_GC_CHECKING
9374 TREE_SET_CODE (obj, TREE_LIST);
9375 #else
9376 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9377 #endif
9378 }
9379
9380 /* Point to the first object in the TREE_LIST freelist. */
9381 static GTY((deletable)) tree tree_list_freelist_head;
9382 /* Return the/an actual TREE_LIST freelist. */
9383 static inline freelist<tree_node>
9384 tree_list_freelist ()
9385 {
9386 return tree_list_freelist_head;
9387 }
9388
9389 /* Point to the first object in the tinst_level freelist. */
9390 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9391 /* Return the/an actual tinst_level freelist. */
9392 static inline freelist<tinst_level>
9393 tinst_level_freelist ()
9394 {
9395 return tinst_level_freelist_head;
9396 }
9397
9398 /* Point to the first object in the pending_template freelist. */
9399 static GTY((deletable)) pending_template *pending_template_freelist_head;
9400 /* Return the/an actual pending_template freelist. */
9401 static inline freelist<pending_template>
9402 pending_template_freelist ()
9403 {
9404 return pending_template_freelist_head;
9405 }
9406
9407 /* Build the TREE_LIST object out of a split list, store it
9408 permanently, and return it. */
9409 tree
9410 tinst_level::to_list ()
9411 {
9412 gcc_assert (split_list_p ());
9413 tree ret = tree_list_freelist ().alloc ();
9414 TREE_PURPOSE (ret) = tldcl;
9415 TREE_VALUE (ret) = targs;
9416 tldcl = ret;
9417 targs = NULL;
9418 gcc_assert (tree_list_p ());
9419 return ret;
9420 }
9421
9422 const unsigned short tinst_level::refcount_infinity;
9423
9424 /* Increment OBJ's refcount unless it is already infinite. */
9425 static tinst_level *
9426 inc_refcount_use (tinst_level *obj)
9427 {
9428 if (obj && obj->refcount != tinst_level::refcount_infinity)
9429 ++obj->refcount;
9430 return obj;
9431 }
9432
9433 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9434 void
9435 tinst_level::free (tinst_level *obj)
9436 {
9437 if (obj->tree_list_p ())
9438 tree_list_freelist ().free (obj->get_node ());
9439 tinst_level_freelist ().free (obj);
9440 }
9441
9442 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9443 OBJ's DECL and OBJ, and start over with the tinst_level object that
9444 used to be referenced by OBJ's NEXT. */
9445 static void
9446 dec_refcount_use (tinst_level *obj)
9447 {
9448 while (obj
9449 && obj->refcount != tinst_level::refcount_infinity
9450 && !--obj->refcount)
9451 {
9452 tinst_level *next = obj->next;
9453 tinst_level::free (obj);
9454 obj = next;
9455 }
9456 }
9457
9458 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9459 and of the former PTR. Omitting the second argument is equivalent
9460 to passing (T*)NULL; this is allowed because passing the
9461 zero-valued integral constant NULL confuses type deduction and/or
9462 overload resolution. */
9463 template <typename T>
9464 static void
9465 set_refcount_ptr (T *& ptr, T *obj = NULL)
9466 {
9467 T *save = ptr;
9468 ptr = inc_refcount_use (obj);
9469 dec_refcount_use (save);
9470 }
9471
9472 static void
9473 add_pending_template (tree d)
9474 {
9475 tree ti = (TYPE_P (d)
9476 ? CLASSTYPE_TEMPLATE_INFO (d)
9477 : DECL_TEMPLATE_INFO (d));
9478 struct pending_template *pt;
9479 int level;
9480
9481 if (TI_PENDING_TEMPLATE_FLAG (ti))
9482 return;
9483
9484 /* We are called both from instantiate_decl, where we've already had a
9485 tinst_level pushed, and instantiate_template, where we haven't.
9486 Compensate. */
9487 gcc_assert (TREE_CODE (d) != TREE_LIST);
9488 level = !current_tinst_level
9489 || current_tinst_level->maybe_get_node () != d;
9490
9491 if (level)
9492 push_tinst_level (d);
9493
9494 pt = pending_template_freelist ().alloc ();
9495 pt->next = NULL;
9496 pt->tinst = NULL;
9497 set_refcount_ptr (pt->tinst, current_tinst_level);
9498 if (last_pending_template)
9499 last_pending_template->next = pt;
9500 else
9501 pending_templates = pt;
9502
9503 last_pending_template = pt;
9504
9505 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9506
9507 if (level)
9508 pop_tinst_level ();
9509 }
9510
9511
9512 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9513 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9514 documentation for TEMPLATE_ID_EXPR. */
9515
9516 tree
9517 lookup_template_function (tree fns, tree arglist)
9518 {
9519 if (fns == error_mark_node || arglist == error_mark_node)
9520 return error_mark_node;
9521
9522 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9523
9524 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9525 {
9526 error ("%q#D is not a function template", fns);
9527 return error_mark_node;
9528 }
9529
9530 if (BASELINK_P (fns))
9531 {
9532 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9533 unknown_type_node,
9534 BASELINK_FUNCTIONS (fns),
9535 arglist);
9536 return fns;
9537 }
9538
9539 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9540 }
9541
9542 /* Within the scope of a template class S<T>, the name S gets bound
9543 (in build_self_reference) to a TYPE_DECL for the class, not a
9544 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9545 or one of its enclosing classes, and that type is a template,
9546 return the associated TEMPLATE_DECL. Otherwise, the original
9547 DECL is returned.
9548
9549 Also handle the case when DECL is a TREE_LIST of ambiguous
9550 injected-class-names from different bases. */
9551
9552 tree
9553 maybe_get_template_decl_from_type_decl (tree decl)
9554 {
9555 if (decl == NULL_TREE)
9556 return decl;
9557
9558 /* DR 176: A lookup that finds an injected-class-name (10.2
9559 [class.member.lookup]) can result in an ambiguity in certain cases
9560 (for example, if it is found in more than one base class). If all of
9561 the injected-class-names that are found refer to specializations of
9562 the same class template, and if the name is followed by a
9563 template-argument-list, the reference refers to the class template
9564 itself and not a specialization thereof, and is not ambiguous. */
9565 if (TREE_CODE (decl) == TREE_LIST)
9566 {
9567 tree t, tmpl = NULL_TREE;
9568 for (t = decl; t; t = TREE_CHAIN (t))
9569 {
9570 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9571 if (!tmpl)
9572 tmpl = elt;
9573 else if (tmpl != elt)
9574 break;
9575 }
9576 if (tmpl && t == NULL_TREE)
9577 return tmpl;
9578 else
9579 return decl;
9580 }
9581
9582 return (decl != NULL_TREE
9583 && DECL_SELF_REFERENCE_P (decl)
9584 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9585 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9586 }
9587
9588 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9589 parameters, find the desired type.
9590
9591 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9592
9593 IN_DECL, if non-NULL, is the template declaration we are trying to
9594 instantiate.
9595
9596 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9597 the class we are looking up.
9598
9599 Issue error and warning messages under control of COMPLAIN.
9600
9601 If the template class is really a local class in a template
9602 function, then the FUNCTION_CONTEXT is the function in which it is
9603 being instantiated.
9604
9605 ??? Note that this function is currently called *twice* for each
9606 template-id: the first time from the parser, while creating the
9607 incomplete type (finish_template_type), and the second type during the
9608 real instantiation (instantiate_template_class). This is surely something
9609 that we want to avoid. It also causes some problems with argument
9610 coercion (see convert_nontype_argument for more information on this). */
9611
9612 static tree
9613 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9614 int entering_scope, tsubst_flags_t complain)
9615 {
9616 tree templ = NULL_TREE, parmlist;
9617 tree t;
9618 spec_entry **slot;
9619 spec_entry *entry;
9620 spec_entry elt;
9621 hashval_t hash;
9622
9623 if (identifier_p (d1))
9624 {
9625 tree value = innermost_non_namespace_value (d1);
9626 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9627 templ = value;
9628 else
9629 {
9630 if (context)
9631 push_decl_namespace (context);
9632 templ = lookup_name (d1);
9633 templ = maybe_get_template_decl_from_type_decl (templ);
9634 if (context)
9635 pop_decl_namespace ();
9636 }
9637 if (templ)
9638 context = DECL_CONTEXT (templ);
9639 }
9640 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9641 {
9642 tree type = TREE_TYPE (d1);
9643
9644 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9645 an implicit typename for the second A. Deal with it. */
9646 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9647 type = TREE_TYPE (type);
9648
9649 if (CLASSTYPE_TEMPLATE_INFO (type))
9650 {
9651 templ = CLASSTYPE_TI_TEMPLATE (type);
9652 d1 = DECL_NAME (templ);
9653 }
9654 }
9655 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9656 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9657 {
9658 templ = TYPE_TI_TEMPLATE (d1);
9659 d1 = DECL_NAME (templ);
9660 }
9661 else if (DECL_TYPE_TEMPLATE_P (d1))
9662 {
9663 templ = d1;
9664 d1 = DECL_NAME (templ);
9665 context = DECL_CONTEXT (templ);
9666 }
9667 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9668 {
9669 templ = d1;
9670 d1 = DECL_NAME (templ);
9671 }
9672
9673 /* Issue an error message if we didn't find a template. */
9674 if (! templ)
9675 {
9676 if (complain & tf_error)
9677 error ("%qT is not a template", d1);
9678 return error_mark_node;
9679 }
9680
9681 if (TREE_CODE (templ) != TEMPLATE_DECL
9682 /* Make sure it's a user visible template, if it was named by
9683 the user. */
9684 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9685 && !PRIMARY_TEMPLATE_P (templ)))
9686 {
9687 if (complain & tf_error)
9688 {
9689 error ("non-template type %qT used as a template", d1);
9690 if (in_decl)
9691 error ("for template declaration %q+D", in_decl);
9692 }
9693 return error_mark_node;
9694 }
9695
9696 complain &= ~tf_user;
9697
9698 /* An alias that just changes the name of a template is equivalent to the
9699 other template, so if any of the arguments are pack expansions, strip
9700 the alias to avoid problems with a pack expansion passed to a non-pack
9701 alias template parameter (DR 1430). */
9702 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9703 templ = get_underlying_template (templ);
9704
9705 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9706 {
9707 tree parm;
9708 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9709 if (arglist2 == error_mark_node
9710 || (!uses_template_parms (arglist2)
9711 && check_instantiated_args (templ, arglist2, complain)))
9712 return error_mark_node;
9713
9714 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9715 return parm;
9716 }
9717 else
9718 {
9719 tree template_type = TREE_TYPE (templ);
9720 tree gen_tmpl;
9721 tree type_decl;
9722 tree found = NULL_TREE;
9723 int arg_depth;
9724 int parm_depth;
9725 int is_dependent_type;
9726 int use_partial_inst_tmpl = false;
9727
9728 if (template_type == error_mark_node)
9729 /* An error occurred while building the template TEMPL, and a
9730 diagnostic has most certainly been emitted for that
9731 already. Let's propagate that error. */
9732 return error_mark_node;
9733
9734 gen_tmpl = most_general_template (templ);
9735 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9736 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9737 arg_depth = TMPL_ARGS_DEPTH (arglist);
9738
9739 if (arg_depth == 1 && parm_depth > 1)
9740 {
9741 /* We've been given an incomplete set of template arguments.
9742 For example, given:
9743
9744 template <class T> struct S1 {
9745 template <class U> struct S2 {};
9746 template <class U> struct S2<U*> {};
9747 };
9748
9749 we will be called with an ARGLIST of `U*', but the
9750 TEMPLATE will be `template <class T> template
9751 <class U> struct S1<T>::S2'. We must fill in the missing
9752 arguments. */
9753 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9754 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9755 arg_depth = TMPL_ARGS_DEPTH (arglist);
9756 }
9757
9758 /* Now we should have enough arguments. */
9759 gcc_assert (parm_depth == arg_depth);
9760
9761 /* From here on, we're only interested in the most general
9762 template. */
9763
9764 /* Calculate the BOUND_ARGS. These will be the args that are
9765 actually tsubst'd into the definition to create the
9766 instantiation. */
9767 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9768 complain,
9769 /*require_all_args=*/true,
9770 /*use_default_args=*/true);
9771
9772 if (arglist == error_mark_node)
9773 /* We were unable to bind the arguments. */
9774 return error_mark_node;
9775
9776 /* In the scope of a template class, explicit references to the
9777 template class refer to the type of the template, not any
9778 instantiation of it. For example, in:
9779
9780 template <class T> class C { void f(C<T>); }
9781
9782 the `C<T>' is just the same as `C'. Outside of the
9783 class, however, such a reference is an instantiation. */
9784 if (entering_scope
9785 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9786 || currently_open_class (template_type))
9787 {
9788 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9789
9790 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9791 return template_type;
9792 }
9793
9794 /* If we already have this specialization, return it. */
9795 elt.tmpl = gen_tmpl;
9796 elt.args = arglist;
9797 elt.spec = NULL_TREE;
9798 hash = spec_hasher::hash (&elt);
9799 entry = type_specializations->find_with_hash (&elt, hash);
9800
9801 if (entry)
9802 return entry->spec;
9803
9804 /* If the template's constraints are not satisfied,
9805 then we cannot form a valid type.
9806
9807 Note that the check is deferred until after the hash
9808 lookup. This prevents redundant checks on previously
9809 instantiated specializations. */
9810 if (flag_concepts
9811 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9812 && !constraints_satisfied_p (gen_tmpl, arglist))
9813 {
9814 if (complain & tf_error)
9815 {
9816 auto_diagnostic_group d;
9817 error ("template constraint failure for %qD", gen_tmpl);
9818 diagnose_constraints (input_location, gen_tmpl, arglist);
9819 }
9820 return error_mark_node;
9821 }
9822
9823 is_dependent_type = uses_template_parms (arglist);
9824
9825 /* If the deduced arguments are invalid, then the binding
9826 failed. */
9827 if (!is_dependent_type
9828 && check_instantiated_args (gen_tmpl,
9829 INNERMOST_TEMPLATE_ARGS (arglist),
9830 complain))
9831 return error_mark_node;
9832
9833 if (!is_dependent_type
9834 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9835 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9836 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9837 {
9838 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9839 DECL_NAME (gen_tmpl),
9840 /*tag_scope=*/ts_global);
9841 return found;
9842 }
9843
9844 context = DECL_CONTEXT (gen_tmpl);
9845 if (context && TYPE_P (context))
9846 {
9847 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9848 context = complete_type (context);
9849 }
9850 else
9851 context = tsubst (context, arglist, complain, in_decl);
9852
9853 if (context == error_mark_node)
9854 return error_mark_node;
9855
9856 if (!context)
9857 context = global_namespace;
9858
9859 /* Create the type. */
9860 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9861 {
9862 /* The user referred to a specialization of an alias
9863 template represented by GEN_TMPL.
9864
9865 [temp.alias]/2 says:
9866
9867 When a template-id refers to the specialization of an
9868 alias template, it is equivalent to the associated
9869 type obtained by substitution of its
9870 template-arguments for the template-parameters in the
9871 type-id of the alias template. */
9872
9873 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9874 /* Note that the call above (by indirectly calling
9875 register_specialization in tsubst_decl) registers the
9876 TYPE_DECL representing the specialization of the alias
9877 template. So next time someone substitutes ARGLIST for
9878 the template parms into the alias template (GEN_TMPL),
9879 she'll get that TYPE_DECL back. */
9880
9881 if (t == error_mark_node)
9882 return t;
9883 }
9884 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9885 {
9886 if (!is_dependent_type)
9887 {
9888 set_current_access_from_decl (TYPE_NAME (template_type));
9889 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9890 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9891 arglist, complain, in_decl),
9892 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9893 arglist, complain, in_decl),
9894 SCOPED_ENUM_P (template_type), NULL);
9895
9896 if (t == error_mark_node)
9897 return t;
9898 }
9899 else
9900 {
9901 /* We don't want to call start_enum for this type, since
9902 the values for the enumeration constants may involve
9903 template parameters. And, no one should be interested
9904 in the enumeration constants for such a type. */
9905 t = cxx_make_type (ENUMERAL_TYPE);
9906 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9907 }
9908 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9909 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9910 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9911 }
9912 else if (CLASS_TYPE_P (template_type))
9913 {
9914 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9915 instantiated here. */
9916 gcc_assert (!LAMBDA_TYPE_P (template_type));
9917
9918 t = make_class_type (TREE_CODE (template_type));
9919 CLASSTYPE_DECLARED_CLASS (t)
9920 = CLASSTYPE_DECLARED_CLASS (template_type);
9921 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9922
9923 /* A local class. Make sure the decl gets registered properly. */
9924 if (context == current_function_decl)
9925 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9926 == error_mark_node)
9927 return error_mark_node;
9928
9929 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9930 /* This instantiation is another name for the primary
9931 template type. Set the TYPE_CANONICAL field
9932 appropriately. */
9933 TYPE_CANONICAL (t) = template_type;
9934 else if (any_template_arguments_need_structural_equality_p (arglist))
9935 /* Some of the template arguments require structural
9936 equality testing, so this template class requires
9937 structural equality testing. */
9938 SET_TYPE_STRUCTURAL_EQUALITY (t);
9939 }
9940 else
9941 gcc_unreachable ();
9942
9943 /* If we called start_enum or pushtag above, this information
9944 will already be set up. */
9945 type_decl = TYPE_NAME (t);
9946 if (!type_decl)
9947 {
9948 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9949
9950 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9951 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9952 DECL_SOURCE_LOCATION (type_decl)
9953 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9954 }
9955
9956 if (CLASS_TYPE_P (template_type))
9957 {
9958 TREE_PRIVATE (type_decl)
9959 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9960 TREE_PROTECTED (type_decl)
9961 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9962 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9963 {
9964 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9965 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9966 }
9967 }
9968
9969 if (OVERLOAD_TYPE_P (t)
9970 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9971 {
9972 static const char *tags[] = {"abi_tag", "may_alias"};
9973
9974 for (unsigned ix = 0; ix != 2; ix++)
9975 {
9976 tree attributes
9977 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9978
9979 if (attributes)
9980 TYPE_ATTRIBUTES (t)
9981 = tree_cons (TREE_PURPOSE (attributes),
9982 TREE_VALUE (attributes),
9983 TYPE_ATTRIBUTES (t));
9984 }
9985 }
9986
9987 /* Let's consider the explicit specialization of a member
9988 of a class template specialization that is implicitly instantiated,
9989 e.g.:
9990 template<class T>
9991 struct S
9992 {
9993 template<class U> struct M {}; //#0
9994 };
9995
9996 template<>
9997 template<>
9998 struct S<int>::M<char> //#1
9999 {
10000 int i;
10001 };
10002 [temp.expl.spec]/4 says this is valid.
10003
10004 In this case, when we write:
10005 S<int>::M<char> m;
10006
10007 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10008 the one of #0.
10009
10010 When we encounter #1, we want to store the partial instantiation
10011 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10012
10013 For all cases other than this "explicit specialization of member of a
10014 class template", we just want to store the most general template into
10015 the CLASSTYPE_TI_TEMPLATE of M.
10016
10017 This case of "explicit specialization of member of a class template"
10018 only happens when:
10019 1/ the enclosing class is an instantiation of, and therefore not
10020 the same as, the context of the most general template, and
10021 2/ we aren't looking at the partial instantiation itself, i.e.
10022 the innermost arguments are not the same as the innermost parms of
10023 the most general template.
10024
10025 So it's only when 1/ and 2/ happens that we want to use the partial
10026 instantiation of the member template in lieu of its most general
10027 template. */
10028
10029 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10030 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10031 /* the enclosing class must be an instantiation... */
10032 && CLASS_TYPE_P (context)
10033 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10034 {
10035 TREE_VEC_LENGTH (arglist)--;
10036 ++processing_template_decl;
10037 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10038 tree partial_inst_args =
10039 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10040 arglist, complain, NULL_TREE);
10041 --processing_template_decl;
10042 TREE_VEC_LENGTH (arglist)++;
10043 if (partial_inst_args == error_mark_node)
10044 return error_mark_node;
10045 use_partial_inst_tmpl =
10046 /*...and we must not be looking at the partial instantiation
10047 itself. */
10048 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10049 partial_inst_args);
10050 }
10051
10052 if (!use_partial_inst_tmpl)
10053 /* This case is easy; there are no member templates involved. */
10054 found = gen_tmpl;
10055 else
10056 {
10057 /* This is a full instantiation of a member template. Find
10058 the partial instantiation of which this is an instance. */
10059
10060 /* Temporarily reduce by one the number of levels in the ARGLIST
10061 so as to avoid comparing the last set of arguments. */
10062 TREE_VEC_LENGTH (arglist)--;
10063 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
10064 TREE_VEC_LENGTH (arglist)++;
10065 /* FOUND is either a proper class type, or an alias
10066 template specialization. In the later case, it's a
10067 TYPE_DECL, resulting from the substituting of arguments
10068 for parameters in the TYPE_DECL of the alias template
10069 done earlier. So be careful while getting the template
10070 of FOUND. */
10071 found = (TREE_CODE (found) == TEMPLATE_DECL
10072 ? found
10073 : (TREE_CODE (found) == TYPE_DECL
10074 ? DECL_TI_TEMPLATE (found)
10075 : CLASSTYPE_TI_TEMPLATE (found)));
10076
10077 if (DECL_CLASS_TEMPLATE_P (found)
10078 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10079 {
10080 /* If this partial instantiation is specialized, we want to
10081 use it for hash table lookup. */
10082 elt.tmpl = found;
10083 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10084 hash = spec_hasher::hash (&elt);
10085 }
10086 }
10087
10088 // Build template info for the new specialization.
10089 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10090
10091 elt.spec = t;
10092 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10093 gcc_checking_assert (*slot == NULL);
10094 entry = ggc_alloc<spec_entry> ();
10095 *entry = elt;
10096 *slot = entry;
10097
10098 /* Note this use of the partial instantiation so we can check it
10099 later in maybe_process_partial_specialization. */
10100 DECL_TEMPLATE_INSTANTIATIONS (found)
10101 = tree_cons (arglist, t,
10102 DECL_TEMPLATE_INSTANTIATIONS (found));
10103
10104 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10105 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10106 /* Now that the type has been registered on the instantiations
10107 list, we set up the enumerators. Because the enumeration
10108 constants may involve the enumeration type itself, we make
10109 sure to register the type first, and then create the
10110 constants. That way, doing tsubst_expr for the enumeration
10111 constants won't result in recursive calls here; we'll find
10112 the instantiation and exit above. */
10113 tsubst_enum (template_type, t, arglist);
10114
10115 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10116 /* If the type makes use of template parameters, the
10117 code that generates debugging information will crash. */
10118 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10119
10120 /* Possibly limit visibility based on template args. */
10121 TREE_PUBLIC (type_decl) = 1;
10122 determine_visibility (type_decl);
10123
10124 inherit_targ_abi_tags (t);
10125
10126 return t;
10127 }
10128 }
10129
10130 /* Wrapper for lookup_template_class_1. */
10131
10132 tree
10133 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10134 int entering_scope, tsubst_flags_t complain)
10135 {
10136 tree ret;
10137 timevar_push (TV_TEMPLATE_INST);
10138 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10139 entering_scope, complain);
10140 timevar_pop (TV_TEMPLATE_INST);
10141 return ret;
10142 }
10143
10144 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10145
10146 tree
10147 lookup_template_variable (tree templ, tree arglist)
10148 {
10149 if (flag_concepts && variable_concept_p (templ))
10150 return build_concept_check (templ, arglist, tf_none);
10151
10152 /* The type of the expression is NULL_TREE since the template-id could refer
10153 to an explicit or partial specialization. */
10154 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10155 }
10156
10157 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10158
10159 tree
10160 finish_template_variable (tree var, tsubst_flags_t complain)
10161 {
10162 tree templ = TREE_OPERAND (var, 0);
10163 tree arglist = TREE_OPERAND (var, 1);
10164
10165 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10166 arglist = add_outermost_template_args (tmpl_args, arglist);
10167
10168 templ = most_general_template (templ);
10169 tree parms = DECL_TEMPLATE_PARMS (templ);
10170 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10171 /*req_all*/true,
10172 /*use_default*/true);
10173 if (arglist == error_mark_node)
10174 return error_mark_node;
10175
10176 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10177 {
10178 if (complain & tf_error)
10179 {
10180 auto_diagnostic_group d;
10181 error ("use of invalid variable template %qE", var);
10182 diagnose_constraints (location_of (var), templ, arglist);
10183 }
10184 return error_mark_node;
10185 }
10186
10187 return instantiate_template (templ, arglist, complain);
10188 }
10189
10190 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10191 TARGS template args, and instantiate it if it's not dependent. */
10192
10193 tree
10194 lookup_and_finish_template_variable (tree templ, tree targs,
10195 tsubst_flags_t complain)
10196 {
10197 templ = lookup_template_variable (templ, targs);
10198 if (!any_dependent_template_arguments_p (targs))
10199 {
10200 templ = finish_template_variable (templ, complain);
10201 mark_used (templ);
10202 }
10203
10204 return convert_from_reference (templ);
10205 }
10206
10207 \f
10208 struct pair_fn_data
10209 {
10210 tree_fn_t fn;
10211 tree_fn_t any_fn;
10212 void *data;
10213 /* True when we should also visit template parameters that occur in
10214 non-deduced contexts. */
10215 bool include_nondeduced_p;
10216 hash_set<tree> *visited;
10217 };
10218
10219 /* Called from for_each_template_parm via walk_tree. */
10220
10221 static tree
10222 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10223 {
10224 tree t = *tp;
10225 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10226 tree_fn_t fn = pfd->fn;
10227 void *data = pfd->data;
10228 tree result = NULL_TREE;
10229
10230 #define WALK_SUBTREE(NODE) \
10231 do \
10232 { \
10233 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10234 pfd->include_nondeduced_p, \
10235 pfd->any_fn); \
10236 if (result) goto out; \
10237 } \
10238 while (0)
10239
10240 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10241 return t;
10242
10243 if (TYPE_P (t)
10244 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10245 WALK_SUBTREE (TYPE_CONTEXT (t));
10246
10247 switch (TREE_CODE (t))
10248 {
10249 case RECORD_TYPE:
10250 if (TYPE_PTRMEMFUNC_P (t))
10251 break;
10252 /* Fall through. */
10253
10254 case UNION_TYPE:
10255 case ENUMERAL_TYPE:
10256 if (!TYPE_TEMPLATE_INFO (t))
10257 *walk_subtrees = 0;
10258 else
10259 WALK_SUBTREE (TYPE_TI_ARGS (t));
10260 break;
10261
10262 case INTEGER_TYPE:
10263 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10264 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10265 break;
10266
10267 case METHOD_TYPE:
10268 /* Since we're not going to walk subtrees, we have to do this
10269 explicitly here. */
10270 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10271 /* Fall through. */
10272
10273 case FUNCTION_TYPE:
10274 /* Check the return type. */
10275 WALK_SUBTREE (TREE_TYPE (t));
10276
10277 /* Check the parameter types. Since default arguments are not
10278 instantiated until they are needed, the TYPE_ARG_TYPES may
10279 contain expressions that involve template parameters. But,
10280 no-one should be looking at them yet. And, once they're
10281 instantiated, they don't contain template parameters, so
10282 there's no point in looking at them then, either. */
10283 {
10284 tree parm;
10285
10286 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10287 WALK_SUBTREE (TREE_VALUE (parm));
10288
10289 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10290 want walk_tree walking into them itself. */
10291 *walk_subtrees = 0;
10292 }
10293
10294 if (flag_noexcept_type)
10295 {
10296 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10297 if (spec)
10298 WALK_SUBTREE (TREE_PURPOSE (spec));
10299 }
10300 break;
10301
10302 case TYPEOF_TYPE:
10303 case DECLTYPE_TYPE:
10304 case UNDERLYING_TYPE:
10305 if (pfd->include_nondeduced_p
10306 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10307 pfd->visited,
10308 pfd->include_nondeduced_p,
10309 pfd->any_fn))
10310 return error_mark_node;
10311 *walk_subtrees = false;
10312 break;
10313
10314 case FUNCTION_DECL:
10315 case VAR_DECL:
10316 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10317 WALK_SUBTREE (DECL_TI_ARGS (t));
10318 /* Fall through. */
10319
10320 case PARM_DECL:
10321 case CONST_DECL:
10322 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10323 WALK_SUBTREE (DECL_INITIAL (t));
10324 if (DECL_CONTEXT (t)
10325 && pfd->include_nondeduced_p)
10326 WALK_SUBTREE (DECL_CONTEXT (t));
10327 break;
10328
10329 case BOUND_TEMPLATE_TEMPLATE_PARM:
10330 /* Record template parameters such as `T' inside `TT<T>'. */
10331 WALK_SUBTREE (TYPE_TI_ARGS (t));
10332 /* Fall through. */
10333
10334 case TEMPLATE_TEMPLATE_PARM:
10335 case TEMPLATE_TYPE_PARM:
10336 case TEMPLATE_PARM_INDEX:
10337 if (fn && (*fn)(t, data))
10338 return t;
10339 else if (!fn)
10340 return t;
10341 break;
10342
10343 case TEMPLATE_DECL:
10344 /* A template template parameter is encountered. */
10345 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10346 WALK_SUBTREE (TREE_TYPE (t));
10347
10348 /* Already substituted template template parameter */
10349 *walk_subtrees = 0;
10350 break;
10351
10352 case TYPENAME_TYPE:
10353 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10354 partial instantiation. */
10355 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10356 *walk_subtrees = 0;
10357 break;
10358
10359 case CONSTRUCTOR:
10360 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10361 && pfd->include_nondeduced_p)
10362 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10363 break;
10364
10365 case INDIRECT_REF:
10366 case COMPONENT_REF:
10367 /* If there's no type, then this thing must be some expression
10368 involving template parameters. */
10369 if (!fn && !TREE_TYPE (t))
10370 return error_mark_node;
10371 break;
10372
10373 case MODOP_EXPR:
10374 case CAST_EXPR:
10375 case IMPLICIT_CONV_EXPR:
10376 case REINTERPRET_CAST_EXPR:
10377 case CONST_CAST_EXPR:
10378 case STATIC_CAST_EXPR:
10379 case DYNAMIC_CAST_EXPR:
10380 case ARROW_EXPR:
10381 case DOTSTAR_EXPR:
10382 case TYPEID_EXPR:
10383 case PSEUDO_DTOR_EXPR:
10384 if (!fn)
10385 return error_mark_node;
10386 break;
10387
10388 case SCOPE_REF:
10389 if (pfd->include_nondeduced_p)
10390 WALK_SUBTREE (TREE_OPERAND (t, 0));
10391 break;
10392
10393 case REQUIRES_EXPR:
10394 {
10395 if (!fn)
10396 return error_mark_node;
10397
10398 /* Recursively walk the type of each constraint variable. */
10399 tree p = TREE_OPERAND (t, 0);
10400 while (p)
10401 {
10402 WALK_SUBTREE (TREE_TYPE (p));
10403 p = TREE_CHAIN (p);
10404 }
10405 }
10406 break;
10407
10408 default:
10409 break;
10410 }
10411
10412 #undef WALK_SUBTREE
10413
10414 /* We didn't find any template parameters we liked. */
10415 out:
10416 return result;
10417 }
10418
10419 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10420 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10421 call FN with the parameter and the DATA.
10422 If FN returns nonzero, the iteration is terminated, and
10423 for_each_template_parm returns 1. Otherwise, the iteration
10424 continues. If FN never returns a nonzero value, the value
10425 returned by for_each_template_parm is 0. If FN is NULL, it is
10426 considered to be the function which always returns 1.
10427
10428 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10429 parameters that occur in non-deduced contexts. When false, only
10430 visits those template parameters that can be deduced. */
10431
10432 static tree
10433 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10434 hash_set<tree> *visited,
10435 bool include_nondeduced_p,
10436 tree_fn_t any_fn)
10437 {
10438 struct pair_fn_data pfd;
10439 tree result;
10440
10441 /* Set up. */
10442 pfd.fn = fn;
10443 pfd.any_fn = any_fn;
10444 pfd.data = data;
10445 pfd.include_nondeduced_p = include_nondeduced_p;
10446
10447 /* Walk the tree. (Conceptually, we would like to walk without
10448 duplicates, but for_each_template_parm_r recursively calls
10449 for_each_template_parm, so we would need to reorganize a fair
10450 bit to use walk_tree_without_duplicates, so we keep our own
10451 visited list.) */
10452 if (visited)
10453 pfd.visited = visited;
10454 else
10455 pfd.visited = new hash_set<tree>;
10456 result = cp_walk_tree (&t,
10457 for_each_template_parm_r,
10458 &pfd,
10459 pfd.visited);
10460
10461 /* Clean up. */
10462 if (!visited)
10463 {
10464 delete pfd.visited;
10465 pfd.visited = 0;
10466 }
10467
10468 return result;
10469 }
10470
10471 struct find_template_parameter_info
10472 {
10473 explicit find_template_parameter_info (tree ctx_parms)
10474 : parm_list (NULL_TREE),
10475 ctx_parms (ctx_parms),
10476 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10477 {}
10478
10479 hash_set<tree> visited;
10480 hash_set<tree> parms;
10481 tree parm_list;
10482 tree ctx_parms;
10483 int max_depth;
10484 };
10485
10486 /* Appends the declaration of T to the list in DATA. */
10487
10488 static int
10489 keep_template_parm (tree t, void* data)
10490 {
10491 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10492
10493 /* Template parameters declared within the expression are not part of
10494 the parameter mapping. For example, in this concept:
10495
10496 template<typename T>
10497 concept C = requires { <expr> } -> same_as<int>;
10498
10499 the return specifier same_as<int> declares a new decltype parameter
10500 that must not be part of the parameter mapping. The same is true
10501 for generic lambda parameters, lambda template parameters, etc. */
10502 int level;
10503 int index;
10504 template_parm_level_and_index (t, &level, &index);
10505 if (level > ftpi->max_depth)
10506 return 0;
10507
10508 /* Arguments like const T yield parameters like const T. This means that
10509 a template-id like X<T, const T> would yield two distinct parameters:
10510 T and const T. Adjust types to their unqualified versions. */
10511 if (TYPE_P (t))
10512 t = TYPE_MAIN_VARIANT (t);
10513 if (!ftpi->parms.add (t))
10514 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10515
10516 return 0;
10517 }
10518
10519 /* Ensure that we recursively examine certain terms that are not normally
10520 visited in for_each_template_parm_r. */
10521
10522 static int
10523 any_template_parm_r (tree t, void *data)
10524 {
10525 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10526
10527 #define WALK_SUBTREE(NODE) \
10528 do \
10529 { \
10530 for_each_template_parm (NODE, keep_template_parm, data, \
10531 &ftpi->visited, true, \
10532 any_template_parm_r); \
10533 } \
10534 while (0)
10535
10536 /* A mention of a member alias/typedef is a use of all of its template
10537 arguments, including those from the enclosing class, so we don't use
10538 alias_template_specialization_p here. */
10539 if (TYPE_P (t) && typedef_variant_p (t))
10540 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10541 WALK_SUBTREE (TI_ARGS (tinfo));
10542
10543 switch (TREE_CODE (t))
10544 {
10545 case TEMPLATE_TYPE_PARM:
10546 /* Type constraints of a placeholder type may contain parameters. */
10547 if (is_auto (t))
10548 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10549 WALK_SUBTREE (constr);
10550 break;
10551
10552 case TEMPLATE_ID_EXPR:
10553 /* Search through references to variable templates. */
10554 WALK_SUBTREE (TREE_OPERAND (t, 0));
10555 WALK_SUBTREE (TREE_OPERAND (t, 1));
10556 break;
10557
10558 case TEMPLATE_PARM_INDEX:
10559 case PARM_DECL:
10560 /* A parameter or constraint variable may also depend on a template
10561 parameter without explicitly naming it. */
10562 WALK_SUBTREE (TREE_TYPE (t));
10563 break;
10564
10565 case TEMPLATE_DECL:
10566 {
10567 /* If T is a member template that shares template parameters with
10568 ctx_parms, we need to mark all those parameters for mapping. */
10569 tree dparms = DECL_TEMPLATE_PARMS (t);
10570 tree cparms = ftpi->ctx_parms;
10571 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10572 dparms = TREE_CHAIN (dparms);
10573 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10574 cparms = TREE_CHAIN (cparms);
10575 while (dparms
10576 && (TREE_TYPE (TREE_VALUE (dparms))
10577 != TREE_TYPE (TREE_VALUE (cparms))))
10578 dparms = TREE_CHAIN (dparms),
10579 cparms = TREE_CHAIN (cparms);
10580 if (dparms)
10581 {
10582 int ddepth = TMPL_PARMS_DEPTH (dparms);
10583 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10584 for (int i = 0; i < ddepth; ++i)
10585 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10586 }
10587 }
10588 break;
10589
10590 case LAMBDA_EXPR:
10591 {
10592 /* Look in the parms and body. */
10593 tree fn = lambda_function (t);
10594 WALK_SUBTREE (TREE_TYPE (fn));
10595 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10596 }
10597 break;
10598
10599 case IDENTIFIER_NODE:
10600 if (IDENTIFIER_CONV_OP_P (t))
10601 /* The conversion-type-id of a conversion operator may be dependent. */
10602 WALK_SUBTREE (TREE_TYPE (t));
10603 break;
10604
10605 default:
10606 break;
10607 }
10608
10609 /* Keep walking. */
10610 return 0;
10611 }
10612
10613 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10614 are the template parameters in scope. */
10615
10616 tree
10617 find_template_parameters (tree t, tree ctx_parms)
10618 {
10619 if (!ctx_parms)
10620 return NULL_TREE;
10621
10622 find_template_parameter_info ftpi (ctx_parms);
10623 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10624 /*include_nondeduced*/true, any_template_parm_r);
10625 return ftpi.parm_list;
10626 }
10627
10628 /* Returns true if T depends on any template parameter. */
10629
10630 int
10631 uses_template_parms (tree t)
10632 {
10633 if (t == NULL_TREE)
10634 return false;
10635
10636 bool dependent_p;
10637 int saved_processing_template_decl;
10638
10639 saved_processing_template_decl = processing_template_decl;
10640 if (!saved_processing_template_decl)
10641 processing_template_decl = 1;
10642 if (TYPE_P (t))
10643 dependent_p = dependent_type_p (t);
10644 else if (TREE_CODE (t) == TREE_VEC)
10645 dependent_p = any_dependent_template_arguments_p (t);
10646 else if (TREE_CODE (t) == TREE_LIST)
10647 dependent_p = (uses_template_parms (TREE_VALUE (t))
10648 || uses_template_parms (TREE_CHAIN (t)));
10649 else if (TREE_CODE (t) == TYPE_DECL)
10650 dependent_p = dependent_type_p (TREE_TYPE (t));
10651 else if (t == error_mark_node)
10652 dependent_p = false;
10653 else
10654 dependent_p = value_dependent_expression_p (t);
10655
10656 processing_template_decl = saved_processing_template_decl;
10657
10658 return dependent_p;
10659 }
10660
10661 /* Returns true iff current_function_decl is an incompletely instantiated
10662 template. Useful instead of processing_template_decl because the latter
10663 is set to 0 during instantiate_non_dependent_expr. */
10664
10665 bool
10666 in_template_function (void)
10667 {
10668 tree fn = current_function_decl;
10669 bool ret;
10670 ++processing_template_decl;
10671 ret = (fn && DECL_LANG_SPECIFIC (fn)
10672 && DECL_TEMPLATE_INFO (fn)
10673 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10674 --processing_template_decl;
10675 return ret;
10676 }
10677
10678 /* Returns true if T depends on any template parameter with level LEVEL. */
10679
10680 bool
10681 uses_template_parms_level (tree t, int level)
10682 {
10683 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10684 /*include_nondeduced_p=*/true);
10685 }
10686
10687 /* Returns true if the signature of DECL depends on any template parameter from
10688 its enclosing class. */
10689
10690 bool
10691 uses_outer_template_parms (tree decl)
10692 {
10693 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10694 if (depth == 0)
10695 return false;
10696 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10697 &depth, NULL, /*include_nondeduced_p=*/true))
10698 return true;
10699 if (PRIMARY_TEMPLATE_P (decl)
10700 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10701 (DECL_TEMPLATE_PARMS (decl)),
10702 template_parm_outer_level,
10703 &depth, NULL, /*include_nondeduced_p=*/true))
10704 return true;
10705 tree ci = get_constraints (decl);
10706 if (ci)
10707 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10708 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10709 &depth, NULL, /*nondeduced*/true))
10710 return true;
10711 return false;
10712 }
10713
10714 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10715 ill-formed translation unit, i.e. a variable or function that isn't
10716 usable in a constant expression. */
10717
10718 static inline bool
10719 neglectable_inst_p (tree d)
10720 {
10721 return (d && DECL_P (d)
10722 && !undeduced_auto_decl (d)
10723 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10724 : decl_maybe_constant_var_p (d)));
10725 }
10726
10727 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10728 neglectable and instantiated from within an erroneous instantiation. */
10729
10730 static bool
10731 limit_bad_template_recursion (tree decl)
10732 {
10733 struct tinst_level *lev = current_tinst_level;
10734 int errs = errorcount + sorrycount;
10735 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10736 return false;
10737
10738 for (; lev; lev = lev->next)
10739 if (neglectable_inst_p (lev->maybe_get_node ()))
10740 break;
10741
10742 return (lev && errs > lev->errors);
10743 }
10744
10745 static int tinst_depth;
10746 extern int max_tinst_depth;
10747 int depth_reached;
10748
10749 static GTY(()) struct tinst_level *last_error_tinst_level;
10750
10751 /* We're starting to instantiate D; record the template instantiation context
10752 at LOC for diagnostics and to restore it later. */
10753
10754 static bool
10755 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10756 {
10757 struct tinst_level *new_level;
10758
10759 if (tinst_depth >= max_tinst_depth)
10760 {
10761 /* Tell error.c not to try to instantiate any templates. */
10762 at_eof = 2;
10763 fatal_error (input_location,
10764 "template instantiation depth exceeds maximum of %d"
10765 " (use %<-ftemplate-depth=%> to increase the maximum)",
10766 max_tinst_depth);
10767 return false;
10768 }
10769
10770 /* If the current instantiation caused problems, don't let it instantiate
10771 anything else. Do allow deduction substitution and decls usable in
10772 constant expressions. */
10773 if (!targs && limit_bad_template_recursion (tldcl))
10774 {
10775 /* Avoid no_linkage_errors and unused function warnings for this
10776 decl. */
10777 TREE_NO_WARNING (tldcl) = 1;
10778 return false;
10779 }
10780
10781 /* When not -quiet, dump template instantiations other than functions, since
10782 announce_function will take care of those. */
10783 if (!quiet_flag && !targs
10784 && TREE_CODE (tldcl) != TREE_LIST
10785 && TREE_CODE (tldcl) != FUNCTION_DECL)
10786 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10787
10788 new_level = tinst_level_freelist ().alloc ();
10789 new_level->tldcl = tldcl;
10790 new_level->targs = targs;
10791 new_level->locus = loc;
10792 new_level->errors = errorcount + sorrycount;
10793 new_level->next = NULL;
10794 new_level->refcount = 0;
10795 set_refcount_ptr (new_level->next, current_tinst_level);
10796 set_refcount_ptr (current_tinst_level, new_level);
10797
10798 ++tinst_depth;
10799 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10800 depth_reached = tinst_depth;
10801
10802 return true;
10803 }
10804
10805 /* We're starting substitution of TMPL<ARGS>; record the template
10806 substitution context for diagnostics and to restore it later. */
10807
10808 static bool
10809 push_tinst_level (tree tmpl, tree args)
10810 {
10811 return push_tinst_level_loc (tmpl, args, input_location);
10812 }
10813
10814 /* We're starting to instantiate D; record INPUT_LOCATION and the
10815 template instantiation context for diagnostics and to restore it
10816 later. */
10817
10818 bool
10819 push_tinst_level (tree d)
10820 {
10821 return push_tinst_level_loc (d, input_location);
10822 }
10823
10824 /* Likewise, but record LOC as the program location. */
10825
10826 bool
10827 push_tinst_level_loc (tree d, location_t loc)
10828 {
10829 gcc_assert (TREE_CODE (d) != TREE_LIST);
10830 return push_tinst_level_loc (d, NULL, loc);
10831 }
10832
10833 /* We're done instantiating this template; return to the instantiation
10834 context. */
10835
10836 void
10837 pop_tinst_level (void)
10838 {
10839 /* Restore the filename and line number stashed away when we started
10840 this instantiation. */
10841 input_location = current_tinst_level->locus;
10842 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10843 --tinst_depth;
10844 }
10845
10846 /* We're instantiating a deferred template; restore the template
10847 instantiation context in which the instantiation was requested, which
10848 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10849
10850 static tree
10851 reopen_tinst_level (struct tinst_level *level)
10852 {
10853 struct tinst_level *t;
10854
10855 tinst_depth = 0;
10856 for (t = level; t; t = t->next)
10857 ++tinst_depth;
10858
10859 set_refcount_ptr (current_tinst_level, level);
10860 pop_tinst_level ();
10861 if (current_tinst_level)
10862 current_tinst_level->errors = errorcount+sorrycount;
10863 return level->maybe_get_node ();
10864 }
10865
10866 /* Returns the TINST_LEVEL which gives the original instantiation
10867 context. */
10868
10869 struct tinst_level *
10870 outermost_tinst_level (void)
10871 {
10872 struct tinst_level *level = current_tinst_level;
10873 if (level)
10874 while (level->next)
10875 level = level->next;
10876 return level;
10877 }
10878
10879 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10880 vector of template arguments, as for tsubst.
10881
10882 Returns an appropriate tsubst'd friend declaration. */
10883
10884 static tree
10885 tsubst_friend_function (tree decl, tree args)
10886 {
10887 tree new_friend;
10888
10889 if (TREE_CODE (decl) == FUNCTION_DECL
10890 && DECL_TEMPLATE_INSTANTIATION (decl)
10891 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10892 /* This was a friend declared with an explicit template
10893 argument list, e.g.:
10894
10895 friend void f<>(T);
10896
10897 to indicate that f was a template instantiation, not a new
10898 function declaration. Now, we have to figure out what
10899 instantiation of what template. */
10900 {
10901 tree template_id, arglist, fns;
10902 tree new_args;
10903 tree tmpl;
10904 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10905
10906 /* Friend functions are looked up in the containing namespace scope.
10907 We must enter that scope, to avoid finding member functions of the
10908 current class with same name. */
10909 push_nested_namespace (ns);
10910 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10911 tf_warning_or_error, NULL_TREE,
10912 /*integral_constant_expression_p=*/false);
10913 pop_nested_namespace (ns);
10914 arglist = tsubst (DECL_TI_ARGS (decl), args,
10915 tf_warning_or_error, NULL_TREE);
10916 template_id = lookup_template_function (fns, arglist);
10917
10918 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10919 tmpl = determine_specialization (template_id, new_friend,
10920 &new_args,
10921 /*need_member_template=*/0,
10922 TREE_VEC_LENGTH (args),
10923 tsk_none);
10924 return instantiate_template (tmpl, new_args, tf_error);
10925 }
10926
10927 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10928 if (new_friend == error_mark_node)
10929 return error_mark_node;
10930
10931 /* The NEW_FRIEND will look like an instantiation, to the
10932 compiler, but is not an instantiation from the point of view of
10933 the language. For example, we might have had:
10934
10935 template <class T> struct S {
10936 template <class U> friend void f(T, U);
10937 };
10938
10939 Then, in S<int>, template <class U> void f(int, U) is not an
10940 instantiation of anything. */
10941
10942 DECL_USE_TEMPLATE (new_friend) = 0;
10943 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10944 {
10945 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10946 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10947 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10948
10949 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10950 match in decls_match. */
10951 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10952 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
10953 treqs = maybe_substitute_reqs_for (treqs, new_friend);
10954 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10955 }
10956
10957 /* The mangled name for the NEW_FRIEND is incorrect. The function
10958 is not a template instantiation and should not be mangled like
10959 one. Therefore, we forget the mangling here; we'll recompute it
10960 later if we need it. */
10961 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10962 {
10963 SET_DECL_RTL (new_friend, NULL);
10964 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10965 }
10966
10967 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10968 {
10969 tree old_decl;
10970 tree ns;
10971
10972 /* We must save some information from NEW_FRIEND before calling
10973 duplicate decls since that function will free NEW_FRIEND if
10974 possible. */
10975 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10976 tree new_friend_result_template_info = NULL_TREE;
10977 bool new_friend_is_defn =
10978 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10979 (template_for_substitution (new_friend)))
10980 != NULL_TREE);
10981 tree not_tmpl = new_friend;
10982
10983 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10984 {
10985 /* This declaration is a `primary' template. */
10986 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10987
10988 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
10989 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
10990 }
10991
10992 /* Inside pushdecl_namespace_level, we will push into the
10993 current namespace. However, the friend function should go
10994 into the namespace of the template. */
10995 ns = decl_namespace_context (new_friend);
10996 push_nested_namespace (ns);
10997 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10998 pop_nested_namespace (ns);
10999
11000 if (old_decl == error_mark_node)
11001 return error_mark_node;
11002
11003 if (old_decl != new_friend)
11004 {
11005 /* This new friend declaration matched an existing
11006 declaration. For example, given:
11007
11008 template <class T> void f(T);
11009 template <class U> class C {
11010 template <class T> friend void f(T) {}
11011 };
11012
11013 the friend declaration actually provides the definition
11014 of `f', once C has been instantiated for some type. So,
11015 old_decl will be the out-of-class template declaration,
11016 while new_friend is the in-class definition.
11017
11018 But, if `f' was called before this point, the
11019 instantiation of `f' will have DECL_TI_ARGS corresponding
11020 to `T' but not to `U', references to which might appear
11021 in the definition of `f'. Previously, the most general
11022 template for an instantiation of `f' was the out-of-class
11023 version; now it is the in-class version. Therefore, we
11024 run through all specialization of `f', adding to their
11025 DECL_TI_ARGS appropriately. In particular, they need a
11026 new set of outer arguments, corresponding to the
11027 arguments for this class instantiation.
11028
11029 The same situation can arise with something like this:
11030
11031 friend void f(int);
11032 template <class T> class C {
11033 friend void f(T) {}
11034 };
11035
11036 when `C<int>' is instantiated. Now, `f(int)' is defined
11037 in the class. */
11038
11039 if (!new_friend_is_defn)
11040 /* On the other hand, if the in-class declaration does
11041 *not* provide a definition, then we don't want to alter
11042 existing definitions. We can just leave everything
11043 alone. */
11044 ;
11045 else
11046 {
11047 tree new_template = TI_TEMPLATE (new_friend_template_info);
11048 tree new_args = TI_ARGS (new_friend_template_info);
11049
11050 /* Overwrite whatever template info was there before, if
11051 any, with the new template information pertaining to
11052 the declaration. */
11053 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11054
11055 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11056 {
11057 /* We should have called reregister_specialization in
11058 duplicate_decls. */
11059 gcc_assert (retrieve_specialization (new_template,
11060 new_args, 0)
11061 == old_decl);
11062
11063 /* Instantiate it if the global has already been used. */
11064 if (DECL_ODR_USED (old_decl))
11065 instantiate_decl (old_decl, /*defer_ok=*/true,
11066 /*expl_inst_class_mem_p=*/false);
11067 }
11068 else
11069 {
11070 tree t;
11071
11072 /* Indicate that the old function template is a partial
11073 instantiation. */
11074 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11075 = new_friend_result_template_info;
11076
11077 gcc_assert (new_template
11078 == most_general_template (new_template));
11079 gcc_assert (new_template != old_decl);
11080
11081 /* Reassign any specializations already in the hash table
11082 to the new more general template, and add the
11083 additional template args. */
11084 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11085 t != NULL_TREE;
11086 t = TREE_CHAIN (t))
11087 {
11088 tree spec = TREE_VALUE (t);
11089 spec_entry elt;
11090
11091 elt.tmpl = old_decl;
11092 elt.args = DECL_TI_ARGS (spec);
11093 elt.spec = NULL_TREE;
11094
11095 decl_specializations->remove_elt (&elt);
11096
11097 DECL_TI_ARGS (spec)
11098 = add_outermost_template_args (new_args,
11099 DECL_TI_ARGS (spec));
11100
11101 register_specialization
11102 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11103
11104 }
11105 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11106 }
11107 }
11108
11109 /* The information from NEW_FRIEND has been merged into OLD_DECL
11110 by duplicate_decls. */
11111 new_friend = old_decl;
11112 }
11113 }
11114 else
11115 {
11116 tree context = DECL_CONTEXT (new_friend);
11117 bool dependent_p;
11118
11119 /* In the code
11120 template <class T> class C {
11121 template <class U> friend void C1<U>::f (); // case 1
11122 friend void C2<T>::f (); // case 2
11123 };
11124 we only need to make sure CONTEXT is a complete type for
11125 case 2. To distinguish between the two cases, we note that
11126 CONTEXT of case 1 remains dependent type after tsubst while
11127 this isn't true for case 2. */
11128 ++processing_template_decl;
11129 dependent_p = dependent_type_p (context);
11130 --processing_template_decl;
11131
11132 if (!dependent_p
11133 && !complete_type_or_else (context, NULL_TREE))
11134 return error_mark_node;
11135
11136 if (COMPLETE_TYPE_P (context))
11137 {
11138 tree fn = new_friend;
11139 /* do_friend adds the TEMPLATE_DECL for any member friend
11140 template even if it isn't a member template, i.e.
11141 template <class T> friend A<T>::f();
11142 Look through it in that case. */
11143 if (TREE_CODE (fn) == TEMPLATE_DECL
11144 && !PRIMARY_TEMPLATE_P (fn))
11145 fn = DECL_TEMPLATE_RESULT (fn);
11146 /* Check to see that the declaration is really present, and,
11147 possibly obtain an improved declaration. */
11148 fn = check_classfn (context, fn, NULL_TREE);
11149
11150 if (fn)
11151 new_friend = fn;
11152 }
11153 }
11154
11155 return new_friend;
11156 }
11157
11158 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11159 template arguments, as for tsubst.
11160
11161 Returns an appropriate tsubst'd friend type or error_mark_node on
11162 failure. */
11163
11164 static tree
11165 tsubst_friend_class (tree friend_tmpl, tree args)
11166 {
11167 tree tmpl;
11168
11169 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11170 {
11171 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11172 return TREE_TYPE (tmpl);
11173 }
11174
11175 tree context = CP_DECL_CONTEXT (friend_tmpl);
11176 if (TREE_CODE (context) == NAMESPACE_DECL)
11177 push_nested_namespace (context);
11178 else
11179 {
11180 context = tsubst (context, args, tf_error, NULL_TREE);
11181 push_nested_class (context);
11182 }
11183
11184 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
11185 /*non_class=*/false, /*block_p=*/false,
11186 /*namespaces_only=*/false, LOOKUP_HIDDEN);
11187
11188 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11189 {
11190 /* The friend template has already been declared. Just
11191 check to see that the declarations match, and install any new
11192 default parameters. We must tsubst the default parameters,
11193 of course. We only need the innermost template parameters
11194 because that is all that redeclare_class_template will look
11195 at. */
11196 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11197 > TMPL_ARGS_DEPTH (args))
11198 {
11199 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11200 args, tf_warning_or_error);
11201 location_t saved_input_location = input_location;
11202 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11203 tree cons = get_constraints (tmpl);
11204 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11205 input_location = saved_input_location;
11206 }
11207 }
11208 else
11209 {
11210 /* The friend template has not already been declared. In this
11211 case, the instantiation of the template class will cause the
11212 injection of this template into the namespace scope. */
11213 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11214
11215 if (tmpl != error_mark_node)
11216 {
11217 /* The new TMPL is not an instantiation of anything, so we
11218 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11219 for the new type because that is supposed to be the
11220 corresponding template decl, i.e., TMPL. */
11221 DECL_USE_TEMPLATE (tmpl) = 0;
11222 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11223 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11224 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11225 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11226
11227 /* It is hidden. */
11228 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11229 DECL_ANTICIPATED (tmpl)
11230 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11231
11232 /* Substitute into and set the constraints on the new declaration. */
11233 if (tree ci = get_constraints (friend_tmpl))
11234 {
11235 ++processing_template_decl;
11236 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11237 DECL_FRIEND_CONTEXT (friend_tmpl));
11238 --processing_template_decl;
11239 set_constraints (tmpl, ci);
11240 }
11241
11242 /* Inject this template into the enclosing namspace scope. */
11243 tmpl = pushdecl_namespace_level (tmpl, true);
11244 }
11245 }
11246
11247 if (TREE_CODE (context) == NAMESPACE_DECL)
11248 pop_nested_namespace (context);
11249 else
11250 pop_nested_class ();
11251
11252 return TREE_TYPE (tmpl);
11253 }
11254
11255 /* Returns zero if TYPE cannot be completed later due to circularity.
11256 Otherwise returns one. */
11257
11258 static int
11259 can_complete_type_without_circularity (tree type)
11260 {
11261 if (type == NULL_TREE || type == error_mark_node)
11262 return 0;
11263 else if (COMPLETE_TYPE_P (type))
11264 return 1;
11265 else if (TREE_CODE (type) == ARRAY_TYPE)
11266 return can_complete_type_without_circularity (TREE_TYPE (type));
11267 else if (CLASS_TYPE_P (type)
11268 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11269 return 0;
11270 else
11271 return 1;
11272 }
11273
11274 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11275 tsubst_flags_t, tree);
11276
11277 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11278 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11279
11280 static tree
11281 tsubst_attribute (tree t, tree *decl_p, tree args,
11282 tsubst_flags_t complain, tree in_decl)
11283 {
11284 gcc_assert (ATTR_IS_DEPENDENT (t));
11285
11286 tree val = TREE_VALUE (t);
11287 if (val == NULL_TREE)
11288 /* Nothing to do. */;
11289 else if ((flag_openmp || flag_openmp_simd)
11290 && is_attribute_p ("omp declare simd",
11291 get_attribute_name (t)))
11292 {
11293 tree clauses = TREE_VALUE (val);
11294 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11295 complain, in_decl);
11296 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11297 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11298 tree parms = DECL_ARGUMENTS (*decl_p);
11299 clauses
11300 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11301 if (clauses)
11302 val = build_tree_list (NULL_TREE, clauses);
11303 else
11304 val = NULL_TREE;
11305 }
11306 else if (flag_openmp
11307 && is_attribute_p ("omp declare variant base",
11308 get_attribute_name (t)))
11309 {
11310 ++cp_unevaluated_operand;
11311 tree varid
11312 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11313 in_decl, /*integral_constant_expression_p=*/false);
11314 --cp_unevaluated_operand;
11315 tree chain = TREE_CHAIN (val);
11316 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11317 tree ctx = copy_list (TREE_VALUE (val));
11318 tree simd = get_identifier ("simd");
11319 tree score = get_identifier (" score");
11320 tree condition = get_identifier ("condition");
11321 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11322 {
11323 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11324 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11325 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11326 {
11327 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11328 {
11329 tree clauses = TREE_VALUE (t2);
11330 clauses = tsubst_omp_clauses (clauses,
11331 C_ORT_OMP_DECLARE_SIMD, args,
11332 complain, in_decl);
11333 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11334 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11335 TREE_VALUE (t2) = clauses;
11336 }
11337 else
11338 {
11339 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11340 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11341 if (TREE_VALUE (t3))
11342 {
11343 bool allow_string
11344 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11345 && TREE_PURPOSE (t3) != score);
11346 tree v = TREE_VALUE (t3);
11347 if (TREE_CODE (v) == STRING_CST && allow_string)
11348 continue;
11349 v = tsubst_expr (v, args, complain, in_decl, true);
11350 v = fold_non_dependent_expr (v);
11351 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11352 || (TREE_PURPOSE (t3) == score
11353 ? TREE_CODE (v) != INTEGER_CST
11354 : !tree_fits_shwi_p (v)))
11355 {
11356 location_t loc
11357 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11358 match_loc);
11359 if (TREE_PURPOSE (t3) == score)
11360 error_at (loc, "score argument must be "
11361 "constant integer expression");
11362 else if (allow_string)
11363 error_at (loc, "property must be constant "
11364 "integer expression or string "
11365 "literal");
11366 else
11367 error_at (loc, "property must be constant "
11368 "integer expression");
11369 return NULL_TREE;
11370 }
11371 else if (TREE_PURPOSE (t3) == score
11372 && tree_int_cst_sgn (v) < 0)
11373 {
11374 location_t loc
11375 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11376 match_loc);
11377 error_at (loc, "score argument must be "
11378 "non-negative");
11379 return NULL_TREE;
11380 }
11381 TREE_VALUE (t3) = v;
11382 }
11383 }
11384 }
11385 }
11386 val = tree_cons (varid, ctx, chain);
11387 }
11388 /* If the first attribute argument is an identifier, don't
11389 pass it through tsubst. Attributes like mode, format,
11390 cleanup and several target specific attributes expect it
11391 unmodified. */
11392 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11393 {
11394 tree chain
11395 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11396 /*integral_constant_expression_p=*/false);
11397 if (chain != TREE_CHAIN (val))
11398 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11399 }
11400 else if (PACK_EXPANSION_P (val))
11401 {
11402 /* An attribute pack expansion. */
11403 tree purp = TREE_PURPOSE (t);
11404 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11405 if (pack == error_mark_node)
11406 return error_mark_node;
11407 int len = TREE_VEC_LENGTH (pack);
11408 tree list = NULL_TREE;
11409 tree *q = &list;
11410 for (int i = 0; i < len; ++i)
11411 {
11412 tree elt = TREE_VEC_ELT (pack, i);
11413 *q = build_tree_list (purp, elt);
11414 q = &TREE_CHAIN (*q);
11415 }
11416 return list;
11417 }
11418 else
11419 val = tsubst_expr (val, args, complain, in_decl,
11420 /*integral_constant_expression_p=*/false);
11421
11422 if (val != TREE_VALUE (t))
11423 return build_tree_list (TREE_PURPOSE (t), val);
11424 return t;
11425 }
11426
11427 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11428 unchanged or a new TREE_LIST chain. */
11429
11430 static tree
11431 tsubst_attributes (tree attributes, tree args,
11432 tsubst_flags_t complain, tree in_decl)
11433 {
11434 tree last_dep = NULL_TREE;
11435
11436 for (tree t = attributes; t; t = TREE_CHAIN (t))
11437 if (ATTR_IS_DEPENDENT (t))
11438 {
11439 last_dep = t;
11440 attributes = copy_list (attributes);
11441 break;
11442 }
11443
11444 if (last_dep)
11445 for (tree *p = &attributes; *p; )
11446 {
11447 tree t = *p;
11448 if (ATTR_IS_DEPENDENT (t))
11449 {
11450 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11451 if (subst != t)
11452 {
11453 *p = subst;
11454 while (*p)
11455 p = &TREE_CHAIN (*p);
11456 *p = TREE_CHAIN (t);
11457 continue;
11458 }
11459 }
11460 p = &TREE_CHAIN (*p);
11461 }
11462
11463 return attributes;
11464 }
11465
11466 /* Apply any attributes which had to be deferred until instantiation
11467 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11468 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11469
11470 static void
11471 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11472 tree args, tsubst_flags_t complain, tree in_decl)
11473 {
11474 tree last_dep = NULL_TREE;
11475 tree t;
11476 tree *p;
11477
11478 if (attributes == NULL_TREE)
11479 return;
11480
11481 if (DECL_P (*decl_p))
11482 {
11483 if (TREE_TYPE (*decl_p) == error_mark_node)
11484 return;
11485 p = &DECL_ATTRIBUTES (*decl_p);
11486 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11487 to our attributes parameter. */
11488 gcc_assert (*p == attributes);
11489 }
11490 else
11491 {
11492 p = &TYPE_ATTRIBUTES (*decl_p);
11493 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11494 lookup_template_class_1, and should be preserved. */
11495 gcc_assert (*p != attributes);
11496 while (*p)
11497 p = &TREE_CHAIN (*p);
11498 }
11499
11500 for (t = attributes; t; t = TREE_CHAIN (t))
11501 if (ATTR_IS_DEPENDENT (t))
11502 {
11503 last_dep = t;
11504 attributes = copy_list (attributes);
11505 break;
11506 }
11507
11508 *p = attributes;
11509 if (last_dep)
11510 {
11511 tree late_attrs = NULL_TREE;
11512 tree *q = &late_attrs;
11513
11514 for (; *p; )
11515 {
11516 t = *p;
11517 if (ATTR_IS_DEPENDENT (t))
11518 {
11519 *p = TREE_CHAIN (t);
11520 TREE_CHAIN (t) = NULL_TREE;
11521 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11522 while (*q)
11523 q = &TREE_CHAIN (*q);
11524 }
11525 else
11526 p = &TREE_CHAIN (t);
11527 }
11528
11529 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11530 }
11531 }
11532
11533 /* The template TMPL is being instantiated with the template arguments TARGS.
11534 Perform the access checks that we deferred when parsing the template. */
11535
11536 static void
11537 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11538 {
11539 unsigned i;
11540 deferred_access_check *chk;
11541
11542 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11543 return;
11544
11545 if (vec<deferred_access_check, va_gc> *access_checks
11546 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11547 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11548 {
11549 tree decl = chk->decl;
11550 tree diag_decl = chk->diag_decl;
11551 tree type_scope = TREE_TYPE (chk->binfo);
11552
11553 if (uses_template_parms (type_scope))
11554 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11555
11556 /* Make access check error messages point to the location
11557 of the use of the typedef. */
11558 iloc_sentinel ils (chk->loc);
11559 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11560 decl, diag_decl, tf_warning_or_error);
11561 }
11562 }
11563
11564 static tree
11565 instantiate_class_template_1 (tree type)
11566 {
11567 tree templ, args, pattern, t, member;
11568 tree typedecl;
11569 tree pbinfo;
11570 tree base_list;
11571 unsigned int saved_maximum_field_alignment;
11572 tree fn_context;
11573
11574 if (type == error_mark_node)
11575 return error_mark_node;
11576
11577 if (COMPLETE_OR_OPEN_TYPE_P (type)
11578 || uses_template_parms (type))
11579 return type;
11580
11581 /* Figure out which template is being instantiated. */
11582 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11583 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11584
11585 /* Mark the type as in the process of being defined. */
11586 TYPE_BEING_DEFINED (type) = 1;
11587
11588 /* We may be in the middle of deferred access check. Disable
11589 it now. */
11590 deferring_access_check_sentinel acs (dk_no_deferred);
11591
11592 /* Determine what specialization of the original template to
11593 instantiate. */
11594 t = most_specialized_partial_spec (type, tf_warning_or_error);
11595 if (t == error_mark_node)
11596 return error_mark_node;
11597 else if (t)
11598 {
11599 /* This TYPE is actually an instantiation of a partial
11600 specialization. We replace the innermost set of ARGS with
11601 the arguments appropriate for substitution. For example,
11602 given:
11603
11604 template <class T> struct S {};
11605 template <class T> struct S<T*> {};
11606
11607 and supposing that we are instantiating S<int*>, ARGS will
11608 presently be {int*} -- but we need {int}. */
11609 pattern = TREE_TYPE (t);
11610 args = TREE_PURPOSE (t);
11611 }
11612 else
11613 {
11614 pattern = TREE_TYPE (templ);
11615 args = CLASSTYPE_TI_ARGS (type);
11616 }
11617
11618 /* If the template we're instantiating is incomplete, then clearly
11619 there's nothing we can do. */
11620 if (!COMPLETE_TYPE_P (pattern))
11621 {
11622 /* We can try again later. */
11623 TYPE_BEING_DEFINED (type) = 0;
11624 return type;
11625 }
11626
11627 /* If we've recursively instantiated too many templates, stop. */
11628 if (! push_tinst_level (type))
11629 return type;
11630
11631 int saved_unevaluated_operand = cp_unevaluated_operand;
11632 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11633
11634 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11635 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11636 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11637 fn_context = error_mark_node;
11638 if (!fn_context)
11639 push_to_top_level ();
11640 else
11641 {
11642 cp_unevaluated_operand = 0;
11643 c_inhibit_evaluation_warnings = 0;
11644 }
11645 /* Use #pragma pack from the template context. */
11646 saved_maximum_field_alignment = maximum_field_alignment;
11647 maximum_field_alignment = TYPE_PRECISION (pattern);
11648
11649 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11650
11651 /* Set the input location to the most specialized template definition.
11652 This is needed if tsubsting causes an error. */
11653 typedecl = TYPE_MAIN_DECL (pattern);
11654 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11655 DECL_SOURCE_LOCATION (typedecl);
11656
11657 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11658 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11659 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11660 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11661 if (ANON_AGGR_TYPE_P (pattern))
11662 SET_ANON_AGGR_TYPE_P (type);
11663 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11664 {
11665 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11666 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11667 /* Adjust visibility for template arguments. */
11668 determine_visibility (TYPE_MAIN_DECL (type));
11669 }
11670 if (CLASS_TYPE_P (type))
11671 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11672
11673 pbinfo = TYPE_BINFO (pattern);
11674
11675 /* We should never instantiate a nested class before its enclosing
11676 class; we need to look up the nested class by name before we can
11677 instantiate it, and that lookup should instantiate the enclosing
11678 class. */
11679 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11680 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11681
11682 base_list = NULL_TREE;
11683 if (BINFO_N_BASE_BINFOS (pbinfo))
11684 {
11685 tree pbase_binfo;
11686 tree pushed_scope;
11687 int i;
11688
11689 /* We must enter the scope containing the type, as that is where
11690 the accessibility of types named in dependent bases are
11691 looked up from. */
11692 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11693
11694 /* Substitute into each of the bases to determine the actual
11695 basetypes. */
11696 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11697 {
11698 tree base;
11699 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11700 tree expanded_bases = NULL_TREE;
11701 int idx, len = 1;
11702
11703 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11704 {
11705 expanded_bases =
11706 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11707 args, tf_error, NULL_TREE);
11708 if (expanded_bases == error_mark_node)
11709 continue;
11710
11711 len = TREE_VEC_LENGTH (expanded_bases);
11712 }
11713
11714 for (idx = 0; idx < len; idx++)
11715 {
11716 if (expanded_bases)
11717 /* Extract the already-expanded base class. */
11718 base = TREE_VEC_ELT (expanded_bases, idx);
11719 else
11720 /* Substitute to figure out the base class. */
11721 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11722 NULL_TREE);
11723
11724 if (base == error_mark_node)
11725 continue;
11726
11727 base_list = tree_cons (access, base, base_list);
11728 if (BINFO_VIRTUAL_P (pbase_binfo))
11729 TREE_TYPE (base_list) = integer_type_node;
11730 }
11731 }
11732
11733 /* The list is now in reverse order; correct that. */
11734 base_list = nreverse (base_list);
11735
11736 if (pushed_scope)
11737 pop_scope (pushed_scope);
11738 }
11739 /* Now call xref_basetypes to set up all the base-class
11740 information. */
11741 xref_basetypes (type, base_list);
11742
11743 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11744 (int) ATTR_FLAG_TYPE_IN_PLACE,
11745 args, tf_error, NULL_TREE);
11746 fixup_attribute_variants (type);
11747
11748 /* Now that our base classes are set up, enter the scope of the
11749 class, so that name lookups into base classes, etc. will work
11750 correctly. This is precisely analogous to what we do in
11751 begin_class_definition when defining an ordinary non-template
11752 class, except we also need to push the enclosing classes. */
11753 push_nested_class (type);
11754
11755 /* Now members are processed in the order of declaration. */
11756 for (member = CLASSTYPE_DECL_LIST (pattern);
11757 member; member = TREE_CHAIN (member))
11758 {
11759 tree t = TREE_VALUE (member);
11760
11761 if (TREE_PURPOSE (member))
11762 {
11763 if (TYPE_P (t))
11764 {
11765 if (LAMBDA_TYPE_P (t))
11766 /* A closure type for a lambda in an NSDMI or default argument.
11767 Ignore it; it will be regenerated when needed. */
11768 continue;
11769
11770 /* Build new CLASSTYPE_NESTED_UTDS. */
11771 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11772 && TYPE_LANG_SPECIFIC (t)
11773 && CLASSTYPE_IS_TEMPLATE (t));
11774
11775 /* If the member is a class template, then -- even after
11776 substitution -- there may be dependent types in the
11777 template argument list for the class. We increment
11778 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11779 that function will assume that no types are dependent
11780 when outside of a template. */
11781 if (class_template_p)
11782 ++processing_template_decl;
11783 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11784 if (class_template_p)
11785 --processing_template_decl;
11786 if (newtag == error_mark_node)
11787 continue;
11788
11789 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11790 {
11791 tree name = TYPE_IDENTIFIER (t);
11792
11793 if (class_template_p)
11794 /* Unfortunately, lookup_template_class sets
11795 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11796 instantiation (i.e., for the type of a member
11797 template class nested within a template class.)
11798 This behavior is required for
11799 maybe_process_partial_specialization to work
11800 correctly, but is not accurate in this case;
11801 the TAG is not an instantiation of anything.
11802 (The corresponding TEMPLATE_DECL is an
11803 instantiation, but the TYPE is not.) */
11804 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11805
11806 /* Now, we call pushtag to put this NEWTAG into the scope of
11807 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11808 pushtag calling push_template_decl. We don't have to do
11809 this for enums because it will already have been done in
11810 tsubst_enum. */
11811 if (name)
11812 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11813 pushtag (name, newtag, /*tag_scope=*/ts_current);
11814 }
11815 }
11816 else if (DECL_DECLARES_FUNCTION_P (t))
11817 {
11818 tree r;
11819
11820 if (TREE_CODE (t) == TEMPLATE_DECL)
11821 ++processing_template_decl;
11822 r = tsubst (t, args, tf_error, NULL_TREE);
11823 if (TREE_CODE (t) == TEMPLATE_DECL)
11824 --processing_template_decl;
11825 set_current_access_from_decl (r);
11826 finish_member_declaration (r);
11827 /* Instantiate members marked with attribute used. */
11828 if (r != error_mark_node && DECL_PRESERVE_P (r))
11829 mark_used (r);
11830 if (TREE_CODE (r) == FUNCTION_DECL
11831 && DECL_OMP_DECLARE_REDUCTION_P (r))
11832 cp_check_omp_declare_reduction (r);
11833 }
11834 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11835 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11836 /* A closure type for a lambda in an NSDMI or default argument.
11837 Ignore it; it will be regenerated when needed. */;
11838 else
11839 {
11840 /* Build new TYPE_FIELDS. */
11841 if (TREE_CODE (t) == STATIC_ASSERT)
11842 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11843 /*integral_constant_expression_p=*/true);
11844 else if (TREE_CODE (t) != CONST_DECL)
11845 {
11846 tree r;
11847 tree vec = NULL_TREE;
11848 int len = 1;
11849
11850 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11851 /* The file and line for this declaration, to
11852 assist in error message reporting. Since we
11853 called push_tinst_level above, we don't need to
11854 restore these. */
11855 input_location = DECL_SOURCE_LOCATION (t);
11856
11857 if (TREE_CODE (t) == TEMPLATE_DECL)
11858 ++processing_template_decl;
11859 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11860 if (TREE_CODE (t) == TEMPLATE_DECL)
11861 --processing_template_decl;
11862
11863 if (TREE_CODE (r) == TREE_VEC)
11864 {
11865 /* A capture pack became multiple fields. */
11866 vec = r;
11867 len = TREE_VEC_LENGTH (vec);
11868 }
11869
11870 for (int i = 0; i < len; ++i)
11871 {
11872 if (vec)
11873 r = TREE_VEC_ELT (vec, i);
11874 if (VAR_P (r))
11875 {
11876 /* In [temp.inst]:
11877
11878 [t]he initialization (and any associated
11879 side-effects) of a static data member does
11880 not occur unless the static data member is
11881 itself used in a way that requires the
11882 definition of the static data member to
11883 exist.
11884
11885 Therefore, we do not substitute into the
11886 initialized for the static data member here. */
11887 finish_static_data_member_decl
11888 (r,
11889 /*init=*/NULL_TREE,
11890 /*init_const_expr_p=*/false,
11891 /*asmspec_tree=*/NULL_TREE,
11892 /*flags=*/0);
11893 /* Instantiate members marked with attribute used. */
11894 if (r != error_mark_node && DECL_PRESERVE_P (r))
11895 mark_used (r);
11896 }
11897 else if (TREE_CODE (r) == FIELD_DECL)
11898 {
11899 /* Determine whether R has a valid type and can be
11900 completed later. If R is invalid, then its type
11901 is replaced by error_mark_node. */
11902 tree rtype = TREE_TYPE (r);
11903 if (can_complete_type_without_circularity (rtype))
11904 complete_type (rtype);
11905
11906 if (!complete_or_array_type_p (rtype))
11907 {
11908 /* If R's type couldn't be completed and
11909 it isn't a flexible array member (whose
11910 type is incomplete by definition) give
11911 an error. */
11912 cxx_incomplete_type_error (r, rtype);
11913 TREE_TYPE (r) = error_mark_node;
11914 }
11915 else if (TREE_CODE (rtype) == ARRAY_TYPE
11916 && TYPE_DOMAIN (rtype) == NULL_TREE
11917 && (TREE_CODE (type) == UNION_TYPE
11918 || TREE_CODE (type) == QUAL_UNION_TYPE))
11919 {
11920 error ("flexible array member %qD in union", r);
11921 TREE_TYPE (r) = error_mark_node;
11922 }
11923 else if (!verify_type_context (input_location,
11924 TCTX_FIELD, rtype))
11925 TREE_TYPE (r) = error_mark_node;
11926 }
11927
11928 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11929 such a thing will already have been added to the field
11930 list by tsubst_enum in finish_member_declaration in the
11931 CLASSTYPE_NESTED_UTDS case above. */
11932 if (!(TREE_CODE (r) == TYPE_DECL
11933 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11934 && DECL_ARTIFICIAL (r)))
11935 {
11936 set_current_access_from_decl (r);
11937 finish_member_declaration (r);
11938 }
11939 }
11940 }
11941 }
11942 }
11943 else
11944 {
11945 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11946 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11947 {
11948 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11949
11950 tree friend_type = t;
11951 bool adjust_processing_template_decl = false;
11952
11953 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11954 {
11955 /* template <class T> friend class C; */
11956 friend_type = tsubst_friend_class (friend_type, args);
11957 adjust_processing_template_decl = true;
11958 }
11959 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11960 {
11961 /* template <class T> friend class C::D; */
11962 friend_type = tsubst (friend_type, args,
11963 tf_warning_or_error, NULL_TREE);
11964 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11965 friend_type = TREE_TYPE (friend_type);
11966 adjust_processing_template_decl = true;
11967 }
11968 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11969 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11970 {
11971 /* This could be either
11972
11973 friend class T::C;
11974
11975 when dependent_type_p is false or
11976
11977 template <class U> friend class T::C;
11978
11979 otherwise. */
11980 /* Bump processing_template_decl in case this is something like
11981 template <class T> friend struct A<T>::B. */
11982 ++processing_template_decl;
11983 friend_type = tsubst (friend_type, args,
11984 tf_warning_or_error, NULL_TREE);
11985 if (dependent_type_p (friend_type))
11986 adjust_processing_template_decl = true;
11987 --processing_template_decl;
11988 }
11989 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11990 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11991 && TYPE_HIDDEN_P (friend_type))
11992 {
11993 /* friend class C;
11994
11995 where C hasn't been declared yet. Let's lookup name
11996 from namespace scope directly, bypassing any name that
11997 come from dependent base class. */
11998 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11999
12000 /* The call to xref_tag_from_type does injection for friend
12001 classes. */
12002 push_nested_namespace (ns);
12003 friend_type =
12004 xref_tag_from_type (friend_type, NULL_TREE,
12005 /*tag_scope=*/ts_current);
12006 pop_nested_namespace (ns);
12007 }
12008 else if (uses_template_parms (friend_type))
12009 /* friend class C<T>; */
12010 friend_type = tsubst (friend_type, args,
12011 tf_warning_or_error, NULL_TREE);
12012 /* Otherwise it's
12013
12014 friend class C;
12015
12016 where C is already declared or
12017
12018 friend class C<int>;
12019
12020 We don't have to do anything in these cases. */
12021
12022 if (adjust_processing_template_decl)
12023 /* Trick make_friend_class into realizing that the friend
12024 we're adding is a template, not an ordinary class. It's
12025 important that we use make_friend_class since it will
12026 perform some error-checking and output cross-reference
12027 information. */
12028 ++processing_template_decl;
12029
12030 if (friend_type != error_mark_node)
12031 make_friend_class (type, friend_type, /*complain=*/false);
12032
12033 if (adjust_processing_template_decl)
12034 --processing_template_decl;
12035 }
12036 else
12037 {
12038 /* Build new DECL_FRIENDLIST. */
12039 tree r;
12040
12041 /* The file and line for this declaration, to
12042 assist in error message reporting. Since we
12043 called push_tinst_level above, we don't need to
12044 restore these. */
12045 input_location = DECL_SOURCE_LOCATION (t);
12046
12047 if (TREE_CODE (t) == TEMPLATE_DECL)
12048 {
12049 ++processing_template_decl;
12050 push_deferring_access_checks (dk_no_check);
12051 }
12052
12053 r = tsubst_friend_function (t, args);
12054 add_friend (type, r, /*complain=*/false);
12055 if (TREE_CODE (t) == TEMPLATE_DECL)
12056 {
12057 pop_deferring_access_checks ();
12058 --processing_template_decl;
12059 }
12060 }
12061 }
12062 }
12063
12064 if (fn_context)
12065 {
12066 /* Restore these before substituting into the lambda capture
12067 initializers. */
12068 cp_unevaluated_operand = saved_unevaluated_operand;
12069 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12070 }
12071
12072 /* Set the file and line number information to whatever is given for
12073 the class itself. This puts error messages involving generated
12074 implicit functions at a predictable point, and the same point
12075 that would be used for non-template classes. */
12076 input_location = DECL_SOURCE_LOCATION (typedecl);
12077
12078 unreverse_member_declarations (type);
12079 finish_struct_1 (type);
12080 TYPE_BEING_DEFINED (type) = 0;
12081
12082 /* We don't instantiate default arguments for member functions. 14.7.1:
12083
12084 The implicit instantiation of a class template specialization causes
12085 the implicit instantiation of the declarations, but not of the
12086 definitions or default arguments, of the class member functions,
12087 member classes, static data members and member templates.... */
12088
12089 perform_instantiation_time_access_checks (pattern, args);
12090 perform_deferred_access_checks (tf_warning_or_error);
12091 pop_nested_class ();
12092 maximum_field_alignment = saved_maximum_field_alignment;
12093 if (!fn_context)
12094 pop_from_top_level ();
12095 pop_tinst_level ();
12096
12097 /* The vtable for a template class can be emitted in any translation
12098 unit in which the class is instantiated. When there is no key
12099 method, however, finish_struct_1 will already have added TYPE to
12100 the keyed_classes. */
12101 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12102 vec_safe_push (keyed_classes, type);
12103
12104 return type;
12105 }
12106
12107 /* Wrapper for instantiate_class_template_1. */
12108
12109 tree
12110 instantiate_class_template (tree type)
12111 {
12112 tree ret;
12113 timevar_push (TV_TEMPLATE_INST);
12114 ret = instantiate_class_template_1 (type);
12115 timevar_pop (TV_TEMPLATE_INST);
12116 return ret;
12117 }
12118
12119 tree
12120 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12121 {
12122 tree r;
12123
12124 if (!t)
12125 r = t;
12126 else if (TYPE_P (t))
12127 r = tsubst (t, args, complain, in_decl);
12128 else
12129 {
12130 if (!(complain & tf_warning))
12131 ++c_inhibit_evaluation_warnings;
12132 r = tsubst_expr (t, args, complain, in_decl,
12133 /*integral_constant_expression_p=*/true);
12134 if (!(complain & tf_warning))
12135 --c_inhibit_evaluation_warnings;
12136 }
12137
12138 return r;
12139 }
12140
12141 /* Given a function parameter pack TMPL_PARM and some function parameters
12142 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12143 and set *SPEC_P to point at the next point in the list. */
12144
12145 tree
12146 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12147 {
12148 /* Collect all of the extra "packed" parameters into an
12149 argument pack. */
12150 tree parmvec;
12151 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12152 tree spec_parm = *spec_p;
12153 int i, len;
12154
12155 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12156 if (tmpl_parm
12157 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12158 break;
12159
12160 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12161 parmvec = make_tree_vec (len);
12162 spec_parm = *spec_p;
12163 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12164 {
12165 tree elt = spec_parm;
12166 if (DECL_PACK_P (elt))
12167 elt = make_pack_expansion (elt);
12168 TREE_VEC_ELT (parmvec, i) = elt;
12169 }
12170
12171 /* Build the argument packs. */
12172 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12173 *spec_p = spec_parm;
12174
12175 return argpack;
12176 }
12177
12178 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12179 NONTYPE_ARGUMENT_PACK. */
12180
12181 static tree
12182 make_fnparm_pack (tree spec_parm)
12183 {
12184 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12185 }
12186
12187 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12188 pack expansion with no extra args, 2 if it has extra args, or 0
12189 if it is not a pack expansion. */
12190
12191 static int
12192 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12193 {
12194 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12195 /* We're being called before this happens in tsubst_pack_expansion. */
12196 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12197 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12198 if (i >= TREE_VEC_LENGTH (vec))
12199 return 0;
12200 tree elt = TREE_VEC_ELT (vec, i);
12201 if (DECL_P (elt))
12202 /* A decl pack is itself an expansion. */
12203 elt = TREE_TYPE (elt);
12204 if (!PACK_EXPANSION_P (elt))
12205 return 0;
12206 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12207 return 2;
12208 return 1;
12209 }
12210
12211
12212 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12213
12214 static tree
12215 make_argument_pack_select (tree arg_pack, unsigned index)
12216 {
12217 tree aps = make_node (ARGUMENT_PACK_SELECT);
12218
12219 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12220 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12221
12222 return aps;
12223 }
12224
12225 /* This is a subroutine of tsubst_pack_expansion.
12226
12227 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12228 mechanism to store the (non complete list of) arguments of the
12229 substitution and return a non substituted pack expansion, in order
12230 to wait for when we have enough arguments to really perform the
12231 substitution. */
12232
12233 static bool
12234 use_pack_expansion_extra_args_p (tree parm_packs,
12235 int arg_pack_len,
12236 bool has_empty_arg)
12237 {
12238 /* If one pack has an expansion and another pack has a normal
12239 argument or if one pack has an empty argument and an another
12240 one hasn't then tsubst_pack_expansion cannot perform the
12241 substitution and need to fall back on the
12242 PACK_EXPANSION_EXTRA mechanism. */
12243 if (parm_packs == NULL_TREE)
12244 return false;
12245 else if (has_empty_arg)
12246 {
12247 /* If all the actual packs are pack expansions, we can still
12248 subsitute directly. */
12249 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12250 {
12251 tree a = TREE_VALUE (p);
12252 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12253 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12254 a = ARGUMENT_PACK_ARGS (a);
12255 if (TREE_VEC_LENGTH (a) == 1)
12256 a = TREE_VEC_ELT (a, 0);
12257 if (PACK_EXPANSION_P (a))
12258 continue;
12259 return true;
12260 }
12261 return false;
12262 }
12263
12264 bool has_expansion_arg = false;
12265 for (int i = 0 ; i < arg_pack_len; ++i)
12266 {
12267 bool has_non_expansion_arg = false;
12268 for (tree parm_pack = parm_packs;
12269 parm_pack;
12270 parm_pack = TREE_CHAIN (parm_pack))
12271 {
12272 tree arg = TREE_VALUE (parm_pack);
12273
12274 int exp = argument_pack_element_is_expansion_p (arg, i);
12275 if (exp == 2)
12276 /* We can't substitute a pack expansion with extra args into
12277 our pattern. */
12278 return true;
12279 else if (exp)
12280 has_expansion_arg = true;
12281 else
12282 has_non_expansion_arg = true;
12283 }
12284
12285 if (has_expansion_arg && has_non_expansion_arg)
12286 return true;
12287 }
12288 return false;
12289 }
12290
12291 /* [temp.variadic]/6 says that:
12292
12293 The instantiation of a pack expansion [...]
12294 produces a list E1,E2, ..., En, where N is the number of elements
12295 in the pack expansion parameters.
12296
12297 This subroutine of tsubst_pack_expansion produces one of these Ei.
12298
12299 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12300 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12301 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12302 INDEX is the index 'i' of the element Ei to produce. ARGS,
12303 COMPLAIN, and IN_DECL are the same parameters as for the
12304 tsubst_pack_expansion function.
12305
12306 The function returns the resulting Ei upon successful completion,
12307 or error_mark_node.
12308
12309 Note that this function possibly modifies the ARGS parameter, so
12310 it's the responsibility of the caller to restore it. */
12311
12312 static tree
12313 gen_elem_of_pack_expansion_instantiation (tree pattern,
12314 tree parm_packs,
12315 unsigned index,
12316 tree args /* This parm gets
12317 modified. */,
12318 tsubst_flags_t complain,
12319 tree in_decl)
12320 {
12321 tree t;
12322 bool ith_elem_is_expansion = false;
12323
12324 /* For each parameter pack, change the substitution of the parameter
12325 pack to the ith argument in its argument pack, then expand the
12326 pattern. */
12327 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12328 {
12329 tree parm = TREE_PURPOSE (pack);
12330 tree arg_pack = TREE_VALUE (pack);
12331 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12332
12333 ith_elem_is_expansion |=
12334 argument_pack_element_is_expansion_p (arg_pack, index);
12335
12336 /* Select the Ith argument from the pack. */
12337 if (TREE_CODE (parm) == PARM_DECL
12338 || VAR_P (parm)
12339 || TREE_CODE (parm) == FIELD_DECL)
12340 {
12341 if (index == 0)
12342 {
12343 aps = make_argument_pack_select (arg_pack, index);
12344 if (!mark_used (parm, complain) && !(complain & tf_error))
12345 return error_mark_node;
12346 register_local_specialization (aps, parm);
12347 }
12348 else
12349 aps = retrieve_local_specialization (parm);
12350 }
12351 else
12352 {
12353 int idx, level;
12354 template_parm_level_and_index (parm, &level, &idx);
12355
12356 if (index == 0)
12357 {
12358 aps = make_argument_pack_select (arg_pack, index);
12359 /* Update the corresponding argument. */
12360 TMPL_ARG (args, level, idx) = aps;
12361 }
12362 else
12363 /* Re-use the ARGUMENT_PACK_SELECT. */
12364 aps = TMPL_ARG (args, level, idx);
12365 }
12366 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12367 }
12368
12369 /* Substitute into the PATTERN with the (possibly altered)
12370 arguments. */
12371 if (pattern == in_decl)
12372 /* Expanding a fixed parameter pack from
12373 coerce_template_parameter_pack. */
12374 t = tsubst_decl (pattern, args, complain);
12375 else if (pattern == error_mark_node)
12376 t = error_mark_node;
12377 else if (!TYPE_P (pattern))
12378 t = tsubst_expr (pattern, args, complain, in_decl,
12379 /*integral_constant_expression_p=*/false);
12380 else
12381 t = tsubst (pattern, args, complain, in_decl);
12382
12383 /* If the Ith argument pack element is a pack expansion, then
12384 the Ith element resulting from the substituting is going to
12385 be a pack expansion as well. */
12386 if (ith_elem_is_expansion)
12387 t = make_pack_expansion (t, complain);
12388
12389 return t;
12390 }
12391
12392 /* When the unexpanded parameter pack in a fold expression expands to an empty
12393 sequence, the value of the expression is as follows; the program is
12394 ill-formed if the operator is not listed in this table.
12395
12396 && true
12397 || false
12398 , void() */
12399
12400 tree
12401 expand_empty_fold (tree t, tsubst_flags_t complain)
12402 {
12403 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12404 if (!FOLD_EXPR_MODIFY_P (t))
12405 switch (code)
12406 {
12407 case TRUTH_ANDIF_EXPR:
12408 return boolean_true_node;
12409 case TRUTH_ORIF_EXPR:
12410 return boolean_false_node;
12411 case COMPOUND_EXPR:
12412 return void_node;
12413 default:
12414 break;
12415 }
12416
12417 if (complain & tf_error)
12418 error_at (location_of (t),
12419 "fold of empty expansion over %O", code);
12420 return error_mark_node;
12421 }
12422
12423 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12424 form an expression that combines the two terms using the
12425 operator of T. */
12426
12427 static tree
12428 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12429 {
12430 tree op = FOLD_EXPR_OP (t);
12431 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12432
12433 // Handle compound assignment operators.
12434 if (FOLD_EXPR_MODIFY_P (t))
12435 return build_x_modify_expr (input_location, left, code, right, complain);
12436
12437 warning_sentinel s(warn_parentheses);
12438 switch (code)
12439 {
12440 case COMPOUND_EXPR:
12441 return build_x_compound_expr (input_location, left, right, complain);
12442 default:
12443 return build_x_binary_op (input_location, code,
12444 left, TREE_CODE (left),
12445 right, TREE_CODE (right),
12446 /*overload=*/NULL,
12447 complain);
12448 }
12449 }
12450
12451 /* Substitute ARGS into the pack of a fold expression T. */
12452
12453 static inline tree
12454 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12455 {
12456 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12457 }
12458
12459 /* Substitute ARGS into the pack of a fold expression T. */
12460
12461 static inline tree
12462 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12463 {
12464 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12465 }
12466
12467 /* Expand a PACK of arguments into a grouped as left fold.
12468 Given a pack containing elements A0, A1, ..., An and an
12469 operator @, this builds the expression:
12470
12471 ((A0 @ A1) @ A2) ... @ An
12472
12473 Note that PACK must not be empty.
12474
12475 The operator is defined by the original fold expression T. */
12476
12477 static tree
12478 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12479 {
12480 tree left = TREE_VEC_ELT (pack, 0);
12481 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12482 {
12483 tree right = TREE_VEC_ELT (pack, i);
12484 left = fold_expression (t, left, right, complain);
12485 }
12486 return left;
12487 }
12488
12489 /* Substitute into a unary left fold expression. */
12490
12491 static tree
12492 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12493 tree in_decl)
12494 {
12495 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12496 if (pack == error_mark_node)
12497 return error_mark_node;
12498 if (PACK_EXPANSION_P (pack))
12499 {
12500 tree r = copy_node (t);
12501 FOLD_EXPR_PACK (r) = pack;
12502 return r;
12503 }
12504 if (TREE_VEC_LENGTH (pack) == 0)
12505 return expand_empty_fold (t, complain);
12506 else
12507 return expand_left_fold (t, pack, complain);
12508 }
12509
12510 /* Substitute into a binary left fold expression.
12511
12512 Do ths by building a single (non-empty) vector of argumnts and
12513 building the expression from those elements. */
12514
12515 static tree
12516 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12517 tree in_decl)
12518 {
12519 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12520 if (pack == error_mark_node)
12521 return error_mark_node;
12522 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12523 if (init == error_mark_node)
12524 return error_mark_node;
12525
12526 if (PACK_EXPANSION_P (pack))
12527 {
12528 tree r = copy_node (t);
12529 FOLD_EXPR_PACK (r) = pack;
12530 FOLD_EXPR_INIT (r) = init;
12531 return r;
12532 }
12533
12534 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12535 TREE_VEC_ELT (vec, 0) = init;
12536 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12537 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12538
12539 return expand_left_fold (t, vec, complain);
12540 }
12541
12542 /* Expand a PACK of arguments into a grouped as right fold.
12543 Given a pack containing elementns A0, A1, ..., and an
12544 operator @, this builds the expression:
12545
12546 A0@ ... (An-2 @ (An-1 @ An))
12547
12548 Note that PACK must not be empty.
12549
12550 The operator is defined by the original fold expression T. */
12551
12552 tree
12553 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12554 {
12555 // Build the expression.
12556 int n = TREE_VEC_LENGTH (pack);
12557 tree right = TREE_VEC_ELT (pack, n - 1);
12558 for (--n; n != 0; --n)
12559 {
12560 tree left = TREE_VEC_ELT (pack, n - 1);
12561 right = fold_expression (t, left, right, complain);
12562 }
12563 return right;
12564 }
12565
12566 /* Substitute into a unary right fold expression. */
12567
12568 static tree
12569 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12570 tree in_decl)
12571 {
12572 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12573 if (pack == error_mark_node)
12574 return error_mark_node;
12575 if (PACK_EXPANSION_P (pack))
12576 {
12577 tree r = copy_node (t);
12578 FOLD_EXPR_PACK (r) = pack;
12579 return r;
12580 }
12581 if (TREE_VEC_LENGTH (pack) == 0)
12582 return expand_empty_fold (t, complain);
12583 else
12584 return expand_right_fold (t, pack, complain);
12585 }
12586
12587 /* Substitute into a binary right fold expression.
12588
12589 Do ths by building a single (non-empty) vector of arguments and
12590 building the expression from those elements. */
12591
12592 static tree
12593 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12594 tree in_decl)
12595 {
12596 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12597 if (pack == error_mark_node)
12598 return error_mark_node;
12599 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12600 if (init == error_mark_node)
12601 return error_mark_node;
12602
12603 if (PACK_EXPANSION_P (pack))
12604 {
12605 tree r = copy_node (t);
12606 FOLD_EXPR_PACK (r) = pack;
12607 FOLD_EXPR_INIT (r) = init;
12608 return r;
12609 }
12610
12611 int n = TREE_VEC_LENGTH (pack);
12612 tree vec = make_tree_vec (n + 1);
12613 for (int i = 0; i < n; ++i)
12614 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12615 TREE_VEC_ELT (vec, n) = init;
12616
12617 return expand_right_fold (t, vec, complain);
12618 }
12619
12620 /* Walk through the pattern of a pack expansion, adding everything in
12621 local_specializations to a list. */
12622
12623 class el_data
12624 {
12625 public:
12626 hash_set<tree> internal;
12627 tree extra;
12628 tsubst_flags_t complain;
12629
12630 el_data (tsubst_flags_t c)
12631 : extra (NULL_TREE), complain (c) {}
12632 };
12633 static tree
12634 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12635 {
12636 el_data &data = *reinterpret_cast<el_data*>(data_);
12637 tree *extra = &data.extra;
12638 tsubst_flags_t complain = data.complain;
12639
12640 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12641 /* Remember local typedefs (85214). */
12642 tp = &TYPE_NAME (*tp);
12643
12644 if (TREE_CODE (*tp) == DECL_EXPR)
12645 data.internal.add (DECL_EXPR_DECL (*tp));
12646 else if (tree spec = retrieve_local_specialization (*tp))
12647 {
12648 if (data.internal.contains (*tp))
12649 /* Don't mess with variables declared within the pattern. */
12650 return NULL_TREE;
12651 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12652 {
12653 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12654 tree args = ARGUMENT_PACK_ARGS (spec);
12655 if (TREE_VEC_LENGTH (args) == 1)
12656 {
12657 tree elt = TREE_VEC_ELT (args, 0);
12658 if (PACK_EXPANSION_P (elt))
12659 elt = PACK_EXPANSION_PATTERN (elt);
12660 if (DECL_PACK_P (elt))
12661 spec = elt;
12662 }
12663 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12664 {
12665 /* Handle lambda capture here, since we aren't doing any
12666 substitution now, and so tsubst_copy won't call
12667 process_outer_var_ref. */
12668 tree args = ARGUMENT_PACK_ARGS (spec);
12669 int len = TREE_VEC_LENGTH (args);
12670 for (int i = 0; i < len; ++i)
12671 {
12672 tree arg = TREE_VEC_ELT (args, i);
12673 tree carg = arg;
12674 if (outer_automatic_var_p (arg))
12675 carg = process_outer_var_ref (arg, complain);
12676 if (carg != arg)
12677 {
12678 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12679 proxies. */
12680 if (i == 0)
12681 {
12682 spec = copy_node (spec);
12683 args = copy_node (args);
12684 SET_ARGUMENT_PACK_ARGS (spec, args);
12685 register_local_specialization (spec, *tp);
12686 }
12687 TREE_VEC_ELT (args, i) = carg;
12688 }
12689 }
12690 }
12691 }
12692 if (outer_automatic_var_p (spec))
12693 spec = process_outer_var_ref (spec, complain);
12694 *extra = tree_cons (*tp, spec, *extra);
12695 }
12696 return NULL_TREE;
12697 }
12698 static tree
12699 extract_local_specs (tree pattern, tsubst_flags_t complain)
12700 {
12701 el_data data (complain);
12702 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12703 return data.extra;
12704 }
12705
12706 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12707 for use in PACK_EXPANSION_EXTRA_ARGS. */
12708
12709 tree
12710 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12711 {
12712 tree extra = args;
12713 if (local_specializations)
12714 if (tree locals = extract_local_specs (pattern, complain))
12715 extra = tree_cons (NULL_TREE, extra, locals);
12716 return extra;
12717 }
12718
12719 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12720 normal template args to ARGS. */
12721
12722 tree
12723 add_extra_args (tree extra, tree args)
12724 {
12725 if (extra && TREE_CODE (extra) == TREE_LIST)
12726 {
12727 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12728 {
12729 /* The partial instantiation involved local declarations collected in
12730 extract_local_specs; map from the general template to our local
12731 context. */
12732 tree gen = TREE_PURPOSE (elt);
12733 tree inst = TREE_VALUE (elt);
12734 if (DECL_P (inst))
12735 if (tree local = retrieve_local_specialization (inst))
12736 inst = local;
12737 /* else inst is already a full instantiation of the pack. */
12738 register_local_specialization (inst, gen);
12739 }
12740 gcc_assert (!TREE_PURPOSE (extra));
12741 extra = TREE_VALUE (extra);
12742 }
12743 #if 1
12744 /* I think we should always be able to substitute dependent args into the
12745 pattern. If that turns out to be incorrect in some cases, enable the
12746 alternate code (and add complain/in_decl parms to this function). */
12747 gcc_checking_assert (!uses_template_parms (extra));
12748 #else
12749 if (!uses_template_parms (extra))
12750 {
12751 gcc_unreachable ();
12752 extra = tsubst_template_args (extra, args, complain, in_decl);
12753 args = add_outermost_template_args (args, extra);
12754 }
12755 else
12756 #endif
12757 args = add_to_template_args (extra, args);
12758 return args;
12759 }
12760
12761 /* Substitute ARGS into T, which is an pack expansion
12762 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12763 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12764 (if only a partial substitution could be performed) or
12765 ERROR_MARK_NODE if there was an error. */
12766 tree
12767 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12768 tree in_decl)
12769 {
12770 tree pattern;
12771 tree pack, packs = NULL_TREE;
12772 bool unsubstituted_packs = false;
12773 int i, len = -1;
12774 tree result;
12775 bool need_local_specializations = false;
12776 int levels;
12777
12778 gcc_assert (PACK_EXPANSION_P (t));
12779 pattern = PACK_EXPANSION_PATTERN (t);
12780
12781 /* Add in any args remembered from an earlier partial instantiation. */
12782 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12783
12784 levels = TMPL_ARGS_DEPTH (args);
12785
12786 /* Determine the argument packs that will instantiate the parameter
12787 packs used in the expansion expression. While we're at it,
12788 compute the number of arguments to be expanded and make sure it
12789 is consistent. */
12790 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12791 pack = TREE_CHAIN (pack))
12792 {
12793 tree parm_pack = TREE_VALUE (pack);
12794 tree arg_pack = NULL_TREE;
12795 tree orig_arg = NULL_TREE;
12796 int level = 0;
12797
12798 if (TREE_CODE (parm_pack) == BASES)
12799 {
12800 gcc_assert (parm_pack == pattern);
12801 if (BASES_DIRECT (parm_pack))
12802 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12803 args, complain,
12804 in_decl, false),
12805 complain);
12806 else
12807 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12808 args, complain, in_decl,
12809 false), complain);
12810 }
12811 else if (builtin_pack_call_p (parm_pack))
12812 {
12813 if (parm_pack != pattern)
12814 {
12815 if (complain & tf_error)
12816 sorry ("%qE is not the entire pattern of the pack expansion",
12817 parm_pack);
12818 return error_mark_node;
12819 }
12820 return expand_builtin_pack_call (parm_pack, args,
12821 complain, in_decl);
12822 }
12823 else if (TREE_CODE (parm_pack) == PARM_DECL)
12824 {
12825 /* We know we have correct local_specializations if this
12826 expansion is at function scope, or if we're dealing with a
12827 local parameter in a requires expression; for the latter,
12828 tsubst_requires_expr set it up appropriately. */
12829 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12830 arg_pack = retrieve_local_specialization (parm_pack);
12831 else
12832 /* We can't rely on local_specializations for a parameter
12833 name used later in a function declaration (such as in a
12834 late-specified return type). Even if it exists, it might
12835 have the wrong value for a recursive call. */
12836 need_local_specializations = true;
12837
12838 if (!arg_pack)
12839 {
12840 /* This parameter pack was used in an unevaluated context. Just
12841 make a dummy decl, since it's only used for its type. */
12842 ++cp_unevaluated_operand;
12843 arg_pack = tsubst_decl (parm_pack, args, complain);
12844 --cp_unevaluated_operand;
12845 if (arg_pack && DECL_PACK_P (arg_pack))
12846 /* Partial instantiation of the parm_pack, we can't build
12847 up an argument pack yet. */
12848 arg_pack = NULL_TREE;
12849 else
12850 arg_pack = make_fnparm_pack (arg_pack);
12851 }
12852 else if (DECL_PACK_P (arg_pack))
12853 /* This argument pack isn't fully instantiated yet. */
12854 arg_pack = NULL_TREE;
12855 }
12856 else if (is_capture_proxy (parm_pack))
12857 {
12858 arg_pack = retrieve_local_specialization (parm_pack);
12859 if (DECL_PACK_P (arg_pack))
12860 arg_pack = NULL_TREE;
12861 }
12862 else
12863 {
12864 int idx;
12865 template_parm_level_and_index (parm_pack, &level, &idx);
12866 if (level <= levels)
12867 arg_pack = TMPL_ARG (args, level, idx);
12868
12869 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
12870 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
12871 arg_pack = NULL_TREE;
12872 }
12873
12874 orig_arg = arg_pack;
12875 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12876 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12877
12878 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12879 /* This can only happen if we forget to expand an argument
12880 pack somewhere else. Just return an error, silently. */
12881 {
12882 result = make_tree_vec (1);
12883 TREE_VEC_ELT (result, 0) = error_mark_node;
12884 return result;
12885 }
12886
12887 if (arg_pack)
12888 {
12889 int my_len =
12890 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12891
12892 /* Don't bother trying to do a partial substitution with
12893 incomplete packs; we'll try again after deduction. */
12894 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12895 return t;
12896
12897 if (len < 0)
12898 len = my_len;
12899 else if (len != my_len)
12900 {
12901 if (!(complain & tf_error))
12902 /* Fail quietly. */;
12903 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12904 error ("mismatched argument pack lengths while expanding %qT",
12905 pattern);
12906 else
12907 error ("mismatched argument pack lengths while expanding %qE",
12908 pattern);
12909 return error_mark_node;
12910 }
12911
12912 /* Keep track of the parameter packs and their corresponding
12913 argument packs. */
12914 packs = tree_cons (parm_pack, arg_pack, packs);
12915 TREE_TYPE (packs) = orig_arg;
12916 }
12917 else
12918 {
12919 /* We can't substitute for this parameter pack. We use a flag as
12920 well as the missing_level counter because function parameter
12921 packs don't have a level. */
12922 gcc_assert (processing_template_decl || is_auto (parm_pack));
12923 unsubstituted_packs = true;
12924 }
12925 }
12926
12927 /* If the expansion is just T..., return the matching argument pack, unless
12928 we need to call convert_from_reference on all the elements. This is an
12929 important optimization; see c++/68422. */
12930 if (!unsubstituted_packs
12931 && TREE_PURPOSE (packs) == pattern)
12932 {
12933 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12934
12935 /* If the argument pack is a single pack expansion, pull it out. */
12936 if (TREE_VEC_LENGTH (args) == 1
12937 && pack_expansion_args_count (args))
12938 return TREE_VEC_ELT (args, 0);
12939
12940 /* Types need no adjustment, nor does sizeof..., and if we still have
12941 some pack expansion args we won't do anything yet. */
12942 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12943 || PACK_EXPANSION_SIZEOF_P (t)
12944 || pack_expansion_args_count (args))
12945 return args;
12946 /* Also optimize expression pack expansions if we can tell that the
12947 elements won't have reference type. */
12948 tree type = TREE_TYPE (pattern);
12949 if (type && !TYPE_REF_P (type)
12950 && !PACK_EXPANSION_P (type)
12951 && !WILDCARD_TYPE_P (type))
12952 return args;
12953 /* Otherwise use the normal path so we get convert_from_reference. */
12954 }
12955
12956 /* We cannot expand this expansion expression, because we don't have
12957 all of the argument packs we need. */
12958 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12959 {
12960 /* We got some full packs, but we can't substitute them in until we
12961 have values for all the packs. So remember these until then. */
12962
12963 t = make_pack_expansion (pattern, complain);
12964 PACK_EXPANSION_EXTRA_ARGS (t)
12965 = build_extra_args (pattern, args, complain);
12966 return t;
12967 }
12968
12969 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
12970 type, so create our own local specializations map; the current map is
12971 either NULL or (in the case of recursive unification) might have
12972 bindings that we don't want to use or alter. */
12973 local_specialization_stack lss (need_local_specializations
12974 ? lss_blank : lss_nop);
12975
12976 if (unsubstituted_packs)
12977 {
12978 /* There were no real arguments, we're just replacing a parameter
12979 pack with another version of itself. Substitute into the
12980 pattern and return a PACK_EXPANSION_*. The caller will need to
12981 deal with that. */
12982 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12983 t = tsubst_expr (pattern, args, complain, in_decl,
12984 /*integral_constant_expression_p=*/false);
12985 else
12986 t = tsubst (pattern, args, complain, in_decl);
12987 t = make_pack_expansion (t, complain);
12988 return t;
12989 }
12990
12991 gcc_assert (len >= 0);
12992
12993 /* For each argument in each argument pack, substitute into the
12994 pattern. */
12995 result = make_tree_vec (len);
12996 tree elem_args = copy_template_args (args);
12997 for (i = 0; i < len; ++i)
12998 {
12999 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13000 i,
13001 elem_args, complain,
13002 in_decl);
13003 TREE_VEC_ELT (result, i) = t;
13004 if (t == error_mark_node)
13005 {
13006 result = error_mark_node;
13007 break;
13008 }
13009 }
13010
13011 /* Update ARGS to restore the substitution from parameter packs to
13012 their argument packs. */
13013 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13014 {
13015 tree parm = TREE_PURPOSE (pack);
13016
13017 if (TREE_CODE (parm) == PARM_DECL
13018 || VAR_P (parm)
13019 || TREE_CODE (parm) == FIELD_DECL)
13020 register_local_specialization (TREE_TYPE (pack), parm);
13021 else
13022 {
13023 int idx, level;
13024
13025 if (TREE_VALUE (pack) == NULL_TREE)
13026 continue;
13027
13028 template_parm_level_and_index (parm, &level, &idx);
13029
13030 /* Update the corresponding argument. */
13031 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13032 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13033 TREE_TYPE (pack);
13034 else
13035 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13036 }
13037 }
13038
13039 /* If the dependent pack arguments were such that we end up with only a
13040 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13041 if (len == 1 && TREE_CODE (result) == TREE_VEC
13042 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13043 return TREE_VEC_ELT (result, 0);
13044
13045 return result;
13046 }
13047
13048 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13049 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13050 parameter packs; all parms generated from a function parameter pack will
13051 have the same DECL_PARM_INDEX. */
13052
13053 tree
13054 get_pattern_parm (tree parm, tree tmpl)
13055 {
13056 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13057 tree patparm;
13058
13059 if (DECL_ARTIFICIAL (parm))
13060 {
13061 for (patparm = DECL_ARGUMENTS (pattern);
13062 patparm; patparm = DECL_CHAIN (patparm))
13063 if (DECL_ARTIFICIAL (patparm)
13064 && DECL_NAME (parm) == DECL_NAME (patparm))
13065 break;
13066 }
13067 else
13068 {
13069 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13070 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13071 gcc_assert (DECL_PARM_INDEX (patparm)
13072 == DECL_PARM_INDEX (parm));
13073 }
13074
13075 return patparm;
13076 }
13077
13078 /* Make an argument pack out of the TREE_VEC VEC. */
13079
13080 static tree
13081 make_argument_pack (tree vec)
13082 {
13083 tree pack;
13084
13085 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13086 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13087 else
13088 {
13089 pack = make_node (NONTYPE_ARGUMENT_PACK);
13090 TREE_CONSTANT (pack) = 1;
13091 }
13092 SET_ARGUMENT_PACK_ARGS (pack, vec);
13093 return pack;
13094 }
13095
13096 /* Return an exact copy of template args T that can be modified
13097 independently. */
13098
13099 static tree
13100 copy_template_args (tree t)
13101 {
13102 if (t == error_mark_node)
13103 return t;
13104
13105 int len = TREE_VEC_LENGTH (t);
13106 tree new_vec = make_tree_vec (len);
13107
13108 for (int i = 0; i < len; ++i)
13109 {
13110 tree elt = TREE_VEC_ELT (t, i);
13111 if (elt && TREE_CODE (elt) == TREE_VEC)
13112 elt = copy_template_args (elt);
13113 TREE_VEC_ELT (new_vec, i) = elt;
13114 }
13115
13116 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13117 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13118
13119 return new_vec;
13120 }
13121
13122 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13123
13124 tree
13125 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13126 tree in_decl)
13127 {
13128 /* Substitute into each of the arguments. */
13129 tree new_arg = TYPE_P (orig_arg)
13130 ? cxx_make_type (TREE_CODE (orig_arg))
13131 : make_node (TREE_CODE (orig_arg));
13132
13133 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13134 args, complain, in_decl);
13135 if (pack_args == error_mark_node)
13136 new_arg = error_mark_node;
13137 else
13138 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13139
13140 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13141 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13142
13143 return new_arg;
13144 }
13145
13146 /* Substitute ARGS into the vector or list of template arguments T. */
13147
13148 tree
13149 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13150 {
13151 tree orig_t = t;
13152 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13153 tree *elts;
13154
13155 if (t == error_mark_node)
13156 return error_mark_node;
13157
13158 len = TREE_VEC_LENGTH (t);
13159 elts = XALLOCAVEC (tree, len);
13160
13161 for (i = 0; i < len; i++)
13162 {
13163 tree orig_arg = TREE_VEC_ELT (t, i);
13164 tree new_arg;
13165
13166 if (TREE_CODE (orig_arg) == TREE_VEC)
13167 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13168 else if (PACK_EXPANSION_P (orig_arg))
13169 {
13170 /* Substitute into an expansion expression. */
13171 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13172
13173 if (TREE_CODE (new_arg) == TREE_VEC)
13174 /* Add to the expanded length adjustment the number of
13175 expanded arguments. We subtract one from this
13176 measurement, because the argument pack expression
13177 itself is already counted as 1 in
13178 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13179 the argument pack is empty. */
13180 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13181 }
13182 else if (ARGUMENT_PACK_P (orig_arg))
13183 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13184 else
13185 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13186
13187 if (new_arg == error_mark_node)
13188 return error_mark_node;
13189
13190 elts[i] = new_arg;
13191 if (new_arg != orig_arg)
13192 need_new = 1;
13193 }
13194
13195 if (!need_new)
13196 return t;
13197
13198 /* Make space for the expanded arguments coming from template
13199 argument packs. */
13200 t = make_tree_vec (len + expanded_len_adjust);
13201 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13202 arguments for a member template.
13203 In that case each TREE_VEC in ORIG_T represents a level of template
13204 arguments, and ORIG_T won't carry any non defaulted argument count.
13205 It will rather be the nested TREE_VECs that will carry one.
13206 In other words, ORIG_T carries a non defaulted argument count only
13207 if it doesn't contain any nested TREE_VEC. */
13208 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13209 {
13210 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13211 count += expanded_len_adjust;
13212 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13213 }
13214 for (i = 0, out = 0; i < len; i++)
13215 {
13216 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13217 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13218 && TREE_CODE (elts[i]) == TREE_VEC)
13219 {
13220 int idx;
13221
13222 /* Now expand the template argument pack "in place". */
13223 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13224 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13225 }
13226 else
13227 {
13228 TREE_VEC_ELT (t, out) = elts[i];
13229 out++;
13230 }
13231 }
13232
13233 return t;
13234 }
13235
13236 /* Substitute ARGS into one level PARMS of template parameters. */
13237
13238 static tree
13239 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13240 {
13241 if (parms == error_mark_node)
13242 return error_mark_node;
13243
13244 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13245
13246 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13247 {
13248 tree tuple = TREE_VEC_ELT (parms, i);
13249
13250 if (tuple == error_mark_node)
13251 continue;
13252
13253 TREE_VEC_ELT (new_vec, i) =
13254 tsubst_template_parm (tuple, args, complain);
13255 }
13256
13257 return new_vec;
13258 }
13259
13260 /* Return the result of substituting ARGS into the template parameters
13261 given by PARMS. If there are m levels of ARGS and m + n levels of
13262 PARMS, then the result will contain n levels of PARMS. For
13263 example, if PARMS is `template <class T> template <class U>
13264 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13265 result will be `template <int*, double, class V>'. */
13266
13267 static tree
13268 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13269 {
13270 tree r = NULL_TREE;
13271 tree* new_parms;
13272
13273 /* When substituting into a template, we must set
13274 PROCESSING_TEMPLATE_DECL as the template parameters may be
13275 dependent if they are based on one-another, and the dependency
13276 predicates are short-circuit outside of templates. */
13277 ++processing_template_decl;
13278
13279 for (new_parms = &r;
13280 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13281 new_parms = &(TREE_CHAIN (*new_parms)),
13282 parms = TREE_CHAIN (parms))
13283 {
13284 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13285 args, complain);
13286 *new_parms =
13287 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13288 - TMPL_ARGS_DEPTH (args)),
13289 new_vec, NULL_TREE);
13290 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13291 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13292 }
13293
13294 --processing_template_decl;
13295
13296 return r;
13297 }
13298
13299 /* Return the result of substituting ARGS into one template parameter
13300 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13301 parameter and which TREE_PURPOSE is the default argument of the
13302 template parameter. */
13303
13304 static tree
13305 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13306 {
13307 tree default_value, parm_decl;
13308
13309 if (args == NULL_TREE
13310 || t == NULL_TREE
13311 || t == error_mark_node)
13312 return t;
13313
13314 gcc_assert (TREE_CODE (t) == TREE_LIST);
13315
13316 default_value = TREE_PURPOSE (t);
13317 parm_decl = TREE_VALUE (t);
13318 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13319
13320 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13321 if (TREE_CODE (parm_decl) == PARM_DECL
13322 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13323 parm_decl = error_mark_node;
13324 default_value = tsubst_template_arg (default_value, args,
13325 complain, NULL_TREE);
13326 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13327
13328 tree r = build_tree_list (default_value, parm_decl);
13329 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13330 return r;
13331 }
13332
13333 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13334 type T. If T is not an aggregate or enumeration type, it is
13335 handled as if by tsubst. IN_DECL is as for tsubst. If
13336 ENTERING_SCOPE is nonzero, T is the context for a template which
13337 we are presently tsubst'ing. Return the substituted value. */
13338
13339 static tree
13340 tsubst_aggr_type (tree t,
13341 tree args,
13342 tsubst_flags_t complain,
13343 tree in_decl,
13344 int entering_scope)
13345 {
13346 if (t == NULL_TREE)
13347 return NULL_TREE;
13348
13349 switch (TREE_CODE (t))
13350 {
13351 case RECORD_TYPE:
13352 if (TYPE_PTRMEMFUNC_P (t))
13353 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13354
13355 /* Fall through. */
13356 case ENUMERAL_TYPE:
13357 case UNION_TYPE:
13358 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13359 {
13360 tree argvec;
13361 tree context;
13362 tree r;
13363
13364 /* In "sizeof(X<I>)" we need to evaluate "I". */
13365 cp_evaluated ev;
13366
13367 /* First, determine the context for the type we are looking
13368 up. */
13369 context = TYPE_CONTEXT (t);
13370 if (context && TYPE_P (context))
13371 {
13372 context = tsubst_aggr_type (context, args, complain,
13373 in_decl, /*entering_scope=*/1);
13374 /* If context is a nested class inside a class template,
13375 it may still need to be instantiated (c++/33959). */
13376 context = complete_type (context);
13377 }
13378
13379 /* Then, figure out what arguments are appropriate for the
13380 type we are trying to find. For example, given:
13381
13382 template <class T> struct S;
13383 template <class T, class U> void f(T, U) { S<U> su; }
13384
13385 and supposing that we are instantiating f<int, double>,
13386 then our ARGS will be {int, double}, but, when looking up
13387 S we only want {double}. */
13388 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13389 complain, in_decl);
13390 if (argvec == error_mark_node)
13391 r = error_mark_node;
13392 else
13393 {
13394 r = lookup_template_class (t, argvec, in_decl, context,
13395 entering_scope, complain);
13396 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13397 }
13398
13399 return r;
13400 }
13401 else
13402 /* This is not a template type, so there's nothing to do. */
13403 return t;
13404
13405 default:
13406 return tsubst (t, args, complain, in_decl);
13407 }
13408 }
13409
13410 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13411
13412 /* Substitute into the default argument ARG (a default argument for
13413 FN), which has the indicated TYPE. */
13414
13415 tree
13416 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13417 tsubst_flags_t complain)
13418 {
13419 int errs = errorcount + sorrycount;
13420
13421 /* This can happen in invalid code. */
13422 if (TREE_CODE (arg) == DEFERRED_PARSE)
13423 return arg;
13424
13425 /* Shortcut {}. */
13426 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13427 && CONSTRUCTOR_NELTS (arg) == 0)
13428 return arg;
13429
13430 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13431 parm = chain_index (parmnum, parm);
13432 tree parmtype = TREE_TYPE (parm);
13433 if (DECL_BY_REFERENCE (parm))
13434 parmtype = TREE_TYPE (parmtype);
13435 if (parmtype == error_mark_node)
13436 return error_mark_node;
13437
13438 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13439
13440 tree *slot;
13441 if (defarg_inst && (slot = defarg_inst->get (parm)))
13442 return *slot;
13443
13444 /* This default argument came from a template. Instantiate the
13445 default argument here, not in tsubst. In the case of
13446 something like:
13447
13448 template <class T>
13449 struct S {
13450 static T t();
13451 void f(T = t());
13452 };
13453
13454 we must be careful to do name lookup in the scope of S<T>,
13455 rather than in the current class. */
13456 push_to_top_level ();
13457 push_access_scope (fn);
13458 push_deferring_access_checks (dk_no_deferred);
13459 start_lambda_scope (parm);
13460
13461 /* The default argument expression may cause implicitly defined
13462 member functions to be synthesized, which will result in garbage
13463 collection. We must treat this situation as if we were within
13464 the body of function so as to avoid collecting live data on the
13465 stack. */
13466 ++function_depth;
13467 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13468 complain, NULL_TREE,
13469 /*integral_constant_expression_p=*/false);
13470 --function_depth;
13471
13472 finish_lambda_scope ();
13473
13474 /* Make sure the default argument is reasonable. */
13475 arg = check_default_argument (type, arg, complain);
13476
13477 if (errorcount+sorrycount > errs
13478 && (complain & tf_warning_or_error))
13479 inform (input_location,
13480 " when instantiating default argument for call to %qD", fn);
13481
13482 pop_deferring_access_checks ();
13483 pop_access_scope (fn);
13484 pop_from_top_level ();
13485
13486 if (arg != error_mark_node && !cp_unevaluated_operand)
13487 {
13488 if (!defarg_inst)
13489 defarg_inst = decl_tree_cache_map::create_ggc (37);
13490 defarg_inst->put (parm, arg);
13491 }
13492
13493 return arg;
13494 }
13495
13496 /* Substitute into all the default arguments for FN. */
13497
13498 static void
13499 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13500 {
13501 tree arg;
13502 tree tmpl_args;
13503
13504 tmpl_args = DECL_TI_ARGS (fn);
13505
13506 /* If this function is not yet instantiated, we certainly don't need
13507 its default arguments. */
13508 if (uses_template_parms (tmpl_args))
13509 return;
13510 /* Don't do this again for clones. */
13511 if (DECL_CLONED_FUNCTION_P (fn))
13512 return;
13513
13514 int i = 0;
13515 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13516 arg;
13517 arg = TREE_CHAIN (arg), ++i)
13518 if (TREE_PURPOSE (arg))
13519 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13520 TREE_VALUE (arg),
13521 TREE_PURPOSE (arg),
13522 complain);
13523 }
13524
13525 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13526 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13527
13528 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13529
13530 void
13531 store_explicit_specifier (tree v, tree t)
13532 {
13533 if (!explicit_specifier_map)
13534 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13535 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13536 explicit_specifier_map->put (v, t);
13537 }
13538
13539 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13540
13541 static tree
13542 lookup_explicit_specifier (tree v)
13543 {
13544 return *explicit_specifier_map->get (v);
13545 }
13546
13547 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13548 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13549 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13550 identical to T. */
13551
13552 static tree
13553 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13554 tree raises, tsubst_flags_t complain)
13555 {
13556 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13557
13558 tree new_type;
13559 if (TREE_CODE (t) == FUNCTION_TYPE)
13560 {
13561 new_type = build_function_type (return_type, arg_types);
13562 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13563 }
13564 else
13565 {
13566 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13567 /* Don't pick up extra function qualifiers from the basetype. */
13568 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13569 if (! MAYBE_CLASS_TYPE_P (r))
13570 {
13571 /* [temp.deduct]
13572
13573 Type deduction may fail for any of the following
13574 reasons:
13575
13576 -- Attempting to create "pointer to member of T" when T
13577 is not a class type. */
13578 if (complain & tf_error)
13579 error ("creating pointer to member function of non-class type %qT",
13580 r);
13581 return error_mark_node;
13582 }
13583
13584 new_type = build_method_type_directly (r, return_type,
13585 TREE_CHAIN (arg_types));
13586 }
13587 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13588
13589 cp_ref_qualifier rqual = type_memfn_rqual (t);
13590 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13591 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13592 }
13593
13594 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13595 each of its formal parameters. If there is a disagreement then rebuild
13596 DECL's function type according to its formal parameter types, as part of a
13597 resolution for Core issues 1001/1322. */
13598
13599 static void
13600 maybe_rebuild_function_decl_type (tree decl)
13601 {
13602 bool function_type_needs_rebuilding = false;
13603 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13604 {
13605 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13606 while (parm_type_list && parm_type_list != void_list_node)
13607 {
13608 tree parm_type = TREE_VALUE (parm_type_list);
13609 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13610 if (!same_type_p (parm_type, formal_parm_type_unqual))
13611 {
13612 function_type_needs_rebuilding = true;
13613 break;
13614 }
13615
13616 parm_list = DECL_CHAIN (parm_list);
13617 parm_type_list = TREE_CHAIN (parm_type_list);
13618 }
13619 }
13620
13621 if (!function_type_needs_rebuilding)
13622 return;
13623
13624 const tree fntype = TREE_TYPE (decl);
13625 tree parm_list = DECL_ARGUMENTS (decl);
13626 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13627 tree new_parm_type_list = NULL_TREE;
13628 tree *q = &new_parm_type_list;
13629 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13630 {
13631 *q = copy_node (old_parm_type_list);
13632 parm_list = DECL_CHAIN (parm_list);
13633 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13634 q = &TREE_CHAIN (*q);
13635 }
13636 while (old_parm_type_list && old_parm_type_list != void_list_node)
13637 {
13638 *q = copy_node (old_parm_type_list);
13639 tree *new_parm_type = &TREE_VALUE (*q);
13640 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13641 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13642 *new_parm_type = formal_parm_type_unqual;
13643
13644 parm_list = DECL_CHAIN (parm_list);
13645 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13646 q = &TREE_CHAIN (*q);
13647 }
13648 if (old_parm_type_list == void_list_node)
13649 *q = void_list_node;
13650
13651 TREE_TYPE (decl)
13652 = rebuild_function_or_method_type (fntype,
13653 TREE_TYPE (fntype), new_parm_type_list,
13654 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13655 }
13656
13657 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13658
13659 static tree
13660 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13661 tree lambda_fntype)
13662 {
13663 tree gen_tmpl, argvec;
13664 hashval_t hash = 0;
13665 tree in_decl = t;
13666
13667 /* Nobody should be tsubst'ing into non-template functions. */
13668 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
13669
13670 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13671 {
13672 /* If T is not dependent, just return it. */
13673 if (!uses_template_parms (DECL_TI_ARGS (t))
13674 && !LAMBDA_FUNCTION_P (t))
13675 return t;
13676
13677 /* Calculate the most general template of which R is a
13678 specialization. */
13679 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13680
13681 /* We're substituting a lambda function under tsubst_lambda_expr but not
13682 directly from it; find the matching function we're already inside.
13683 But don't do this if T is a generic lambda with a single level of
13684 template parms, as in that case we're doing a normal instantiation. */
13685 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13686 && (!generic_lambda_fn_p (t)
13687 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13688 return enclosing_instantiation_of (t);
13689
13690 /* Calculate the complete set of arguments used to
13691 specialize R. */
13692 argvec = tsubst_template_args (DECL_TI_ARGS
13693 (DECL_TEMPLATE_RESULT
13694 (DECL_TI_TEMPLATE (t))),
13695 args, complain, in_decl);
13696 if (argvec == error_mark_node)
13697 return error_mark_node;
13698
13699 /* Check to see if we already have this specialization. */
13700 if (!lambda_fntype)
13701 {
13702 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13703 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13704 return spec;
13705 }
13706
13707 /* We can see more levels of arguments than parameters if
13708 there was a specialization of a member template, like
13709 this:
13710
13711 template <class T> struct S { template <class U> void f(); }
13712 template <> template <class U> void S<int>::f(U);
13713
13714 Here, we'll be substituting into the specialization,
13715 because that's where we can find the code we actually
13716 want to generate, but we'll have enough arguments for
13717 the most general template.
13718
13719 We also deal with the peculiar case:
13720
13721 template <class T> struct S {
13722 template <class U> friend void f();
13723 };
13724 template <class U> void f() {}
13725 template S<int>;
13726 template void f<double>();
13727
13728 Here, the ARGS for the instantiation of will be {int,
13729 double}. But, we only need as many ARGS as there are
13730 levels of template parameters in CODE_PATTERN. We are
13731 careful not to get fooled into reducing the ARGS in
13732 situations like:
13733
13734 template <class T> struct S { template <class U> void f(U); }
13735 template <class T> template <> void S<T>::f(int) {}
13736
13737 which we can spot because the pattern will be a
13738 specialization in this case. */
13739 int args_depth = TMPL_ARGS_DEPTH (args);
13740 int parms_depth =
13741 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13742
13743 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13744 args = get_innermost_template_args (args, parms_depth);
13745 }
13746 else
13747 {
13748 /* This special case arises when we have something like this:
13749
13750 template <class T> struct S {
13751 friend void f<int>(int, double);
13752 };
13753
13754 Here, the DECL_TI_TEMPLATE for the friend declaration
13755 will be an IDENTIFIER_NODE. We are being called from
13756 tsubst_friend_function, and we want only to create a
13757 new decl (R) with appropriate types so that we can call
13758 determine_specialization. */
13759 gen_tmpl = NULL_TREE;
13760 argvec = NULL_TREE;
13761 }
13762
13763 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13764 : NULL_TREE);
13765 tree ctx = closure ? closure : DECL_CONTEXT (t);
13766 bool member = ctx && TYPE_P (ctx);
13767
13768 if (member && !closure)
13769 ctx = tsubst_aggr_type (ctx, args,
13770 complain, t, /*entering_scope=*/1);
13771
13772 tree type = (lambda_fntype ? lambda_fntype
13773 : tsubst (TREE_TYPE (t), args,
13774 complain | tf_fndecl_type, in_decl));
13775 if (type == error_mark_node)
13776 return error_mark_node;
13777
13778 /* If we hit excessive deduction depth, the type is bogus even if
13779 it isn't error_mark_node, so don't build a decl. */
13780 if (excessive_deduction_depth)
13781 return error_mark_node;
13782
13783 /* We do NOT check for matching decls pushed separately at this
13784 point, as they may not represent instantiations of this
13785 template, and in any case are considered separate under the
13786 discrete model. */
13787 tree r = copy_decl (t);
13788 DECL_USE_TEMPLATE (r) = 0;
13789 TREE_TYPE (r) = type;
13790 /* Clear out the mangled name and RTL for the instantiation. */
13791 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13792 SET_DECL_RTL (r, NULL);
13793 /* Leave DECL_INITIAL set on deleted instantiations. */
13794 if (!DECL_DELETED_FN (r))
13795 DECL_INITIAL (r) = NULL_TREE;
13796 DECL_CONTEXT (r) = ctx;
13797
13798 /* Handle explicit(dependent-expr). */
13799 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13800 {
13801 tree spec = lookup_explicit_specifier (t);
13802 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13803 /*function_p=*/false,
13804 /*i_c_e_p=*/true);
13805 spec = build_explicit_specifier (spec, complain);
13806 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13807 }
13808
13809 /* OpenMP UDRs have the only argument a reference to the declared
13810 type. We want to diagnose if the declared type is a reference,
13811 which is invalid, but as references to references are usually
13812 quietly merged, diagnose it here. */
13813 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13814 {
13815 tree argtype
13816 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13817 argtype = tsubst (argtype, args, complain, in_decl);
13818 if (TYPE_REF_P (argtype))
13819 error_at (DECL_SOURCE_LOCATION (t),
13820 "reference type %qT in "
13821 "%<#pragma omp declare reduction%>", argtype);
13822 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13823 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13824 argtype);
13825 }
13826
13827 if (member && DECL_CONV_FN_P (r))
13828 /* Type-conversion operator. Reconstruct the name, in
13829 case it's the name of one of the template's parameters. */
13830 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13831
13832 tree parms = DECL_ARGUMENTS (t);
13833 if (closure)
13834 parms = DECL_CHAIN (parms);
13835 parms = tsubst (parms, args, complain, t);
13836 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13837 DECL_CONTEXT (parm) = r;
13838 if (closure)
13839 {
13840 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13841 DECL_NAME (tparm) = closure_identifier;
13842 DECL_CHAIN (tparm) = parms;
13843 parms = tparm;
13844 }
13845 DECL_ARGUMENTS (r) = parms;
13846 DECL_RESULT (r) = NULL_TREE;
13847
13848 maybe_rebuild_function_decl_type (r);
13849
13850 TREE_STATIC (r) = 0;
13851 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13852 DECL_EXTERNAL (r) = 1;
13853 /* If this is an instantiation of a function with internal
13854 linkage, we already know what object file linkage will be
13855 assigned to the instantiation. */
13856 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13857 DECL_DEFER_OUTPUT (r) = 0;
13858 DECL_CHAIN (r) = NULL_TREE;
13859 DECL_PENDING_INLINE_INFO (r) = 0;
13860 DECL_PENDING_INLINE_P (r) = 0;
13861 DECL_SAVED_TREE (r) = NULL_TREE;
13862 DECL_STRUCT_FUNCTION (r) = NULL;
13863 TREE_USED (r) = 0;
13864 /* We'll re-clone as appropriate in instantiate_template. */
13865 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13866
13867 /* If we aren't complaining now, return on error before we register
13868 the specialization so that we'll complain eventually. */
13869 if ((complain & tf_error) == 0
13870 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13871 && !grok_op_properties (r, /*complain=*/false))
13872 return error_mark_node;
13873
13874 /* Associate the constraints directly with the instantiation. We
13875 don't substitute through the constraints; that's only done when
13876 they are checked. */
13877 if (tree ci = get_constraints (t))
13878 /* Unless we're regenerating a lambda, in which case we'll set the
13879 lambda's constraints in tsubst_lambda_expr. */
13880 if (!lambda_fntype)
13881 set_constraints (r, ci);
13882
13883 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13884 SET_DECL_FRIEND_CONTEXT (r,
13885 tsubst (DECL_FRIEND_CONTEXT (t),
13886 args, complain, in_decl));
13887
13888 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13889 this in the special friend case mentioned above where
13890 GEN_TMPL is NULL. */
13891 if (gen_tmpl && !closure)
13892 {
13893 DECL_TEMPLATE_INFO (r)
13894 = build_template_info (gen_tmpl, argvec);
13895 SET_DECL_IMPLICIT_INSTANTIATION (r);
13896
13897 tree new_r
13898 = register_specialization (r, gen_tmpl, argvec, false, hash);
13899 if (new_r != r)
13900 /* We instantiated this while substituting into
13901 the type earlier (template/friend54.C). */
13902 return new_r;
13903
13904 /* We're not supposed to instantiate default arguments
13905 until they are called, for a template. But, for a
13906 declaration like:
13907
13908 template <class T> void f ()
13909 { extern void g(int i = T()); }
13910
13911 we should do the substitution when the template is
13912 instantiated. We handle the member function case in
13913 instantiate_class_template since the default arguments
13914 might refer to other members of the class. */
13915 if (!member
13916 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13917 && !uses_template_parms (argvec))
13918 tsubst_default_arguments (r, complain);
13919 }
13920 else
13921 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13922
13923 /* Copy the list of befriending classes. */
13924 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13925 *friends;
13926 friends = &TREE_CHAIN (*friends))
13927 {
13928 *friends = copy_node (*friends);
13929 TREE_VALUE (*friends)
13930 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13931 }
13932
13933 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13934 {
13935 maybe_retrofit_in_chrg (r);
13936 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13937 return error_mark_node;
13938 /* If this is an instantiation of a member template, clone it.
13939 If it isn't, that'll be handled by
13940 clone_constructors_and_destructors. */
13941 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13942 clone_cdtor (r, /*update_methods=*/false);
13943 }
13944 else if ((complain & tf_error) != 0
13945 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13946 && !grok_op_properties (r, /*complain=*/true))
13947 return error_mark_node;
13948
13949 /* Possibly limit visibility based on template args. */
13950 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13951 if (DECL_VISIBILITY_SPECIFIED (t))
13952 {
13953 DECL_VISIBILITY_SPECIFIED (r) = 0;
13954 DECL_ATTRIBUTES (r)
13955 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13956 }
13957 determine_visibility (r);
13958 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13959 && !processing_template_decl)
13960 defaulted_late_check (r);
13961
13962 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13963 args, complain, in_decl);
13964 if (flag_openmp)
13965 if (tree attr = lookup_attribute ("omp declare variant base",
13966 DECL_ATTRIBUTES (r)))
13967 omp_declare_variant_finalize (r, attr);
13968
13969 return r;
13970 }
13971
13972 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13973
13974 static tree
13975 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13976 tree lambda_fntype)
13977 {
13978 /* We can get here when processing a member function template,
13979 member class template, or template template parameter. */
13980 tree decl = DECL_TEMPLATE_RESULT (t);
13981 tree in_decl = t;
13982 tree spec;
13983 tree tmpl_args;
13984 tree full_args;
13985 tree r;
13986 hashval_t hash = 0;
13987
13988 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13989 {
13990 /* Template template parameter is treated here. */
13991 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13992 if (new_type == error_mark_node)
13993 r = error_mark_node;
13994 /* If we get a real template back, return it. This can happen in
13995 the context of most_specialized_partial_spec. */
13996 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13997 r = new_type;
13998 else
13999 /* The new TEMPLATE_DECL was built in
14000 reduce_template_parm_level. */
14001 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14002 return r;
14003 }
14004
14005 if (!lambda_fntype)
14006 {
14007 /* We might already have an instance of this template.
14008 The ARGS are for the surrounding class type, so the
14009 full args contain the tsubst'd args for the context,
14010 plus the innermost args from the template decl. */
14011 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14012 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14013 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14014 /* Because this is a template, the arguments will still be
14015 dependent, even after substitution. If
14016 PROCESSING_TEMPLATE_DECL is not set, the dependency
14017 predicates will short-circuit. */
14018 ++processing_template_decl;
14019 full_args = tsubst_template_args (tmpl_args, args,
14020 complain, in_decl);
14021 --processing_template_decl;
14022 if (full_args == error_mark_node)
14023 return error_mark_node;
14024
14025 /* If this is a default template template argument,
14026 tsubst might not have changed anything. */
14027 if (full_args == tmpl_args)
14028 return t;
14029
14030 hash = hash_tmpl_and_args (t, full_args);
14031 spec = retrieve_specialization (t, full_args, hash);
14032 if (spec != NULL_TREE)
14033 {
14034 if (TYPE_P (spec))
14035 /* Type partial instantiations are stored as the type by
14036 lookup_template_class_1, not here as the template. */
14037 spec = CLASSTYPE_TI_TEMPLATE (spec);
14038 return spec;
14039 }
14040 }
14041
14042 /* Make a new template decl. It will be similar to the
14043 original, but will record the current template arguments.
14044 We also create a new function declaration, which is just
14045 like the old one, but points to this new template, rather
14046 than the old one. */
14047 r = copy_decl (t);
14048 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14049 DECL_CHAIN (r) = NULL_TREE;
14050
14051 // Build new template info linking to the original template decl.
14052 if (!lambda_fntype)
14053 {
14054 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14055 SET_DECL_IMPLICIT_INSTANTIATION (r);
14056 }
14057 else
14058 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14059
14060 /* The template parameters for this new template are all the
14061 template parameters for the old template, except the
14062 outermost level of parameters. */
14063 DECL_TEMPLATE_PARMS (r)
14064 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14065 complain);
14066
14067 bool class_p = false;
14068 tree inner = decl;
14069 ++processing_template_decl;
14070 if (TREE_CODE (inner) == FUNCTION_DECL)
14071 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14072 else
14073 {
14074 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14075 {
14076 class_p = true;
14077 inner = TREE_TYPE (inner);
14078 }
14079 inner = tsubst (inner, args, complain, in_decl);
14080 }
14081 --processing_template_decl;
14082 if (inner == error_mark_node)
14083 return error_mark_node;
14084
14085 if (class_p)
14086 {
14087 /* For a partial specialization, we need to keep pointing to
14088 the primary template. */
14089 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14090 CLASSTYPE_TI_TEMPLATE (inner) = r;
14091
14092 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14093 inner = TYPE_MAIN_DECL (inner);
14094 }
14095 else if (lambda_fntype)
14096 {
14097 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14098 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14099 }
14100 else
14101 {
14102 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14103 DECL_TI_TEMPLATE (inner) = r;
14104 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14105 }
14106
14107 DECL_TEMPLATE_RESULT (r) = inner;
14108 TREE_TYPE (r) = TREE_TYPE (inner);
14109 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14110
14111 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14112 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14113
14114 if (PRIMARY_TEMPLATE_P (t))
14115 DECL_PRIMARY_TEMPLATE (r) = r;
14116
14117 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
14118 && !lambda_fntype)
14119 /* Record this non-type partial instantiation. */
14120 register_specialization (r, t,
14121 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14122 false, hash);
14123
14124 return r;
14125 }
14126
14127 /* True if FN is the op() for a lambda in an uninstantiated template. */
14128
14129 bool
14130 lambda_fn_in_template_p (tree fn)
14131 {
14132 if (!fn || !LAMBDA_FUNCTION_P (fn))
14133 return false;
14134 tree closure = DECL_CONTEXT (fn);
14135 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14136 }
14137
14138 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14139 which the above is true. */
14140
14141 bool
14142 instantiated_lambda_fn_p (tree fn)
14143 {
14144 if (!fn || !LAMBDA_FUNCTION_P (fn))
14145 return false;
14146 tree closure = DECL_CONTEXT (fn);
14147 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14148 return LAMBDA_EXPR_INSTANTIATED (lam);
14149 }
14150
14151 /* We're instantiating a variable from template function TCTX. Return the
14152 corresponding current enclosing scope. This gets complicated because lambda
14153 functions in templates are regenerated rather than instantiated, but generic
14154 lambda functions are subsequently instantiated. */
14155
14156 static tree
14157 enclosing_instantiation_of (tree otctx)
14158 {
14159 tree tctx = otctx;
14160 tree fn = current_function_decl;
14161 int lambda_count = 0;
14162
14163 for (; tctx && (lambda_fn_in_template_p (tctx)
14164 || instantiated_lambda_fn_p (tctx));
14165 tctx = decl_function_context (tctx))
14166 ++lambda_count;
14167 for (; fn; fn = decl_function_context (fn))
14168 {
14169 tree ofn = fn;
14170 int flambda_count = 0;
14171 for (; fn && instantiated_lambda_fn_p (fn);
14172 fn = decl_function_context (fn))
14173 ++flambda_count;
14174 if ((fn && DECL_TEMPLATE_INFO (fn))
14175 ? most_general_template (fn) != most_general_template (tctx)
14176 : fn != tctx)
14177 continue;
14178 if (flambda_count != lambda_count)
14179 {
14180 gcc_assert (flambda_count > lambda_count);
14181 for (; flambda_count > lambda_count; --flambda_count)
14182 ofn = decl_function_context (ofn);
14183 }
14184 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14185 || DECL_CONV_FN_P (ofn));
14186 return ofn;
14187 }
14188 gcc_unreachable ();
14189 }
14190
14191 /* Substitute the ARGS into the T, which is a _DECL. Return the
14192 result of the substitution. Issue error and warning messages under
14193 control of COMPLAIN. */
14194
14195 static tree
14196 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14197 {
14198 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14199 location_t saved_loc;
14200 tree r = NULL_TREE;
14201 tree in_decl = t;
14202 hashval_t hash = 0;
14203
14204 /* Set the filename and linenumber to improve error-reporting. */
14205 saved_loc = input_location;
14206 input_location = DECL_SOURCE_LOCATION (t);
14207
14208 switch (TREE_CODE (t))
14209 {
14210 case TEMPLATE_DECL:
14211 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14212 break;
14213
14214 case FUNCTION_DECL:
14215 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14216 break;
14217
14218 case PARM_DECL:
14219 {
14220 tree type = NULL_TREE;
14221 int i, len = 1;
14222 tree expanded_types = NULL_TREE;
14223 tree prev_r = NULL_TREE;
14224 tree first_r = NULL_TREE;
14225
14226 if (DECL_PACK_P (t))
14227 {
14228 /* If there is a local specialization that isn't a
14229 parameter pack, it means that we're doing a "simple"
14230 substitution from inside tsubst_pack_expansion. Just
14231 return the local specialization (which will be a single
14232 parm). */
14233 tree spec = retrieve_local_specialization (t);
14234 if (spec
14235 && TREE_CODE (spec) == PARM_DECL
14236 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14237 RETURN (spec);
14238
14239 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14240 the parameters in this function parameter pack. */
14241 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14242 complain, in_decl);
14243 if (TREE_CODE (expanded_types) == TREE_VEC)
14244 {
14245 len = TREE_VEC_LENGTH (expanded_types);
14246
14247 /* Zero-length parameter packs are boring. Just substitute
14248 into the chain. */
14249 if (len == 0 && !cp_unevaluated_operand)
14250 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14251 TREE_CHAIN (t)));
14252 }
14253 else
14254 {
14255 /* All we did was update the type. Make a note of that. */
14256 type = expanded_types;
14257 expanded_types = NULL_TREE;
14258 }
14259 }
14260
14261 /* Loop through all of the parameters we'll build. When T is
14262 a function parameter pack, LEN is the number of expanded
14263 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14264 r = NULL_TREE;
14265 for (i = 0; i < len; ++i)
14266 {
14267 prev_r = r;
14268 r = copy_node (t);
14269 if (DECL_TEMPLATE_PARM_P (t))
14270 SET_DECL_TEMPLATE_PARM_P (r);
14271
14272 if (expanded_types)
14273 /* We're on the Ith parameter of the function parameter
14274 pack. */
14275 {
14276 /* Get the Ith type. */
14277 type = TREE_VEC_ELT (expanded_types, i);
14278
14279 /* Rename the parameter to include the index. */
14280 DECL_NAME (r)
14281 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14282 }
14283 else if (!type)
14284 /* We're dealing with a normal parameter. */
14285 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14286
14287 type = type_decays_to (type);
14288 TREE_TYPE (r) = type;
14289 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14290
14291 if (DECL_INITIAL (r))
14292 {
14293 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14294 DECL_INITIAL (r) = TREE_TYPE (r);
14295 else
14296 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14297 complain, in_decl);
14298 }
14299
14300 DECL_CONTEXT (r) = NULL_TREE;
14301
14302 if (!DECL_TEMPLATE_PARM_P (r))
14303 DECL_ARG_TYPE (r) = type_passed_as (type);
14304
14305 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14306 args, complain, in_decl);
14307
14308 /* Keep track of the first new parameter we
14309 generate. That's what will be returned to the
14310 caller. */
14311 if (!first_r)
14312 first_r = r;
14313
14314 /* Build a proper chain of parameters when substituting
14315 into a function parameter pack. */
14316 if (prev_r)
14317 DECL_CHAIN (prev_r) = r;
14318 }
14319
14320 /* If cp_unevaluated_operand is set, we're just looking for a
14321 single dummy parameter, so don't keep going. */
14322 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14323 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14324 complain, DECL_CHAIN (t));
14325
14326 /* FIRST_R contains the start of the chain we've built. */
14327 r = first_r;
14328 }
14329 break;
14330
14331 case FIELD_DECL:
14332 {
14333 tree type = NULL_TREE;
14334 tree vec = NULL_TREE;
14335 tree expanded_types = NULL_TREE;
14336 int len = 1;
14337
14338 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14339 {
14340 /* This field is a lambda capture pack. Return a TREE_VEC of
14341 the expanded fields to instantiate_class_template_1. */
14342 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14343 complain, in_decl);
14344 if (TREE_CODE (expanded_types) == TREE_VEC)
14345 {
14346 len = TREE_VEC_LENGTH (expanded_types);
14347 vec = make_tree_vec (len);
14348 }
14349 else
14350 {
14351 /* All we did was update the type. Make a note of that. */
14352 type = expanded_types;
14353 expanded_types = NULL_TREE;
14354 }
14355 }
14356
14357 for (int i = 0; i < len; ++i)
14358 {
14359 r = copy_decl (t);
14360 if (expanded_types)
14361 {
14362 type = TREE_VEC_ELT (expanded_types, i);
14363 DECL_NAME (r)
14364 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14365 }
14366 else if (!type)
14367 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14368
14369 if (type == error_mark_node)
14370 RETURN (error_mark_node);
14371 TREE_TYPE (r) = type;
14372 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14373
14374 if (DECL_C_BIT_FIELD (r))
14375 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14376 number of bits. */
14377 DECL_BIT_FIELD_REPRESENTATIVE (r)
14378 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14379 complain, in_decl,
14380 /*integral_constant_expression_p=*/true);
14381 if (DECL_INITIAL (t))
14382 {
14383 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14384 NSDMI in perform_member_init. Still set DECL_INITIAL
14385 so that we know there is one. */
14386 DECL_INITIAL (r) = void_node;
14387 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14388 retrofit_lang_decl (r);
14389 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14390 }
14391 /* We don't have to set DECL_CONTEXT here; it is set by
14392 finish_member_declaration. */
14393 DECL_CHAIN (r) = NULL_TREE;
14394
14395 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14396 args, complain, in_decl);
14397
14398 if (vec)
14399 TREE_VEC_ELT (vec, i) = r;
14400 }
14401
14402 if (vec)
14403 r = vec;
14404 }
14405 break;
14406
14407 case USING_DECL:
14408 /* We reach here only for member using decls. We also need to check
14409 uses_template_parms because DECL_DEPENDENT_P is not set for a
14410 using-declaration that designates a member of the current
14411 instantiation (c++/53549). */
14412 if (DECL_DEPENDENT_P (t)
14413 || uses_template_parms (USING_DECL_SCOPE (t)))
14414 {
14415 tree scope = USING_DECL_SCOPE (t);
14416 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14417 if (PACK_EXPANSION_P (scope))
14418 {
14419 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14420 int len = TREE_VEC_LENGTH (vec);
14421 r = make_tree_vec (len);
14422 for (int i = 0; i < len; ++i)
14423 {
14424 tree escope = TREE_VEC_ELT (vec, i);
14425 tree elt = do_class_using_decl (escope, name);
14426 if (!elt)
14427 {
14428 r = error_mark_node;
14429 break;
14430 }
14431 else
14432 {
14433 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14434 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14435 }
14436 TREE_VEC_ELT (r, i) = elt;
14437 }
14438 }
14439 else
14440 {
14441 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14442 complain, in_decl);
14443 r = do_class_using_decl (inst_scope, name);
14444 if (!r)
14445 r = error_mark_node;
14446 else
14447 {
14448 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14449 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14450 }
14451 }
14452 }
14453 else
14454 {
14455 r = copy_node (t);
14456 DECL_CHAIN (r) = NULL_TREE;
14457 }
14458 break;
14459
14460 case TYPE_DECL:
14461 case VAR_DECL:
14462 {
14463 tree argvec = NULL_TREE;
14464 tree gen_tmpl = NULL_TREE;
14465 tree spec;
14466 tree tmpl = NULL_TREE;
14467 tree ctx;
14468 tree type = NULL_TREE;
14469 bool local_p;
14470
14471 if (TREE_TYPE (t) == error_mark_node)
14472 RETURN (error_mark_node);
14473
14474 if (TREE_CODE (t) == TYPE_DECL
14475 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14476 {
14477 /* If this is the canonical decl, we don't have to
14478 mess with instantiations, and often we can't (for
14479 typename, template type parms and such). Note that
14480 TYPE_NAME is not correct for the above test if
14481 we've copied the type for a typedef. */
14482 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14483 if (type == error_mark_node)
14484 RETURN (error_mark_node);
14485 r = TYPE_NAME (type);
14486 break;
14487 }
14488
14489 /* Check to see if we already have the specialization we
14490 need. */
14491 spec = NULL_TREE;
14492 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
14493 {
14494 /* T is a static data member or namespace-scope entity.
14495 We have to substitute into namespace-scope variables
14496 (not just variable templates) because of cases like:
14497
14498 template <class T> void f() { extern T t; }
14499
14500 where the entity referenced is not known until
14501 instantiation time. */
14502 local_p = false;
14503 ctx = DECL_CONTEXT (t);
14504 if (DECL_CLASS_SCOPE_P (t))
14505 {
14506 ctx = tsubst_aggr_type (ctx, args,
14507 complain,
14508 in_decl, /*entering_scope=*/1);
14509 /* If CTX is unchanged, then T is in fact the
14510 specialization we want. That situation occurs when
14511 referencing a static data member within in its own
14512 class. We can use pointer equality, rather than
14513 same_type_p, because DECL_CONTEXT is always
14514 canonical... */
14515 if (ctx == DECL_CONTEXT (t)
14516 /* ... unless T is a member template; in which
14517 case our caller can be willing to create a
14518 specialization of that template represented
14519 by T. */
14520 && !(DECL_TI_TEMPLATE (t)
14521 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14522 spec = t;
14523 }
14524
14525 if (!spec)
14526 {
14527 tmpl = DECL_TI_TEMPLATE (t);
14528 gen_tmpl = most_general_template (tmpl);
14529 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14530 if (argvec != error_mark_node)
14531 argvec = (coerce_innermost_template_parms
14532 (DECL_TEMPLATE_PARMS (gen_tmpl),
14533 argvec, t, complain,
14534 /*all*/true, /*defarg*/true));
14535 if (argvec == error_mark_node)
14536 RETURN (error_mark_node);
14537 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14538 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14539 }
14540 }
14541 else
14542 {
14543 /* A local variable. */
14544 local_p = true;
14545 /* Subsequent calls to pushdecl will fill this in. */
14546 ctx = NULL_TREE;
14547 /* Unless this is a reference to a static variable from an
14548 enclosing function, in which case we need to fill it in now. */
14549 if (TREE_STATIC (t))
14550 {
14551 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14552 if (fn != current_function_decl)
14553 ctx = fn;
14554 }
14555 spec = retrieve_local_specialization (t);
14556 }
14557 /* If we already have the specialization we need, there is
14558 nothing more to do. */
14559 if (spec)
14560 {
14561 r = spec;
14562 break;
14563 }
14564
14565 /* Create a new node for the specialization we need. */
14566 if (type == NULL_TREE)
14567 {
14568 if (is_typedef_decl (t))
14569 type = DECL_ORIGINAL_TYPE (t);
14570 else
14571 type = TREE_TYPE (t);
14572 if (VAR_P (t)
14573 && VAR_HAD_UNKNOWN_BOUND (t)
14574 && type != error_mark_node)
14575 type = strip_array_domain (type);
14576 tree sub_args = args;
14577 if (tree auto_node = type_uses_auto (type))
14578 {
14579 /* Mask off any template args past the variable's context so we
14580 don't replace the auto with an unrelated argument. */
14581 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14582 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14583 if (extra > 0)
14584 /* This should never happen with the new lambda instantiation
14585 model, but keep the handling just in case. */
14586 gcc_assert (!CHECKING_P),
14587 sub_args = strip_innermost_template_args (args, extra);
14588 }
14589 type = tsubst (type, sub_args, complain, in_decl);
14590 /* Substituting the type might have recursively instantiated this
14591 same alias (c++/86171). */
14592 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14593 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14594 {
14595 r = spec;
14596 break;
14597 }
14598 }
14599 r = copy_decl (t);
14600 if (VAR_P (r))
14601 {
14602 DECL_INITIALIZED_P (r) = 0;
14603 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14604 if (type == error_mark_node)
14605 RETURN (error_mark_node);
14606 if (TREE_CODE (type) == FUNCTION_TYPE)
14607 {
14608 /* It may seem that this case cannot occur, since:
14609
14610 typedef void f();
14611 void g() { f x; }
14612
14613 declares a function, not a variable. However:
14614
14615 typedef void f();
14616 template <typename T> void g() { T t; }
14617 template void g<f>();
14618
14619 is an attempt to declare a variable with function
14620 type. */
14621 error ("variable %qD has function type",
14622 /* R is not yet sufficiently initialized, so we
14623 just use its name. */
14624 DECL_NAME (r));
14625 RETURN (error_mark_node);
14626 }
14627 type = complete_type (type);
14628 /* Wait until cp_finish_decl to set this again, to handle
14629 circular dependency (template/instantiate6.C). */
14630 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14631 type = check_var_type (DECL_NAME (r), type,
14632 DECL_SOURCE_LOCATION (r));
14633 if (DECL_HAS_VALUE_EXPR_P (t))
14634 {
14635 tree ve = DECL_VALUE_EXPR (t);
14636 /* If the DECL_VALUE_EXPR is converted to the declared type,
14637 preserve the identity so that gimplify_type_sizes works. */
14638 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14639 if (nop)
14640 ve = TREE_OPERAND (ve, 0);
14641 ve = tsubst_expr (ve, args, complain, in_decl,
14642 /*constant_expression_p=*/false);
14643 if (REFERENCE_REF_P (ve))
14644 {
14645 gcc_assert (TYPE_REF_P (type));
14646 ve = TREE_OPERAND (ve, 0);
14647 }
14648 if (nop)
14649 ve = build_nop (type, ve);
14650 else if (DECL_LANG_SPECIFIC (t)
14651 && DECL_OMP_PRIVATIZED_MEMBER (t)
14652 && TREE_CODE (ve) == COMPONENT_REF
14653 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14654 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14655 type = TREE_TYPE (ve);
14656 else
14657 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14658 == TYPE_MAIN_VARIANT (type));
14659 SET_DECL_VALUE_EXPR (r, ve);
14660 }
14661 if (CP_DECL_THREAD_LOCAL_P (r)
14662 && !processing_template_decl)
14663 set_decl_tls_model (r, decl_default_tls_model (r));
14664 }
14665 else if (DECL_SELF_REFERENCE_P (t))
14666 SET_DECL_SELF_REFERENCE_P (r);
14667 TREE_TYPE (r) = type;
14668 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14669 DECL_CONTEXT (r) = ctx;
14670 /* Clear out the mangled name and RTL for the instantiation. */
14671 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14672 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14673 SET_DECL_RTL (r, NULL);
14674 /* The initializer must not be expanded until it is required;
14675 see [temp.inst]. */
14676 DECL_INITIAL (r) = NULL_TREE;
14677 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14678 if (VAR_P (r))
14679 {
14680 if (DECL_LANG_SPECIFIC (r))
14681 SET_DECL_DEPENDENT_INIT_P (r, false);
14682
14683 SET_DECL_MODE (r, VOIDmode);
14684
14685 /* Possibly limit visibility based on template args. */
14686 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14687 if (DECL_VISIBILITY_SPECIFIED (t))
14688 {
14689 DECL_VISIBILITY_SPECIFIED (r) = 0;
14690 DECL_ATTRIBUTES (r)
14691 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14692 }
14693 determine_visibility (r);
14694 }
14695
14696 if (!local_p)
14697 {
14698 /* A static data member declaration is always marked
14699 external when it is declared in-class, even if an
14700 initializer is present. We mimic the non-template
14701 processing here. */
14702 DECL_EXTERNAL (r) = 1;
14703 if (DECL_NAMESPACE_SCOPE_P (t))
14704 DECL_NOT_REALLY_EXTERN (r) = 1;
14705
14706 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14707 SET_DECL_IMPLICIT_INSTANTIATION (r);
14708 /* Remember whether we require constant initialization of
14709 a non-constant template variable. */
14710 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
14711 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
14712 if (!error_operand_p (r) || (complain & tf_error))
14713 register_specialization (r, gen_tmpl, argvec, false, hash);
14714 }
14715 else
14716 {
14717 if (DECL_LANG_SPECIFIC (r))
14718 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14719 if (!cp_unevaluated_operand)
14720 register_local_specialization (r, t);
14721 }
14722
14723 DECL_CHAIN (r) = NULL_TREE;
14724
14725 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14726 /*flags=*/0,
14727 args, complain, in_decl);
14728
14729 /* Preserve a typedef that names a type. */
14730 if (is_typedef_decl (r) && type != error_mark_node)
14731 {
14732 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14733 set_underlying_type (r);
14734 if (TYPE_DECL_ALIAS_P (r))
14735 /* An alias template specialization can be dependent
14736 even if its underlying type is not. */
14737 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14738 }
14739
14740 layout_decl (r, 0);
14741 }
14742 break;
14743
14744 default:
14745 gcc_unreachable ();
14746 }
14747 #undef RETURN
14748
14749 out:
14750 /* Restore the file and line information. */
14751 input_location = saved_loc;
14752
14753 return r;
14754 }
14755
14756 /* Substitute into the complete parameter type list PARMS. */
14757
14758 tree
14759 tsubst_function_parms (tree parms,
14760 tree args,
14761 tsubst_flags_t complain,
14762 tree in_decl)
14763 {
14764 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14765 }
14766
14767 /* Substitute into the ARG_TYPES of a function type.
14768 If END is a TREE_CHAIN, leave it and any following types
14769 un-substituted. */
14770
14771 static tree
14772 tsubst_arg_types (tree arg_types,
14773 tree args,
14774 tree end,
14775 tsubst_flags_t complain,
14776 tree in_decl)
14777 {
14778 tree remaining_arg_types;
14779 tree type = NULL_TREE;
14780 int i = 1;
14781 tree expanded_args = NULL_TREE;
14782 tree default_arg;
14783
14784 if (!arg_types || arg_types == void_list_node || arg_types == end)
14785 return arg_types;
14786
14787 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14788 args, end, complain, in_decl);
14789 if (remaining_arg_types == error_mark_node)
14790 return error_mark_node;
14791
14792 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14793 {
14794 /* For a pack expansion, perform substitution on the
14795 entire expression. Later on, we'll handle the arguments
14796 one-by-one. */
14797 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14798 args, complain, in_decl);
14799
14800 if (TREE_CODE (expanded_args) == TREE_VEC)
14801 /* So that we'll spin through the parameters, one by one. */
14802 i = TREE_VEC_LENGTH (expanded_args);
14803 else
14804 {
14805 /* We only partially substituted into the parameter
14806 pack. Our type is TYPE_PACK_EXPANSION. */
14807 type = expanded_args;
14808 expanded_args = NULL_TREE;
14809 }
14810 }
14811
14812 while (i > 0) {
14813 --i;
14814
14815 if (expanded_args)
14816 type = TREE_VEC_ELT (expanded_args, i);
14817 else if (!type)
14818 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14819
14820 if (type == error_mark_node)
14821 return error_mark_node;
14822 if (VOID_TYPE_P (type))
14823 {
14824 if (complain & tf_error)
14825 {
14826 error ("invalid parameter type %qT", type);
14827 if (in_decl)
14828 error ("in declaration %q+D", in_decl);
14829 }
14830 return error_mark_node;
14831 }
14832 /* DR 657. */
14833 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14834 return error_mark_node;
14835
14836 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14837 top-level qualifiers as required. */
14838 type = cv_unqualified (type_decays_to (type));
14839
14840 /* We do not substitute into default arguments here. The standard
14841 mandates that they be instantiated only when needed, which is
14842 done in build_over_call. */
14843 default_arg = TREE_PURPOSE (arg_types);
14844
14845 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14846 since the new op() won't have any associated template arguments for us
14847 to refer to later. */
14848 if (lambda_fn_in_template_p (in_decl))
14849 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14850 false/*fn*/, false/*constexpr*/);
14851
14852 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14853 {
14854 /* We've instantiated a template before its default arguments
14855 have been parsed. This can happen for a nested template
14856 class, and is not an error unless we require the default
14857 argument in a call of this function. */
14858 remaining_arg_types =
14859 tree_cons (default_arg, type, remaining_arg_types);
14860 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14861 remaining_arg_types);
14862 }
14863 else
14864 remaining_arg_types =
14865 hash_tree_cons (default_arg, type, remaining_arg_types);
14866 }
14867
14868 return remaining_arg_types;
14869 }
14870
14871 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14872 *not* handle the exception-specification for FNTYPE, because the
14873 initial substitution of explicitly provided template parameters
14874 during argument deduction forbids substitution into the
14875 exception-specification:
14876
14877 [temp.deduct]
14878
14879 All references in the function type of the function template to the
14880 corresponding template parameters are replaced by the specified tem-
14881 plate argument values. If a substitution in a template parameter or
14882 in the function type of the function template results in an invalid
14883 type, type deduction fails. [Note: The equivalent substitution in
14884 exception specifications is done only when the function is instanti-
14885 ated, at which point a program is ill-formed if the substitution
14886 results in an invalid type.] */
14887
14888 static tree
14889 tsubst_function_type (tree t,
14890 tree args,
14891 tsubst_flags_t complain,
14892 tree in_decl)
14893 {
14894 tree return_type;
14895 tree arg_types = NULL_TREE;
14896
14897 /* The TYPE_CONTEXT is not used for function/method types. */
14898 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14899
14900 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14901 failure. */
14902 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14903
14904 if (late_return_type_p)
14905 {
14906 /* Substitute the argument types. */
14907 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14908 complain, in_decl);
14909 if (arg_types == error_mark_node)
14910 return error_mark_node;
14911
14912 tree save_ccp = current_class_ptr;
14913 tree save_ccr = current_class_ref;
14914 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14915 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14916 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14917 if (do_inject)
14918 {
14919 /* DR 1207: 'this' is in scope in the trailing return type. */
14920 inject_this_parameter (this_type, cp_type_quals (this_type));
14921 }
14922
14923 /* Substitute the return type. */
14924 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14925
14926 if (do_inject)
14927 {
14928 current_class_ptr = save_ccp;
14929 current_class_ref = save_ccr;
14930 }
14931 }
14932 else
14933 /* Substitute the return type. */
14934 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14935
14936 if (return_type == error_mark_node)
14937 return error_mark_node;
14938 /* DR 486 clarifies that creation of a function type with an
14939 invalid return type is a deduction failure. */
14940 if (TREE_CODE (return_type) == ARRAY_TYPE
14941 || TREE_CODE (return_type) == FUNCTION_TYPE)
14942 {
14943 if (complain & tf_error)
14944 {
14945 if (TREE_CODE (return_type) == ARRAY_TYPE)
14946 error ("function returning an array");
14947 else
14948 error ("function returning a function");
14949 }
14950 return error_mark_node;
14951 }
14952 /* And DR 657. */
14953 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14954 return error_mark_node;
14955
14956 if (!late_return_type_p)
14957 {
14958 /* Substitute the argument types. */
14959 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14960 complain, in_decl);
14961 if (arg_types == error_mark_node)
14962 return error_mark_node;
14963 }
14964
14965 /* Construct a new type node and return it. */
14966 return rebuild_function_or_method_type (t, return_type, arg_types,
14967 /*raises=*/NULL_TREE, complain);
14968 }
14969
14970 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14971 ARGS into that specification, and return the substituted
14972 specification. If there is no specification, return NULL_TREE. */
14973
14974 static tree
14975 tsubst_exception_specification (tree fntype,
14976 tree args,
14977 tsubst_flags_t complain,
14978 tree in_decl,
14979 bool defer_ok)
14980 {
14981 tree specs;
14982 tree new_specs;
14983
14984 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14985 new_specs = NULL_TREE;
14986 if (specs && TREE_PURPOSE (specs))
14987 {
14988 /* A noexcept-specifier. */
14989 tree expr = TREE_PURPOSE (specs);
14990 if (TREE_CODE (expr) == INTEGER_CST)
14991 new_specs = expr;
14992 else if (defer_ok)
14993 {
14994 /* Defer instantiation of noexcept-specifiers to avoid
14995 excessive instantiations (c++/49107). */
14996 new_specs = make_node (DEFERRED_NOEXCEPT);
14997 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14998 {
14999 /* We already partially instantiated this member template,
15000 so combine the new args with the old. */
15001 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15002 = DEFERRED_NOEXCEPT_PATTERN (expr);
15003 DEFERRED_NOEXCEPT_ARGS (new_specs)
15004 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15005 }
15006 else
15007 {
15008 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15009 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15010 }
15011 }
15012 else
15013 {
15014 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15015 {
15016 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15017 args);
15018 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15019 }
15020 new_specs = tsubst_copy_and_build
15021 (expr, args, complain, in_decl, /*function_p=*/false,
15022 /*integral_constant_expression_p=*/true);
15023 }
15024 new_specs = build_noexcept_spec (new_specs, complain);
15025 }
15026 else if (specs)
15027 {
15028 if (! TREE_VALUE (specs))
15029 new_specs = specs;
15030 else
15031 while (specs)
15032 {
15033 tree spec;
15034 int i, len = 1;
15035 tree expanded_specs = NULL_TREE;
15036
15037 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15038 {
15039 /* Expand the pack expansion type. */
15040 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15041 args, complain,
15042 in_decl);
15043
15044 if (expanded_specs == error_mark_node)
15045 return error_mark_node;
15046 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15047 len = TREE_VEC_LENGTH (expanded_specs);
15048 else
15049 {
15050 /* We're substituting into a member template, so
15051 we got a TYPE_PACK_EXPANSION back. Add that
15052 expansion and move on. */
15053 gcc_assert (TREE_CODE (expanded_specs)
15054 == TYPE_PACK_EXPANSION);
15055 new_specs = add_exception_specifier (new_specs,
15056 expanded_specs,
15057 complain);
15058 specs = TREE_CHAIN (specs);
15059 continue;
15060 }
15061 }
15062
15063 for (i = 0; i < len; ++i)
15064 {
15065 if (expanded_specs)
15066 spec = TREE_VEC_ELT (expanded_specs, i);
15067 else
15068 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15069 if (spec == error_mark_node)
15070 return spec;
15071 new_specs = add_exception_specifier (new_specs, spec,
15072 complain);
15073 }
15074
15075 specs = TREE_CHAIN (specs);
15076 }
15077 }
15078 return new_specs;
15079 }
15080
15081 /* Substitute through a TREE_LIST of types or expressions, handling pack
15082 expansions. */
15083
15084 tree
15085 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15086 {
15087 if (t == void_list_node)
15088 return t;
15089
15090 tree purpose = TREE_PURPOSE (t);
15091 tree purposevec = NULL_TREE;
15092 if (!purpose)
15093 ;
15094 else if (PACK_EXPANSION_P (purpose))
15095 {
15096 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15097 if (TREE_CODE (purpose) == TREE_VEC)
15098 purposevec = purpose;
15099 }
15100 else if (TYPE_P (purpose))
15101 purpose = tsubst (purpose, args, complain, in_decl);
15102 else
15103 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15104 if (purpose == error_mark_node || purposevec == error_mark_node)
15105 return error_mark_node;
15106
15107 tree value = TREE_VALUE (t);
15108 tree valuevec = NULL_TREE;
15109 if (!value)
15110 ;
15111 else if (PACK_EXPANSION_P (value))
15112 {
15113 value = tsubst_pack_expansion (value, args, complain, in_decl);
15114 if (TREE_CODE (value) == TREE_VEC)
15115 valuevec = value;
15116 }
15117 else if (TYPE_P (value))
15118 value = tsubst (value, args, complain, in_decl);
15119 else
15120 value = tsubst_copy_and_build (value, args, complain, in_decl);
15121 if (value == error_mark_node || valuevec == error_mark_node)
15122 return error_mark_node;
15123
15124 tree chain = TREE_CHAIN (t);
15125 if (!chain)
15126 ;
15127 else if (TREE_CODE (chain) == TREE_LIST)
15128 chain = tsubst_tree_list (chain, args, complain, in_decl);
15129 else if (TYPE_P (chain))
15130 chain = tsubst (chain, args, complain, in_decl);
15131 else
15132 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15133 if (chain == error_mark_node)
15134 return error_mark_node;
15135
15136 if (purpose == TREE_PURPOSE (t)
15137 && value == TREE_VALUE (t)
15138 && chain == TREE_CHAIN (t))
15139 return t;
15140
15141 int len;
15142 /* Determine the number of arguments. */
15143 if (purposevec)
15144 {
15145 len = TREE_VEC_LENGTH (purposevec);
15146 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15147 }
15148 else if (valuevec)
15149 len = TREE_VEC_LENGTH (valuevec);
15150 else
15151 len = 1;
15152
15153 for (int i = len; i-- > 0; )
15154 {
15155 if (purposevec)
15156 purpose = TREE_VEC_ELT (purposevec, i);
15157 if (valuevec)
15158 value = TREE_VEC_ELT (valuevec, i);
15159
15160 if (value && TYPE_P (value))
15161 chain = hash_tree_cons (purpose, value, chain);
15162 else
15163 chain = tree_cons (purpose, value, chain);
15164 }
15165
15166 return chain;
15167 }
15168
15169 /* Take the tree structure T and replace template parameters used
15170 therein with the argument vector ARGS. IN_DECL is an associated
15171 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15172 Issue error and warning messages under control of COMPLAIN. Note
15173 that we must be relatively non-tolerant of extensions here, in
15174 order to preserve conformance; if we allow substitutions that
15175 should not be allowed, we may allow argument deductions that should
15176 not succeed, and therefore report ambiguous overload situations
15177 where there are none. In theory, we could allow the substitution,
15178 but indicate that it should have failed, and allow our caller to
15179 make sure that the right thing happens, but we don't try to do this
15180 yet.
15181
15182 This function is used for dealing with types, decls and the like;
15183 for expressions, use tsubst_expr or tsubst_copy. */
15184
15185 tree
15186 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15187 {
15188 enum tree_code code;
15189 tree type, r = NULL_TREE;
15190
15191 if (t == NULL_TREE || t == error_mark_node
15192 || t == integer_type_node
15193 || t == void_type_node
15194 || t == char_type_node
15195 || t == unknown_type_node
15196 || TREE_CODE (t) == NAMESPACE_DECL
15197 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15198 return t;
15199
15200 if (DECL_P (t))
15201 return tsubst_decl (t, args, complain);
15202
15203 if (args == NULL_TREE)
15204 return t;
15205
15206 code = TREE_CODE (t);
15207
15208 if (code == IDENTIFIER_NODE)
15209 type = IDENTIFIER_TYPE_VALUE (t);
15210 else
15211 type = TREE_TYPE (t);
15212
15213 gcc_assert (type != unknown_type_node);
15214
15215 /* Reuse typedefs. We need to do this to handle dependent attributes,
15216 such as attribute aligned. */
15217 if (TYPE_P (t)
15218 && typedef_variant_p (t))
15219 {
15220 tree decl = TYPE_NAME (t);
15221
15222 if (alias_template_specialization_p (t, nt_opaque))
15223 {
15224 /* DECL represents an alias template and we want to
15225 instantiate it. */
15226 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15227 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15228 r = instantiate_alias_template (tmpl, gen_args, complain);
15229 }
15230 else if (DECL_CLASS_SCOPE_P (decl)
15231 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15232 && uses_template_parms (DECL_CONTEXT (decl)))
15233 {
15234 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15235 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15236 r = retrieve_specialization (tmpl, gen_args, 0);
15237 }
15238 else if (DECL_FUNCTION_SCOPE_P (decl)
15239 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15240 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15241 r = retrieve_local_specialization (decl);
15242 else
15243 /* The typedef is from a non-template context. */
15244 return t;
15245
15246 if (r)
15247 {
15248 r = TREE_TYPE (r);
15249 r = cp_build_qualified_type_real
15250 (r, cp_type_quals (t) | cp_type_quals (r),
15251 complain | tf_ignore_bad_quals);
15252 return r;
15253 }
15254 else
15255 {
15256 /* We don't have an instantiation yet, so drop the typedef. */
15257 int quals = cp_type_quals (t);
15258 t = DECL_ORIGINAL_TYPE (decl);
15259 t = cp_build_qualified_type_real (t, quals,
15260 complain | tf_ignore_bad_quals);
15261 }
15262 }
15263
15264 bool fndecl_type = (complain & tf_fndecl_type);
15265 complain &= ~tf_fndecl_type;
15266
15267 if (type
15268 && code != TYPENAME_TYPE
15269 && code != TEMPLATE_TYPE_PARM
15270 && code != TEMPLATE_PARM_INDEX
15271 && code != IDENTIFIER_NODE
15272 && code != FUNCTION_TYPE
15273 && code != METHOD_TYPE)
15274 type = tsubst (type, args, complain, in_decl);
15275 if (type == error_mark_node)
15276 return error_mark_node;
15277
15278 switch (code)
15279 {
15280 case RECORD_TYPE:
15281 case UNION_TYPE:
15282 case ENUMERAL_TYPE:
15283 return tsubst_aggr_type (t, args, complain, in_decl,
15284 /*entering_scope=*/0);
15285
15286 case ERROR_MARK:
15287 case IDENTIFIER_NODE:
15288 case VOID_TYPE:
15289 case REAL_TYPE:
15290 case COMPLEX_TYPE:
15291 case VECTOR_TYPE:
15292 case BOOLEAN_TYPE:
15293 case NULLPTR_TYPE:
15294 case LANG_TYPE:
15295 return t;
15296
15297 case INTEGER_TYPE:
15298 if (t == integer_type_node)
15299 return t;
15300
15301 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15302 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15303 return t;
15304
15305 {
15306 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15307
15308 max = tsubst_expr (omax, args, complain, in_decl,
15309 /*integral_constant_expression_p=*/false);
15310
15311 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15312 needed. */
15313 if (TREE_CODE (max) == NOP_EXPR
15314 && TREE_SIDE_EFFECTS (omax)
15315 && !TREE_TYPE (max))
15316 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15317
15318 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15319 with TREE_SIDE_EFFECTS that indicates this is not an integral
15320 constant expression. */
15321 if (processing_template_decl
15322 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15323 {
15324 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15325 TREE_SIDE_EFFECTS (max) = 1;
15326 }
15327
15328 return compute_array_index_type (NULL_TREE, max, complain);
15329 }
15330
15331 case TEMPLATE_TYPE_PARM:
15332 case TEMPLATE_TEMPLATE_PARM:
15333 case BOUND_TEMPLATE_TEMPLATE_PARM:
15334 case TEMPLATE_PARM_INDEX:
15335 {
15336 int idx;
15337 int level;
15338 int levels;
15339 tree arg = NULL_TREE;
15340
15341 r = NULL_TREE;
15342
15343 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15344 template_parm_level_and_index (t, &level, &idx);
15345
15346 levels = TMPL_ARGS_DEPTH (args);
15347 if (level <= levels
15348 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15349 {
15350 arg = TMPL_ARG (args, level, idx);
15351
15352 /* See through ARGUMENT_PACK_SELECT arguments. */
15353 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15354 arg = argument_pack_select_arg (arg);
15355 }
15356
15357 if (arg == error_mark_node)
15358 return error_mark_node;
15359 else if (arg != NULL_TREE)
15360 {
15361 if (ARGUMENT_PACK_P (arg))
15362 /* If ARG is an argument pack, we don't actually want to
15363 perform a substitution here, because substitutions
15364 for argument packs are only done
15365 element-by-element. We can get to this point when
15366 substituting the type of a non-type template
15367 parameter pack, when that type actually contains
15368 template parameter packs from an outer template, e.g.,
15369
15370 template<typename... Types> struct A {
15371 template<Types... Values> struct B { };
15372 }; */
15373 return t;
15374
15375 if (code == TEMPLATE_TYPE_PARM)
15376 {
15377 int quals;
15378
15379 /* When building concept checks for the purpose of
15380 deducing placeholders, we can end up with wildcards
15381 where types are expected. Adjust this to the deduced
15382 value. */
15383 if (TREE_CODE (arg) == WILDCARD_DECL)
15384 arg = TREE_TYPE (TREE_TYPE (arg));
15385
15386 gcc_assert (TYPE_P (arg));
15387
15388 quals = cp_type_quals (arg) | cp_type_quals (t);
15389
15390 return cp_build_qualified_type_real
15391 (arg, quals, complain | tf_ignore_bad_quals);
15392 }
15393 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15394 {
15395 /* We are processing a type constructed from a
15396 template template parameter. */
15397 tree argvec = tsubst (TYPE_TI_ARGS (t),
15398 args, complain, in_decl);
15399 if (argvec == error_mark_node)
15400 return error_mark_node;
15401
15402 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15403 || TREE_CODE (arg) == TEMPLATE_DECL
15404 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15405
15406 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15407 /* Consider this code:
15408
15409 template <template <class> class Template>
15410 struct Internal {
15411 template <class Arg> using Bind = Template<Arg>;
15412 };
15413
15414 template <template <class> class Template, class Arg>
15415 using Instantiate = Template<Arg>; //#0
15416
15417 template <template <class> class Template,
15418 class Argument>
15419 using Bind =
15420 Instantiate<Internal<Template>::template Bind,
15421 Argument>; //#1
15422
15423 When #1 is parsed, the
15424 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15425 parameter `Template' in #0 matches the
15426 UNBOUND_CLASS_TEMPLATE representing the argument
15427 `Internal<Template>::template Bind'; We then want
15428 to assemble the type `Bind<Argument>' that can't
15429 be fully created right now, because
15430 `Internal<Template>' not being complete, the Bind
15431 template cannot be looked up in that context. So
15432 we need to "store" `Bind<Argument>' for later
15433 when the context of Bind becomes complete. Let's
15434 store that in a TYPENAME_TYPE. */
15435 return make_typename_type (TYPE_CONTEXT (arg),
15436 build_nt (TEMPLATE_ID_EXPR,
15437 TYPE_IDENTIFIER (arg),
15438 argvec),
15439 typename_type,
15440 complain);
15441
15442 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15443 are resolving nested-types in the signature of a
15444 member function templates. Otherwise ARG is a
15445 TEMPLATE_DECL and is the real template to be
15446 instantiated. */
15447 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15448 arg = TYPE_NAME (arg);
15449
15450 r = lookup_template_class (arg,
15451 argvec, in_decl,
15452 DECL_CONTEXT (arg),
15453 /*entering_scope=*/0,
15454 complain);
15455 return cp_build_qualified_type_real
15456 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15457 }
15458 else if (code == TEMPLATE_TEMPLATE_PARM)
15459 return arg;
15460 else
15461 /* TEMPLATE_PARM_INDEX. */
15462 return convert_from_reference (unshare_expr (arg));
15463 }
15464
15465 if (level == 1)
15466 /* This can happen during the attempted tsubst'ing in
15467 unify. This means that we don't yet have any information
15468 about the template parameter in question. */
15469 return t;
15470
15471 /* Early in template argument deduction substitution, we don't
15472 want to reduce the level of 'auto', or it will be confused
15473 with a normal template parm in subsequent deduction.
15474 Similarly, don't reduce the level of template parameters to
15475 avoid mismatches when deducing their types. */
15476 if (complain & tf_partial)
15477 return t;
15478
15479 /* If we get here, we must have been looking at a parm for a
15480 more deeply nested template. Make a new version of this
15481 template parameter, but with a lower level. */
15482 switch (code)
15483 {
15484 case TEMPLATE_TYPE_PARM:
15485 case TEMPLATE_TEMPLATE_PARM:
15486 case BOUND_TEMPLATE_TEMPLATE_PARM:
15487 if (cp_type_quals (t))
15488 {
15489 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15490 r = cp_build_qualified_type_real
15491 (r, cp_type_quals (t),
15492 complain | (code == TEMPLATE_TYPE_PARM
15493 ? tf_ignore_bad_quals : 0));
15494 }
15495 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15496 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15497 && (r = (TEMPLATE_PARM_DESCENDANTS
15498 (TEMPLATE_TYPE_PARM_INDEX (t))))
15499 && (r = TREE_TYPE (r))
15500 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15501 /* Break infinite recursion when substituting the constraints
15502 of a constrained placeholder. */;
15503 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15504 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15505 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15506 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15507 r = TEMPLATE_PARM_DESCENDANTS (arg))
15508 && (TEMPLATE_PARM_LEVEL (r)
15509 == TEMPLATE_PARM_LEVEL (arg) - levels))
15510 /* Cache the simple case of lowering a type parameter. */
15511 r = TREE_TYPE (r);
15512 else
15513 {
15514 r = copy_type (t);
15515 TEMPLATE_TYPE_PARM_INDEX (r)
15516 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15517 r, levels, args, complain);
15518 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15519 TYPE_MAIN_VARIANT (r) = r;
15520 TYPE_POINTER_TO (r) = NULL_TREE;
15521 TYPE_REFERENCE_TO (r) = NULL_TREE;
15522
15523 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15524 {
15525 /* Propagate constraints on placeholders since they are
15526 only instantiated during satisfaction. */
15527 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15528 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15529 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15530 {
15531 pl = tsubst_copy (pl, args, complain, in_decl);
15532 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15533 }
15534 }
15535
15536 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15537 /* We have reduced the level of the template
15538 template parameter, but not the levels of its
15539 template parameters, so canonical_type_parameter
15540 will not be able to find the canonical template
15541 template parameter for this level. Thus, we
15542 require structural equality checking to compare
15543 TEMPLATE_TEMPLATE_PARMs. */
15544 SET_TYPE_STRUCTURAL_EQUALITY (r);
15545 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15546 SET_TYPE_STRUCTURAL_EQUALITY (r);
15547 else
15548 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15549
15550 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15551 {
15552 tree tinfo = TYPE_TEMPLATE_INFO (t);
15553 /* We might need to substitute into the types of non-type
15554 template parameters. */
15555 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15556 complain, in_decl);
15557 if (tmpl == error_mark_node)
15558 return error_mark_node;
15559 tree argvec = tsubst (TI_ARGS (tinfo), args,
15560 complain, in_decl);
15561 if (argvec == error_mark_node)
15562 return error_mark_node;
15563
15564 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15565 = build_template_info (tmpl, argvec);
15566 }
15567 }
15568 break;
15569
15570 case TEMPLATE_PARM_INDEX:
15571 /* OK, now substitute the type of the non-type parameter. We
15572 couldn't do it earlier because it might be an auto parameter,
15573 and we wouldn't need to if we had an argument. */
15574 type = tsubst (type, args, complain, in_decl);
15575 if (type == error_mark_node)
15576 return error_mark_node;
15577 r = reduce_template_parm_level (t, type, levels, args, complain);
15578 break;
15579
15580 default:
15581 gcc_unreachable ();
15582 }
15583
15584 return r;
15585 }
15586
15587 case TREE_LIST:
15588 return tsubst_tree_list (t, args, complain, in_decl);
15589
15590 case TREE_BINFO:
15591 /* We should never be tsubsting a binfo. */
15592 gcc_unreachable ();
15593
15594 case TREE_VEC:
15595 /* A vector of template arguments. */
15596 gcc_assert (!type);
15597 return tsubst_template_args (t, args, complain, in_decl);
15598
15599 case POINTER_TYPE:
15600 case REFERENCE_TYPE:
15601 {
15602 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15603 return t;
15604
15605 /* [temp.deduct]
15606
15607 Type deduction may fail for any of the following
15608 reasons:
15609
15610 -- Attempting to create a pointer to reference type.
15611 -- Attempting to create a reference to a reference type or
15612 a reference to void.
15613
15614 Core issue 106 says that creating a reference to a reference
15615 during instantiation is no longer a cause for failure. We
15616 only enforce this check in strict C++98 mode. */
15617 if ((TYPE_REF_P (type)
15618 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15619 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15620 {
15621 static location_t last_loc;
15622
15623 /* We keep track of the last time we issued this error
15624 message to avoid spewing a ton of messages during a
15625 single bad template instantiation. */
15626 if (complain & tf_error
15627 && last_loc != input_location)
15628 {
15629 if (VOID_TYPE_P (type))
15630 error ("forming reference to void");
15631 else if (code == POINTER_TYPE)
15632 error ("forming pointer to reference type %qT", type);
15633 else
15634 error ("forming reference to reference type %qT", type);
15635 last_loc = input_location;
15636 }
15637
15638 return error_mark_node;
15639 }
15640 else if (TREE_CODE (type) == FUNCTION_TYPE
15641 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15642 || type_memfn_rqual (type) != REF_QUAL_NONE))
15643 {
15644 if (complain & tf_error)
15645 {
15646 if (code == POINTER_TYPE)
15647 error ("forming pointer to qualified function type %qT",
15648 type);
15649 else
15650 error ("forming reference to qualified function type %qT",
15651 type);
15652 }
15653 return error_mark_node;
15654 }
15655 else if (code == POINTER_TYPE)
15656 {
15657 r = build_pointer_type (type);
15658 if (TREE_CODE (type) == METHOD_TYPE)
15659 r = build_ptrmemfunc_type (r);
15660 }
15661 else if (TYPE_REF_P (type))
15662 /* In C++0x, during template argument substitution, when there is an
15663 attempt to create a reference to a reference type, reference
15664 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15665
15666 "If a template-argument for a template-parameter T names a type
15667 that is a reference to a type A, an attempt to create the type
15668 'lvalue reference to cv T' creates the type 'lvalue reference to
15669 A,' while an attempt to create the type type rvalue reference to
15670 cv T' creates the type T"
15671 */
15672 r = cp_build_reference_type
15673 (TREE_TYPE (type),
15674 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15675 else
15676 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15677 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15678
15679 if (r != error_mark_node)
15680 /* Will this ever be needed for TYPE_..._TO values? */
15681 layout_type (r);
15682
15683 return r;
15684 }
15685 case OFFSET_TYPE:
15686 {
15687 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15688 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15689 {
15690 /* [temp.deduct]
15691
15692 Type deduction may fail for any of the following
15693 reasons:
15694
15695 -- Attempting to create "pointer to member of T" when T
15696 is not a class type. */
15697 if (complain & tf_error)
15698 error ("creating pointer to member of non-class type %qT", r);
15699 return error_mark_node;
15700 }
15701 if (TYPE_REF_P (type))
15702 {
15703 if (complain & tf_error)
15704 error ("creating pointer to member reference type %qT", type);
15705 return error_mark_node;
15706 }
15707 if (VOID_TYPE_P (type))
15708 {
15709 if (complain & tf_error)
15710 error ("creating pointer to member of type void");
15711 return error_mark_node;
15712 }
15713 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15714 if (TREE_CODE (type) == FUNCTION_TYPE)
15715 {
15716 /* The type of the implicit object parameter gets its
15717 cv-qualifiers from the FUNCTION_TYPE. */
15718 tree memptr;
15719 tree method_type
15720 = build_memfn_type (type, r, type_memfn_quals (type),
15721 type_memfn_rqual (type));
15722 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15723 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15724 complain);
15725 }
15726 else
15727 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15728 cp_type_quals (t),
15729 complain);
15730 }
15731 case FUNCTION_TYPE:
15732 case METHOD_TYPE:
15733 {
15734 tree fntype;
15735 tree specs;
15736 fntype = tsubst_function_type (t, args, complain, in_decl);
15737 if (fntype == error_mark_node)
15738 return error_mark_node;
15739
15740 /* Substitute the exception specification. */
15741 specs = tsubst_exception_specification (t, args, complain, in_decl,
15742 /*defer_ok*/fndecl_type);
15743 if (specs == error_mark_node)
15744 return error_mark_node;
15745 if (specs)
15746 fntype = build_exception_variant (fntype, specs);
15747 return fntype;
15748 }
15749 case ARRAY_TYPE:
15750 {
15751 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15752 if (domain == error_mark_node)
15753 return error_mark_node;
15754
15755 /* As an optimization, we avoid regenerating the array type if
15756 it will obviously be the same as T. */
15757 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15758 return t;
15759
15760 /* These checks should match the ones in create_array_type_for_decl.
15761
15762 [temp.deduct]
15763
15764 The deduction may fail for any of the following reasons:
15765
15766 -- Attempting to create an array with an element type that
15767 is void, a function type, or a reference type, or [DR337]
15768 an abstract class type. */
15769 if (VOID_TYPE_P (type)
15770 || TREE_CODE (type) == FUNCTION_TYPE
15771 || (TREE_CODE (type) == ARRAY_TYPE
15772 && TYPE_DOMAIN (type) == NULL_TREE)
15773 || TYPE_REF_P (type))
15774 {
15775 if (complain & tf_error)
15776 error ("creating array of %qT", type);
15777 return error_mark_node;
15778 }
15779
15780 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15781 return error_mark_node;
15782
15783 r = build_cplus_array_type (type, domain);
15784
15785 if (!valid_array_size_p (input_location, r, in_decl,
15786 (complain & tf_error)))
15787 return error_mark_node;
15788
15789 if (TYPE_USER_ALIGN (t))
15790 {
15791 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15792 TYPE_USER_ALIGN (r) = 1;
15793 }
15794
15795 return r;
15796 }
15797
15798 case TYPENAME_TYPE:
15799 {
15800 tree ctx = TYPE_CONTEXT (t);
15801 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15802 {
15803 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15804 if (ctx == error_mark_node
15805 || TREE_VEC_LENGTH (ctx) > 1)
15806 return error_mark_node;
15807 if (TREE_VEC_LENGTH (ctx) == 0)
15808 {
15809 if (complain & tf_error)
15810 error ("%qD is instantiated for an empty pack",
15811 TYPENAME_TYPE_FULLNAME (t));
15812 return error_mark_node;
15813 }
15814 ctx = TREE_VEC_ELT (ctx, 0);
15815 }
15816 else
15817 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15818 /*entering_scope=*/1);
15819 if (ctx == error_mark_node)
15820 return error_mark_node;
15821
15822 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15823 complain, in_decl);
15824 if (f == error_mark_node)
15825 return error_mark_node;
15826
15827 if (!MAYBE_CLASS_TYPE_P (ctx))
15828 {
15829 if (complain & tf_error)
15830 error ("%qT is not a class, struct, or union type", ctx);
15831 return error_mark_node;
15832 }
15833 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15834 {
15835 /* Normally, make_typename_type does not require that the CTX
15836 have complete type in order to allow things like:
15837
15838 template <class T> struct S { typename S<T>::X Y; };
15839
15840 But, such constructs have already been resolved by this
15841 point, so here CTX really should have complete type, unless
15842 it's a partial instantiation. */
15843 ctx = complete_type (ctx);
15844 if (!COMPLETE_TYPE_P (ctx))
15845 {
15846 if (complain & tf_error)
15847 cxx_incomplete_type_error (NULL_TREE, ctx);
15848 return error_mark_node;
15849 }
15850 }
15851
15852 f = make_typename_type (ctx, f, typename_type,
15853 complain | tf_keep_type_decl);
15854 if (f == error_mark_node)
15855 return f;
15856 if (TREE_CODE (f) == TYPE_DECL)
15857 {
15858 complain |= tf_ignore_bad_quals;
15859 f = TREE_TYPE (f);
15860 }
15861
15862 if (TREE_CODE (f) != TYPENAME_TYPE)
15863 {
15864 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15865 {
15866 if (complain & tf_error)
15867 error ("%qT resolves to %qT, which is not an enumeration type",
15868 t, f);
15869 else
15870 return error_mark_node;
15871 }
15872 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15873 {
15874 if (complain & tf_error)
15875 error ("%qT resolves to %qT, which is not a class type",
15876 t, f);
15877 else
15878 return error_mark_node;
15879 }
15880 }
15881
15882 return cp_build_qualified_type_real
15883 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15884 }
15885
15886 case UNBOUND_CLASS_TEMPLATE:
15887 {
15888 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15889 in_decl, /*entering_scope=*/1);
15890 tree name = TYPE_IDENTIFIER (t);
15891 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15892
15893 if (ctx == error_mark_node || name == error_mark_node)
15894 return error_mark_node;
15895
15896 if (parm_list)
15897 parm_list = tsubst_template_parms (parm_list, args, complain);
15898 return make_unbound_class_template (ctx, name, parm_list, complain);
15899 }
15900
15901 case TYPEOF_TYPE:
15902 {
15903 tree type;
15904
15905 ++cp_unevaluated_operand;
15906 ++c_inhibit_evaluation_warnings;
15907
15908 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15909 complain, in_decl,
15910 /*integral_constant_expression_p=*/false);
15911
15912 --cp_unevaluated_operand;
15913 --c_inhibit_evaluation_warnings;
15914
15915 type = finish_typeof (type);
15916 return cp_build_qualified_type_real (type,
15917 cp_type_quals (t)
15918 | cp_type_quals (type),
15919 complain);
15920 }
15921
15922 case DECLTYPE_TYPE:
15923 {
15924 tree type;
15925
15926 ++cp_unevaluated_operand;
15927 ++c_inhibit_evaluation_warnings;
15928
15929 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15930 complain|tf_decltype, in_decl,
15931 /*function_p*/false,
15932 /*integral_constant_expression*/false);
15933
15934 --cp_unevaluated_operand;
15935 --c_inhibit_evaluation_warnings;
15936
15937 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15938 type = lambda_capture_field_type (type,
15939 false /*explicit_init*/,
15940 DECLTYPE_FOR_REF_CAPTURE (t));
15941 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15942 type = lambda_proxy_type (type);
15943 else
15944 {
15945 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15946 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15947 && EXPR_P (type))
15948 /* In a template ~id could be either a complement expression
15949 or an unqualified-id naming a destructor; if instantiating
15950 it produces an expression, it's not an id-expression or
15951 member access. */
15952 id = false;
15953 type = finish_decltype_type (type, id, complain);
15954 }
15955 return cp_build_qualified_type_real (type,
15956 cp_type_quals (t)
15957 | cp_type_quals (type),
15958 complain | tf_ignore_bad_quals);
15959 }
15960
15961 case UNDERLYING_TYPE:
15962 {
15963 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15964 complain, in_decl);
15965 return finish_underlying_type (type);
15966 }
15967
15968 case TYPE_ARGUMENT_PACK:
15969 case NONTYPE_ARGUMENT_PACK:
15970 {
15971 tree r;
15972
15973 if (code == NONTYPE_ARGUMENT_PACK)
15974 r = make_node (code);
15975 else
15976 r = cxx_make_type (code);
15977
15978 tree pack_args = ARGUMENT_PACK_ARGS (t);
15979 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15980 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15981
15982 return r;
15983 }
15984
15985 case VOID_CST:
15986 case INTEGER_CST:
15987 case REAL_CST:
15988 case STRING_CST:
15989 case PLUS_EXPR:
15990 case MINUS_EXPR:
15991 case NEGATE_EXPR:
15992 case NOP_EXPR:
15993 case INDIRECT_REF:
15994 case ADDR_EXPR:
15995 case CALL_EXPR:
15996 case ARRAY_REF:
15997 case SCOPE_REF:
15998 /* We should use one of the expression tsubsts for these codes. */
15999 gcc_unreachable ();
16000
16001 default:
16002 sorry ("use of %qs in template", get_tree_code_name (code));
16003 return error_mark_node;
16004 }
16005 }
16006
16007 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16008 expression on the left-hand side of the "." or "->" operator. We
16009 only do the lookup if we had a dependent BASELINK. Otherwise we
16010 adjust it onto the instantiated heirarchy. */
16011
16012 static tree
16013 tsubst_baselink (tree baselink, tree object_type,
16014 tree args, tsubst_flags_t complain, tree in_decl)
16015 {
16016 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16017 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16018 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16019
16020 tree optype = BASELINK_OPTYPE (baselink);
16021 optype = tsubst (optype, args, complain, in_decl);
16022
16023 tree template_args = NULL_TREE;
16024 bool template_id_p = false;
16025 tree fns = BASELINK_FUNCTIONS (baselink);
16026 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16027 {
16028 template_id_p = true;
16029 template_args = TREE_OPERAND (fns, 1);
16030 fns = TREE_OPERAND (fns, 0);
16031 if (template_args)
16032 template_args = tsubst_template_args (template_args, args,
16033 complain, in_decl);
16034 }
16035
16036 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16037 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16038 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16039
16040 if (dependent_p)
16041 {
16042 tree name = OVL_NAME (fns);
16043 if (IDENTIFIER_CONV_OP_P (name))
16044 name = make_conv_op_name (optype);
16045
16046 if (name == complete_dtor_identifier)
16047 /* Treat as-if non-dependent below. */
16048 dependent_p = false;
16049
16050 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16051 complain);
16052 if (!baselink)
16053 {
16054 if ((complain & tf_error)
16055 && constructor_name_p (name, qualifying_scope))
16056 error ("cannot call constructor %<%T::%D%> directly",
16057 qualifying_scope, name);
16058 return error_mark_node;
16059 }
16060
16061 if (BASELINK_P (baselink))
16062 fns = BASELINK_FUNCTIONS (baselink);
16063 }
16064 else
16065 /* We're going to overwrite pieces below, make a duplicate. */
16066 baselink = copy_node (baselink);
16067
16068 /* If lookup found a single function, mark it as used at this point.
16069 (If lookup found multiple functions the one selected later by
16070 overload resolution will be marked as used at that point.) */
16071 if (!template_id_p && !really_overloaded_fn (fns))
16072 {
16073 tree fn = OVL_FIRST (fns);
16074 bool ok = mark_used (fn, complain);
16075 if (!ok && !(complain & tf_error))
16076 return error_mark_node;
16077 if (ok && BASELINK_P (baselink))
16078 /* We might have instantiated an auto function. */
16079 TREE_TYPE (baselink) = TREE_TYPE (fn);
16080 }
16081
16082 if (BASELINK_P (baselink))
16083 {
16084 /* Add back the template arguments, if present. */
16085 if (template_id_p)
16086 BASELINK_FUNCTIONS (baselink)
16087 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16088
16089 /* Update the conversion operator type. */
16090 BASELINK_OPTYPE (baselink) = optype;
16091 }
16092
16093 if (!object_type)
16094 object_type = current_class_type;
16095
16096 if (qualified_p || !dependent_p)
16097 {
16098 baselink = adjust_result_of_qualified_name_lookup (baselink,
16099 qualifying_scope,
16100 object_type);
16101 if (!qualified_p)
16102 /* We need to call adjust_result_of_qualified_name_lookup in case the
16103 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16104 so that we still get virtual function binding. */
16105 BASELINK_QUALIFIED_P (baselink) = false;
16106 }
16107
16108 return baselink;
16109 }
16110
16111 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16112 true if the qualified-id will be a postfix-expression in-and-of
16113 itself; false if more of the postfix-expression follows the
16114 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16115 of "&". */
16116
16117 static tree
16118 tsubst_qualified_id (tree qualified_id, tree args,
16119 tsubst_flags_t complain, tree in_decl,
16120 bool done, bool address_p)
16121 {
16122 tree expr;
16123 tree scope;
16124 tree name;
16125 bool is_template;
16126 tree template_args;
16127 location_t loc = UNKNOWN_LOCATION;
16128
16129 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16130
16131 /* Figure out what name to look up. */
16132 name = TREE_OPERAND (qualified_id, 1);
16133 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16134 {
16135 is_template = true;
16136 loc = EXPR_LOCATION (name);
16137 template_args = TREE_OPERAND (name, 1);
16138 if (template_args)
16139 template_args = tsubst_template_args (template_args, args,
16140 complain, in_decl);
16141 if (template_args == error_mark_node)
16142 return error_mark_node;
16143 name = TREE_OPERAND (name, 0);
16144 }
16145 else
16146 {
16147 is_template = false;
16148 template_args = NULL_TREE;
16149 }
16150
16151 /* Substitute into the qualifying scope. When there are no ARGS, we
16152 are just trying to simplify a non-dependent expression. In that
16153 case the qualifying scope may be dependent, and, in any case,
16154 substituting will not help. */
16155 scope = TREE_OPERAND (qualified_id, 0);
16156 if (args)
16157 {
16158 scope = tsubst (scope, args, complain, in_decl);
16159 expr = tsubst_copy (name, args, complain, in_decl);
16160 }
16161 else
16162 expr = name;
16163
16164 if (dependent_scope_p (scope))
16165 {
16166 if (is_template)
16167 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16168 tree r = build_qualified_name (NULL_TREE, scope, expr,
16169 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16170 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16171 return r;
16172 }
16173
16174 if (!BASELINK_P (name) && !DECL_P (expr))
16175 {
16176 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16177 {
16178 /* A BIT_NOT_EXPR is used to represent a destructor. */
16179 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16180 {
16181 error ("qualifying type %qT does not match destructor name ~%qT",
16182 scope, TREE_OPERAND (expr, 0));
16183 expr = error_mark_node;
16184 }
16185 else
16186 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16187 /*is_type_p=*/0, false);
16188 }
16189 else
16190 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
16191 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16192 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16193 {
16194 if (complain & tf_error)
16195 {
16196 error ("dependent-name %qE is parsed as a non-type, but "
16197 "instantiation yields a type", qualified_id);
16198 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16199 }
16200 return error_mark_node;
16201 }
16202 }
16203
16204 if (DECL_P (expr))
16205 {
16206 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16207 scope, complain))
16208 return error_mark_node;
16209 /* Remember that there was a reference to this entity. */
16210 if (!mark_used (expr, complain) && !(complain & tf_error))
16211 return error_mark_node;
16212 }
16213
16214 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16215 {
16216 if (complain & tf_error)
16217 qualified_name_lookup_error (scope,
16218 TREE_OPERAND (qualified_id, 1),
16219 expr, input_location);
16220 return error_mark_node;
16221 }
16222
16223 if (is_template)
16224 {
16225 /* We may be repeating a check already done during parsing, but
16226 if it was well-formed and passed then, it will pass again
16227 now, and if it didn't, we wouldn't have got here. The case
16228 we want to catch is when we couldn't tell then, and can now,
16229 namely when templ prior to substitution was an
16230 identifier. */
16231 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16232 return error_mark_node;
16233
16234 if (variable_template_p (expr))
16235 expr = lookup_and_finish_template_variable (expr, template_args,
16236 complain);
16237 else
16238 expr = lookup_template_function (expr, template_args);
16239 }
16240
16241 if (expr == error_mark_node && complain & tf_error)
16242 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16243 expr, input_location);
16244 else if (TYPE_P (scope))
16245 {
16246 expr = (adjust_result_of_qualified_name_lookup
16247 (expr, scope, current_nonlambda_class_type ()));
16248 expr = (finish_qualified_id_expr
16249 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16250 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16251 /*template_arg_p=*/false, complain));
16252 }
16253
16254 /* Expressions do not generally have reference type. */
16255 if (TREE_CODE (expr) != SCOPE_REF
16256 /* However, if we're about to form a pointer-to-member, we just
16257 want the referenced member referenced. */
16258 && TREE_CODE (expr) != OFFSET_REF)
16259 expr = convert_from_reference (expr);
16260
16261 if (REF_PARENTHESIZED_P (qualified_id))
16262 expr = force_paren_expr (expr);
16263
16264 return expr;
16265 }
16266
16267 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16268 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16269 for tsubst. */
16270
16271 static tree
16272 tsubst_init (tree init, tree decl, tree args,
16273 tsubst_flags_t complain, tree in_decl)
16274 {
16275 if (!init)
16276 return NULL_TREE;
16277
16278 init = tsubst_expr (init, args, complain, in_decl, false);
16279
16280 tree type = TREE_TYPE (decl);
16281
16282 if (!init && type != error_mark_node)
16283 {
16284 if (tree auto_node = type_uses_auto (type))
16285 {
16286 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16287 {
16288 if (complain & tf_error)
16289 error ("initializer for %q#D expands to an empty list "
16290 "of expressions", decl);
16291 return error_mark_node;
16292 }
16293 }
16294 else if (!dependent_type_p (type))
16295 {
16296 /* If we had an initializer but it
16297 instantiated to nothing,
16298 value-initialize the object. This will
16299 only occur when the initializer was a
16300 pack expansion where the parameter packs
16301 used in that expansion were of length
16302 zero. */
16303 init = build_value_init (type, complain);
16304 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16305 init = get_target_expr_sfinae (init, complain);
16306 if (TREE_CODE (init) == TARGET_EXPR)
16307 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16308 }
16309 }
16310
16311 return init;
16312 }
16313
16314 /* Like tsubst, but deals with expressions. This function just replaces
16315 template parms; to finish processing the resultant expression, use
16316 tsubst_copy_and_build or tsubst_expr. */
16317
16318 static tree
16319 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16320 {
16321 enum tree_code code;
16322 tree r;
16323
16324 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16325 return t;
16326
16327 code = TREE_CODE (t);
16328
16329 switch (code)
16330 {
16331 case PARM_DECL:
16332 r = retrieve_local_specialization (t);
16333
16334 if (r == NULL_TREE)
16335 {
16336 /* We get here for a use of 'this' in an NSDMI. */
16337 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16338 return current_class_ptr;
16339
16340 /* This can happen for a parameter name used later in a function
16341 declaration (such as in a late-specified return type). Just
16342 make a dummy decl, since it's only used for its type. */
16343 gcc_assert (cp_unevaluated_operand != 0);
16344 r = tsubst_decl (t, args, complain);
16345 /* Give it the template pattern as its context; its true context
16346 hasn't been instantiated yet and this is good enough for
16347 mangling. */
16348 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16349 }
16350
16351 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16352 r = argument_pack_select_arg (r);
16353 if (!mark_used (r, complain) && !(complain & tf_error))
16354 return error_mark_node;
16355 return r;
16356
16357 case CONST_DECL:
16358 {
16359 tree enum_type;
16360 tree v;
16361
16362 if (DECL_TEMPLATE_PARM_P (t))
16363 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16364 /* There is no need to substitute into namespace-scope
16365 enumerators. */
16366 if (DECL_NAMESPACE_SCOPE_P (t))
16367 return t;
16368 /* If ARGS is NULL, then T is known to be non-dependent. */
16369 if (args == NULL_TREE)
16370 return scalar_constant_value (t);
16371
16372 /* Unfortunately, we cannot just call lookup_name here.
16373 Consider:
16374
16375 template <int I> int f() {
16376 enum E { a = I };
16377 struct S { void g() { E e = a; } };
16378 };
16379
16380 When we instantiate f<7>::S::g(), say, lookup_name is not
16381 clever enough to find f<7>::a. */
16382 enum_type
16383 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16384 /*entering_scope=*/0);
16385
16386 for (v = TYPE_VALUES (enum_type);
16387 v != NULL_TREE;
16388 v = TREE_CHAIN (v))
16389 if (TREE_PURPOSE (v) == DECL_NAME (t))
16390 return TREE_VALUE (v);
16391
16392 /* We didn't find the name. That should never happen; if
16393 name-lookup found it during preliminary parsing, we
16394 should find it again here during instantiation. */
16395 gcc_unreachable ();
16396 }
16397 return t;
16398
16399 case FIELD_DECL:
16400 if (DECL_CONTEXT (t))
16401 {
16402 tree ctx;
16403
16404 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16405 /*entering_scope=*/1);
16406 if (ctx != DECL_CONTEXT (t))
16407 {
16408 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16409 if (!r)
16410 {
16411 if (complain & tf_error)
16412 error ("using invalid field %qD", t);
16413 return error_mark_node;
16414 }
16415 return r;
16416 }
16417 }
16418
16419 return t;
16420
16421 case VAR_DECL:
16422 case FUNCTION_DECL:
16423 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16424 r = tsubst (t, args, complain, in_decl);
16425 else if (local_variable_p (t)
16426 && uses_template_parms (DECL_CONTEXT (t)))
16427 {
16428 r = retrieve_local_specialization (t);
16429 if (r == NULL_TREE)
16430 {
16431 /* First try name lookup to find the instantiation. */
16432 r = lookup_name (DECL_NAME (t));
16433 if (r)
16434 {
16435 if (!VAR_P (r))
16436 {
16437 /* During error-recovery we may find a non-variable,
16438 even an OVERLOAD: just bail out and avoid ICEs and
16439 duplicate diagnostics (c++/62207). */
16440 gcc_assert (seen_error ());
16441 return error_mark_node;
16442 }
16443 if (!is_capture_proxy (r))
16444 {
16445 /* Make sure the one we found is the one we want. */
16446 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16447 if (ctx != DECL_CONTEXT (r))
16448 r = NULL_TREE;
16449 }
16450 }
16451
16452 if (r)
16453 /* OK */;
16454 else
16455 {
16456 /* This can happen for a variable used in a
16457 late-specified return type of a local lambda, or for a
16458 local static or constant. Building a new VAR_DECL
16459 should be OK in all those cases. */
16460 r = tsubst_decl (t, args, complain);
16461 if (local_specializations)
16462 /* Avoid infinite recursion (79640). */
16463 register_local_specialization (r, t);
16464 if (decl_maybe_constant_var_p (r))
16465 {
16466 /* We can't call cp_finish_decl, so handle the
16467 initializer by hand. */
16468 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16469 complain, in_decl);
16470 if (!processing_template_decl)
16471 init = maybe_constant_init (init);
16472 if (processing_template_decl
16473 ? potential_constant_expression (init)
16474 : reduced_constant_expression_p (init))
16475 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16476 = TREE_CONSTANT (r) = true;
16477 DECL_INITIAL (r) = init;
16478 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16479 TREE_TYPE (r)
16480 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16481 complain, adc_variable_type);
16482 }
16483 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16484 || decl_constant_var_p (r)
16485 || seen_error ());
16486 if (!processing_template_decl
16487 && !TREE_STATIC (r))
16488 r = process_outer_var_ref (r, complain);
16489 }
16490 /* Remember this for subsequent uses. */
16491 if (local_specializations)
16492 register_local_specialization (r, t);
16493 }
16494 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16495 r = argument_pack_select_arg (r);
16496 }
16497 else
16498 r = t;
16499 if (!mark_used (r, complain))
16500 return error_mark_node;
16501 return r;
16502
16503 case NAMESPACE_DECL:
16504 return t;
16505
16506 case OVERLOAD:
16507 return t;
16508
16509 case BASELINK:
16510 return tsubst_baselink (t, current_nonlambda_class_type (),
16511 args, complain, in_decl);
16512
16513 case TEMPLATE_DECL:
16514 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16515 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16516 args, complain, in_decl);
16517 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16518 return tsubst (t, args, complain, in_decl);
16519 else if (DECL_CLASS_SCOPE_P (t)
16520 && uses_template_parms (DECL_CONTEXT (t)))
16521 {
16522 /* Template template argument like the following example need
16523 special treatment:
16524
16525 template <template <class> class TT> struct C {};
16526 template <class T> struct D {
16527 template <class U> struct E {};
16528 C<E> c; // #1
16529 };
16530 D<int> d; // #2
16531
16532 We are processing the template argument `E' in #1 for
16533 the template instantiation #2. Originally, `E' is a
16534 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16535 have to substitute this with one having context `D<int>'. */
16536
16537 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16538 if (dependent_scope_p (context))
16539 {
16540 /* When rewriting a constructor into a deduction guide, a
16541 non-dependent name can become dependent, so memtmpl<args>
16542 becomes context::template memtmpl<args>. */
16543 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16544 return build_qualified_name (type, context, DECL_NAME (t),
16545 /*template*/true);
16546 }
16547 return lookup_field (context, DECL_NAME(t), 0, false);
16548 }
16549 else
16550 /* Ordinary template template argument. */
16551 return t;
16552
16553 case NON_LVALUE_EXPR:
16554 case VIEW_CONVERT_EXPR:
16555 {
16556 /* Handle location wrappers by substituting the wrapped node
16557 first, *then* reusing the resulting type. Doing the type
16558 first ensures that we handle template parameters and
16559 parameter pack expansions. */
16560 if (location_wrapper_p (t))
16561 {
16562 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16563 complain, in_decl);
16564 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16565 }
16566 tree op = TREE_OPERAND (t, 0);
16567 if (code == VIEW_CONVERT_EXPR
16568 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16569 {
16570 /* Wrapper to make a C++20 template parameter object const. */
16571 op = tsubst_copy (op, args, complain, in_decl);
16572 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16573 {
16574 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16575 return build1 (code, type, op);
16576 }
16577 else
16578 {
16579 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16580 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16581 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16582 return op;
16583 }
16584 }
16585 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16586 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16587 {
16588 op = tsubst_copy (op, args, complain, in_decl);
16589 op = build1 (code, TREE_TYPE (op), op);
16590 REF_PARENTHESIZED_P (op) = true;
16591 return op;
16592 }
16593 /* We shouldn't see any other uses of these in templates. */
16594 gcc_unreachable ();
16595 }
16596
16597 case CAST_EXPR:
16598 case REINTERPRET_CAST_EXPR:
16599 case CONST_CAST_EXPR:
16600 case STATIC_CAST_EXPR:
16601 case DYNAMIC_CAST_EXPR:
16602 case IMPLICIT_CONV_EXPR:
16603 case CONVERT_EXPR:
16604 case NOP_EXPR:
16605 {
16606 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16607 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16608 return build1 (code, type, op0);
16609 }
16610
16611 case SIZEOF_EXPR:
16612 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16613 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16614 {
16615 tree expanded, op = TREE_OPERAND (t, 0);
16616 int len = 0;
16617
16618 if (SIZEOF_EXPR_TYPE_P (t))
16619 op = TREE_TYPE (op);
16620
16621 ++cp_unevaluated_operand;
16622 ++c_inhibit_evaluation_warnings;
16623 /* We only want to compute the number of arguments. */
16624 if (PACK_EXPANSION_P (op))
16625 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16626 else
16627 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16628 args, complain, in_decl);
16629 --cp_unevaluated_operand;
16630 --c_inhibit_evaluation_warnings;
16631
16632 if (TREE_CODE (expanded) == TREE_VEC)
16633 {
16634 len = TREE_VEC_LENGTH (expanded);
16635 /* Set TREE_USED for the benefit of -Wunused. */
16636 for (int i = 0; i < len; i++)
16637 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16638 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16639 }
16640
16641 if (expanded == error_mark_node)
16642 return error_mark_node;
16643 else if (PACK_EXPANSION_P (expanded)
16644 || (TREE_CODE (expanded) == TREE_VEC
16645 && pack_expansion_args_count (expanded)))
16646
16647 {
16648 if (PACK_EXPANSION_P (expanded))
16649 /* OK. */;
16650 else if (TREE_VEC_LENGTH (expanded) == 1)
16651 expanded = TREE_VEC_ELT (expanded, 0);
16652 else
16653 expanded = make_argument_pack (expanded);
16654
16655 if (TYPE_P (expanded))
16656 return cxx_sizeof_or_alignof_type (input_location,
16657 expanded, SIZEOF_EXPR,
16658 false,
16659 complain & tf_error);
16660 else
16661 return cxx_sizeof_or_alignof_expr (input_location,
16662 expanded, SIZEOF_EXPR,
16663 complain & tf_error);
16664 }
16665 else
16666 return build_int_cst (size_type_node, len);
16667 }
16668 if (SIZEOF_EXPR_TYPE_P (t))
16669 {
16670 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16671 args, complain, in_decl);
16672 r = build1 (NOP_EXPR, r, error_mark_node);
16673 r = build1 (SIZEOF_EXPR,
16674 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16675 SIZEOF_EXPR_TYPE_P (r) = 1;
16676 return r;
16677 }
16678 /* Fall through */
16679
16680 case INDIRECT_REF:
16681 case NEGATE_EXPR:
16682 case TRUTH_NOT_EXPR:
16683 case BIT_NOT_EXPR:
16684 case ADDR_EXPR:
16685 case UNARY_PLUS_EXPR: /* Unary + */
16686 case ALIGNOF_EXPR:
16687 case AT_ENCODE_EXPR:
16688 case ARROW_EXPR:
16689 case THROW_EXPR:
16690 case TYPEID_EXPR:
16691 case REALPART_EXPR:
16692 case IMAGPART_EXPR:
16693 case PAREN_EXPR:
16694 {
16695 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16696 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16697 r = build1 (code, type, op0);
16698 if (code == ALIGNOF_EXPR)
16699 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16700 return r;
16701 }
16702
16703 case COMPONENT_REF:
16704 {
16705 tree object;
16706 tree name;
16707
16708 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16709 name = TREE_OPERAND (t, 1);
16710 if (TREE_CODE (name) == BIT_NOT_EXPR)
16711 {
16712 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16713 complain, in_decl);
16714 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16715 }
16716 else if (TREE_CODE (name) == SCOPE_REF
16717 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16718 {
16719 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16720 complain, in_decl);
16721 name = TREE_OPERAND (name, 1);
16722 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16723 complain, in_decl);
16724 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16725 name = build_qualified_name (/*type=*/NULL_TREE,
16726 base, name,
16727 /*template_p=*/false);
16728 }
16729 else if (BASELINK_P (name))
16730 name = tsubst_baselink (name,
16731 non_reference (TREE_TYPE (object)),
16732 args, complain,
16733 in_decl);
16734 else
16735 name = tsubst_copy (name, args, complain, in_decl);
16736 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16737 }
16738
16739 case PLUS_EXPR:
16740 case MINUS_EXPR:
16741 case MULT_EXPR:
16742 case TRUNC_DIV_EXPR:
16743 case CEIL_DIV_EXPR:
16744 case FLOOR_DIV_EXPR:
16745 case ROUND_DIV_EXPR:
16746 case EXACT_DIV_EXPR:
16747 case BIT_AND_EXPR:
16748 case BIT_IOR_EXPR:
16749 case BIT_XOR_EXPR:
16750 case TRUNC_MOD_EXPR:
16751 case FLOOR_MOD_EXPR:
16752 case TRUTH_ANDIF_EXPR:
16753 case TRUTH_ORIF_EXPR:
16754 case TRUTH_AND_EXPR:
16755 case TRUTH_OR_EXPR:
16756 case RSHIFT_EXPR:
16757 case LSHIFT_EXPR:
16758 case EQ_EXPR:
16759 case NE_EXPR:
16760 case MAX_EXPR:
16761 case MIN_EXPR:
16762 case LE_EXPR:
16763 case GE_EXPR:
16764 case LT_EXPR:
16765 case GT_EXPR:
16766 case COMPOUND_EXPR:
16767 case DOTSTAR_EXPR:
16768 case MEMBER_REF:
16769 case PREDECREMENT_EXPR:
16770 case PREINCREMENT_EXPR:
16771 case POSTDECREMENT_EXPR:
16772 case POSTINCREMENT_EXPR:
16773 {
16774 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16775 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16776 return build_nt (code, op0, op1);
16777 }
16778
16779 case SCOPE_REF:
16780 {
16781 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16782 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16783 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16784 QUALIFIED_NAME_IS_TEMPLATE (t));
16785 }
16786
16787 case ARRAY_REF:
16788 {
16789 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16790 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16791 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16792 }
16793
16794 case CALL_EXPR:
16795 {
16796 int n = VL_EXP_OPERAND_LENGTH (t);
16797 tree result = build_vl_exp (CALL_EXPR, n);
16798 int i;
16799 for (i = 0; i < n; i++)
16800 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16801 complain, in_decl);
16802 return result;
16803 }
16804
16805 case COND_EXPR:
16806 case MODOP_EXPR:
16807 case PSEUDO_DTOR_EXPR:
16808 case VEC_PERM_EXPR:
16809 {
16810 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16811 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16812 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16813 r = build_nt (code, op0, op1, op2);
16814 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16815 return r;
16816 }
16817
16818 case NEW_EXPR:
16819 {
16820 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16821 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16822 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16823 r = build_nt (code, op0, op1, op2);
16824 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16825 return r;
16826 }
16827
16828 case DELETE_EXPR:
16829 {
16830 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16831 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16832 r = build_nt (code, op0, op1);
16833 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16834 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16835 return r;
16836 }
16837
16838 case TEMPLATE_ID_EXPR:
16839 {
16840 /* Substituted template arguments */
16841 tree fn = TREE_OPERAND (t, 0);
16842 tree targs = TREE_OPERAND (t, 1);
16843
16844 fn = tsubst_copy (fn, args, complain, in_decl);
16845 if (targs)
16846 targs = tsubst_template_args (targs, args, complain, in_decl);
16847
16848 return lookup_template_function (fn, targs);
16849 }
16850
16851 case TREE_LIST:
16852 {
16853 tree purpose, value, chain;
16854
16855 if (t == void_list_node)
16856 return t;
16857
16858 purpose = TREE_PURPOSE (t);
16859 if (purpose)
16860 purpose = tsubst_copy (purpose, args, complain, in_decl);
16861 value = TREE_VALUE (t);
16862 if (value)
16863 value = tsubst_copy (value, args, complain, in_decl);
16864 chain = TREE_CHAIN (t);
16865 if (chain && chain != void_type_node)
16866 chain = tsubst_copy (chain, args, complain, in_decl);
16867 if (purpose == TREE_PURPOSE (t)
16868 && value == TREE_VALUE (t)
16869 && chain == TREE_CHAIN (t))
16870 return t;
16871 return tree_cons (purpose, value, chain);
16872 }
16873
16874 case RECORD_TYPE:
16875 case UNION_TYPE:
16876 case ENUMERAL_TYPE:
16877 case INTEGER_TYPE:
16878 case TEMPLATE_TYPE_PARM:
16879 case TEMPLATE_TEMPLATE_PARM:
16880 case BOUND_TEMPLATE_TEMPLATE_PARM:
16881 case TEMPLATE_PARM_INDEX:
16882 case POINTER_TYPE:
16883 case REFERENCE_TYPE:
16884 case OFFSET_TYPE:
16885 case FUNCTION_TYPE:
16886 case METHOD_TYPE:
16887 case ARRAY_TYPE:
16888 case TYPENAME_TYPE:
16889 case UNBOUND_CLASS_TEMPLATE:
16890 case TYPEOF_TYPE:
16891 case DECLTYPE_TYPE:
16892 case TYPE_DECL:
16893 return tsubst (t, args, complain, in_decl);
16894
16895 case USING_DECL:
16896 t = DECL_NAME (t);
16897 /* Fall through. */
16898 case IDENTIFIER_NODE:
16899 if (IDENTIFIER_CONV_OP_P (t))
16900 {
16901 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16902 return make_conv_op_name (new_type);
16903 }
16904 else
16905 return t;
16906
16907 case CONSTRUCTOR:
16908 /* This is handled by tsubst_copy_and_build. */
16909 gcc_unreachable ();
16910
16911 case VA_ARG_EXPR:
16912 {
16913 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16914 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16915 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16916 }
16917
16918 case CLEANUP_POINT_EXPR:
16919 /* We shouldn't have built any of these during initial template
16920 generation. Instead, they should be built during instantiation
16921 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16922 gcc_unreachable ();
16923
16924 case OFFSET_REF:
16925 {
16926 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16927 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16928 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16929 r = build2 (code, type, op0, op1);
16930 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16931 if (!mark_used (TREE_OPERAND (r, 1), complain)
16932 && !(complain & tf_error))
16933 return error_mark_node;
16934 return r;
16935 }
16936
16937 case EXPR_PACK_EXPANSION:
16938 error ("invalid use of pack expansion expression");
16939 return error_mark_node;
16940
16941 case NONTYPE_ARGUMENT_PACK:
16942 error ("use %<...%> to expand argument pack");
16943 return error_mark_node;
16944
16945 case VOID_CST:
16946 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16947 return t;
16948
16949 case INTEGER_CST:
16950 case REAL_CST:
16951 case COMPLEX_CST:
16952 {
16953 /* Instantiate any typedefs in the type. */
16954 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16955 r = fold_convert (type, t);
16956 gcc_assert (TREE_CODE (r) == code);
16957 return r;
16958 }
16959
16960 case STRING_CST:
16961 {
16962 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16963 r = t;
16964 if (type != TREE_TYPE (t))
16965 {
16966 r = copy_node (t);
16967 TREE_TYPE (r) = type;
16968 }
16969 return r;
16970 }
16971
16972 case PTRMEM_CST:
16973 /* These can sometimes show up in a partial instantiation, but never
16974 involve template parms. */
16975 gcc_assert (!uses_template_parms (t));
16976 return t;
16977
16978 case UNARY_LEFT_FOLD_EXPR:
16979 return tsubst_unary_left_fold (t, args, complain, in_decl);
16980 case UNARY_RIGHT_FOLD_EXPR:
16981 return tsubst_unary_right_fold (t, args, complain, in_decl);
16982 case BINARY_LEFT_FOLD_EXPR:
16983 return tsubst_binary_left_fold (t, args, complain, in_decl);
16984 case BINARY_RIGHT_FOLD_EXPR:
16985 return tsubst_binary_right_fold (t, args, complain, in_decl);
16986 case PREDICT_EXPR:
16987 return t;
16988
16989 case DEBUG_BEGIN_STMT:
16990 /* ??? There's no point in copying it for now, but maybe some
16991 day it will contain more information, such as a pointer back
16992 to the containing function, inlined copy or so. */
16993 return t;
16994
16995 case CO_AWAIT_EXPR:
16996 return tsubst_expr (t, args, complain, in_decl,
16997 /*integral_constant_expression_p=*/false);
16998 break;
16999
17000 default:
17001 /* We shouldn't get here, but keep going if !flag_checking. */
17002 if (flag_checking)
17003 gcc_unreachable ();
17004 return t;
17005 }
17006 }
17007
17008 /* Helper function for tsubst_omp_clauses, used for instantiation of
17009 OMP_CLAUSE_DECL of clauses. */
17010
17011 static tree
17012 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17013 tree in_decl, tree *iterator_cache)
17014 {
17015 if (decl == NULL_TREE)
17016 return NULL_TREE;
17017
17018 /* Handle OpenMP iterators. */
17019 if (TREE_CODE (decl) == TREE_LIST
17020 && TREE_PURPOSE (decl)
17021 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17022 {
17023 tree ret;
17024 if (iterator_cache[0] == TREE_PURPOSE (decl))
17025 ret = iterator_cache[1];
17026 else
17027 {
17028 tree *tp = &ret;
17029 begin_scope (sk_omp, NULL);
17030 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17031 {
17032 *tp = copy_node (it);
17033 TREE_VEC_ELT (*tp, 0)
17034 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17035 TREE_VEC_ELT (*tp, 1)
17036 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17037 /*integral_constant_expression_p=*/false);
17038 TREE_VEC_ELT (*tp, 2)
17039 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17040 /*integral_constant_expression_p=*/false);
17041 TREE_VEC_ELT (*tp, 3)
17042 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17043 /*integral_constant_expression_p=*/false);
17044 TREE_CHAIN (*tp) = NULL_TREE;
17045 tp = &TREE_CHAIN (*tp);
17046 }
17047 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17048 iterator_cache[0] = TREE_PURPOSE (decl);
17049 iterator_cache[1] = ret;
17050 }
17051 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17052 args, complain,
17053 in_decl, NULL));
17054 }
17055
17056 /* Handle an OpenMP array section represented as a TREE_LIST (or
17057 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17058 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17059 TREE_LIST. We can handle it exactly the same as an array section
17060 (purpose, value, and a chain), even though the nomenclature
17061 (low_bound, length, etc) is different. */
17062 if (TREE_CODE (decl) == TREE_LIST)
17063 {
17064 tree low_bound
17065 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17066 /*integral_constant_expression_p=*/false);
17067 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17068 /*integral_constant_expression_p=*/false);
17069 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17070 in_decl, NULL);
17071 if (TREE_PURPOSE (decl) == low_bound
17072 && TREE_VALUE (decl) == length
17073 && TREE_CHAIN (decl) == chain)
17074 return decl;
17075 tree ret = tree_cons (low_bound, length, chain);
17076 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17077 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17078 return ret;
17079 }
17080 tree ret = tsubst_expr (decl, args, complain, in_decl,
17081 /*integral_constant_expression_p=*/false);
17082 /* Undo convert_from_reference tsubst_expr could have called. */
17083 if (decl
17084 && REFERENCE_REF_P (ret)
17085 && !REFERENCE_REF_P (decl))
17086 ret = TREE_OPERAND (ret, 0);
17087 return ret;
17088 }
17089
17090 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17091
17092 static tree
17093 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17094 tree args, tsubst_flags_t complain, tree in_decl)
17095 {
17096 tree new_clauses = NULL_TREE, nc, oc;
17097 tree linear_no_step = NULL_TREE;
17098 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17099
17100 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17101 {
17102 nc = copy_node (oc);
17103 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17104 new_clauses = nc;
17105
17106 switch (OMP_CLAUSE_CODE (nc))
17107 {
17108 case OMP_CLAUSE_LASTPRIVATE:
17109 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17110 {
17111 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17112 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17113 in_decl, /*integral_constant_expression_p=*/false);
17114 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17115 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17116 }
17117 /* FALLTHRU */
17118 case OMP_CLAUSE_PRIVATE:
17119 case OMP_CLAUSE_SHARED:
17120 case OMP_CLAUSE_FIRSTPRIVATE:
17121 case OMP_CLAUSE_COPYIN:
17122 case OMP_CLAUSE_COPYPRIVATE:
17123 case OMP_CLAUSE_UNIFORM:
17124 case OMP_CLAUSE_DEPEND:
17125 case OMP_CLAUSE_FROM:
17126 case OMP_CLAUSE_TO:
17127 case OMP_CLAUSE_MAP:
17128 case OMP_CLAUSE_NONTEMPORAL:
17129 case OMP_CLAUSE_USE_DEVICE_PTR:
17130 case OMP_CLAUSE_USE_DEVICE_ADDR:
17131 case OMP_CLAUSE_IS_DEVICE_PTR:
17132 case OMP_CLAUSE_INCLUSIVE:
17133 case OMP_CLAUSE_EXCLUSIVE:
17134 OMP_CLAUSE_DECL (nc)
17135 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17136 in_decl, iterator_cache);
17137 break;
17138 case OMP_CLAUSE_TILE:
17139 case OMP_CLAUSE_IF:
17140 case OMP_CLAUSE_NUM_THREADS:
17141 case OMP_CLAUSE_SCHEDULE:
17142 case OMP_CLAUSE_COLLAPSE:
17143 case OMP_CLAUSE_FINAL:
17144 case OMP_CLAUSE_DEVICE:
17145 case OMP_CLAUSE_DIST_SCHEDULE:
17146 case OMP_CLAUSE_NUM_TEAMS:
17147 case OMP_CLAUSE_THREAD_LIMIT:
17148 case OMP_CLAUSE_SAFELEN:
17149 case OMP_CLAUSE_SIMDLEN:
17150 case OMP_CLAUSE_NUM_TASKS:
17151 case OMP_CLAUSE_GRAINSIZE:
17152 case OMP_CLAUSE_PRIORITY:
17153 case OMP_CLAUSE_ORDERED:
17154 case OMP_CLAUSE_HINT:
17155 case OMP_CLAUSE_NUM_GANGS:
17156 case OMP_CLAUSE_NUM_WORKERS:
17157 case OMP_CLAUSE_VECTOR_LENGTH:
17158 case OMP_CLAUSE_WORKER:
17159 case OMP_CLAUSE_VECTOR:
17160 case OMP_CLAUSE_ASYNC:
17161 case OMP_CLAUSE_WAIT:
17162 OMP_CLAUSE_OPERAND (nc, 0)
17163 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17164 in_decl, /*integral_constant_expression_p=*/false);
17165 break;
17166 case OMP_CLAUSE_REDUCTION:
17167 case OMP_CLAUSE_IN_REDUCTION:
17168 case OMP_CLAUSE_TASK_REDUCTION:
17169 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17170 {
17171 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17172 if (TREE_CODE (placeholder) == SCOPE_REF)
17173 {
17174 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17175 complain, in_decl);
17176 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17177 = build_qualified_name (NULL_TREE, scope,
17178 TREE_OPERAND (placeholder, 1),
17179 false);
17180 }
17181 else
17182 gcc_assert (identifier_p (placeholder));
17183 }
17184 OMP_CLAUSE_DECL (nc)
17185 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17186 in_decl, NULL);
17187 break;
17188 case OMP_CLAUSE_GANG:
17189 case OMP_CLAUSE_ALIGNED:
17190 OMP_CLAUSE_DECL (nc)
17191 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17192 in_decl, NULL);
17193 OMP_CLAUSE_OPERAND (nc, 1)
17194 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17195 in_decl, /*integral_constant_expression_p=*/false);
17196 break;
17197 case OMP_CLAUSE_LINEAR:
17198 OMP_CLAUSE_DECL (nc)
17199 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17200 in_decl, NULL);
17201 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17202 {
17203 gcc_assert (!linear_no_step);
17204 linear_no_step = nc;
17205 }
17206 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17207 OMP_CLAUSE_LINEAR_STEP (nc)
17208 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17209 complain, in_decl, NULL);
17210 else
17211 OMP_CLAUSE_LINEAR_STEP (nc)
17212 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17213 in_decl,
17214 /*integral_constant_expression_p=*/false);
17215 break;
17216 case OMP_CLAUSE_NOWAIT:
17217 case OMP_CLAUSE_DEFAULT:
17218 case OMP_CLAUSE_UNTIED:
17219 case OMP_CLAUSE_MERGEABLE:
17220 case OMP_CLAUSE_INBRANCH:
17221 case OMP_CLAUSE_NOTINBRANCH:
17222 case OMP_CLAUSE_PROC_BIND:
17223 case OMP_CLAUSE_FOR:
17224 case OMP_CLAUSE_PARALLEL:
17225 case OMP_CLAUSE_SECTIONS:
17226 case OMP_CLAUSE_TASKGROUP:
17227 case OMP_CLAUSE_NOGROUP:
17228 case OMP_CLAUSE_THREADS:
17229 case OMP_CLAUSE_SIMD:
17230 case OMP_CLAUSE_DEFAULTMAP:
17231 case OMP_CLAUSE_ORDER:
17232 case OMP_CLAUSE_BIND:
17233 case OMP_CLAUSE_INDEPENDENT:
17234 case OMP_CLAUSE_AUTO:
17235 case OMP_CLAUSE_SEQ:
17236 case OMP_CLAUSE_IF_PRESENT:
17237 case OMP_CLAUSE_FINALIZE:
17238 break;
17239 default:
17240 gcc_unreachable ();
17241 }
17242 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17243 switch (OMP_CLAUSE_CODE (nc))
17244 {
17245 case OMP_CLAUSE_SHARED:
17246 case OMP_CLAUSE_PRIVATE:
17247 case OMP_CLAUSE_FIRSTPRIVATE:
17248 case OMP_CLAUSE_LASTPRIVATE:
17249 case OMP_CLAUSE_COPYPRIVATE:
17250 case OMP_CLAUSE_LINEAR:
17251 case OMP_CLAUSE_REDUCTION:
17252 case OMP_CLAUSE_IN_REDUCTION:
17253 case OMP_CLAUSE_TASK_REDUCTION:
17254 case OMP_CLAUSE_USE_DEVICE_PTR:
17255 case OMP_CLAUSE_USE_DEVICE_ADDR:
17256 case OMP_CLAUSE_IS_DEVICE_PTR:
17257 case OMP_CLAUSE_INCLUSIVE:
17258 case OMP_CLAUSE_EXCLUSIVE:
17259 /* tsubst_expr on SCOPE_REF results in returning
17260 finish_non_static_data_member result. Undo that here. */
17261 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17262 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17263 == IDENTIFIER_NODE))
17264 {
17265 tree t = OMP_CLAUSE_DECL (nc);
17266 tree v = t;
17267 while (v)
17268 switch (TREE_CODE (v))
17269 {
17270 case COMPONENT_REF:
17271 case MEM_REF:
17272 case INDIRECT_REF:
17273 CASE_CONVERT:
17274 case POINTER_PLUS_EXPR:
17275 v = TREE_OPERAND (v, 0);
17276 continue;
17277 case PARM_DECL:
17278 if (DECL_CONTEXT (v) == current_function_decl
17279 && DECL_ARTIFICIAL (v)
17280 && DECL_NAME (v) == this_identifier)
17281 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17282 /* FALLTHRU */
17283 default:
17284 v = NULL_TREE;
17285 break;
17286 }
17287 }
17288 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17289 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17290 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17291 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17292 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17293 {
17294 tree decl = OMP_CLAUSE_DECL (nc);
17295 if (VAR_P (decl))
17296 {
17297 retrofit_lang_decl (decl);
17298 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17299 }
17300 }
17301 break;
17302 default:
17303 break;
17304 }
17305 }
17306
17307 new_clauses = nreverse (new_clauses);
17308 if (ort != C_ORT_OMP_DECLARE_SIMD)
17309 {
17310 new_clauses = finish_omp_clauses (new_clauses, ort);
17311 if (linear_no_step)
17312 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17313 if (nc == linear_no_step)
17314 {
17315 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17316 break;
17317 }
17318 }
17319 return new_clauses;
17320 }
17321
17322 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17323
17324 static tree
17325 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17326 tree in_decl)
17327 {
17328 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17329
17330 tree purpose, value, chain;
17331
17332 if (t == NULL)
17333 return t;
17334
17335 if (TREE_CODE (t) != TREE_LIST)
17336 return tsubst_copy_and_build (t, args, complain, in_decl,
17337 /*function_p=*/false,
17338 /*integral_constant_expression_p=*/false);
17339
17340 if (t == void_list_node)
17341 return t;
17342
17343 purpose = TREE_PURPOSE (t);
17344 if (purpose)
17345 purpose = RECUR (purpose);
17346 value = TREE_VALUE (t);
17347 if (value)
17348 {
17349 if (TREE_CODE (value) != LABEL_DECL)
17350 value = RECUR (value);
17351 else
17352 {
17353 value = lookup_label (DECL_NAME (value));
17354 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17355 TREE_USED (value) = 1;
17356 }
17357 }
17358 chain = TREE_CHAIN (t);
17359 if (chain && chain != void_type_node)
17360 chain = RECUR (chain);
17361 return tree_cons (purpose, value, chain);
17362 #undef RECUR
17363 }
17364
17365 /* Used to temporarily communicate the list of #pragma omp parallel
17366 clauses to #pragma omp for instantiation if they are combined
17367 together. */
17368
17369 static tree *omp_parallel_combined_clauses;
17370
17371 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17372 tree *, unsigned int *);
17373
17374 /* Substitute one OMP_FOR iterator. */
17375
17376 static bool
17377 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17378 tree initv, tree condv, tree incrv, tree *clauses,
17379 tree args, tsubst_flags_t complain, tree in_decl,
17380 bool integral_constant_expression_p)
17381 {
17382 #define RECUR(NODE) \
17383 tsubst_expr ((NODE), args, complain, in_decl, \
17384 integral_constant_expression_p)
17385 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17386 bool ret = false;
17387
17388 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17389 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17390
17391 decl = TREE_OPERAND (init, 0);
17392 init = TREE_OPERAND (init, 1);
17393 tree decl_expr = NULL_TREE;
17394 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17395 if (range_for)
17396 {
17397 bool decomp = false;
17398 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17399 {
17400 tree v = DECL_VALUE_EXPR (decl);
17401 if (TREE_CODE (v) == ARRAY_REF
17402 && VAR_P (TREE_OPERAND (v, 0))
17403 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17404 {
17405 tree decomp_first = NULL_TREE;
17406 unsigned decomp_cnt = 0;
17407 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17408 maybe_push_decl (d);
17409 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17410 in_decl, &decomp_first, &decomp_cnt);
17411 decomp = true;
17412 if (d == error_mark_node)
17413 decl = error_mark_node;
17414 else
17415 for (unsigned int i = 0; i < decomp_cnt; i++)
17416 {
17417 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17418 {
17419 tree v = build_nt (ARRAY_REF, d,
17420 size_int (decomp_cnt - i - 1),
17421 NULL_TREE, NULL_TREE);
17422 SET_DECL_VALUE_EXPR (decomp_first, v);
17423 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17424 }
17425 fit_decomposition_lang_decl (decomp_first, d);
17426 decomp_first = DECL_CHAIN (decomp_first);
17427 }
17428 }
17429 }
17430 decl = tsubst_decl (decl, args, complain);
17431 if (!decomp)
17432 maybe_push_decl (decl);
17433 }
17434 else if (init && TREE_CODE (init) == DECL_EXPR)
17435 {
17436 /* We need to jump through some hoops to handle declarations in the
17437 init-statement, since we might need to handle auto deduction,
17438 but we need to keep control of initialization. */
17439 decl_expr = init;
17440 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17441 decl = tsubst_decl (decl, args, complain);
17442 }
17443 else
17444 {
17445 if (TREE_CODE (decl) == SCOPE_REF)
17446 {
17447 decl = RECUR (decl);
17448 if (TREE_CODE (decl) == COMPONENT_REF)
17449 {
17450 tree v = decl;
17451 while (v)
17452 switch (TREE_CODE (v))
17453 {
17454 case COMPONENT_REF:
17455 case MEM_REF:
17456 case INDIRECT_REF:
17457 CASE_CONVERT:
17458 case POINTER_PLUS_EXPR:
17459 v = TREE_OPERAND (v, 0);
17460 continue;
17461 case PARM_DECL:
17462 if (DECL_CONTEXT (v) == current_function_decl
17463 && DECL_ARTIFICIAL (v)
17464 && DECL_NAME (v) == this_identifier)
17465 {
17466 decl = TREE_OPERAND (decl, 1);
17467 decl = omp_privatize_field (decl, false);
17468 }
17469 /* FALLTHRU */
17470 default:
17471 v = NULL_TREE;
17472 break;
17473 }
17474 }
17475 }
17476 else
17477 decl = RECUR (decl);
17478 }
17479 if (init && TREE_CODE (init) == TREE_VEC)
17480 {
17481 init = copy_node (init);
17482 TREE_VEC_ELT (init, 0)
17483 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17484 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17485 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17486 }
17487 else
17488 init = RECUR (init);
17489
17490 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17491 {
17492 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17493 if (TREE_CODE (o) == TREE_LIST)
17494 TREE_VEC_ELT (orig_declv, i)
17495 = tree_cons (RECUR (TREE_PURPOSE (o)),
17496 RECUR (TREE_VALUE (o)),
17497 NULL_TREE);
17498 else
17499 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17500 }
17501
17502 if (range_for)
17503 {
17504 tree this_pre_body = NULL_TREE;
17505 tree orig_init = NULL_TREE;
17506 tree orig_decl = NULL_TREE;
17507 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17508 orig_init, cond, incr);
17509 if (orig_decl)
17510 {
17511 if (orig_declv == NULL_TREE)
17512 orig_declv = copy_node (declv);
17513 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17514 ret = true;
17515 }
17516 else if (orig_declv)
17517 TREE_VEC_ELT (orig_declv, i) = decl;
17518 }
17519
17520 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17521 if (!range_for && auto_node && init)
17522 TREE_TYPE (decl)
17523 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17524
17525 gcc_assert (!type_dependent_expression_p (decl));
17526
17527 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17528 {
17529 if (decl_expr)
17530 {
17531 /* Declare the variable, but don't let that initialize it. */
17532 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17533 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17534 RECUR (decl_expr);
17535 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17536 }
17537
17538 if (!range_for)
17539 {
17540 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17541 if (COMPARISON_CLASS_P (cond)
17542 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17543 {
17544 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17545 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17546 TREE_VEC_ELT (rhs, 0)
17547 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17548 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17549 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17550 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17551 lhs, rhs);
17552 }
17553 else
17554 cond = RECUR (cond);
17555 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17556 if (TREE_CODE (incr) == MODIFY_EXPR)
17557 {
17558 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17559 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17560 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17561 NOP_EXPR, rhs, complain);
17562 }
17563 else
17564 incr = RECUR (incr);
17565 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17566 TREE_VEC_ELT (orig_declv, i) = decl;
17567 }
17568 TREE_VEC_ELT (declv, i) = decl;
17569 TREE_VEC_ELT (initv, i) = init;
17570 TREE_VEC_ELT (condv, i) = cond;
17571 TREE_VEC_ELT (incrv, i) = incr;
17572 return ret;
17573 }
17574
17575 if (decl_expr)
17576 {
17577 /* Declare and initialize the variable. */
17578 RECUR (decl_expr);
17579 init = NULL_TREE;
17580 }
17581 else if (init)
17582 {
17583 tree *pc;
17584 int j;
17585 for (j = ((omp_parallel_combined_clauses == NULL
17586 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17587 {
17588 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17589 {
17590 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17591 && OMP_CLAUSE_DECL (*pc) == decl)
17592 break;
17593 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17594 && OMP_CLAUSE_DECL (*pc) == decl)
17595 {
17596 if (j)
17597 break;
17598 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17599 tree c = *pc;
17600 *pc = OMP_CLAUSE_CHAIN (c);
17601 OMP_CLAUSE_CHAIN (c) = *clauses;
17602 *clauses = c;
17603 }
17604 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17605 && OMP_CLAUSE_DECL (*pc) == decl)
17606 {
17607 error ("iteration variable %qD should not be firstprivate",
17608 decl);
17609 *pc = OMP_CLAUSE_CHAIN (*pc);
17610 }
17611 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17612 && OMP_CLAUSE_DECL (*pc) == decl)
17613 {
17614 error ("iteration variable %qD should not be reduction",
17615 decl);
17616 *pc = OMP_CLAUSE_CHAIN (*pc);
17617 }
17618 else
17619 pc = &OMP_CLAUSE_CHAIN (*pc);
17620 }
17621 if (*pc)
17622 break;
17623 }
17624 if (*pc == NULL_TREE)
17625 {
17626 tree c = build_omp_clause (input_location,
17627 TREE_CODE (t) == OMP_LOOP
17628 ? OMP_CLAUSE_LASTPRIVATE
17629 : OMP_CLAUSE_PRIVATE);
17630 OMP_CLAUSE_DECL (c) = decl;
17631 c = finish_omp_clauses (c, C_ORT_OMP);
17632 if (c)
17633 {
17634 OMP_CLAUSE_CHAIN (c) = *clauses;
17635 *clauses = c;
17636 }
17637 }
17638 }
17639 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17640 if (COMPARISON_CLASS_P (cond))
17641 {
17642 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17643 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17644 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17645 }
17646 else
17647 cond = RECUR (cond);
17648 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17649 switch (TREE_CODE (incr))
17650 {
17651 case PREINCREMENT_EXPR:
17652 case PREDECREMENT_EXPR:
17653 case POSTINCREMENT_EXPR:
17654 case POSTDECREMENT_EXPR:
17655 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17656 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17657 break;
17658 case MODIFY_EXPR:
17659 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17660 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17661 {
17662 tree rhs = TREE_OPERAND (incr, 1);
17663 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17664 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17665 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17666 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17667 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17668 rhs0, rhs1));
17669 }
17670 else
17671 incr = RECUR (incr);
17672 break;
17673 case MODOP_EXPR:
17674 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17675 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17676 {
17677 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17678 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17679 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17680 TREE_TYPE (decl), lhs,
17681 RECUR (TREE_OPERAND (incr, 2))));
17682 }
17683 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17684 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17685 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17686 {
17687 tree rhs = TREE_OPERAND (incr, 2);
17688 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17689 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17690 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17691 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17692 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17693 rhs0, rhs1));
17694 }
17695 else
17696 incr = RECUR (incr);
17697 break;
17698 default:
17699 incr = RECUR (incr);
17700 break;
17701 }
17702
17703 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17704 TREE_VEC_ELT (orig_declv, i) = decl;
17705 TREE_VEC_ELT (declv, i) = decl;
17706 TREE_VEC_ELT (initv, i) = init;
17707 TREE_VEC_ELT (condv, i) = cond;
17708 TREE_VEC_ELT (incrv, i) = incr;
17709 return false;
17710 #undef RECUR
17711 }
17712
17713 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17714 of OMP_TARGET's body. */
17715
17716 static tree
17717 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17718 {
17719 *walk_subtrees = 0;
17720 switch (TREE_CODE (*tp))
17721 {
17722 case OMP_TEAMS:
17723 return *tp;
17724 case BIND_EXPR:
17725 case STATEMENT_LIST:
17726 *walk_subtrees = 1;
17727 break;
17728 default:
17729 break;
17730 }
17731 return NULL_TREE;
17732 }
17733
17734 /* Helper function for tsubst_expr. For decomposition declaration
17735 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17736 also the corresponding decls representing the identifiers
17737 of the decomposition declaration. Return DECL if successful
17738 or error_mark_node otherwise, set *FIRST to the first decl
17739 in the list chained through DECL_CHAIN and *CNT to the number
17740 of such decls. */
17741
17742 static tree
17743 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17744 tsubst_flags_t complain, tree in_decl, tree *first,
17745 unsigned int *cnt)
17746 {
17747 tree decl2, decl3, prev = decl;
17748 *cnt = 0;
17749 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17750 for (decl2 = DECL_CHAIN (pattern_decl);
17751 decl2
17752 && VAR_P (decl2)
17753 && DECL_DECOMPOSITION_P (decl2)
17754 && DECL_NAME (decl2);
17755 decl2 = DECL_CHAIN (decl2))
17756 {
17757 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17758 {
17759 gcc_assert (errorcount);
17760 return error_mark_node;
17761 }
17762 (*cnt)++;
17763 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17764 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17765 tree v = DECL_VALUE_EXPR (decl2);
17766 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17767 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17768 decl3 = tsubst (decl2, args, complain, in_decl);
17769 SET_DECL_VALUE_EXPR (decl2, v);
17770 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17771 if (VAR_P (decl3))
17772 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17773 else
17774 {
17775 gcc_assert (errorcount);
17776 decl = error_mark_node;
17777 continue;
17778 }
17779 maybe_push_decl (decl3);
17780 if (error_operand_p (decl3))
17781 decl = error_mark_node;
17782 else if (decl != error_mark_node
17783 && DECL_CHAIN (decl3) != prev
17784 && decl != prev)
17785 {
17786 gcc_assert (errorcount);
17787 decl = error_mark_node;
17788 }
17789 else
17790 prev = decl3;
17791 }
17792 *first = prev;
17793 return decl;
17794 }
17795
17796 /* Return the proper local_specialization for init-capture pack DECL. */
17797
17798 static tree
17799 lookup_init_capture_pack (tree decl)
17800 {
17801 /* We handle normal pack captures by forwarding to the specialization of the
17802 captured parameter. We can't do that for pack init-captures; we need them
17803 to have their own local_specialization. We created the individual
17804 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17805 when we process the DECL_EXPR for the pack init-capture in the template.
17806 So, how do we find them? We don't know the capture proxy pack when
17807 building the individual resulting proxies, and we don't know the
17808 individual proxies when instantiating the pack. What we have in common is
17809 the FIELD_DECL.
17810
17811 So...when we instantiate the FIELD_DECL, we stick the result in
17812 local_specializations. Then at the DECL_EXPR we look up that result, see
17813 how many elements it has, synthesize the names, and look them up. */
17814
17815 tree cname = DECL_NAME (decl);
17816 tree val = DECL_VALUE_EXPR (decl);
17817 tree field = TREE_OPERAND (val, 1);
17818 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17819 tree fpack = retrieve_local_specialization (field);
17820 if (fpack == error_mark_node)
17821 return error_mark_node;
17822
17823 int len = 1;
17824 tree vec = NULL_TREE;
17825 tree r = NULL_TREE;
17826 if (TREE_CODE (fpack) == TREE_VEC)
17827 {
17828 len = TREE_VEC_LENGTH (fpack);
17829 vec = make_tree_vec (len);
17830 r = make_node (NONTYPE_ARGUMENT_PACK);
17831 SET_ARGUMENT_PACK_ARGS (r, vec);
17832 }
17833 for (int i = 0; i < len; ++i)
17834 {
17835 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17836 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17837 if (vec)
17838 TREE_VEC_ELT (vec, i) = elt;
17839 else
17840 r = elt;
17841 }
17842 return r;
17843 }
17844
17845 /* Like tsubst_copy for expressions, etc. but also does semantic
17846 processing. */
17847
17848 tree
17849 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17850 bool integral_constant_expression_p)
17851 {
17852 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17853 #define RECUR(NODE) \
17854 tsubst_expr ((NODE), args, complain, in_decl, \
17855 integral_constant_expression_p)
17856
17857 tree stmt, tmp;
17858 tree r;
17859 location_t loc;
17860
17861 if (t == NULL_TREE || t == error_mark_node)
17862 return t;
17863
17864 loc = input_location;
17865 if (location_t eloc = cp_expr_location (t))
17866 input_location = eloc;
17867 if (STATEMENT_CODE_P (TREE_CODE (t)))
17868 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17869
17870 switch (TREE_CODE (t))
17871 {
17872 case STATEMENT_LIST:
17873 {
17874 tree_stmt_iterator i;
17875 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17876 RECUR (tsi_stmt (i));
17877 break;
17878 }
17879
17880 case CTOR_INITIALIZER:
17881 finish_mem_initializers (tsubst_initializer_list
17882 (TREE_OPERAND (t, 0), args));
17883 break;
17884
17885 case RETURN_EXPR:
17886 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17887 break;
17888
17889 case CO_RETURN_EXPR:
17890 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
17891 break;
17892
17893 case CO_YIELD_EXPR:
17894 stmt = finish_co_yield_expr (input_location,
17895 RECUR (TREE_OPERAND (t, 0)));
17896 RETURN (stmt);
17897 break;
17898
17899 case CO_AWAIT_EXPR:
17900 stmt = finish_co_await_expr (input_location,
17901 RECUR (TREE_OPERAND (t, 0)));
17902 RETURN (stmt);
17903 break;
17904
17905 case EXPR_STMT:
17906 tmp = RECUR (EXPR_STMT_EXPR (t));
17907 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17908 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17909 else
17910 finish_expr_stmt (tmp);
17911 break;
17912
17913 case USING_STMT:
17914 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17915 break;
17916
17917 case DECL_EXPR:
17918 {
17919 tree decl, pattern_decl;
17920 tree init;
17921
17922 pattern_decl = decl = DECL_EXPR_DECL (t);
17923 if (TREE_CODE (decl) == LABEL_DECL)
17924 finish_label_decl (DECL_NAME (decl));
17925 else if (TREE_CODE (decl) == USING_DECL)
17926 {
17927 tree scope = USING_DECL_SCOPE (decl);
17928 tree name = DECL_NAME (decl);
17929
17930 scope = tsubst (scope, args, complain, in_decl);
17931 finish_nonmember_using_decl (scope, name);
17932 }
17933 else if (is_capture_proxy (decl)
17934 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17935 {
17936 /* We're in tsubst_lambda_expr, we've already inserted a new
17937 capture proxy, so look it up and register it. */
17938 tree inst;
17939 if (!DECL_PACK_P (decl))
17940 {
17941 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17942 /*nonclass*/1, /*block_p=*/true,
17943 /*ns_only*/0, LOOKUP_HIDDEN);
17944 gcc_assert (inst != decl && is_capture_proxy (inst));
17945 }
17946 else if (is_normal_capture_proxy (decl))
17947 {
17948 inst = (retrieve_local_specialization
17949 (DECL_CAPTURED_VARIABLE (decl)));
17950 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
17951 || DECL_PACK_P (inst));
17952 }
17953 else
17954 inst = lookup_init_capture_pack (decl);
17955
17956 register_local_specialization (inst, decl);
17957 break;
17958 }
17959 else if (DECL_PRETTY_FUNCTION_P (decl))
17960 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17961 DECL_NAME (decl),
17962 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17963 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17964 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17965 /* Don't copy the old closure; we'll create a new one in
17966 tsubst_lambda_expr. */
17967 break;
17968 else
17969 {
17970 init = DECL_INITIAL (decl);
17971 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17972 for local variables, so save if DECL was declared constinit. */
17973 const bool constinit_p
17974 = (VAR_P (decl)
17975 && DECL_LANG_SPECIFIC (decl)
17976 && DECL_TEMPLATE_INFO (decl)
17977 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17978 decl = tsubst (decl, args, complain, in_decl);
17979 if (decl != error_mark_node)
17980 {
17981 /* By marking the declaration as instantiated, we avoid
17982 trying to instantiate it. Since instantiate_decl can't
17983 handle local variables, and since we've already done
17984 all that needs to be done, that's the right thing to
17985 do. */
17986 if (VAR_P (decl))
17987 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17988 if (VAR_P (decl) && !DECL_NAME (decl)
17989 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17990 /* Anonymous aggregates are a special case. */
17991 finish_anon_union (decl);
17992 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17993 {
17994 DECL_CONTEXT (decl) = current_function_decl;
17995 if (DECL_NAME (decl) == this_identifier)
17996 {
17997 tree lam = DECL_CONTEXT (current_function_decl);
17998 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17999 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18000 }
18001 insert_capture_proxy (decl);
18002 }
18003 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18004 /* We already did a pushtag. */;
18005 else if (TREE_CODE (decl) == FUNCTION_DECL
18006 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18007 && DECL_FUNCTION_SCOPE_P (pattern_decl))
18008 {
18009 DECL_CONTEXT (decl) = NULL_TREE;
18010 pushdecl (decl);
18011 DECL_CONTEXT (decl) = current_function_decl;
18012 cp_check_omp_declare_reduction (decl);
18013 }
18014 else
18015 {
18016 bool const_init = false;
18017 unsigned int cnt = 0;
18018 tree first = NULL_TREE, ndecl = error_mark_node;
18019 maybe_push_decl (decl);
18020
18021 if (VAR_P (decl)
18022 && DECL_DECOMPOSITION_P (decl)
18023 && TREE_TYPE (pattern_decl) != error_mark_node)
18024 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18025 complain, in_decl, &first,
18026 &cnt);
18027
18028 init = tsubst_init (init, decl, args, complain, in_decl);
18029
18030 if (VAR_P (decl))
18031 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18032 (pattern_decl));
18033
18034 if (ndecl != error_mark_node)
18035 cp_maybe_mangle_decomp (ndecl, first, cnt);
18036
18037 /* In a non-template function, VLA type declarations are
18038 handled in grokdeclarator; for templates, handle them
18039 now. */
18040 predeclare_vla (decl);
18041
18042 cp_finish_decl (decl, init, const_init, NULL_TREE,
18043 constinit_p ? LOOKUP_CONSTINIT : 0);
18044
18045 if (ndecl != error_mark_node)
18046 cp_finish_decomp (ndecl, first, cnt);
18047 }
18048 }
18049 }
18050
18051 break;
18052 }
18053
18054 case FOR_STMT:
18055 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18056 RECUR (FOR_INIT_STMT (t));
18057 finish_init_stmt (stmt);
18058 tmp = RECUR (FOR_COND (t));
18059 finish_for_cond (tmp, stmt, false, 0);
18060 tmp = RECUR (FOR_EXPR (t));
18061 finish_for_expr (tmp, stmt);
18062 {
18063 bool prev = note_iteration_stmt_body_start ();
18064 RECUR (FOR_BODY (t));
18065 note_iteration_stmt_body_end (prev);
18066 }
18067 finish_for_stmt (stmt);
18068 break;
18069
18070 case RANGE_FOR_STMT:
18071 {
18072 /* Construct another range_for, if this is not a final
18073 substitution (for inside a generic lambda of a
18074 template). Otherwise convert to a regular for. */
18075 tree decl, expr;
18076 stmt = (processing_template_decl
18077 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18078 : begin_for_stmt (NULL_TREE, NULL_TREE));
18079 RECUR (RANGE_FOR_INIT_STMT (t));
18080 decl = RANGE_FOR_DECL (t);
18081 decl = tsubst (decl, args, complain, in_decl);
18082 maybe_push_decl (decl);
18083 expr = RECUR (RANGE_FOR_EXPR (t));
18084
18085 tree decomp_first = NULL_TREE;
18086 unsigned decomp_cnt = 0;
18087 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18088 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18089 complain, in_decl,
18090 &decomp_first, &decomp_cnt);
18091
18092 if (processing_template_decl)
18093 {
18094 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18095 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18096 finish_range_for_decl (stmt, decl, expr);
18097 if (decomp_first && decl != error_mark_node)
18098 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18099 }
18100 else
18101 {
18102 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18103 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18104 stmt = cp_convert_range_for (stmt, decl, expr,
18105 decomp_first, decomp_cnt,
18106 RANGE_FOR_IVDEP (t), unroll);
18107 }
18108
18109 bool prev = note_iteration_stmt_body_start ();
18110 RECUR (RANGE_FOR_BODY (t));
18111 note_iteration_stmt_body_end (prev);
18112 finish_for_stmt (stmt);
18113 }
18114 break;
18115
18116 case WHILE_STMT:
18117 stmt = begin_while_stmt ();
18118 tmp = RECUR (WHILE_COND (t));
18119 finish_while_stmt_cond (tmp, stmt, false, 0);
18120 {
18121 bool prev = note_iteration_stmt_body_start ();
18122 RECUR (WHILE_BODY (t));
18123 note_iteration_stmt_body_end (prev);
18124 }
18125 finish_while_stmt (stmt);
18126 break;
18127
18128 case DO_STMT:
18129 stmt = begin_do_stmt ();
18130 {
18131 bool prev = note_iteration_stmt_body_start ();
18132 RECUR (DO_BODY (t));
18133 note_iteration_stmt_body_end (prev);
18134 }
18135 finish_do_body (stmt);
18136 tmp = RECUR (DO_COND (t));
18137 finish_do_stmt (tmp, stmt, false, 0);
18138 break;
18139
18140 case IF_STMT:
18141 stmt = begin_if_stmt ();
18142 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18143 if (IF_STMT_CONSTEXPR_P (t))
18144 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18145 tmp = RECUR (IF_COND (t));
18146 tmp = finish_if_stmt_cond (tmp, stmt);
18147 if (IF_STMT_CONSTEXPR_P (t)
18148 && instantiation_dependent_expression_p (tmp))
18149 {
18150 /* We're partially instantiating a generic lambda, but the condition
18151 of the constexpr if is still dependent. Don't substitute into the
18152 branches now, just remember the template arguments. */
18153 do_poplevel (IF_SCOPE (stmt));
18154 IF_COND (stmt) = IF_COND (t);
18155 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18156 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18157 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18158 add_stmt (stmt);
18159 break;
18160 }
18161 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18162 /* Don't instantiate the THEN_CLAUSE. */;
18163 else
18164 {
18165 tree folded = fold_non_dependent_expr (tmp, complain);
18166 bool inhibit = integer_zerop (folded);
18167 if (inhibit)
18168 ++c_inhibit_evaluation_warnings;
18169 RECUR (THEN_CLAUSE (t));
18170 if (inhibit)
18171 --c_inhibit_evaluation_warnings;
18172 }
18173 finish_then_clause (stmt);
18174
18175 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18176 /* Don't instantiate the ELSE_CLAUSE. */;
18177 else if (ELSE_CLAUSE (t))
18178 {
18179 tree folded = fold_non_dependent_expr (tmp, complain);
18180 bool inhibit = integer_nonzerop (folded);
18181 begin_else_clause (stmt);
18182 if (inhibit)
18183 ++c_inhibit_evaluation_warnings;
18184 RECUR (ELSE_CLAUSE (t));
18185 if (inhibit)
18186 --c_inhibit_evaluation_warnings;
18187 finish_else_clause (stmt);
18188 }
18189
18190 finish_if_stmt (stmt);
18191 break;
18192
18193 case BIND_EXPR:
18194 if (BIND_EXPR_BODY_BLOCK (t))
18195 stmt = begin_function_body ();
18196 else
18197 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18198 ? BCS_TRY_BLOCK : 0);
18199
18200 RECUR (BIND_EXPR_BODY (t));
18201
18202 if (BIND_EXPR_BODY_BLOCK (t))
18203 finish_function_body (stmt);
18204 else
18205 finish_compound_stmt (stmt);
18206 break;
18207
18208 case BREAK_STMT:
18209 finish_break_stmt ();
18210 break;
18211
18212 case CONTINUE_STMT:
18213 finish_continue_stmt ();
18214 break;
18215
18216 case SWITCH_STMT:
18217 stmt = begin_switch_stmt ();
18218 tmp = RECUR (SWITCH_STMT_COND (t));
18219 finish_switch_cond (tmp, stmt);
18220 RECUR (SWITCH_STMT_BODY (t));
18221 finish_switch_stmt (stmt);
18222 break;
18223
18224 case CASE_LABEL_EXPR:
18225 {
18226 tree decl = CASE_LABEL (t);
18227 tree low = RECUR (CASE_LOW (t));
18228 tree high = RECUR (CASE_HIGH (t));
18229 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18230 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18231 {
18232 tree label = CASE_LABEL (l);
18233 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18234 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18235 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18236 }
18237 }
18238 break;
18239
18240 case LABEL_EXPR:
18241 {
18242 tree decl = LABEL_EXPR_LABEL (t);
18243 tree label;
18244
18245 label = finish_label_stmt (DECL_NAME (decl));
18246 if (TREE_CODE (label) == LABEL_DECL)
18247 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18248 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18249 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18250 }
18251 break;
18252
18253 case GOTO_EXPR:
18254 tmp = GOTO_DESTINATION (t);
18255 if (TREE_CODE (tmp) != LABEL_DECL)
18256 /* Computed goto's must be tsubst'd into. On the other hand,
18257 non-computed gotos must not be; the identifier in question
18258 will have no binding. */
18259 tmp = RECUR (tmp);
18260 else
18261 tmp = DECL_NAME (tmp);
18262 finish_goto_stmt (tmp);
18263 break;
18264
18265 case ASM_EXPR:
18266 {
18267 tree string = RECUR (ASM_STRING (t));
18268 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18269 complain, in_decl);
18270 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18271 complain, in_decl);
18272 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18273 complain, in_decl);
18274 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18275 complain, in_decl);
18276 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18277 outputs, inputs, clobbers, labels,
18278 ASM_INLINE_P (t));
18279 tree asm_expr = tmp;
18280 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18281 asm_expr = TREE_OPERAND (asm_expr, 0);
18282 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18283 }
18284 break;
18285
18286 case TRY_BLOCK:
18287 if (CLEANUP_P (t))
18288 {
18289 stmt = begin_try_block ();
18290 RECUR (TRY_STMTS (t));
18291 finish_cleanup_try_block (stmt);
18292 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18293 }
18294 else
18295 {
18296 tree compound_stmt = NULL_TREE;
18297
18298 if (FN_TRY_BLOCK_P (t))
18299 stmt = begin_function_try_block (&compound_stmt);
18300 else
18301 stmt = begin_try_block ();
18302
18303 RECUR (TRY_STMTS (t));
18304
18305 if (FN_TRY_BLOCK_P (t))
18306 finish_function_try_block (stmt);
18307 else
18308 finish_try_block (stmt);
18309
18310 RECUR (TRY_HANDLERS (t));
18311 if (FN_TRY_BLOCK_P (t))
18312 finish_function_handler_sequence (stmt, compound_stmt);
18313 else
18314 finish_handler_sequence (stmt);
18315 }
18316 break;
18317
18318 case HANDLER:
18319 {
18320 tree decl = HANDLER_PARMS (t);
18321
18322 if (decl)
18323 {
18324 decl = tsubst (decl, args, complain, in_decl);
18325 /* Prevent instantiate_decl from trying to instantiate
18326 this variable. We've already done all that needs to be
18327 done. */
18328 if (decl != error_mark_node)
18329 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18330 }
18331 stmt = begin_handler ();
18332 finish_handler_parms (decl, stmt);
18333 RECUR (HANDLER_BODY (t));
18334 finish_handler (stmt);
18335 }
18336 break;
18337
18338 case TAG_DEFN:
18339 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18340 if (CLASS_TYPE_P (tmp))
18341 {
18342 /* Local classes are not independent templates; they are
18343 instantiated along with their containing function. And this
18344 way we don't have to deal with pushing out of one local class
18345 to instantiate a member of another local class. */
18346 /* Closures are handled by the LAMBDA_EXPR. */
18347 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18348 complete_type (tmp);
18349 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18350 if ((VAR_P (fld)
18351 || (TREE_CODE (fld) == FUNCTION_DECL
18352 && !DECL_ARTIFICIAL (fld)))
18353 && DECL_TEMPLATE_INSTANTIATION (fld))
18354 instantiate_decl (fld, /*defer_ok=*/false,
18355 /*expl_inst_class=*/false);
18356 }
18357 break;
18358
18359 case STATIC_ASSERT:
18360 {
18361 tree condition;
18362
18363 ++c_inhibit_evaluation_warnings;
18364 condition =
18365 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18366 args,
18367 complain, in_decl,
18368 /*integral_constant_expression_p=*/true);
18369 --c_inhibit_evaluation_warnings;
18370
18371 finish_static_assert (condition,
18372 STATIC_ASSERT_MESSAGE (t),
18373 STATIC_ASSERT_SOURCE_LOCATION (t),
18374 /*member_p=*/false);
18375 }
18376 break;
18377
18378 case OACC_KERNELS:
18379 case OACC_PARALLEL:
18380 case OACC_SERIAL:
18381 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18382 in_decl);
18383 stmt = begin_omp_parallel ();
18384 RECUR (OMP_BODY (t));
18385 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18386 break;
18387
18388 case OMP_PARALLEL:
18389 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18390 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18391 complain, in_decl);
18392 if (OMP_PARALLEL_COMBINED (t))
18393 omp_parallel_combined_clauses = &tmp;
18394 stmt = begin_omp_parallel ();
18395 RECUR (OMP_PARALLEL_BODY (t));
18396 gcc_assert (omp_parallel_combined_clauses == NULL);
18397 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18398 = OMP_PARALLEL_COMBINED (t);
18399 pop_omp_privatization_clauses (r);
18400 break;
18401
18402 case OMP_TASK:
18403 if (OMP_TASK_BODY (t) == NULL_TREE)
18404 {
18405 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18406 complain, in_decl);
18407 t = copy_node (t);
18408 OMP_TASK_CLAUSES (t) = tmp;
18409 add_stmt (t);
18410 break;
18411 }
18412 r = push_omp_privatization_clauses (false);
18413 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18414 complain, in_decl);
18415 stmt = begin_omp_task ();
18416 RECUR (OMP_TASK_BODY (t));
18417 finish_omp_task (tmp, stmt);
18418 pop_omp_privatization_clauses (r);
18419 break;
18420
18421 case OMP_FOR:
18422 case OMP_LOOP:
18423 case OMP_SIMD:
18424 case OMP_DISTRIBUTE:
18425 case OMP_TASKLOOP:
18426 case OACC_LOOP:
18427 {
18428 tree clauses, body, pre_body;
18429 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18430 tree orig_declv = NULL_TREE;
18431 tree incrv = NULL_TREE;
18432 enum c_omp_region_type ort = C_ORT_OMP;
18433 bool any_range_for = false;
18434 int i;
18435
18436 if (TREE_CODE (t) == OACC_LOOP)
18437 ort = C_ORT_ACC;
18438
18439 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18440 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18441 in_decl);
18442 if (OMP_FOR_INIT (t) != NULL_TREE)
18443 {
18444 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18445 if (OMP_FOR_ORIG_DECLS (t))
18446 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18447 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18448 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18449 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18450 }
18451
18452 keep_next_level (true);
18453 stmt = begin_omp_structured_block ();
18454
18455 pre_body = push_stmt_list ();
18456 RECUR (OMP_FOR_PRE_BODY (t));
18457 pre_body = pop_stmt_list (pre_body);
18458
18459 if (OMP_FOR_INIT (t) != NULL_TREE)
18460 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18461 any_range_for
18462 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18463 condv, incrv, &clauses, args,
18464 complain, in_decl,
18465 integral_constant_expression_p);
18466 omp_parallel_combined_clauses = NULL;
18467
18468 if (any_range_for)
18469 {
18470 gcc_assert (orig_declv);
18471 body = begin_omp_structured_block ();
18472 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18473 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18474 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18475 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18476 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18477 TREE_VEC_ELT (declv, i));
18478 }
18479 else
18480 body = push_stmt_list ();
18481 RECUR (OMP_FOR_BODY (t));
18482 if (any_range_for)
18483 body = finish_omp_structured_block (body);
18484 else
18485 body = pop_stmt_list (body);
18486
18487 if (OMP_FOR_INIT (t) != NULL_TREE)
18488 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18489 orig_declv, initv, condv, incrv, body, pre_body,
18490 NULL, clauses);
18491 else
18492 {
18493 t = make_node (TREE_CODE (t));
18494 TREE_TYPE (t) = void_type_node;
18495 OMP_FOR_BODY (t) = body;
18496 OMP_FOR_PRE_BODY (t) = pre_body;
18497 OMP_FOR_CLAUSES (t) = clauses;
18498 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18499 add_stmt (t);
18500 }
18501
18502 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18503 t));
18504 pop_omp_privatization_clauses (r);
18505 }
18506 break;
18507
18508 case OMP_SECTIONS:
18509 omp_parallel_combined_clauses = NULL;
18510 /* FALLTHRU */
18511 case OMP_SINGLE:
18512 case OMP_TEAMS:
18513 case OMP_CRITICAL:
18514 case OMP_TASKGROUP:
18515 case OMP_SCAN:
18516 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18517 && OMP_TEAMS_COMBINED (t));
18518 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18519 in_decl);
18520 if (TREE_CODE (t) == OMP_TEAMS)
18521 {
18522 keep_next_level (true);
18523 stmt = begin_omp_structured_block ();
18524 RECUR (OMP_BODY (t));
18525 stmt = finish_omp_structured_block (stmt);
18526 }
18527 else
18528 {
18529 stmt = push_stmt_list ();
18530 RECUR (OMP_BODY (t));
18531 stmt = pop_stmt_list (stmt);
18532 }
18533
18534 if (TREE_CODE (t) == OMP_CRITICAL
18535 && tmp != NULL_TREE
18536 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18537 {
18538 error_at (OMP_CLAUSE_LOCATION (tmp),
18539 "%<#pragma omp critical%> with %<hint%> clause requires "
18540 "a name, except when %<omp_sync_hint_none%> is used");
18541 RETURN (error_mark_node);
18542 }
18543 t = copy_node (t);
18544 OMP_BODY (t) = stmt;
18545 OMP_CLAUSES (t) = tmp;
18546 add_stmt (t);
18547 pop_omp_privatization_clauses (r);
18548 break;
18549
18550 case OMP_DEPOBJ:
18551 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18552 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18553 {
18554 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18555 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18556 {
18557 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18558 args, complain, in_decl);
18559 if (tmp == NULL_TREE)
18560 tmp = error_mark_node;
18561 }
18562 else
18563 {
18564 kind = (enum omp_clause_depend_kind)
18565 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18566 tmp = NULL_TREE;
18567 }
18568 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18569 }
18570 else
18571 finish_omp_depobj (EXPR_LOCATION (t), r,
18572 OMP_CLAUSE_DEPEND_SOURCE,
18573 OMP_DEPOBJ_CLAUSES (t));
18574 break;
18575
18576 case OACC_DATA:
18577 case OMP_TARGET_DATA:
18578 case OMP_TARGET:
18579 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18580 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18581 in_decl);
18582 keep_next_level (true);
18583 stmt = begin_omp_structured_block ();
18584
18585 RECUR (OMP_BODY (t));
18586 stmt = finish_omp_structured_block (stmt);
18587
18588 t = copy_node (t);
18589 OMP_BODY (t) = stmt;
18590 OMP_CLAUSES (t) = tmp;
18591 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18592 {
18593 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18594 if (teams)
18595 {
18596 /* For combined target teams, ensure the num_teams and
18597 thread_limit clause expressions are evaluated on the host,
18598 before entering the target construct. */
18599 tree c;
18600 for (c = OMP_TEAMS_CLAUSES (teams);
18601 c; c = OMP_CLAUSE_CHAIN (c))
18602 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18603 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18604 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18605 {
18606 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18607 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18608 if (expr == error_mark_node)
18609 continue;
18610 tmp = TARGET_EXPR_SLOT (expr);
18611 add_stmt (expr);
18612 OMP_CLAUSE_OPERAND (c, 0) = expr;
18613 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18614 OMP_CLAUSE_FIRSTPRIVATE);
18615 OMP_CLAUSE_DECL (tc) = tmp;
18616 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18617 OMP_TARGET_CLAUSES (t) = tc;
18618 }
18619 }
18620 }
18621 add_stmt (t);
18622 break;
18623
18624 case OACC_DECLARE:
18625 t = copy_node (t);
18626 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18627 complain, in_decl);
18628 OACC_DECLARE_CLAUSES (t) = tmp;
18629 add_stmt (t);
18630 break;
18631
18632 case OMP_TARGET_UPDATE:
18633 case OMP_TARGET_ENTER_DATA:
18634 case OMP_TARGET_EXIT_DATA:
18635 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18636 complain, in_decl);
18637 t = copy_node (t);
18638 OMP_STANDALONE_CLAUSES (t) = tmp;
18639 add_stmt (t);
18640 break;
18641
18642 case OACC_ENTER_DATA:
18643 case OACC_EXIT_DATA:
18644 case OACC_UPDATE:
18645 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18646 complain, in_decl);
18647 t = copy_node (t);
18648 OMP_STANDALONE_CLAUSES (t) = tmp;
18649 add_stmt (t);
18650 break;
18651
18652 case OMP_ORDERED:
18653 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18654 complain, in_decl);
18655 stmt = push_stmt_list ();
18656 RECUR (OMP_BODY (t));
18657 stmt = pop_stmt_list (stmt);
18658
18659 t = copy_node (t);
18660 OMP_BODY (t) = stmt;
18661 OMP_ORDERED_CLAUSES (t) = tmp;
18662 add_stmt (t);
18663 break;
18664
18665 case OMP_MASTER:
18666 omp_parallel_combined_clauses = NULL;
18667 /* FALLTHRU */
18668 case OMP_SECTION:
18669 stmt = push_stmt_list ();
18670 RECUR (OMP_BODY (t));
18671 stmt = pop_stmt_list (stmt);
18672
18673 t = copy_node (t);
18674 OMP_BODY (t) = stmt;
18675 add_stmt (t);
18676 break;
18677
18678 case OMP_ATOMIC:
18679 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18680 tmp = NULL_TREE;
18681 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18682 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18683 complain, in_decl);
18684 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18685 {
18686 tree op1 = TREE_OPERAND (t, 1);
18687 tree rhs1 = NULL_TREE;
18688 tree lhs, rhs;
18689 if (TREE_CODE (op1) == COMPOUND_EXPR)
18690 {
18691 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18692 op1 = TREE_OPERAND (op1, 1);
18693 }
18694 lhs = RECUR (TREE_OPERAND (op1, 0));
18695 rhs = RECUR (TREE_OPERAND (op1, 1));
18696 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18697 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18698 OMP_ATOMIC_MEMORY_ORDER (t));
18699 }
18700 else
18701 {
18702 tree op1 = TREE_OPERAND (t, 1);
18703 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18704 tree rhs1 = NULL_TREE;
18705 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18706 enum tree_code opcode = NOP_EXPR;
18707 if (code == OMP_ATOMIC_READ)
18708 {
18709 v = RECUR (TREE_OPERAND (op1, 0));
18710 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18711 }
18712 else if (code == OMP_ATOMIC_CAPTURE_OLD
18713 || code == OMP_ATOMIC_CAPTURE_NEW)
18714 {
18715 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18716 v = RECUR (TREE_OPERAND (op1, 0));
18717 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18718 if (TREE_CODE (op11) == COMPOUND_EXPR)
18719 {
18720 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18721 op11 = TREE_OPERAND (op11, 1);
18722 }
18723 lhs = RECUR (TREE_OPERAND (op11, 0));
18724 rhs = RECUR (TREE_OPERAND (op11, 1));
18725 opcode = TREE_CODE (op11);
18726 if (opcode == MODIFY_EXPR)
18727 opcode = NOP_EXPR;
18728 }
18729 else
18730 {
18731 code = OMP_ATOMIC;
18732 lhs = RECUR (TREE_OPERAND (op1, 0));
18733 rhs = RECUR (TREE_OPERAND (op1, 1));
18734 }
18735 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18736 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18737 }
18738 break;
18739
18740 case TRANSACTION_EXPR:
18741 {
18742 int flags = 0;
18743 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18744 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18745
18746 if (TRANSACTION_EXPR_IS_STMT (t))
18747 {
18748 tree body = TRANSACTION_EXPR_BODY (t);
18749 tree noex = NULL_TREE;
18750 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18751 {
18752 noex = MUST_NOT_THROW_COND (body);
18753 if (noex == NULL_TREE)
18754 noex = boolean_true_node;
18755 body = TREE_OPERAND (body, 0);
18756 }
18757 stmt = begin_transaction_stmt (input_location, NULL, flags);
18758 RECUR (body);
18759 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18760 }
18761 else
18762 {
18763 stmt = build_transaction_expr (EXPR_LOCATION (t),
18764 RECUR (TRANSACTION_EXPR_BODY (t)),
18765 flags, NULL_TREE);
18766 RETURN (stmt);
18767 }
18768 }
18769 break;
18770
18771 case MUST_NOT_THROW_EXPR:
18772 {
18773 tree op0 = RECUR (TREE_OPERAND (t, 0));
18774 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18775 RETURN (build_must_not_throw_expr (op0, cond));
18776 }
18777
18778 case EXPR_PACK_EXPANSION:
18779 error ("invalid use of pack expansion expression");
18780 RETURN (error_mark_node);
18781
18782 case NONTYPE_ARGUMENT_PACK:
18783 error ("use %<...%> to expand argument pack");
18784 RETURN (error_mark_node);
18785
18786 case COMPOUND_EXPR:
18787 tmp = RECUR (TREE_OPERAND (t, 0));
18788 if (tmp == NULL_TREE)
18789 /* If the first operand was a statement, we're done with it. */
18790 RETURN (RECUR (TREE_OPERAND (t, 1)));
18791 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18792 RECUR (TREE_OPERAND (t, 1)),
18793 complain));
18794
18795 case ANNOTATE_EXPR:
18796 tmp = RECUR (TREE_OPERAND (t, 0));
18797 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18798 TREE_TYPE (tmp), tmp,
18799 RECUR (TREE_OPERAND (t, 1)),
18800 RECUR (TREE_OPERAND (t, 2))));
18801
18802 case PREDICT_EXPR:
18803 RETURN (add_stmt (copy_node (t)));
18804
18805 default:
18806 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18807
18808 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18809 /*function_p=*/false,
18810 integral_constant_expression_p));
18811 }
18812
18813 RETURN (NULL_TREE);
18814 out:
18815 input_location = loc;
18816 return r;
18817 #undef RECUR
18818 #undef RETURN
18819 }
18820
18821 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18822 function. For description of the body see comment above
18823 cp_parser_omp_declare_reduction_exprs. */
18824
18825 static void
18826 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18827 {
18828 if (t == NULL_TREE || t == error_mark_node)
18829 return;
18830
18831 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
18832
18833 tree_stmt_iterator tsi;
18834 int i;
18835 tree stmts[7];
18836 memset (stmts, 0, sizeof stmts);
18837 for (i = 0, tsi = tsi_start (t);
18838 i < 7 && !tsi_end_p (tsi);
18839 i++, tsi_next (&tsi))
18840 stmts[i] = tsi_stmt (tsi);
18841 gcc_assert (tsi_end_p (tsi));
18842
18843 if (i >= 3)
18844 {
18845 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18846 && TREE_CODE (stmts[1]) == DECL_EXPR);
18847 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18848 args, complain, in_decl);
18849 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18850 args, complain, in_decl);
18851 DECL_CONTEXT (omp_out) = current_function_decl;
18852 DECL_CONTEXT (omp_in) = current_function_decl;
18853 keep_next_level (true);
18854 tree block = begin_omp_structured_block ();
18855 tsubst_expr (stmts[2], args, complain, in_decl, false);
18856 block = finish_omp_structured_block (block);
18857 block = maybe_cleanup_point_expr_void (block);
18858 add_decl_expr (omp_out);
18859 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18860 TREE_NO_WARNING (omp_out) = 1;
18861 add_decl_expr (omp_in);
18862 finish_expr_stmt (block);
18863 }
18864 if (i >= 6)
18865 {
18866 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18867 && TREE_CODE (stmts[4]) == DECL_EXPR);
18868 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18869 args, complain, in_decl);
18870 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18871 args, complain, in_decl);
18872 DECL_CONTEXT (omp_priv) = current_function_decl;
18873 DECL_CONTEXT (omp_orig) = current_function_decl;
18874 keep_next_level (true);
18875 tree block = begin_omp_structured_block ();
18876 tsubst_expr (stmts[5], args, complain, in_decl, false);
18877 block = finish_omp_structured_block (block);
18878 block = maybe_cleanup_point_expr_void (block);
18879 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18880 add_decl_expr (omp_priv);
18881 add_decl_expr (omp_orig);
18882 finish_expr_stmt (block);
18883 if (i == 7)
18884 add_decl_expr (omp_orig);
18885 }
18886 }
18887
18888 /* T is a postfix-expression that is not being used in a function
18889 call. Return the substituted version of T. */
18890
18891 static tree
18892 tsubst_non_call_postfix_expression (tree t, tree args,
18893 tsubst_flags_t complain,
18894 tree in_decl)
18895 {
18896 if (TREE_CODE (t) == SCOPE_REF)
18897 t = tsubst_qualified_id (t, args, complain, in_decl,
18898 /*done=*/false, /*address_p=*/false);
18899 else
18900 t = tsubst_copy_and_build (t, args, complain, in_decl,
18901 /*function_p=*/false,
18902 /*integral_constant_expression_p=*/false);
18903
18904 return t;
18905 }
18906
18907 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18908 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18909 dependent init-capture. */
18910
18911 static void
18912 prepend_one_capture (tree field, tree init, tree &list,
18913 tsubst_flags_t complain)
18914 {
18915 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18916 {
18917 tree type = NULL_TREE;
18918 if (!init)
18919 {
18920 if (complain & tf_error)
18921 error ("empty initializer in lambda init-capture");
18922 init = error_mark_node;
18923 }
18924 else if (TREE_CODE (init) == TREE_LIST)
18925 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18926 if (!type)
18927 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18928 TREE_TYPE (field) = type;
18929 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18930 }
18931 list = tree_cons (field, init, list);
18932 }
18933
18934 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18935 instantiation context. Instantiating a pack expansion containing a lambda
18936 might result in multiple lambdas all based on the same lambda in the
18937 template. */
18938
18939 tree
18940 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18941 {
18942 tree oldfn = lambda_function (t);
18943 in_decl = oldfn;
18944
18945 tree r = build_lambda_expr ();
18946
18947 LAMBDA_EXPR_LOCATION (r)
18948 = LAMBDA_EXPR_LOCATION (t);
18949 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18950 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18951 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18952 LAMBDA_EXPR_INSTANTIATED (r) = true;
18953
18954 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18955 /* A lambda in a default argument outside a class gets no
18956 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18957 tsubst_default_argument calls start_lambda_scope, so we need to
18958 specifically ignore it here, and use the global scope. */
18959 record_null_lambda_scope (r);
18960 else
18961 record_lambda_scope (r);
18962
18963 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18964 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18965
18966 vec<tree,va_gc>* field_packs = NULL;
18967
18968 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18969 cap = TREE_CHAIN (cap))
18970 {
18971 tree ofield = TREE_PURPOSE (cap);
18972 tree init = TREE_VALUE (cap);
18973 if (PACK_EXPANSION_P (init))
18974 init = tsubst_pack_expansion (init, args, complain, in_decl);
18975 else
18976 init = tsubst_copy_and_build (init, args, complain, in_decl,
18977 /*fn*/false, /*constexpr*/false);
18978
18979 if (init == error_mark_node)
18980 return error_mark_node;
18981
18982 if (init && TREE_CODE (init) == TREE_LIST)
18983 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18984
18985 if (!processing_template_decl
18986 && init && TREE_CODE (init) != TREE_VEC
18987 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
18988 {
18989 /* For a VLA, simply tsubsting the field type won't work, we need to
18990 go through add_capture again. XXX do we want to do this for all
18991 captures? */
18992 tree name = (get_identifier
18993 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
18994 tree ftype = TREE_TYPE (ofield);
18995 bool by_ref = (TYPE_REF_P (ftype)
18996 || (TREE_CODE (ftype) == DECLTYPE_TYPE
18997 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
18998 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
18999 continue;
19000 }
19001
19002 if (PACK_EXPANSION_P (ofield))
19003 ofield = PACK_EXPANSION_PATTERN (ofield);
19004 tree field = tsubst_decl (ofield, args, complain);
19005
19006 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19007 {
19008 /* Remember these for when we've pushed local_specializations. */
19009 vec_safe_push (field_packs, ofield);
19010 vec_safe_push (field_packs, field);
19011 }
19012
19013 if (field == error_mark_node)
19014 return error_mark_node;
19015
19016 if (TREE_CODE (field) == TREE_VEC)
19017 {
19018 int len = TREE_VEC_LENGTH (field);
19019 gcc_assert (TREE_CODE (init) == TREE_VEC
19020 && TREE_VEC_LENGTH (init) == len);
19021 for (int i = 0; i < len; ++i)
19022 prepend_one_capture (TREE_VEC_ELT (field, i),
19023 TREE_VEC_ELT (init, i),
19024 LAMBDA_EXPR_CAPTURE_LIST (r),
19025 complain);
19026 }
19027 else
19028 {
19029 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19030 complain);
19031
19032 if (id_equal (DECL_NAME (field), "__this"))
19033 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19034 }
19035 }
19036
19037 tree type = begin_lambda_type (r);
19038 if (type == error_mark_node)
19039 return error_mark_node;
19040
19041 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19042 determine_visibility (TYPE_NAME (type));
19043
19044 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19045
19046 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19047 ? DECL_TI_TEMPLATE (oldfn)
19048 : NULL_TREE);
19049
19050 tree fntype = static_fn_type (oldfn);
19051 if (oldtmpl)
19052 ++processing_template_decl;
19053 fntype = tsubst (fntype, args, complain, in_decl);
19054 if (oldtmpl)
19055 --processing_template_decl;
19056
19057 if (fntype == error_mark_node)
19058 r = error_mark_node;
19059 else
19060 {
19061 /* The body of a lambda-expression is not a subexpression of the
19062 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19063 which would be skipped if cp_unevaluated_operand. */
19064 cp_evaluated ev;
19065
19066 /* Fix the type of 'this'. */
19067 fntype = build_memfn_type (fntype, type,
19068 type_memfn_quals (fntype),
19069 type_memfn_rqual (fntype));
19070 tree fn, tmpl;
19071 if (oldtmpl)
19072 {
19073 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19074 if (tmpl == error_mark_node)
19075 {
19076 r = error_mark_node;
19077 goto out;
19078 }
19079 fn = DECL_TEMPLATE_RESULT (tmpl);
19080 finish_member_declaration (tmpl);
19081 }
19082 else
19083 {
19084 tmpl = NULL_TREE;
19085 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19086 if (fn == error_mark_node)
19087 {
19088 r = error_mark_node;
19089 goto out;
19090 }
19091 finish_member_declaration (fn);
19092 }
19093
19094 if (tree ci = get_constraints (oldfn))
19095 {
19096 /* Substitute into the lambda's constraints. */
19097 if (oldtmpl)
19098 ++processing_template_decl;
19099 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19100 if (oldtmpl)
19101 --processing_template_decl;
19102 set_constraints (fn, ci);
19103 }
19104
19105 /* Let finish_function set this. */
19106 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19107
19108 bool nested = cfun;
19109 if (nested)
19110 push_function_context ();
19111 else
19112 /* Still increment function_depth so that we don't GC in the
19113 middle of an expression. */
19114 ++function_depth;
19115
19116 local_specialization_stack s (lss_copy);
19117
19118 tree body = start_lambda_function (fn, r);
19119
19120 /* Now record them for lookup_init_capture_pack. */
19121 int fplen = vec_safe_length (field_packs);
19122 for (int i = 0; i < fplen; )
19123 {
19124 tree pack = (*field_packs)[i++];
19125 tree inst = (*field_packs)[i++];
19126 register_local_specialization (inst, pack);
19127 }
19128 release_tree_vector (field_packs);
19129
19130 register_parameter_specializations (oldfn, fn);
19131
19132 if (oldtmpl)
19133 {
19134 /* We might not partially instantiate some parts of the function, so
19135 copy these flags from the original template. */
19136 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19137 current_function_returns_value = ol->returns_value;
19138 current_function_returns_null = ol->returns_null;
19139 current_function_returns_abnormally = ol->returns_abnormally;
19140 current_function_infinite_loop = ol->infinite_loop;
19141 }
19142
19143 /* [temp.deduct] A lambda-expression appearing in a function type or a
19144 template parameter is not considered part of the immediate context for
19145 the purposes of template argument deduction. */
19146 complain = tf_warning_or_error;
19147
19148 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19149 /*constexpr*/false);
19150
19151 finish_lambda_function (body);
19152
19153 if (nested)
19154 pop_function_context ();
19155 else
19156 --function_depth;
19157
19158 /* The capture list was built up in reverse order; fix that now. */
19159 LAMBDA_EXPR_CAPTURE_LIST (r)
19160 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19161
19162 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19163
19164 maybe_add_lambda_conv_op (type);
19165 }
19166
19167 out:
19168 finish_struct (type, /*attr*/NULL_TREE);
19169
19170 insert_pending_capture_proxies ();
19171
19172 return r;
19173 }
19174
19175 /* Like tsubst but deals with expressions and performs semantic
19176 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19177
19178 tree
19179 tsubst_copy_and_build (tree t,
19180 tree args,
19181 tsubst_flags_t complain,
19182 tree in_decl,
19183 bool function_p,
19184 bool integral_constant_expression_p)
19185 {
19186 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19187 #define RECUR(NODE) \
19188 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19189 /*function_p=*/false, \
19190 integral_constant_expression_p)
19191
19192 tree retval, op1;
19193 location_t save_loc;
19194
19195 if (t == NULL_TREE || t == error_mark_node)
19196 return t;
19197
19198 save_loc = input_location;
19199 if (location_t eloc = cp_expr_location (t))
19200 input_location = eloc;
19201
19202 /* N3276 decltype magic only applies to calls at the top level or on the
19203 right side of a comma. */
19204 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19205 complain &= ~tf_decltype;
19206
19207 switch (TREE_CODE (t))
19208 {
19209 case USING_DECL:
19210 t = DECL_NAME (t);
19211 /* Fall through. */
19212 case IDENTIFIER_NODE:
19213 {
19214 tree decl;
19215 cp_id_kind idk;
19216 bool non_integral_constant_expression_p;
19217 const char *error_msg;
19218
19219 if (IDENTIFIER_CONV_OP_P (t))
19220 {
19221 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19222 t = make_conv_op_name (new_type);
19223 }
19224
19225 /* Look up the name. */
19226 decl = lookup_name (t);
19227
19228 /* By convention, expressions use ERROR_MARK_NODE to indicate
19229 failure, not NULL_TREE. */
19230 if (decl == NULL_TREE)
19231 decl = error_mark_node;
19232
19233 decl = finish_id_expression (t, decl, NULL_TREE,
19234 &idk,
19235 integral_constant_expression_p,
19236 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19237 &non_integral_constant_expression_p,
19238 /*template_p=*/false,
19239 /*done=*/true,
19240 /*address_p=*/false,
19241 /*template_arg_p=*/false,
19242 &error_msg,
19243 input_location);
19244 if (error_msg)
19245 error (error_msg);
19246 if (!function_p && identifier_p (decl))
19247 {
19248 if (complain & tf_error)
19249 unqualified_name_lookup_error (decl);
19250 decl = error_mark_node;
19251 }
19252 RETURN (decl);
19253 }
19254
19255 case TEMPLATE_ID_EXPR:
19256 {
19257 tree object;
19258 tree templ = RECUR (TREE_OPERAND (t, 0));
19259 tree targs = TREE_OPERAND (t, 1);
19260
19261 if (targs)
19262 targs = tsubst_template_args (targs, args, complain, in_decl);
19263 if (targs == error_mark_node)
19264 RETURN (error_mark_node);
19265
19266 if (TREE_CODE (templ) == SCOPE_REF)
19267 {
19268 tree name = TREE_OPERAND (templ, 1);
19269 tree tid = lookup_template_function (name, targs);
19270 TREE_OPERAND (templ, 1) = tid;
19271 RETURN (templ);
19272 }
19273
19274 if (concept_definition_p (templ))
19275 {
19276 tree check = build_concept_check (templ, targs, complain);
19277 if (check == error_mark_node)
19278 RETURN (error_mark_node);
19279
19280 tree id = unpack_concept_check (check);
19281
19282 /* If we built a function concept check, return the underlying
19283 template-id. So we can evaluate it as a function call. */
19284 if (function_concept_p (TREE_OPERAND (id, 0)))
19285 RETURN (id);
19286
19287 RETURN (check);
19288 }
19289
19290 if (variable_template_p (templ))
19291 {
19292 tree r = lookup_and_finish_template_variable (templ, targs,
19293 complain);
19294 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19295 RETURN (r);
19296 }
19297
19298 if (TREE_CODE (templ) == COMPONENT_REF)
19299 {
19300 object = TREE_OPERAND (templ, 0);
19301 templ = TREE_OPERAND (templ, 1);
19302 }
19303 else
19304 object = NULL_TREE;
19305 templ = lookup_template_function (templ, targs);
19306
19307 if (object)
19308 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
19309 object, templ, NULL_TREE));
19310 else
19311 RETURN (baselink_for_fns (templ));
19312 }
19313
19314 case INDIRECT_REF:
19315 {
19316 tree r = RECUR (TREE_OPERAND (t, 0));
19317
19318 if (REFERENCE_REF_P (t))
19319 {
19320 /* A type conversion to reference type will be enclosed in
19321 such an indirect ref, but the substitution of the cast
19322 will have also added such an indirect ref. */
19323 r = convert_from_reference (r);
19324 }
19325 else
19326 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19327 complain|decltype_flag);
19328
19329 if (REF_PARENTHESIZED_P (t))
19330 r = force_paren_expr (r);
19331
19332 RETURN (r);
19333 }
19334
19335 case NOP_EXPR:
19336 {
19337 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19338 tree op0 = RECUR (TREE_OPERAND (t, 0));
19339 RETURN (build_nop (type, op0));
19340 }
19341
19342 case IMPLICIT_CONV_EXPR:
19343 {
19344 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19345 tree expr = RECUR (TREE_OPERAND (t, 0));
19346 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19347 {
19348 retval = copy_node (t);
19349 TREE_TYPE (retval) = type;
19350 TREE_OPERAND (retval, 0) = expr;
19351 RETURN (retval);
19352 }
19353 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19354 /* We'll pass this to convert_nontype_argument again, we don't need
19355 to actually perform any conversion here. */
19356 RETURN (expr);
19357 int flags = LOOKUP_IMPLICIT;
19358 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19359 flags = LOOKUP_NORMAL;
19360 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19361 flags |= LOOKUP_NO_NARROWING;
19362 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19363 flags));
19364 }
19365
19366 case CONVERT_EXPR:
19367 {
19368 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19369 tree op0 = RECUR (TREE_OPERAND (t, 0));
19370 if (op0 == error_mark_node)
19371 RETURN (error_mark_node);
19372 RETURN (build1 (CONVERT_EXPR, type, op0));
19373 }
19374
19375 case CAST_EXPR:
19376 case REINTERPRET_CAST_EXPR:
19377 case CONST_CAST_EXPR:
19378 case DYNAMIC_CAST_EXPR:
19379 case STATIC_CAST_EXPR:
19380 {
19381 tree type;
19382 tree op, r = NULL_TREE;
19383
19384 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19385 if (integral_constant_expression_p
19386 && !cast_valid_in_integral_constant_expression_p (type))
19387 {
19388 if (complain & tf_error)
19389 error ("a cast to a type other than an integral or "
19390 "enumeration type cannot appear in a constant-expression");
19391 RETURN (error_mark_node);
19392 }
19393
19394 op = RECUR (TREE_OPERAND (t, 0));
19395
19396 warning_sentinel s(warn_useless_cast);
19397 warning_sentinel s2(warn_ignored_qualifiers);
19398 switch (TREE_CODE (t))
19399 {
19400 case CAST_EXPR:
19401 r = build_functional_cast (input_location, type, op, complain);
19402 break;
19403 case REINTERPRET_CAST_EXPR:
19404 r = build_reinterpret_cast (input_location, type, op, complain);
19405 break;
19406 case CONST_CAST_EXPR:
19407 r = build_const_cast (input_location, type, op, complain);
19408 break;
19409 case DYNAMIC_CAST_EXPR:
19410 r = build_dynamic_cast (input_location, type, op, complain);
19411 break;
19412 case STATIC_CAST_EXPR:
19413 r = build_static_cast (input_location, type, op, complain);
19414 break;
19415 default:
19416 gcc_unreachable ();
19417 }
19418
19419 RETURN (r);
19420 }
19421
19422 case POSTDECREMENT_EXPR:
19423 case POSTINCREMENT_EXPR:
19424 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19425 args, complain, in_decl);
19426 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19427 complain|decltype_flag));
19428
19429 case PREDECREMENT_EXPR:
19430 case PREINCREMENT_EXPR:
19431 case NEGATE_EXPR:
19432 case BIT_NOT_EXPR:
19433 case ABS_EXPR:
19434 case TRUTH_NOT_EXPR:
19435 case UNARY_PLUS_EXPR: /* Unary + */
19436 case REALPART_EXPR:
19437 case IMAGPART_EXPR:
19438 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19439 RECUR (TREE_OPERAND (t, 0)),
19440 complain|decltype_flag));
19441
19442 case FIX_TRUNC_EXPR:
19443 gcc_unreachable ();
19444
19445 case ADDR_EXPR:
19446 op1 = TREE_OPERAND (t, 0);
19447 if (TREE_CODE (op1) == LABEL_DECL)
19448 RETURN (finish_label_address_expr (DECL_NAME (op1),
19449 EXPR_LOCATION (op1)));
19450 if (TREE_CODE (op1) == SCOPE_REF)
19451 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19452 /*done=*/true, /*address_p=*/true);
19453 else
19454 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19455 in_decl);
19456 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19457 complain|decltype_flag));
19458
19459 case PLUS_EXPR:
19460 case MINUS_EXPR:
19461 case MULT_EXPR:
19462 case TRUNC_DIV_EXPR:
19463 case CEIL_DIV_EXPR:
19464 case FLOOR_DIV_EXPR:
19465 case ROUND_DIV_EXPR:
19466 case EXACT_DIV_EXPR:
19467 case BIT_AND_EXPR:
19468 case BIT_IOR_EXPR:
19469 case BIT_XOR_EXPR:
19470 case TRUNC_MOD_EXPR:
19471 case FLOOR_MOD_EXPR:
19472 case TRUTH_ANDIF_EXPR:
19473 case TRUTH_ORIF_EXPR:
19474 case TRUTH_AND_EXPR:
19475 case TRUTH_OR_EXPR:
19476 case RSHIFT_EXPR:
19477 case LSHIFT_EXPR:
19478 case EQ_EXPR:
19479 case NE_EXPR:
19480 case MAX_EXPR:
19481 case MIN_EXPR:
19482 case LE_EXPR:
19483 case GE_EXPR:
19484 case LT_EXPR:
19485 case GT_EXPR:
19486 case SPACESHIP_EXPR:
19487 case MEMBER_REF:
19488 case DOTSTAR_EXPR:
19489 {
19490 /* If T was type-dependent, suppress warnings that depend on the range
19491 of the types involved. */
19492 bool was_dep = type_dependent_expression_p_push (t);
19493
19494 tree op0 = RECUR (TREE_OPERAND (t, 0));
19495 tree op1 = RECUR (TREE_OPERAND (t, 1));
19496
19497 warning_sentinel s1(warn_type_limits, was_dep);
19498 warning_sentinel s2(warn_div_by_zero, was_dep);
19499 warning_sentinel s3(warn_logical_op, was_dep);
19500 warning_sentinel s4(warn_tautological_compare, was_dep);
19501
19502 tree r = build_x_binary_op
19503 (input_location, TREE_CODE (t),
19504 op0,
19505 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19506 ? ERROR_MARK
19507 : TREE_CODE (TREE_OPERAND (t, 0))),
19508 op1,
19509 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19510 ? ERROR_MARK
19511 : TREE_CODE (TREE_OPERAND (t, 1))),
19512 /*overload=*/NULL,
19513 complain|decltype_flag);
19514 if (EXPR_P (r) && TREE_NO_WARNING (t))
19515 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19516
19517 RETURN (r);
19518 }
19519
19520 case POINTER_PLUS_EXPR:
19521 {
19522 tree op0 = RECUR (TREE_OPERAND (t, 0));
19523 if (op0 == error_mark_node)
19524 RETURN (error_mark_node);
19525 tree op1 = RECUR (TREE_OPERAND (t, 1));
19526 if (op1 == error_mark_node)
19527 RETURN (error_mark_node);
19528 RETURN (fold_build_pointer_plus (op0, op1));
19529 }
19530
19531 case SCOPE_REF:
19532 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19533 /*address_p=*/false));
19534 case ARRAY_REF:
19535 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19536 args, complain, in_decl);
19537 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19538 RECUR (TREE_OPERAND (t, 1)),
19539 complain|decltype_flag));
19540
19541 case SIZEOF_EXPR:
19542 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19543 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19544 RETURN (tsubst_copy (t, args, complain, in_decl));
19545 /* Fall through */
19546
19547 case ALIGNOF_EXPR:
19548 {
19549 tree r;
19550
19551 op1 = TREE_OPERAND (t, 0);
19552 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19553 op1 = TREE_TYPE (op1);
19554 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19555 && ALIGNOF_EXPR_STD_P (t));
19556 if (!args)
19557 {
19558 /* When there are no ARGS, we are trying to evaluate a
19559 non-dependent expression from the parser. Trying to do
19560 the substitutions may not work. */
19561 if (!TYPE_P (op1))
19562 op1 = TREE_TYPE (op1);
19563 }
19564 else
19565 {
19566 ++cp_unevaluated_operand;
19567 ++c_inhibit_evaluation_warnings;
19568 if (TYPE_P (op1))
19569 op1 = tsubst (op1, args, complain, in_decl);
19570 else
19571 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19572 /*function_p=*/false,
19573 /*integral_constant_expression_p=*/
19574 false);
19575 --cp_unevaluated_operand;
19576 --c_inhibit_evaluation_warnings;
19577 }
19578 if (TYPE_P (op1))
19579 r = cxx_sizeof_or_alignof_type (input_location,
19580 op1, TREE_CODE (t), std_alignof,
19581 complain & tf_error);
19582 else
19583 r = cxx_sizeof_or_alignof_expr (input_location,
19584 op1, TREE_CODE (t),
19585 complain & tf_error);
19586 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19587 {
19588 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19589 {
19590 if (!processing_template_decl && TYPE_P (op1))
19591 {
19592 r = build_min (SIZEOF_EXPR, size_type_node,
19593 build1 (NOP_EXPR, op1, error_mark_node));
19594 SIZEOF_EXPR_TYPE_P (r) = 1;
19595 }
19596 else
19597 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19598 TREE_SIDE_EFFECTS (r) = 0;
19599 TREE_READONLY (r) = 1;
19600 }
19601 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19602 }
19603 RETURN (r);
19604 }
19605
19606 case AT_ENCODE_EXPR:
19607 {
19608 op1 = TREE_OPERAND (t, 0);
19609 ++cp_unevaluated_operand;
19610 ++c_inhibit_evaluation_warnings;
19611 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19612 /*function_p=*/false,
19613 /*integral_constant_expression_p=*/false);
19614 --cp_unevaluated_operand;
19615 --c_inhibit_evaluation_warnings;
19616 RETURN (objc_build_encode_expr (op1));
19617 }
19618
19619 case NOEXCEPT_EXPR:
19620 op1 = TREE_OPERAND (t, 0);
19621 ++cp_unevaluated_operand;
19622 ++c_inhibit_evaluation_warnings;
19623 ++cp_noexcept_operand;
19624 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19625 /*function_p=*/false,
19626 /*integral_constant_expression_p=*/false);
19627 --cp_unevaluated_operand;
19628 --c_inhibit_evaluation_warnings;
19629 --cp_noexcept_operand;
19630 RETURN (finish_noexcept_expr (op1, complain));
19631
19632 case MODOP_EXPR:
19633 {
19634 warning_sentinel s(warn_div_by_zero);
19635 tree lhs = RECUR (TREE_OPERAND (t, 0));
19636 tree rhs = RECUR (TREE_OPERAND (t, 2));
19637 tree r = build_x_modify_expr
19638 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19639 complain|decltype_flag);
19640 /* TREE_NO_WARNING must be set if either the expression was
19641 parenthesized or it uses an operator such as >>= rather
19642 than plain assignment. In the former case, it was already
19643 set and must be copied. In the latter case,
19644 build_x_modify_expr sets it and it must not be reset
19645 here. */
19646 if (TREE_NO_WARNING (t))
19647 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19648
19649 RETURN (r);
19650 }
19651
19652 case ARROW_EXPR:
19653 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19654 args, complain, in_decl);
19655 /* Remember that there was a reference to this entity. */
19656 if (DECL_P (op1)
19657 && !mark_used (op1, complain) && !(complain & tf_error))
19658 RETURN (error_mark_node);
19659 RETURN (build_x_arrow (input_location, op1, complain));
19660
19661 case NEW_EXPR:
19662 {
19663 tree placement = RECUR (TREE_OPERAND (t, 0));
19664 tree init = RECUR (TREE_OPERAND (t, 3));
19665 vec<tree, va_gc> *placement_vec;
19666 vec<tree, va_gc> *init_vec;
19667 tree ret;
19668 location_t loc = EXPR_LOCATION (t);
19669
19670 if (placement == NULL_TREE)
19671 placement_vec = NULL;
19672 else if (placement == error_mark_node)
19673 RETURN (error_mark_node);
19674 else
19675 {
19676 placement_vec = make_tree_vector ();
19677 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19678 vec_safe_push (placement_vec, TREE_VALUE (placement));
19679 }
19680
19681 /* If there was an initializer in the original tree, but it
19682 instantiated to an empty list, then we should pass a
19683 non-NULL empty vector to tell build_new that it was an
19684 empty initializer() rather than no initializer. This can
19685 only happen when the initializer is a pack expansion whose
19686 parameter packs are of length zero. */
19687 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19688 init_vec = NULL;
19689 else
19690 {
19691 init_vec = make_tree_vector ();
19692 if (init == void_node)
19693 gcc_assert (init_vec != NULL);
19694 else
19695 {
19696 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19697 vec_safe_push (init_vec, TREE_VALUE (init));
19698 }
19699 }
19700
19701 /* Avoid passing an enclosing decl to valid_array_size_p. */
19702 in_decl = NULL_TREE;
19703
19704 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19705 tree op2 = RECUR (TREE_OPERAND (t, 2));
19706 ret = build_new (loc, &placement_vec, op1, op2,
19707 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19708 complain);
19709
19710 if (placement_vec != NULL)
19711 release_tree_vector (placement_vec);
19712 if (init_vec != NULL)
19713 release_tree_vector (init_vec);
19714
19715 RETURN (ret);
19716 }
19717
19718 case DELETE_EXPR:
19719 {
19720 tree op0 = RECUR (TREE_OPERAND (t, 0));
19721 tree op1 = RECUR (TREE_OPERAND (t, 1));
19722 RETURN (delete_sanity (input_location, op0, op1,
19723 DELETE_EXPR_USE_VEC (t),
19724 DELETE_EXPR_USE_GLOBAL (t),
19725 complain));
19726 }
19727
19728 case COMPOUND_EXPR:
19729 {
19730 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19731 complain & ~tf_decltype, in_decl,
19732 /*function_p=*/false,
19733 integral_constant_expression_p);
19734 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19735 op0,
19736 RECUR (TREE_OPERAND (t, 1)),
19737 complain|decltype_flag));
19738 }
19739
19740 case CALL_EXPR:
19741 {
19742 tree function;
19743 unsigned int nargs, i;
19744 bool qualified_p;
19745 bool koenig_p;
19746 tree ret;
19747
19748 function = CALL_EXPR_FN (t);
19749 /* Internal function with no arguments. */
19750 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19751 RETURN (t);
19752
19753 /* When we parsed the expression, we determined whether or
19754 not Koenig lookup should be performed. */
19755 koenig_p = KOENIG_LOOKUP_P (t);
19756 if (function == NULL_TREE)
19757 {
19758 koenig_p = false;
19759 qualified_p = false;
19760 }
19761 else if (TREE_CODE (function) == SCOPE_REF)
19762 {
19763 qualified_p = true;
19764 function = tsubst_qualified_id (function, args, complain, in_decl,
19765 /*done=*/false,
19766 /*address_p=*/false);
19767 }
19768 else if (koenig_p && identifier_p (function))
19769 {
19770 /* Do nothing; calling tsubst_copy_and_build on an identifier
19771 would incorrectly perform unqualified lookup again.
19772
19773 Note that we can also have an IDENTIFIER_NODE if the earlier
19774 unqualified lookup found a member function; in that case
19775 koenig_p will be false and we do want to do the lookup
19776 again to find the instantiated member function.
19777
19778 FIXME but doing that causes c++/15272, so we need to stop
19779 using IDENTIFIER_NODE in that situation. */
19780 qualified_p = false;
19781 }
19782 else
19783 {
19784 if (TREE_CODE (function) == COMPONENT_REF)
19785 {
19786 tree op = TREE_OPERAND (function, 1);
19787
19788 qualified_p = (TREE_CODE (op) == SCOPE_REF
19789 || (BASELINK_P (op)
19790 && BASELINK_QUALIFIED_P (op)));
19791 }
19792 else
19793 qualified_p = false;
19794
19795 if (TREE_CODE (function) == ADDR_EXPR
19796 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19797 /* Avoid error about taking the address of a constructor. */
19798 function = TREE_OPERAND (function, 0);
19799
19800 function = tsubst_copy_and_build (function, args, complain,
19801 in_decl,
19802 !qualified_p,
19803 integral_constant_expression_p);
19804
19805 if (BASELINK_P (function))
19806 qualified_p = true;
19807 }
19808
19809 nargs = call_expr_nargs (t);
19810 releasing_vec call_args;
19811 for (i = 0; i < nargs; ++i)
19812 {
19813 tree arg = CALL_EXPR_ARG (t, i);
19814
19815 if (!PACK_EXPANSION_P (arg))
19816 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19817 else
19818 {
19819 /* Expand the pack expansion and push each entry onto
19820 CALL_ARGS. */
19821 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19822 if (TREE_CODE (arg) == TREE_VEC)
19823 {
19824 unsigned int len, j;
19825
19826 len = TREE_VEC_LENGTH (arg);
19827 for (j = 0; j < len; ++j)
19828 {
19829 tree value = TREE_VEC_ELT (arg, j);
19830 if (value != NULL_TREE)
19831 value = convert_from_reference (value);
19832 vec_safe_push (call_args, value);
19833 }
19834 }
19835 else
19836 {
19837 /* A partial substitution. Add one entry. */
19838 vec_safe_push (call_args, arg);
19839 }
19840 }
19841 }
19842
19843 /* Stripped-down processing for a call in a thunk. Specifically, in
19844 the thunk template for a generic lambda. */
19845 if (CALL_FROM_THUNK_P (t))
19846 {
19847 /* Now that we've expanded any packs, the number of call args
19848 might be different. */
19849 unsigned int cargs = call_args->length ();
19850 tree thisarg = NULL_TREE;
19851 if (TREE_CODE (function) == COMPONENT_REF)
19852 {
19853 thisarg = TREE_OPERAND (function, 0);
19854 if (TREE_CODE (thisarg) == INDIRECT_REF)
19855 thisarg = TREE_OPERAND (thisarg, 0);
19856 function = TREE_OPERAND (function, 1);
19857 if (TREE_CODE (function) == BASELINK)
19858 function = BASELINK_FUNCTIONS (function);
19859 }
19860 /* We aren't going to do normal overload resolution, so force the
19861 template-id to resolve. */
19862 function = resolve_nondeduced_context (function, complain);
19863 for (unsigned i = 0; i < cargs; ++i)
19864 {
19865 /* In a thunk, pass through args directly, without any
19866 conversions. */
19867 tree arg = (*call_args)[i];
19868 while (TREE_CODE (arg) != PARM_DECL)
19869 arg = TREE_OPERAND (arg, 0);
19870 (*call_args)[i] = arg;
19871 }
19872 if (thisarg)
19873 {
19874 /* If there are no other args, just push 'this'. */
19875 if (cargs == 0)
19876 vec_safe_push (call_args, thisarg);
19877 else
19878 {
19879 /* Otherwise, shift the other args over to make room. */
19880 tree last = (*call_args)[cargs - 1];
19881 vec_safe_push (call_args, last);
19882 for (int i = cargs - 1; i > 0; --i)
19883 (*call_args)[i] = (*call_args)[i - 1];
19884 (*call_args)[0] = thisarg;
19885 }
19886 }
19887 ret = build_call_a (function, call_args->length (),
19888 call_args->address ());
19889 /* The thunk location is not interesting. */
19890 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19891 CALL_FROM_THUNK_P (ret) = true;
19892 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19893 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19894
19895 RETURN (ret);
19896 }
19897
19898 /* We do not perform argument-dependent lookup if normal
19899 lookup finds a non-function, in accordance with the
19900 expected resolution of DR 218. */
19901 if (koenig_p
19902 && ((is_overloaded_fn (function)
19903 /* If lookup found a member function, the Koenig lookup is
19904 not appropriate, even if an unqualified-name was used
19905 to denote the function. */
19906 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19907 || identifier_p (function))
19908 /* Only do this when substitution turns a dependent call
19909 into a non-dependent call. */
19910 && type_dependent_expression_p_push (t)
19911 && !any_type_dependent_arguments_p (call_args))
19912 function = perform_koenig_lookup (function, call_args, tf_none);
19913
19914 if (function != NULL_TREE
19915 && identifier_p (function)
19916 && !any_type_dependent_arguments_p (call_args))
19917 {
19918 if (koenig_p && (complain & tf_warning_or_error))
19919 {
19920 /* For backwards compatibility and good diagnostics, try
19921 the unqualified lookup again if we aren't in SFINAE
19922 context. */
19923 tree unq = (tsubst_copy_and_build
19924 (function, args, complain, in_decl, true,
19925 integral_constant_expression_p));
19926 if (unq == error_mark_node)
19927 RETURN (error_mark_node);
19928
19929 if (unq != function)
19930 {
19931 /* In a lambda fn, we have to be careful to not
19932 introduce new this captures. Legacy code can't
19933 be using lambdas anyway, so it's ok to be
19934 stricter. */
19935 bool in_lambda = (current_class_type
19936 && LAMBDA_TYPE_P (current_class_type));
19937 char const *const msg
19938 = G_("%qD was not declared in this scope, "
19939 "and no declarations were found by "
19940 "argument-dependent lookup at the point "
19941 "of instantiation");
19942
19943 bool diag = true;
19944 if (in_lambda)
19945 error_at (cp_expr_loc_or_input_loc (t),
19946 msg, function);
19947 else
19948 diag = permerror (cp_expr_loc_or_input_loc (t),
19949 msg, function);
19950 if (diag)
19951 {
19952 tree fn = unq;
19953
19954 if (INDIRECT_REF_P (fn))
19955 fn = TREE_OPERAND (fn, 0);
19956 if (is_overloaded_fn (fn))
19957 fn = get_first_fn (fn);
19958
19959 if (!DECL_P (fn))
19960 /* Can't say anything more. */;
19961 else if (DECL_CLASS_SCOPE_P (fn))
19962 {
19963 location_t loc = cp_expr_loc_or_input_loc (t);
19964 inform (loc,
19965 "declarations in dependent base %qT are "
19966 "not found by unqualified lookup",
19967 DECL_CLASS_CONTEXT (fn));
19968 if (current_class_ptr)
19969 inform (loc,
19970 "use %<this->%D%> instead", function);
19971 else
19972 inform (loc,
19973 "use %<%T::%D%> instead",
19974 current_class_name, function);
19975 }
19976 else
19977 inform (DECL_SOURCE_LOCATION (fn),
19978 "%qD declared here, later in the "
19979 "translation unit", fn);
19980 if (in_lambda)
19981 RETURN (error_mark_node);
19982 }
19983
19984 function = unq;
19985 }
19986 }
19987 if (identifier_p (function))
19988 {
19989 if (complain & tf_error)
19990 unqualified_name_lookup_error (function);
19991 RETURN (error_mark_node);
19992 }
19993 }
19994
19995 /* Remember that there was a reference to this entity. */
19996 if (function != NULL_TREE
19997 && DECL_P (function)
19998 && !mark_used (function, complain) && !(complain & tf_error))
19999 RETURN (error_mark_node);
20000
20001 /* Put back tf_decltype for the actual call. */
20002 complain |= decltype_flag;
20003
20004 if (function == NULL_TREE)
20005 switch (CALL_EXPR_IFN (t))
20006 {
20007 case IFN_LAUNDER:
20008 gcc_assert (nargs == 1);
20009 if (vec_safe_length (call_args) != 1)
20010 {
20011 error_at (cp_expr_loc_or_input_loc (t),
20012 "wrong number of arguments to "
20013 "%<__builtin_launder%>");
20014 ret = error_mark_node;
20015 }
20016 else
20017 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20018 (*call_args)[0], complain);
20019 break;
20020
20021 case IFN_VEC_CONVERT:
20022 gcc_assert (nargs == 1);
20023 if (vec_safe_length (call_args) != 1)
20024 {
20025 error_at (cp_expr_loc_or_input_loc (t),
20026 "wrong number of arguments to "
20027 "%<__builtin_convertvector%>");
20028 ret = error_mark_node;
20029 break;
20030 }
20031 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20032 tsubst (TREE_TYPE (t), args,
20033 complain, in_decl),
20034 complain);
20035 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20036 RETURN (ret);
20037 break;
20038
20039 default:
20040 /* Unsupported internal function with arguments. */
20041 gcc_unreachable ();
20042 }
20043 else if (TREE_CODE (function) == OFFSET_REF
20044 || TREE_CODE (function) == DOTSTAR_EXPR
20045 || TREE_CODE (function) == MEMBER_REF)
20046 ret = build_offset_ref_call_from_tree (function, &call_args,
20047 complain);
20048 else if (TREE_CODE (function) == COMPONENT_REF)
20049 {
20050 tree instance = TREE_OPERAND (function, 0);
20051 tree fn = TREE_OPERAND (function, 1);
20052
20053 if (processing_template_decl
20054 && (type_dependent_expression_p (instance)
20055 || (!BASELINK_P (fn)
20056 && TREE_CODE (fn) != FIELD_DECL)
20057 || type_dependent_expression_p (fn)
20058 || any_type_dependent_arguments_p (call_args)))
20059 ret = build_min_nt_call_vec (function, call_args);
20060 else if (!BASELINK_P (fn))
20061 ret = finish_call_expr (function, &call_args,
20062 /*disallow_virtual=*/false,
20063 /*koenig_p=*/false,
20064 complain);
20065 else
20066 ret = (build_new_method_call
20067 (instance, fn,
20068 &call_args, NULL_TREE,
20069 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20070 /*fn_p=*/NULL,
20071 complain));
20072 }
20073 else if (concept_check_p (function))
20074 {
20075 /* FUNCTION is a template-id referring to a concept definition. */
20076 tree id = unpack_concept_check (function);
20077 tree tmpl = TREE_OPERAND (id, 0);
20078 tree args = TREE_OPERAND (id, 1);
20079
20080 /* Calls to standard and variable concepts should have been
20081 previously diagnosed. */
20082 gcc_assert (function_concept_p (tmpl));
20083
20084 /* Ensure the result is wrapped as a call expression. */
20085 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20086 }
20087 else
20088 ret = finish_call_expr (function, &call_args,
20089 /*disallow_virtual=*/qualified_p,
20090 koenig_p,
20091 complain);
20092
20093 if (ret != error_mark_node)
20094 {
20095 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20096 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20097 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20098 if (op || ord || rev)
20099 {
20100 function = extract_call_expr (ret);
20101 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20102 CALL_EXPR_ORDERED_ARGS (function) = ord;
20103 CALL_EXPR_REVERSE_ARGS (function) = rev;
20104 }
20105 }
20106
20107 RETURN (ret);
20108 }
20109
20110 case COND_EXPR:
20111 {
20112 tree cond = RECUR (TREE_OPERAND (t, 0));
20113 cond = mark_rvalue_use (cond);
20114 tree folded_cond = fold_non_dependent_expr (cond, complain);
20115 tree exp1, exp2;
20116
20117 if (TREE_CODE (folded_cond) == INTEGER_CST)
20118 {
20119 if (integer_zerop (folded_cond))
20120 {
20121 ++c_inhibit_evaluation_warnings;
20122 exp1 = RECUR (TREE_OPERAND (t, 1));
20123 --c_inhibit_evaluation_warnings;
20124 exp2 = RECUR (TREE_OPERAND (t, 2));
20125 }
20126 else
20127 {
20128 exp1 = RECUR (TREE_OPERAND (t, 1));
20129 ++c_inhibit_evaluation_warnings;
20130 exp2 = RECUR (TREE_OPERAND (t, 2));
20131 --c_inhibit_evaluation_warnings;
20132 }
20133 cond = folded_cond;
20134 }
20135 else
20136 {
20137 exp1 = RECUR (TREE_OPERAND (t, 1));
20138 exp2 = RECUR (TREE_OPERAND (t, 2));
20139 }
20140
20141 warning_sentinel s(warn_duplicated_branches);
20142 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20143 cond, exp1, exp2, complain));
20144 }
20145
20146 case PSEUDO_DTOR_EXPR:
20147 {
20148 tree op0 = RECUR (TREE_OPERAND (t, 0));
20149 tree op1 = RECUR (TREE_OPERAND (t, 1));
20150 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20151 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20152 input_location));
20153 }
20154
20155 case TREE_LIST:
20156 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20157
20158 case COMPONENT_REF:
20159 {
20160 tree object;
20161 tree object_type;
20162 tree member;
20163 tree r;
20164
20165 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20166 args, complain, in_decl);
20167 /* Remember that there was a reference to this entity. */
20168 if (DECL_P (object)
20169 && !mark_used (object, complain) && !(complain & tf_error))
20170 RETURN (error_mark_node);
20171 object_type = TREE_TYPE (object);
20172
20173 member = TREE_OPERAND (t, 1);
20174 if (BASELINK_P (member))
20175 member = tsubst_baselink (member,
20176 non_reference (TREE_TYPE (object)),
20177 args, complain, in_decl);
20178 else
20179 member = tsubst_copy (member, args, complain, in_decl);
20180 if (member == error_mark_node)
20181 RETURN (error_mark_node);
20182
20183 if (TREE_CODE (member) == FIELD_DECL)
20184 {
20185 r = finish_non_static_data_member (member, object, NULL_TREE);
20186 if (TREE_CODE (r) == COMPONENT_REF)
20187 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20188 RETURN (r);
20189 }
20190 else if (type_dependent_expression_p (object))
20191 /* We can't do much here. */;
20192 else if (!CLASS_TYPE_P (object_type))
20193 {
20194 if (scalarish_type_p (object_type))
20195 {
20196 tree s = NULL_TREE;
20197 tree dtor = member;
20198
20199 if (TREE_CODE (dtor) == SCOPE_REF)
20200 {
20201 s = TREE_OPERAND (dtor, 0);
20202 dtor = TREE_OPERAND (dtor, 1);
20203 }
20204 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20205 {
20206 dtor = TREE_OPERAND (dtor, 0);
20207 if (TYPE_P (dtor))
20208 RETURN (finish_pseudo_destructor_expr
20209 (object, s, dtor, input_location));
20210 }
20211 }
20212 }
20213 else if (TREE_CODE (member) == SCOPE_REF
20214 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20215 {
20216 /* Lookup the template functions now that we know what the
20217 scope is. */
20218 tree scope = TREE_OPERAND (member, 0);
20219 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20220 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20221 member = lookup_qualified_name (scope, tmpl,
20222 /*is_type_p=*/false,
20223 /*complain=*/false);
20224 if (BASELINK_P (member))
20225 {
20226 BASELINK_FUNCTIONS (member)
20227 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20228 args);
20229 member = (adjust_result_of_qualified_name_lookup
20230 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20231 object_type));
20232 }
20233 else
20234 {
20235 qualified_name_lookup_error (scope, tmpl, member,
20236 input_location);
20237 RETURN (error_mark_node);
20238 }
20239 }
20240 else if (TREE_CODE (member) == SCOPE_REF
20241 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20242 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20243 {
20244 if (complain & tf_error)
20245 {
20246 if (TYPE_P (TREE_OPERAND (member, 0)))
20247 error ("%qT is not a class or namespace",
20248 TREE_OPERAND (member, 0));
20249 else
20250 error ("%qD is not a class or namespace",
20251 TREE_OPERAND (member, 0));
20252 }
20253 RETURN (error_mark_node);
20254 }
20255
20256 r = finish_class_member_access_expr (object, member,
20257 /*template_p=*/false,
20258 complain);
20259 if (TREE_CODE (r) == COMPONENT_REF)
20260 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20261 RETURN (r);
20262 }
20263
20264 case THROW_EXPR:
20265 RETURN (build_throw
20266 (input_location, RECUR (TREE_OPERAND (t, 0))));
20267
20268 case CONSTRUCTOR:
20269 {
20270 vec<constructor_elt, va_gc> *n;
20271 constructor_elt *ce;
20272 unsigned HOST_WIDE_INT idx;
20273 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20274 bool process_index_p;
20275 int newlen;
20276 bool need_copy_p = false;
20277 tree r;
20278
20279 if (type == error_mark_node)
20280 RETURN (error_mark_node);
20281
20282 /* We do not want to process the index of aggregate
20283 initializers as they are identifier nodes which will be
20284 looked up by digest_init. */
20285 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20286
20287 if (null_member_pointer_value_p (t))
20288 {
20289 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20290 RETURN (t);
20291 }
20292
20293 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20294 newlen = vec_safe_length (n);
20295 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20296 {
20297 if (ce->index && process_index_p
20298 /* An identifier index is looked up in the type
20299 being initialized, not the current scope. */
20300 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20301 ce->index = RECUR (ce->index);
20302
20303 if (PACK_EXPANSION_P (ce->value))
20304 {
20305 /* Substitute into the pack expansion. */
20306 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20307 in_decl);
20308
20309 if (ce->value == error_mark_node
20310 || PACK_EXPANSION_P (ce->value))
20311 ;
20312 else if (TREE_VEC_LENGTH (ce->value) == 1)
20313 /* Just move the argument into place. */
20314 ce->value = TREE_VEC_ELT (ce->value, 0);
20315 else
20316 {
20317 /* Update the length of the final CONSTRUCTOR
20318 arguments vector, and note that we will need to
20319 copy.*/
20320 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20321 need_copy_p = true;
20322 }
20323 }
20324 else
20325 ce->value = RECUR (ce->value);
20326 }
20327
20328 if (need_copy_p)
20329 {
20330 vec<constructor_elt, va_gc> *old_n = n;
20331
20332 vec_alloc (n, newlen);
20333 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20334 {
20335 if (TREE_CODE (ce->value) == TREE_VEC)
20336 {
20337 int i, len = TREE_VEC_LENGTH (ce->value);
20338 for (i = 0; i < len; ++i)
20339 CONSTRUCTOR_APPEND_ELT (n, 0,
20340 TREE_VEC_ELT (ce->value, i));
20341 }
20342 else
20343 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20344 }
20345 }
20346
20347 r = build_constructor (init_list_type_node, n);
20348 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20349 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20350 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20351
20352 if (TREE_HAS_CONSTRUCTOR (t))
20353 {
20354 fcl_t cl = fcl_functional;
20355 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20356 cl = fcl_c99;
20357 RETURN (finish_compound_literal (type, r, complain, cl));
20358 }
20359
20360 TREE_TYPE (r) = type;
20361 RETURN (r);
20362 }
20363
20364 case TYPEID_EXPR:
20365 {
20366 tree operand_0 = TREE_OPERAND (t, 0);
20367 if (TYPE_P (operand_0))
20368 {
20369 operand_0 = tsubst (operand_0, args, complain, in_decl);
20370 RETURN (get_typeid (operand_0, complain));
20371 }
20372 else
20373 {
20374 operand_0 = RECUR (operand_0);
20375 RETURN (build_typeid (operand_0, complain));
20376 }
20377 }
20378
20379 case VAR_DECL:
20380 if (!args)
20381 RETURN (t);
20382 /* Fall through */
20383
20384 case PARM_DECL:
20385 {
20386 tree r = tsubst_copy (t, args, complain, in_decl);
20387 /* ??? We're doing a subset of finish_id_expression here. */
20388 if (tree wrap = maybe_get_tls_wrapper_call (r))
20389 /* Replace an evaluated use of the thread_local variable with
20390 a call to its wrapper. */
20391 r = wrap;
20392 else if (outer_automatic_var_p (r))
20393 r = process_outer_var_ref (r, complain);
20394
20395 if (!TYPE_REF_P (TREE_TYPE (t)))
20396 /* If the original type was a reference, we'll be wrapped in
20397 the appropriate INDIRECT_REF. */
20398 r = convert_from_reference (r);
20399 RETURN (r);
20400 }
20401
20402 case VA_ARG_EXPR:
20403 {
20404 tree op0 = RECUR (TREE_OPERAND (t, 0));
20405 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20406 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20407 }
20408
20409 case OFFSETOF_EXPR:
20410 {
20411 tree object_ptr
20412 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20413 in_decl, /*function_p=*/false,
20414 /*integral_constant_expression_p=*/false);
20415 RETURN (finish_offsetof (object_ptr,
20416 RECUR (TREE_OPERAND (t, 0)),
20417 EXPR_LOCATION (t)));
20418 }
20419
20420 case ADDRESSOF_EXPR:
20421 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20422 RECUR (TREE_OPERAND (t, 0)), complain));
20423
20424 case TRAIT_EXPR:
20425 {
20426 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20427 complain, in_decl);
20428 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20429 complain, in_decl);
20430 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20431 TRAIT_EXPR_KIND (t), type1, type2));
20432 }
20433
20434 case STMT_EXPR:
20435 {
20436 tree old_stmt_expr = cur_stmt_expr;
20437 tree stmt_expr = begin_stmt_expr ();
20438
20439 cur_stmt_expr = stmt_expr;
20440 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20441 integral_constant_expression_p);
20442 stmt_expr = finish_stmt_expr (stmt_expr, false);
20443 cur_stmt_expr = old_stmt_expr;
20444
20445 /* If the resulting list of expression statement is empty,
20446 fold it further into void_node. */
20447 if (empty_expr_stmt_p (stmt_expr))
20448 stmt_expr = void_node;
20449
20450 RETURN (stmt_expr);
20451 }
20452
20453 case LAMBDA_EXPR:
20454 {
20455 if (complain & tf_partial)
20456 {
20457 /* We don't have a full set of template arguments yet; don't touch
20458 the lambda at all. */
20459 gcc_assert (processing_template_decl);
20460 return t;
20461 }
20462 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20463
20464 RETURN (build_lambda_object (r));
20465 }
20466
20467 case TARGET_EXPR:
20468 /* We can get here for a constant initializer of non-dependent type.
20469 FIXME stop folding in cp_parser_initializer_clause. */
20470 {
20471 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20472 complain);
20473 RETURN (r);
20474 }
20475
20476 case TRANSACTION_EXPR:
20477 RETURN (tsubst_expr(t, args, complain, in_decl,
20478 integral_constant_expression_p));
20479
20480 case PAREN_EXPR:
20481 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20482
20483 case VEC_PERM_EXPR:
20484 {
20485 tree op0 = RECUR (TREE_OPERAND (t, 0));
20486 tree op1 = RECUR (TREE_OPERAND (t, 1));
20487 tree op2 = RECUR (TREE_OPERAND (t, 2));
20488 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20489 complain));
20490 }
20491
20492 case REQUIRES_EXPR:
20493 {
20494 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20495 RETURN (r);
20496 }
20497
20498 case RANGE_EXPR:
20499 /* No need to substitute further, a RANGE_EXPR will always be built
20500 with constant operands. */
20501 RETURN (t);
20502
20503 case NON_LVALUE_EXPR:
20504 case VIEW_CONVERT_EXPR:
20505 if (location_wrapper_p (t))
20506 /* We need to do this here as well as in tsubst_copy so we get the
20507 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20508 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20509 EXPR_LOCATION (t)));
20510 /* fallthrough. */
20511
20512 default:
20513 /* Handle Objective-C++ constructs, if appropriate. */
20514 {
20515 tree subst
20516 = objcp_tsubst_copy_and_build (t, args, complain,
20517 in_decl, /*function_p=*/false);
20518 if (subst)
20519 RETURN (subst);
20520 }
20521 RETURN (tsubst_copy (t, args, complain, in_decl));
20522 }
20523
20524 #undef RECUR
20525 #undef RETURN
20526 out:
20527 input_location = save_loc;
20528 return retval;
20529 }
20530
20531 /* Verify that the instantiated ARGS are valid. For type arguments,
20532 make sure that the type's linkage is ok. For non-type arguments,
20533 make sure they are constants if they are integral or enumerations.
20534 Emit an error under control of COMPLAIN, and return TRUE on error. */
20535
20536 static bool
20537 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20538 {
20539 if (dependent_template_arg_p (t))
20540 return false;
20541 if (ARGUMENT_PACK_P (t))
20542 {
20543 tree vec = ARGUMENT_PACK_ARGS (t);
20544 int len = TREE_VEC_LENGTH (vec);
20545 bool result = false;
20546 int i;
20547
20548 for (i = 0; i < len; ++i)
20549 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20550 result = true;
20551 return result;
20552 }
20553 else if (TYPE_P (t))
20554 {
20555 /* [basic.link]: A name with no linkage (notably, the name
20556 of a class or enumeration declared in a local scope)
20557 shall not be used to declare an entity with linkage.
20558 This implies that names with no linkage cannot be used as
20559 template arguments
20560
20561 DR 757 relaxes this restriction for C++0x. */
20562 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20563 : no_linkage_check (t, /*relaxed_p=*/false));
20564
20565 if (nt)
20566 {
20567 /* DR 488 makes use of a type with no linkage cause
20568 type deduction to fail. */
20569 if (complain & tf_error)
20570 {
20571 if (TYPE_UNNAMED_P (nt))
20572 error ("%qT is/uses unnamed type", t);
20573 else
20574 error ("template argument for %qD uses local type %qT",
20575 tmpl, t);
20576 }
20577 return true;
20578 }
20579 /* In order to avoid all sorts of complications, we do not
20580 allow variably-modified types as template arguments. */
20581 else if (variably_modified_type_p (t, NULL_TREE))
20582 {
20583 if (complain & tf_error)
20584 error ("%qT is a variably modified type", t);
20585 return true;
20586 }
20587 }
20588 /* Class template and alias template arguments should be OK. */
20589 else if (DECL_TYPE_TEMPLATE_P (t))
20590 ;
20591 /* A non-type argument of integral or enumerated type must be a
20592 constant. */
20593 else if (TREE_TYPE (t)
20594 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20595 && !REFERENCE_REF_P (t)
20596 && !TREE_CONSTANT (t))
20597 {
20598 if (complain & tf_error)
20599 error ("integral expression %qE is not constant", t);
20600 return true;
20601 }
20602 return false;
20603 }
20604
20605 static bool
20606 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20607 {
20608 int ix, len = DECL_NTPARMS (tmpl);
20609 bool result = false;
20610
20611 for (ix = 0; ix != len; ix++)
20612 {
20613 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20614 result = true;
20615 }
20616 if (result && (complain & tf_error))
20617 error (" trying to instantiate %qD", tmpl);
20618 return result;
20619 }
20620
20621 /* We're out of SFINAE context now, so generate diagnostics for the access
20622 errors we saw earlier when instantiating D from TMPL and ARGS. */
20623
20624 static void
20625 recheck_decl_substitution (tree d, tree tmpl, tree args)
20626 {
20627 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20628 tree type = TREE_TYPE (pattern);
20629 location_t loc = input_location;
20630
20631 push_access_scope (d);
20632 push_deferring_access_checks (dk_no_deferred);
20633 input_location = DECL_SOURCE_LOCATION (pattern);
20634 tsubst (type, args, tf_warning_or_error, d);
20635 input_location = loc;
20636 pop_deferring_access_checks ();
20637 pop_access_scope (d);
20638 }
20639
20640 /* Instantiate the indicated variable, function, or alias template TMPL with
20641 the template arguments in TARG_PTR. */
20642
20643 static tree
20644 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20645 {
20646 tree targ_ptr = orig_args;
20647 tree fndecl;
20648 tree gen_tmpl;
20649 tree spec;
20650 bool access_ok = true;
20651
20652 if (tmpl == error_mark_node)
20653 return error_mark_node;
20654
20655 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20656
20657 /* If this function is a clone, handle it specially. */
20658 if (DECL_CLONED_FUNCTION_P (tmpl))
20659 {
20660 tree spec;
20661 tree clone;
20662
20663 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20664 DECL_CLONED_FUNCTION. */
20665 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20666 targ_ptr, complain);
20667 if (spec == error_mark_node)
20668 return error_mark_node;
20669
20670 /* Look for the clone. */
20671 FOR_EACH_CLONE (clone, spec)
20672 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20673 return clone;
20674 /* We should always have found the clone by now. */
20675 gcc_unreachable ();
20676 return NULL_TREE;
20677 }
20678
20679 if (targ_ptr == error_mark_node)
20680 return error_mark_node;
20681
20682 /* Check to see if we already have this specialization. */
20683 gen_tmpl = most_general_template (tmpl);
20684 if (TMPL_ARGS_DEPTH (targ_ptr)
20685 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20686 /* targ_ptr only has the innermost template args, so add the outer ones
20687 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20688 the case of a non-dependent call within a template definition). */
20689 targ_ptr = (add_outermost_template_args
20690 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20691 targ_ptr));
20692
20693 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20694 but it doesn't seem to be on the hot path. */
20695 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20696
20697 gcc_checking_assert (tmpl == gen_tmpl
20698 || ((fndecl
20699 = retrieve_specialization (tmpl, orig_args, 0))
20700 == spec)
20701 || fndecl == NULL_TREE);
20702
20703 if (spec != NULL_TREE)
20704 {
20705 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20706 {
20707 if (complain & tf_error)
20708 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20709 return error_mark_node;
20710 }
20711 return spec;
20712 }
20713
20714 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20715 complain))
20716 return error_mark_node;
20717
20718 /* We are building a FUNCTION_DECL, during which the access of its
20719 parameters and return types have to be checked. However this
20720 FUNCTION_DECL which is the desired context for access checking
20721 is not built yet. We solve this chicken-and-egg problem by
20722 deferring all checks until we have the FUNCTION_DECL. */
20723 push_deferring_access_checks (dk_deferred);
20724
20725 /* Instantiation of the function happens in the context of the function
20726 template, not the context of the overload resolution we're doing. */
20727 push_to_top_level ();
20728 /* If there are dependent arguments, e.g. because we're doing partial
20729 ordering, make sure processing_template_decl stays set. */
20730 if (uses_template_parms (targ_ptr))
20731 ++processing_template_decl;
20732 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20733 {
20734 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20735 complain, gen_tmpl, true);
20736 push_nested_class (ctx);
20737 }
20738
20739 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20740
20741 fndecl = NULL_TREE;
20742 if (VAR_P (pattern))
20743 {
20744 /* We need to determine if we're using a partial or explicit
20745 specialization now, because the type of the variable could be
20746 different. */
20747 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20748 tree elt = most_specialized_partial_spec (tid, complain);
20749 if (elt == error_mark_node)
20750 pattern = error_mark_node;
20751 else if (elt)
20752 {
20753 tree partial_tmpl = TREE_VALUE (elt);
20754 tree partial_args = TREE_PURPOSE (elt);
20755 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20756 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20757 }
20758 }
20759
20760 /* Substitute template parameters to obtain the specialization. */
20761 if (fndecl == NULL_TREE)
20762 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20763 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20764 pop_nested_class ();
20765 pop_from_top_level ();
20766
20767 if (fndecl == error_mark_node)
20768 {
20769 pop_deferring_access_checks ();
20770 return error_mark_node;
20771 }
20772
20773 /* The DECL_TI_TEMPLATE should always be the immediate parent
20774 template, not the most general template. */
20775 DECL_TI_TEMPLATE (fndecl) = tmpl;
20776 DECL_TI_ARGS (fndecl) = targ_ptr;
20777
20778 /* Now we know the specialization, compute access previously
20779 deferred. Do no access control for inheriting constructors,
20780 as we already checked access for the inherited constructor. */
20781 if (!(flag_new_inheriting_ctors
20782 && DECL_INHERITED_CTOR (fndecl)))
20783 {
20784 push_access_scope (fndecl);
20785 if (!perform_deferred_access_checks (complain))
20786 access_ok = false;
20787 pop_access_scope (fndecl);
20788 }
20789 pop_deferring_access_checks ();
20790
20791 /* If we've just instantiated the main entry point for a function,
20792 instantiate all the alternate entry points as well. We do this
20793 by cloning the instantiation of the main entry point, not by
20794 instantiating the template clones. */
20795 if (tree chain = DECL_CHAIN (gen_tmpl))
20796 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20797 clone_cdtor (fndecl, /*update_methods=*/false);
20798
20799 if (!access_ok)
20800 {
20801 if (!(complain & tf_error))
20802 {
20803 /* Remember to reinstantiate when we're out of SFINAE so the user
20804 can see the errors. */
20805 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20806 }
20807 return error_mark_node;
20808 }
20809 return fndecl;
20810 }
20811
20812 /* Wrapper for instantiate_template_1. */
20813
20814 tree
20815 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20816 {
20817 tree ret;
20818 timevar_push (TV_TEMPLATE_INST);
20819 ret = instantiate_template_1 (tmpl, orig_args, complain);
20820 timevar_pop (TV_TEMPLATE_INST);
20821 return ret;
20822 }
20823
20824 /* Instantiate the alias template TMPL with ARGS. Also push a template
20825 instantiation level, which instantiate_template doesn't do because
20826 functions and variables have sufficient context established by the
20827 callers. */
20828
20829 static tree
20830 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20831 {
20832 if (tmpl == error_mark_node || args == error_mark_node)
20833 return error_mark_node;
20834
20835 args =
20836 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20837 args, tmpl, complain,
20838 /*require_all_args=*/true,
20839 /*use_default_args=*/true);
20840
20841 /* FIXME check for satisfaction in check_instantiated_args. */
20842 if (flag_concepts
20843 && !any_dependent_template_arguments_p (args)
20844 && !constraints_satisfied_p (tmpl, args))
20845 {
20846 if (complain & tf_error)
20847 {
20848 auto_diagnostic_group d;
20849 error ("template constraint failure for %qD", tmpl);
20850 diagnose_constraints (input_location, tmpl, args);
20851 }
20852 return error_mark_node;
20853 }
20854
20855 if (!push_tinst_level (tmpl, args))
20856 return error_mark_node;
20857 tree r = instantiate_template (tmpl, args, complain);
20858 pop_tinst_level ();
20859
20860 return r;
20861 }
20862
20863 /* PARM is a template parameter pack for FN. Returns true iff
20864 PARM is used in a deducible way in the argument list of FN. */
20865
20866 static bool
20867 pack_deducible_p (tree parm, tree fn)
20868 {
20869 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20870 for (; t; t = TREE_CHAIN (t))
20871 {
20872 tree type = TREE_VALUE (t);
20873 tree packs;
20874 if (!PACK_EXPANSION_P (type))
20875 continue;
20876 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20877 packs; packs = TREE_CHAIN (packs))
20878 if (template_args_equal (TREE_VALUE (packs), parm))
20879 {
20880 /* The template parameter pack is used in a function parameter
20881 pack. If this is the end of the parameter list, the
20882 template parameter pack is deducible. */
20883 if (TREE_CHAIN (t) == void_list_node)
20884 return true;
20885 else
20886 /* Otherwise, not. Well, it could be deduced from
20887 a non-pack parameter, but doing so would end up with
20888 a deduction mismatch, so don't bother. */
20889 return false;
20890 }
20891 }
20892 /* The template parameter pack isn't used in any function parameter
20893 packs, but it might be used deeper, e.g. tuple<Args...>. */
20894 return true;
20895 }
20896
20897 /* Subroutine of fn_type_unification: check non-dependent parms for
20898 convertibility. */
20899
20900 static int
20901 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20902 tree fn, unification_kind_t strict, int flags,
20903 struct conversion **convs, bool explain_p)
20904 {
20905 /* Non-constructor methods need to leave a conversion for 'this', which
20906 isn't included in nargs here. */
20907 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20908 && !DECL_CONSTRUCTOR_P (fn));
20909
20910 for (unsigned ia = 0;
20911 parms && parms != void_list_node && ia < nargs; )
20912 {
20913 tree parm = TREE_VALUE (parms);
20914
20915 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20916 && (!TREE_CHAIN (parms)
20917 || TREE_CHAIN (parms) == void_list_node))
20918 /* For a function parameter pack that occurs at the end of the
20919 parameter-declaration-list, the type A of each remaining
20920 argument of the call is compared with the type P of the
20921 declarator-id of the function parameter pack. */
20922 break;
20923
20924 parms = TREE_CHAIN (parms);
20925
20926 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20927 /* For a function parameter pack that does not occur at the
20928 end of the parameter-declaration-list, the type of the
20929 parameter pack is a non-deduced context. */
20930 continue;
20931
20932 if (!uses_template_parms (parm))
20933 {
20934 tree arg = args[ia];
20935 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20936 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20937
20938 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20939 conv_p, explain_p))
20940 return 1;
20941 }
20942
20943 ++ia;
20944 }
20945
20946 return 0;
20947 }
20948
20949 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20950 NARGS elements of the arguments that are being used when calling
20951 it. TARGS is a vector into which the deduced template arguments
20952 are placed.
20953
20954 Returns either a FUNCTION_DECL for the matching specialization of FN or
20955 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20956 true, diagnostics will be printed to explain why it failed.
20957
20958 If FN is a conversion operator, or we are trying to produce a specific
20959 specialization, RETURN_TYPE is the return type desired.
20960
20961 The EXPLICIT_TARGS are explicit template arguments provided via a
20962 template-id.
20963
20964 The parameter STRICT is one of:
20965
20966 DEDUCE_CALL:
20967 We are deducing arguments for a function call, as in
20968 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20969 deducing arguments for a call to the result of a conversion
20970 function template, as in [over.call.object].
20971
20972 DEDUCE_CONV:
20973 We are deducing arguments for a conversion function, as in
20974 [temp.deduct.conv].
20975
20976 DEDUCE_EXACT:
20977 We are deducing arguments when doing an explicit instantiation
20978 as in [temp.explicit], when determining an explicit specialization
20979 as in [temp.expl.spec], or when taking the address of a function
20980 template, as in [temp.deduct.funcaddr]. */
20981
20982 tree
20983 fn_type_unification (tree fn,
20984 tree explicit_targs,
20985 tree targs,
20986 const tree *args,
20987 unsigned int nargs,
20988 tree return_type,
20989 unification_kind_t strict,
20990 int flags,
20991 struct conversion **convs,
20992 bool explain_p,
20993 bool decltype_p)
20994 {
20995 tree parms;
20996 tree fntype;
20997 tree decl = NULL_TREE;
20998 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20999 bool ok;
21000 static int deduction_depth;
21001 /* type_unification_real will pass back any access checks from default
21002 template argument substitution. */
21003 vec<deferred_access_check, va_gc> *checks = NULL;
21004 /* We don't have all the template args yet. */
21005 bool incomplete = true;
21006
21007 tree orig_fn = fn;
21008 if (flag_new_inheriting_ctors)
21009 fn = strip_inheriting_ctors (fn);
21010
21011 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21012 tree r = error_mark_node;
21013
21014 tree full_targs = targs;
21015 if (TMPL_ARGS_DEPTH (targs)
21016 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21017 full_targs = (add_outermost_template_args
21018 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21019 targs));
21020
21021 if (decltype_p)
21022 complain |= tf_decltype;
21023
21024 /* In C++0x, it's possible to have a function template whose type depends
21025 on itself recursively. This is most obvious with decltype, but can also
21026 occur with enumeration scope (c++/48969). So we need to catch infinite
21027 recursion and reject the substitution at deduction time; this function
21028 will return error_mark_node for any repeated substitution.
21029
21030 This also catches excessive recursion such as when f<N> depends on
21031 f<N-1> across all integers, and returns error_mark_node for all the
21032 substitutions back up to the initial one.
21033
21034 This is, of course, not reentrant. */
21035 if (excessive_deduction_depth)
21036 return error_mark_node;
21037 ++deduction_depth;
21038
21039 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21040
21041 fntype = TREE_TYPE (fn);
21042 if (explicit_targs)
21043 {
21044 /* [temp.deduct]
21045
21046 The specified template arguments must match the template
21047 parameters in kind (i.e., type, nontype, template), and there
21048 must not be more arguments than there are parameters;
21049 otherwise type deduction fails.
21050
21051 Nontype arguments must match the types of the corresponding
21052 nontype template parameters, or must be convertible to the
21053 types of the corresponding nontype parameters as specified in
21054 _temp.arg.nontype_, otherwise type deduction fails.
21055
21056 All references in the function type of the function template
21057 to the corresponding template parameters are replaced by the
21058 specified template argument values. If a substitution in a
21059 template parameter or in the function type of the function
21060 template results in an invalid type, type deduction fails. */
21061 int i, len = TREE_VEC_LENGTH (tparms);
21062 location_t loc = input_location;
21063 incomplete = false;
21064
21065 if (explicit_targs == error_mark_node)
21066 goto fail;
21067
21068 if (TMPL_ARGS_DEPTH (explicit_targs)
21069 < TMPL_ARGS_DEPTH (full_targs))
21070 explicit_targs = add_outermost_template_args (full_targs,
21071 explicit_targs);
21072
21073 /* Adjust any explicit template arguments before entering the
21074 substitution context. */
21075 explicit_targs
21076 = (coerce_template_parms (tparms, explicit_targs, fn,
21077 complain|tf_partial,
21078 /*require_all_args=*/false,
21079 /*use_default_args=*/false));
21080 if (explicit_targs == error_mark_node)
21081 goto fail;
21082
21083 /* Substitute the explicit args into the function type. This is
21084 necessary so that, for instance, explicitly declared function
21085 arguments can match null pointed constants. If we were given
21086 an incomplete set of explicit args, we must not do semantic
21087 processing during substitution as we could create partial
21088 instantiations. */
21089 for (i = 0; i < len; i++)
21090 {
21091 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21092 bool parameter_pack = false;
21093 tree targ = TREE_VEC_ELT (explicit_targs, i);
21094
21095 /* Dig out the actual parm. */
21096 if (TREE_CODE (parm) == TYPE_DECL
21097 || TREE_CODE (parm) == TEMPLATE_DECL)
21098 {
21099 parm = TREE_TYPE (parm);
21100 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21101 }
21102 else if (TREE_CODE (parm) == PARM_DECL)
21103 {
21104 parm = DECL_INITIAL (parm);
21105 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21106 }
21107
21108 if (targ == NULL_TREE)
21109 /* No explicit argument for this template parameter. */
21110 incomplete = true;
21111 else if (parameter_pack && pack_deducible_p (parm, fn))
21112 {
21113 /* Mark the argument pack as "incomplete". We could
21114 still deduce more arguments during unification.
21115 We remove this mark in type_unification_real. */
21116 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21117 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21118 = ARGUMENT_PACK_ARGS (targ);
21119
21120 /* We have some incomplete argument packs. */
21121 incomplete = true;
21122 }
21123 }
21124
21125 if (incomplete)
21126 {
21127 if (!push_tinst_level (fn, explicit_targs))
21128 {
21129 excessive_deduction_depth = true;
21130 goto fail;
21131 }
21132 ++processing_template_decl;
21133 input_location = DECL_SOURCE_LOCATION (fn);
21134 /* Ignore any access checks; we'll see them again in
21135 instantiate_template and they might have the wrong
21136 access path at this point. */
21137 push_deferring_access_checks (dk_deferred);
21138 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21139 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21140 pop_deferring_access_checks ();
21141 input_location = loc;
21142 --processing_template_decl;
21143 pop_tinst_level ();
21144
21145 if (fntype == error_mark_node)
21146 goto fail;
21147 }
21148
21149 /* Place the explicitly specified arguments in TARGS. */
21150 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21151 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21152 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21153 if (!incomplete && CHECKING_P
21154 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21155 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21156 (targs, NUM_TMPL_ARGS (explicit_targs));
21157 }
21158
21159 if (return_type && strict != DEDUCE_CALL)
21160 {
21161 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21162 new_args[0] = return_type;
21163 memcpy (new_args + 1, args, nargs * sizeof (tree));
21164 args = new_args;
21165 ++nargs;
21166 }
21167
21168 if (!incomplete)
21169 goto deduced;
21170
21171 /* Never do unification on the 'this' parameter. */
21172 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21173
21174 if (return_type && strict == DEDUCE_CALL)
21175 {
21176 /* We're deducing for a call to the result of a template conversion
21177 function. The parms we really want are in return_type. */
21178 if (INDIRECT_TYPE_P (return_type))
21179 return_type = TREE_TYPE (return_type);
21180 parms = TYPE_ARG_TYPES (return_type);
21181 }
21182 else if (return_type)
21183 {
21184 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21185 }
21186
21187 /* We allow incomplete unification without an error message here
21188 because the standard doesn't seem to explicitly prohibit it. Our
21189 callers must be ready to deal with unification failures in any
21190 event. */
21191
21192 /* If we aren't explaining yet, push tinst context so we can see where
21193 any errors (e.g. from class instantiations triggered by instantiation
21194 of default template arguments) come from. If we are explaining, this
21195 context is redundant. */
21196 if (!explain_p && !push_tinst_level (fn, targs))
21197 {
21198 excessive_deduction_depth = true;
21199 goto fail;
21200 }
21201
21202 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21203 full_targs, parms, args, nargs, /*subr=*/0,
21204 strict, &checks, explain_p);
21205 if (!explain_p)
21206 pop_tinst_level ();
21207 if (!ok)
21208 goto fail;
21209
21210 /* Now that we have bindings for all of the template arguments,
21211 ensure that the arguments deduced for the template template
21212 parameters have compatible template parameter lists. We cannot
21213 check this property before we have deduced all template
21214 arguments, because the template parameter types of a template
21215 template parameter might depend on prior template parameters
21216 deduced after the template template parameter. The following
21217 ill-formed example illustrates this issue:
21218
21219 template<typename T, template<T> class C> void f(C<5>, T);
21220
21221 template<int N> struct X {};
21222
21223 void g() {
21224 f(X<5>(), 5l); // error: template argument deduction fails
21225 }
21226
21227 The template parameter list of 'C' depends on the template type
21228 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21229 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21230 time that we deduce 'C'. */
21231 if (!template_template_parm_bindings_ok_p
21232 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21233 {
21234 unify_inconsistent_template_template_parameters (explain_p);
21235 goto fail;
21236 }
21237
21238 /* DR 1391: All parameters have args, now check non-dependent parms for
21239 convertibility. */
21240 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21241 convs, explain_p))
21242 goto fail;
21243
21244 deduced:
21245 /* All is well so far. Now, check:
21246
21247 [temp.deduct]
21248
21249 When all template arguments have been deduced, all uses of
21250 template parameters in nondeduced contexts are replaced with
21251 the corresponding deduced argument values. If the
21252 substitution results in an invalid type, as described above,
21253 type deduction fails. */
21254 if (!push_tinst_level (fn, targs))
21255 {
21256 excessive_deduction_depth = true;
21257 goto fail;
21258 }
21259
21260 /* Also collect access checks from the instantiation. */
21261 reopen_deferring_access_checks (checks);
21262
21263 decl = instantiate_template (fn, targs, complain);
21264
21265 checks = get_deferred_access_checks ();
21266 pop_deferring_access_checks ();
21267
21268 pop_tinst_level ();
21269
21270 if (decl == error_mark_node)
21271 goto fail;
21272
21273 /* Now perform any access checks encountered during substitution. */
21274 push_access_scope (decl);
21275 ok = perform_access_checks (checks, complain);
21276 pop_access_scope (decl);
21277 if (!ok)
21278 goto fail;
21279
21280 /* If we're looking for an exact match, check that what we got
21281 is indeed an exact match. It might not be if some template
21282 parameters are used in non-deduced contexts. But don't check
21283 for an exact match if we have dependent template arguments;
21284 in that case we're doing partial ordering, and we already know
21285 that we have two candidates that will provide the actual type. */
21286 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21287 {
21288 tree substed = TREE_TYPE (decl);
21289 unsigned int i;
21290
21291 tree sarg
21292 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21293 if (return_type)
21294 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21295 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21296 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21297 {
21298 unify_type_mismatch (explain_p, args[i],
21299 TREE_VALUE (sarg));
21300 goto fail;
21301 }
21302 }
21303
21304 /* After doing deduction with the inherited constructor, actually return an
21305 instantiation of the inheriting constructor. */
21306 if (orig_fn != fn)
21307 decl = instantiate_template (orig_fn, targs, complain);
21308
21309 r = decl;
21310
21311 fail:
21312 --deduction_depth;
21313 if (excessive_deduction_depth)
21314 {
21315 if (deduction_depth == 0)
21316 /* Reset once we're all the way out. */
21317 excessive_deduction_depth = false;
21318 }
21319
21320 return r;
21321 }
21322
21323 /* Adjust types before performing type deduction, as described in
21324 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21325 sections are symmetric. PARM is the type of a function parameter
21326 or the return type of the conversion function. ARG is the type of
21327 the argument passed to the call, or the type of the value
21328 initialized with the result of the conversion function.
21329 ARG_EXPR is the original argument expression, which may be null. */
21330
21331 static int
21332 maybe_adjust_types_for_deduction (unification_kind_t strict,
21333 tree* parm,
21334 tree* arg,
21335 tree arg_expr)
21336 {
21337 int result = 0;
21338
21339 switch (strict)
21340 {
21341 case DEDUCE_CALL:
21342 break;
21343
21344 case DEDUCE_CONV:
21345 /* Swap PARM and ARG throughout the remainder of this
21346 function; the handling is precisely symmetric since PARM
21347 will initialize ARG rather than vice versa. */
21348 std::swap (parm, arg);
21349 break;
21350
21351 case DEDUCE_EXACT:
21352 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21353 too, but here handle it by stripping the reference from PARM
21354 rather than by adding it to ARG. */
21355 if (TYPE_REF_P (*parm)
21356 && TYPE_REF_IS_RVALUE (*parm)
21357 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21358 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21359 && TYPE_REF_P (*arg)
21360 && !TYPE_REF_IS_RVALUE (*arg))
21361 *parm = TREE_TYPE (*parm);
21362 /* Nothing else to do in this case. */
21363 return 0;
21364
21365 default:
21366 gcc_unreachable ();
21367 }
21368
21369 if (!TYPE_REF_P (*parm))
21370 {
21371 /* [temp.deduct.call]
21372
21373 If P is not a reference type:
21374
21375 --If A is an array type, the pointer type produced by the
21376 array-to-pointer standard conversion (_conv.array_) is
21377 used in place of A for type deduction; otherwise,
21378
21379 --If A is a function type, the pointer type produced by
21380 the function-to-pointer standard conversion
21381 (_conv.func_) is used in place of A for type deduction;
21382 otherwise,
21383
21384 --If A is a cv-qualified type, the top level
21385 cv-qualifiers of A's type are ignored for type
21386 deduction. */
21387 if (TREE_CODE (*arg) == ARRAY_TYPE)
21388 *arg = build_pointer_type (TREE_TYPE (*arg));
21389 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21390 *arg = build_pointer_type (*arg);
21391 else
21392 *arg = TYPE_MAIN_VARIANT (*arg);
21393 }
21394
21395 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21396 reference to a cv-unqualified template parameter that does not represent a
21397 template parameter of a class template (during class template argument
21398 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21399 an lvalue, the type "lvalue reference to A" is used in place of A for type
21400 deduction. */
21401 if (TYPE_REF_P (*parm)
21402 && TYPE_REF_IS_RVALUE (*parm)
21403 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21404 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21405 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21406 && (arg_expr ? lvalue_p (arg_expr)
21407 /* try_one_overload doesn't provide an arg_expr, but
21408 functions are always lvalues. */
21409 : TREE_CODE (*arg) == FUNCTION_TYPE))
21410 *arg = build_reference_type (*arg);
21411
21412 /* [temp.deduct.call]
21413
21414 If P is a cv-qualified type, the top level cv-qualifiers
21415 of P's type are ignored for type deduction. If P is a
21416 reference type, the type referred to by P is used for
21417 type deduction. */
21418 *parm = TYPE_MAIN_VARIANT (*parm);
21419 if (TYPE_REF_P (*parm))
21420 {
21421 *parm = TREE_TYPE (*parm);
21422 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21423 }
21424
21425 /* DR 322. For conversion deduction, remove a reference type on parm
21426 too (which has been swapped into ARG). */
21427 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21428 *arg = TREE_TYPE (*arg);
21429
21430 return result;
21431 }
21432
21433 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21434 template which doesn't contain any deducible template parameters; check if
21435 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21436 unify_one_argument. */
21437
21438 static int
21439 check_non_deducible_conversion (tree parm, tree arg, int strict,
21440 int flags, struct conversion **conv_p,
21441 bool explain_p)
21442 {
21443 tree type;
21444
21445 if (!TYPE_P (arg))
21446 type = TREE_TYPE (arg);
21447 else
21448 type = arg;
21449
21450 if (same_type_p (parm, type))
21451 return unify_success (explain_p);
21452
21453 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21454 if (strict == DEDUCE_CONV)
21455 {
21456 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21457 return unify_success (explain_p);
21458 }
21459 else if (strict != DEDUCE_EXACT)
21460 {
21461 bool ok = false;
21462 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21463 if (conv_p)
21464 /* Avoid recalculating this in add_function_candidate. */
21465 ok = (*conv_p
21466 = good_conversion (parm, type, conv_arg, flags, complain));
21467 else
21468 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21469 if (ok)
21470 return unify_success (explain_p);
21471 }
21472
21473 if (strict == DEDUCE_EXACT)
21474 return unify_type_mismatch (explain_p, parm, arg);
21475 else
21476 return unify_arg_conversion (explain_p, parm, type, arg);
21477 }
21478
21479 static bool uses_deducible_template_parms (tree type);
21480
21481 /* Returns true iff the expression EXPR is one from which a template
21482 argument can be deduced. In other words, if it's an undecorated
21483 use of a template non-type parameter. */
21484
21485 static bool
21486 deducible_expression (tree expr)
21487 {
21488 /* Strip implicit conversions. */
21489 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21490 expr = TREE_OPERAND (expr, 0);
21491 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21492 }
21493
21494 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21495 deducible way; that is, if it has a max value of <PARM> - 1. */
21496
21497 static bool
21498 deducible_array_bound (tree domain)
21499 {
21500 if (domain == NULL_TREE)
21501 return false;
21502
21503 tree max = TYPE_MAX_VALUE (domain);
21504 if (TREE_CODE (max) != MINUS_EXPR)
21505 return false;
21506
21507 return deducible_expression (TREE_OPERAND (max, 0));
21508 }
21509
21510 /* Returns true iff the template arguments ARGS use a template parameter
21511 in a deducible way. */
21512
21513 static bool
21514 deducible_template_args (tree args)
21515 {
21516 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21517 {
21518 bool deducible;
21519 tree elt = TREE_VEC_ELT (args, i);
21520 if (ARGUMENT_PACK_P (elt))
21521 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21522 else
21523 {
21524 if (PACK_EXPANSION_P (elt))
21525 elt = PACK_EXPANSION_PATTERN (elt);
21526 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21527 deducible = true;
21528 else if (TYPE_P (elt))
21529 deducible = uses_deducible_template_parms (elt);
21530 else
21531 deducible = deducible_expression (elt);
21532 }
21533 if (deducible)
21534 return true;
21535 }
21536 return false;
21537 }
21538
21539 /* Returns true iff TYPE contains any deducible references to template
21540 parameters, as per 14.8.2.5. */
21541
21542 static bool
21543 uses_deducible_template_parms (tree type)
21544 {
21545 if (PACK_EXPANSION_P (type))
21546 type = PACK_EXPANSION_PATTERN (type);
21547
21548 /* T
21549 cv-list T
21550 TT<T>
21551 TT<i>
21552 TT<> */
21553 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21554 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21555 return true;
21556
21557 /* T*
21558 T&
21559 T&& */
21560 if (INDIRECT_TYPE_P (type))
21561 return uses_deducible_template_parms (TREE_TYPE (type));
21562
21563 /* T[integer-constant ]
21564 type [i] */
21565 if (TREE_CODE (type) == ARRAY_TYPE)
21566 return (uses_deducible_template_parms (TREE_TYPE (type))
21567 || deducible_array_bound (TYPE_DOMAIN (type)));
21568
21569 /* T type ::*
21570 type T::*
21571 T T::*
21572 T (type ::*)()
21573 type (T::*)()
21574 type (type ::*)(T)
21575 type (T::*)(T)
21576 T (type ::*)(T)
21577 T (T::*)()
21578 T (T::*)(T) */
21579 if (TYPE_PTRMEM_P (type))
21580 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21581 || (uses_deducible_template_parms
21582 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21583
21584 /* template-name <T> (where template-name refers to a class template)
21585 template-name <i> (where template-name refers to a class template) */
21586 if (CLASS_TYPE_P (type)
21587 && CLASSTYPE_TEMPLATE_INFO (type)
21588 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21589 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21590 (CLASSTYPE_TI_ARGS (type)));
21591
21592 /* type (T)
21593 T()
21594 T(T) */
21595 if (FUNC_OR_METHOD_TYPE_P (type))
21596 {
21597 if (uses_deducible_template_parms (TREE_TYPE (type)))
21598 return true;
21599 tree parm = TYPE_ARG_TYPES (type);
21600 if (TREE_CODE (type) == METHOD_TYPE)
21601 parm = TREE_CHAIN (parm);
21602 for (; parm; parm = TREE_CHAIN (parm))
21603 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21604 return true;
21605 }
21606
21607 return false;
21608 }
21609
21610 /* Subroutine of type_unification_real and unify_pack_expansion to
21611 handle unification of a single P/A pair. Parameters are as
21612 for those functions. */
21613
21614 static int
21615 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21616 int subr, unification_kind_t strict,
21617 bool explain_p)
21618 {
21619 tree arg_expr = NULL_TREE;
21620 int arg_strict;
21621
21622 if (arg == error_mark_node || parm == error_mark_node)
21623 return unify_invalid (explain_p);
21624 if (arg == unknown_type_node)
21625 /* We can't deduce anything from this, but we might get all the
21626 template args from other function args. */
21627 return unify_success (explain_p);
21628
21629 /* Implicit conversions (Clause 4) will be performed on a function
21630 argument to convert it to the type of the corresponding function
21631 parameter if the parameter type contains no template-parameters that
21632 participate in template argument deduction. */
21633 if (strict != DEDUCE_EXACT
21634 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21635 /* For function parameters with no deducible template parameters,
21636 just return. We'll check non-dependent conversions later. */
21637 return unify_success (explain_p);
21638
21639 switch (strict)
21640 {
21641 case DEDUCE_CALL:
21642 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21643 | UNIFY_ALLOW_MORE_CV_QUAL
21644 | UNIFY_ALLOW_DERIVED);
21645 break;
21646
21647 case DEDUCE_CONV:
21648 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21649 break;
21650
21651 case DEDUCE_EXACT:
21652 arg_strict = UNIFY_ALLOW_NONE;
21653 break;
21654
21655 default:
21656 gcc_unreachable ();
21657 }
21658
21659 /* We only do these transformations if this is the top-level
21660 parameter_type_list in a call or declaration matching; in other
21661 situations (nested function declarators, template argument lists) we
21662 won't be comparing a type to an expression, and we don't do any type
21663 adjustments. */
21664 if (!subr)
21665 {
21666 if (!TYPE_P (arg))
21667 {
21668 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21669 if (type_unknown_p (arg))
21670 {
21671 /* [temp.deduct.type] A template-argument can be
21672 deduced from a pointer to function or pointer
21673 to member function argument if the set of
21674 overloaded functions does not contain function
21675 templates and at most one of a set of
21676 overloaded functions provides a unique
21677 match. */
21678 resolve_overloaded_unification (tparms, targs, parm,
21679 arg, strict,
21680 arg_strict, explain_p);
21681 /* If a unique match was not found, this is a
21682 non-deduced context, so we still succeed. */
21683 return unify_success (explain_p);
21684 }
21685
21686 arg_expr = arg;
21687 arg = unlowered_expr_type (arg);
21688 if (arg == error_mark_node)
21689 return unify_invalid (explain_p);
21690 }
21691
21692 arg_strict |=
21693 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21694 }
21695 else
21696 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21697 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21698 return unify_template_argument_mismatch (explain_p, parm, arg);
21699
21700 /* For deduction from an init-list we need the actual list. */
21701 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21702 arg = arg_expr;
21703 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21704 }
21705
21706 /* for_each_template_parm callback that always returns 0. */
21707
21708 static int
21709 zero_r (tree, void *)
21710 {
21711 return 0;
21712 }
21713
21714 /* for_each_template_parm any_fn callback to handle deduction of a template
21715 type argument from the type of an array bound. */
21716
21717 static int
21718 array_deduction_r (tree t, void *data)
21719 {
21720 tree_pair_p d = (tree_pair_p)data;
21721 tree &tparms = d->purpose;
21722 tree &targs = d->value;
21723
21724 if (TREE_CODE (t) == ARRAY_TYPE)
21725 if (tree dom = TYPE_DOMAIN (t))
21726 if (tree max = TYPE_MAX_VALUE (dom))
21727 {
21728 if (TREE_CODE (max) == MINUS_EXPR)
21729 max = TREE_OPERAND (max, 0);
21730 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21731 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21732 UNIFY_ALLOW_NONE, /*explain*/false);
21733 }
21734
21735 /* Keep walking. */
21736 return 0;
21737 }
21738
21739 /* Try to deduce any not-yet-deduced template type arguments from the type of
21740 an array bound. This is handled separately from unify because 14.8.2.5 says
21741 "The type of a type parameter is only deduced from an array bound if it is
21742 not otherwise deduced." */
21743
21744 static void
21745 try_array_deduction (tree tparms, tree targs, tree parm)
21746 {
21747 tree_pair_s data = { tparms, targs };
21748 hash_set<tree> visited;
21749 for_each_template_parm (parm, zero_r, &data, &visited,
21750 /*nondeduced*/false, array_deduction_r);
21751 }
21752
21753 /* Most parms like fn_type_unification.
21754
21755 If SUBR is 1, we're being called recursively (to unify the
21756 arguments of a function or method parameter of a function
21757 template).
21758
21759 CHECKS is a pointer to a vector of access checks encountered while
21760 substituting default template arguments. */
21761
21762 static int
21763 type_unification_real (tree tparms,
21764 tree full_targs,
21765 tree xparms,
21766 const tree *xargs,
21767 unsigned int xnargs,
21768 int subr,
21769 unification_kind_t strict,
21770 vec<deferred_access_check, va_gc> **checks,
21771 bool explain_p)
21772 {
21773 tree parm, arg;
21774 int i;
21775 int ntparms = TREE_VEC_LENGTH (tparms);
21776 int saw_undeduced = 0;
21777 tree parms;
21778 const tree *args;
21779 unsigned int nargs;
21780 unsigned int ia;
21781
21782 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21783 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21784 gcc_assert (ntparms > 0);
21785
21786 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21787
21788 /* Reset the number of non-defaulted template arguments contained
21789 in TARGS. */
21790 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21791
21792 again:
21793 parms = xparms;
21794 args = xargs;
21795 nargs = xnargs;
21796
21797 ia = 0;
21798 while (parms && parms != void_list_node
21799 && ia < nargs)
21800 {
21801 parm = TREE_VALUE (parms);
21802
21803 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21804 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21805 /* For a function parameter pack that occurs at the end of the
21806 parameter-declaration-list, the type A of each remaining
21807 argument of the call is compared with the type P of the
21808 declarator-id of the function parameter pack. */
21809 break;
21810
21811 parms = TREE_CHAIN (parms);
21812
21813 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21814 /* For a function parameter pack that does not occur at the
21815 end of the parameter-declaration-list, the type of the
21816 parameter pack is a non-deduced context. */
21817 continue;
21818
21819 arg = args[ia];
21820 ++ia;
21821
21822 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21823 explain_p))
21824 return 1;
21825 }
21826
21827 if (parms
21828 && parms != void_list_node
21829 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21830 {
21831 /* Unify the remaining arguments with the pack expansion type. */
21832 tree argvec;
21833 tree parmvec = make_tree_vec (1);
21834
21835 /* Allocate a TREE_VEC and copy in all of the arguments */
21836 argvec = make_tree_vec (nargs - ia);
21837 for (i = 0; ia < nargs; ++ia, ++i)
21838 TREE_VEC_ELT (argvec, i) = args[ia];
21839
21840 /* Copy the parameter into parmvec. */
21841 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21842 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21843 /*subr=*/subr, explain_p))
21844 return 1;
21845
21846 /* Advance to the end of the list of parameters. */
21847 parms = TREE_CHAIN (parms);
21848 }
21849
21850 /* Fail if we've reached the end of the parm list, and more args
21851 are present, and the parm list isn't variadic. */
21852 if (ia < nargs && parms == void_list_node)
21853 return unify_too_many_arguments (explain_p, nargs, ia);
21854 /* Fail if parms are left and they don't have default values and
21855 they aren't all deduced as empty packs (c++/57397). This is
21856 consistent with sufficient_parms_p. */
21857 if (parms && parms != void_list_node
21858 && TREE_PURPOSE (parms) == NULL_TREE)
21859 {
21860 unsigned int count = nargs;
21861 tree p = parms;
21862 bool type_pack_p;
21863 do
21864 {
21865 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21866 if (!type_pack_p)
21867 count++;
21868 p = TREE_CHAIN (p);
21869 }
21870 while (p && p != void_list_node);
21871 if (count != nargs)
21872 return unify_too_few_arguments (explain_p, ia, count,
21873 type_pack_p);
21874 }
21875
21876 if (!subr)
21877 {
21878 tsubst_flags_t complain = (explain_p
21879 ? tf_warning_or_error
21880 : tf_none);
21881 bool tried_array_deduction = (cxx_dialect < cxx17);
21882
21883 for (i = 0; i < ntparms; i++)
21884 {
21885 tree targ = TREE_VEC_ELT (targs, i);
21886 tree tparm = TREE_VEC_ELT (tparms, i);
21887
21888 /* Clear the "incomplete" flags on all argument packs now so that
21889 substituting them into later default arguments works. */
21890 if (targ && ARGUMENT_PACK_P (targ))
21891 {
21892 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21893 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21894 }
21895
21896 if (targ || tparm == error_mark_node)
21897 continue;
21898 tparm = TREE_VALUE (tparm);
21899
21900 if (TREE_CODE (tparm) == TYPE_DECL
21901 && !tried_array_deduction)
21902 {
21903 try_array_deduction (tparms, targs, xparms);
21904 tried_array_deduction = true;
21905 if (TREE_VEC_ELT (targs, i))
21906 continue;
21907 }
21908
21909 /* If this is an undeduced nontype parameter that depends on
21910 a type parameter, try another pass; its type may have been
21911 deduced from a later argument than the one from which
21912 this parameter can be deduced. */
21913 if (TREE_CODE (tparm) == PARM_DECL
21914 && uses_template_parms (TREE_TYPE (tparm))
21915 && saw_undeduced < 2)
21916 {
21917 saw_undeduced = 1;
21918 continue;
21919 }
21920
21921 /* Core issue #226 (C++0x) [temp.deduct]:
21922
21923 If a template argument has not been deduced, its
21924 default template argument, if any, is used.
21925
21926 When we are in C++98 mode, TREE_PURPOSE will either
21927 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21928 to explicitly check cxx_dialect here. */
21929 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21930 /* OK, there is a default argument. Wait until after the
21931 conversion check to do substitution. */
21932 continue;
21933
21934 /* If the type parameter is a parameter pack, then it will
21935 be deduced to an empty parameter pack. */
21936 if (template_parameter_pack_p (tparm))
21937 {
21938 tree arg;
21939
21940 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21941 {
21942 arg = make_node (NONTYPE_ARGUMENT_PACK);
21943 TREE_CONSTANT (arg) = 1;
21944 }
21945 else
21946 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21947
21948 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21949
21950 TREE_VEC_ELT (targs, i) = arg;
21951 continue;
21952 }
21953
21954 return unify_parameter_deduction_failure (explain_p, tparm);
21955 }
21956
21957 /* Now substitute into the default template arguments. */
21958 for (i = 0; i < ntparms; i++)
21959 {
21960 tree targ = TREE_VEC_ELT (targs, i);
21961 tree tparm = TREE_VEC_ELT (tparms, i);
21962
21963 if (targ || tparm == error_mark_node)
21964 continue;
21965 tree parm = TREE_VALUE (tparm);
21966 tree arg = TREE_PURPOSE (tparm);
21967 reopen_deferring_access_checks (*checks);
21968 location_t save_loc = input_location;
21969 if (DECL_P (parm))
21970 input_location = DECL_SOURCE_LOCATION (parm);
21971
21972 if (saw_undeduced == 1
21973 && TREE_CODE (parm) == PARM_DECL
21974 && uses_template_parms (TREE_TYPE (parm)))
21975 {
21976 /* The type of this non-type parameter depends on undeduced
21977 parameters. Don't try to use its default argument yet,
21978 since we might deduce an argument for it on the next pass,
21979 but do check whether the arguments we already have cause
21980 substitution failure, so that that happens before we try
21981 later default arguments (78489). */
21982 ++processing_template_decl;
21983 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21984 NULL_TREE);
21985 --processing_template_decl;
21986 if (type == error_mark_node)
21987 arg = error_mark_node;
21988 else
21989 arg = NULL_TREE;
21990 }
21991 else
21992 {
21993 /* Even if the call is happening in template context, getting
21994 here means it's non-dependent, and a default argument is
21995 considered a separate definition under [temp.decls], so we can
21996 do this substitution without processing_template_decl. This
21997 is important if the default argument contains something that
21998 might be instantiation-dependent like access (87480). */
21999 processing_template_decl_sentinel s;
22000 tree substed = NULL_TREE;
22001 if (saw_undeduced == 1)
22002 {
22003 /* First instatiate in template context, in case we still
22004 depend on undeduced template parameters. */
22005 ++processing_template_decl;
22006 substed = tsubst_template_arg (arg, full_targs, complain,
22007 NULL_TREE);
22008 --processing_template_decl;
22009 if (substed != error_mark_node
22010 && !uses_template_parms (substed))
22011 /* We replaced all the tparms, substitute again out of
22012 template context. */
22013 substed = NULL_TREE;
22014 }
22015 if (!substed)
22016 substed = tsubst_template_arg (arg, full_targs, complain,
22017 NULL_TREE);
22018
22019 if (!uses_template_parms (substed))
22020 arg = convert_template_argument (parm, substed, full_targs,
22021 complain, i, NULL_TREE);
22022 else if (saw_undeduced == 1)
22023 arg = NULL_TREE;
22024 else
22025 arg = error_mark_node;
22026 }
22027
22028 input_location = save_loc;
22029 *checks = get_deferred_access_checks ();
22030 pop_deferring_access_checks ();
22031
22032 if (arg == error_mark_node)
22033 return 1;
22034 else if (arg)
22035 {
22036 TREE_VEC_ELT (targs, i) = arg;
22037 /* The position of the first default template argument,
22038 is also the number of non-defaulted arguments in TARGS.
22039 Record that. */
22040 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22041 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22042 }
22043 }
22044
22045 if (saw_undeduced++ == 1)
22046 goto again;
22047 }
22048
22049 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22050 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22051
22052 return unify_success (explain_p);
22053 }
22054
22055 /* Subroutine of type_unification_real. Args are like the variables
22056 at the call site. ARG is an overloaded function (or template-id);
22057 we try deducing template args from each of the overloads, and if
22058 only one succeeds, we go with that. Modifies TARGS and returns
22059 true on success. */
22060
22061 static bool
22062 resolve_overloaded_unification (tree tparms,
22063 tree targs,
22064 tree parm,
22065 tree arg,
22066 unification_kind_t strict,
22067 int sub_strict,
22068 bool explain_p)
22069 {
22070 tree tempargs = copy_node (targs);
22071 int good = 0;
22072 tree goodfn = NULL_TREE;
22073 bool addr_p;
22074
22075 if (TREE_CODE (arg) == ADDR_EXPR)
22076 {
22077 arg = TREE_OPERAND (arg, 0);
22078 addr_p = true;
22079 }
22080 else
22081 addr_p = false;
22082
22083 if (TREE_CODE (arg) == COMPONENT_REF)
22084 /* Handle `&x' where `x' is some static or non-static member
22085 function name. */
22086 arg = TREE_OPERAND (arg, 1);
22087
22088 if (TREE_CODE (arg) == OFFSET_REF)
22089 arg = TREE_OPERAND (arg, 1);
22090
22091 /* Strip baselink information. */
22092 if (BASELINK_P (arg))
22093 arg = BASELINK_FUNCTIONS (arg);
22094
22095 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22096 {
22097 /* If we got some explicit template args, we need to plug them into
22098 the affected templates before we try to unify, in case the
22099 explicit args will completely resolve the templates in question. */
22100
22101 int ok = 0;
22102 tree expl_subargs = TREE_OPERAND (arg, 1);
22103 arg = TREE_OPERAND (arg, 0);
22104
22105 for (lkp_iterator iter (arg); iter; ++iter)
22106 {
22107 tree fn = *iter;
22108 tree subargs, elem;
22109
22110 if (TREE_CODE (fn) != TEMPLATE_DECL)
22111 continue;
22112
22113 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22114 expl_subargs, NULL_TREE, tf_none,
22115 /*require_all_args=*/true,
22116 /*use_default_args=*/true);
22117 if (subargs != error_mark_node
22118 && !any_dependent_template_arguments_p (subargs))
22119 {
22120 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
22121 if (try_one_overload (tparms, targs, tempargs, parm,
22122 elem, strict, sub_strict, addr_p, explain_p)
22123 && (!goodfn || !same_type_p (goodfn, elem)))
22124 {
22125 goodfn = elem;
22126 ++good;
22127 }
22128 }
22129 else if (subargs)
22130 ++ok;
22131 }
22132 /* If no templates (or more than one) are fully resolved by the
22133 explicit arguments, this template-id is a non-deduced context; it
22134 could still be OK if we deduce all template arguments for the
22135 enclosing call through other arguments. */
22136 if (good != 1)
22137 good = ok;
22138 }
22139 else if (!OVL_P (arg))
22140 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22141 -- but the deduction does not succeed because the expression is
22142 not just the function on its own. */
22143 return false;
22144 else
22145 for (lkp_iterator iter (arg); iter; ++iter)
22146 {
22147 tree fn = *iter;
22148 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22149 strict, sub_strict, addr_p, explain_p)
22150 && (!goodfn || !decls_match (goodfn, fn)))
22151 {
22152 goodfn = fn;
22153 ++good;
22154 }
22155 }
22156
22157 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22158 to function or pointer to member function argument if the set of
22159 overloaded functions does not contain function templates and at most
22160 one of a set of overloaded functions provides a unique match.
22161
22162 So if we found multiple possibilities, we return success but don't
22163 deduce anything. */
22164
22165 if (good == 1)
22166 {
22167 int i = TREE_VEC_LENGTH (targs);
22168 for (; i--; )
22169 if (TREE_VEC_ELT (tempargs, i))
22170 {
22171 tree old = TREE_VEC_ELT (targs, i);
22172 tree new_ = TREE_VEC_ELT (tempargs, i);
22173 if (new_ && old && ARGUMENT_PACK_P (old)
22174 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22175 /* Don't forget explicit template arguments in a pack. */
22176 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22177 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22178 TREE_VEC_ELT (targs, i) = new_;
22179 }
22180 }
22181 if (good)
22182 return true;
22183
22184 return false;
22185 }
22186
22187 /* Core DR 115: In contexts where deduction is done and fails, or in
22188 contexts where deduction is not done, if a template argument list is
22189 specified and it, along with any default template arguments, identifies
22190 a single function template specialization, then the template-id is an
22191 lvalue for the function template specialization. */
22192
22193 tree
22194 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22195 {
22196 tree expr, offset, baselink;
22197 bool addr;
22198
22199 if (!type_unknown_p (orig_expr))
22200 return orig_expr;
22201
22202 expr = orig_expr;
22203 addr = false;
22204 offset = NULL_TREE;
22205 baselink = NULL_TREE;
22206
22207 if (TREE_CODE (expr) == ADDR_EXPR)
22208 {
22209 expr = TREE_OPERAND (expr, 0);
22210 addr = true;
22211 }
22212 if (TREE_CODE (expr) == OFFSET_REF)
22213 {
22214 offset = expr;
22215 expr = TREE_OPERAND (expr, 1);
22216 }
22217 if (BASELINK_P (expr))
22218 {
22219 baselink = expr;
22220 expr = BASELINK_FUNCTIONS (expr);
22221 }
22222
22223 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22224 {
22225 int good = 0;
22226 tree goodfn = NULL_TREE;
22227
22228 /* If we got some explicit template args, we need to plug them into
22229 the affected templates before we try to unify, in case the
22230 explicit args will completely resolve the templates in question. */
22231
22232 tree expl_subargs = TREE_OPERAND (expr, 1);
22233 tree arg = TREE_OPERAND (expr, 0);
22234 tree badfn = NULL_TREE;
22235 tree badargs = NULL_TREE;
22236
22237 for (lkp_iterator iter (arg); iter; ++iter)
22238 {
22239 tree fn = *iter;
22240 tree subargs, elem;
22241
22242 if (TREE_CODE (fn) != TEMPLATE_DECL)
22243 continue;
22244
22245 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22246 expl_subargs, NULL_TREE, tf_none,
22247 /*require_all_args=*/true,
22248 /*use_default_args=*/true);
22249 if (subargs != error_mark_node
22250 && !any_dependent_template_arguments_p (subargs))
22251 {
22252 elem = instantiate_template (fn, subargs, tf_none);
22253 if (elem == error_mark_node)
22254 {
22255 badfn = fn;
22256 badargs = subargs;
22257 }
22258 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
22259 {
22260 goodfn = elem;
22261 ++good;
22262 }
22263 }
22264 }
22265 if (good == 1)
22266 {
22267 mark_used (goodfn);
22268 expr = goodfn;
22269 if (baselink)
22270 expr = build_baselink (BASELINK_BINFO (baselink),
22271 BASELINK_ACCESS_BINFO (baselink),
22272 expr, BASELINK_OPTYPE (baselink));
22273 if (offset)
22274 {
22275 tree base
22276 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22277 expr = build_offset_ref (base, expr, addr, complain);
22278 }
22279 if (addr)
22280 expr = cp_build_addr_expr (expr, complain);
22281 return expr;
22282 }
22283 else if (good == 0 && badargs && (complain & tf_error))
22284 /* There were no good options and at least one bad one, so let the
22285 user know what the problem is. */
22286 instantiate_template (badfn, badargs, complain);
22287 }
22288 return orig_expr;
22289 }
22290
22291 /* As above, but error out if the expression remains overloaded. */
22292
22293 tree
22294 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22295 {
22296 exp = resolve_nondeduced_context (exp, complain);
22297 if (type_unknown_p (exp))
22298 {
22299 if (complain & tf_error)
22300 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22301 return error_mark_node;
22302 }
22303 return exp;
22304 }
22305
22306 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22307 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22308 different overloads deduce different arguments for a given parm.
22309 ADDR_P is true if the expression for which deduction is being
22310 performed was of the form "& fn" rather than simply "fn".
22311
22312 Returns 1 on success. */
22313
22314 static int
22315 try_one_overload (tree tparms,
22316 tree orig_targs,
22317 tree targs,
22318 tree parm,
22319 tree arg,
22320 unification_kind_t strict,
22321 int sub_strict,
22322 bool addr_p,
22323 bool explain_p)
22324 {
22325 int nargs;
22326 tree tempargs;
22327 int i;
22328
22329 if (arg == error_mark_node)
22330 return 0;
22331
22332 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22333 to function or pointer to member function argument if the set of
22334 overloaded functions does not contain function templates and at most
22335 one of a set of overloaded functions provides a unique match.
22336
22337 So if this is a template, just return success. */
22338
22339 if (uses_template_parms (arg))
22340 return 1;
22341
22342 if (TREE_CODE (arg) == METHOD_TYPE)
22343 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22344 else if (addr_p)
22345 arg = build_pointer_type (arg);
22346
22347 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22348
22349 /* We don't copy orig_targs for this because if we have already deduced
22350 some template args from previous args, unify would complain when we
22351 try to deduce a template parameter for the same argument, even though
22352 there isn't really a conflict. */
22353 nargs = TREE_VEC_LENGTH (targs);
22354 tempargs = make_tree_vec (nargs);
22355
22356 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22357 return 0;
22358
22359 /* First make sure we didn't deduce anything that conflicts with
22360 explicitly specified args. */
22361 for (i = nargs; i--; )
22362 {
22363 tree elt = TREE_VEC_ELT (tempargs, i);
22364 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22365
22366 if (!elt)
22367 /*NOP*/;
22368 else if (uses_template_parms (elt))
22369 /* Since we're unifying against ourselves, we will fill in
22370 template args used in the function parm list with our own
22371 template parms. Discard them. */
22372 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22373 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22374 {
22375 /* Check that the argument at each index of the deduced argument pack
22376 is equivalent to the corresponding explicitly specified argument.
22377 We may have deduced more arguments than were explicitly specified,
22378 and that's OK. */
22379
22380 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22381 that's wrong if we deduce the same argument pack from multiple
22382 function arguments: it's only incomplete the first time. */
22383
22384 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22385 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22386
22387 if (TREE_VEC_LENGTH (deduced_pack)
22388 < TREE_VEC_LENGTH (explicit_pack))
22389 return 0;
22390
22391 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22392 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22393 TREE_VEC_ELT (deduced_pack, j)))
22394 return 0;
22395 }
22396 else if (oldelt && !template_args_equal (oldelt, elt))
22397 return 0;
22398 }
22399
22400 for (i = nargs; i--; )
22401 {
22402 tree elt = TREE_VEC_ELT (tempargs, i);
22403
22404 if (elt)
22405 TREE_VEC_ELT (targs, i) = elt;
22406 }
22407
22408 return 1;
22409 }
22410
22411 /* PARM is a template class (perhaps with unbound template
22412 parameters). ARG is a fully instantiated type. If ARG can be
22413 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22414 TARGS are as for unify. */
22415
22416 static tree
22417 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22418 bool explain_p)
22419 {
22420 tree copy_of_targs;
22421
22422 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22423 return NULL_TREE;
22424 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22425 /* Matches anything. */;
22426 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22427 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22428 return NULL_TREE;
22429
22430 /* We need to make a new template argument vector for the call to
22431 unify. If we used TARGS, we'd clutter it up with the result of
22432 the attempted unification, even if this class didn't work out.
22433 We also don't want to commit ourselves to all the unifications
22434 we've already done, since unification is supposed to be done on
22435 an argument-by-argument basis. In other words, consider the
22436 following pathological case:
22437
22438 template <int I, int J, int K>
22439 struct S {};
22440
22441 template <int I, int J>
22442 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22443
22444 template <int I, int J, int K>
22445 void f(S<I, J, K>, S<I, I, I>);
22446
22447 void g() {
22448 S<0, 0, 0> s0;
22449 S<0, 1, 2> s2;
22450
22451 f(s0, s2);
22452 }
22453
22454 Now, by the time we consider the unification involving `s2', we
22455 already know that we must have `f<0, 0, 0>'. But, even though
22456 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22457 because there are two ways to unify base classes of S<0, 1, 2>
22458 with S<I, I, I>. If we kept the already deduced knowledge, we
22459 would reject the possibility I=1. */
22460 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22461
22462 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22463 {
22464 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22465 return NULL_TREE;
22466 return arg;
22467 }
22468
22469 /* If unification failed, we're done. */
22470 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22471 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22472 return NULL_TREE;
22473
22474 return arg;
22475 }
22476
22477 /* Given a template type PARM and a class type ARG, find the unique
22478 base type in ARG that is an instance of PARM. We do not examine
22479 ARG itself; only its base-classes. If there is not exactly one
22480 appropriate base class, return NULL_TREE. PARM may be the type of
22481 a partial specialization, as well as a plain template type. Used
22482 by unify. */
22483
22484 static enum template_base_result
22485 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22486 bool explain_p, tree *result)
22487 {
22488 tree rval = NULL_TREE;
22489 tree binfo;
22490
22491 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22492
22493 binfo = TYPE_BINFO (complete_type (arg));
22494 if (!binfo)
22495 {
22496 /* The type could not be completed. */
22497 *result = NULL_TREE;
22498 return tbr_incomplete_type;
22499 }
22500
22501 /* Walk in inheritance graph order. The search order is not
22502 important, and this avoids multiple walks of virtual bases. */
22503 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22504 {
22505 tree r = try_class_unification (tparms, targs, parm,
22506 BINFO_TYPE (binfo), explain_p);
22507
22508 if (r)
22509 {
22510 /* If there is more than one satisfactory baseclass, then:
22511
22512 [temp.deduct.call]
22513
22514 If they yield more than one possible deduced A, the type
22515 deduction fails.
22516
22517 applies. */
22518 if (rval && !same_type_p (r, rval))
22519 {
22520 *result = NULL_TREE;
22521 return tbr_ambiguous_baseclass;
22522 }
22523
22524 rval = r;
22525 }
22526 }
22527
22528 *result = rval;
22529 return tbr_success;
22530 }
22531
22532 /* Returns the level of DECL, which declares a template parameter. */
22533
22534 static int
22535 template_decl_level (tree decl)
22536 {
22537 switch (TREE_CODE (decl))
22538 {
22539 case TYPE_DECL:
22540 case TEMPLATE_DECL:
22541 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22542
22543 case PARM_DECL:
22544 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22545
22546 default:
22547 gcc_unreachable ();
22548 }
22549 return 0;
22550 }
22551
22552 /* Decide whether ARG can be unified with PARM, considering only the
22553 cv-qualifiers of each type, given STRICT as documented for unify.
22554 Returns nonzero iff the unification is OK on that basis. */
22555
22556 static int
22557 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22558 {
22559 int arg_quals = cp_type_quals (arg);
22560 int parm_quals = cp_type_quals (parm);
22561
22562 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22563 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22564 {
22565 /* Although a CVR qualifier is ignored when being applied to a
22566 substituted template parameter ([8.3.2]/1 for example), that
22567 does not allow us to unify "const T" with "int&" because both
22568 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22569 It is ok when we're allowing additional CV qualifiers
22570 at the outer level [14.8.2.1]/3,1st bullet. */
22571 if ((TYPE_REF_P (arg)
22572 || FUNC_OR_METHOD_TYPE_P (arg))
22573 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22574 return 0;
22575
22576 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22577 && (parm_quals & TYPE_QUAL_RESTRICT))
22578 return 0;
22579 }
22580
22581 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22582 && (arg_quals & parm_quals) != parm_quals)
22583 return 0;
22584
22585 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22586 && (parm_quals & arg_quals) != arg_quals)
22587 return 0;
22588
22589 return 1;
22590 }
22591
22592 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22593 void
22594 template_parm_level_and_index (tree parm, int* level, int* index)
22595 {
22596 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22597 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22598 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22599 {
22600 *index = TEMPLATE_TYPE_IDX (parm);
22601 *level = TEMPLATE_TYPE_LEVEL (parm);
22602 }
22603 else
22604 {
22605 *index = TEMPLATE_PARM_IDX (parm);
22606 *level = TEMPLATE_PARM_LEVEL (parm);
22607 }
22608 }
22609
22610 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22611 do { \
22612 if (unify (TP, TA, P, A, S, EP)) \
22613 return 1; \
22614 } while (0)
22615
22616 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22617 expansion at the end of PACKED_PARMS. Returns 0 if the type
22618 deduction succeeds, 1 otherwise. STRICT is the same as in
22619 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22620 function call argument list. We'll need to adjust the arguments to make them
22621 types. SUBR tells us if this is from a recursive call to
22622 type_unification_real, or for comparing two template argument
22623 lists. */
22624
22625 static int
22626 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22627 tree packed_args, unification_kind_t strict,
22628 bool subr, bool explain_p)
22629 {
22630 tree parm
22631 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22632 tree pattern = PACK_EXPANSION_PATTERN (parm);
22633 tree pack, packs = NULL_TREE;
22634 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22635
22636 /* Add in any args remembered from an earlier partial instantiation. */
22637 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22638 int levels = TMPL_ARGS_DEPTH (targs);
22639
22640 packed_args = expand_template_argument_pack (packed_args);
22641
22642 int len = TREE_VEC_LENGTH (packed_args);
22643
22644 /* Determine the parameter packs we will be deducing from the
22645 pattern, and record their current deductions. */
22646 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22647 pack; pack = TREE_CHAIN (pack))
22648 {
22649 tree parm_pack = TREE_VALUE (pack);
22650 int idx, level;
22651
22652 /* Only template parameter packs can be deduced, not e.g. function
22653 parameter packs or __bases or __integer_pack. */
22654 if (!TEMPLATE_PARM_P (parm_pack))
22655 continue;
22656
22657 /* Determine the index and level of this parameter pack. */
22658 template_parm_level_and_index (parm_pack, &level, &idx);
22659 if (level < levels)
22660 continue;
22661
22662 /* Keep track of the parameter packs and their corresponding
22663 argument packs. */
22664 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22665 TREE_TYPE (packs) = make_tree_vec (len - start);
22666 }
22667
22668 /* Loop through all of the arguments that have not yet been
22669 unified and unify each with the pattern. */
22670 for (i = start; i < len; i++)
22671 {
22672 tree parm;
22673 bool any_explicit = false;
22674 tree arg = TREE_VEC_ELT (packed_args, i);
22675
22676 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22677 or the element of its argument pack at the current index if
22678 this argument was explicitly specified. */
22679 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22680 {
22681 int idx, level;
22682 tree arg, pargs;
22683 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22684
22685 arg = NULL_TREE;
22686 if (TREE_VALUE (pack)
22687 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22688 && (i - start < TREE_VEC_LENGTH (pargs)))
22689 {
22690 any_explicit = true;
22691 arg = TREE_VEC_ELT (pargs, i - start);
22692 }
22693 TMPL_ARG (targs, level, idx) = arg;
22694 }
22695
22696 /* If we had explicit template arguments, substitute them into the
22697 pattern before deduction. */
22698 if (any_explicit)
22699 {
22700 /* Some arguments might still be unspecified or dependent. */
22701 bool dependent;
22702 ++processing_template_decl;
22703 dependent = any_dependent_template_arguments_p (targs);
22704 if (!dependent)
22705 --processing_template_decl;
22706 parm = tsubst (pattern, targs,
22707 explain_p ? tf_warning_or_error : tf_none,
22708 NULL_TREE);
22709 if (dependent)
22710 --processing_template_decl;
22711 if (parm == error_mark_node)
22712 return 1;
22713 }
22714 else
22715 parm = pattern;
22716
22717 /* Unify the pattern with the current argument. */
22718 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22719 explain_p))
22720 return 1;
22721
22722 /* For each parameter pack, collect the deduced value. */
22723 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22724 {
22725 int idx, level;
22726 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22727
22728 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22729 TMPL_ARG (targs, level, idx);
22730 }
22731 }
22732
22733 /* Verify that the results of unification with the parameter packs
22734 produce results consistent with what we've seen before, and make
22735 the deduced argument packs available. */
22736 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22737 {
22738 tree old_pack = TREE_VALUE (pack);
22739 tree new_args = TREE_TYPE (pack);
22740 int i, len = TREE_VEC_LENGTH (new_args);
22741 int idx, level;
22742 bool nondeduced_p = false;
22743
22744 /* By default keep the original deduced argument pack.
22745 If necessary, more specific code is going to update the
22746 resulting deduced argument later down in this function. */
22747 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22748 TMPL_ARG (targs, level, idx) = old_pack;
22749
22750 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22751 actually deduce anything. */
22752 for (i = 0; i < len && !nondeduced_p; ++i)
22753 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22754 nondeduced_p = true;
22755 if (nondeduced_p)
22756 continue;
22757
22758 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22759 {
22760 /* If we had fewer function args than explicit template args,
22761 just use the explicits. */
22762 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22763 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22764 if (len < explicit_len)
22765 new_args = explicit_args;
22766 }
22767
22768 if (!old_pack)
22769 {
22770 tree result;
22771 /* Build the deduced *_ARGUMENT_PACK. */
22772 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22773 {
22774 result = make_node (NONTYPE_ARGUMENT_PACK);
22775 TREE_CONSTANT (result) = 1;
22776 }
22777 else
22778 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22779
22780 SET_ARGUMENT_PACK_ARGS (result, new_args);
22781
22782 /* Note the deduced argument packs for this parameter
22783 pack. */
22784 TMPL_ARG (targs, level, idx) = result;
22785 }
22786 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22787 && (ARGUMENT_PACK_ARGS (old_pack)
22788 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22789 {
22790 /* We only had the explicitly-provided arguments before, but
22791 now we have a complete set of arguments. */
22792 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22793
22794 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22795 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22796 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22797 }
22798 else
22799 {
22800 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22801 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22802 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
22803 /* During template argument deduction for the aggregate deduction
22804 candidate, the number of elements in a trailing parameter pack
22805 is only deduced from the number of remaining function
22806 arguments if it is not otherwise deduced. */
22807 if (cxx_dialect >= cxx20
22808 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
22809 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
22810 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
22811 if (!comp_template_args (old_args, new_args,
22812 &bad_old_arg, &bad_new_arg))
22813 /* Inconsistent unification of this parameter pack. */
22814 return unify_parameter_pack_inconsistent (explain_p,
22815 bad_old_arg,
22816 bad_new_arg);
22817 }
22818 }
22819
22820 return unify_success (explain_p);
22821 }
22822
22823 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22824 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22825 parameters and return value are as for unify. */
22826
22827 static int
22828 unify_array_domain (tree tparms, tree targs,
22829 tree parm_dom, tree arg_dom,
22830 bool explain_p)
22831 {
22832 tree parm_max;
22833 tree arg_max;
22834 bool parm_cst;
22835 bool arg_cst;
22836
22837 /* Our representation of array types uses "N - 1" as the
22838 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22839 not an integer constant. We cannot unify arbitrarily
22840 complex expressions, so we eliminate the MINUS_EXPRs
22841 here. */
22842 parm_max = TYPE_MAX_VALUE (parm_dom);
22843 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22844 if (!parm_cst)
22845 {
22846 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22847 parm_max = TREE_OPERAND (parm_max, 0);
22848 }
22849 arg_max = TYPE_MAX_VALUE (arg_dom);
22850 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22851 if (!arg_cst)
22852 {
22853 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22854 trying to unify the type of a variable with the type
22855 of a template parameter. For example:
22856
22857 template <unsigned int N>
22858 void f (char (&) [N]);
22859 int g();
22860 void h(int i) {
22861 char a[g(i)];
22862 f(a);
22863 }
22864
22865 Here, the type of the ARG will be "int [g(i)]", and
22866 may be a SAVE_EXPR, etc. */
22867 if (TREE_CODE (arg_max) != MINUS_EXPR)
22868 return unify_vla_arg (explain_p, arg_dom);
22869 arg_max = TREE_OPERAND (arg_max, 0);
22870 }
22871
22872 /* If only one of the bounds used a MINUS_EXPR, compensate
22873 by adding one to the other bound. */
22874 if (parm_cst && !arg_cst)
22875 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22876 integer_type_node,
22877 parm_max,
22878 integer_one_node);
22879 else if (arg_cst && !parm_cst)
22880 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22881 integer_type_node,
22882 arg_max,
22883 integer_one_node);
22884
22885 return unify (tparms, targs, parm_max, arg_max,
22886 UNIFY_ALLOW_INTEGER, explain_p);
22887 }
22888
22889 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22890
22891 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22892
22893 static pa_kind_t
22894 pa_kind (tree t)
22895 {
22896 if (PACK_EXPANSION_P (t))
22897 t = PACK_EXPANSION_PATTERN (t);
22898 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22899 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22900 || DECL_TYPE_TEMPLATE_P (t))
22901 return pa_tmpl;
22902 else if (TYPE_P (t))
22903 return pa_type;
22904 else
22905 return pa_expr;
22906 }
22907
22908 /* Deduce the value of template parameters. TPARMS is the (innermost)
22909 set of template parameters to a template. TARGS is the bindings
22910 for those template parameters, as determined thus far; TARGS may
22911 include template arguments for outer levels of template parameters
22912 as well. PARM is a parameter to a template function, or a
22913 subcomponent of that parameter; ARG is the corresponding argument.
22914 This function attempts to match PARM with ARG in a manner
22915 consistent with the existing assignments in TARGS. If more values
22916 are deduced, then TARGS is updated.
22917
22918 Returns 0 if the type deduction succeeds, 1 otherwise. The
22919 parameter STRICT is a bitwise or of the following flags:
22920
22921 UNIFY_ALLOW_NONE:
22922 Require an exact match between PARM and ARG.
22923 UNIFY_ALLOW_MORE_CV_QUAL:
22924 Allow the deduced ARG to be more cv-qualified (by qualification
22925 conversion) than ARG.
22926 UNIFY_ALLOW_LESS_CV_QUAL:
22927 Allow the deduced ARG to be less cv-qualified than ARG.
22928 UNIFY_ALLOW_DERIVED:
22929 Allow the deduced ARG to be a template base class of ARG,
22930 or a pointer to a template base class of the type pointed to by
22931 ARG.
22932 UNIFY_ALLOW_INTEGER:
22933 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22934 case for more information.
22935 UNIFY_ALLOW_OUTER_LEVEL:
22936 This is the outermost level of a deduction. Used to determine validity
22937 of qualification conversions. A valid qualification conversion must
22938 have const qualified pointers leading up to the inner type which
22939 requires additional CV quals, except at the outer level, where const
22940 is not required [conv.qual]. It would be normal to set this flag in
22941 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22942 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22943 This is the outermost level of a deduction, and PARM can be more CV
22944 qualified at this point.
22945 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22946 This is the outermost level of a deduction, and PARM can be less CV
22947 qualified at this point. */
22948
22949 static int
22950 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22951 bool explain_p)
22952 {
22953 int idx;
22954 tree targ;
22955 tree tparm;
22956 int strict_in = strict;
22957 tsubst_flags_t complain = (explain_p
22958 ? tf_warning_or_error
22959 : tf_none);
22960
22961 /* I don't think this will do the right thing with respect to types.
22962 But the only case I've seen it in so far has been array bounds, where
22963 signedness is the only information lost, and I think that will be
22964 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22965 finish_id_expression_1, and are also OK. */
22966 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
22967 parm = TREE_OPERAND (parm, 0);
22968
22969 if (arg == error_mark_node)
22970 return unify_invalid (explain_p);
22971 if (arg == unknown_type_node
22972 || arg == init_list_type_node)
22973 /* We can't deduce anything from this, but we might get all the
22974 template args from other function args. */
22975 return unify_success (explain_p);
22976
22977 if (parm == any_targ_node || arg == any_targ_node)
22978 return unify_success (explain_p);
22979
22980 /* If PARM uses template parameters, then we can't bail out here,
22981 even if ARG == PARM, since we won't record unifications for the
22982 template parameters. We might need them if we're trying to
22983 figure out which of two things is more specialized. */
22984 if (arg == parm && !uses_template_parms (parm))
22985 return unify_success (explain_p);
22986
22987 /* Handle init lists early, so the rest of the function can assume
22988 we're dealing with a type. */
22989 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22990 {
22991 tree elt, elttype;
22992 unsigned i;
22993 tree orig_parm = parm;
22994
22995 if (!is_std_init_list (parm)
22996 && TREE_CODE (parm) != ARRAY_TYPE)
22997 /* We can only deduce from an initializer list argument if the
22998 parameter is std::initializer_list or an array; otherwise this
22999 is a non-deduced context. */
23000 return unify_success (explain_p);
23001
23002 if (TREE_CODE (parm) == ARRAY_TYPE)
23003 elttype = TREE_TYPE (parm);
23004 else
23005 {
23006 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23007 /* Deduction is defined in terms of a single type, so just punt
23008 on the (bizarre) std::initializer_list<T...>. */
23009 if (PACK_EXPANSION_P (elttype))
23010 return unify_success (explain_p);
23011 }
23012
23013 if (strict != DEDUCE_EXACT
23014 && TYPE_P (elttype)
23015 && !uses_deducible_template_parms (elttype))
23016 /* If ELTTYPE has no deducible template parms, skip deduction from
23017 the list elements. */;
23018 else
23019 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23020 {
23021 int elt_strict = strict;
23022
23023 if (elt == error_mark_node)
23024 return unify_invalid (explain_p);
23025
23026 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23027 {
23028 tree type = TREE_TYPE (elt);
23029 if (type == error_mark_node)
23030 return unify_invalid (explain_p);
23031 /* It should only be possible to get here for a call. */
23032 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23033 elt_strict |= maybe_adjust_types_for_deduction
23034 (DEDUCE_CALL, &elttype, &type, elt);
23035 elt = type;
23036 }
23037
23038 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23039 explain_p);
23040 }
23041
23042 if (TREE_CODE (parm) == ARRAY_TYPE
23043 && deducible_array_bound (TYPE_DOMAIN (parm)))
23044 {
23045 /* Also deduce from the length of the initializer list. */
23046 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23047 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23048 if (idx == error_mark_node)
23049 return unify_invalid (explain_p);
23050 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23051 idx, explain_p);
23052 }
23053
23054 /* If the std::initializer_list<T> deduction worked, replace the
23055 deduced A with std::initializer_list<A>. */
23056 if (orig_parm != parm)
23057 {
23058 idx = TEMPLATE_TYPE_IDX (orig_parm);
23059 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23060 targ = listify (targ);
23061 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23062 }
23063 return unify_success (explain_p);
23064 }
23065
23066 /* If parm and arg aren't the same kind of thing (template, type, or
23067 expression), fail early. */
23068 if (pa_kind (parm) != pa_kind (arg))
23069 return unify_invalid (explain_p);
23070
23071 /* Immediately reject some pairs that won't unify because of
23072 cv-qualification mismatches. */
23073 if (TREE_CODE (arg) == TREE_CODE (parm)
23074 && TYPE_P (arg)
23075 /* It is the elements of the array which hold the cv quals of an array
23076 type, and the elements might be template type parms. We'll check
23077 when we recurse. */
23078 && TREE_CODE (arg) != ARRAY_TYPE
23079 /* We check the cv-qualifiers when unifying with template type
23080 parameters below. We want to allow ARG `const T' to unify with
23081 PARM `T' for example, when computing which of two templates
23082 is more specialized, for example. */
23083 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23084 && !check_cv_quals_for_unify (strict_in, arg, parm))
23085 return unify_cv_qual_mismatch (explain_p, parm, arg);
23086
23087 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23088 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23089 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23090 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23091 strict &= ~UNIFY_ALLOW_DERIVED;
23092 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23093 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23094
23095 switch (TREE_CODE (parm))
23096 {
23097 case TYPENAME_TYPE:
23098 case SCOPE_REF:
23099 case UNBOUND_CLASS_TEMPLATE:
23100 /* In a type which contains a nested-name-specifier, template
23101 argument values cannot be deduced for template parameters used
23102 within the nested-name-specifier. */
23103 return unify_success (explain_p);
23104
23105 case TEMPLATE_TYPE_PARM:
23106 case TEMPLATE_TEMPLATE_PARM:
23107 case BOUND_TEMPLATE_TEMPLATE_PARM:
23108 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23109 if (error_operand_p (tparm))
23110 return unify_invalid (explain_p);
23111
23112 if (TEMPLATE_TYPE_LEVEL (parm)
23113 != template_decl_level (tparm))
23114 /* The PARM is not one we're trying to unify. Just check
23115 to see if it matches ARG. */
23116 {
23117 if (TREE_CODE (arg) == TREE_CODE (parm)
23118 && (is_auto (parm) ? is_auto (arg)
23119 : same_type_p (parm, arg)))
23120 return unify_success (explain_p);
23121 else
23122 return unify_type_mismatch (explain_p, parm, arg);
23123 }
23124 idx = TEMPLATE_TYPE_IDX (parm);
23125 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23126 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23127 if (error_operand_p (tparm))
23128 return unify_invalid (explain_p);
23129
23130 /* Check for mixed types and values. */
23131 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23132 && TREE_CODE (tparm) != TYPE_DECL)
23133 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23134 && TREE_CODE (tparm) != TEMPLATE_DECL))
23135 gcc_unreachable ();
23136
23137 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23138 {
23139 if ((strict_in & UNIFY_ALLOW_DERIVED)
23140 && CLASS_TYPE_P (arg))
23141 {
23142 /* First try to match ARG directly. */
23143 tree t = try_class_unification (tparms, targs, parm, arg,
23144 explain_p);
23145 if (!t)
23146 {
23147 /* Otherwise, look for a suitable base of ARG, as below. */
23148 enum template_base_result r;
23149 r = get_template_base (tparms, targs, parm, arg,
23150 explain_p, &t);
23151 if (!t)
23152 return unify_no_common_base (explain_p, r, parm, arg);
23153 arg = t;
23154 }
23155 }
23156 /* ARG must be constructed from a template class or a template
23157 template parameter. */
23158 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23159 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23160 return unify_template_deduction_failure (explain_p, parm, arg);
23161
23162 /* Deduce arguments T, i from TT<T> or TT<i>. */
23163 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23164 return 1;
23165
23166 arg = TYPE_TI_TEMPLATE (arg);
23167
23168 /* Fall through to deduce template name. */
23169 }
23170
23171 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23172 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23173 {
23174 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23175
23176 /* Simple cases: Value already set, does match or doesn't. */
23177 if (targ != NULL_TREE && template_args_equal (targ, arg))
23178 return unify_success (explain_p);
23179 else if (targ)
23180 return unify_inconsistency (explain_p, parm, targ, arg);
23181 }
23182 else
23183 {
23184 /* If PARM is `const T' and ARG is only `int', we don't have
23185 a match unless we are allowing additional qualification.
23186 If ARG is `const int' and PARM is just `T' that's OK;
23187 that binds `const int' to `T'. */
23188 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23189 arg, parm))
23190 return unify_cv_qual_mismatch (explain_p, parm, arg);
23191
23192 /* Consider the case where ARG is `const volatile int' and
23193 PARM is `const T'. Then, T should be `volatile int'. */
23194 arg = cp_build_qualified_type_real
23195 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23196 if (arg == error_mark_node)
23197 return unify_invalid (explain_p);
23198
23199 /* Simple cases: Value already set, does match or doesn't. */
23200 if (targ != NULL_TREE && same_type_p (targ, arg))
23201 return unify_success (explain_p);
23202 else if (targ)
23203 return unify_inconsistency (explain_p, parm, targ, arg);
23204
23205 /* Make sure that ARG is not a variable-sized array. (Note
23206 that were talking about variable-sized arrays (like
23207 `int[n]'), rather than arrays of unknown size (like
23208 `int[]').) We'll get very confused by such a type since
23209 the bound of the array is not constant, and therefore
23210 not mangleable. Besides, such types are not allowed in
23211 ISO C++, so we can do as we please here. We do allow
23212 them for 'auto' deduction, since that isn't ABI-exposed. */
23213 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23214 return unify_vla_arg (explain_p, arg);
23215
23216 /* Strip typedefs as in convert_template_argument. */
23217 arg = canonicalize_type_argument (arg, tf_none);
23218 }
23219
23220 /* If ARG is a parameter pack or an expansion, we cannot unify
23221 against it unless PARM is also a parameter pack. */
23222 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23223 && !template_parameter_pack_p (parm))
23224 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23225
23226 /* If the argument deduction results is a METHOD_TYPE,
23227 then there is a problem.
23228 METHOD_TYPE doesn't map to any real C++ type the result of
23229 the deduction cannot be of that type. */
23230 if (TREE_CODE (arg) == METHOD_TYPE)
23231 return unify_method_type_error (explain_p, arg);
23232
23233 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23234 return unify_success (explain_p);
23235
23236 case TEMPLATE_PARM_INDEX:
23237 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23238 if (error_operand_p (tparm))
23239 return unify_invalid (explain_p);
23240
23241 if (TEMPLATE_PARM_LEVEL (parm)
23242 != template_decl_level (tparm))
23243 {
23244 /* The PARM is not one we're trying to unify. Just check
23245 to see if it matches ARG. */
23246 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23247 && cp_tree_equal (parm, arg));
23248 if (result)
23249 unify_expression_unequal (explain_p, parm, arg);
23250 return result;
23251 }
23252
23253 idx = TEMPLATE_PARM_IDX (parm);
23254 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23255
23256 if (targ)
23257 {
23258 if ((strict & UNIFY_ALLOW_INTEGER)
23259 && TREE_TYPE (targ) && TREE_TYPE (arg)
23260 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23261 /* We're deducing from an array bound, the type doesn't matter. */
23262 arg = fold_convert (TREE_TYPE (targ), arg);
23263 int x = !cp_tree_equal (targ, arg);
23264 if (x)
23265 unify_inconsistency (explain_p, parm, targ, arg);
23266 return x;
23267 }
23268
23269 /* [temp.deduct.type] If, in the declaration of a function template
23270 with a non-type template-parameter, the non-type
23271 template-parameter is used in an expression in the function
23272 parameter-list and, if the corresponding template-argument is
23273 deduced, the template-argument type shall match the type of the
23274 template-parameter exactly, except that a template-argument
23275 deduced from an array bound may be of any integral type.
23276 The non-type parameter might use already deduced type parameters. */
23277 tparm = TREE_TYPE (parm);
23278 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23279 /* We don't have enough levels of args to do any substitution. This
23280 can happen in the context of -fnew-ttp-matching. */;
23281 else
23282 {
23283 ++processing_template_decl;
23284 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23285 --processing_template_decl;
23286
23287 if (tree a = type_uses_auto (tparm))
23288 {
23289 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23290 if (tparm == error_mark_node)
23291 return 1;
23292 }
23293 }
23294
23295 if (!TREE_TYPE (arg))
23296 /* Template-parameter dependent expression. Just accept it for now.
23297 It will later be processed in convert_template_argument. */
23298 ;
23299 else if (same_type_ignoring_top_level_qualifiers_p
23300 (non_reference (TREE_TYPE (arg)),
23301 non_reference (tparm)))
23302 /* OK. Ignore top-level quals here because a class-type template
23303 parameter object is const. */;
23304 else if ((strict & UNIFY_ALLOW_INTEGER)
23305 && CP_INTEGRAL_TYPE_P (tparm))
23306 /* Convert the ARG to the type of PARM; the deduced non-type
23307 template argument must exactly match the types of the
23308 corresponding parameter. */
23309 arg = fold (build_nop (tparm, arg));
23310 else if (uses_template_parms (tparm))
23311 {
23312 /* We haven't deduced the type of this parameter yet. */
23313 if (cxx_dialect >= cxx17
23314 /* We deduce from array bounds in try_array_deduction. */
23315 && !(strict & UNIFY_ALLOW_INTEGER))
23316 {
23317 /* Deduce it from the non-type argument. */
23318 tree atype = TREE_TYPE (arg);
23319 RECUR_AND_CHECK_FAILURE (tparms, targs,
23320 tparm, atype,
23321 UNIFY_ALLOW_NONE, explain_p);
23322 }
23323 else
23324 /* Try again later. */
23325 return unify_success (explain_p);
23326 }
23327 else
23328 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23329
23330 /* If ARG is a parameter pack or an expansion, we cannot unify
23331 against it unless PARM is also a parameter pack. */
23332 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23333 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23334 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23335
23336 {
23337 bool removed_attr = false;
23338 arg = strip_typedefs_expr (arg, &removed_attr);
23339 }
23340 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23341 return unify_success (explain_p);
23342
23343 case PTRMEM_CST:
23344 {
23345 /* A pointer-to-member constant can be unified only with
23346 another constant. */
23347 if (TREE_CODE (arg) != PTRMEM_CST)
23348 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23349
23350 /* Just unify the class member. It would be useless (and possibly
23351 wrong, depending on the strict flags) to unify also
23352 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23353 arg refer to the same variable, even if through different
23354 classes. For instance:
23355
23356 struct A { int x; };
23357 struct B : A { };
23358
23359 Unification of &A::x and &B::x must succeed. */
23360 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23361 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23362 }
23363
23364 case POINTER_TYPE:
23365 {
23366 if (!TYPE_PTR_P (arg))
23367 return unify_type_mismatch (explain_p, parm, arg);
23368
23369 /* [temp.deduct.call]
23370
23371 A can be another pointer or pointer to member type that can
23372 be converted to the deduced A via a qualification
23373 conversion (_conv.qual_).
23374
23375 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23376 This will allow for additional cv-qualification of the
23377 pointed-to types if appropriate. */
23378
23379 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23380 /* The derived-to-base conversion only persists through one
23381 level of pointers. */
23382 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23383
23384 return unify (tparms, targs, TREE_TYPE (parm),
23385 TREE_TYPE (arg), strict, explain_p);
23386 }
23387
23388 case REFERENCE_TYPE:
23389 if (!TYPE_REF_P (arg))
23390 return unify_type_mismatch (explain_p, parm, arg);
23391 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23392 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23393
23394 case ARRAY_TYPE:
23395 if (TREE_CODE (arg) != ARRAY_TYPE)
23396 return unify_type_mismatch (explain_p, parm, arg);
23397 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23398 != (TYPE_DOMAIN (arg) == NULL_TREE))
23399 return unify_type_mismatch (explain_p, parm, arg);
23400 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23401 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23402 if (TYPE_DOMAIN (parm) != NULL_TREE)
23403 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23404 TYPE_DOMAIN (arg), explain_p);
23405 return unify_success (explain_p);
23406
23407 case REAL_TYPE:
23408 case COMPLEX_TYPE:
23409 case VECTOR_TYPE:
23410 case INTEGER_TYPE:
23411 case BOOLEAN_TYPE:
23412 case ENUMERAL_TYPE:
23413 case VOID_TYPE:
23414 case NULLPTR_TYPE:
23415 if (TREE_CODE (arg) != TREE_CODE (parm))
23416 return unify_type_mismatch (explain_p, parm, arg);
23417
23418 /* We have already checked cv-qualification at the top of the
23419 function. */
23420 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23421 return unify_type_mismatch (explain_p, parm, arg);
23422
23423 /* As far as unification is concerned, this wins. Later checks
23424 will invalidate it if necessary. */
23425 return unify_success (explain_p);
23426
23427 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23428 /* Type INTEGER_CST can come from ordinary constant template args. */
23429 case INTEGER_CST:
23430 while (CONVERT_EXPR_P (arg))
23431 arg = TREE_OPERAND (arg, 0);
23432
23433 if (TREE_CODE (arg) != INTEGER_CST)
23434 return unify_template_argument_mismatch (explain_p, parm, arg);
23435 return (tree_int_cst_equal (parm, arg)
23436 ? unify_success (explain_p)
23437 : unify_template_argument_mismatch (explain_p, parm, arg));
23438
23439 case TREE_VEC:
23440 {
23441 int i, len, argslen;
23442 int parm_variadic_p = 0;
23443
23444 if (TREE_CODE (arg) != TREE_VEC)
23445 return unify_template_argument_mismatch (explain_p, parm, arg);
23446
23447 len = TREE_VEC_LENGTH (parm);
23448 argslen = TREE_VEC_LENGTH (arg);
23449
23450 /* Check for pack expansions in the parameters. */
23451 for (i = 0; i < len; ++i)
23452 {
23453 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23454 {
23455 if (i == len - 1)
23456 /* We can unify against something with a trailing
23457 parameter pack. */
23458 parm_variadic_p = 1;
23459 else
23460 /* [temp.deduct.type]/9: If the template argument list of
23461 P contains a pack expansion that is not the last
23462 template argument, the entire template argument list
23463 is a non-deduced context. */
23464 return unify_success (explain_p);
23465 }
23466 }
23467
23468 /* If we don't have enough arguments to satisfy the parameters
23469 (not counting the pack expression at the end), or we have
23470 too many arguments for a parameter list that doesn't end in
23471 a pack expression, we can't unify. */
23472 if (parm_variadic_p
23473 ? argslen < len - parm_variadic_p
23474 : argslen != len)
23475 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23476
23477 /* Unify all of the parameters that precede the (optional)
23478 pack expression. */
23479 for (i = 0; i < len - parm_variadic_p; ++i)
23480 {
23481 RECUR_AND_CHECK_FAILURE (tparms, targs,
23482 TREE_VEC_ELT (parm, i),
23483 TREE_VEC_ELT (arg, i),
23484 UNIFY_ALLOW_NONE, explain_p);
23485 }
23486 if (parm_variadic_p)
23487 return unify_pack_expansion (tparms, targs, parm, arg,
23488 DEDUCE_EXACT,
23489 /*subr=*/true, explain_p);
23490 return unify_success (explain_p);
23491 }
23492
23493 case RECORD_TYPE:
23494 case UNION_TYPE:
23495 if (TREE_CODE (arg) != TREE_CODE (parm))
23496 return unify_type_mismatch (explain_p, parm, arg);
23497
23498 if (TYPE_PTRMEMFUNC_P (parm))
23499 {
23500 if (!TYPE_PTRMEMFUNC_P (arg))
23501 return unify_type_mismatch (explain_p, parm, arg);
23502
23503 return unify (tparms, targs,
23504 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23505 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23506 strict, explain_p);
23507 }
23508 else if (TYPE_PTRMEMFUNC_P (arg))
23509 return unify_type_mismatch (explain_p, parm, arg);
23510
23511 if (CLASSTYPE_TEMPLATE_INFO (parm))
23512 {
23513 tree t = NULL_TREE;
23514
23515 if (strict_in & UNIFY_ALLOW_DERIVED)
23516 {
23517 /* First, we try to unify the PARM and ARG directly. */
23518 t = try_class_unification (tparms, targs,
23519 parm, arg, explain_p);
23520
23521 if (!t)
23522 {
23523 /* Fallback to the special case allowed in
23524 [temp.deduct.call]:
23525
23526 If P is a class, and P has the form
23527 template-id, then A can be a derived class of
23528 the deduced A. Likewise, if P is a pointer to
23529 a class of the form template-id, A can be a
23530 pointer to a derived class pointed to by the
23531 deduced A. */
23532 enum template_base_result r;
23533 r = get_template_base (tparms, targs, parm, arg,
23534 explain_p, &t);
23535
23536 if (!t)
23537 {
23538 /* Don't give the derived diagnostic if we're
23539 already dealing with the same template. */
23540 bool same_template
23541 = (CLASSTYPE_TEMPLATE_INFO (arg)
23542 && (CLASSTYPE_TI_TEMPLATE (parm)
23543 == CLASSTYPE_TI_TEMPLATE (arg)));
23544 return unify_no_common_base (explain_p && !same_template,
23545 r, parm, arg);
23546 }
23547 }
23548 }
23549 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23550 && (CLASSTYPE_TI_TEMPLATE (parm)
23551 == CLASSTYPE_TI_TEMPLATE (arg)))
23552 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23553 Then, we should unify `int' and `U'. */
23554 t = arg;
23555 else
23556 /* There's no chance of unification succeeding. */
23557 return unify_type_mismatch (explain_p, parm, arg);
23558
23559 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23560 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23561 }
23562 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23563 return unify_type_mismatch (explain_p, parm, arg);
23564 return unify_success (explain_p);
23565
23566 case METHOD_TYPE:
23567 case FUNCTION_TYPE:
23568 {
23569 unsigned int nargs;
23570 tree *args;
23571 tree a;
23572 unsigned int i;
23573
23574 if (TREE_CODE (arg) != TREE_CODE (parm))
23575 return unify_type_mismatch (explain_p, parm, arg);
23576
23577 /* CV qualifications for methods can never be deduced, they must
23578 match exactly. We need to check them explicitly here,
23579 because type_unification_real treats them as any other
23580 cv-qualified parameter. */
23581 if (TREE_CODE (parm) == METHOD_TYPE
23582 && (!check_cv_quals_for_unify
23583 (UNIFY_ALLOW_NONE,
23584 class_of_this_parm (arg),
23585 class_of_this_parm (parm))))
23586 return unify_cv_qual_mismatch (explain_p, parm, arg);
23587 if (TREE_CODE (arg) == FUNCTION_TYPE
23588 && type_memfn_quals (parm) != type_memfn_quals (arg))
23589 return unify_cv_qual_mismatch (explain_p, parm, arg);
23590 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23591 return unify_type_mismatch (explain_p, parm, arg);
23592
23593 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23594 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23595
23596 nargs = list_length (TYPE_ARG_TYPES (arg));
23597 args = XALLOCAVEC (tree, nargs);
23598 for (a = TYPE_ARG_TYPES (arg), i = 0;
23599 a != NULL_TREE && a != void_list_node;
23600 a = TREE_CHAIN (a), ++i)
23601 args[i] = TREE_VALUE (a);
23602 nargs = i;
23603
23604 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23605 args, nargs, 1, DEDUCE_EXACT,
23606 NULL, explain_p))
23607 return 1;
23608
23609 if (flag_noexcept_type)
23610 {
23611 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23612 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23613 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23614 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23615 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23616 && uses_template_parms (TREE_PURPOSE (pspec)))
23617 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23618 TREE_PURPOSE (aspec),
23619 UNIFY_ALLOW_NONE, explain_p);
23620 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23621 return unify_type_mismatch (explain_p, parm, arg);
23622 }
23623
23624 return 0;
23625 }
23626
23627 case OFFSET_TYPE:
23628 /* Unify a pointer to member with a pointer to member function, which
23629 deduces the type of the member as a function type. */
23630 if (TYPE_PTRMEMFUNC_P (arg))
23631 {
23632 /* Check top-level cv qualifiers */
23633 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23634 return unify_cv_qual_mismatch (explain_p, parm, arg);
23635
23636 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23637 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23638 UNIFY_ALLOW_NONE, explain_p);
23639
23640 /* Determine the type of the function we are unifying against. */
23641 tree fntype = static_fn_type (arg);
23642
23643 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23644 }
23645
23646 if (TREE_CODE (arg) != OFFSET_TYPE)
23647 return unify_type_mismatch (explain_p, parm, arg);
23648 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23649 TYPE_OFFSET_BASETYPE (arg),
23650 UNIFY_ALLOW_NONE, explain_p);
23651 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23652 strict, explain_p);
23653
23654 case CONST_DECL:
23655 if (DECL_TEMPLATE_PARM_P (parm))
23656 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23657 if (arg != scalar_constant_value (parm))
23658 return unify_template_argument_mismatch (explain_p, parm, arg);
23659 return unify_success (explain_p);
23660
23661 case FIELD_DECL:
23662 case TEMPLATE_DECL:
23663 /* Matched cases are handled by the ARG == PARM test above. */
23664 return unify_template_argument_mismatch (explain_p, parm, arg);
23665
23666 case VAR_DECL:
23667 /* We might get a variable as a non-type template argument in parm if the
23668 corresponding parameter is type-dependent. Make any necessary
23669 adjustments based on whether arg is a reference. */
23670 if (CONSTANT_CLASS_P (arg))
23671 parm = fold_non_dependent_expr (parm, complain);
23672 else if (REFERENCE_REF_P (arg))
23673 {
23674 tree sub = TREE_OPERAND (arg, 0);
23675 STRIP_NOPS (sub);
23676 if (TREE_CODE (sub) == ADDR_EXPR)
23677 arg = TREE_OPERAND (sub, 0);
23678 }
23679 /* Now use the normal expression code to check whether they match. */
23680 goto expr;
23681
23682 case TYPE_ARGUMENT_PACK:
23683 case NONTYPE_ARGUMENT_PACK:
23684 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23685 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23686
23687 case TYPEOF_TYPE:
23688 case DECLTYPE_TYPE:
23689 case UNDERLYING_TYPE:
23690 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23691 or UNDERLYING_TYPE nodes. */
23692 return unify_success (explain_p);
23693
23694 case ERROR_MARK:
23695 /* Unification fails if we hit an error node. */
23696 return unify_invalid (explain_p);
23697
23698 case INDIRECT_REF:
23699 if (REFERENCE_REF_P (parm))
23700 {
23701 bool pexp = PACK_EXPANSION_P (arg);
23702 if (pexp)
23703 arg = PACK_EXPANSION_PATTERN (arg);
23704 if (REFERENCE_REF_P (arg))
23705 arg = TREE_OPERAND (arg, 0);
23706 if (pexp)
23707 arg = make_pack_expansion (arg, complain);
23708 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23709 strict, explain_p);
23710 }
23711 /* FALLTHRU */
23712
23713 default:
23714 /* An unresolved overload is a nondeduced context. */
23715 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23716 return unify_success (explain_p);
23717 gcc_assert (EXPR_P (parm)
23718 || COMPOUND_LITERAL_P (parm)
23719 || TREE_CODE (parm) == TRAIT_EXPR);
23720 expr:
23721 /* We must be looking at an expression. This can happen with
23722 something like:
23723
23724 template <int I>
23725 void foo(S<I>, S<I + 2>);
23726
23727 or
23728
23729 template<typename T>
23730 void foo(A<T, T{}>);
23731
23732 This is a "non-deduced context":
23733
23734 [deduct.type]
23735
23736 The non-deduced contexts are:
23737
23738 --A non-type template argument or an array bound in which
23739 a subexpression references a template parameter.
23740
23741 In these cases, we assume deduction succeeded, but don't
23742 actually infer any unifications. */
23743
23744 if (!uses_template_parms (parm)
23745 && !template_args_equal (parm, arg))
23746 return unify_expression_unequal (explain_p, parm, arg);
23747 else
23748 return unify_success (explain_p);
23749 }
23750 }
23751 #undef RECUR_AND_CHECK_FAILURE
23752 \f
23753 /* Note that DECL can be defined in this translation unit, if
23754 required. */
23755
23756 static void
23757 mark_definable (tree decl)
23758 {
23759 tree clone;
23760 DECL_NOT_REALLY_EXTERN (decl) = 1;
23761 FOR_EACH_CLONE (clone, decl)
23762 DECL_NOT_REALLY_EXTERN (clone) = 1;
23763 }
23764
23765 /* Called if RESULT is explicitly instantiated, or is a member of an
23766 explicitly instantiated class. */
23767
23768 void
23769 mark_decl_instantiated (tree result, int extern_p)
23770 {
23771 SET_DECL_EXPLICIT_INSTANTIATION (result);
23772
23773 /* If this entity has already been written out, it's too late to
23774 make any modifications. */
23775 if (TREE_ASM_WRITTEN (result))
23776 return;
23777
23778 /* For anonymous namespace we don't need to do anything. */
23779 if (decl_anon_ns_mem_p (result))
23780 {
23781 gcc_assert (!TREE_PUBLIC (result));
23782 return;
23783 }
23784
23785 if (TREE_CODE (result) != FUNCTION_DECL)
23786 /* The TREE_PUBLIC flag for function declarations will have been
23787 set correctly by tsubst. */
23788 TREE_PUBLIC (result) = 1;
23789
23790 /* This might have been set by an earlier implicit instantiation. */
23791 DECL_COMDAT (result) = 0;
23792
23793 if (extern_p)
23794 DECL_NOT_REALLY_EXTERN (result) = 0;
23795 else
23796 {
23797 mark_definable (result);
23798 mark_needed (result);
23799 /* Always make artificials weak. */
23800 if (DECL_ARTIFICIAL (result) && flag_weak)
23801 comdat_linkage (result);
23802 /* For WIN32 we also want to put explicit instantiations in
23803 linkonce sections. */
23804 else if (TREE_PUBLIC (result))
23805 maybe_make_one_only (result);
23806 if (TREE_CODE (result) == FUNCTION_DECL
23807 && DECL_TEMPLATE_INSTANTIATED (result))
23808 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23809 since start_preparsed_function wouldn't have if we had an earlier
23810 extern explicit instantiation. */
23811 DECL_EXTERNAL (result) = 0;
23812 }
23813
23814 /* If EXTERN_P, then this function will not be emitted -- unless
23815 followed by an explicit instantiation, at which point its linkage
23816 will be adjusted. If !EXTERN_P, then this function will be
23817 emitted here. In neither circumstance do we want
23818 import_export_decl to adjust the linkage. */
23819 DECL_INTERFACE_KNOWN (result) = 1;
23820 }
23821
23822 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23823 important template arguments. If any are missing, we check whether
23824 they're important by using error_mark_node for substituting into any
23825 args that were used for partial ordering (the ones between ARGS and END)
23826 and seeing if it bubbles up. */
23827
23828 static bool
23829 check_undeduced_parms (tree targs, tree args, tree end)
23830 {
23831 bool found = false;
23832 int i;
23833 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23834 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23835 {
23836 found = true;
23837 TREE_VEC_ELT (targs, i) = error_mark_node;
23838 }
23839 if (found)
23840 {
23841 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23842 if (substed == error_mark_node)
23843 return true;
23844 }
23845 return false;
23846 }
23847
23848 /* Given two function templates PAT1 and PAT2, return:
23849
23850 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23851 -1 if PAT2 is more specialized than PAT1.
23852 0 if neither is more specialized.
23853
23854 LEN indicates the number of parameters we should consider
23855 (defaulted parameters should not be considered).
23856
23857 The 1998 std underspecified function template partial ordering, and
23858 DR214 addresses the issue. We take pairs of arguments, one from
23859 each of the templates, and deduce them against each other. One of
23860 the templates will be more specialized if all the *other*
23861 template's arguments deduce against its arguments and at least one
23862 of its arguments *does* *not* deduce against the other template's
23863 corresponding argument. Deduction is done as for class templates.
23864 The arguments used in deduction have reference and top level cv
23865 qualifiers removed. Iff both arguments were originally reference
23866 types *and* deduction succeeds in both directions, an lvalue reference
23867 wins against an rvalue reference and otherwise the template
23868 with the more cv-qualified argument wins for that pairing (if
23869 neither is more cv-qualified, they both are equal). Unlike regular
23870 deduction, after all the arguments have been deduced in this way,
23871 we do *not* verify the deduced template argument values can be
23872 substituted into non-deduced contexts.
23873
23874 The logic can be a bit confusing here, because we look at deduce1 and
23875 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23876 can find template arguments for pat1 to make arg1 look like arg2, that
23877 means that arg2 is at least as specialized as arg1. */
23878
23879 int
23880 more_specialized_fn (tree pat1, tree pat2, int len)
23881 {
23882 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23883 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23884 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23885 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23886 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23887 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23888 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23889 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23890 tree origs1, origs2;
23891 bool lose1 = false;
23892 bool lose2 = false;
23893
23894 /* Remove the this parameter from non-static member functions. If
23895 one is a non-static member function and the other is not a static
23896 member function, remove the first parameter from that function
23897 also. This situation occurs for operator functions where we
23898 locate both a member function (with this pointer) and non-member
23899 operator (with explicit first operand). */
23900 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23901 {
23902 len--; /* LEN is the number of significant arguments for DECL1 */
23903 args1 = TREE_CHAIN (args1);
23904 if (!DECL_STATIC_FUNCTION_P (decl2))
23905 args2 = TREE_CHAIN (args2);
23906 }
23907 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
23908 {
23909 args2 = TREE_CHAIN (args2);
23910 if (!DECL_STATIC_FUNCTION_P (decl1))
23911 {
23912 len--;
23913 args1 = TREE_CHAIN (args1);
23914 }
23915 }
23916
23917 /* If only one is a conversion operator, they are unordered. */
23918 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23919 return 0;
23920
23921 /* Consider the return type for a conversion function */
23922 if (DECL_CONV_FN_P (decl1))
23923 {
23924 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23925 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23926 len++;
23927 }
23928
23929 processing_template_decl++;
23930
23931 origs1 = args1;
23932 origs2 = args2;
23933
23934 while (len--
23935 /* Stop when an ellipsis is seen. */
23936 && args1 != NULL_TREE && args2 != NULL_TREE)
23937 {
23938 tree arg1 = TREE_VALUE (args1);
23939 tree arg2 = TREE_VALUE (args2);
23940 int deduce1, deduce2;
23941 int quals1 = -1;
23942 int quals2 = -1;
23943 int ref1 = 0;
23944 int ref2 = 0;
23945
23946 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23947 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23948 {
23949 /* When both arguments are pack expansions, we need only
23950 unify the patterns themselves. */
23951 arg1 = PACK_EXPANSION_PATTERN (arg1);
23952 arg2 = PACK_EXPANSION_PATTERN (arg2);
23953
23954 /* This is the last comparison we need to do. */
23955 len = 0;
23956 }
23957
23958 if (TYPE_REF_P (arg1))
23959 {
23960 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23961 arg1 = TREE_TYPE (arg1);
23962 quals1 = cp_type_quals (arg1);
23963 }
23964
23965 if (TYPE_REF_P (arg2))
23966 {
23967 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23968 arg2 = TREE_TYPE (arg2);
23969 quals2 = cp_type_quals (arg2);
23970 }
23971
23972 arg1 = TYPE_MAIN_VARIANT (arg1);
23973 arg2 = TYPE_MAIN_VARIANT (arg2);
23974
23975 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23976 {
23977 int i, len2 = remaining_arguments (args2);
23978 tree parmvec = make_tree_vec (1);
23979 tree argvec = make_tree_vec (len2);
23980 tree ta = args2;
23981
23982 /* Setup the parameter vector, which contains only ARG1. */
23983 TREE_VEC_ELT (parmvec, 0) = arg1;
23984
23985 /* Setup the argument vector, which contains the remaining
23986 arguments. */
23987 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23988 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23989
23990 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23991 argvec, DEDUCE_EXACT,
23992 /*subr=*/true, /*explain_p=*/false)
23993 == 0);
23994
23995 /* We cannot deduce in the other direction, because ARG1 is
23996 a pack expansion but ARG2 is not. */
23997 deduce2 = 0;
23998 }
23999 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24000 {
24001 int i, len1 = remaining_arguments (args1);
24002 tree parmvec = make_tree_vec (1);
24003 tree argvec = make_tree_vec (len1);
24004 tree ta = args1;
24005
24006 /* Setup the parameter vector, which contains only ARG1. */
24007 TREE_VEC_ELT (parmvec, 0) = arg2;
24008
24009 /* Setup the argument vector, which contains the remaining
24010 arguments. */
24011 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24012 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24013
24014 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24015 argvec, DEDUCE_EXACT,
24016 /*subr=*/true, /*explain_p=*/false)
24017 == 0);
24018
24019 /* We cannot deduce in the other direction, because ARG2 is
24020 a pack expansion but ARG1 is not.*/
24021 deduce1 = 0;
24022 }
24023
24024 else
24025 {
24026 /* The normal case, where neither argument is a pack
24027 expansion. */
24028 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24029 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24030 == 0);
24031 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24032 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24033 == 0);
24034 }
24035
24036 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24037 arg2, then arg2 is not as specialized as arg1. */
24038 if (!deduce1)
24039 lose2 = true;
24040 if (!deduce2)
24041 lose1 = true;
24042
24043 /* "If, for a given type, deduction succeeds in both directions
24044 (i.e., the types are identical after the transformations above)
24045 and both P and A were reference types (before being replaced with
24046 the type referred to above):
24047 - if the type from the argument template was an lvalue reference and
24048 the type from the parameter template was not, the argument type is
24049 considered to be more specialized than the other; otherwise,
24050 - if the type from the argument template is more cv-qualified
24051 than the type from the parameter template (as described above),
24052 the argument type is considered to be more specialized than the other;
24053 otherwise,
24054 - neither type is more specialized than the other." */
24055
24056 if (deduce1 && deduce2)
24057 {
24058 if (ref1 && ref2 && ref1 != ref2)
24059 {
24060 if (ref1 > ref2)
24061 lose1 = true;
24062 else
24063 lose2 = true;
24064 }
24065 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24066 {
24067 if ((quals1 & quals2) == quals2)
24068 lose2 = true;
24069 if ((quals1 & quals2) == quals1)
24070 lose1 = true;
24071 }
24072 }
24073
24074 if (lose1 && lose2)
24075 /* We've failed to deduce something in either direction.
24076 These must be unordered. */
24077 break;
24078
24079 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24080 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24081 /* We have already processed all of the arguments in our
24082 handing of the pack expansion type. */
24083 len = 0;
24084
24085 args1 = TREE_CHAIN (args1);
24086 args2 = TREE_CHAIN (args2);
24087 }
24088
24089 /* "In most cases, all template parameters must have values in order for
24090 deduction to succeed, but for partial ordering purposes a template
24091 parameter may remain without a value provided it is not used in the
24092 types being used for partial ordering."
24093
24094 Thus, if we are missing any of the targs1 we need to substitute into
24095 origs1, then pat2 is not as specialized as pat1. This can happen when
24096 there is a nondeduced context. */
24097 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24098 lose2 = true;
24099 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24100 lose1 = true;
24101
24102 processing_template_decl--;
24103
24104 /* If both deductions succeed, the partial ordering selects the more
24105 constrained template. */
24106 /* P2113: If the corresponding template-parameters of the
24107 template-parameter-lists are not equivalent ([temp.over.link]) or if
24108 the function parameters that positionally correspond between the two
24109 templates are not of the same type, neither template is more
24110 specialized than the other. */
24111 if (!lose1 && !lose2
24112 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24113 DECL_TEMPLATE_PARMS (pat2))
24114 && compparms (origs1, origs2))
24115 {
24116 int winner = more_constrained (decl1, decl2);
24117 if (winner > 0)
24118 lose2 = true;
24119 else if (winner < 0)
24120 lose1 = true;
24121 }
24122
24123 /* All things being equal, if the next argument is a pack expansion
24124 for one function but not for the other, prefer the
24125 non-variadic function. FIXME this is bogus; see c++/41958. */
24126 if (lose1 == lose2
24127 && args1 && TREE_VALUE (args1)
24128 && args2 && TREE_VALUE (args2))
24129 {
24130 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24131 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24132 }
24133
24134 if (lose1 == lose2)
24135 return 0;
24136 else if (!lose1)
24137 return 1;
24138 else
24139 return -1;
24140 }
24141
24142 /* Determine which of two partial specializations of TMPL is more
24143 specialized.
24144
24145 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24146 to the first partial specialization. The TREE_PURPOSE is the
24147 innermost set of template parameters for the partial
24148 specialization. PAT2 is similar, but for the second template.
24149
24150 Return 1 if the first partial specialization is more specialized;
24151 -1 if the second is more specialized; 0 if neither is more
24152 specialized.
24153
24154 See [temp.class.order] for information about determining which of
24155 two templates is more specialized. */
24156
24157 static int
24158 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24159 {
24160 tree targs;
24161 int winner = 0;
24162 bool any_deductions = false;
24163
24164 tree tmpl1 = TREE_VALUE (pat1);
24165 tree tmpl2 = TREE_VALUE (pat2);
24166 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24167 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24168
24169 /* Just like what happens for functions, if we are ordering between
24170 different template specializations, we may encounter dependent
24171 types in the arguments, and we need our dependency check functions
24172 to behave correctly. */
24173 ++processing_template_decl;
24174 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24175 if (targs)
24176 {
24177 --winner;
24178 any_deductions = true;
24179 }
24180
24181 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24182 if (targs)
24183 {
24184 ++winner;
24185 any_deductions = true;
24186 }
24187 --processing_template_decl;
24188
24189 /* If both deductions succeed, the partial ordering selects the more
24190 constrained template. */
24191 if (!winner && any_deductions)
24192 winner = more_constrained (tmpl1, tmpl2);
24193
24194 /* In the case of a tie where at least one of the templates
24195 has a parameter pack at the end, the template with the most
24196 non-packed parameters wins. */
24197 if (winner == 0
24198 && any_deductions
24199 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24200 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24201 {
24202 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24203 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24204 int len1 = TREE_VEC_LENGTH (args1);
24205 int len2 = TREE_VEC_LENGTH (args2);
24206
24207 /* We don't count the pack expansion at the end. */
24208 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24209 --len1;
24210 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24211 --len2;
24212
24213 if (len1 > len2)
24214 return 1;
24215 else if (len1 < len2)
24216 return -1;
24217 }
24218
24219 return winner;
24220 }
24221
24222 /* Return the template arguments that will produce the function signature
24223 DECL from the function template FN, with the explicit template
24224 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24225 also match. Return NULL_TREE if no satisfactory arguments could be
24226 found. */
24227
24228 static tree
24229 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24230 {
24231 int ntparms = DECL_NTPARMS (fn);
24232 tree targs = make_tree_vec (ntparms);
24233 tree decl_type = TREE_TYPE (decl);
24234 tree decl_arg_types;
24235 tree *args;
24236 unsigned int nargs, ix;
24237 tree arg;
24238
24239 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24240
24241 /* Never do unification on the 'this' parameter. */
24242 decl_arg_types = skip_artificial_parms_for (decl,
24243 TYPE_ARG_TYPES (decl_type));
24244
24245 nargs = list_length (decl_arg_types);
24246 args = XALLOCAVEC (tree, nargs);
24247 for (arg = decl_arg_types, ix = 0;
24248 arg != NULL_TREE && arg != void_list_node;
24249 arg = TREE_CHAIN (arg), ++ix)
24250 args[ix] = TREE_VALUE (arg);
24251
24252 if (fn_type_unification (fn, explicit_args, targs,
24253 args, ix,
24254 (check_rettype || DECL_CONV_FN_P (fn)
24255 ? TREE_TYPE (decl_type) : NULL_TREE),
24256 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24257 /*explain_p=*/false,
24258 /*decltype*/false)
24259 == error_mark_node)
24260 return NULL_TREE;
24261
24262 return targs;
24263 }
24264
24265 /* Return the innermost template arguments that, when applied to a partial
24266 specialization SPEC_TMPL of TMPL, yield the ARGS.
24267
24268 For example, suppose we have:
24269
24270 template <class T, class U> struct S {};
24271 template <class T> struct S<T*, int> {};
24272
24273 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24274 partial specialization and the ARGS will be {double*, int}. The resulting
24275 vector will be {double}, indicating that `T' is bound to `double'. */
24276
24277 static tree
24278 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24279 {
24280 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24281 tree spec_args
24282 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24283 int i, ntparms = TREE_VEC_LENGTH (tparms);
24284 tree deduced_args;
24285 tree innermost_deduced_args;
24286
24287 innermost_deduced_args = make_tree_vec (ntparms);
24288 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24289 {
24290 deduced_args = copy_node (args);
24291 SET_TMPL_ARGS_LEVEL (deduced_args,
24292 TMPL_ARGS_DEPTH (deduced_args),
24293 innermost_deduced_args);
24294 }
24295 else
24296 deduced_args = innermost_deduced_args;
24297
24298 bool tried_array_deduction = (cxx_dialect < cxx17);
24299 again:
24300 if (unify (tparms, deduced_args,
24301 INNERMOST_TEMPLATE_ARGS (spec_args),
24302 INNERMOST_TEMPLATE_ARGS (args),
24303 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24304 return NULL_TREE;
24305
24306 for (i = 0; i < ntparms; ++i)
24307 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24308 {
24309 if (!tried_array_deduction)
24310 {
24311 try_array_deduction (tparms, innermost_deduced_args,
24312 INNERMOST_TEMPLATE_ARGS (spec_args));
24313 tried_array_deduction = true;
24314 if (TREE_VEC_ELT (innermost_deduced_args, i))
24315 goto again;
24316 }
24317 return NULL_TREE;
24318 }
24319
24320 if (!push_tinst_level (spec_tmpl, deduced_args))
24321 {
24322 excessive_deduction_depth = true;
24323 return NULL_TREE;
24324 }
24325
24326 /* Verify that nondeduced template arguments agree with the type
24327 obtained from argument deduction.
24328
24329 For example:
24330
24331 struct A { typedef int X; };
24332 template <class T, class U> struct C {};
24333 template <class T> struct C<T, typename T::X> {};
24334
24335 Then with the instantiation `C<A, int>', we can deduce that
24336 `T' is `A' but unify () does not check whether `typename T::X'
24337 is `int'. */
24338 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24339
24340 if (spec_args != error_mark_node)
24341 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24342 INNERMOST_TEMPLATE_ARGS (spec_args),
24343 tmpl, tf_none, false, false);
24344
24345 pop_tinst_level ();
24346
24347 if (spec_args == error_mark_node
24348 /* We only need to check the innermost arguments; the other
24349 arguments will always agree. */
24350 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24351 INNERMOST_TEMPLATE_ARGS (args)))
24352 return NULL_TREE;
24353
24354 /* Now that we have bindings for all of the template arguments,
24355 ensure that the arguments deduced for the template template
24356 parameters have compatible template parameter lists. See the use
24357 of template_template_parm_bindings_ok_p in fn_type_unification
24358 for more information. */
24359 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24360 return NULL_TREE;
24361
24362 return deduced_args;
24363 }
24364
24365 // Compare two function templates T1 and T2 by deducing bindings
24366 // from one against the other. If both deductions succeed, compare
24367 // constraints to see which is more constrained.
24368 static int
24369 more_specialized_inst (tree t1, tree t2)
24370 {
24371 int fate = 0;
24372 int count = 0;
24373
24374 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24375 {
24376 --fate;
24377 ++count;
24378 }
24379
24380 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24381 {
24382 ++fate;
24383 ++count;
24384 }
24385
24386 // If both deductions succeed, then one may be more constrained.
24387 if (count == 2 && fate == 0)
24388 fate = more_constrained (t1, t2);
24389
24390 return fate;
24391 }
24392
24393 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24394 Return the TREE_LIST node with the most specialized template, if
24395 any. If there is no most specialized template, the error_mark_node
24396 is returned.
24397
24398 Note that this function does not look at, or modify, the
24399 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24400 returned is one of the elements of INSTANTIATIONS, callers may
24401 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24402 and retrieve it from the value returned. */
24403
24404 tree
24405 most_specialized_instantiation (tree templates)
24406 {
24407 tree fn, champ;
24408
24409 ++processing_template_decl;
24410
24411 champ = templates;
24412 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24413 {
24414 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24415 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24416 if (fate == -1)
24417 champ = fn;
24418 else if (!fate)
24419 {
24420 /* Equally specialized, move to next function. If there
24421 is no next function, nothing's most specialized. */
24422 fn = TREE_CHAIN (fn);
24423 champ = fn;
24424 if (!fn)
24425 break;
24426 }
24427 }
24428
24429 if (champ)
24430 /* Now verify that champ is better than everything earlier in the
24431 instantiation list. */
24432 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24433 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24434 {
24435 champ = NULL_TREE;
24436 break;
24437 }
24438 }
24439
24440 processing_template_decl--;
24441
24442 if (!champ)
24443 return error_mark_node;
24444
24445 return champ;
24446 }
24447
24448 /* If DECL is a specialization of some template, return the most
24449 general such template. Otherwise, returns NULL_TREE.
24450
24451 For example, given:
24452
24453 template <class T> struct S { template <class U> void f(U); };
24454
24455 if TMPL is `template <class U> void S<int>::f(U)' this will return
24456 the full template. This function will not trace past partial
24457 specializations, however. For example, given in addition:
24458
24459 template <class T> struct S<T*> { template <class U> void f(U); };
24460
24461 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24462 `template <class T> template <class U> S<T*>::f(U)'. */
24463
24464 tree
24465 most_general_template (tree decl)
24466 {
24467 if (TREE_CODE (decl) != TEMPLATE_DECL)
24468 {
24469 if (tree tinfo = get_template_info (decl))
24470 decl = TI_TEMPLATE (tinfo);
24471 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24472 template friend, or a FIELD_DECL for a capture pack. */
24473 if (TREE_CODE (decl) != TEMPLATE_DECL)
24474 return NULL_TREE;
24475 }
24476
24477 /* Look for more and more general templates. */
24478 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24479 {
24480 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24481 (See cp-tree.h for details.) */
24482 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24483 break;
24484
24485 if (CLASS_TYPE_P (TREE_TYPE (decl))
24486 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24487 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24488 break;
24489
24490 /* Stop if we run into an explicitly specialized class template. */
24491 if (!DECL_NAMESPACE_SCOPE_P (decl)
24492 && DECL_CONTEXT (decl)
24493 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24494 break;
24495
24496 decl = DECL_TI_TEMPLATE (decl);
24497 }
24498
24499 return decl;
24500 }
24501
24502 /* Return the most specialized of the template partial specializations
24503 which can produce TARGET, a specialization of some class or variable
24504 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24505 a TEMPLATE_DECL node corresponding to the partial specialization, while
24506 the TREE_PURPOSE is the set of template arguments that must be
24507 substituted into the template pattern in order to generate TARGET.
24508
24509 If the choice of partial specialization is ambiguous, a diagnostic
24510 is issued, and the error_mark_node is returned. If there are no
24511 partial specializations matching TARGET, then NULL_TREE is
24512 returned, indicating that the primary template should be used. */
24513
24514 tree
24515 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24516 {
24517 tree list = NULL_TREE;
24518 tree t;
24519 tree champ;
24520 int fate;
24521 bool ambiguous_p;
24522 tree outer_args = NULL_TREE;
24523 tree tmpl, args;
24524
24525 if (TYPE_P (target))
24526 {
24527 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24528 tmpl = TI_TEMPLATE (tinfo);
24529 args = TI_ARGS (tinfo);
24530 }
24531 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24532 {
24533 tmpl = TREE_OPERAND (target, 0);
24534 args = TREE_OPERAND (target, 1);
24535 }
24536 else if (VAR_P (target))
24537 {
24538 tree tinfo = DECL_TEMPLATE_INFO (target);
24539 tmpl = TI_TEMPLATE (tinfo);
24540 args = TI_ARGS (tinfo);
24541 }
24542 else
24543 gcc_unreachable ();
24544
24545 tree main_tmpl = most_general_template (tmpl);
24546
24547 /* For determining which partial specialization to use, only the
24548 innermost args are interesting. */
24549 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24550 {
24551 outer_args = strip_innermost_template_args (args, 1);
24552 args = INNERMOST_TEMPLATE_ARGS (args);
24553 }
24554
24555 /* The caller hasn't called push_to_top_level yet, but we need
24556 get_partial_spec_bindings to be done in non-template context so that we'll
24557 fully resolve everything. */
24558 processing_template_decl_sentinel ptds;
24559
24560 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24561 {
24562 const tree ospec_tmpl = TREE_VALUE (t);
24563
24564 tree spec_tmpl;
24565 if (outer_args)
24566 {
24567 /* Substitute in the template args from the enclosing class. */
24568 ++processing_template_decl;
24569 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
24570 --processing_template_decl;
24571 if (spec_tmpl == error_mark_node)
24572 return error_mark_node;
24573 }
24574 else
24575 spec_tmpl = ospec_tmpl;
24576
24577 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24578 if (spec_args)
24579 {
24580 if (outer_args)
24581 spec_args = add_to_template_args (outer_args, spec_args);
24582
24583 /* Keep the candidate only if the constraints are satisfied,
24584 or if we're not compiling with concepts. */
24585 if (!flag_concepts
24586 || constraints_satisfied_p (ospec_tmpl, spec_args))
24587 {
24588 list = tree_cons (spec_args, ospec_tmpl, list);
24589 TREE_TYPE (list) = TREE_TYPE (t);
24590 }
24591 }
24592 }
24593
24594 if (! list)
24595 return NULL_TREE;
24596
24597 ambiguous_p = false;
24598 t = list;
24599 champ = t;
24600 t = TREE_CHAIN (t);
24601 for (; t; t = TREE_CHAIN (t))
24602 {
24603 fate = more_specialized_partial_spec (tmpl, champ, t);
24604 if (fate == 1)
24605 ;
24606 else
24607 {
24608 if (fate == 0)
24609 {
24610 t = TREE_CHAIN (t);
24611 if (! t)
24612 {
24613 ambiguous_p = true;
24614 break;
24615 }
24616 }
24617 champ = t;
24618 }
24619 }
24620
24621 if (!ambiguous_p)
24622 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24623 {
24624 fate = more_specialized_partial_spec (tmpl, champ, t);
24625 if (fate != 1)
24626 {
24627 ambiguous_p = true;
24628 break;
24629 }
24630 }
24631
24632 if (ambiguous_p)
24633 {
24634 const char *str;
24635 char *spaces = NULL;
24636 if (!(complain & tf_error))
24637 return error_mark_node;
24638 if (TYPE_P (target))
24639 error ("ambiguous template instantiation for %q#T", target);
24640 else
24641 error ("ambiguous template instantiation for %q#D", target);
24642 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24643 for (t = list; t; t = TREE_CHAIN (t))
24644 {
24645 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24646 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24647 "%s %#qS", spaces ? spaces : str, subst);
24648 spaces = spaces ? spaces : get_spaces (str);
24649 }
24650 free (spaces);
24651 return error_mark_node;
24652 }
24653
24654 return champ;
24655 }
24656
24657 /* Explicitly instantiate DECL. */
24658
24659 void
24660 do_decl_instantiation (tree decl, tree storage)
24661 {
24662 tree result = NULL_TREE;
24663 int extern_p = 0;
24664
24665 if (!decl || decl == error_mark_node)
24666 /* An error occurred, for which grokdeclarator has already issued
24667 an appropriate message. */
24668 return;
24669 else if (! DECL_LANG_SPECIFIC (decl))
24670 {
24671 error ("explicit instantiation of non-template %q#D", decl);
24672 return;
24673 }
24674 else if (DECL_DECLARED_CONCEPT_P (decl))
24675 {
24676 if (VAR_P (decl))
24677 error ("explicit instantiation of variable concept %q#D", decl);
24678 else
24679 error ("explicit instantiation of function concept %q#D", decl);
24680 return;
24681 }
24682
24683 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24684 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24685
24686 if (VAR_P (decl) && !var_templ)
24687 {
24688 /* There is an asymmetry here in the way VAR_DECLs and
24689 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24690 the latter, the DECL we get back will be marked as a
24691 template instantiation, and the appropriate
24692 DECL_TEMPLATE_INFO will be set up. This does not happen for
24693 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24694 should handle VAR_DECLs as it currently handles
24695 FUNCTION_DECLs. */
24696 if (!DECL_CLASS_SCOPE_P (decl))
24697 {
24698 error ("%qD is not a static data member of a class template", decl);
24699 return;
24700 }
24701 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24702 if (!result || !VAR_P (result))
24703 {
24704 error ("no matching template for %qD found", decl);
24705 return;
24706 }
24707 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24708 {
24709 error ("type %qT for explicit instantiation %qD does not match "
24710 "declared type %qT", TREE_TYPE (result), decl,
24711 TREE_TYPE (decl));
24712 return;
24713 }
24714 }
24715 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24716 {
24717 error ("explicit instantiation of %q#D", decl);
24718 return;
24719 }
24720 else
24721 result = decl;
24722
24723 /* Check for various error cases. Note that if the explicit
24724 instantiation is valid the RESULT will currently be marked as an
24725 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24726 until we get here. */
24727
24728 if (DECL_TEMPLATE_SPECIALIZATION (result))
24729 {
24730 /* DR 259 [temp.spec].
24731
24732 Both an explicit instantiation and a declaration of an explicit
24733 specialization shall not appear in a program unless the explicit
24734 instantiation follows a declaration of the explicit specialization.
24735
24736 For a given set of template parameters, if an explicit
24737 instantiation of a template appears after a declaration of an
24738 explicit specialization for that template, the explicit
24739 instantiation has no effect. */
24740 return;
24741 }
24742 else if (DECL_EXPLICIT_INSTANTIATION (result))
24743 {
24744 /* [temp.spec]
24745
24746 No program shall explicitly instantiate any template more
24747 than once.
24748
24749 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24750 the first instantiation was `extern' and the second is not,
24751 and EXTERN_P for the opposite case. */
24752 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24753 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24754 /* If an "extern" explicit instantiation follows an ordinary
24755 explicit instantiation, the template is instantiated. */
24756 if (extern_p)
24757 return;
24758 }
24759 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24760 {
24761 error ("no matching template for %qD found", result);
24762 return;
24763 }
24764 else if (!DECL_TEMPLATE_INFO (result))
24765 {
24766 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24767 return;
24768 }
24769
24770 if (storage == NULL_TREE)
24771 ;
24772 else if (storage == ridpointers[(int) RID_EXTERN])
24773 {
24774 if (cxx_dialect == cxx98)
24775 pedwarn (input_location, OPT_Wpedantic,
24776 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24777 "instantiations");
24778 extern_p = 1;
24779 }
24780 else
24781 error ("storage class %qD applied to template instantiation", storage);
24782
24783 check_explicit_instantiation_namespace (result);
24784 mark_decl_instantiated (result, extern_p);
24785 if (! extern_p)
24786 instantiate_decl (result, /*defer_ok=*/true,
24787 /*expl_inst_class_mem_p=*/false);
24788 }
24789
24790 static void
24791 mark_class_instantiated (tree t, int extern_p)
24792 {
24793 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24794 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24795 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24796 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24797 if (! extern_p)
24798 {
24799 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24800 rest_of_type_compilation (t, 1);
24801 }
24802 }
24803
24804 /* Called from do_type_instantiation through binding_table_foreach to
24805 do recursive instantiation for the type bound in ENTRY. */
24806 static void
24807 bt_instantiate_type_proc (binding_entry entry, void *data)
24808 {
24809 tree storage = *(tree *) data;
24810
24811 if (MAYBE_CLASS_TYPE_P (entry->type)
24812 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24813 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24814 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24815 }
24816
24817 /* Perform an explicit instantiation of template class T. STORAGE, if
24818 non-null, is the RID for extern, inline or static. COMPLAIN is
24819 nonzero if this is called from the parser, zero if called recursively,
24820 since the standard is unclear (as detailed below). */
24821
24822 void
24823 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24824 {
24825 int extern_p = 0;
24826 int nomem_p = 0;
24827 int static_p = 0;
24828 int previous_instantiation_extern_p = 0;
24829
24830 if (TREE_CODE (t) == TYPE_DECL)
24831 t = TREE_TYPE (t);
24832
24833 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24834 {
24835 tree tmpl =
24836 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24837 if (tmpl)
24838 error ("explicit instantiation of non-class template %qD", tmpl);
24839 else
24840 error ("explicit instantiation of non-template type %qT", t);
24841 return;
24842 }
24843
24844 complete_type (t);
24845
24846 if (!COMPLETE_TYPE_P (t))
24847 {
24848 if (complain & tf_error)
24849 error ("explicit instantiation of %q#T before definition of template",
24850 t);
24851 return;
24852 }
24853
24854 if (storage != NULL_TREE)
24855 {
24856 if (storage == ridpointers[(int) RID_EXTERN])
24857 {
24858 if (cxx_dialect == cxx98)
24859 pedwarn (input_location, OPT_Wpedantic,
24860 "ISO C++ 1998 forbids the use of %<extern%> on "
24861 "explicit instantiations");
24862 }
24863 else
24864 pedwarn (input_location, OPT_Wpedantic,
24865 "ISO C++ forbids the use of %qE"
24866 " on explicit instantiations", storage);
24867
24868 if (storage == ridpointers[(int) RID_INLINE])
24869 nomem_p = 1;
24870 else if (storage == ridpointers[(int) RID_EXTERN])
24871 extern_p = 1;
24872 else if (storage == ridpointers[(int) RID_STATIC])
24873 static_p = 1;
24874 else
24875 {
24876 error ("storage class %qD applied to template instantiation",
24877 storage);
24878 extern_p = 0;
24879 }
24880 }
24881
24882 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24883 {
24884 /* DR 259 [temp.spec].
24885
24886 Both an explicit instantiation and a declaration of an explicit
24887 specialization shall not appear in a program unless the explicit
24888 instantiation follows a declaration of the explicit specialization.
24889
24890 For a given set of template parameters, if an explicit
24891 instantiation of a template appears after a declaration of an
24892 explicit specialization for that template, the explicit
24893 instantiation has no effect. */
24894 return;
24895 }
24896 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24897 {
24898 /* [temp.spec]
24899
24900 No program shall explicitly instantiate any template more
24901 than once.
24902
24903 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24904 instantiation was `extern'. If EXTERN_P then the second is.
24905 These cases are OK. */
24906 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
24907
24908 if (!previous_instantiation_extern_p && !extern_p
24909 && (complain & tf_error))
24910 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24911
24912 /* If we've already instantiated the template, just return now. */
24913 if (!CLASSTYPE_INTERFACE_ONLY (t))
24914 return;
24915 }
24916
24917 check_explicit_instantiation_namespace (TYPE_NAME (t));
24918 mark_class_instantiated (t, extern_p);
24919
24920 if (nomem_p)
24921 return;
24922
24923 /* In contrast to implicit instantiation, where only the
24924 declarations, and not the definitions, of members are
24925 instantiated, we have here:
24926
24927 [temp.explicit]
24928
24929 The explicit instantiation of a class template specialization
24930 implies the instantiation of all of its members not
24931 previously explicitly specialized in the translation unit
24932 containing the explicit instantiation.
24933
24934 Of course, we can't instantiate member template classes, since we
24935 don't have any arguments for them. Note that the standard is
24936 unclear on whether the instantiation of the members are
24937 *explicit* instantiations or not. However, the most natural
24938 interpretation is that it should be an explicit
24939 instantiation. */
24940 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24941 if ((VAR_P (fld)
24942 || (TREE_CODE (fld) == FUNCTION_DECL
24943 && !static_p
24944 && user_provided_p (fld)))
24945 && DECL_TEMPLATE_INSTANTIATION (fld))
24946 {
24947 mark_decl_instantiated (fld, extern_p);
24948 if (! extern_p)
24949 instantiate_decl (fld, /*defer_ok=*/true,
24950 /*expl_inst_class_mem_p=*/true);
24951 }
24952
24953 if (CLASSTYPE_NESTED_UTDS (t))
24954 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24955 bt_instantiate_type_proc, &storage);
24956 }
24957
24958 /* Given a function DECL, which is a specialization of TMPL, modify
24959 DECL to be a re-instantiation of TMPL with the same template
24960 arguments. TMPL should be the template into which tsubst'ing
24961 should occur for DECL, not the most general template.
24962
24963 One reason for doing this is a scenario like this:
24964
24965 template <class T>
24966 void f(const T&, int i);
24967
24968 void g() { f(3, 7); }
24969
24970 template <class T>
24971 void f(const T& t, const int i) { }
24972
24973 Note that when the template is first instantiated, with
24974 instantiate_template, the resulting DECL will have no name for the
24975 first parameter, and the wrong type for the second. So, when we go
24976 to instantiate the DECL, we regenerate it. */
24977
24978 static void
24979 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24980 {
24981 /* The arguments used to instantiate DECL, from the most general
24982 template. */
24983 tree code_pattern;
24984
24985 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24986
24987 /* Make sure that we can see identifiers, and compute access
24988 correctly. */
24989 push_access_scope (decl);
24990
24991 if (TREE_CODE (decl) == FUNCTION_DECL)
24992 {
24993 tree decl_parm;
24994 tree pattern_parm;
24995 tree specs;
24996 int args_depth;
24997 int parms_depth;
24998
24999 args_depth = TMPL_ARGS_DEPTH (args);
25000 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25001 if (args_depth > parms_depth)
25002 args = get_innermost_template_args (args, parms_depth);
25003
25004 /* Instantiate a dynamic exception-specification. noexcept will be
25005 handled below. */
25006 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25007 if (TREE_VALUE (raises))
25008 {
25009 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25010 args, tf_error, NULL_TREE,
25011 /*defer_ok*/false);
25012 if (specs && specs != error_mark_node)
25013 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25014 specs);
25015 }
25016
25017 /* Merge parameter declarations. */
25018 decl_parm = skip_artificial_parms_for (decl,
25019 DECL_ARGUMENTS (decl));
25020 pattern_parm
25021 = skip_artificial_parms_for (code_pattern,
25022 DECL_ARGUMENTS (code_pattern));
25023 while (decl_parm && !DECL_PACK_P (pattern_parm))
25024 {
25025 tree parm_type;
25026 tree attributes;
25027
25028 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25029 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25030 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25031 NULL_TREE);
25032 parm_type = type_decays_to (parm_type);
25033 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25034 TREE_TYPE (decl_parm) = parm_type;
25035 attributes = DECL_ATTRIBUTES (pattern_parm);
25036 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25037 {
25038 DECL_ATTRIBUTES (decl_parm) = attributes;
25039 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25040 }
25041 decl_parm = DECL_CHAIN (decl_parm);
25042 pattern_parm = DECL_CHAIN (pattern_parm);
25043 }
25044 /* Merge any parameters that match with the function parameter
25045 pack. */
25046 if (pattern_parm && DECL_PACK_P (pattern_parm))
25047 {
25048 int i, len;
25049 tree expanded_types;
25050 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25051 the parameters in this function parameter pack. */
25052 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25053 args, tf_error, NULL_TREE);
25054 len = TREE_VEC_LENGTH (expanded_types);
25055 for (i = 0; i < len; i++)
25056 {
25057 tree parm_type;
25058 tree attributes;
25059
25060 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25061 /* Rename the parameter to include the index. */
25062 DECL_NAME (decl_parm) =
25063 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25064 parm_type = TREE_VEC_ELT (expanded_types, i);
25065 parm_type = type_decays_to (parm_type);
25066 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25067 TREE_TYPE (decl_parm) = parm_type;
25068 attributes = DECL_ATTRIBUTES (pattern_parm);
25069 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25070 {
25071 DECL_ATTRIBUTES (decl_parm) = attributes;
25072 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25073 }
25074 decl_parm = DECL_CHAIN (decl_parm);
25075 }
25076 }
25077 /* Merge additional specifiers from the CODE_PATTERN. */
25078 if (DECL_DECLARED_INLINE_P (code_pattern)
25079 && !DECL_DECLARED_INLINE_P (decl))
25080 DECL_DECLARED_INLINE_P (decl) = 1;
25081
25082 maybe_instantiate_noexcept (decl, tf_error);
25083 }
25084 else if (VAR_P (decl))
25085 {
25086 start_lambda_scope (decl);
25087 DECL_INITIAL (decl) =
25088 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25089 tf_error, DECL_TI_TEMPLATE (decl));
25090 finish_lambda_scope ();
25091 if (VAR_HAD_UNKNOWN_BOUND (decl))
25092 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25093 tf_error, DECL_TI_TEMPLATE (decl));
25094 }
25095 else
25096 gcc_unreachable ();
25097
25098 pop_access_scope (decl);
25099 }
25100
25101 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25102 substituted to get DECL. */
25103
25104 tree
25105 template_for_substitution (tree decl)
25106 {
25107 tree tmpl = DECL_TI_TEMPLATE (decl);
25108
25109 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25110 for the instantiation. This is not always the most general
25111 template. Consider, for example:
25112
25113 template <class T>
25114 struct S { template <class U> void f();
25115 template <> void f<int>(); };
25116
25117 and an instantiation of S<double>::f<int>. We want TD to be the
25118 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25119 while (/* An instantiation cannot have a definition, so we need a
25120 more general template. */
25121 DECL_TEMPLATE_INSTANTIATION (tmpl)
25122 /* We must also deal with friend templates. Given:
25123
25124 template <class T> struct S {
25125 template <class U> friend void f() {};
25126 };
25127
25128 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25129 so far as the language is concerned, but that's still
25130 where we get the pattern for the instantiation from. On
25131 other hand, if the definition comes outside the class, say:
25132
25133 template <class T> struct S {
25134 template <class U> friend void f();
25135 };
25136 template <class U> friend void f() {}
25137
25138 we don't need to look any further. That's what the check for
25139 DECL_INITIAL is for. */
25140 || (TREE_CODE (decl) == FUNCTION_DECL
25141 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25142 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25143 {
25144 /* The present template, TD, should not be a definition. If it
25145 were a definition, we should be using it! Note that we
25146 cannot restructure the loop to just keep going until we find
25147 a template with a definition, since that might go too far if
25148 a specialization was declared, but not defined. */
25149
25150 /* Fetch the more general template. */
25151 tmpl = DECL_TI_TEMPLATE (tmpl);
25152 }
25153
25154 return tmpl;
25155 }
25156
25157 /* Returns true if we need to instantiate this template instance even if we
25158 know we aren't going to emit it. */
25159
25160 bool
25161 always_instantiate_p (tree decl)
25162 {
25163 /* We always instantiate inline functions so that we can inline them. An
25164 explicit instantiation declaration prohibits implicit instantiation of
25165 non-inline functions. With high levels of optimization, we would
25166 normally inline non-inline functions -- but we're not allowed to do
25167 that for "extern template" functions. Therefore, we check
25168 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25169 return ((TREE_CODE (decl) == FUNCTION_DECL
25170 && (DECL_DECLARED_INLINE_P (decl)
25171 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25172 /* And we need to instantiate static data members so that
25173 their initializers are available in integral constant
25174 expressions. */
25175 || (VAR_P (decl)
25176 && decl_maybe_constant_var_p (decl)));
25177 }
25178
25179 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25180 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25181 error, true otherwise. */
25182
25183 bool
25184 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25185 {
25186 tree fntype, spec, noex;
25187
25188 /* Don't instantiate a noexcept-specification from template context. */
25189 if (processing_template_decl
25190 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25191 return true;
25192
25193 if (DECL_MAYBE_DELETED (fn))
25194 {
25195 if (fn == current_function_decl)
25196 /* We're in start_preparsed_function, keep going. */
25197 return true;
25198
25199 ++function_depth;
25200 synthesize_method (fn);
25201 --function_depth;
25202 return !DECL_MAYBE_DELETED (fn);
25203 }
25204
25205 fntype = TREE_TYPE (fn);
25206 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25207
25208 if (!spec || !TREE_PURPOSE (spec))
25209 return true;
25210
25211 noex = TREE_PURPOSE (spec);
25212 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25213 && TREE_CODE (noex) != DEFERRED_PARSE)
25214 return true;
25215
25216 tree orig_fn = NULL_TREE;
25217 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25218 its FUNCTION_DECL for the rest of this function -- push_access_scope
25219 doesn't accept TEMPLATE_DECLs. */
25220 if (DECL_FUNCTION_TEMPLATE_P (fn))
25221 {
25222 orig_fn = fn;
25223 fn = DECL_TEMPLATE_RESULT (fn);
25224 }
25225
25226 if (DECL_CLONED_FUNCTION_P (fn))
25227 {
25228 tree prime = DECL_CLONED_FUNCTION (fn);
25229 if (!maybe_instantiate_noexcept (prime, complain))
25230 return false;
25231 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25232 }
25233 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25234 {
25235 static hash_set<tree>* fns = new hash_set<tree>;
25236 bool added = false;
25237 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25238 {
25239 spec = get_defaulted_eh_spec (fn, complain);
25240 if (spec == error_mark_node)
25241 /* This might have failed because of an unparsed DMI, so
25242 let's try again later. */
25243 return false;
25244 }
25245 else if (!(added = !fns->add (fn)))
25246 {
25247 /* If hash_set::add returns true, the element was already there. */
25248 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25249 DECL_SOURCE_LOCATION (fn));
25250 error_at (loc,
25251 "exception specification of %qD depends on itself",
25252 fn);
25253 spec = noexcept_false_spec;
25254 }
25255 else if (push_tinst_level (fn))
25256 {
25257 push_to_top_level ();
25258 push_access_scope (fn);
25259 push_deferring_access_checks (dk_no_deferred);
25260 input_location = DECL_SOURCE_LOCATION (fn);
25261
25262 /* If needed, set current_class_ptr for the benefit of
25263 tsubst_copy/PARM_DECL. */
25264 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25265 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25266 {
25267 tree this_parm = DECL_ARGUMENTS (tdecl);
25268 current_class_ptr = NULL_TREE;
25269 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25270 current_class_ptr = this_parm;
25271 }
25272
25273 /* If this function is represented by a TEMPLATE_DECL, then
25274 the deferred noexcept-specification might still contain
25275 dependent types, even after substitution. And we need the
25276 dependency check functions to work in build_noexcept_spec. */
25277 if (orig_fn)
25278 ++processing_template_decl;
25279
25280 /* Do deferred instantiation of the noexcept-specifier. */
25281 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25282 DEFERRED_NOEXCEPT_ARGS (noex),
25283 tf_warning_or_error, fn,
25284 /*function_p=*/false,
25285 /*i_c_e_p=*/true);
25286
25287 /* Build up the noexcept-specification. */
25288 spec = build_noexcept_spec (noex, tf_warning_or_error);
25289
25290 if (orig_fn)
25291 --processing_template_decl;
25292
25293 pop_deferring_access_checks ();
25294 pop_access_scope (fn);
25295 pop_tinst_level ();
25296 pop_from_top_level ();
25297 }
25298 else
25299 spec = noexcept_false_spec;
25300
25301 if (added)
25302 fns->remove (fn);
25303 }
25304
25305 if (spec == error_mark_node)
25306 {
25307 /* This failed with a hard error, so let's go with false. */
25308 gcc_assert (seen_error ());
25309 spec = noexcept_false_spec;
25310 }
25311
25312 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25313 if (orig_fn)
25314 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25315
25316 return true;
25317 }
25318
25319 /* We're starting to process the function INST, an instantiation of PATTERN;
25320 add their parameters to local_specializations. */
25321
25322 static void
25323 register_parameter_specializations (tree pattern, tree inst)
25324 {
25325 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25326 tree spec_parm = DECL_ARGUMENTS (inst);
25327 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25328 {
25329 register_local_specialization (spec_parm, tmpl_parm);
25330 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25331 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25332 }
25333 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25334 {
25335 if (!DECL_PACK_P (tmpl_parm)
25336 || (spec_parm && DECL_PACK_P (spec_parm)))
25337 {
25338 register_local_specialization (spec_parm, tmpl_parm);
25339 spec_parm = DECL_CHAIN (spec_parm);
25340 }
25341 else
25342 {
25343 /* Register the (value) argument pack as a specialization of
25344 TMPL_PARM, then move on. */
25345 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25346 register_local_specialization (argpack, tmpl_parm);
25347 }
25348 }
25349 gcc_assert (!spec_parm);
25350 }
25351
25352 /* Produce the definition of D, a _DECL generated from a template. If
25353 DEFER_OK is true, then we don't have to actually do the
25354 instantiation now; we just have to do it sometime. Normally it is
25355 an error if this is an explicit instantiation but D is undefined.
25356 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25357 instantiated class template. */
25358
25359 tree
25360 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25361 {
25362 tree tmpl = DECL_TI_TEMPLATE (d);
25363 tree gen_args;
25364 tree args;
25365 tree td;
25366 tree code_pattern;
25367 tree spec;
25368 tree gen_tmpl;
25369 bool pattern_defined;
25370 location_t saved_loc = input_location;
25371 int saved_unevaluated_operand = cp_unevaluated_operand;
25372 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25373 bool external_p;
25374 bool deleted_p;
25375
25376 /* This function should only be used to instantiate templates for
25377 functions and static member variables. */
25378 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25379
25380 /* A concept is never instantiated. */
25381 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25382
25383 /* Variables are never deferred; if instantiation is required, they
25384 are instantiated right away. That allows for better code in the
25385 case that an expression refers to the value of the variable --
25386 if the variable has a constant value the referring expression can
25387 take advantage of that fact. */
25388 if (VAR_P (d))
25389 defer_ok = false;
25390
25391 /* Don't instantiate cloned functions. Instead, instantiate the
25392 functions they cloned. */
25393 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25394 d = DECL_CLONED_FUNCTION (d);
25395
25396 if (DECL_TEMPLATE_INSTANTIATED (d)
25397 || TREE_TYPE (d) == error_mark_node
25398 || (TREE_CODE (d) == FUNCTION_DECL
25399 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25400 || DECL_TEMPLATE_SPECIALIZATION (d))
25401 /* D has already been instantiated or explicitly specialized, so
25402 there's nothing for us to do here.
25403
25404 It might seem reasonable to check whether or not D is an explicit
25405 instantiation, and, if so, stop here. But when an explicit
25406 instantiation is deferred until the end of the compilation,
25407 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25408 the instantiation. */
25409 return d;
25410
25411 /* Check to see whether we know that this template will be
25412 instantiated in some other file, as with "extern template"
25413 extension. */
25414 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25415
25416 /* In general, we do not instantiate such templates. */
25417 if (external_p && !always_instantiate_p (d))
25418 return d;
25419
25420 gen_tmpl = most_general_template (tmpl);
25421 gen_args = DECL_TI_ARGS (d);
25422
25423 /* We should already have the extra args. */
25424 gcc_checking_assert (tmpl == gen_tmpl
25425 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25426 == TMPL_ARGS_DEPTH (gen_args)));
25427 /* And what's in the hash table should match D. */
25428 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25429 == d
25430 || spec == NULL_TREE);
25431
25432 /* This needs to happen before any tsubsting. */
25433 if (! push_tinst_level (d))
25434 return d;
25435
25436 timevar_push (TV_TEMPLATE_INST);
25437
25438 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25439 for the instantiation. */
25440 td = template_for_substitution (d);
25441 args = gen_args;
25442
25443 if (VAR_P (d))
25444 {
25445 /* Look up an explicit specialization, if any. */
25446 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25447 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25448 if (elt && elt != error_mark_node)
25449 {
25450 td = TREE_VALUE (elt);
25451 args = TREE_PURPOSE (elt);
25452 }
25453 }
25454
25455 code_pattern = DECL_TEMPLATE_RESULT (td);
25456
25457 /* We should never be trying to instantiate a member of a class
25458 template or partial specialization. */
25459 gcc_assert (d != code_pattern);
25460
25461 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25462 || DECL_TEMPLATE_SPECIALIZATION (td))
25463 /* In the case of a friend template whose definition is provided
25464 outside the class, we may have too many arguments. Drop the
25465 ones we don't need. The same is true for specializations. */
25466 args = get_innermost_template_args
25467 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25468
25469 if (TREE_CODE (d) == FUNCTION_DECL)
25470 {
25471 deleted_p = DECL_DELETED_FN (code_pattern);
25472 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25473 && DECL_INITIAL (code_pattern) != error_mark_node)
25474 || DECL_DEFAULTED_FN (code_pattern)
25475 || deleted_p);
25476 }
25477 else
25478 {
25479 deleted_p = false;
25480 if (DECL_CLASS_SCOPE_P (code_pattern))
25481 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25482 else
25483 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25484 }
25485
25486 /* We may be in the middle of deferred access check. Disable it now. */
25487 push_deferring_access_checks (dk_no_deferred);
25488
25489 /* Unless an explicit instantiation directive has already determined
25490 the linkage of D, remember that a definition is available for
25491 this entity. */
25492 if (pattern_defined
25493 && !DECL_INTERFACE_KNOWN (d)
25494 && !DECL_NOT_REALLY_EXTERN (d))
25495 mark_definable (d);
25496
25497 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25498 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25499 input_location = DECL_SOURCE_LOCATION (d);
25500
25501 /* If D is a member of an explicitly instantiated class template,
25502 and no definition is available, treat it like an implicit
25503 instantiation. */
25504 if (!pattern_defined && expl_inst_class_mem_p
25505 && DECL_EXPLICIT_INSTANTIATION (d))
25506 {
25507 /* Leave linkage flags alone on instantiations with anonymous
25508 visibility. */
25509 if (TREE_PUBLIC (d))
25510 {
25511 DECL_NOT_REALLY_EXTERN (d) = 0;
25512 DECL_INTERFACE_KNOWN (d) = 0;
25513 }
25514 SET_DECL_IMPLICIT_INSTANTIATION (d);
25515 }
25516
25517 /* Defer all other templates, unless we have been explicitly
25518 forbidden from doing so. */
25519 if (/* If there is no definition, we cannot instantiate the
25520 template. */
25521 ! pattern_defined
25522 /* If it's OK to postpone instantiation, do so. */
25523 || defer_ok
25524 /* If this is a static data member that will be defined
25525 elsewhere, we don't want to instantiate the entire data
25526 member, but we do want to instantiate the initializer so that
25527 we can substitute that elsewhere. */
25528 || (external_p && VAR_P (d))
25529 /* Handle here a deleted function too, avoid generating
25530 its body (c++/61080). */
25531 || deleted_p)
25532 {
25533 /* The definition of the static data member is now required so
25534 we must substitute the initializer. */
25535 if (VAR_P (d)
25536 && !DECL_INITIAL (d)
25537 && DECL_INITIAL (code_pattern))
25538 {
25539 tree ns;
25540 tree init;
25541 bool const_init = false;
25542 bool enter_context = DECL_CLASS_SCOPE_P (d);
25543
25544 ns = decl_namespace_context (d);
25545 push_nested_namespace (ns);
25546 if (enter_context)
25547 push_nested_class (DECL_CONTEXT (d));
25548 init = tsubst_expr (DECL_INITIAL (code_pattern),
25549 args,
25550 tf_warning_or_error, NULL_TREE,
25551 /*integral_constant_expression_p=*/false);
25552 /* If instantiating the initializer involved instantiating this
25553 again, don't call cp_finish_decl twice. */
25554 if (!DECL_INITIAL (d))
25555 {
25556 /* Make sure the initializer is still constant, in case of
25557 circular dependency (template/instantiate6.C). */
25558 const_init
25559 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25560 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25561 /*asmspec_tree=*/NULL_TREE,
25562 LOOKUP_ONLYCONVERTING);
25563 }
25564 if (enter_context)
25565 pop_nested_class ();
25566 pop_nested_namespace (ns);
25567 }
25568
25569 /* We restore the source position here because it's used by
25570 add_pending_template. */
25571 input_location = saved_loc;
25572
25573 if (at_eof && !pattern_defined
25574 && DECL_EXPLICIT_INSTANTIATION (d)
25575 && DECL_NOT_REALLY_EXTERN (d))
25576 /* [temp.explicit]
25577
25578 The definition of a non-exported function template, a
25579 non-exported member function template, or a non-exported
25580 member function or static data member of a class template
25581 shall be present in every translation unit in which it is
25582 explicitly instantiated. */
25583 permerror (input_location, "explicit instantiation of %qD "
25584 "but no definition available", d);
25585
25586 /* If we're in unevaluated context, we just wanted to get the
25587 constant value; this isn't an odr use, so don't queue
25588 a full instantiation. */
25589 if (cp_unevaluated_operand != 0)
25590 goto out;
25591 /* ??? Historically, we have instantiated inline functions, even
25592 when marked as "extern template". */
25593 if (!(external_p && VAR_P (d)))
25594 add_pending_template (d);
25595 goto out;
25596 }
25597
25598 bool push_to_top, nested;
25599 tree fn_context;
25600 fn_context = decl_function_context (d);
25601 if (LAMBDA_FUNCTION_P (d))
25602 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25603 fn_context = NULL_TREE;
25604 nested = current_function_decl != NULL_TREE;
25605 push_to_top = !(nested && fn_context == current_function_decl);
25606
25607 vec<tree> omp_privatization_save;
25608 if (nested)
25609 save_omp_privatization_clauses (omp_privatization_save);
25610
25611 if (push_to_top)
25612 push_to_top_level ();
25613 else
25614 {
25615 gcc_assert (!processing_template_decl);
25616 push_function_context ();
25617 cp_unevaluated_operand = 0;
25618 c_inhibit_evaluation_warnings = 0;
25619 }
25620
25621 if (VAR_P (d))
25622 {
25623 /* The variable might be a lambda's extra scope, and that
25624 lambda's visibility depends on D's. */
25625 maybe_commonize_var (d);
25626 determine_visibility (d);
25627 }
25628
25629 /* Mark D as instantiated so that recursive calls to
25630 instantiate_decl do not try to instantiate it again. */
25631 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25632
25633 /* Regenerate the declaration in case the template has been modified
25634 by a subsequent redeclaration. */
25635 regenerate_decl_from_template (d, td, args);
25636
25637 /* We already set the file and line above. Reset them now in case
25638 they changed as a result of calling regenerate_decl_from_template. */
25639 input_location = DECL_SOURCE_LOCATION (d);
25640
25641 if (VAR_P (d))
25642 {
25643 tree init;
25644 bool const_init = false;
25645
25646 /* Clear out DECL_RTL; whatever was there before may not be right
25647 since we've reset the type of the declaration. */
25648 SET_DECL_RTL (d, NULL);
25649 DECL_IN_AGGR_P (d) = 0;
25650
25651 /* The initializer is placed in DECL_INITIAL by
25652 regenerate_decl_from_template so we don't need to
25653 push/pop_access_scope again here. Pull it out so that
25654 cp_finish_decl can process it. */
25655 init = DECL_INITIAL (d);
25656 DECL_INITIAL (d) = NULL_TREE;
25657 DECL_INITIALIZED_P (d) = 0;
25658
25659 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25660 initializer. That function will defer actual emission until
25661 we have a chance to determine linkage. */
25662 DECL_EXTERNAL (d) = 0;
25663
25664 /* Enter the scope of D so that access-checking works correctly. */
25665 bool enter_context = DECL_CLASS_SCOPE_P (d);
25666 if (enter_context)
25667 push_nested_class (DECL_CONTEXT (d));
25668
25669 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25670 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
25671 ? LOOKUP_CONSTINIT : 0);
25672 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
25673
25674 if (enter_context)
25675 pop_nested_class ();
25676
25677 if (variable_template_p (gen_tmpl))
25678 note_variable_template_instantiation (d);
25679 }
25680 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25681 synthesize_method (d);
25682 else if (TREE_CODE (d) == FUNCTION_DECL)
25683 {
25684 /* Set up the list of local specializations. */
25685 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25686 tree block = NULL_TREE;
25687
25688 /* Set up context. */
25689 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25690 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25691 block = push_stmt_list ();
25692 else
25693 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25694
25695 perform_instantiation_time_access_checks (DECL_TEMPLATE_RESULT (td),
25696 args);
25697
25698 /* Create substitution entries for the parameters. */
25699 register_parameter_specializations (code_pattern, d);
25700
25701 /* Substitute into the body of the function. */
25702 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25703 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25704 tf_warning_or_error, tmpl);
25705 else
25706 {
25707 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25708 tf_warning_or_error, tmpl,
25709 /*integral_constant_expression_p=*/false);
25710
25711 /* Set the current input_location to the end of the function
25712 so that finish_function knows where we are. */
25713 input_location
25714 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25715
25716 /* Remember if we saw an infinite loop in the template. */
25717 current_function_infinite_loop
25718 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25719 }
25720
25721 /* Finish the function. */
25722 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25723 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25724 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25725 else
25726 {
25727 d = finish_function (/*inline_p=*/false);
25728 expand_or_defer_fn (d);
25729 }
25730
25731 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25732 cp_check_omp_declare_reduction (d);
25733 }
25734
25735 /* We're not deferring instantiation any more. */
25736 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25737
25738 if (push_to_top)
25739 pop_from_top_level ();
25740 else
25741 pop_function_context ();
25742
25743 if (nested)
25744 restore_omp_privatization_clauses (omp_privatization_save);
25745
25746 out:
25747 pop_deferring_access_checks ();
25748 timevar_pop (TV_TEMPLATE_INST);
25749 pop_tinst_level ();
25750 input_location = saved_loc;
25751 cp_unevaluated_operand = saved_unevaluated_operand;
25752 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25753
25754 return d;
25755 }
25756
25757 /* Run through the list of templates that we wish we could
25758 instantiate, and instantiate any we can. RETRIES is the
25759 number of times we retry pending template instantiation. */
25760
25761 void
25762 instantiate_pending_templates (int retries)
25763 {
25764 int reconsider;
25765 location_t saved_loc = input_location;
25766
25767 /* Instantiating templates may trigger vtable generation. This in turn
25768 may require further template instantiations. We place a limit here
25769 to avoid infinite loop. */
25770 if (pending_templates && retries >= max_tinst_depth)
25771 {
25772 tree decl = pending_templates->tinst->maybe_get_node ();
25773
25774 fatal_error (input_location,
25775 "template instantiation depth exceeds maximum of %d"
25776 " instantiating %q+D, possibly from virtual table generation"
25777 " (use %<-ftemplate-depth=%> to increase the maximum)",
25778 max_tinst_depth, decl);
25779 if (TREE_CODE (decl) == FUNCTION_DECL)
25780 /* Pretend that we defined it. */
25781 DECL_INITIAL (decl) = error_mark_node;
25782 return;
25783 }
25784
25785 do
25786 {
25787 struct pending_template **t = &pending_templates;
25788 struct pending_template *last = NULL;
25789 reconsider = 0;
25790 while (*t)
25791 {
25792 tree instantiation = reopen_tinst_level ((*t)->tinst);
25793 bool complete = false;
25794
25795 if (TYPE_P (instantiation))
25796 {
25797 if (!COMPLETE_TYPE_P (instantiation))
25798 {
25799 instantiate_class_template (instantiation);
25800 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25801 for (tree fld = TYPE_FIELDS (instantiation);
25802 fld; fld = TREE_CHAIN (fld))
25803 if ((VAR_P (fld)
25804 || (TREE_CODE (fld) == FUNCTION_DECL
25805 && !DECL_ARTIFICIAL (fld)))
25806 && DECL_TEMPLATE_INSTANTIATION (fld))
25807 instantiate_decl (fld,
25808 /*defer_ok=*/false,
25809 /*expl_inst_class_mem_p=*/false);
25810
25811 if (COMPLETE_TYPE_P (instantiation))
25812 reconsider = 1;
25813 }
25814
25815 complete = COMPLETE_TYPE_P (instantiation);
25816 }
25817 else
25818 {
25819 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25820 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25821 {
25822 instantiation
25823 = instantiate_decl (instantiation,
25824 /*defer_ok=*/false,
25825 /*expl_inst_class_mem_p=*/false);
25826 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25827 reconsider = 1;
25828 }
25829
25830 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25831 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25832 }
25833
25834 if (complete)
25835 {
25836 /* If INSTANTIATION has been instantiated, then we don't
25837 need to consider it again in the future. */
25838 struct pending_template *drop = *t;
25839 *t = (*t)->next;
25840 set_refcount_ptr (drop->tinst);
25841 pending_template_freelist ().free (drop);
25842 }
25843 else
25844 {
25845 last = *t;
25846 t = &(*t)->next;
25847 }
25848 tinst_depth = 0;
25849 set_refcount_ptr (current_tinst_level);
25850 }
25851 last_pending_template = last;
25852 }
25853 while (reconsider);
25854
25855 input_location = saved_loc;
25856 }
25857
25858 /* Substitute ARGVEC into T, which is a list of initializers for
25859 either base class or a non-static data member. The TREE_PURPOSEs
25860 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25861 instantiate_decl. */
25862
25863 static tree
25864 tsubst_initializer_list (tree t, tree argvec)
25865 {
25866 tree inits = NULL_TREE;
25867 tree target_ctor = error_mark_node;
25868
25869 for (; t; t = TREE_CHAIN (t))
25870 {
25871 tree decl;
25872 tree init;
25873 tree expanded_bases = NULL_TREE;
25874 tree expanded_arguments = NULL_TREE;
25875 int i, len = 1;
25876
25877 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25878 {
25879 tree expr;
25880 tree arg;
25881
25882 /* Expand the base class expansion type into separate base
25883 classes. */
25884 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25885 tf_warning_or_error,
25886 NULL_TREE);
25887 if (expanded_bases == error_mark_node)
25888 continue;
25889
25890 /* We'll be building separate TREE_LISTs of arguments for
25891 each base. */
25892 len = TREE_VEC_LENGTH (expanded_bases);
25893 expanded_arguments = make_tree_vec (len);
25894 for (i = 0; i < len; i++)
25895 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
25896
25897 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25898 expand each argument in the TREE_VALUE of t. */
25899 expr = make_node (EXPR_PACK_EXPANSION);
25900 PACK_EXPANSION_LOCAL_P (expr) = true;
25901 PACK_EXPANSION_PARAMETER_PACKS (expr) =
25902 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
25903
25904 if (TREE_VALUE (t) == void_type_node)
25905 /* VOID_TYPE_NODE is used to indicate
25906 value-initialization. */
25907 {
25908 for (i = 0; i < len; i++)
25909 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
25910 }
25911 else
25912 {
25913 /* Substitute parameter packs into each argument in the
25914 TREE_LIST. */
25915 in_base_initializer = 1;
25916 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
25917 {
25918 tree expanded_exprs;
25919
25920 /* Expand the argument. */
25921 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25922 expanded_exprs
25923 = tsubst_pack_expansion (expr, argvec,
25924 tf_warning_or_error,
25925 NULL_TREE);
25926 if (expanded_exprs == error_mark_node)
25927 continue;
25928
25929 /* Prepend each of the expanded expressions to the
25930 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25931 for (i = 0; i < len; i++)
25932 {
25933 TREE_VEC_ELT (expanded_arguments, i) =
25934 tree_cons (NULL_TREE,
25935 TREE_VEC_ELT (expanded_exprs, i),
25936 TREE_VEC_ELT (expanded_arguments, i));
25937 }
25938 }
25939 in_base_initializer = 0;
25940
25941 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25942 since we built them backwards. */
25943 for (i = 0; i < len; i++)
25944 {
25945 TREE_VEC_ELT (expanded_arguments, i) =
25946 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25947 }
25948 }
25949 }
25950
25951 for (i = 0; i < len; ++i)
25952 {
25953 if (expanded_bases)
25954 {
25955 decl = TREE_VEC_ELT (expanded_bases, i);
25956 decl = expand_member_init (decl);
25957 init = TREE_VEC_ELT (expanded_arguments, i);
25958 }
25959 else
25960 {
25961 tree tmp;
25962 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25963 tf_warning_or_error, NULL_TREE);
25964
25965 decl = expand_member_init (decl);
25966 if (decl && !DECL_P (decl))
25967 in_base_initializer = 1;
25968
25969 init = TREE_VALUE (t);
25970 tmp = init;
25971 if (init != void_type_node)
25972 init = tsubst_expr (init, argvec,
25973 tf_warning_or_error, NULL_TREE,
25974 /*integral_constant_expression_p=*/false);
25975 if (init == NULL_TREE && tmp != NULL_TREE)
25976 /* If we had an initializer but it instantiated to nothing,
25977 value-initialize the object. This will only occur when
25978 the initializer was a pack expansion where the parameter
25979 packs used in that expansion were of length zero. */
25980 init = void_type_node;
25981 in_base_initializer = 0;
25982 }
25983
25984 if (target_ctor != error_mark_node
25985 && init != error_mark_node)
25986 {
25987 error ("mem-initializer for %qD follows constructor delegation",
25988 decl);
25989 return inits;
25990 }
25991 /* Look for a target constructor. */
25992 if (init != error_mark_node
25993 && decl && CLASS_TYPE_P (decl)
25994 && same_type_p (decl, current_class_type))
25995 {
25996 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25997 if (inits)
25998 {
25999 error ("constructor delegation follows mem-initializer for %qD",
26000 TREE_PURPOSE (inits));
26001 continue;
26002 }
26003 target_ctor = init;
26004 }
26005
26006 if (decl)
26007 {
26008 init = build_tree_list (decl, init);
26009 TREE_CHAIN (init) = inits;
26010 inits = init;
26011 }
26012 }
26013 }
26014 return inits;
26015 }
26016
26017 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26018
26019 static void
26020 set_current_access_from_decl (tree decl)
26021 {
26022 if (TREE_PRIVATE (decl))
26023 current_access_specifier = access_private_node;
26024 else if (TREE_PROTECTED (decl))
26025 current_access_specifier = access_protected_node;
26026 else
26027 current_access_specifier = access_public_node;
26028 }
26029
26030 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26031 is the instantiation (which should have been created with
26032 start_enum) and ARGS are the template arguments to use. */
26033
26034 static void
26035 tsubst_enum (tree tag, tree newtag, tree args)
26036 {
26037 tree e;
26038
26039 if (SCOPED_ENUM_P (newtag))
26040 begin_scope (sk_scoped_enum, newtag);
26041
26042 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26043 {
26044 tree value;
26045 tree decl;
26046
26047 decl = TREE_VALUE (e);
26048 /* Note that in a template enum, the TREE_VALUE is the
26049 CONST_DECL, not the corresponding INTEGER_CST. */
26050 value = tsubst_expr (DECL_INITIAL (decl),
26051 args, tf_warning_or_error, NULL_TREE,
26052 /*integral_constant_expression_p=*/true);
26053
26054 /* Give this enumeration constant the correct access. */
26055 set_current_access_from_decl (decl);
26056
26057 /* Actually build the enumerator itself. Here we're assuming that
26058 enumerators can't have dependent attributes. */
26059 build_enumerator (DECL_NAME (decl), value, newtag,
26060 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26061 }
26062
26063 if (SCOPED_ENUM_P (newtag))
26064 finish_scope ();
26065
26066 finish_enum_value_list (newtag);
26067 finish_enum (newtag);
26068
26069 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26070 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26071 }
26072
26073 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26074 its type -- but without substituting the innermost set of template
26075 arguments. So, innermost set of template parameters will appear in
26076 the type. */
26077
26078 tree
26079 get_mostly_instantiated_function_type (tree decl)
26080 {
26081 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26082 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26083 }
26084
26085 /* Return truthvalue if we're processing a template different from
26086 the last one involved in diagnostics. */
26087 bool
26088 problematic_instantiation_changed (void)
26089 {
26090 return current_tinst_level != last_error_tinst_level;
26091 }
26092
26093 /* Remember current template involved in diagnostics. */
26094 void
26095 record_last_problematic_instantiation (void)
26096 {
26097 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26098 }
26099
26100 struct tinst_level *
26101 current_instantiation (void)
26102 {
26103 return current_tinst_level;
26104 }
26105
26106 /* Return TRUE if current_function_decl is being instantiated, false
26107 otherwise. */
26108
26109 bool
26110 instantiating_current_function_p (void)
26111 {
26112 return (current_instantiation ()
26113 && (current_instantiation ()->maybe_get_node ()
26114 == current_function_decl));
26115 }
26116
26117 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26118 type. Return false for ok, true for disallowed. Issue error and
26119 inform messages under control of COMPLAIN. */
26120
26121 static bool
26122 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26123 {
26124 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26125 return false;
26126 else if (TYPE_PTR_P (type))
26127 return false;
26128 else if (TYPE_REF_P (type)
26129 && !TYPE_REF_IS_RVALUE (type))
26130 return false;
26131 else if (TYPE_PTRMEM_P (type))
26132 return false;
26133 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26134 {
26135 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26136 {
26137 if (complain & tf_error)
26138 error ("non-type template parameters of deduced class type only "
26139 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26140 return true;
26141 }
26142 return false;
26143 }
26144 else if (TREE_CODE (type) == TYPENAME_TYPE)
26145 return false;
26146 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26147 return false;
26148 else if (TREE_CODE (type) == NULLPTR_TYPE)
26149 return false;
26150 /* A bound template template parm could later be instantiated to have a valid
26151 nontype parm type via an alias template. */
26152 else if (cxx_dialect >= cxx11
26153 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26154 return false;
26155 else if (VOID_TYPE_P (type))
26156 /* Fall through. */;
26157 else if (cxx_dialect >= cxx20)
26158 {
26159 if (dependent_type_p (type))
26160 return false;
26161 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26162 return true;
26163 if (structural_type_p (type))
26164 return false;
26165 if (complain & tf_error)
26166 {
26167 auto_diagnostic_group d;
26168 error ("%qT is not a valid type for a template non-type "
26169 "parameter because it is not structural", type);
26170 structural_type_p (type, true);
26171 }
26172 return true;
26173 }
26174 else if (CLASS_TYPE_P (type))
26175 {
26176 if (complain & tf_error)
26177 error ("non-type template parameters of class type only available "
26178 "with %<-std=c++20%> or %<-std=gnu++20%>");
26179 return true;
26180 }
26181
26182 if (complain & tf_error)
26183 {
26184 if (type == error_mark_node)
26185 inform (input_location, "invalid template non-type parameter");
26186 else
26187 error ("%q#T is not a valid type for a template non-type parameter",
26188 type);
26189 }
26190 return true;
26191 }
26192
26193 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26194 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26195
26196 static bool
26197 dependent_type_p_r (tree type)
26198 {
26199 tree scope;
26200
26201 /* [temp.dep.type]
26202
26203 A type is dependent if it is:
26204
26205 -- a template parameter. Template template parameters are types
26206 for us (since TYPE_P holds true for them) so we handle
26207 them here. */
26208 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26209 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26210 return true;
26211 /* -- a qualified-id with a nested-name-specifier which contains a
26212 class-name that names a dependent type or whose unqualified-id
26213 names a dependent type. */
26214 if (TREE_CODE (type) == TYPENAME_TYPE)
26215 return true;
26216
26217 /* An alias template specialization can be dependent even if the
26218 resulting type is not. */
26219 if (dependent_alias_template_spec_p (type, nt_transparent))
26220 return true;
26221
26222 /* -- a cv-qualified type where the cv-unqualified type is
26223 dependent.
26224 No code is necessary for this bullet; the code below handles
26225 cv-qualified types, and we don't want to strip aliases with
26226 TYPE_MAIN_VARIANT because of DR 1558. */
26227 /* -- a compound type constructed from any dependent type. */
26228 if (TYPE_PTRMEM_P (type))
26229 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26230 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26231 (type)));
26232 else if (INDIRECT_TYPE_P (type))
26233 return dependent_type_p (TREE_TYPE (type));
26234 else if (FUNC_OR_METHOD_TYPE_P (type))
26235 {
26236 tree arg_type;
26237
26238 if (dependent_type_p (TREE_TYPE (type)))
26239 return true;
26240 for (arg_type = TYPE_ARG_TYPES (type);
26241 arg_type;
26242 arg_type = TREE_CHAIN (arg_type))
26243 if (dependent_type_p (TREE_VALUE (arg_type)))
26244 return true;
26245 if (cxx_dialect >= cxx17)
26246 /* A value-dependent noexcept-specifier makes the type dependent. */
26247 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26248 if (tree noex = TREE_PURPOSE (spec))
26249 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26250 affect overload resolution and treating it as dependent breaks
26251 things. Same for an unparsed noexcept expression. */
26252 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26253 && TREE_CODE (noex) != DEFERRED_PARSE
26254 && value_dependent_expression_p (noex))
26255 return true;
26256 return false;
26257 }
26258 /* -- an array type constructed from any dependent type or whose
26259 size is specified by a constant expression that is
26260 value-dependent.
26261
26262 We checked for type- and value-dependence of the bounds in
26263 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26264 if (TREE_CODE (type) == ARRAY_TYPE)
26265 {
26266 if (TYPE_DOMAIN (type)
26267 && dependent_type_p (TYPE_DOMAIN (type)))
26268 return true;
26269 return dependent_type_p (TREE_TYPE (type));
26270 }
26271
26272 /* -- a template-id in which either the template name is a template
26273 parameter ... */
26274 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26275 return true;
26276 /* ... or any of the template arguments is a dependent type or
26277 an expression that is type-dependent or value-dependent. */
26278 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26279 && (any_dependent_template_arguments_p
26280 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26281 return true;
26282
26283 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26284 dependent; if the argument of the `typeof' expression is not
26285 type-dependent, then it should already been have resolved. */
26286 if (TREE_CODE (type) == TYPEOF_TYPE
26287 || TREE_CODE (type) == DECLTYPE_TYPE
26288 || TREE_CODE (type) == UNDERLYING_TYPE)
26289 return true;
26290
26291 /* A template argument pack is dependent if any of its packed
26292 arguments are. */
26293 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26294 {
26295 tree args = ARGUMENT_PACK_ARGS (type);
26296 int i, len = TREE_VEC_LENGTH (args);
26297 for (i = 0; i < len; ++i)
26298 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26299 return true;
26300 }
26301
26302 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26303 be template parameters. */
26304 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26305 return true;
26306
26307 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26308 return true;
26309
26310 /* The standard does not specifically mention types that are local
26311 to template functions or local classes, but they should be
26312 considered dependent too. For example:
26313
26314 template <int I> void f() {
26315 enum E { a = I };
26316 S<sizeof (E)> s;
26317 }
26318
26319 The size of `E' cannot be known until the value of `I' has been
26320 determined. Therefore, `E' must be considered dependent. */
26321 scope = TYPE_CONTEXT (type);
26322 if (scope && TYPE_P (scope))
26323 return dependent_type_p (scope);
26324 /* Don't use type_dependent_expression_p here, as it can lead
26325 to infinite recursion trying to determine whether a lambda
26326 nested in a lambda is dependent (c++/47687). */
26327 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26328 && DECL_LANG_SPECIFIC (scope)
26329 && DECL_TEMPLATE_INFO (scope)
26330 && (any_dependent_template_arguments_p
26331 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26332 return true;
26333
26334 /* Other types are non-dependent. */
26335 return false;
26336 }
26337
26338 /* Returns TRUE if TYPE is dependent, in the sense of
26339 [temp.dep.type]. Note that a NULL type is considered dependent. */
26340
26341 bool
26342 dependent_type_p (tree type)
26343 {
26344 /* If there are no template parameters in scope, then there can't be
26345 any dependent types. */
26346 if (!processing_template_decl)
26347 {
26348 /* If we are not processing a template, then nobody should be
26349 providing us with a dependent type. */
26350 gcc_assert (type);
26351 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26352 return false;
26353 }
26354
26355 /* If the type is NULL, we have not computed a type for the entity
26356 in question; in that case, the type is dependent. */
26357 if (!type)
26358 return true;
26359
26360 /* Erroneous types can be considered non-dependent. */
26361 if (type == error_mark_node)
26362 return false;
26363
26364 /* Getting here with global_type_node means we improperly called this
26365 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26366 gcc_checking_assert (type != global_type_node);
26367
26368 /* If we have not already computed the appropriate value for TYPE,
26369 do so now. */
26370 if (!TYPE_DEPENDENT_P_VALID (type))
26371 {
26372 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26373 TYPE_DEPENDENT_P_VALID (type) = 1;
26374 }
26375
26376 return TYPE_DEPENDENT_P (type);
26377 }
26378
26379 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26380 lookup. In other words, a dependent type that is not the current
26381 instantiation. */
26382
26383 bool
26384 dependent_scope_p (tree scope)
26385 {
26386 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26387 && !currently_open_class (scope));
26388 }
26389
26390 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26391 an unknown base of 'this' (and is therefore instantiation-dependent). */
26392
26393 static bool
26394 unknown_base_ref_p (tree t)
26395 {
26396 if (!current_class_ptr)
26397 return false;
26398
26399 tree mem = TREE_OPERAND (t, 1);
26400 if (shared_member_p (mem))
26401 return false;
26402
26403 tree cur = current_nonlambda_class_type ();
26404 if (!any_dependent_bases_p (cur))
26405 return false;
26406
26407 tree ctx = TREE_OPERAND (t, 0);
26408 if (DERIVED_FROM_P (ctx, cur))
26409 return false;
26410
26411 return true;
26412 }
26413
26414 /* T is a SCOPE_REF; return whether we need to consider it
26415 instantiation-dependent so that we can check access at instantiation
26416 time even though we know which member it resolves to. */
26417
26418 static bool
26419 instantiation_dependent_scope_ref_p (tree t)
26420 {
26421 if (DECL_P (TREE_OPERAND (t, 1))
26422 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26423 && !unknown_base_ref_p (t)
26424 && accessible_in_template_p (TREE_OPERAND (t, 0),
26425 TREE_OPERAND (t, 1)))
26426 return false;
26427 else
26428 return true;
26429 }
26430
26431 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26432 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26433 expression. */
26434
26435 /* Note that this predicate is not appropriate for general expressions;
26436 only constant expressions (that satisfy potential_constant_expression)
26437 can be tested for value dependence. */
26438
26439 bool
26440 value_dependent_expression_p (tree expression)
26441 {
26442 if (!processing_template_decl || expression == NULL_TREE)
26443 return false;
26444
26445 /* A type-dependent expression is also value-dependent. */
26446 if (type_dependent_expression_p (expression))
26447 return true;
26448
26449 switch (TREE_CODE (expression))
26450 {
26451 case BASELINK:
26452 /* A dependent member function of the current instantiation. */
26453 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26454
26455 case FUNCTION_DECL:
26456 /* A dependent member function of the current instantiation. */
26457 if (DECL_CLASS_SCOPE_P (expression)
26458 && dependent_type_p (DECL_CONTEXT (expression)))
26459 return true;
26460 break;
26461
26462 case IDENTIFIER_NODE:
26463 /* A name that has not been looked up -- must be dependent. */
26464 return true;
26465
26466 case TEMPLATE_PARM_INDEX:
26467 /* A non-type template parm. */
26468 return true;
26469
26470 case CONST_DECL:
26471 /* A non-type template parm. */
26472 if (DECL_TEMPLATE_PARM_P (expression))
26473 return true;
26474 return value_dependent_expression_p (DECL_INITIAL (expression));
26475
26476 case VAR_DECL:
26477 /* A constant with literal type and is initialized
26478 with an expression that is value-dependent. */
26479 if (DECL_DEPENDENT_INIT_P (expression)
26480 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26481 || TYPE_REF_P (TREE_TYPE (expression)))
26482 return true;
26483 if (DECL_HAS_VALUE_EXPR_P (expression))
26484 {
26485 tree value_expr = DECL_VALUE_EXPR (expression);
26486 if (value_dependent_expression_p (value_expr)
26487 /* __PRETTY_FUNCTION__ inside a template function is dependent
26488 on the name of the function. */
26489 || (DECL_PRETTY_FUNCTION_P (expression)
26490 /* It might be used in a template, but not a template
26491 function, in which case its DECL_VALUE_EXPR will be
26492 "top level". */
26493 && value_expr == error_mark_node))
26494 return true;
26495 }
26496 return false;
26497
26498 case DYNAMIC_CAST_EXPR:
26499 case STATIC_CAST_EXPR:
26500 case CONST_CAST_EXPR:
26501 case REINTERPRET_CAST_EXPR:
26502 case CAST_EXPR:
26503 case IMPLICIT_CONV_EXPR:
26504 /* These expressions are value-dependent if the type to which
26505 the cast occurs is dependent or the expression being casted
26506 is value-dependent. */
26507 {
26508 tree type = TREE_TYPE (expression);
26509
26510 if (dependent_type_p (type))
26511 return true;
26512
26513 /* A functional cast has a list of operands. */
26514 expression = TREE_OPERAND (expression, 0);
26515 if (!expression)
26516 {
26517 /* If there are no operands, it must be an expression such
26518 as "int()". This should not happen for aggregate types
26519 because it would form non-constant expressions. */
26520 gcc_assert (cxx_dialect >= cxx11
26521 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26522
26523 return false;
26524 }
26525
26526 if (TREE_CODE (expression) == TREE_LIST)
26527 return any_value_dependent_elements_p (expression);
26528
26529 return value_dependent_expression_p (expression);
26530 }
26531
26532 case SIZEOF_EXPR:
26533 if (SIZEOF_EXPR_TYPE_P (expression))
26534 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26535 /* FALLTHRU */
26536 case ALIGNOF_EXPR:
26537 case TYPEID_EXPR:
26538 /* A `sizeof' expression is value-dependent if the operand is
26539 type-dependent or is a pack expansion. */
26540 expression = TREE_OPERAND (expression, 0);
26541 if (PACK_EXPANSION_P (expression))
26542 return true;
26543 else if (TYPE_P (expression))
26544 return dependent_type_p (expression);
26545 return instantiation_dependent_uneval_expression_p (expression);
26546
26547 case AT_ENCODE_EXPR:
26548 /* An 'encode' expression is value-dependent if the operand is
26549 type-dependent. */
26550 expression = TREE_OPERAND (expression, 0);
26551 return dependent_type_p (expression);
26552
26553 case NOEXCEPT_EXPR:
26554 expression = TREE_OPERAND (expression, 0);
26555 return instantiation_dependent_uneval_expression_p (expression);
26556
26557 case SCOPE_REF:
26558 /* All instantiation-dependent expressions should also be considered
26559 value-dependent. */
26560 return instantiation_dependent_scope_ref_p (expression);
26561
26562 case COMPONENT_REF:
26563 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26564 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26565
26566 case NONTYPE_ARGUMENT_PACK:
26567 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26568 is value-dependent. */
26569 {
26570 tree values = ARGUMENT_PACK_ARGS (expression);
26571 int i, len = TREE_VEC_LENGTH (values);
26572
26573 for (i = 0; i < len; ++i)
26574 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26575 return true;
26576
26577 return false;
26578 }
26579
26580 case TRAIT_EXPR:
26581 {
26582 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26583
26584 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26585 return true;
26586
26587 if (!type2)
26588 return false;
26589
26590 if (TREE_CODE (type2) != TREE_LIST)
26591 return dependent_type_p (type2);
26592
26593 for (; type2; type2 = TREE_CHAIN (type2))
26594 if (dependent_type_p (TREE_VALUE (type2)))
26595 return true;
26596
26597 return false;
26598 }
26599
26600 case MODOP_EXPR:
26601 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26602 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26603
26604 case ARRAY_REF:
26605 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26606 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26607
26608 case ADDR_EXPR:
26609 {
26610 tree op = TREE_OPERAND (expression, 0);
26611 return (value_dependent_expression_p (op)
26612 || has_value_dependent_address (op));
26613 }
26614
26615 case REQUIRES_EXPR:
26616 /* Treat all requires-expressions as value-dependent so
26617 we don't try to fold them. */
26618 return true;
26619
26620 case TYPE_REQ:
26621 return dependent_type_p (TREE_OPERAND (expression, 0));
26622
26623 case CALL_EXPR:
26624 {
26625 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26626 return true;
26627 tree fn = get_callee_fndecl (expression);
26628 int i, nargs;
26629 nargs = call_expr_nargs (expression);
26630 for (i = 0; i < nargs; ++i)
26631 {
26632 tree op = CALL_EXPR_ARG (expression, i);
26633 /* In a call to a constexpr member function, look through the
26634 implicit ADDR_EXPR on the object argument so that it doesn't
26635 cause the call to be considered value-dependent. We also
26636 look through it in potential_constant_expression. */
26637 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26638 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26639 && TREE_CODE (op) == ADDR_EXPR)
26640 op = TREE_OPERAND (op, 0);
26641 if (value_dependent_expression_p (op))
26642 return true;
26643 }
26644 return false;
26645 }
26646
26647 case TEMPLATE_ID_EXPR:
26648 return concept_definition_p (TREE_OPERAND (expression, 0));
26649
26650 case CONSTRUCTOR:
26651 {
26652 unsigned ix;
26653 tree val;
26654 if (dependent_type_p (TREE_TYPE (expression)))
26655 return true;
26656 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26657 if (value_dependent_expression_p (val))
26658 return true;
26659 return false;
26660 }
26661
26662 case STMT_EXPR:
26663 /* Treat a GNU statement expression as dependent to avoid crashing
26664 under instantiate_non_dependent_expr; it can't be constant. */
26665 return true;
26666
26667 default:
26668 /* A constant expression is value-dependent if any subexpression is
26669 value-dependent. */
26670 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26671 {
26672 case tcc_reference:
26673 case tcc_unary:
26674 case tcc_comparison:
26675 case tcc_binary:
26676 case tcc_expression:
26677 case tcc_vl_exp:
26678 {
26679 int i, len = cp_tree_operand_length (expression);
26680
26681 for (i = 0; i < len; i++)
26682 {
26683 tree t = TREE_OPERAND (expression, i);
26684
26685 /* In some cases, some of the operands may be missing.
26686 (For example, in the case of PREDECREMENT_EXPR, the
26687 amount to increment by may be missing.) That doesn't
26688 make the expression dependent. */
26689 if (t && value_dependent_expression_p (t))
26690 return true;
26691 }
26692 }
26693 break;
26694 default:
26695 break;
26696 }
26697 break;
26698 }
26699
26700 /* The expression is not value-dependent. */
26701 return false;
26702 }
26703
26704 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26705 [temp.dep.expr]. Note that an expression with no type is
26706 considered dependent. Other parts of the compiler arrange for an
26707 expression with type-dependent subexpressions to have no type, so
26708 this function doesn't have to be fully recursive. */
26709
26710 bool
26711 type_dependent_expression_p (tree expression)
26712 {
26713 if (!processing_template_decl)
26714 return false;
26715
26716 if (expression == NULL_TREE || expression == error_mark_node)
26717 return false;
26718
26719 STRIP_ANY_LOCATION_WRAPPER (expression);
26720
26721 /* An unresolved name is always dependent. */
26722 if (identifier_p (expression)
26723 || TREE_CODE (expression) == USING_DECL
26724 || TREE_CODE (expression) == WILDCARD_DECL)
26725 return true;
26726
26727 /* A lambda-expression in template context is dependent. dependent_type_p is
26728 true for a lambda in the scope of a class or function template, but that
26729 doesn't cover all template contexts, like a default template argument. */
26730 if (TREE_CODE (expression) == LAMBDA_EXPR)
26731 return true;
26732
26733 /* A fold expression is type-dependent. */
26734 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26735 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26736 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26737 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26738 return true;
26739
26740 /* Some expression forms are never type-dependent. */
26741 if (TREE_CODE (expression) == SIZEOF_EXPR
26742 || TREE_CODE (expression) == ALIGNOF_EXPR
26743 || TREE_CODE (expression) == AT_ENCODE_EXPR
26744 || TREE_CODE (expression) == NOEXCEPT_EXPR
26745 || TREE_CODE (expression) == TRAIT_EXPR
26746 || TREE_CODE (expression) == TYPEID_EXPR
26747 || TREE_CODE (expression) == DELETE_EXPR
26748 || TREE_CODE (expression) == VEC_DELETE_EXPR
26749 || TREE_CODE (expression) == THROW_EXPR
26750 || TREE_CODE (expression) == REQUIRES_EXPR)
26751 return false;
26752
26753 /* The types of these expressions depends only on the type to which
26754 the cast occurs. */
26755 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26756 || TREE_CODE (expression) == STATIC_CAST_EXPR
26757 || TREE_CODE (expression) == CONST_CAST_EXPR
26758 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26759 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26760 || TREE_CODE (expression) == CAST_EXPR)
26761 return dependent_type_p (TREE_TYPE (expression));
26762
26763 /* The types of these expressions depends only on the type created
26764 by the expression. */
26765 if (TREE_CODE (expression) == NEW_EXPR
26766 || TREE_CODE (expression) == VEC_NEW_EXPR)
26767 {
26768 /* For NEW_EXPR tree nodes created inside a template, either
26769 the object type itself or a TREE_LIST may appear as the
26770 operand 1. */
26771 tree type = TREE_OPERAND (expression, 1);
26772 if (TREE_CODE (type) == TREE_LIST)
26773 /* This is an array type. We need to check array dimensions
26774 as well. */
26775 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26776 || value_dependent_expression_p
26777 (TREE_OPERAND (TREE_VALUE (type), 1));
26778 else
26779 return dependent_type_p (type);
26780 }
26781
26782 if (TREE_CODE (expression) == SCOPE_REF)
26783 {
26784 tree scope = TREE_OPERAND (expression, 0);
26785 tree name = TREE_OPERAND (expression, 1);
26786
26787 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26788 contains an identifier associated by name lookup with one or more
26789 declarations declared with a dependent type, or...a
26790 nested-name-specifier or qualified-id that names a member of an
26791 unknown specialization. */
26792 return (type_dependent_expression_p (name)
26793 || dependent_scope_p (scope));
26794 }
26795
26796 if (TREE_CODE (expression) == TEMPLATE_DECL
26797 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26798 return uses_outer_template_parms (expression);
26799
26800 if (TREE_CODE (expression) == STMT_EXPR)
26801 expression = stmt_expr_value_expr (expression);
26802
26803 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26804 {
26805 tree elt;
26806 unsigned i;
26807
26808 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26809 {
26810 if (type_dependent_expression_p (elt))
26811 return true;
26812 }
26813 return false;
26814 }
26815
26816 /* A static data member of the current instantiation with incomplete
26817 array type is type-dependent, as the definition and specializations
26818 can have different bounds. */
26819 if (VAR_P (expression)
26820 && DECL_CLASS_SCOPE_P (expression)
26821 && dependent_type_p (DECL_CONTEXT (expression))
26822 && VAR_HAD_UNKNOWN_BOUND (expression))
26823 return true;
26824
26825 /* An array of unknown bound depending on a variadic parameter, eg:
26826
26827 template<typename... Args>
26828 void foo (Args... args)
26829 {
26830 int arr[] = { args... };
26831 }
26832
26833 template<int... vals>
26834 void bar ()
26835 {
26836 int arr[] = { vals... };
26837 }
26838
26839 If the array has no length and has an initializer, it must be that
26840 we couldn't determine its length in cp_complete_array_type because
26841 it is dependent. */
26842 if (VAR_P (expression)
26843 && TREE_TYPE (expression) != NULL_TREE
26844 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26845 && !TYPE_DOMAIN (TREE_TYPE (expression))
26846 && DECL_INITIAL (expression))
26847 return true;
26848
26849 /* A function or variable template-id is type-dependent if it has any
26850 dependent template arguments. */
26851 if (VAR_OR_FUNCTION_DECL_P (expression)
26852 && DECL_LANG_SPECIFIC (expression)
26853 && DECL_TEMPLATE_INFO (expression))
26854 {
26855 /* Consider the innermost template arguments, since those are the ones
26856 that come from the template-id; the template arguments for the
26857 enclosing class do not make it type-dependent unless they are used in
26858 the type of the decl. */
26859 if (instantiates_primary_template_p (expression)
26860 && (any_dependent_template_arguments_p
26861 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26862 return true;
26863 }
26864
26865 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26866 type-dependent. Checking this is important for functions with auto return
26867 type, which looks like a dependent type. */
26868 if (TREE_CODE (expression) == FUNCTION_DECL
26869 && !(DECL_CLASS_SCOPE_P (expression)
26870 && dependent_type_p (DECL_CONTEXT (expression)))
26871 && !(DECL_LANG_SPECIFIC (expression)
26872 && DECL_FRIEND_P (expression)
26873 && (!DECL_FRIEND_CONTEXT (expression)
26874 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26875 && !DECL_LOCAL_FUNCTION_P (expression))
26876 {
26877 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26878 || undeduced_auto_decl (expression));
26879 return false;
26880 }
26881
26882 /* Always dependent, on the number of arguments if nothing else. */
26883 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
26884 return true;
26885
26886 if (TREE_TYPE (expression) == unknown_type_node)
26887 {
26888 if (TREE_CODE (expression) == ADDR_EXPR)
26889 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
26890 if (TREE_CODE (expression) == COMPONENT_REF
26891 || TREE_CODE (expression) == OFFSET_REF)
26892 {
26893 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
26894 return true;
26895 expression = TREE_OPERAND (expression, 1);
26896 if (identifier_p (expression))
26897 return false;
26898 }
26899 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26900 if (TREE_CODE (expression) == SCOPE_REF)
26901 return false;
26902
26903 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
26904 if (TREE_CODE (expression) == CO_AWAIT_EXPR
26905 || TREE_CODE (expression) == CO_YIELD_EXPR)
26906 return true;
26907
26908 if (BASELINK_P (expression))
26909 {
26910 if (BASELINK_OPTYPE (expression)
26911 && dependent_type_p (BASELINK_OPTYPE (expression)))
26912 return true;
26913 expression = BASELINK_FUNCTIONS (expression);
26914 }
26915
26916 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
26917 {
26918 if (any_dependent_template_arguments_p
26919 (TREE_OPERAND (expression, 1)))
26920 return true;
26921 expression = TREE_OPERAND (expression, 0);
26922 if (identifier_p (expression))
26923 return true;
26924 }
26925
26926 gcc_assert (OVL_P (expression));
26927
26928 for (lkp_iterator iter (expression); iter; ++iter)
26929 if (type_dependent_expression_p (*iter))
26930 return true;
26931
26932 return false;
26933 }
26934
26935 /* The type of a non-type template parm declared with a placeholder type
26936 depends on the corresponding template argument, even though
26937 placeholders are not normally considered dependent. */
26938 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26939 && is_auto (TREE_TYPE (expression)))
26940 return true;
26941
26942 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26943
26944 /* Dependent type attributes might not have made it from the decl to
26945 the type yet. */
26946 if (DECL_P (expression)
26947 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26948 return true;
26949
26950 return (dependent_type_p (TREE_TYPE (expression)));
26951 }
26952
26953 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26954 type-dependent if the expression refers to a member of the current
26955 instantiation and the type of the referenced member is dependent, or the
26956 class member access expression refers to a member of an unknown
26957 specialization.
26958
26959 This function returns true if the OBJECT in such a class member access
26960 expression is of an unknown specialization. */
26961
26962 bool
26963 type_dependent_object_expression_p (tree object)
26964 {
26965 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26966 dependent. */
26967 if (TREE_CODE (object) == IDENTIFIER_NODE)
26968 return true;
26969 tree scope = TREE_TYPE (object);
26970 return (!scope || dependent_scope_p (scope));
26971 }
26972
26973 /* walk_tree callback function for instantiation_dependent_expression_p,
26974 below. Returns non-zero if a dependent subexpression is found. */
26975
26976 static tree
26977 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26978 void * /*data*/)
26979 {
26980 if (TYPE_P (*tp))
26981 {
26982 /* We don't have to worry about decltype currently because decltype
26983 of an instantiation-dependent expr is a dependent type. This
26984 might change depending on the resolution of DR 1172. */
26985 *walk_subtrees = false;
26986 return NULL_TREE;
26987 }
26988 enum tree_code code = TREE_CODE (*tp);
26989 switch (code)
26990 {
26991 /* Don't treat an argument list as dependent just because it has no
26992 TREE_TYPE. */
26993 case TREE_LIST:
26994 case TREE_VEC:
26995 case NONTYPE_ARGUMENT_PACK:
26996 return NULL_TREE;
26997
26998 case TEMPLATE_PARM_INDEX:
26999 if (dependent_type_p (TREE_TYPE (*tp)))
27000 return *tp;
27001 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27002 return *tp;
27003 /* We'll check value-dependence separately. */
27004 return NULL_TREE;
27005
27006 /* Handle expressions with type operands. */
27007 case SIZEOF_EXPR:
27008 case ALIGNOF_EXPR:
27009 case TYPEID_EXPR:
27010 case AT_ENCODE_EXPR:
27011 {
27012 tree op = TREE_OPERAND (*tp, 0);
27013 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27014 op = TREE_TYPE (op);
27015 if (TYPE_P (op))
27016 {
27017 if (dependent_type_p (op))
27018 return *tp;
27019 else
27020 {
27021 *walk_subtrees = false;
27022 return NULL_TREE;
27023 }
27024 }
27025 break;
27026 }
27027
27028 case COMPONENT_REF:
27029 if (identifier_p (TREE_OPERAND (*tp, 1)))
27030 /* In a template, finish_class_member_access_expr creates a
27031 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27032 type-dependent, so that we can check access control at
27033 instantiation time (PR 42277). See also Core issue 1273. */
27034 return *tp;
27035 break;
27036
27037 case SCOPE_REF:
27038 if (instantiation_dependent_scope_ref_p (*tp))
27039 return *tp;
27040 else
27041 break;
27042
27043 /* Treat statement-expressions as dependent. */
27044 case BIND_EXPR:
27045 return *tp;
27046
27047 /* Treat requires-expressions as dependent. */
27048 case REQUIRES_EXPR:
27049 return *tp;
27050
27051 case CALL_EXPR:
27052 /* Treat concept checks as dependent. */
27053 if (concept_check_p (*tp))
27054 return *tp;
27055 break;
27056
27057 case TEMPLATE_ID_EXPR:
27058 /* Treat concept checks as dependent. */
27059 if (concept_check_p (*tp))
27060 return *tp;
27061 break;
27062
27063 case CONSTRUCTOR:
27064 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27065 return *tp;
27066 break;
27067
27068 default:
27069 break;
27070 }
27071
27072 if (type_dependent_expression_p (*tp))
27073 return *tp;
27074 else
27075 return NULL_TREE;
27076 }
27077
27078 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27079 sense defined by the ABI:
27080
27081 "An expression is instantiation-dependent if it is type-dependent
27082 or value-dependent, or it has a subexpression that is type-dependent
27083 or value-dependent."
27084
27085 Except don't actually check value-dependence for unevaluated expressions,
27086 because in sizeof(i) we don't care about the value of i. Checking
27087 type-dependence will in turn check value-dependence of array bounds/template
27088 arguments as needed. */
27089
27090 bool
27091 instantiation_dependent_uneval_expression_p (tree expression)
27092 {
27093 tree result;
27094
27095 if (!processing_template_decl)
27096 return false;
27097
27098 if (expression == error_mark_node)
27099 return false;
27100
27101 result = cp_walk_tree_without_duplicates (&expression,
27102 instantiation_dependent_r, NULL);
27103 return result != NULL_TREE;
27104 }
27105
27106 /* As above, but also check value-dependence of the expression as a whole. */
27107
27108 bool
27109 instantiation_dependent_expression_p (tree expression)
27110 {
27111 return (instantiation_dependent_uneval_expression_p (expression)
27112 || value_dependent_expression_p (expression));
27113 }
27114
27115 /* Like type_dependent_expression_p, but it also works while not processing
27116 a template definition, i.e. during substitution or mangling. */
27117
27118 bool
27119 type_dependent_expression_p_push (tree expr)
27120 {
27121 bool b;
27122 ++processing_template_decl;
27123 b = type_dependent_expression_p (expr);
27124 --processing_template_decl;
27125 return b;
27126 }
27127
27128 /* Returns TRUE if ARGS contains a type-dependent expression. */
27129
27130 bool
27131 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27132 {
27133 unsigned int i;
27134 tree arg;
27135
27136 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27137 {
27138 if (type_dependent_expression_p (arg))
27139 return true;
27140 }
27141 return false;
27142 }
27143
27144 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27145 expressions) contains any type-dependent expressions. */
27146
27147 bool
27148 any_type_dependent_elements_p (const_tree list)
27149 {
27150 for (; list; list = TREE_CHAIN (list))
27151 if (type_dependent_expression_p (TREE_VALUE (list)))
27152 return true;
27153
27154 return false;
27155 }
27156
27157 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27158 expressions) contains any value-dependent expressions. */
27159
27160 bool
27161 any_value_dependent_elements_p (const_tree list)
27162 {
27163 for (; list; list = TREE_CHAIN (list))
27164 if (value_dependent_expression_p (TREE_VALUE (list)))
27165 return true;
27166
27167 return false;
27168 }
27169
27170 /* Returns TRUE if the ARG (a template argument) is dependent. */
27171
27172 bool
27173 dependent_template_arg_p (tree arg)
27174 {
27175 if (!processing_template_decl)
27176 return false;
27177
27178 /* Assume a template argument that was wrongly written by the user
27179 is dependent. This is consistent with what
27180 any_dependent_template_arguments_p [that calls this function]
27181 does. */
27182 if (!arg || arg == error_mark_node)
27183 return true;
27184
27185 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27186 arg = argument_pack_select_arg (arg);
27187
27188 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27189 return true;
27190 if (TREE_CODE (arg) == TEMPLATE_DECL)
27191 {
27192 if (DECL_TEMPLATE_PARM_P (arg))
27193 return true;
27194 /* A member template of a dependent class is not necessarily
27195 type-dependent, but it is a dependent template argument because it
27196 will be a member of an unknown specialization to that template. */
27197 tree scope = CP_DECL_CONTEXT (arg);
27198 return TYPE_P (scope) && dependent_type_p (scope);
27199 }
27200 else if (ARGUMENT_PACK_P (arg))
27201 {
27202 tree args = ARGUMENT_PACK_ARGS (arg);
27203 int i, len = TREE_VEC_LENGTH (args);
27204 for (i = 0; i < len; ++i)
27205 {
27206 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27207 return true;
27208 }
27209
27210 return false;
27211 }
27212 else if (TYPE_P (arg))
27213 return dependent_type_p (arg);
27214 else
27215 return value_dependent_expression_p (arg);
27216 }
27217
27218 /* Returns true if ARGS (a collection of template arguments) contains
27219 any types that require structural equality testing. */
27220
27221 bool
27222 any_template_arguments_need_structural_equality_p (tree args)
27223 {
27224 int i;
27225 int j;
27226
27227 if (!args)
27228 return false;
27229 if (args == error_mark_node)
27230 return true;
27231
27232 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27233 {
27234 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27235 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27236 {
27237 tree arg = TREE_VEC_ELT (level, j);
27238 tree packed_args = NULL_TREE;
27239 int k, len = 1;
27240
27241 if (ARGUMENT_PACK_P (arg))
27242 {
27243 /* Look inside the argument pack. */
27244 packed_args = ARGUMENT_PACK_ARGS (arg);
27245 len = TREE_VEC_LENGTH (packed_args);
27246 }
27247
27248 for (k = 0; k < len; ++k)
27249 {
27250 if (packed_args)
27251 arg = TREE_VEC_ELT (packed_args, k);
27252
27253 if (error_operand_p (arg))
27254 return true;
27255 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27256 continue;
27257 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27258 return true;
27259 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27260 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27261 return true;
27262 }
27263 }
27264 }
27265
27266 return false;
27267 }
27268
27269 /* Returns true if ARGS (a collection of template arguments) contains
27270 any dependent arguments. */
27271
27272 bool
27273 any_dependent_template_arguments_p (const_tree args)
27274 {
27275 int i;
27276 int j;
27277
27278 if (!args)
27279 return false;
27280 if (args == error_mark_node)
27281 return true;
27282
27283 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27284 {
27285 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27286 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27287 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27288 return true;
27289 }
27290
27291 return false;
27292 }
27293
27294 /* Returns true if ARGS contains any errors. */
27295
27296 bool
27297 any_erroneous_template_args_p (const_tree args)
27298 {
27299 int i;
27300 int j;
27301
27302 if (args == error_mark_node)
27303 return true;
27304
27305 if (args && TREE_CODE (args) != TREE_VEC)
27306 {
27307 if (tree ti = get_template_info (args))
27308 args = TI_ARGS (ti);
27309 else
27310 args = NULL_TREE;
27311 }
27312
27313 if (!args)
27314 return false;
27315
27316 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27317 {
27318 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27319 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27320 if (error_operand_p (TREE_VEC_ELT (level, j)))
27321 return true;
27322 }
27323
27324 return false;
27325 }
27326
27327 /* Returns TRUE if the template TMPL is type-dependent. */
27328
27329 bool
27330 dependent_template_p (tree tmpl)
27331 {
27332 if (TREE_CODE (tmpl) == OVERLOAD)
27333 {
27334 for (lkp_iterator iter (tmpl); iter; ++iter)
27335 if (dependent_template_p (*iter))
27336 return true;
27337 return false;
27338 }
27339
27340 /* Template template parameters are dependent. */
27341 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27342 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27343 return true;
27344 /* So are names that have not been looked up. */
27345 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27346 return true;
27347 return false;
27348 }
27349
27350 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27351
27352 bool
27353 dependent_template_id_p (tree tmpl, tree args)
27354 {
27355 return (dependent_template_p (tmpl)
27356 || any_dependent_template_arguments_p (args));
27357 }
27358
27359 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27360 are dependent. */
27361
27362 bool
27363 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27364 {
27365 int i;
27366
27367 if (!processing_template_decl)
27368 return false;
27369
27370 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27371 {
27372 tree decl = TREE_VEC_ELT (declv, i);
27373 tree init = TREE_VEC_ELT (initv, i);
27374 tree cond = TREE_VEC_ELT (condv, i);
27375 tree incr = TREE_VEC_ELT (incrv, i);
27376
27377 if (type_dependent_expression_p (decl)
27378 || TREE_CODE (decl) == SCOPE_REF)
27379 return true;
27380
27381 if (init && type_dependent_expression_p (init))
27382 return true;
27383
27384 if (cond == global_namespace)
27385 return true;
27386
27387 if (type_dependent_expression_p (cond))
27388 return true;
27389
27390 if (COMPARISON_CLASS_P (cond)
27391 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27392 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27393 return true;
27394
27395 if (TREE_CODE (incr) == MODOP_EXPR)
27396 {
27397 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27398 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27399 return true;
27400 }
27401 else if (type_dependent_expression_p (incr))
27402 return true;
27403 else if (TREE_CODE (incr) == MODIFY_EXPR)
27404 {
27405 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27406 return true;
27407 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27408 {
27409 tree t = TREE_OPERAND (incr, 1);
27410 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27411 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27412 return true;
27413
27414 /* If this loop has a class iterator with != comparison
27415 with increment other than i++/++i/i--/--i, make sure the
27416 increment is constant. */
27417 if (CLASS_TYPE_P (TREE_TYPE (decl))
27418 && TREE_CODE (cond) == NE_EXPR)
27419 {
27420 if (TREE_OPERAND (t, 0) == decl)
27421 t = TREE_OPERAND (t, 1);
27422 else
27423 t = TREE_OPERAND (t, 0);
27424 if (TREE_CODE (t) != INTEGER_CST)
27425 return true;
27426 }
27427 }
27428 }
27429 }
27430
27431 return false;
27432 }
27433
27434 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27435 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27436 no such TYPE can be found. Note that this function peers inside
27437 uninstantiated templates and therefore should be used only in
27438 extremely limited situations. ONLY_CURRENT_P restricts this
27439 peering to the currently open classes hierarchy (which is required
27440 when comparing types). */
27441
27442 tree
27443 resolve_typename_type (tree type, bool only_current_p)
27444 {
27445 tree scope;
27446 tree name;
27447 tree decl;
27448 int quals;
27449 tree pushed_scope;
27450 tree result;
27451
27452 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27453
27454 scope = TYPE_CONTEXT (type);
27455 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27456 gcc_checking_assert (uses_template_parms (scope));
27457
27458 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27459 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27460 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27461 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27462 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27463 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27464 the TYPENAME_TYPE instead, we avoid messing up with a possible
27465 typedef variant case. */
27466 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27467
27468 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27469 it first before we can figure out what NAME refers to. */
27470 if (TREE_CODE (scope) == TYPENAME_TYPE)
27471 {
27472 if (TYPENAME_IS_RESOLVING_P (scope))
27473 /* Given a class template A with a dependent base with nested type C,
27474 typedef typename A::C::C C will land us here, as trying to resolve
27475 the initial A::C leads to the local C typedef, which leads back to
27476 A::C::C. So we break the recursion now. */
27477 return type;
27478 else
27479 scope = resolve_typename_type (scope, only_current_p);
27480 }
27481 /* If we don't know what SCOPE refers to, then we cannot resolve the
27482 TYPENAME_TYPE. */
27483 if (!CLASS_TYPE_P (scope))
27484 return type;
27485 /* If this is a typedef, we don't want to look inside (c++/11987). */
27486 if (typedef_variant_p (type))
27487 return type;
27488 /* If SCOPE isn't the template itself, it will not have a valid
27489 TYPE_FIELDS list. */
27490 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27491 /* scope is either the template itself or a compatible instantiation
27492 like X<T>, so look up the name in the original template. */
27493 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27494 /* If scope has no fields, it can't be a current instantiation. Check this
27495 before currently_open_class to avoid infinite recursion (71515). */
27496 if (!TYPE_FIELDS (scope))
27497 return type;
27498 /* If the SCOPE is not the current instantiation, there's no reason
27499 to look inside it. */
27500 if (only_current_p && !currently_open_class (scope))
27501 return type;
27502 /* Enter the SCOPE so that name lookup will be resolved as if we
27503 were in the class definition. In particular, SCOPE will no
27504 longer be considered a dependent type. */
27505 pushed_scope = push_scope (scope);
27506 /* Look up the declaration. */
27507 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27508 tf_warning_or_error);
27509
27510 result = NULL_TREE;
27511
27512 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27513 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27514 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27515 if (!decl)
27516 /*nop*/;
27517 else if (identifier_p (fullname)
27518 && TREE_CODE (decl) == TYPE_DECL)
27519 {
27520 result = TREE_TYPE (decl);
27521 if (result == error_mark_node)
27522 result = NULL_TREE;
27523 }
27524 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27525 && DECL_CLASS_TEMPLATE_P (decl))
27526 {
27527 /* Obtain the template and the arguments. */
27528 tree tmpl = TREE_OPERAND (fullname, 0);
27529 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27530 {
27531 /* We get here with a plain identifier because a previous tentative
27532 parse of the nested-name-specifier as part of a ptr-operator saw
27533 ::template X<A>. The use of ::template is necessary in a
27534 ptr-operator, but wrong in a declarator-id.
27535
27536 [temp.names]: In a qualified-id of a declarator-id, the keyword
27537 template shall not appear at the top level. */
27538 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27539 "keyword %<template%> not allowed in declarator-id");
27540 tmpl = decl;
27541 }
27542 tree args = TREE_OPERAND (fullname, 1);
27543 /* Instantiate the template. */
27544 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27545 /*entering_scope=*/true,
27546 tf_error | tf_user);
27547 if (result == error_mark_node)
27548 result = NULL_TREE;
27549 }
27550
27551 /* Leave the SCOPE. */
27552 if (pushed_scope)
27553 pop_scope (pushed_scope);
27554
27555 /* If we failed to resolve it, return the original typename. */
27556 if (!result)
27557 return type;
27558
27559 /* If lookup found a typename type, resolve that too. */
27560 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27561 {
27562 /* Ill-formed programs can cause infinite recursion here, so we
27563 must catch that. */
27564 TYPENAME_IS_RESOLVING_P (result) = 1;
27565 result = resolve_typename_type (result, only_current_p);
27566 TYPENAME_IS_RESOLVING_P (result) = 0;
27567 }
27568
27569 /* Qualify the resulting type. */
27570 quals = cp_type_quals (type);
27571 if (quals)
27572 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27573
27574 return result;
27575 }
27576
27577 /* EXPR is an expression which is not type-dependent. Return a proxy
27578 for EXPR that can be used to compute the types of larger
27579 expressions containing EXPR. */
27580
27581 tree
27582 build_non_dependent_expr (tree expr)
27583 {
27584 tree orig_expr = expr;
27585 tree inner_expr;
27586
27587 /* When checking, try to get a constant value for all non-dependent
27588 expressions in order to expose bugs in *_dependent_expression_p
27589 and constexpr. This can affect code generation, see PR70704, so
27590 only do this for -fchecking=2. */
27591 if (flag_checking > 1
27592 && cxx_dialect >= cxx11
27593 /* Don't do this during nsdmi parsing as it can lead to
27594 unexpected recursive instantiations. */
27595 && !parsing_nsdmi ()
27596 /* Don't do this during concept processing either and for
27597 the same reason. */
27598 && !processing_constraint_expression_p ())
27599 fold_non_dependent_expr (expr, tf_none);
27600
27601 STRIP_ANY_LOCATION_WRAPPER (expr);
27602
27603 /* Preserve OVERLOADs; the functions must be available to resolve
27604 types. */
27605 inner_expr = expr;
27606 if (TREE_CODE (inner_expr) == STMT_EXPR)
27607 inner_expr = stmt_expr_value_expr (inner_expr);
27608 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27609 inner_expr = TREE_OPERAND (inner_expr, 0);
27610 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27611 inner_expr = TREE_OPERAND (inner_expr, 1);
27612 if (is_overloaded_fn (inner_expr)
27613 || TREE_CODE (inner_expr) == OFFSET_REF)
27614 return orig_expr;
27615 /* There is no need to return a proxy for a variable or enumerator. */
27616 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27617 return orig_expr;
27618 /* Preserve string constants; conversions from string constants to
27619 "char *" are allowed, even though normally a "const char *"
27620 cannot be used to initialize a "char *". */
27621 if (TREE_CODE (expr) == STRING_CST)
27622 return orig_expr;
27623 /* Preserve void and arithmetic constants, as an optimization -- there is no
27624 reason to create a new node. */
27625 if (TREE_CODE (expr) == VOID_CST
27626 || TREE_CODE (expr) == INTEGER_CST
27627 || TREE_CODE (expr) == REAL_CST)
27628 return orig_expr;
27629 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27630 There is at least one place where we want to know that a
27631 particular expression is a throw-expression: when checking a ?:
27632 expression, there are special rules if the second or third
27633 argument is a throw-expression. */
27634 if (TREE_CODE (expr) == THROW_EXPR)
27635 return orig_expr;
27636
27637 /* Don't wrap an initializer list, we need to be able to look inside. */
27638 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27639 return orig_expr;
27640
27641 /* Don't wrap a dummy object, we need to be able to test for it. */
27642 if (is_dummy_object (expr))
27643 return orig_expr;
27644
27645 if (TREE_CODE (expr) == COND_EXPR)
27646 return build3 (COND_EXPR,
27647 TREE_TYPE (expr),
27648 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27649 (TREE_OPERAND (expr, 1)
27650 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27651 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27652 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27653 if (TREE_CODE (expr) == COMPOUND_EXPR
27654 && !COMPOUND_EXPR_OVERLOADED (expr))
27655 return build2 (COMPOUND_EXPR,
27656 TREE_TYPE (expr),
27657 TREE_OPERAND (expr, 0),
27658 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27659
27660 /* If the type is unknown, it can't really be non-dependent */
27661 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27662
27663 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27664 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27665 TREE_TYPE (expr), expr);
27666 }
27667
27668 /* ARGS is a vector of expressions as arguments to a function call.
27669 Replace the arguments with equivalent non-dependent expressions.
27670 This modifies ARGS in place. */
27671
27672 void
27673 make_args_non_dependent (vec<tree, va_gc> *args)
27674 {
27675 unsigned int ix;
27676 tree arg;
27677
27678 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27679 {
27680 tree newarg = build_non_dependent_expr (arg);
27681 if (newarg != arg)
27682 (*args)[ix] = newarg;
27683 }
27684 }
27685
27686 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27687 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27688 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27689
27690 static tree
27691 make_auto_1 (tree name, bool set_canonical)
27692 {
27693 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27694 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27695 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27696 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27697 (0, processing_template_decl + 1, processing_template_decl + 1,
27698 TYPE_NAME (au), NULL_TREE);
27699 if (set_canonical)
27700 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27701 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27702 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27703 if (name == decltype_auto_identifier)
27704 AUTO_IS_DECLTYPE (au) = true;
27705
27706 return au;
27707 }
27708
27709 tree
27710 make_decltype_auto (void)
27711 {
27712 return make_auto_1 (decltype_auto_identifier, true);
27713 }
27714
27715 tree
27716 make_auto (void)
27717 {
27718 return make_auto_1 (auto_identifier, true);
27719 }
27720
27721 /* Return a C++17 deduction placeholder for class template TMPL. */
27722
27723 tree
27724 make_template_placeholder (tree tmpl)
27725 {
27726 tree t = make_auto_1 (auto_identifier, false);
27727 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27728 /* Our canonical type depends on the placeholder. */
27729 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27730 return t;
27731 }
27732
27733 /* True iff T is a C++17 class template deduction placeholder. */
27734
27735 bool
27736 template_placeholder_p (tree t)
27737 {
27738 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27739 }
27740
27741 /* Make a "constrained auto" type-specifier. This is an auto or
27742 decltype(auto) type with constraints that must be associated after
27743 deduction. The constraint is formed from the given concept CON
27744 and its optional sequence of template arguments ARGS.
27745
27746 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27747
27748 static tree
27749 make_constrained_placeholder_type (tree type, tree con, tree args)
27750 {
27751 /* Build the constraint. */
27752 tree tmpl = DECL_TI_TEMPLATE (con);
27753 tree expr = tmpl;
27754 if (TREE_CODE (con) == FUNCTION_DECL)
27755 expr = ovl_make (tmpl);
27756 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27757
27758 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27759
27760 /* Our canonical type depends on the constraint. */
27761 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27762
27763 /* Attach the constraint to the type declaration. */
27764 return TYPE_NAME (type);
27765 }
27766
27767 /* Make a "constrained auto" type-specifier. */
27768
27769 tree
27770 make_constrained_auto (tree con, tree args)
27771 {
27772 tree type = make_auto_1 (auto_identifier, false);
27773 return make_constrained_placeholder_type (type, con, args);
27774 }
27775
27776 /* Make a "constrained decltype(auto)" type-specifier. */
27777
27778 tree
27779 make_constrained_decltype_auto (tree con, tree args)
27780 {
27781 tree type = make_auto_1 (decltype_auto_identifier, false);
27782 return make_constrained_placeholder_type (type, con, args);
27783 }
27784
27785 /* Build and return a concept definition. Like other templates, the
27786 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27787 the TEMPLATE_DECL. */
27788
27789 tree
27790 finish_concept_definition (cp_expr id, tree init)
27791 {
27792 gcc_assert (identifier_p (id));
27793 gcc_assert (processing_template_decl);
27794
27795 location_t loc = id.get_location();
27796
27797 /* A concept-definition shall not have associated constraints. */
27798 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27799 {
27800 error_at (loc, "a concept cannot be constrained");
27801 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27802 }
27803
27804 /* A concept-definition shall appear in namespace scope. Templates
27805 aren't allowed in block scope, so we only need to check for class
27806 scope. */
27807 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27808 {
27809 error_at (loc, "concept %qE not in namespace scope", *id);
27810 return error_mark_node;
27811 }
27812
27813 /* Initially build the concept declaration; its type is bool. */
27814 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27815 DECL_CONTEXT (decl) = current_scope ();
27816 DECL_INITIAL (decl) = init;
27817
27818 /* Push the enclosing template. */
27819 return push_template_decl (decl);
27820 }
27821
27822 /* Given type ARG, return std::initializer_list<ARG>. */
27823
27824 static tree
27825 listify (tree arg)
27826 {
27827 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27828
27829 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27830 {
27831 gcc_rich_location richloc (input_location);
27832 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27833 error_at (&richloc,
27834 "deducing from brace-enclosed initializer list"
27835 " requires %<#include <initializer_list>%>");
27836
27837 return error_mark_node;
27838 }
27839 tree argvec = make_tree_vec (1);
27840 TREE_VEC_ELT (argvec, 0) = arg;
27841
27842 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27843 NULL_TREE, 0, tf_warning_or_error);
27844 }
27845
27846 /* Replace auto in TYPE with std::initializer_list<auto>. */
27847
27848 static tree
27849 listify_autos (tree type, tree auto_node)
27850 {
27851 tree init_auto = listify (strip_top_quals (auto_node));
27852 tree argvec = make_tree_vec (1);
27853 TREE_VEC_ELT (argvec, 0) = init_auto;
27854 if (processing_template_decl)
27855 argvec = add_to_template_args (current_template_args (), argvec);
27856 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27857 }
27858
27859 /* Hash traits for hashing possibly constrained 'auto'
27860 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27861
27862 struct auto_hash : default_hash_traits<tree>
27863 {
27864 static inline hashval_t hash (tree);
27865 static inline bool equal (tree, tree);
27866 };
27867
27868 /* Hash the 'auto' T. */
27869
27870 inline hashval_t
27871 auto_hash::hash (tree t)
27872 {
27873 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27874 /* Matching constrained-type-specifiers denote the same template
27875 parameter, so hash the constraint. */
27876 return hash_placeholder_constraint (c);
27877 else
27878 /* But unconstrained autos are all separate, so just hash the pointer. */
27879 return iterative_hash_object (t, 0);
27880 }
27881
27882 /* Compare two 'auto's. */
27883
27884 inline bool
27885 auto_hash::equal (tree t1, tree t2)
27886 {
27887 if (t1 == t2)
27888 return true;
27889
27890 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
27891 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
27892
27893 /* Two unconstrained autos are distinct. */
27894 if (!c1 || !c2)
27895 return false;
27896
27897 return equivalent_placeholder_constraints (c1, c2);
27898 }
27899
27900 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27901 constrained) auto, add it to the vector. */
27902
27903 static int
27904 extract_autos_r (tree t, void *data)
27905 {
27906 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
27907 if (is_auto (t))
27908 {
27909 /* All the autos were built with index 0; fix that up now. */
27910 tree *p = hash.find_slot (t, INSERT);
27911 unsigned idx;
27912 if (*p)
27913 /* If this is a repeated constrained-type-specifier, use the index we
27914 chose before. */
27915 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
27916 else
27917 {
27918 /* Otherwise this is new, so use the current count. */
27919 *p = t;
27920 idx = hash.elements () - 1;
27921 }
27922 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
27923 }
27924
27925 /* Always keep walking. */
27926 return 0;
27927 }
27928
27929 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27930 says they can appear anywhere in the type. */
27931
27932 static tree
27933 extract_autos (tree type)
27934 {
27935 hash_set<tree> visited;
27936 hash_table<auto_hash> hash (2);
27937
27938 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
27939
27940 tree tree_vec = make_tree_vec (hash.elements());
27941 for (hash_table<auto_hash>::iterator iter = hash.begin();
27942 iter != hash.end(); ++iter)
27943 {
27944 tree elt = *iter;
27945 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
27946 TREE_VEC_ELT (tree_vec, i)
27947 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
27948 }
27949
27950 return tree_vec;
27951 }
27952
27953 /* The stem for deduction guide names. */
27954 const char *const dguide_base = "__dguide_";
27955
27956 /* Return the name for a deduction guide for class template TMPL. */
27957
27958 tree
27959 dguide_name (tree tmpl)
27960 {
27961 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
27962 tree tname = TYPE_IDENTIFIER (type);
27963 char *buf = (char *) alloca (1 + strlen (dguide_base)
27964 + IDENTIFIER_LENGTH (tname));
27965 memcpy (buf, dguide_base, strlen (dguide_base));
27966 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
27967 IDENTIFIER_LENGTH (tname) + 1);
27968 tree dname = get_identifier (buf);
27969 TREE_TYPE (dname) = type;
27970 return dname;
27971 }
27972
27973 /* True if NAME is the name of a deduction guide. */
27974
27975 bool
27976 dguide_name_p (tree name)
27977 {
27978 return (TREE_CODE (name) == IDENTIFIER_NODE
27979 && TREE_TYPE (name)
27980 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
27981 strlen (dguide_base)));
27982 }
27983
27984 /* True if FN is a deduction guide. */
27985
27986 bool
27987 deduction_guide_p (const_tree fn)
27988 {
27989 if (DECL_P (fn))
27990 if (tree name = DECL_NAME (fn))
27991 return dguide_name_p (name);
27992 return false;
27993 }
27994
27995 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27996
27997 bool
27998 copy_guide_p (const_tree fn)
27999 {
28000 gcc_assert (deduction_guide_p (fn));
28001 if (!DECL_ARTIFICIAL (fn))
28002 return false;
28003 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28004 return (TREE_CHAIN (parms) == void_list_node
28005 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28006 }
28007
28008 /* True if FN is a guide generated from a constructor template. */
28009
28010 bool
28011 template_guide_p (const_tree fn)
28012 {
28013 gcc_assert (deduction_guide_p (fn));
28014 if (!DECL_ARTIFICIAL (fn))
28015 return false;
28016 tree tmpl = DECL_TI_TEMPLATE (fn);
28017 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28018 return PRIMARY_TEMPLATE_P (org);
28019 return false;
28020 }
28021
28022 /* True if FN is an aggregate initialization guide or the copy deduction
28023 guide. */
28024
28025 bool
28026 builtin_guide_p (const_tree fn)
28027 {
28028 if (!deduction_guide_p (fn))
28029 return false;
28030 if (!DECL_ARTIFICIAL (fn))
28031 /* Explicitly declared. */
28032 return false;
28033 if (DECL_ABSTRACT_ORIGIN (fn))
28034 /* Derived from a constructor. */
28035 return false;
28036 return true;
28037 }
28038
28039 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28040 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28041 template parameter types. Note that the handling of template template
28042 parameters relies on current_template_parms being set appropriately for the
28043 new template. */
28044
28045 static tree
28046 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28047 tree tsubst_args, tsubst_flags_t complain)
28048 {
28049 if (olddecl == error_mark_node)
28050 return error_mark_node;
28051
28052 tree oldidx = get_template_parm_index (olddecl);
28053
28054 tree newtype;
28055 if (TREE_CODE (olddecl) == TYPE_DECL
28056 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28057 {
28058 tree oldtype = TREE_TYPE (olddecl);
28059 newtype = cxx_make_type (TREE_CODE (oldtype));
28060 TYPE_MAIN_VARIANT (newtype) = newtype;
28061 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28062 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28063 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28064 }
28065 else
28066 {
28067 newtype = TREE_TYPE (olddecl);
28068 if (type_uses_auto (newtype))
28069 {
28070 // Substitute once to fix references to other template parameters.
28071 newtype = tsubst (newtype, tsubst_args,
28072 complain|tf_partial, NULL_TREE);
28073 // Now substitute again to reduce the level of the auto.
28074 newtype = tsubst (newtype, current_template_args (),
28075 complain, NULL_TREE);
28076 }
28077 else
28078 newtype = tsubst (newtype, tsubst_args,
28079 complain, NULL_TREE);
28080 }
28081
28082 tree newdecl
28083 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28084 DECL_NAME (olddecl), newtype);
28085 SET_DECL_TEMPLATE_PARM_P (newdecl);
28086
28087 tree newidx;
28088 if (TREE_CODE (olddecl) == TYPE_DECL
28089 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28090 {
28091 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28092 = build_template_parm_index (index, level, level,
28093 newdecl, newtype);
28094 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28095 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28096 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28097 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28098 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28099 else
28100 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28101
28102 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28103 {
28104 DECL_TEMPLATE_RESULT (newdecl)
28105 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28106 DECL_NAME (olddecl), newtype);
28107 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28108 // First create a copy (ttargs) of tsubst_args with an
28109 // additional level for the template template parameter's own
28110 // template parameters (ttparms).
28111 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28112 (DECL_TEMPLATE_PARMS (olddecl)));
28113 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28114 tree ttargs = make_tree_vec (depth + 1);
28115 for (int i = 0; i < depth; ++i)
28116 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28117 TREE_VEC_ELT (ttargs, depth)
28118 = template_parms_level_to_args (ttparms);
28119 // Substitute ttargs into ttparms to fix references to
28120 // other template parameters.
28121 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28122 complain|tf_partial);
28123 // Now substitute again with args based on tparms, to reduce
28124 // the level of the ttparms.
28125 ttargs = current_template_args ();
28126 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28127 complain);
28128 // Finally, tack the adjusted parms onto tparms.
28129 ttparms = tree_cons (size_int (depth), ttparms,
28130 current_template_parms);
28131 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28132 }
28133 }
28134 else
28135 {
28136 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28137 tree newconst
28138 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28139 TREE_CODE (oldconst),
28140 DECL_NAME (oldconst), newtype);
28141 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28142 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28143 SET_DECL_TEMPLATE_PARM_P (newconst);
28144 newidx = build_template_parm_index (index, level, level,
28145 newconst, newtype);
28146 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28147 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28148 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28149 }
28150
28151 return newdecl;
28152 }
28153
28154 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28155 template parameter. */
28156
28157 static tree
28158 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28159 tree targs, unsigned targs_index, tsubst_flags_t complain)
28160 {
28161 tree olddecl = TREE_VALUE (oldelt);
28162 tree newdecl = rewrite_template_parm (olddecl, index, level,
28163 targs, complain);
28164 if (newdecl == error_mark_node)
28165 return error_mark_node;
28166 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28167 targs, complain, NULL_TREE);
28168 tree list = build_tree_list (newdef, newdecl);
28169 TEMPLATE_PARM_CONSTRAINTS (list)
28170 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28171 targs, complain, NULL_TREE);
28172 int depth = TMPL_ARGS_DEPTH (targs);
28173 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28174 return list;
28175 }
28176
28177 /* Returns a C++17 class deduction guide template based on the constructor
28178 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28179 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28180 aggregate initialization guide. */
28181
28182 static tree
28183 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28184 {
28185 tree tparms, targs, fparms, fargs, ci;
28186 bool memtmpl = false;
28187 bool explicit_p;
28188 location_t loc;
28189 tree fn_tmpl = NULL_TREE;
28190
28191 if (outer_args)
28192 {
28193 ++processing_template_decl;
28194 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28195 --processing_template_decl;
28196 }
28197
28198 if (!DECL_DECLARES_FUNCTION_P (ctor))
28199 {
28200 if (TYPE_P (ctor))
28201 {
28202 bool copy_p = TYPE_REF_P (ctor);
28203 if (copy_p)
28204 fparms = tree_cons (NULL_TREE, type, void_list_node);
28205 else
28206 fparms = void_list_node;
28207 }
28208 else if (TREE_CODE (ctor) == TREE_LIST)
28209 fparms = ctor;
28210 else
28211 gcc_unreachable ();
28212
28213 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28214 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28215 targs = CLASSTYPE_TI_ARGS (type);
28216 ci = NULL_TREE;
28217 fargs = NULL_TREE;
28218 loc = DECL_SOURCE_LOCATION (ctmpl);
28219 explicit_p = false;
28220 }
28221 else
28222 {
28223 ++processing_template_decl;
28224 bool ok = true;
28225
28226 fn_tmpl
28227 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28228 : DECL_TI_TEMPLATE (ctor));
28229 if (outer_args)
28230 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28231 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28232
28233 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28234 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28235 fully specialized args for the enclosing class. Strip those off, as
28236 the deduction guide won't have those template parameters. */
28237 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28238 TMPL_PARMS_DEPTH (tparms));
28239 /* Discard the 'this' parameter. */
28240 fparms = FUNCTION_ARG_CHAIN (ctor);
28241 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28242 ci = get_constraints (ctor);
28243 loc = DECL_SOURCE_LOCATION (ctor);
28244 explicit_p = DECL_NONCONVERTING_P (ctor);
28245
28246 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28247 {
28248 memtmpl = true;
28249
28250 /* For a member template constructor, we need to flatten the two
28251 template parameter lists into one, and then adjust the function
28252 signature accordingly. This gets...complicated. */
28253 tree save_parms = current_template_parms;
28254
28255 /* For a member template we should have two levels of parms/args, one
28256 for the class and one for the constructor. We stripped
28257 specialized args for further enclosing classes above. */
28258 const int depth = 2;
28259 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28260
28261 /* Template args for translating references to the two-level template
28262 parameters into references to the one-level template parameters we
28263 are creating. */
28264 tree tsubst_args = copy_node (targs);
28265 TMPL_ARGS_LEVEL (tsubst_args, depth)
28266 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28267
28268 /* Template parms for the constructor template. */
28269 tree ftparms = TREE_VALUE (tparms);
28270 unsigned flen = TREE_VEC_LENGTH (ftparms);
28271 /* Template parms for the class template. */
28272 tparms = TREE_CHAIN (tparms);
28273 tree ctparms = TREE_VALUE (tparms);
28274 unsigned clen = TREE_VEC_LENGTH (ctparms);
28275 /* Template parms for the deduction guide start as a copy of the
28276 template parms for the class. We set current_template_parms for
28277 lookup_template_class_1. */
28278 current_template_parms = tparms = copy_node (tparms);
28279 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28280 for (unsigned i = 0; i < clen; ++i)
28281 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28282
28283 /* Now we need to rewrite the constructor parms to append them to the
28284 class parms. */
28285 for (unsigned i = 0; i < flen; ++i)
28286 {
28287 unsigned index = i + clen;
28288 unsigned level = 1;
28289 tree oldelt = TREE_VEC_ELT (ftparms, i);
28290 tree newelt
28291 = rewrite_tparm_list (oldelt, index, level,
28292 tsubst_args, i, complain);
28293 if (newelt == error_mark_node)
28294 ok = false;
28295 TREE_VEC_ELT (new_vec, index) = newelt;
28296 }
28297
28298 /* Now we have a final set of template parms to substitute into the
28299 function signature. */
28300 targs = template_parms_to_args (tparms);
28301 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28302 complain, ctor);
28303 if (fparms == error_mark_node)
28304 ok = false;
28305 if (ci)
28306 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28307
28308 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28309 cp_unevaluated_operand. */
28310 cp_evaluated ev;
28311 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28312 current_template_parms = save_parms;
28313 }
28314
28315 --processing_template_decl;
28316 if (!ok)
28317 return error_mark_node;
28318 }
28319
28320 if (!memtmpl)
28321 {
28322 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28323 tparms = copy_node (tparms);
28324 INNERMOST_TEMPLATE_PARMS (tparms)
28325 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28326 }
28327
28328 tree fntype = build_function_type (type, fparms);
28329 tree ded_fn = build_lang_decl_loc (loc,
28330 FUNCTION_DECL,
28331 dguide_name (type), fntype);
28332 DECL_ARGUMENTS (ded_fn) = fargs;
28333 DECL_ARTIFICIAL (ded_fn) = true;
28334 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28335 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28336 DECL_ARTIFICIAL (ded_tmpl) = true;
28337 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28338 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28339 if (DECL_P (ctor))
28340 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28341 if (ci)
28342 set_constraints (ded_tmpl, ci);
28343
28344 return ded_tmpl;
28345 }
28346
28347 /* Add to LIST the member types for the reshaped initializer CTOR. */
28348
28349 static tree
28350 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28351 {
28352 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28353 tree idx, val; unsigned i;
28354 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28355 {
28356 tree ftype = elt ? elt : TREE_TYPE (idx);
28357 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28358 && CONSTRUCTOR_NELTS (val)
28359 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28360 type gets a single initializer. */
28361 && CP_AGGREGATE_TYPE_P (ftype)
28362 && !(TREE_CODE (ftype) == ARRAY_TYPE
28363 && uses_template_parms (TYPE_DOMAIN (ftype))))
28364 {
28365 tree subelt = NULL_TREE;
28366 if (TREE_CODE (ftype) == ARRAY_TYPE)
28367 subelt = TREE_TYPE (ftype);
28368 list = collect_ctor_idx_types (val, list, subelt);
28369 continue;
28370 }
28371 tree arg = NULL_TREE;
28372 if (i == v->length() - 1
28373 && PACK_EXPANSION_P (ftype))
28374 /* Give the trailing pack expansion parameter a default argument to
28375 match aggregate initialization behavior, even if we deduce the
28376 length of the pack separately to more than we have initializers. */
28377 arg = build_constructor (init_list_type_node, NULL);
28378 /* if ei is of array type and xi is a braced-init-list or string literal,
28379 Ti is an rvalue reference to the declared type of ei */
28380 STRIP_ANY_LOCATION_WRAPPER (val);
28381 if (TREE_CODE (ftype) == ARRAY_TYPE
28382 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28383 || TREE_CODE (val) == STRING_CST))
28384 {
28385 if (TREE_CODE (val) == STRING_CST)
28386 ftype = cp_build_qualified_type
28387 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28388 ftype = (cp_build_reference_type
28389 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28390 }
28391 list = tree_cons (arg, ftype, list);
28392 }
28393
28394 return list;
28395 }
28396
28397 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28398
28399 static bool
28400 is_spec_or_derived (tree etype, tree tmpl)
28401 {
28402 if (!etype || !CLASS_TYPE_P (etype))
28403 return false;
28404
28405 tree type = TREE_TYPE (tmpl);
28406 tree tparms = (INNERMOST_TEMPLATE_PARMS
28407 (DECL_TEMPLATE_PARMS (tmpl)));
28408 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28409 int err = unify (tparms, targs, type, etype,
28410 UNIFY_ALLOW_DERIVED, /*explain*/false);
28411 ggc_free (targs);
28412 return !err;
28413 }
28414
28415 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28416 INIT. */
28417
28418 static tree
28419 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28420 {
28421 if (cxx_dialect < cxx20)
28422 return NULL_TREE;
28423
28424 if (init == NULL_TREE)
28425 return NULL_TREE;
28426
28427 tree type = TREE_TYPE (tmpl);
28428 if (!CP_AGGREGATE_TYPE_P (type))
28429 return NULL_TREE;
28430
28431 /* No aggregate candidate for copy-initialization. */
28432 if (args->length() == 1)
28433 {
28434 tree val = (*args)[0];
28435 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28436 return NULL_TREE;
28437 }
28438
28439 /* If we encounter a problem, we just won't add the candidate. */
28440 tsubst_flags_t complain = tf_none;
28441
28442 tree parms = NULL_TREE;
28443 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28444 {
28445 init = reshape_init (type, init, complain);
28446 if (init == error_mark_node)
28447 return NULL_TREE;
28448 parms = collect_ctor_idx_types (init, parms);
28449 }
28450 else if (TREE_CODE (init) == TREE_LIST)
28451 {
28452 int len = list_length (init);
28453 for (tree field = TYPE_FIELDS (type);
28454 len;
28455 --len, field = DECL_CHAIN (field))
28456 {
28457 field = next_initializable_field (field);
28458 if (!field)
28459 return NULL_TREE;
28460 tree ftype = finish_decltype_type (field, true, complain);
28461 parms = tree_cons (NULL_TREE, ftype, parms);
28462 }
28463 }
28464 else
28465 /* Aggregate initialization doesn't apply to an initializer expression. */
28466 return NULL_TREE;
28467
28468 if (parms)
28469 {
28470 tree last = parms;
28471 parms = nreverse (parms);
28472 TREE_CHAIN (last) = void_list_node;
28473 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28474 return guide;
28475 }
28476
28477 return NULL_TREE;
28478 }
28479
28480 /* UGUIDES are the deduction guides for the underlying template of alias
28481 template TMPL; adjust them to be deduction guides for TMPL. */
28482
28483 static tree
28484 alias_ctad_tweaks (tree tmpl, tree uguides)
28485 {
28486 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28487 class type (9.2.8.2) where the template-name names an alias template A,
28488 the defining-type-id of A must be of the form
28489
28490 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28491
28492 as specified in 9.2.8.2. The guides of A are the set of functions or
28493 function templates formed as follows. For each function or function
28494 template f in the guides of the template named by the simple-template-id
28495 of the defining-type-id, the template arguments of the return type of f
28496 are deduced from the defining-type-id of A according to the process in
28497 13.10.2.5 with the exception that deduction does not fail if not all
28498 template arguments are deduced. Let g denote the result of substituting
28499 these deductions into f. If substitution succeeds, form a function or
28500 function template f' with the following properties and add it to the set
28501 of guides of A:
28502
28503 * The function type of f' is the function type of g.
28504
28505 * If f is a function template, f' is a function template whose template
28506 parameter list consists of all the template parameters of A (including
28507 their default template arguments) that appear in the above deductions or
28508 (recursively) in their default template arguments, followed by the
28509 template parameters of f that were not deduced (including their default
28510 template arguments), otherwise f' is not a function template.
28511
28512 * The associated constraints (13.5.2) are the conjunction of the
28513 associated constraints of g and a constraint that is satisfied if and only
28514 if the arguments of A are deducible (see below) from the return type.
28515
28516 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28517 be so as well.
28518
28519 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28520 considered to be so as well.
28521
28522 * The explicit-specifier of f' is the explicit-specifier of g (if
28523 any). */
28524
28525 /* This implementation differs from the above in two significant ways:
28526
28527 1) We include all template parameters of A, not just some.
28528 2) The added constraint is same_type instead of deducible.
28529
28530 I believe that while it's probably possible to construct a testcase that
28531 behaves differently with this simplification, it should have the same
28532 effect for real uses. Including all template parameters means that we
28533 deduce all parameters of A when resolving the call, so when we're in the
28534 constraint we don't need to deduce them again, we can just check whether
28535 the deduction produced the desired result. */
28536
28537 tsubst_flags_t complain = tf_warning_or_error;
28538 tree atype = TREE_TYPE (tmpl);
28539 tree aguides = NULL_TREE;
28540 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28541 unsigned natparms = TREE_VEC_LENGTH (atparms);
28542 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28543 for (ovl_iterator iter (uguides); iter; ++iter)
28544 {
28545 tree f = *iter;
28546 tree in_decl = f;
28547 location_t loc = DECL_SOURCE_LOCATION (f);
28548 tree ret = TREE_TYPE (TREE_TYPE (f));
28549 tree fprime = f;
28550 if (TREE_CODE (f) == TEMPLATE_DECL)
28551 {
28552 processing_template_decl_sentinel ptds (/*reset*/false);
28553 ++processing_template_decl;
28554
28555 /* Deduce template arguments for f from the type-id of A. */
28556 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28557 unsigned len = TREE_VEC_LENGTH (ftparms);
28558 tree targs = make_tree_vec (len);
28559 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28560 gcc_assert (!err);
28561
28562 /* The number of parms for f' is the number of parms for A plus
28563 non-deduced parms of f. */
28564 unsigned ndlen = 0;
28565 unsigned j;
28566 for (unsigned i = 0; i < len; ++i)
28567 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28568 ++ndlen;
28569 tree gtparms = make_tree_vec (natparms + ndlen);
28570
28571 /* First copy over the parms of A. */
28572 for (j = 0; j < natparms; ++j)
28573 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28574 /* Now rewrite the non-deduced parms of f. */
28575 for (unsigned i = 0; ndlen && i < len; ++i)
28576 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28577 {
28578 --ndlen;
28579 unsigned index = j++;
28580 unsigned level = 1;
28581 tree oldlist = TREE_VEC_ELT (ftparms, i);
28582 tree list = rewrite_tparm_list (oldlist, index, level,
28583 targs, i, complain);
28584 TREE_VEC_ELT (gtparms, index) = list;
28585 }
28586 gtparms = build_tree_list (size_one_node, gtparms);
28587
28588 /* Substitute the deduced arguments plus the rewritten template
28589 parameters into f to get g. This covers the type, copyness,
28590 guideness, and explicit-specifier. */
28591 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28592 if (g == error_mark_node)
28593 return error_mark_node;
28594 DECL_USE_TEMPLATE (g) = 0;
28595 fprime = build_template_decl (g, gtparms, false);
28596 DECL_TEMPLATE_RESULT (fprime) = g;
28597 TREE_TYPE (fprime) = TREE_TYPE (g);
28598 tree gtargs = template_parms_to_args (gtparms);
28599 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28600 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28601
28602 /* Substitute the associated constraints. */
28603 tree ci = get_constraints (f);
28604 if (ci)
28605 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28606 if (ci == error_mark_node)
28607 return error_mark_node;
28608
28609 /* Add a constraint that the return type matches the instantiation of
28610 A with the same template arguments. */
28611 ret = TREE_TYPE (TREE_TYPE (fprime));
28612 if (!same_type_p (atype, ret)
28613 /* FIXME this should mean they don't compare as equivalent. */
28614 || dependent_alias_template_spec_p (atype, nt_opaque))
28615 {
28616 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28617 ci = append_constraint (ci, same);
28618 }
28619
28620 if (ci)
28621 set_constraints (fprime, ci);
28622 }
28623 else
28624 {
28625 /* For a non-template deduction guide, if the arguments of A aren't
28626 deducible from the return type, don't add the candidate. */
28627 tree targs = make_tree_vec (natparms);
28628 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28629 for (unsigned i = 0; !err && i < natparms; ++i)
28630 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28631 err = true;
28632 if (err)
28633 continue;
28634 }
28635
28636 aguides = lookup_add (fprime, aguides);
28637 }
28638
28639 return aguides;
28640 }
28641
28642 /* Return artificial deduction guides built from the constructors of class
28643 template TMPL. */
28644
28645 static tree
28646 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28647 {
28648 tree type = TREE_TYPE (tmpl);
28649 tree outer_args = NULL_TREE;
28650 if (DECL_CLASS_SCOPE_P (tmpl)
28651 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28652 {
28653 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28654 type = TREE_TYPE (most_general_template (tmpl));
28655 }
28656
28657 tree cands = NULL_TREE;
28658
28659 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28660 {
28661 /* Skip inherited constructors. */
28662 if (iter.using_p ())
28663 continue;
28664
28665 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28666 cands = lookup_add (guide, cands);
28667 }
28668
28669 /* Add implicit default constructor deduction guide. */
28670 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28671 {
28672 tree guide = build_deduction_guide (type, type, outer_args,
28673 complain);
28674 cands = lookup_add (guide, cands);
28675 }
28676
28677 /* Add copy guide. */
28678 {
28679 tree gtype = build_reference_type (type);
28680 tree guide = build_deduction_guide (type, gtype, outer_args,
28681 complain);
28682 cands = lookup_add (guide, cands);
28683 }
28684
28685 return cands;
28686 }
28687
28688 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28689
28690 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28691 aggregate candidate is added separately because it depends on the
28692 initializer. */
28693
28694 static tree
28695 deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28696 {
28697 tree guides = NULL_TREE;
28698 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28699 {
28700 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28701 tree tinfo = get_template_info (under);
28702 guides = deduction_guides_for (TI_TEMPLATE (tinfo), complain);
28703 }
28704 else
28705 {
28706 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28707 dguide_name (tmpl),
28708 /*type*/false, /*complain*/false,
28709 /*hidden*/false);
28710 if (guides == error_mark_node)
28711 guides = NULL_TREE;
28712 }
28713
28714 /* Cache the deduction guides for a template. We also remember the result of
28715 lookup, and rebuild everything if it changes; should be very rare. */
28716 tree_pair_p cache = NULL;
28717 if (tree_pair_p &r
28718 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28719 {
28720 cache = r;
28721 if (cache->purpose == guides)
28722 return cache->value;
28723 }
28724 else
28725 {
28726 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28727 cache->purpose = guides;
28728 }
28729
28730 tree cands = NULL_TREE;
28731 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28732 cands = alias_ctad_tweaks (tmpl, guides);
28733 else
28734 {
28735 cands = ctor_deduction_guides_for (tmpl, complain);
28736 for (ovl_iterator it (guides); it; ++it)
28737 cands = lookup_add (*it, cands);
28738 }
28739
28740 cache->value = cands;
28741 return cands;
28742 }
28743
28744 /* Return whether TMPL is a (class template argument-) deducible template. */
28745
28746 bool
28747 ctad_template_p (tree tmpl)
28748 {
28749 /* A deducible template is either a class template or is an alias template
28750 whose defining-type-id is of the form
28751
28752 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28753
28754 where the nested-name-specifier (if any) is non-dependent and the
28755 template-name of the simple-template-id names a deducible template. */
28756
28757 if (DECL_CLASS_TEMPLATE_P (tmpl)
28758 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28759 return true;
28760 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28761 return false;
28762 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28763 if (tree tinfo = get_template_info (orig))
28764 return ctad_template_p (TI_TEMPLATE (tinfo));
28765 return false;
28766 }
28767
28768 /* Deduce template arguments for the class template placeholder PTYPE for
28769 template TMPL based on the initializer INIT, and return the resulting
28770 type. */
28771
28772 static tree
28773 do_class_deduction (tree ptype, tree tmpl, tree init,
28774 int flags, tsubst_flags_t complain)
28775 {
28776 /* We should have handled this in the caller. */
28777 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28778 return ptype;
28779
28780 /* Look through alias templates that just rename another template. */
28781 tmpl = get_underlying_template (tmpl);
28782 if (!ctad_template_p (tmpl))
28783 {
28784 if (complain & tf_error)
28785 error ("non-deducible template %qT used without template arguments", tmpl);
28786 return error_mark_node;
28787 }
28788 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
28789 {
28790 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28791 if (complain & tf_warning_or_error)
28792 pedwarn (input_location, 0, "alias template deduction only available "
28793 "with %<-std=c++20%> or %<-std=gnu++20%>");
28794 }
28795
28796 if (init && TREE_TYPE (init) == ptype)
28797 /* Using the template parm as its own argument. */
28798 return ptype;
28799
28800 tree type = TREE_TYPE (tmpl);
28801
28802 bool try_list_ctor = false;
28803
28804 releasing_vec rv_args = NULL;
28805 vec<tree,va_gc> *&args = *&rv_args;
28806 if (init == NULL_TREE)
28807 args = make_tree_vector ();
28808 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28809 {
28810 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28811 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28812 {
28813 /* As an exception, the first phase in 16.3.1.7 (considering the
28814 initializer list as a single argument) is omitted if the
28815 initializer list consists of a single expression of type cv U,
28816 where U is a specialization of C or a class derived from a
28817 specialization of C. */
28818 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28819 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28820 try_list_ctor = false;
28821 }
28822 if (try_list_ctor || is_std_init_list (type))
28823 args = make_tree_vector_single (init);
28824 else
28825 args = make_tree_vector_from_ctor (init);
28826 }
28827 else if (TREE_CODE (init) == TREE_LIST)
28828 args = make_tree_vector_from_list (init);
28829 else
28830 args = make_tree_vector_single (init);
28831
28832 /* Do this now to avoid problems with erroneous args later on. */
28833 args = resolve_args (args, complain);
28834 if (args == NULL)
28835 return error_mark_node;
28836
28837 tree cands = deduction_guides_for (tmpl, complain);
28838 if (cands == error_mark_node)
28839 return error_mark_node;
28840
28841 /* Prune explicit deduction guides in copy-initialization context. */
28842 bool elided = false;
28843 if (flags & LOOKUP_ONLYCONVERTING)
28844 {
28845 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28846 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28847 elided = true;
28848
28849 if (elided)
28850 {
28851 /* Found a nonconverting guide, prune the candidates. */
28852 tree pruned = NULL_TREE;
28853 for (lkp_iterator iter (cands); iter; ++iter)
28854 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28855 pruned = lookup_add (*iter, pruned);
28856
28857 cands = pruned;
28858 }
28859 }
28860
28861 if (tree guide = maybe_aggr_guide (tmpl, init, args))
28862 cands = lookup_add (guide, cands);
28863
28864 tree call = error_mark_node;
28865
28866 /* If this is list-initialization and the class has a list constructor, first
28867 try deducing from the list as a single argument, as [over.match.list]. */
28868 tree list_cands = NULL_TREE;
28869 if (try_list_ctor && cands)
28870 for (lkp_iterator iter (cands); iter; ++iter)
28871 {
28872 tree dg = *iter;
28873 if (is_list_ctor (dg))
28874 list_cands = lookup_add (dg, list_cands);
28875 }
28876 if (list_cands)
28877 {
28878 ++cp_unevaluated_operand;
28879 call = build_new_function_call (list_cands, &args, tf_decltype);
28880 --cp_unevaluated_operand;
28881
28882 if (call == error_mark_node)
28883 {
28884 /* That didn't work, now try treating the list as a sequence of
28885 arguments. */
28886 release_tree_vector (args);
28887 args = make_tree_vector_from_ctor (init);
28888 }
28889 }
28890
28891 if (elided && !cands)
28892 {
28893 error ("cannot deduce template arguments for copy-initialization"
28894 " of %qT, as it has no non-explicit deduction guides or "
28895 "user-declared constructors", type);
28896 return error_mark_node;
28897 }
28898 else if (!cands && call == error_mark_node)
28899 {
28900 error ("cannot deduce template arguments of %qT, as it has no viable "
28901 "deduction guides", type);
28902 return error_mark_node;
28903 }
28904
28905 if (call == error_mark_node)
28906 {
28907 ++cp_unevaluated_operand;
28908 call = build_new_function_call (cands, &args, tf_decltype);
28909 --cp_unevaluated_operand;
28910 }
28911
28912 if (call == error_mark_node
28913 && (complain & tf_warning_or_error))
28914 {
28915 error ("class template argument deduction failed:");
28916
28917 ++cp_unevaluated_operand;
28918 call = build_new_function_call (cands, &args, complain | tf_decltype);
28919 --cp_unevaluated_operand;
28920
28921 if (elided)
28922 inform (input_location, "explicit deduction guides not considered "
28923 "for copy-initialization");
28924 }
28925
28926 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
28927 }
28928
28929 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28930 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28931 The CONTEXT determines the context in which auto deduction is performed
28932 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28933 OUTER_TARGS are used during template argument deduction
28934 (context == adc_unify) to properly substitute the result, and is ignored
28935 in other contexts.
28936
28937 For partial-concept-ids, extra args may be appended to the list of deduced
28938 template arguments prior to determining constraint satisfaction. */
28939
28940 tree
28941 do_auto_deduction (tree type, tree init, tree auto_node,
28942 tsubst_flags_t complain, auto_deduction_context context,
28943 tree outer_targs, int flags)
28944 {
28945 tree targs;
28946
28947 if (init == error_mark_node)
28948 return error_mark_node;
28949
28950 if (init && type_dependent_expression_p (init)
28951 && context != adc_unify)
28952 /* Defining a subset of type-dependent expressions that we can deduce
28953 from ahead of time isn't worth the trouble. */
28954 return type;
28955
28956 /* Similarly, we can't deduce from another undeduced decl. */
28957 if (init && undeduced_auto_decl (init))
28958 return type;
28959
28960 /* We may be doing a partial substitution, but we still want to replace
28961 auto_node. */
28962 complain &= ~tf_partial;
28963
28964 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
28965 /* C++17 class template argument deduction. */
28966 return do_class_deduction (type, tmpl, init, flags, complain);
28967
28968 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
28969 /* Nothing we can do with this, even in deduction context. */
28970 return type;
28971
28972 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28973 with either a new invented type template parameter U or, if the
28974 initializer is a braced-init-list (8.5.4), with
28975 std::initializer_list<U>. */
28976 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28977 {
28978 if (!DIRECT_LIST_INIT_P (init))
28979 type = listify_autos (type, auto_node);
28980 else if (CONSTRUCTOR_NELTS (init) == 1)
28981 init = CONSTRUCTOR_ELT (init, 0)->value;
28982 else
28983 {
28984 if (complain & tf_warning_or_error)
28985 {
28986 if (permerror (input_location, "direct-list-initialization of "
28987 "%<auto%> requires exactly one element"))
28988 inform (input_location,
28989 "for deduction to %<std::initializer_list%>, use copy-"
28990 "list-initialization (i.e. add %<=%> before the %<{%>)");
28991 }
28992 type = listify_autos (type, auto_node);
28993 }
28994 }
28995
28996 if (type == error_mark_node)
28997 return error_mark_node;
28998
28999 init = resolve_nondeduced_context (init, complain);
29000
29001 if (context == adc_decomp_type
29002 && auto_node == type
29003 && init != error_mark_node
29004 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29005 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29006 and initializer has array type, deduce cv-qualified array type. */
29007 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
29008 complain);
29009 else if (AUTO_IS_DECLTYPE (auto_node))
29010 {
29011 tree stripped_init = tree_strip_any_location_wrapper (init);
29012 bool id = (DECL_P (stripped_init)
29013 || ((TREE_CODE (init) == COMPONENT_REF
29014 || TREE_CODE (init) == SCOPE_REF)
29015 && !REF_PARENTHESIZED_P (init)));
29016 targs = make_tree_vec (1);
29017 TREE_VEC_ELT (targs, 0)
29018 = finish_decltype_type (init, id, tf_warning_or_error);
29019 if (type != auto_node)
29020 {
29021 if (complain & tf_error)
29022 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29023 return error_mark_node;
29024 }
29025 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29026 {
29027 if (complain & tf_error)
29028 error ("%<decltype(auto)%> cannot be cv-qualified");
29029 return error_mark_node;
29030 }
29031 }
29032 else
29033 {
29034 if (error_operand_p (init))
29035 return error_mark_node;
29036
29037 tree parms = build_tree_list (NULL_TREE, type);
29038 tree tparms;
29039
29040 if (flag_concepts)
29041 tparms = extract_autos (type);
29042 else
29043 {
29044 tparms = make_tree_vec (1);
29045 TREE_VEC_ELT (tparms, 0)
29046 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29047 }
29048
29049 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29050 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29051 DEDUCE_CALL,
29052 NULL, /*explain_p=*/false);
29053 if (val > 0)
29054 {
29055 if (processing_template_decl)
29056 /* Try again at instantiation time. */
29057 return type;
29058 if (type && type != error_mark_node
29059 && (complain & tf_error))
29060 /* If type is error_mark_node a diagnostic must have been
29061 emitted by now. Also, having a mention to '<type error>'
29062 in the diagnostic is not really useful to the user. */
29063 {
29064 if (cfun
29065 && FNDECL_USED_AUTO (current_function_decl)
29066 && (auto_node
29067 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29068 && LAMBDA_FUNCTION_P (current_function_decl))
29069 error ("unable to deduce lambda return type from %qE", init);
29070 else
29071 error ("unable to deduce %qT from %qE", type, init);
29072 type_unification_real (tparms, targs, parms, &init, 1, 0,
29073 DEDUCE_CALL,
29074 NULL, /*explain_p=*/true);
29075 }
29076 return error_mark_node;
29077 }
29078 }
29079
29080 /* Check any placeholder constraints against the deduced type. */
29081 if (flag_concepts && !processing_template_decl)
29082 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29083 {
29084 /* Use the deduced type to check the associated constraints. If we
29085 have a partial-concept-id, rebuild the argument list so that
29086 we check using the extra arguments. */
29087 check = unpack_concept_check (check);
29088 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29089 tree cdecl = TREE_OPERAND (check, 0);
29090 if (OVL_P (cdecl))
29091 cdecl = OVL_FIRST (cdecl);
29092 tree cargs = TREE_OPERAND (check, 1);
29093 if (TREE_VEC_LENGTH (cargs) > 1)
29094 {
29095 cargs = copy_node (cargs);
29096 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29097 }
29098 else
29099 cargs = targs;
29100
29101 /* Rebuild the check using the deduced arguments. */
29102 check = build_concept_check (cdecl, cargs, tf_none);
29103
29104 if (!constraints_satisfied_p (check))
29105 {
29106 if (complain & tf_warning_or_error)
29107 {
29108 auto_diagnostic_group d;
29109 switch (context)
29110 {
29111 case adc_unspecified:
29112 case adc_unify:
29113 error("placeholder constraints not satisfied");
29114 break;
29115 case adc_variable_type:
29116 case adc_decomp_type:
29117 error ("deduced initializer does not satisfy "
29118 "placeholder constraints");
29119 break;
29120 case adc_return_type:
29121 error ("deduced return type does not satisfy "
29122 "placeholder constraints");
29123 break;
29124 case adc_requirement:
29125 error ("deduced expression type does not satisfy "
29126 "placeholder constraints");
29127 break;
29128 }
29129 diagnose_constraints (input_location, check, targs);
29130 }
29131 return error_mark_node;
29132 }
29133 }
29134
29135 if (processing_template_decl && context != adc_unify)
29136 outer_targs = current_template_args ();
29137 targs = add_to_template_args (outer_targs, targs);
29138 return tsubst (type, targs, complain, NULL_TREE);
29139 }
29140
29141 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29142 result. */
29143
29144 tree
29145 splice_late_return_type (tree type, tree late_return_type)
29146 {
29147 if (late_return_type)
29148 {
29149 gcc_assert (is_auto (type) || seen_error ());
29150 return late_return_type;
29151 }
29152
29153 if (tree *auto_node = find_type_usage (&type, is_auto))
29154 {
29155 tree idx = get_template_parm_index (*auto_node);
29156 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29157 {
29158 /* In an abbreviated function template we didn't know we were dealing
29159 with a function template when we saw the auto return type, so update
29160 it to have the correct level. */
29161 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29162 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29163 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29164 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29165 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29166 *auto_node = new_auto;
29167 }
29168 }
29169 return type;
29170 }
29171
29172 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29173 'decltype(auto)' or a deduced class template. */
29174
29175 bool
29176 is_auto (const_tree type)
29177 {
29178 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29179 && (TYPE_IDENTIFIER (type) == auto_identifier
29180 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29181 return true;
29182 else
29183 return false;
29184 }
29185
29186 /* for_each_template_parm callback for type_uses_auto. */
29187
29188 int
29189 is_auto_r (tree tp, void */*data*/)
29190 {
29191 return is_auto (tp);
29192 }
29193
29194 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29195 a use of `auto'. Returns NULL_TREE otherwise. */
29196
29197 tree
29198 type_uses_auto (tree type)
29199 {
29200 if (type == NULL_TREE)
29201 return NULL_TREE;
29202 else if (flag_concepts)
29203 {
29204 /* The Concepts TS allows multiple autos in one type-specifier; just
29205 return the first one we find, do_auto_deduction will collect all of
29206 them. */
29207 if (uses_template_parms (type))
29208 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29209 /*visited*/NULL, /*nondeduced*/false);
29210 else
29211 return NULL_TREE;
29212 }
29213 else if (tree *tp = find_type_usage (&type, is_auto))
29214 return *tp;
29215 else
29216 return NULL_TREE;
29217 }
29218
29219 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29220 concepts are enabled, auto is acceptable in template arguments, but
29221 only when TEMPL identifies a template class. Return TRUE if any
29222 such errors were reported. */
29223
29224 bool
29225 check_auto_in_tmpl_args (tree tmpl, tree args)
29226 {
29227 /* If there were previous errors, nevermind. */
29228 if (!args || TREE_CODE (args) != TREE_VEC)
29229 return false;
29230
29231 /* If TMPL is an identifier, we're parsing and we can't tell yet
29232 whether TMPL is supposed to be a type, a function or a variable.
29233 We'll only be able to tell during template substitution, so we
29234 expect to be called again then. If concepts are enabled and we
29235 know we have a type, we're ok. */
29236 if (flag_concepts
29237 && (identifier_p (tmpl)
29238 || (DECL_P (tmpl)
29239 && (DECL_TYPE_TEMPLATE_P (tmpl)
29240 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29241 return false;
29242
29243 /* Quickly search for any occurrences of auto; usually there won't
29244 be any, and then we'll avoid allocating the vector. */
29245 if (!type_uses_auto (args))
29246 return false;
29247
29248 bool errors = false;
29249
29250 tree vec = extract_autos (args);
29251 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29252 {
29253 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29254 error_at (DECL_SOURCE_LOCATION (xauto),
29255 "invalid use of %qT in template argument", xauto);
29256 errors = true;
29257 }
29258
29259 return errors;
29260 }
29261
29262 /* Recursively walk over && expressions searching for EXPR. Return a reference
29263 to that expression. */
29264
29265 static tree *find_template_requirement (tree *t, tree key)
29266 {
29267 if (*t == key)
29268 return t;
29269 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29270 {
29271 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29272 return p;
29273 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29274 return p;
29275 }
29276 return 0;
29277 }
29278
29279 /* Convert the generic type parameters in PARM that match the types given in the
29280 range [START_IDX, END_IDX) from the current_template_parms into generic type
29281 packs. */
29282
29283 tree
29284 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29285 {
29286 tree current = current_template_parms;
29287 int depth = TMPL_PARMS_DEPTH (current);
29288 current = INNERMOST_TEMPLATE_PARMS (current);
29289 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29290
29291 for (int i = 0; i < start_idx; ++i)
29292 TREE_VEC_ELT (replacement, i)
29293 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29294
29295 for (int i = start_idx; i < end_idx; ++i)
29296 {
29297 /* Create a distinct parameter pack type from the current parm and add it
29298 to the replacement args to tsubst below into the generic function
29299 parameter. */
29300 tree node = TREE_VEC_ELT (current, i);
29301 tree o = TREE_TYPE (TREE_VALUE (node));
29302 tree t = copy_type (o);
29303 TEMPLATE_TYPE_PARM_INDEX (t)
29304 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29305 t, 0, 0, tf_none);
29306 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29307 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29308 TYPE_MAIN_VARIANT (t) = t;
29309 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29310 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29311 TREE_VEC_ELT (replacement, i) = t;
29312
29313 /* Replace the current template parameter with new pack. */
29314 TREE_VALUE (node) = TREE_CHAIN (t);
29315
29316 /* Surgically adjust the associated constraint of adjusted parameter
29317 and it's corresponding contribution to the current template
29318 requirements. */
29319 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29320 {
29321 tree id = unpack_concept_check (constr);
29322 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
29323 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29324 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29325
29326 /* If there was a constraint, we also need to replace that in
29327 the template requirements, which we've already built. */
29328 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29329 reqs = find_template_requirement (reqs, constr);
29330 *reqs = fold;
29331 }
29332 }
29333
29334 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29335 TREE_VEC_ELT (replacement, i)
29336 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29337
29338 /* If there are more levels then build up the replacement with the outer
29339 template parms. */
29340 if (depth > 1)
29341 replacement = add_to_template_args (template_parms_to_args
29342 (TREE_CHAIN (current_template_parms)),
29343 replacement);
29344
29345 return tsubst (parm, replacement, tf_none, NULL_TREE);
29346 }
29347
29348 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29349 0..N-1. */
29350
29351 void
29352 declare_integer_pack (void)
29353 {
29354 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29355 build_function_type_list (integer_type_node,
29356 integer_type_node,
29357 NULL_TREE),
29358 NULL_TREE, ECF_CONST);
29359 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29360 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29361 CP_BUILT_IN_INTEGER_PACK);
29362 }
29363
29364 /* Set up the hash tables for template instantiations. */
29365
29366 void
29367 init_template_processing (void)
29368 {
29369 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29370 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29371
29372 if (cxx_dialect >= cxx11)
29373 declare_integer_pack ();
29374 }
29375
29376 /* Print stats about the template hash tables for -fstats. */
29377
29378 void
29379 print_template_statistics (void)
29380 {
29381 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29382 "%f collisions\n", (long) decl_specializations->size (),
29383 (long) decl_specializations->elements (),
29384 decl_specializations->collisions ());
29385 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29386 "%f collisions\n", (long) type_specializations->size (),
29387 (long) type_specializations->elements (),
29388 type_specializations->collisions ());
29389 }
29390
29391 #if CHECKING_P
29392
29393 namespace selftest {
29394
29395 /* Verify that build_non_dependent_expr () works, for various expressions,
29396 and that location wrappers don't affect the results. */
29397
29398 static void
29399 test_build_non_dependent_expr ()
29400 {
29401 location_t loc = BUILTINS_LOCATION;
29402
29403 /* Verify constants, without and with location wrappers. */
29404 tree int_cst = build_int_cst (integer_type_node, 42);
29405 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29406
29407 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29408 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29409 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29410
29411 tree string_lit = build_string (4, "foo");
29412 TREE_TYPE (string_lit) = char_array_type_node;
29413 string_lit = fix_string_type (string_lit);
29414 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29415
29416 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29417 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29418 ASSERT_EQ (wrapped_string_lit,
29419 build_non_dependent_expr (wrapped_string_lit));
29420 }
29421
29422 /* Verify that type_dependent_expression_p () works correctly, even
29423 in the presence of location wrapper nodes. */
29424
29425 static void
29426 test_type_dependent_expression_p ()
29427 {
29428 location_t loc = BUILTINS_LOCATION;
29429
29430 tree name = get_identifier ("foo");
29431
29432 /* If no templates are involved, nothing is type-dependent. */
29433 gcc_assert (!processing_template_decl);
29434 ASSERT_FALSE (type_dependent_expression_p (name));
29435
29436 ++processing_template_decl;
29437
29438 /* Within a template, an unresolved name is always type-dependent. */
29439 ASSERT_TRUE (type_dependent_expression_p (name));
29440
29441 /* Ensure it copes with NULL_TREE and errors. */
29442 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29443 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29444
29445 /* A USING_DECL in a template should be type-dependent, even if wrapped
29446 with a location wrapper (PR c++/83799). */
29447 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29448 TREE_TYPE (using_decl) = integer_type_node;
29449 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29450 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29451 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29452 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29453
29454 --processing_template_decl;
29455 }
29456
29457 /* Run all of the selftests within this file. */
29458
29459 void
29460 cp_pt_c_tests ()
29461 {
29462 test_build_non_dependent_expr ();
29463 test_type_dependent_expression_p ();
29464 }
29465
29466 } // namespace selftest
29467
29468 #endif /* #if CHECKING_P */
29469
29470 #include "gt-cp-pt.h"