]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/pt.c
c++: Alias template instantiation template info
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45 #include "target.h"
46
47 /* The type of functions taking a tree, and some additional data, and
48 returning an int. */
49 typedef int (*tree_fn_t) (tree, void*);
50
51 /* The PENDING_TEMPLATES is a list of templates whose instantiations
52 have been deferred, either because their definitions were not yet
53 available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
55 {
56 struct pending_template *next;
57 struct tinst_level *tinst;
58 };
59
60 static GTY(()) struct pending_template *pending_templates;
61 static GTY(()) struct pending_template *last_pending_template;
62
63 int processing_template_parmlist;
64 static int template_header_count;
65
66 static GTY(()) tree saved_trees;
67 static vec<int> inline_parm_levels;
68
69 static GTY(()) struct tinst_level *current_tinst_level;
70
71 static GTY(()) vec<tree, va_gc> *saved_access_scope;
72
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
77
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
80 //
81 // Implementation of the RAII helper for creating new local
82 // specializations.
83 local_specialization_stack::local_specialization_stack (lss_policy policy)
84 : saved (local_specializations)
85 {
86 if (policy == lss_nop)
87 ;
88 else if (policy == lss_blank || !saved)
89 local_specializations = new hash_map<tree, tree>;
90 else
91 local_specializations = new hash_map<tree, tree>(*saved);
92 }
93
94 local_specialization_stack::~local_specialization_stack ()
95 {
96 if (local_specializations != saved)
97 {
98 delete local_specializations;
99 local_specializations = saved;
100 }
101 }
102
103 /* True if we've recursed into fn_type_unification too many times. */
104 static bool excessive_deduction_depth;
105
106 struct GTY((for_user)) spec_entry
107 {
108 tree tmpl;
109 tree args;
110 tree spec;
111 };
112
113 struct spec_hasher : ggc_ptr_hash<spec_entry>
114 {
115 static hashval_t hash (spec_entry *);
116 static bool equal (spec_entry *, spec_entry *);
117 };
118
119 /* The general template is not in these tables. */
120 typedef hash_table<spec_hasher> spec_hash_table;
121 static GTY (()) spec_hash_table *decl_specializations;
122 static GTY (()) spec_hash_table *type_specializations;
123
124 /* Contains canonical template parameter types. The vector is indexed by
125 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
126 TREE_LIST, whose TREE_VALUEs contain the canonical template
127 parameters of various types and levels. */
128 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
129
130 #define UNIFY_ALLOW_NONE 0
131 #define UNIFY_ALLOW_MORE_CV_QUAL 1
132 #define UNIFY_ALLOW_LESS_CV_QUAL 2
133 #define UNIFY_ALLOW_DERIVED 4
134 #define UNIFY_ALLOW_INTEGER 8
135 #define UNIFY_ALLOW_OUTER_LEVEL 16
136 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
137 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
138
139 enum template_base_result {
140 tbr_incomplete_type,
141 tbr_ambiguous_baseclass,
142 tbr_success
143 };
144
145 static bool resolve_overloaded_unification (tree, tree, tree, tree,
146 unification_kind_t, int,
147 bool);
148 static int try_one_overload (tree, tree, tree, tree, tree,
149 unification_kind_t, int, bool, bool);
150 static int unify (tree, tree, tree, tree, int, bool);
151 static void add_pending_template (tree);
152 static tree reopen_tinst_level (struct tinst_level *);
153 static tree tsubst_initializer_list (tree, tree);
154 static tree get_partial_spec_bindings (tree, tree, tree);
155 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
156 bool, bool);
157 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
158 bool, bool);
159 static void tsubst_enum (tree, tree, tree);
160 static tree add_to_template_args (tree, tree);
161 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
162 static int check_non_deducible_conversion (tree, tree, int, int,
163 struct conversion **, bool);
164 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
165 tree);
166 static int type_unification_real (tree, tree, tree, const tree *,
167 unsigned int, int, unification_kind_t,
168 vec<deferred_access_check, va_gc> **,
169 bool);
170 static void note_template_header (int);
171 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
172 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
173 static tree convert_template_argument (tree, tree, tree,
174 tsubst_flags_t, int, tree);
175 static tree for_each_template_parm (tree, tree_fn_t, void*,
176 hash_set<tree> *, bool, tree_fn_t = NULL);
177 static tree expand_template_argument_pack (tree);
178 static tree build_template_parm_index (int, int, int, tree, tree);
179 static bool inline_needs_template_parms (tree, bool);
180 static void push_inline_template_parms_recursive (tree, int);
181 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
182 static int mark_template_parm (tree, void *);
183 static int template_parm_this_level_p (tree, void *);
184 static tree tsubst_friend_function (tree, tree);
185 static tree tsubst_friend_class (tree, tree);
186 static int can_complete_type_without_circularity (tree);
187 static tree get_bindings (tree, tree, tree, bool);
188 static int template_decl_level (tree);
189 static int check_cv_quals_for_unify (int, tree, tree);
190 static int unify_pack_expansion (tree, tree, tree,
191 tree, unification_kind_t, bool, bool);
192 static tree copy_template_args (tree);
193 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
194 tree most_specialized_partial_spec (tree, tsubst_flags_t);
195 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
196 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
197 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
198 static bool check_specialization_scope (void);
199 static tree process_partial_specialization (tree);
200 static void set_current_access_from_decl (tree);
201 static enum template_base_result get_template_base (tree, tree, tree, tree,
202 bool , tree *);
203 static tree try_class_unification (tree, tree, tree, tree, bool);
204 static bool class_nttp_const_wrapper_p (tree t);
205 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
206 tree, tree);
207 static bool template_template_parm_bindings_ok_p (tree, tree);
208 static void tsubst_default_arguments (tree, tsubst_flags_t);
209 static tree for_each_template_parm_r (tree *, int *, void *);
210 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
211 static void copy_default_args_to_explicit_spec (tree);
212 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
213 static bool dependent_template_arg_p (tree);
214 static bool any_template_arguments_need_structural_equality_p (tree);
215 static bool dependent_type_p_r (tree);
216 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
217 static tree tsubst_decl (tree, tree, tsubst_flags_t);
218 static void perform_typedefs_access_check (tree tmpl, tree targs);
219 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
220 location_t);
221 static tree listify (tree);
222 static tree listify_autos (tree, tree);
223 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
224 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
225 static bool complex_alias_template_p (const_tree tmpl);
226 static tree get_underlying_template (tree);
227 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
228 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
229 static tree make_argument_pack (tree);
230 static void register_parameter_specializations (tree, tree);
231 static tree enclosing_instantiation_of (tree tctx);
232
233 /* Make the current scope suitable for access checking when we are
234 processing T. T can be FUNCTION_DECL for instantiated function
235 template, VAR_DECL for static member variable, or TYPE_DECL for
236 alias template (needed by instantiate_decl). */
237
238 void
239 push_access_scope (tree t)
240 {
241 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
242 || TREE_CODE (t) == TYPE_DECL);
243
244 if (DECL_FRIEND_CONTEXT (t))
245 push_nested_class (DECL_FRIEND_CONTEXT (t));
246 else if (DECL_CLASS_SCOPE_P (t))
247 push_nested_class (DECL_CONTEXT (t));
248 else
249 push_to_top_level ();
250
251 if (TREE_CODE (t) == FUNCTION_DECL)
252 {
253 vec_safe_push (saved_access_scope, current_function_decl);
254 current_function_decl = t;
255 }
256 }
257
258 /* Restore the scope set up by push_access_scope. T is the node we
259 are processing. */
260
261 void
262 pop_access_scope (tree t)
263 {
264 if (TREE_CODE (t) == FUNCTION_DECL)
265 current_function_decl = saved_access_scope->pop();
266
267 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
268 pop_nested_class ();
269 else
270 pop_from_top_level ();
271 }
272
273 /* Do any processing required when DECL (a member template
274 declaration) is finished. Returns the TEMPLATE_DECL corresponding
275 to DECL, unless it is a specialization, in which case the DECL
276 itself is returned. */
277
278 tree
279 finish_member_template_decl (tree decl)
280 {
281 if (decl == error_mark_node)
282 return error_mark_node;
283
284 gcc_assert (DECL_P (decl));
285
286 if (TREE_CODE (decl) == TYPE_DECL)
287 {
288 tree type;
289
290 type = TREE_TYPE (decl);
291 if (type == error_mark_node)
292 return error_mark_node;
293 if (MAYBE_CLASS_TYPE_P (type)
294 && CLASSTYPE_TEMPLATE_INFO (type)
295 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
296 {
297 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
298 check_member_template (tmpl);
299 return tmpl;
300 }
301 return NULL_TREE;
302 }
303 else if (TREE_CODE (decl) == FIELD_DECL)
304 error_at (DECL_SOURCE_LOCATION (decl),
305 "data member %qD cannot be a member template", decl);
306 else if (DECL_TEMPLATE_INFO (decl))
307 {
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
309 {
310 check_member_template (DECL_TI_TEMPLATE (decl));
311 return DECL_TI_TEMPLATE (decl);
312 }
313 else
314 return decl;
315 }
316 else
317 error_at (DECL_SOURCE_LOCATION (decl),
318 "invalid member template declaration %qD", decl);
319
320 return error_mark_node;
321 }
322
323 /* Create a template info node. */
324
325 tree
326 build_template_info (tree template_decl, tree template_args)
327 {
328 tree result = make_node (TEMPLATE_INFO);
329 TI_TEMPLATE (result) = template_decl;
330 TI_ARGS (result) = template_args;
331 return result;
332 }
333
334 /* Return the template info node corresponding to T, whatever T is. */
335
336 tree
337 get_template_info (const_tree t)
338 {
339 tree tinfo = NULL_TREE;
340
341 if (!t || t == error_mark_node)
342 return NULL;
343
344 if (TREE_CODE (t) == NAMESPACE_DECL
345 || TREE_CODE (t) == PARM_DECL)
346 return NULL;
347
348 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
349 tinfo = DECL_TEMPLATE_INFO (t);
350
351 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
352 t = TREE_TYPE (t);
353
354 if (OVERLOAD_TYPE_P (t))
355 tinfo = TYPE_TEMPLATE_INFO (t);
356 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
357 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
358
359 return tinfo;
360 }
361
362 /* Returns the template nesting level of the indicated class TYPE.
363
364 For example, in:
365 template <class T>
366 struct A
367 {
368 template <class U>
369 struct B {};
370 };
371
372 A<T>::B<U> has depth two, while A<T> has depth one.
373 Both A<T>::B<int> and A<int>::B<U> have depth one, if
374 they are instantiations, not specializations.
375
376 This function is guaranteed to return 0 if passed NULL_TREE so
377 that, for example, `template_class_depth (current_class_type)' is
378 always safe. */
379
380 int
381 template_class_depth (tree type)
382 {
383 int depth;
384
385 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
386 {
387 tree tinfo = get_template_info (type);
388
389 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
390 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
391 ++depth;
392
393 if (DECL_P (type))
394 {
395 if (tree fctx = DECL_FRIEND_CONTEXT (type))
396 type = fctx;
397 else
398 type = CP_DECL_CONTEXT (type);
399 }
400 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
401 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
402 else
403 type = CP_TYPE_CONTEXT (type);
404 }
405
406 return depth;
407 }
408
409 /* Return TRUE if NODE instantiates a template that has arguments of
410 its own, be it directly a primary template or indirectly through a
411 partial specializations. */
412 static bool
413 instantiates_primary_template_p (tree node)
414 {
415 tree tinfo = get_template_info (node);
416 if (!tinfo)
417 return false;
418
419 tree tmpl = TI_TEMPLATE (tinfo);
420 if (PRIMARY_TEMPLATE_P (tmpl))
421 return true;
422
423 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
424 return false;
425
426 /* So now we know we have a specialization, but it could be a full
427 or a partial specialization. To tell which, compare the depth of
428 its template arguments with those of its context. */
429
430 tree ctxt = DECL_CONTEXT (tmpl);
431 tree ctinfo = get_template_info (ctxt);
432 if (!ctinfo)
433 return true;
434
435 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
436 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
437 }
438
439 /* Subroutine of maybe_begin_member_template_processing.
440 Returns true if processing DECL needs us to push template parms. */
441
442 static bool
443 inline_needs_template_parms (tree decl, bool nsdmi)
444 {
445 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
446 return false;
447
448 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
449 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
450 }
451
452 /* Subroutine of maybe_begin_member_template_processing.
453 Push the template parms in PARMS, starting from LEVELS steps into the
454 chain, and ending at the beginning, since template parms are listed
455 innermost first. */
456
457 static void
458 push_inline_template_parms_recursive (tree parmlist, int levels)
459 {
460 tree parms = TREE_VALUE (parmlist);
461 int i;
462
463 if (levels > 1)
464 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
465
466 ++processing_template_decl;
467 current_template_parms
468 = tree_cons (size_int (processing_template_decl),
469 parms, current_template_parms);
470 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
471
472 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
473 NULL);
474 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
475 {
476 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
477
478 if (error_operand_p (parm))
479 continue;
480
481 gcc_assert (DECL_P (parm));
482
483 switch (TREE_CODE (parm))
484 {
485 case TYPE_DECL:
486 case TEMPLATE_DECL:
487 pushdecl (parm);
488 break;
489
490 case PARM_DECL:
491 /* Push the CONST_DECL. */
492 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
493 break;
494
495 default:
496 gcc_unreachable ();
497 }
498 }
499 }
500
501 /* Restore the template parameter context for a member template, a
502 friend template defined in a class definition, or a non-template
503 member of template class. */
504
505 void
506 maybe_begin_member_template_processing (tree decl)
507 {
508 tree parms;
509 int levels = 0;
510 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
511
512 if (nsdmi)
513 {
514 tree ctx = DECL_CONTEXT (decl);
515 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
516 /* Disregard full specializations (c++/60999). */
517 && uses_template_parms (ctx)
518 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
519 }
520
521 if (inline_needs_template_parms (decl, nsdmi))
522 {
523 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
524 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
525
526 if (DECL_TEMPLATE_SPECIALIZATION (decl))
527 {
528 --levels;
529 parms = TREE_CHAIN (parms);
530 }
531
532 push_inline_template_parms_recursive (parms, levels);
533 }
534
535 /* Remember how many levels of template parameters we pushed so that
536 we can pop them later. */
537 inline_parm_levels.safe_push (levels);
538 }
539
540 /* Undo the effects of maybe_begin_member_template_processing. */
541
542 void
543 maybe_end_member_template_processing (void)
544 {
545 int i;
546 int last;
547
548 if (inline_parm_levels.length () == 0)
549 return;
550
551 last = inline_parm_levels.pop ();
552 for (i = 0; i < last; ++i)
553 {
554 --processing_template_decl;
555 current_template_parms = TREE_CHAIN (current_template_parms);
556 poplevel (0, 0, 0);
557 }
558 }
559
560 /* Return a new template argument vector which contains all of ARGS,
561 but has as its innermost set of arguments the EXTRA_ARGS. */
562
563 static tree
564 add_to_template_args (tree args, tree extra_args)
565 {
566 tree new_args;
567 int extra_depth;
568 int i;
569 int j;
570
571 if (args == NULL_TREE || extra_args == error_mark_node)
572 return extra_args;
573
574 extra_depth = TMPL_ARGS_DEPTH (extra_args);
575 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
576
577 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
578 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
579
580 for (j = 1; j <= extra_depth; ++j, ++i)
581 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
582
583 return new_args;
584 }
585
586 /* Like add_to_template_args, but only the outermost ARGS are added to
587 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
588 (EXTRA_ARGS) levels are added. This function is used to combine
589 the template arguments from a partial instantiation with the
590 template arguments used to attain the full instantiation from the
591 partial instantiation. */
592
593 tree
594 add_outermost_template_args (tree args, tree extra_args)
595 {
596 tree new_args;
597
598 /* If there are more levels of EXTRA_ARGS than there are ARGS,
599 something very fishy is going on. */
600 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
601
602 /* If *all* the new arguments will be the EXTRA_ARGS, just return
603 them. */
604 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
605 return extra_args;
606
607 /* For the moment, we make ARGS look like it contains fewer levels. */
608 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
609
610 new_args = add_to_template_args (args, extra_args);
611
612 /* Now, we restore ARGS to its full dimensions. */
613 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
614
615 return new_args;
616 }
617
618 /* Return the N levels of innermost template arguments from the ARGS. */
619
620 tree
621 get_innermost_template_args (tree args, int n)
622 {
623 tree new_args;
624 int extra_levels;
625 int i;
626
627 gcc_assert (n >= 0);
628
629 /* If N is 1, just return the innermost set of template arguments. */
630 if (n == 1)
631 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
632
633 /* If we're not removing anything, just return the arguments we were
634 given. */
635 extra_levels = TMPL_ARGS_DEPTH (args) - n;
636 gcc_assert (extra_levels >= 0);
637 if (extra_levels == 0)
638 return args;
639
640 /* Make a new set of arguments, not containing the outer arguments. */
641 new_args = make_tree_vec (n);
642 for (i = 1; i <= n; ++i)
643 SET_TMPL_ARGS_LEVEL (new_args, i,
644 TMPL_ARGS_LEVEL (args, i + extra_levels));
645
646 return new_args;
647 }
648
649 /* The inverse of get_innermost_template_args: Return all but the innermost
650 EXTRA_LEVELS levels of template arguments from the ARGS. */
651
652 static tree
653 strip_innermost_template_args (tree args, int extra_levels)
654 {
655 tree new_args;
656 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
657 int i;
658
659 gcc_assert (n >= 0);
660
661 /* If N is 1, just return the outermost set of template arguments. */
662 if (n == 1)
663 return TMPL_ARGS_LEVEL (args, 1);
664
665 /* If we're not removing anything, just return the arguments we were
666 given. */
667 gcc_assert (extra_levels >= 0);
668 if (extra_levels == 0)
669 return args;
670
671 /* Make a new set of arguments, not containing the inner arguments. */
672 new_args = make_tree_vec (n);
673 for (i = 1; i <= n; ++i)
674 SET_TMPL_ARGS_LEVEL (new_args, i,
675 TMPL_ARGS_LEVEL (args, i));
676
677 return new_args;
678 }
679
680 /* We've got a template header coming up; push to a new level for storing
681 the parms. */
682
683 void
684 begin_template_parm_list (void)
685 {
686 /* We use a non-tag-transparent scope here, which causes pushtag to
687 put tags in this scope, rather than in the enclosing class or
688 namespace scope. This is the right thing, since we want
689 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
690 global template class, push_template_decl handles putting the
691 TEMPLATE_DECL into top-level scope. For a nested template class,
692 e.g.:
693
694 template <class T> struct S1 {
695 template <class T> struct S2 {};
696 };
697
698 pushtag contains special code to insert the TEMPLATE_DECL for S2
699 at the right scope. */
700 begin_scope (sk_template_parms, NULL);
701 ++processing_template_decl;
702 ++processing_template_parmlist;
703 note_template_header (0);
704
705 /* Add a dummy parameter level while we process the parameter list. */
706 current_template_parms
707 = tree_cons (size_int (processing_template_decl),
708 make_tree_vec (0),
709 current_template_parms);
710 }
711
712 /* This routine is called when a specialization is declared. If it is
713 invalid to declare a specialization here, an error is reported and
714 false is returned, otherwise this routine will return true. */
715
716 static bool
717 check_specialization_scope (void)
718 {
719 tree scope = current_scope ();
720
721 /* [temp.expl.spec]
722
723 An explicit specialization shall be declared in the namespace of
724 which the template is a member, or, for member templates, in the
725 namespace of which the enclosing class or enclosing class
726 template is a member. An explicit specialization of a member
727 function, member class or static data member of a class template
728 shall be declared in the namespace of which the class template
729 is a member. */
730 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
731 {
732 error ("explicit specialization in non-namespace scope %qD", scope);
733 return false;
734 }
735
736 /* [temp.expl.spec]
737
738 In an explicit specialization declaration for a member of a class
739 template or a member template that appears in namespace scope,
740 the member template and some of its enclosing class templates may
741 remain unspecialized, except that the declaration shall not
742 explicitly specialize a class member template if its enclosing
743 class templates are not explicitly specialized as well. */
744 if (current_template_parms)
745 {
746 error ("enclosing class templates are not explicitly specialized");
747 return false;
748 }
749
750 return true;
751 }
752
753 /* We've just seen template <>. */
754
755 bool
756 begin_specialization (void)
757 {
758 begin_scope (sk_template_spec, NULL);
759 note_template_header (1);
760 return check_specialization_scope ();
761 }
762
763 /* Called at then end of processing a declaration preceded by
764 template<>. */
765
766 void
767 end_specialization (void)
768 {
769 finish_scope ();
770 reset_specialization ();
771 }
772
773 /* Any template <>'s that we have seen thus far are not referring to a
774 function specialization. */
775
776 void
777 reset_specialization (void)
778 {
779 processing_specialization = 0;
780 template_header_count = 0;
781 }
782
783 /* We've just seen a template header. If SPECIALIZATION is nonzero,
784 it was of the form template <>. */
785
786 static void
787 note_template_header (int specialization)
788 {
789 processing_specialization = specialization;
790 template_header_count++;
791 }
792
793 /* We're beginning an explicit instantiation. */
794
795 void
796 begin_explicit_instantiation (void)
797 {
798 gcc_assert (!processing_explicit_instantiation);
799 processing_explicit_instantiation = true;
800 }
801
802
803 void
804 end_explicit_instantiation (void)
805 {
806 gcc_assert (processing_explicit_instantiation);
807 processing_explicit_instantiation = false;
808 }
809
810 /* An explicit specialization or partial specialization of TMPL is being
811 declared. Check that the namespace in which the specialization is
812 occurring is permissible. Returns false iff it is invalid to
813 specialize TMPL in the current namespace. */
814
815 static bool
816 check_specialization_namespace (tree tmpl)
817 {
818 tree tpl_ns = decl_namespace_context (tmpl);
819
820 /* [tmpl.expl.spec]
821
822 An explicit specialization shall be declared in a namespace enclosing the
823 specialized template. An explicit specialization whose declarator-id is
824 not qualified shall be declared in the nearest enclosing namespace of the
825 template, or, if the namespace is inline (7.3.1), any namespace from its
826 enclosing namespace set. */
827 if (current_scope() != DECL_CONTEXT (tmpl)
828 && !at_namespace_scope_p ())
829 {
830 error ("specialization of %qD must appear at namespace scope", tmpl);
831 return false;
832 }
833
834 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
835 /* Same or enclosing namespace. */
836 return true;
837 else
838 {
839 auto_diagnostic_group d;
840 if (permerror (input_location,
841 "specialization of %qD in different namespace", tmpl))
842 inform (DECL_SOURCE_LOCATION (tmpl),
843 " from definition of %q#D", tmpl);
844 return false;
845 }
846 }
847
848 /* SPEC is an explicit instantiation. Check that it is valid to
849 perform this explicit instantiation in the current namespace. */
850
851 static void
852 check_explicit_instantiation_namespace (tree spec)
853 {
854 tree ns;
855
856 /* DR 275: An explicit instantiation shall appear in an enclosing
857 namespace of its template. */
858 ns = decl_namespace_context (spec);
859 if (!is_nested_namespace (current_namespace, ns))
860 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
861 "(which does not enclose namespace %qD)",
862 spec, current_namespace, ns);
863 }
864
865 /* Returns the type of a template specialization only if that
866 specialization needs to be defined. Otherwise (e.g., if the type has
867 already been defined), the function returns NULL_TREE. */
868
869 static tree
870 maybe_new_partial_specialization (tree type)
871 {
872 /* An implicit instantiation of an incomplete type implies
873 the definition of a new class template.
874
875 template<typename T>
876 struct S;
877
878 template<typename T>
879 struct S<T*>;
880
881 Here, S<T*> is an implicit instantiation of S whose type
882 is incomplete. */
883 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
884 return type;
885
886 /* It can also be the case that TYPE is a completed specialization.
887 Continuing the previous example, suppose we also declare:
888
889 template<typename T>
890 requires Integral<T>
891 struct S<T*>;
892
893 Here, S<T*> refers to the specialization S<T*> defined
894 above. However, we need to differentiate definitions because
895 we intend to define a new partial specialization. In this case,
896 we rely on the fact that the constraints are different for
897 this declaration than that above.
898
899 Note that we also get here for injected class names and
900 late-parsed template definitions. We must ensure that we
901 do not create new type declarations for those cases. */
902 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
903 {
904 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
905 tree args = CLASSTYPE_TI_ARGS (type);
906
907 /* If there are no template parameters, this cannot be a new
908 partial template specialization? */
909 if (!current_template_parms)
910 return NULL_TREE;
911
912 /* The injected-class-name is not a new partial specialization. */
913 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
914 return NULL_TREE;
915
916 /* If the constraints are not the same as those of the primary
917 then, we can probably create a new specialization. */
918 tree type_constr = current_template_constraints ();
919
920 if (type == TREE_TYPE (tmpl))
921 {
922 tree main_constr = get_constraints (tmpl);
923 if (equivalent_constraints (type_constr, main_constr))
924 return NULL_TREE;
925 }
926
927 /* Also, if there's a pre-existing specialization with matching
928 constraints, then this also isn't new. */
929 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
930 while (specs)
931 {
932 tree spec_tmpl = TREE_VALUE (specs);
933 tree spec_args = TREE_PURPOSE (specs);
934 tree spec_constr = get_constraints (spec_tmpl);
935 if (comp_template_args (args, spec_args)
936 && equivalent_constraints (type_constr, spec_constr))
937 return NULL_TREE;
938 specs = TREE_CHAIN (specs);
939 }
940
941 /* Create a new type node (and corresponding type decl)
942 for the newly declared specialization. */
943 tree t = make_class_type (TREE_CODE (type));
944 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
945 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
946
947 /* We only need a separate type node for storing the definition of this
948 partial specialization; uses of S<T*> are unconstrained, so all are
949 equivalent. So keep TYPE_CANONICAL the same. */
950 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
951
952 /* Build the corresponding type decl. */
953 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
954 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
955 DECL_SOURCE_LOCATION (d) = input_location;
956 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
957 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
958
959 return t;
960 }
961
962 return NULL_TREE;
963 }
964
965 /* The TYPE is being declared. If it is a template type, that means it
966 is a partial specialization. Do appropriate error-checking. */
967
968 tree
969 maybe_process_partial_specialization (tree type)
970 {
971 tree context;
972
973 if (type == error_mark_node)
974 return error_mark_node;
975
976 /* A lambda that appears in specialization context is not itself a
977 specialization. */
978 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
979 return type;
980
981 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
982 {
983 error ("name of class shadows template template parameter %qD",
984 TYPE_NAME (type));
985 return error_mark_node;
986 }
987
988 context = TYPE_CONTEXT (type);
989
990 if (TYPE_ALIAS_P (type))
991 {
992 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
993
994 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
995 error ("specialization of alias template %qD",
996 TI_TEMPLATE (tinfo));
997 else
998 error ("explicit specialization of non-template %qT", type);
999 return error_mark_node;
1000 }
1001 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1002 {
1003 /* This is for ordinary explicit specialization and partial
1004 specialization of a template class such as:
1005
1006 template <> class C<int>;
1007
1008 or:
1009
1010 template <class T> class C<T*>;
1011
1012 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1013
1014 if (tree t = maybe_new_partial_specialization (type))
1015 {
1016 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1017 && !at_namespace_scope_p ())
1018 return error_mark_node;
1019 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1020 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1021 if (processing_template_decl)
1022 {
1023 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1024 if (decl == error_mark_node)
1025 return error_mark_node;
1026 return TREE_TYPE (decl);
1027 }
1028 }
1029 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1030 error ("specialization of %qT after instantiation", type);
1031 else if (errorcount && !processing_specialization
1032 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1033 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1034 /* Trying to define a specialization either without a template<> header
1035 or in an inappropriate place. We've already given an error, so just
1036 bail now so we don't actually define the specialization. */
1037 return error_mark_node;
1038 }
1039 else if (CLASS_TYPE_P (type)
1040 && !CLASSTYPE_USE_TEMPLATE (type)
1041 && CLASSTYPE_TEMPLATE_INFO (type)
1042 && context && CLASS_TYPE_P (context)
1043 && CLASSTYPE_TEMPLATE_INFO (context))
1044 {
1045 /* This is for an explicit specialization of member class
1046 template according to [temp.expl.spec/18]:
1047
1048 template <> template <class U> class C<int>::D;
1049
1050 The context `C<int>' must be an implicit instantiation.
1051 Otherwise this is just a member class template declared
1052 earlier like:
1053
1054 template <> class C<int> { template <class U> class D; };
1055 template <> template <class U> class C<int>::D;
1056
1057 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1058 while in the second case, `C<int>::D' is a primary template
1059 and `C<T>::D' may not exist. */
1060
1061 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1062 && !COMPLETE_TYPE_P (type))
1063 {
1064 tree t;
1065 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1066
1067 if (current_namespace
1068 != decl_namespace_context (tmpl))
1069 {
1070 if (permerror (input_location,
1071 "specialization of %qD in different namespace",
1072 type))
1073 inform (DECL_SOURCE_LOCATION (tmpl),
1074 "from definition of %q#D", tmpl);
1075 }
1076
1077 /* Check for invalid specialization after instantiation:
1078
1079 template <> template <> class C<int>::D<int>;
1080 template <> template <class U> class C<int>::D; */
1081
1082 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1083 t; t = TREE_CHAIN (t))
1084 {
1085 tree inst = TREE_VALUE (t);
1086 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1087 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1088 {
1089 /* We already have a full specialization of this partial
1090 instantiation, or a full specialization has been
1091 looked up but not instantiated. Reassign it to the
1092 new member specialization template. */
1093 spec_entry elt;
1094 spec_entry *entry;
1095
1096 elt.tmpl = most_general_template (tmpl);
1097 elt.args = CLASSTYPE_TI_ARGS (inst);
1098 elt.spec = inst;
1099
1100 type_specializations->remove_elt (&elt);
1101
1102 elt.tmpl = tmpl;
1103 CLASSTYPE_TI_ARGS (inst)
1104 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1105
1106 spec_entry **slot
1107 = type_specializations->find_slot (&elt, INSERT);
1108 entry = ggc_alloc<spec_entry> ();
1109 *entry = elt;
1110 *slot = entry;
1111 }
1112 else
1113 /* But if we've had an implicit instantiation, that's a
1114 problem ([temp.expl.spec]/6). */
1115 error ("specialization %qT after instantiation %qT",
1116 type, inst);
1117 }
1118
1119 /* Mark TYPE as a specialization. And as a result, we only
1120 have one level of template argument for the innermost
1121 class template. */
1122 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1123 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1124 CLASSTYPE_TI_ARGS (type)
1125 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1126 }
1127 }
1128 else if (processing_specialization)
1129 {
1130 /* Someday C++0x may allow for enum template specialization. */
1131 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1132 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1133 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1134 "of %qD not allowed by ISO C++", type);
1135 else
1136 {
1137 error ("explicit specialization of non-template %qT", type);
1138 return error_mark_node;
1139 }
1140 }
1141
1142 return type;
1143 }
1144
1145 /* Returns nonzero if we can optimize the retrieval of specializations
1146 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1147 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1148
1149 static inline bool
1150 optimize_specialization_lookup_p (tree tmpl)
1151 {
1152 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1153 && DECL_CLASS_SCOPE_P (tmpl)
1154 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1155 parameter. */
1156 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1157 /* The optimized lookup depends on the fact that the
1158 template arguments for the member function template apply
1159 purely to the containing class, which is not true if the
1160 containing class is an explicit or partial
1161 specialization. */
1162 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1163 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1164 && !DECL_CONV_FN_P (tmpl)
1165 /* It is possible to have a template that is not a member
1166 template and is not a member of a template class:
1167
1168 template <typename T>
1169 struct S { friend A::f(); };
1170
1171 Here, the friend function is a template, but the context does
1172 not have template information. The optimized lookup relies
1173 on having ARGS be the template arguments for both the class
1174 and the function template. */
1175 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1176 }
1177
1178 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1179 gone through coerce_template_parms by now. */
1180
1181 static void
1182 verify_unstripped_args_1 (tree inner)
1183 {
1184 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1185 {
1186 tree arg = TREE_VEC_ELT (inner, i);
1187 if (TREE_CODE (arg) == TEMPLATE_DECL)
1188 /* OK */;
1189 else if (TYPE_P (arg))
1190 gcc_assert (strip_typedefs (arg, NULL) == arg);
1191 else if (ARGUMENT_PACK_P (arg))
1192 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1193 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1194 /* Allow typedefs on the type of a non-type argument, since a
1195 parameter can have them. */;
1196 else
1197 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1198 }
1199 }
1200
1201 static void
1202 verify_unstripped_args (tree args)
1203 {
1204 ++processing_template_decl;
1205 if (!any_dependent_template_arguments_p (args))
1206 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1207 --processing_template_decl;
1208 }
1209
1210 /* Retrieve the specialization (in the sense of [temp.spec] - a
1211 specialization is either an instantiation or an explicit
1212 specialization) of TMPL for the given template ARGS. If there is
1213 no such specialization, return NULL_TREE. The ARGS are a vector of
1214 arguments, or a vector of vectors of arguments, in the case of
1215 templates with more than one level of parameters.
1216
1217 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1218 then we search for a partial specialization matching ARGS. This
1219 parameter is ignored if TMPL is not a class template.
1220
1221 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1222 result is a NONTYPE_ARGUMENT_PACK. */
1223
1224 static tree
1225 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1226 {
1227 if (tmpl == NULL_TREE)
1228 return NULL_TREE;
1229
1230 if (args == error_mark_node)
1231 return NULL_TREE;
1232
1233 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1234 || TREE_CODE (tmpl) == FIELD_DECL);
1235
1236 /* There should be as many levels of arguments as there are
1237 levels of parameters. */
1238 gcc_assert (TMPL_ARGS_DEPTH (args)
1239 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1240 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1241 : template_class_depth (DECL_CONTEXT (tmpl))));
1242
1243 if (flag_checking)
1244 verify_unstripped_args (args);
1245
1246 /* Lambda functions in templates aren't instantiated normally, but through
1247 tsubst_lambda_expr. */
1248 if (lambda_fn_in_template_p (tmpl))
1249 return NULL_TREE;
1250
1251 if (optimize_specialization_lookup_p (tmpl))
1252 {
1253 /* The template arguments actually apply to the containing
1254 class. Find the class specialization with those
1255 arguments. */
1256 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1257 tree class_specialization
1258 = retrieve_specialization (class_template, args, 0);
1259 if (!class_specialization)
1260 return NULL_TREE;
1261
1262 /* Find the instance of TMPL. */
1263 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1264 for (ovl_iterator iter (fns); iter; ++iter)
1265 {
1266 tree fn = *iter;
1267 if (tree ti = get_template_info (fn))
1268 if (TI_TEMPLATE (ti) == tmpl
1269 /* using-declarations can bring in a different
1270 instantiation of tmpl as a member of a different
1271 instantiation of tmpl's class. We don't want those
1272 here. */
1273 && DECL_CONTEXT (fn) == class_specialization)
1274 return fn;
1275 }
1276 return NULL_TREE;
1277 }
1278 else
1279 {
1280 spec_entry *found;
1281 spec_entry elt;
1282 spec_hash_table *specializations;
1283
1284 elt.tmpl = tmpl;
1285 elt.args = args;
1286 elt.spec = NULL_TREE;
1287
1288 if (DECL_CLASS_TEMPLATE_P (tmpl))
1289 specializations = type_specializations;
1290 else
1291 specializations = decl_specializations;
1292
1293 if (hash == 0)
1294 hash = spec_hasher::hash (&elt);
1295 found = specializations->find_with_hash (&elt, hash);
1296 if (found)
1297 return found->spec;
1298 }
1299
1300 return NULL_TREE;
1301 }
1302
1303 /* Like retrieve_specialization, but for local declarations. */
1304
1305 tree
1306 retrieve_local_specialization (tree tmpl)
1307 {
1308 if (local_specializations == NULL)
1309 return NULL_TREE;
1310
1311 tree *slot = local_specializations->get (tmpl);
1312 return slot ? *slot : NULL_TREE;
1313 }
1314
1315 /* Returns nonzero iff DECL is a specialization of TMPL. */
1316
1317 int
1318 is_specialization_of (tree decl, tree tmpl)
1319 {
1320 tree t;
1321
1322 if (TREE_CODE (decl) == FUNCTION_DECL)
1323 {
1324 for (t = decl;
1325 t != NULL_TREE;
1326 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1327 if (t == tmpl)
1328 return 1;
1329 }
1330 else
1331 {
1332 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1333
1334 for (t = TREE_TYPE (decl);
1335 t != NULL_TREE;
1336 t = CLASSTYPE_USE_TEMPLATE (t)
1337 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1338 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1339 return 1;
1340 }
1341
1342 return 0;
1343 }
1344
1345 /* Returns nonzero iff DECL is a specialization of friend declaration
1346 FRIEND_DECL according to [temp.friend]. */
1347
1348 bool
1349 is_specialization_of_friend (tree decl, tree friend_decl)
1350 {
1351 bool need_template = true;
1352 int template_depth;
1353
1354 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1355 || TREE_CODE (decl) == TYPE_DECL);
1356
1357 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1358 of a template class, we want to check if DECL is a specialization
1359 if this. */
1360 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1361 && DECL_TEMPLATE_INFO (friend_decl)
1362 && !DECL_USE_TEMPLATE (friend_decl))
1363 {
1364 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1365 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1366 need_template = false;
1367 }
1368 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1369 && !PRIMARY_TEMPLATE_P (friend_decl))
1370 need_template = false;
1371
1372 /* There is nothing to do if this is not a template friend. */
1373 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1374 return false;
1375
1376 if (is_specialization_of (decl, friend_decl))
1377 return true;
1378
1379 /* [temp.friend/6]
1380 A member of a class template may be declared to be a friend of a
1381 non-template class. In this case, the corresponding member of
1382 every specialization of the class template is a friend of the
1383 class granting friendship.
1384
1385 For example, given a template friend declaration
1386
1387 template <class T> friend void A<T>::f();
1388
1389 the member function below is considered a friend
1390
1391 template <> struct A<int> {
1392 void f();
1393 };
1394
1395 For this type of template friend, TEMPLATE_DEPTH below will be
1396 nonzero. To determine if DECL is a friend of FRIEND, we first
1397 check if the enclosing class is a specialization of another. */
1398
1399 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1400 if (template_depth
1401 && DECL_CLASS_SCOPE_P (decl)
1402 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1403 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1404 {
1405 /* Next, we check the members themselves. In order to handle
1406 a few tricky cases, such as when FRIEND_DECL's are
1407
1408 template <class T> friend void A<T>::g(T t);
1409 template <class T> template <T t> friend void A<T>::h();
1410
1411 and DECL's are
1412
1413 void A<int>::g(int);
1414 template <int> void A<int>::h();
1415
1416 we need to figure out ARGS, the template arguments from
1417 the context of DECL. This is required for template substitution
1418 of `T' in the function parameter of `g' and template parameter
1419 of `h' in the above examples. Here ARGS corresponds to `int'. */
1420
1421 tree context = DECL_CONTEXT (decl);
1422 tree args = NULL_TREE;
1423 int current_depth = 0;
1424
1425 while (current_depth < template_depth)
1426 {
1427 if (CLASSTYPE_TEMPLATE_INFO (context))
1428 {
1429 if (current_depth == 0)
1430 args = TYPE_TI_ARGS (context);
1431 else
1432 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1433 current_depth++;
1434 }
1435 context = TYPE_CONTEXT (context);
1436 }
1437
1438 if (TREE_CODE (decl) == FUNCTION_DECL)
1439 {
1440 bool is_template;
1441 tree friend_type;
1442 tree decl_type;
1443 tree friend_args_type;
1444 tree decl_args_type;
1445
1446 /* Make sure that both DECL and FRIEND_DECL are templates or
1447 non-templates. */
1448 is_template = DECL_TEMPLATE_INFO (decl)
1449 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1450 if (need_template ^ is_template)
1451 return false;
1452 else if (is_template)
1453 {
1454 /* If both are templates, check template parameter list. */
1455 tree friend_parms
1456 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1457 args, tf_none);
1458 if (!comp_template_parms
1459 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1460 friend_parms))
1461 return false;
1462
1463 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1464 }
1465 else
1466 decl_type = TREE_TYPE (decl);
1467
1468 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1469 tf_none, NULL_TREE);
1470 if (friend_type == error_mark_node)
1471 return false;
1472
1473 /* Check if return types match. */
1474 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1475 return false;
1476
1477 /* Check if function parameter types match, ignoring the
1478 `this' parameter. */
1479 friend_args_type = TYPE_ARG_TYPES (friend_type);
1480 decl_args_type = TYPE_ARG_TYPES (decl_type);
1481 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1482 friend_args_type = TREE_CHAIN (friend_args_type);
1483 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1484 decl_args_type = TREE_CHAIN (decl_args_type);
1485
1486 return compparms (decl_args_type, friend_args_type);
1487 }
1488 else
1489 {
1490 /* DECL is a TYPE_DECL */
1491 bool is_template;
1492 tree decl_type = TREE_TYPE (decl);
1493
1494 /* Make sure that both DECL and FRIEND_DECL are templates or
1495 non-templates. */
1496 is_template
1497 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1498 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1499
1500 if (need_template ^ is_template)
1501 return false;
1502 else if (is_template)
1503 {
1504 tree friend_parms;
1505 /* If both are templates, check the name of the two
1506 TEMPLATE_DECL's first because is_friend didn't. */
1507 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1508 != DECL_NAME (friend_decl))
1509 return false;
1510
1511 /* Now check template parameter list. */
1512 friend_parms
1513 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1514 args, tf_none);
1515 return comp_template_parms
1516 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1517 friend_parms);
1518 }
1519 else
1520 return (DECL_NAME (decl)
1521 == DECL_NAME (friend_decl));
1522 }
1523 }
1524 return false;
1525 }
1526
1527 /* Register the specialization SPEC as a specialization of TMPL with
1528 the indicated ARGS. IS_FRIEND indicates whether the specialization
1529 is actually just a friend declaration. ATTRLIST is the list of
1530 attributes that the specialization is declared with or NULL when
1531 it isn't. Returns SPEC, or an equivalent prior declaration, if
1532 available.
1533
1534 We also store instantiations of field packs in the hash table, even
1535 though they are not themselves templates, to make lookup easier. */
1536
1537 static tree
1538 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1539 hashval_t hash)
1540 {
1541 tree fn;
1542 spec_entry **slot = NULL;
1543 spec_entry elt;
1544
1545 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1546 || (TREE_CODE (tmpl) == FIELD_DECL
1547 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1548
1549 if (TREE_CODE (spec) == FUNCTION_DECL
1550 && uses_template_parms (DECL_TI_ARGS (spec)))
1551 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1552 register it; we want the corresponding TEMPLATE_DECL instead.
1553 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1554 the more obvious `uses_template_parms (spec)' to avoid problems
1555 with default function arguments. In particular, given
1556 something like this:
1557
1558 template <class T> void f(T t1, T t = T())
1559
1560 the default argument expression is not substituted for in an
1561 instantiation unless and until it is actually needed. */
1562 return spec;
1563
1564 if (optimize_specialization_lookup_p (tmpl))
1565 /* We don't put these specializations in the hash table, but we might
1566 want to give an error about a mismatch. */
1567 fn = retrieve_specialization (tmpl, args, 0);
1568 else
1569 {
1570 elt.tmpl = tmpl;
1571 elt.args = args;
1572 elt.spec = spec;
1573
1574 if (hash == 0)
1575 hash = spec_hasher::hash (&elt);
1576
1577 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1578 if (*slot)
1579 fn = (*slot)->spec;
1580 else
1581 fn = NULL_TREE;
1582 }
1583
1584 /* We can sometimes try to re-register a specialization that we've
1585 already got. In particular, regenerate_decl_from_template calls
1586 duplicate_decls which will update the specialization list. But,
1587 we'll still get called again here anyhow. It's more convenient
1588 to simply allow this than to try to prevent it. */
1589 if (fn == spec)
1590 return spec;
1591 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1592 {
1593 if (DECL_TEMPLATE_INSTANTIATION (fn))
1594 {
1595 if (DECL_ODR_USED (fn)
1596 || DECL_EXPLICIT_INSTANTIATION (fn))
1597 {
1598 error ("specialization of %qD after instantiation",
1599 fn);
1600 return error_mark_node;
1601 }
1602 else
1603 {
1604 tree clone;
1605 /* This situation should occur only if the first
1606 specialization is an implicit instantiation, the
1607 second is an explicit specialization, and the
1608 implicit instantiation has not yet been used. That
1609 situation can occur if we have implicitly
1610 instantiated a member function and then specialized
1611 it later.
1612
1613 We can also wind up here if a friend declaration that
1614 looked like an instantiation turns out to be a
1615 specialization:
1616
1617 template <class T> void foo(T);
1618 class S { friend void foo<>(int) };
1619 template <> void foo(int);
1620
1621 We transform the existing DECL in place so that any
1622 pointers to it become pointers to the updated
1623 declaration.
1624
1625 If there was a definition for the template, but not
1626 for the specialization, we want this to look as if
1627 there were no definition, and vice versa. */
1628 DECL_INITIAL (fn) = NULL_TREE;
1629 duplicate_decls (spec, fn, is_friend);
1630 /* The call to duplicate_decls will have applied
1631 [temp.expl.spec]:
1632
1633 An explicit specialization of a function template
1634 is inline only if it is explicitly declared to be,
1635 and independently of whether its function template
1636 is.
1637
1638 to the primary function; now copy the inline bits to
1639 the various clones. */
1640 FOR_EACH_CLONE (clone, fn)
1641 {
1642 DECL_DECLARED_INLINE_P (clone)
1643 = DECL_DECLARED_INLINE_P (fn);
1644 DECL_SOURCE_LOCATION (clone)
1645 = DECL_SOURCE_LOCATION (fn);
1646 DECL_DELETED_FN (clone)
1647 = DECL_DELETED_FN (fn);
1648 }
1649 check_specialization_namespace (tmpl);
1650
1651 return fn;
1652 }
1653 }
1654 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1655 {
1656 tree dd = duplicate_decls (spec, fn, is_friend);
1657 if (dd == error_mark_node)
1658 /* We've already complained in duplicate_decls. */
1659 return error_mark_node;
1660
1661 if (dd == NULL_TREE && DECL_INITIAL (spec))
1662 /* Dup decl failed, but this is a new definition. Set the
1663 line number so any errors match this new
1664 definition. */
1665 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1666
1667 return fn;
1668 }
1669 }
1670 else if (fn)
1671 return duplicate_decls (spec, fn, is_friend);
1672
1673 /* A specialization must be declared in the same namespace as the
1674 template it is specializing. */
1675 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1676 && !check_specialization_namespace (tmpl))
1677 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1678
1679 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1680 {
1681 spec_entry *entry = ggc_alloc<spec_entry> ();
1682 gcc_assert (tmpl && args && spec);
1683 *entry = elt;
1684 *slot = entry;
1685 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1686 && PRIMARY_TEMPLATE_P (tmpl)
1687 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1688 || variable_template_p (tmpl))
1689 /* If TMPL is a forward declaration of a template function, keep a list
1690 of all specializations in case we need to reassign them to a friend
1691 template later in tsubst_friend_function.
1692
1693 Also keep a list of all variable template instantiations so that
1694 process_partial_specialization can check whether a later partial
1695 specialization would have used it. */
1696 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1697 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1698 }
1699
1700 return spec;
1701 }
1702
1703 /* Returns true iff two spec_entry nodes are equivalent. */
1704
1705 int comparing_specializations;
1706
1707 bool
1708 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1709 {
1710 int equal;
1711
1712 ++comparing_specializations;
1713 equal = (e1->tmpl == e2->tmpl
1714 && comp_template_args (e1->args, e2->args));
1715 if (equal && flag_concepts
1716 /* tmpl could be a FIELD_DECL for a capture pack. */
1717 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1718 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1719 && uses_template_parms (e1->args))
1720 {
1721 /* Partial specializations of a variable template can be distinguished by
1722 constraints. */
1723 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1724 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1725 equal = equivalent_constraints (c1, c2);
1726 }
1727 --comparing_specializations;
1728
1729 return equal;
1730 }
1731
1732 /* Returns a hash for a template TMPL and template arguments ARGS. */
1733
1734 static hashval_t
1735 hash_tmpl_and_args (tree tmpl, tree args)
1736 {
1737 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1738 return iterative_hash_template_arg (args, val);
1739 }
1740
1741 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1742 ignoring SPEC. */
1743
1744 hashval_t
1745 spec_hasher::hash (spec_entry *e)
1746 {
1747 return hash_tmpl_and_args (e->tmpl, e->args);
1748 }
1749
1750 /* Recursively calculate a hash value for a template argument ARG, for use
1751 in the hash tables of template specializations. We must be
1752 careful to (at least) skip the same entities template_args_equal
1753 does. */
1754
1755 hashval_t
1756 iterative_hash_template_arg (tree arg, hashval_t val)
1757 {
1758 if (arg == NULL_TREE)
1759 return iterative_hash_object (arg, val);
1760
1761 if (!TYPE_P (arg))
1762 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1763 while (CONVERT_EXPR_P (arg)
1764 || TREE_CODE (arg) == NON_LVALUE_EXPR
1765 || class_nttp_const_wrapper_p (arg))
1766 arg = TREE_OPERAND (arg, 0);
1767
1768 enum tree_code code = TREE_CODE (arg);
1769
1770 val = iterative_hash_object (code, val);
1771
1772 switch (code)
1773 {
1774 case ARGUMENT_PACK_SELECT:
1775 gcc_unreachable ();
1776
1777 case ERROR_MARK:
1778 return val;
1779
1780 case IDENTIFIER_NODE:
1781 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1782
1783 case TREE_VEC:
1784 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1785 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1786 return val;
1787
1788 case TYPE_PACK_EXPANSION:
1789 case EXPR_PACK_EXPANSION:
1790 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1791 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1792
1793 case TYPE_ARGUMENT_PACK:
1794 case NONTYPE_ARGUMENT_PACK:
1795 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1796
1797 case TREE_LIST:
1798 for (; arg; arg = TREE_CHAIN (arg))
1799 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1800 return val;
1801
1802 case OVERLOAD:
1803 for (lkp_iterator iter (arg); iter; ++iter)
1804 val = iterative_hash_template_arg (*iter, val);
1805 return val;
1806
1807 case CONSTRUCTOR:
1808 {
1809 tree field, value;
1810 unsigned i;
1811 iterative_hash_template_arg (TREE_TYPE (arg), val);
1812 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1813 {
1814 val = iterative_hash_template_arg (field, val);
1815 val = iterative_hash_template_arg (value, val);
1816 }
1817 return val;
1818 }
1819
1820 case PARM_DECL:
1821 if (!DECL_ARTIFICIAL (arg))
1822 {
1823 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1824 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1825 }
1826 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1827
1828 case TARGET_EXPR:
1829 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1830
1831 case PTRMEM_CST:
1832 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1833 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1834
1835 case TEMPLATE_PARM_INDEX:
1836 val = iterative_hash_template_arg
1837 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1838 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1839 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1840
1841 case TRAIT_EXPR:
1842 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1843 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1844 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1845
1846 case BASELINK:
1847 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1848 val);
1849 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1850 val);
1851
1852 case MODOP_EXPR:
1853 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1854 code = TREE_CODE (TREE_OPERAND (arg, 1));
1855 val = iterative_hash_object (code, val);
1856 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1857
1858 case LAMBDA_EXPR:
1859 /* [temp.over.link] Two lambda-expressions are never considered
1860 equivalent.
1861
1862 So just hash the closure type. */
1863 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1864
1865 case CAST_EXPR:
1866 case IMPLICIT_CONV_EXPR:
1867 case STATIC_CAST_EXPR:
1868 case REINTERPRET_CAST_EXPR:
1869 case CONST_CAST_EXPR:
1870 case DYNAMIC_CAST_EXPR:
1871 case NEW_EXPR:
1872 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1873 /* Now hash operands as usual. */
1874 break;
1875
1876 case CALL_EXPR:
1877 {
1878 tree fn = CALL_EXPR_FN (arg);
1879 if (tree name = dependent_name (fn))
1880 {
1881 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1882 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1883 fn = name;
1884 }
1885 val = iterative_hash_template_arg (fn, val);
1886 call_expr_arg_iterator ai;
1887 for (tree x = first_call_expr_arg (arg, &ai); x;
1888 x = next_call_expr_arg (&ai))
1889 val = iterative_hash_template_arg (x, val);
1890 return val;
1891 }
1892
1893 default:
1894 break;
1895 }
1896
1897 char tclass = TREE_CODE_CLASS (code);
1898 switch (tclass)
1899 {
1900 case tcc_type:
1901 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1902 {
1903 // We want an alias specialization that survived strip_typedefs
1904 // to hash differently from its TYPE_CANONICAL, to avoid hash
1905 // collisions that compare as different in template_args_equal.
1906 // These could be dependent specializations that strip_typedefs
1907 // left alone, or untouched specializations because
1908 // coerce_template_parms returns the unconverted template
1909 // arguments if it sees incomplete argument packs.
1910 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1911 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1912 }
1913
1914 switch (TREE_CODE (arg))
1915 {
1916 case TEMPLATE_TEMPLATE_PARM:
1917 {
1918 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1919
1920 /* Do not recurse with TPI directly, as that is unbounded
1921 recursion. */
1922 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1923 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1924 }
1925 break;
1926
1927 case DECLTYPE_TYPE:
1928 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1929 break;
1930
1931 default:
1932 if (tree canonical = TYPE_CANONICAL (arg))
1933 val = iterative_hash_object (TYPE_HASH (canonical), val);
1934 break;
1935 }
1936
1937 return val;
1938
1939 case tcc_declaration:
1940 case tcc_constant:
1941 return iterative_hash_expr (arg, val);
1942
1943 default:
1944 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1945 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1946 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1947 return val;
1948 }
1949
1950 gcc_unreachable ();
1951 return 0;
1952 }
1953
1954 /* Unregister the specialization SPEC as a specialization of TMPL.
1955 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1956 if the SPEC was listed as a specialization of TMPL.
1957
1958 Note that SPEC has been ggc_freed, so we can't look inside it. */
1959
1960 bool
1961 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1962 {
1963 spec_entry *entry;
1964 spec_entry elt;
1965
1966 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1967 elt.args = TI_ARGS (tinfo);
1968 elt.spec = NULL_TREE;
1969
1970 entry = decl_specializations->find (&elt);
1971 if (entry != NULL)
1972 {
1973 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1974 gcc_assert (new_spec != NULL_TREE);
1975 entry->spec = new_spec;
1976 return 1;
1977 }
1978
1979 return 0;
1980 }
1981
1982 /* Like register_specialization, but for local declarations. We are
1983 registering SPEC, an instantiation of TMPL. */
1984
1985 void
1986 register_local_specialization (tree spec, tree tmpl)
1987 {
1988 gcc_assert (tmpl != spec);
1989 local_specializations->put (tmpl, spec);
1990 }
1991
1992 /* TYPE is a class type. Returns true if TYPE is an explicitly
1993 specialized class. */
1994
1995 bool
1996 explicit_class_specialization_p (tree type)
1997 {
1998 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1999 return false;
2000 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2001 }
2002
2003 /* Print the list of functions at FNS, going through all the overloads
2004 for each element of the list. Alternatively, FNS cannot be a
2005 TREE_LIST, in which case it will be printed together with all the
2006 overloads.
2007
2008 MORE and *STR should respectively be FALSE and NULL when the function
2009 is called from the outside. They are used internally on recursive
2010 calls. print_candidates manages the two parameters and leaves NULL
2011 in *STR when it ends. */
2012
2013 static void
2014 print_candidates_1 (tree fns, char **str, bool more = false)
2015 {
2016 if (TREE_CODE (fns) == TREE_LIST)
2017 for (; fns; fns = TREE_CHAIN (fns))
2018 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2019 else
2020 for (lkp_iterator iter (fns); iter;)
2021 {
2022 tree cand = *iter;
2023 ++iter;
2024
2025 const char *pfx = *str;
2026 if (!pfx)
2027 {
2028 if (more || iter)
2029 pfx = _("candidates are:");
2030 else
2031 pfx = _("candidate is:");
2032 *str = get_spaces (pfx);
2033 }
2034 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2035 }
2036 }
2037
2038 /* Print the list of candidate FNS in an error message. FNS can also
2039 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2040
2041 void
2042 print_candidates (tree fns)
2043 {
2044 char *str = NULL;
2045 print_candidates_1 (fns, &str);
2046 free (str);
2047 }
2048
2049 /* Get a (possibly) constrained template declaration for the
2050 purpose of ordering candidates. */
2051 static tree
2052 get_template_for_ordering (tree list)
2053 {
2054 gcc_assert (TREE_CODE (list) == TREE_LIST);
2055 tree f = TREE_VALUE (list);
2056 if (tree ti = DECL_TEMPLATE_INFO (f))
2057 return TI_TEMPLATE (ti);
2058 return f;
2059 }
2060
2061 /* Among candidates having the same signature, return the
2062 most constrained or NULL_TREE if there is no best candidate.
2063 If the signatures of candidates vary (e.g., template
2064 specialization vs. member function), then there can be no
2065 most constrained.
2066
2067 Note that we don't compare constraints on the functions
2068 themselves, but rather those of their templates. */
2069 static tree
2070 most_constrained_function (tree candidates)
2071 {
2072 // Try to find the best candidate in a first pass.
2073 tree champ = candidates;
2074 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2075 {
2076 int winner = more_constrained (get_template_for_ordering (champ),
2077 get_template_for_ordering (c));
2078 if (winner == -1)
2079 champ = c; // The candidate is more constrained
2080 else if (winner == 0)
2081 return NULL_TREE; // Neither is more constrained
2082 }
2083
2084 // Verify that the champ is better than previous candidates.
2085 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2086 if (!more_constrained (get_template_for_ordering (champ),
2087 get_template_for_ordering (c)))
2088 return NULL_TREE;
2089 }
2090
2091 return champ;
2092 }
2093
2094
2095 /* Returns the template (one of the functions given by TEMPLATE_ID)
2096 which can be specialized to match the indicated DECL with the
2097 explicit template args given in TEMPLATE_ID. The DECL may be
2098 NULL_TREE if none is available. In that case, the functions in
2099 TEMPLATE_ID are non-members.
2100
2101 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2102 specialization of a member template.
2103
2104 The TEMPLATE_COUNT is the number of references to qualifying
2105 template classes that appeared in the name of the function. See
2106 check_explicit_specialization for a more accurate description.
2107
2108 TSK indicates what kind of template declaration (if any) is being
2109 declared. TSK_TEMPLATE indicates that the declaration given by
2110 DECL, though a FUNCTION_DECL, has template parameters, and is
2111 therefore a template function.
2112
2113 The template args (those explicitly specified and those deduced)
2114 are output in a newly created vector *TARGS_OUT.
2115
2116 If it is impossible to determine the result, an error message is
2117 issued. The error_mark_node is returned to indicate failure. */
2118
2119 static tree
2120 determine_specialization (tree template_id,
2121 tree decl,
2122 tree* targs_out,
2123 int need_member_template,
2124 int template_count,
2125 tmpl_spec_kind tsk)
2126 {
2127 tree fns;
2128 tree targs;
2129 tree explicit_targs;
2130 tree candidates = NULL_TREE;
2131
2132 /* A TREE_LIST of templates of which DECL may be a specialization.
2133 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2134 corresponding TREE_PURPOSE is the set of template arguments that,
2135 when used to instantiate the template, would produce a function
2136 with the signature of DECL. */
2137 tree templates = NULL_TREE;
2138 int header_count;
2139 cp_binding_level *b;
2140
2141 *targs_out = NULL_TREE;
2142
2143 if (template_id == error_mark_node || decl == error_mark_node)
2144 return error_mark_node;
2145
2146 /* We shouldn't be specializing a member template of an
2147 unspecialized class template; we already gave an error in
2148 check_specialization_scope, now avoid crashing. */
2149 if (!VAR_P (decl)
2150 && template_count && DECL_CLASS_SCOPE_P (decl)
2151 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2152 {
2153 gcc_assert (errorcount);
2154 return error_mark_node;
2155 }
2156
2157 fns = TREE_OPERAND (template_id, 0);
2158 explicit_targs = TREE_OPERAND (template_id, 1);
2159
2160 if (fns == error_mark_node)
2161 return error_mark_node;
2162
2163 /* Check for baselinks. */
2164 if (BASELINK_P (fns))
2165 fns = BASELINK_FUNCTIONS (fns);
2166
2167 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2168 {
2169 error_at (DECL_SOURCE_LOCATION (decl),
2170 "%qD is not a function template", fns);
2171 return error_mark_node;
2172 }
2173 else if (VAR_P (decl) && !variable_template_p (fns))
2174 {
2175 error ("%qD is not a variable template", fns);
2176 return error_mark_node;
2177 }
2178
2179 /* Count the number of template headers specified for this
2180 specialization. */
2181 header_count = 0;
2182 for (b = current_binding_level;
2183 b->kind == sk_template_parms;
2184 b = b->level_chain)
2185 ++header_count;
2186
2187 tree orig_fns = fns;
2188
2189 if (variable_template_p (fns))
2190 {
2191 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2192 targs = coerce_template_parms (parms, explicit_targs, fns,
2193 tf_warning_or_error,
2194 /*req_all*/true, /*use_defarg*/true);
2195 if (targs != error_mark_node)
2196 templates = tree_cons (targs, fns, templates);
2197 }
2198 else for (lkp_iterator iter (fns); iter; ++iter)
2199 {
2200 tree fn = *iter;
2201
2202 if (TREE_CODE (fn) == TEMPLATE_DECL)
2203 {
2204 tree decl_arg_types;
2205 tree fn_arg_types;
2206 tree insttype;
2207
2208 /* In case of explicit specialization, we need to check if
2209 the number of template headers appearing in the specialization
2210 is correct. This is usually done in check_explicit_specialization,
2211 but the check done there cannot be exhaustive when specializing
2212 member functions. Consider the following code:
2213
2214 template <> void A<int>::f(int);
2215 template <> template <> void A<int>::f(int);
2216
2217 Assuming that A<int> is not itself an explicit specialization
2218 already, the first line specializes "f" which is a non-template
2219 member function, whilst the second line specializes "f" which
2220 is a template member function. So both lines are syntactically
2221 correct, and check_explicit_specialization does not reject
2222 them.
2223
2224 Here, we can do better, as we are matching the specialization
2225 against the declarations. We count the number of template
2226 headers, and we check if they match TEMPLATE_COUNT + 1
2227 (TEMPLATE_COUNT is the number of qualifying template classes,
2228 plus there must be another header for the member template
2229 itself).
2230
2231 Notice that if header_count is zero, this is not a
2232 specialization but rather a template instantiation, so there
2233 is no check we can perform here. */
2234 if (header_count && header_count != template_count + 1)
2235 continue;
2236
2237 /* Check that the number of template arguments at the
2238 innermost level for DECL is the same as for FN. */
2239 if (current_binding_level->kind == sk_template_parms
2240 && !current_binding_level->explicit_spec_p
2241 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2242 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2243 (current_template_parms))))
2244 continue;
2245
2246 /* DECL might be a specialization of FN. */
2247 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2248 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2249
2250 /* For a non-static member function, we need to make sure
2251 that the const qualification is the same. Since
2252 get_bindings does not try to merge the "this" parameter,
2253 we must do the comparison explicitly. */
2254 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2255 {
2256 if (!same_type_p (TREE_VALUE (fn_arg_types),
2257 TREE_VALUE (decl_arg_types)))
2258 continue;
2259
2260 /* And the ref-qualification. */
2261 if (type_memfn_rqual (TREE_TYPE (decl))
2262 != type_memfn_rqual (TREE_TYPE (fn)))
2263 continue;
2264 }
2265
2266 /* Skip the "this" parameter and, for constructors of
2267 classes with virtual bases, the VTT parameter. A
2268 full specialization of a constructor will have a VTT
2269 parameter, but a template never will. */
2270 decl_arg_types
2271 = skip_artificial_parms_for (decl, decl_arg_types);
2272 fn_arg_types
2273 = skip_artificial_parms_for (fn, fn_arg_types);
2274
2275 /* Function templates cannot be specializations; there are
2276 no partial specializations of functions. Therefore, if
2277 the type of DECL does not match FN, there is no
2278 match.
2279
2280 Note that it should never be the case that we have both
2281 candidates added here, and for regular member functions
2282 below. */
2283 if (tsk == tsk_template)
2284 {
2285 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2286 current_template_parms))
2287 continue;
2288 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2289 TREE_TYPE (TREE_TYPE (fn))))
2290 continue;
2291 if (!compparms (fn_arg_types, decl_arg_types))
2292 continue;
2293
2294 tree freq = get_trailing_function_requirements (fn);
2295 tree dreq = get_trailing_function_requirements (decl);
2296 if (!freq != !dreq)
2297 continue;
2298 if (freq)
2299 {
2300 tree fargs = DECL_TI_ARGS (fn);
2301 tsubst_flags_t complain = tf_none;
2302 freq = tsubst_constraint (freq, fargs, complain, fn);
2303 if (!cp_tree_equal (freq, dreq))
2304 continue;
2305 }
2306
2307 candidates = tree_cons (NULL_TREE, fn, candidates);
2308 continue;
2309 }
2310
2311 /* See whether this function might be a specialization of this
2312 template. Suppress access control because we might be trying
2313 to make this specialization a friend, and we have already done
2314 access control for the declaration of the specialization. */
2315 push_deferring_access_checks (dk_no_check);
2316 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2317 pop_deferring_access_checks ();
2318
2319 if (!targs)
2320 /* We cannot deduce template arguments that when used to
2321 specialize TMPL will produce DECL. */
2322 continue;
2323
2324 if (uses_template_parms (targs))
2325 /* We deduced something involving 'auto', which isn't a valid
2326 template argument. */
2327 continue;
2328
2329 /* Remove, from the set of candidates, all those functions
2330 whose constraints are not satisfied. */
2331 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2332 continue;
2333
2334 // Then, try to form the new function type.
2335 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2336 if (insttype == error_mark_node)
2337 continue;
2338 fn_arg_types
2339 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2340 if (!compparms (fn_arg_types, decl_arg_types))
2341 continue;
2342
2343 /* Save this template, and the arguments deduced. */
2344 templates = tree_cons (targs, fn, templates);
2345 }
2346 else if (need_member_template)
2347 /* FN is an ordinary member function, and we need a
2348 specialization of a member template. */
2349 ;
2350 else if (TREE_CODE (fn) != FUNCTION_DECL)
2351 /* We can get IDENTIFIER_NODEs here in certain erroneous
2352 cases. */
2353 ;
2354 else if (!DECL_FUNCTION_MEMBER_P (fn))
2355 /* This is just an ordinary non-member function. Nothing can
2356 be a specialization of that. */
2357 ;
2358 else if (DECL_ARTIFICIAL (fn))
2359 /* Cannot specialize functions that are created implicitly. */
2360 ;
2361 else
2362 {
2363 tree decl_arg_types;
2364
2365 /* This is an ordinary member function. However, since
2366 we're here, we can assume its enclosing class is a
2367 template class. For example,
2368
2369 template <typename T> struct S { void f(); };
2370 template <> void S<int>::f() {}
2371
2372 Here, S<int>::f is a non-template, but S<int> is a
2373 template class. If FN has the same type as DECL, we
2374 might be in business. */
2375
2376 if (!DECL_TEMPLATE_INFO (fn))
2377 /* Its enclosing class is an explicit specialization
2378 of a template class. This is not a candidate. */
2379 continue;
2380
2381 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2382 TREE_TYPE (TREE_TYPE (fn))))
2383 /* The return types differ. */
2384 continue;
2385
2386 /* Adjust the type of DECL in case FN is a static member. */
2387 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2388 if (DECL_STATIC_FUNCTION_P (fn)
2389 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2390 decl_arg_types = TREE_CHAIN (decl_arg_types);
2391
2392 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2393 decl_arg_types))
2394 continue;
2395
2396 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2397 && (type_memfn_rqual (TREE_TYPE (decl))
2398 != type_memfn_rqual (TREE_TYPE (fn))))
2399 continue;
2400
2401 // If the deduced arguments do not satisfy the constraints,
2402 // this is not a candidate.
2403 if (flag_concepts && !constraints_satisfied_p (fn))
2404 continue;
2405
2406 // Add the candidate.
2407 candidates = tree_cons (NULL_TREE, fn, candidates);
2408 }
2409 }
2410
2411 if (templates && TREE_CHAIN (templates))
2412 {
2413 /* We have:
2414
2415 [temp.expl.spec]
2416
2417 It is possible for a specialization with a given function
2418 signature to be instantiated from more than one function
2419 template. In such cases, explicit specification of the
2420 template arguments must be used to uniquely identify the
2421 function template specialization being specialized.
2422
2423 Note that here, there's no suggestion that we're supposed to
2424 determine which of the candidate templates is most
2425 specialized. However, we, also have:
2426
2427 [temp.func.order]
2428
2429 Partial ordering of overloaded function template
2430 declarations is used in the following contexts to select
2431 the function template to which a function template
2432 specialization refers:
2433
2434 -- when an explicit specialization refers to a function
2435 template.
2436
2437 So, we do use the partial ordering rules, at least for now.
2438 This extension can only serve to make invalid programs valid,
2439 so it's safe. And, there is strong anecdotal evidence that
2440 the committee intended the partial ordering rules to apply;
2441 the EDG front end has that behavior, and John Spicer claims
2442 that the committee simply forgot to delete the wording in
2443 [temp.expl.spec]. */
2444 tree tmpl = most_specialized_instantiation (templates);
2445 if (tmpl != error_mark_node)
2446 {
2447 templates = tmpl;
2448 TREE_CHAIN (templates) = NULL_TREE;
2449 }
2450 }
2451
2452 // Concepts allows multiple declarations of member functions
2453 // with the same signature. Like above, we need to rely on
2454 // on the partial ordering of those candidates to determine which
2455 // is the best.
2456 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2457 {
2458 if (tree cand = most_constrained_function (candidates))
2459 {
2460 candidates = cand;
2461 TREE_CHAIN (cand) = NULL_TREE;
2462 }
2463 }
2464
2465 if (templates == NULL_TREE && candidates == NULL_TREE)
2466 {
2467 error ("template-id %qD for %q+D does not match any template "
2468 "declaration", template_id, decl);
2469 if (header_count && header_count != template_count + 1)
2470 inform (DECL_SOURCE_LOCATION (decl),
2471 "saw %d %<template<>%>, need %d for "
2472 "specializing a member function template",
2473 header_count, template_count + 1);
2474 else
2475 print_candidates (orig_fns);
2476 return error_mark_node;
2477 }
2478 else if ((templates && TREE_CHAIN (templates))
2479 || (candidates && TREE_CHAIN (candidates))
2480 || (templates && candidates))
2481 {
2482 error ("ambiguous template specialization %qD for %q+D",
2483 template_id, decl);
2484 candidates = chainon (candidates, templates);
2485 print_candidates (candidates);
2486 return error_mark_node;
2487 }
2488
2489 /* We have one, and exactly one, match. */
2490 if (candidates)
2491 {
2492 tree fn = TREE_VALUE (candidates);
2493 *targs_out = copy_node (DECL_TI_ARGS (fn));
2494
2495 /* Propagate the candidate's constraints to the declaration. */
2496 if (tsk != tsk_template)
2497 set_constraints (decl, get_constraints (fn));
2498
2499 /* DECL is a re-declaration or partial instantiation of a template
2500 function. */
2501 if (TREE_CODE (fn) == TEMPLATE_DECL)
2502 return fn;
2503 /* It was a specialization of an ordinary member function in a
2504 template class. */
2505 return DECL_TI_TEMPLATE (fn);
2506 }
2507
2508 /* It was a specialization of a template. */
2509 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2510 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2511 {
2512 *targs_out = copy_node (targs);
2513 SET_TMPL_ARGS_LEVEL (*targs_out,
2514 TMPL_ARGS_DEPTH (*targs_out),
2515 TREE_PURPOSE (templates));
2516 }
2517 else
2518 *targs_out = TREE_PURPOSE (templates);
2519 return TREE_VALUE (templates);
2520 }
2521
2522 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2523 but with the default argument values filled in from those in the
2524 TMPL_TYPES. */
2525
2526 static tree
2527 copy_default_args_to_explicit_spec_1 (tree spec_types,
2528 tree tmpl_types)
2529 {
2530 tree new_spec_types;
2531
2532 if (!spec_types)
2533 return NULL_TREE;
2534
2535 if (spec_types == void_list_node)
2536 return void_list_node;
2537
2538 /* Substitute into the rest of the list. */
2539 new_spec_types =
2540 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2541 TREE_CHAIN (tmpl_types));
2542
2543 /* Add the default argument for this parameter. */
2544 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2545 TREE_VALUE (spec_types),
2546 new_spec_types);
2547 }
2548
2549 /* DECL is an explicit specialization. Replicate default arguments
2550 from the template it specializes. (That way, code like:
2551
2552 template <class T> void f(T = 3);
2553 template <> void f(double);
2554 void g () { f (); }
2555
2556 works, as required.) An alternative approach would be to look up
2557 the correct default arguments at the call-site, but this approach
2558 is consistent with how implicit instantiations are handled. */
2559
2560 static void
2561 copy_default_args_to_explicit_spec (tree decl)
2562 {
2563 tree tmpl;
2564 tree spec_types;
2565 tree tmpl_types;
2566 tree new_spec_types;
2567 tree old_type;
2568 tree new_type;
2569 tree t;
2570 tree object_type = NULL_TREE;
2571 tree in_charge = NULL_TREE;
2572 tree vtt = NULL_TREE;
2573
2574 /* See if there's anything we need to do. */
2575 tmpl = DECL_TI_TEMPLATE (decl);
2576 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2577 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2578 if (TREE_PURPOSE (t))
2579 break;
2580 if (!t)
2581 return;
2582
2583 old_type = TREE_TYPE (decl);
2584 spec_types = TYPE_ARG_TYPES (old_type);
2585
2586 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2587 {
2588 /* Remove the this pointer, but remember the object's type for
2589 CV quals. */
2590 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2591 spec_types = TREE_CHAIN (spec_types);
2592 tmpl_types = TREE_CHAIN (tmpl_types);
2593
2594 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2595 {
2596 /* DECL may contain more parameters than TMPL due to the extra
2597 in-charge parameter in constructors and destructors. */
2598 in_charge = spec_types;
2599 spec_types = TREE_CHAIN (spec_types);
2600 }
2601 if (DECL_HAS_VTT_PARM_P (decl))
2602 {
2603 vtt = spec_types;
2604 spec_types = TREE_CHAIN (spec_types);
2605 }
2606 }
2607
2608 /* Compute the merged default arguments. */
2609 new_spec_types =
2610 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2611
2612 /* Compute the new FUNCTION_TYPE. */
2613 if (object_type)
2614 {
2615 if (vtt)
2616 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2617 TREE_VALUE (vtt),
2618 new_spec_types);
2619
2620 if (in_charge)
2621 /* Put the in-charge parameter back. */
2622 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2623 TREE_VALUE (in_charge),
2624 new_spec_types);
2625
2626 new_type = build_method_type_directly (object_type,
2627 TREE_TYPE (old_type),
2628 new_spec_types);
2629 }
2630 else
2631 new_type = build_function_type (TREE_TYPE (old_type),
2632 new_spec_types);
2633 new_type = cp_build_type_attribute_variant (new_type,
2634 TYPE_ATTRIBUTES (old_type));
2635 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2636
2637 TREE_TYPE (decl) = new_type;
2638 }
2639
2640 /* Return the number of template headers we expect to see for a definition
2641 or specialization of CTYPE or one of its non-template members. */
2642
2643 int
2644 num_template_headers_for_class (tree ctype)
2645 {
2646 int num_templates = 0;
2647
2648 while (ctype && CLASS_TYPE_P (ctype))
2649 {
2650 /* You're supposed to have one `template <...>' for every
2651 template class, but you don't need one for a full
2652 specialization. For example:
2653
2654 template <class T> struct S{};
2655 template <> struct S<int> { void f(); };
2656 void S<int>::f () {}
2657
2658 is correct; there shouldn't be a `template <>' for the
2659 definition of `S<int>::f'. */
2660 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2661 /* If CTYPE does not have template information of any
2662 kind, then it is not a template, nor is it nested
2663 within a template. */
2664 break;
2665 if (explicit_class_specialization_p (ctype))
2666 break;
2667 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2668 ++num_templates;
2669
2670 ctype = TYPE_CONTEXT (ctype);
2671 }
2672
2673 return num_templates;
2674 }
2675
2676 /* Do a simple sanity check on the template headers that precede the
2677 variable declaration DECL. */
2678
2679 void
2680 check_template_variable (tree decl)
2681 {
2682 tree ctx = CP_DECL_CONTEXT (decl);
2683 int wanted = num_template_headers_for_class (ctx);
2684 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2685 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2686 {
2687 if (cxx_dialect < cxx14)
2688 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2689 "variable templates only available with "
2690 "%<-std=c++14%> or %<-std=gnu++14%>");
2691
2692 // Namespace-scope variable templates should have a template header.
2693 ++wanted;
2694 }
2695 if (template_header_count > wanted)
2696 {
2697 auto_diagnostic_group d;
2698 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2699 "too many template headers for %qD "
2700 "(should be %d)",
2701 decl, wanted);
2702 if (warned && CLASS_TYPE_P (ctx)
2703 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2704 inform (DECL_SOURCE_LOCATION (decl),
2705 "members of an explicitly specialized class are defined "
2706 "without a template header");
2707 }
2708 }
2709
2710 /* An explicit specialization whose declarator-id or class-head-name is not
2711 qualified shall be declared in the nearest enclosing namespace of the
2712 template, or, if the namespace is inline (7.3.1), any namespace from its
2713 enclosing namespace set.
2714
2715 If the name declared in the explicit instantiation is an unqualified name,
2716 the explicit instantiation shall appear in the namespace where its template
2717 is declared or, if that namespace is inline (7.3.1), any namespace from its
2718 enclosing namespace set. */
2719
2720 void
2721 check_unqualified_spec_or_inst (tree t, location_t loc)
2722 {
2723 tree tmpl = most_general_template (t);
2724 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2725 && !is_nested_namespace (current_namespace,
2726 CP_DECL_CONTEXT (tmpl), true))
2727 {
2728 if (processing_specialization)
2729 permerror (loc, "explicit specialization of %qD outside its "
2730 "namespace must use a nested-name-specifier", tmpl);
2731 else if (processing_explicit_instantiation
2732 && cxx_dialect >= cxx11)
2733 /* This was allowed in C++98, so only pedwarn. */
2734 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2735 "outside its namespace must use a nested-name-"
2736 "specifier", tmpl);
2737 }
2738 }
2739
2740 /* Warn for a template specialization SPEC that is missing some of a set
2741 of function or type attributes that the template TEMPL is declared with.
2742 ATTRLIST is a list of additional attributes that SPEC should be taken
2743 to ultimately be declared with. */
2744
2745 static void
2746 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2747 {
2748 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2749 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2750
2751 /* Avoid warning if the difference between the primary and
2752 the specialization is not in one of the attributes below. */
2753 const char* const blacklist[] = {
2754 "alloc_align", "alloc_size", "assume_aligned", "format",
2755 "format_arg", "malloc", "nonnull", NULL
2756 };
2757
2758 /* Put together a list of the black listed attributes that the primary
2759 template is declared with that the specialization is not, in case
2760 it's not apparent from the most recent declaration of the primary. */
2761 pretty_printer str;
2762 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2763 blacklist, &str);
2764
2765 if (!nattrs)
2766 return;
2767
2768 auto_diagnostic_group d;
2769 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2770 "explicit specialization %q#D may be missing attributes",
2771 spec))
2772 inform (DECL_SOURCE_LOCATION (tmpl),
2773 nattrs > 1
2774 ? G_("missing primary template attributes %s")
2775 : G_("missing primary template attribute %s"),
2776 pp_formatted_text (&str));
2777 }
2778
2779 /* Check to see if the function just declared, as indicated in
2780 DECLARATOR, and in DECL, is a specialization of a function
2781 template. We may also discover that the declaration is an explicit
2782 instantiation at this point.
2783
2784 Returns DECL, or an equivalent declaration that should be used
2785 instead if all goes well. Issues an error message if something is
2786 amiss. Returns error_mark_node if the error is not easily
2787 recoverable.
2788
2789 FLAGS is a bitmask consisting of the following flags:
2790
2791 2: The function has a definition.
2792 4: The function is a friend.
2793
2794 The TEMPLATE_COUNT is the number of references to qualifying
2795 template classes that appeared in the name of the function. For
2796 example, in
2797
2798 template <class T> struct S { void f(); };
2799 void S<int>::f();
2800
2801 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2802 classes are not counted in the TEMPLATE_COUNT, so that in
2803
2804 template <class T> struct S {};
2805 template <> struct S<int> { void f(); }
2806 template <> void S<int>::f();
2807
2808 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2809 invalid; there should be no template <>.)
2810
2811 If the function is a specialization, it is marked as such via
2812 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2813 is set up correctly, and it is added to the list of specializations
2814 for that template. */
2815
2816 tree
2817 check_explicit_specialization (tree declarator,
2818 tree decl,
2819 int template_count,
2820 int flags,
2821 tree attrlist)
2822 {
2823 int have_def = flags & 2;
2824 int is_friend = flags & 4;
2825 bool is_concept = flags & 8;
2826 int specialization = 0;
2827 int explicit_instantiation = 0;
2828 int member_specialization = 0;
2829 tree ctype = DECL_CLASS_CONTEXT (decl);
2830 tree dname = DECL_NAME (decl);
2831 tmpl_spec_kind tsk;
2832
2833 if (is_friend)
2834 {
2835 if (!processing_specialization)
2836 tsk = tsk_none;
2837 else
2838 tsk = tsk_excessive_parms;
2839 }
2840 else
2841 tsk = current_tmpl_spec_kind (template_count);
2842
2843 switch (tsk)
2844 {
2845 case tsk_none:
2846 if (processing_specialization && !VAR_P (decl))
2847 {
2848 specialization = 1;
2849 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2850 }
2851 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2852 {
2853 if (is_friend)
2854 /* This could be something like:
2855
2856 template <class T> void f(T);
2857 class S { friend void f<>(int); } */
2858 specialization = 1;
2859 else
2860 {
2861 /* This case handles bogus declarations like template <>
2862 template <class T> void f<int>(); */
2863
2864 error_at (cp_expr_loc_or_input_loc (declarator),
2865 "template-id %qE in declaration of primary template",
2866 declarator);
2867 return decl;
2868 }
2869 }
2870 break;
2871
2872 case tsk_invalid_member_spec:
2873 /* The error has already been reported in
2874 check_specialization_scope. */
2875 return error_mark_node;
2876
2877 case tsk_invalid_expl_inst:
2878 error ("template parameter list used in explicit instantiation");
2879
2880 /* Fall through. */
2881
2882 case tsk_expl_inst:
2883 if (have_def)
2884 error ("definition provided for explicit instantiation");
2885
2886 explicit_instantiation = 1;
2887 break;
2888
2889 case tsk_excessive_parms:
2890 case tsk_insufficient_parms:
2891 if (tsk == tsk_excessive_parms)
2892 error ("too many template parameter lists in declaration of %qD",
2893 decl);
2894 else if (template_header_count)
2895 error("too few template parameter lists in declaration of %qD", decl);
2896 else
2897 error("explicit specialization of %qD must be introduced by "
2898 "%<template <>%>", decl);
2899
2900 /* Fall through. */
2901 case tsk_expl_spec:
2902 if (is_concept)
2903 error ("explicit specialization declared %<concept%>");
2904
2905 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2906 /* In cases like template<> constexpr bool v = true;
2907 We'll give an error in check_template_variable. */
2908 break;
2909
2910 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2911 if (ctype)
2912 member_specialization = 1;
2913 else
2914 specialization = 1;
2915 break;
2916
2917 case tsk_template:
2918 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2919 {
2920 /* This case handles bogus declarations like template <>
2921 template <class T> void f<int>(); */
2922
2923 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2924 error_at (cp_expr_loc_or_input_loc (declarator),
2925 "template-id %qE in declaration of primary template",
2926 declarator);
2927 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2928 {
2929 /* Partial specialization of variable template. */
2930 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2931 specialization = 1;
2932 goto ok;
2933 }
2934 else if (cxx_dialect < cxx14)
2935 error_at (cp_expr_loc_or_input_loc (declarator),
2936 "non-type partial specialization %qE "
2937 "is not allowed", declarator);
2938 else
2939 error_at (cp_expr_loc_or_input_loc (declarator),
2940 "non-class, non-variable partial specialization %qE "
2941 "is not allowed", declarator);
2942 return decl;
2943 ok:;
2944 }
2945
2946 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2947 /* This is a specialization of a member template, without
2948 specialization the containing class. Something like:
2949
2950 template <class T> struct S {
2951 template <class U> void f (U);
2952 };
2953 template <> template <class U> void S<int>::f(U) {}
2954
2955 That's a specialization -- but of the entire template. */
2956 specialization = 1;
2957 break;
2958
2959 default:
2960 gcc_unreachable ();
2961 }
2962
2963 if ((specialization || member_specialization)
2964 /* This doesn't apply to variable templates. */
2965 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2966 {
2967 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2968 for (; t; t = TREE_CHAIN (t))
2969 if (TREE_PURPOSE (t))
2970 {
2971 permerror (input_location,
2972 "default argument specified in explicit specialization");
2973 break;
2974 }
2975 }
2976
2977 if (specialization || member_specialization || explicit_instantiation)
2978 {
2979 tree tmpl = NULL_TREE;
2980 tree targs = NULL_TREE;
2981 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2982
2983 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2984 if (!was_template_id)
2985 {
2986 tree fns;
2987
2988 gcc_assert (identifier_p (declarator));
2989 if (ctype)
2990 fns = dname;
2991 else
2992 {
2993 /* If there is no class context, the explicit instantiation
2994 must be at namespace scope. */
2995 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2996
2997 /* Find the namespace binding, using the declaration
2998 context. */
2999 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3000 false, true);
3001 if (fns == error_mark_node)
3002 /* If lookup fails, look for a friend declaration so we can
3003 give a better diagnostic. */
3004 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3005 /*type*/false, /*complain*/true,
3006 /*hidden*/true);
3007
3008 if (fns == error_mark_node || !is_overloaded_fn (fns))
3009 {
3010 error ("%qD is not a template function", dname);
3011 fns = error_mark_node;
3012 }
3013 }
3014
3015 declarator = lookup_template_function (fns, NULL_TREE);
3016 }
3017
3018 if (declarator == error_mark_node)
3019 return error_mark_node;
3020
3021 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3022 {
3023 if (!explicit_instantiation)
3024 /* A specialization in class scope. This is invalid,
3025 but the error will already have been flagged by
3026 check_specialization_scope. */
3027 return error_mark_node;
3028 else
3029 {
3030 /* It's not valid to write an explicit instantiation in
3031 class scope, e.g.:
3032
3033 class C { template void f(); }
3034
3035 This case is caught by the parser. However, on
3036 something like:
3037
3038 template class C { void f(); };
3039
3040 (which is invalid) we can get here. The error will be
3041 issued later. */
3042 ;
3043 }
3044
3045 return decl;
3046 }
3047 else if (ctype != NULL_TREE
3048 && (identifier_p (TREE_OPERAND (declarator, 0))))
3049 {
3050 // We'll match variable templates in start_decl.
3051 if (VAR_P (decl))
3052 return decl;
3053
3054 /* Find the list of functions in ctype that have the same
3055 name as the declared function. */
3056 tree name = TREE_OPERAND (declarator, 0);
3057
3058 if (constructor_name_p (name, ctype))
3059 {
3060 if (DECL_CONSTRUCTOR_P (decl)
3061 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3062 : !CLASSTYPE_DESTRUCTOR (ctype))
3063 {
3064 /* From [temp.expl.spec]:
3065
3066 If such an explicit specialization for the member
3067 of a class template names an implicitly-declared
3068 special member function (clause _special_), the
3069 program is ill-formed.
3070
3071 Similar language is found in [temp.explicit]. */
3072 error ("specialization of implicitly-declared special member function");
3073 return error_mark_node;
3074 }
3075
3076 name = DECL_NAME (decl);
3077 }
3078
3079 /* For a type-conversion operator, We might be looking for
3080 `operator int' which will be a specialization of
3081 `operator T'. Grab all the conversion operators, and
3082 then select from them. */
3083 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3084 ? conv_op_identifier : name);
3085
3086 if (fns == NULL_TREE)
3087 {
3088 error ("no member function %qD declared in %qT", name, ctype);
3089 return error_mark_node;
3090 }
3091 else
3092 TREE_OPERAND (declarator, 0) = fns;
3093 }
3094
3095 /* Figure out what exactly is being specialized at this point.
3096 Note that for an explicit instantiation, even one for a
3097 member function, we cannot tell a priori whether the
3098 instantiation is for a member template, or just a member
3099 function of a template class. Even if a member template is
3100 being instantiated, the member template arguments may be
3101 elided if they can be deduced from the rest of the
3102 declaration. */
3103 tmpl = determine_specialization (declarator, decl,
3104 &targs,
3105 member_specialization,
3106 template_count,
3107 tsk);
3108
3109 if (!tmpl || tmpl == error_mark_node)
3110 /* We couldn't figure out what this declaration was
3111 specializing. */
3112 return error_mark_node;
3113 else
3114 {
3115 if (TREE_CODE (decl) == FUNCTION_DECL
3116 && DECL_HIDDEN_FRIEND_P (tmpl))
3117 {
3118 auto_diagnostic_group d;
3119 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3120 "friend declaration %qD is not visible to "
3121 "explicit specialization", tmpl))
3122 inform (DECL_SOURCE_LOCATION (tmpl),
3123 "friend declaration here");
3124 }
3125 else if (!ctype && !is_friend
3126 && CP_DECL_CONTEXT (decl) == current_namespace)
3127 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3128
3129 tree gen_tmpl = most_general_template (tmpl);
3130
3131 if (explicit_instantiation)
3132 {
3133 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3134 is done by do_decl_instantiation later. */
3135
3136 int arg_depth = TMPL_ARGS_DEPTH (targs);
3137 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3138
3139 if (arg_depth > parm_depth)
3140 {
3141 /* If TMPL is not the most general template (for
3142 example, if TMPL is a friend template that is
3143 injected into namespace scope), then there will
3144 be too many levels of TARGS. Remove some of them
3145 here. */
3146 int i;
3147 tree new_targs;
3148
3149 new_targs = make_tree_vec (parm_depth);
3150 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3151 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3152 = TREE_VEC_ELT (targs, i);
3153 targs = new_targs;
3154 }
3155
3156 return instantiate_template (tmpl, targs, tf_error);
3157 }
3158
3159 /* If we thought that the DECL was a member function, but it
3160 turns out to be specializing a static member function,
3161 make DECL a static member function as well. */
3162 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3163 && DECL_STATIC_FUNCTION_P (tmpl)
3164 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3165 revert_static_member_fn (decl);
3166
3167 /* If this is a specialization of a member template of a
3168 template class, we want to return the TEMPLATE_DECL, not
3169 the specialization of it. */
3170 if (tsk == tsk_template && !was_template_id)
3171 {
3172 tree result = DECL_TEMPLATE_RESULT (tmpl);
3173 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3174 DECL_INITIAL (result) = NULL_TREE;
3175 if (have_def)
3176 {
3177 tree parm;
3178 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3179 DECL_SOURCE_LOCATION (result)
3180 = DECL_SOURCE_LOCATION (decl);
3181 /* We want to use the argument list specified in the
3182 definition, not in the original declaration. */
3183 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3184 for (parm = DECL_ARGUMENTS (result); parm;
3185 parm = DECL_CHAIN (parm))
3186 DECL_CONTEXT (parm) = result;
3187 }
3188 return register_specialization (tmpl, gen_tmpl, targs,
3189 is_friend, 0);
3190 }
3191
3192 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3193 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3194
3195 if (was_template_id)
3196 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3197
3198 /* Inherit default function arguments from the template
3199 DECL is specializing. */
3200 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3201 copy_default_args_to_explicit_spec (decl);
3202
3203 /* This specialization has the same protection as the
3204 template it specializes. */
3205 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3206 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3207
3208 /* 7.1.1-1 [dcl.stc]
3209
3210 A storage-class-specifier shall not be specified in an
3211 explicit specialization...
3212
3213 The parser rejects these, so unless action is taken here,
3214 explicit function specializations will always appear with
3215 global linkage.
3216
3217 The action recommended by the C++ CWG in response to C++
3218 defect report 605 is to make the storage class and linkage
3219 of the explicit specialization match the templated function:
3220
3221 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3222 */
3223 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3224 {
3225 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3226 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3227
3228 /* A concept cannot be specialized. */
3229 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3230 {
3231 error ("explicit specialization of function concept %qD",
3232 gen_tmpl);
3233 return error_mark_node;
3234 }
3235
3236 /* This specialization has the same linkage and visibility as
3237 the function template it specializes. */
3238 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3239 if (! TREE_PUBLIC (decl))
3240 {
3241 DECL_INTERFACE_KNOWN (decl) = 1;
3242 DECL_NOT_REALLY_EXTERN (decl) = 1;
3243 }
3244 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3245 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3246 {
3247 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3248 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3249 }
3250 }
3251
3252 /* If DECL is a friend declaration, declared using an
3253 unqualified name, the namespace associated with DECL may
3254 have been set incorrectly. For example, in:
3255
3256 template <typename T> void f(T);
3257 namespace N {
3258 struct S { friend void f<int>(int); }
3259 }
3260
3261 we will have set the DECL_CONTEXT for the friend
3262 declaration to N, rather than to the global namespace. */
3263 if (DECL_NAMESPACE_SCOPE_P (decl))
3264 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3265
3266 if (is_friend && !have_def)
3267 /* This is not really a declaration of a specialization.
3268 It's just the name of an instantiation. But, it's not
3269 a request for an instantiation, either. */
3270 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3271 else if (TREE_CODE (decl) == FUNCTION_DECL)
3272 /* A specialization is not necessarily COMDAT. */
3273 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3274 && DECL_DECLARED_INLINE_P (decl));
3275 else if (VAR_P (decl))
3276 DECL_COMDAT (decl) = false;
3277
3278 /* If this is a full specialization, register it so that we can find
3279 it again. Partial specializations will be registered in
3280 process_partial_specialization. */
3281 if (!processing_template_decl)
3282 {
3283 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3284
3285 decl = register_specialization (decl, gen_tmpl, targs,
3286 is_friend, 0);
3287 }
3288
3289
3290 /* A 'structor should already have clones. */
3291 gcc_assert (decl == error_mark_node
3292 || variable_template_p (tmpl)
3293 || !(DECL_CONSTRUCTOR_P (decl)
3294 || DECL_DESTRUCTOR_P (decl))
3295 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3296 }
3297 }
3298
3299 return decl;
3300 }
3301
3302 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3303 parameters. These are represented in the same format used for
3304 DECL_TEMPLATE_PARMS. */
3305
3306 int
3307 comp_template_parms (const_tree parms1, const_tree parms2)
3308 {
3309 const_tree p1;
3310 const_tree p2;
3311
3312 if (parms1 == parms2)
3313 return 1;
3314
3315 for (p1 = parms1, p2 = parms2;
3316 p1 != NULL_TREE && p2 != NULL_TREE;
3317 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3318 {
3319 tree t1 = TREE_VALUE (p1);
3320 tree t2 = TREE_VALUE (p2);
3321 int i;
3322
3323 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3324 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3325
3326 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3327 return 0;
3328
3329 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3330 {
3331 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3332 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3333
3334 /* If either of the template parameters are invalid, assume
3335 they match for the sake of error recovery. */
3336 if (error_operand_p (parm1) || error_operand_p (parm2))
3337 return 1;
3338
3339 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3340 return 0;
3341
3342 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3343 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3344 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3345 continue;
3346 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3347 return 0;
3348 }
3349 }
3350
3351 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3352 /* One set of parameters has more parameters lists than the
3353 other. */
3354 return 0;
3355
3356 return 1;
3357 }
3358
3359 /* Returns true if two template parameters are declared with
3360 equivalent constraints. */
3361
3362 static bool
3363 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3364 {
3365 tree req1 = TREE_TYPE (parm1);
3366 tree req2 = TREE_TYPE (parm2);
3367 if (!req1 != !req2)
3368 return false;
3369 if (req1)
3370 return cp_tree_equal (req1, req2);
3371 return true;
3372 }
3373
3374 /* Returns true when two template parameters are equivalent. */
3375
3376 static bool
3377 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3378 {
3379 tree decl1 = TREE_VALUE (parm1);
3380 tree decl2 = TREE_VALUE (parm2);
3381
3382 /* If either of the template parameters are invalid, assume
3383 they match for the sake of error recovery. */
3384 if (error_operand_p (decl1) || error_operand_p (decl2))
3385 return true;
3386
3387 /* ... they declare parameters of the same kind. */
3388 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3389 return false;
3390
3391 /* ... one parameter was introduced by a parameter declaration, then
3392 both are. This case arises as a result of eagerly rewriting declarations
3393 during parsing. */
3394 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3395 return false;
3396
3397 /* ... if either declares a pack, they both do. */
3398 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3399 return false;
3400
3401 if (TREE_CODE (decl1) == PARM_DECL)
3402 {
3403 /* ... if they declare non-type parameters, the types are equivalent. */
3404 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3405 return false;
3406 }
3407 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3408 {
3409 /* ... if they declare template template parameters, their template
3410 parameter lists are equivalent. */
3411 if (!template_heads_equivalent_p (decl1, decl2))
3412 return false;
3413 }
3414
3415 /* ... if they are declared with a qualified-concept name, they both
3416 are, and those names are equivalent. */
3417 return template_parameter_constraints_equivalent_p (parm1, parm2);
3418 }
3419
3420 /* Returns true if two template parameters lists are equivalent.
3421 Two template parameter lists are equivalent if they have the
3422 same length and their corresponding parameters are equivalent.
3423
3424 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3425 data structure returned by DECL_TEMPLATE_PARMS.
3426
3427 This is generally the same implementation as comp_template_parms
3428 except that it also the concept names and arguments used to
3429 introduce parameters. */
3430
3431 static bool
3432 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3433 {
3434 if (parms1 == parms2)
3435 return true;
3436
3437 const_tree p1 = parms1;
3438 const_tree p2 = parms2;
3439 while (p1 != NULL_TREE && p2 != NULL_TREE)
3440 {
3441 tree list1 = TREE_VALUE (p1);
3442 tree list2 = TREE_VALUE (p2);
3443
3444 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3445 return 0;
3446
3447 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3448 {
3449 tree parm1 = TREE_VEC_ELT (list1, i);
3450 tree parm2 = TREE_VEC_ELT (list2, i);
3451 if (!template_parameters_equivalent_p (parm1, parm2))
3452 return false;
3453 }
3454
3455 p1 = TREE_CHAIN (p1);
3456 p2 = TREE_CHAIN (p2);
3457 }
3458
3459 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3460 return false;
3461
3462 return true;
3463 }
3464
3465 /* Return true if the requires-clause of the template parameter lists are
3466 equivalent and false otherwise. */
3467 static bool
3468 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3469 {
3470 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3471 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3472 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3473 return false;
3474 if (!cp_tree_equal (req1, req2))
3475 return false;
3476 return true;
3477 }
3478
3479 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3480 Two template heads are equivalent if their template parameter
3481 lists are equivalent and their requires clauses are equivalent.
3482
3483 In pre-C++20, this is equivalent to calling comp_template_parms
3484 for the template parameters of TMPL1 and TMPL2. */
3485
3486 bool
3487 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3488 {
3489 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3490 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3491
3492 /* Don't change the matching rules for pre-C++20. */
3493 if (cxx_dialect < cxx20)
3494 return comp_template_parms (parms1, parms2);
3495
3496 /* ... have the same number of template parameters, and their
3497 corresponding parameters are equivalent. */
3498 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3499 return false;
3500
3501 /* ... if either has a requires-clause, they both do and their
3502 corresponding constraint-expressions are equivalent. */
3503 return template_requirements_equivalent_p (parms1, parms2);
3504 }
3505
3506 /* Determine whether PARM is a parameter pack. */
3507
3508 bool
3509 template_parameter_pack_p (const_tree parm)
3510 {
3511 /* Determine if we have a non-type template parameter pack. */
3512 if (TREE_CODE (parm) == PARM_DECL)
3513 return (DECL_TEMPLATE_PARM_P (parm)
3514 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3515 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3516 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3517
3518 /* If this is a list of template parameters, we could get a
3519 TYPE_DECL or a TEMPLATE_DECL. */
3520 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3521 parm = TREE_TYPE (parm);
3522
3523 /* Otherwise it must be a type template parameter. */
3524 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3525 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3526 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3527 }
3528
3529 /* Determine if T is a function parameter pack. */
3530
3531 bool
3532 function_parameter_pack_p (const_tree t)
3533 {
3534 if (t && TREE_CODE (t) == PARM_DECL)
3535 return DECL_PACK_P (t);
3536 return false;
3537 }
3538
3539 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3540 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3541
3542 tree
3543 get_function_template_decl (const_tree primary_func_tmpl_inst)
3544 {
3545 if (! primary_func_tmpl_inst
3546 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3547 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3548 return NULL;
3549
3550 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3551 }
3552
3553 /* Return true iff the function parameter PARAM_DECL was expanded
3554 from the function parameter pack PACK. */
3555
3556 bool
3557 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3558 {
3559 if (DECL_ARTIFICIAL (param_decl)
3560 || !function_parameter_pack_p (pack))
3561 return false;
3562
3563 /* The parameter pack and its pack arguments have the same
3564 DECL_PARM_INDEX. */
3565 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3566 }
3567
3568 /* Determine whether ARGS describes a variadic template args list,
3569 i.e., one that is terminated by a template argument pack. */
3570
3571 static bool
3572 template_args_variadic_p (tree args)
3573 {
3574 int nargs;
3575 tree last_parm;
3576
3577 if (args == NULL_TREE)
3578 return false;
3579
3580 args = INNERMOST_TEMPLATE_ARGS (args);
3581 nargs = TREE_VEC_LENGTH (args);
3582
3583 if (nargs == 0)
3584 return false;
3585
3586 last_parm = TREE_VEC_ELT (args, nargs - 1);
3587
3588 return ARGUMENT_PACK_P (last_parm);
3589 }
3590
3591 /* Generate a new name for the parameter pack name NAME (an
3592 IDENTIFIER_NODE) that incorporates its */
3593
3594 static tree
3595 make_ith_pack_parameter_name (tree name, int i)
3596 {
3597 /* Munge the name to include the parameter index. */
3598 #define NUMBUF_LEN 128
3599 char numbuf[NUMBUF_LEN];
3600 char* newname;
3601 int newname_len;
3602
3603 if (name == NULL_TREE)
3604 return name;
3605 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3606 newname_len = IDENTIFIER_LENGTH (name)
3607 + strlen (numbuf) + 2;
3608 newname = (char*)alloca (newname_len);
3609 snprintf (newname, newname_len,
3610 "%s#%i", IDENTIFIER_POINTER (name), i);
3611 return get_identifier (newname);
3612 }
3613
3614 /* Return true if T is a primary function, class or alias template
3615 specialization, not including the template pattern. */
3616
3617 bool
3618 primary_template_specialization_p (const_tree t)
3619 {
3620 if (!t)
3621 return false;
3622
3623 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3624 return (DECL_LANG_SPECIFIC (t)
3625 && DECL_USE_TEMPLATE (t)
3626 && DECL_TEMPLATE_INFO (t)
3627 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3628 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3629 return (CLASSTYPE_TEMPLATE_INFO (t)
3630 && CLASSTYPE_USE_TEMPLATE (t)
3631 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3632 else if (alias_template_specialization_p (t, nt_transparent))
3633 return true;
3634 return false;
3635 }
3636
3637 /* Return true if PARM is a template template parameter. */
3638
3639 bool
3640 template_template_parameter_p (const_tree parm)
3641 {
3642 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3643 }
3644
3645 /* Return true iff PARM is a DECL representing a type template
3646 parameter. */
3647
3648 bool
3649 template_type_parameter_p (const_tree parm)
3650 {
3651 return (parm
3652 && (TREE_CODE (parm) == TYPE_DECL
3653 || TREE_CODE (parm) == TEMPLATE_DECL)
3654 && DECL_TEMPLATE_PARM_P (parm));
3655 }
3656
3657 /* Return the template parameters of T if T is a
3658 primary template instantiation, NULL otherwise. */
3659
3660 tree
3661 get_primary_template_innermost_parameters (const_tree t)
3662 {
3663 tree parms = NULL, template_info = NULL;
3664
3665 if ((template_info = get_template_info (t))
3666 && primary_template_specialization_p (t))
3667 parms = INNERMOST_TEMPLATE_PARMS
3668 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3669
3670 return parms;
3671 }
3672
3673 /* Return the template parameters of the LEVELth level from the full list
3674 of template parameters PARMS. */
3675
3676 tree
3677 get_template_parms_at_level (tree parms, int level)
3678 {
3679 tree p;
3680 if (!parms
3681 || TREE_CODE (parms) != TREE_LIST
3682 || level > TMPL_PARMS_DEPTH (parms))
3683 return NULL_TREE;
3684
3685 for (p = parms; p; p = TREE_CHAIN (p))
3686 if (TMPL_PARMS_DEPTH (p) == level)
3687 return p;
3688
3689 return NULL_TREE;
3690 }
3691
3692 /* Returns the template arguments of T if T is a template instantiation,
3693 NULL otherwise. */
3694
3695 tree
3696 get_template_innermost_arguments (const_tree t)
3697 {
3698 tree args = NULL, template_info = NULL;
3699
3700 if ((template_info = get_template_info (t))
3701 && TI_ARGS (template_info))
3702 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3703
3704 return args;
3705 }
3706
3707 /* Return the argument pack elements of T if T is a template argument pack,
3708 NULL otherwise. */
3709
3710 tree
3711 get_template_argument_pack_elems (const_tree t)
3712 {
3713 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3714 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3715 return NULL;
3716
3717 return ARGUMENT_PACK_ARGS (t);
3718 }
3719
3720 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3721 ARGUMENT_PACK_SELECT represents. */
3722
3723 static tree
3724 argument_pack_select_arg (tree t)
3725 {
3726 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3727 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3728
3729 /* If the selected argument is an expansion E, that most likely means we were
3730 called from gen_elem_of_pack_expansion_instantiation during the
3731 substituting of an argument pack (of which the Ith element is a pack
3732 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3733 In this case, the Ith element resulting from this substituting is going to
3734 be a pack expansion, which pattern is the pattern of E. Let's return the
3735 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3736 resulting pack expansion from it. */
3737 if (PACK_EXPANSION_P (arg))
3738 {
3739 /* Make sure we aren't throwing away arg info. */
3740 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3741 arg = PACK_EXPANSION_PATTERN (arg);
3742 }
3743
3744 return arg;
3745 }
3746
3747
3748 /* True iff FN is a function representing a built-in variadic parameter
3749 pack. */
3750
3751 bool
3752 builtin_pack_fn_p (tree fn)
3753 {
3754 if (!fn
3755 || TREE_CODE (fn) != FUNCTION_DECL
3756 || !DECL_IS_BUILTIN (fn))
3757 return false;
3758
3759 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3760 return true;
3761
3762 return false;
3763 }
3764
3765 /* True iff CALL is a call to a function representing a built-in variadic
3766 parameter pack. */
3767
3768 static bool
3769 builtin_pack_call_p (tree call)
3770 {
3771 if (TREE_CODE (call) != CALL_EXPR)
3772 return false;
3773 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3774 }
3775
3776 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3777
3778 static tree
3779 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3780 tree in_decl)
3781 {
3782 tree ohi = CALL_EXPR_ARG (call, 0);
3783 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3784 false/*fn*/, true/*int_cst*/);
3785
3786 if (value_dependent_expression_p (hi))
3787 {
3788 if (hi != ohi)
3789 {
3790 call = copy_node (call);
3791 CALL_EXPR_ARG (call, 0) = hi;
3792 }
3793 tree ex = make_pack_expansion (call, complain);
3794 tree vec = make_tree_vec (1);
3795 TREE_VEC_ELT (vec, 0) = ex;
3796 return vec;
3797 }
3798 else
3799 {
3800 hi = cxx_constant_value (hi);
3801 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3802
3803 /* Calculate the largest value of len that won't make the size of the vec
3804 overflow an int. The compiler will exceed resource limits long before
3805 this, but it seems a decent place to diagnose. */
3806 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3807
3808 if (len < 0 || len > max)
3809 {
3810 if ((complain & tf_error)
3811 && hi != error_mark_node)
3812 error ("argument to %<__integer_pack%> must be between 0 and %d",
3813 max);
3814 return error_mark_node;
3815 }
3816
3817 tree vec = make_tree_vec (len);
3818
3819 for (int i = 0; i < len; ++i)
3820 TREE_VEC_ELT (vec, i) = size_int (i);
3821
3822 return vec;
3823 }
3824 }
3825
3826 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3827 CALL. */
3828
3829 static tree
3830 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3831 tree in_decl)
3832 {
3833 if (!builtin_pack_call_p (call))
3834 return NULL_TREE;
3835
3836 tree fn = CALL_EXPR_FN (call);
3837
3838 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3839 return expand_integer_pack (call, args, complain, in_decl);
3840
3841 return NULL_TREE;
3842 }
3843
3844 /* Structure used to track the progress of find_parameter_packs_r. */
3845 struct find_parameter_pack_data
3846 {
3847 /* TREE_LIST that will contain all of the parameter packs found by
3848 the traversal. */
3849 tree* parameter_packs;
3850
3851 /* Set of AST nodes that have been visited by the traversal. */
3852 hash_set<tree> *visited;
3853
3854 /* True iff we're making a type pack expansion. */
3855 bool type_pack_expansion_p;
3856 };
3857
3858 /* Identifies all of the argument packs that occur in a template
3859 argument and appends them to the TREE_LIST inside DATA, which is a
3860 find_parameter_pack_data structure. This is a subroutine of
3861 make_pack_expansion and uses_parameter_packs. */
3862 static tree
3863 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3864 {
3865 tree t = *tp;
3866 struct find_parameter_pack_data* ppd =
3867 (struct find_parameter_pack_data*)data;
3868 bool parameter_pack_p = false;
3869
3870 /* Don't look through typedefs; we are interested in whether a
3871 parameter pack is actually written in the expression/type we're
3872 looking at, not the target type. */
3873 if (TYPE_P (t) && typedef_variant_p (t))
3874 {
3875 /* But do look at arguments for an alias template. */
3876 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3877 cp_walk_tree (&TI_ARGS (tinfo),
3878 &find_parameter_packs_r,
3879 ppd, ppd->visited);
3880 *walk_subtrees = 0;
3881 return NULL_TREE;
3882 }
3883
3884 /* Identify whether this is a parameter pack or not. */
3885 switch (TREE_CODE (t))
3886 {
3887 case TEMPLATE_PARM_INDEX:
3888 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3889 parameter_pack_p = true;
3890 break;
3891
3892 case TEMPLATE_TYPE_PARM:
3893 t = TYPE_MAIN_VARIANT (t);
3894 /* FALLTHRU */
3895 case TEMPLATE_TEMPLATE_PARM:
3896 /* If the placeholder appears in the decl-specifier-seq of a function
3897 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3898 is a pack expansion, the invented template parameter is a template
3899 parameter pack. */
3900 if (ppd->type_pack_expansion_p && is_auto (t))
3901 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3902 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3903 parameter_pack_p = true;
3904 break;
3905
3906 case FIELD_DECL:
3907 case PARM_DECL:
3908 if (DECL_PACK_P (t))
3909 {
3910 /* We don't want to walk into the type of a PARM_DECL,
3911 because we don't want to see the type parameter pack. */
3912 *walk_subtrees = 0;
3913 parameter_pack_p = true;
3914 }
3915 break;
3916
3917 case VAR_DECL:
3918 if (DECL_PACK_P (t))
3919 {
3920 /* We don't want to walk into the type of a variadic capture proxy,
3921 because we don't want to see the type parameter pack. */
3922 *walk_subtrees = 0;
3923 parameter_pack_p = true;
3924 }
3925 else if (variable_template_specialization_p (t))
3926 {
3927 cp_walk_tree (&DECL_TI_ARGS (t),
3928 find_parameter_packs_r,
3929 ppd, ppd->visited);
3930 *walk_subtrees = 0;
3931 }
3932 break;
3933
3934 case CALL_EXPR:
3935 if (builtin_pack_call_p (t))
3936 parameter_pack_p = true;
3937 break;
3938
3939 case BASES:
3940 parameter_pack_p = true;
3941 break;
3942 default:
3943 /* Not a parameter pack. */
3944 break;
3945 }
3946
3947 if (parameter_pack_p)
3948 {
3949 /* Add this parameter pack to the list. */
3950 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3951 }
3952
3953 if (TYPE_P (t))
3954 cp_walk_tree (&TYPE_CONTEXT (t),
3955 &find_parameter_packs_r, ppd, ppd->visited);
3956
3957 /* This switch statement will return immediately if we don't find a
3958 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3959 switch (TREE_CODE (t))
3960 {
3961 case BOUND_TEMPLATE_TEMPLATE_PARM:
3962 /* Check the template itself. */
3963 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3964 &find_parameter_packs_r, ppd, ppd->visited);
3965 return NULL_TREE;
3966
3967 case DECL_EXPR:
3968 {
3969 tree decl = DECL_EXPR_DECL (t);
3970 /* Ignore the declaration of a capture proxy for a parameter pack. */
3971 if (is_capture_proxy (decl))
3972 *walk_subtrees = 0;
3973 if (is_typedef_decl (decl))
3974 /* Since we stop at typedefs above, we need to look through them at
3975 the point of the DECL_EXPR. */
3976 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3977 &find_parameter_packs_r, ppd, ppd->visited);
3978 return NULL_TREE;
3979 }
3980
3981 case TEMPLATE_DECL:
3982 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3983 return NULL_TREE;
3984 cp_walk_tree (&TREE_TYPE (t),
3985 &find_parameter_packs_r, ppd, ppd->visited);
3986 return NULL_TREE;
3987
3988 case TYPE_PACK_EXPANSION:
3989 case EXPR_PACK_EXPANSION:
3990 *walk_subtrees = 0;
3991 return NULL_TREE;
3992
3993 case INTEGER_TYPE:
3994 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3995 ppd, ppd->visited);
3996 *walk_subtrees = 0;
3997 return NULL_TREE;
3998
3999 case IDENTIFIER_NODE:
4000 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4001 ppd->visited);
4002 *walk_subtrees = 0;
4003 return NULL_TREE;
4004
4005 case LAMBDA_EXPR:
4006 {
4007 /* Since we defer implicit capture, look in the parms and body. */
4008 tree fn = lambda_function (t);
4009 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4010 ppd->visited);
4011 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4012 ppd->visited);
4013 return NULL_TREE;
4014 }
4015
4016 case DECLTYPE_TYPE:
4017 {
4018 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4019 type_pack_expansion_p to false so that any placeholders
4020 within the expression don't get marked as parameter packs. */
4021 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4022 ppd->type_pack_expansion_p = false;
4023 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4024 ppd, ppd->visited);
4025 ppd->type_pack_expansion_p = type_pack_expansion_p;
4026 *walk_subtrees = 0;
4027 return NULL_TREE;
4028 }
4029
4030 case IF_STMT:
4031 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4032 ppd, ppd->visited);
4033 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4034 ppd, ppd->visited);
4035 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4036 ppd, ppd->visited);
4037 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4038 *walk_subtrees = 0;
4039 return NULL_TREE;
4040
4041 default:
4042 return NULL_TREE;
4043 }
4044
4045 return NULL_TREE;
4046 }
4047
4048 /* Determines if the expression or type T uses any parameter packs. */
4049 tree
4050 uses_parameter_packs (tree t)
4051 {
4052 tree parameter_packs = NULL_TREE;
4053 struct find_parameter_pack_data ppd;
4054 ppd.parameter_packs = &parameter_packs;
4055 ppd.visited = new hash_set<tree>;
4056 ppd.type_pack_expansion_p = false;
4057 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4058 delete ppd.visited;
4059 return parameter_packs;
4060 }
4061
4062 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4063 representation a base-class initializer into a parameter pack
4064 expansion. If all goes well, the resulting node will be an
4065 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4066 respectively. */
4067 tree
4068 make_pack_expansion (tree arg, tsubst_flags_t complain)
4069 {
4070 tree result;
4071 tree parameter_packs = NULL_TREE;
4072 bool for_types = false;
4073 struct find_parameter_pack_data ppd;
4074
4075 if (!arg || arg == error_mark_node)
4076 return arg;
4077
4078 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4079 {
4080 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4081 class initializer. In this case, the TREE_PURPOSE will be a
4082 _TYPE node (representing the base class expansion we're
4083 initializing) and the TREE_VALUE will be a TREE_LIST
4084 containing the initialization arguments.
4085
4086 The resulting expansion looks somewhat different from most
4087 expansions. Rather than returning just one _EXPANSION, we
4088 return a TREE_LIST whose TREE_PURPOSE is a
4089 TYPE_PACK_EXPANSION containing the bases that will be
4090 initialized. The TREE_VALUE will be identical to the
4091 original TREE_VALUE, which is a list of arguments that will
4092 be passed to each base. We do not introduce any new pack
4093 expansion nodes into the TREE_VALUE (although it is possible
4094 that some already exist), because the TREE_PURPOSE and
4095 TREE_VALUE all need to be expanded together with the same
4096 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4097 resulting TREE_PURPOSE will mention the parameter packs in
4098 both the bases and the arguments to the bases. */
4099 tree purpose;
4100 tree value;
4101 tree parameter_packs = NULL_TREE;
4102
4103 /* Determine which parameter packs will be used by the base
4104 class expansion. */
4105 ppd.visited = new hash_set<tree>;
4106 ppd.parameter_packs = &parameter_packs;
4107 ppd.type_pack_expansion_p = false;
4108 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4109 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4110 &ppd, ppd.visited);
4111
4112 if (parameter_packs == NULL_TREE)
4113 {
4114 if (complain & tf_error)
4115 error ("base initializer expansion %qT contains no parameter packs",
4116 arg);
4117 delete ppd.visited;
4118 return error_mark_node;
4119 }
4120
4121 if (TREE_VALUE (arg) != void_type_node)
4122 {
4123 /* Collect the sets of parameter packs used in each of the
4124 initialization arguments. */
4125 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4126 {
4127 /* Determine which parameter packs will be expanded in this
4128 argument. */
4129 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4130 &ppd, ppd.visited);
4131 }
4132 }
4133
4134 delete ppd.visited;
4135
4136 /* Create the pack expansion type for the base type. */
4137 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4138 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4139 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4140 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4141
4142 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4143 they will rarely be compared to anything. */
4144 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4145
4146 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4147 }
4148
4149 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4150 for_types = true;
4151
4152 /* Build the PACK_EXPANSION_* node. */
4153 result = for_types
4154 ? cxx_make_type (TYPE_PACK_EXPANSION)
4155 : make_node (EXPR_PACK_EXPANSION);
4156 SET_PACK_EXPANSION_PATTERN (result, arg);
4157 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4158 {
4159 /* Propagate type and const-expression information. */
4160 TREE_TYPE (result) = TREE_TYPE (arg);
4161 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4162 /* Mark this read now, since the expansion might be length 0. */
4163 mark_exp_read (arg);
4164 }
4165 else
4166 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4167 they will rarely be compared to anything. */
4168 SET_TYPE_STRUCTURAL_EQUALITY (result);
4169
4170 /* Determine which parameter packs will be expanded. */
4171 ppd.parameter_packs = &parameter_packs;
4172 ppd.visited = new hash_set<tree>;
4173 ppd.type_pack_expansion_p = TYPE_P (arg);
4174 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4175 delete ppd.visited;
4176
4177 /* Make sure we found some parameter packs. */
4178 if (parameter_packs == NULL_TREE)
4179 {
4180 if (complain & tf_error)
4181 {
4182 if (TYPE_P (arg))
4183 error ("expansion pattern %qT contains no parameter packs", arg);
4184 else
4185 error ("expansion pattern %qE contains no parameter packs", arg);
4186 }
4187 return error_mark_node;
4188 }
4189 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4190
4191 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4192
4193 return result;
4194 }
4195
4196 /* Checks T for any "bare" parameter packs, which have not yet been
4197 expanded, and issues an error if any are found. This operation can
4198 only be done on full expressions or types (e.g., an expression
4199 statement, "if" condition, etc.), because we could have expressions like:
4200
4201 foo(f(g(h(args)))...)
4202
4203 where "args" is a parameter pack. check_for_bare_parameter_packs
4204 should not be called for the subexpressions args, h(args),
4205 g(h(args)), or f(g(h(args))), because we would produce erroneous
4206 error messages.
4207
4208 Returns TRUE and emits an error if there were bare parameter packs,
4209 returns FALSE otherwise. */
4210 bool
4211 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4212 {
4213 tree parameter_packs = NULL_TREE;
4214 struct find_parameter_pack_data ppd;
4215
4216 if (!processing_template_decl || !t || t == error_mark_node)
4217 return false;
4218
4219 /* A lambda might use a parameter pack from the containing context. */
4220 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4221 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4222 return false;
4223
4224 if (TREE_CODE (t) == TYPE_DECL)
4225 t = TREE_TYPE (t);
4226
4227 ppd.parameter_packs = &parameter_packs;
4228 ppd.visited = new hash_set<tree>;
4229 ppd.type_pack_expansion_p = false;
4230 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4231 delete ppd.visited;
4232
4233 if (parameter_packs)
4234 {
4235 if (loc == UNKNOWN_LOCATION)
4236 loc = cp_expr_loc_or_input_loc (t);
4237 error_at (loc, "parameter packs not expanded with %<...%>:");
4238 while (parameter_packs)
4239 {
4240 tree pack = TREE_VALUE (parameter_packs);
4241 tree name = NULL_TREE;
4242
4243 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4244 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4245 name = TYPE_NAME (pack);
4246 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4247 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4248 else if (TREE_CODE (pack) == CALL_EXPR)
4249 name = DECL_NAME (CALL_EXPR_FN (pack));
4250 else
4251 name = DECL_NAME (pack);
4252
4253 if (name)
4254 inform (loc, " %qD", name);
4255 else
4256 inform (loc, " %s", "<anonymous>");
4257
4258 parameter_packs = TREE_CHAIN (parameter_packs);
4259 }
4260
4261 return true;
4262 }
4263
4264 return false;
4265 }
4266
4267 /* Expand any parameter packs that occur in the template arguments in
4268 ARGS. */
4269 tree
4270 expand_template_argument_pack (tree args)
4271 {
4272 if (args == error_mark_node)
4273 return error_mark_node;
4274
4275 tree result_args = NULL_TREE;
4276 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4277 int num_result_args = -1;
4278 int non_default_args_count = -1;
4279
4280 /* First, determine if we need to expand anything, and the number of
4281 slots we'll need. */
4282 for (in_arg = 0; in_arg < nargs; ++in_arg)
4283 {
4284 tree arg = TREE_VEC_ELT (args, in_arg);
4285 if (arg == NULL_TREE)
4286 return args;
4287 if (ARGUMENT_PACK_P (arg))
4288 {
4289 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4290 if (num_result_args < 0)
4291 num_result_args = in_arg + num_packed;
4292 else
4293 num_result_args += num_packed;
4294 }
4295 else
4296 {
4297 if (num_result_args >= 0)
4298 num_result_args++;
4299 }
4300 }
4301
4302 /* If no expansion is necessary, we're done. */
4303 if (num_result_args < 0)
4304 return args;
4305
4306 /* Expand arguments. */
4307 result_args = make_tree_vec (num_result_args);
4308 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4309 non_default_args_count =
4310 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4311 for (in_arg = 0; in_arg < nargs; ++in_arg)
4312 {
4313 tree arg = TREE_VEC_ELT (args, in_arg);
4314 if (ARGUMENT_PACK_P (arg))
4315 {
4316 tree packed = ARGUMENT_PACK_ARGS (arg);
4317 int i, num_packed = TREE_VEC_LENGTH (packed);
4318 for (i = 0; i < num_packed; ++i, ++out_arg)
4319 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4320 if (non_default_args_count > 0)
4321 non_default_args_count += num_packed - 1;
4322 }
4323 else
4324 {
4325 TREE_VEC_ELT (result_args, out_arg) = arg;
4326 ++out_arg;
4327 }
4328 }
4329 if (non_default_args_count >= 0)
4330 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4331 return result_args;
4332 }
4333
4334 /* Checks if DECL shadows a template parameter.
4335
4336 [temp.local]: A template-parameter shall not be redeclared within its
4337 scope (including nested scopes).
4338
4339 Emits an error and returns TRUE if the DECL shadows a parameter,
4340 returns FALSE otherwise. */
4341
4342 bool
4343 check_template_shadow (tree decl)
4344 {
4345 tree olddecl;
4346
4347 /* If we're not in a template, we can't possibly shadow a template
4348 parameter. */
4349 if (!current_template_parms)
4350 return true;
4351
4352 /* Figure out what we're shadowing. */
4353 decl = OVL_FIRST (decl);
4354 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4355
4356 /* If there's no previous binding for this name, we're not shadowing
4357 anything, let alone a template parameter. */
4358 if (!olddecl)
4359 return true;
4360
4361 /* If we're not shadowing a template parameter, we're done. Note
4362 that OLDDECL might be an OVERLOAD (or perhaps even an
4363 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4364 node. */
4365 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4366 return true;
4367
4368 /* We check for decl != olddecl to avoid bogus errors for using a
4369 name inside a class. We check TPFI to avoid duplicate errors for
4370 inline member templates. */
4371 if (decl == olddecl
4372 || (DECL_TEMPLATE_PARM_P (decl)
4373 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4374 return true;
4375
4376 /* Don't complain about the injected class name, as we've already
4377 complained about the class itself. */
4378 if (DECL_SELF_REFERENCE_P (decl))
4379 return false;
4380
4381 if (DECL_TEMPLATE_PARM_P (decl))
4382 error ("declaration of template parameter %q+D shadows "
4383 "template parameter", decl);
4384 else
4385 error ("declaration of %q+#D shadows template parameter", decl);
4386 inform (DECL_SOURCE_LOCATION (olddecl),
4387 "template parameter %qD declared here", olddecl);
4388 return false;
4389 }
4390
4391 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4392 ORIG_LEVEL, DECL, and TYPE. */
4393
4394 static tree
4395 build_template_parm_index (int index,
4396 int level,
4397 int orig_level,
4398 tree decl,
4399 tree type)
4400 {
4401 tree t = make_node (TEMPLATE_PARM_INDEX);
4402 TEMPLATE_PARM_IDX (t) = index;
4403 TEMPLATE_PARM_LEVEL (t) = level;
4404 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4405 TEMPLATE_PARM_DECL (t) = decl;
4406 TREE_TYPE (t) = type;
4407 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4408 TREE_READONLY (t) = TREE_READONLY (decl);
4409
4410 return t;
4411 }
4412
4413 /* Find the canonical type parameter for the given template type
4414 parameter. Returns the canonical type parameter, which may be TYPE
4415 if no such parameter existed. */
4416
4417 static tree
4418 canonical_type_parameter (tree type)
4419 {
4420 int idx = TEMPLATE_TYPE_IDX (type);
4421
4422 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4423
4424 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4425 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4426
4427 for (tree list = (*canonical_template_parms)[idx];
4428 list; list = TREE_CHAIN (list))
4429 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4430 return TREE_VALUE (list);
4431
4432 (*canonical_template_parms)[idx]
4433 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4434 return type;
4435 }
4436
4437 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4438 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4439 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4440 new one is created. */
4441
4442 static tree
4443 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4444 tsubst_flags_t complain)
4445 {
4446 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4447 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4448 != TEMPLATE_PARM_LEVEL (index) - levels)
4449 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4450 {
4451 tree orig_decl = TEMPLATE_PARM_DECL (index);
4452
4453 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4454 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4455 type);
4456 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4457 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4458 DECL_ARTIFICIAL (decl) = 1;
4459 SET_DECL_TEMPLATE_PARM_P (decl);
4460
4461 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4462 TEMPLATE_PARM_LEVEL (index) - levels,
4463 TEMPLATE_PARM_ORIG_LEVEL (index),
4464 decl, type);
4465 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4466 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4467 = TEMPLATE_PARM_PARAMETER_PACK (index);
4468
4469 /* Template template parameters need this. */
4470 tree inner = decl;
4471 if (TREE_CODE (decl) == TEMPLATE_DECL)
4472 {
4473 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4474 TYPE_DECL, DECL_NAME (decl), type);
4475 DECL_TEMPLATE_RESULT (decl) = inner;
4476 DECL_ARTIFICIAL (inner) = true;
4477 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4478 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4479 }
4480
4481 /* Attach the TPI to the decl. */
4482 if (TREE_CODE (inner) == TYPE_DECL)
4483 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4484 else
4485 DECL_INITIAL (decl) = tpi;
4486 }
4487
4488 return TEMPLATE_PARM_DESCENDANTS (index);
4489 }
4490
4491 /* Process information from new template parameter PARM and append it
4492 to the LIST being built. This new parameter is a non-type
4493 parameter iff IS_NON_TYPE is true. This new parameter is a
4494 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4495 is in PARM_LOC. */
4496
4497 tree
4498 process_template_parm (tree list, location_t parm_loc, tree parm,
4499 bool is_non_type, bool is_parameter_pack)
4500 {
4501 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4502 tree prev = NULL_TREE;
4503 int idx = 0;
4504
4505 if (list)
4506 {
4507 prev = tree_last (list);
4508
4509 tree p = TREE_VALUE (prev);
4510 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4511 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4512 else if (TREE_CODE (p) == PARM_DECL)
4513 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4514
4515 ++idx;
4516 }
4517
4518 tree decl = NULL_TREE;
4519 tree defval = TREE_PURPOSE (parm);
4520 tree constr = TREE_TYPE (parm);
4521
4522 if (is_non_type)
4523 {
4524 parm = TREE_VALUE (parm);
4525
4526 SET_DECL_TEMPLATE_PARM_P (parm);
4527
4528 if (TREE_TYPE (parm) != error_mark_node)
4529 {
4530 /* [temp.param]
4531
4532 The top-level cv-qualifiers on the template-parameter are
4533 ignored when determining its type. */
4534 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4535 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4536 TREE_TYPE (parm) = error_mark_node;
4537 else if (uses_parameter_packs (TREE_TYPE (parm))
4538 && !is_parameter_pack
4539 /* If we're in a nested template parameter list, the template
4540 template parameter could be a parameter pack. */
4541 && processing_template_parmlist == 1)
4542 {
4543 /* This template parameter is not a parameter pack, but it
4544 should be. Complain about "bare" parameter packs. */
4545 check_for_bare_parameter_packs (TREE_TYPE (parm));
4546
4547 /* Recover by calling this a parameter pack. */
4548 is_parameter_pack = true;
4549 }
4550 }
4551
4552 /* A template parameter is not modifiable. */
4553 TREE_CONSTANT (parm) = 1;
4554 TREE_READONLY (parm) = 1;
4555 decl = build_decl (parm_loc,
4556 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4557 TREE_CONSTANT (decl) = 1;
4558 TREE_READONLY (decl) = 1;
4559 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4560 = build_template_parm_index (idx, processing_template_decl,
4561 processing_template_decl,
4562 decl, TREE_TYPE (parm));
4563
4564 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4565 = is_parameter_pack;
4566 }
4567 else
4568 {
4569 tree t;
4570 parm = TREE_VALUE (TREE_VALUE (parm));
4571
4572 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4573 {
4574 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4575 /* This is for distinguishing between real templates and template
4576 template parameters */
4577 TREE_TYPE (parm) = t;
4578 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4579 decl = parm;
4580 }
4581 else
4582 {
4583 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4584 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4585 decl = build_decl (parm_loc,
4586 TYPE_DECL, parm, t);
4587 }
4588
4589 TYPE_NAME (t) = decl;
4590 TYPE_STUB_DECL (t) = decl;
4591 parm = decl;
4592 TEMPLATE_TYPE_PARM_INDEX (t)
4593 = build_template_parm_index (idx, processing_template_decl,
4594 processing_template_decl,
4595 decl, TREE_TYPE (parm));
4596 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4597 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4598 SET_TYPE_STRUCTURAL_EQUALITY (t);
4599 else
4600 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4601 }
4602 DECL_ARTIFICIAL (decl) = 1;
4603 SET_DECL_TEMPLATE_PARM_P (decl);
4604
4605 /* Build requirements for the type/template parameter.
4606 This must be done after SET_DECL_TEMPLATE_PARM_P or
4607 process_template_parm could fail. */
4608 tree reqs = finish_shorthand_constraint (parm, constr);
4609
4610 decl = pushdecl (decl);
4611 if (!is_non_type)
4612 parm = decl;
4613
4614 /* Build the parameter node linking the parameter declaration,
4615 its default argument (if any), and its constraints (if any). */
4616 parm = build_tree_list (defval, parm);
4617 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4618
4619 if (prev)
4620 TREE_CHAIN (prev) = parm;
4621 else
4622 list = parm;
4623
4624 return list;
4625 }
4626
4627 /* The end of a template parameter list has been reached. Process the
4628 tree list into a parameter vector, converting each parameter into a more
4629 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4630 as PARM_DECLs. */
4631
4632 tree
4633 end_template_parm_list (tree parms)
4634 {
4635 tree saved_parmlist = make_tree_vec (list_length (parms));
4636
4637 /* Pop the dummy parameter level and add the real one. We do not
4638 morph the dummy parameter in place, as it might have been
4639 captured by a (nested) template-template-parm. */
4640 current_template_parms = TREE_CHAIN (current_template_parms);
4641
4642 current_template_parms
4643 = tree_cons (size_int (processing_template_decl),
4644 saved_parmlist, current_template_parms);
4645
4646 for (unsigned ix = 0; parms; ix++)
4647 {
4648 tree parm = parms;
4649 parms = TREE_CHAIN (parms);
4650 TREE_CHAIN (parm) = NULL_TREE;
4651
4652 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4653 }
4654
4655 --processing_template_parmlist;
4656
4657 return saved_parmlist;
4658 }
4659
4660 // Explicitly indicate the end of the template parameter list. We assume
4661 // that the current template parameters have been constructed and/or
4662 // managed explicitly, as when creating new template template parameters
4663 // from a shorthand constraint.
4664 void
4665 end_template_parm_list ()
4666 {
4667 --processing_template_parmlist;
4668 }
4669
4670 /* end_template_decl is called after a template declaration is seen. */
4671
4672 void
4673 end_template_decl (void)
4674 {
4675 reset_specialization ();
4676
4677 if (! processing_template_decl)
4678 return;
4679
4680 /* This matches the pushlevel in begin_template_parm_list. */
4681 finish_scope ();
4682
4683 --processing_template_decl;
4684 current_template_parms = TREE_CHAIN (current_template_parms);
4685 }
4686
4687 /* Takes a TREE_LIST representing a template parameter and convert it
4688 into an argument suitable to be passed to the type substitution
4689 functions. Note that If the TREE_LIST contains an error_mark
4690 node, the returned argument is error_mark_node. */
4691
4692 tree
4693 template_parm_to_arg (tree t)
4694 {
4695
4696 if (t == NULL_TREE
4697 || TREE_CODE (t) != TREE_LIST)
4698 return t;
4699
4700 if (error_operand_p (TREE_VALUE (t)))
4701 return error_mark_node;
4702
4703 t = TREE_VALUE (t);
4704
4705 if (TREE_CODE (t) == TYPE_DECL
4706 || TREE_CODE (t) == TEMPLATE_DECL)
4707 {
4708 t = TREE_TYPE (t);
4709
4710 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4711 {
4712 /* Turn this argument into a TYPE_ARGUMENT_PACK
4713 with a single element, which expands T. */
4714 tree vec = make_tree_vec (1);
4715 if (CHECKING_P)
4716 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4717
4718 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4719
4720 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4721 SET_ARGUMENT_PACK_ARGS (t, vec);
4722 }
4723 }
4724 else
4725 {
4726 t = DECL_INITIAL (t);
4727
4728 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4729 {
4730 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4731 with a single element, which expands T. */
4732 tree vec = make_tree_vec (1);
4733 if (CHECKING_P)
4734 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4735
4736 t = convert_from_reference (t);
4737 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4738
4739 t = make_node (NONTYPE_ARGUMENT_PACK);
4740 SET_ARGUMENT_PACK_ARGS (t, vec);
4741 }
4742 else
4743 t = convert_from_reference (t);
4744 }
4745 return t;
4746 }
4747
4748 /* Given a single level of template parameters (a TREE_VEC), return it
4749 as a set of template arguments. */
4750
4751 tree
4752 template_parms_level_to_args (tree parms)
4753 {
4754 tree a = copy_node (parms);
4755 TREE_TYPE (a) = NULL_TREE;
4756 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4757 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4758
4759 if (CHECKING_P)
4760 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4761
4762 return a;
4763 }
4764
4765 /* Given a set of template parameters, return them as a set of template
4766 arguments. The template parameters are represented as a TREE_VEC, in
4767 the form documented in cp-tree.h for template arguments. */
4768
4769 tree
4770 template_parms_to_args (tree parms)
4771 {
4772 tree header;
4773 tree args = NULL_TREE;
4774 int length = TMPL_PARMS_DEPTH (parms);
4775 int l = length;
4776
4777 /* If there is only one level of template parameters, we do not
4778 create a TREE_VEC of TREE_VECs. Instead, we return a single
4779 TREE_VEC containing the arguments. */
4780 if (length > 1)
4781 args = make_tree_vec (length);
4782
4783 for (header = parms; header; header = TREE_CHAIN (header))
4784 {
4785 tree a = template_parms_level_to_args (TREE_VALUE (header));
4786
4787 if (length > 1)
4788 TREE_VEC_ELT (args, --l) = a;
4789 else
4790 args = a;
4791 }
4792
4793 return args;
4794 }
4795
4796 /* Within the declaration of a template, return the currently active
4797 template parameters as an argument TREE_VEC. */
4798
4799 static tree
4800 current_template_args (void)
4801 {
4802 return template_parms_to_args (current_template_parms);
4803 }
4804
4805 /* Return the fully generic arguments for of TMPL, i.e. what
4806 current_template_args would be while parsing it. */
4807
4808 tree
4809 generic_targs_for (tree tmpl)
4810 {
4811 if (tmpl == NULL_TREE)
4812 return NULL_TREE;
4813 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4814 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4815 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4816 template parameter, it has no TEMPLATE_INFO; for a partial
4817 specialization, it has the arguments for the primary template, and we
4818 want the arguments for the partial specialization. */;
4819 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4820 if (tree ti = get_template_info (result))
4821 return TI_ARGS (ti);
4822 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4823 }
4824
4825 /* Update the declared TYPE by doing any lookups which were thought to be
4826 dependent, but are not now that we know the SCOPE of the declarator. */
4827
4828 tree
4829 maybe_update_decl_type (tree orig_type, tree scope)
4830 {
4831 tree type = orig_type;
4832
4833 if (type == NULL_TREE)
4834 return type;
4835
4836 if (TREE_CODE (orig_type) == TYPE_DECL)
4837 type = TREE_TYPE (type);
4838
4839 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4840 && dependent_type_p (type)
4841 /* Don't bother building up the args in this case. */
4842 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4843 {
4844 /* tsubst in the args corresponding to the template parameters,
4845 including auto if present. Most things will be unchanged, but
4846 make_typename_type and tsubst_qualified_id will resolve
4847 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4848 tree args = current_template_args ();
4849 tree auto_node = type_uses_auto (type);
4850 tree pushed;
4851 if (auto_node)
4852 {
4853 tree auto_vec = make_tree_vec (1);
4854 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4855 args = add_to_template_args (args, auto_vec);
4856 }
4857 pushed = push_scope (scope);
4858 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4859 if (pushed)
4860 pop_scope (scope);
4861 }
4862
4863 if (type == error_mark_node)
4864 return orig_type;
4865
4866 if (TREE_CODE (orig_type) == TYPE_DECL)
4867 {
4868 if (same_type_p (type, TREE_TYPE (orig_type)))
4869 type = orig_type;
4870 else
4871 type = TYPE_NAME (type);
4872 }
4873 return type;
4874 }
4875
4876 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4877 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4878 the new template is a member template. */
4879
4880 static tree
4881 build_template_decl (tree decl, tree parms, bool member_template_p)
4882 {
4883 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4884 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4885 DECL_TEMPLATE_PARMS (tmpl) = parms;
4886 DECL_TEMPLATE_RESULT (tmpl) = decl;
4887 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4888 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4889 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4890 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4891
4892 return tmpl;
4893 }
4894
4895 struct template_parm_data
4896 {
4897 /* The level of the template parameters we are currently
4898 processing. */
4899 int level;
4900
4901 /* The index of the specialization argument we are currently
4902 processing. */
4903 int current_arg;
4904
4905 /* An array whose size is the number of template parameters. The
4906 elements are nonzero if the parameter has been used in any one
4907 of the arguments processed so far. */
4908 int* parms;
4909
4910 /* An array whose size is the number of template arguments. The
4911 elements are nonzero if the argument makes use of template
4912 parameters of this level. */
4913 int* arg_uses_template_parms;
4914 };
4915
4916 /* Subroutine of push_template_decl used to see if each template
4917 parameter in a partial specialization is used in the explicit
4918 argument list. If T is of the LEVEL given in DATA (which is
4919 treated as a template_parm_data*), then DATA->PARMS is marked
4920 appropriately. */
4921
4922 static int
4923 mark_template_parm (tree t, void* data)
4924 {
4925 int level;
4926 int idx;
4927 struct template_parm_data* tpd = (struct template_parm_data*) data;
4928
4929 template_parm_level_and_index (t, &level, &idx);
4930
4931 if (level == tpd->level)
4932 {
4933 tpd->parms[idx] = 1;
4934 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4935 }
4936
4937 /* In C++17 the type of a non-type argument is a deduced context. */
4938 if (cxx_dialect >= cxx17
4939 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4940 for_each_template_parm (TREE_TYPE (t),
4941 &mark_template_parm,
4942 data,
4943 NULL,
4944 /*include_nondeduced_p=*/false);
4945
4946 /* Return zero so that for_each_template_parm will continue the
4947 traversal of the tree; we want to mark *every* template parm. */
4948 return 0;
4949 }
4950
4951 /* Process the partial specialization DECL. */
4952
4953 static tree
4954 process_partial_specialization (tree decl)
4955 {
4956 tree type = TREE_TYPE (decl);
4957 tree tinfo = get_template_info (decl);
4958 tree maintmpl = TI_TEMPLATE (tinfo);
4959 tree specargs = TI_ARGS (tinfo);
4960 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4961 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4962 tree inner_parms;
4963 tree inst;
4964 int nargs = TREE_VEC_LENGTH (inner_args);
4965 int ntparms;
4966 int i;
4967 bool did_error_intro = false;
4968 struct template_parm_data tpd;
4969 struct template_parm_data tpd2;
4970
4971 gcc_assert (current_template_parms);
4972
4973 /* A concept cannot be specialized. */
4974 if (flag_concepts && variable_concept_p (maintmpl))
4975 {
4976 error ("specialization of variable concept %q#D", maintmpl);
4977 return error_mark_node;
4978 }
4979
4980 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4981 ntparms = TREE_VEC_LENGTH (inner_parms);
4982
4983 /* We check that each of the template parameters given in the
4984 partial specialization is used in the argument list to the
4985 specialization. For example:
4986
4987 template <class T> struct S;
4988 template <class T> struct S<T*>;
4989
4990 The second declaration is OK because `T*' uses the template
4991 parameter T, whereas
4992
4993 template <class T> struct S<int>;
4994
4995 is no good. Even trickier is:
4996
4997 template <class T>
4998 struct S1
4999 {
5000 template <class U>
5001 struct S2;
5002 template <class U>
5003 struct S2<T>;
5004 };
5005
5006 The S2<T> declaration is actually invalid; it is a
5007 full-specialization. Of course,
5008
5009 template <class U>
5010 struct S2<T (*)(U)>;
5011
5012 or some such would have been OK. */
5013 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5014 tpd.parms = XALLOCAVEC (int, ntparms);
5015 memset (tpd.parms, 0, sizeof (int) * ntparms);
5016
5017 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5018 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5019 for (i = 0; i < nargs; ++i)
5020 {
5021 tpd.current_arg = i;
5022 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5023 &mark_template_parm,
5024 &tpd,
5025 NULL,
5026 /*include_nondeduced_p=*/false);
5027 }
5028 for (i = 0; i < ntparms; ++i)
5029 if (tpd.parms[i] == 0)
5030 {
5031 /* One of the template parms was not used in a deduced context in the
5032 specialization. */
5033 if (!did_error_intro)
5034 {
5035 error ("template parameters not deducible in "
5036 "partial specialization:");
5037 did_error_intro = true;
5038 }
5039
5040 inform (input_location, " %qD",
5041 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5042 }
5043
5044 if (did_error_intro)
5045 return error_mark_node;
5046
5047 /* [temp.class.spec]
5048
5049 The argument list of the specialization shall not be identical to
5050 the implicit argument list of the primary template. */
5051 tree main_args
5052 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5053 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5054 && (!flag_concepts
5055 || !strictly_subsumes (current_template_constraints (),
5056 inner_args, maintmpl)))
5057 {
5058 if (!flag_concepts)
5059 error ("partial specialization %q+D does not specialize "
5060 "any template arguments; to define the primary template, "
5061 "remove the template argument list", decl);
5062 else
5063 error ("partial specialization %q+D does not specialize any "
5064 "template arguments and is not more constrained than "
5065 "the primary template; to define the primary template, "
5066 "remove the template argument list", decl);
5067 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5068 }
5069
5070 /* A partial specialization that replaces multiple parameters of the
5071 primary template with a pack expansion is less specialized for those
5072 parameters. */
5073 if (nargs < DECL_NTPARMS (maintmpl))
5074 {
5075 error ("partial specialization is not more specialized than the "
5076 "primary template because it replaces multiple parameters "
5077 "with a pack expansion");
5078 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5079 /* Avoid crash in process_partial_specialization. */
5080 return decl;
5081 }
5082
5083 else if (nargs > DECL_NTPARMS (maintmpl))
5084 {
5085 error ("too many arguments for partial specialization %qT", type);
5086 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5087 /* Avoid crash below. */
5088 return decl;
5089 }
5090
5091 /* If we aren't in a dependent class, we can actually try deduction. */
5092 else if (tpd.level == 1
5093 /* FIXME we should be able to handle a partial specialization of a
5094 partial instantiation, but currently we can't (c++/41727). */
5095 && TMPL_ARGS_DEPTH (specargs) == 1
5096 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5097 {
5098 auto_diagnostic_group d;
5099 if (permerror (input_location, "partial specialization %qD is not "
5100 "more specialized than", decl))
5101 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5102 maintmpl);
5103 }
5104
5105 /* [temp.class.spec]
5106
5107 A partially specialized non-type argument expression shall not
5108 involve template parameters of the partial specialization except
5109 when the argument expression is a simple identifier.
5110
5111 The type of a template parameter corresponding to a specialized
5112 non-type argument shall not be dependent on a parameter of the
5113 specialization.
5114
5115 Also, we verify that pack expansions only occur at the
5116 end of the argument list. */
5117 tpd2.parms = 0;
5118 for (i = 0; i < nargs; ++i)
5119 {
5120 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5121 tree arg = TREE_VEC_ELT (inner_args, i);
5122 tree packed_args = NULL_TREE;
5123 int j, len = 1;
5124
5125 if (ARGUMENT_PACK_P (arg))
5126 {
5127 /* Extract the arguments from the argument pack. We'll be
5128 iterating over these in the following loop. */
5129 packed_args = ARGUMENT_PACK_ARGS (arg);
5130 len = TREE_VEC_LENGTH (packed_args);
5131 }
5132
5133 for (j = 0; j < len; j++)
5134 {
5135 if (packed_args)
5136 /* Get the Jth argument in the parameter pack. */
5137 arg = TREE_VEC_ELT (packed_args, j);
5138
5139 if (PACK_EXPANSION_P (arg))
5140 {
5141 /* Pack expansions must come at the end of the
5142 argument list. */
5143 if ((packed_args && j < len - 1)
5144 || (!packed_args && i < nargs - 1))
5145 {
5146 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5147 error ("parameter pack argument %qE must be at the "
5148 "end of the template argument list", arg);
5149 else
5150 error ("parameter pack argument %qT must be at the "
5151 "end of the template argument list", arg);
5152 }
5153 }
5154
5155 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5156 /* We only care about the pattern. */
5157 arg = PACK_EXPANSION_PATTERN (arg);
5158
5159 if (/* These first two lines are the `non-type' bit. */
5160 !TYPE_P (arg)
5161 && TREE_CODE (arg) != TEMPLATE_DECL
5162 /* This next two lines are the `argument expression is not just a
5163 simple identifier' condition and also the `specialized
5164 non-type argument' bit. */
5165 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5166 && !((REFERENCE_REF_P (arg)
5167 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5168 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5169 {
5170 if ((!packed_args && tpd.arg_uses_template_parms[i])
5171 || (packed_args && uses_template_parms (arg)))
5172 error_at (cp_expr_loc_or_input_loc (arg),
5173 "template argument %qE involves template "
5174 "parameter(s)", arg);
5175 else
5176 {
5177 /* Look at the corresponding template parameter,
5178 marking which template parameters its type depends
5179 upon. */
5180 tree type = TREE_TYPE (parm);
5181
5182 if (!tpd2.parms)
5183 {
5184 /* We haven't yet initialized TPD2. Do so now. */
5185 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5186 /* The number of parameters here is the number in the
5187 main template, which, as checked in the assertion
5188 above, is NARGS. */
5189 tpd2.parms = XALLOCAVEC (int, nargs);
5190 tpd2.level =
5191 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5192 }
5193
5194 /* Mark the template parameters. But this time, we're
5195 looking for the template parameters of the main
5196 template, not in the specialization. */
5197 tpd2.current_arg = i;
5198 tpd2.arg_uses_template_parms[i] = 0;
5199 memset (tpd2.parms, 0, sizeof (int) * nargs);
5200 for_each_template_parm (type,
5201 &mark_template_parm,
5202 &tpd2,
5203 NULL,
5204 /*include_nondeduced_p=*/false);
5205
5206 if (tpd2.arg_uses_template_parms [i])
5207 {
5208 /* The type depended on some template parameters.
5209 If they are fully specialized in the
5210 specialization, that's OK. */
5211 int j;
5212 int count = 0;
5213 for (j = 0; j < nargs; ++j)
5214 if (tpd2.parms[j] != 0
5215 && tpd.arg_uses_template_parms [j])
5216 ++count;
5217 if (count != 0)
5218 error_n (input_location, count,
5219 "type %qT of template argument %qE depends "
5220 "on a template parameter",
5221 "type %qT of template argument %qE depends "
5222 "on template parameters",
5223 type,
5224 arg);
5225 }
5226 }
5227 }
5228 }
5229 }
5230
5231 /* We should only get here once. */
5232 if (TREE_CODE (decl) == TYPE_DECL)
5233 gcc_assert (!COMPLETE_TYPE_P (type));
5234
5235 // Build the template decl.
5236 tree tmpl = build_template_decl (decl, current_template_parms,
5237 DECL_MEMBER_TEMPLATE_P (maintmpl));
5238 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5239 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5240 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5241
5242 /* Give template template parms a DECL_CONTEXT of the template
5243 for which they are a parameter. */
5244 for (i = 0; i < ntparms; ++i)
5245 {
5246 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5247 if (TREE_CODE (parm) == TEMPLATE_DECL)
5248 DECL_CONTEXT (parm) = tmpl;
5249 }
5250
5251 if (VAR_P (decl))
5252 /* We didn't register this in check_explicit_specialization so we could
5253 wait until the constraints were set. */
5254 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5255 else
5256 associate_classtype_constraints (type);
5257
5258 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5259 = tree_cons (specargs, tmpl,
5260 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5261 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5262
5263 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5264 inst = TREE_CHAIN (inst))
5265 {
5266 tree instance = TREE_VALUE (inst);
5267 if (TYPE_P (instance)
5268 ? (COMPLETE_TYPE_P (instance)
5269 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5270 : DECL_TEMPLATE_INSTANTIATION (instance))
5271 {
5272 tree spec = most_specialized_partial_spec (instance, tf_none);
5273 tree inst_decl = (DECL_P (instance)
5274 ? instance : TYPE_NAME (instance));
5275 if (!spec)
5276 /* OK */;
5277 else if (spec == error_mark_node)
5278 permerror (input_location,
5279 "declaration of %qD ambiguates earlier template "
5280 "instantiation for %qD", decl, inst_decl);
5281 else if (TREE_VALUE (spec) == tmpl)
5282 permerror (input_location,
5283 "partial specialization of %qD after instantiation "
5284 "of %qD", decl, inst_decl);
5285 }
5286 }
5287
5288 return decl;
5289 }
5290
5291 /* PARM is a template parameter of some form; return the corresponding
5292 TEMPLATE_PARM_INDEX. */
5293
5294 static tree
5295 get_template_parm_index (tree parm)
5296 {
5297 if (TREE_CODE (parm) == PARM_DECL
5298 || TREE_CODE (parm) == CONST_DECL)
5299 parm = DECL_INITIAL (parm);
5300 else if (TREE_CODE (parm) == TYPE_DECL
5301 || TREE_CODE (parm) == TEMPLATE_DECL)
5302 parm = TREE_TYPE (parm);
5303 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5304 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5305 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5306 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5307 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5308 return parm;
5309 }
5310
5311 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5312 parameter packs used by the template parameter PARM. */
5313
5314 static void
5315 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5316 {
5317 /* A type parm can't refer to another parm. */
5318 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5319 return;
5320 else if (TREE_CODE (parm) == PARM_DECL)
5321 {
5322 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5323 ppd, ppd->visited);
5324 return;
5325 }
5326
5327 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5328
5329 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5330 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5331 {
5332 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5333 if (template_parameter_pack_p (p))
5334 /* Any packs in the type are expanded by this parameter. */;
5335 else
5336 fixed_parameter_pack_p_1 (p, ppd);
5337 }
5338 }
5339
5340 /* PARM is a template parameter pack. Return any parameter packs used in
5341 its type or the type of any of its template parameters. If there are
5342 any such packs, it will be instantiated into a fixed template parameter
5343 list by partial instantiation rather than be fully deduced. */
5344
5345 tree
5346 fixed_parameter_pack_p (tree parm)
5347 {
5348 /* This can only be true in a member template. */
5349 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5350 return NULL_TREE;
5351 /* This can only be true for a parameter pack. */
5352 if (!template_parameter_pack_p (parm))
5353 return NULL_TREE;
5354 /* A type parm can't refer to another parm. */
5355 if (TREE_CODE (parm) == TYPE_DECL)
5356 return NULL_TREE;
5357
5358 tree parameter_packs = NULL_TREE;
5359 struct find_parameter_pack_data ppd;
5360 ppd.parameter_packs = &parameter_packs;
5361 ppd.visited = new hash_set<tree>;
5362 ppd.type_pack_expansion_p = false;
5363
5364 fixed_parameter_pack_p_1 (parm, &ppd);
5365
5366 delete ppd.visited;
5367 return parameter_packs;
5368 }
5369
5370 /* Check that a template declaration's use of default arguments and
5371 parameter packs is not invalid. Here, PARMS are the template
5372 parameters. IS_PRIMARY is true if DECL is the thing declared by
5373 a primary template. IS_PARTIAL is true if DECL is a partial
5374 specialization.
5375
5376 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5377 function template declaration or a friend class template
5378 declaration. In the function case, 1 indicates a declaration, 2
5379 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5380 emitted for extraneous default arguments.
5381
5382 Returns TRUE if there were no errors found, FALSE otherwise. */
5383
5384 bool
5385 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5386 bool is_partial, int is_friend_decl)
5387 {
5388 const char *msg;
5389 int last_level_to_check;
5390 tree parm_level;
5391 bool no_errors = true;
5392
5393 /* [temp.param]
5394
5395 A default template-argument shall not be specified in a
5396 function template declaration or a function template definition, nor
5397 in the template-parameter-list of the definition of a member of a
5398 class template. */
5399
5400 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5401 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5402 /* You can't have a function template declaration in a local
5403 scope, nor you can you define a member of a class template in a
5404 local scope. */
5405 return true;
5406
5407 if ((TREE_CODE (decl) == TYPE_DECL
5408 && TREE_TYPE (decl)
5409 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5410 || (TREE_CODE (decl) == FUNCTION_DECL
5411 && LAMBDA_FUNCTION_P (decl)))
5412 /* A lambda doesn't have an explicit declaration; don't complain
5413 about the parms of the enclosing class. */
5414 return true;
5415
5416 if (current_class_type
5417 && !TYPE_BEING_DEFINED (current_class_type)
5418 && DECL_LANG_SPECIFIC (decl)
5419 && DECL_DECLARES_FUNCTION_P (decl)
5420 /* If this is either a friend defined in the scope of the class
5421 or a member function. */
5422 && (DECL_FUNCTION_MEMBER_P (decl)
5423 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5424 : DECL_FRIEND_CONTEXT (decl)
5425 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5426 : false)
5427 /* And, if it was a member function, it really was defined in
5428 the scope of the class. */
5429 && (!DECL_FUNCTION_MEMBER_P (decl)
5430 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5431 /* We already checked these parameters when the template was
5432 declared, so there's no need to do it again now. This function
5433 was defined in class scope, but we're processing its body now
5434 that the class is complete. */
5435 return true;
5436
5437 /* Core issue 226 (C++0x only): the following only applies to class
5438 templates. */
5439 if (is_primary
5440 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5441 {
5442 /* [temp.param]
5443
5444 If a template-parameter has a default template-argument, all
5445 subsequent template-parameters shall have a default
5446 template-argument supplied. */
5447 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5448 {
5449 tree inner_parms = TREE_VALUE (parm_level);
5450 int ntparms = TREE_VEC_LENGTH (inner_parms);
5451 int seen_def_arg_p = 0;
5452 int i;
5453
5454 for (i = 0; i < ntparms; ++i)
5455 {
5456 tree parm = TREE_VEC_ELT (inner_parms, i);
5457
5458 if (parm == error_mark_node)
5459 continue;
5460
5461 if (TREE_PURPOSE (parm))
5462 seen_def_arg_p = 1;
5463 else if (seen_def_arg_p
5464 && !template_parameter_pack_p (TREE_VALUE (parm)))
5465 {
5466 error ("no default argument for %qD", TREE_VALUE (parm));
5467 /* For better subsequent error-recovery, we indicate that
5468 there should have been a default argument. */
5469 TREE_PURPOSE (parm) = error_mark_node;
5470 no_errors = false;
5471 }
5472 else if (!is_partial
5473 && !is_friend_decl
5474 /* Don't complain about an enclosing partial
5475 specialization. */
5476 && parm_level == parms
5477 && TREE_CODE (decl) == TYPE_DECL
5478 && i < ntparms - 1
5479 && template_parameter_pack_p (TREE_VALUE (parm))
5480 /* A fixed parameter pack will be partially
5481 instantiated into a fixed length list. */
5482 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5483 {
5484 /* A primary class template can only have one
5485 parameter pack, at the end of the template
5486 parameter list. */
5487
5488 error ("parameter pack %q+D must be at the end of the"
5489 " template parameter list", TREE_VALUE (parm));
5490
5491 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5492 = error_mark_node;
5493 no_errors = false;
5494 }
5495 }
5496 }
5497 }
5498
5499 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5500 || is_partial
5501 || !is_primary
5502 || is_friend_decl)
5503 /* For an ordinary class template, default template arguments are
5504 allowed at the innermost level, e.g.:
5505 template <class T = int>
5506 struct S {};
5507 but, in a partial specialization, they're not allowed even
5508 there, as we have in [temp.class.spec]:
5509
5510 The template parameter list of a specialization shall not
5511 contain default template argument values.
5512
5513 So, for a partial specialization, or for a function template
5514 (in C++98/C++03), we look at all of them. */
5515 ;
5516 else
5517 /* But, for a primary class template that is not a partial
5518 specialization we look at all template parameters except the
5519 innermost ones. */
5520 parms = TREE_CHAIN (parms);
5521
5522 /* Figure out what error message to issue. */
5523 if (is_friend_decl == 2)
5524 msg = G_("default template arguments may not be used in function template "
5525 "friend re-declaration");
5526 else if (is_friend_decl)
5527 msg = G_("default template arguments may not be used in template "
5528 "friend declarations");
5529 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5530 msg = G_("default template arguments may not be used in function templates "
5531 "without %<-std=c++11%> or %<-std=gnu++11%>");
5532 else if (is_partial)
5533 msg = G_("default template arguments may not be used in "
5534 "partial specializations");
5535 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5536 msg = G_("default argument for template parameter for class enclosing %qD");
5537 else
5538 /* Per [temp.param]/9, "A default template-argument shall not be
5539 specified in the template-parameter-lists of the definition of
5540 a member of a class template that appears outside of the member's
5541 class.", thus if we aren't handling a member of a class template
5542 there is no need to examine the parameters. */
5543 return true;
5544
5545 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5546 /* If we're inside a class definition, there's no need to
5547 examine the parameters to the class itself. On the one
5548 hand, they will be checked when the class is defined, and,
5549 on the other, default arguments are valid in things like:
5550 template <class T = double>
5551 struct S { template <class U> void f(U); };
5552 Here the default argument for `S' has no bearing on the
5553 declaration of `f'. */
5554 last_level_to_check = template_class_depth (current_class_type) + 1;
5555 else
5556 /* Check everything. */
5557 last_level_to_check = 0;
5558
5559 for (parm_level = parms;
5560 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5561 parm_level = TREE_CHAIN (parm_level))
5562 {
5563 tree inner_parms = TREE_VALUE (parm_level);
5564 int i;
5565 int ntparms;
5566
5567 ntparms = TREE_VEC_LENGTH (inner_parms);
5568 for (i = 0; i < ntparms; ++i)
5569 {
5570 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5571 continue;
5572
5573 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5574 {
5575 if (msg)
5576 {
5577 no_errors = false;
5578 if (is_friend_decl == 2)
5579 return no_errors;
5580
5581 error (msg, decl);
5582 msg = 0;
5583 }
5584
5585 /* Clear out the default argument so that we are not
5586 confused later. */
5587 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5588 }
5589 }
5590
5591 /* At this point, if we're still interested in issuing messages,
5592 they must apply to classes surrounding the object declared. */
5593 if (msg)
5594 msg = G_("default argument for template parameter for class "
5595 "enclosing %qD");
5596 }
5597
5598 return no_errors;
5599 }
5600
5601 /* Worker for push_template_decl_real, called via
5602 for_each_template_parm. DATA is really an int, indicating the
5603 level of the parameters we are interested in. If T is a template
5604 parameter of that level, return nonzero. */
5605
5606 static int
5607 template_parm_this_level_p (tree t, void* data)
5608 {
5609 int this_level = *(int *)data;
5610 int level;
5611
5612 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5613 level = TEMPLATE_PARM_LEVEL (t);
5614 else
5615 level = TEMPLATE_TYPE_LEVEL (t);
5616 return level == this_level;
5617 }
5618
5619 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5620 DATA is really an int, indicating the innermost outer level of parameters.
5621 If T is a template parameter of that level or further out, return
5622 nonzero. */
5623
5624 static int
5625 template_parm_outer_level (tree t, void *data)
5626 {
5627 int this_level = *(int *)data;
5628 int level;
5629
5630 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5631 level = TEMPLATE_PARM_LEVEL (t);
5632 else
5633 level = TEMPLATE_TYPE_LEVEL (t);
5634 return level <= this_level;
5635 }
5636
5637 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5638 parameters given by current_template_args, or reuses a
5639 previously existing one, if appropriate. Returns the DECL, or an
5640 equivalent one, if it is replaced via a call to duplicate_decls.
5641
5642 If IS_FRIEND is true, DECL is a friend declaration. */
5643
5644 tree
5645 push_template_decl_real (tree decl, bool is_friend)
5646 {
5647 tree tmpl;
5648 tree args;
5649 tree info;
5650 tree ctx;
5651 bool is_primary;
5652 bool is_partial;
5653 int new_template_p = 0;
5654 /* True if the template is a member template, in the sense of
5655 [temp.mem]. */
5656 bool member_template_p = false;
5657
5658 if (decl == error_mark_node || !current_template_parms)
5659 return error_mark_node;
5660
5661 /* See if this is a partial specialization. */
5662 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5663 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5664 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5665 || (VAR_P (decl)
5666 && DECL_LANG_SPECIFIC (decl)
5667 && DECL_TEMPLATE_SPECIALIZATION (decl)
5668 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5669
5670 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5671 is_friend = true;
5672
5673 if (is_friend)
5674 /* For a friend, we want the context of the friend, not
5675 the type of which it is a friend. */
5676 ctx = CP_DECL_CONTEXT (decl);
5677 else if (CP_DECL_CONTEXT (decl)
5678 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5679 /* In the case of a virtual function, we want the class in which
5680 it is defined. */
5681 ctx = CP_DECL_CONTEXT (decl);
5682 else
5683 /* Otherwise, if we're currently defining some class, the DECL
5684 is assumed to be a member of the class. */
5685 ctx = current_scope ();
5686
5687 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5688 ctx = NULL_TREE;
5689
5690 if (!DECL_CONTEXT (decl))
5691 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5692
5693 /* See if this is a primary template. */
5694 if (is_friend && ctx
5695 && uses_template_parms_level (ctx, processing_template_decl))
5696 /* A friend template that specifies a class context, i.e.
5697 template <typename T> friend void A<T>::f();
5698 is not primary. */
5699 is_primary = false;
5700 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5701 is_primary = false;
5702 else
5703 is_primary = template_parm_scope_p ();
5704
5705 if (is_primary)
5706 {
5707 warning (OPT_Wtemplates, "template %qD declared", decl);
5708
5709 if (DECL_CLASS_SCOPE_P (decl))
5710 member_template_p = true;
5711 if (TREE_CODE (decl) == TYPE_DECL
5712 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5713 {
5714 error ("template class without a name");
5715 return error_mark_node;
5716 }
5717 else if (TREE_CODE (decl) == FUNCTION_DECL)
5718 {
5719 if (member_template_p)
5720 {
5721 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5722 error ("member template %qD may not have virt-specifiers", decl);
5723 }
5724 if (DECL_DESTRUCTOR_P (decl))
5725 {
5726 /* [temp.mem]
5727
5728 A destructor shall not be a member template. */
5729 error_at (DECL_SOURCE_LOCATION (decl),
5730 "destructor %qD declared as member template", decl);
5731 return error_mark_node;
5732 }
5733 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5734 && (!prototype_p (TREE_TYPE (decl))
5735 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5736 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5737 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5738 == void_list_node)))
5739 {
5740 /* [basic.stc.dynamic.allocation]
5741
5742 An allocation function can be a function
5743 template. ... Template allocation functions shall
5744 have two or more parameters. */
5745 error ("invalid template declaration of %qD", decl);
5746 return error_mark_node;
5747 }
5748 }
5749 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5750 && CLASS_TYPE_P (TREE_TYPE (decl)))
5751 {
5752 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5753 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5754 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5755 {
5756 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5757 if (TREE_CODE (t) == TYPE_DECL)
5758 t = TREE_TYPE (t);
5759 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5760 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5761 }
5762 }
5763 else if (TREE_CODE (decl) == TYPE_DECL
5764 && TYPE_DECL_ALIAS_P (decl))
5765 /* alias-declaration */
5766 gcc_assert (!DECL_ARTIFICIAL (decl));
5767 else if (VAR_P (decl))
5768 /* C++14 variable template. */;
5769 else if (TREE_CODE (decl) == CONCEPT_DECL)
5770 /* C++20 concept definitions. */;
5771 else
5772 {
5773 error ("template declaration of %q#D", decl);
5774 return error_mark_node;
5775 }
5776 }
5777
5778 /* Check to see that the rules regarding the use of default
5779 arguments are not being violated. We check args for a friend
5780 functions when we know whether it's a definition, introducing
5781 declaration or re-declaration. */
5782 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5783 check_default_tmpl_args (decl, current_template_parms,
5784 is_primary, is_partial, is_friend);
5785
5786 /* Ensure that there are no parameter packs in the type of this
5787 declaration that have not been expanded. */
5788 if (TREE_CODE (decl) == FUNCTION_DECL)
5789 {
5790 /* Check each of the arguments individually to see if there are
5791 any bare parameter packs. */
5792 tree type = TREE_TYPE (decl);
5793 tree arg = DECL_ARGUMENTS (decl);
5794 tree argtype = TYPE_ARG_TYPES (type);
5795
5796 while (arg && argtype)
5797 {
5798 if (!DECL_PACK_P (arg)
5799 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5800 {
5801 /* This is a PARM_DECL that contains unexpanded parameter
5802 packs. We have already complained about this in the
5803 check_for_bare_parameter_packs call, so just replace
5804 these types with ERROR_MARK_NODE. */
5805 TREE_TYPE (arg) = error_mark_node;
5806 TREE_VALUE (argtype) = error_mark_node;
5807 }
5808
5809 arg = DECL_CHAIN (arg);
5810 argtype = TREE_CHAIN (argtype);
5811 }
5812
5813 /* Check for bare parameter packs in the return type and the
5814 exception specifiers. */
5815 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5816 /* Errors were already issued, set return type to int
5817 as the frontend doesn't expect error_mark_node as
5818 the return type. */
5819 TREE_TYPE (type) = integer_type_node;
5820 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5821 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5822 }
5823 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5824 ? DECL_ORIGINAL_TYPE (decl)
5825 : TREE_TYPE (decl)))
5826 {
5827 TREE_TYPE (decl) = error_mark_node;
5828 return error_mark_node;
5829 }
5830
5831 if (is_partial)
5832 return process_partial_specialization (decl);
5833
5834 args = current_template_args ();
5835
5836 if (!ctx
5837 || TREE_CODE (ctx) == FUNCTION_DECL
5838 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5839 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5840 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5841 {
5842 if (DECL_LANG_SPECIFIC (decl)
5843 && DECL_TEMPLATE_INFO (decl)
5844 && DECL_TI_TEMPLATE (decl))
5845 tmpl = DECL_TI_TEMPLATE (decl);
5846 /* If DECL is a TYPE_DECL for a class-template, then there won't
5847 be DECL_LANG_SPECIFIC. The information equivalent to
5848 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5849 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5850 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5851 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5852 {
5853 /* Since a template declaration already existed for this
5854 class-type, we must be redeclaring it here. Make sure
5855 that the redeclaration is valid. */
5856 redeclare_class_template (TREE_TYPE (decl),
5857 current_template_parms,
5858 current_template_constraints ());
5859 /* We don't need to create a new TEMPLATE_DECL; just use the
5860 one we already had. */
5861 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5862 }
5863 else
5864 {
5865 tmpl = build_template_decl (decl, current_template_parms,
5866 member_template_p);
5867 new_template_p = 1;
5868
5869 if (DECL_LANG_SPECIFIC (decl)
5870 && DECL_TEMPLATE_SPECIALIZATION (decl))
5871 {
5872 /* A specialization of a member template of a template
5873 class. */
5874 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5875 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5876 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5877 }
5878 }
5879 }
5880 else
5881 {
5882 tree a, t, current, parms;
5883 int i;
5884 tree tinfo = get_template_info (decl);
5885
5886 if (!tinfo)
5887 {
5888 error ("template definition of non-template %q#D", decl);
5889 return error_mark_node;
5890 }
5891
5892 tmpl = TI_TEMPLATE (tinfo);
5893
5894 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5895 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5896 && DECL_TEMPLATE_SPECIALIZATION (decl)
5897 && DECL_MEMBER_TEMPLATE_P (tmpl))
5898 {
5899 tree new_tmpl;
5900
5901 /* The declaration is a specialization of a member
5902 template, declared outside the class. Therefore, the
5903 innermost template arguments will be NULL, so we
5904 replace them with the arguments determined by the
5905 earlier call to check_explicit_specialization. */
5906 args = DECL_TI_ARGS (decl);
5907
5908 new_tmpl
5909 = build_template_decl (decl, current_template_parms,
5910 member_template_p);
5911 DECL_TI_TEMPLATE (decl) = new_tmpl;
5912 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5913 DECL_TEMPLATE_INFO (new_tmpl)
5914 = build_template_info (tmpl, args);
5915
5916 register_specialization (new_tmpl,
5917 most_general_template (tmpl),
5918 args,
5919 is_friend, 0);
5920 return decl;
5921 }
5922
5923 /* Make sure the template headers we got make sense. */
5924
5925 parms = DECL_TEMPLATE_PARMS (tmpl);
5926 i = TMPL_PARMS_DEPTH (parms);
5927 if (TMPL_ARGS_DEPTH (args) != i)
5928 {
5929 error ("expected %d levels of template parms for %q#D, got %d",
5930 i, decl, TMPL_ARGS_DEPTH (args));
5931 DECL_INTERFACE_KNOWN (decl) = 1;
5932 return error_mark_node;
5933 }
5934 else
5935 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5936 {
5937 a = TMPL_ARGS_LEVEL (args, i);
5938 t = INNERMOST_TEMPLATE_PARMS (parms);
5939
5940 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5941 {
5942 if (current == decl)
5943 error ("got %d template parameters for %q#D",
5944 TREE_VEC_LENGTH (a), decl);
5945 else
5946 error ("got %d template parameters for %q#T",
5947 TREE_VEC_LENGTH (a), current);
5948 error (" but %d required", TREE_VEC_LENGTH (t));
5949 /* Avoid crash in import_export_decl. */
5950 DECL_INTERFACE_KNOWN (decl) = 1;
5951 return error_mark_node;
5952 }
5953
5954 if (current == decl)
5955 current = ctx;
5956 else if (current == NULL_TREE)
5957 /* Can happen in erroneous input. */
5958 break;
5959 else
5960 current = get_containing_scope (current);
5961 }
5962
5963 /* Check that the parms are used in the appropriate qualifying scopes
5964 in the declarator. */
5965 if (!comp_template_args
5966 (TI_ARGS (tinfo),
5967 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5968 {
5969 error ("template arguments to %qD do not match original "
5970 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5971 if (!uses_template_parms (TI_ARGS (tinfo)))
5972 inform (input_location, "use %<template<>%> for"
5973 " an explicit specialization");
5974 /* Avoid crash in import_export_decl. */
5975 DECL_INTERFACE_KNOWN (decl) = 1;
5976 return error_mark_node;
5977 }
5978 }
5979
5980 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl) == decl);
5981
5982
5983 if (new_template_p)
5984 {
5985 /* Push template declarations for global functions and types.
5986 Note that we do not try to push a global template friend
5987 declared in a template class; such a thing may well depend on
5988 the template parameters of the class and we'll push it when
5989 instantiating the befriending class. */
5990 if (!ctx
5991 && !(is_friend && template_class_depth (current_class_type) > 0))
5992 {
5993 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5994 if (tmpl == error_mark_node)
5995 return error_mark_node;
5996
5997 /* Hide template friend classes that haven't been declared yet. */
5998 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5999 {
6000 DECL_ANTICIPATED (tmpl) = 1;
6001 DECL_FRIEND_P (tmpl) = 1;
6002 }
6003 }
6004 }
6005 else
6006 /* The type may have been completed, or (erroneously) changed. */
6007 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6008
6009 if (is_primary)
6010 {
6011 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6012
6013 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6014
6015 /* Give template template parms a DECL_CONTEXT of the template
6016 for which they are a parameter. */
6017 parms = INNERMOST_TEMPLATE_PARMS (parms);
6018 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6019 {
6020 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6021 if (TREE_CODE (parm) == TEMPLATE_DECL)
6022 DECL_CONTEXT (parm) = tmpl;
6023 }
6024
6025 if (TREE_CODE (decl) == TYPE_DECL
6026 && TYPE_DECL_ALIAS_P (decl))
6027 {
6028 if (tree constr
6029 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6030 {
6031 /* ??? Why don't we do this here for all templates? */
6032 constr = build_constraints (constr, NULL_TREE);
6033 set_constraints (decl, constr);
6034 }
6035 if (complex_alias_template_p (tmpl))
6036 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6037 }
6038 }
6039
6040 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6041 back to its most general template. If TMPL is a specialization,
6042 ARGS may only have the innermost set of arguments. Add the missing
6043 argument levels if necessary. */
6044 if (DECL_TEMPLATE_INFO (tmpl))
6045 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6046
6047 info = build_template_info (tmpl, args);
6048
6049 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6050 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6051 else
6052 {
6053 if (is_primary)
6054 retrofit_lang_decl (decl);
6055 if (DECL_LANG_SPECIFIC (decl))
6056 DECL_TEMPLATE_INFO (decl) = info;
6057 }
6058
6059 if (flag_implicit_templates
6060 && !is_friend
6061 && TREE_PUBLIC (decl)
6062 && VAR_OR_FUNCTION_DECL_P (decl))
6063 /* Set DECL_COMDAT on template instantiations; if we force
6064 them to be emitted by explicit instantiation,
6065 mark_needed will tell cgraph to do the right thing. */
6066 DECL_COMDAT (decl) = true;
6067
6068 return DECL_TEMPLATE_RESULT (tmpl);
6069 }
6070
6071 tree
6072 push_template_decl (tree decl)
6073 {
6074 return push_template_decl_real (decl, false);
6075 }
6076
6077 /* FN is an inheriting constructor that inherits from the constructor
6078 template INHERITED; turn FN into a constructor template with a matching
6079 template header. */
6080
6081 tree
6082 add_inherited_template_parms (tree fn, tree inherited)
6083 {
6084 tree inner_parms
6085 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6086 inner_parms = copy_node (inner_parms);
6087 tree parms
6088 = tree_cons (size_int (processing_template_decl + 1),
6089 inner_parms, current_template_parms);
6090 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6091 tree args = template_parms_to_args (parms);
6092 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6093 DECL_ARTIFICIAL (tmpl) = true;
6094 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6095 return tmpl;
6096 }
6097
6098 /* Called when a class template TYPE is redeclared with the indicated
6099 template PARMS, e.g.:
6100
6101 template <class T> struct S;
6102 template <class T> struct S {}; */
6103
6104 bool
6105 redeclare_class_template (tree type, tree parms, tree cons)
6106 {
6107 tree tmpl;
6108 tree tmpl_parms;
6109 int i;
6110
6111 if (!TYPE_TEMPLATE_INFO (type))
6112 {
6113 error ("%qT is not a template type", type);
6114 return false;
6115 }
6116
6117 tmpl = TYPE_TI_TEMPLATE (type);
6118 if (!PRIMARY_TEMPLATE_P (tmpl))
6119 /* The type is nested in some template class. Nothing to worry
6120 about here; there are no new template parameters for the nested
6121 type. */
6122 return true;
6123
6124 if (!parms)
6125 {
6126 error ("template specifiers not specified in declaration of %qD",
6127 tmpl);
6128 return false;
6129 }
6130
6131 parms = INNERMOST_TEMPLATE_PARMS (parms);
6132 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6133
6134 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6135 {
6136 error_n (input_location, TREE_VEC_LENGTH (parms),
6137 "redeclared with %d template parameter",
6138 "redeclared with %d template parameters",
6139 TREE_VEC_LENGTH (parms));
6140 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6141 "previous declaration %qD used %d template parameter",
6142 "previous declaration %qD used %d template parameters",
6143 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6144 return false;
6145 }
6146
6147 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6148 {
6149 tree tmpl_parm;
6150 tree parm;
6151 tree tmpl_default;
6152 tree parm_default;
6153
6154 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6155 || TREE_VEC_ELT (parms, i) == error_mark_node)
6156 continue;
6157
6158 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6159 if (error_operand_p (tmpl_parm))
6160 return false;
6161
6162 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6163 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6164 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6165
6166 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6167 TEMPLATE_DECL. */
6168 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6169 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6170 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6171 || (TREE_CODE (tmpl_parm) != PARM_DECL
6172 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6173 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6174 || (TREE_CODE (tmpl_parm) == PARM_DECL
6175 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6176 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6177 {
6178 auto_diagnostic_group d;
6179 error ("template parameter %q+#D", tmpl_parm);
6180 inform (input_location, "redeclared here as %q#D", parm);
6181 return false;
6182 }
6183
6184 /* The parameters can be declared to introduce different
6185 constraints. */
6186 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6187 tree p2 = TREE_VEC_ELT (parms, i);
6188 if (!template_parameter_constraints_equivalent_p (p1, p2))
6189 {
6190 auto_diagnostic_group d;
6191 error ("declaration of template parameter %q+#D with different "
6192 "constraints", parm);
6193 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6194 "original declaration appeared here");
6195 return false;
6196 }
6197
6198 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6199 {
6200 /* We have in [temp.param]:
6201
6202 A template-parameter may not be given default arguments
6203 by two different declarations in the same scope. */
6204 auto_diagnostic_group d;
6205 error_at (input_location, "redefinition of default argument for %q#D", parm);
6206 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6207 "original definition appeared here");
6208 return false;
6209 }
6210
6211 if (parm_default != NULL_TREE)
6212 /* Update the previous template parameters (which are the ones
6213 that will really count) with the new default value. */
6214 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6215 else if (tmpl_default != NULL_TREE)
6216 /* Update the new parameters, too; they'll be used as the
6217 parameters for any members. */
6218 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6219
6220 /* Give each template template parm in this redeclaration a
6221 DECL_CONTEXT of the template for which they are a parameter. */
6222 if (TREE_CODE (parm) == TEMPLATE_DECL)
6223 {
6224 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6225 DECL_CONTEXT (parm) = tmpl;
6226 }
6227
6228 if (TREE_CODE (parm) == TYPE_DECL)
6229 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6230 }
6231
6232 tree ci = get_constraints (tmpl);
6233 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6234 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6235
6236 /* Two classes with different constraints declare different entities. */
6237 if (!cp_tree_equal (req1, req2))
6238 {
6239 auto_diagnostic_group d;
6240 error_at (input_location, "redeclaration %q#D with different "
6241 "constraints", tmpl);
6242 inform (DECL_SOURCE_LOCATION (tmpl),
6243 "original declaration appeared here");
6244 return false;
6245 }
6246
6247 return true;
6248 }
6249
6250 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6251 to be used when the caller has already checked
6252 (processing_template_decl
6253 && !instantiation_dependent_expression_p (expr)
6254 && potential_constant_expression (expr))
6255 and cleared processing_template_decl. */
6256
6257 tree
6258 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6259 {
6260 return tsubst_copy_and_build (expr,
6261 /*args=*/NULL_TREE,
6262 complain,
6263 /*in_decl=*/NULL_TREE,
6264 /*function_p=*/false,
6265 /*integral_constant_expression_p=*/true);
6266 }
6267
6268 /* Simplify EXPR if it is a non-dependent expression. Returns the
6269 (possibly simplified) expression. */
6270
6271 tree
6272 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6273 {
6274 if (expr == NULL_TREE)
6275 return NULL_TREE;
6276
6277 /* If we're in a template, but EXPR isn't value dependent, simplify
6278 it. We're supposed to treat:
6279
6280 template <typename T> void f(T[1 + 1]);
6281 template <typename T> void f(T[2]);
6282
6283 as two declarations of the same function, for example. */
6284 if (processing_template_decl
6285 && is_nondependent_constant_expression (expr))
6286 {
6287 processing_template_decl_sentinel s;
6288 expr = instantiate_non_dependent_expr_internal (expr, complain);
6289 }
6290 return expr;
6291 }
6292
6293 tree
6294 instantiate_non_dependent_expr (tree expr)
6295 {
6296 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6297 }
6298
6299 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6300 an uninstantiated expression. */
6301
6302 tree
6303 instantiate_non_dependent_or_null (tree expr)
6304 {
6305 if (expr == NULL_TREE)
6306 return NULL_TREE;
6307 if (processing_template_decl)
6308 {
6309 if (!is_nondependent_constant_expression (expr))
6310 expr = NULL_TREE;
6311 else
6312 {
6313 processing_template_decl_sentinel s;
6314 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6315 }
6316 }
6317 return expr;
6318 }
6319
6320 /* True iff T is a specialization of a variable template. */
6321
6322 bool
6323 variable_template_specialization_p (tree t)
6324 {
6325 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6326 return false;
6327 tree tmpl = DECL_TI_TEMPLATE (t);
6328 return variable_template_p (tmpl);
6329 }
6330
6331 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6332 template declaration, or a TYPE_DECL for an alias declaration. */
6333
6334 bool
6335 alias_type_or_template_p (tree t)
6336 {
6337 if (t == NULL_TREE)
6338 return false;
6339 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6340 || (TYPE_P (t)
6341 && TYPE_NAME (t)
6342 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6343 || DECL_ALIAS_TEMPLATE_P (t));
6344 }
6345
6346 /* If T is a specialization of an alias template, return it; otherwise return
6347 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6348
6349 tree
6350 alias_template_specialization_p (const_tree t,
6351 bool transparent_typedefs)
6352 {
6353 if (!TYPE_P (t))
6354 return NULL_TREE;
6355
6356 /* It's an alias template specialization if it's an alias and its
6357 TYPE_NAME is a specialization of a primary template. */
6358 if (typedef_variant_p (t))
6359 {
6360 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6361 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6362 return CONST_CAST_TREE (t);
6363 if (transparent_typedefs)
6364 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6365 (TYPE_NAME (t)),
6366 transparent_typedefs);
6367 }
6368
6369 return NULL_TREE;
6370 }
6371
6372 /* An alias template is complex from a SFINAE perspective if a template-id
6373 using that alias can be ill-formed when the expansion is not, as with
6374 the void_t template. We determine this by checking whether the
6375 expansion for the alias template uses all its template parameters. */
6376
6377 struct uses_all_template_parms_data
6378 {
6379 int level;
6380 bool *seen;
6381 };
6382
6383 static int
6384 uses_all_template_parms_r (tree t, void *data_)
6385 {
6386 struct uses_all_template_parms_data &data
6387 = *(struct uses_all_template_parms_data*)data_;
6388 tree idx = get_template_parm_index (t);
6389
6390 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6391 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6392 return 0;
6393 }
6394
6395 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6396
6397 static int
6398 complex_pack_expansion_r (tree t, void *data_)
6399 {
6400 /* An alias template with a pack expansion that expands a pack from the
6401 enclosing class needs to be considered complex, to avoid confusion with
6402 the same pack being used as an argument to the alias's own template
6403 parameter (91966). */
6404 if (!PACK_EXPANSION_P (t))
6405 return 0;
6406 struct uses_all_template_parms_data &data
6407 = *(struct uses_all_template_parms_data*)data_;
6408 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6409 pack = TREE_CHAIN (pack))
6410 {
6411 tree parm_pack = TREE_VALUE (pack);
6412 if (!TEMPLATE_PARM_P (parm_pack))
6413 continue;
6414 int idx, level;
6415 template_parm_level_and_index (parm_pack, &level, &idx);
6416 if (level < data.level)
6417 return 1;
6418 }
6419 return 0;
6420 }
6421
6422 static bool
6423 complex_alias_template_p (const_tree tmpl)
6424 {
6425 /* A renaming alias isn't complex. */
6426 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6427 return false;
6428
6429 /* Any other constrained alias is complex. */
6430 if (get_constraints (tmpl))
6431 return true;
6432
6433 struct uses_all_template_parms_data data;
6434 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6435 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6436 data.level = TMPL_PARMS_DEPTH (parms);
6437 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6438 data.seen = XALLOCAVEC (bool, len);
6439 for (int i = 0; i < len; ++i)
6440 data.seen[i] = false;
6441
6442 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6443 NULL, true, complex_pack_expansion_r))
6444 return true;
6445 for (int i = 0; i < len; ++i)
6446 if (!data.seen[i])
6447 return true;
6448 return false;
6449 }
6450
6451 /* If T is a specialization of a complex alias template with dependent
6452 template-arguments, return it; otherwise return NULL_TREE. If T is a
6453 typedef to such a specialization, return the specialization. */
6454
6455 tree
6456 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6457 {
6458 if (!TYPE_P (t) || !typedef_variant_p (t))
6459 return NULL_TREE;
6460
6461 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6462 if (tinfo
6463 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6464 && (any_dependent_template_arguments_p
6465 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6466 return CONST_CAST_TREE (t);
6467
6468 if (transparent_typedefs)
6469 {
6470 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6471 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6472 }
6473
6474 return NULL_TREE;
6475 }
6476
6477 /* Return the number of innermost template parameters in TMPL. */
6478
6479 static int
6480 num_innermost_template_parms (const_tree tmpl)
6481 {
6482 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6483 return TREE_VEC_LENGTH (parms);
6484 }
6485
6486 /* Return either TMPL or another template that it is equivalent to under DR
6487 1286: An alias that just changes the name of a template is equivalent to
6488 the other template. */
6489
6490 static tree
6491 get_underlying_template (tree tmpl)
6492 {
6493 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6494 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6495 {
6496 /* Determine if the alias is equivalent to an underlying template. */
6497 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6498 /* The underlying type may have been ill-formed. Don't proceed. */
6499 if (!orig_type)
6500 break;
6501 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6502 if (!tinfo)
6503 break;
6504
6505 tree underlying = TI_TEMPLATE (tinfo);
6506 if (!PRIMARY_TEMPLATE_P (underlying)
6507 || (num_innermost_template_parms (tmpl)
6508 != num_innermost_template_parms (underlying)))
6509 break;
6510
6511 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6512 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6513 break;
6514
6515 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6516 it's appropriate to treat a less-constrained alias as equivalent. */
6517 if (!at_least_as_constrained (underlying, tmpl))
6518 break;
6519
6520 /* Alias is equivalent. Strip it and repeat. */
6521 tmpl = underlying;
6522 }
6523
6524 return tmpl;
6525 }
6526
6527 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6528 must be a reference-to-function or a pointer-to-function type, as specified
6529 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6530 and check that the resulting function has external linkage. */
6531
6532 static tree
6533 convert_nontype_argument_function (tree type, tree expr,
6534 tsubst_flags_t complain)
6535 {
6536 tree fns = expr;
6537 tree fn, fn_no_ptr;
6538 linkage_kind linkage;
6539
6540 fn = instantiate_type (type, fns, tf_none);
6541 if (fn == error_mark_node)
6542 return error_mark_node;
6543
6544 if (value_dependent_expression_p (fn))
6545 goto accept;
6546
6547 fn_no_ptr = strip_fnptr_conv (fn);
6548 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6549 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6550 if (BASELINK_P (fn_no_ptr))
6551 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6552
6553 /* [temp.arg.nontype]/1
6554
6555 A template-argument for a non-type, non-template template-parameter
6556 shall be one of:
6557 [...]
6558 -- the address of an object or function with external [C++11: or
6559 internal] linkage. */
6560
6561 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6562 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6563 {
6564 if (complain & tf_error)
6565 {
6566 location_t loc = cp_expr_loc_or_input_loc (expr);
6567 error_at (loc, "%qE is not a valid template argument for type %qT",
6568 expr, type);
6569 if (TYPE_PTR_P (type))
6570 inform (loc, "it must be the address of a function "
6571 "with external linkage");
6572 else
6573 inform (loc, "it must be the name of a function with "
6574 "external linkage");
6575 }
6576 return NULL_TREE;
6577 }
6578
6579 linkage = decl_linkage (fn_no_ptr);
6580 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6581 {
6582 if (complain & tf_error)
6583 {
6584 location_t loc = cp_expr_loc_or_input_loc (expr);
6585 if (cxx_dialect >= cxx11)
6586 error_at (loc, "%qE is not a valid template argument for type "
6587 "%qT because %qD has no linkage",
6588 expr, type, fn_no_ptr);
6589 else
6590 error_at (loc, "%qE is not a valid template argument for type "
6591 "%qT because %qD does not have external linkage",
6592 expr, type, fn_no_ptr);
6593 }
6594 return NULL_TREE;
6595 }
6596
6597 accept:
6598 if (TYPE_REF_P (type))
6599 {
6600 if (REFERENCE_REF_P (fn))
6601 fn = TREE_OPERAND (fn, 0);
6602 else
6603 fn = build_address (fn);
6604 }
6605 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6606 fn = build_nop (type, fn);
6607
6608 return fn;
6609 }
6610
6611 /* Subroutine of convert_nontype_argument.
6612 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6613 Emit an error otherwise. */
6614
6615 static bool
6616 check_valid_ptrmem_cst_expr (tree type, tree expr,
6617 tsubst_flags_t complain)
6618 {
6619 tree orig_expr = expr;
6620 STRIP_NOPS (expr);
6621 if (null_ptr_cst_p (expr))
6622 return true;
6623 if (TREE_CODE (expr) == PTRMEM_CST
6624 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6625 PTRMEM_CST_CLASS (expr)))
6626 return true;
6627 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6628 return true;
6629 if (processing_template_decl
6630 && TREE_CODE (expr) == ADDR_EXPR
6631 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6632 return true;
6633 if (complain & tf_error)
6634 {
6635 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6636 error_at (loc, "%qE is not a valid template argument for type %qT",
6637 orig_expr, type);
6638 if (TREE_CODE (expr) != PTRMEM_CST)
6639 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6640 else
6641 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6642 }
6643 return false;
6644 }
6645
6646 /* Returns TRUE iff the address of OP is value-dependent.
6647
6648 14.6.2.4 [temp.dep.temp]:
6649 A non-integral non-type template-argument is dependent if its type is
6650 dependent or it has either of the following forms
6651 qualified-id
6652 & qualified-id
6653 and contains a nested-name-specifier which specifies a class-name that
6654 names a dependent type.
6655
6656 We generalize this to just say that the address of a member of a
6657 dependent class is value-dependent; the above doesn't cover the
6658 address of a static data member named with an unqualified-id. */
6659
6660 static bool
6661 has_value_dependent_address (tree op)
6662 {
6663 STRIP_ANY_LOCATION_WRAPPER (op);
6664
6665 /* We could use get_inner_reference here, but there's no need;
6666 this is only relevant for template non-type arguments, which
6667 can only be expressed as &id-expression. */
6668 if (DECL_P (op))
6669 {
6670 tree ctx = CP_DECL_CONTEXT (op);
6671 if (TYPE_P (ctx) && dependent_type_p (ctx))
6672 return true;
6673 }
6674
6675 return false;
6676 }
6677
6678 /* The next set of functions are used for providing helpful explanatory
6679 diagnostics for failed overload resolution. Their messages should be
6680 indented by two spaces for consistency with the messages in
6681 call.c */
6682
6683 static int
6684 unify_success (bool /*explain_p*/)
6685 {
6686 return 0;
6687 }
6688
6689 /* Other failure functions should call this one, to provide a single function
6690 for setting a breakpoint on. */
6691
6692 static int
6693 unify_invalid (bool /*explain_p*/)
6694 {
6695 return 1;
6696 }
6697
6698 static int
6699 unify_parameter_deduction_failure (bool explain_p, tree parm)
6700 {
6701 if (explain_p)
6702 inform (input_location,
6703 " couldn%'t deduce template parameter %qD", parm);
6704 return unify_invalid (explain_p);
6705 }
6706
6707 static int
6708 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6709 {
6710 if (explain_p)
6711 inform (input_location,
6712 " types %qT and %qT have incompatible cv-qualifiers",
6713 parm, arg);
6714 return unify_invalid (explain_p);
6715 }
6716
6717 static int
6718 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6719 {
6720 if (explain_p)
6721 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6722 return unify_invalid (explain_p);
6723 }
6724
6725 static int
6726 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6727 {
6728 if (explain_p)
6729 inform (input_location,
6730 " template parameter %qD is not a parameter pack, but "
6731 "argument %qD is",
6732 parm, arg);
6733 return unify_invalid (explain_p);
6734 }
6735
6736 static int
6737 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6738 {
6739 if (explain_p)
6740 inform (input_location,
6741 " template argument %qE does not match "
6742 "pointer-to-member constant %qE",
6743 arg, parm);
6744 return unify_invalid (explain_p);
6745 }
6746
6747 static int
6748 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6749 {
6750 if (explain_p)
6751 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6752 return unify_invalid (explain_p);
6753 }
6754
6755 static int
6756 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6757 {
6758 if (explain_p)
6759 inform (input_location,
6760 " inconsistent parameter pack deduction with %qT and %qT",
6761 old_arg, new_arg);
6762 return unify_invalid (explain_p);
6763 }
6764
6765 static int
6766 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6767 {
6768 if (explain_p)
6769 {
6770 if (TYPE_P (parm))
6771 inform (input_location,
6772 " deduced conflicting types for parameter %qT (%qT and %qT)",
6773 parm, first, second);
6774 else
6775 inform (input_location,
6776 " deduced conflicting values for non-type parameter "
6777 "%qE (%qE and %qE)", parm, first, second);
6778 }
6779 return unify_invalid (explain_p);
6780 }
6781
6782 static int
6783 unify_vla_arg (bool explain_p, tree arg)
6784 {
6785 if (explain_p)
6786 inform (input_location,
6787 " variable-sized array type %qT is not "
6788 "a valid template argument",
6789 arg);
6790 return unify_invalid (explain_p);
6791 }
6792
6793 static int
6794 unify_method_type_error (bool explain_p, tree arg)
6795 {
6796 if (explain_p)
6797 inform (input_location,
6798 " member function type %qT is not a valid template argument",
6799 arg);
6800 return unify_invalid (explain_p);
6801 }
6802
6803 static int
6804 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6805 {
6806 if (explain_p)
6807 {
6808 if (least_p)
6809 inform_n (input_location, wanted,
6810 " candidate expects at least %d argument, %d provided",
6811 " candidate expects at least %d arguments, %d provided",
6812 wanted, have);
6813 else
6814 inform_n (input_location, wanted,
6815 " candidate expects %d argument, %d provided",
6816 " candidate expects %d arguments, %d provided",
6817 wanted, have);
6818 }
6819 return unify_invalid (explain_p);
6820 }
6821
6822 static int
6823 unify_too_many_arguments (bool explain_p, int have, int wanted)
6824 {
6825 return unify_arity (explain_p, have, wanted);
6826 }
6827
6828 static int
6829 unify_too_few_arguments (bool explain_p, int have, int wanted,
6830 bool least_p = false)
6831 {
6832 return unify_arity (explain_p, have, wanted, least_p);
6833 }
6834
6835 static int
6836 unify_arg_conversion (bool explain_p, tree to_type,
6837 tree from_type, tree arg)
6838 {
6839 if (explain_p)
6840 inform (cp_expr_loc_or_input_loc (arg),
6841 " cannot convert %qE (type %qT) to type %qT",
6842 arg, from_type, to_type);
6843 return unify_invalid (explain_p);
6844 }
6845
6846 static int
6847 unify_no_common_base (bool explain_p, enum template_base_result r,
6848 tree parm, tree arg)
6849 {
6850 if (explain_p)
6851 switch (r)
6852 {
6853 case tbr_ambiguous_baseclass:
6854 inform (input_location, " %qT is an ambiguous base class of %qT",
6855 parm, arg);
6856 break;
6857 default:
6858 inform (input_location, " %qT is not derived from %qT", arg, parm);
6859 break;
6860 }
6861 return unify_invalid (explain_p);
6862 }
6863
6864 static int
6865 unify_inconsistent_template_template_parameters (bool explain_p)
6866 {
6867 if (explain_p)
6868 inform (input_location,
6869 " template parameters of a template template argument are "
6870 "inconsistent with other deduced template arguments");
6871 return unify_invalid (explain_p);
6872 }
6873
6874 static int
6875 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6876 {
6877 if (explain_p)
6878 inform (input_location,
6879 " cannot deduce a template for %qT from non-template type %qT",
6880 parm, arg);
6881 return unify_invalid (explain_p);
6882 }
6883
6884 static int
6885 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6886 {
6887 if (explain_p)
6888 inform (input_location,
6889 " template argument %qE does not match %qE", arg, parm);
6890 return unify_invalid (explain_p);
6891 }
6892
6893 /* True if T is a C++20 template parameter object to store the argument for a
6894 template parameter of class type. */
6895
6896 bool
6897 template_parm_object_p (const_tree t)
6898 {
6899 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6900 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6901 }
6902
6903 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6904 argument for TYPE, points to an unsuitable object. */
6905
6906 static bool
6907 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6908 {
6909 switch (TREE_CODE (expr))
6910 {
6911 CASE_CONVERT:
6912 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6913 complain);
6914
6915 case TARGET_EXPR:
6916 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6917 complain);
6918
6919 case CONSTRUCTOR:
6920 {
6921 unsigned i; tree elt;
6922 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6923 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6924 return true;
6925 }
6926 break;
6927
6928 case ADDR_EXPR:
6929 {
6930 tree decl = TREE_OPERAND (expr, 0);
6931
6932 if (!VAR_P (decl))
6933 {
6934 if (complain & tf_error)
6935 error_at (cp_expr_loc_or_input_loc (expr),
6936 "%qE is not a valid template argument of type %qT "
6937 "because %qE is not a variable", expr, type, decl);
6938 return true;
6939 }
6940 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6941 {
6942 if (complain & tf_error)
6943 error_at (cp_expr_loc_or_input_loc (expr),
6944 "%qE is not a valid template argument of type %qT "
6945 "in C++98 because %qD does not have external linkage",
6946 expr, type, decl);
6947 return true;
6948 }
6949 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6950 && decl_linkage (decl) == lk_none)
6951 {
6952 if (complain & tf_error)
6953 error_at (cp_expr_loc_or_input_loc (expr),
6954 "%qE is not a valid template argument of type %qT "
6955 "because %qD has no linkage", expr, type, decl);
6956 return true;
6957 }
6958 /* C++17: For a non-type template-parameter of reference or pointer
6959 type, the value of the constant expression shall not refer to (or
6960 for a pointer type, shall not be the address of):
6961 * a subobject (4.5),
6962 * a temporary object (15.2),
6963 * a string literal (5.13.5),
6964 * the result of a typeid expression (8.2.8), or
6965 * a predefined __func__ variable (11.4.1). */
6966 else if (DECL_ARTIFICIAL (decl))
6967 {
6968 if (complain & tf_error)
6969 error ("the address of %qD is not a valid template argument",
6970 decl);
6971 return true;
6972 }
6973 else if (!same_type_ignoring_top_level_qualifiers_p
6974 (strip_array_types (TREE_TYPE (type)),
6975 strip_array_types (TREE_TYPE (decl))))
6976 {
6977 if (complain & tf_error)
6978 error ("the address of the %qT subobject of %qD is not a "
6979 "valid template argument", TREE_TYPE (type), decl);
6980 return true;
6981 }
6982 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6983 {
6984 if (complain & tf_error)
6985 error ("the address of %qD is not a valid template argument "
6986 "because it does not have static storage duration",
6987 decl);
6988 return true;
6989 }
6990 }
6991 break;
6992
6993 default:
6994 if (!INDIRECT_TYPE_P (type))
6995 /* We're only concerned about pointers and references here. */;
6996 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6997 /* Null pointer values are OK in C++11. */;
6998 else
6999 {
7000 if (VAR_P (expr))
7001 {
7002 if (complain & tf_error)
7003 error ("%qD is not a valid template argument "
7004 "because %qD is a variable, not the address of "
7005 "a variable", expr, expr);
7006 return true;
7007 }
7008 else
7009 {
7010 if (complain & tf_error)
7011 error ("%qE is not a valid template argument for %qT "
7012 "because it is not the address of a variable",
7013 expr, type);
7014 return true;
7015 }
7016 }
7017 }
7018 return false;
7019
7020 }
7021
7022 /* The template arguments corresponding to template parameter objects of types
7023 that contain pointers to members. */
7024
7025 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7026
7027 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7028 template argument EXPR. */
7029
7030 static tree
7031 get_template_parm_object (tree expr, tsubst_flags_t complain)
7032 {
7033 if (TREE_CODE (expr) == TARGET_EXPR)
7034 expr = TARGET_EXPR_INITIAL (expr);
7035
7036 if (!TREE_CONSTANT (expr))
7037 {
7038 if ((complain & tf_error)
7039 && require_rvalue_constant_expression (expr))
7040 cxx_constant_value (expr);
7041 return error_mark_node;
7042 }
7043 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7044 return error_mark_node;
7045
7046 tree name = mangle_template_parm_object (expr);
7047 tree decl = get_global_binding (name);
7048 if (decl)
7049 return decl;
7050
7051 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7052 decl = create_temporary_var (type);
7053 TREE_STATIC (decl) = true;
7054 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7055 TREE_READONLY (decl) = true;
7056 DECL_NAME (decl) = name;
7057 SET_DECL_ASSEMBLER_NAME (decl, name);
7058 DECL_CONTEXT (decl) = global_namespace;
7059 comdat_linkage (decl);
7060
7061 if (!zero_init_p (type))
7062 {
7063 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7064 lower_var_init before we're done mangling. So store the original
7065 value elsewhere. */
7066 tree copy = unshare_constructor (expr);
7067 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7068 }
7069
7070 pushdecl_top_level_and_finish (decl, expr);
7071
7072 return decl;
7073 }
7074
7075 /* Return the actual template argument corresponding to template parameter
7076 object VAR. */
7077
7078 tree
7079 tparm_object_argument (tree var)
7080 {
7081 if (zero_init_p (TREE_TYPE (var)))
7082 return DECL_INITIAL (var);
7083 return *(tparm_obj_values->get (var));
7084 }
7085
7086 /* Attempt to convert the non-type template parameter EXPR to the
7087 indicated TYPE. If the conversion is successful, return the
7088 converted value. If the conversion is unsuccessful, return
7089 NULL_TREE if we issued an error message, or error_mark_node if we
7090 did not. We issue error messages for out-and-out bad template
7091 parameters, but not simply because the conversion failed, since we
7092 might be just trying to do argument deduction. Both TYPE and EXPR
7093 must be non-dependent.
7094
7095 The conversion follows the special rules described in
7096 [temp.arg.nontype], and it is much more strict than an implicit
7097 conversion.
7098
7099 This function is called twice for each template argument (see
7100 lookup_template_class for a more accurate description of this
7101 problem). This means that we need to handle expressions which
7102 are not valid in a C++ source, but can be created from the
7103 first call (for instance, casts to perform conversions). These
7104 hacks can go away after we fix the double coercion problem. */
7105
7106 static tree
7107 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7108 {
7109 tree expr_type;
7110 location_t loc = cp_expr_loc_or_input_loc (expr);
7111
7112 /* Detect immediately string literals as invalid non-type argument.
7113 This special-case is not needed for correctness (we would easily
7114 catch this later), but only to provide better diagnostic for this
7115 common user mistake. As suggested by DR 100, we do not mention
7116 linkage issues in the diagnostic as this is not the point. */
7117 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7118 {
7119 if (complain & tf_error)
7120 error ("%qE is not a valid template argument for type %qT "
7121 "because string literals can never be used in this context",
7122 expr, type);
7123 return NULL_TREE;
7124 }
7125
7126 /* Add the ADDR_EXPR now for the benefit of
7127 value_dependent_expression_p. */
7128 if (TYPE_PTROBV_P (type)
7129 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7130 {
7131 expr = decay_conversion (expr, complain);
7132 if (expr == error_mark_node)
7133 return error_mark_node;
7134 }
7135
7136 /* If we are in a template, EXPR may be non-dependent, but still
7137 have a syntactic, rather than semantic, form. For example, EXPR
7138 might be a SCOPE_REF, rather than the VAR_DECL to which the
7139 SCOPE_REF refers. Preserving the qualifying scope is necessary
7140 so that access checking can be performed when the template is
7141 instantiated -- but here we need the resolved form so that we can
7142 convert the argument. */
7143 bool non_dep = false;
7144 if (TYPE_REF_OBJ_P (type)
7145 && has_value_dependent_address (expr))
7146 /* If we want the address and it's value-dependent, don't fold. */;
7147 else if (processing_template_decl
7148 && is_nondependent_constant_expression (expr))
7149 non_dep = true;
7150 if (error_operand_p (expr))
7151 return error_mark_node;
7152 expr_type = TREE_TYPE (expr);
7153
7154 /* If the argument is non-dependent, perform any conversions in
7155 non-dependent context as well. */
7156 processing_template_decl_sentinel s (non_dep);
7157 if (non_dep)
7158 expr = instantiate_non_dependent_expr_internal (expr, complain);
7159
7160 const bool val_dep_p = value_dependent_expression_p (expr);
7161 if (val_dep_p)
7162 expr = canonicalize_expr_argument (expr, complain);
7163
7164 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7165 to a non-type argument of "nullptr". */
7166 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7167 expr = fold_simple (convert (type, expr));
7168
7169 /* In C++11, integral or enumeration non-type template arguments can be
7170 arbitrary constant expressions. Pointer and pointer to
7171 member arguments can be general constant expressions that evaluate
7172 to a null value, but otherwise still need to be of a specific form. */
7173 if (cxx_dialect >= cxx11)
7174 {
7175 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7176 /* A PTRMEM_CST is already constant, and a valid template
7177 argument for a parameter of pointer to member type, we just want
7178 to leave it in that form rather than lower it to a
7179 CONSTRUCTOR. */;
7180 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7181 || cxx_dialect >= cxx17)
7182 {
7183 /* C++17: A template-argument for a non-type template-parameter shall
7184 be a converted constant expression (8.20) of the type of the
7185 template-parameter. */
7186 expr = build_converted_constant_expr (type, expr, complain);
7187 if (expr == error_mark_node)
7188 /* Make sure we return NULL_TREE only if we have really issued
7189 an error, as described above. */
7190 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7191 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7192 {
7193 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7194 return expr;
7195 }
7196 expr = maybe_constant_value (expr, NULL_TREE,
7197 /*manifestly_const_eval=*/true);
7198 expr = convert_from_reference (expr);
7199 }
7200 else if (TYPE_PTR_OR_PTRMEM_P (type))
7201 {
7202 tree folded = maybe_constant_value (expr, NULL_TREE,
7203 /*manifestly_const_eval=*/true);
7204 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7205 : null_member_pointer_value_p (folded))
7206 expr = folded;
7207 }
7208 }
7209
7210 if (TYPE_REF_P (type))
7211 expr = mark_lvalue_use (expr);
7212 else
7213 expr = mark_rvalue_use (expr);
7214
7215 /* HACK: Due to double coercion, we can get a
7216 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7217 which is the tree that we built on the first call (see
7218 below when coercing to reference to object or to reference to
7219 function). We just strip everything and get to the arg.
7220 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7221 for examples. */
7222 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7223 {
7224 tree probe_type, probe = expr;
7225 if (REFERENCE_REF_P (probe))
7226 probe = TREE_OPERAND (probe, 0);
7227 probe_type = TREE_TYPE (probe);
7228 if (TREE_CODE (probe) == NOP_EXPR)
7229 {
7230 /* ??? Maybe we could use convert_from_reference here, but we
7231 would need to relax its constraints because the NOP_EXPR
7232 could actually change the type to something more cv-qualified,
7233 and this is not folded by convert_from_reference. */
7234 tree addr = TREE_OPERAND (probe, 0);
7235 if (TYPE_REF_P (probe_type)
7236 && TREE_CODE (addr) == ADDR_EXPR
7237 && TYPE_PTR_P (TREE_TYPE (addr))
7238 && (same_type_ignoring_top_level_qualifiers_p
7239 (TREE_TYPE (probe_type),
7240 TREE_TYPE (TREE_TYPE (addr)))))
7241 {
7242 expr = TREE_OPERAND (addr, 0);
7243 expr_type = TREE_TYPE (probe_type);
7244 }
7245 }
7246 }
7247
7248 /* [temp.arg.nontype]/5, bullet 1
7249
7250 For a non-type template-parameter of integral or enumeration type,
7251 integral promotions (_conv.prom_) and integral conversions
7252 (_conv.integral_) are applied. */
7253 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7254 {
7255 if (cxx_dialect < cxx11)
7256 {
7257 tree t = build_converted_constant_expr (type, expr, complain);
7258 t = maybe_constant_value (t);
7259 if (t != error_mark_node)
7260 expr = t;
7261 }
7262
7263 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7264 return error_mark_node;
7265
7266 /* Notice that there are constant expressions like '4 % 0' which
7267 do not fold into integer constants. */
7268 if (TREE_CODE (expr) != INTEGER_CST && !val_dep_p)
7269 {
7270 if (complain & tf_error)
7271 {
7272 int errs = errorcount, warns = warningcount + werrorcount;
7273 if (!require_potential_constant_expression (expr))
7274 expr = error_mark_node;
7275 else
7276 expr = cxx_constant_value (expr);
7277 if (errorcount > errs || warningcount + werrorcount > warns)
7278 inform (loc, "in template argument for type %qT", type);
7279 if (expr == error_mark_node)
7280 return NULL_TREE;
7281 /* else cxx_constant_value complained but gave us
7282 a real constant, so go ahead. */
7283 if (TREE_CODE (expr) != INTEGER_CST)
7284 {
7285 /* Some assemble time constant expressions like
7286 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7287 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7288 as we can emit them into .rodata initializers of
7289 variables, yet they can't fold into an INTEGER_CST at
7290 compile time. Refuse them here. */
7291 gcc_checking_assert (reduced_constant_expression_p (expr));
7292 error_at (loc, "template argument %qE for type %qT not "
7293 "a constant integer", expr, type);
7294 return NULL_TREE;
7295 }
7296 }
7297 else
7298 return NULL_TREE;
7299 }
7300
7301 /* Avoid typedef problems. */
7302 if (TREE_TYPE (expr) != type)
7303 expr = fold_convert (type, expr);
7304 }
7305 /* [temp.arg.nontype]/5, bullet 2
7306
7307 For a non-type template-parameter of type pointer to object,
7308 qualification conversions (_conv.qual_) and the array-to-pointer
7309 conversion (_conv.array_) are applied. */
7310 else if (TYPE_PTROBV_P (type))
7311 {
7312 tree decayed = expr;
7313
7314 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7315 decay_conversion or an explicit cast. If it's a problematic cast,
7316 we'll complain about it below. */
7317 if (TREE_CODE (expr) == NOP_EXPR)
7318 {
7319 tree probe = expr;
7320 STRIP_NOPS (probe);
7321 if (TREE_CODE (probe) == ADDR_EXPR
7322 && TYPE_PTR_P (TREE_TYPE (probe)))
7323 {
7324 expr = probe;
7325 expr_type = TREE_TYPE (expr);
7326 }
7327 }
7328
7329 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7330
7331 A template-argument for a non-type, non-template template-parameter
7332 shall be one of: [...]
7333
7334 -- the name of a non-type template-parameter;
7335 -- the address of an object or function with external linkage, [...]
7336 expressed as "& id-expression" where the & is optional if the name
7337 refers to a function or array, or if the corresponding
7338 template-parameter is a reference.
7339
7340 Here, we do not care about functions, as they are invalid anyway
7341 for a parameter of type pointer-to-object. */
7342
7343 if (val_dep_p)
7344 /* Non-type template parameters are OK. */
7345 ;
7346 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7347 /* Null pointer values are OK in C++11. */;
7348 else if (TREE_CODE (expr) != ADDR_EXPR
7349 && !INDIRECT_TYPE_P (expr_type))
7350 /* Other values, like integer constants, might be valid
7351 non-type arguments of some other type. */
7352 return error_mark_node;
7353 else if (invalid_tparm_referent_p (type, expr, complain))
7354 return NULL_TREE;
7355
7356 expr = decayed;
7357
7358 expr = perform_qualification_conversions (type, expr);
7359 if (expr == error_mark_node)
7360 return error_mark_node;
7361 }
7362 /* [temp.arg.nontype]/5, bullet 3
7363
7364 For a non-type template-parameter of type reference to object, no
7365 conversions apply. The type referred to by the reference may be more
7366 cv-qualified than the (otherwise identical) type of the
7367 template-argument. The template-parameter is bound directly to the
7368 template-argument, which must be an lvalue. */
7369 else if (TYPE_REF_OBJ_P (type))
7370 {
7371 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7372 expr_type))
7373 return error_mark_node;
7374
7375 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7376 {
7377 if (complain & tf_error)
7378 error ("%qE is not a valid template argument for type %qT "
7379 "because of conflicts in cv-qualification", expr, type);
7380 return NULL_TREE;
7381 }
7382
7383 if (!lvalue_p (expr))
7384 {
7385 if (complain & tf_error)
7386 error ("%qE is not a valid template argument for type %qT "
7387 "because it is not an lvalue", expr, type);
7388 return NULL_TREE;
7389 }
7390
7391 /* [temp.arg.nontype]/1
7392
7393 A template-argument for a non-type, non-template template-parameter
7394 shall be one of: [...]
7395
7396 -- the address of an object or function with external linkage. */
7397 if (INDIRECT_REF_P (expr)
7398 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7399 {
7400 expr = TREE_OPERAND (expr, 0);
7401 if (DECL_P (expr))
7402 {
7403 if (complain & tf_error)
7404 error ("%q#D is not a valid template argument for type %qT "
7405 "because a reference variable does not have a constant "
7406 "address", expr, type);
7407 return NULL_TREE;
7408 }
7409 }
7410
7411 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7412 /* OK, dependent reference. We don't want to ask whether a DECL is
7413 itself value-dependent, since what we want here is its address. */;
7414 else
7415 {
7416 expr = build_address (expr);
7417
7418 if (invalid_tparm_referent_p (type, expr, complain))
7419 return NULL_TREE;
7420 }
7421
7422 if (!same_type_p (type, TREE_TYPE (expr)))
7423 expr = build_nop (type, expr);
7424 }
7425 /* [temp.arg.nontype]/5, bullet 4
7426
7427 For a non-type template-parameter of type pointer to function, only
7428 the function-to-pointer conversion (_conv.func_) is applied. If the
7429 template-argument represents a set of overloaded functions (or a
7430 pointer to such), the matching function is selected from the set
7431 (_over.over_). */
7432 else if (TYPE_PTRFN_P (type))
7433 {
7434 /* If the argument is a template-id, we might not have enough
7435 context information to decay the pointer. */
7436 if (!type_unknown_p (expr_type))
7437 {
7438 expr = decay_conversion (expr, complain);
7439 if (expr == error_mark_node)
7440 return error_mark_node;
7441 }
7442
7443 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7444 /* Null pointer values are OK in C++11. */
7445 return perform_qualification_conversions (type, expr);
7446
7447 expr = convert_nontype_argument_function (type, expr, complain);
7448 if (!expr || expr == error_mark_node)
7449 return expr;
7450 }
7451 /* [temp.arg.nontype]/5, bullet 5
7452
7453 For a non-type template-parameter of type reference to function, no
7454 conversions apply. If the template-argument represents a set of
7455 overloaded functions, the matching function is selected from the set
7456 (_over.over_). */
7457 else if (TYPE_REFFN_P (type))
7458 {
7459 if (TREE_CODE (expr) == ADDR_EXPR)
7460 {
7461 if (complain & tf_error)
7462 {
7463 error ("%qE is not a valid template argument for type %qT "
7464 "because it is a pointer", expr, type);
7465 inform (input_location, "try using %qE instead",
7466 TREE_OPERAND (expr, 0));
7467 }
7468 return NULL_TREE;
7469 }
7470
7471 expr = convert_nontype_argument_function (type, expr, complain);
7472 if (!expr || expr == error_mark_node)
7473 return expr;
7474 }
7475 /* [temp.arg.nontype]/5, bullet 6
7476
7477 For a non-type template-parameter of type pointer to member function,
7478 no conversions apply. If the template-argument represents a set of
7479 overloaded member functions, the matching member function is selected
7480 from the set (_over.over_). */
7481 else if (TYPE_PTRMEMFUNC_P (type))
7482 {
7483 expr = instantiate_type (type, expr, tf_none);
7484 if (expr == error_mark_node)
7485 return error_mark_node;
7486
7487 /* [temp.arg.nontype] bullet 1 says the pointer to member
7488 expression must be a pointer-to-member constant. */
7489 if (!val_dep_p
7490 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7491 return NULL_TREE;
7492
7493 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7494 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7495 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7496 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7497 }
7498 /* [temp.arg.nontype]/5, bullet 7
7499
7500 For a non-type template-parameter of type pointer to data member,
7501 qualification conversions (_conv.qual_) are applied. */
7502 else if (TYPE_PTRDATAMEM_P (type))
7503 {
7504 /* [temp.arg.nontype] bullet 1 says the pointer to member
7505 expression must be a pointer-to-member constant. */
7506 if (!val_dep_p
7507 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7508 return NULL_TREE;
7509
7510 expr = perform_qualification_conversions (type, expr);
7511 if (expr == error_mark_node)
7512 return expr;
7513 }
7514 else if (NULLPTR_TYPE_P (type))
7515 {
7516 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7517 {
7518 if (complain & tf_error)
7519 error ("%qE is not a valid template argument for type %qT "
7520 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7521 return NULL_TREE;
7522 }
7523 return expr;
7524 }
7525 else if (CLASS_TYPE_P (type))
7526 {
7527 /* Replace the argument with a reference to the corresponding template
7528 parameter object. */
7529 if (!val_dep_p)
7530 expr = get_template_parm_object (expr, complain);
7531 if (expr == error_mark_node)
7532 return NULL_TREE;
7533 }
7534 /* A template non-type parameter must be one of the above. */
7535 else
7536 gcc_unreachable ();
7537
7538 /* Sanity check: did we actually convert the argument to the
7539 right type? */
7540 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7541 (type, TREE_TYPE (expr)));
7542 return convert_from_reference (expr);
7543 }
7544
7545 /* Subroutine of coerce_template_template_parms, which returns 1 if
7546 PARM_PARM and ARG_PARM match using the rule for the template
7547 parameters of template template parameters. Both PARM and ARG are
7548 template parameters; the rest of the arguments are the same as for
7549 coerce_template_template_parms.
7550 */
7551 static int
7552 coerce_template_template_parm (tree parm,
7553 tree arg,
7554 tsubst_flags_t complain,
7555 tree in_decl,
7556 tree outer_args)
7557 {
7558 if (arg == NULL_TREE || error_operand_p (arg)
7559 || parm == NULL_TREE || error_operand_p (parm))
7560 return 0;
7561
7562 if (TREE_CODE (arg) != TREE_CODE (parm))
7563 return 0;
7564
7565 switch (TREE_CODE (parm))
7566 {
7567 case TEMPLATE_DECL:
7568 /* We encounter instantiations of templates like
7569 template <template <template <class> class> class TT>
7570 class C; */
7571 {
7572 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7573 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7574
7575 if (!coerce_template_template_parms
7576 (parmparm, argparm, complain, in_decl, outer_args))
7577 return 0;
7578 }
7579 /* Fall through. */
7580
7581 case TYPE_DECL:
7582 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7583 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7584 /* Argument is a parameter pack but parameter is not. */
7585 return 0;
7586 break;
7587
7588 case PARM_DECL:
7589 /* The tsubst call is used to handle cases such as
7590
7591 template <int> class C {};
7592 template <class T, template <T> class TT> class D {};
7593 D<int, C> d;
7594
7595 i.e. the parameter list of TT depends on earlier parameters. */
7596 if (!uses_template_parms (TREE_TYPE (arg)))
7597 {
7598 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7599 if (!uses_template_parms (t)
7600 && !same_type_p (t, TREE_TYPE (arg)))
7601 return 0;
7602 }
7603
7604 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7605 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7606 /* Argument is a parameter pack but parameter is not. */
7607 return 0;
7608
7609 break;
7610
7611 default:
7612 gcc_unreachable ();
7613 }
7614
7615 return 1;
7616 }
7617
7618 /* Coerce template argument list ARGLIST for use with template
7619 template-parameter TEMPL. */
7620
7621 static tree
7622 coerce_template_args_for_ttp (tree templ, tree arglist,
7623 tsubst_flags_t complain)
7624 {
7625 /* Consider an example where a template template parameter declared as
7626
7627 template <class T, class U = std::allocator<T> > class TT
7628
7629 The template parameter level of T and U are one level larger than
7630 of TT. To proper process the default argument of U, say when an
7631 instantiation `TT<int>' is seen, we need to build the full
7632 arguments containing {int} as the innermost level. Outer levels,
7633 available when not appearing as default template argument, can be
7634 obtained from the arguments of the enclosing template.
7635
7636 Suppose that TT is later substituted with std::vector. The above
7637 instantiation is `TT<int, std::allocator<T> >' with TT at
7638 level 1, and T at level 2, while the template arguments at level 1
7639 becomes {std::vector} and the inner level 2 is {int}. */
7640
7641 tree outer = DECL_CONTEXT (templ);
7642 if (outer)
7643 outer = generic_targs_for (outer);
7644 else if (current_template_parms)
7645 {
7646 /* This is an argument of the current template, so we haven't set
7647 DECL_CONTEXT yet. */
7648 tree relevant_template_parms;
7649
7650 /* Parameter levels that are greater than the level of the given
7651 template template parm are irrelevant. */
7652 relevant_template_parms = current_template_parms;
7653 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7654 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7655 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7656
7657 outer = template_parms_to_args (relevant_template_parms);
7658 }
7659
7660 if (outer)
7661 arglist = add_to_template_args (outer, arglist);
7662
7663 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7664 return coerce_template_parms (parmlist, arglist, templ,
7665 complain,
7666 /*require_all_args=*/true,
7667 /*use_default_args=*/true);
7668 }
7669
7670 /* A cache of template template parameters with match-all default
7671 arguments. */
7672 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7673
7674 /* T is a bound template template-parameter. Copy its arguments into default
7675 arguments of the template template-parameter's template parameters. */
7676
7677 static tree
7678 add_defaults_to_ttp (tree otmpl)
7679 {
7680 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7681 return *c;
7682
7683 tree ntmpl = copy_node (otmpl);
7684
7685 tree ntype = copy_node (TREE_TYPE (otmpl));
7686 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7687 TYPE_MAIN_VARIANT (ntype) = ntype;
7688 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7689 TYPE_NAME (ntype) = ntmpl;
7690 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7691
7692 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7693 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7694 TEMPLATE_PARM_DECL (idx) = ntmpl;
7695 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7696
7697 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7698 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7699 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7700 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7701 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7702 {
7703 tree o = TREE_VEC_ELT (vec, i);
7704 if (!template_parameter_pack_p (TREE_VALUE (o)))
7705 {
7706 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7707 TREE_PURPOSE (n) = any_targ_node;
7708 }
7709 }
7710
7711 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7712 return ntmpl;
7713 }
7714
7715 /* ARG is a bound potential template template-argument, and PARGS is a list
7716 of arguments for the corresponding template template-parameter. Adjust
7717 PARGS as appropriate for application to ARG's template, and if ARG is a
7718 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7719 arguments to the template template parameter. */
7720
7721 static tree
7722 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7723 {
7724 ++processing_template_decl;
7725 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7726 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7727 {
7728 /* When comparing two template template-parameters in partial ordering,
7729 rewrite the one currently being used as an argument to have default
7730 arguments for all parameters. */
7731 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7732 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7733 if (pargs != error_mark_node)
7734 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7735 TYPE_TI_ARGS (arg));
7736 }
7737 else
7738 {
7739 tree aparms
7740 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7741 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7742 /*require_all*/true,
7743 /*use_default*/true);
7744 }
7745 --processing_template_decl;
7746 return pargs;
7747 }
7748
7749 /* Subroutine of unify for the case when PARM is a
7750 BOUND_TEMPLATE_TEMPLATE_PARM. */
7751
7752 static int
7753 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7754 bool explain_p)
7755 {
7756 tree parmvec = TYPE_TI_ARGS (parm);
7757 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7758
7759 /* The template template parm might be variadic and the argument
7760 not, so flatten both argument lists. */
7761 parmvec = expand_template_argument_pack (parmvec);
7762 argvec = expand_template_argument_pack (argvec);
7763
7764 if (flag_new_ttp)
7765 {
7766 /* In keeping with P0522R0, adjust P's template arguments
7767 to apply to A's template; then flatten it again. */
7768 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7769 nparmvec = expand_template_argument_pack (nparmvec);
7770
7771 if (unify (tparms, targs, nparmvec, argvec,
7772 UNIFY_ALLOW_NONE, explain_p))
7773 return 1;
7774
7775 /* If the P0522 adjustment eliminated a pack expansion, deduce
7776 empty packs. */
7777 if (flag_new_ttp
7778 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7779 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7780 DEDUCE_EXACT, /*sub*/true, explain_p))
7781 return 1;
7782 }
7783 else
7784 {
7785 /* Deduce arguments T, i from TT<T> or TT<i>.
7786 We check each element of PARMVEC and ARGVEC individually
7787 rather than the whole TREE_VEC since they can have
7788 different number of elements, which is allowed under N2555. */
7789
7790 int len = TREE_VEC_LENGTH (parmvec);
7791
7792 /* Check if the parameters end in a pack, making them
7793 variadic. */
7794 int parm_variadic_p = 0;
7795 if (len > 0
7796 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7797 parm_variadic_p = 1;
7798
7799 for (int i = 0; i < len - parm_variadic_p; ++i)
7800 /* If the template argument list of P contains a pack
7801 expansion that is not the last template argument, the
7802 entire template argument list is a non-deduced
7803 context. */
7804 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7805 return unify_success (explain_p);
7806
7807 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7808 return unify_too_few_arguments (explain_p,
7809 TREE_VEC_LENGTH (argvec), len);
7810
7811 for (int i = 0; i < len - parm_variadic_p; ++i)
7812 if (unify (tparms, targs,
7813 TREE_VEC_ELT (parmvec, i),
7814 TREE_VEC_ELT (argvec, i),
7815 UNIFY_ALLOW_NONE, explain_p))
7816 return 1;
7817
7818 if (parm_variadic_p
7819 && unify_pack_expansion (tparms, targs,
7820 parmvec, argvec,
7821 DEDUCE_EXACT,
7822 /*subr=*/true, explain_p))
7823 return 1;
7824 }
7825
7826 return 0;
7827 }
7828
7829 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7830 template template parameters. Both PARM_PARMS and ARG_PARMS are
7831 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7832 or PARM_DECL.
7833
7834 Consider the example:
7835 template <class T> class A;
7836 template<template <class U> class TT> class B;
7837
7838 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7839 the parameters to A, and OUTER_ARGS contains A. */
7840
7841 static int
7842 coerce_template_template_parms (tree parm_parms,
7843 tree arg_parms,
7844 tsubst_flags_t complain,
7845 tree in_decl,
7846 tree outer_args)
7847 {
7848 int nparms, nargs, i;
7849 tree parm, arg;
7850 int variadic_p = 0;
7851
7852 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7853 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7854
7855 nparms = TREE_VEC_LENGTH (parm_parms);
7856 nargs = TREE_VEC_LENGTH (arg_parms);
7857
7858 if (flag_new_ttp)
7859 {
7860 /* P0522R0: A template template-parameter P is at least as specialized as
7861 a template template-argument A if, given the following rewrite to two
7862 function templates, the function template corresponding to P is at
7863 least as specialized as the function template corresponding to A
7864 according to the partial ordering rules for function templates
7865 ([temp.func.order]). Given an invented class template X with the
7866 template parameter list of A (including default arguments):
7867
7868 * Each of the two function templates has the same template parameters,
7869 respectively, as P or A.
7870
7871 * Each function template has a single function parameter whose type is
7872 a specialization of X with template arguments corresponding to the
7873 template parameters from the respective function template where, for
7874 each template parameter PP in the template parameter list of the
7875 function template, a corresponding template argument AA is formed. If
7876 PP declares a parameter pack, then AA is the pack expansion
7877 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7878
7879 If the rewrite produces an invalid type, then P is not at least as
7880 specialized as A. */
7881
7882 /* So coerce P's args to apply to A's parms, and then deduce between A's
7883 args and the converted args. If that succeeds, A is at least as
7884 specialized as P, so they match.*/
7885 tree pargs = template_parms_level_to_args (parm_parms);
7886 pargs = add_outermost_template_args (outer_args, pargs);
7887 ++processing_template_decl;
7888 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7889 /*require_all*/true, /*use_default*/true);
7890 --processing_template_decl;
7891 if (pargs != error_mark_node)
7892 {
7893 tree targs = make_tree_vec (nargs);
7894 tree aargs = template_parms_level_to_args (arg_parms);
7895 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7896 /*explain*/false))
7897 return 1;
7898 }
7899 }
7900
7901 /* Determine whether we have a parameter pack at the end of the
7902 template template parameter's template parameter list. */
7903 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7904 {
7905 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7906
7907 if (error_operand_p (parm))
7908 return 0;
7909
7910 switch (TREE_CODE (parm))
7911 {
7912 case TEMPLATE_DECL:
7913 case TYPE_DECL:
7914 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7915 variadic_p = 1;
7916 break;
7917
7918 case PARM_DECL:
7919 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7920 variadic_p = 1;
7921 break;
7922
7923 default:
7924 gcc_unreachable ();
7925 }
7926 }
7927
7928 if (nargs != nparms
7929 && !(variadic_p && nargs >= nparms - 1))
7930 return 0;
7931
7932 /* Check all of the template parameters except the parameter pack at
7933 the end (if any). */
7934 for (i = 0; i < nparms - variadic_p; ++i)
7935 {
7936 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7937 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7938 continue;
7939
7940 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7941 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7942
7943 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7944 outer_args))
7945 return 0;
7946
7947 }
7948
7949 if (variadic_p)
7950 {
7951 /* Check each of the template parameters in the template
7952 argument against the template parameter pack at the end of
7953 the template template parameter. */
7954 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7955 return 0;
7956
7957 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7958
7959 for (; i < nargs; ++i)
7960 {
7961 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7962 continue;
7963
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 return 1;
7973 }
7974
7975 /* Verifies that the deduced template arguments (in TARGS) for the
7976 template template parameters (in TPARMS) represent valid bindings,
7977 by comparing the template parameter list of each template argument
7978 to the template parameter list of its corresponding template
7979 template parameter, in accordance with DR150. This
7980 routine can only be called after all template arguments have been
7981 deduced. It will return TRUE if all of the template template
7982 parameter bindings are okay, FALSE otherwise. */
7983 bool
7984 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7985 {
7986 int i, ntparms = TREE_VEC_LENGTH (tparms);
7987 bool ret = true;
7988
7989 /* We're dealing with template parms in this process. */
7990 ++processing_template_decl;
7991
7992 targs = INNERMOST_TEMPLATE_ARGS (targs);
7993
7994 for (i = 0; i < ntparms; ++i)
7995 {
7996 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7997 tree targ = TREE_VEC_ELT (targs, i);
7998
7999 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8000 {
8001 tree packed_args = NULL_TREE;
8002 int idx, len = 1;
8003
8004 if (ARGUMENT_PACK_P (targ))
8005 {
8006 /* Look inside the argument pack. */
8007 packed_args = ARGUMENT_PACK_ARGS (targ);
8008 len = TREE_VEC_LENGTH (packed_args);
8009 }
8010
8011 for (idx = 0; idx < len; ++idx)
8012 {
8013 tree targ_parms = NULL_TREE;
8014
8015 if (packed_args)
8016 /* Extract the next argument from the argument
8017 pack. */
8018 targ = TREE_VEC_ELT (packed_args, idx);
8019
8020 if (PACK_EXPANSION_P (targ))
8021 /* Look at the pattern of the pack expansion. */
8022 targ = PACK_EXPANSION_PATTERN (targ);
8023
8024 /* Extract the template parameters from the template
8025 argument. */
8026 if (TREE_CODE (targ) == TEMPLATE_DECL)
8027 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8028 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8029 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8030
8031 /* Verify that we can coerce the template template
8032 parameters from the template argument to the template
8033 parameter. This requires an exact match. */
8034 if (targ_parms
8035 && !coerce_template_template_parms
8036 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8037 targ_parms,
8038 tf_none,
8039 tparm,
8040 targs))
8041 {
8042 ret = false;
8043 goto out;
8044 }
8045 }
8046 }
8047 }
8048
8049 out:
8050
8051 --processing_template_decl;
8052 return ret;
8053 }
8054
8055 /* Since type attributes aren't mangled, we need to strip them from
8056 template type arguments. */
8057
8058 tree
8059 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8060 {
8061 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8062 return arg;
8063 bool removed_attributes = false;
8064 tree canon = strip_typedefs (arg, &removed_attributes);
8065 if (removed_attributes
8066 && (complain & tf_warning))
8067 warning (OPT_Wignored_attributes,
8068 "ignoring attributes on template argument %qT", arg);
8069 return canon;
8070 }
8071
8072 /* And from inside dependent non-type arguments like sizeof(Type). */
8073
8074 static tree
8075 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8076 {
8077 if (!arg || arg == error_mark_node)
8078 return arg;
8079 bool removed_attributes = false;
8080 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8081 if (removed_attributes
8082 && (complain & tf_warning))
8083 warning (OPT_Wignored_attributes,
8084 "ignoring attributes in template argument %qE", arg);
8085 return canon;
8086 }
8087
8088 // A template declaration can be substituted for a constrained
8089 // template template parameter only when the argument is more
8090 // constrained than the parameter.
8091 static bool
8092 is_compatible_template_arg (tree parm, tree arg)
8093 {
8094 tree parm_cons = get_constraints (parm);
8095
8096 /* For now, allow constrained template template arguments
8097 and unconstrained template template parameters. */
8098 if (parm_cons == NULL_TREE)
8099 return true;
8100
8101 /* If the template parameter is constrained, we need to rewrite its
8102 constraints in terms of the ARG's template parameters. This ensures
8103 that all of the template parameter types will have the same depth.
8104
8105 Note that this is only valid when coerce_template_template_parm is
8106 true for the innermost template parameters of PARM and ARG. In other
8107 words, because coercion is successful, this conversion will be valid. */
8108 tree new_args = NULL_TREE;
8109 if (parm_cons)
8110 {
8111 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8112 new_args = template_parms_level_to_args (aparms);
8113 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8114 tf_none, NULL_TREE);
8115 if (parm_cons == error_mark_node)
8116 return false;
8117 }
8118
8119 return weakly_subsumes (parm_cons, new_args, arg);
8120 }
8121
8122 // Convert a placeholder argument into a binding to the original
8123 // parameter. The original parameter is saved as the TREE_TYPE of
8124 // ARG.
8125 static inline tree
8126 convert_wildcard_argument (tree parm, tree arg)
8127 {
8128 TREE_TYPE (arg) = parm;
8129 return arg;
8130 }
8131
8132 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8133 because one of them is dependent. But we need to represent the
8134 conversion for the benefit of cp_tree_equal. */
8135
8136 static tree
8137 maybe_convert_nontype_argument (tree type, tree arg)
8138 {
8139 /* Auto parms get no conversion. */
8140 if (type_uses_auto (type))
8141 return arg;
8142 /* We don't need or want to add this conversion now if we're going to use the
8143 argument for deduction. */
8144 if (value_dependent_expression_p (arg))
8145 return arg;
8146
8147 type = cv_unqualified (type);
8148 tree argtype = TREE_TYPE (arg);
8149 if (same_type_p (type, argtype))
8150 return arg;
8151
8152 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8153 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8154 return arg;
8155 }
8156
8157 /* Convert the indicated template ARG as necessary to match the
8158 indicated template PARM. Returns the converted ARG, or
8159 error_mark_node if the conversion was unsuccessful. Error and
8160 warning messages are issued under control of COMPLAIN. This
8161 conversion is for the Ith parameter in the parameter list. ARGS is
8162 the full set of template arguments deduced so far. */
8163
8164 static tree
8165 convert_template_argument (tree parm,
8166 tree arg,
8167 tree args,
8168 tsubst_flags_t complain,
8169 int i,
8170 tree in_decl)
8171 {
8172 tree orig_arg;
8173 tree val;
8174 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8175
8176 if (parm == error_mark_node || error_operand_p (arg))
8177 return error_mark_node;
8178
8179 /* Trivially convert placeholders. */
8180 if (TREE_CODE (arg) == WILDCARD_DECL)
8181 return convert_wildcard_argument (parm, arg);
8182
8183 if (arg == any_targ_node)
8184 return arg;
8185
8186 if (TREE_CODE (arg) == TREE_LIST
8187 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8188 {
8189 /* The template argument was the name of some
8190 member function. That's usually
8191 invalid, but static members are OK. In any
8192 case, grab the underlying fields/functions
8193 and issue an error later if required. */
8194 TREE_TYPE (arg) = unknown_type_node;
8195 }
8196
8197 orig_arg = arg;
8198
8199 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8200 requires_type = (TREE_CODE (parm) == TYPE_DECL
8201 || requires_tmpl_type);
8202
8203 /* When determining whether an argument pack expansion is a template,
8204 look at the pattern. */
8205 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8206 arg = PACK_EXPANSION_PATTERN (arg);
8207
8208 /* Deal with an injected-class-name used as a template template arg. */
8209 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8210 {
8211 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8212 if (TREE_CODE (t) == TEMPLATE_DECL)
8213 {
8214 if (cxx_dialect >= cxx11)
8215 /* OK under DR 1004. */;
8216 else if (complain & tf_warning_or_error)
8217 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8218 " used as template template argument", TYPE_NAME (arg));
8219 else if (flag_pedantic_errors)
8220 t = arg;
8221
8222 arg = t;
8223 }
8224 }
8225
8226 is_tmpl_type =
8227 ((TREE_CODE (arg) == TEMPLATE_DECL
8228 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8229 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8230 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8231 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8232
8233 if (is_tmpl_type
8234 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8235 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8236 arg = TYPE_STUB_DECL (arg);
8237
8238 is_type = TYPE_P (arg) || is_tmpl_type;
8239
8240 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8241 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8242 {
8243 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8244 {
8245 if (complain & tf_error)
8246 error ("invalid use of destructor %qE as a type", orig_arg);
8247 return error_mark_node;
8248 }
8249
8250 permerror (input_location,
8251 "to refer to a type member of a template parameter, "
8252 "use %<typename %E%>", orig_arg);
8253
8254 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8255 TREE_OPERAND (arg, 1),
8256 typename_type,
8257 complain);
8258 arg = orig_arg;
8259 is_type = 1;
8260 }
8261 if (is_type != requires_type)
8262 {
8263 if (in_decl)
8264 {
8265 if (complain & tf_error)
8266 {
8267 error ("type/value mismatch at argument %d in template "
8268 "parameter list for %qD",
8269 i + 1, in_decl);
8270 if (is_type)
8271 {
8272 /* The template argument is a type, but we're expecting
8273 an expression. */
8274 inform (input_location,
8275 " expected a constant of type %qT, got %qT",
8276 TREE_TYPE (parm),
8277 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8278 /* [temp.arg]/2: "In a template-argument, an ambiguity
8279 between a type-id and an expression is resolved to a
8280 type-id, regardless of the form of the corresponding
8281 template-parameter." So give the user a clue. */
8282 if (TREE_CODE (arg) == FUNCTION_TYPE)
8283 inform (input_location, " ambiguous template argument "
8284 "for non-type template parameter is treated as "
8285 "function type");
8286 }
8287 else if (requires_tmpl_type)
8288 inform (input_location,
8289 " expected a class template, got %qE", orig_arg);
8290 else
8291 inform (input_location,
8292 " expected a type, got %qE", orig_arg);
8293 }
8294 }
8295 return error_mark_node;
8296 }
8297 if (is_tmpl_type ^ requires_tmpl_type)
8298 {
8299 if (in_decl && (complain & tf_error))
8300 {
8301 error ("type/value mismatch at argument %d in template "
8302 "parameter list for %qD",
8303 i + 1, in_decl);
8304 if (is_tmpl_type)
8305 inform (input_location,
8306 " expected a type, got %qT", DECL_NAME (arg));
8307 else
8308 inform (input_location,
8309 " expected a class template, got %qT", orig_arg);
8310 }
8311 return error_mark_node;
8312 }
8313
8314 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8315 /* We already did the appropriate conversion when packing args. */
8316 val = orig_arg;
8317 else if (is_type)
8318 {
8319 if (requires_tmpl_type)
8320 {
8321 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8322 /* The number of argument required is not known yet.
8323 Just accept it for now. */
8324 val = orig_arg;
8325 else
8326 {
8327 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8328 tree argparm;
8329
8330 /* Strip alias templates that are equivalent to another
8331 template. */
8332 arg = get_underlying_template (arg);
8333 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8334
8335 if (coerce_template_template_parms (parmparm, argparm,
8336 complain, in_decl,
8337 args))
8338 {
8339 val = arg;
8340
8341 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8342 TEMPLATE_DECL. */
8343 if (val != error_mark_node)
8344 {
8345 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8346 val = TREE_TYPE (val);
8347 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8348 val = make_pack_expansion (val, complain);
8349 }
8350 }
8351 else
8352 {
8353 if (in_decl && (complain & tf_error))
8354 {
8355 error ("type/value mismatch at argument %d in "
8356 "template parameter list for %qD",
8357 i + 1, in_decl);
8358 inform (input_location,
8359 " expected a template of type %qD, got %qT",
8360 parm, orig_arg);
8361 }
8362
8363 val = error_mark_node;
8364 }
8365
8366 // Check that the constraints are compatible before allowing the
8367 // substitution.
8368 if (val != error_mark_node)
8369 if (!is_compatible_template_arg (parm, arg))
8370 {
8371 if (in_decl && (complain & tf_error))
8372 {
8373 error ("constraint mismatch at argument %d in "
8374 "template parameter list for %qD",
8375 i + 1, in_decl);
8376 inform (input_location, " expected %qD but got %qD",
8377 parm, arg);
8378 }
8379 val = error_mark_node;
8380 }
8381 }
8382 }
8383 else
8384 val = orig_arg;
8385 /* We only form one instance of each template specialization.
8386 Therefore, if we use a non-canonical variant (i.e., a
8387 typedef), any future messages referring to the type will use
8388 the typedef, which is confusing if those future uses do not
8389 themselves also use the typedef. */
8390 if (TYPE_P (val))
8391 val = canonicalize_type_argument (val, complain);
8392 }
8393 else
8394 {
8395 tree t = TREE_TYPE (parm);
8396
8397 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8398 > TMPL_ARGS_DEPTH (args))
8399 /* We don't have enough levels of args to do any substitution. This
8400 can happen in the context of -fnew-ttp-matching. */;
8401 else if (tree a = type_uses_auto (t))
8402 {
8403 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8404 if (t == error_mark_node)
8405 return error_mark_node;
8406 }
8407 else
8408 t = tsubst (t, args, complain, in_decl);
8409
8410 if (invalid_nontype_parm_type_p (t, complain))
8411 return error_mark_node;
8412
8413 if (t != TREE_TYPE (parm))
8414 t = canonicalize_type_argument (t, complain);
8415
8416 if (!type_dependent_expression_p (orig_arg)
8417 && !uses_template_parms (t))
8418 /* We used to call digest_init here. However, digest_init
8419 will report errors, which we don't want when complain
8420 is zero. More importantly, digest_init will try too
8421 hard to convert things: for example, `0' should not be
8422 converted to pointer type at this point according to
8423 the standard. Accepting this is not merely an
8424 extension, since deciding whether or not these
8425 conversions can occur is part of determining which
8426 function template to call, or whether a given explicit
8427 argument specification is valid. */
8428 val = convert_nontype_argument (t, orig_arg, complain);
8429 else
8430 {
8431 val = canonicalize_expr_argument (orig_arg, complain);
8432 val = maybe_convert_nontype_argument (t, val);
8433 }
8434
8435
8436 if (val == NULL_TREE)
8437 val = error_mark_node;
8438 else if (val == error_mark_node && (complain & tf_error))
8439 error_at (cp_expr_loc_or_input_loc (orig_arg),
8440 "could not convert template argument %qE from %qT to %qT",
8441 orig_arg, TREE_TYPE (orig_arg), t);
8442
8443 if (INDIRECT_REF_P (val))
8444 {
8445 /* Reject template arguments that are references to built-in
8446 functions with no library fallbacks. */
8447 const_tree inner = TREE_OPERAND (val, 0);
8448 const_tree innertype = TREE_TYPE (inner);
8449 if (innertype
8450 && TYPE_REF_P (innertype)
8451 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8452 && TREE_OPERAND_LENGTH (inner) > 0
8453 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8454 return error_mark_node;
8455 }
8456
8457 if (TREE_CODE (val) == SCOPE_REF)
8458 {
8459 /* Strip typedefs from the SCOPE_REF. */
8460 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8461 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8462 complain);
8463 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8464 QUALIFIED_NAME_IS_TEMPLATE (val));
8465 }
8466 }
8467
8468 return val;
8469 }
8470
8471 /* Coerces the remaining template arguments in INNER_ARGS (from
8472 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8473 Returns the coerced argument pack. PARM_IDX is the position of this
8474 parameter in the template parameter list. ARGS is the original
8475 template argument list. */
8476 static tree
8477 coerce_template_parameter_pack (tree parms,
8478 int parm_idx,
8479 tree args,
8480 tree inner_args,
8481 int arg_idx,
8482 tree new_args,
8483 int* lost,
8484 tree in_decl,
8485 tsubst_flags_t complain)
8486 {
8487 tree parm = TREE_VEC_ELT (parms, parm_idx);
8488 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8489 tree packed_args;
8490 tree argument_pack;
8491 tree packed_parms = NULL_TREE;
8492
8493 if (arg_idx > nargs)
8494 arg_idx = nargs;
8495
8496 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8497 {
8498 /* When the template parameter is a non-type template parameter pack
8499 or template template parameter pack whose type or template
8500 parameters use parameter packs, we know exactly how many arguments
8501 we are looking for. Build a vector of the instantiated decls for
8502 these template parameters in PACKED_PARMS. */
8503 /* We can't use make_pack_expansion here because it would interpret a
8504 _DECL as a use rather than a declaration. */
8505 tree decl = TREE_VALUE (parm);
8506 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8507 SET_PACK_EXPANSION_PATTERN (exp, decl);
8508 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8509 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8510
8511 TREE_VEC_LENGTH (args)--;
8512 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8513 TREE_VEC_LENGTH (args)++;
8514
8515 if (packed_parms == error_mark_node)
8516 return error_mark_node;
8517
8518 /* If we're doing a partial instantiation of a member template,
8519 verify that all of the types used for the non-type
8520 template parameter pack are, in fact, valid for non-type
8521 template parameters. */
8522 if (arg_idx < nargs
8523 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8524 {
8525 int j, len = TREE_VEC_LENGTH (packed_parms);
8526 for (j = 0; j < len; ++j)
8527 {
8528 tree t = TREE_VEC_ELT (packed_parms, j);
8529 if (TREE_CODE (t) == PARM_DECL
8530 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8531 return error_mark_node;
8532 }
8533 /* We don't know how many args we have yet, just
8534 use the unconverted ones for now. */
8535 return NULL_TREE;
8536 }
8537
8538 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8539 }
8540 /* Check if we have a placeholder pack, which indicates we're
8541 in the context of a introduction list. In that case we want
8542 to match this pack to the single placeholder. */
8543 else if (arg_idx < nargs
8544 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8545 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8546 {
8547 nargs = arg_idx + 1;
8548 packed_args = make_tree_vec (1);
8549 }
8550 else
8551 packed_args = make_tree_vec (nargs - arg_idx);
8552
8553 /* Convert the remaining arguments, which will be a part of the
8554 parameter pack "parm". */
8555 int first_pack_arg = arg_idx;
8556 for (; arg_idx < nargs; ++arg_idx)
8557 {
8558 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8559 tree actual_parm = TREE_VALUE (parm);
8560 int pack_idx = arg_idx - first_pack_arg;
8561
8562 if (packed_parms)
8563 {
8564 /* Once we've packed as many args as we have types, stop. */
8565 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8566 break;
8567 else if (PACK_EXPANSION_P (arg))
8568 /* We don't know how many args we have yet, just
8569 use the unconverted ones for now. */
8570 return NULL_TREE;
8571 else
8572 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8573 }
8574
8575 if (arg == error_mark_node)
8576 {
8577 if (complain & tf_error)
8578 error ("template argument %d is invalid", arg_idx + 1);
8579 }
8580 else
8581 arg = convert_template_argument (actual_parm,
8582 arg, new_args, complain, parm_idx,
8583 in_decl);
8584 if (arg == error_mark_node)
8585 (*lost)++;
8586 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8587 }
8588
8589 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8590 && TREE_VEC_LENGTH (packed_args) > 0)
8591 {
8592 if (complain & tf_error)
8593 error ("wrong number of template arguments (%d, should be %d)",
8594 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8595 return error_mark_node;
8596 }
8597
8598 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8599 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8600 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8601 else
8602 {
8603 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8604 TREE_CONSTANT (argument_pack) = 1;
8605 }
8606
8607 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8608 if (CHECKING_P)
8609 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8610 TREE_VEC_LENGTH (packed_args));
8611 return argument_pack;
8612 }
8613
8614 /* Returns the number of pack expansions in the template argument vector
8615 ARGS. */
8616
8617 static int
8618 pack_expansion_args_count (tree args)
8619 {
8620 int i;
8621 int count = 0;
8622 if (args)
8623 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8624 {
8625 tree elt = TREE_VEC_ELT (args, i);
8626 if (elt && PACK_EXPANSION_P (elt))
8627 ++count;
8628 }
8629 return count;
8630 }
8631
8632 /* Convert all template arguments to their appropriate types, and
8633 return a vector containing the innermost resulting template
8634 arguments. If any error occurs, return error_mark_node. Error and
8635 warning messages are issued under control of COMPLAIN.
8636
8637 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8638 for arguments not specified in ARGS. Otherwise, if
8639 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8640 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8641 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8642 ARGS. */
8643
8644 static tree
8645 coerce_template_parms (tree parms,
8646 tree args,
8647 tree in_decl,
8648 tsubst_flags_t complain,
8649 bool require_all_args,
8650 bool use_default_args)
8651 {
8652 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8653 tree orig_inner_args;
8654 tree inner_args;
8655 tree new_args;
8656 tree new_inner_args;
8657
8658 /* When used as a boolean value, indicates whether this is a
8659 variadic template parameter list. Since it's an int, we can also
8660 subtract it from nparms to get the number of non-variadic
8661 parameters. */
8662 int variadic_p = 0;
8663 int variadic_args_p = 0;
8664 int post_variadic_parms = 0;
8665
8666 /* Adjustment to nparms for fixed parameter packs. */
8667 int fixed_pack_adjust = 0;
8668 int fixed_packs = 0;
8669 int missing = 0;
8670
8671 /* Likewise for parameters with default arguments. */
8672 int default_p = 0;
8673
8674 if (args == error_mark_node)
8675 return error_mark_node;
8676
8677 nparms = TREE_VEC_LENGTH (parms);
8678
8679 /* Determine if there are any parameter packs or default arguments. */
8680 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8681 {
8682 tree parm = TREE_VEC_ELT (parms, parm_idx);
8683 if (variadic_p)
8684 ++post_variadic_parms;
8685 if (template_parameter_pack_p (TREE_VALUE (parm)))
8686 ++variadic_p;
8687 if (TREE_PURPOSE (parm))
8688 ++default_p;
8689 }
8690
8691 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8692 /* If there are no parameters that follow a parameter pack, we need to
8693 expand any argument packs so that we can deduce a parameter pack from
8694 some non-packed args followed by an argument pack, as in variadic85.C.
8695 If there are such parameters, we need to leave argument packs intact
8696 so the arguments are assigned properly. This can happen when dealing
8697 with a nested class inside a partial specialization of a class
8698 template, as in variadic92.C, or when deducing a template parameter pack
8699 from a sub-declarator, as in variadic114.C. */
8700 if (!post_variadic_parms)
8701 inner_args = expand_template_argument_pack (inner_args);
8702
8703 /* Count any pack expansion args. */
8704 variadic_args_p = pack_expansion_args_count (inner_args);
8705
8706 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8707 if ((nargs - variadic_args_p > nparms && !variadic_p)
8708 || (nargs < nparms - variadic_p
8709 && require_all_args
8710 && !variadic_args_p
8711 && (!use_default_args
8712 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8713 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8714 {
8715 bad_nargs:
8716 if (complain & tf_error)
8717 {
8718 if (variadic_p || default_p)
8719 {
8720 nparms -= variadic_p + default_p;
8721 error ("wrong number of template arguments "
8722 "(%d, should be at least %d)", nargs, nparms);
8723 }
8724 else
8725 error ("wrong number of template arguments "
8726 "(%d, should be %d)", nargs, nparms);
8727
8728 if (in_decl)
8729 inform (DECL_SOURCE_LOCATION (in_decl),
8730 "provided for %qD", in_decl);
8731 }
8732
8733 return error_mark_node;
8734 }
8735 /* We can't pass a pack expansion to a non-pack parameter of an alias
8736 template (DR 1430). */
8737 else if (in_decl
8738 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8739 || concept_definition_p (in_decl))
8740 && variadic_args_p
8741 && nargs - variadic_args_p < nparms - variadic_p)
8742 {
8743 if (complain & tf_error)
8744 {
8745 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8746 {
8747 tree arg = TREE_VEC_ELT (inner_args, i);
8748 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8749
8750 if (PACK_EXPANSION_P (arg)
8751 && !template_parameter_pack_p (parm))
8752 {
8753 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8754 error_at (location_of (arg),
8755 "pack expansion argument for non-pack parameter "
8756 "%qD of alias template %qD", parm, in_decl);
8757 else
8758 error_at (location_of (arg),
8759 "pack expansion argument for non-pack parameter "
8760 "%qD of concept %qD", parm, in_decl);
8761 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8762 goto found;
8763 }
8764 }
8765 gcc_unreachable ();
8766 found:;
8767 }
8768 return error_mark_node;
8769 }
8770
8771 /* We need to evaluate the template arguments, even though this
8772 template-id may be nested within a "sizeof". */
8773 cp_evaluated ev;
8774
8775 new_inner_args = make_tree_vec (nparms);
8776 new_args = add_outermost_template_args (args, new_inner_args);
8777 int pack_adjust = 0;
8778 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8779 {
8780 tree arg;
8781 tree parm;
8782
8783 /* Get the Ith template parameter. */
8784 parm = TREE_VEC_ELT (parms, parm_idx);
8785
8786 if (parm == error_mark_node)
8787 {
8788 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8789 continue;
8790 }
8791
8792 /* Calculate the next argument. */
8793 if (arg_idx < nargs)
8794 arg = TREE_VEC_ELT (inner_args, arg_idx);
8795 else
8796 arg = NULL_TREE;
8797
8798 if (template_parameter_pack_p (TREE_VALUE (parm))
8799 && (arg || require_all_args || !(complain & tf_partial))
8800 && !(arg && ARGUMENT_PACK_P (arg)))
8801 {
8802 /* Some arguments will be placed in the
8803 template parameter pack PARM. */
8804 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8805 inner_args, arg_idx,
8806 new_args, &lost,
8807 in_decl, complain);
8808
8809 if (arg == NULL_TREE)
8810 {
8811 /* We don't know how many args we have yet, just use the
8812 unconverted (and still packed) ones for now. */
8813 new_inner_args = orig_inner_args;
8814 arg_idx = nargs;
8815 break;
8816 }
8817
8818 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8819
8820 /* Store this argument. */
8821 if (arg == error_mark_node)
8822 {
8823 lost++;
8824 /* We are done with all of the arguments. */
8825 arg_idx = nargs;
8826 break;
8827 }
8828 else
8829 {
8830 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8831 arg_idx += pack_adjust;
8832 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8833 {
8834 ++fixed_packs;
8835 fixed_pack_adjust += pack_adjust;
8836 }
8837 }
8838
8839 continue;
8840 }
8841 else if (arg)
8842 {
8843 if (PACK_EXPANSION_P (arg))
8844 {
8845 /* "If every valid specialization of a variadic template
8846 requires an empty template parameter pack, the template is
8847 ill-formed, no diagnostic required." So check that the
8848 pattern works with this parameter. */
8849 tree pattern = PACK_EXPANSION_PATTERN (arg);
8850 tree conv = convert_template_argument (TREE_VALUE (parm),
8851 pattern, new_args,
8852 complain, parm_idx,
8853 in_decl);
8854 if (conv == error_mark_node)
8855 {
8856 if (complain & tf_error)
8857 inform (input_location, "so any instantiation with a "
8858 "non-empty parameter pack would be ill-formed");
8859 ++lost;
8860 }
8861 else if (TYPE_P (conv) && !TYPE_P (pattern))
8862 /* Recover from missing typename. */
8863 TREE_VEC_ELT (inner_args, arg_idx)
8864 = make_pack_expansion (conv, complain);
8865
8866 /* We don't know how many args we have yet, just
8867 use the unconverted ones for now. */
8868 new_inner_args = inner_args;
8869 arg_idx = nargs;
8870 break;
8871 }
8872 }
8873 else if (require_all_args)
8874 {
8875 /* There must be a default arg in this case. */
8876 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8877 complain, in_decl);
8878 /* The position of the first default template argument,
8879 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8880 Record that. */
8881 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8882 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8883 arg_idx - pack_adjust);
8884 }
8885 else
8886 break;
8887
8888 if (arg == error_mark_node)
8889 {
8890 if (complain & tf_error)
8891 error ("template argument %d is invalid", arg_idx + 1);
8892 }
8893 else if (!arg)
8894 {
8895 /* This can occur if there was an error in the template
8896 parameter list itself (which we would already have
8897 reported) that we are trying to recover from, e.g., a class
8898 template with a parameter list such as
8899 template<typename..., typename> (cpp0x/variadic150.C). */
8900 ++lost;
8901
8902 /* This can also happen with a fixed parameter pack (71834). */
8903 if (arg_idx >= nargs)
8904 ++missing;
8905 }
8906 else
8907 arg = convert_template_argument (TREE_VALUE (parm),
8908 arg, new_args, complain,
8909 parm_idx, in_decl);
8910
8911 if (arg == error_mark_node)
8912 lost++;
8913
8914 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8915 }
8916
8917 if (missing || arg_idx < nargs - variadic_args_p)
8918 {
8919 /* If we had fixed parameter packs, we didn't know how many arguments we
8920 actually needed earlier; now we do. */
8921 nparms += fixed_pack_adjust;
8922 variadic_p -= fixed_packs;
8923 goto bad_nargs;
8924 }
8925
8926 if (arg_idx < nargs)
8927 {
8928 /* We had some pack expansion arguments that will only work if the packs
8929 are empty, but wait until instantiation time to complain.
8930 See variadic-ttp3.C. */
8931
8932 /* Except that we can't provide empty packs to alias templates or
8933 concepts when there are no corresponding parameters. Basically,
8934 we can get here with this:
8935
8936 template<typename T> concept C = true;
8937
8938 template<typename... Args>
8939 requires C<Args...>
8940 void f();
8941
8942 When parsing C<Args...>, we try to form a concept check of
8943 C<?, Args...>. Without the extra check for substituting an empty
8944 pack past the last parameter, we can accept the check as valid.
8945
8946 FIXME: This may be valid for alias templates (but I doubt it).
8947
8948 FIXME: The error could be better also. */
8949 if (in_decl && concept_definition_p (in_decl))
8950 {
8951 if (complain & tf_error)
8952 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8953 "too many arguments");
8954 return error_mark_node;
8955 }
8956
8957 int len = nparms + (nargs - arg_idx);
8958 tree args = make_tree_vec (len);
8959 int i = 0;
8960 for (; i < nparms; ++i)
8961 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8962 for (; i < len; ++i, ++arg_idx)
8963 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8964 arg_idx - pack_adjust);
8965 new_inner_args = args;
8966 }
8967
8968 if (lost)
8969 {
8970 gcc_assert (!(complain & tf_error) || seen_error ());
8971 return error_mark_node;
8972 }
8973
8974 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8975 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8976 TREE_VEC_LENGTH (new_inner_args));
8977
8978 return new_inner_args;
8979 }
8980
8981 /* Convert all template arguments to their appropriate types, and
8982 return a vector containing the innermost resulting template
8983 arguments. If any error occurs, return error_mark_node. Error and
8984 warning messages are not issued.
8985
8986 Note that no function argument deduction is performed, and default
8987 arguments are used to fill in unspecified arguments. */
8988 tree
8989 coerce_template_parms (tree parms, tree args, tree in_decl)
8990 {
8991 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8992 }
8993
8994 /* Convert all template arguments to their appropriate type, and
8995 instantiate default arguments as needed. This returns a vector
8996 containing the innermost resulting template arguments, or
8997 error_mark_node if unsuccessful. */
8998 tree
8999 coerce_template_parms (tree parms, tree args, tree in_decl,
9000 tsubst_flags_t complain)
9001 {
9002 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9003 }
9004
9005 /* Like coerce_template_parms. If PARMS represents all template
9006 parameters levels, this function returns a vector of vectors
9007 representing all the resulting argument levels. Note that in this
9008 case, only the innermost arguments are coerced because the
9009 outermost ones are supposed to have been coerced already.
9010
9011 Otherwise, if PARMS represents only (the innermost) vector of
9012 parameters, this function returns a vector containing just the
9013 innermost resulting arguments. */
9014
9015 static tree
9016 coerce_innermost_template_parms (tree parms,
9017 tree args,
9018 tree in_decl,
9019 tsubst_flags_t complain,
9020 bool require_all_args,
9021 bool use_default_args)
9022 {
9023 int parms_depth = TMPL_PARMS_DEPTH (parms);
9024 int args_depth = TMPL_ARGS_DEPTH (args);
9025 tree coerced_args;
9026
9027 if (parms_depth > 1)
9028 {
9029 coerced_args = make_tree_vec (parms_depth);
9030 tree level;
9031 int cur_depth;
9032
9033 for (level = parms, cur_depth = parms_depth;
9034 parms_depth > 0 && level != NULL_TREE;
9035 level = TREE_CHAIN (level), --cur_depth)
9036 {
9037 tree l;
9038 if (cur_depth == args_depth)
9039 l = coerce_template_parms (TREE_VALUE (level),
9040 args, in_decl, complain,
9041 require_all_args,
9042 use_default_args);
9043 else
9044 l = TMPL_ARGS_LEVEL (args, cur_depth);
9045
9046 if (l == error_mark_node)
9047 return error_mark_node;
9048
9049 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9050 }
9051 }
9052 else
9053 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9054 args, in_decl, complain,
9055 require_all_args,
9056 use_default_args);
9057 return coerced_args;
9058 }
9059
9060 /* Returns true if T is a wrapper to make a C++20 template parameter
9061 object const. */
9062
9063 static bool
9064 class_nttp_const_wrapper_p (tree t)
9065 {
9066 if (cxx_dialect < cxx20)
9067 return false;
9068 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9069 && CP_TYPE_CONST_P (TREE_TYPE (t))
9070 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9071 }
9072
9073 /* Returns 1 if template args OT and NT are equivalent. */
9074
9075 int
9076 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9077 {
9078 if (nt == ot)
9079 return 1;
9080 if (nt == NULL_TREE || ot == NULL_TREE)
9081 return false;
9082 if (nt == any_targ_node || ot == any_targ_node)
9083 return true;
9084
9085 if (class_nttp_const_wrapper_p (nt))
9086 nt = TREE_OPERAND (nt, 0);
9087 if (class_nttp_const_wrapper_p (ot))
9088 ot = TREE_OPERAND (ot, 0);
9089
9090 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9091 /* For member templates */
9092 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9093 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9094 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9095 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9096 PACK_EXPANSION_PATTERN (nt))
9097 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9098 PACK_EXPANSION_EXTRA_ARGS (nt)));
9099 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9100 return cp_tree_equal (ot, nt);
9101 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9102 gcc_unreachable ();
9103 else if (TYPE_P (nt) || TYPE_P (ot))
9104 {
9105 if (!(TYPE_P (nt) && TYPE_P (ot)))
9106 return false;
9107 /* Don't treat an alias template specialization with dependent
9108 arguments as equivalent to its underlying type when used as a
9109 template argument; we need them to be distinct so that we
9110 substitute into the specialization arguments at instantiation
9111 time. And aliases can't be equivalent without being ==, so
9112 we don't need to look any deeper.
9113
9114 During partial ordering, however, we need to treat them normally so
9115 that we can order uses of the same alias with different
9116 cv-qualification (79960). */
9117 if (!partial_order
9118 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9119 return false;
9120 else
9121 return same_type_p (ot, nt);
9122 }
9123 else
9124 {
9125 /* Try to treat a template non-type argument that has been converted
9126 to the parameter type as equivalent to one that hasn't yet. */
9127 for (enum tree_code code1 = TREE_CODE (ot);
9128 CONVERT_EXPR_CODE_P (code1)
9129 || code1 == NON_LVALUE_EXPR;
9130 code1 = TREE_CODE (ot))
9131 ot = TREE_OPERAND (ot, 0);
9132
9133 for (enum tree_code code2 = TREE_CODE (nt);
9134 CONVERT_EXPR_CODE_P (code2)
9135 || code2 == NON_LVALUE_EXPR;
9136 code2 = TREE_CODE (nt))
9137 nt = TREE_OPERAND (nt, 0);
9138
9139 return cp_tree_equal (ot, nt);
9140 }
9141 }
9142
9143 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9144 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9145 NEWARG_PTR with the offending arguments if they are non-NULL. */
9146
9147 int
9148 comp_template_args (tree oldargs, tree newargs,
9149 tree *oldarg_ptr, tree *newarg_ptr,
9150 bool partial_order)
9151 {
9152 int i;
9153
9154 if (oldargs == newargs)
9155 return 1;
9156
9157 if (!oldargs || !newargs)
9158 return 0;
9159
9160 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9161 return 0;
9162
9163 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9164 {
9165 tree nt = TREE_VEC_ELT (newargs, i);
9166 tree ot = TREE_VEC_ELT (oldargs, i);
9167
9168 if (! template_args_equal (ot, nt, partial_order))
9169 {
9170 if (oldarg_ptr != NULL)
9171 *oldarg_ptr = ot;
9172 if (newarg_ptr != NULL)
9173 *newarg_ptr = nt;
9174 return 0;
9175 }
9176 }
9177 return 1;
9178 }
9179
9180 inline bool
9181 comp_template_args_porder (tree oargs, tree nargs)
9182 {
9183 return comp_template_args (oargs, nargs, NULL, NULL, true);
9184 }
9185
9186 /* Implement a freelist interface for objects of type T.
9187
9188 Head is a separate object, rather than a regular member, so that we
9189 can define it as a GTY deletable pointer, which is highly
9190 desirable. A data member could be declared that way, but then the
9191 containing object would implicitly get GTY((user)), which would
9192 prevent us from instantiating freelists as global objects.
9193 Although this way we can create freelist global objects, they're
9194 such thin wrappers that instantiating temporaries at every use
9195 loses nothing and saves permanent storage for the freelist object.
9196
9197 Member functions next, anew, poison and reinit have default
9198 implementations that work for most of the types we're interested
9199 in, but if they don't work for some type, they should be explicitly
9200 specialized. See the comments before them for requirements, and
9201 the example specializations for the tree_list_freelist. */
9202 template <typename T>
9203 class freelist
9204 {
9205 /* Return the next object in a chain. We could just do type
9206 punning, but if we access the object with its underlying type, we
9207 avoid strict-aliasing trouble. This needs only work between
9208 poison and reinit. */
9209 static T *&next (T *obj) { return obj->next; }
9210
9211 /* Return a newly allocated, uninitialized or minimally-initialized
9212 object of type T. Any initialization performed by anew should
9213 either remain across the life of the object and the execution of
9214 poison, or be redone by reinit. */
9215 static T *anew () { return ggc_alloc<T> (); }
9216
9217 /* Optionally scribble all over the bits holding the object, so that
9218 they become (mostly?) uninitialized memory. This is called while
9219 preparing to make the object part of the free list. */
9220 static void poison (T *obj) {
9221 T *p ATTRIBUTE_UNUSED = obj;
9222 T **q ATTRIBUTE_UNUSED = &next (obj);
9223
9224 #ifdef ENABLE_GC_CHECKING
9225 /* Poison the data, to indicate the data is garbage. */
9226 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9227 memset (p, 0xa5, sizeof (*p));
9228 #endif
9229 /* Let valgrind know the object is free. */
9230 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9231
9232 /* Let valgrind know the next portion of the object is available,
9233 but uninitialized. */
9234 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9235 }
9236
9237 /* Bring an object that underwent at least one lifecycle after anew
9238 and before the most recent free and poison, back to a usable
9239 state, reinitializing whatever is needed for it to be
9240 functionally equivalent to an object just allocated and returned
9241 by anew. This may poison or clear the next field, used by
9242 freelist housekeeping after poison was called. */
9243 static void reinit (T *obj) {
9244 T **q ATTRIBUTE_UNUSED = &next (obj);
9245
9246 #ifdef ENABLE_GC_CHECKING
9247 memset (q, 0xa5, sizeof (*q));
9248 #endif
9249 /* Let valgrind know the entire object is available, but
9250 uninitialized. */
9251 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9252 }
9253
9254 /* Reference a GTY-deletable pointer that points to the first object
9255 in the free list proper. */
9256 T *&head;
9257 public:
9258 /* Construct a freelist object chaining objects off of HEAD. */
9259 freelist (T *&head) : head(head) {}
9260
9261 /* Add OBJ to the free object list. The former head becomes OBJ's
9262 successor. */
9263 void free (T *obj)
9264 {
9265 poison (obj);
9266 next (obj) = head;
9267 head = obj;
9268 }
9269
9270 /* Take an object from the free list, if one is available, or
9271 allocate a new one. Objects taken from the free list should be
9272 regarded as filled with garbage, except for bits that are
9273 configured to be preserved across free and alloc. */
9274 T *alloc ()
9275 {
9276 if (head)
9277 {
9278 T *obj = head;
9279 head = next (head);
9280 reinit (obj);
9281 return obj;
9282 }
9283 else
9284 return anew ();
9285 }
9286 };
9287
9288 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9289 want to allocate a TREE_LIST using the usual interface, and ensure
9290 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9291 build_tree_list logic in reinit, so this could go out of sync. */
9292 template <>
9293 inline tree &
9294 freelist<tree_node>::next (tree obj)
9295 {
9296 return TREE_CHAIN (obj);
9297 }
9298 template <>
9299 inline tree
9300 freelist<tree_node>::anew ()
9301 {
9302 return build_tree_list (NULL, NULL);
9303 }
9304 template <>
9305 inline void
9306 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9307 {
9308 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9309 tree p ATTRIBUTE_UNUSED = obj;
9310 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9311 tree *q ATTRIBUTE_UNUSED = &next (obj);
9312
9313 #ifdef ENABLE_GC_CHECKING
9314 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9315
9316 /* Poison the data, to indicate the data is garbage. */
9317 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9318 memset (p, 0xa5, size);
9319 #endif
9320 /* Let valgrind know the object is free. */
9321 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9322 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9323 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9324 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9325
9326 #ifdef ENABLE_GC_CHECKING
9327 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9328 /* Keep TREE_CHAIN functional. */
9329 TREE_SET_CODE (obj, TREE_LIST);
9330 #else
9331 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9332 #endif
9333 }
9334 template <>
9335 inline void
9336 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9337 {
9338 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9339
9340 #ifdef ENABLE_GC_CHECKING
9341 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9342 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9343 memset (obj, 0, sizeof (tree_list));
9344 #endif
9345
9346 /* Let valgrind know the entire object is available, but
9347 uninitialized. */
9348 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9349
9350 #ifdef ENABLE_GC_CHECKING
9351 TREE_SET_CODE (obj, TREE_LIST);
9352 #else
9353 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9354 #endif
9355 }
9356
9357 /* Point to the first object in the TREE_LIST freelist. */
9358 static GTY((deletable)) tree tree_list_freelist_head;
9359 /* Return the/an actual TREE_LIST freelist. */
9360 static inline freelist<tree_node>
9361 tree_list_freelist ()
9362 {
9363 return tree_list_freelist_head;
9364 }
9365
9366 /* Point to the first object in the tinst_level freelist. */
9367 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9368 /* Return the/an actual tinst_level freelist. */
9369 static inline freelist<tinst_level>
9370 tinst_level_freelist ()
9371 {
9372 return tinst_level_freelist_head;
9373 }
9374
9375 /* Point to the first object in the pending_template freelist. */
9376 static GTY((deletable)) pending_template *pending_template_freelist_head;
9377 /* Return the/an actual pending_template freelist. */
9378 static inline freelist<pending_template>
9379 pending_template_freelist ()
9380 {
9381 return pending_template_freelist_head;
9382 }
9383
9384 /* Build the TREE_LIST object out of a split list, store it
9385 permanently, and return it. */
9386 tree
9387 tinst_level::to_list ()
9388 {
9389 gcc_assert (split_list_p ());
9390 tree ret = tree_list_freelist ().alloc ();
9391 TREE_PURPOSE (ret) = tldcl;
9392 TREE_VALUE (ret) = targs;
9393 tldcl = ret;
9394 targs = NULL;
9395 gcc_assert (tree_list_p ());
9396 return ret;
9397 }
9398
9399 const unsigned short tinst_level::refcount_infinity;
9400
9401 /* Increment OBJ's refcount unless it is already infinite. */
9402 static tinst_level *
9403 inc_refcount_use (tinst_level *obj)
9404 {
9405 if (obj && obj->refcount != tinst_level::refcount_infinity)
9406 ++obj->refcount;
9407 return obj;
9408 }
9409
9410 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9411 void
9412 tinst_level::free (tinst_level *obj)
9413 {
9414 if (obj->tree_list_p ())
9415 tree_list_freelist ().free (obj->get_node ());
9416 tinst_level_freelist ().free (obj);
9417 }
9418
9419 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9420 OBJ's DECL and OBJ, and start over with the tinst_level object that
9421 used to be referenced by OBJ's NEXT. */
9422 static void
9423 dec_refcount_use (tinst_level *obj)
9424 {
9425 while (obj
9426 && obj->refcount != tinst_level::refcount_infinity
9427 && !--obj->refcount)
9428 {
9429 tinst_level *next = obj->next;
9430 tinst_level::free (obj);
9431 obj = next;
9432 }
9433 }
9434
9435 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9436 and of the former PTR. Omitting the second argument is equivalent
9437 to passing (T*)NULL; this is allowed because passing the
9438 zero-valued integral constant NULL confuses type deduction and/or
9439 overload resolution. */
9440 template <typename T>
9441 static void
9442 set_refcount_ptr (T *& ptr, T *obj = NULL)
9443 {
9444 T *save = ptr;
9445 ptr = inc_refcount_use (obj);
9446 dec_refcount_use (save);
9447 }
9448
9449 static void
9450 add_pending_template (tree d)
9451 {
9452 tree ti = (TYPE_P (d)
9453 ? CLASSTYPE_TEMPLATE_INFO (d)
9454 : DECL_TEMPLATE_INFO (d));
9455 struct pending_template *pt;
9456 int level;
9457
9458 if (TI_PENDING_TEMPLATE_FLAG (ti))
9459 return;
9460
9461 /* We are called both from instantiate_decl, where we've already had a
9462 tinst_level pushed, and instantiate_template, where we haven't.
9463 Compensate. */
9464 gcc_assert (TREE_CODE (d) != TREE_LIST);
9465 level = !current_tinst_level
9466 || current_tinst_level->maybe_get_node () != d;
9467
9468 if (level)
9469 push_tinst_level (d);
9470
9471 pt = pending_template_freelist ().alloc ();
9472 pt->next = NULL;
9473 pt->tinst = NULL;
9474 set_refcount_ptr (pt->tinst, current_tinst_level);
9475 if (last_pending_template)
9476 last_pending_template->next = pt;
9477 else
9478 pending_templates = pt;
9479
9480 last_pending_template = pt;
9481
9482 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9483
9484 if (level)
9485 pop_tinst_level ();
9486 }
9487
9488
9489 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9490 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9491 documentation for TEMPLATE_ID_EXPR. */
9492
9493 tree
9494 lookup_template_function (tree fns, tree arglist)
9495 {
9496 if (fns == error_mark_node || arglist == error_mark_node)
9497 return error_mark_node;
9498
9499 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9500
9501 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9502 {
9503 error ("%q#D is not a function template", fns);
9504 return error_mark_node;
9505 }
9506
9507 if (BASELINK_P (fns))
9508 {
9509 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9510 unknown_type_node,
9511 BASELINK_FUNCTIONS (fns),
9512 arglist);
9513 return fns;
9514 }
9515
9516 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9517 }
9518
9519 /* Within the scope of a template class S<T>, the name S gets bound
9520 (in build_self_reference) to a TYPE_DECL for the class, not a
9521 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9522 or one of its enclosing classes, and that type is a template,
9523 return the associated TEMPLATE_DECL. Otherwise, the original
9524 DECL is returned.
9525
9526 Also handle the case when DECL is a TREE_LIST of ambiguous
9527 injected-class-names from different bases. */
9528
9529 tree
9530 maybe_get_template_decl_from_type_decl (tree decl)
9531 {
9532 if (decl == NULL_TREE)
9533 return decl;
9534
9535 /* DR 176: A lookup that finds an injected-class-name (10.2
9536 [class.member.lookup]) can result in an ambiguity in certain cases
9537 (for example, if it is found in more than one base class). If all of
9538 the injected-class-names that are found refer to specializations of
9539 the same class template, and if the name is followed by a
9540 template-argument-list, the reference refers to the class template
9541 itself and not a specialization thereof, and is not ambiguous. */
9542 if (TREE_CODE (decl) == TREE_LIST)
9543 {
9544 tree t, tmpl = NULL_TREE;
9545 for (t = decl; t; t = TREE_CHAIN (t))
9546 {
9547 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9548 if (!tmpl)
9549 tmpl = elt;
9550 else if (tmpl != elt)
9551 break;
9552 }
9553 if (tmpl && t == NULL_TREE)
9554 return tmpl;
9555 else
9556 return decl;
9557 }
9558
9559 return (decl != NULL_TREE
9560 && DECL_SELF_REFERENCE_P (decl)
9561 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9562 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9563 }
9564
9565 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9566 parameters, find the desired type.
9567
9568 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9569
9570 IN_DECL, if non-NULL, is the template declaration we are trying to
9571 instantiate.
9572
9573 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9574 the class we are looking up.
9575
9576 Issue error and warning messages under control of COMPLAIN.
9577
9578 If the template class is really a local class in a template
9579 function, then the FUNCTION_CONTEXT is the function in which it is
9580 being instantiated.
9581
9582 ??? Note that this function is currently called *twice* for each
9583 template-id: the first time from the parser, while creating the
9584 incomplete type (finish_template_type), and the second type during the
9585 real instantiation (instantiate_template_class). This is surely something
9586 that we want to avoid. It also causes some problems with argument
9587 coercion (see convert_nontype_argument for more information on this). */
9588
9589 static tree
9590 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9591 int entering_scope, tsubst_flags_t complain)
9592 {
9593 tree templ = NULL_TREE, parmlist;
9594 tree t;
9595 spec_entry **slot;
9596 spec_entry *entry;
9597 spec_entry elt;
9598 hashval_t hash;
9599
9600 if (identifier_p (d1))
9601 {
9602 tree value = innermost_non_namespace_value (d1);
9603 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9604 templ = value;
9605 else
9606 {
9607 if (context)
9608 push_decl_namespace (context);
9609 templ = lookup_name (d1);
9610 templ = maybe_get_template_decl_from_type_decl (templ);
9611 if (context)
9612 pop_decl_namespace ();
9613 }
9614 if (templ)
9615 context = DECL_CONTEXT (templ);
9616 }
9617 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9618 {
9619 tree type = TREE_TYPE (d1);
9620
9621 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9622 an implicit typename for the second A. Deal with it. */
9623 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9624 type = TREE_TYPE (type);
9625
9626 if (CLASSTYPE_TEMPLATE_INFO (type))
9627 {
9628 templ = CLASSTYPE_TI_TEMPLATE (type);
9629 d1 = DECL_NAME (templ);
9630 }
9631 }
9632 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9633 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9634 {
9635 templ = TYPE_TI_TEMPLATE (d1);
9636 d1 = DECL_NAME (templ);
9637 }
9638 else if (DECL_TYPE_TEMPLATE_P (d1))
9639 {
9640 templ = d1;
9641 d1 = DECL_NAME (templ);
9642 context = DECL_CONTEXT (templ);
9643 }
9644 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9645 {
9646 templ = d1;
9647 d1 = DECL_NAME (templ);
9648 }
9649
9650 /* Issue an error message if we didn't find a template. */
9651 if (! templ)
9652 {
9653 if (complain & tf_error)
9654 error ("%qT is not a template", d1);
9655 return error_mark_node;
9656 }
9657
9658 if (TREE_CODE (templ) != TEMPLATE_DECL
9659 /* Make sure it's a user visible template, if it was named by
9660 the user. */
9661 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9662 && !PRIMARY_TEMPLATE_P (templ)))
9663 {
9664 if (complain & tf_error)
9665 {
9666 error ("non-template type %qT used as a template", d1);
9667 if (in_decl)
9668 error ("for template declaration %q+D", in_decl);
9669 }
9670 return error_mark_node;
9671 }
9672
9673 complain &= ~tf_user;
9674
9675 /* An alias that just changes the name of a template is equivalent to the
9676 other template, so if any of the arguments are pack expansions, strip
9677 the alias to avoid problems with a pack expansion passed to a non-pack
9678 alias template parameter (DR 1430). */
9679 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9680 templ = get_underlying_template (templ);
9681
9682 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9683 {
9684 tree parm;
9685 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9686 if (arglist2 == error_mark_node
9687 || (!uses_template_parms (arglist2)
9688 && check_instantiated_args (templ, arglist2, complain)))
9689 return error_mark_node;
9690
9691 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9692 return parm;
9693 }
9694 else
9695 {
9696 tree template_type = TREE_TYPE (templ);
9697 tree gen_tmpl;
9698 tree type_decl;
9699 tree found = NULL_TREE;
9700 int arg_depth;
9701 int parm_depth;
9702 int is_dependent_type;
9703 int use_partial_inst_tmpl = false;
9704
9705 if (template_type == error_mark_node)
9706 /* An error occurred while building the template TEMPL, and a
9707 diagnostic has most certainly been emitted for that
9708 already. Let's propagate that error. */
9709 return error_mark_node;
9710
9711 gen_tmpl = most_general_template (templ);
9712 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9713 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9714 arg_depth = TMPL_ARGS_DEPTH (arglist);
9715
9716 if (arg_depth == 1 && parm_depth > 1)
9717 {
9718 /* We've been given an incomplete set of template arguments.
9719 For example, given:
9720
9721 template <class T> struct S1 {
9722 template <class U> struct S2 {};
9723 template <class U> struct S2<U*> {};
9724 };
9725
9726 we will be called with an ARGLIST of `U*', but the
9727 TEMPLATE will be `template <class T> template
9728 <class U> struct S1<T>::S2'. We must fill in the missing
9729 arguments. */
9730 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9731 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9732 arg_depth = TMPL_ARGS_DEPTH (arglist);
9733 }
9734
9735 /* Now we should have enough arguments. */
9736 gcc_assert (parm_depth == arg_depth);
9737
9738 /* From here on, we're only interested in the most general
9739 template. */
9740
9741 /* Calculate the BOUND_ARGS. These will be the args that are
9742 actually tsubst'd into the definition to create the
9743 instantiation. */
9744 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9745 complain,
9746 /*require_all_args=*/true,
9747 /*use_default_args=*/true);
9748
9749 if (arglist == error_mark_node)
9750 /* We were unable to bind the arguments. */
9751 return error_mark_node;
9752
9753 /* In the scope of a template class, explicit references to the
9754 template class refer to the type of the template, not any
9755 instantiation of it. For example, in:
9756
9757 template <class T> class C { void f(C<T>); }
9758
9759 the `C<T>' is just the same as `C'. Outside of the
9760 class, however, such a reference is an instantiation. */
9761 if (entering_scope
9762 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9763 || currently_open_class (template_type))
9764 {
9765 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9766
9767 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9768 return template_type;
9769 }
9770
9771 /* If we already have this specialization, return it. */
9772 elt.tmpl = gen_tmpl;
9773 elt.args = arglist;
9774 elt.spec = NULL_TREE;
9775 hash = spec_hasher::hash (&elt);
9776 entry = type_specializations->find_with_hash (&elt, hash);
9777
9778 if (entry)
9779 return entry->spec;
9780
9781 /* If the template's constraints are not satisfied,
9782 then we cannot form a valid type.
9783
9784 Note that the check is deferred until after the hash
9785 lookup. This prevents redundant checks on previously
9786 instantiated specializations. */
9787 if (flag_concepts
9788 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9789 && !constraints_satisfied_p (gen_tmpl, arglist))
9790 {
9791 if (complain & tf_error)
9792 {
9793 auto_diagnostic_group d;
9794 error ("template constraint failure for %qD", gen_tmpl);
9795 diagnose_constraints (input_location, gen_tmpl, arglist);
9796 }
9797 return error_mark_node;
9798 }
9799
9800 is_dependent_type = uses_template_parms (arglist);
9801
9802 /* If the deduced arguments are invalid, then the binding
9803 failed. */
9804 if (!is_dependent_type
9805 && check_instantiated_args (gen_tmpl,
9806 INNERMOST_TEMPLATE_ARGS (arglist),
9807 complain))
9808 return error_mark_node;
9809
9810 if (!is_dependent_type
9811 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9812 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9813 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9814 {
9815 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9816 DECL_NAME (gen_tmpl),
9817 /*tag_scope=*/ts_global);
9818 return found;
9819 }
9820
9821 context = DECL_CONTEXT (gen_tmpl);
9822 if (context && TYPE_P (context))
9823 {
9824 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9825 context = complete_type (context);
9826 }
9827 else
9828 context = tsubst (context, arglist, complain, in_decl);
9829
9830 if (context == error_mark_node)
9831 return error_mark_node;
9832
9833 if (!context)
9834 context = global_namespace;
9835
9836 /* Create the type. */
9837 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9838 {
9839 /* The user referred to a specialization of an alias
9840 template represented by GEN_TMPL.
9841
9842 [temp.alias]/2 says:
9843
9844 When a template-id refers to the specialization of an
9845 alias template, it is equivalent to the associated
9846 type obtained by substitution of its
9847 template-arguments for the template-parameters in the
9848 type-id of the alias template. */
9849
9850 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9851 /* Note that the call above (by indirectly calling
9852 register_specialization in tsubst_decl) registers the
9853 TYPE_DECL representing the specialization of the alias
9854 template. So next time someone substitutes ARGLIST for
9855 the template parms into the alias template (GEN_TMPL),
9856 she'll get that TYPE_DECL back. */
9857
9858 if (t == error_mark_node)
9859 return t;
9860 }
9861 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9862 {
9863 if (!is_dependent_type)
9864 {
9865 set_current_access_from_decl (TYPE_NAME (template_type));
9866 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9867 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9868 arglist, complain, in_decl),
9869 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9870 arglist, complain, in_decl),
9871 SCOPED_ENUM_P (template_type), NULL);
9872
9873 if (t == error_mark_node)
9874 return t;
9875 }
9876 else
9877 {
9878 /* We don't want to call start_enum for this type, since
9879 the values for the enumeration constants may involve
9880 template parameters. And, no one should be interested
9881 in the enumeration constants for such a type. */
9882 t = cxx_make_type (ENUMERAL_TYPE);
9883 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9884 }
9885 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9886 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9887 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9888 }
9889 else if (CLASS_TYPE_P (template_type))
9890 {
9891 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9892 instantiated here. */
9893 gcc_assert (!LAMBDA_TYPE_P (template_type));
9894
9895 t = make_class_type (TREE_CODE (template_type));
9896 CLASSTYPE_DECLARED_CLASS (t)
9897 = CLASSTYPE_DECLARED_CLASS (template_type);
9898 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9899
9900 /* A local class. Make sure the decl gets registered properly. */
9901 if (context == current_function_decl)
9902 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9903 == error_mark_node)
9904 return error_mark_node;
9905
9906 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9907 /* This instantiation is another name for the primary
9908 template type. Set the TYPE_CANONICAL field
9909 appropriately. */
9910 TYPE_CANONICAL (t) = template_type;
9911 else if (any_template_arguments_need_structural_equality_p (arglist))
9912 /* Some of the template arguments require structural
9913 equality testing, so this template class requires
9914 structural equality testing. */
9915 SET_TYPE_STRUCTURAL_EQUALITY (t);
9916 }
9917 else
9918 gcc_unreachable ();
9919
9920 /* If we called start_enum or pushtag above, this information
9921 will already be set up. */
9922 type_decl = TYPE_NAME (t);
9923 if (!type_decl)
9924 {
9925 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9926
9927 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9928 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9929 DECL_SOURCE_LOCATION (type_decl)
9930 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9931 }
9932
9933 if (CLASS_TYPE_P (template_type))
9934 {
9935 TREE_PRIVATE (type_decl)
9936 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9937 TREE_PROTECTED (type_decl)
9938 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9939 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9940 {
9941 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9942 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9943 }
9944 }
9945
9946 if (OVERLOAD_TYPE_P (t)
9947 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9948 {
9949 static const char *tags[] = {"abi_tag", "may_alias"};
9950
9951 for (unsigned ix = 0; ix != 2; ix++)
9952 {
9953 tree attributes
9954 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9955
9956 if (attributes)
9957 TYPE_ATTRIBUTES (t)
9958 = tree_cons (TREE_PURPOSE (attributes),
9959 TREE_VALUE (attributes),
9960 TYPE_ATTRIBUTES (t));
9961 }
9962 }
9963
9964 /* Let's consider the explicit specialization of a member
9965 of a class template specialization that is implicitly instantiated,
9966 e.g.:
9967 template<class T>
9968 struct S
9969 {
9970 template<class U> struct M {}; //#0
9971 };
9972
9973 template<>
9974 template<>
9975 struct S<int>::M<char> //#1
9976 {
9977 int i;
9978 };
9979 [temp.expl.spec]/4 says this is valid.
9980
9981 In this case, when we write:
9982 S<int>::M<char> m;
9983
9984 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9985 the one of #0.
9986
9987 When we encounter #1, we want to store the partial instantiation
9988 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9989
9990 For all cases other than this "explicit specialization of member of a
9991 class template", we just want to store the most general template into
9992 the CLASSTYPE_TI_TEMPLATE of M.
9993
9994 This case of "explicit specialization of member of a class template"
9995 only happens when:
9996 1/ the enclosing class is an instantiation of, and therefore not
9997 the same as, the context of the most general template, and
9998 2/ we aren't looking at the partial instantiation itself, i.e.
9999 the innermost arguments are not the same as the innermost parms of
10000 the most general template.
10001
10002 So it's only when 1/ and 2/ happens that we want to use the partial
10003 instantiation of the member template in lieu of its most general
10004 template. */
10005
10006 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10007 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10008 /* the enclosing class must be an instantiation... */
10009 && CLASS_TYPE_P (context)
10010 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10011 {
10012 TREE_VEC_LENGTH (arglist)--;
10013 ++processing_template_decl;
10014 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10015 tree partial_inst_args =
10016 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10017 arglist, complain, NULL_TREE);
10018 --processing_template_decl;
10019 TREE_VEC_LENGTH (arglist)++;
10020 if (partial_inst_args == error_mark_node)
10021 return error_mark_node;
10022 use_partial_inst_tmpl =
10023 /*...and we must not be looking at the partial instantiation
10024 itself. */
10025 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10026 partial_inst_args);
10027 }
10028
10029 if (!use_partial_inst_tmpl)
10030 /* This case is easy; there are no member templates involved. */
10031 found = gen_tmpl;
10032 else
10033 {
10034 /* This is a full instantiation of a member template. Find
10035 the partial instantiation of which this is an instance. */
10036
10037 /* Temporarily reduce by one the number of levels in the ARGLIST
10038 so as to avoid comparing the last set of arguments. */
10039 TREE_VEC_LENGTH (arglist)--;
10040 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
10041 TREE_VEC_LENGTH (arglist)++;
10042 /* FOUND is either a proper class type, or an alias
10043 template specialization. In the later case, it's a
10044 TYPE_DECL, resulting from the substituting of arguments
10045 for parameters in the TYPE_DECL of the alias template
10046 done earlier. So be careful while getting the template
10047 of FOUND. */
10048 found = (TREE_CODE (found) == TEMPLATE_DECL
10049 ? found
10050 : (TREE_CODE (found) == TYPE_DECL
10051 ? DECL_TI_TEMPLATE (found)
10052 : CLASSTYPE_TI_TEMPLATE (found)));
10053
10054 if (DECL_CLASS_TEMPLATE_P (found)
10055 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10056 {
10057 /* If this partial instantiation is specialized, we want to
10058 use it for hash table lookup. */
10059 elt.tmpl = found;
10060 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10061 hash = spec_hasher::hash (&elt);
10062 }
10063 }
10064
10065 /* Build template info for the new specialization. This can
10066 overwrite the existing TEMPLATE_INFO for T (that points to
10067 its instantiated TEMPLATE_DECL), with this one that points to
10068 the most general template, but that's what we want. */
10069
10070 if (TYPE_ALIAS_P (t))
10071 {
10072 /* This should already have been constructed during
10073 instantiation of the alias decl. */
10074 tree ti = DECL_TEMPLATE_INFO (TYPE_NAME (t));
10075 gcc_checking_assert (template_args_equal (TI_ARGS (ti), arglist)
10076 && TI_TEMPLATE (ti) == found);
10077 }
10078 else
10079 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10080
10081 elt.spec = t;
10082 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10083 gcc_checking_assert (*slot == NULL);
10084 entry = ggc_alloc<spec_entry> ();
10085 *entry = elt;
10086 *slot = entry;
10087
10088 /* Note this use of the partial instantiation so we can check it
10089 later in maybe_process_partial_specialization. */
10090 DECL_TEMPLATE_INSTANTIATIONS (found)
10091 = tree_cons (arglist, t,
10092 DECL_TEMPLATE_INSTANTIATIONS (found));
10093
10094 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10095 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10096 /* Now that the type has been registered on the instantiations
10097 list, we set up the enumerators. Because the enumeration
10098 constants may involve the enumeration type itself, we make
10099 sure to register the type first, and then create the
10100 constants. That way, doing tsubst_expr for the enumeration
10101 constants won't result in recursive calls here; we'll find
10102 the instantiation and exit above. */
10103 tsubst_enum (template_type, t, arglist);
10104
10105 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10106 /* If the type makes use of template parameters, the
10107 code that generates debugging information will crash. */
10108 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10109
10110 /* Possibly limit visibility based on template args. */
10111 TREE_PUBLIC (type_decl) = 1;
10112 determine_visibility (type_decl);
10113
10114 inherit_targ_abi_tags (t);
10115
10116 return t;
10117 }
10118 }
10119
10120 /* Wrapper for lookup_template_class_1. */
10121
10122 tree
10123 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10124 int entering_scope, tsubst_flags_t complain)
10125 {
10126 tree ret;
10127 timevar_push (TV_TEMPLATE_INST);
10128 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10129 entering_scope, complain);
10130 timevar_pop (TV_TEMPLATE_INST);
10131 return ret;
10132 }
10133
10134 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10135
10136 tree
10137 lookup_template_variable (tree templ, tree arglist)
10138 {
10139 if (flag_concepts && variable_concept_p (templ))
10140 return build_concept_check (templ, arglist, tf_none);
10141
10142 /* The type of the expression is NULL_TREE since the template-id could refer
10143 to an explicit or partial specialization. */
10144 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10145 }
10146
10147 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10148
10149 tree
10150 finish_template_variable (tree var, tsubst_flags_t complain)
10151 {
10152 tree templ = TREE_OPERAND (var, 0);
10153 tree arglist = TREE_OPERAND (var, 1);
10154
10155 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10156 arglist = add_outermost_template_args (tmpl_args, arglist);
10157
10158 templ = most_general_template (templ);
10159 tree parms = DECL_TEMPLATE_PARMS (templ);
10160 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10161 /*req_all*/true,
10162 /*use_default*/true);
10163
10164 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10165 {
10166 if (complain & tf_error)
10167 {
10168 auto_diagnostic_group d;
10169 error ("use of invalid variable template %qE", var);
10170 diagnose_constraints (location_of (var), templ, arglist);
10171 }
10172 return error_mark_node;
10173 }
10174
10175 return instantiate_template (templ, arglist, complain);
10176 }
10177
10178 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10179 TARGS template args, and instantiate it if it's not dependent. */
10180
10181 tree
10182 lookup_and_finish_template_variable (tree templ, tree targs,
10183 tsubst_flags_t complain)
10184 {
10185 templ = lookup_template_variable (templ, targs);
10186 if (!any_dependent_template_arguments_p (targs))
10187 {
10188 templ = finish_template_variable (templ, complain);
10189 mark_used (templ);
10190 }
10191
10192 return convert_from_reference (templ);
10193 }
10194
10195 \f
10196 struct pair_fn_data
10197 {
10198 tree_fn_t fn;
10199 tree_fn_t any_fn;
10200 void *data;
10201 /* True when we should also visit template parameters that occur in
10202 non-deduced contexts. */
10203 bool include_nondeduced_p;
10204 hash_set<tree> *visited;
10205 };
10206
10207 /* Called from for_each_template_parm via walk_tree. */
10208
10209 static tree
10210 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10211 {
10212 tree t = *tp;
10213 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10214 tree_fn_t fn = pfd->fn;
10215 void *data = pfd->data;
10216 tree result = NULL_TREE;
10217
10218 #define WALK_SUBTREE(NODE) \
10219 do \
10220 { \
10221 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10222 pfd->include_nondeduced_p, \
10223 pfd->any_fn); \
10224 if (result) goto out; \
10225 } \
10226 while (0)
10227
10228 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10229 return t;
10230
10231 if (TYPE_P (t)
10232 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10233 WALK_SUBTREE (TYPE_CONTEXT (t));
10234
10235 switch (TREE_CODE (t))
10236 {
10237 case RECORD_TYPE:
10238 if (TYPE_PTRMEMFUNC_P (t))
10239 break;
10240 /* Fall through. */
10241
10242 case UNION_TYPE:
10243 case ENUMERAL_TYPE:
10244 if (!TYPE_TEMPLATE_INFO (t))
10245 *walk_subtrees = 0;
10246 else
10247 WALK_SUBTREE (TYPE_TI_ARGS (t));
10248 break;
10249
10250 case INTEGER_TYPE:
10251 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10252 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10253 break;
10254
10255 case METHOD_TYPE:
10256 /* Since we're not going to walk subtrees, we have to do this
10257 explicitly here. */
10258 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10259 /* Fall through. */
10260
10261 case FUNCTION_TYPE:
10262 /* Check the return type. */
10263 WALK_SUBTREE (TREE_TYPE (t));
10264
10265 /* Check the parameter types. Since default arguments are not
10266 instantiated until they are needed, the TYPE_ARG_TYPES may
10267 contain expressions that involve template parameters. But,
10268 no-one should be looking at them yet. And, once they're
10269 instantiated, they don't contain template parameters, so
10270 there's no point in looking at them then, either. */
10271 {
10272 tree parm;
10273
10274 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10275 WALK_SUBTREE (TREE_VALUE (parm));
10276
10277 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10278 want walk_tree walking into them itself. */
10279 *walk_subtrees = 0;
10280 }
10281
10282 if (flag_noexcept_type)
10283 {
10284 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10285 if (spec)
10286 WALK_SUBTREE (TREE_PURPOSE (spec));
10287 }
10288 break;
10289
10290 case TYPEOF_TYPE:
10291 case DECLTYPE_TYPE:
10292 case UNDERLYING_TYPE:
10293 if (pfd->include_nondeduced_p
10294 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10295 pfd->visited,
10296 pfd->include_nondeduced_p,
10297 pfd->any_fn))
10298 return error_mark_node;
10299 *walk_subtrees = false;
10300 break;
10301
10302 case FUNCTION_DECL:
10303 case VAR_DECL:
10304 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10305 WALK_SUBTREE (DECL_TI_ARGS (t));
10306 /* Fall through. */
10307
10308 case PARM_DECL:
10309 case CONST_DECL:
10310 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10311 WALK_SUBTREE (DECL_INITIAL (t));
10312 if (DECL_CONTEXT (t)
10313 && pfd->include_nondeduced_p)
10314 WALK_SUBTREE (DECL_CONTEXT (t));
10315 break;
10316
10317 case BOUND_TEMPLATE_TEMPLATE_PARM:
10318 /* Record template parameters such as `T' inside `TT<T>'. */
10319 WALK_SUBTREE (TYPE_TI_ARGS (t));
10320 /* Fall through. */
10321
10322 case TEMPLATE_TEMPLATE_PARM:
10323 case TEMPLATE_TYPE_PARM:
10324 case TEMPLATE_PARM_INDEX:
10325 if (fn && (*fn)(t, data))
10326 return t;
10327 else if (!fn)
10328 return t;
10329 break;
10330
10331 case TEMPLATE_DECL:
10332 /* A template template parameter is encountered. */
10333 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10334 WALK_SUBTREE (TREE_TYPE (t));
10335
10336 /* Already substituted template template parameter */
10337 *walk_subtrees = 0;
10338 break;
10339
10340 case TYPENAME_TYPE:
10341 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10342 partial instantiation. */
10343 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10344 *walk_subtrees = 0;
10345 break;
10346
10347 case CONSTRUCTOR:
10348 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10349 && pfd->include_nondeduced_p)
10350 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10351 break;
10352
10353 case INDIRECT_REF:
10354 case COMPONENT_REF:
10355 /* If there's no type, then this thing must be some expression
10356 involving template parameters. */
10357 if (!fn && !TREE_TYPE (t))
10358 return error_mark_node;
10359 break;
10360
10361 case MODOP_EXPR:
10362 case CAST_EXPR:
10363 case IMPLICIT_CONV_EXPR:
10364 case REINTERPRET_CAST_EXPR:
10365 case CONST_CAST_EXPR:
10366 case STATIC_CAST_EXPR:
10367 case DYNAMIC_CAST_EXPR:
10368 case ARROW_EXPR:
10369 case DOTSTAR_EXPR:
10370 case TYPEID_EXPR:
10371 case PSEUDO_DTOR_EXPR:
10372 if (!fn)
10373 return error_mark_node;
10374 break;
10375
10376 case SCOPE_REF:
10377 if (pfd->include_nondeduced_p)
10378 WALK_SUBTREE (TREE_OPERAND (t, 0));
10379 break;
10380
10381 case REQUIRES_EXPR:
10382 {
10383 if (!fn)
10384 return error_mark_node;
10385
10386 /* Recursively walk the type of each constraint variable. */
10387 tree p = TREE_OPERAND (t, 0);
10388 while (p)
10389 {
10390 WALK_SUBTREE (TREE_TYPE (p));
10391 p = TREE_CHAIN (p);
10392 }
10393 }
10394 break;
10395
10396 default:
10397 break;
10398 }
10399
10400 #undef WALK_SUBTREE
10401
10402 /* We didn't find any template parameters we liked. */
10403 out:
10404 return result;
10405 }
10406
10407 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10408 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10409 call FN with the parameter and the DATA.
10410 If FN returns nonzero, the iteration is terminated, and
10411 for_each_template_parm returns 1. Otherwise, the iteration
10412 continues. If FN never returns a nonzero value, the value
10413 returned by for_each_template_parm is 0. If FN is NULL, it is
10414 considered to be the function which always returns 1.
10415
10416 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10417 parameters that occur in non-deduced contexts. When false, only
10418 visits those template parameters that can be deduced. */
10419
10420 static tree
10421 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10422 hash_set<tree> *visited,
10423 bool include_nondeduced_p,
10424 tree_fn_t any_fn)
10425 {
10426 struct pair_fn_data pfd;
10427 tree result;
10428
10429 /* Set up. */
10430 pfd.fn = fn;
10431 pfd.any_fn = any_fn;
10432 pfd.data = data;
10433 pfd.include_nondeduced_p = include_nondeduced_p;
10434
10435 /* Walk the tree. (Conceptually, we would like to walk without
10436 duplicates, but for_each_template_parm_r recursively calls
10437 for_each_template_parm, so we would need to reorganize a fair
10438 bit to use walk_tree_without_duplicates, so we keep our own
10439 visited list.) */
10440 if (visited)
10441 pfd.visited = visited;
10442 else
10443 pfd.visited = new hash_set<tree>;
10444 result = cp_walk_tree (&t,
10445 for_each_template_parm_r,
10446 &pfd,
10447 pfd.visited);
10448
10449 /* Clean up. */
10450 if (!visited)
10451 {
10452 delete pfd.visited;
10453 pfd.visited = 0;
10454 }
10455
10456 return result;
10457 }
10458
10459 struct find_template_parameter_info
10460 {
10461 explicit find_template_parameter_info (tree ctx_parms)
10462 : parm_list (NULL_TREE),
10463 ctx_parms (ctx_parms),
10464 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10465 {}
10466
10467 hash_set<tree> visited;
10468 hash_set<tree> parms;
10469 tree parm_list;
10470 tree ctx_parms;
10471 int max_depth;
10472 };
10473
10474 /* Appends the declaration of T to the list in DATA. */
10475
10476 static int
10477 keep_template_parm (tree t, void* data)
10478 {
10479 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10480
10481 /* Template parameters declared within the expression are not part of
10482 the parameter mapping. For example, in this concept:
10483
10484 template<typename T>
10485 concept C = requires { <expr> } -> same_as<int>;
10486
10487 the return specifier same_as<int> declares a new decltype parameter
10488 that must not be part of the parameter mapping. The same is true
10489 for generic lambda parameters, lambda template parameters, etc. */
10490 int level;
10491 int index;
10492 template_parm_level_and_index (t, &level, &index);
10493 if (level > ftpi->max_depth)
10494 return 0;
10495
10496 /* Arguments like const T yield parameters like const T. This means that
10497 a template-id like X<T, const T> would yield two distinct parameters:
10498 T and const T. Adjust types to their unqualified versions. */
10499 if (TYPE_P (t))
10500 t = TYPE_MAIN_VARIANT (t);
10501 if (!ftpi->parms.add (t))
10502 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10503
10504 return 0;
10505 }
10506
10507 /* Ensure that we recursively examine certain terms that are not normally
10508 visited in for_each_template_parm_r. */
10509
10510 static int
10511 any_template_parm_r (tree t, void *data)
10512 {
10513 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10514
10515 #define WALK_SUBTREE(NODE) \
10516 do \
10517 { \
10518 for_each_template_parm (NODE, keep_template_parm, data, \
10519 &ftpi->visited, true, \
10520 any_template_parm_r); \
10521 } \
10522 while (0)
10523
10524 /* A mention of a member alias/typedef is a use of all of its template
10525 arguments, including those from the enclosing class, so we don't use
10526 alias_template_specialization_p here. */
10527 if (TYPE_P (t) && typedef_variant_p (t))
10528 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10529 WALK_SUBTREE (TI_ARGS (tinfo));
10530
10531 switch (TREE_CODE (t))
10532 {
10533 case TEMPLATE_TYPE_PARM:
10534 /* Type constraints of a placeholder type may contain parameters. */
10535 if (is_auto (t))
10536 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10537 WALK_SUBTREE (constr);
10538 break;
10539
10540 case TEMPLATE_ID_EXPR:
10541 /* Search through references to variable templates. */
10542 WALK_SUBTREE (TREE_OPERAND (t, 0));
10543 WALK_SUBTREE (TREE_OPERAND (t, 1));
10544 break;
10545
10546 case TEMPLATE_PARM_INDEX:
10547 case PARM_DECL:
10548 /* A parameter or constraint variable may also depend on a template
10549 parameter without explicitly naming it. */
10550 WALK_SUBTREE (TREE_TYPE (t));
10551 break;
10552
10553 case TEMPLATE_DECL:
10554 {
10555 /* If T is a member template that shares template parameters with
10556 ctx_parms, we need to mark all those parameters for mapping. */
10557 tree dparms = DECL_TEMPLATE_PARMS (t);
10558 tree cparms = ftpi->ctx_parms;
10559 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10560 dparms = TREE_CHAIN (dparms);
10561 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10562 cparms = TREE_CHAIN (cparms);
10563 while (dparms
10564 && (TREE_TYPE (TREE_VALUE (dparms))
10565 != TREE_TYPE (TREE_VALUE (cparms))))
10566 dparms = TREE_CHAIN (dparms),
10567 cparms = TREE_CHAIN (cparms);
10568 if (dparms)
10569 {
10570 int ddepth = TMPL_PARMS_DEPTH (dparms);
10571 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10572 for (int i = 0; i < ddepth; ++i)
10573 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10574 }
10575 }
10576 break;
10577
10578 case LAMBDA_EXPR:
10579 {
10580 /* Look in the parms and body. */
10581 tree fn = lambda_function (t);
10582 WALK_SUBTREE (TREE_TYPE (fn));
10583 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10584 }
10585 break;
10586
10587 case IDENTIFIER_NODE:
10588 if (IDENTIFIER_CONV_OP_P (t))
10589 /* The conversion-type-id of a conversion operator may be dependent. */
10590 WALK_SUBTREE (TREE_TYPE (t));
10591 break;
10592
10593 default:
10594 break;
10595 }
10596
10597 /* Keep walking. */
10598 return 0;
10599 }
10600
10601 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10602 are the template parameters in scope. */
10603
10604 tree
10605 find_template_parameters (tree t, tree ctx_parms)
10606 {
10607 if (!ctx_parms)
10608 return NULL_TREE;
10609
10610 find_template_parameter_info ftpi (ctx_parms);
10611 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10612 /*include_nondeduced*/true, any_template_parm_r);
10613 return ftpi.parm_list;
10614 }
10615
10616 /* Returns true if T depends on any template parameter. */
10617
10618 int
10619 uses_template_parms (tree t)
10620 {
10621 if (t == NULL_TREE)
10622 return false;
10623
10624 bool dependent_p;
10625 int saved_processing_template_decl;
10626
10627 saved_processing_template_decl = processing_template_decl;
10628 if (!saved_processing_template_decl)
10629 processing_template_decl = 1;
10630 if (TYPE_P (t))
10631 dependent_p = dependent_type_p (t);
10632 else if (TREE_CODE (t) == TREE_VEC)
10633 dependent_p = any_dependent_template_arguments_p (t);
10634 else if (TREE_CODE (t) == TREE_LIST)
10635 dependent_p = (uses_template_parms (TREE_VALUE (t))
10636 || uses_template_parms (TREE_CHAIN (t)));
10637 else if (TREE_CODE (t) == TYPE_DECL)
10638 dependent_p = dependent_type_p (TREE_TYPE (t));
10639 else if (t == error_mark_node)
10640 dependent_p = false;
10641 else
10642 dependent_p = value_dependent_expression_p (t);
10643
10644 processing_template_decl = saved_processing_template_decl;
10645
10646 return dependent_p;
10647 }
10648
10649 /* Returns true iff current_function_decl is an incompletely instantiated
10650 template. Useful instead of processing_template_decl because the latter
10651 is set to 0 during instantiate_non_dependent_expr. */
10652
10653 bool
10654 in_template_function (void)
10655 {
10656 tree fn = current_function_decl;
10657 bool ret;
10658 ++processing_template_decl;
10659 ret = (fn && DECL_LANG_SPECIFIC (fn)
10660 && DECL_TEMPLATE_INFO (fn)
10661 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10662 --processing_template_decl;
10663 return ret;
10664 }
10665
10666 /* Returns true if T depends on any template parameter with level LEVEL. */
10667
10668 bool
10669 uses_template_parms_level (tree t, int level)
10670 {
10671 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10672 /*include_nondeduced_p=*/true);
10673 }
10674
10675 /* Returns true if the signature of DECL depends on any template parameter from
10676 its enclosing class. */
10677
10678 bool
10679 uses_outer_template_parms (tree decl)
10680 {
10681 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10682 if (depth == 0)
10683 return false;
10684 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10685 &depth, NULL, /*include_nondeduced_p=*/true))
10686 return true;
10687 if (PRIMARY_TEMPLATE_P (decl)
10688 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10689 (DECL_TEMPLATE_PARMS (decl)),
10690 template_parm_outer_level,
10691 &depth, NULL, /*include_nondeduced_p=*/true))
10692 return true;
10693 tree ci = get_constraints (decl);
10694 if (ci)
10695 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10696 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10697 &depth, NULL, /*nondeduced*/true))
10698 return true;
10699 return false;
10700 }
10701
10702 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10703 ill-formed translation unit, i.e. a variable or function that isn't
10704 usable in a constant expression. */
10705
10706 static inline bool
10707 neglectable_inst_p (tree d)
10708 {
10709 return (d && DECL_P (d)
10710 && !undeduced_auto_decl (d)
10711 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10712 : decl_maybe_constant_var_p (d)));
10713 }
10714
10715 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10716 neglectable and instantiated from within an erroneous instantiation. */
10717
10718 static bool
10719 limit_bad_template_recursion (tree decl)
10720 {
10721 struct tinst_level *lev = current_tinst_level;
10722 int errs = errorcount + sorrycount;
10723 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10724 return false;
10725
10726 for (; lev; lev = lev->next)
10727 if (neglectable_inst_p (lev->maybe_get_node ()))
10728 break;
10729
10730 return (lev && errs > lev->errors);
10731 }
10732
10733 static int tinst_depth;
10734 extern int max_tinst_depth;
10735 int depth_reached;
10736
10737 static GTY(()) struct tinst_level *last_error_tinst_level;
10738
10739 /* We're starting to instantiate D; record the template instantiation context
10740 at LOC for diagnostics and to restore it later. */
10741
10742 static bool
10743 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10744 {
10745 struct tinst_level *new_level;
10746
10747 if (tinst_depth >= max_tinst_depth)
10748 {
10749 /* Tell error.c not to try to instantiate any templates. */
10750 at_eof = 2;
10751 fatal_error (input_location,
10752 "template instantiation depth exceeds maximum of %d"
10753 " (use %<-ftemplate-depth=%> to increase the maximum)",
10754 max_tinst_depth);
10755 return false;
10756 }
10757
10758 /* If the current instantiation caused problems, don't let it instantiate
10759 anything else. Do allow deduction substitution and decls usable in
10760 constant expressions. */
10761 if (!targs && limit_bad_template_recursion (tldcl))
10762 {
10763 /* Avoid no_linkage_errors and unused function warnings for this
10764 decl. */
10765 TREE_NO_WARNING (tldcl) = 1;
10766 return false;
10767 }
10768
10769 /* When not -quiet, dump template instantiations other than functions, since
10770 announce_function will take care of those. */
10771 if (!quiet_flag && !targs
10772 && TREE_CODE (tldcl) != TREE_LIST
10773 && TREE_CODE (tldcl) != FUNCTION_DECL)
10774 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10775
10776 new_level = tinst_level_freelist ().alloc ();
10777 new_level->tldcl = tldcl;
10778 new_level->targs = targs;
10779 new_level->locus = loc;
10780 new_level->errors = errorcount + sorrycount;
10781 new_level->next = NULL;
10782 new_level->refcount = 0;
10783 set_refcount_ptr (new_level->next, current_tinst_level);
10784 set_refcount_ptr (current_tinst_level, new_level);
10785
10786 ++tinst_depth;
10787 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10788 depth_reached = tinst_depth;
10789
10790 return true;
10791 }
10792
10793 /* We're starting substitution of TMPL<ARGS>; record the template
10794 substitution context for diagnostics and to restore it later. */
10795
10796 static bool
10797 push_tinst_level (tree tmpl, tree args)
10798 {
10799 return push_tinst_level_loc (tmpl, args, input_location);
10800 }
10801
10802 /* We're starting to instantiate D; record INPUT_LOCATION and the
10803 template instantiation context for diagnostics and to restore it
10804 later. */
10805
10806 bool
10807 push_tinst_level (tree d)
10808 {
10809 return push_tinst_level_loc (d, input_location);
10810 }
10811
10812 /* Likewise, but record LOC as the program location. */
10813
10814 bool
10815 push_tinst_level_loc (tree d, location_t loc)
10816 {
10817 gcc_assert (TREE_CODE (d) != TREE_LIST);
10818 return push_tinst_level_loc (d, NULL, loc);
10819 }
10820
10821 /* We're done instantiating this template; return to the instantiation
10822 context. */
10823
10824 void
10825 pop_tinst_level (void)
10826 {
10827 /* Restore the filename and line number stashed away when we started
10828 this instantiation. */
10829 input_location = current_tinst_level->locus;
10830 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10831 --tinst_depth;
10832 }
10833
10834 /* We're instantiating a deferred template; restore the template
10835 instantiation context in which the instantiation was requested, which
10836 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10837
10838 static tree
10839 reopen_tinst_level (struct tinst_level *level)
10840 {
10841 struct tinst_level *t;
10842
10843 tinst_depth = 0;
10844 for (t = level; t; t = t->next)
10845 ++tinst_depth;
10846
10847 set_refcount_ptr (current_tinst_level, level);
10848 pop_tinst_level ();
10849 if (current_tinst_level)
10850 current_tinst_level->errors = errorcount+sorrycount;
10851 return level->maybe_get_node ();
10852 }
10853
10854 /* Returns the TINST_LEVEL which gives the original instantiation
10855 context. */
10856
10857 struct tinst_level *
10858 outermost_tinst_level (void)
10859 {
10860 struct tinst_level *level = current_tinst_level;
10861 if (level)
10862 while (level->next)
10863 level = level->next;
10864 return level;
10865 }
10866
10867 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10868 vector of template arguments, as for tsubst.
10869
10870 Returns an appropriate tsubst'd friend declaration. */
10871
10872 static tree
10873 tsubst_friend_function (tree decl, tree args)
10874 {
10875 tree new_friend;
10876
10877 if (TREE_CODE (decl) == FUNCTION_DECL
10878 && DECL_TEMPLATE_INSTANTIATION (decl)
10879 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10880 /* This was a friend declared with an explicit template
10881 argument list, e.g.:
10882
10883 friend void f<>(T);
10884
10885 to indicate that f was a template instantiation, not a new
10886 function declaration. Now, we have to figure out what
10887 instantiation of what template. */
10888 {
10889 tree template_id, arglist, fns;
10890 tree new_args;
10891 tree tmpl;
10892 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10893
10894 /* Friend functions are looked up in the containing namespace scope.
10895 We must enter that scope, to avoid finding member functions of the
10896 current class with same name. */
10897 push_nested_namespace (ns);
10898 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10899 tf_warning_or_error, NULL_TREE,
10900 /*integral_constant_expression_p=*/false);
10901 pop_nested_namespace (ns);
10902 arglist = tsubst (DECL_TI_ARGS (decl), args,
10903 tf_warning_or_error, NULL_TREE);
10904 template_id = lookup_template_function (fns, arglist);
10905
10906 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10907 tmpl = determine_specialization (template_id, new_friend,
10908 &new_args,
10909 /*need_member_template=*/0,
10910 TREE_VEC_LENGTH (args),
10911 tsk_none);
10912 return instantiate_template (tmpl, new_args, tf_error);
10913 }
10914
10915 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10916 if (new_friend == error_mark_node)
10917 return error_mark_node;
10918
10919 /* The NEW_FRIEND will look like an instantiation, to the
10920 compiler, but is not an instantiation from the point of view of
10921 the language. For example, we might have had:
10922
10923 template <class T> struct S {
10924 template <class U> friend void f(T, U);
10925 };
10926
10927 Then, in S<int>, template <class U> void f(int, U) is not an
10928 instantiation of anything. */
10929
10930 DECL_USE_TEMPLATE (new_friend) = 0;
10931 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10932 {
10933 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10934 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10935 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10936
10937 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10938 match in decls_match. */
10939 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10940 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
10941 treqs = maybe_substitute_reqs_for (treqs, new_friend);
10942 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10943 }
10944
10945 /* The mangled name for the NEW_FRIEND is incorrect. The function
10946 is not a template instantiation and should not be mangled like
10947 one. Therefore, we forget the mangling here; we'll recompute it
10948 later if we need it. */
10949 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10950 {
10951 SET_DECL_RTL (new_friend, NULL);
10952 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10953 }
10954
10955 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10956 {
10957 tree old_decl;
10958 tree ns;
10959
10960 /* We must save some information from NEW_FRIEND before calling
10961 duplicate decls since that function will free NEW_FRIEND if
10962 possible. */
10963 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10964 tree new_friend_result_template_info = NULL_TREE;
10965 bool new_friend_is_defn =
10966 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10967 (template_for_substitution (new_friend)))
10968 != NULL_TREE);
10969 tree not_tmpl = new_friend;
10970
10971 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10972 {
10973 /* This declaration is a `primary' template. */
10974 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10975
10976 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
10977 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
10978 }
10979
10980 /* Inside pushdecl_namespace_level, we will push into the
10981 current namespace. However, the friend function should go
10982 into the namespace of the template. */
10983 ns = decl_namespace_context (new_friend);
10984 push_nested_namespace (ns);
10985 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10986 pop_nested_namespace (ns);
10987
10988 if (old_decl == error_mark_node)
10989 return error_mark_node;
10990
10991 if (old_decl != new_friend)
10992 {
10993 /* This new friend declaration matched an existing
10994 declaration. For example, given:
10995
10996 template <class T> void f(T);
10997 template <class U> class C {
10998 template <class T> friend void f(T) {}
10999 };
11000
11001 the friend declaration actually provides the definition
11002 of `f', once C has been instantiated for some type. So,
11003 old_decl will be the out-of-class template declaration,
11004 while new_friend is the in-class definition.
11005
11006 But, if `f' was called before this point, the
11007 instantiation of `f' will have DECL_TI_ARGS corresponding
11008 to `T' but not to `U', references to which might appear
11009 in the definition of `f'. Previously, the most general
11010 template for an instantiation of `f' was the out-of-class
11011 version; now it is the in-class version. Therefore, we
11012 run through all specialization of `f', adding to their
11013 DECL_TI_ARGS appropriately. In particular, they need a
11014 new set of outer arguments, corresponding to the
11015 arguments for this class instantiation.
11016
11017 The same situation can arise with something like this:
11018
11019 friend void f(int);
11020 template <class T> class C {
11021 friend void f(T) {}
11022 };
11023
11024 when `C<int>' is instantiated. Now, `f(int)' is defined
11025 in the class. */
11026
11027 if (!new_friend_is_defn)
11028 /* On the other hand, if the in-class declaration does
11029 *not* provide a definition, then we don't want to alter
11030 existing definitions. We can just leave everything
11031 alone. */
11032 ;
11033 else
11034 {
11035 tree new_template = TI_TEMPLATE (new_friend_template_info);
11036 tree new_args = TI_ARGS (new_friend_template_info);
11037
11038 /* Overwrite whatever template info was there before, if
11039 any, with the new template information pertaining to
11040 the declaration. */
11041 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11042
11043 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11044 {
11045 /* We should have called reregister_specialization in
11046 duplicate_decls. */
11047 gcc_assert (retrieve_specialization (new_template,
11048 new_args, 0)
11049 == old_decl);
11050
11051 /* Instantiate it if the global has already been used. */
11052 if (DECL_ODR_USED (old_decl))
11053 instantiate_decl (old_decl, /*defer_ok=*/true,
11054 /*expl_inst_class_mem_p=*/false);
11055 }
11056 else
11057 {
11058 tree t;
11059
11060 /* Indicate that the old function template is a partial
11061 instantiation. */
11062 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11063 = new_friend_result_template_info;
11064
11065 gcc_assert (new_template
11066 == most_general_template (new_template));
11067 gcc_assert (new_template != old_decl);
11068
11069 /* Reassign any specializations already in the hash table
11070 to the new more general template, and add the
11071 additional template args. */
11072 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11073 t != NULL_TREE;
11074 t = TREE_CHAIN (t))
11075 {
11076 tree spec = TREE_VALUE (t);
11077 spec_entry elt;
11078
11079 elt.tmpl = old_decl;
11080 elt.args = DECL_TI_ARGS (spec);
11081 elt.spec = NULL_TREE;
11082
11083 decl_specializations->remove_elt (&elt);
11084
11085 DECL_TI_ARGS (spec)
11086 = add_outermost_template_args (new_args,
11087 DECL_TI_ARGS (spec));
11088
11089 register_specialization
11090 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11091
11092 }
11093 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11094 }
11095 }
11096
11097 /* The information from NEW_FRIEND has been merged into OLD_DECL
11098 by duplicate_decls. */
11099 new_friend = old_decl;
11100 }
11101 }
11102 else
11103 {
11104 tree context = DECL_CONTEXT (new_friend);
11105 bool dependent_p;
11106
11107 /* In the code
11108 template <class T> class C {
11109 template <class U> friend void C1<U>::f (); // case 1
11110 friend void C2<T>::f (); // case 2
11111 };
11112 we only need to make sure CONTEXT is a complete type for
11113 case 2. To distinguish between the two cases, we note that
11114 CONTEXT of case 1 remains dependent type after tsubst while
11115 this isn't true for case 2. */
11116 ++processing_template_decl;
11117 dependent_p = dependent_type_p (context);
11118 --processing_template_decl;
11119
11120 if (!dependent_p
11121 && !complete_type_or_else (context, NULL_TREE))
11122 return error_mark_node;
11123
11124 if (COMPLETE_TYPE_P (context))
11125 {
11126 tree fn = new_friend;
11127 /* do_friend adds the TEMPLATE_DECL for any member friend
11128 template even if it isn't a member template, i.e.
11129 template <class T> friend A<T>::f();
11130 Look through it in that case. */
11131 if (TREE_CODE (fn) == TEMPLATE_DECL
11132 && !PRIMARY_TEMPLATE_P (fn))
11133 fn = DECL_TEMPLATE_RESULT (fn);
11134 /* Check to see that the declaration is really present, and,
11135 possibly obtain an improved declaration. */
11136 fn = check_classfn (context, fn, NULL_TREE);
11137
11138 if (fn)
11139 new_friend = fn;
11140 }
11141 }
11142
11143 return new_friend;
11144 }
11145
11146 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11147 template arguments, as for tsubst.
11148
11149 Returns an appropriate tsubst'd friend type or error_mark_node on
11150 failure. */
11151
11152 static tree
11153 tsubst_friend_class (tree friend_tmpl, tree args)
11154 {
11155 tree tmpl;
11156
11157 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11158 {
11159 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11160 return TREE_TYPE (tmpl);
11161 }
11162
11163 tree context = CP_DECL_CONTEXT (friend_tmpl);
11164 if (TREE_CODE (context) == NAMESPACE_DECL)
11165 push_nested_namespace (context);
11166 else
11167 {
11168 context = tsubst (context, args, tf_error, NULL_TREE);
11169 push_nested_class (context);
11170 }
11171
11172 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
11173 /*non_class=*/false, /*block_p=*/false,
11174 /*namespaces_only=*/false, LOOKUP_HIDDEN);
11175
11176 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11177 {
11178 /* The friend template has already been declared. Just
11179 check to see that the declarations match, and install any new
11180 default parameters. We must tsubst the default parameters,
11181 of course. We only need the innermost template parameters
11182 because that is all that redeclare_class_template will look
11183 at. */
11184 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11185 > TMPL_ARGS_DEPTH (args))
11186 {
11187 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11188 args, tf_warning_or_error);
11189 location_t saved_input_location = input_location;
11190 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11191 tree cons = get_constraints (tmpl);
11192 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11193 input_location = saved_input_location;
11194 }
11195 }
11196 else
11197 {
11198 /* The friend template has not already been declared. In this
11199 case, the instantiation of the template class will cause the
11200 injection of this template into the namespace scope. */
11201 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11202
11203 if (tmpl != error_mark_node)
11204 {
11205 /* The new TMPL is not an instantiation of anything, so we
11206 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11207 for the new type because that is supposed to be the
11208 corresponding template decl, i.e., TMPL. */
11209 DECL_USE_TEMPLATE (tmpl) = 0;
11210 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11211 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11212 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11213 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11214
11215 /* It is hidden. */
11216 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11217 DECL_ANTICIPATED (tmpl)
11218 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11219
11220 /* Inject this template into the enclosing namspace scope. */
11221 tmpl = pushdecl_namespace_level (tmpl, true);
11222 }
11223 }
11224
11225 if (TREE_CODE (context) == NAMESPACE_DECL)
11226 pop_nested_namespace (context);
11227 else
11228 pop_nested_class ();
11229
11230 return TREE_TYPE (tmpl);
11231 }
11232
11233 /* Returns zero if TYPE cannot be completed later due to circularity.
11234 Otherwise returns one. */
11235
11236 static int
11237 can_complete_type_without_circularity (tree type)
11238 {
11239 if (type == NULL_TREE || type == error_mark_node)
11240 return 0;
11241 else if (COMPLETE_TYPE_P (type))
11242 return 1;
11243 else if (TREE_CODE (type) == ARRAY_TYPE)
11244 return can_complete_type_without_circularity (TREE_TYPE (type));
11245 else if (CLASS_TYPE_P (type)
11246 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11247 return 0;
11248 else
11249 return 1;
11250 }
11251
11252 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11253 tsubst_flags_t, tree);
11254
11255 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11256 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11257
11258 static tree
11259 tsubst_attribute (tree t, tree *decl_p, tree args,
11260 tsubst_flags_t complain, tree in_decl)
11261 {
11262 gcc_assert (ATTR_IS_DEPENDENT (t));
11263
11264 tree val = TREE_VALUE (t);
11265 if (val == NULL_TREE)
11266 /* Nothing to do. */;
11267 else if ((flag_openmp || flag_openmp_simd)
11268 && is_attribute_p ("omp declare simd",
11269 get_attribute_name (t)))
11270 {
11271 tree clauses = TREE_VALUE (val);
11272 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11273 complain, in_decl);
11274 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11275 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11276 tree parms = DECL_ARGUMENTS (*decl_p);
11277 clauses
11278 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11279 if (clauses)
11280 val = build_tree_list (NULL_TREE, clauses);
11281 else
11282 val = NULL_TREE;
11283 }
11284 else if (flag_openmp
11285 && is_attribute_p ("omp declare variant base",
11286 get_attribute_name (t)))
11287 {
11288 ++cp_unevaluated_operand;
11289 tree varid
11290 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11291 in_decl, /*integral_constant_expression_p=*/false);
11292 --cp_unevaluated_operand;
11293 tree chain = TREE_CHAIN (val);
11294 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11295 tree ctx = copy_list (TREE_VALUE (val));
11296 tree simd = get_identifier ("simd");
11297 tree score = get_identifier (" score");
11298 tree condition = get_identifier ("condition");
11299 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11300 {
11301 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11302 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11303 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11304 {
11305 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11306 {
11307 tree clauses = TREE_VALUE (t2);
11308 clauses = tsubst_omp_clauses (clauses,
11309 C_ORT_OMP_DECLARE_SIMD, args,
11310 complain, in_decl);
11311 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11312 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11313 TREE_VALUE (t2) = clauses;
11314 }
11315 else
11316 {
11317 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11318 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11319 if (TREE_VALUE (t3))
11320 {
11321 bool allow_string
11322 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11323 && TREE_PURPOSE (t3) != score);
11324 tree v = TREE_VALUE (t3);
11325 if (TREE_CODE (v) == STRING_CST && allow_string)
11326 continue;
11327 v = tsubst_expr (v, args, complain, in_decl, true);
11328 v = fold_non_dependent_expr (v);
11329 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11330 || (TREE_PURPOSE (t3) == score
11331 ? TREE_CODE (v) != INTEGER_CST
11332 : !tree_fits_shwi_p (v)))
11333 {
11334 location_t loc
11335 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11336 match_loc);
11337 if (TREE_PURPOSE (t3) == score)
11338 error_at (loc, "score argument must be "
11339 "constant integer expression");
11340 else if (allow_string)
11341 error_at (loc, "property must be constant "
11342 "integer expression or string "
11343 "literal");
11344 else
11345 error_at (loc, "property must be constant "
11346 "integer expression");
11347 return NULL_TREE;
11348 }
11349 else if (TREE_PURPOSE (t3) == score
11350 && tree_int_cst_sgn (v) < 0)
11351 {
11352 location_t loc
11353 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11354 match_loc);
11355 error_at (loc, "score argument must be "
11356 "non-negative");
11357 return NULL_TREE;
11358 }
11359 TREE_VALUE (t3) = v;
11360 }
11361 }
11362 }
11363 }
11364 val = tree_cons (varid, ctx, chain);
11365 }
11366 /* If the first attribute argument is an identifier, don't
11367 pass it through tsubst. Attributes like mode, format,
11368 cleanup and several target specific attributes expect it
11369 unmodified. */
11370 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11371 {
11372 tree chain
11373 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11374 /*integral_constant_expression_p=*/false);
11375 if (chain != TREE_CHAIN (val))
11376 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11377 }
11378 else if (PACK_EXPANSION_P (val))
11379 {
11380 /* An attribute pack expansion. */
11381 tree purp = TREE_PURPOSE (t);
11382 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11383 if (pack == error_mark_node)
11384 return error_mark_node;
11385 int len = TREE_VEC_LENGTH (pack);
11386 tree list = NULL_TREE;
11387 tree *q = &list;
11388 for (int i = 0; i < len; ++i)
11389 {
11390 tree elt = TREE_VEC_ELT (pack, i);
11391 *q = build_tree_list (purp, elt);
11392 q = &TREE_CHAIN (*q);
11393 }
11394 return list;
11395 }
11396 else
11397 val = tsubst_expr (val, args, complain, in_decl,
11398 /*integral_constant_expression_p=*/false);
11399
11400 if (val != TREE_VALUE (t))
11401 return build_tree_list (TREE_PURPOSE (t), val);
11402 return t;
11403 }
11404
11405 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11406 unchanged or a new TREE_LIST chain. */
11407
11408 static tree
11409 tsubst_attributes (tree attributes, tree args,
11410 tsubst_flags_t complain, tree in_decl)
11411 {
11412 tree last_dep = NULL_TREE;
11413
11414 for (tree t = attributes; t; t = TREE_CHAIN (t))
11415 if (ATTR_IS_DEPENDENT (t))
11416 {
11417 last_dep = t;
11418 attributes = copy_list (attributes);
11419 break;
11420 }
11421
11422 if (last_dep)
11423 for (tree *p = &attributes; *p; )
11424 {
11425 tree t = *p;
11426 if (ATTR_IS_DEPENDENT (t))
11427 {
11428 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11429 if (subst != t)
11430 {
11431 *p = subst;
11432 while (*p)
11433 p = &TREE_CHAIN (*p);
11434 *p = TREE_CHAIN (t);
11435 continue;
11436 }
11437 }
11438 p = &TREE_CHAIN (*p);
11439 }
11440
11441 return attributes;
11442 }
11443
11444 /* Apply any attributes which had to be deferred until instantiation
11445 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11446 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11447
11448 static void
11449 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11450 tree args, tsubst_flags_t complain, tree in_decl)
11451 {
11452 tree last_dep = NULL_TREE;
11453 tree t;
11454 tree *p;
11455
11456 if (attributes == NULL_TREE)
11457 return;
11458
11459 if (DECL_P (*decl_p))
11460 {
11461 if (TREE_TYPE (*decl_p) == error_mark_node)
11462 return;
11463 p = &DECL_ATTRIBUTES (*decl_p);
11464 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11465 to our attributes parameter. */
11466 gcc_assert (*p == attributes);
11467 }
11468 else
11469 {
11470 p = &TYPE_ATTRIBUTES (*decl_p);
11471 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11472 lookup_template_class_1, and should be preserved. */
11473 gcc_assert (*p != attributes);
11474 while (*p)
11475 p = &TREE_CHAIN (*p);
11476 }
11477
11478 for (t = attributes; t; t = TREE_CHAIN (t))
11479 if (ATTR_IS_DEPENDENT (t))
11480 {
11481 last_dep = t;
11482 attributes = copy_list (attributes);
11483 break;
11484 }
11485
11486 *p = attributes;
11487 if (last_dep)
11488 {
11489 tree late_attrs = NULL_TREE;
11490 tree *q = &late_attrs;
11491
11492 for (; *p; )
11493 {
11494 t = *p;
11495 if (ATTR_IS_DEPENDENT (t))
11496 {
11497 *p = TREE_CHAIN (t);
11498 TREE_CHAIN (t) = NULL_TREE;
11499 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11500 while (*q)
11501 q = &TREE_CHAIN (*q);
11502 }
11503 else
11504 p = &TREE_CHAIN (t);
11505 }
11506
11507 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11508 }
11509 }
11510
11511 /* Perform (or defer) access check for typedefs that were referenced
11512 from within the template TMPL code.
11513 This is a subroutine of instantiate_decl and instantiate_class_template.
11514 TMPL is the template to consider and TARGS is the list of arguments of
11515 that template. */
11516
11517 static void
11518 perform_typedefs_access_check (tree tmpl, tree targs)
11519 {
11520 unsigned i;
11521 qualified_typedef_usage_t *iter;
11522
11523 if (!tmpl
11524 || (!CLASS_TYPE_P (tmpl)
11525 && TREE_CODE (tmpl) != FUNCTION_DECL))
11526 return;
11527
11528 if (vec<qualified_typedef_usage_t, va_gc> *tdefs
11529 = get_types_needing_access_check (tmpl))
11530 FOR_EACH_VEC_ELT (*tdefs, i, iter)
11531 {
11532 tree type_decl = iter->typedef_decl;
11533 tree type_scope = iter->context;
11534
11535 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
11536 continue;
11537
11538 if (uses_template_parms (type_decl))
11539 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
11540 if (uses_template_parms (type_scope))
11541 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11542
11543 /* Make access check error messages point to the location
11544 of the use of the typedef. */
11545 iloc_sentinel ils (iter->locus);
11546 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11547 type_decl, type_decl,
11548 tf_warning_or_error);
11549 }
11550 }
11551
11552 static tree
11553 instantiate_class_template_1 (tree type)
11554 {
11555 tree templ, args, pattern, t, member;
11556 tree typedecl;
11557 tree pbinfo;
11558 tree base_list;
11559 unsigned int saved_maximum_field_alignment;
11560 tree fn_context;
11561
11562 if (type == error_mark_node)
11563 return error_mark_node;
11564
11565 if (COMPLETE_OR_OPEN_TYPE_P (type)
11566 || uses_template_parms (type))
11567 return type;
11568
11569 /* Figure out which template is being instantiated. */
11570 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11571 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11572
11573 /* Mark the type as in the process of being defined. */
11574 TYPE_BEING_DEFINED (type) = 1;
11575
11576 /* We may be in the middle of deferred access check. Disable
11577 it now. */
11578 deferring_access_check_sentinel acs (dk_no_deferred);
11579
11580 /* Determine what specialization of the original template to
11581 instantiate. */
11582 t = most_specialized_partial_spec (type, tf_warning_or_error);
11583 if (t == error_mark_node)
11584 return error_mark_node;
11585 else if (t)
11586 {
11587 /* This TYPE is actually an instantiation of a partial
11588 specialization. We replace the innermost set of ARGS with
11589 the arguments appropriate for substitution. For example,
11590 given:
11591
11592 template <class T> struct S {};
11593 template <class T> struct S<T*> {};
11594
11595 and supposing that we are instantiating S<int*>, ARGS will
11596 presently be {int*} -- but we need {int}. */
11597 pattern = TREE_TYPE (t);
11598 args = TREE_PURPOSE (t);
11599 }
11600 else
11601 {
11602 pattern = TREE_TYPE (templ);
11603 args = CLASSTYPE_TI_ARGS (type);
11604 }
11605
11606 /* If the template we're instantiating is incomplete, then clearly
11607 there's nothing we can do. */
11608 if (!COMPLETE_TYPE_P (pattern))
11609 {
11610 /* We can try again later. */
11611 TYPE_BEING_DEFINED (type) = 0;
11612 return type;
11613 }
11614
11615 /* If we've recursively instantiated too many templates, stop. */
11616 if (! push_tinst_level (type))
11617 return type;
11618
11619 int saved_unevaluated_operand = cp_unevaluated_operand;
11620 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11621
11622 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11623 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11624 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11625 fn_context = error_mark_node;
11626 if (!fn_context)
11627 push_to_top_level ();
11628 else
11629 {
11630 cp_unevaluated_operand = 0;
11631 c_inhibit_evaluation_warnings = 0;
11632 }
11633 /* Use #pragma pack from the template context. */
11634 saved_maximum_field_alignment = maximum_field_alignment;
11635 maximum_field_alignment = TYPE_PRECISION (pattern);
11636
11637 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11638
11639 /* Set the input location to the most specialized template definition.
11640 This is needed if tsubsting causes an error. */
11641 typedecl = TYPE_MAIN_DECL (pattern);
11642 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11643 DECL_SOURCE_LOCATION (typedecl);
11644
11645 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11646 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11647 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11648 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11649 if (ANON_AGGR_TYPE_P (pattern))
11650 SET_ANON_AGGR_TYPE_P (type);
11651 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11652 {
11653 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11654 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11655 /* Adjust visibility for template arguments. */
11656 determine_visibility (TYPE_MAIN_DECL (type));
11657 }
11658 if (CLASS_TYPE_P (type))
11659 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11660
11661 pbinfo = TYPE_BINFO (pattern);
11662
11663 /* We should never instantiate a nested class before its enclosing
11664 class; we need to look up the nested class by name before we can
11665 instantiate it, and that lookup should instantiate the enclosing
11666 class. */
11667 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11668 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11669
11670 base_list = NULL_TREE;
11671 if (BINFO_N_BASE_BINFOS (pbinfo))
11672 {
11673 tree pbase_binfo;
11674 tree pushed_scope;
11675 int i;
11676
11677 /* We must enter the scope containing the type, as that is where
11678 the accessibility of types named in dependent bases are
11679 looked up from. */
11680 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11681
11682 /* Substitute into each of the bases to determine the actual
11683 basetypes. */
11684 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11685 {
11686 tree base;
11687 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11688 tree expanded_bases = NULL_TREE;
11689 int idx, len = 1;
11690
11691 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11692 {
11693 expanded_bases =
11694 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11695 args, tf_error, NULL_TREE);
11696 if (expanded_bases == error_mark_node)
11697 continue;
11698
11699 len = TREE_VEC_LENGTH (expanded_bases);
11700 }
11701
11702 for (idx = 0; idx < len; idx++)
11703 {
11704 if (expanded_bases)
11705 /* Extract the already-expanded base class. */
11706 base = TREE_VEC_ELT (expanded_bases, idx);
11707 else
11708 /* Substitute to figure out the base class. */
11709 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11710 NULL_TREE);
11711
11712 if (base == error_mark_node)
11713 continue;
11714
11715 base_list = tree_cons (access, base, base_list);
11716 if (BINFO_VIRTUAL_P (pbase_binfo))
11717 TREE_TYPE (base_list) = integer_type_node;
11718 }
11719 }
11720
11721 /* The list is now in reverse order; correct that. */
11722 base_list = nreverse (base_list);
11723
11724 if (pushed_scope)
11725 pop_scope (pushed_scope);
11726 }
11727 /* Now call xref_basetypes to set up all the base-class
11728 information. */
11729 xref_basetypes (type, base_list);
11730
11731 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11732 (int) ATTR_FLAG_TYPE_IN_PLACE,
11733 args, tf_error, NULL_TREE);
11734 fixup_attribute_variants (type);
11735
11736 /* Now that our base classes are set up, enter the scope of the
11737 class, so that name lookups into base classes, etc. will work
11738 correctly. This is precisely analogous to what we do in
11739 begin_class_definition when defining an ordinary non-template
11740 class, except we also need to push the enclosing classes. */
11741 push_nested_class (type);
11742
11743 /* Now members are processed in the order of declaration. */
11744 for (member = CLASSTYPE_DECL_LIST (pattern);
11745 member; member = TREE_CHAIN (member))
11746 {
11747 tree t = TREE_VALUE (member);
11748
11749 if (TREE_PURPOSE (member))
11750 {
11751 if (TYPE_P (t))
11752 {
11753 if (LAMBDA_TYPE_P (t))
11754 /* A closure type for a lambda in an NSDMI or default argument.
11755 Ignore it; it will be regenerated when needed. */
11756 continue;
11757
11758 /* Build new CLASSTYPE_NESTED_UTDS. */
11759 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11760 && TYPE_LANG_SPECIFIC (t)
11761 && CLASSTYPE_IS_TEMPLATE (t));
11762
11763 /* If the member is a class template, then -- even after
11764 substitution -- there may be dependent types in the
11765 template argument list for the class. We increment
11766 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11767 that function will assume that no types are dependent
11768 when outside of a template. */
11769 if (class_template_p)
11770 ++processing_template_decl;
11771 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11772 if (class_template_p)
11773 --processing_template_decl;
11774 if (newtag == error_mark_node)
11775 continue;
11776
11777 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11778 {
11779 tree name = TYPE_IDENTIFIER (t);
11780
11781 if (class_template_p)
11782 /* Unfortunately, lookup_template_class sets
11783 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11784 instantiation (i.e., for the type of a member
11785 template class nested within a template class.)
11786 This behavior is required for
11787 maybe_process_partial_specialization to work
11788 correctly, but is not accurate in this case;
11789 the TAG is not an instantiation of anything.
11790 (The corresponding TEMPLATE_DECL is an
11791 instantiation, but the TYPE is not.) */
11792 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11793
11794 /* Now, we call pushtag to put this NEWTAG into the scope of
11795 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11796 pushtag calling push_template_decl. We don't have to do
11797 this for enums because it will already have been done in
11798 tsubst_enum. */
11799 if (name)
11800 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11801 pushtag (name, newtag, /*tag_scope=*/ts_current);
11802 }
11803 }
11804 else if (DECL_DECLARES_FUNCTION_P (t))
11805 {
11806 tree r;
11807
11808 if (TREE_CODE (t) == TEMPLATE_DECL)
11809 ++processing_template_decl;
11810 r = tsubst (t, args, tf_error, NULL_TREE);
11811 if (TREE_CODE (t) == TEMPLATE_DECL)
11812 --processing_template_decl;
11813 set_current_access_from_decl (r);
11814 finish_member_declaration (r);
11815 /* Instantiate members marked with attribute used. */
11816 if (r != error_mark_node && DECL_PRESERVE_P (r))
11817 mark_used (r);
11818 if (TREE_CODE (r) == FUNCTION_DECL
11819 && DECL_OMP_DECLARE_REDUCTION_P (r))
11820 cp_check_omp_declare_reduction (r);
11821 }
11822 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11823 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11824 /* A closure type for a lambda in an NSDMI or default argument.
11825 Ignore it; it will be regenerated when needed. */;
11826 else
11827 {
11828 /* Build new TYPE_FIELDS. */
11829 if (TREE_CODE (t) == STATIC_ASSERT)
11830 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11831 /*integral_constant_expression_p=*/true);
11832 else if (TREE_CODE (t) != CONST_DECL)
11833 {
11834 tree r;
11835 tree vec = NULL_TREE;
11836 int len = 1;
11837
11838 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11839 /* The file and line for this declaration, to
11840 assist in error message reporting. Since we
11841 called push_tinst_level above, we don't need to
11842 restore these. */
11843 input_location = DECL_SOURCE_LOCATION (t);
11844
11845 if (TREE_CODE (t) == TEMPLATE_DECL)
11846 ++processing_template_decl;
11847 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11848 if (TREE_CODE (t) == TEMPLATE_DECL)
11849 --processing_template_decl;
11850
11851 if (TREE_CODE (r) == TREE_VEC)
11852 {
11853 /* A capture pack became multiple fields. */
11854 vec = r;
11855 len = TREE_VEC_LENGTH (vec);
11856 }
11857
11858 for (int i = 0; i < len; ++i)
11859 {
11860 if (vec)
11861 r = TREE_VEC_ELT (vec, i);
11862 if (VAR_P (r))
11863 {
11864 /* In [temp.inst]:
11865
11866 [t]he initialization (and any associated
11867 side-effects) of a static data member does
11868 not occur unless the static data member is
11869 itself used in a way that requires the
11870 definition of the static data member to
11871 exist.
11872
11873 Therefore, we do not substitute into the
11874 initialized for the static data member here. */
11875 finish_static_data_member_decl
11876 (r,
11877 /*init=*/NULL_TREE,
11878 /*init_const_expr_p=*/false,
11879 /*asmspec_tree=*/NULL_TREE,
11880 /*flags=*/0);
11881 /* Instantiate members marked with attribute used. */
11882 if (r != error_mark_node && DECL_PRESERVE_P (r))
11883 mark_used (r);
11884 }
11885 else if (TREE_CODE (r) == FIELD_DECL)
11886 {
11887 /* Determine whether R has a valid type and can be
11888 completed later. If R is invalid, then its type
11889 is replaced by error_mark_node. */
11890 tree rtype = TREE_TYPE (r);
11891 if (can_complete_type_without_circularity (rtype))
11892 complete_type (rtype);
11893
11894 if (!complete_or_array_type_p (rtype))
11895 {
11896 /* If R's type couldn't be completed and
11897 it isn't a flexible array member (whose
11898 type is incomplete by definition) give
11899 an error. */
11900 cxx_incomplete_type_error (r, rtype);
11901 TREE_TYPE (r) = error_mark_node;
11902 }
11903 else if (TREE_CODE (rtype) == ARRAY_TYPE
11904 && TYPE_DOMAIN (rtype) == NULL_TREE
11905 && (TREE_CODE (type) == UNION_TYPE
11906 || TREE_CODE (type) == QUAL_UNION_TYPE))
11907 {
11908 error ("flexible array member %qD in union", r);
11909 TREE_TYPE (r) = error_mark_node;
11910 }
11911 else if (!verify_type_context (input_location,
11912 TCTX_FIELD, rtype))
11913 TREE_TYPE (r) = error_mark_node;
11914 }
11915
11916 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11917 such a thing will already have been added to the field
11918 list by tsubst_enum in finish_member_declaration in the
11919 CLASSTYPE_NESTED_UTDS case above. */
11920 if (!(TREE_CODE (r) == TYPE_DECL
11921 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11922 && DECL_ARTIFICIAL (r)))
11923 {
11924 set_current_access_from_decl (r);
11925 finish_member_declaration (r);
11926 }
11927 }
11928 }
11929 }
11930 }
11931 else
11932 {
11933 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11934 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11935 {
11936 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11937
11938 tree friend_type = t;
11939 bool adjust_processing_template_decl = false;
11940
11941 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11942 {
11943 /* template <class T> friend class C; */
11944 friend_type = tsubst_friend_class (friend_type, args);
11945 adjust_processing_template_decl = true;
11946 }
11947 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11948 {
11949 /* template <class T> friend class C::D; */
11950 friend_type = tsubst (friend_type, args,
11951 tf_warning_or_error, NULL_TREE);
11952 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11953 friend_type = TREE_TYPE (friend_type);
11954 adjust_processing_template_decl = true;
11955 }
11956 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11957 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11958 {
11959 /* This could be either
11960
11961 friend class T::C;
11962
11963 when dependent_type_p is false or
11964
11965 template <class U> friend class T::C;
11966
11967 otherwise. */
11968 /* Bump processing_template_decl in case this is something like
11969 template <class T> friend struct A<T>::B. */
11970 ++processing_template_decl;
11971 friend_type = tsubst (friend_type, args,
11972 tf_warning_or_error, NULL_TREE);
11973 if (dependent_type_p (friend_type))
11974 adjust_processing_template_decl = true;
11975 --processing_template_decl;
11976 }
11977 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11978 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11979 && TYPE_HIDDEN_P (friend_type))
11980 {
11981 /* friend class C;
11982
11983 where C hasn't been declared yet. Let's lookup name
11984 from namespace scope directly, bypassing any name that
11985 come from dependent base class. */
11986 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11987
11988 /* The call to xref_tag_from_type does injection for friend
11989 classes. */
11990 push_nested_namespace (ns);
11991 friend_type =
11992 xref_tag_from_type (friend_type, NULL_TREE,
11993 /*tag_scope=*/ts_current);
11994 pop_nested_namespace (ns);
11995 }
11996 else if (uses_template_parms (friend_type))
11997 /* friend class C<T>; */
11998 friend_type = tsubst (friend_type, args,
11999 tf_warning_or_error, NULL_TREE);
12000 /* Otherwise it's
12001
12002 friend class C;
12003
12004 where C is already declared or
12005
12006 friend class C<int>;
12007
12008 We don't have to do anything in these cases. */
12009
12010 if (adjust_processing_template_decl)
12011 /* Trick make_friend_class into realizing that the friend
12012 we're adding is a template, not an ordinary class. It's
12013 important that we use make_friend_class since it will
12014 perform some error-checking and output cross-reference
12015 information. */
12016 ++processing_template_decl;
12017
12018 if (friend_type != error_mark_node)
12019 make_friend_class (type, friend_type, /*complain=*/false);
12020
12021 if (adjust_processing_template_decl)
12022 --processing_template_decl;
12023 }
12024 else
12025 {
12026 /* Build new DECL_FRIENDLIST. */
12027 tree r;
12028
12029 /* The file and line for this declaration, to
12030 assist in error message reporting. Since we
12031 called push_tinst_level above, we don't need to
12032 restore these. */
12033 input_location = DECL_SOURCE_LOCATION (t);
12034
12035 if (TREE_CODE (t) == TEMPLATE_DECL)
12036 {
12037 ++processing_template_decl;
12038 push_deferring_access_checks (dk_no_check);
12039 }
12040
12041 r = tsubst_friend_function (t, args);
12042 add_friend (type, r, /*complain=*/false);
12043 if (TREE_CODE (t) == TEMPLATE_DECL)
12044 {
12045 pop_deferring_access_checks ();
12046 --processing_template_decl;
12047 }
12048 }
12049 }
12050 }
12051
12052 if (fn_context)
12053 {
12054 /* Restore these before substituting into the lambda capture
12055 initializers. */
12056 cp_unevaluated_operand = saved_unevaluated_operand;
12057 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12058 }
12059
12060 /* Set the file and line number information to whatever is given for
12061 the class itself. This puts error messages involving generated
12062 implicit functions at a predictable point, and the same point
12063 that would be used for non-template classes. */
12064 input_location = DECL_SOURCE_LOCATION (typedecl);
12065
12066 unreverse_member_declarations (type);
12067 finish_struct_1 (type);
12068 TYPE_BEING_DEFINED (type) = 0;
12069
12070 /* We don't instantiate default arguments for member functions. 14.7.1:
12071
12072 The implicit instantiation of a class template specialization causes
12073 the implicit instantiation of the declarations, but not of the
12074 definitions or default arguments, of the class member functions,
12075 member classes, static data members and member templates.... */
12076
12077 /* Some typedefs referenced from within the template code need to be access
12078 checked at template instantiation time, i.e now. These types were
12079 added to the template at parsing time. Let's get those and perform
12080 the access checks then. */
12081 perform_typedefs_access_check (pattern, args);
12082 perform_deferred_access_checks (tf_warning_or_error);
12083 pop_nested_class ();
12084 maximum_field_alignment = saved_maximum_field_alignment;
12085 if (!fn_context)
12086 pop_from_top_level ();
12087 pop_tinst_level ();
12088
12089 /* The vtable for a template class can be emitted in any translation
12090 unit in which the class is instantiated. When there is no key
12091 method, however, finish_struct_1 will already have added TYPE to
12092 the keyed_classes. */
12093 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12094 vec_safe_push (keyed_classes, type);
12095
12096 return type;
12097 }
12098
12099 /* Wrapper for instantiate_class_template_1. */
12100
12101 tree
12102 instantiate_class_template (tree type)
12103 {
12104 tree ret;
12105 timevar_push (TV_TEMPLATE_INST);
12106 ret = instantiate_class_template_1 (type);
12107 timevar_pop (TV_TEMPLATE_INST);
12108 return ret;
12109 }
12110
12111 tree
12112 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12113 {
12114 tree r;
12115
12116 if (!t)
12117 r = t;
12118 else if (TYPE_P (t))
12119 r = tsubst (t, args, complain, in_decl);
12120 else
12121 {
12122 if (!(complain & tf_warning))
12123 ++c_inhibit_evaluation_warnings;
12124 r = tsubst_expr (t, args, complain, in_decl,
12125 /*integral_constant_expression_p=*/true);
12126 if (!(complain & tf_warning))
12127 --c_inhibit_evaluation_warnings;
12128 }
12129
12130 return r;
12131 }
12132
12133 /* Given a function parameter pack TMPL_PARM and some function parameters
12134 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12135 and set *SPEC_P to point at the next point in the list. */
12136
12137 tree
12138 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12139 {
12140 /* Collect all of the extra "packed" parameters into an
12141 argument pack. */
12142 tree parmvec;
12143 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12144 tree spec_parm = *spec_p;
12145 int i, len;
12146
12147 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12148 if (tmpl_parm
12149 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12150 break;
12151
12152 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12153 parmvec = make_tree_vec (len);
12154 spec_parm = *spec_p;
12155 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12156 {
12157 tree elt = spec_parm;
12158 if (DECL_PACK_P (elt))
12159 elt = make_pack_expansion (elt);
12160 TREE_VEC_ELT (parmvec, i) = elt;
12161 }
12162
12163 /* Build the argument packs. */
12164 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12165 *spec_p = spec_parm;
12166
12167 return argpack;
12168 }
12169
12170 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12171 NONTYPE_ARGUMENT_PACK. */
12172
12173 static tree
12174 make_fnparm_pack (tree spec_parm)
12175 {
12176 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12177 }
12178
12179 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12180 pack expansion with no extra args, 2 if it has extra args, or 0
12181 if it is not a pack expansion. */
12182
12183 static int
12184 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12185 {
12186 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12187 /* We're being called before this happens in tsubst_pack_expansion. */
12188 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12189 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12190 if (i >= TREE_VEC_LENGTH (vec))
12191 return 0;
12192 tree elt = TREE_VEC_ELT (vec, i);
12193 if (DECL_P (elt))
12194 /* A decl pack is itself an expansion. */
12195 elt = TREE_TYPE (elt);
12196 if (!PACK_EXPANSION_P (elt))
12197 return 0;
12198 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12199 return 2;
12200 return 1;
12201 }
12202
12203
12204 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12205
12206 static tree
12207 make_argument_pack_select (tree arg_pack, unsigned index)
12208 {
12209 tree aps = make_node (ARGUMENT_PACK_SELECT);
12210
12211 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12212 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12213
12214 return aps;
12215 }
12216
12217 /* This is a subroutine of tsubst_pack_expansion.
12218
12219 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12220 mechanism to store the (non complete list of) arguments of the
12221 substitution and return a non substituted pack expansion, in order
12222 to wait for when we have enough arguments to really perform the
12223 substitution. */
12224
12225 static bool
12226 use_pack_expansion_extra_args_p (tree parm_packs,
12227 int arg_pack_len,
12228 bool has_empty_arg)
12229 {
12230 /* If one pack has an expansion and another pack has a normal
12231 argument or if one pack has an empty argument and an another
12232 one hasn't then tsubst_pack_expansion cannot perform the
12233 substitution and need to fall back on the
12234 PACK_EXPANSION_EXTRA mechanism. */
12235 if (parm_packs == NULL_TREE)
12236 return false;
12237 else if (has_empty_arg)
12238 {
12239 /* If all the actual packs are pack expansions, we can still
12240 subsitute directly. */
12241 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12242 {
12243 tree a = TREE_VALUE (p);
12244 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12245 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12246 a = ARGUMENT_PACK_ARGS (a);
12247 if (TREE_VEC_LENGTH (a) == 1)
12248 a = TREE_VEC_ELT (a, 0);
12249 if (PACK_EXPANSION_P (a))
12250 continue;
12251 return true;
12252 }
12253 return false;
12254 }
12255
12256 bool has_expansion_arg = false;
12257 for (int i = 0 ; i < arg_pack_len; ++i)
12258 {
12259 bool has_non_expansion_arg = false;
12260 for (tree parm_pack = parm_packs;
12261 parm_pack;
12262 parm_pack = TREE_CHAIN (parm_pack))
12263 {
12264 tree arg = TREE_VALUE (parm_pack);
12265
12266 int exp = argument_pack_element_is_expansion_p (arg, i);
12267 if (exp == 2)
12268 /* We can't substitute a pack expansion with extra args into
12269 our pattern. */
12270 return true;
12271 else if (exp)
12272 has_expansion_arg = true;
12273 else
12274 has_non_expansion_arg = true;
12275 }
12276
12277 if (has_expansion_arg && has_non_expansion_arg)
12278 return true;
12279 }
12280 return false;
12281 }
12282
12283 /* [temp.variadic]/6 says that:
12284
12285 The instantiation of a pack expansion [...]
12286 produces a list E1,E2, ..., En, where N is the number of elements
12287 in the pack expansion parameters.
12288
12289 This subroutine of tsubst_pack_expansion produces one of these Ei.
12290
12291 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12292 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12293 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12294 INDEX is the index 'i' of the element Ei to produce. ARGS,
12295 COMPLAIN, and IN_DECL are the same parameters as for the
12296 tsubst_pack_expansion function.
12297
12298 The function returns the resulting Ei upon successful completion,
12299 or error_mark_node.
12300
12301 Note that this function possibly modifies the ARGS parameter, so
12302 it's the responsibility of the caller to restore it. */
12303
12304 static tree
12305 gen_elem_of_pack_expansion_instantiation (tree pattern,
12306 tree parm_packs,
12307 unsigned index,
12308 tree args /* This parm gets
12309 modified. */,
12310 tsubst_flags_t complain,
12311 tree in_decl)
12312 {
12313 tree t;
12314 bool ith_elem_is_expansion = false;
12315
12316 /* For each parameter pack, change the substitution of the parameter
12317 pack to the ith argument in its argument pack, then expand the
12318 pattern. */
12319 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12320 {
12321 tree parm = TREE_PURPOSE (pack);
12322 tree arg_pack = TREE_VALUE (pack);
12323 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12324
12325 ith_elem_is_expansion |=
12326 argument_pack_element_is_expansion_p (arg_pack, index);
12327
12328 /* Select the Ith argument from the pack. */
12329 if (TREE_CODE (parm) == PARM_DECL
12330 || VAR_P (parm)
12331 || TREE_CODE (parm) == FIELD_DECL)
12332 {
12333 if (index == 0)
12334 {
12335 aps = make_argument_pack_select (arg_pack, index);
12336 if (!mark_used (parm, complain) && !(complain & tf_error))
12337 return error_mark_node;
12338 register_local_specialization (aps, parm);
12339 }
12340 else
12341 aps = retrieve_local_specialization (parm);
12342 }
12343 else
12344 {
12345 int idx, level;
12346 template_parm_level_and_index (parm, &level, &idx);
12347
12348 if (index == 0)
12349 {
12350 aps = make_argument_pack_select (arg_pack, index);
12351 /* Update the corresponding argument. */
12352 TMPL_ARG (args, level, idx) = aps;
12353 }
12354 else
12355 /* Re-use the ARGUMENT_PACK_SELECT. */
12356 aps = TMPL_ARG (args, level, idx);
12357 }
12358 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12359 }
12360
12361 /* Substitute into the PATTERN with the (possibly altered)
12362 arguments. */
12363 if (pattern == in_decl)
12364 /* Expanding a fixed parameter pack from
12365 coerce_template_parameter_pack. */
12366 t = tsubst_decl (pattern, args, complain);
12367 else if (pattern == error_mark_node)
12368 t = error_mark_node;
12369 else if (!TYPE_P (pattern))
12370 t = tsubst_expr (pattern, args, complain, in_decl,
12371 /*integral_constant_expression_p=*/false);
12372 else
12373 t = tsubst (pattern, args, complain, in_decl);
12374
12375 /* If the Ith argument pack element is a pack expansion, then
12376 the Ith element resulting from the substituting is going to
12377 be a pack expansion as well. */
12378 if (ith_elem_is_expansion)
12379 t = make_pack_expansion (t, complain);
12380
12381 return t;
12382 }
12383
12384 /* When the unexpanded parameter pack in a fold expression expands to an empty
12385 sequence, the value of the expression is as follows; the program is
12386 ill-formed if the operator is not listed in this table.
12387
12388 && true
12389 || false
12390 , void() */
12391
12392 tree
12393 expand_empty_fold (tree t, tsubst_flags_t complain)
12394 {
12395 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12396 if (!FOLD_EXPR_MODIFY_P (t))
12397 switch (code)
12398 {
12399 case TRUTH_ANDIF_EXPR:
12400 return boolean_true_node;
12401 case TRUTH_ORIF_EXPR:
12402 return boolean_false_node;
12403 case COMPOUND_EXPR:
12404 return void_node;
12405 default:
12406 break;
12407 }
12408
12409 if (complain & tf_error)
12410 error_at (location_of (t),
12411 "fold of empty expansion over %O", code);
12412 return error_mark_node;
12413 }
12414
12415 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12416 form an expression that combines the two terms using the
12417 operator of T. */
12418
12419 static tree
12420 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12421 {
12422 tree op = FOLD_EXPR_OP (t);
12423 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12424
12425 // Handle compound assignment operators.
12426 if (FOLD_EXPR_MODIFY_P (t))
12427 return build_x_modify_expr (input_location, left, code, right, complain);
12428
12429 warning_sentinel s(warn_parentheses);
12430 switch (code)
12431 {
12432 case COMPOUND_EXPR:
12433 return build_x_compound_expr (input_location, left, right, complain);
12434 default:
12435 return build_x_binary_op (input_location, code,
12436 left, TREE_CODE (left),
12437 right, TREE_CODE (right),
12438 /*overload=*/NULL,
12439 complain);
12440 }
12441 }
12442
12443 /* Substitute ARGS into the pack of a fold expression T. */
12444
12445 static inline tree
12446 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12447 {
12448 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12449 }
12450
12451 /* Substitute ARGS into the pack of a fold expression T. */
12452
12453 static inline tree
12454 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12455 {
12456 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12457 }
12458
12459 /* Expand a PACK of arguments into a grouped as left fold.
12460 Given a pack containing elements A0, A1, ..., An and an
12461 operator @, this builds the expression:
12462
12463 ((A0 @ A1) @ A2) ... @ An
12464
12465 Note that PACK must not be empty.
12466
12467 The operator is defined by the original fold expression T. */
12468
12469 static tree
12470 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12471 {
12472 tree left = TREE_VEC_ELT (pack, 0);
12473 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12474 {
12475 tree right = TREE_VEC_ELT (pack, i);
12476 left = fold_expression (t, left, right, complain);
12477 }
12478 return left;
12479 }
12480
12481 /* Substitute into a unary left fold expression. */
12482
12483 static tree
12484 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12485 tree in_decl)
12486 {
12487 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12488 if (pack == error_mark_node)
12489 return error_mark_node;
12490 if (PACK_EXPANSION_P (pack))
12491 {
12492 tree r = copy_node (t);
12493 FOLD_EXPR_PACK (r) = pack;
12494 return r;
12495 }
12496 if (TREE_VEC_LENGTH (pack) == 0)
12497 return expand_empty_fold (t, complain);
12498 else
12499 return expand_left_fold (t, pack, complain);
12500 }
12501
12502 /* Substitute into a binary left fold expression.
12503
12504 Do ths by building a single (non-empty) vector of argumnts and
12505 building the expression from those elements. */
12506
12507 static tree
12508 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12509 tree in_decl)
12510 {
12511 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12512 if (pack == error_mark_node)
12513 return error_mark_node;
12514 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12515 if (init == error_mark_node)
12516 return error_mark_node;
12517
12518 if (PACK_EXPANSION_P (pack))
12519 {
12520 tree r = copy_node (t);
12521 FOLD_EXPR_PACK (r) = pack;
12522 FOLD_EXPR_INIT (r) = init;
12523 return r;
12524 }
12525
12526 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12527 TREE_VEC_ELT (vec, 0) = init;
12528 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12529 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12530
12531 return expand_left_fold (t, vec, complain);
12532 }
12533
12534 /* Expand a PACK of arguments into a grouped as right fold.
12535 Given a pack containing elementns A0, A1, ..., and an
12536 operator @, this builds the expression:
12537
12538 A0@ ... (An-2 @ (An-1 @ An))
12539
12540 Note that PACK must not be empty.
12541
12542 The operator is defined by the original fold expression T. */
12543
12544 tree
12545 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12546 {
12547 // Build the expression.
12548 int n = TREE_VEC_LENGTH (pack);
12549 tree right = TREE_VEC_ELT (pack, n - 1);
12550 for (--n; n != 0; --n)
12551 {
12552 tree left = TREE_VEC_ELT (pack, n - 1);
12553 right = fold_expression (t, left, right, complain);
12554 }
12555 return right;
12556 }
12557
12558 /* Substitute into a unary right fold expression. */
12559
12560 static tree
12561 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12562 tree in_decl)
12563 {
12564 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12565 if (pack == error_mark_node)
12566 return error_mark_node;
12567 if (PACK_EXPANSION_P (pack))
12568 {
12569 tree r = copy_node (t);
12570 FOLD_EXPR_PACK (r) = pack;
12571 return r;
12572 }
12573 if (TREE_VEC_LENGTH (pack) == 0)
12574 return expand_empty_fold (t, complain);
12575 else
12576 return expand_right_fold (t, pack, complain);
12577 }
12578
12579 /* Substitute into a binary right fold expression.
12580
12581 Do ths by building a single (non-empty) vector of arguments and
12582 building the expression from those elements. */
12583
12584 static tree
12585 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12586 tree in_decl)
12587 {
12588 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12589 if (pack == error_mark_node)
12590 return error_mark_node;
12591 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12592 if (init == error_mark_node)
12593 return error_mark_node;
12594
12595 if (PACK_EXPANSION_P (pack))
12596 {
12597 tree r = copy_node (t);
12598 FOLD_EXPR_PACK (r) = pack;
12599 FOLD_EXPR_INIT (r) = init;
12600 return r;
12601 }
12602
12603 int n = TREE_VEC_LENGTH (pack);
12604 tree vec = make_tree_vec (n + 1);
12605 for (int i = 0; i < n; ++i)
12606 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12607 TREE_VEC_ELT (vec, n) = init;
12608
12609 return expand_right_fold (t, vec, complain);
12610 }
12611
12612 /* Walk through the pattern of a pack expansion, adding everything in
12613 local_specializations to a list. */
12614
12615 class el_data
12616 {
12617 public:
12618 hash_set<tree> internal;
12619 tree extra;
12620 tsubst_flags_t complain;
12621
12622 el_data (tsubst_flags_t c)
12623 : extra (NULL_TREE), complain (c) {}
12624 };
12625 static tree
12626 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12627 {
12628 el_data &data = *reinterpret_cast<el_data*>(data_);
12629 tree *extra = &data.extra;
12630 tsubst_flags_t complain = data.complain;
12631
12632 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12633 /* Remember local typedefs (85214). */
12634 tp = &TYPE_NAME (*tp);
12635
12636 if (TREE_CODE (*tp) == DECL_EXPR)
12637 data.internal.add (DECL_EXPR_DECL (*tp));
12638 else if (tree spec = retrieve_local_specialization (*tp))
12639 {
12640 if (data.internal.contains (*tp))
12641 /* Don't mess with variables declared within the pattern. */
12642 return NULL_TREE;
12643 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12644 {
12645 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12646 tree args = ARGUMENT_PACK_ARGS (spec);
12647 if (TREE_VEC_LENGTH (args) == 1)
12648 {
12649 tree elt = TREE_VEC_ELT (args, 0);
12650 if (PACK_EXPANSION_P (elt))
12651 elt = PACK_EXPANSION_PATTERN (elt);
12652 if (DECL_PACK_P (elt))
12653 spec = elt;
12654 }
12655 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12656 {
12657 /* Handle lambda capture here, since we aren't doing any
12658 substitution now, and so tsubst_copy won't call
12659 process_outer_var_ref. */
12660 tree args = ARGUMENT_PACK_ARGS (spec);
12661 int len = TREE_VEC_LENGTH (args);
12662 for (int i = 0; i < len; ++i)
12663 {
12664 tree arg = TREE_VEC_ELT (args, i);
12665 tree carg = arg;
12666 if (outer_automatic_var_p (arg))
12667 carg = process_outer_var_ref (arg, complain);
12668 if (carg != arg)
12669 {
12670 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12671 proxies. */
12672 if (i == 0)
12673 {
12674 spec = copy_node (spec);
12675 args = copy_node (args);
12676 SET_ARGUMENT_PACK_ARGS (spec, args);
12677 register_local_specialization (spec, *tp);
12678 }
12679 TREE_VEC_ELT (args, i) = carg;
12680 }
12681 }
12682 }
12683 }
12684 if (outer_automatic_var_p (spec))
12685 spec = process_outer_var_ref (spec, complain);
12686 *extra = tree_cons (*tp, spec, *extra);
12687 }
12688 return NULL_TREE;
12689 }
12690 static tree
12691 extract_local_specs (tree pattern, tsubst_flags_t complain)
12692 {
12693 el_data data (complain);
12694 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12695 return data.extra;
12696 }
12697
12698 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12699 for use in PACK_EXPANSION_EXTRA_ARGS. */
12700
12701 tree
12702 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12703 {
12704 tree extra = args;
12705 if (local_specializations)
12706 if (tree locals = extract_local_specs (pattern, complain))
12707 extra = tree_cons (NULL_TREE, extra, locals);
12708 return extra;
12709 }
12710
12711 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12712 normal template args to ARGS. */
12713
12714 tree
12715 add_extra_args (tree extra, tree args)
12716 {
12717 if (extra && TREE_CODE (extra) == TREE_LIST)
12718 {
12719 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12720 {
12721 /* The partial instantiation involved local declarations collected in
12722 extract_local_specs; map from the general template to our local
12723 context. */
12724 tree gen = TREE_PURPOSE (elt);
12725 tree inst = TREE_VALUE (elt);
12726 if (DECL_P (inst))
12727 if (tree local = retrieve_local_specialization (inst))
12728 inst = local;
12729 /* else inst is already a full instantiation of the pack. */
12730 register_local_specialization (inst, gen);
12731 }
12732 gcc_assert (!TREE_PURPOSE (extra));
12733 extra = TREE_VALUE (extra);
12734 }
12735 #if 1
12736 /* I think we should always be able to substitute dependent args into the
12737 pattern. If that turns out to be incorrect in some cases, enable the
12738 alternate code (and add complain/in_decl parms to this function). */
12739 gcc_checking_assert (!uses_template_parms (extra));
12740 #else
12741 if (!uses_template_parms (extra))
12742 {
12743 gcc_unreachable ();
12744 extra = tsubst_template_args (extra, args, complain, in_decl);
12745 args = add_outermost_template_args (args, extra);
12746 }
12747 else
12748 #endif
12749 args = add_to_template_args (extra, args);
12750 return args;
12751 }
12752
12753 /* Substitute ARGS into T, which is an pack expansion
12754 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12755 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12756 (if only a partial substitution could be performed) or
12757 ERROR_MARK_NODE if there was an error. */
12758 tree
12759 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12760 tree in_decl)
12761 {
12762 tree pattern;
12763 tree pack, packs = NULL_TREE;
12764 bool unsubstituted_packs = false;
12765 int i, len = -1;
12766 tree result;
12767 bool need_local_specializations = false;
12768 int levels;
12769
12770 gcc_assert (PACK_EXPANSION_P (t));
12771 pattern = PACK_EXPANSION_PATTERN (t);
12772
12773 /* Add in any args remembered from an earlier partial instantiation. */
12774 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12775
12776 levels = TMPL_ARGS_DEPTH (args);
12777
12778 /* Determine the argument packs that will instantiate the parameter
12779 packs used in the expansion expression. While we're at it,
12780 compute the number of arguments to be expanded and make sure it
12781 is consistent. */
12782 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12783 pack = TREE_CHAIN (pack))
12784 {
12785 tree parm_pack = TREE_VALUE (pack);
12786 tree arg_pack = NULL_TREE;
12787 tree orig_arg = NULL_TREE;
12788 int level = 0;
12789
12790 if (TREE_CODE (parm_pack) == BASES)
12791 {
12792 gcc_assert (parm_pack == pattern);
12793 if (BASES_DIRECT (parm_pack))
12794 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12795 args, complain,
12796 in_decl, false),
12797 complain);
12798 else
12799 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12800 args, complain, in_decl,
12801 false), complain);
12802 }
12803 else if (builtin_pack_call_p (parm_pack))
12804 {
12805 if (parm_pack != pattern)
12806 {
12807 if (complain & tf_error)
12808 sorry ("%qE is not the entire pattern of the pack expansion",
12809 parm_pack);
12810 return error_mark_node;
12811 }
12812 return expand_builtin_pack_call (parm_pack, args,
12813 complain, in_decl);
12814 }
12815 else if (TREE_CODE (parm_pack) == PARM_DECL)
12816 {
12817 /* We know we have correct local_specializations if this
12818 expansion is at function scope, or if we're dealing with a
12819 local parameter in a requires expression; for the latter,
12820 tsubst_requires_expr set it up appropriately. */
12821 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12822 arg_pack = retrieve_local_specialization (parm_pack);
12823 else
12824 /* We can't rely on local_specializations for a parameter
12825 name used later in a function declaration (such as in a
12826 late-specified return type). Even if it exists, it might
12827 have the wrong value for a recursive call. */
12828 need_local_specializations = true;
12829
12830 if (!arg_pack)
12831 {
12832 /* This parameter pack was used in an unevaluated context. Just
12833 make a dummy decl, since it's only used for its type. */
12834 ++cp_unevaluated_operand;
12835 arg_pack = tsubst_decl (parm_pack, args, complain);
12836 --cp_unevaluated_operand;
12837 if (arg_pack && DECL_PACK_P (arg_pack))
12838 /* Partial instantiation of the parm_pack, we can't build
12839 up an argument pack yet. */
12840 arg_pack = NULL_TREE;
12841 else
12842 arg_pack = make_fnparm_pack (arg_pack);
12843 }
12844 else if (DECL_PACK_P (arg_pack))
12845 /* This argument pack isn't fully instantiated yet. */
12846 arg_pack = NULL_TREE;
12847 }
12848 else if (is_capture_proxy (parm_pack))
12849 {
12850 arg_pack = retrieve_local_specialization (parm_pack);
12851 if (DECL_PACK_P (arg_pack))
12852 arg_pack = NULL_TREE;
12853 }
12854 else
12855 {
12856 int idx;
12857 template_parm_level_and_index (parm_pack, &level, &idx);
12858 if (level <= levels)
12859 arg_pack = TMPL_ARG (args, level, idx);
12860 }
12861
12862 orig_arg = arg_pack;
12863 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12864 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12865
12866 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12867 /* This can only happen if we forget to expand an argument
12868 pack somewhere else. Just return an error, silently. */
12869 {
12870 result = make_tree_vec (1);
12871 TREE_VEC_ELT (result, 0) = error_mark_node;
12872 return result;
12873 }
12874
12875 if (arg_pack)
12876 {
12877 int my_len =
12878 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12879
12880 /* Don't bother trying to do a partial substitution with
12881 incomplete packs; we'll try again after deduction. */
12882 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12883 return t;
12884
12885 if (len < 0)
12886 len = my_len;
12887 else if (len != my_len)
12888 {
12889 if (!(complain & tf_error))
12890 /* Fail quietly. */;
12891 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12892 error ("mismatched argument pack lengths while expanding %qT",
12893 pattern);
12894 else
12895 error ("mismatched argument pack lengths while expanding %qE",
12896 pattern);
12897 return error_mark_node;
12898 }
12899
12900 /* Keep track of the parameter packs and their corresponding
12901 argument packs. */
12902 packs = tree_cons (parm_pack, arg_pack, packs);
12903 TREE_TYPE (packs) = orig_arg;
12904 }
12905 else
12906 {
12907 /* We can't substitute for this parameter pack. We use a flag as
12908 well as the missing_level counter because function parameter
12909 packs don't have a level. */
12910 gcc_assert (processing_template_decl || is_auto (parm_pack));
12911 unsubstituted_packs = true;
12912 }
12913 }
12914
12915 /* If the expansion is just T..., return the matching argument pack, unless
12916 we need to call convert_from_reference on all the elements. This is an
12917 important optimization; see c++/68422. */
12918 if (!unsubstituted_packs
12919 && TREE_PURPOSE (packs) == pattern)
12920 {
12921 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12922
12923 /* If the argument pack is a single pack expansion, pull it out. */
12924 if (TREE_VEC_LENGTH (args) == 1
12925 && pack_expansion_args_count (args))
12926 return TREE_VEC_ELT (args, 0);
12927
12928 /* Types need no adjustment, nor does sizeof..., and if we still have
12929 some pack expansion args we won't do anything yet. */
12930 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12931 || PACK_EXPANSION_SIZEOF_P (t)
12932 || pack_expansion_args_count (args))
12933 return args;
12934 /* Also optimize expression pack expansions if we can tell that the
12935 elements won't have reference type. */
12936 tree type = TREE_TYPE (pattern);
12937 if (type && !TYPE_REF_P (type)
12938 && !PACK_EXPANSION_P (type)
12939 && !WILDCARD_TYPE_P (type))
12940 return args;
12941 /* Otherwise use the normal path so we get convert_from_reference. */
12942 }
12943
12944 /* We cannot expand this expansion expression, because we don't have
12945 all of the argument packs we need. */
12946 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12947 {
12948 /* We got some full packs, but we can't substitute them in until we
12949 have values for all the packs. So remember these until then. */
12950
12951 t = make_pack_expansion (pattern, complain);
12952 PACK_EXPANSION_EXTRA_ARGS (t)
12953 = build_extra_args (pattern, args, complain);
12954 return t;
12955 }
12956
12957 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
12958 type, so create our own local specializations map; the current map is
12959 either NULL or (in the case of recursive unification) might have
12960 bindings that we don't want to use or alter. */
12961 local_specialization_stack lss (need_local_specializations
12962 ? lss_blank : lss_nop);
12963
12964 if (unsubstituted_packs)
12965 {
12966 /* There were no real arguments, we're just replacing a parameter
12967 pack with another version of itself. Substitute into the
12968 pattern and return a PACK_EXPANSION_*. The caller will need to
12969 deal with that. */
12970 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12971 t = tsubst_expr (pattern, args, complain, in_decl,
12972 /*integral_constant_expression_p=*/false);
12973 else
12974 t = tsubst (pattern, args, complain, in_decl);
12975 t = make_pack_expansion (t, complain);
12976 return t;
12977 }
12978
12979 gcc_assert (len >= 0);
12980
12981 /* For each argument in each argument pack, substitute into the
12982 pattern. */
12983 result = make_tree_vec (len);
12984 tree elem_args = copy_template_args (args);
12985 for (i = 0; i < len; ++i)
12986 {
12987 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12988 i,
12989 elem_args, complain,
12990 in_decl);
12991 TREE_VEC_ELT (result, i) = t;
12992 if (t == error_mark_node)
12993 {
12994 result = error_mark_node;
12995 break;
12996 }
12997 }
12998
12999 /* Update ARGS to restore the substitution from parameter packs to
13000 their argument packs. */
13001 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13002 {
13003 tree parm = TREE_PURPOSE (pack);
13004
13005 if (TREE_CODE (parm) == PARM_DECL
13006 || VAR_P (parm)
13007 || TREE_CODE (parm) == FIELD_DECL)
13008 register_local_specialization (TREE_TYPE (pack), parm);
13009 else
13010 {
13011 int idx, level;
13012
13013 if (TREE_VALUE (pack) == NULL_TREE)
13014 continue;
13015
13016 template_parm_level_and_index (parm, &level, &idx);
13017
13018 /* Update the corresponding argument. */
13019 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13020 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13021 TREE_TYPE (pack);
13022 else
13023 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13024 }
13025 }
13026
13027 /* If the dependent pack arguments were such that we end up with only a
13028 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13029 if (len == 1 && TREE_CODE (result) == TREE_VEC
13030 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13031 return TREE_VEC_ELT (result, 0);
13032
13033 return result;
13034 }
13035
13036 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13037 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13038 parameter packs; all parms generated from a function parameter pack will
13039 have the same DECL_PARM_INDEX. */
13040
13041 tree
13042 get_pattern_parm (tree parm, tree tmpl)
13043 {
13044 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13045 tree patparm;
13046
13047 if (DECL_ARTIFICIAL (parm))
13048 {
13049 for (patparm = DECL_ARGUMENTS (pattern);
13050 patparm; patparm = DECL_CHAIN (patparm))
13051 if (DECL_ARTIFICIAL (patparm)
13052 && DECL_NAME (parm) == DECL_NAME (patparm))
13053 break;
13054 }
13055 else
13056 {
13057 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13058 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13059 gcc_assert (DECL_PARM_INDEX (patparm)
13060 == DECL_PARM_INDEX (parm));
13061 }
13062
13063 return patparm;
13064 }
13065
13066 /* Make an argument pack out of the TREE_VEC VEC. */
13067
13068 static tree
13069 make_argument_pack (tree vec)
13070 {
13071 tree pack;
13072
13073 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13074 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13075 else
13076 {
13077 pack = make_node (NONTYPE_ARGUMENT_PACK);
13078 TREE_CONSTANT (pack) = 1;
13079 }
13080 SET_ARGUMENT_PACK_ARGS (pack, vec);
13081 return pack;
13082 }
13083
13084 /* Return an exact copy of template args T that can be modified
13085 independently. */
13086
13087 static tree
13088 copy_template_args (tree t)
13089 {
13090 if (t == error_mark_node)
13091 return t;
13092
13093 int len = TREE_VEC_LENGTH (t);
13094 tree new_vec = make_tree_vec (len);
13095
13096 for (int i = 0; i < len; ++i)
13097 {
13098 tree elt = TREE_VEC_ELT (t, i);
13099 if (elt && TREE_CODE (elt) == TREE_VEC)
13100 elt = copy_template_args (elt);
13101 TREE_VEC_ELT (new_vec, i) = elt;
13102 }
13103
13104 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13105 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13106
13107 return new_vec;
13108 }
13109
13110 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13111
13112 tree
13113 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13114 tree in_decl)
13115 {
13116 /* Substitute into each of the arguments. */
13117 tree new_arg = TYPE_P (orig_arg)
13118 ? cxx_make_type (TREE_CODE (orig_arg))
13119 : make_node (TREE_CODE (orig_arg));
13120
13121 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13122 args, complain, in_decl);
13123 if (pack_args == error_mark_node)
13124 new_arg = error_mark_node;
13125 else
13126 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13127
13128 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13129 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13130
13131 return new_arg;
13132 }
13133
13134 /* Substitute ARGS into the vector or list of template arguments T. */
13135
13136 tree
13137 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13138 {
13139 tree orig_t = t;
13140 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13141 tree *elts;
13142
13143 if (t == error_mark_node)
13144 return error_mark_node;
13145
13146 len = TREE_VEC_LENGTH (t);
13147 elts = XALLOCAVEC (tree, len);
13148
13149 for (i = 0; i < len; i++)
13150 {
13151 tree orig_arg = TREE_VEC_ELT (t, i);
13152 tree new_arg;
13153
13154 if (TREE_CODE (orig_arg) == TREE_VEC)
13155 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13156 else if (PACK_EXPANSION_P (orig_arg))
13157 {
13158 /* Substitute into an expansion expression. */
13159 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13160
13161 if (TREE_CODE (new_arg) == TREE_VEC)
13162 /* Add to the expanded length adjustment the number of
13163 expanded arguments. We subtract one from this
13164 measurement, because the argument pack expression
13165 itself is already counted as 1 in
13166 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13167 the argument pack is empty. */
13168 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13169 }
13170 else if (ARGUMENT_PACK_P (orig_arg))
13171 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13172 else
13173 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13174
13175 if (new_arg == error_mark_node)
13176 return error_mark_node;
13177
13178 elts[i] = new_arg;
13179 if (new_arg != orig_arg)
13180 need_new = 1;
13181 }
13182
13183 if (!need_new)
13184 return t;
13185
13186 /* Make space for the expanded arguments coming from template
13187 argument packs. */
13188 t = make_tree_vec (len + expanded_len_adjust);
13189 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13190 arguments for a member template.
13191 In that case each TREE_VEC in ORIG_T represents a level of template
13192 arguments, and ORIG_T won't carry any non defaulted argument count.
13193 It will rather be the nested TREE_VECs that will carry one.
13194 In other words, ORIG_T carries a non defaulted argument count only
13195 if it doesn't contain any nested TREE_VEC. */
13196 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13197 {
13198 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13199 count += expanded_len_adjust;
13200 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13201 }
13202 for (i = 0, out = 0; i < len; i++)
13203 {
13204 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13205 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13206 && TREE_CODE (elts[i]) == TREE_VEC)
13207 {
13208 int idx;
13209
13210 /* Now expand the template argument pack "in place". */
13211 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13212 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13213 }
13214 else
13215 {
13216 TREE_VEC_ELT (t, out) = elts[i];
13217 out++;
13218 }
13219 }
13220
13221 return t;
13222 }
13223
13224 /* Substitute ARGS into one level PARMS of template parameters. */
13225
13226 static tree
13227 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13228 {
13229 if (parms == error_mark_node)
13230 return error_mark_node;
13231
13232 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13233
13234 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13235 {
13236 tree tuple = TREE_VEC_ELT (parms, i);
13237
13238 if (tuple == error_mark_node)
13239 continue;
13240
13241 TREE_VEC_ELT (new_vec, i) =
13242 tsubst_template_parm (tuple, args, complain);
13243 }
13244
13245 return new_vec;
13246 }
13247
13248 /* Return the result of substituting ARGS into the template parameters
13249 given by PARMS. If there are m levels of ARGS and m + n levels of
13250 PARMS, then the result will contain n levels of PARMS. For
13251 example, if PARMS is `template <class T> template <class U>
13252 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13253 result will be `template <int*, double, class V>'. */
13254
13255 static tree
13256 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13257 {
13258 tree r = NULL_TREE;
13259 tree* new_parms;
13260
13261 /* When substituting into a template, we must set
13262 PROCESSING_TEMPLATE_DECL as the template parameters may be
13263 dependent if they are based on one-another, and the dependency
13264 predicates are short-circuit outside of templates. */
13265 ++processing_template_decl;
13266
13267 for (new_parms = &r;
13268 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13269 new_parms = &(TREE_CHAIN (*new_parms)),
13270 parms = TREE_CHAIN (parms))
13271 {
13272 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13273 args, complain);
13274 *new_parms =
13275 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13276 - TMPL_ARGS_DEPTH (args)),
13277 new_vec, NULL_TREE);
13278 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13279 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13280 }
13281
13282 --processing_template_decl;
13283
13284 return r;
13285 }
13286
13287 /* Return the result of substituting ARGS into one template parameter
13288 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13289 parameter and which TREE_PURPOSE is the default argument of the
13290 template parameter. */
13291
13292 static tree
13293 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13294 {
13295 tree default_value, parm_decl;
13296
13297 if (args == NULL_TREE
13298 || t == NULL_TREE
13299 || t == error_mark_node)
13300 return t;
13301
13302 gcc_assert (TREE_CODE (t) == TREE_LIST);
13303
13304 default_value = TREE_PURPOSE (t);
13305 parm_decl = TREE_VALUE (t);
13306 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13307
13308 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13309 if (TREE_CODE (parm_decl) == PARM_DECL
13310 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13311 parm_decl = error_mark_node;
13312 default_value = tsubst_template_arg (default_value, args,
13313 complain, NULL_TREE);
13314 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13315
13316 tree r = build_tree_list (default_value, parm_decl);
13317 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13318 return r;
13319 }
13320
13321 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13322 type T. If T is not an aggregate or enumeration type, it is
13323 handled as if by tsubst. IN_DECL is as for tsubst. If
13324 ENTERING_SCOPE is nonzero, T is the context for a template which
13325 we are presently tsubst'ing. Return the substituted value. */
13326
13327 static tree
13328 tsubst_aggr_type (tree t,
13329 tree args,
13330 tsubst_flags_t complain,
13331 tree in_decl,
13332 int entering_scope)
13333 {
13334 if (t == NULL_TREE)
13335 return NULL_TREE;
13336
13337 switch (TREE_CODE (t))
13338 {
13339 case RECORD_TYPE:
13340 if (TYPE_PTRMEMFUNC_P (t))
13341 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13342
13343 /* Fall through. */
13344 case ENUMERAL_TYPE:
13345 case UNION_TYPE:
13346 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13347 {
13348 tree argvec;
13349 tree context;
13350 tree r;
13351
13352 /* In "sizeof(X<I>)" we need to evaluate "I". */
13353 cp_evaluated ev;
13354
13355 /* First, determine the context for the type we are looking
13356 up. */
13357 context = TYPE_CONTEXT (t);
13358 if (context && TYPE_P (context))
13359 {
13360 context = tsubst_aggr_type (context, args, complain,
13361 in_decl, /*entering_scope=*/1);
13362 /* If context is a nested class inside a class template,
13363 it may still need to be instantiated (c++/33959). */
13364 context = complete_type (context);
13365 }
13366
13367 /* Then, figure out what arguments are appropriate for the
13368 type we are trying to find. For example, given:
13369
13370 template <class T> struct S;
13371 template <class T, class U> void f(T, U) { S<U> su; }
13372
13373 and supposing that we are instantiating f<int, double>,
13374 then our ARGS will be {int, double}, but, when looking up
13375 S we only want {double}. */
13376 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13377 complain, in_decl);
13378 if (argvec == error_mark_node)
13379 r = error_mark_node;
13380 else
13381 {
13382 r = lookup_template_class (t, argvec, in_decl, context,
13383 entering_scope, complain);
13384 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13385 }
13386
13387 return r;
13388 }
13389 else
13390 /* This is not a template type, so there's nothing to do. */
13391 return t;
13392
13393 default:
13394 return tsubst (t, args, complain, in_decl);
13395 }
13396 }
13397
13398 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13399
13400 /* Substitute into the default argument ARG (a default argument for
13401 FN), which has the indicated TYPE. */
13402
13403 tree
13404 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13405 tsubst_flags_t complain)
13406 {
13407 int errs = errorcount + sorrycount;
13408
13409 /* This can happen in invalid code. */
13410 if (TREE_CODE (arg) == DEFERRED_PARSE)
13411 return arg;
13412
13413 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13414 parm = chain_index (parmnum, parm);
13415 tree parmtype = TREE_TYPE (parm);
13416 if (DECL_BY_REFERENCE (parm))
13417 parmtype = TREE_TYPE (parmtype);
13418 if (parmtype == error_mark_node)
13419 return error_mark_node;
13420
13421 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13422
13423 tree *slot;
13424 if (defarg_inst && (slot = defarg_inst->get (parm)))
13425 return *slot;
13426
13427 /* This default argument came from a template. Instantiate the
13428 default argument here, not in tsubst. In the case of
13429 something like:
13430
13431 template <class T>
13432 struct S {
13433 static T t();
13434 void f(T = t());
13435 };
13436
13437 we must be careful to do name lookup in the scope of S<T>,
13438 rather than in the current class. */
13439 push_to_top_level ();
13440 push_access_scope (fn);
13441 push_deferring_access_checks (dk_no_deferred);
13442 start_lambda_scope (parm);
13443
13444 /* The default argument expression may cause implicitly defined
13445 member functions to be synthesized, which will result in garbage
13446 collection. We must treat this situation as if we were within
13447 the body of function so as to avoid collecting live data on the
13448 stack. */
13449 ++function_depth;
13450 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13451 complain, NULL_TREE,
13452 /*integral_constant_expression_p=*/false);
13453 --function_depth;
13454
13455 finish_lambda_scope ();
13456
13457 /* Make sure the default argument is reasonable. */
13458 arg = check_default_argument (type, arg, complain);
13459
13460 if (errorcount+sorrycount > errs
13461 && (complain & tf_warning_or_error))
13462 inform (input_location,
13463 " when instantiating default argument for call to %qD", fn);
13464
13465 pop_deferring_access_checks ();
13466 pop_access_scope (fn);
13467 pop_from_top_level ();
13468
13469 if (arg != error_mark_node && !cp_unevaluated_operand)
13470 {
13471 if (!defarg_inst)
13472 defarg_inst = decl_tree_cache_map::create_ggc (37);
13473 defarg_inst->put (parm, arg);
13474 }
13475
13476 return arg;
13477 }
13478
13479 /* Substitute into all the default arguments for FN. */
13480
13481 static void
13482 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13483 {
13484 tree arg;
13485 tree tmpl_args;
13486
13487 tmpl_args = DECL_TI_ARGS (fn);
13488
13489 /* If this function is not yet instantiated, we certainly don't need
13490 its default arguments. */
13491 if (uses_template_parms (tmpl_args))
13492 return;
13493 /* Don't do this again for clones. */
13494 if (DECL_CLONED_FUNCTION_P (fn))
13495 return;
13496
13497 int i = 0;
13498 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13499 arg;
13500 arg = TREE_CHAIN (arg), ++i)
13501 if (TREE_PURPOSE (arg))
13502 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13503 TREE_VALUE (arg),
13504 TREE_PURPOSE (arg),
13505 complain);
13506 }
13507
13508 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13509 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13510
13511 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13512
13513 void
13514 store_explicit_specifier (tree v, tree t)
13515 {
13516 if (!explicit_specifier_map)
13517 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13518 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13519 explicit_specifier_map->put (v, t);
13520 }
13521
13522 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13523
13524 static tree
13525 lookup_explicit_specifier (tree v)
13526 {
13527 return *explicit_specifier_map->get (v);
13528 }
13529
13530 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13531 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13532 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13533 identical to T. */
13534
13535 static tree
13536 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13537 tree raises, tsubst_flags_t complain)
13538 {
13539 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13540
13541 tree new_type;
13542 if (TREE_CODE (t) == FUNCTION_TYPE)
13543 {
13544 new_type = build_function_type (return_type, arg_types);
13545 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13546 }
13547 else
13548 {
13549 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13550 /* Don't pick up extra function qualifiers from the basetype. */
13551 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13552 if (! MAYBE_CLASS_TYPE_P (r))
13553 {
13554 /* [temp.deduct]
13555
13556 Type deduction may fail for any of the following
13557 reasons:
13558
13559 -- Attempting to create "pointer to member of T" when T
13560 is not a class type. */
13561 if (complain & tf_error)
13562 error ("creating pointer to member function of non-class type %qT",
13563 r);
13564 return error_mark_node;
13565 }
13566
13567 new_type = build_method_type_directly (r, return_type,
13568 TREE_CHAIN (arg_types));
13569 }
13570 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13571
13572 cp_ref_qualifier rqual = type_memfn_rqual (t);
13573 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13574 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13575 }
13576
13577 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13578 each of its formal parameters. If there is a disagreement then rebuild
13579 DECL's function type according to its formal parameter types, as part of a
13580 resolution for Core issues 1001/1322. */
13581
13582 static void
13583 maybe_rebuild_function_decl_type (tree decl)
13584 {
13585 bool function_type_needs_rebuilding = false;
13586 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13587 {
13588 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13589 while (parm_type_list && parm_type_list != void_list_node)
13590 {
13591 tree parm_type = TREE_VALUE (parm_type_list);
13592 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13593 if (!same_type_p (parm_type, formal_parm_type_unqual))
13594 {
13595 function_type_needs_rebuilding = true;
13596 break;
13597 }
13598
13599 parm_list = DECL_CHAIN (parm_list);
13600 parm_type_list = TREE_CHAIN (parm_type_list);
13601 }
13602 }
13603
13604 if (!function_type_needs_rebuilding)
13605 return;
13606
13607 const tree fntype = TREE_TYPE (decl);
13608 tree parm_list = DECL_ARGUMENTS (decl);
13609 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13610 tree new_parm_type_list = NULL_TREE;
13611 tree *q = &new_parm_type_list;
13612 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13613 {
13614 *q = copy_node (old_parm_type_list);
13615 parm_list = DECL_CHAIN (parm_list);
13616 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13617 q = &TREE_CHAIN (*q);
13618 }
13619 while (old_parm_type_list && old_parm_type_list != void_list_node)
13620 {
13621 *q = copy_node (old_parm_type_list);
13622 tree *new_parm_type = &TREE_VALUE (*q);
13623 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13624 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13625 *new_parm_type = formal_parm_type_unqual;
13626
13627 parm_list = DECL_CHAIN (parm_list);
13628 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13629 q = &TREE_CHAIN (*q);
13630 }
13631 if (old_parm_type_list == void_list_node)
13632 *q = void_list_node;
13633
13634 TREE_TYPE (decl)
13635 = rebuild_function_or_method_type (fntype,
13636 TREE_TYPE (fntype), new_parm_type_list,
13637 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13638 }
13639
13640 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13641
13642 static tree
13643 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13644 tree lambda_fntype)
13645 {
13646 tree gen_tmpl, argvec;
13647 hashval_t hash = 0;
13648 tree in_decl = t;
13649
13650 /* Nobody should be tsubst'ing into non-template functions. */
13651 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
13652
13653 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13654 {
13655 /* If T is not dependent, just return it. */
13656 if (!uses_template_parms (DECL_TI_ARGS (t))
13657 && !LAMBDA_FUNCTION_P (t))
13658 return t;
13659
13660 /* Calculate the most general template of which R is a
13661 specialization. */
13662 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13663
13664 /* We're substituting a lambda function under tsubst_lambda_expr but not
13665 directly from it; find the matching function we're already inside.
13666 But don't do this if T is a generic lambda with a single level of
13667 template parms, as in that case we're doing a normal instantiation. */
13668 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13669 && (!generic_lambda_fn_p (t)
13670 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13671 return enclosing_instantiation_of (t);
13672
13673 /* Calculate the complete set of arguments used to
13674 specialize R. */
13675 argvec = tsubst_template_args (DECL_TI_ARGS
13676 (DECL_TEMPLATE_RESULT
13677 (DECL_TI_TEMPLATE (t))),
13678 args, complain, in_decl);
13679 if (argvec == error_mark_node)
13680 return error_mark_node;
13681
13682 /* Check to see if we already have this specialization. */
13683 if (!lambda_fntype)
13684 {
13685 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13686 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13687 return spec;
13688 }
13689
13690 /* We can see more levels of arguments than parameters if
13691 there was a specialization of a member template, like
13692 this:
13693
13694 template <class T> struct S { template <class U> void f(); }
13695 template <> template <class U> void S<int>::f(U);
13696
13697 Here, we'll be substituting into the specialization,
13698 because that's where we can find the code we actually
13699 want to generate, but we'll have enough arguments for
13700 the most general template.
13701
13702 We also deal with the peculiar case:
13703
13704 template <class T> struct S {
13705 template <class U> friend void f();
13706 };
13707 template <class U> void f() {}
13708 template S<int>;
13709 template void f<double>();
13710
13711 Here, the ARGS for the instantiation of will be {int,
13712 double}. But, we only need as many ARGS as there are
13713 levels of template parameters in CODE_PATTERN. We are
13714 careful not to get fooled into reducing the ARGS in
13715 situations like:
13716
13717 template <class T> struct S { template <class U> void f(U); }
13718 template <class T> template <> void S<T>::f(int) {}
13719
13720 which we can spot because the pattern will be a
13721 specialization in this case. */
13722 int args_depth = TMPL_ARGS_DEPTH (args);
13723 int parms_depth =
13724 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13725
13726 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13727 args = get_innermost_template_args (args, parms_depth);
13728 }
13729 else
13730 {
13731 /* This special case arises when we have something like this:
13732
13733 template <class T> struct S {
13734 friend void f<int>(int, double);
13735 };
13736
13737 Here, the DECL_TI_TEMPLATE for the friend declaration
13738 will be an IDENTIFIER_NODE. We are being called from
13739 tsubst_friend_function, and we want only to create a
13740 new decl (R) with appropriate types so that we can call
13741 determine_specialization. */
13742 gen_tmpl = NULL_TREE;
13743 argvec = NULL_TREE;
13744 }
13745
13746 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13747 : NULL_TREE);
13748 tree ctx = closure ? closure : DECL_CONTEXT (t);
13749 bool member = ctx && TYPE_P (ctx);
13750
13751 if (member && !closure)
13752 ctx = tsubst_aggr_type (ctx, args,
13753 complain, t, /*entering_scope=*/1);
13754
13755 tree type = (lambda_fntype ? lambda_fntype
13756 : tsubst (TREE_TYPE (t), args,
13757 complain | tf_fndecl_type, in_decl));
13758 if (type == error_mark_node)
13759 return error_mark_node;
13760
13761 /* If we hit excessive deduction depth, the type is bogus even if
13762 it isn't error_mark_node, so don't build a decl. */
13763 if (excessive_deduction_depth)
13764 return error_mark_node;
13765
13766 /* We do NOT check for matching decls pushed separately at this
13767 point, as they may not represent instantiations of this
13768 template, and in any case are considered separate under the
13769 discrete model. */
13770 tree r = copy_decl (t);
13771 DECL_USE_TEMPLATE (r) = 0;
13772 TREE_TYPE (r) = type;
13773 /* Clear out the mangled name and RTL for the instantiation. */
13774 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13775 SET_DECL_RTL (r, NULL);
13776 /* Leave DECL_INITIAL set on deleted instantiations. */
13777 if (!DECL_DELETED_FN (r))
13778 DECL_INITIAL (r) = NULL_TREE;
13779 DECL_CONTEXT (r) = ctx;
13780
13781 /* Handle explicit(dependent-expr). */
13782 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13783 {
13784 tree spec = lookup_explicit_specifier (t);
13785 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13786 /*function_p=*/false,
13787 /*i_c_e_p=*/true);
13788 spec = build_explicit_specifier (spec, complain);
13789 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13790 }
13791
13792 /* OpenMP UDRs have the only argument a reference to the declared
13793 type. We want to diagnose if the declared type is a reference,
13794 which is invalid, but as references to references are usually
13795 quietly merged, diagnose it here. */
13796 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13797 {
13798 tree argtype
13799 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13800 argtype = tsubst (argtype, args, complain, in_decl);
13801 if (TYPE_REF_P (argtype))
13802 error_at (DECL_SOURCE_LOCATION (t),
13803 "reference type %qT in "
13804 "%<#pragma omp declare reduction%>", argtype);
13805 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13806 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13807 argtype);
13808 }
13809
13810 if (member && DECL_CONV_FN_P (r))
13811 /* Type-conversion operator. Reconstruct the name, in
13812 case it's the name of one of the template's parameters. */
13813 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13814
13815 tree parms = DECL_ARGUMENTS (t);
13816 if (closure)
13817 parms = DECL_CHAIN (parms);
13818 parms = tsubst (parms, args, complain, t);
13819 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13820 DECL_CONTEXT (parm) = r;
13821 if (closure)
13822 {
13823 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13824 DECL_NAME (tparm) = closure_identifier;
13825 DECL_CHAIN (tparm) = parms;
13826 parms = tparm;
13827 }
13828 DECL_ARGUMENTS (r) = parms;
13829 DECL_RESULT (r) = NULL_TREE;
13830
13831 maybe_rebuild_function_decl_type (r);
13832
13833 TREE_STATIC (r) = 0;
13834 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13835 DECL_EXTERNAL (r) = 1;
13836 /* If this is an instantiation of a function with internal
13837 linkage, we already know what object file linkage will be
13838 assigned to the instantiation. */
13839 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13840 DECL_DEFER_OUTPUT (r) = 0;
13841 DECL_CHAIN (r) = NULL_TREE;
13842 DECL_PENDING_INLINE_INFO (r) = 0;
13843 DECL_PENDING_INLINE_P (r) = 0;
13844 DECL_SAVED_TREE (r) = NULL_TREE;
13845 DECL_STRUCT_FUNCTION (r) = NULL;
13846 TREE_USED (r) = 0;
13847 /* We'll re-clone as appropriate in instantiate_template. */
13848 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13849
13850 /* If we aren't complaining now, return on error before we register
13851 the specialization so that we'll complain eventually. */
13852 if ((complain & tf_error) == 0
13853 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13854 && !grok_op_properties (r, /*complain=*/false))
13855 return error_mark_node;
13856
13857 /* Associate the constraints directly with the instantiation. We
13858 don't substitute through the constraints; that's only done when
13859 they are checked. */
13860 if (tree ci = get_constraints (t))
13861 set_constraints (r, ci);
13862
13863 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13864 SET_DECL_FRIEND_CONTEXT (r,
13865 tsubst (DECL_FRIEND_CONTEXT (t),
13866 args, complain, in_decl));
13867
13868 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13869 this in the special friend case mentioned above where
13870 GEN_TMPL is NULL. */
13871 if (gen_tmpl && !closure)
13872 {
13873 DECL_TEMPLATE_INFO (r)
13874 = build_template_info (gen_tmpl, argvec);
13875 SET_DECL_IMPLICIT_INSTANTIATION (r);
13876
13877 tree new_r
13878 = register_specialization (r, gen_tmpl, argvec, false, hash);
13879 if (new_r != r)
13880 /* We instantiated this while substituting into
13881 the type earlier (template/friend54.C). */
13882 return new_r;
13883
13884 /* We're not supposed to instantiate default arguments
13885 until they are called, for a template. But, for a
13886 declaration like:
13887
13888 template <class T> void f ()
13889 { extern void g(int i = T()); }
13890
13891 we should do the substitution when the template is
13892 instantiated. We handle the member function case in
13893 instantiate_class_template since the default arguments
13894 might refer to other members of the class. */
13895 if (!member
13896 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13897 && !uses_template_parms (argvec))
13898 tsubst_default_arguments (r, complain);
13899 }
13900 else
13901 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13902
13903 /* Copy the list of befriending classes. */
13904 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13905 *friends;
13906 friends = &TREE_CHAIN (*friends))
13907 {
13908 *friends = copy_node (*friends);
13909 TREE_VALUE (*friends)
13910 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13911 }
13912
13913 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13914 {
13915 maybe_retrofit_in_chrg (r);
13916 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13917 return error_mark_node;
13918 /* If this is an instantiation of a member template, clone it.
13919 If it isn't, that'll be handled by
13920 clone_constructors_and_destructors. */
13921 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13922 clone_function_decl (r, /*update_methods=*/false);
13923 }
13924 else if ((complain & tf_error) != 0
13925 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13926 && !grok_op_properties (r, /*complain=*/true))
13927 return error_mark_node;
13928
13929 /* Possibly limit visibility based on template args. */
13930 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13931 if (DECL_VISIBILITY_SPECIFIED (t))
13932 {
13933 DECL_VISIBILITY_SPECIFIED (r) = 0;
13934 DECL_ATTRIBUTES (r)
13935 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13936 }
13937 determine_visibility (r);
13938 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13939 && !processing_template_decl)
13940 defaulted_late_check (r);
13941
13942 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13943 args, complain, in_decl);
13944 if (flag_openmp)
13945 if (tree attr = lookup_attribute ("omp declare variant base",
13946 DECL_ATTRIBUTES (r)))
13947 omp_declare_variant_finalize (r, attr);
13948
13949 return r;
13950 }
13951
13952 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13953
13954 static tree
13955 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13956 tree lambda_fntype)
13957 {
13958 /* We can get here when processing a member function template,
13959 member class template, or template template parameter. */
13960 tree decl = DECL_TEMPLATE_RESULT (t);
13961 tree in_decl = t;
13962 tree spec;
13963 tree tmpl_args;
13964 tree full_args;
13965 tree r;
13966 hashval_t hash = 0;
13967
13968 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13969 {
13970 /* Template template parameter is treated here. */
13971 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13972 if (new_type == error_mark_node)
13973 r = error_mark_node;
13974 /* If we get a real template back, return it. This can happen in
13975 the context of most_specialized_partial_spec. */
13976 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13977 r = new_type;
13978 else
13979 /* The new TEMPLATE_DECL was built in
13980 reduce_template_parm_level. */
13981 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13982 return r;
13983 }
13984
13985 if (!lambda_fntype)
13986 {
13987 /* We might already have an instance of this template.
13988 The ARGS are for the surrounding class type, so the
13989 full args contain the tsubst'd args for the context,
13990 plus the innermost args from the template decl. */
13991 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13992 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13993 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13994 /* Because this is a template, the arguments will still be
13995 dependent, even after substitution. If
13996 PROCESSING_TEMPLATE_DECL is not set, the dependency
13997 predicates will short-circuit. */
13998 ++processing_template_decl;
13999 full_args = tsubst_template_args (tmpl_args, args,
14000 complain, in_decl);
14001 --processing_template_decl;
14002 if (full_args == error_mark_node)
14003 return error_mark_node;
14004
14005 /* If this is a default template template argument,
14006 tsubst might not have changed anything. */
14007 if (full_args == tmpl_args)
14008 return t;
14009
14010 hash = hash_tmpl_and_args (t, full_args);
14011 spec = retrieve_specialization (t, full_args, hash);
14012 if (spec != NULL_TREE)
14013 {
14014 if (TYPE_P (spec))
14015 /* Type partial instantiations are stored as the type by
14016 lookup_template_class_1, not here as the template. */
14017 spec = CLASSTYPE_TI_TEMPLATE (spec);
14018 return spec;
14019 }
14020 }
14021
14022 /* Make a new template decl. It will be similar to the
14023 original, but will record the current template arguments.
14024 We also create a new function declaration, which is just
14025 like the old one, but points to this new template, rather
14026 than the old one. */
14027 r = copy_decl (t);
14028 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14029 DECL_CHAIN (r) = NULL_TREE;
14030
14031 // Build new template info linking to the original template decl.
14032 if (!lambda_fntype)
14033 {
14034 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14035 SET_DECL_IMPLICIT_INSTANTIATION (r);
14036 }
14037 else
14038 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14039
14040 /* The template parameters for this new template are all the
14041 template parameters for the old template, except the
14042 outermost level of parameters. */
14043 DECL_TEMPLATE_PARMS (r)
14044 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14045 complain);
14046
14047 bool class_p = false;
14048 tree inner = decl;
14049 ++processing_template_decl;
14050 if (TREE_CODE (inner) == FUNCTION_DECL)
14051 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14052 else
14053 {
14054 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14055 {
14056 class_p = true;
14057 inner = TREE_TYPE (inner);
14058 }
14059 inner = tsubst (inner, args, complain, in_decl);
14060 }
14061 --processing_template_decl;
14062 if (inner == error_mark_node)
14063 return error_mark_node;
14064
14065 if (class_p)
14066 {
14067 /* For a partial specialization, we need to keep pointing to
14068 the primary template. */
14069 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14070 CLASSTYPE_TI_TEMPLATE (inner) = r;
14071
14072 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14073 inner = TYPE_MAIN_DECL (inner);
14074 }
14075 else if (lambda_fntype)
14076 {
14077 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14078 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14079 }
14080 else
14081 {
14082 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14083 DECL_TI_TEMPLATE (inner) = r;
14084 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14085 }
14086
14087 DECL_TEMPLATE_RESULT (r) = inner;
14088 TREE_TYPE (r) = TREE_TYPE (inner);
14089 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14090
14091 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14092 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14093
14094 if (PRIMARY_TEMPLATE_P (t))
14095 DECL_PRIMARY_TEMPLATE (r) = r;
14096
14097 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
14098 && !lambda_fntype)
14099 /* Record this non-type partial instantiation. */
14100 register_specialization (r, t,
14101 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14102 false, hash);
14103
14104 return r;
14105 }
14106
14107 /* True if FN is the op() for a lambda in an uninstantiated template. */
14108
14109 bool
14110 lambda_fn_in_template_p (tree fn)
14111 {
14112 if (!fn || !LAMBDA_FUNCTION_P (fn))
14113 return false;
14114 tree closure = DECL_CONTEXT (fn);
14115 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14116 }
14117
14118 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14119 which the above is true. */
14120
14121 bool
14122 instantiated_lambda_fn_p (tree fn)
14123 {
14124 if (!fn || !LAMBDA_FUNCTION_P (fn))
14125 return false;
14126 tree closure = DECL_CONTEXT (fn);
14127 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14128 return LAMBDA_EXPR_INSTANTIATED (lam);
14129 }
14130
14131 /* We're instantiating a variable from template function TCTX. Return the
14132 corresponding current enclosing scope. This gets complicated because lambda
14133 functions in templates are regenerated rather than instantiated, but generic
14134 lambda functions are subsequently instantiated. */
14135
14136 static tree
14137 enclosing_instantiation_of (tree otctx)
14138 {
14139 tree tctx = otctx;
14140 tree fn = current_function_decl;
14141 int lambda_count = 0;
14142
14143 for (; tctx && (lambda_fn_in_template_p (tctx)
14144 || instantiated_lambda_fn_p (tctx));
14145 tctx = decl_function_context (tctx))
14146 ++lambda_count;
14147 for (; fn; fn = decl_function_context (fn))
14148 {
14149 tree ofn = fn;
14150 int flambda_count = 0;
14151 for (; fn && instantiated_lambda_fn_p (fn);
14152 fn = decl_function_context (fn))
14153 ++flambda_count;
14154 if ((fn && DECL_TEMPLATE_INFO (fn))
14155 ? most_general_template (fn) != most_general_template (tctx)
14156 : fn != tctx)
14157 continue;
14158 if (flambda_count != lambda_count)
14159 {
14160 gcc_assert (flambda_count > lambda_count);
14161 for (; flambda_count > lambda_count; --flambda_count)
14162 ofn = decl_function_context (ofn);
14163 }
14164 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14165 || DECL_CONV_FN_P (ofn));
14166 return ofn;
14167 }
14168 gcc_unreachable ();
14169 }
14170
14171 /* Substitute the ARGS into the T, which is a _DECL. Return the
14172 result of the substitution. Issue error and warning messages under
14173 control of COMPLAIN. */
14174
14175 static tree
14176 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14177 {
14178 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14179 location_t saved_loc;
14180 tree r = NULL_TREE;
14181 tree in_decl = t;
14182 hashval_t hash = 0;
14183
14184 /* Set the filename and linenumber to improve error-reporting. */
14185 saved_loc = input_location;
14186 input_location = DECL_SOURCE_LOCATION (t);
14187
14188 switch (TREE_CODE (t))
14189 {
14190 case TEMPLATE_DECL:
14191 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14192 break;
14193
14194 case FUNCTION_DECL:
14195 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14196 break;
14197
14198 case PARM_DECL:
14199 {
14200 tree type = NULL_TREE;
14201 int i, len = 1;
14202 tree expanded_types = NULL_TREE;
14203 tree prev_r = NULL_TREE;
14204 tree first_r = NULL_TREE;
14205
14206 if (DECL_PACK_P (t))
14207 {
14208 /* If there is a local specialization that isn't a
14209 parameter pack, it means that we're doing a "simple"
14210 substitution from inside tsubst_pack_expansion. Just
14211 return the local specialization (which will be a single
14212 parm). */
14213 tree spec = retrieve_local_specialization (t);
14214 if (spec
14215 && TREE_CODE (spec) == PARM_DECL
14216 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14217 RETURN (spec);
14218
14219 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14220 the parameters in this function parameter pack. */
14221 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14222 complain, in_decl);
14223 if (TREE_CODE (expanded_types) == TREE_VEC)
14224 {
14225 len = TREE_VEC_LENGTH (expanded_types);
14226
14227 /* Zero-length parameter packs are boring. Just substitute
14228 into the chain. */
14229 if (len == 0 && !cp_unevaluated_operand)
14230 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14231 TREE_CHAIN (t)));
14232 }
14233 else
14234 {
14235 /* All we did was update the type. Make a note of that. */
14236 type = expanded_types;
14237 expanded_types = NULL_TREE;
14238 }
14239 }
14240
14241 /* Loop through all of the parameters we'll build. When T is
14242 a function parameter pack, LEN is the number of expanded
14243 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14244 r = NULL_TREE;
14245 for (i = 0; i < len; ++i)
14246 {
14247 prev_r = r;
14248 r = copy_node (t);
14249 if (DECL_TEMPLATE_PARM_P (t))
14250 SET_DECL_TEMPLATE_PARM_P (r);
14251
14252 if (expanded_types)
14253 /* We're on the Ith parameter of the function parameter
14254 pack. */
14255 {
14256 /* Get the Ith type. */
14257 type = TREE_VEC_ELT (expanded_types, i);
14258
14259 /* Rename the parameter to include the index. */
14260 DECL_NAME (r)
14261 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14262 }
14263 else if (!type)
14264 /* We're dealing with a normal parameter. */
14265 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14266
14267 type = type_decays_to (type);
14268 TREE_TYPE (r) = type;
14269 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14270
14271 if (DECL_INITIAL (r))
14272 {
14273 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14274 DECL_INITIAL (r) = TREE_TYPE (r);
14275 else
14276 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14277 complain, in_decl);
14278 }
14279
14280 DECL_CONTEXT (r) = NULL_TREE;
14281
14282 if (!DECL_TEMPLATE_PARM_P (r))
14283 DECL_ARG_TYPE (r) = type_passed_as (type);
14284
14285 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14286 args, complain, in_decl);
14287
14288 /* Keep track of the first new parameter we
14289 generate. That's what will be returned to the
14290 caller. */
14291 if (!first_r)
14292 first_r = r;
14293
14294 /* Build a proper chain of parameters when substituting
14295 into a function parameter pack. */
14296 if (prev_r)
14297 DECL_CHAIN (prev_r) = r;
14298 }
14299
14300 /* If cp_unevaluated_operand is set, we're just looking for a
14301 single dummy parameter, so don't keep going. */
14302 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14303 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14304 complain, DECL_CHAIN (t));
14305
14306 /* FIRST_R contains the start of the chain we've built. */
14307 r = first_r;
14308 }
14309 break;
14310
14311 case FIELD_DECL:
14312 {
14313 tree type = NULL_TREE;
14314 tree vec = NULL_TREE;
14315 tree expanded_types = NULL_TREE;
14316 int len = 1;
14317
14318 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14319 {
14320 /* This field is a lambda capture pack. Return a TREE_VEC of
14321 the expanded fields to instantiate_class_template_1. */
14322 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14323 complain, in_decl);
14324 if (TREE_CODE (expanded_types) == TREE_VEC)
14325 {
14326 len = TREE_VEC_LENGTH (expanded_types);
14327 vec = make_tree_vec (len);
14328 }
14329 else
14330 {
14331 /* All we did was update the type. Make a note of that. */
14332 type = expanded_types;
14333 expanded_types = NULL_TREE;
14334 }
14335 }
14336
14337 for (int i = 0; i < len; ++i)
14338 {
14339 r = copy_decl (t);
14340 if (expanded_types)
14341 {
14342 type = TREE_VEC_ELT (expanded_types, i);
14343 DECL_NAME (r)
14344 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14345 }
14346 else if (!type)
14347 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14348
14349 if (type == error_mark_node)
14350 RETURN (error_mark_node);
14351 TREE_TYPE (r) = type;
14352 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14353
14354 if (DECL_C_BIT_FIELD (r))
14355 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14356 number of bits. */
14357 DECL_BIT_FIELD_REPRESENTATIVE (r)
14358 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14359 complain, in_decl,
14360 /*integral_constant_expression_p=*/true);
14361 if (DECL_INITIAL (t))
14362 {
14363 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14364 NSDMI in perform_member_init. Still set DECL_INITIAL
14365 so that we know there is one. */
14366 DECL_INITIAL (r) = void_node;
14367 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14368 retrofit_lang_decl (r);
14369 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14370 }
14371 /* We don't have to set DECL_CONTEXT here; it is set by
14372 finish_member_declaration. */
14373 DECL_CHAIN (r) = NULL_TREE;
14374
14375 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14376 args, complain, in_decl);
14377
14378 if (vec)
14379 TREE_VEC_ELT (vec, i) = r;
14380 }
14381
14382 if (vec)
14383 r = vec;
14384 }
14385 break;
14386
14387 case USING_DECL:
14388 /* We reach here only for member using decls. We also need to check
14389 uses_template_parms because DECL_DEPENDENT_P is not set for a
14390 using-declaration that designates a member of the current
14391 instantiation (c++/53549). */
14392 if (DECL_DEPENDENT_P (t)
14393 || uses_template_parms (USING_DECL_SCOPE (t)))
14394 {
14395 tree scope = USING_DECL_SCOPE (t);
14396 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14397 if (PACK_EXPANSION_P (scope))
14398 {
14399 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14400 int len = TREE_VEC_LENGTH (vec);
14401 r = make_tree_vec (len);
14402 for (int i = 0; i < len; ++i)
14403 {
14404 tree escope = TREE_VEC_ELT (vec, i);
14405 tree elt = do_class_using_decl (escope, name);
14406 if (!elt)
14407 {
14408 r = error_mark_node;
14409 break;
14410 }
14411 else
14412 {
14413 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14414 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14415 }
14416 TREE_VEC_ELT (r, i) = elt;
14417 }
14418 }
14419 else
14420 {
14421 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14422 complain, in_decl);
14423 r = do_class_using_decl (inst_scope, name);
14424 if (!r)
14425 r = error_mark_node;
14426 else
14427 {
14428 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14429 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14430 }
14431 }
14432 }
14433 else
14434 {
14435 r = copy_node (t);
14436 DECL_CHAIN (r) = NULL_TREE;
14437 }
14438 break;
14439
14440 case TYPE_DECL:
14441 case VAR_DECL:
14442 {
14443 tree argvec = NULL_TREE;
14444 tree gen_tmpl = NULL_TREE;
14445 tree spec;
14446 tree tmpl = NULL_TREE;
14447 tree ctx;
14448 tree type = NULL_TREE;
14449 bool local_p;
14450
14451 if (TREE_TYPE (t) == error_mark_node)
14452 RETURN (error_mark_node);
14453
14454 if (TREE_CODE (t) == TYPE_DECL
14455 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14456 {
14457 /* If this is the canonical decl, we don't have to
14458 mess with instantiations, and often we can't (for
14459 typename, template type parms and such). Note that
14460 TYPE_NAME is not correct for the above test if
14461 we've copied the type for a typedef. */
14462 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14463 if (type == error_mark_node)
14464 RETURN (error_mark_node);
14465 r = TYPE_NAME (type);
14466 break;
14467 }
14468
14469 /* Check to see if we already have the specialization we
14470 need. */
14471 spec = NULL_TREE;
14472 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
14473 {
14474 /* T is a static data member or namespace-scope entity.
14475 We have to substitute into namespace-scope variables
14476 (not just variable templates) because of cases like:
14477
14478 template <class T> void f() { extern T t; }
14479
14480 where the entity referenced is not known until
14481 instantiation time. */
14482 local_p = false;
14483 ctx = DECL_CONTEXT (t);
14484 if (DECL_CLASS_SCOPE_P (t))
14485 {
14486 ctx = tsubst_aggr_type (ctx, args,
14487 complain,
14488 in_decl, /*entering_scope=*/1);
14489 /* If CTX is unchanged, then T is in fact the
14490 specialization we want. That situation occurs when
14491 referencing a static data member within in its own
14492 class. We can use pointer equality, rather than
14493 same_type_p, because DECL_CONTEXT is always
14494 canonical... */
14495 if (ctx == DECL_CONTEXT (t)
14496 /* ... unless T is a member template; in which
14497 case our caller can be willing to create a
14498 specialization of that template represented
14499 by T. */
14500 && !(DECL_TI_TEMPLATE (t)
14501 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14502 spec = t;
14503 }
14504
14505 if (!spec)
14506 {
14507 tmpl = DECL_TI_TEMPLATE (t);
14508 gen_tmpl = most_general_template (tmpl);
14509 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14510 if (argvec != error_mark_node)
14511 argvec = (coerce_innermost_template_parms
14512 (DECL_TEMPLATE_PARMS (gen_tmpl),
14513 argvec, t, complain,
14514 /*all*/true, /*defarg*/true));
14515 if (argvec == error_mark_node)
14516 RETURN (error_mark_node);
14517 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14518 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14519 }
14520 }
14521 else
14522 {
14523 /* A local variable. */
14524 local_p = true;
14525 /* Subsequent calls to pushdecl will fill this in. */
14526 ctx = NULL_TREE;
14527 /* Unless this is a reference to a static variable from an
14528 enclosing function, in which case we need to fill it in now. */
14529 if (TREE_STATIC (t))
14530 {
14531 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14532 if (fn != current_function_decl)
14533 ctx = fn;
14534 }
14535 spec = retrieve_local_specialization (t);
14536 }
14537 /* If we already have the specialization we need, there is
14538 nothing more to do. */
14539 if (spec)
14540 {
14541 r = spec;
14542 break;
14543 }
14544
14545 /* Create a new node for the specialization we need. */
14546 if (type == NULL_TREE)
14547 {
14548 if (is_typedef_decl (t))
14549 type = DECL_ORIGINAL_TYPE (t);
14550 else
14551 type = TREE_TYPE (t);
14552 if (VAR_P (t)
14553 && VAR_HAD_UNKNOWN_BOUND (t)
14554 && type != error_mark_node)
14555 type = strip_array_domain (type);
14556 tree sub_args = args;
14557 if (tree auto_node = type_uses_auto (type))
14558 {
14559 /* Mask off any template args past the variable's context so we
14560 don't replace the auto with an unrelated argument. */
14561 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14562 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14563 if (extra > 0)
14564 /* This should never happen with the new lambda instantiation
14565 model, but keep the handling just in case. */
14566 gcc_assert (!CHECKING_P),
14567 sub_args = strip_innermost_template_args (args, extra);
14568 }
14569 type = tsubst (type, sub_args, complain, in_decl);
14570 /* Substituting the type might have recursively instantiated this
14571 same alias (c++/86171). */
14572 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14573 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14574 {
14575 r = spec;
14576 break;
14577 }
14578 }
14579 r = copy_decl (t);
14580 if (VAR_P (r))
14581 {
14582 DECL_INITIALIZED_P (r) = 0;
14583 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14584 if (type == error_mark_node)
14585 RETURN (error_mark_node);
14586 if (TREE_CODE (type) == FUNCTION_TYPE)
14587 {
14588 /* It may seem that this case cannot occur, since:
14589
14590 typedef void f();
14591 void g() { f x; }
14592
14593 declares a function, not a variable. However:
14594
14595 typedef void f();
14596 template <typename T> void g() { T t; }
14597 template void g<f>();
14598
14599 is an attempt to declare a variable with function
14600 type. */
14601 error ("variable %qD has function type",
14602 /* R is not yet sufficiently initialized, so we
14603 just use its name. */
14604 DECL_NAME (r));
14605 RETURN (error_mark_node);
14606 }
14607 type = complete_type (type);
14608 /* Wait until cp_finish_decl to set this again, to handle
14609 circular dependency (template/instantiate6.C). */
14610 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14611 type = check_var_type (DECL_NAME (r), type,
14612 DECL_SOURCE_LOCATION (r));
14613 if (DECL_HAS_VALUE_EXPR_P (t))
14614 {
14615 tree ve = DECL_VALUE_EXPR (t);
14616 /* If the DECL_VALUE_EXPR is converted to the declared type,
14617 preserve the identity so that gimplify_type_sizes works. */
14618 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14619 if (nop)
14620 ve = TREE_OPERAND (ve, 0);
14621 ve = tsubst_expr (ve, args, complain, in_decl,
14622 /*constant_expression_p=*/false);
14623 if (REFERENCE_REF_P (ve))
14624 {
14625 gcc_assert (TYPE_REF_P (type));
14626 ve = TREE_OPERAND (ve, 0);
14627 }
14628 if (nop)
14629 ve = build_nop (type, ve);
14630 else if (DECL_LANG_SPECIFIC (t)
14631 && DECL_OMP_PRIVATIZED_MEMBER (t)
14632 && TREE_CODE (ve) == COMPONENT_REF
14633 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14634 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14635 type = TREE_TYPE (ve);
14636 else
14637 gcc_checking_assert (TREE_TYPE (ve) == type);
14638 SET_DECL_VALUE_EXPR (r, ve);
14639 }
14640 if (CP_DECL_THREAD_LOCAL_P (r)
14641 && !processing_template_decl)
14642 set_decl_tls_model (r, decl_default_tls_model (r));
14643 }
14644 else if (DECL_SELF_REFERENCE_P (t))
14645 SET_DECL_SELF_REFERENCE_P (r);
14646 TREE_TYPE (r) = type;
14647 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14648 DECL_CONTEXT (r) = ctx;
14649 /* Clear out the mangled name and RTL for the instantiation. */
14650 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14651 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14652 SET_DECL_RTL (r, NULL);
14653 /* The initializer must not be expanded until it is required;
14654 see [temp.inst]. */
14655 DECL_INITIAL (r) = NULL_TREE;
14656 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14657 if (VAR_P (r))
14658 {
14659 if (DECL_LANG_SPECIFIC (r))
14660 SET_DECL_DEPENDENT_INIT_P (r, false);
14661
14662 SET_DECL_MODE (r, VOIDmode);
14663
14664 /* Possibly limit visibility based on template args. */
14665 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14666 if (DECL_VISIBILITY_SPECIFIED (t))
14667 {
14668 DECL_VISIBILITY_SPECIFIED (r) = 0;
14669 DECL_ATTRIBUTES (r)
14670 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14671 }
14672 determine_visibility (r);
14673 }
14674
14675 if (!local_p)
14676 {
14677 /* A static data member declaration is always marked
14678 external when it is declared in-class, even if an
14679 initializer is present. We mimic the non-template
14680 processing here. */
14681 DECL_EXTERNAL (r) = 1;
14682 if (DECL_NAMESPACE_SCOPE_P (t))
14683 DECL_NOT_REALLY_EXTERN (r) = 1;
14684
14685 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14686 SET_DECL_IMPLICIT_INSTANTIATION (r);
14687 /* Remember whether we require constant initialization of
14688 a non-constant template variable. */
14689 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
14690 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
14691 if (!error_operand_p (r) || (complain & tf_error))
14692 register_specialization (r, gen_tmpl, argvec, false, hash);
14693 }
14694 else
14695 {
14696 if (DECL_LANG_SPECIFIC (r))
14697 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14698 if (!cp_unevaluated_operand)
14699 register_local_specialization (r, t);
14700 }
14701
14702 DECL_CHAIN (r) = NULL_TREE;
14703
14704 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14705 /*flags=*/0,
14706 args, complain, in_decl);
14707
14708 /* Preserve a typedef that names a type. */
14709 if (is_typedef_decl (r) && type != error_mark_node)
14710 {
14711 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14712 set_underlying_type (r);
14713 if (TYPE_DECL_ALIAS_P (r))
14714 /* An alias template specialization can be dependent
14715 even if its underlying type is not. */
14716 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14717 }
14718
14719 layout_decl (r, 0);
14720 }
14721 break;
14722
14723 default:
14724 gcc_unreachable ();
14725 }
14726 #undef RETURN
14727
14728 out:
14729 /* Restore the file and line information. */
14730 input_location = saved_loc;
14731
14732 return r;
14733 }
14734
14735 /* Substitute into the complete parameter type list PARMS. */
14736
14737 tree
14738 tsubst_function_parms (tree parms,
14739 tree args,
14740 tsubst_flags_t complain,
14741 tree in_decl)
14742 {
14743 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14744 }
14745
14746 /* Substitute into the ARG_TYPES of a function type.
14747 If END is a TREE_CHAIN, leave it and any following types
14748 un-substituted. */
14749
14750 static tree
14751 tsubst_arg_types (tree arg_types,
14752 tree args,
14753 tree end,
14754 tsubst_flags_t complain,
14755 tree in_decl)
14756 {
14757 tree remaining_arg_types;
14758 tree type = NULL_TREE;
14759 int i = 1;
14760 tree expanded_args = NULL_TREE;
14761 tree default_arg;
14762
14763 if (!arg_types || arg_types == void_list_node || arg_types == end)
14764 return arg_types;
14765
14766 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14767 args, end, complain, in_decl);
14768 if (remaining_arg_types == error_mark_node)
14769 return error_mark_node;
14770
14771 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14772 {
14773 /* For a pack expansion, perform substitution on the
14774 entire expression. Later on, we'll handle the arguments
14775 one-by-one. */
14776 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14777 args, complain, in_decl);
14778
14779 if (TREE_CODE (expanded_args) == TREE_VEC)
14780 /* So that we'll spin through the parameters, one by one. */
14781 i = TREE_VEC_LENGTH (expanded_args);
14782 else
14783 {
14784 /* We only partially substituted into the parameter
14785 pack. Our type is TYPE_PACK_EXPANSION. */
14786 type = expanded_args;
14787 expanded_args = NULL_TREE;
14788 }
14789 }
14790
14791 while (i > 0) {
14792 --i;
14793
14794 if (expanded_args)
14795 type = TREE_VEC_ELT (expanded_args, i);
14796 else if (!type)
14797 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14798
14799 if (type == error_mark_node)
14800 return error_mark_node;
14801 if (VOID_TYPE_P (type))
14802 {
14803 if (complain & tf_error)
14804 {
14805 error ("invalid parameter type %qT", type);
14806 if (in_decl)
14807 error ("in declaration %q+D", in_decl);
14808 }
14809 return error_mark_node;
14810 }
14811 /* DR 657. */
14812 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14813 return error_mark_node;
14814
14815 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14816 top-level qualifiers as required. */
14817 type = cv_unqualified (type_decays_to (type));
14818
14819 /* We do not substitute into default arguments here. The standard
14820 mandates that they be instantiated only when needed, which is
14821 done in build_over_call. */
14822 default_arg = TREE_PURPOSE (arg_types);
14823
14824 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14825 since the new op() won't have any associated template arguments for us
14826 to refer to later. */
14827 if (lambda_fn_in_template_p (in_decl))
14828 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14829 false/*fn*/, false/*constexpr*/);
14830
14831 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14832 {
14833 /* We've instantiated a template before its default arguments
14834 have been parsed. This can happen for a nested template
14835 class, and is not an error unless we require the default
14836 argument in a call of this function. */
14837 remaining_arg_types =
14838 tree_cons (default_arg, type, remaining_arg_types);
14839 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14840 remaining_arg_types);
14841 }
14842 else
14843 remaining_arg_types =
14844 hash_tree_cons (default_arg, type, remaining_arg_types);
14845 }
14846
14847 return remaining_arg_types;
14848 }
14849
14850 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14851 *not* handle the exception-specification for FNTYPE, because the
14852 initial substitution of explicitly provided template parameters
14853 during argument deduction forbids substitution into the
14854 exception-specification:
14855
14856 [temp.deduct]
14857
14858 All references in the function type of the function template to the
14859 corresponding template parameters are replaced by the specified tem-
14860 plate argument values. If a substitution in a template parameter or
14861 in the function type of the function template results in an invalid
14862 type, type deduction fails. [Note: The equivalent substitution in
14863 exception specifications is done only when the function is instanti-
14864 ated, at which point a program is ill-formed if the substitution
14865 results in an invalid type.] */
14866
14867 static tree
14868 tsubst_function_type (tree t,
14869 tree args,
14870 tsubst_flags_t complain,
14871 tree in_decl)
14872 {
14873 tree return_type;
14874 tree arg_types = NULL_TREE;
14875
14876 /* The TYPE_CONTEXT is not used for function/method types. */
14877 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14878
14879 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14880 failure. */
14881 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14882
14883 if (late_return_type_p)
14884 {
14885 /* Substitute the argument types. */
14886 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14887 complain, in_decl);
14888 if (arg_types == error_mark_node)
14889 return error_mark_node;
14890
14891 tree save_ccp = current_class_ptr;
14892 tree save_ccr = current_class_ref;
14893 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14894 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14895 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14896 if (do_inject)
14897 {
14898 /* DR 1207: 'this' is in scope in the trailing return type. */
14899 inject_this_parameter (this_type, cp_type_quals (this_type));
14900 }
14901
14902 /* Substitute the return type. */
14903 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14904
14905 if (do_inject)
14906 {
14907 current_class_ptr = save_ccp;
14908 current_class_ref = save_ccr;
14909 }
14910 }
14911 else
14912 /* Substitute the return type. */
14913 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14914
14915 if (return_type == error_mark_node)
14916 return error_mark_node;
14917 /* DR 486 clarifies that creation of a function type with an
14918 invalid return type is a deduction failure. */
14919 if (TREE_CODE (return_type) == ARRAY_TYPE
14920 || TREE_CODE (return_type) == FUNCTION_TYPE)
14921 {
14922 if (complain & tf_error)
14923 {
14924 if (TREE_CODE (return_type) == ARRAY_TYPE)
14925 error ("function returning an array");
14926 else
14927 error ("function returning a function");
14928 }
14929 return error_mark_node;
14930 }
14931 /* And DR 657. */
14932 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14933 return error_mark_node;
14934
14935 if (!late_return_type_p)
14936 {
14937 /* Substitute the argument types. */
14938 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14939 complain, in_decl);
14940 if (arg_types == error_mark_node)
14941 return error_mark_node;
14942 }
14943
14944 /* Construct a new type node and return it. */
14945 return rebuild_function_or_method_type (t, return_type, arg_types,
14946 /*raises=*/NULL_TREE, complain);
14947 }
14948
14949 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14950 ARGS into that specification, and return the substituted
14951 specification. If there is no specification, return NULL_TREE. */
14952
14953 static tree
14954 tsubst_exception_specification (tree fntype,
14955 tree args,
14956 tsubst_flags_t complain,
14957 tree in_decl,
14958 bool defer_ok)
14959 {
14960 tree specs;
14961 tree new_specs;
14962
14963 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14964 new_specs = NULL_TREE;
14965 if (specs && TREE_PURPOSE (specs))
14966 {
14967 /* A noexcept-specifier. */
14968 tree expr = TREE_PURPOSE (specs);
14969 if (TREE_CODE (expr) == INTEGER_CST)
14970 new_specs = expr;
14971 else if (defer_ok)
14972 {
14973 /* Defer instantiation of noexcept-specifiers to avoid
14974 excessive instantiations (c++/49107). */
14975 new_specs = make_node (DEFERRED_NOEXCEPT);
14976 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14977 {
14978 /* We already partially instantiated this member template,
14979 so combine the new args with the old. */
14980 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14981 = DEFERRED_NOEXCEPT_PATTERN (expr);
14982 DEFERRED_NOEXCEPT_ARGS (new_specs)
14983 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14984 }
14985 else
14986 {
14987 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14988 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14989 }
14990 }
14991 else
14992 {
14993 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14994 {
14995 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14996 args);
14997 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14998 }
14999 new_specs = tsubst_copy_and_build
15000 (expr, args, complain, in_decl, /*function_p=*/false,
15001 /*integral_constant_expression_p=*/true);
15002 }
15003 new_specs = build_noexcept_spec (new_specs, complain);
15004 }
15005 else if (specs)
15006 {
15007 if (! TREE_VALUE (specs))
15008 new_specs = specs;
15009 else
15010 while (specs)
15011 {
15012 tree spec;
15013 int i, len = 1;
15014 tree expanded_specs = NULL_TREE;
15015
15016 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15017 {
15018 /* Expand the pack expansion type. */
15019 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15020 args, complain,
15021 in_decl);
15022
15023 if (expanded_specs == error_mark_node)
15024 return error_mark_node;
15025 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15026 len = TREE_VEC_LENGTH (expanded_specs);
15027 else
15028 {
15029 /* We're substituting into a member template, so
15030 we got a TYPE_PACK_EXPANSION back. Add that
15031 expansion and move on. */
15032 gcc_assert (TREE_CODE (expanded_specs)
15033 == TYPE_PACK_EXPANSION);
15034 new_specs = add_exception_specifier (new_specs,
15035 expanded_specs,
15036 complain);
15037 specs = TREE_CHAIN (specs);
15038 continue;
15039 }
15040 }
15041
15042 for (i = 0; i < len; ++i)
15043 {
15044 if (expanded_specs)
15045 spec = TREE_VEC_ELT (expanded_specs, i);
15046 else
15047 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15048 if (spec == error_mark_node)
15049 return spec;
15050 new_specs = add_exception_specifier (new_specs, spec,
15051 complain);
15052 }
15053
15054 specs = TREE_CHAIN (specs);
15055 }
15056 }
15057 return new_specs;
15058 }
15059
15060 /* Substitute through a TREE_LIST of types or expressions, handling pack
15061 expansions. */
15062
15063 tree
15064 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15065 {
15066 if (t == void_list_node)
15067 return t;
15068
15069 tree purpose = TREE_PURPOSE (t);
15070 tree purposevec = NULL_TREE;
15071 if (!purpose)
15072 ;
15073 else if (PACK_EXPANSION_P (purpose))
15074 {
15075 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15076 if (TREE_CODE (purpose) == TREE_VEC)
15077 purposevec = purpose;
15078 }
15079 else if (TYPE_P (purpose))
15080 purpose = tsubst (purpose, args, complain, in_decl);
15081 else
15082 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15083 if (purpose == error_mark_node || purposevec == error_mark_node)
15084 return error_mark_node;
15085
15086 tree value = TREE_VALUE (t);
15087 tree valuevec = NULL_TREE;
15088 if (!value)
15089 ;
15090 else if (PACK_EXPANSION_P (value))
15091 {
15092 value = tsubst_pack_expansion (value, args, complain, in_decl);
15093 if (TREE_CODE (value) == TREE_VEC)
15094 valuevec = value;
15095 }
15096 else if (TYPE_P (value))
15097 value = tsubst (value, args, complain, in_decl);
15098 else
15099 value = tsubst_copy_and_build (value, args, complain, in_decl);
15100 if (value == error_mark_node || valuevec == error_mark_node)
15101 return error_mark_node;
15102
15103 tree chain = TREE_CHAIN (t);
15104 if (!chain)
15105 ;
15106 else if (TREE_CODE (chain) == TREE_LIST)
15107 chain = tsubst_tree_list (chain, args, complain, in_decl);
15108 else if (TYPE_P (chain))
15109 chain = tsubst (chain, args, complain, in_decl);
15110 else
15111 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15112 if (chain == error_mark_node)
15113 return error_mark_node;
15114
15115 if (purpose == TREE_PURPOSE (t)
15116 && value == TREE_VALUE (t)
15117 && chain == TREE_CHAIN (t))
15118 return t;
15119
15120 int len;
15121 /* Determine the number of arguments. */
15122 if (purposevec)
15123 {
15124 len = TREE_VEC_LENGTH (purposevec);
15125 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15126 }
15127 else if (valuevec)
15128 len = TREE_VEC_LENGTH (valuevec);
15129 else
15130 len = 1;
15131
15132 for (int i = len; i-- > 0; )
15133 {
15134 if (purposevec)
15135 purpose = TREE_VEC_ELT (purposevec, i);
15136 if (valuevec)
15137 value = TREE_VEC_ELT (valuevec, i);
15138
15139 if (value && TYPE_P (value))
15140 chain = hash_tree_cons (purpose, value, chain);
15141 else
15142 chain = tree_cons (purpose, value, chain);
15143 }
15144
15145 return chain;
15146 }
15147
15148 /* Take the tree structure T and replace template parameters used
15149 therein with the argument vector ARGS. IN_DECL is an associated
15150 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15151 Issue error and warning messages under control of COMPLAIN. Note
15152 that we must be relatively non-tolerant of extensions here, in
15153 order to preserve conformance; if we allow substitutions that
15154 should not be allowed, we may allow argument deductions that should
15155 not succeed, and therefore report ambiguous overload situations
15156 where there are none. In theory, we could allow the substitution,
15157 but indicate that it should have failed, and allow our caller to
15158 make sure that the right thing happens, but we don't try to do this
15159 yet.
15160
15161 This function is used for dealing with types, decls and the like;
15162 for expressions, use tsubst_expr or tsubst_copy. */
15163
15164 tree
15165 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15166 {
15167 enum tree_code code;
15168 tree type, r = NULL_TREE;
15169
15170 if (t == NULL_TREE || t == error_mark_node
15171 || t == integer_type_node
15172 || t == void_type_node
15173 || t == char_type_node
15174 || t == unknown_type_node
15175 || TREE_CODE (t) == NAMESPACE_DECL
15176 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15177 return t;
15178
15179 if (DECL_P (t))
15180 return tsubst_decl (t, args, complain);
15181
15182 if (args == NULL_TREE)
15183 return t;
15184
15185 code = TREE_CODE (t);
15186
15187 if (code == IDENTIFIER_NODE)
15188 type = IDENTIFIER_TYPE_VALUE (t);
15189 else
15190 type = TREE_TYPE (t);
15191
15192 gcc_assert (type != unknown_type_node);
15193
15194 /* Reuse typedefs. We need to do this to handle dependent attributes,
15195 such as attribute aligned. */
15196 if (TYPE_P (t)
15197 && typedef_variant_p (t))
15198 {
15199 tree decl = TYPE_NAME (t);
15200
15201 if (alias_template_specialization_p (t, nt_opaque))
15202 {
15203 /* DECL represents an alias template and we want to
15204 instantiate it. */
15205 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15206 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15207 r = instantiate_alias_template (tmpl, gen_args, complain);
15208 }
15209 else if (DECL_CLASS_SCOPE_P (decl)
15210 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15211 && uses_template_parms (DECL_CONTEXT (decl)))
15212 {
15213 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15214 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15215 r = retrieve_specialization (tmpl, gen_args, 0);
15216 }
15217 else if (DECL_FUNCTION_SCOPE_P (decl)
15218 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15219 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15220 r = retrieve_local_specialization (decl);
15221 else
15222 /* The typedef is from a non-template context. */
15223 return t;
15224
15225 if (r)
15226 {
15227 r = TREE_TYPE (r);
15228 r = cp_build_qualified_type_real
15229 (r, cp_type_quals (t) | cp_type_quals (r),
15230 complain | tf_ignore_bad_quals);
15231 return r;
15232 }
15233 else
15234 {
15235 /* We don't have an instantiation yet, so drop the typedef. */
15236 int quals = cp_type_quals (t);
15237 t = DECL_ORIGINAL_TYPE (decl);
15238 t = cp_build_qualified_type_real (t, quals,
15239 complain | tf_ignore_bad_quals);
15240 }
15241 }
15242
15243 bool fndecl_type = (complain & tf_fndecl_type);
15244 complain &= ~tf_fndecl_type;
15245
15246 if (type
15247 && code != TYPENAME_TYPE
15248 && code != TEMPLATE_TYPE_PARM
15249 && code != TEMPLATE_PARM_INDEX
15250 && code != IDENTIFIER_NODE
15251 && code != FUNCTION_TYPE
15252 && code != METHOD_TYPE)
15253 type = tsubst (type, args, complain, in_decl);
15254 if (type == error_mark_node)
15255 return error_mark_node;
15256
15257 switch (code)
15258 {
15259 case RECORD_TYPE:
15260 case UNION_TYPE:
15261 case ENUMERAL_TYPE:
15262 return tsubst_aggr_type (t, args, complain, in_decl,
15263 /*entering_scope=*/0);
15264
15265 case ERROR_MARK:
15266 case IDENTIFIER_NODE:
15267 case VOID_TYPE:
15268 case REAL_TYPE:
15269 case COMPLEX_TYPE:
15270 case VECTOR_TYPE:
15271 case BOOLEAN_TYPE:
15272 case NULLPTR_TYPE:
15273 case LANG_TYPE:
15274 return t;
15275
15276 case INTEGER_TYPE:
15277 if (t == integer_type_node)
15278 return t;
15279
15280 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15281 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15282 return t;
15283
15284 {
15285 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15286
15287 max = tsubst_expr (omax, args, complain, in_decl,
15288 /*integral_constant_expression_p=*/false);
15289
15290 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15291 needed. */
15292 if (TREE_CODE (max) == NOP_EXPR
15293 && TREE_SIDE_EFFECTS (omax)
15294 && !TREE_TYPE (max))
15295 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15296
15297 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15298 with TREE_SIDE_EFFECTS that indicates this is not an integral
15299 constant expression. */
15300 if (processing_template_decl
15301 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15302 {
15303 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15304 TREE_SIDE_EFFECTS (max) = 1;
15305 }
15306
15307 return compute_array_index_type (NULL_TREE, max, complain);
15308 }
15309
15310 case TEMPLATE_TYPE_PARM:
15311 case TEMPLATE_TEMPLATE_PARM:
15312 case BOUND_TEMPLATE_TEMPLATE_PARM:
15313 case TEMPLATE_PARM_INDEX:
15314 {
15315 int idx;
15316 int level;
15317 int levels;
15318 tree arg = NULL_TREE;
15319
15320 r = NULL_TREE;
15321
15322 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15323 template_parm_level_and_index (t, &level, &idx);
15324
15325 levels = TMPL_ARGS_DEPTH (args);
15326 if (level <= levels
15327 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15328 {
15329 arg = TMPL_ARG (args, level, idx);
15330
15331 /* See through ARGUMENT_PACK_SELECT arguments. */
15332 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15333 arg = argument_pack_select_arg (arg);
15334 }
15335
15336 if (arg == error_mark_node)
15337 return error_mark_node;
15338 else if (arg != NULL_TREE)
15339 {
15340 if (ARGUMENT_PACK_P (arg))
15341 /* If ARG is an argument pack, we don't actually want to
15342 perform a substitution here, because substitutions
15343 for argument packs are only done
15344 element-by-element. We can get to this point when
15345 substituting the type of a non-type template
15346 parameter pack, when that type actually contains
15347 template parameter packs from an outer template, e.g.,
15348
15349 template<typename... Types> struct A {
15350 template<Types... Values> struct B { };
15351 }; */
15352 return t;
15353
15354 if (code == TEMPLATE_TYPE_PARM)
15355 {
15356 int quals;
15357
15358 /* When building concept checks for the purpose of
15359 deducing placeholders, we can end up with wildcards
15360 where types are expected. Adjust this to the deduced
15361 value. */
15362 if (TREE_CODE (arg) == WILDCARD_DECL)
15363 arg = TREE_TYPE (TREE_TYPE (arg));
15364
15365 gcc_assert (TYPE_P (arg));
15366
15367 quals = cp_type_quals (arg) | cp_type_quals (t);
15368
15369 return cp_build_qualified_type_real
15370 (arg, quals, complain | tf_ignore_bad_quals);
15371 }
15372 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15373 {
15374 /* We are processing a type constructed from a
15375 template template parameter. */
15376 tree argvec = tsubst (TYPE_TI_ARGS (t),
15377 args, complain, in_decl);
15378 if (argvec == error_mark_node)
15379 return error_mark_node;
15380
15381 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15382 || TREE_CODE (arg) == TEMPLATE_DECL
15383 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15384
15385 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15386 /* Consider this code:
15387
15388 template <template <class> class Template>
15389 struct Internal {
15390 template <class Arg> using Bind = Template<Arg>;
15391 };
15392
15393 template <template <class> class Template, class Arg>
15394 using Instantiate = Template<Arg>; //#0
15395
15396 template <template <class> class Template,
15397 class Argument>
15398 using Bind =
15399 Instantiate<Internal<Template>::template Bind,
15400 Argument>; //#1
15401
15402 When #1 is parsed, the
15403 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15404 parameter `Template' in #0 matches the
15405 UNBOUND_CLASS_TEMPLATE representing the argument
15406 `Internal<Template>::template Bind'; We then want
15407 to assemble the type `Bind<Argument>' that can't
15408 be fully created right now, because
15409 `Internal<Template>' not being complete, the Bind
15410 template cannot be looked up in that context. So
15411 we need to "store" `Bind<Argument>' for later
15412 when the context of Bind becomes complete. Let's
15413 store that in a TYPENAME_TYPE. */
15414 return make_typename_type (TYPE_CONTEXT (arg),
15415 build_nt (TEMPLATE_ID_EXPR,
15416 TYPE_IDENTIFIER (arg),
15417 argvec),
15418 typename_type,
15419 complain);
15420
15421 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15422 are resolving nested-types in the signature of a
15423 member function templates. Otherwise ARG is a
15424 TEMPLATE_DECL and is the real template to be
15425 instantiated. */
15426 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15427 arg = TYPE_NAME (arg);
15428
15429 r = lookup_template_class (arg,
15430 argvec, in_decl,
15431 DECL_CONTEXT (arg),
15432 /*entering_scope=*/0,
15433 complain);
15434 return cp_build_qualified_type_real
15435 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15436 }
15437 else if (code == TEMPLATE_TEMPLATE_PARM)
15438 return arg;
15439 else
15440 /* TEMPLATE_PARM_INDEX. */
15441 return convert_from_reference (unshare_expr (arg));
15442 }
15443
15444 if (level == 1)
15445 /* This can happen during the attempted tsubst'ing in
15446 unify. This means that we don't yet have any information
15447 about the template parameter in question. */
15448 return t;
15449
15450 /* Early in template argument deduction substitution, we don't
15451 want to reduce the level of 'auto', or it will be confused
15452 with a normal template parm in subsequent deduction.
15453 Similarly, don't reduce the level of template parameters to
15454 avoid mismatches when deducing their types. */
15455 if (complain & tf_partial)
15456 return t;
15457
15458 /* If we get here, we must have been looking at a parm for a
15459 more deeply nested template. Make a new version of this
15460 template parameter, but with a lower level. */
15461 switch (code)
15462 {
15463 case TEMPLATE_TYPE_PARM:
15464 case TEMPLATE_TEMPLATE_PARM:
15465 case BOUND_TEMPLATE_TEMPLATE_PARM:
15466 if (cp_type_quals (t))
15467 {
15468 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15469 r = cp_build_qualified_type_real
15470 (r, cp_type_quals (t),
15471 complain | (code == TEMPLATE_TYPE_PARM
15472 ? tf_ignore_bad_quals : 0));
15473 }
15474 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15475 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15476 && (r = (TEMPLATE_PARM_DESCENDANTS
15477 (TEMPLATE_TYPE_PARM_INDEX (t))))
15478 && (r = TREE_TYPE (r))
15479 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15480 /* Break infinite recursion when substituting the constraints
15481 of a constrained placeholder. */;
15482 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15483 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15484 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15485 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15486 r = TEMPLATE_PARM_DESCENDANTS (arg))
15487 && (TEMPLATE_PARM_LEVEL (r)
15488 == TEMPLATE_PARM_LEVEL (arg) - levels))
15489 /* Cache the simple case of lowering a type parameter. */
15490 r = TREE_TYPE (r);
15491 else
15492 {
15493 r = copy_type (t);
15494 TEMPLATE_TYPE_PARM_INDEX (r)
15495 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15496 r, levels, args, complain);
15497 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15498 TYPE_MAIN_VARIANT (r) = r;
15499 TYPE_POINTER_TO (r) = NULL_TREE;
15500 TYPE_REFERENCE_TO (r) = NULL_TREE;
15501
15502 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15503 {
15504 /* Propagate constraints on placeholders since they are
15505 only instantiated during satisfaction. */
15506 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15507 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15508 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15509 {
15510 pl = tsubst_copy (pl, args, complain, in_decl);
15511 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15512 }
15513 }
15514
15515 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15516 /* We have reduced the level of the template
15517 template parameter, but not the levels of its
15518 template parameters, so canonical_type_parameter
15519 will not be able to find the canonical template
15520 template parameter for this level. Thus, we
15521 require structural equality checking to compare
15522 TEMPLATE_TEMPLATE_PARMs. */
15523 SET_TYPE_STRUCTURAL_EQUALITY (r);
15524 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15525 SET_TYPE_STRUCTURAL_EQUALITY (r);
15526 else
15527 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15528
15529 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15530 {
15531 tree tinfo = TYPE_TEMPLATE_INFO (t);
15532 /* We might need to substitute into the types of non-type
15533 template parameters. */
15534 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15535 complain, in_decl);
15536 if (tmpl == error_mark_node)
15537 return error_mark_node;
15538 tree argvec = tsubst (TI_ARGS (tinfo), args,
15539 complain, in_decl);
15540 if (argvec == error_mark_node)
15541 return error_mark_node;
15542
15543 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15544 = build_template_info (tmpl, argvec);
15545 }
15546 }
15547 break;
15548
15549 case TEMPLATE_PARM_INDEX:
15550 /* OK, now substitute the type of the non-type parameter. We
15551 couldn't do it earlier because it might be an auto parameter,
15552 and we wouldn't need to if we had an argument. */
15553 type = tsubst (type, args, complain, in_decl);
15554 if (type == error_mark_node)
15555 return error_mark_node;
15556 r = reduce_template_parm_level (t, type, levels, args, complain);
15557 break;
15558
15559 default:
15560 gcc_unreachable ();
15561 }
15562
15563 return r;
15564 }
15565
15566 case TREE_LIST:
15567 return tsubst_tree_list (t, args, complain, in_decl);
15568
15569 case TREE_BINFO:
15570 /* We should never be tsubsting a binfo. */
15571 gcc_unreachable ();
15572
15573 case TREE_VEC:
15574 /* A vector of template arguments. */
15575 gcc_assert (!type);
15576 return tsubst_template_args (t, args, complain, in_decl);
15577
15578 case POINTER_TYPE:
15579 case REFERENCE_TYPE:
15580 {
15581 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15582 return t;
15583
15584 /* [temp.deduct]
15585
15586 Type deduction may fail for any of the following
15587 reasons:
15588
15589 -- Attempting to create a pointer to reference type.
15590 -- Attempting to create a reference to a reference type or
15591 a reference to void.
15592
15593 Core issue 106 says that creating a reference to a reference
15594 during instantiation is no longer a cause for failure. We
15595 only enforce this check in strict C++98 mode. */
15596 if ((TYPE_REF_P (type)
15597 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15598 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15599 {
15600 static location_t last_loc;
15601
15602 /* We keep track of the last time we issued this error
15603 message to avoid spewing a ton of messages during a
15604 single bad template instantiation. */
15605 if (complain & tf_error
15606 && last_loc != input_location)
15607 {
15608 if (VOID_TYPE_P (type))
15609 error ("forming reference to void");
15610 else if (code == POINTER_TYPE)
15611 error ("forming pointer to reference type %qT", type);
15612 else
15613 error ("forming reference to reference type %qT", type);
15614 last_loc = input_location;
15615 }
15616
15617 return error_mark_node;
15618 }
15619 else if (TREE_CODE (type) == FUNCTION_TYPE
15620 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15621 || type_memfn_rqual (type) != REF_QUAL_NONE))
15622 {
15623 if (complain & tf_error)
15624 {
15625 if (code == POINTER_TYPE)
15626 error ("forming pointer to qualified function type %qT",
15627 type);
15628 else
15629 error ("forming reference to qualified function type %qT",
15630 type);
15631 }
15632 return error_mark_node;
15633 }
15634 else if (code == POINTER_TYPE)
15635 {
15636 r = build_pointer_type (type);
15637 if (TREE_CODE (type) == METHOD_TYPE)
15638 r = build_ptrmemfunc_type (r);
15639 }
15640 else if (TYPE_REF_P (type))
15641 /* In C++0x, during template argument substitution, when there is an
15642 attempt to create a reference to a reference type, reference
15643 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15644
15645 "If a template-argument for a template-parameter T names a type
15646 that is a reference to a type A, an attempt to create the type
15647 'lvalue reference to cv T' creates the type 'lvalue reference to
15648 A,' while an attempt to create the type type rvalue reference to
15649 cv T' creates the type T"
15650 */
15651 r = cp_build_reference_type
15652 (TREE_TYPE (type),
15653 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15654 else
15655 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15656 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15657
15658 if (r != error_mark_node)
15659 /* Will this ever be needed for TYPE_..._TO values? */
15660 layout_type (r);
15661
15662 return r;
15663 }
15664 case OFFSET_TYPE:
15665 {
15666 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15667 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15668 {
15669 /* [temp.deduct]
15670
15671 Type deduction may fail for any of the following
15672 reasons:
15673
15674 -- Attempting to create "pointer to member of T" when T
15675 is not a class type. */
15676 if (complain & tf_error)
15677 error ("creating pointer to member of non-class type %qT", r);
15678 return error_mark_node;
15679 }
15680 if (TYPE_REF_P (type))
15681 {
15682 if (complain & tf_error)
15683 error ("creating pointer to member reference type %qT", type);
15684 return error_mark_node;
15685 }
15686 if (VOID_TYPE_P (type))
15687 {
15688 if (complain & tf_error)
15689 error ("creating pointer to member of type void");
15690 return error_mark_node;
15691 }
15692 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15693 if (TREE_CODE (type) == FUNCTION_TYPE)
15694 {
15695 /* The type of the implicit object parameter gets its
15696 cv-qualifiers from the FUNCTION_TYPE. */
15697 tree memptr;
15698 tree method_type
15699 = build_memfn_type (type, r, type_memfn_quals (type),
15700 type_memfn_rqual (type));
15701 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15702 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15703 complain);
15704 }
15705 else
15706 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15707 cp_type_quals (t),
15708 complain);
15709 }
15710 case FUNCTION_TYPE:
15711 case METHOD_TYPE:
15712 {
15713 tree fntype;
15714 tree specs;
15715 fntype = tsubst_function_type (t, args, complain, in_decl);
15716 if (fntype == error_mark_node)
15717 return error_mark_node;
15718
15719 /* Substitute the exception specification. */
15720 specs = tsubst_exception_specification (t, args, complain, in_decl,
15721 /*defer_ok*/fndecl_type);
15722 if (specs == error_mark_node)
15723 return error_mark_node;
15724 if (specs)
15725 fntype = build_exception_variant (fntype, specs);
15726 return fntype;
15727 }
15728 case ARRAY_TYPE:
15729 {
15730 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15731 if (domain == error_mark_node)
15732 return error_mark_node;
15733
15734 /* As an optimization, we avoid regenerating the array type if
15735 it will obviously be the same as T. */
15736 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15737 return t;
15738
15739 /* These checks should match the ones in create_array_type_for_decl.
15740
15741 [temp.deduct]
15742
15743 The deduction may fail for any of the following reasons:
15744
15745 -- Attempting to create an array with an element type that
15746 is void, a function type, or a reference type, or [DR337]
15747 an abstract class type. */
15748 if (VOID_TYPE_P (type)
15749 || TREE_CODE (type) == FUNCTION_TYPE
15750 || (TREE_CODE (type) == ARRAY_TYPE
15751 && TYPE_DOMAIN (type) == NULL_TREE)
15752 || TYPE_REF_P (type))
15753 {
15754 if (complain & tf_error)
15755 error ("creating array of %qT", type);
15756 return error_mark_node;
15757 }
15758
15759 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15760 return error_mark_node;
15761
15762 r = build_cplus_array_type (type, domain);
15763
15764 if (!valid_array_size_p (input_location, r, in_decl,
15765 (complain & tf_error)))
15766 return error_mark_node;
15767
15768 if (TYPE_USER_ALIGN (t))
15769 {
15770 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15771 TYPE_USER_ALIGN (r) = 1;
15772 }
15773
15774 return r;
15775 }
15776
15777 case TYPENAME_TYPE:
15778 {
15779 tree ctx = TYPE_CONTEXT (t);
15780 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15781 {
15782 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15783 if (ctx == error_mark_node
15784 || TREE_VEC_LENGTH (ctx) > 1)
15785 return error_mark_node;
15786 if (TREE_VEC_LENGTH (ctx) == 0)
15787 {
15788 if (complain & tf_error)
15789 error ("%qD is instantiated for an empty pack",
15790 TYPENAME_TYPE_FULLNAME (t));
15791 return error_mark_node;
15792 }
15793 ctx = TREE_VEC_ELT (ctx, 0);
15794 }
15795 else
15796 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15797 /*entering_scope=*/1);
15798 if (ctx == error_mark_node)
15799 return error_mark_node;
15800
15801 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15802 complain, in_decl);
15803 if (f == error_mark_node)
15804 return error_mark_node;
15805
15806 if (!MAYBE_CLASS_TYPE_P (ctx))
15807 {
15808 if (complain & tf_error)
15809 error ("%qT is not a class, struct, or union type", ctx);
15810 return error_mark_node;
15811 }
15812 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15813 {
15814 /* Normally, make_typename_type does not require that the CTX
15815 have complete type in order to allow things like:
15816
15817 template <class T> struct S { typename S<T>::X Y; };
15818
15819 But, such constructs have already been resolved by this
15820 point, so here CTX really should have complete type, unless
15821 it's a partial instantiation. */
15822 ctx = complete_type (ctx);
15823 if (!COMPLETE_TYPE_P (ctx))
15824 {
15825 if (complain & tf_error)
15826 cxx_incomplete_type_error (NULL_TREE, ctx);
15827 return error_mark_node;
15828 }
15829 }
15830
15831 f = make_typename_type (ctx, f, typename_type,
15832 complain | tf_keep_type_decl);
15833 if (f == error_mark_node)
15834 return f;
15835 if (TREE_CODE (f) == TYPE_DECL)
15836 {
15837 complain |= tf_ignore_bad_quals;
15838 f = TREE_TYPE (f);
15839 }
15840
15841 if (TREE_CODE (f) != TYPENAME_TYPE)
15842 {
15843 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15844 {
15845 if (complain & tf_error)
15846 error ("%qT resolves to %qT, which is not an enumeration type",
15847 t, f);
15848 else
15849 return error_mark_node;
15850 }
15851 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15852 {
15853 if (complain & tf_error)
15854 error ("%qT resolves to %qT, which is not a class type",
15855 t, f);
15856 else
15857 return error_mark_node;
15858 }
15859 }
15860
15861 return cp_build_qualified_type_real
15862 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15863 }
15864
15865 case UNBOUND_CLASS_TEMPLATE:
15866 {
15867 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15868 in_decl, /*entering_scope=*/1);
15869 tree name = TYPE_IDENTIFIER (t);
15870 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15871
15872 if (ctx == error_mark_node || name == error_mark_node)
15873 return error_mark_node;
15874
15875 if (parm_list)
15876 parm_list = tsubst_template_parms (parm_list, args, complain);
15877 return make_unbound_class_template (ctx, name, parm_list, complain);
15878 }
15879
15880 case TYPEOF_TYPE:
15881 {
15882 tree type;
15883
15884 ++cp_unevaluated_operand;
15885 ++c_inhibit_evaluation_warnings;
15886
15887 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15888 complain, in_decl,
15889 /*integral_constant_expression_p=*/false);
15890
15891 --cp_unevaluated_operand;
15892 --c_inhibit_evaluation_warnings;
15893
15894 type = finish_typeof (type);
15895 return cp_build_qualified_type_real (type,
15896 cp_type_quals (t)
15897 | cp_type_quals (type),
15898 complain);
15899 }
15900
15901 case DECLTYPE_TYPE:
15902 {
15903 tree type;
15904
15905 ++cp_unevaluated_operand;
15906 ++c_inhibit_evaluation_warnings;
15907
15908 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15909 complain|tf_decltype, in_decl,
15910 /*function_p*/false,
15911 /*integral_constant_expression*/false);
15912
15913 --cp_unevaluated_operand;
15914 --c_inhibit_evaluation_warnings;
15915
15916 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15917 type = lambda_capture_field_type (type,
15918 false /*explicit_init*/,
15919 DECLTYPE_FOR_REF_CAPTURE (t));
15920 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15921 type = lambda_proxy_type (type);
15922 else
15923 {
15924 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15925 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15926 && EXPR_P (type))
15927 /* In a template ~id could be either a complement expression
15928 or an unqualified-id naming a destructor; if instantiating
15929 it produces an expression, it's not an id-expression or
15930 member access. */
15931 id = false;
15932 type = finish_decltype_type (type, id, complain);
15933 }
15934 return cp_build_qualified_type_real (type,
15935 cp_type_quals (t)
15936 | cp_type_quals (type),
15937 complain | tf_ignore_bad_quals);
15938 }
15939
15940 case UNDERLYING_TYPE:
15941 {
15942 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15943 complain, in_decl);
15944 return finish_underlying_type (type);
15945 }
15946
15947 case TYPE_ARGUMENT_PACK:
15948 case NONTYPE_ARGUMENT_PACK:
15949 {
15950 tree r;
15951
15952 if (code == NONTYPE_ARGUMENT_PACK)
15953 r = make_node (code);
15954 else
15955 r = cxx_make_type (code);
15956
15957 tree pack_args = ARGUMENT_PACK_ARGS (t);
15958 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15959 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15960
15961 return r;
15962 }
15963
15964 case VOID_CST:
15965 case INTEGER_CST:
15966 case REAL_CST:
15967 case STRING_CST:
15968 case PLUS_EXPR:
15969 case MINUS_EXPR:
15970 case NEGATE_EXPR:
15971 case NOP_EXPR:
15972 case INDIRECT_REF:
15973 case ADDR_EXPR:
15974 case CALL_EXPR:
15975 case ARRAY_REF:
15976 case SCOPE_REF:
15977 /* We should use one of the expression tsubsts for these codes. */
15978 gcc_unreachable ();
15979
15980 default:
15981 sorry ("use of %qs in template", get_tree_code_name (code));
15982 return error_mark_node;
15983 }
15984 }
15985
15986 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15987 expression on the left-hand side of the "." or "->" operator. We
15988 only do the lookup if we had a dependent BASELINK. Otherwise we
15989 adjust it onto the instantiated heirarchy. */
15990
15991 static tree
15992 tsubst_baselink (tree baselink, tree object_type,
15993 tree args, tsubst_flags_t complain, tree in_decl)
15994 {
15995 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15996 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15997 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15998
15999 tree optype = BASELINK_OPTYPE (baselink);
16000 optype = tsubst (optype, args, complain, in_decl);
16001
16002 tree template_args = NULL_TREE;
16003 bool template_id_p = false;
16004 tree fns = BASELINK_FUNCTIONS (baselink);
16005 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16006 {
16007 template_id_p = true;
16008 template_args = TREE_OPERAND (fns, 1);
16009 fns = TREE_OPERAND (fns, 0);
16010 if (template_args)
16011 template_args = tsubst_template_args (template_args, args,
16012 complain, in_decl);
16013 }
16014
16015 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16016 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16017 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16018
16019 if (dependent_p)
16020 {
16021 tree name = OVL_NAME (fns);
16022 if (IDENTIFIER_CONV_OP_P (name))
16023 name = make_conv_op_name (optype);
16024
16025 if (name == complete_dtor_identifier)
16026 /* Treat as-if non-dependent below. */
16027 dependent_p = false;
16028
16029 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16030 complain);
16031 if (!baselink)
16032 {
16033 if ((complain & tf_error)
16034 && constructor_name_p (name, qualifying_scope))
16035 error ("cannot call constructor %<%T::%D%> directly",
16036 qualifying_scope, name);
16037 return error_mark_node;
16038 }
16039
16040 if (BASELINK_P (baselink))
16041 fns = BASELINK_FUNCTIONS (baselink);
16042 }
16043 else
16044 /* We're going to overwrite pieces below, make a duplicate. */
16045 baselink = copy_node (baselink);
16046
16047 /* If lookup found a single function, mark it as used at this point.
16048 (If lookup found multiple functions the one selected later by
16049 overload resolution will be marked as used at that point.) */
16050 if (!template_id_p && !really_overloaded_fn (fns))
16051 {
16052 tree fn = OVL_FIRST (fns);
16053 bool ok = mark_used (fn, complain);
16054 if (!ok && !(complain & tf_error))
16055 return error_mark_node;
16056 if (ok && BASELINK_P (baselink))
16057 /* We might have instantiated an auto function. */
16058 TREE_TYPE (baselink) = TREE_TYPE (fn);
16059 }
16060
16061 if (BASELINK_P (baselink))
16062 {
16063 /* Add back the template arguments, if present. */
16064 if (template_id_p)
16065 BASELINK_FUNCTIONS (baselink)
16066 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16067
16068 /* Update the conversion operator type. */
16069 BASELINK_OPTYPE (baselink) = optype;
16070 }
16071
16072 if (!object_type)
16073 object_type = current_class_type;
16074
16075 if (qualified_p || !dependent_p)
16076 {
16077 baselink = adjust_result_of_qualified_name_lookup (baselink,
16078 qualifying_scope,
16079 object_type);
16080 if (!qualified_p)
16081 /* We need to call adjust_result_of_qualified_name_lookup in case the
16082 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16083 so that we still get virtual function binding. */
16084 BASELINK_QUALIFIED_P (baselink) = false;
16085 }
16086
16087 return baselink;
16088 }
16089
16090 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16091 true if the qualified-id will be a postfix-expression in-and-of
16092 itself; false if more of the postfix-expression follows the
16093 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16094 of "&". */
16095
16096 static tree
16097 tsubst_qualified_id (tree qualified_id, tree args,
16098 tsubst_flags_t complain, tree in_decl,
16099 bool done, bool address_p)
16100 {
16101 tree expr;
16102 tree scope;
16103 tree name;
16104 bool is_template;
16105 tree template_args;
16106 location_t loc = UNKNOWN_LOCATION;
16107
16108 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16109
16110 /* Figure out what name to look up. */
16111 name = TREE_OPERAND (qualified_id, 1);
16112 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16113 {
16114 is_template = true;
16115 loc = EXPR_LOCATION (name);
16116 template_args = TREE_OPERAND (name, 1);
16117 if (template_args)
16118 template_args = tsubst_template_args (template_args, args,
16119 complain, in_decl);
16120 if (template_args == error_mark_node)
16121 return error_mark_node;
16122 name = TREE_OPERAND (name, 0);
16123 }
16124 else
16125 {
16126 is_template = false;
16127 template_args = NULL_TREE;
16128 }
16129
16130 /* Substitute into the qualifying scope. When there are no ARGS, we
16131 are just trying to simplify a non-dependent expression. In that
16132 case the qualifying scope may be dependent, and, in any case,
16133 substituting will not help. */
16134 scope = TREE_OPERAND (qualified_id, 0);
16135 if (args)
16136 {
16137 scope = tsubst (scope, args, complain, in_decl);
16138 expr = tsubst_copy (name, args, complain, in_decl);
16139 }
16140 else
16141 expr = name;
16142
16143 if (dependent_scope_p (scope))
16144 {
16145 if (is_template)
16146 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16147 tree r = build_qualified_name (NULL_TREE, scope, expr,
16148 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16149 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16150 return r;
16151 }
16152
16153 if (!BASELINK_P (name) && !DECL_P (expr))
16154 {
16155 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16156 {
16157 /* A BIT_NOT_EXPR is used to represent a destructor. */
16158 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16159 {
16160 error ("qualifying type %qT does not match destructor name ~%qT",
16161 scope, TREE_OPERAND (expr, 0));
16162 expr = error_mark_node;
16163 }
16164 else
16165 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16166 /*is_type_p=*/0, false);
16167 }
16168 else
16169 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
16170 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16171 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16172 {
16173 if (complain & tf_error)
16174 {
16175 error ("dependent-name %qE is parsed as a non-type, but "
16176 "instantiation yields a type", qualified_id);
16177 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16178 }
16179 return error_mark_node;
16180 }
16181 }
16182
16183 if (DECL_P (expr))
16184 {
16185 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16186 scope, complain))
16187 return error_mark_node;
16188 /* Remember that there was a reference to this entity. */
16189 if (!mark_used (expr, complain) && !(complain & tf_error))
16190 return error_mark_node;
16191 }
16192
16193 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16194 {
16195 if (complain & tf_error)
16196 qualified_name_lookup_error (scope,
16197 TREE_OPERAND (qualified_id, 1),
16198 expr, input_location);
16199 return error_mark_node;
16200 }
16201
16202 if (is_template)
16203 {
16204 /* We may be repeating a check already done during parsing, but
16205 if it was well-formed and passed then, it will pass again
16206 now, and if it didn't, we wouldn't have got here. The case
16207 we want to catch is when we couldn't tell then, and can now,
16208 namely when templ prior to substitution was an
16209 identifier. */
16210 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16211 return error_mark_node;
16212
16213 if (variable_template_p (expr))
16214 expr = lookup_and_finish_template_variable (expr, template_args,
16215 complain);
16216 else
16217 expr = lookup_template_function (expr, template_args);
16218 }
16219
16220 if (expr == error_mark_node && complain & tf_error)
16221 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16222 expr, input_location);
16223 else if (TYPE_P (scope))
16224 {
16225 expr = (adjust_result_of_qualified_name_lookup
16226 (expr, scope, current_nonlambda_class_type ()));
16227 expr = (finish_qualified_id_expr
16228 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16229 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16230 /*template_arg_p=*/false, complain));
16231 }
16232
16233 /* Expressions do not generally have reference type. */
16234 if (TREE_CODE (expr) != SCOPE_REF
16235 /* However, if we're about to form a pointer-to-member, we just
16236 want the referenced member referenced. */
16237 && TREE_CODE (expr) != OFFSET_REF)
16238 expr = convert_from_reference (expr);
16239
16240 if (REF_PARENTHESIZED_P (qualified_id))
16241 expr = force_paren_expr (expr);
16242
16243 return expr;
16244 }
16245
16246 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16247 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16248 for tsubst. */
16249
16250 static tree
16251 tsubst_init (tree init, tree decl, tree args,
16252 tsubst_flags_t complain, tree in_decl)
16253 {
16254 if (!init)
16255 return NULL_TREE;
16256
16257 init = tsubst_expr (init, args, complain, in_decl, false);
16258
16259 tree type = TREE_TYPE (decl);
16260
16261 if (!init && type != error_mark_node)
16262 {
16263 if (tree auto_node = type_uses_auto (type))
16264 {
16265 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16266 {
16267 if (complain & tf_error)
16268 error ("initializer for %q#D expands to an empty list "
16269 "of expressions", decl);
16270 return error_mark_node;
16271 }
16272 }
16273 else if (!dependent_type_p (type))
16274 {
16275 /* If we had an initializer but it
16276 instantiated to nothing,
16277 value-initialize the object. This will
16278 only occur when the initializer was a
16279 pack expansion where the parameter packs
16280 used in that expansion were of length
16281 zero. */
16282 init = build_value_init (type, complain);
16283 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16284 init = get_target_expr_sfinae (init, complain);
16285 if (TREE_CODE (init) == TARGET_EXPR)
16286 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16287 }
16288 }
16289
16290 return init;
16291 }
16292
16293 /* Like tsubst, but deals with expressions. This function just replaces
16294 template parms; to finish processing the resultant expression, use
16295 tsubst_copy_and_build or tsubst_expr. */
16296
16297 static tree
16298 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16299 {
16300 enum tree_code code;
16301 tree r;
16302
16303 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16304 return t;
16305
16306 code = TREE_CODE (t);
16307
16308 switch (code)
16309 {
16310 case PARM_DECL:
16311 r = retrieve_local_specialization (t);
16312
16313 if (r == NULL_TREE)
16314 {
16315 /* We get here for a use of 'this' in an NSDMI. */
16316 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16317 return current_class_ptr;
16318
16319 /* This can happen for a parameter name used later in a function
16320 declaration (such as in a late-specified return type). Just
16321 make a dummy decl, since it's only used for its type. */
16322 gcc_assert (cp_unevaluated_operand != 0);
16323 r = tsubst_decl (t, args, complain);
16324 /* Give it the template pattern as its context; its true context
16325 hasn't been instantiated yet and this is good enough for
16326 mangling. */
16327 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16328 }
16329
16330 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16331 r = argument_pack_select_arg (r);
16332 if (!mark_used (r, complain) && !(complain & tf_error))
16333 return error_mark_node;
16334 return r;
16335
16336 case CONST_DECL:
16337 {
16338 tree enum_type;
16339 tree v;
16340
16341 if (DECL_TEMPLATE_PARM_P (t))
16342 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16343 /* There is no need to substitute into namespace-scope
16344 enumerators. */
16345 if (DECL_NAMESPACE_SCOPE_P (t))
16346 return t;
16347 /* If ARGS is NULL, then T is known to be non-dependent. */
16348 if (args == NULL_TREE)
16349 return scalar_constant_value (t);
16350
16351 /* Unfortunately, we cannot just call lookup_name here.
16352 Consider:
16353
16354 template <int I> int f() {
16355 enum E { a = I };
16356 struct S { void g() { E e = a; } };
16357 };
16358
16359 When we instantiate f<7>::S::g(), say, lookup_name is not
16360 clever enough to find f<7>::a. */
16361 enum_type
16362 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16363 /*entering_scope=*/0);
16364
16365 for (v = TYPE_VALUES (enum_type);
16366 v != NULL_TREE;
16367 v = TREE_CHAIN (v))
16368 if (TREE_PURPOSE (v) == DECL_NAME (t))
16369 return TREE_VALUE (v);
16370
16371 /* We didn't find the name. That should never happen; if
16372 name-lookup found it during preliminary parsing, we
16373 should find it again here during instantiation. */
16374 gcc_unreachable ();
16375 }
16376 return t;
16377
16378 case FIELD_DECL:
16379 if (DECL_CONTEXT (t))
16380 {
16381 tree ctx;
16382
16383 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16384 /*entering_scope=*/1);
16385 if (ctx != DECL_CONTEXT (t))
16386 {
16387 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16388 if (!r)
16389 {
16390 if (complain & tf_error)
16391 error ("using invalid field %qD", t);
16392 return error_mark_node;
16393 }
16394 return r;
16395 }
16396 }
16397
16398 return t;
16399
16400 case VAR_DECL:
16401 case FUNCTION_DECL:
16402 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16403 r = tsubst (t, args, complain, in_decl);
16404 else if (local_variable_p (t)
16405 && uses_template_parms (DECL_CONTEXT (t)))
16406 {
16407 r = retrieve_local_specialization (t);
16408 if (r == NULL_TREE)
16409 {
16410 /* First try name lookup to find the instantiation. */
16411 r = lookup_name (DECL_NAME (t));
16412 if (r)
16413 {
16414 if (!VAR_P (r))
16415 {
16416 /* During error-recovery we may find a non-variable,
16417 even an OVERLOAD: just bail out and avoid ICEs and
16418 duplicate diagnostics (c++/62207). */
16419 gcc_assert (seen_error ());
16420 return error_mark_node;
16421 }
16422 if (!is_capture_proxy (r))
16423 {
16424 /* Make sure the one we found is the one we want. */
16425 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16426 if (ctx != DECL_CONTEXT (r))
16427 r = NULL_TREE;
16428 }
16429 }
16430
16431 if (r)
16432 /* OK */;
16433 else
16434 {
16435 /* This can happen for a variable used in a
16436 late-specified return type of a local lambda, or for a
16437 local static or constant. Building a new VAR_DECL
16438 should be OK in all those cases. */
16439 r = tsubst_decl (t, args, complain);
16440 if (local_specializations)
16441 /* Avoid infinite recursion (79640). */
16442 register_local_specialization (r, t);
16443 if (decl_maybe_constant_var_p (r))
16444 {
16445 /* We can't call cp_finish_decl, so handle the
16446 initializer by hand. */
16447 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16448 complain, in_decl);
16449 if (!processing_template_decl)
16450 init = maybe_constant_init (init);
16451 if (processing_template_decl
16452 ? potential_constant_expression (init)
16453 : reduced_constant_expression_p (init))
16454 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16455 = TREE_CONSTANT (r) = true;
16456 DECL_INITIAL (r) = init;
16457 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16458 TREE_TYPE (r)
16459 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16460 complain, adc_variable_type);
16461 }
16462 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16463 || decl_constant_var_p (r)
16464 || seen_error ());
16465 if (!processing_template_decl
16466 && !TREE_STATIC (r))
16467 r = process_outer_var_ref (r, complain);
16468 }
16469 /* Remember this for subsequent uses. */
16470 if (local_specializations)
16471 register_local_specialization (r, t);
16472 }
16473 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16474 r = argument_pack_select_arg (r);
16475 }
16476 else
16477 r = t;
16478 if (!mark_used (r, complain))
16479 return error_mark_node;
16480 return r;
16481
16482 case NAMESPACE_DECL:
16483 return t;
16484
16485 case OVERLOAD:
16486 return t;
16487
16488 case BASELINK:
16489 return tsubst_baselink (t, current_nonlambda_class_type (),
16490 args, complain, in_decl);
16491
16492 case TEMPLATE_DECL:
16493 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16494 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16495 args, complain, in_decl);
16496 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16497 return tsubst (t, args, complain, in_decl);
16498 else if (DECL_CLASS_SCOPE_P (t)
16499 && uses_template_parms (DECL_CONTEXT (t)))
16500 {
16501 /* Template template argument like the following example need
16502 special treatment:
16503
16504 template <template <class> class TT> struct C {};
16505 template <class T> struct D {
16506 template <class U> struct E {};
16507 C<E> c; // #1
16508 };
16509 D<int> d; // #2
16510
16511 We are processing the template argument `E' in #1 for
16512 the template instantiation #2. Originally, `E' is a
16513 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16514 have to substitute this with one having context `D<int>'. */
16515
16516 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16517 if (dependent_scope_p (context))
16518 {
16519 /* When rewriting a constructor into a deduction guide, a
16520 non-dependent name can become dependent, so memtmpl<args>
16521 becomes context::template memtmpl<args>. */
16522 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16523 return build_qualified_name (type, context, DECL_NAME (t),
16524 /*template*/true);
16525 }
16526 return lookup_field (context, DECL_NAME(t), 0, false);
16527 }
16528 else
16529 /* Ordinary template template argument. */
16530 return t;
16531
16532 case NON_LVALUE_EXPR:
16533 case VIEW_CONVERT_EXPR:
16534 {
16535 /* Handle location wrappers by substituting the wrapped node
16536 first, *then* reusing the resulting type. Doing the type
16537 first ensures that we handle template parameters and
16538 parameter pack expansions. */
16539 if (location_wrapper_p (t))
16540 {
16541 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16542 complain, in_decl);
16543 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16544 }
16545 tree op = TREE_OPERAND (t, 0);
16546 if (code == VIEW_CONVERT_EXPR
16547 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16548 {
16549 /* Wrapper to make a C++20 template parameter object const. */
16550 op = tsubst_copy (op, args, complain, in_decl);
16551 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16552 {
16553 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16554 return build1 (code, type, op);
16555 }
16556 else
16557 {
16558 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16559 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16560 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16561 return op;
16562 }
16563 }
16564 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16565 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16566 {
16567 op = tsubst_copy (op, args, complain, in_decl);
16568 op = build1 (code, TREE_TYPE (op), op);
16569 REF_PARENTHESIZED_P (op) = true;
16570 return op;
16571 }
16572 /* We shouldn't see any other uses of these in templates. */
16573 gcc_unreachable ();
16574 }
16575
16576 case CAST_EXPR:
16577 case REINTERPRET_CAST_EXPR:
16578 case CONST_CAST_EXPR:
16579 case STATIC_CAST_EXPR:
16580 case DYNAMIC_CAST_EXPR:
16581 case IMPLICIT_CONV_EXPR:
16582 case CONVERT_EXPR:
16583 case NOP_EXPR:
16584 {
16585 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16586 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16587 return build1 (code, type, op0);
16588 }
16589
16590 case SIZEOF_EXPR:
16591 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16592 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16593 {
16594 tree expanded, op = TREE_OPERAND (t, 0);
16595 int len = 0;
16596
16597 if (SIZEOF_EXPR_TYPE_P (t))
16598 op = TREE_TYPE (op);
16599
16600 ++cp_unevaluated_operand;
16601 ++c_inhibit_evaluation_warnings;
16602 /* We only want to compute the number of arguments. */
16603 if (PACK_EXPANSION_P (op))
16604 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16605 else
16606 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16607 args, complain, in_decl);
16608 --cp_unevaluated_operand;
16609 --c_inhibit_evaluation_warnings;
16610
16611 if (TREE_CODE (expanded) == TREE_VEC)
16612 {
16613 len = TREE_VEC_LENGTH (expanded);
16614 /* Set TREE_USED for the benefit of -Wunused. */
16615 for (int i = 0; i < len; i++)
16616 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16617 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16618 }
16619
16620 if (expanded == error_mark_node)
16621 return error_mark_node;
16622 else if (PACK_EXPANSION_P (expanded)
16623 || (TREE_CODE (expanded) == TREE_VEC
16624 && pack_expansion_args_count (expanded)))
16625
16626 {
16627 if (PACK_EXPANSION_P (expanded))
16628 /* OK. */;
16629 else if (TREE_VEC_LENGTH (expanded) == 1)
16630 expanded = TREE_VEC_ELT (expanded, 0);
16631 else
16632 expanded = make_argument_pack (expanded);
16633
16634 if (TYPE_P (expanded))
16635 return cxx_sizeof_or_alignof_type (input_location,
16636 expanded, SIZEOF_EXPR,
16637 false,
16638 complain & tf_error);
16639 else
16640 return cxx_sizeof_or_alignof_expr (input_location,
16641 expanded, SIZEOF_EXPR,
16642 complain & tf_error);
16643 }
16644 else
16645 return build_int_cst (size_type_node, len);
16646 }
16647 if (SIZEOF_EXPR_TYPE_P (t))
16648 {
16649 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16650 args, complain, in_decl);
16651 r = build1 (NOP_EXPR, r, error_mark_node);
16652 r = build1 (SIZEOF_EXPR,
16653 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16654 SIZEOF_EXPR_TYPE_P (r) = 1;
16655 return r;
16656 }
16657 /* Fall through */
16658
16659 case INDIRECT_REF:
16660 case NEGATE_EXPR:
16661 case TRUTH_NOT_EXPR:
16662 case BIT_NOT_EXPR:
16663 case ADDR_EXPR:
16664 case UNARY_PLUS_EXPR: /* Unary + */
16665 case ALIGNOF_EXPR:
16666 case AT_ENCODE_EXPR:
16667 case ARROW_EXPR:
16668 case THROW_EXPR:
16669 case TYPEID_EXPR:
16670 case REALPART_EXPR:
16671 case IMAGPART_EXPR:
16672 case PAREN_EXPR:
16673 {
16674 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16675 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16676 r = build1 (code, type, op0);
16677 if (code == ALIGNOF_EXPR)
16678 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16679 return r;
16680 }
16681
16682 case COMPONENT_REF:
16683 {
16684 tree object;
16685 tree name;
16686
16687 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16688 name = TREE_OPERAND (t, 1);
16689 if (TREE_CODE (name) == BIT_NOT_EXPR)
16690 {
16691 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16692 complain, in_decl);
16693 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16694 }
16695 else if (TREE_CODE (name) == SCOPE_REF
16696 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16697 {
16698 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16699 complain, in_decl);
16700 name = TREE_OPERAND (name, 1);
16701 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16702 complain, in_decl);
16703 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16704 name = build_qualified_name (/*type=*/NULL_TREE,
16705 base, name,
16706 /*template_p=*/false);
16707 }
16708 else if (BASELINK_P (name))
16709 name = tsubst_baselink (name,
16710 non_reference (TREE_TYPE (object)),
16711 args, complain,
16712 in_decl);
16713 else
16714 name = tsubst_copy (name, args, complain, in_decl);
16715 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16716 }
16717
16718 case PLUS_EXPR:
16719 case MINUS_EXPR:
16720 case MULT_EXPR:
16721 case TRUNC_DIV_EXPR:
16722 case CEIL_DIV_EXPR:
16723 case FLOOR_DIV_EXPR:
16724 case ROUND_DIV_EXPR:
16725 case EXACT_DIV_EXPR:
16726 case BIT_AND_EXPR:
16727 case BIT_IOR_EXPR:
16728 case BIT_XOR_EXPR:
16729 case TRUNC_MOD_EXPR:
16730 case FLOOR_MOD_EXPR:
16731 case TRUTH_ANDIF_EXPR:
16732 case TRUTH_ORIF_EXPR:
16733 case TRUTH_AND_EXPR:
16734 case TRUTH_OR_EXPR:
16735 case RSHIFT_EXPR:
16736 case LSHIFT_EXPR:
16737 case EQ_EXPR:
16738 case NE_EXPR:
16739 case MAX_EXPR:
16740 case MIN_EXPR:
16741 case LE_EXPR:
16742 case GE_EXPR:
16743 case LT_EXPR:
16744 case GT_EXPR:
16745 case COMPOUND_EXPR:
16746 case DOTSTAR_EXPR:
16747 case MEMBER_REF:
16748 case PREDECREMENT_EXPR:
16749 case PREINCREMENT_EXPR:
16750 case POSTDECREMENT_EXPR:
16751 case POSTINCREMENT_EXPR:
16752 {
16753 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16754 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16755 return build_nt (code, op0, op1);
16756 }
16757
16758 case SCOPE_REF:
16759 {
16760 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16761 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16762 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16763 QUALIFIED_NAME_IS_TEMPLATE (t));
16764 }
16765
16766 case ARRAY_REF:
16767 {
16768 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16769 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16770 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16771 }
16772
16773 case CALL_EXPR:
16774 {
16775 int n = VL_EXP_OPERAND_LENGTH (t);
16776 tree result = build_vl_exp (CALL_EXPR, n);
16777 int i;
16778 for (i = 0; i < n; i++)
16779 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16780 complain, in_decl);
16781 return result;
16782 }
16783
16784 case COND_EXPR:
16785 case MODOP_EXPR:
16786 case PSEUDO_DTOR_EXPR:
16787 case VEC_PERM_EXPR:
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 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16792 r = build_nt (code, op0, op1, op2);
16793 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16794 return r;
16795 }
16796
16797 case NEW_EXPR:
16798 {
16799 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16800 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16801 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16802 r = build_nt (code, op0, op1, op2);
16803 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16804 return r;
16805 }
16806
16807 case DELETE_EXPR:
16808 {
16809 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16810 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16811 r = build_nt (code, op0, op1);
16812 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16813 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16814 return r;
16815 }
16816
16817 case TEMPLATE_ID_EXPR:
16818 {
16819 /* Substituted template arguments */
16820 tree fn = TREE_OPERAND (t, 0);
16821 tree targs = TREE_OPERAND (t, 1);
16822
16823 fn = tsubst_copy (fn, args, complain, in_decl);
16824 if (targs)
16825 targs = tsubst_template_args (targs, args, complain, in_decl);
16826
16827 return lookup_template_function (fn, targs);
16828 }
16829
16830 case TREE_LIST:
16831 {
16832 tree purpose, value, chain;
16833
16834 if (t == void_list_node)
16835 return t;
16836
16837 purpose = TREE_PURPOSE (t);
16838 if (purpose)
16839 purpose = tsubst_copy (purpose, args, complain, in_decl);
16840 value = TREE_VALUE (t);
16841 if (value)
16842 value = tsubst_copy (value, args, complain, in_decl);
16843 chain = TREE_CHAIN (t);
16844 if (chain && chain != void_type_node)
16845 chain = tsubst_copy (chain, args, complain, in_decl);
16846 if (purpose == TREE_PURPOSE (t)
16847 && value == TREE_VALUE (t)
16848 && chain == TREE_CHAIN (t))
16849 return t;
16850 return tree_cons (purpose, value, chain);
16851 }
16852
16853 case RECORD_TYPE:
16854 case UNION_TYPE:
16855 case ENUMERAL_TYPE:
16856 case INTEGER_TYPE:
16857 case TEMPLATE_TYPE_PARM:
16858 case TEMPLATE_TEMPLATE_PARM:
16859 case BOUND_TEMPLATE_TEMPLATE_PARM:
16860 case TEMPLATE_PARM_INDEX:
16861 case POINTER_TYPE:
16862 case REFERENCE_TYPE:
16863 case OFFSET_TYPE:
16864 case FUNCTION_TYPE:
16865 case METHOD_TYPE:
16866 case ARRAY_TYPE:
16867 case TYPENAME_TYPE:
16868 case UNBOUND_CLASS_TEMPLATE:
16869 case TYPEOF_TYPE:
16870 case DECLTYPE_TYPE:
16871 case TYPE_DECL:
16872 return tsubst (t, args, complain, in_decl);
16873
16874 case USING_DECL:
16875 t = DECL_NAME (t);
16876 /* Fall through. */
16877 case IDENTIFIER_NODE:
16878 if (IDENTIFIER_CONV_OP_P (t))
16879 {
16880 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16881 return make_conv_op_name (new_type);
16882 }
16883 else
16884 return t;
16885
16886 case CONSTRUCTOR:
16887 /* This is handled by tsubst_copy_and_build. */
16888 gcc_unreachable ();
16889
16890 case VA_ARG_EXPR:
16891 {
16892 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16893 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16894 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16895 }
16896
16897 case CLEANUP_POINT_EXPR:
16898 /* We shouldn't have built any of these during initial template
16899 generation. Instead, they should be built during instantiation
16900 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16901 gcc_unreachable ();
16902
16903 case OFFSET_REF:
16904 {
16905 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16906 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16907 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16908 r = build2 (code, type, op0, op1);
16909 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16910 if (!mark_used (TREE_OPERAND (r, 1), complain)
16911 && !(complain & tf_error))
16912 return error_mark_node;
16913 return r;
16914 }
16915
16916 case EXPR_PACK_EXPANSION:
16917 error ("invalid use of pack expansion expression");
16918 return error_mark_node;
16919
16920 case NONTYPE_ARGUMENT_PACK:
16921 error ("use %<...%> to expand argument pack");
16922 return error_mark_node;
16923
16924 case VOID_CST:
16925 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16926 return t;
16927
16928 case INTEGER_CST:
16929 case REAL_CST:
16930 case COMPLEX_CST:
16931 {
16932 /* Instantiate any typedefs in the type. */
16933 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16934 r = fold_convert (type, t);
16935 gcc_assert (TREE_CODE (r) == code);
16936 return r;
16937 }
16938
16939 case STRING_CST:
16940 {
16941 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16942 r = t;
16943 if (type != TREE_TYPE (t))
16944 {
16945 r = copy_node (t);
16946 TREE_TYPE (r) = type;
16947 }
16948 return r;
16949 }
16950
16951 case PTRMEM_CST:
16952 /* These can sometimes show up in a partial instantiation, but never
16953 involve template parms. */
16954 gcc_assert (!uses_template_parms (t));
16955 return t;
16956
16957 case UNARY_LEFT_FOLD_EXPR:
16958 return tsubst_unary_left_fold (t, args, complain, in_decl);
16959 case UNARY_RIGHT_FOLD_EXPR:
16960 return tsubst_unary_right_fold (t, args, complain, in_decl);
16961 case BINARY_LEFT_FOLD_EXPR:
16962 return tsubst_binary_left_fold (t, args, complain, in_decl);
16963 case BINARY_RIGHT_FOLD_EXPR:
16964 return tsubst_binary_right_fold (t, args, complain, in_decl);
16965 case PREDICT_EXPR:
16966 return t;
16967
16968 case DEBUG_BEGIN_STMT:
16969 /* ??? There's no point in copying it for now, but maybe some
16970 day it will contain more information, such as a pointer back
16971 to the containing function, inlined copy or so. */
16972 return t;
16973
16974 case CO_AWAIT_EXPR:
16975 return tsubst_expr (t, args, complain, in_decl,
16976 /*integral_constant_expression_p=*/false);
16977 break;
16978
16979 default:
16980 /* We shouldn't get here, but keep going if !flag_checking. */
16981 if (flag_checking)
16982 gcc_unreachable ();
16983 return t;
16984 }
16985 }
16986
16987 /* Helper function for tsubst_omp_clauses, used for instantiation of
16988 OMP_CLAUSE_DECL of clauses. */
16989
16990 static tree
16991 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16992 tree in_decl, tree *iterator_cache)
16993 {
16994 if (decl == NULL_TREE)
16995 return NULL_TREE;
16996
16997 /* Handle OpenMP iterators. */
16998 if (TREE_CODE (decl) == TREE_LIST
16999 && TREE_PURPOSE (decl)
17000 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17001 {
17002 tree ret;
17003 if (iterator_cache[0] == TREE_PURPOSE (decl))
17004 ret = iterator_cache[1];
17005 else
17006 {
17007 tree *tp = &ret;
17008 begin_scope (sk_omp, NULL);
17009 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17010 {
17011 *tp = copy_node (it);
17012 TREE_VEC_ELT (*tp, 0)
17013 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17014 TREE_VEC_ELT (*tp, 1)
17015 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17016 /*integral_constant_expression_p=*/false);
17017 TREE_VEC_ELT (*tp, 2)
17018 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17019 /*integral_constant_expression_p=*/false);
17020 TREE_VEC_ELT (*tp, 3)
17021 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17022 /*integral_constant_expression_p=*/false);
17023 TREE_CHAIN (*tp) = NULL_TREE;
17024 tp = &TREE_CHAIN (*tp);
17025 }
17026 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17027 iterator_cache[0] = TREE_PURPOSE (decl);
17028 iterator_cache[1] = ret;
17029 }
17030 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17031 args, complain,
17032 in_decl, NULL));
17033 }
17034
17035 /* Handle an OpenMP array section represented as a TREE_LIST (or
17036 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17037 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17038 TREE_LIST. We can handle it exactly the same as an array section
17039 (purpose, value, and a chain), even though the nomenclature
17040 (low_bound, length, etc) is different. */
17041 if (TREE_CODE (decl) == TREE_LIST)
17042 {
17043 tree low_bound
17044 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17045 /*integral_constant_expression_p=*/false);
17046 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17047 /*integral_constant_expression_p=*/false);
17048 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17049 in_decl, NULL);
17050 if (TREE_PURPOSE (decl) == low_bound
17051 && TREE_VALUE (decl) == length
17052 && TREE_CHAIN (decl) == chain)
17053 return decl;
17054 tree ret = tree_cons (low_bound, length, chain);
17055 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17056 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17057 return ret;
17058 }
17059 tree ret = tsubst_expr (decl, args, complain, in_decl,
17060 /*integral_constant_expression_p=*/false);
17061 /* Undo convert_from_reference tsubst_expr could have called. */
17062 if (decl
17063 && REFERENCE_REF_P (ret)
17064 && !REFERENCE_REF_P (decl))
17065 ret = TREE_OPERAND (ret, 0);
17066 return ret;
17067 }
17068
17069 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17070
17071 static tree
17072 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17073 tree args, tsubst_flags_t complain, tree in_decl)
17074 {
17075 tree new_clauses = NULL_TREE, nc, oc;
17076 tree linear_no_step = NULL_TREE;
17077 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17078
17079 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17080 {
17081 nc = copy_node (oc);
17082 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17083 new_clauses = nc;
17084
17085 switch (OMP_CLAUSE_CODE (nc))
17086 {
17087 case OMP_CLAUSE_LASTPRIVATE:
17088 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17089 {
17090 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17091 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17092 in_decl, /*integral_constant_expression_p=*/false);
17093 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17094 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17095 }
17096 /* FALLTHRU */
17097 case OMP_CLAUSE_PRIVATE:
17098 case OMP_CLAUSE_SHARED:
17099 case OMP_CLAUSE_FIRSTPRIVATE:
17100 case OMP_CLAUSE_COPYIN:
17101 case OMP_CLAUSE_COPYPRIVATE:
17102 case OMP_CLAUSE_UNIFORM:
17103 case OMP_CLAUSE_DEPEND:
17104 case OMP_CLAUSE_FROM:
17105 case OMP_CLAUSE_TO:
17106 case OMP_CLAUSE_MAP:
17107 case OMP_CLAUSE_NONTEMPORAL:
17108 case OMP_CLAUSE_USE_DEVICE_PTR:
17109 case OMP_CLAUSE_USE_DEVICE_ADDR:
17110 case OMP_CLAUSE_IS_DEVICE_PTR:
17111 case OMP_CLAUSE_INCLUSIVE:
17112 case OMP_CLAUSE_EXCLUSIVE:
17113 OMP_CLAUSE_DECL (nc)
17114 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17115 in_decl, iterator_cache);
17116 break;
17117 case OMP_CLAUSE_TILE:
17118 case OMP_CLAUSE_IF:
17119 case OMP_CLAUSE_NUM_THREADS:
17120 case OMP_CLAUSE_SCHEDULE:
17121 case OMP_CLAUSE_COLLAPSE:
17122 case OMP_CLAUSE_FINAL:
17123 case OMP_CLAUSE_DEVICE:
17124 case OMP_CLAUSE_DIST_SCHEDULE:
17125 case OMP_CLAUSE_NUM_TEAMS:
17126 case OMP_CLAUSE_THREAD_LIMIT:
17127 case OMP_CLAUSE_SAFELEN:
17128 case OMP_CLAUSE_SIMDLEN:
17129 case OMP_CLAUSE_NUM_TASKS:
17130 case OMP_CLAUSE_GRAINSIZE:
17131 case OMP_CLAUSE_PRIORITY:
17132 case OMP_CLAUSE_ORDERED:
17133 case OMP_CLAUSE_HINT:
17134 case OMP_CLAUSE_NUM_GANGS:
17135 case OMP_CLAUSE_NUM_WORKERS:
17136 case OMP_CLAUSE_VECTOR_LENGTH:
17137 case OMP_CLAUSE_WORKER:
17138 case OMP_CLAUSE_VECTOR:
17139 case OMP_CLAUSE_ASYNC:
17140 case OMP_CLAUSE_WAIT:
17141 OMP_CLAUSE_OPERAND (nc, 0)
17142 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17143 in_decl, /*integral_constant_expression_p=*/false);
17144 break;
17145 case OMP_CLAUSE_REDUCTION:
17146 case OMP_CLAUSE_IN_REDUCTION:
17147 case OMP_CLAUSE_TASK_REDUCTION:
17148 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17149 {
17150 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17151 if (TREE_CODE (placeholder) == SCOPE_REF)
17152 {
17153 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17154 complain, in_decl);
17155 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17156 = build_qualified_name (NULL_TREE, scope,
17157 TREE_OPERAND (placeholder, 1),
17158 false);
17159 }
17160 else
17161 gcc_assert (identifier_p (placeholder));
17162 }
17163 OMP_CLAUSE_DECL (nc)
17164 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17165 in_decl, NULL);
17166 break;
17167 case OMP_CLAUSE_GANG:
17168 case OMP_CLAUSE_ALIGNED:
17169 OMP_CLAUSE_DECL (nc)
17170 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17171 in_decl, NULL);
17172 OMP_CLAUSE_OPERAND (nc, 1)
17173 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17174 in_decl, /*integral_constant_expression_p=*/false);
17175 break;
17176 case OMP_CLAUSE_LINEAR:
17177 OMP_CLAUSE_DECL (nc)
17178 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17179 in_decl, NULL);
17180 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17181 {
17182 gcc_assert (!linear_no_step);
17183 linear_no_step = nc;
17184 }
17185 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17186 OMP_CLAUSE_LINEAR_STEP (nc)
17187 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17188 complain, in_decl, NULL);
17189 else
17190 OMP_CLAUSE_LINEAR_STEP (nc)
17191 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17192 in_decl,
17193 /*integral_constant_expression_p=*/false);
17194 break;
17195 case OMP_CLAUSE_NOWAIT:
17196 case OMP_CLAUSE_DEFAULT:
17197 case OMP_CLAUSE_UNTIED:
17198 case OMP_CLAUSE_MERGEABLE:
17199 case OMP_CLAUSE_INBRANCH:
17200 case OMP_CLAUSE_NOTINBRANCH:
17201 case OMP_CLAUSE_PROC_BIND:
17202 case OMP_CLAUSE_FOR:
17203 case OMP_CLAUSE_PARALLEL:
17204 case OMP_CLAUSE_SECTIONS:
17205 case OMP_CLAUSE_TASKGROUP:
17206 case OMP_CLAUSE_NOGROUP:
17207 case OMP_CLAUSE_THREADS:
17208 case OMP_CLAUSE_SIMD:
17209 case OMP_CLAUSE_DEFAULTMAP:
17210 case OMP_CLAUSE_ORDER:
17211 case OMP_CLAUSE_BIND:
17212 case OMP_CLAUSE_INDEPENDENT:
17213 case OMP_CLAUSE_AUTO:
17214 case OMP_CLAUSE_SEQ:
17215 case OMP_CLAUSE_IF_PRESENT:
17216 case OMP_CLAUSE_FINALIZE:
17217 break;
17218 default:
17219 gcc_unreachable ();
17220 }
17221 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17222 switch (OMP_CLAUSE_CODE (nc))
17223 {
17224 case OMP_CLAUSE_SHARED:
17225 case OMP_CLAUSE_PRIVATE:
17226 case OMP_CLAUSE_FIRSTPRIVATE:
17227 case OMP_CLAUSE_LASTPRIVATE:
17228 case OMP_CLAUSE_COPYPRIVATE:
17229 case OMP_CLAUSE_LINEAR:
17230 case OMP_CLAUSE_REDUCTION:
17231 case OMP_CLAUSE_IN_REDUCTION:
17232 case OMP_CLAUSE_TASK_REDUCTION:
17233 case OMP_CLAUSE_USE_DEVICE_PTR:
17234 case OMP_CLAUSE_USE_DEVICE_ADDR:
17235 case OMP_CLAUSE_IS_DEVICE_PTR:
17236 case OMP_CLAUSE_INCLUSIVE:
17237 case OMP_CLAUSE_EXCLUSIVE:
17238 /* tsubst_expr on SCOPE_REF results in returning
17239 finish_non_static_data_member result. Undo that here. */
17240 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17241 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17242 == IDENTIFIER_NODE))
17243 {
17244 tree t = OMP_CLAUSE_DECL (nc);
17245 tree v = t;
17246 while (v)
17247 switch (TREE_CODE (v))
17248 {
17249 case COMPONENT_REF:
17250 case MEM_REF:
17251 case INDIRECT_REF:
17252 CASE_CONVERT:
17253 case POINTER_PLUS_EXPR:
17254 v = TREE_OPERAND (v, 0);
17255 continue;
17256 case PARM_DECL:
17257 if (DECL_CONTEXT (v) == current_function_decl
17258 && DECL_ARTIFICIAL (v)
17259 && DECL_NAME (v) == this_identifier)
17260 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17261 /* FALLTHRU */
17262 default:
17263 v = NULL_TREE;
17264 break;
17265 }
17266 }
17267 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17268 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17269 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17270 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17271 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17272 {
17273 tree decl = OMP_CLAUSE_DECL (nc);
17274 if (VAR_P (decl))
17275 {
17276 retrofit_lang_decl (decl);
17277 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17278 }
17279 }
17280 break;
17281 default:
17282 break;
17283 }
17284 }
17285
17286 new_clauses = nreverse (new_clauses);
17287 if (ort != C_ORT_OMP_DECLARE_SIMD)
17288 {
17289 new_clauses = finish_omp_clauses (new_clauses, ort);
17290 if (linear_no_step)
17291 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17292 if (nc == linear_no_step)
17293 {
17294 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17295 break;
17296 }
17297 }
17298 return new_clauses;
17299 }
17300
17301 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17302
17303 static tree
17304 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17305 tree in_decl)
17306 {
17307 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17308
17309 tree purpose, value, chain;
17310
17311 if (t == NULL)
17312 return t;
17313
17314 if (TREE_CODE (t) != TREE_LIST)
17315 return tsubst_copy_and_build (t, args, complain, in_decl,
17316 /*function_p=*/false,
17317 /*integral_constant_expression_p=*/false);
17318
17319 if (t == void_list_node)
17320 return t;
17321
17322 purpose = TREE_PURPOSE (t);
17323 if (purpose)
17324 purpose = RECUR (purpose);
17325 value = TREE_VALUE (t);
17326 if (value)
17327 {
17328 if (TREE_CODE (value) != LABEL_DECL)
17329 value = RECUR (value);
17330 else
17331 {
17332 value = lookup_label (DECL_NAME (value));
17333 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17334 TREE_USED (value) = 1;
17335 }
17336 }
17337 chain = TREE_CHAIN (t);
17338 if (chain && chain != void_type_node)
17339 chain = RECUR (chain);
17340 return tree_cons (purpose, value, chain);
17341 #undef RECUR
17342 }
17343
17344 /* Used to temporarily communicate the list of #pragma omp parallel
17345 clauses to #pragma omp for instantiation if they are combined
17346 together. */
17347
17348 static tree *omp_parallel_combined_clauses;
17349
17350 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17351 tree *, unsigned int *);
17352
17353 /* Substitute one OMP_FOR iterator. */
17354
17355 static bool
17356 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17357 tree initv, tree condv, tree incrv, tree *clauses,
17358 tree args, tsubst_flags_t complain, tree in_decl,
17359 bool integral_constant_expression_p)
17360 {
17361 #define RECUR(NODE) \
17362 tsubst_expr ((NODE), args, complain, in_decl, \
17363 integral_constant_expression_p)
17364 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17365 bool ret = false;
17366
17367 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17368 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17369
17370 decl = TREE_OPERAND (init, 0);
17371 init = TREE_OPERAND (init, 1);
17372 tree decl_expr = NULL_TREE;
17373 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17374 if (range_for)
17375 {
17376 bool decomp = false;
17377 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17378 {
17379 tree v = DECL_VALUE_EXPR (decl);
17380 if (TREE_CODE (v) == ARRAY_REF
17381 && VAR_P (TREE_OPERAND (v, 0))
17382 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17383 {
17384 tree decomp_first = NULL_TREE;
17385 unsigned decomp_cnt = 0;
17386 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17387 maybe_push_decl (d);
17388 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17389 in_decl, &decomp_first, &decomp_cnt);
17390 decomp = true;
17391 if (d == error_mark_node)
17392 decl = error_mark_node;
17393 else
17394 for (unsigned int i = 0; i < decomp_cnt; i++)
17395 {
17396 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17397 {
17398 tree v = build_nt (ARRAY_REF, d,
17399 size_int (decomp_cnt - i - 1),
17400 NULL_TREE, NULL_TREE);
17401 SET_DECL_VALUE_EXPR (decomp_first, v);
17402 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17403 }
17404 fit_decomposition_lang_decl (decomp_first, d);
17405 decomp_first = DECL_CHAIN (decomp_first);
17406 }
17407 }
17408 }
17409 decl = tsubst_decl (decl, args, complain);
17410 if (!decomp)
17411 maybe_push_decl (decl);
17412 }
17413 else if (init && TREE_CODE (init) == DECL_EXPR)
17414 {
17415 /* We need to jump through some hoops to handle declarations in the
17416 init-statement, since we might need to handle auto deduction,
17417 but we need to keep control of initialization. */
17418 decl_expr = init;
17419 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17420 decl = tsubst_decl (decl, args, complain);
17421 }
17422 else
17423 {
17424 if (TREE_CODE (decl) == SCOPE_REF)
17425 {
17426 decl = RECUR (decl);
17427 if (TREE_CODE (decl) == COMPONENT_REF)
17428 {
17429 tree v = decl;
17430 while (v)
17431 switch (TREE_CODE (v))
17432 {
17433 case COMPONENT_REF:
17434 case MEM_REF:
17435 case INDIRECT_REF:
17436 CASE_CONVERT:
17437 case POINTER_PLUS_EXPR:
17438 v = TREE_OPERAND (v, 0);
17439 continue;
17440 case PARM_DECL:
17441 if (DECL_CONTEXT (v) == current_function_decl
17442 && DECL_ARTIFICIAL (v)
17443 && DECL_NAME (v) == this_identifier)
17444 {
17445 decl = TREE_OPERAND (decl, 1);
17446 decl = omp_privatize_field (decl, false);
17447 }
17448 /* FALLTHRU */
17449 default:
17450 v = NULL_TREE;
17451 break;
17452 }
17453 }
17454 }
17455 else
17456 decl = RECUR (decl);
17457 }
17458 init = RECUR (init);
17459
17460 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17461 {
17462 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17463 if (TREE_CODE (o) == TREE_LIST)
17464 TREE_VEC_ELT (orig_declv, i)
17465 = tree_cons (RECUR (TREE_PURPOSE (o)),
17466 RECUR (TREE_VALUE (o)),
17467 NULL_TREE);
17468 else
17469 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17470 }
17471
17472 if (range_for)
17473 {
17474 tree this_pre_body = NULL_TREE;
17475 tree orig_init = NULL_TREE;
17476 tree orig_decl = NULL_TREE;
17477 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17478 orig_init, cond, incr);
17479 if (orig_decl)
17480 {
17481 if (orig_declv == NULL_TREE)
17482 orig_declv = copy_node (declv);
17483 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17484 ret = true;
17485 }
17486 else if (orig_declv)
17487 TREE_VEC_ELT (orig_declv, i) = decl;
17488 }
17489
17490 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17491 if (!range_for && auto_node && init)
17492 TREE_TYPE (decl)
17493 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17494
17495 gcc_assert (!type_dependent_expression_p (decl));
17496
17497 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17498 {
17499 if (decl_expr)
17500 {
17501 /* Declare the variable, but don't let that initialize it. */
17502 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17503 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17504 RECUR (decl_expr);
17505 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17506 }
17507
17508 if (!range_for)
17509 {
17510 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
17511 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17512 if (TREE_CODE (incr) == MODIFY_EXPR)
17513 {
17514 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17515 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17516 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17517 NOP_EXPR, rhs, complain);
17518 }
17519 else
17520 incr = RECUR (incr);
17521 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17522 TREE_VEC_ELT (orig_declv, i) = decl;
17523 }
17524 TREE_VEC_ELT (declv, i) = decl;
17525 TREE_VEC_ELT (initv, i) = init;
17526 TREE_VEC_ELT (condv, i) = cond;
17527 TREE_VEC_ELT (incrv, i) = incr;
17528 return ret;
17529 }
17530
17531 if (decl_expr)
17532 {
17533 /* Declare and initialize the variable. */
17534 RECUR (decl_expr);
17535 init = NULL_TREE;
17536 }
17537 else if (init)
17538 {
17539 tree *pc;
17540 int j;
17541 for (j = ((omp_parallel_combined_clauses == NULL
17542 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17543 {
17544 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17545 {
17546 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17547 && OMP_CLAUSE_DECL (*pc) == decl)
17548 break;
17549 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17550 && OMP_CLAUSE_DECL (*pc) == decl)
17551 {
17552 if (j)
17553 break;
17554 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17555 tree c = *pc;
17556 *pc = OMP_CLAUSE_CHAIN (c);
17557 OMP_CLAUSE_CHAIN (c) = *clauses;
17558 *clauses = c;
17559 }
17560 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17561 && OMP_CLAUSE_DECL (*pc) == decl)
17562 {
17563 error ("iteration variable %qD should not be firstprivate",
17564 decl);
17565 *pc = OMP_CLAUSE_CHAIN (*pc);
17566 }
17567 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17568 && OMP_CLAUSE_DECL (*pc) == decl)
17569 {
17570 error ("iteration variable %qD should not be reduction",
17571 decl);
17572 *pc = OMP_CLAUSE_CHAIN (*pc);
17573 }
17574 else
17575 pc = &OMP_CLAUSE_CHAIN (*pc);
17576 }
17577 if (*pc)
17578 break;
17579 }
17580 if (*pc == NULL_TREE)
17581 {
17582 tree c = build_omp_clause (input_location,
17583 TREE_CODE (t) == OMP_LOOP
17584 ? OMP_CLAUSE_LASTPRIVATE
17585 : OMP_CLAUSE_PRIVATE);
17586 OMP_CLAUSE_DECL (c) = decl;
17587 c = finish_omp_clauses (c, C_ORT_OMP);
17588 if (c)
17589 {
17590 OMP_CLAUSE_CHAIN (c) = *clauses;
17591 *clauses = c;
17592 }
17593 }
17594 }
17595 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17596 if (COMPARISON_CLASS_P (cond))
17597 {
17598 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17599 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17600 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17601 }
17602 else
17603 cond = RECUR (cond);
17604 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17605 switch (TREE_CODE (incr))
17606 {
17607 case PREINCREMENT_EXPR:
17608 case PREDECREMENT_EXPR:
17609 case POSTINCREMENT_EXPR:
17610 case POSTDECREMENT_EXPR:
17611 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17612 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17613 break;
17614 case MODIFY_EXPR:
17615 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17616 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17617 {
17618 tree rhs = TREE_OPERAND (incr, 1);
17619 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17620 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17621 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17622 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17623 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17624 rhs0, rhs1));
17625 }
17626 else
17627 incr = RECUR (incr);
17628 break;
17629 case MODOP_EXPR:
17630 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17631 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17632 {
17633 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17634 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17635 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17636 TREE_TYPE (decl), lhs,
17637 RECUR (TREE_OPERAND (incr, 2))));
17638 }
17639 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17640 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17641 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17642 {
17643 tree rhs = TREE_OPERAND (incr, 2);
17644 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17645 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17646 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17647 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17648 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17649 rhs0, rhs1));
17650 }
17651 else
17652 incr = RECUR (incr);
17653 break;
17654 default:
17655 incr = RECUR (incr);
17656 break;
17657 }
17658
17659 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17660 TREE_VEC_ELT (orig_declv, i) = decl;
17661 TREE_VEC_ELT (declv, i) = decl;
17662 TREE_VEC_ELT (initv, i) = init;
17663 TREE_VEC_ELT (condv, i) = cond;
17664 TREE_VEC_ELT (incrv, i) = incr;
17665 return false;
17666 #undef RECUR
17667 }
17668
17669 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17670 of OMP_TARGET's body. */
17671
17672 static tree
17673 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17674 {
17675 *walk_subtrees = 0;
17676 switch (TREE_CODE (*tp))
17677 {
17678 case OMP_TEAMS:
17679 return *tp;
17680 case BIND_EXPR:
17681 case STATEMENT_LIST:
17682 *walk_subtrees = 1;
17683 break;
17684 default:
17685 break;
17686 }
17687 return NULL_TREE;
17688 }
17689
17690 /* Helper function for tsubst_expr. For decomposition declaration
17691 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17692 also the corresponding decls representing the identifiers
17693 of the decomposition declaration. Return DECL if successful
17694 or error_mark_node otherwise, set *FIRST to the first decl
17695 in the list chained through DECL_CHAIN and *CNT to the number
17696 of such decls. */
17697
17698 static tree
17699 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17700 tsubst_flags_t complain, tree in_decl, tree *first,
17701 unsigned int *cnt)
17702 {
17703 tree decl2, decl3, prev = decl;
17704 *cnt = 0;
17705 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17706 for (decl2 = DECL_CHAIN (pattern_decl);
17707 decl2
17708 && VAR_P (decl2)
17709 && DECL_DECOMPOSITION_P (decl2)
17710 && DECL_NAME (decl2);
17711 decl2 = DECL_CHAIN (decl2))
17712 {
17713 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17714 {
17715 gcc_assert (errorcount);
17716 return error_mark_node;
17717 }
17718 (*cnt)++;
17719 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17720 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17721 tree v = DECL_VALUE_EXPR (decl2);
17722 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17723 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17724 decl3 = tsubst (decl2, args, complain, in_decl);
17725 SET_DECL_VALUE_EXPR (decl2, v);
17726 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17727 if (VAR_P (decl3))
17728 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17729 else
17730 {
17731 gcc_assert (errorcount);
17732 decl = error_mark_node;
17733 continue;
17734 }
17735 maybe_push_decl (decl3);
17736 if (error_operand_p (decl3))
17737 decl = error_mark_node;
17738 else if (decl != error_mark_node
17739 && DECL_CHAIN (decl3) != prev
17740 && decl != prev)
17741 {
17742 gcc_assert (errorcount);
17743 decl = error_mark_node;
17744 }
17745 else
17746 prev = decl3;
17747 }
17748 *first = prev;
17749 return decl;
17750 }
17751
17752 /* Return the proper local_specialization for init-capture pack DECL. */
17753
17754 static tree
17755 lookup_init_capture_pack (tree decl)
17756 {
17757 /* We handle normal pack captures by forwarding to the specialization of the
17758 captured parameter. We can't do that for pack init-captures; we need them
17759 to have their own local_specialization. We created the individual
17760 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17761 when we process the DECL_EXPR for the pack init-capture in the template.
17762 So, how do we find them? We don't know the capture proxy pack when
17763 building the individual resulting proxies, and we don't know the
17764 individual proxies when instantiating the pack. What we have in common is
17765 the FIELD_DECL.
17766
17767 So...when we instantiate the FIELD_DECL, we stick the result in
17768 local_specializations. Then at the DECL_EXPR we look up that result, see
17769 how many elements it has, synthesize the names, and look them up. */
17770
17771 tree cname = DECL_NAME (decl);
17772 tree val = DECL_VALUE_EXPR (decl);
17773 tree field = TREE_OPERAND (val, 1);
17774 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17775 tree fpack = retrieve_local_specialization (field);
17776 if (fpack == error_mark_node)
17777 return error_mark_node;
17778
17779 int len = 1;
17780 tree vec = NULL_TREE;
17781 tree r = NULL_TREE;
17782 if (TREE_CODE (fpack) == TREE_VEC)
17783 {
17784 len = TREE_VEC_LENGTH (fpack);
17785 vec = make_tree_vec (len);
17786 r = make_node (NONTYPE_ARGUMENT_PACK);
17787 SET_ARGUMENT_PACK_ARGS (r, vec);
17788 }
17789 for (int i = 0; i < len; ++i)
17790 {
17791 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17792 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17793 if (vec)
17794 TREE_VEC_ELT (vec, i) = elt;
17795 else
17796 r = elt;
17797 }
17798 return r;
17799 }
17800
17801 /* Like tsubst_copy for expressions, etc. but also does semantic
17802 processing. */
17803
17804 tree
17805 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17806 bool integral_constant_expression_p)
17807 {
17808 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17809 #define RECUR(NODE) \
17810 tsubst_expr ((NODE), args, complain, in_decl, \
17811 integral_constant_expression_p)
17812
17813 tree stmt, tmp;
17814 tree r;
17815 location_t loc;
17816
17817 if (t == NULL_TREE || t == error_mark_node)
17818 return t;
17819
17820 loc = input_location;
17821 if (location_t eloc = cp_expr_location (t))
17822 input_location = eloc;
17823 if (STATEMENT_CODE_P (TREE_CODE (t)))
17824 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17825
17826 switch (TREE_CODE (t))
17827 {
17828 case STATEMENT_LIST:
17829 {
17830 tree_stmt_iterator i;
17831 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17832 RECUR (tsi_stmt (i));
17833 break;
17834 }
17835
17836 case CTOR_INITIALIZER:
17837 finish_mem_initializers (tsubst_initializer_list
17838 (TREE_OPERAND (t, 0), args));
17839 break;
17840
17841 case RETURN_EXPR:
17842 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17843 break;
17844
17845 case CO_RETURN_EXPR:
17846 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
17847 break;
17848
17849 case CO_YIELD_EXPR:
17850 stmt = finish_co_yield_expr (input_location,
17851 RECUR (TREE_OPERAND (t, 0)));
17852 RETURN (stmt);
17853 break;
17854
17855 case CO_AWAIT_EXPR:
17856 stmt = finish_co_await_expr (input_location,
17857 RECUR (TREE_OPERAND (t, 0)));
17858 RETURN (stmt);
17859 break;
17860
17861 case EXPR_STMT:
17862 tmp = RECUR (EXPR_STMT_EXPR (t));
17863 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17864 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17865 else
17866 finish_expr_stmt (tmp);
17867 break;
17868
17869 case USING_STMT:
17870 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17871 break;
17872
17873 case DECL_EXPR:
17874 {
17875 tree decl, pattern_decl;
17876 tree init;
17877
17878 pattern_decl = decl = DECL_EXPR_DECL (t);
17879 if (TREE_CODE (decl) == LABEL_DECL)
17880 finish_label_decl (DECL_NAME (decl));
17881 else if (TREE_CODE (decl) == USING_DECL)
17882 {
17883 tree scope = USING_DECL_SCOPE (decl);
17884 tree name = DECL_NAME (decl);
17885
17886 scope = tsubst (scope, args, complain, in_decl);
17887 finish_nonmember_using_decl (scope, name);
17888 }
17889 else if (is_capture_proxy (decl)
17890 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17891 {
17892 /* We're in tsubst_lambda_expr, we've already inserted a new
17893 capture proxy, so look it up and register it. */
17894 tree inst;
17895 if (!DECL_PACK_P (decl))
17896 {
17897 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17898 /*nonclass*/1, /*block_p=*/true,
17899 /*ns_only*/0, LOOKUP_HIDDEN);
17900 gcc_assert (inst != decl && is_capture_proxy (inst));
17901 }
17902 else if (is_normal_capture_proxy (decl))
17903 {
17904 inst = (retrieve_local_specialization
17905 (DECL_CAPTURED_VARIABLE (decl)));
17906 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
17907 || DECL_PACK_P (inst));
17908 }
17909 else
17910 inst = lookup_init_capture_pack (decl);
17911
17912 register_local_specialization (inst, decl);
17913 break;
17914 }
17915 else if (DECL_PRETTY_FUNCTION_P (decl))
17916 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17917 DECL_NAME (decl),
17918 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17919 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17920 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17921 /* Don't copy the old closure; we'll create a new one in
17922 tsubst_lambda_expr. */
17923 break;
17924 else
17925 {
17926 init = DECL_INITIAL (decl);
17927 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17928 for local variables, so save if DECL was declared constinit. */
17929 const bool constinit_p
17930 = (VAR_P (decl)
17931 && DECL_LANG_SPECIFIC (decl)
17932 && DECL_TEMPLATE_INFO (decl)
17933 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17934 decl = tsubst (decl, args, complain, in_decl);
17935 if (decl != error_mark_node)
17936 {
17937 /* By marking the declaration as instantiated, we avoid
17938 trying to instantiate it. Since instantiate_decl can't
17939 handle local variables, and since we've already done
17940 all that needs to be done, that's the right thing to
17941 do. */
17942 if (VAR_P (decl))
17943 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17944 if (VAR_P (decl) && !DECL_NAME (decl)
17945 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17946 /* Anonymous aggregates are a special case. */
17947 finish_anon_union (decl);
17948 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17949 {
17950 DECL_CONTEXT (decl) = current_function_decl;
17951 if (DECL_NAME (decl) == this_identifier)
17952 {
17953 tree lam = DECL_CONTEXT (current_function_decl);
17954 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17955 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17956 }
17957 insert_capture_proxy (decl);
17958 }
17959 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17960 /* We already did a pushtag. */;
17961 else if (TREE_CODE (decl) == FUNCTION_DECL
17962 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17963 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17964 {
17965 DECL_CONTEXT (decl) = NULL_TREE;
17966 pushdecl (decl);
17967 DECL_CONTEXT (decl) = current_function_decl;
17968 cp_check_omp_declare_reduction (decl);
17969 }
17970 else
17971 {
17972 bool const_init = false;
17973 unsigned int cnt = 0;
17974 tree first = NULL_TREE, ndecl = error_mark_node;
17975 maybe_push_decl (decl);
17976
17977 if (VAR_P (decl)
17978 && DECL_DECOMPOSITION_P (decl)
17979 && TREE_TYPE (pattern_decl) != error_mark_node)
17980 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17981 complain, in_decl, &first,
17982 &cnt);
17983
17984 init = tsubst_init (init, decl, args, complain, in_decl);
17985
17986 if (VAR_P (decl))
17987 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17988 (pattern_decl));
17989
17990 if (ndecl != error_mark_node)
17991 cp_maybe_mangle_decomp (ndecl, first, cnt);
17992
17993 cp_finish_decl (decl, init, const_init, NULL_TREE,
17994 constinit_p ? LOOKUP_CONSTINIT : 0);
17995
17996 if (ndecl != error_mark_node)
17997 cp_finish_decomp (ndecl, first, cnt);
17998 }
17999 }
18000 }
18001
18002 break;
18003 }
18004
18005 case FOR_STMT:
18006 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18007 RECUR (FOR_INIT_STMT (t));
18008 finish_init_stmt (stmt);
18009 tmp = RECUR (FOR_COND (t));
18010 finish_for_cond (tmp, stmt, false, 0);
18011 tmp = RECUR (FOR_EXPR (t));
18012 finish_for_expr (tmp, stmt);
18013 {
18014 bool prev = note_iteration_stmt_body_start ();
18015 RECUR (FOR_BODY (t));
18016 note_iteration_stmt_body_end (prev);
18017 }
18018 finish_for_stmt (stmt);
18019 break;
18020
18021 case RANGE_FOR_STMT:
18022 {
18023 /* Construct another range_for, if this is not a final
18024 substitution (for inside a generic lambda of a
18025 template). Otherwise convert to a regular for. */
18026 tree decl, expr;
18027 stmt = (processing_template_decl
18028 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18029 : begin_for_stmt (NULL_TREE, NULL_TREE));
18030 RECUR (RANGE_FOR_INIT_STMT (t));
18031 decl = RANGE_FOR_DECL (t);
18032 decl = tsubst (decl, args, complain, in_decl);
18033 maybe_push_decl (decl);
18034 expr = RECUR (RANGE_FOR_EXPR (t));
18035
18036 tree decomp_first = NULL_TREE;
18037 unsigned decomp_cnt = 0;
18038 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18039 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18040 complain, in_decl,
18041 &decomp_first, &decomp_cnt);
18042
18043 if (processing_template_decl)
18044 {
18045 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18046 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18047 finish_range_for_decl (stmt, decl, expr);
18048 if (decomp_first && decl != error_mark_node)
18049 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18050 }
18051 else
18052 {
18053 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18054 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18055 stmt = cp_convert_range_for (stmt, decl, expr,
18056 decomp_first, decomp_cnt,
18057 RANGE_FOR_IVDEP (t), unroll);
18058 }
18059
18060 bool prev = note_iteration_stmt_body_start ();
18061 RECUR (RANGE_FOR_BODY (t));
18062 note_iteration_stmt_body_end (prev);
18063 finish_for_stmt (stmt);
18064 }
18065 break;
18066
18067 case WHILE_STMT:
18068 stmt = begin_while_stmt ();
18069 tmp = RECUR (WHILE_COND (t));
18070 finish_while_stmt_cond (tmp, stmt, false, 0);
18071 {
18072 bool prev = note_iteration_stmt_body_start ();
18073 RECUR (WHILE_BODY (t));
18074 note_iteration_stmt_body_end (prev);
18075 }
18076 finish_while_stmt (stmt);
18077 break;
18078
18079 case DO_STMT:
18080 stmt = begin_do_stmt ();
18081 {
18082 bool prev = note_iteration_stmt_body_start ();
18083 RECUR (DO_BODY (t));
18084 note_iteration_stmt_body_end (prev);
18085 }
18086 finish_do_body (stmt);
18087 tmp = RECUR (DO_COND (t));
18088 finish_do_stmt (tmp, stmt, false, 0);
18089 break;
18090
18091 case IF_STMT:
18092 stmt = begin_if_stmt ();
18093 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18094 if (IF_STMT_CONSTEXPR_P (t))
18095 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18096 tmp = RECUR (IF_COND (t));
18097 tmp = finish_if_stmt_cond (tmp, stmt);
18098 if (IF_STMT_CONSTEXPR_P (t)
18099 && instantiation_dependent_expression_p (tmp))
18100 {
18101 /* We're partially instantiating a generic lambda, but the condition
18102 of the constexpr if is still dependent. Don't substitute into the
18103 branches now, just remember the template arguments. */
18104 do_poplevel (IF_SCOPE (stmt));
18105 IF_COND (stmt) = IF_COND (t);
18106 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18107 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18108 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18109 add_stmt (stmt);
18110 break;
18111 }
18112 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18113 /* Don't instantiate the THEN_CLAUSE. */;
18114 else
18115 {
18116 tree folded = fold_non_dependent_expr (tmp, complain);
18117 bool inhibit = integer_zerop (folded);
18118 if (inhibit)
18119 ++c_inhibit_evaluation_warnings;
18120 RECUR (THEN_CLAUSE (t));
18121 if (inhibit)
18122 --c_inhibit_evaluation_warnings;
18123 }
18124 finish_then_clause (stmt);
18125
18126 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18127 /* Don't instantiate the ELSE_CLAUSE. */;
18128 else if (ELSE_CLAUSE (t))
18129 {
18130 tree folded = fold_non_dependent_expr (tmp, complain);
18131 bool inhibit = integer_nonzerop (folded);
18132 begin_else_clause (stmt);
18133 if (inhibit)
18134 ++c_inhibit_evaluation_warnings;
18135 RECUR (ELSE_CLAUSE (t));
18136 if (inhibit)
18137 --c_inhibit_evaluation_warnings;
18138 finish_else_clause (stmt);
18139 }
18140
18141 finish_if_stmt (stmt);
18142 break;
18143
18144 case BIND_EXPR:
18145 if (BIND_EXPR_BODY_BLOCK (t))
18146 stmt = begin_function_body ();
18147 else
18148 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18149 ? BCS_TRY_BLOCK : 0);
18150
18151 RECUR (BIND_EXPR_BODY (t));
18152
18153 if (BIND_EXPR_BODY_BLOCK (t))
18154 finish_function_body (stmt);
18155 else
18156 finish_compound_stmt (stmt);
18157 break;
18158
18159 case BREAK_STMT:
18160 finish_break_stmt ();
18161 break;
18162
18163 case CONTINUE_STMT:
18164 finish_continue_stmt ();
18165 break;
18166
18167 case SWITCH_STMT:
18168 stmt = begin_switch_stmt ();
18169 tmp = RECUR (SWITCH_STMT_COND (t));
18170 finish_switch_cond (tmp, stmt);
18171 RECUR (SWITCH_STMT_BODY (t));
18172 finish_switch_stmt (stmt);
18173 break;
18174
18175 case CASE_LABEL_EXPR:
18176 {
18177 tree decl = CASE_LABEL (t);
18178 tree low = RECUR (CASE_LOW (t));
18179 tree high = RECUR (CASE_HIGH (t));
18180 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18181 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18182 {
18183 tree label = CASE_LABEL (l);
18184 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18185 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18186 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18187 }
18188 }
18189 break;
18190
18191 case LABEL_EXPR:
18192 {
18193 tree decl = LABEL_EXPR_LABEL (t);
18194 tree label;
18195
18196 label = finish_label_stmt (DECL_NAME (decl));
18197 if (TREE_CODE (label) == LABEL_DECL)
18198 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18199 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18200 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18201 }
18202 break;
18203
18204 case GOTO_EXPR:
18205 tmp = GOTO_DESTINATION (t);
18206 if (TREE_CODE (tmp) != LABEL_DECL)
18207 /* Computed goto's must be tsubst'd into. On the other hand,
18208 non-computed gotos must not be; the identifier in question
18209 will have no binding. */
18210 tmp = RECUR (tmp);
18211 else
18212 tmp = DECL_NAME (tmp);
18213 finish_goto_stmt (tmp);
18214 break;
18215
18216 case ASM_EXPR:
18217 {
18218 tree string = RECUR (ASM_STRING (t));
18219 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18220 complain, in_decl);
18221 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18222 complain, in_decl);
18223 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18224 complain, in_decl);
18225 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18226 complain, in_decl);
18227 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18228 outputs, inputs, clobbers, labels,
18229 ASM_INLINE_P (t));
18230 tree asm_expr = tmp;
18231 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18232 asm_expr = TREE_OPERAND (asm_expr, 0);
18233 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18234 }
18235 break;
18236
18237 case TRY_BLOCK:
18238 if (CLEANUP_P (t))
18239 {
18240 stmt = begin_try_block ();
18241 RECUR (TRY_STMTS (t));
18242 finish_cleanup_try_block (stmt);
18243 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18244 }
18245 else
18246 {
18247 tree compound_stmt = NULL_TREE;
18248
18249 if (FN_TRY_BLOCK_P (t))
18250 stmt = begin_function_try_block (&compound_stmt);
18251 else
18252 stmt = begin_try_block ();
18253
18254 RECUR (TRY_STMTS (t));
18255
18256 if (FN_TRY_BLOCK_P (t))
18257 finish_function_try_block (stmt);
18258 else
18259 finish_try_block (stmt);
18260
18261 RECUR (TRY_HANDLERS (t));
18262 if (FN_TRY_BLOCK_P (t))
18263 finish_function_handler_sequence (stmt, compound_stmt);
18264 else
18265 finish_handler_sequence (stmt);
18266 }
18267 break;
18268
18269 case HANDLER:
18270 {
18271 tree decl = HANDLER_PARMS (t);
18272
18273 if (decl)
18274 {
18275 decl = tsubst (decl, args, complain, in_decl);
18276 /* Prevent instantiate_decl from trying to instantiate
18277 this variable. We've already done all that needs to be
18278 done. */
18279 if (decl != error_mark_node)
18280 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18281 }
18282 stmt = begin_handler ();
18283 finish_handler_parms (decl, stmt);
18284 RECUR (HANDLER_BODY (t));
18285 finish_handler (stmt);
18286 }
18287 break;
18288
18289 case TAG_DEFN:
18290 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18291 if (CLASS_TYPE_P (tmp))
18292 {
18293 /* Local classes are not independent templates; they are
18294 instantiated along with their containing function. And this
18295 way we don't have to deal with pushing out of one local class
18296 to instantiate a member of another local class. */
18297 /* Closures are handled by the LAMBDA_EXPR. */
18298 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18299 complete_type (tmp);
18300 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18301 if ((VAR_P (fld)
18302 || (TREE_CODE (fld) == FUNCTION_DECL
18303 && !DECL_ARTIFICIAL (fld)))
18304 && DECL_TEMPLATE_INSTANTIATION (fld))
18305 instantiate_decl (fld, /*defer_ok=*/false,
18306 /*expl_inst_class=*/false);
18307 }
18308 break;
18309
18310 case STATIC_ASSERT:
18311 {
18312 tree condition;
18313
18314 ++c_inhibit_evaluation_warnings;
18315 condition =
18316 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18317 args,
18318 complain, in_decl,
18319 /*integral_constant_expression_p=*/true);
18320 --c_inhibit_evaluation_warnings;
18321
18322 finish_static_assert (condition,
18323 STATIC_ASSERT_MESSAGE (t),
18324 STATIC_ASSERT_SOURCE_LOCATION (t),
18325 /*member_p=*/false);
18326 }
18327 break;
18328
18329 case OACC_KERNELS:
18330 case OACC_PARALLEL:
18331 case OACC_SERIAL:
18332 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18333 in_decl);
18334 stmt = begin_omp_parallel ();
18335 RECUR (OMP_BODY (t));
18336 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18337 break;
18338
18339 case OMP_PARALLEL:
18340 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18341 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18342 complain, in_decl);
18343 if (OMP_PARALLEL_COMBINED (t))
18344 omp_parallel_combined_clauses = &tmp;
18345 stmt = begin_omp_parallel ();
18346 RECUR (OMP_PARALLEL_BODY (t));
18347 gcc_assert (omp_parallel_combined_clauses == NULL);
18348 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18349 = OMP_PARALLEL_COMBINED (t);
18350 pop_omp_privatization_clauses (r);
18351 break;
18352
18353 case OMP_TASK:
18354 if (OMP_TASK_BODY (t) == NULL_TREE)
18355 {
18356 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18357 complain, in_decl);
18358 t = copy_node (t);
18359 OMP_TASK_CLAUSES (t) = tmp;
18360 add_stmt (t);
18361 break;
18362 }
18363 r = push_omp_privatization_clauses (false);
18364 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18365 complain, in_decl);
18366 stmt = begin_omp_task ();
18367 RECUR (OMP_TASK_BODY (t));
18368 finish_omp_task (tmp, stmt);
18369 pop_omp_privatization_clauses (r);
18370 break;
18371
18372 case OMP_FOR:
18373 case OMP_LOOP:
18374 case OMP_SIMD:
18375 case OMP_DISTRIBUTE:
18376 case OMP_TASKLOOP:
18377 case OACC_LOOP:
18378 {
18379 tree clauses, body, pre_body;
18380 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18381 tree orig_declv = NULL_TREE;
18382 tree incrv = NULL_TREE;
18383 enum c_omp_region_type ort = C_ORT_OMP;
18384 bool any_range_for = false;
18385 int i;
18386
18387 if (TREE_CODE (t) == OACC_LOOP)
18388 ort = C_ORT_ACC;
18389
18390 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18391 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18392 in_decl);
18393 if (OMP_FOR_INIT (t) != NULL_TREE)
18394 {
18395 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18396 if (OMP_FOR_ORIG_DECLS (t))
18397 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18398 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18399 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18400 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18401 }
18402
18403 keep_next_level (true);
18404 stmt = begin_omp_structured_block ();
18405
18406 pre_body = push_stmt_list ();
18407 RECUR (OMP_FOR_PRE_BODY (t));
18408 pre_body = pop_stmt_list (pre_body);
18409
18410 if (OMP_FOR_INIT (t) != NULL_TREE)
18411 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18412 any_range_for
18413 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18414 condv, incrv, &clauses, args,
18415 complain, in_decl,
18416 integral_constant_expression_p);
18417 omp_parallel_combined_clauses = NULL;
18418
18419 if (any_range_for)
18420 {
18421 gcc_assert (orig_declv);
18422 body = begin_omp_structured_block ();
18423 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18424 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18425 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18426 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18427 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18428 TREE_VEC_ELT (declv, i));
18429 }
18430 else
18431 body = push_stmt_list ();
18432 RECUR (OMP_FOR_BODY (t));
18433 if (any_range_for)
18434 body = finish_omp_structured_block (body);
18435 else
18436 body = pop_stmt_list (body);
18437
18438 if (OMP_FOR_INIT (t) != NULL_TREE)
18439 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18440 orig_declv, initv, condv, incrv, body, pre_body,
18441 NULL, clauses);
18442 else
18443 {
18444 t = make_node (TREE_CODE (t));
18445 TREE_TYPE (t) = void_type_node;
18446 OMP_FOR_BODY (t) = body;
18447 OMP_FOR_PRE_BODY (t) = pre_body;
18448 OMP_FOR_CLAUSES (t) = clauses;
18449 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18450 add_stmt (t);
18451 }
18452
18453 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18454 t));
18455 pop_omp_privatization_clauses (r);
18456 }
18457 break;
18458
18459 case OMP_SECTIONS:
18460 omp_parallel_combined_clauses = NULL;
18461 /* FALLTHRU */
18462 case OMP_SINGLE:
18463 case OMP_TEAMS:
18464 case OMP_CRITICAL:
18465 case OMP_TASKGROUP:
18466 case OMP_SCAN:
18467 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18468 && OMP_TEAMS_COMBINED (t));
18469 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18470 in_decl);
18471 if (TREE_CODE (t) == OMP_TEAMS)
18472 {
18473 keep_next_level (true);
18474 stmt = begin_omp_structured_block ();
18475 RECUR (OMP_BODY (t));
18476 stmt = finish_omp_structured_block (stmt);
18477 }
18478 else
18479 {
18480 stmt = push_stmt_list ();
18481 RECUR (OMP_BODY (t));
18482 stmt = pop_stmt_list (stmt);
18483 }
18484
18485 t = copy_node (t);
18486 OMP_BODY (t) = stmt;
18487 OMP_CLAUSES (t) = tmp;
18488 add_stmt (t);
18489 pop_omp_privatization_clauses (r);
18490 break;
18491
18492 case OMP_DEPOBJ:
18493 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18494 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18495 {
18496 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18497 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18498 {
18499 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18500 args, complain, in_decl);
18501 if (tmp == NULL_TREE)
18502 tmp = error_mark_node;
18503 }
18504 else
18505 {
18506 kind = (enum omp_clause_depend_kind)
18507 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18508 tmp = NULL_TREE;
18509 }
18510 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18511 }
18512 else
18513 finish_omp_depobj (EXPR_LOCATION (t), r,
18514 OMP_CLAUSE_DEPEND_SOURCE,
18515 OMP_DEPOBJ_CLAUSES (t));
18516 break;
18517
18518 case OACC_DATA:
18519 case OMP_TARGET_DATA:
18520 case OMP_TARGET:
18521 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18522 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18523 in_decl);
18524 keep_next_level (true);
18525 stmt = begin_omp_structured_block ();
18526
18527 RECUR (OMP_BODY (t));
18528 stmt = finish_omp_structured_block (stmt);
18529
18530 t = copy_node (t);
18531 OMP_BODY (t) = stmt;
18532 OMP_CLAUSES (t) = tmp;
18533 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18534 {
18535 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18536 if (teams)
18537 {
18538 /* For combined target teams, ensure the num_teams and
18539 thread_limit clause expressions are evaluated on the host,
18540 before entering the target construct. */
18541 tree c;
18542 for (c = OMP_TEAMS_CLAUSES (teams);
18543 c; c = OMP_CLAUSE_CHAIN (c))
18544 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18545 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18546 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18547 {
18548 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18549 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18550 if (expr == error_mark_node)
18551 continue;
18552 tmp = TARGET_EXPR_SLOT (expr);
18553 add_stmt (expr);
18554 OMP_CLAUSE_OPERAND (c, 0) = expr;
18555 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18556 OMP_CLAUSE_FIRSTPRIVATE);
18557 OMP_CLAUSE_DECL (tc) = tmp;
18558 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18559 OMP_TARGET_CLAUSES (t) = tc;
18560 }
18561 }
18562 }
18563 add_stmt (t);
18564 break;
18565
18566 case OACC_DECLARE:
18567 t = copy_node (t);
18568 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18569 complain, in_decl);
18570 OACC_DECLARE_CLAUSES (t) = tmp;
18571 add_stmt (t);
18572 break;
18573
18574 case OMP_TARGET_UPDATE:
18575 case OMP_TARGET_ENTER_DATA:
18576 case OMP_TARGET_EXIT_DATA:
18577 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18578 complain, in_decl);
18579 t = copy_node (t);
18580 OMP_STANDALONE_CLAUSES (t) = tmp;
18581 add_stmt (t);
18582 break;
18583
18584 case OACC_ENTER_DATA:
18585 case OACC_EXIT_DATA:
18586 case OACC_UPDATE:
18587 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18588 complain, in_decl);
18589 t = copy_node (t);
18590 OMP_STANDALONE_CLAUSES (t) = tmp;
18591 add_stmt (t);
18592 break;
18593
18594 case OMP_ORDERED:
18595 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18596 complain, in_decl);
18597 stmt = push_stmt_list ();
18598 RECUR (OMP_BODY (t));
18599 stmt = pop_stmt_list (stmt);
18600
18601 t = copy_node (t);
18602 OMP_BODY (t) = stmt;
18603 OMP_ORDERED_CLAUSES (t) = tmp;
18604 add_stmt (t);
18605 break;
18606
18607 case OMP_MASTER:
18608 omp_parallel_combined_clauses = NULL;
18609 /* FALLTHRU */
18610 case OMP_SECTION:
18611 stmt = push_stmt_list ();
18612 RECUR (OMP_BODY (t));
18613 stmt = pop_stmt_list (stmt);
18614
18615 t = copy_node (t);
18616 OMP_BODY (t) = stmt;
18617 add_stmt (t);
18618 break;
18619
18620 case OMP_ATOMIC:
18621 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18622 tmp = NULL_TREE;
18623 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18624 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18625 complain, in_decl);
18626 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18627 {
18628 tree op1 = TREE_OPERAND (t, 1);
18629 tree rhs1 = NULL_TREE;
18630 tree lhs, rhs;
18631 if (TREE_CODE (op1) == COMPOUND_EXPR)
18632 {
18633 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18634 op1 = TREE_OPERAND (op1, 1);
18635 }
18636 lhs = RECUR (TREE_OPERAND (op1, 0));
18637 rhs = RECUR (TREE_OPERAND (op1, 1));
18638 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18639 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18640 OMP_ATOMIC_MEMORY_ORDER (t));
18641 }
18642 else
18643 {
18644 tree op1 = TREE_OPERAND (t, 1);
18645 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18646 tree rhs1 = NULL_TREE;
18647 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18648 enum tree_code opcode = NOP_EXPR;
18649 if (code == OMP_ATOMIC_READ)
18650 {
18651 v = RECUR (TREE_OPERAND (op1, 0));
18652 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18653 }
18654 else if (code == OMP_ATOMIC_CAPTURE_OLD
18655 || code == OMP_ATOMIC_CAPTURE_NEW)
18656 {
18657 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18658 v = RECUR (TREE_OPERAND (op1, 0));
18659 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18660 if (TREE_CODE (op11) == COMPOUND_EXPR)
18661 {
18662 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18663 op11 = TREE_OPERAND (op11, 1);
18664 }
18665 lhs = RECUR (TREE_OPERAND (op11, 0));
18666 rhs = RECUR (TREE_OPERAND (op11, 1));
18667 opcode = TREE_CODE (op11);
18668 if (opcode == MODIFY_EXPR)
18669 opcode = NOP_EXPR;
18670 }
18671 else
18672 {
18673 code = OMP_ATOMIC;
18674 lhs = RECUR (TREE_OPERAND (op1, 0));
18675 rhs = RECUR (TREE_OPERAND (op1, 1));
18676 }
18677 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18678 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18679 }
18680 break;
18681
18682 case TRANSACTION_EXPR:
18683 {
18684 int flags = 0;
18685 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18686 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18687
18688 if (TRANSACTION_EXPR_IS_STMT (t))
18689 {
18690 tree body = TRANSACTION_EXPR_BODY (t);
18691 tree noex = NULL_TREE;
18692 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18693 {
18694 noex = MUST_NOT_THROW_COND (body);
18695 if (noex == NULL_TREE)
18696 noex = boolean_true_node;
18697 body = TREE_OPERAND (body, 0);
18698 }
18699 stmt = begin_transaction_stmt (input_location, NULL, flags);
18700 RECUR (body);
18701 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18702 }
18703 else
18704 {
18705 stmt = build_transaction_expr (EXPR_LOCATION (t),
18706 RECUR (TRANSACTION_EXPR_BODY (t)),
18707 flags, NULL_TREE);
18708 RETURN (stmt);
18709 }
18710 }
18711 break;
18712
18713 case MUST_NOT_THROW_EXPR:
18714 {
18715 tree op0 = RECUR (TREE_OPERAND (t, 0));
18716 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18717 RETURN (build_must_not_throw_expr (op0, cond));
18718 }
18719
18720 case EXPR_PACK_EXPANSION:
18721 error ("invalid use of pack expansion expression");
18722 RETURN (error_mark_node);
18723
18724 case NONTYPE_ARGUMENT_PACK:
18725 error ("use %<...%> to expand argument pack");
18726 RETURN (error_mark_node);
18727
18728 case COMPOUND_EXPR:
18729 tmp = RECUR (TREE_OPERAND (t, 0));
18730 if (tmp == NULL_TREE)
18731 /* If the first operand was a statement, we're done with it. */
18732 RETURN (RECUR (TREE_OPERAND (t, 1)));
18733 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18734 RECUR (TREE_OPERAND (t, 1)),
18735 complain));
18736
18737 case ANNOTATE_EXPR:
18738 tmp = RECUR (TREE_OPERAND (t, 0));
18739 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18740 TREE_TYPE (tmp), tmp,
18741 RECUR (TREE_OPERAND (t, 1)),
18742 RECUR (TREE_OPERAND (t, 2))));
18743
18744 case PREDICT_EXPR:
18745 RETURN (add_stmt (copy_node (t)));
18746
18747 default:
18748 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18749
18750 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18751 /*function_p=*/false,
18752 integral_constant_expression_p));
18753 }
18754
18755 RETURN (NULL_TREE);
18756 out:
18757 input_location = loc;
18758 return r;
18759 #undef RECUR
18760 #undef RETURN
18761 }
18762
18763 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18764 function. For description of the body see comment above
18765 cp_parser_omp_declare_reduction_exprs. */
18766
18767 static void
18768 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18769 {
18770 if (t == NULL_TREE || t == error_mark_node)
18771 return;
18772
18773 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
18774
18775 tree_stmt_iterator tsi;
18776 int i;
18777 tree stmts[7];
18778 memset (stmts, 0, sizeof stmts);
18779 for (i = 0, tsi = tsi_start (t);
18780 i < 7 && !tsi_end_p (tsi);
18781 i++, tsi_next (&tsi))
18782 stmts[i] = tsi_stmt (tsi);
18783 gcc_assert (tsi_end_p (tsi));
18784
18785 if (i >= 3)
18786 {
18787 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18788 && TREE_CODE (stmts[1]) == DECL_EXPR);
18789 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18790 args, complain, in_decl);
18791 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18792 args, complain, in_decl);
18793 DECL_CONTEXT (omp_out) = current_function_decl;
18794 DECL_CONTEXT (omp_in) = current_function_decl;
18795 keep_next_level (true);
18796 tree block = begin_omp_structured_block ();
18797 tsubst_expr (stmts[2], args, complain, in_decl, false);
18798 block = finish_omp_structured_block (block);
18799 block = maybe_cleanup_point_expr_void (block);
18800 add_decl_expr (omp_out);
18801 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18802 TREE_NO_WARNING (omp_out) = 1;
18803 add_decl_expr (omp_in);
18804 finish_expr_stmt (block);
18805 }
18806 if (i >= 6)
18807 {
18808 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18809 && TREE_CODE (stmts[4]) == DECL_EXPR);
18810 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18811 args, complain, in_decl);
18812 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18813 args, complain, in_decl);
18814 DECL_CONTEXT (omp_priv) = current_function_decl;
18815 DECL_CONTEXT (omp_orig) = current_function_decl;
18816 keep_next_level (true);
18817 tree block = begin_omp_structured_block ();
18818 tsubst_expr (stmts[5], args, complain, in_decl, false);
18819 block = finish_omp_structured_block (block);
18820 block = maybe_cleanup_point_expr_void (block);
18821 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18822 add_decl_expr (omp_priv);
18823 add_decl_expr (omp_orig);
18824 finish_expr_stmt (block);
18825 if (i == 7)
18826 add_decl_expr (omp_orig);
18827 }
18828 }
18829
18830 /* T is a postfix-expression that is not being used in a function
18831 call. Return the substituted version of T. */
18832
18833 static tree
18834 tsubst_non_call_postfix_expression (tree t, tree args,
18835 tsubst_flags_t complain,
18836 tree in_decl)
18837 {
18838 if (TREE_CODE (t) == SCOPE_REF)
18839 t = tsubst_qualified_id (t, args, complain, in_decl,
18840 /*done=*/false, /*address_p=*/false);
18841 else
18842 t = tsubst_copy_and_build (t, args, complain, in_decl,
18843 /*function_p=*/false,
18844 /*integral_constant_expression_p=*/false);
18845
18846 return t;
18847 }
18848
18849 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18850 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18851 dependent init-capture. */
18852
18853 static void
18854 prepend_one_capture (tree field, tree init, tree &list,
18855 tsubst_flags_t complain)
18856 {
18857 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18858 {
18859 tree type = NULL_TREE;
18860 if (!init)
18861 {
18862 if (complain & tf_error)
18863 error ("empty initializer in lambda init-capture");
18864 init = error_mark_node;
18865 }
18866 else if (TREE_CODE (init) == TREE_LIST)
18867 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18868 if (!type)
18869 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18870 TREE_TYPE (field) = type;
18871 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18872 }
18873 list = tree_cons (field, init, list);
18874 }
18875
18876 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18877 instantiation context. Instantiating a pack expansion containing a lambda
18878 might result in multiple lambdas all based on the same lambda in the
18879 template. */
18880
18881 tree
18882 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18883 {
18884 tree oldfn = lambda_function (t);
18885 in_decl = oldfn;
18886
18887 tree r = build_lambda_expr ();
18888
18889 LAMBDA_EXPR_LOCATION (r)
18890 = LAMBDA_EXPR_LOCATION (t);
18891 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18892 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18893 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18894 LAMBDA_EXPR_INSTANTIATED (r) = true;
18895
18896 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18897 /* A lambda in a default argument outside a class gets no
18898 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18899 tsubst_default_argument calls start_lambda_scope, so we need to
18900 specifically ignore it here, and use the global scope. */
18901 record_null_lambda_scope (r);
18902 else
18903 record_lambda_scope (r);
18904
18905 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18906 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18907
18908 vec<tree,va_gc>* field_packs = NULL;
18909
18910 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18911 cap = TREE_CHAIN (cap))
18912 {
18913 tree ofield = TREE_PURPOSE (cap);
18914 tree init = TREE_VALUE (cap);
18915 if (PACK_EXPANSION_P (init))
18916 init = tsubst_pack_expansion (init, args, complain, in_decl);
18917 else
18918 init = tsubst_copy_and_build (init, args, complain, in_decl,
18919 /*fn*/false, /*constexpr*/false);
18920
18921 if (init == error_mark_node)
18922 return error_mark_node;
18923
18924 if (init && TREE_CODE (init) == TREE_LIST)
18925 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18926
18927 if (!processing_template_decl
18928 && init && TREE_CODE (init) != TREE_VEC
18929 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
18930 {
18931 /* For a VLA, simply tsubsting the field type won't work, we need to
18932 go through add_capture again. XXX do we want to do this for all
18933 captures? */
18934 tree name = (get_identifier
18935 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
18936 tree ftype = TREE_TYPE (ofield);
18937 bool by_ref = (TYPE_REF_P (ftype)
18938 || (TREE_CODE (ftype) == DECLTYPE_TYPE
18939 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
18940 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
18941 continue;
18942 }
18943
18944 if (PACK_EXPANSION_P (ofield))
18945 ofield = PACK_EXPANSION_PATTERN (ofield);
18946 tree field = tsubst_decl (ofield, args, complain);
18947
18948 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18949 {
18950 /* Remember these for when we've pushed local_specializations. */
18951 vec_safe_push (field_packs, ofield);
18952 vec_safe_push (field_packs, field);
18953 }
18954
18955 if (field == error_mark_node)
18956 return error_mark_node;
18957
18958 if (TREE_CODE (field) == TREE_VEC)
18959 {
18960 int len = TREE_VEC_LENGTH (field);
18961 gcc_assert (TREE_CODE (init) == TREE_VEC
18962 && TREE_VEC_LENGTH (init) == len);
18963 for (int i = 0; i < len; ++i)
18964 prepend_one_capture (TREE_VEC_ELT (field, i),
18965 TREE_VEC_ELT (init, i),
18966 LAMBDA_EXPR_CAPTURE_LIST (r),
18967 complain);
18968 }
18969 else
18970 {
18971 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
18972 complain);
18973
18974 if (id_equal (DECL_NAME (field), "__this"))
18975 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18976 }
18977 }
18978
18979 tree type = begin_lambda_type (r);
18980 if (type == error_mark_node)
18981 return error_mark_node;
18982
18983 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18984 determine_visibility (TYPE_NAME (type));
18985
18986 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18987
18988 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18989 ? DECL_TI_TEMPLATE (oldfn)
18990 : NULL_TREE);
18991
18992 tree fntype = static_fn_type (oldfn);
18993 if (oldtmpl)
18994 ++processing_template_decl;
18995 fntype = tsubst (fntype, args, complain, in_decl);
18996 if (oldtmpl)
18997 --processing_template_decl;
18998
18999 if (fntype == error_mark_node)
19000 r = error_mark_node;
19001 else
19002 {
19003 /* The body of a lambda-expression is not a subexpression of the
19004 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19005 which would be skipped if cp_unevaluated_operand. */
19006 cp_evaluated ev;
19007
19008 /* Fix the type of 'this'. */
19009 fntype = build_memfn_type (fntype, type,
19010 type_memfn_quals (fntype),
19011 type_memfn_rqual (fntype));
19012 tree fn, tmpl;
19013 if (oldtmpl)
19014 {
19015 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19016 if (tmpl == error_mark_node)
19017 {
19018 r = error_mark_node;
19019 goto out;
19020 }
19021 fn = DECL_TEMPLATE_RESULT (tmpl);
19022 finish_member_declaration (tmpl);
19023 }
19024 else
19025 {
19026 tmpl = NULL_TREE;
19027 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19028 if (fn == error_mark_node)
19029 {
19030 r = error_mark_node;
19031 goto out;
19032 }
19033 finish_member_declaration (fn);
19034 }
19035
19036 /* Let finish_function set this. */
19037 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19038
19039 bool nested = cfun;
19040 if (nested)
19041 push_function_context ();
19042 else
19043 /* Still increment function_depth so that we don't GC in the
19044 middle of an expression. */
19045 ++function_depth;
19046
19047 local_specialization_stack s (lss_copy);
19048
19049 tree body = start_lambda_function (fn, r);
19050
19051 /* Now record them for lookup_init_capture_pack. */
19052 int fplen = vec_safe_length (field_packs);
19053 for (int i = 0; i < fplen; )
19054 {
19055 tree pack = (*field_packs)[i++];
19056 tree inst = (*field_packs)[i++];
19057 register_local_specialization (inst, pack);
19058 }
19059 release_tree_vector (field_packs);
19060
19061 register_parameter_specializations (oldfn, fn);
19062
19063 if (oldtmpl)
19064 {
19065 /* We might not partially instantiate some parts of the function, so
19066 copy these flags from the original template. */
19067 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19068 current_function_returns_value = ol->returns_value;
19069 current_function_returns_null = ol->returns_null;
19070 current_function_returns_abnormally = ol->returns_abnormally;
19071 current_function_infinite_loop = ol->infinite_loop;
19072 }
19073
19074 /* [temp.deduct] A lambda-expression appearing in a function type or a
19075 template parameter is not considered part of the immediate context for
19076 the purposes of template argument deduction. */
19077 complain = tf_warning_or_error;
19078
19079 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19080 /*constexpr*/false);
19081
19082 finish_lambda_function (body);
19083
19084 if (nested)
19085 pop_function_context ();
19086 else
19087 --function_depth;
19088
19089 /* The capture list was built up in reverse order; fix that now. */
19090 LAMBDA_EXPR_CAPTURE_LIST (r)
19091 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19092
19093 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19094
19095 maybe_add_lambda_conv_op (type);
19096 }
19097
19098 out:
19099 finish_struct (type, /*attr*/NULL_TREE);
19100
19101 insert_pending_capture_proxies ();
19102
19103 return r;
19104 }
19105
19106 /* Like tsubst but deals with expressions and performs semantic
19107 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19108
19109 tree
19110 tsubst_copy_and_build (tree t,
19111 tree args,
19112 tsubst_flags_t complain,
19113 tree in_decl,
19114 bool function_p,
19115 bool integral_constant_expression_p)
19116 {
19117 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19118 #define RECUR(NODE) \
19119 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19120 /*function_p=*/false, \
19121 integral_constant_expression_p)
19122
19123 tree retval, op1;
19124 location_t save_loc;
19125
19126 if (t == NULL_TREE || t == error_mark_node)
19127 return t;
19128
19129 save_loc = input_location;
19130 if (location_t eloc = cp_expr_location (t))
19131 input_location = eloc;
19132
19133 /* N3276 decltype magic only applies to calls at the top level or on the
19134 right side of a comma. */
19135 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19136 complain &= ~tf_decltype;
19137
19138 switch (TREE_CODE (t))
19139 {
19140 case USING_DECL:
19141 t = DECL_NAME (t);
19142 /* Fall through. */
19143 case IDENTIFIER_NODE:
19144 {
19145 tree decl;
19146 cp_id_kind idk;
19147 bool non_integral_constant_expression_p;
19148 const char *error_msg;
19149
19150 if (IDENTIFIER_CONV_OP_P (t))
19151 {
19152 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19153 t = make_conv_op_name (new_type);
19154 }
19155
19156 /* Look up the name. */
19157 decl = lookup_name (t);
19158
19159 /* By convention, expressions use ERROR_MARK_NODE to indicate
19160 failure, not NULL_TREE. */
19161 if (decl == NULL_TREE)
19162 decl = error_mark_node;
19163
19164 decl = finish_id_expression (t, decl, NULL_TREE,
19165 &idk,
19166 integral_constant_expression_p,
19167 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19168 &non_integral_constant_expression_p,
19169 /*template_p=*/false,
19170 /*done=*/true,
19171 /*address_p=*/false,
19172 /*template_arg_p=*/false,
19173 &error_msg,
19174 input_location);
19175 if (error_msg)
19176 error (error_msg);
19177 if (!function_p && identifier_p (decl))
19178 {
19179 if (complain & tf_error)
19180 unqualified_name_lookup_error (decl);
19181 decl = error_mark_node;
19182 }
19183 RETURN (decl);
19184 }
19185
19186 case TEMPLATE_ID_EXPR:
19187 {
19188 tree object;
19189 tree templ = RECUR (TREE_OPERAND (t, 0));
19190 tree targs = TREE_OPERAND (t, 1);
19191
19192 if (targs)
19193 targs = tsubst_template_args (targs, args, complain, in_decl);
19194 if (targs == error_mark_node)
19195 RETURN (error_mark_node);
19196
19197 if (TREE_CODE (templ) == SCOPE_REF)
19198 {
19199 tree name = TREE_OPERAND (templ, 1);
19200 tree tid = lookup_template_function (name, targs);
19201 TREE_OPERAND (templ, 1) = tid;
19202 RETURN (templ);
19203 }
19204
19205 if (concept_definition_p (templ))
19206 {
19207 tree check = build_concept_check (templ, targs, complain);
19208 if (check == error_mark_node)
19209 RETURN (error_mark_node);
19210
19211 tree id = unpack_concept_check (check);
19212
19213 /* If we built a function concept check, return the underlying
19214 template-id. So we can evaluate it as a function call. */
19215 if (function_concept_p (TREE_OPERAND (id, 0)))
19216 RETURN (id);
19217
19218 RETURN (check);
19219 }
19220
19221 if (variable_template_p (templ))
19222 {
19223 tree r = lookup_and_finish_template_variable (templ, targs,
19224 complain);
19225 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19226 RETURN (r);
19227 }
19228
19229 if (TREE_CODE (templ) == COMPONENT_REF)
19230 {
19231 object = TREE_OPERAND (templ, 0);
19232 templ = TREE_OPERAND (templ, 1);
19233 }
19234 else
19235 object = NULL_TREE;
19236 templ = lookup_template_function (templ, targs);
19237
19238 if (object)
19239 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
19240 object, templ, NULL_TREE));
19241 else
19242 RETURN (baselink_for_fns (templ));
19243 }
19244
19245 case INDIRECT_REF:
19246 {
19247 tree r = RECUR (TREE_OPERAND (t, 0));
19248
19249 if (REFERENCE_REF_P (t))
19250 {
19251 /* A type conversion to reference type will be enclosed in
19252 such an indirect ref, but the substitution of the cast
19253 will have also added such an indirect ref. */
19254 r = convert_from_reference (r);
19255 }
19256 else
19257 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19258 complain|decltype_flag);
19259
19260 if (REF_PARENTHESIZED_P (t))
19261 r = force_paren_expr (r);
19262
19263 RETURN (r);
19264 }
19265
19266 case NOP_EXPR:
19267 {
19268 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19269 tree op0 = RECUR (TREE_OPERAND (t, 0));
19270 RETURN (build_nop (type, op0));
19271 }
19272
19273 case IMPLICIT_CONV_EXPR:
19274 {
19275 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19276 tree expr = RECUR (TREE_OPERAND (t, 0));
19277 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19278 {
19279 retval = copy_node (t);
19280 TREE_TYPE (retval) = type;
19281 TREE_OPERAND (retval, 0) = expr;
19282 RETURN (retval);
19283 }
19284 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19285 /* We'll pass this to convert_nontype_argument again, we don't need
19286 to actually perform any conversion here. */
19287 RETURN (expr);
19288 int flags = LOOKUP_IMPLICIT;
19289 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19290 flags = LOOKUP_NORMAL;
19291 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19292 flags |= LOOKUP_NO_NARROWING;
19293 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19294 flags));
19295 }
19296
19297 case CONVERT_EXPR:
19298 {
19299 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19300 tree op0 = RECUR (TREE_OPERAND (t, 0));
19301 if (op0 == error_mark_node)
19302 RETURN (error_mark_node);
19303 RETURN (build1 (CONVERT_EXPR, type, op0));
19304 }
19305
19306 case CAST_EXPR:
19307 case REINTERPRET_CAST_EXPR:
19308 case CONST_CAST_EXPR:
19309 case DYNAMIC_CAST_EXPR:
19310 case STATIC_CAST_EXPR:
19311 {
19312 tree type;
19313 tree op, r = NULL_TREE;
19314
19315 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19316 if (integral_constant_expression_p
19317 && !cast_valid_in_integral_constant_expression_p (type))
19318 {
19319 if (complain & tf_error)
19320 error ("a cast to a type other than an integral or "
19321 "enumeration type cannot appear in a constant-expression");
19322 RETURN (error_mark_node);
19323 }
19324
19325 op = RECUR (TREE_OPERAND (t, 0));
19326
19327 warning_sentinel s(warn_useless_cast);
19328 warning_sentinel s2(warn_ignored_qualifiers);
19329 switch (TREE_CODE (t))
19330 {
19331 case CAST_EXPR:
19332 r = build_functional_cast (input_location, type, op, complain);
19333 break;
19334 case REINTERPRET_CAST_EXPR:
19335 r = build_reinterpret_cast (input_location, type, op, complain);
19336 break;
19337 case CONST_CAST_EXPR:
19338 r = build_const_cast (input_location, type, op, complain);
19339 break;
19340 case DYNAMIC_CAST_EXPR:
19341 r = build_dynamic_cast (input_location, type, op, complain);
19342 break;
19343 case STATIC_CAST_EXPR:
19344 r = build_static_cast (input_location, type, op, complain);
19345 break;
19346 default:
19347 gcc_unreachable ();
19348 }
19349
19350 RETURN (r);
19351 }
19352
19353 case POSTDECREMENT_EXPR:
19354 case POSTINCREMENT_EXPR:
19355 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19356 args, complain, in_decl);
19357 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19358 complain|decltype_flag));
19359
19360 case PREDECREMENT_EXPR:
19361 case PREINCREMENT_EXPR:
19362 case NEGATE_EXPR:
19363 case BIT_NOT_EXPR:
19364 case ABS_EXPR:
19365 case TRUTH_NOT_EXPR:
19366 case UNARY_PLUS_EXPR: /* Unary + */
19367 case REALPART_EXPR:
19368 case IMAGPART_EXPR:
19369 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19370 RECUR (TREE_OPERAND (t, 0)),
19371 complain|decltype_flag));
19372
19373 case FIX_TRUNC_EXPR:
19374 gcc_unreachable ();
19375
19376 case ADDR_EXPR:
19377 op1 = TREE_OPERAND (t, 0);
19378 if (TREE_CODE (op1) == LABEL_DECL)
19379 RETURN (finish_label_address_expr (DECL_NAME (op1),
19380 EXPR_LOCATION (op1)));
19381 if (TREE_CODE (op1) == SCOPE_REF)
19382 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19383 /*done=*/true, /*address_p=*/true);
19384 else
19385 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19386 in_decl);
19387 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19388 complain|decltype_flag));
19389
19390 case PLUS_EXPR:
19391 case MINUS_EXPR:
19392 case MULT_EXPR:
19393 case TRUNC_DIV_EXPR:
19394 case CEIL_DIV_EXPR:
19395 case FLOOR_DIV_EXPR:
19396 case ROUND_DIV_EXPR:
19397 case EXACT_DIV_EXPR:
19398 case BIT_AND_EXPR:
19399 case BIT_IOR_EXPR:
19400 case BIT_XOR_EXPR:
19401 case TRUNC_MOD_EXPR:
19402 case FLOOR_MOD_EXPR:
19403 case TRUTH_ANDIF_EXPR:
19404 case TRUTH_ORIF_EXPR:
19405 case TRUTH_AND_EXPR:
19406 case TRUTH_OR_EXPR:
19407 case RSHIFT_EXPR:
19408 case LSHIFT_EXPR:
19409 case EQ_EXPR:
19410 case NE_EXPR:
19411 case MAX_EXPR:
19412 case MIN_EXPR:
19413 case LE_EXPR:
19414 case GE_EXPR:
19415 case LT_EXPR:
19416 case GT_EXPR:
19417 case SPACESHIP_EXPR:
19418 case MEMBER_REF:
19419 case DOTSTAR_EXPR:
19420 {
19421 /* If T was type-dependent, suppress warnings that depend on the range
19422 of the types involved. */
19423 bool was_dep = type_dependent_expression_p_push (t);
19424
19425 tree op0 = RECUR (TREE_OPERAND (t, 0));
19426 tree op1 = RECUR (TREE_OPERAND (t, 1));
19427
19428 warning_sentinel s1(warn_type_limits, was_dep);
19429 warning_sentinel s2(warn_div_by_zero, was_dep);
19430 warning_sentinel s3(warn_logical_op, was_dep);
19431 warning_sentinel s4(warn_tautological_compare, was_dep);
19432
19433 tree r = build_x_binary_op
19434 (input_location, TREE_CODE (t),
19435 op0,
19436 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19437 ? ERROR_MARK
19438 : TREE_CODE (TREE_OPERAND (t, 0))),
19439 op1,
19440 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19441 ? ERROR_MARK
19442 : TREE_CODE (TREE_OPERAND (t, 1))),
19443 /*overload=*/NULL,
19444 complain|decltype_flag);
19445 if (EXPR_P (r) && TREE_NO_WARNING (t))
19446 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19447
19448 RETURN (r);
19449 }
19450
19451 case POINTER_PLUS_EXPR:
19452 {
19453 tree op0 = RECUR (TREE_OPERAND (t, 0));
19454 if (op0 == error_mark_node)
19455 RETURN (error_mark_node);
19456 tree op1 = RECUR (TREE_OPERAND (t, 1));
19457 if (op1 == error_mark_node)
19458 RETURN (error_mark_node);
19459 RETURN (fold_build_pointer_plus (op0, op1));
19460 }
19461
19462 case SCOPE_REF:
19463 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19464 /*address_p=*/false));
19465 case ARRAY_REF:
19466 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19467 args, complain, in_decl);
19468 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19469 RECUR (TREE_OPERAND (t, 1)),
19470 complain|decltype_flag));
19471
19472 case SIZEOF_EXPR:
19473 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19474 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19475 RETURN (tsubst_copy (t, args, complain, in_decl));
19476 /* Fall through */
19477
19478 case ALIGNOF_EXPR:
19479 {
19480 tree r;
19481
19482 op1 = TREE_OPERAND (t, 0);
19483 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19484 op1 = TREE_TYPE (op1);
19485 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19486 && ALIGNOF_EXPR_STD_P (t));
19487 if (!args)
19488 {
19489 /* When there are no ARGS, we are trying to evaluate a
19490 non-dependent expression from the parser. Trying to do
19491 the substitutions may not work. */
19492 if (!TYPE_P (op1))
19493 op1 = TREE_TYPE (op1);
19494 }
19495 else
19496 {
19497 ++cp_unevaluated_operand;
19498 ++c_inhibit_evaluation_warnings;
19499 if (TYPE_P (op1))
19500 op1 = tsubst (op1, args, complain, in_decl);
19501 else
19502 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19503 /*function_p=*/false,
19504 /*integral_constant_expression_p=*/
19505 false);
19506 --cp_unevaluated_operand;
19507 --c_inhibit_evaluation_warnings;
19508 }
19509 if (TYPE_P (op1))
19510 r = cxx_sizeof_or_alignof_type (input_location,
19511 op1, TREE_CODE (t), std_alignof,
19512 complain & tf_error);
19513 else
19514 r = cxx_sizeof_or_alignof_expr (input_location,
19515 op1, TREE_CODE (t),
19516 complain & tf_error);
19517 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19518 {
19519 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19520 {
19521 if (!processing_template_decl && TYPE_P (op1))
19522 {
19523 r = build_min (SIZEOF_EXPR, size_type_node,
19524 build1 (NOP_EXPR, op1, error_mark_node));
19525 SIZEOF_EXPR_TYPE_P (r) = 1;
19526 }
19527 else
19528 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19529 TREE_SIDE_EFFECTS (r) = 0;
19530 TREE_READONLY (r) = 1;
19531 }
19532 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19533 }
19534 RETURN (r);
19535 }
19536
19537 case AT_ENCODE_EXPR:
19538 {
19539 op1 = TREE_OPERAND (t, 0);
19540 ++cp_unevaluated_operand;
19541 ++c_inhibit_evaluation_warnings;
19542 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19543 /*function_p=*/false,
19544 /*integral_constant_expression_p=*/false);
19545 --cp_unevaluated_operand;
19546 --c_inhibit_evaluation_warnings;
19547 RETURN (objc_build_encode_expr (op1));
19548 }
19549
19550 case NOEXCEPT_EXPR:
19551 op1 = TREE_OPERAND (t, 0);
19552 ++cp_unevaluated_operand;
19553 ++c_inhibit_evaluation_warnings;
19554 ++cp_noexcept_operand;
19555 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19556 /*function_p=*/false,
19557 /*integral_constant_expression_p=*/false);
19558 --cp_unevaluated_operand;
19559 --c_inhibit_evaluation_warnings;
19560 --cp_noexcept_operand;
19561 RETURN (finish_noexcept_expr (op1, complain));
19562
19563 case MODOP_EXPR:
19564 {
19565 warning_sentinel s(warn_div_by_zero);
19566 tree lhs = RECUR (TREE_OPERAND (t, 0));
19567 tree rhs = RECUR (TREE_OPERAND (t, 2));
19568 tree r = build_x_modify_expr
19569 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19570 complain|decltype_flag);
19571 /* TREE_NO_WARNING must be set if either the expression was
19572 parenthesized or it uses an operator such as >>= rather
19573 than plain assignment. In the former case, it was already
19574 set and must be copied. In the latter case,
19575 build_x_modify_expr sets it and it must not be reset
19576 here. */
19577 if (TREE_NO_WARNING (t))
19578 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19579
19580 RETURN (r);
19581 }
19582
19583 case ARROW_EXPR:
19584 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19585 args, complain, in_decl);
19586 /* Remember that there was a reference to this entity. */
19587 if (DECL_P (op1)
19588 && !mark_used (op1, complain) && !(complain & tf_error))
19589 RETURN (error_mark_node);
19590 RETURN (build_x_arrow (input_location, op1, complain));
19591
19592 case NEW_EXPR:
19593 {
19594 tree placement = RECUR (TREE_OPERAND (t, 0));
19595 tree init = RECUR (TREE_OPERAND (t, 3));
19596 vec<tree, va_gc> *placement_vec;
19597 vec<tree, va_gc> *init_vec;
19598 tree ret;
19599 location_t loc = EXPR_LOCATION (t);
19600
19601 if (placement == NULL_TREE)
19602 placement_vec = NULL;
19603 else
19604 {
19605 placement_vec = make_tree_vector ();
19606 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19607 vec_safe_push (placement_vec, TREE_VALUE (placement));
19608 }
19609
19610 /* If there was an initializer in the original tree, but it
19611 instantiated to an empty list, then we should pass a
19612 non-NULL empty vector to tell build_new that it was an
19613 empty initializer() rather than no initializer. This can
19614 only happen when the initializer is a pack expansion whose
19615 parameter packs are of length zero. */
19616 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19617 init_vec = NULL;
19618 else
19619 {
19620 init_vec = make_tree_vector ();
19621 if (init == void_node)
19622 gcc_assert (init_vec != NULL);
19623 else
19624 {
19625 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19626 vec_safe_push (init_vec, TREE_VALUE (init));
19627 }
19628 }
19629
19630 /* Avoid passing an enclosing decl to valid_array_size_p. */
19631 in_decl = NULL_TREE;
19632
19633 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19634 tree op2 = RECUR (TREE_OPERAND (t, 2));
19635 ret = build_new (loc, &placement_vec, op1, op2,
19636 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19637 complain);
19638
19639 if (placement_vec != NULL)
19640 release_tree_vector (placement_vec);
19641 if (init_vec != NULL)
19642 release_tree_vector (init_vec);
19643
19644 RETURN (ret);
19645 }
19646
19647 case DELETE_EXPR:
19648 {
19649 tree op0 = RECUR (TREE_OPERAND (t, 0));
19650 tree op1 = RECUR (TREE_OPERAND (t, 1));
19651 RETURN (delete_sanity (input_location, op0, op1,
19652 DELETE_EXPR_USE_VEC (t),
19653 DELETE_EXPR_USE_GLOBAL (t),
19654 complain));
19655 }
19656
19657 case COMPOUND_EXPR:
19658 {
19659 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19660 complain & ~tf_decltype, in_decl,
19661 /*function_p=*/false,
19662 integral_constant_expression_p);
19663 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19664 op0,
19665 RECUR (TREE_OPERAND (t, 1)),
19666 complain|decltype_flag));
19667 }
19668
19669 case CALL_EXPR:
19670 {
19671 tree function;
19672 unsigned int nargs, i;
19673 bool qualified_p;
19674 bool koenig_p;
19675 tree ret;
19676
19677 function = CALL_EXPR_FN (t);
19678 /* Internal function with no arguments. */
19679 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19680 RETURN (t);
19681
19682 /* When we parsed the expression, we determined whether or
19683 not Koenig lookup should be performed. */
19684 koenig_p = KOENIG_LOOKUP_P (t);
19685 if (function == NULL_TREE)
19686 {
19687 koenig_p = false;
19688 qualified_p = false;
19689 }
19690 else if (TREE_CODE (function) == SCOPE_REF)
19691 {
19692 qualified_p = true;
19693 function = tsubst_qualified_id (function, args, complain, in_decl,
19694 /*done=*/false,
19695 /*address_p=*/false);
19696 }
19697 else if (koenig_p && identifier_p (function))
19698 {
19699 /* Do nothing; calling tsubst_copy_and_build on an identifier
19700 would incorrectly perform unqualified lookup again.
19701
19702 Note that we can also have an IDENTIFIER_NODE if the earlier
19703 unqualified lookup found a member function; in that case
19704 koenig_p will be false and we do want to do the lookup
19705 again to find the instantiated member function.
19706
19707 FIXME but doing that causes c++/15272, so we need to stop
19708 using IDENTIFIER_NODE in that situation. */
19709 qualified_p = false;
19710 }
19711 else
19712 {
19713 if (TREE_CODE (function) == COMPONENT_REF)
19714 {
19715 tree op = TREE_OPERAND (function, 1);
19716
19717 qualified_p = (TREE_CODE (op) == SCOPE_REF
19718 || (BASELINK_P (op)
19719 && BASELINK_QUALIFIED_P (op)));
19720 }
19721 else
19722 qualified_p = false;
19723
19724 if (TREE_CODE (function) == ADDR_EXPR
19725 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19726 /* Avoid error about taking the address of a constructor. */
19727 function = TREE_OPERAND (function, 0);
19728
19729 function = tsubst_copy_and_build (function, args, complain,
19730 in_decl,
19731 !qualified_p,
19732 integral_constant_expression_p);
19733
19734 if (BASELINK_P (function))
19735 qualified_p = true;
19736 }
19737
19738 nargs = call_expr_nargs (t);
19739 releasing_vec call_args;
19740 for (i = 0; i < nargs; ++i)
19741 {
19742 tree arg = CALL_EXPR_ARG (t, i);
19743
19744 if (!PACK_EXPANSION_P (arg))
19745 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19746 else
19747 {
19748 /* Expand the pack expansion and push each entry onto
19749 CALL_ARGS. */
19750 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19751 if (TREE_CODE (arg) == TREE_VEC)
19752 {
19753 unsigned int len, j;
19754
19755 len = TREE_VEC_LENGTH (arg);
19756 for (j = 0; j < len; ++j)
19757 {
19758 tree value = TREE_VEC_ELT (arg, j);
19759 if (value != NULL_TREE)
19760 value = convert_from_reference (value);
19761 vec_safe_push (call_args, value);
19762 }
19763 }
19764 else
19765 {
19766 /* A partial substitution. Add one entry. */
19767 vec_safe_push (call_args, arg);
19768 }
19769 }
19770 }
19771
19772 /* Stripped-down processing for a call in a thunk. Specifically, in
19773 the thunk template for a generic lambda. */
19774 if (CALL_FROM_THUNK_P (t))
19775 {
19776 /* Now that we've expanded any packs, the number of call args
19777 might be different. */
19778 unsigned int cargs = call_args->length ();
19779 tree thisarg = NULL_TREE;
19780 if (TREE_CODE (function) == COMPONENT_REF)
19781 {
19782 thisarg = TREE_OPERAND (function, 0);
19783 if (TREE_CODE (thisarg) == INDIRECT_REF)
19784 thisarg = TREE_OPERAND (thisarg, 0);
19785 function = TREE_OPERAND (function, 1);
19786 if (TREE_CODE (function) == BASELINK)
19787 function = BASELINK_FUNCTIONS (function);
19788 }
19789 /* We aren't going to do normal overload resolution, so force the
19790 template-id to resolve. */
19791 function = resolve_nondeduced_context (function, complain);
19792 for (unsigned i = 0; i < cargs; ++i)
19793 {
19794 /* In a thunk, pass through args directly, without any
19795 conversions. */
19796 tree arg = (*call_args)[i];
19797 while (TREE_CODE (arg) != PARM_DECL)
19798 arg = TREE_OPERAND (arg, 0);
19799 (*call_args)[i] = arg;
19800 }
19801 if (thisarg)
19802 {
19803 /* If there are no other args, just push 'this'. */
19804 if (cargs == 0)
19805 vec_safe_push (call_args, thisarg);
19806 else
19807 {
19808 /* Otherwise, shift the other args over to make room. */
19809 tree last = (*call_args)[cargs - 1];
19810 vec_safe_push (call_args, last);
19811 for (int i = cargs - 1; i > 0; --i)
19812 (*call_args)[i] = (*call_args)[i - 1];
19813 (*call_args)[0] = thisarg;
19814 }
19815 }
19816 ret = build_call_a (function, call_args->length (),
19817 call_args->address ());
19818 /* The thunk location is not interesting. */
19819 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19820 CALL_FROM_THUNK_P (ret) = true;
19821 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19822 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19823
19824 RETURN (ret);
19825 }
19826
19827 /* We do not perform argument-dependent lookup if normal
19828 lookup finds a non-function, in accordance with the
19829 expected resolution of DR 218. */
19830 if (koenig_p
19831 && ((is_overloaded_fn (function)
19832 /* If lookup found a member function, the Koenig lookup is
19833 not appropriate, even if an unqualified-name was used
19834 to denote the function. */
19835 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19836 || identifier_p (function))
19837 /* Only do this when substitution turns a dependent call
19838 into a non-dependent call. */
19839 && type_dependent_expression_p_push (t)
19840 && !any_type_dependent_arguments_p (call_args))
19841 function = perform_koenig_lookup (function, call_args, tf_none);
19842
19843 if (function != NULL_TREE
19844 && identifier_p (function)
19845 && !any_type_dependent_arguments_p (call_args))
19846 {
19847 if (koenig_p && (complain & tf_warning_or_error))
19848 {
19849 /* For backwards compatibility and good diagnostics, try
19850 the unqualified lookup again if we aren't in SFINAE
19851 context. */
19852 tree unq = (tsubst_copy_and_build
19853 (function, args, complain, in_decl, true,
19854 integral_constant_expression_p));
19855 if (unq == error_mark_node)
19856 RETURN (error_mark_node);
19857
19858 if (unq != function)
19859 {
19860 /* In a lambda fn, we have to be careful to not
19861 introduce new this captures. Legacy code can't
19862 be using lambdas anyway, so it's ok to be
19863 stricter. */
19864 bool in_lambda = (current_class_type
19865 && LAMBDA_TYPE_P (current_class_type));
19866 char const *const msg
19867 = G_("%qD was not declared in this scope, "
19868 "and no declarations were found by "
19869 "argument-dependent lookup at the point "
19870 "of instantiation");
19871
19872 bool diag = true;
19873 if (in_lambda)
19874 error_at (cp_expr_loc_or_input_loc (t),
19875 msg, function);
19876 else
19877 diag = permerror (cp_expr_loc_or_input_loc (t),
19878 msg, function);
19879 if (diag)
19880 {
19881 tree fn = unq;
19882
19883 if (INDIRECT_REF_P (fn))
19884 fn = TREE_OPERAND (fn, 0);
19885 if (is_overloaded_fn (fn))
19886 fn = get_first_fn (fn);
19887
19888 if (!DECL_P (fn))
19889 /* Can't say anything more. */;
19890 else if (DECL_CLASS_SCOPE_P (fn))
19891 {
19892 location_t loc = cp_expr_loc_or_input_loc (t);
19893 inform (loc,
19894 "declarations in dependent base %qT are "
19895 "not found by unqualified lookup",
19896 DECL_CLASS_CONTEXT (fn));
19897 if (current_class_ptr)
19898 inform (loc,
19899 "use %<this->%D%> instead", function);
19900 else
19901 inform (loc,
19902 "use %<%T::%D%> instead",
19903 current_class_name, function);
19904 }
19905 else
19906 inform (DECL_SOURCE_LOCATION (fn),
19907 "%qD declared here, later in the "
19908 "translation unit", fn);
19909 if (in_lambda)
19910 RETURN (error_mark_node);
19911 }
19912
19913 function = unq;
19914 }
19915 }
19916 if (identifier_p (function))
19917 {
19918 if (complain & tf_error)
19919 unqualified_name_lookup_error (function);
19920 RETURN (error_mark_node);
19921 }
19922 }
19923
19924 /* Remember that there was a reference to this entity. */
19925 if (function != NULL_TREE
19926 && DECL_P (function)
19927 && !mark_used (function, complain) && !(complain & tf_error))
19928 RETURN (error_mark_node);
19929
19930 /* Put back tf_decltype for the actual call. */
19931 complain |= decltype_flag;
19932
19933 if (function == NULL_TREE)
19934 switch (CALL_EXPR_IFN (t))
19935 {
19936 case IFN_LAUNDER:
19937 gcc_assert (nargs == 1);
19938 if (vec_safe_length (call_args) != 1)
19939 {
19940 error_at (cp_expr_loc_or_input_loc (t),
19941 "wrong number of arguments to "
19942 "%<__builtin_launder%>");
19943 ret = error_mark_node;
19944 }
19945 else
19946 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
19947 (*call_args)[0], complain);
19948 break;
19949
19950 case IFN_VEC_CONVERT:
19951 gcc_assert (nargs == 1);
19952 if (vec_safe_length (call_args) != 1)
19953 {
19954 error_at (cp_expr_loc_or_input_loc (t),
19955 "wrong number of arguments to "
19956 "%<__builtin_convertvector%>");
19957 ret = error_mark_node;
19958 break;
19959 }
19960 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19961 tsubst (TREE_TYPE (t), args,
19962 complain, in_decl),
19963 complain);
19964 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19965 RETURN (ret);
19966 break;
19967
19968 default:
19969 /* Unsupported internal function with arguments. */
19970 gcc_unreachable ();
19971 }
19972 else if (TREE_CODE (function) == OFFSET_REF
19973 || TREE_CODE (function) == DOTSTAR_EXPR
19974 || TREE_CODE (function) == MEMBER_REF)
19975 ret = build_offset_ref_call_from_tree (function, &call_args,
19976 complain);
19977 else if (TREE_CODE (function) == COMPONENT_REF)
19978 {
19979 tree instance = TREE_OPERAND (function, 0);
19980 tree fn = TREE_OPERAND (function, 1);
19981
19982 if (processing_template_decl
19983 && (type_dependent_expression_p (instance)
19984 || (!BASELINK_P (fn)
19985 && TREE_CODE (fn) != FIELD_DECL)
19986 || type_dependent_expression_p (fn)
19987 || any_type_dependent_arguments_p (call_args)))
19988 ret = build_min_nt_call_vec (function, call_args);
19989 else if (!BASELINK_P (fn))
19990 ret = finish_call_expr (function, &call_args,
19991 /*disallow_virtual=*/false,
19992 /*koenig_p=*/false,
19993 complain);
19994 else
19995 ret = (build_new_method_call
19996 (instance, fn,
19997 &call_args, NULL_TREE,
19998 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19999 /*fn_p=*/NULL,
20000 complain));
20001 }
20002 else if (concept_check_p (function))
20003 {
20004 /* FUNCTION is a template-id referring to a concept definition. */
20005 tree id = unpack_concept_check (function);
20006 tree tmpl = TREE_OPERAND (id, 0);
20007 tree args = TREE_OPERAND (id, 1);
20008
20009 /* Calls to standard and variable concepts should have been
20010 previously diagnosed. */
20011 gcc_assert (function_concept_p (tmpl));
20012
20013 /* Ensure the result is wrapped as a call expression. */
20014 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20015 }
20016 else
20017 ret = finish_call_expr (function, &call_args,
20018 /*disallow_virtual=*/qualified_p,
20019 koenig_p,
20020 complain);
20021
20022 if (ret != error_mark_node)
20023 {
20024 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20025 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20026 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20027 if (op || ord || rev)
20028 {
20029 function = extract_call_expr (ret);
20030 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20031 CALL_EXPR_ORDERED_ARGS (function) = ord;
20032 CALL_EXPR_REVERSE_ARGS (function) = rev;
20033 }
20034 }
20035
20036 RETURN (ret);
20037 }
20038
20039 case COND_EXPR:
20040 {
20041 tree cond = RECUR (TREE_OPERAND (t, 0));
20042 cond = mark_rvalue_use (cond);
20043 tree folded_cond = fold_non_dependent_expr (cond, complain);
20044 tree exp1, exp2;
20045
20046 if (TREE_CODE (folded_cond) == INTEGER_CST)
20047 {
20048 if (integer_zerop (folded_cond))
20049 {
20050 ++c_inhibit_evaluation_warnings;
20051 exp1 = RECUR (TREE_OPERAND (t, 1));
20052 --c_inhibit_evaluation_warnings;
20053 exp2 = RECUR (TREE_OPERAND (t, 2));
20054 }
20055 else
20056 {
20057 exp1 = RECUR (TREE_OPERAND (t, 1));
20058 ++c_inhibit_evaluation_warnings;
20059 exp2 = RECUR (TREE_OPERAND (t, 2));
20060 --c_inhibit_evaluation_warnings;
20061 }
20062 cond = folded_cond;
20063 }
20064 else
20065 {
20066 exp1 = RECUR (TREE_OPERAND (t, 1));
20067 exp2 = RECUR (TREE_OPERAND (t, 2));
20068 }
20069
20070 warning_sentinel s(warn_duplicated_branches);
20071 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20072 cond, exp1, exp2, complain));
20073 }
20074
20075 case PSEUDO_DTOR_EXPR:
20076 {
20077 tree op0 = RECUR (TREE_OPERAND (t, 0));
20078 tree op1 = RECUR (TREE_OPERAND (t, 1));
20079 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20080 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20081 input_location));
20082 }
20083
20084 case TREE_LIST:
20085 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20086
20087 case COMPONENT_REF:
20088 {
20089 tree object;
20090 tree object_type;
20091 tree member;
20092 tree r;
20093
20094 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20095 args, complain, in_decl);
20096 /* Remember that there was a reference to this entity. */
20097 if (DECL_P (object)
20098 && !mark_used (object, complain) && !(complain & tf_error))
20099 RETURN (error_mark_node);
20100 object_type = TREE_TYPE (object);
20101
20102 member = TREE_OPERAND (t, 1);
20103 if (BASELINK_P (member))
20104 member = tsubst_baselink (member,
20105 non_reference (TREE_TYPE (object)),
20106 args, complain, in_decl);
20107 else
20108 member = tsubst_copy (member, args, complain, in_decl);
20109 if (member == error_mark_node)
20110 RETURN (error_mark_node);
20111
20112 if (TREE_CODE (member) == FIELD_DECL)
20113 {
20114 r = finish_non_static_data_member (member, object, NULL_TREE);
20115 if (TREE_CODE (r) == COMPONENT_REF)
20116 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20117 RETURN (r);
20118 }
20119 else if (type_dependent_expression_p (object))
20120 /* We can't do much here. */;
20121 else if (!CLASS_TYPE_P (object_type))
20122 {
20123 if (scalarish_type_p (object_type))
20124 {
20125 tree s = NULL_TREE;
20126 tree dtor = member;
20127
20128 if (TREE_CODE (dtor) == SCOPE_REF)
20129 {
20130 s = TREE_OPERAND (dtor, 0);
20131 dtor = TREE_OPERAND (dtor, 1);
20132 }
20133 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20134 {
20135 dtor = TREE_OPERAND (dtor, 0);
20136 if (TYPE_P (dtor))
20137 RETURN (finish_pseudo_destructor_expr
20138 (object, s, dtor, input_location));
20139 }
20140 }
20141 }
20142 else if (TREE_CODE (member) == SCOPE_REF
20143 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20144 {
20145 /* Lookup the template functions now that we know what the
20146 scope is. */
20147 tree scope = TREE_OPERAND (member, 0);
20148 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20149 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20150 member = lookup_qualified_name (scope, tmpl,
20151 /*is_type_p=*/false,
20152 /*complain=*/false);
20153 if (BASELINK_P (member))
20154 {
20155 BASELINK_FUNCTIONS (member)
20156 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20157 args);
20158 member = (adjust_result_of_qualified_name_lookup
20159 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20160 object_type));
20161 }
20162 else
20163 {
20164 qualified_name_lookup_error (scope, tmpl, member,
20165 input_location);
20166 RETURN (error_mark_node);
20167 }
20168 }
20169 else if (TREE_CODE (member) == SCOPE_REF
20170 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20171 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20172 {
20173 if (complain & tf_error)
20174 {
20175 if (TYPE_P (TREE_OPERAND (member, 0)))
20176 error ("%qT is not a class or namespace",
20177 TREE_OPERAND (member, 0));
20178 else
20179 error ("%qD is not a class or namespace",
20180 TREE_OPERAND (member, 0));
20181 }
20182 RETURN (error_mark_node);
20183 }
20184
20185 r = finish_class_member_access_expr (object, member,
20186 /*template_p=*/false,
20187 complain);
20188 if (TREE_CODE (r) == COMPONENT_REF)
20189 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20190 RETURN (r);
20191 }
20192
20193 case THROW_EXPR:
20194 RETURN (build_throw
20195 (input_location, RECUR (TREE_OPERAND (t, 0))));
20196
20197 case CONSTRUCTOR:
20198 {
20199 vec<constructor_elt, va_gc> *n;
20200 constructor_elt *ce;
20201 unsigned HOST_WIDE_INT idx;
20202 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20203 bool process_index_p;
20204 int newlen;
20205 bool need_copy_p = false;
20206 tree r;
20207
20208 if (type == error_mark_node)
20209 RETURN (error_mark_node);
20210
20211 /* We do not want to process the index of aggregate
20212 initializers as they are identifier nodes which will be
20213 looked up by digest_init. */
20214 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20215
20216 if (null_member_pointer_value_p (t))
20217 {
20218 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20219 RETURN (t);
20220 }
20221
20222 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20223 newlen = vec_safe_length (n);
20224 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20225 {
20226 if (ce->index && process_index_p
20227 /* An identifier index is looked up in the type
20228 being initialized, not the current scope. */
20229 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20230 ce->index = RECUR (ce->index);
20231
20232 if (PACK_EXPANSION_P (ce->value))
20233 {
20234 /* Substitute into the pack expansion. */
20235 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20236 in_decl);
20237
20238 if (ce->value == error_mark_node
20239 || PACK_EXPANSION_P (ce->value))
20240 ;
20241 else if (TREE_VEC_LENGTH (ce->value) == 1)
20242 /* Just move the argument into place. */
20243 ce->value = TREE_VEC_ELT (ce->value, 0);
20244 else
20245 {
20246 /* Update the length of the final CONSTRUCTOR
20247 arguments vector, and note that we will need to
20248 copy.*/
20249 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20250 need_copy_p = true;
20251 }
20252 }
20253 else
20254 ce->value = RECUR (ce->value);
20255 }
20256
20257 if (need_copy_p)
20258 {
20259 vec<constructor_elt, va_gc> *old_n = n;
20260
20261 vec_alloc (n, newlen);
20262 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20263 {
20264 if (TREE_CODE (ce->value) == TREE_VEC)
20265 {
20266 int i, len = TREE_VEC_LENGTH (ce->value);
20267 for (i = 0; i < len; ++i)
20268 CONSTRUCTOR_APPEND_ELT (n, 0,
20269 TREE_VEC_ELT (ce->value, i));
20270 }
20271 else
20272 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20273 }
20274 }
20275
20276 r = build_constructor (init_list_type_node, n);
20277 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20278 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20279 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20280
20281 if (TREE_HAS_CONSTRUCTOR (t))
20282 {
20283 fcl_t cl = fcl_functional;
20284 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20285 cl = fcl_c99;
20286 RETURN (finish_compound_literal (type, r, complain, cl));
20287 }
20288
20289 TREE_TYPE (r) = type;
20290 RETURN (r);
20291 }
20292
20293 case TYPEID_EXPR:
20294 {
20295 tree operand_0 = TREE_OPERAND (t, 0);
20296 if (TYPE_P (operand_0))
20297 {
20298 operand_0 = tsubst (operand_0, args, complain, in_decl);
20299 RETURN (get_typeid (operand_0, complain));
20300 }
20301 else
20302 {
20303 operand_0 = RECUR (operand_0);
20304 RETURN (build_typeid (operand_0, complain));
20305 }
20306 }
20307
20308 case VAR_DECL:
20309 if (!args)
20310 RETURN (t);
20311 /* Fall through */
20312
20313 case PARM_DECL:
20314 {
20315 tree r = tsubst_copy (t, args, complain, in_decl);
20316 /* ??? We're doing a subset of finish_id_expression here. */
20317 if (tree wrap = maybe_get_tls_wrapper_call (r))
20318 /* Replace an evaluated use of the thread_local variable with
20319 a call to its wrapper. */
20320 r = wrap;
20321 else if (outer_automatic_var_p (r))
20322 r = process_outer_var_ref (r, complain);
20323
20324 if (!TYPE_REF_P (TREE_TYPE (t)))
20325 /* If the original type was a reference, we'll be wrapped in
20326 the appropriate INDIRECT_REF. */
20327 r = convert_from_reference (r);
20328 RETURN (r);
20329 }
20330
20331 case VA_ARG_EXPR:
20332 {
20333 tree op0 = RECUR (TREE_OPERAND (t, 0));
20334 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20335 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20336 }
20337
20338 case OFFSETOF_EXPR:
20339 {
20340 tree object_ptr
20341 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20342 in_decl, /*function_p=*/false,
20343 /*integral_constant_expression_p=*/false);
20344 RETURN (finish_offsetof (object_ptr,
20345 RECUR (TREE_OPERAND (t, 0)),
20346 EXPR_LOCATION (t)));
20347 }
20348
20349 case ADDRESSOF_EXPR:
20350 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20351 RECUR (TREE_OPERAND (t, 0)), complain));
20352
20353 case TRAIT_EXPR:
20354 {
20355 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20356 complain, in_decl);
20357 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20358 complain, in_decl);
20359 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20360 TRAIT_EXPR_KIND (t), type1, type2));
20361 }
20362
20363 case STMT_EXPR:
20364 {
20365 tree old_stmt_expr = cur_stmt_expr;
20366 tree stmt_expr = begin_stmt_expr ();
20367
20368 cur_stmt_expr = stmt_expr;
20369 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20370 integral_constant_expression_p);
20371 stmt_expr = finish_stmt_expr (stmt_expr, false);
20372 cur_stmt_expr = old_stmt_expr;
20373
20374 /* If the resulting list of expression statement is empty,
20375 fold it further into void_node. */
20376 if (empty_expr_stmt_p (stmt_expr))
20377 stmt_expr = void_node;
20378
20379 RETURN (stmt_expr);
20380 }
20381
20382 case LAMBDA_EXPR:
20383 {
20384 if (complain & tf_partial)
20385 {
20386 /* We don't have a full set of template arguments yet; don't touch
20387 the lambda at all. */
20388 gcc_assert (processing_template_decl);
20389 return t;
20390 }
20391 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20392
20393 RETURN (build_lambda_object (r));
20394 }
20395
20396 case TARGET_EXPR:
20397 /* We can get here for a constant initializer of non-dependent type.
20398 FIXME stop folding in cp_parser_initializer_clause. */
20399 {
20400 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20401 complain);
20402 RETURN (r);
20403 }
20404
20405 case TRANSACTION_EXPR:
20406 RETURN (tsubst_expr(t, args, complain, in_decl,
20407 integral_constant_expression_p));
20408
20409 case PAREN_EXPR:
20410 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20411
20412 case VEC_PERM_EXPR:
20413 {
20414 tree op0 = RECUR (TREE_OPERAND (t, 0));
20415 tree op1 = RECUR (TREE_OPERAND (t, 1));
20416 tree op2 = RECUR (TREE_OPERAND (t, 2));
20417 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20418 complain));
20419 }
20420
20421 case REQUIRES_EXPR:
20422 {
20423 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20424 RETURN (r);
20425 }
20426
20427 case RANGE_EXPR:
20428 /* No need to substitute further, a RANGE_EXPR will always be built
20429 with constant operands. */
20430 RETURN (t);
20431
20432 case NON_LVALUE_EXPR:
20433 case VIEW_CONVERT_EXPR:
20434 if (location_wrapper_p (t))
20435 /* We need to do this here as well as in tsubst_copy so we get the
20436 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20437 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20438 EXPR_LOCATION (t)));
20439 /* fallthrough. */
20440
20441 default:
20442 /* Handle Objective-C++ constructs, if appropriate. */
20443 {
20444 tree subst
20445 = objcp_tsubst_copy_and_build (t, args, complain,
20446 in_decl, /*function_p=*/false);
20447 if (subst)
20448 RETURN (subst);
20449 }
20450 RETURN (tsubst_copy (t, args, complain, in_decl));
20451 }
20452
20453 #undef RECUR
20454 #undef RETURN
20455 out:
20456 input_location = save_loc;
20457 return retval;
20458 }
20459
20460 /* Verify that the instantiated ARGS are valid. For type arguments,
20461 make sure that the type's linkage is ok. For non-type arguments,
20462 make sure they are constants if they are integral or enumerations.
20463 Emit an error under control of COMPLAIN, and return TRUE on error. */
20464
20465 static bool
20466 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20467 {
20468 if (dependent_template_arg_p (t))
20469 return false;
20470 if (ARGUMENT_PACK_P (t))
20471 {
20472 tree vec = ARGUMENT_PACK_ARGS (t);
20473 int len = TREE_VEC_LENGTH (vec);
20474 bool result = false;
20475 int i;
20476
20477 for (i = 0; i < len; ++i)
20478 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20479 result = true;
20480 return result;
20481 }
20482 else if (TYPE_P (t))
20483 {
20484 /* [basic.link]: A name with no linkage (notably, the name
20485 of a class or enumeration declared in a local scope)
20486 shall not be used to declare an entity with linkage.
20487 This implies that names with no linkage cannot be used as
20488 template arguments
20489
20490 DR 757 relaxes this restriction for C++0x. */
20491 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20492 : no_linkage_check (t, /*relaxed_p=*/false));
20493
20494 if (nt)
20495 {
20496 /* DR 488 makes use of a type with no linkage cause
20497 type deduction to fail. */
20498 if (complain & tf_error)
20499 {
20500 if (TYPE_UNNAMED_P (nt))
20501 error ("%qT is/uses unnamed type", t);
20502 else
20503 error ("template argument for %qD uses local type %qT",
20504 tmpl, t);
20505 }
20506 return true;
20507 }
20508 /* In order to avoid all sorts of complications, we do not
20509 allow variably-modified types as template arguments. */
20510 else if (variably_modified_type_p (t, NULL_TREE))
20511 {
20512 if (complain & tf_error)
20513 error ("%qT is a variably modified type", t);
20514 return true;
20515 }
20516 }
20517 /* Class template and alias template arguments should be OK. */
20518 else if (DECL_TYPE_TEMPLATE_P (t))
20519 ;
20520 /* A non-type argument of integral or enumerated type must be a
20521 constant. */
20522 else if (TREE_TYPE (t)
20523 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20524 && !REFERENCE_REF_P (t)
20525 && !TREE_CONSTANT (t))
20526 {
20527 if (complain & tf_error)
20528 error ("integral expression %qE is not constant", t);
20529 return true;
20530 }
20531 return false;
20532 }
20533
20534 static bool
20535 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20536 {
20537 int ix, len = DECL_NTPARMS (tmpl);
20538 bool result = false;
20539
20540 for (ix = 0; ix != len; ix++)
20541 {
20542 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20543 result = true;
20544 }
20545 if (result && (complain & tf_error))
20546 error (" trying to instantiate %qD", tmpl);
20547 return result;
20548 }
20549
20550 /* We're out of SFINAE context now, so generate diagnostics for the access
20551 errors we saw earlier when instantiating D from TMPL and ARGS. */
20552
20553 static void
20554 recheck_decl_substitution (tree d, tree tmpl, tree args)
20555 {
20556 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20557 tree type = TREE_TYPE (pattern);
20558 location_t loc = input_location;
20559
20560 push_access_scope (d);
20561 push_deferring_access_checks (dk_no_deferred);
20562 input_location = DECL_SOURCE_LOCATION (pattern);
20563 tsubst (type, args, tf_warning_or_error, d);
20564 input_location = loc;
20565 pop_deferring_access_checks ();
20566 pop_access_scope (d);
20567 }
20568
20569 /* Instantiate the indicated variable, function, or alias template TMPL with
20570 the template arguments in TARG_PTR. */
20571
20572 static tree
20573 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20574 {
20575 tree targ_ptr = orig_args;
20576 tree fndecl;
20577 tree gen_tmpl;
20578 tree spec;
20579 bool access_ok = true;
20580
20581 if (tmpl == error_mark_node)
20582 return error_mark_node;
20583
20584 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20585
20586 /* If this function is a clone, handle it specially. */
20587 if (DECL_CLONED_FUNCTION_P (tmpl))
20588 {
20589 tree spec;
20590 tree clone;
20591
20592 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20593 DECL_CLONED_FUNCTION. */
20594 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20595 targ_ptr, complain);
20596 if (spec == error_mark_node)
20597 return error_mark_node;
20598
20599 /* Look for the clone. */
20600 FOR_EACH_CLONE (clone, spec)
20601 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20602 return clone;
20603 /* We should always have found the clone by now. */
20604 gcc_unreachable ();
20605 return NULL_TREE;
20606 }
20607
20608 if (targ_ptr == error_mark_node)
20609 return error_mark_node;
20610
20611 /* Check to see if we already have this specialization. */
20612 gen_tmpl = most_general_template (tmpl);
20613 if (TMPL_ARGS_DEPTH (targ_ptr)
20614 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20615 /* targ_ptr only has the innermost template args, so add the outer ones
20616 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20617 the case of a non-dependent call within a template definition). */
20618 targ_ptr = (add_outermost_template_args
20619 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20620 targ_ptr));
20621
20622 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20623 but it doesn't seem to be on the hot path. */
20624 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20625
20626 gcc_checking_assert (tmpl == gen_tmpl
20627 || ((fndecl
20628 = retrieve_specialization (tmpl, orig_args, 0))
20629 == spec)
20630 || fndecl == NULL_TREE);
20631
20632 if (spec != NULL_TREE)
20633 {
20634 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20635 {
20636 if (complain & tf_error)
20637 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20638 return error_mark_node;
20639 }
20640 return spec;
20641 }
20642
20643 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20644 complain))
20645 return error_mark_node;
20646
20647 /* We are building a FUNCTION_DECL, during which the access of its
20648 parameters and return types have to be checked. However this
20649 FUNCTION_DECL which is the desired context for access checking
20650 is not built yet. We solve this chicken-and-egg problem by
20651 deferring all checks until we have the FUNCTION_DECL. */
20652 push_deferring_access_checks (dk_deferred);
20653
20654 /* Instantiation of the function happens in the context of the function
20655 template, not the context of the overload resolution we're doing. */
20656 push_to_top_level ();
20657 /* If there are dependent arguments, e.g. because we're doing partial
20658 ordering, make sure processing_template_decl stays set. */
20659 if (uses_template_parms (targ_ptr))
20660 ++processing_template_decl;
20661 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20662 {
20663 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20664 complain, gen_tmpl, true);
20665 push_nested_class (ctx);
20666 }
20667
20668 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20669
20670 fndecl = NULL_TREE;
20671 if (VAR_P (pattern))
20672 {
20673 /* We need to determine if we're using a partial or explicit
20674 specialization now, because the type of the variable could be
20675 different. */
20676 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20677 tree elt = most_specialized_partial_spec (tid, complain);
20678 if (elt == error_mark_node)
20679 pattern = error_mark_node;
20680 else if (elt)
20681 {
20682 tree partial_tmpl = TREE_VALUE (elt);
20683 tree partial_args = TREE_PURPOSE (elt);
20684 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20685 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20686 }
20687 }
20688
20689 /* Substitute template parameters to obtain the specialization. */
20690 if (fndecl == NULL_TREE)
20691 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20692 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20693 pop_nested_class ();
20694 pop_from_top_level ();
20695
20696 if (fndecl == error_mark_node)
20697 {
20698 pop_deferring_access_checks ();
20699 return error_mark_node;
20700 }
20701
20702 /* The DECL_TI_TEMPLATE should always be the immediate parent
20703 template, not the most general template. */
20704 DECL_TI_TEMPLATE (fndecl) = tmpl;
20705 DECL_TI_ARGS (fndecl) = targ_ptr;
20706
20707 /* Now we know the specialization, compute access previously
20708 deferred. Do no access control for inheriting constructors,
20709 as we already checked access for the inherited constructor. */
20710 if (!(flag_new_inheriting_ctors
20711 && DECL_INHERITED_CTOR (fndecl)))
20712 {
20713 push_access_scope (fndecl);
20714 if (!perform_deferred_access_checks (complain))
20715 access_ok = false;
20716 pop_access_scope (fndecl);
20717 }
20718 pop_deferring_access_checks ();
20719
20720 /* If we've just instantiated the main entry point for a function,
20721 instantiate all the alternate entry points as well. We do this
20722 by cloning the instantiation of the main entry point, not by
20723 instantiating the template clones. */
20724 if (tree chain = DECL_CHAIN (gen_tmpl))
20725 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20726 clone_function_decl (fndecl, /*update_methods=*/false);
20727
20728 if (!access_ok)
20729 {
20730 if (!(complain & tf_error))
20731 {
20732 /* Remember to reinstantiate when we're out of SFINAE so the user
20733 can see the errors. */
20734 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20735 }
20736 return error_mark_node;
20737 }
20738 return fndecl;
20739 }
20740
20741 /* Wrapper for instantiate_template_1. */
20742
20743 tree
20744 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20745 {
20746 tree ret;
20747 timevar_push (TV_TEMPLATE_INST);
20748 ret = instantiate_template_1 (tmpl, orig_args, complain);
20749 timevar_pop (TV_TEMPLATE_INST);
20750 return ret;
20751 }
20752
20753 /* Instantiate the alias template TMPL with ARGS. Also push a template
20754 instantiation level, which instantiate_template doesn't do because
20755 functions and variables have sufficient context established by the
20756 callers. */
20757
20758 static tree
20759 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20760 {
20761 if (tmpl == error_mark_node || args == error_mark_node)
20762 return error_mark_node;
20763
20764 args =
20765 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20766 args, tmpl, complain,
20767 /*require_all_args=*/true,
20768 /*use_default_args=*/true);
20769
20770 /* FIXME check for satisfaction in check_instantiated_args. */
20771 if (flag_concepts
20772 && !any_dependent_template_arguments_p (args)
20773 && !constraints_satisfied_p (tmpl, args))
20774 {
20775 if (complain & tf_error)
20776 {
20777 auto_diagnostic_group d;
20778 error ("template constraint failure for %qD", tmpl);
20779 diagnose_constraints (input_location, tmpl, args);
20780 }
20781 return error_mark_node;
20782 }
20783
20784 if (!push_tinst_level (tmpl, args))
20785 return error_mark_node;
20786 tree r = instantiate_template (tmpl, args, complain);
20787 pop_tinst_level ();
20788
20789 return r;
20790 }
20791
20792 /* PARM is a template parameter pack for FN. Returns true iff
20793 PARM is used in a deducible way in the argument list of FN. */
20794
20795 static bool
20796 pack_deducible_p (tree parm, tree fn)
20797 {
20798 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20799 for (; t; t = TREE_CHAIN (t))
20800 {
20801 tree type = TREE_VALUE (t);
20802 tree packs;
20803 if (!PACK_EXPANSION_P (type))
20804 continue;
20805 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20806 packs; packs = TREE_CHAIN (packs))
20807 if (template_args_equal (TREE_VALUE (packs), parm))
20808 {
20809 /* The template parameter pack is used in a function parameter
20810 pack. If this is the end of the parameter list, the
20811 template parameter pack is deducible. */
20812 if (TREE_CHAIN (t) == void_list_node)
20813 return true;
20814 else
20815 /* Otherwise, not. Well, it could be deduced from
20816 a non-pack parameter, but doing so would end up with
20817 a deduction mismatch, so don't bother. */
20818 return false;
20819 }
20820 }
20821 /* The template parameter pack isn't used in any function parameter
20822 packs, but it might be used deeper, e.g. tuple<Args...>. */
20823 return true;
20824 }
20825
20826 /* Subroutine of fn_type_unification: check non-dependent parms for
20827 convertibility. */
20828
20829 static int
20830 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20831 tree fn, unification_kind_t strict, int flags,
20832 struct conversion **convs, bool explain_p)
20833 {
20834 /* Non-constructor methods need to leave a conversion for 'this', which
20835 isn't included in nargs here. */
20836 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20837 && !DECL_CONSTRUCTOR_P (fn));
20838
20839 for (unsigned ia = 0;
20840 parms && parms != void_list_node && ia < nargs; )
20841 {
20842 tree parm = TREE_VALUE (parms);
20843
20844 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20845 && (!TREE_CHAIN (parms)
20846 || TREE_CHAIN (parms) == void_list_node))
20847 /* For a function parameter pack that occurs at the end of the
20848 parameter-declaration-list, the type A of each remaining
20849 argument of the call is compared with the type P of the
20850 declarator-id of the function parameter pack. */
20851 break;
20852
20853 parms = TREE_CHAIN (parms);
20854
20855 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20856 /* For a function parameter pack that does not occur at the
20857 end of the parameter-declaration-list, the type of the
20858 parameter pack is a non-deduced context. */
20859 continue;
20860
20861 if (!uses_template_parms (parm))
20862 {
20863 tree arg = args[ia];
20864 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20865 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20866
20867 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20868 conv_p, explain_p))
20869 return 1;
20870 }
20871
20872 ++ia;
20873 }
20874
20875 return 0;
20876 }
20877
20878 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20879 NARGS elements of the arguments that are being used when calling
20880 it. TARGS is a vector into which the deduced template arguments
20881 are placed.
20882
20883 Returns either a FUNCTION_DECL for the matching specialization of FN or
20884 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20885 true, diagnostics will be printed to explain why it failed.
20886
20887 If FN is a conversion operator, or we are trying to produce a specific
20888 specialization, RETURN_TYPE is the return type desired.
20889
20890 The EXPLICIT_TARGS are explicit template arguments provided via a
20891 template-id.
20892
20893 The parameter STRICT is one of:
20894
20895 DEDUCE_CALL:
20896 We are deducing arguments for a function call, as in
20897 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20898 deducing arguments for a call to the result of a conversion
20899 function template, as in [over.call.object].
20900
20901 DEDUCE_CONV:
20902 We are deducing arguments for a conversion function, as in
20903 [temp.deduct.conv].
20904
20905 DEDUCE_EXACT:
20906 We are deducing arguments when doing an explicit instantiation
20907 as in [temp.explicit], when determining an explicit specialization
20908 as in [temp.expl.spec], or when taking the address of a function
20909 template, as in [temp.deduct.funcaddr]. */
20910
20911 tree
20912 fn_type_unification (tree fn,
20913 tree explicit_targs,
20914 tree targs,
20915 const tree *args,
20916 unsigned int nargs,
20917 tree return_type,
20918 unification_kind_t strict,
20919 int flags,
20920 struct conversion **convs,
20921 bool explain_p,
20922 bool decltype_p)
20923 {
20924 tree parms;
20925 tree fntype;
20926 tree decl = NULL_TREE;
20927 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20928 bool ok;
20929 static int deduction_depth;
20930 /* type_unification_real will pass back any access checks from default
20931 template argument substitution. */
20932 vec<deferred_access_check, va_gc> *checks = NULL;
20933 /* We don't have all the template args yet. */
20934 bool incomplete = true;
20935
20936 tree orig_fn = fn;
20937 if (flag_new_inheriting_ctors)
20938 fn = strip_inheriting_ctors (fn);
20939
20940 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20941 tree r = error_mark_node;
20942
20943 tree full_targs = targs;
20944 if (TMPL_ARGS_DEPTH (targs)
20945 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20946 full_targs = (add_outermost_template_args
20947 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20948 targs));
20949
20950 if (decltype_p)
20951 complain |= tf_decltype;
20952
20953 /* In C++0x, it's possible to have a function template whose type depends
20954 on itself recursively. This is most obvious with decltype, but can also
20955 occur with enumeration scope (c++/48969). So we need to catch infinite
20956 recursion and reject the substitution at deduction time; this function
20957 will return error_mark_node for any repeated substitution.
20958
20959 This also catches excessive recursion such as when f<N> depends on
20960 f<N-1> across all integers, and returns error_mark_node for all the
20961 substitutions back up to the initial one.
20962
20963 This is, of course, not reentrant. */
20964 if (excessive_deduction_depth)
20965 return error_mark_node;
20966 ++deduction_depth;
20967
20968 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20969
20970 fntype = TREE_TYPE (fn);
20971 if (explicit_targs)
20972 {
20973 /* [temp.deduct]
20974
20975 The specified template arguments must match the template
20976 parameters in kind (i.e., type, nontype, template), and there
20977 must not be more arguments than there are parameters;
20978 otherwise type deduction fails.
20979
20980 Nontype arguments must match the types of the corresponding
20981 nontype template parameters, or must be convertible to the
20982 types of the corresponding nontype parameters as specified in
20983 _temp.arg.nontype_, otherwise type deduction fails.
20984
20985 All references in the function type of the function template
20986 to the corresponding template parameters are replaced by the
20987 specified template argument values. If a substitution in a
20988 template parameter or in the function type of the function
20989 template results in an invalid type, type deduction fails. */
20990 int i, len = TREE_VEC_LENGTH (tparms);
20991 location_t loc = input_location;
20992 incomplete = false;
20993
20994 if (explicit_targs == error_mark_node)
20995 goto fail;
20996
20997 if (TMPL_ARGS_DEPTH (explicit_targs)
20998 < TMPL_ARGS_DEPTH (full_targs))
20999 explicit_targs = add_outermost_template_args (full_targs,
21000 explicit_targs);
21001
21002 /* Adjust any explicit template arguments before entering the
21003 substitution context. */
21004 explicit_targs
21005 = (coerce_template_parms (tparms, explicit_targs, fn,
21006 complain|tf_partial,
21007 /*require_all_args=*/false,
21008 /*use_default_args=*/false));
21009 if (explicit_targs == error_mark_node)
21010 goto fail;
21011
21012 /* Substitute the explicit args into the function type. This is
21013 necessary so that, for instance, explicitly declared function
21014 arguments can match null pointed constants. If we were given
21015 an incomplete set of explicit args, we must not do semantic
21016 processing during substitution as we could create partial
21017 instantiations. */
21018 for (i = 0; i < len; i++)
21019 {
21020 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21021 bool parameter_pack = false;
21022 tree targ = TREE_VEC_ELT (explicit_targs, i);
21023
21024 /* Dig out the actual parm. */
21025 if (TREE_CODE (parm) == TYPE_DECL
21026 || TREE_CODE (parm) == TEMPLATE_DECL)
21027 {
21028 parm = TREE_TYPE (parm);
21029 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21030 }
21031 else if (TREE_CODE (parm) == PARM_DECL)
21032 {
21033 parm = DECL_INITIAL (parm);
21034 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21035 }
21036
21037 if (targ == NULL_TREE)
21038 /* No explicit argument for this template parameter. */
21039 incomplete = true;
21040 else if (parameter_pack && pack_deducible_p (parm, fn))
21041 {
21042 /* Mark the argument pack as "incomplete". We could
21043 still deduce more arguments during unification.
21044 We remove this mark in type_unification_real. */
21045 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21046 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21047 = ARGUMENT_PACK_ARGS (targ);
21048
21049 /* We have some incomplete argument packs. */
21050 incomplete = true;
21051 }
21052 }
21053
21054 if (incomplete)
21055 {
21056 if (!push_tinst_level (fn, explicit_targs))
21057 {
21058 excessive_deduction_depth = true;
21059 goto fail;
21060 }
21061 ++processing_template_decl;
21062 input_location = DECL_SOURCE_LOCATION (fn);
21063 /* Ignore any access checks; we'll see them again in
21064 instantiate_template and they might have the wrong
21065 access path at this point. */
21066 push_deferring_access_checks (dk_deferred);
21067 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21068 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21069 pop_deferring_access_checks ();
21070 input_location = loc;
21071 --processing_template_decl;
21072 pop_tinst_level ();
21073
21074 if (fntype == error_mark_node)
21075 goto fail;
21076 }
21077
21078 /* Place the explicitly specified arguments in TARGS. */
21079 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21080 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21081 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21082 if (!incomplete && CHECKING_P
21083 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21084 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21085 (targs, NUM_TMPL_ARGS (explicit_targs));
21086 }
21087
21088 if (return_type && strict != DEDUCE_CALL)
21089 {
21090 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21091 new_args[0] = return_type;
21092 memcpy (new_args + 1, args, nargs * sizeof (tree));
21093 args = new_args;
21094 ++nargs;
21095 }
21096
21097 if (!incomplete)
21098 goto deduced;
21099
21100 /* Never do unification on the 'this' parameter. */
21101 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21102
21103 if (return_type && strict == DEDUCE_CALL)
21104 {
21105 /* We're deducing for a call to the result of a template conversion
21106 function. The parms we really want are in return_type. */
21107 if (INDIRECT_TYPE_P (return_type))
21108 return_type = TREE_TYPE (return_type);
21109 parms = TYPE_ARG_TYPES (return_type);
21110 }
21111 else if (return_type)
21112 {
21113 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21114 }
21115
21116 /* We allow incomplete unification without an error message here
21117 because the standard doesn't seem to explicitly prohibit it. Our
21118 callers must be ready to deal with unification failures in any
21119 event. */
21120
21121 /* If we aren't explaining yet, push tinst context so we can see where
21122 any errors (e.g. from class instantiations triggered by instantiation
21123 of default template arguments) come from. If we are explaining, this
21124 context is redundant. */
21125 if (!explain_p && !push_tinst_level (fn, targs))
21126 {
21127 excessive_deduction_depth = true;
21128 goto fail;
21129 }
21130
21131 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21132 full_targs, parms, args, nargs, /*subr=*/0,
21133 strict, &checks, explain_p);
21134 if (!explain_p)
21135 pop_tinst_level ();
21136 if (!ok)
21137 goto fail;
21138
21139 /* Now that we have bindings for all of the template arguments,
21140 ensure that the arguments deduced for the template template
21141 parameters have compatible template parameter lists. We cannot
21142 check this property before we have deduced all template
21143 arguments, because the template parameter types of a template
21144 template parameter might depend on prior template parameters
21145 deduced after the template template parameter. The following
21146 ill-formed example illustrates this issue:
21147
21148 template<typename T, template<T> class C> void f(C<5>, T);
21149
21150 template<int N> struct X {};
21151
21152 void g() {
21153 f(X<5>(), 5l); // error: template argument deduction fails
21154 }
21155
21156 The template parameter list of 'C' depends on the template type
21157 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21158 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21159 time that we deduce 'C'. */
21160 if (!template_template_parm_bindings_ok_p
21161 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21162 {
21163 unify_inconsistent_template_template_parameters (explain_p);
21164 goto fail;
21165 }
21166
21167 /* DR 1391: All parameters have args, now check non-dependent parms for
21168 convertibility. */
21169 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21170 convs, explain_p))
21171 goto fail;
21172
21173 deduced:
21174 /* All is well so far. Now, check:
21175
21176 [temp.deduct]
21177
21178 When all template arguments have been deduced, all uses of
21179 template parameters in nondeduced contexts are replaced with
21180 the corresponding deduced argument values. If the
21181 substitution results in an invalid type, as described above,
21182 type deduction fails. */
21183 if (!push_tinst_level (fn, targs))
21184 {
21185 excessive_deduction_depth = true;
21186 goto fail;
21187 }
21188
21189 /* Also collect access checks from the instantiation. */
21190 reopen_deferring_access_checks (checks);
21191
21192 decl = instantiate_template (fn, targs, complain);
21193
21194 checks = get_deferred_access_checks ();
21195 pop_deferring_access_checks ();
21196
21197 pop_tinst_level ();
21198
21199 if (decl == error_mark_node)
21200 goto fail;
21201
21202 /* Now perform any access checks encountered during substitution. */
21203 push_access_scope (decl);
21204 ok = perform_access_checks (checks, complain);
21205 pop_access_scope (decl);
21206 if (!ok)
21207 goto fail;
21208
21209 /* If we're looking for an exact match, check that what we got
21210 is indeed an exact match. It might not be if some template
21211 parameters are used in non-deduced contexts. But don't check
21212 for an exact match if we have dependent template arguments;
21213 in that case we're doing partial ordering, and we already know
21214 that we have two candidates that will provide the actual type. */
21215 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21216 {
21217 tree substed = TREE_TYPE (decl);
21218 unsigned int i;
21219
21220 tree sarg
21221 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21222 if (return_type)
21223 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21224 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21225 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21226 {
21227 unify_type_mismatch (explain_p, args[i],
21228 TREE_VALUE (sarg));
21229 goto fail;
21230 }
21231 }
21232
21233 /* After doing deduction with the inherited constructor, actually return an
21234 instantiation of the inheriting constructor. */
21235 if (orig_fn != fn)
21236 decl = instantiate_template (orig_fn, targs, complain);
21237
21238 r = decl;
21239
21240 fail:
21241 --deduction_depth;
21242 if (excessive_deduction_depth)
21243 {
21244 if (deduction_depth == 0)
21245 /* Reset once we're all the way out. */
21246 excessive_deduction_depth = false;
21247 }
21248
21249 return r;
21250 }
21251
21252 /* Adjust types before performing type deduction, as described in
21253 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21254 sections are symmetric. PARM is the type of a function parameter
21255 or the return type of the conversion function. ARG is the type of
21256 the argument passed to the call, or the type of the value
21257 initialized with the result of the conversion function.
21258 ARG_EXPR is the original argument expression, which may be null. */
21259
21260 static int
21261 maybe_adjust_types_for_deduction (unification_kind_t strict,
21262 tree* parm,
21263 tree* arg,
21264 tree arg_expr)
21265 {
21266 int result = 0;
21267
21268 switch (strict)
21269 {
21270 case DEDUCE_CALL:
21271 break;
21272
21273 case DEDUCE_CONV:
21274 /* Swap PARM and ARG throughout the remainder of this
21275 function; the handling is precisely symmetric since PARM
21276 will initialize ARG rather than vice versa. */
21277 std::swap (parm, arg);
21278 break;
21279
21280 case DEDUCE_EXACT:
21281 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21282 too, but here handle it by stripping the reference from PARM
21283 rather than by adding it to ARG. */
21284 if (TYPE_REF_P (*parm)
21285 && TYPE_REF_IS_RVALUE (*parm)
21286 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21287 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21288 && TYPE_REF_P (*arg)
21289 && !TYPE_REF_IS_RVALUE (*arg))
21290 *parm = TREE_TYPE (*parm);
21291 /* Nothing else to do in this case. */
21292 return 0;
21293
21294 default:
21295 gcc_unreachable ();
21296 }
21297
21298 if (!TYPE_REF_P (*parm))
21299 {
21300 /* [temp.deduct.call]
21301
21302 If P is not a reference type:
21303
21304 --If A is an array type, the pointer type produced by the
21305 array-to-pointer standard conversion (_conv.array_) is
21306 used in place of A for type deduction; otherwise,
21307
21308 --If A is a function type, the pointer type produced by
21309 the function-to-pointer standard conversion
21310 (_conv.func_) is used in place of A for type deduction;
21311 otherwise,
21312
21313 --If A is a cv-qualified type, the top level
21314 cv-qualifiers of A's type are ignored for type
21315 deduction. */
21316 if (TREE_CODE (*arg) == ARRAY_TYPE)
21317 *arg = build_pointer_type (TREE_TYPE (*arg));
21318 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21319 *arg = build_pointer_type (*arg);
21320 else
21321 *arg = TYPE_MAIN_VARIANT (*arg);
21322 }
21323
21324 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21325 reference to a cv-unqualified template parameter that does not represent a
21326 template parameter of a class template (during class template argument
21327 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21328 an lvalue, the type "lvalue reference to A" is used in place of A for type
21329 deduction. */
21330 if (TYPE_REF_P (*parm)
21331 && TYPE_REF_IS_RVALUE (*parm)
21332 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21333 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21334 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21335 && (arg_expr ? lvalue_p (arg_expr)
21336 /* try_one_overload doesn't provide an arg_expr, but
21337 functions are always lvalues. */
21338 : TREE_CODE (*arg) == FUNCTION_TYPE))
21339 *arg = build_reference_type (*arg);
21340
21341 /* [temp.deduct.call]
21342
21343 If P is a cv-qualified type, the top level cv-qualifiers
21344 of P's type are ignored for type deduction. If P is a
21345 reference type, the type referred to by P is used for
21346 type deduction. */
21347 *parm = TYPE_MAIN_VARIANT (*parm);
21348 if (TYPE_REF_P (*parm))
21349 {
21350 *parm = TREE_TYPE (*parm);
21351 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21352 }
21353
21354 /* DR 322. For conversion deduction, remove a reference type on parm
21355 too (which has been swapped into ARG). */
21356 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21357 *arg = TREE_TYPE (*arg);
21358
21359 return result;
21360 }
21361
21362 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21363 template which doesn't contain any deducible template parameters; check if
21364 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21365 unify_one_argument. */
21366
21367 static int
21368 check_non_deducible_conversion (tree parm, tree arg, int strict,
21369 int flags, struct conversion **conv_p,
21370 bool explain_p)
21371 {
21372 tree type;
21373
21374 if (!TYPE_P (arg))
21375 type = TREE_TYPE (arg);
21376 else
21377 type = arg;
21378
21379 if (same_type_p (parm, type))
21380 return unify_success (explain_p);
21381
21382 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21383 if (strict == DEDUCE_CONV)
21384 {
21385 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21386 return unify_success (explain_p);
21387 }
21388 else if (strict != DEDUCE_EXACT)
21389 {
21390 bool ok = false;
21391 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21392 if (conv_p)
21393 /* Avoid recalculating this in add_function_candidate. */
21394 ok = (*conv_p
21395 = good_conversion (parm, type, conv_arg, flags, complain));
21396 else
21397 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21398 if (ok)
21399 return unify_success (explain_p);
21400 }
21401
21402 if (strict == DEDUCE_EXACT)
21403 return unify_type_mismatch (explain_p, parm, arg);
21404 else
21405 return unify_arg_conversion (explain_p, parm, type, arg);
21406 }
21407
21408 static bool uses_deducible_template_parms (tree type);
21409
21410 /* Returns true iff the expression EXPR is one from which a template
21411 argument can be deduced. In other words, if it's an undecorated
21412 use of a template non-type parameter. */
21413
21414 static bool
21415 deducible_expression (tree expr)
21416 {
21417 /* Strip implicit conversions. */
21418 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21419 expr = TREE_OPERAND (expr, 0);
21420 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21421 }
21422
21423 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21424 deducible way; that is, if it has a max value of <PARM> - 1. */
21425
21426 static bool
21427 deducible_array_bound (tree domain)
21428 {
21429 if (domain == NULL_TREE)
21430 return false;
21431
21432 tree max = TYPE_MAX_VALUE (domain);
21433 if (TREE_CODE (max) != MINUS_EXPR)
21434 return false;
21435
21436 return deducible_expression (TREE_OPERAND (max, 0));
21437 }
21438
21439 /* Returns true iff the template arguments ARGS use a template parameter
21440 in a deducible way. */
21441
21442 static bool
21443 deducible_template_args (tree args)
21444 {
21445 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21446 {
21447 bool deducible;
21448 tree elt = TREE_VEC_ELT (args, i);
21449 if (ARGUMENT_PACK_P (elt))
21450 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21451 else
21452 {
21453 if (PACK_EXPANSION_P (elt))
21454 elt = PACK_EXPANSION_PATTERN (elt);
21455 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21456 deducible = true;
21457 else if (TYPE_P (elt))
21458 deducible = uses_deducible_template_parms (elt);
21459 else
21460 deducible = deducible_expression (elt);
21461 }
21462 if (deducible)
21463 return true;
21464 }
21465 return false;
21466 }
21467
21468 /* Returns true iff TYPE contains any deducible references to template
21469 parameters, as per 14.8.2.5. */
21470
21471 static bool
21472 uses_deducible_template_parms (tree type)
21473 {
21474 if (PACK_EXPANSION_P (type))
21475 type = PACK_EXPANSION_PATTERN (type);
21476
21477 /* T
21478 cv-list T
21479 TT<T>
21480 TT<i>
21481 TT<> */
21482 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21483 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21484 return true;
21485
21486 /* T*
21487 T&
21488 T&& */
21489 if (INDIRECT_TYPE_P (type))
21490 return uses_deducible_template_parms (TREE_TYPE (type));
21491
21492 /* T[integer-constant ]
21493 type [i] */
21494 if (TREE_CODE (type) == ARRAY_TYPE)
21495 return (uses_deducible_template_parms (TREE_TYPE (type))
21496 || deducible_array_bound (TYPE_DOMAIN (type)));
21497
21498 /* T type ::*
21499 type T::*
21500 T T::*
21501 T (type ::*)()
21502 type (T::*)()
21503 type (type ::*)(T)
21504 type (T::*)(T)
21505 T (type ::*)(T)
21506 T (T::*)()
21507 T (T::*)(T) */
21508 if (TYPE_PTRMEM_P (type))
21509 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21510 || (uses_deducible_template_parms
21511 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21512
21513 /* template-name <T> (where template-name refers to a class template)
21514 template-name <i> (where template-name refers to a class template) */
21515 if (CLASS_TYPE_P (type)
21516 && CLASSTYPE_TEMPLATE_INFO (type)
21517 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21518 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21519 (CLASSTYPE_TI_ARGS (type)));
21520
21521 /* type (T)
21522 T()
21523 T(T) */
21524 if (FUNC_OR_METHOD_TYPE_P (type))
21525 {
21526 if (uses_deducible_template_parms (TREE_TYPE (type)))
21527 return true;
21528 tree parm = TYPE_ARG_TYPES (type);
21529 if (TREE_CODE (type) == METHOD_TYPE)
21530 parm = TREE_CHAIN (parm);
21531 for (; parm; parm = TREE_CHAIN (parm))
21532 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21533 return true;
21534 }
21535
21536 return false;
21537 }
21538
21539 /* Subroutine of type_unification_real and unify_pack_expansion to
21540 handle unification of a single P/A pair. Parameters are as
21541 for those functions. */
21542
21543 static int
21544 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21545 int subr, unification_kind_t strict,
21546 bool explain_p)
21547 {
21548 tree arg_expr = NULL_TREE;
21549 int arg_strict;
21550
21551 if (arg == error_mark_node || parm == error_mark_node)
21552 return unify_invalid (explain_p);
21553 if (arg == unknown_type_node)
21554 /* We can't deduce anything from this, but we might get all the
21555 template args from other function args. */
21556 return unify_success (explain_p);
21557
21558 /* Implicit conversions (Clause 4) will be performed on a function
21559 argument to convert it to the type of the corresponding function
21560 parameter if the parameter type contains no template-parameters that
21561 participate in template argument deduction. */
21562 if (strict != DEDUCE_EXACT
21563 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21564 /* For function parameters with no deducible template parameters,
21565 just return. We'll check non-dependent conversions later. */
21566 return unify_success (explain_p);
21567
21568 switch (strict)
21569 {
21570 case DEDUCE_CALL:
21571 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21572 | UNIFY_ALLOW_MORE_CV_QUAL
21573 | UNIFY_ALLOW_DERIVED);
21574 break;
21575
21576 case DEDUCE_CONV:
21577 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21578 break;
21579
21580 case DEDUCE_EXACT:
21581 arg_strict = UNIFY_ALLOW_NONE;
21582 break;
21583
21584 default:
21585 gcc_unreachable ();
21586 }
21587
21588 /* We only do these transformations if this is the top-level
21589 parameter_type_list in a call or declaration matching; in other
21590 situations (nested function declarators, template argument lists) we
21591 won't be comparing a type to an expression, and we don't do any type
21592 adjustments. */
21593 if (!subr)
21594 {
21595 if (!TYPE_P (arg))
21596 {
21597 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21598 if (type_unknown_p (arg))
21599 {
21600 /* [temp.deduct.type] A template-argument can be
21601 deduced from a pointer to function or pointer
21602 to member function argument if the set of
21603 overloaded functions does not contain function
21604 templates and at most one of a set of
21605 overloaded functions provides a unique
21606 match. */
21607 resolve_overloaded_unification (tparms, targs, parm,
21608 arg, strict,
21609 arg_strict, explain_p);
21610 /* If a unique match was not found, this is a
21611 non-deduced context, so we still succeed. */
21612 return unify_success (explain_p);
21613 }
21614
21615 arg_expr = arg;
21616 arg = unlowered_expr_type (arg);
21617 if (arg == error_mark_node)
21618 return unify_invalid (explain_p);
21619 }
21620
21621 arg_strict |=
21622 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21623 }
21624 else
21625 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21626 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21627 return unify_template_argument_mismatch (explain_p, parm, arg);
21628
21629 /* For deduction from an init-list we need the actual list. */
21630 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21631 arg = arg_expr;
21632 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21633 }
21634
21635 /* for_each_template_parm callback that always returns 0. */
21636
21637 static int
21638 zero_r (tree, void *)
21639 {
21640 return 0;
21641 }
21642
21643 /* for_each_template_parm any_fn callback to handle deduction of a template
21644 type argument from the type of an array bound. */
21645
21646 static int
21647 array_deduction_r (tree t, void *data)
21648 {
21649 tree_pair_p d = (tree_pair_p)data;
21650 tree &tparms = d->purpose;
21651 tree &targs = d->value;
21652
21653 if (TREE_CODE (t) == ARRAY_TYPE)
21654 if (tree dom = TYPE_DOMAIN (t))
21655 if (tree max = TYPE_MAX_VALUE (dom))
21656 {
21657 if (TREE_CODE (max) == MINUS_EXPR)
21658 max = TREE_OPERAND (max, 0);
21659 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21660 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21661 UNIFY_ALLOW_NONE, /*explain*/false);
21662 }
21663
21664 /* Keep walking. */
21665 return 0;
21666 }
21667
21668 /* Try to deduce any not-yet-deduced template type arguments from the type of
21669 an array bound. This is handled separately from unify because 14.8.2.5 says
21670 "The type of a type parameter is only deduced from an array bound if it is
21671 not otherwise deduced." */
21672
21673 static void
21674 try_array_deduction (tree tparms, tree targs, tree parm)
21675 {
21676 tree_pair_s data = { tparms, targs };
21677 hash_set<tree> visited;
21678 for_each_template_parm (parm, zero_r, &data, &visited,
21679 /*nondeduced*/false, array_deduction_r);
21680 }
21681
21682 /* Most parms like fn_type_unification.
21683
21684 If SUBR is 1, we're being called recursively (to unify the
21685 arguments of a function or method parameter of a function
21686 template).
21687
21688 CHECKS is a pointer to a vector of access checks encountered while
21689 substituting default template arguments. */
21690
21691 static int
21692 type_unification_real (tree tparms,
21693 tree full_targs,
21694 tree xparms,
21695 const tree *xargs,
21696 unsigned int xnargs,
21697 int subr,
21698 unification_kind_t strict,
21699 vec<deferred_access_check, va_gc> **checks,
21700 bool explain_p)
21701 {
21702 tree parm, arg;
21703 int i;
21704 int ntparms = TREE_VEC_LENGTH (tparms);
21705 int saw_undeduced = 0;
21706 tree parms;
21707 const tree *args;
21708 unsigned int nargs;
21709 unsigned int ia;
21710
21711 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21712 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21713 gcc_assert (ntparms > 0);
21714
21715 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21716
21717 /* Reset the number of non-defaulted template arguments contained
21718 in TARGS. */
21719 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21720
21721 again:
21722 parms = xparms;
21723 args = xargs;
21724 nargs = xnargs;
21725
21726 ia = 0;
21727 while (parms && parms != void_list_node
21728 && ia < nargs)
21729 {
21730 parm = TREE_VALUE (parms);
21731
21732 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21733 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21734 /* For a function parameter pack that occurs at the end of the
21735 parameter-declaration-list, the type A of each remaining
21736 argument of the call is compared with the type P of the
21737 declarator-id of the function parameter pack. */
21738 break;
21739
21740 parms = TREE_CHAIN (parms);
21741
21742 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21743 /* For a function parameter pack that does not occur at the
21744 end of the parameter-declaration-list, the type of the
21745 parameter pack is a non-deduced context. */
21746 continue;
21747
21748 arg = args[ia];
21749 ++ia;
21750
21751 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21752 explain_p))
21753 return 1;
21754 }
21755
21756 if (parms
21757 && parms != void_list_node
21758 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21759 {
21760 /* Unify the remaining arguments with the pack expansion type. */
21761 tree argvec;
21762 tree parmvec = make_tree_vec (1);
21763
21764 /* Allocate a TREE_VEC and copy in all of the arguments */
21765 argvec = make_tree_vec (nargs - ia);
21766 for (i = 0; ia < nargs; ++ia, ++i)
21767 TREE_VEC_ELT (argvec, i) = args[ia];
21768
21769 /* Copy the parameter into parmvec. */
21770 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21771 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21772 /*subr=*/subr, explain_p))
21773 return 1;
21774
21775 /* Advance to the end of the list of parameters. */
21776 parms = TREE_CHAIN (parms);
21777 }
21778
21779 /* Fail if we've reached the end of the parm list, and more args
21780 are present, and the parm list isn't variadic. */
21781 if (ia < nargs && parms == void_list_node)
21782 return unify_too_many_arguments (explain_p, nargs, ia);
21783 /* Fail if parms are left and they don't have default values and
21784 they aren't all deduced as empty packs (c++/57397). This is
21785 consistent with sufficient_parms_p. */
21786 if (parms && parms != void_list_node
21787 && TREE_PURPOSE (parms) == NULL_TREE)
21788 {
21789 unsigned int count = nargs;
21790 tree p = parms;
21791 bool type_pack_p;
21792 do
21793 {
21794 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21795 if (!type_pack_p)
21796 count++;
21797 p = TREE_CHAIN (p);
21798 }
21799 while (p && p != void_list_node);
21800 if (count != nargs)
21801 return unify_too_few_arguments (explain_p, ia, count,
21802 type_pack_p);
21803 }
21804
21805 if (!subr)
21806 {
21807 tsubst_flags_t complain = (explain_p
21808 ? tf_warning_or_error
21809 : tf_none);
21810 bool tried_array_deduction = (cxx_dialect < cxx17);
21811
21812 for (i = 0; i < ntparms; i++)
21813 {
21814 tree targ = TREE_VEC_ELT (targs, i);
21815 tree tparm = TREE_VEC_ELT (tparms, i);
21816
21817 /* Clear the "incomplete" flags on all argument packs now so that
21818 substituting them into later default arguments works. */
21819 if (targ && ARGUMENT_PACK_P (targ))
21820 {
21821 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21822 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21823 }
21824
21825 if (targ || tparm == error_mark_node)
21826 continue;
21827 tparm = TREE_VALUE (tparm);
21828
21829 if (TREE_CODE (tparm) == TYPE_DECL
21830 && !tried_array_deduction)
21831 {
21832 try_array_deduction (tparms, targs, xparms);
21833 tried_array_deduction = true;
21834 if (TREE_VEC_ELT (targs, i))
21835 continue;
21836 }
21837
21838 /* If this is an undeduced nontype parameter that depends on
21839 a type parameter, try another pass; its type may have been
21840 deduced from a later argument than the one from which
21841 this parameter can be deduced. */
21842 if (TREE_CODE (tparm) == PARM_DECL
21843 && uses_template_parms (TREE_TYPE (tparm))
21844 && saw_undeduced < 2)
21845 {
21846 saw_undeduced = 1;
21847 continue;
21848 }
21849
21850 /* Core issue #226 (C++0x) [temp.deduct]:
21851
21852 If a template argument has not been deduced, its
21853 default template argument, if any, is used.
21854
21855 When we are in C++98 mode, TREE_PURPOSE will either
21856 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21857 to explicitly check cxx_dialect here. */
21858 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21859 /* OK, there is a default argument. Wait until after the
21860 conversion check to do substitution. */
21861 continue;
21862
21863 /* If the type parameter is a parameter pack, then it will
21864 be deduced to an empty parameter pack. */
21865 if (template_parameter_pack_p (tparm))
21866 {
21867 tree arg;
21868
21869 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21870 {
21871 arg = make_node (NONTYPE_ARGUMENT_PACK);
21872 TREE_CONSTANT (arg) = 1;
21873 }
21874 else
21875 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21876
21877 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21878
21879 TREE_VEC_ELT (targs, i) = arg;
21880 continue;
21881 }
21882
21883 return unify_parameter_deduction_failure (explain_p, tparm);
21884 }
21885
21886 /* Now substitute into the default template arguments. */
21887 for (i = 0; i < ntparms; i++)
21888 {
21889 tree targ = TREE_VEC_ELT (targs, i);
21890 tree tparm = TREE_VEC_ELT (tparms, i);
21891
21892 if (targ || tparm == error_mark_node)
21893 continue;
21894 tree parm = TREE_VALUE (tparm);
21895 tree arg = TREE_PURPOSE (tparm);
21896 reopen_deferring_access_checks (*checks);
21897 location_t save_loc = input_location;
21898 if (DECL_P (parm))
21899 input_location = DECL_SOURCE_LOCATION (parm);
21900
21901 if (saw_undeduced == 1
21902 && TREE_CODE (parm) == PARM_DECL
21903 && uses_template_parms (TREE_TYPE (parm)))
21904 {
21905 /* The type of this non-type parameter depends on undeduced
21906 parameters. Don't try to use its default argument yet,
21907 since we might deduce an argument for it on the next pass,
21908 but do check whether the arguments we already have cause
21909 substitution failure, so that that happens before we try
21910 later default arguments (78489). */
21911 ++processing_template_decl;
21912 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21913 NULL_TREE);
21914 --processing_template_decl;
21915 if (type == error_mark_node)
21916 arg = error_mark_node;
21917 else
21918 arg = NULL_TREE;
21919 }
21920 else
21921 {
21922 /* Even if the call is happening in template context, getting
21923 here means it's non-dependent, and a default argument is
21924 considered a separate definition under [temp.decls], so we can
21925 do this substitution without processing_template_decl. This
21926 is important if the default argument contains something that
21927 might be instantiation-dependent like access (87480). */
21928 processing_template_decl_sentinel s;
21929 tree substed = NULL_TREE;
21930 if (saw_undeduced == 1)
21931 {
21932 /* First instatiate in template context, in case we still
21933 depend on undeduced template parameters. */
21934 ++processing_template_decl;
21935 substed = tsubst_template_arg (arg, full_targs, complain,
21936 NULL_TREE);
21937 --processing_template_decl;
21938 if (substed != error_mark_node
21939 && !uses_template_parms (substed))
21940 /* We replaced all the tparms, substitute again out of
21941 template context. */
21942 substed = NULL_TREE;
21943 }
21944 if (!substed)
21945 substed = tsubst_template_arg (arg, full_targs, complain,
21946 NULL_TREE);
21947
21948 if (!uses_template_parms (substed))
21949 arg = convert_template_argument (parm, substed, full_targs,
21950 complain, i, NULL_TREE);
21951 else if (saw_undeduced == 1)
21952 arg = NULL_TREE;
21953 else
21954 arg = error_mark_node;
21955 }
21956
21957 input_location = save_loc;
21958 *checks = get_deferred_access_checks ();
21959 pop_deferring_access_checks ();
21960
21961 if (arg == error_mark_node)
21962 return 1;
21963 else if (arg)
21964 {
21965 TREE_VEC_ELT (targs, i) = arg;
21966 /* The position of the first default template argument,
21967 is also the number of non-defaulted arguments in TARGS.
21968 Record that. */
21969 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21970 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21971 }
21972 }
21973
21974 if (saw_undeduced++ == 1)
21975 goto again;
21976 }
21977
21978 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21979 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21980
21981 return unify_success (explain_p);
21982 }
21983
21984 /* Subroutine of type_unification_real. Args are like the variables
21985 at the call site. ARG is an overloaded function (or template-id);
21986 we try deducing template args from each of the overloads, and if
21987 only one succeeds, we go with that. Modifies TARGS and returns
21988 true on success. */
21989
21990 static bool
21991 resolve_overloaded_unification (tree tparms,
21992 tree targs,
21993 tree parm,
21994 tree arg,
21995 unification_kind_t strict,
21996 int sub_strict,
21997 bool explain_p)
21998 {
21999 tree tempargs = copy_node (targs);
22000 int good = 0;
22001 tree goodfn = NULL_TREE;
22002 bool addr_p;
22003
22004 if (TREE_CODE (arg) == ADDR_EXPR)
22005 {
22006 arg = TREE_OPERAND (arg, 0);
22007 addr_p = true;
22008 }
22009 else
22010 addr_p = false;
22011
22012 if (TREE_CODE (arg) == COMPONENT_REF)
22013 /* Handle `&x' where `x' is some static or non-static member
22014 function name. */
22015 arg = TREE_OPERAND (arg, 1);
22016
22017 if (TREE_CODE (arg) == OFFSET_REF)
22018 arg = TREE_OPERAND (arg, 1);
22019
22020 /* Strip baselink information. */
22021 if (BASELINK_P (arg))
22022 arg = BASELINK_FUNCTIONS (arg);
22023
22024 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22025 {
22026 /* If we got some explicit template args, we need to plug them into
22027 the affected templates before we try to unify, in case the
22028 explicit args will completely resolve the templates in question. */
22029
22030 int ok = 0;
22031 tree expl_subargs = TREE_OPERAND (arg, 1);
22032 arg = TREE_OPERAND (arg, 0);
22033
22034 for (lkp_iterator iter (arg); iter; ++iter)
22035 {
22036 tree fn = *iter;
22037 tree subargs, elem;
22038
22039 if (TREE_CODE (fn) != TEMPLATE_DECL)
22040 continue;
22041
22042 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22043 expl_subargs, NULL_TREE, tf_none,
22044 /*require_all_args=*/true,
22045 /*use_default_args=*/true);
22046 if (subargs != error_mark_node
22047 && !any_dependent_template_arguments_p (subargs))
22048 {
22049 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
22050 if (try_one_overload (tparms, targs, tempargs, parm,
22051 elem, strict, sub_strict, addr_p, explain_p)
22052 && (!goodfn || !same_type_p (goodfn, elem)))
22053 {
22054 goodfn = elem;
22055 ++good;
22056 }
22057 }
22058 else if (subargs)
22059 ++ok;
22060 }
22061 /* If no templates (or more than one) are fully resolved by the
22062 explicit arguments, this template-id is a non-deduced context; it
22063 could still be OK if we deduce all template arguments for the
22064 enclosing call through other arguments. */
22065 if (good != 1)
22066 good = ok;
22067 }
22068 else if (!OVL_P (arg))
22069 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22070 -- but the deduction does not succeed because the expression is
22071 not just the function on its own. */
22072 return false;
22073 else
22074 for (lkp_iterator iter (arg); iter; ++iter)
22075 {
22076 tree fn = *iter;
22077 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22078 strict, sub_strict, addr_p, explain_p)
22079 && (!goodfn || !decls_match (goodfn, fn)))
22080 {
22081 goodfn = fn;
22082 ++good;
22083 }
22084 }
22085
22086 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22087 to function or pointer to member function argument if the set of
22088 overloaded functions does not contain function templates and at most
22089 one of a set of overloaded functions provides a unique match.
22090
22091 So if we found multiple possibilities, we return success but don't
22092 deduce anything. */
22093
22094 if (good == 1)
22095 {
22096 int i = TREE_VEC_LENGTH (targs);
22097 for (; i--; )
22098 if (TREE_VEC_ELT (tempargs, i))
22099 {
22100 tree old = TREE_VEC_ELT (targs, i);
22101 tree new_ = TREE_VEC_ELT (tempargs, i);
22102 if (new_ && old && ARGUMENT_PACK_P (old)
22103 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22104 /* Don't forget explicit template arguments in a pack. */
22105 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22106 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22107 TREE_VEC_ELT (targs, i) = new_;
22108 }
22109 }
22110 if (good)
22111 return true;
22112
22113 return false;
22114 }
22115
22116 /* Core DR 115: In contexts where deduction is done and fails, or in
22117 contexts where deduction is not done, if a template argument list is
22118 specified and it, along with any default template arguments, identifies
22119 a single function template specialization, then the template-id is an
22120 lvalue for the function template specialization. */
22121
22122 tree
22123 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22124 {
22125 tree expr, offset, baselink;
22126 bool addr;
22127
22128 if (!type_unknown_p (orig_expr))
22129 return orig_expr;
22130
22131 expr = orig_expr;
22132 addr = false;
22133 offset = NULL_TREE;
22134 baselink = NULL_TREE;
22135
22136 if (TREE_CODE (expr) == ADDR_EXPR)
22137 {
22138 expr = TREE_OPERAND (expr, 0);
22139 addr = true;
22140 }
22141 if (TREE_CODE (expr) == OFFSET_REF)
22142 {
22143 offset = expr;
22144 expr = TREE_OPERAND (expr, 1);
22145 }
22146 if (BASELINK_P (expr))
22147 {
22148 baselink = expr;
22149 expr = BASELINK_FUNCTIONS (expr);
22150 }
22151
22152 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22153 {
22154 int good = 0;
22155 tree goodfn = NULL_TREE;
22156
22157 /* If we got some explicit template args, we need to plug them into
22158 the affected templates before we try to unify, in case the
22159 explicit args will completely resolve the templates in question. */
22160
22161 tree expl_subargs = TREE_OPERAND (expr, 1);
22162 tree arg = TREE_OPERAND (expr, 0);
22163 tree badfn = NULL_TREE;
22164 tree badargs = NULL_TREE;
22165
22166 for (lkp_iterator iter (arg); iter; ++iter)
22167 {
22168 tree fn = *iter;
22169 tree subargs, elem;
22170
22171 if (TREE_CODE (fn) != TEMPLATE_DECL)
22172 continue;
22173
22174 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22175 expl_subargs, NULL_TREE, tf_none,
22176 /*require_all_args=*/true,
22177 /*use_default_args=*/true);
22178 if (subargs != error_mark_node
22179 && !any_dependent_template_arguments_p (subargs))
22180 {
22181 elem = instantiate_template (fn, subargs, tf_none);
22182 if (elem == error_mark_node)
22183 {
22184 badfn = fn;
22185 badargs = subargs;
22186 }
22187 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
22188 {
22189 goodfn = elem;
22190 ++good;
22191 }
22192 }
22193 }
22194 if (good == 1)
22195 {
22196 mark_used (goodfn);
22197 expr = goodfn;
22198 if (baselink)
22199 expr = build_baselink (BASELINK_BINFO (baselink),
22200 BASELINK_ACCESS_BINFO (baselink),
22201 expr, BASELINK_OPTYPE (baselink));
22202 if (offset)
22203 {
22204 tree base
22205 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22206 expr = build_offset_ref (base, expr, addr, complain);
22207 }
22208 if (addr)
22209 expr = cp_build_addr_expr (expr, complain);
22210 return expr;
22211 }
22212 else if (good == 0 && badargs && (complain & tf_error))
22213 /* There were no good options and at least one bad one, so let the
22214 user know what the problem is. */
22215 instantiate_template (badfn, badargs, complain);
22216 }
22217 return orig_expr;
22218 }
22219
22220 /* As above, but error out if the expression remains overloaded. */
22221
22222 tree
22223 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22224 {
22225 exp = resolve_nondeduced_context (exp, complain);
22226 if (type_unknown_p (exp))
22227 {
22228 if (complain & tf_error)
22229 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22230 return error_mark_node;
22231 }
22232 return exp;
22233 }
22234
22235 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22236 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22237 different overloads deduce different arguments for a given parm.
22238 ADDR_P is true if the expression for which deduction is being
22239 performed was of the form "& fn" rather than simply "fn".
22240
22241 Returns 1 on success. */
22242
22243 static int
22244 try_one_overload (tree tparms,
22245 tree orig_targs,
22246 tree targs,
22247 tree parm,
22248 tree arg,
22249 unification_kind_t strict,
22250 int sub_strict,
22251 bool addr_p,
22252 bool explain_p)
22253 {
22254 int nargs;
22255 tree tempargs;
22256 int i;
22257
22258 if (arg == error_mark_node)
22259 return 0;
22260
22261 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22262 to function or pointer to member function argument if the set of
22263 overloaded functions does not contain function templates and at most
22264 one of a set of overloaded functions provides a unique match.
22265
22266 So if this is a template, just return success. */
22267
22268 if (uses_template_parms (arg))
22269 return 1;
22270
22271 if (TREE_CODE (arg) == METHOD_TYPE)
22272 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22273 else if (addr_p)
22274 arg = build_pointer_type (arg);
22275
22276 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22277
22278 /* We don't copy orig_targs for this because if we have already deduced
22279 some template args from previous args, unify would complain when we
22280 try to deduce a template parameter for the same argument, even though
22281 there isn't really a conflict. */
22282 nargs = TREE_VEC_LENGTH (targs);
22283 tempargs = make_tree_vec (nargs);
22284
22285 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22286 return 0;
22287
22288 /* First make sure we didn't deduce anything that conflicts with
22289 explicitly specified args. */
22290 for (i = nargs; i--; )
22291 {
22292 tree elt = TREE_VEC_ELT (tempargs, i);
22293 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22294
22295 if (!elt)
22296 /*NOP*/;
22297 else if (uses_template_parms (elt))
22298 /* Since we're unifying against ourselves, we will fill in
22299 template args used in the function parm list with our own
22300 template parms. Discard them. */
22301 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22302 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22303 {
22304 /* Check that the argument at each index of the deduced argument pack
22305 is equivalent to the corresponding explicitly specified argument.
22306 We may have deduced more arguments than were explicitly specified,
22307 and that's OK. */
22308
22309 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22310 that's wrong if we deduce the same argument pack from multiple
22311 function arguments: it's only incomplete the first time. */
22312
22313 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22314 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22315
22316 if (TREE_VEC_LENGTH (deduced_pack)
22317 < TREE_VEC_LENGTH (explicit_pack))
22318 return 0;
22319
22320 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22321 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22322 TREE_VEC_ELT (deduced_pack, j)))
22323 return 0;
22324 }
22325 else if (oldelt && !template_args_equal (oldelt, elt))
22326 return 0;
22327 }
22328
22329 for (i = nargs; i--; )
22330 {
22331 tree elt = TREE_VEC_ELT (tempargs, i);
22332
22333 if (elt)
22334 TREE_VEC_ELT (targs, i) = elt;
22335 }
22336
22337 return 1;
22338 }
22339
22340 /* PARM is a template class (perhaps with unbound template
22341 parameters). ARG is a fully instantiated type. If ARG can be
22342 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22343 TARGS are as for unify. */
22344
22345 static tree
22346 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22347 bool explain_p)
22348 {
22349 tree copy_of_targs;
22350
22351 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22352 return NULL_TREE;
22353 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22354 /* Matches anything. */;
22355 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22356 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22357 return NULL_TREE;
22358
22359 /* We need to make a new template argument vector for the call to
22360 unify. If we used TARGS, we'd clutter it up with the result of
22361 the attempted unification, even if this class didn't work out.
22362 We also don't want to commit ourselves to all the unifications
22363 we've already done, since unification is supposed to be done on
22364 an argument-by-argument basis. In other words, consider the
22365 following pathological case:
22366
22367 template <int I, int J, int K>
22368 struct S {};
22369
22370 template <int I, int J>
22371 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22372
22373 template <int I, int J, int K>
22374 void f(S<I, J, K>, S<I, I, I>);
22375
22376 void g() {
22377 S<0, 0, 0> s0;
22378 S<0, 1, 2> s2;
22379
22380 f(s0, s2);
22381 }
22382
22383 Now, by the time we consider the unification involving `s2', we
22384 already know that we must have `f<0, 0, 0>'. But, even though
22385 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22386 because there are two ways to unify base classes of S<0, 1, 2>
22387 with S<I, I, I>. If we kept the already deduced knowledge, we
22388 would reject the possibility I=1. */
22389 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22390
22391 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22392 {
22393 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22394 return NULL_TREE;
22395 return arg;
22396 }
22397
22398 /* If unification failed, we're done. */
22399 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22400 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22401 return NULL_TREE;
22402
22403 return arg;
22404 }
22405
22406 /* Given a template type PARM and a class type ARG, find the unique
22407 base type in ARG that is an instance of PARM. We do not examine
22408 ARG itself; only its base-classes. If there is not exactly one
22409 appropriate base class, return NULL_TREE. PARM may be the type of
22410 a partial specialization, as well as a plain template type. Used
22411 by unify. */
22412
22413 static enum template_base_result
22414 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22415 bool explain_p, tree *result)
22416 {
22417 tree rval = NULL_TREE;
22418 tree binfo;
22419
22420 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22421
22422 binfo = TYPE_BINFO (complete_type (arg));
22423 if (!binfo)
22424 {
22425 /* The type could not be completed. */
22426 *result = NULL_TREE;
22427 return tbr_incomplete_type;
22428 }
22429
22430 /* Walk in inheritance graph order. The search order is not
22431 important, and this avoids multiple walks of virtual bases. */
22432 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22433 {
22434 tree r = try_class_unification (tparms, targs, parm,
22435 BINFO_TYPE (binfo), explain_p);
22436
22437 if (r)
22438 {
22439 /* If there is more than one satisfactory baseclass, then:
22440
22441 [temp.deduct.call]
22442
22443 If they yield more than one possible deduced A, the type
22444 deduction fails.
22445
22446 applies. */
22447 if (rval && !same_type_p (r, rval))
22448 {
22449 *result = NULL_TREE;
22450 return tbr_ambiguous_baseclass;
22451 }
22452
22453 rval = r;
22454 }
22455 }
22456
22457 *result = rval;
22458 return tbr_success;
22459 }
22460
22461 /* Returns the level of DECL, which declares a template parameter. */
22462
22463 static int
22464 template_decl_level (tree decl)
22465 {
22466 switch (TREE_CODE (decl))
22467 {
22468 case TYPE_DECL:
22469 case TEMPLATE_DECL:
22470 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22471
22472 case PARM_DECL:
22473 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22474
22475 default:
22476 gcc_unreachable ();
22477 }
22478 return 0;
22479 }
22480
22481 /* Decide whether ARG can be unified with PARM, considering only the
22482 cv-qualifiers of each type, given STRICT as documented for unify.
22483 Returns nonzero iff the unification is OK on that basis. */
22484
22485 static int
22486 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22487 {
22488 int arg_quals = cp_type_quals (arg);
22489 int parm_quals = cp_type_quals (parm);
22490
22491 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22492 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22493 {
22494 /* Although a CVR qualifier is ignored when being applied to a
22495 substituted template parameter ([8.3.2]/1 for example), that
22496 does not allow us to unify "const T" with "int&" because both
22497 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22498 It is ok when we're allowing additional CV qualifiers
22499 at the outer level [14.8.2.1]/3,1st bullet. */
22500 if ((TYPE_REF_P (arg)
22501 || FUNC_OR_METHOD_TYPE_P (arg))
22502 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22503 return 0;
22504
22505 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22506 && (parm_quals & TYPE_QUAL_RESTRICT))
22507 return 0;
22508 }
22509
22510 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22511 && (arg_quals & parm_quals) != parm_quals)
22512 return 0;
22513
22514 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22515 && (parm_quals & arg_quals) != arg_quals)
22516 return 0;
22517
22518 return 1;
22519 }
22520
22521 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22522 void
22523 template_parm_level_and_index (tree parm, int* level, int* index)
22524 {
22525 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22526 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22527 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22528 {
22529 *index = TEMPLATE_TYPE_IDX (parm);
22530 *level = TEMPLATE_TYPE_LEVEL (parm);
22531 }
22532 else
22533 {
22534 *index = TEMPLATE_PARM_IDX (parm);
22535 *level = TEMPLATE_PARM_LEVEL (parm);
22536 }
22537 }
22538
22539 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22540 do { \
22541 if (unify (TP, TA, P, A, S, EP)) \
22542 return 1; \
22543 } while (0)
22544
22545 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22546 expansion at the end of PACKED_PARMS. Returns 0 if the type
22547 deduction succeeds, 1 otherwise. STRICT is the same as in
22548 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22549 function call argument list. We'll need to adjust the arguments to make them
22550 types. SUBR tells us if this is from a recursive call to
22551 type_unification_real, or for comparing two template argument
22552 lists. */
22553
22554 static int
22555 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22556 tree packed_args, unification_kind_t strict,
22557 bool subr, bool explain_p)
22558 {
22559 tree parm
22560 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22561 tree pattern = PACK_EXPANSION_PATTERN (parm);
22562 tree pack, packs = NULL_TREE;
22563 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22564
22565 /* Add in any args remembered from an earlier partial instantiation. */
22566 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22567 int levels = TMPL_ARGS_DEPTH (targs);
22568
22569 packed_args = expand_template_argument_pack (packed_args);
22570
22571 int len = TREE_VEC_LENGTH (packed_args);
22572
22573 /* Determine the parameter packs we will be deducing from the
22574 pattern, and record their current deductions. */
22575 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22576 pack; pack = TREE_CHAIN (pack))
22577 {
22578 tree parm_pack = TREE_VALUE (pack);
22579 int idx, level;
22580
22581 /* Only template parameter packs can be deduced, not e.g. function
22582 parameter packs or __bases or __integer_pack. */
22583 if (!TEMPLATE_PARM_P (parm_pack))
22584 continue;
22585
22586 /* Determine the index and level of this parameter pack. */
22587 template_parm_level_and_index (parm_pack, &level, &idx);
22588 if (level < levels)
22589 continue;
22590
22591 /* Keep track of the parameter packs and their corresponding
22592 argument packs. */
22593 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22594 TREE_TYPE (packs) = make_tree_vec (len - start);
22595 }
22596
22597 /* Loop through all of the arguments that have not yet been
22598 unified and unify each with the pattern. */
22599 for (i = start; i < len; i++)
22600 {
22601 tree parm;
22602 bool any_explicit = false;
22603 tree arg = TREE_VEC_ELT (packed_args, i);
22604
22605 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22606 or the element of its argument pack at the current index if
22607 this argument was explicitly specified. */
22608 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22609 {
22610 int idx, level;
22611 tree arg, pargs;
22612 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22613
22614 arg = NULL_TREE;
22615 if (TREE_VALUE (pack)
22616 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22617 && (i - start < TREE_VEC_LENGTH (pargs)))
22618 {
22619 any_explicit = true;
22620 arg = TREE_VEC_ELT (pargs, i - start);
22621 }
22622 TMPL_ARG (targs, level, idx) = arg;
22623 }
22624
22625 /* If we had explicit template arguments, substitute them into the
22626 pattern before deduction. */
22627 if (any_explicit)
22628 {
22629 /* Some arguments might still be unspecified or dependent. */
22630 bool dependent;
22631 ++processing_template_decl;
22632 dependent = any_dependent_template_arguments_p (targs);
22633 if (!dependent)
22634 --processing_template_decl;
22635 parm = tsubst (pattern, targs,
22636 explain_p ? tf_warning_or_error : tf_none,
22637 NULL_TREE);
22638 if (dependent)
22639 --processing_template_decl;
22640 if (parm == error_mark_node)
22641 return 1;
22642 }
22643 else
22644 parm = pattern;
22645
22646 /* Unify the pattern with the current argument. */
22647 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22648 explain_p))
22649 return 1;
22650
22651 /* For each parameter pack, collect the deduced value. */
22652 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22653 {
22654 int idx, level;
22655 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22656
22657 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22658 TMPL_ARG (targs, level, idx);
22659 }
22660 }
22661
22662 /* Verify that the results of unification with the parameter packs
22663 produce results consistent with what we've seen before, and make
22664 the deduced argument packs available. */
22665 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22666 {
22667 tree old_pack = TREE_VALUE (pack);
22668 tree new_args = TREE_TYPE (pack);
22669 int i, len = TREE_VEC_LENGTH (new_args);
22670 int idx, level;
22671 bool nondeduced_p = false;
22672
22673 /* By default keep the original deduced argument pack.
22674 If necessary, more specific code is going to update the
22675 resulting deduced argument later down in this function. */
22676 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22677 TMPL_ARG (targs, level, idx) = old_pack;
22678
22679 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22680 actually deduce anything. */
22681 for (i = 0; i < len && !nondeduced_p; ++i)
22682 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22683 nondeduced_p = true;
22684 if (nondeduced_p)
22685 continue;
22686
22687 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22688 {
22689 /* If we had fewer function args than explicit template args,
22690 just use the explicits. */
22691 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22692 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22693 if (len < explicit_len)
22694 new_args = explicit_args;
22695 }
22696
22697 if (!old_pack)
22698 {
22699 tree result;
22700 /* Build the deduced *_ARGUMENT_PACK. */
22701 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22702 {
22703 result = make_node (NONTYPE_ARGUMENT_PACK);
22704 TREE_CONSTANT (result) = 1;
22705 }
22706 else
22707 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22708
22709 SET_ARGUMENT_PACK_ARGS (result, new_args);
22710
22711 /* Note the deduced argument packs for this parameter
22712 pack. */
22713 TMPL_ARG (targs, level, idx) = result;
22714 }
22715 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22716 && (ARGUMENT_PACK_ARGS (old_pack)
22717 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22718 {
22719 /* We only had the explicitly-provided arguments before, but
22720 now we have a complete set of arguments. */
22721 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22722
22723 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22724 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22725 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22726 }
22727 else
22728 {
22729 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22730 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22731
22732 if (!comp_template_args (old_args, new_args,
22733 &bad_old_arg, &bad_new_arg))
22734 /* Inconsistent unification of this parameter pack. */
22735 return unify_parameter_pack_inconsistent (explain_p,
22736 bad_old_arg,
22737 bad_new_arg);
22738 }
22739 }
22740
22741 return unify_success (explain_p);
22742 }
22743
22744 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22745 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22746 parameters and return value are as for unify. */
22747
22748 static int
22749 unify_array_domain (tree tparms, tree targs,
22750 tree parm_dom, tree arg_dom,
22751 bool explain_p)
22752 {
22753 tree parm_max;
22754 tree arg_max;
22755 bool parm_cst;
22756 bool arg_cst;
22757
22758 /* Our representation of array types uses "N - 1" as the
22759 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22760 not an integer constant. We cannot unify arbitrarily
22761 complex expressions, so we eliminate the MINUS_EXPRs
22762 here. */
22763 parm_max = TYPE_MAX_VALUE (parm_dom);
22764 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22765 if (!parm_cst)
22766 {
22767 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22768 parm_max = TREE_OPERAND (parm_max, 0);
22769 }
22770 arg_max = TYPE_MAX_VALUE (arg_dom);
22771 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22772 if (!arg_cst)
22773 {
22774 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22775 trying to unify the type of a variable with the type
22776 of a template parameter. For example:
22777
22778 template <unsigned int N>
22779 void f (char (&) [N]);
22780 int g();
22781 void h(int i) {
22782 char a[g(i)];
22783 f(a);
22784 }
22785
22786 Here, the type of the ARG will be "int [g(i)]", and
22787 may be a SAVE_EXPR, etc. */
22788 if (TREE_CODE (arg_max) != MINUS_EXPR)
22789 return unify_vla_arg (explain_p, arg_dom);
22790 arg_max = TREE_OPERAND (arg_max, 0);
22791 }
22792
22793 /* If only one of the bounds used a MINUS_EXPR, compensate
22794 by adding one to the other bound. */
22795 if (parm_cst && !arg_cst)
22796 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22797 integer_type_node,
22798 parm_max,
22799 integer_one_node);
22800 else if (arg_cst && !parm_cst)
22801 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22802 integer_type_node,
22803 arg_max,
22804 integer_one_node);
22805
22806 return unify (tparms, targs, parm_max, arg_max,
22807 UNIFY_ALLOW_INTEGER, explain_p);
22808 }
22809
22810 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22811
22812 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22813
22814 static pa_kind_t
22815 pa_kind (tree t)
22816 {
22817 if (PACK_EXPANSION_P (t))
22818 t = PACK_EXPANSION_PATTERN (t);
22819 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22820 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22821 || DECL_TYPE_TEMPLATE_P (t))
22822 return pa_tmpl;
22823 else if (TYPE_P (t))
22824 return pa_type;
22825 else
22826 return pa_expr;
22827 }
22828
22829 /* Deduce the value of template parameters. TPARMS is the (innermost)
22830 set of template parameters to a template. TARGS is the bindings
22831 for those template parameters, as determined thus far; TARGS may
22832 include template arguments for outer levels of template parameters
22833 as well. PARM is a parameter to a template function, or a
22834 subcomponent of that parameter; ARG is the corresponding argument.
22835 This function attempts to match PARM with ARG in a manner
22836 consistent with the existing assignments in TARGS. If more values
22837 are deduced, then TARGS is updated.
22838
22839 Returns 0 if the type deduction succeeds, 1 otherwise. The
22840 parameter STRICT is a bitwise or of the following flags:
22841
22842 UNIFY_ALLOW_NONE:
22843 Require an exact match between PARM and ARG.
22844 UNIFY_ALLOW_MORE_CV_QUAL:
22845 Allow the deduced ARG to be more cv-qualified (by qualification
22846 conversion) than ARG.
22847 UNIFY_ALLOW_LESS_CV_QUAL:
22848 Allow the deduced ARG to be less cv-qualified than ARG.
22849 UNIFY_ALLOW_DERIVED:
22850 Allow the deduced ARG to be a template base class of ARG,
22851 or a pointer to a template base class of the type pointed to by
22852 ARG.
22853 UNIFY_ALLOW_INTEGER:
22854 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22855 case for more information.
22856 UNIFY_ALLOW_OUTER_LEVEL:
22857 This is the outermost level of a deduction. Used to determine validity
22858 of qualification conversions. A valid qualification conversion must
22859 have const qualified pointers leading up to the inner type which
22860 requires additional CV quals, except at the outer level, where const
22861 is not required [conv.qual]. It would be normal to set this flag in
22862 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22863 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22864 This is the outermost level of a deduction, and PARM can be more CV
22865 qualified at this point.
22866 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22867 This is the outermost level of a deduction, and PARM can be less CV
22868 qualified at this point. */
22869
22870 static int
22871 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22872 bool explain_p)
22873 {
22874 int idx;
22875 tree targ;
22876 tree tparm;
22877 int strict_in = strict;
22878 tsubst_flags_t complain = (explain_p
22879 ? tf_warning_or_error
22880 : tf_none);
22881
22882 /* I don't think this will do the right thing with respect to types.
22883 But the only case I've seen it in so far has been array bounds, where
22884 signedness is the only information lost, and I think that will be
22885 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22886 finish_id_expression_1, and are also OK. */
22887 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
22888 parm = TREE_OPERAND (parm, 0);
22889
22890 if (arg == error_mark_node)
22891 return unify_invalid (explain_p);
22892 if (arg == unknown_type_node
22893 || arg == init_list_type_node)
22894 /* We can't deduce anything from this, but we might get all the
22895 template args from other function args. */
22896 return unify_success (explain_p);
22897
22898 if (parm == any_targ_node || arg == any_targ_node)
22899 return unify_success (explain_p);
22900
22901 /* If PARM uses template parameters, then we can't bail out here,
22902 even if ARG == PARM, since we won't record unifications for the
22903 template parameters. We might need them if we're trying to
22904 figure out which of two things is more specialized. */
22905 if (arg == parm && !uses_template_parms (parm))
22906 return unify_success (explain_p);
22907
22908 /* Handle init lists early, so the rest of the function can assume
22909 we're dealing with a type. */
22910 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22911 {
22912 tree elt, elttype;
22913 unsigned i;
22914 tree orig_parm = parm;
22915
22916 if (!is_std_init_list (parm)
22917 && TREE_CODE (parm) != ARRAY_TYPE)
22918 /* We can only deduce from an initializer list argument if the
22919 parameter is std::initializer_list or an array; otherwise this
22920 is a non-deduced context. */
22921 return unify_success (explain_p);
22922
22923 if (TREE_CODE (parm) == ARRAY_TYPE)
22924 elttype = TREE_TYPE (parm);
22925 else
22926 {
22927 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22928 /* Deduction is defined in terms of a single type, so just punt
22929 on the (bizarre) std::initializer_list<T...>. */
22930 if (PACK_EXPANSION_P (elttype))
22931 return unify_success (explain_p);
22932 }
22933
22934 if (strict != DEDUCE_EXACT
22935 && TYPE_P (elttype)
22936 && !uses_deducible_template_parms (elttype))
22937 /* If ELTTYPE has no deducible template parms, skip deduction from
22938 the list elements. */;
22939 else
22940 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22941 {
22942 int elt_strict = strict;
22943
22944 if (elt == error_mark_node)
22945 return unify_invalid (explain_p);
22946
22947 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22948 {
22949 tree type = TREE_TYPE (elt);
22950 if (type == error_mark_node)
22951 return unify_invalid (explain_p);
22952 /* It should only be possible to get here for a call. */
22953 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22954 elt_strict |= maybe_adjust_types_for_deduction
22955 (DEDUCE_CALL, &elttype, &type, elt);
22956 elt = type;
22957 }
22958
22959 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22960 explain_p);
22961 }
22962
22963 if (TREE_CODE (parm) == ARRAY_TYPE
22964 && deducible_array_bound (TYPE_DOMAIN (parm)))
22965 {
22966 /* Also deduce from the length of the initializer list. */
22967 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22968 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22969 if (idx == error_mark_node)
22970 return unify_invalid (explain_p);
22971 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22972 idx, explain_p);
22973 }
22974
22975 /* If the std::initializer_list<T> deduction worked, replace the
22976 deduced A with std::initializer_list<A>. */
22977 if (orig_parm != parm)
22978 {
22979 idx = TEMPLATE_TYPE_IDX (orig_parm);
22980 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22981 targ = listify (targ);
22982 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22983 }
22984 return unify_success (explain_p);
22985 }
22986
22987 /* If parm and arg aren't the same kind of thing (template, type, or
22988 expression), fail early. */
22989 if (pa_kind (parm) != pa_kind (arg))
22990 return unify_invalid (explain_p);
22991
22992 /* Immediately reject some pairs that won't unify because of
22993 cv-qualification mismatches. */
22994 if (TREE_CODE (arg) == TREE_CODE (parm)
22995 && TYPE_P (arg)
22996 /* It is the elements of the array which hold the cv quals of an array
22997 type, and the elements might be template type parms. We'll check
22998 when we recurse. */
22999 && TREE_CODE (arg) != ARRAY_TYPE
23000 /* We check the cv-qualifiers when unifying with template type
23001 parameters below. We want to allow ARG `const T' to unify with
23002 PARM `T' for example, when computing which of two templates
23003 is more specialized, for example. */
23004 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23005 && !check_cv_quals_for_unify (strict_in, arg, parm))
23006 return unify_cv_qual_mismatch (explain_p, parm, arg);
23007
23008 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23009 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23010 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23011 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23012 strict &= ~UNIFY_ALLOW_DERIVED;
23013 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23014 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23015
23016 switch (TREE_CODE (parm))
23017 {
23018 case TYPENAME_TYPE:
23019 case SCOPE_REF:
23020 case UNBOUND_CLASS_TEMPLATE:
23021 /* In a type which contains a nested-name-specifier, template
23022 argument values cannot be deduced for template parameters used
23023 within the nested-name-specifier. */
23024 return unify_success (explain_p);
23025
23026 case TEMPLATE_TYPE_PARM:
23027 case TEMPLATE_TEMPLATE_PARM:
23028 case BOUND_TEMPLATE_TEMPLATE_PARM:
23029 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23030 if (error_operand_p (tparm))
23031 return unify_invalid (explain_p);
23032
23033 if (TEMPLATE_TYPE_LEVEL (parm)
23034 != template_decl_level (tparm))
23035 /* The PARM is not one we're trying to unify. Just check
23036 to see if it matches ARG. */
23037 {
23038 if (TREE_CODE (arg) == TREE_CODE (parm)
23039 && (is_auto (parm) ? is_auto (arg)
23040 : same_type_p (parm, arg)))
23041 return unify_success (explain_p);
23042 else
23043 return unify_type_mismatch (explain_p, parm, arg);
23044 }
23045 idx = TEMPLATE_TYPE_IDX (parm);
23046 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23047 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23048 if (error_operand_p (tparm))
23049 return unify_invalid (explain_p);
23050
23051 /* Check for mixed types and values. */
23052 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23053 && TREE_CODE (tparm) != TYPE_DECL)
23054 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23055 && TREE_CODE (tparm) != TEMPLATE_DECL))
23056 gcc_unreachable ();
23057
23058 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23059 {
23060 if ((strict_in & UNIFY_ALLOW_DERIVED)
23061 && CLASS_TYPE_P (arg))
23062 {
23063 /* First try to match ARG directly. */
23064 tree t = try_class_unification (tparms, targs, parm, arg,
23065 explain_p);
23066 if (!t)
23067 {
23068 /* Otherwise, look for a suitable base of ARG, as below. */
23069 enum template_base_result r;
23070 r = get_template_base (tparms, targs, parm, arg,
23071 explain_p, &t);
23072 if (!t)
23073 return unify_no_common_base (explain_p, r, parm, arg);
23074 arg = t;
23075 }
23076 }
23077 /* ARG must be constructed from a template class or a template
23078 template parameter. */
23079 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23080 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23081 return unify_template_deduction_failure (explain_p, parm, arg);
23082
23083 /* Deduce arguments T, i from TT<T> or TT<i>. */
23084 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23085 return 1;
23086
23087 arg = TYPE_TI_TEMPLATE (arg);
23088
23089 /* Fall through to deduce template name. */
23090 }
23091
23092 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23093 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23094 {
23095 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23096
23097 /* Simple cases: Value already set, does match or doesn't. */
23098 if (targ != NULL_TREE && template_args_equal (targ, arg))
23099 return unify_success (explain_p);
23100 else if (targ)
23101 return unify_inconsistency (explain_p, parm, targ, arg);
23102 }
23103 else
23104 {
23105 /* If PARM is `const T' and ARG is only `int', we don't have
23106 a match unless we are allowing additional qualification.
23107 If ARG is `const int' and PARM is just `T' that's OK;
23108 that binds `const int' to `T'. */
23109 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23110 arg, parm))
23111 return unify_cv_qual_mismatch (explain_p, parm, arg);
23112
23113 /* Consider the case where ARG is `const volatile int' and
23114 PARM is `const T'. Then, T should be `volatile int'. */
23115 arg = cp_build_qualified_type_real
23116 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23117 if (arg == error_mark_node)
23118 return unify_invalid (explain_p);
23119
23120 /* Simple cases: Value already set, does match or doesn't. */
23121 if (targ != NULL_TREE && same_type_p (targ, arg))
23122 return unify_success (explain_p);
23123 else if (targ)
23124 return unify_inconsistency (explain_p, parm, targ, arg);
23125
23126 /* Make sure that ARG is not a variable-sized array. (Note
23127 that were talking about variable-sized arrays (like
23128 `int[n]'), rather than arrays of unknown size (like
23129 `int[]').) We'll get very confused by such a type since
23130 the bound of the array is not constant, and therefore
23131 not mangleable. Besides, such types are not allowed in
23132 ISO C++, so we can do as we please here. We do allow
23133 them for 'auto' deduction, since that isn't ABI-exposed. */
23134 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23135 return unify_vla_arg (explain_p, arg);
23136
23137 /* Strip typedefs as in convert_template_argument. */
23138 arg = canonicalize_type_argument (arg, tf_none);
23139 }
23140
23141 /* If ARG is a parameter pack or an expansion, we cannot unify
23142 against it unless PARM is also a parameter pack. */
23143 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23144 && !template_parameter_pack_p (parm))
23145 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23146
23147 /* If the argument deduction results is a METHOD_TYPE,
23148 then there is a problem.
23149 METHOD_TYPE doesn't map to any real C++ type the result of
23150 the deduction cannot be of that type. */
23151 if (TREE_CODE (arg) == METHOD_TYPE)
23152 return unify_method_type_error (explain_p, arg);
23153
23154 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23155 return unify_success (explain_p);
23156
23157 case TEMPLATE_PARM_INDEX:
23158 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23159 if (error_operand_p (tparm))
23160 return unify_invalid (explain_p);
23161
23162 if (TEMPLATE_PARM_LEVEL (parm)
23163 != template_decl_level (tparm))
23164 {
23165 /* The PARM is not one we're trying to unify. Just check
23166 to see if it matches ARG. */
23167 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23168 && cp_tree_equal (parm, arg));
23169 if (result)
23170 unify_expression_unequal (explain_p, parm, arg);
23171 return result;
23172 }
23173
23174 idx = TEMPLATE_PARM_IDX (parm);
23175 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23176
23177 if (targ)
23178 {
23179 if ((strict & UNIFY_ALLOW_INTEGER)
23180 && TREE_TYPE (targ) && TREE_TYPE (arg)
23181 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23182 /* We're deducing from an array bound, the type doesn't matter. */
23183 arg = fold_convert (TREE_TYPE (targ), arg);
23184 int x = !cp_tree_equal (targ, arg);
23185 if (x)
23186 unify_inconsistency (explain_p, parm, targ, arg);
23187 return x;
23188 }
23189
23190 /* [temp.deduct.type] If, in the declaration of a function template
23191 with a non-type template-parameter, the non-type
23192 template-parameter is used in an expression in the function
23193 parameter-list and, if the corresponding template-argument is
23194 deduced, the template-argument type shall match the type of the
23195 template-parameter exactly, except that a template-argument
23196 deduced from an array bound may be of any integral type.
23197 The non-type parameter might use already deduced type parameters. */
23198 tparm = TREE_TYPE (parm);
23199 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23200 /* We don't have enough levels of args to do any substitution. This
23201 can happen in the context of -fnew-ttp-matching. */;
23202 else
23203 {
23204 ++processing_template_decl;
23205 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23206 --processing_template_decl;
23207
23208 if (tree a = type_uses_auto (tparm))
23209 {
23210 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23211 if (tparm == error_mark_node)
23212 return 1;
23213 }
23214 }
23215
23216 if (!TREE_TYPE (arg))
23217 /* Template-parameter dependent expression. Just accept it for now.
23218 It will later be processed in convert_template_argument. */
23219 ;
23220 else if (same_type_ignoring_top_level_qualifiers_p
23221 (non_reference (TREE_TYPE (arg)),
23222 non_reference (tparm)))
23223 /* OK. Ignore top-level quals here because a class-type template
23224 parameter object is const. */;
23225 else if ((strict & UNIFY_ALLOW_INTEGER)
23226 && CP_INTEGRAL_TYPE_P (tparm))
23227 /* Convert the ARG to the type of PARM; the deduced non-type
23228 template argument must exactly match the types of the
23229 corresponding parameter. */
23230 arg = fold (build_nop (tparm, arg));
23231 else if (uses_template_parms (tparm))
23232 {
23233 /* We haven't deduced the type of this parameter yet. */
23234 if (cxx_dialect >= cxx17
23235 /* We deduce from array bounds in try_array_deduction. */
23236 && !(strict & UNIFY_ALLOW_INTEGER))
23237 {
23238 /* Deduce it from the non-type argument. */
23239 tree atype = TREE_TYPE (arg);
23240 RECUR_AND_CHECK_FAILURE (tparms, targs,
23241 tparm, atype,
23242 UNIFY_ALLOW_NONE, explain_p);
23243 }
23244 else
23245 /* Try again later. */
23246 return unify_success (explain_p);
23247 }
23248 else
23249 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23250
23251 /* If ARG is a parameter pack or an expansion, we cannot unify
23252 against it unless PARM is also a parameter pack. */
23253 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23254 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23255 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23256
23257 {
23258 bool removed_attr = false;
23259 arg = strip_typedefs_expr (arg, &removed_attr);
23260 }
23261 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23262 return unify_success (explain_p);
23263
23264 case PTRMEM_CST:
23265 {
23266 /* A pointer-to-member constant can be unified only with
23267 another constant. */
23268 if (TREE_CODE (arg) != PTRMEM_CST)
23269 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23270
23271 /* Just unify the class member. It would be useless (and possibly
23272 wrong, depending on the strict flags) to unify also
23273 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23274 arg refer to the same variable, even if through different
23275 classes. For instance:
23276
23277 struct A { int x; };
23278 struct B : A { };
23279
23280 Unification of &A::x and &B::x must succeed. */
23281 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23282 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23283 }
23284
23285 case POINTER_TYPE:
23286 {
23287 if (!TYPE_PTR_P (arg))
23288 return unify_type_mismatch (explain_p, parm, arg);
23289
23290 /* [temp.deduct.call]
23291
23292 A can be another pointer or pointer to member type that can
23293 be converted to the deduced A via a qualification
23294 conversion (_conv.qual_).
23295
23296 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23297 This will allow for additional cv-qualification of the
23298 pointed-to types if appropriate. */
23299
23300 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23301 /* The derived-to-base conversion only persists through one
23302 level of pointers. */
23303 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23304
23305 return unify (tparms, targs, TREE_TYPE (parm),
23306 TREE_TYPE (arg), strict, explain_p);
23307 }
23308
23309 case REFERENCE_TYPE:
23310 if (!TYPE_REF_P (arg))
23311 return unify_type_mismatch (explain_p, parm, arg);
23312 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23313 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23314
23315 case ARRAY_TYPE:
23316 if (TREE_CODE (arg) != ARRAY_TYPE)
23317 return unify_type_mismatch (explain_p, parm, arg);
23318 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23319 != (TYPE_DOMAIN (arg) == NULL_TREE))
23320 return unify_type_mismatch (explain_p, parm, arg);
23321 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23322 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23323 if (TYPE_DOMAIN (parm) != NULL_TREE)
23324 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23325 TYPE_DOMAIN (arg), explain_p);
23326 return unify_success (explain_p);
23327
23328 case REAL_TYPE:
23329 case COMPLEX_TYPE:
23330 case VECTOR_TYPE:
23331 case INTEGER_TYPE:
23332 case BOOLEAN_TYPE:
23333 case ENUMERAL_TYPE:
23334 case VOID_TYPE:
23335 case NULLPTR_TYPE:
23336 if (TREE_CODE (arg) != TREE_CODE (parm))
23337 return unify_type_mismatch (explain_p, parm, arg);
23338
23339 /* We have already checked cv-qualification at the top of the
23340 function. */
23341 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23342 return unify_type_mismatch (explain_p, parm, arg);
23343
23344 /* As far as unification is concerned, this wins. Later checks
23345 will invalidate it if necessary. */
23346 return unify_success (explain_p);
23347
23348 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23349 /* Type INTEGER_CST can come from ordinary constant template args. */
23350 case INTEGER_CST:
23351 while (CONVERT_EXPR_P (arg))
23352 arg = TREE_OPERAND (arg, 0);
23353
23354 if (TREE_CODE (arg) != INTEGER_CST)
23355 return unify_template_argument_mismatch (explain_p, parm, arg);
23356 return (tree_int_cst_equal (parm, arg)
23357 ? unify_success (explain_p)
23358 : unify_template_argument_mismatch (explain_p, parm, arg));
23359
23360 case TREE_VEC:
23361 {
23362 int i, len, argslen;
23363 int parm_variadic_p = 0;
23364
23365 if (TREE_CODE (arg) != TREE_VEC)
23366 return unify_template_argument_mismatch (explain_p, parm, arg);
23367
23368 len = TREE_VEC_LENGTH (parm);
23369 argslen = TREE_VEC_LENGTH (arg);
23370
23371 /* Check for pack expansions in the parameters. */
23372 for (i = 0; i < len; ++i)
23373 {
23374 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23375 {
23376 if (i == len - 1)
23377 /* We can unify against something with a trailing
23378 parameter pack. */
23379 parm_variadic_p = 1;
23380 else
23381 /* [temp.deduct.type]/9: If the template argument list of
23382 P contains a pack expansion that is not the last
23383 template argument, the entire template argument list
23384 is a non-deduced context. */
23385 return unify_success (explain_p);
23386 }
23387 }
23388
23389 /* If we don't have enough arguments to satisfy the parameters
23390 (not counting the pack expression at the end), or we have
23391 too many arguments for a parameter list that doesn't end in
23392 a pack expression, we can't unify. */
23393 if (parm_variadic_p
23394 ? argslen < len - parm_variadic_p
23395 : argslen != len)
23396 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23397
23398 /* Unify all of the parameters that precede the (optional)
23399 pack expression. */
23400 for (i = 0; i < len - parm_variadic_p; ++i)
23401 {
23402 RECUR_AND_CHECK_FAILURE (tparms, targs,
23403 TREE_VEC_ELT (parm, i),
23404 TREE_VEC_ELT (arg, i),
23405 UNIFY_ALLOW_NONE, explain_p);
23406 }
23407 if (parm_variadic_p)
23408 return unify_pack_expansion (tparms, targs, parm, arg,
23409 DEDUCE_EXACT,
23410 /*subr=*/true, explain_p);
23411 return unify_success (explain_p);
23412 }
23413
23414 case RECORD_TYPE:
23415 case UNION_TYPE:
23416 if (TREE_CODE (arg) != TREE_CODE (parm))
23417 return unify_type_mismatch (explain_p, parm, arg);
23418
23419 if (TYPE_PTRMEMFUNC_P (parm))
23420 {
23421 if (!TYPE_PTRMEMFUNC_P (arg))
23422 return unify_type_mismatch (explain_p, parm, arg);
23423
23424 return unify (tparms, targs,
23425 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23426 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23427 strict, explain_p);
23428 }
23429 else if (TYPE_PTRMEMFUNC_P (arg))
23430 return unify_type_mismatch (explain_p, parm, arg);
23431
23432 if (CLASSTYPE_TEMPLATE_INFO (parm))
23433 {
23434 tree t = NULL_TREE;
23435
23436 if (strict_in & UNIFY_ALLOW_DERIVED)
23437 {
23438 /* First, we try to unify the PARM and ARG directly. */
23439 t = try_class_unification (tparms, targs,
23440 parm, arg, explain_p);
23441
23442 if (!t)
23443 {
23444 /* Fallback to the special case allowed in
23445 [temp.deduct.call]:
23446
23447 If P is a class, and P has the form
23448 template-id, then A can be a derived class of
23449 the deduced A. Likewise, if P is a pointer to
23450 a class of the form template-id, A can be a
23451 pointer to a derived class pointed to by the
23452 deduced A. */
23453 enum template_base_result r;
23454 r = get_template_base (tparms, targs, parm, arg,
23455 explain_p, &t);
23456
23457 if (!t)
23458 {
23459 /* Don't give the derived diagnostic if we're
23460 already dealing with the same template. */
23461 bool same_template
23462 = (CLASSTYPE_TEMPLATE_INFO (arg)
23463 && (CLASSTYPE_TI_TEMPLATE (parm)
23464 == CLASSTYPE_TI_TEMPLATE (arg)));
23465 return unify_no_common_base (explain_p && !same_template,
23466 r, parm, arg);
23467 }
23468 }
23469 }
23470 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23471 && (CLASSTYPE_TI_TEMPLATE (parm)
23472 == CLASSTYPE_TI_TEMPLATE (arg)))
23473 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23474 Then, we should unify `int' and `U'. */
23475 t = arg;
23476 else
23477 /* There's no chance of unification succeeding. */
23478 return unify_type_mismatch (explain_p, parm, arg);
23479
23480 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23481 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23482 }
23483 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23484 return unify_type_mismatch (explain_p, parm, arg);
23485 return unify_success (explain_p);
23486
23487 case METHOD_TYPE:
23488 case FUNCTION_TYPE:
23489 {
23490 unsigned int nargs;
23491 tree *args;
23492 tree a;
23493 unsigned int i;
23494
23495 if (TREE_CODE (arg) != TREE_CODE (parm))
23496 return unify_type_mismatch (explain_p, parm, arg);
23497
23498 /* CV qualifications for methods can never be deduced, they must
23499 match exactly. We need to check them explicitly here,
23500 because type_unification_real treats them as any other
23501 cv-qualified parameter. */
23502 if (TREE_CODE (parm) == METHOD_TYPE
23503 && (!check_cv_quals_for_unify
23504 (UNIFY_ALLOW_NONE,
23505 class_of_this_parm (arg),
23506 class_of_this_parm (parm))))
23507 return unify_cv_qual_mismatch (explain_p, parm, arg);
23508 if (TREE_CODE (arg) == FUNCTION_TYPE
23509 && type_memfn_quals (parm) != type_memfn_quals (arg))
23510 return unify_cv_qual_mismatch (explain_p, parm, arg);
23511 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23512 return unify_type_mismatch (explain_p, parm, arg);
23513
23514 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23515 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23516
23517 nargs = list_length (TYPE_ARG_TYPES (arg));
23518 args = XALLOCAVEC (tree, nargs);
23519 for (a = TYPE_ARG_TYPES (arg), i = 0;
23520 a != NULL_TREE && a != void_list_node;
23521 a = TREE_CHAIN (a), ++i)
23522 args[i] = TREE_VALUE (a);
23523 nargs = i;
23524
23525 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23526 args, nargs, 1, DEDUCE_EXACT,
23527 NULL, explain_p))
23528 return 1;
23529
23530 if (flag_noexcept_type)
23531 {
23532 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23533 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23534 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23535 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23536 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23537 && uses_template_parms (TREE_PURPOSE (pspec)))
23538 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23539 TREE_PURPOSE (aspec),
23540 UNIFY_ALLOW_NONE, explain_p);
23541 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23542 return unify_type_mismatch (explain_p, parm, arg);
23543 }
23544
23545 return 0;
23546 }
23547
23548 case OFFSET_TYPE:
23549 /* Unify a pointer to member with a pointer to member function, which
23550 deduces the type of the member as a function type. */
23551 if (TYPE_PTRMEMFUNC_P (arg))
23552 {
23553 /* Check top-level cv qualifiers */
23554 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23555 return unify_cv_qual_mismatch (explain_p, parm, arg);
23556
23557 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23558 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23559 UNIFY_ALLOW_NONE, explain_p);
23560
23561 /* Determine the type of the function we are unifying against. */
23562 tree fntype = static_fn_type (arg);
23563
23564 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23565 }
23566
23567 if (TREE_CODE (arg) != OFFSET_TYPE)
23568 return unify_type_mismatch (explain_p, parm, arg);
23569 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23570 TYPE_OFFSET_BASETYPE (arg),
23571 UNIFY_ALLOW_NONE, explain_p);
23572 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23573 strict, explain_p);
23574
23575 case CONST_DECL:
23576 if (DECL_TEMPLATE_PARM_P (parm))
23577 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23578 if (arg != scalar_constant_value (parm))
23579 return unify_template_argument_mismatch (explain_p, parm, arg);
23580 return unify_success (explain_p);
23581
23582 case FIELD_DECL:
23583 case TEMPLATE_DECL:
23584 /* Matched cases are handled by the ARG == PARM test above. */
23585 return unify_template_argument_mismatch (explain_p, parm, arg);
23586
23587 case VAR_DECL:
23588 /* We might get a variable as a non-type template argument in parm if the
23589 corresponding parameter is type-dependent. Make any necessary
23590 adjustments based on whether arg is a reference. */
23591 if (CONSTANT_CLASS_P (arg))
23592 parm = fold_non_dependent_expr (parm, complain);
23593 else if (REFERENCE_REF_P (arg))
23594 {
23595 tree sub = TREE_OPERAND (arg, 0);
23596 STRIP_NOPS (sub);
23597 if (TREE_CODE (sub) == ADDR_EXPR)
23598 arg = TREE_OPERAND (sub, 0);
23599 }
23600 /* Now use the normal expression code to check whether they match. */
23601 goto expr;
23602
23603 case TYPE_ARGUMENT_PACK:
23604 case NONTYPE_ARGUMENT_PACK:
23605 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23606 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23607
23608 case TYPEOF_TYPE:
23609 case DECLTYPE_TYPE:
23610 case UNDERLYING_TYPE:
23611 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23612 or UNDERLYING_TYPE nodes. */
23613 return unify_success (explain_p);
23614
23615 case ERROR_MARK:
23616 /* Unification fails if we hit an error node. */
23617 return unify_invalid (explain_p);
23618
23619 case INDIRECT_REF:
23620 if (REFERENCE_REF_P (parm))
23621 {
23622 bool pexp = PACK_EXPANSION_P (arg);
23623 if (pexp)
23624 arg = PACK_EXPANSION_PATTERN (arg);
23625 if (REFERENCE_REF_P (arg))
23626 arg = TREE_OPERAND (arg, 0);
23627 if (pexp)
23628 arg = make_pack_expansion (arg, complain);
23629 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23630 strict, explain_p);
23631 }
23632 /* FALLTHRU */
23633
23634 default:
23635 /* An unresolved overload is a nondeduced context. */
23636 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23637 return unify_success (explain_p);
23638 gcc_assert (EXPR_P (parm)
23639 || COMPOUND_LITERAL_P (parm)
23640 || TREE_CODE (parm) == TRAIT_EXPR);
23641 expr:
23642 /* We must be looking at an expression. This can happen with
23643 something like:
23644
23645 template <int I>
23646 void foo(S<I>, S<I + 2>);
23647
23648 or
23649
23650 template<typename T>
23651 void foo(A<T, T{}>);
23652
23653 This is a "non-deduced context":
23654
23655 [deduct.type]
23656
23657 The non-deduced contexts are:
23658
23659 --A non-type template argument or an array bound in which
23660 a subexpression references a template parameter.
23661
23662 In these cases, we assume deduction succeeded, but don't
23663 actually infer any unifications. */
23664
23665 if (!uses_template_parms (parm)
23666 && !template_args_equal (parm, arg))
23667 return unify_expression_unequal (explain_p, parm, arg);
23668 else
23669 return unify_success (explain_p);
23670 }
23671 }
23672 #undef RECUR_AND_CHECK_FAILURE
23673 \f
23674 /* Note that DECL can be defined in this translation unit, if
23675 required. */
23676
23677 static void
23678 mark_definable (tree decl)
23679 {
23680 tree clone;
23681 DECL_NOT_REALLY_EXTERN (decl) = 1;
23682 FOR_EACH_CLONE (clone, decl)
23683 DECL_NOT_REALLY_EXTERN (clone) = 1;
23684 }
23685
23686 /* Called if RESULT is explicitly instantiated, or is a member of an
23687 explicitly instantiated class. */
23688
23689 void
23690 mark_decl_instantiated (tree result, int extern_p)
23691 {
23692 SET_DECL_EXPLICIT_INSTANTIATION (result);
23693
23694 /* If this entity has already been written out, it's too late to
23695 make any modifications. */
23696 if (TREE_ASM_WRITTEN (result))
23697 return;
23698
23699 /* For anonymous namespace we don't need to do anything. */
23700 if (decl_anon_ns_mem_p (result))
23701 {
23702 gcc_assert (!TREE_PUBLIC (result));
23703 return;
23704 }
23705
23706 if (TREE_CODE (result) != FUNCTION_DECL)
23707 /* The TREE_PUBLIC flag for function declarations will have been
23708 set correctly by tsubst. */
23709 TREE_PUBLIC (result) = 1;
23710
23711 /* This might have been set by an earlier implicit instantiation. */
23712 DECL_COMDAT (result) = 0;
23713
23714 if (extern_p)
23715 DECL_NOT_REALLY_EXTERN (result) = 0;
23716 else
23717 {
23718 mark_definable (result);
23719 mark_needed (result);
23720 /* Always make artificials weak. */
23721 if (DECL_ARTIFICIAL (result) && flag_weak)
23722 comdat_linkage (result);
23723 /* For WIN32 we also want to put explicit instantiations in
23724 linkonce sections. */
23725 else if (TREE_PUBLIC (result))
23726 maybe_make_one_only (result);
23727 if (TREE_CODE (result) == FUNCTION_DECL
23728 && DECL_TEMPLATE_INSTANTIATED (result))
23729 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23730 since start_preparsed_function wouldn't have if we had an earlier
23731 extern explicit instantiation. */
23732 DECL_EXTERNAL (result) = 0;
23733 }
23734
23735 /* If EXTERN_P, then this function will not be emitted -- unless
23736 followed by an explicit instantiation, at which point its linkage
23737 will be adjusted. If !EXTERN_P, then this function will be
23738 emitted here. In neither circumstance do we want
23739 import_export_decl to adjust the linkage. */
23740 DECL_INTERFACE_KNOWN (result) = 1;
23741 }
23742
23743 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23744 important template arguments. If any are missing, we check whether
23745 they're important by using error_mark_node for substituting into any
23746 args that were used for partial ordering (the ones between ARGS and END)
23747 and seeing if it bubbles up. */
23748
23749 static bool
23750 check_undeduced_parms (tree targs, tree args, tree end)
23751 {
23752 bool found = false;
23753 int i;
23754 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23755 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23756 {
23757 found = true;
23758 TREE_VEC_ELT (targs, i) = error_mark_node;
23759 }
23760 if (found)
23761 {
23762 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23763 if (substed == error_mark_node)
23764 return true;
23765 }
23766 return false;
23767 }
23768
23769 /* Given two function templates PAT1 and PAT2, return:
23770
23771 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23772 -1 if PAT2 is more specialized than PAT1.
23773 0 if neither is more specialized.
23774
23775 LEN indicates the number of parameters we should consider
23776 (defaulted parameters should not be considered).
23777
23778 The 1998 std underspecified function template partial ordering, and
23779 DR214 addresses the issue. We take pairs of arguments, one from
23780 each of the templates, and deduce them against each other. One of
23781 the templates will be more specialized if all the *other*
23782 template's arguments deduce against its arguments and at least one
23783 of its arguments *does* *not* deduce against the other template's
23784 corresponding argument. Deduction is done as for class templates.
23785 The arguments used in deduction have reference and top level cv
23786 qualifiers removed. Iff both arguments were originally reference
23787 types *and* deduction succeeds in both directions, an lvalue reference
23788 wins against an rvalue reference and otherwise the template
23789 with the more cv-qualified argument wins for that pairing (if
23790 neither is more cv-qualified, they both are equal). Unlike regular
23791 deduction, after all the arguments have been deduced in this way,
23792 we do *not* verify the deduced template argument values can be
23793 substituted into non-deduced contexts.
23794
23795 The logic can be a bit confusing here, because we look at deduce1 and
23796 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23797 can find template arguments for pat1 to make arg1 look like arg2, that
23798 means that arg2 is at least as specialized as arg1. */
23799
23800 int
23801 more_specialized_fn (tree pat1, tree pat2, int len)
23802 {
23803 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23804 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23805 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23806 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23807 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23808 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23809 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23810 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23811 tree origs1, origs2;
23812 bool lose1 = false;
23813 bool lose2 = false;
23814
23815 /* Remove the this parameter from non-static member functions. If
23816 one is a non-static member function and the other is not a static
23817 member function, remove the first parameter from that function
23818 also. This situation occurs for operator functions where we
23819 locate both a member function (with this pointer) and non-member
23820 operator (with explicit first operand). */
23821 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23822 {
23823 len--; /* LEN is the number of significant arguments for DECL1 */
23824 args1 = TREE_CHAIN (args1);
23825 if (!DECL_STATIC_FUNCTION_P (decl2))
23826 args2 = TREE_CHAIN (args2);
23827 }
23828 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
23829 {
23830 args2 = TREE_CHAIN (args2);
23831 if (!DECL_STATIC_FUNCTION_P (decl1))
23832 {
23833 len--;
23834 args1 = TREE_CHAIN (args1);
23835 }
23836 }
23837
23838 /* If only one is a conversion operator, they are unordered. */
23839 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23840 return 0;
23841
23842 /* Consider the return type for a conversion function */
23843 if (DECL_CONV_FN_P (decl1))
23844 {
23845 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23846 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23847 len++;
23848 }
23849
23850 processing_template_decl++;
23851
23852 origs1 = args1;
23853 origs2 = args2;
23854
23855 while (len--
23856 /* Stop when an ellipsis is seen. */
23857 && args1 != NULL_TREE && args2 != NULL_TREE)
23858 {
23859 tree arg1 = TREE_VALUE (args1);
23860 tree arg2 = TREE_VALUE (args2);
23861 int deduce1, deduce2;
23862 int quals1 = -1;
23863 int quals2 = -1;
23864 int ref1 = 0;
23865 int ref2 = 0;
23866
23867 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23868 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23869 {
23870 /* When both arguments are pack expansions, we need only
23871 unify the patterns themselves. */
23872 arg1 = PACK_EXPANSION_PATTERN (arg1);
23873 arg2 = PACK_EXPANSION_PATTERN (arg2);
23874
23875 /* This is the last comparison we need to do. */
23876 len = 0;
23877 }
23878
23879 if (TYPE_REF_P (arg1))
23880 {
23881 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23882 arg1 = TREE_TYPE (arg1);
23883 quals1 = cp_type_quals (arg1);
23884 }
23885
23886 if (TYPE_REF_P (arg2))
23887 {
23888 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23889 arg2 = TREE_TYPE (arg2);
23890 quals2 = cp_type_quals (arg2);
23891 }
23892
23893 arg1 = TYPE_MAIN_VARIANT (arg1);
23894 arg2 = TYPE_MAIN_VARIANT (arg2);
23895
23896 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23897 {
23898 int i, len2 = remaining_arguments (args2);
23899 tree parmvec = make_tree_vec (1);
23900 tree argvec = make_tree_vec (len2);
23901 tree ta = args2;
23902
23903 /* Setup the parameter vector, which contains only ARG1. */
23904 TREE_VEC_ELT (parmvec, 0) = arg1;
23905
23906 /* Setup the argument vector, which contains the remaining
23907 arguments. */
23908 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23909 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23910
23911 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23912 argvec, DEDUCE_EXACT,
23913 /*subr=*/true, /*explain_p=*/false)
23914 == 0);
23915
23916 /* We cannot deduce in the other direction, because ARG1 is
23917 a pack expansion but ARG2 is not. */
23918 deduce2 = 0;
23919 }
23920 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23921 {
23922 int i, len1 = remaining_arguments (args1);
23923 tree parmvec = make_tree_vec (1);
23924 tree argvec = make_tree_vec (len1);
23925 tree ta = args1;
23926
23927 /* Setup the parameter vector, which contains only ARG1. */
23928 TREE_VEC_ELT (parmvec, 0) = arg2;
23929
23930 /* Setup the argument vector, which contains the remaining
23931 arguments. */
23932 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23933 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23934
23935 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23936 argvec, DEDUCE_EXACT,
23937 /*subr=*/true, /*explain_p=*/false)
23938 == 0);
23939
23940 /* We cannot deduce in the other direction, because ARG2 is
23941 a pack expansion but ARG1 is not.*/
23942 deduce1 = 0;
23943 }
23944
23945 else
23946 {
23947 /* The normal case, where neither argument is a pack
23948 expansion. */
23949 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23950 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23951 == 0);
23952 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23953 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23954 == 0);
23955 }
23956
23957 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23958 arg2, then arg2 is not as specialized as arg1. */
23959 if (!deduce1)
23960 lose2 = true;
23961 if (!deduce2)
23962 lose1 = true;
23963
23964 /* "If, for a given type, deduction succeeds in both directions
23965 (i.e., the types are identical after the transformations above)
23966 and both P and A were reference types (before being replaced with
23967 the type referred to above):
23968 - if the type from the argument template was an lvalue reference and
23969 the type from the parameter template was not, the argument type is
23970 considered to be more specialized than the other; otherwise,
23971 - if the type from the argument template is more cv-qualified
23972 than the type from the parameter template (as described above),
23973 the argument type is considered to be more specialized than the other;
23974 otherwise,
23975 - neither type is more specialized than the other." */
23976
23977 if (deduce1 && deduce2)
23978 {
23979 if (ref1 && ref2 && ref1 != ref2)
23980 {
23981 if (ref1 > ref2)
23982 lose1 = true;
23983 else
23984 lose2 = true;
23985 }
23986 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23987 {
23988 if ((quals1 & quals2) == quals2)
23989 lose2 = true;
23990 if ((quals1 & quals2) == quals1)
23991 lose1 = true;
23992 }
23993 }
23994
23995 if (lose1 && lose2)
23996 /* We've failed to deduce something in either direction.
23997 These must be unordered. */
23998 break;
23999
24000 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24001 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24002 /* We have already processed all of the arguments in our
24003 handing of the pack expansion type. */
24004 len = 0;
24005
24006 args1 = TREE_CHAIN (args1);
24007 args2 = TREE_CHAIN (args2);
24008 }
24009
24010 /* "In most cases, all template parameters must have values in order for
24011 deduction to succeed, but for partial ordering purposes a template
24012 parameter may remain without a value provided it is not used in the
24013 types being used for partial ordering."
24014
24015 Thus, if we are missing any of the targs1 we need to substitute into
24016 origs1, then pat2 is not as specialized as pat1. This can happen when
24017 there is a nondeduced context. */
24018 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24019 lose2 = true;
24020 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24021 lose1 = true;
24022
24023 processing_template_decl--;
24024
24025 /* If both deductions succeed, the partial ordering selects the more
24026 constrained template. */
24027 if (!lose1 && !lose2)
24028 {
24029 int winner = more_constrained (decl1, decl2);
24030 if (winner > 0)
24031 lose2 = true;
24032 else if (winner < 0)
24033 lose1 = true;
24034 }
24035
24036 /* All things being equal, if the next argument is a pack expansion
24037 for one function but not for the other, prefer the
24038 non-variadic function. FIXME this is bogus; see c++/41958. */
24039 if (lose1 == lose2
24040 && args1 && TREE_VALUE (args1)
24041 && args2 && TREE_VALUE (args2))
24042 {
24043 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24044 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24045 }
24046
24047 if (lose1 == lose2)
24048 return 0;
24049 else if (!lose1)
24050 return 1;
24051 else
24052 return -1;
24053 }
24054
24055 /* Determine which of two partial specializations of TMPL is more
24056 specialized.
24057
24058 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24059 to the first partial specialization. The TREE_PURPOSE is the
24060 innermost set of template parameters for the partial
24061 specialization. PAT2 is similar, but for the second template.
24062
24063 Return 1 if the first partial specialization is more specialized;
24064 -1 if the second is more specialized; 0 if neither is more
24065 specialized.
24066
24067 See [temp.class.order] for information about determining which of
24068 two templates is more specialized. */
24069
24070 static int
24071 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24072 {
24073 tree targs;
24074 int winner = 0;
24075 bool any_deductions = false;
24076
24077 tree tmpl1 = TREE_VALUE (pat1);
24078 tree tmpl2 = TREE_VALUE (pat2);
24079 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24080 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24081
24082 /* Just like what happens for functions, if we are ordering between
24083 different template specializations, we may encounter dependent
24084 types in the arguments, and we need our dependency check functions
24085 to behave correctly. */
24086 ++processing_template_decl;
24087 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24088 if (targs)
24089 {
24090 --winner;
24091 any_deductions = true;
24092 }
24093
24094 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24095 if (targs)
24096 {
24097 ++winner;
24098 any_deductions = true;
24099 }
24100 --processing_template_decl;
24101
24102 /* If both deductions succeed, the partial ordering selects the more
24103 constrained template. */
24104 if (!winner && any_deductions)
24105 winner = more_constrained (tmpl1, tmpl2);
24106
24107 /* In the case of a tie where at least one of the templates
24108 has a parameter pack at the end, the template with the most
24109 non-packed parameters wins. */
24110 if (winner == 0
24111 && any_deductions
24112 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24113 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24114 {
24115 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24116 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24117 int len1 = TREE_VEC_LENGTH (args1);
24118 int len2 = TREE_VEC_LENGTH (args2);
24119
24120 /* We don't count the pack expansion at the end. */
24121 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24122 --len1;
24123 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24124 --len2;
24125
24126 if (len1 > len2)
24127 return 1;
24128 else if (len1 < len2)
24129 return -1;
24130 }
24131
24132 return winner;
24133 }
24134
24135 /* Return the template arguments that will produce the function signature
24136 DECL from the function template FN, with the explicit template
24137 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24138 also match. Return NULL_TREE if no satisfactory arguments could be
24139 found. */
24140
24141 static tree
24142 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24143 {
24144 int ntparms = DECL_NTPARMS (fn);
24145 tree targs = make_tree_vec (ntparms);
24146 tree decl_type = TREE_TYPE (decl);
24147 tree decl_arg_types;
24148 tree *args;
24149 unsigned int nargs, ix;
24150 tree arg;
24151
24152 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24153
24154 /* Never do unification on the 'this' parameter. */
24155 decl_arg_types = skip_artificial_parms_for (decl,
24156 TYPE_ARG_TYPES (decl_type));
24157
24158 nargs = list_length (decl_arg_types);
24159 args = XALLOCAVEC (tree, nargs);
24160 for (arg = decl_arg_types, ix = 0;
24161 arg != NULL_TREE && arg != void_list_node;
24162 arg = TREE_CHAIN (arg), ++ix)
24163 args[ix] = TREE_VALUE (arg);
24164
24165 if (fn_type_unification (fn, explicit_args, targs,
24166 args, ix,
24167 (check_rettype || DECL_CONV_FN_P (fn)
24168 ? TREE_TYPE (decl_type) : NULL_TREE),
24169 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24170 /*explain_p=*/false,
24171 /*decltype*/false)
24172 == error_mark_node)
24173 return NULL_TREE;
24174
24175 return targs;
24176 }
24177
24178 /* Return the innermost template arguments that, when applied to a partial
24179 specialization SPEC_TMPL of TMPL, yield the ARGS.
24180
24181 For example, suppose we have:
24182
24183 template <class T, class U> struct S {};
24184 template <class T> struct S<T*, int> {};
24185
24186 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24187 partial specialization and the ARGS will be {double*, int}. The resulting
24188 vector will be {double}, indicating that `T' is bound to `double'. */
24189
24190 static tree
24191 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24192 {
24193 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24194 tree spec_args
24195 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24196 int i, ntparms = TREE_VEC_LENGTH (tparms);
24197 tree deduced_args;
24198 tree innermost_deduced_args;
24199
24200 innermost_deduced_args = make_tree_vec (ntparms);
24201 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24202 {
24203 deduced_args = copy_node (args);
24204 SET_TMPL_ARGS_LEVEL (deduced_args,
24205 TMPL_ARGS_DEPTH (deduced_args),
24206 innermost_deduced_args);
24207 }
24208 else
24209 deduced_args = innermost_deduced_args;
24210
24211 bool tried_array_deduction = (cxx_dialect < cxx17);
24212 again:
24213 if (unify (tparms, deduced_args,
24214 INNERMOST_TEMPLATE_ARGS (spec_args),
24215 INNERMOST_TEMPLATE_ARGS (args),
24216 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24217 return NULL_TREE;
24218
24219 for (i = 0; i < ntparms; ++i)
24220 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24221 {
24222 if (!tried_array_deduction)
24223 {
24224 try_array_deduction (tparms, innermost_deduced_args,
24225 INNERMOST_TEMPLATE_ARGS (spec_args));
24226 tried_array_deduction = true;
24227 if (TREE_VEC_ELT (innermost_deduced_args, i))
24228 goto again;
24229 }
24230 return NULL_TREE;
24231 }
24232
24233 if (!push_tinst_level (spec_tmpl, deduced_args))
24234 {
24235 excessive_deduction_depth = true;
24236 return NULL_TREE;
24237 }
24238
24239 /* Verify that nondeduced template arguments agree with the type
24240 obtained from argument deduction.
24241
24242 For example:
24243
24244 struct A { typedef int X; };
24245 template <class T, class U> struct C {};
24246 template <class T> struct C<T, typename T::X> {};
24247
24248 Then with the instantiation `C<A, int>', we can deduce that
24249 `T' is `A' but unify () does not check whether `typename T::X'
24250 is `int'. */
24251 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24252
24253 if (spec_args != error_mark_node)
24254 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24255 INNERMOST_TEMPLATE_ARGS (spec_args),
24256 tmpl, tf_none, false, false);
24257
24258 pop_tinst_level ();
24259
24260 if (spec_args == error_mark_node
24261 /* We only need to check the innermost arguments; the other
24262 arguments will always agree. */
24263 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24264 INNERMOST_TEMPLATE_ARGS (args)))
24265 return NULL_TREE;
24266
24267 /* Now that we have bindings for all of the template arguments,
24268 ensure that the arguments deduced for the template template
24269 parameters have compatible template parameter lists. See the use
24270 of template_template_parm_bindings_ok_p in fn_type_unification
24271 for more information. */
24272 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24273 return NULL_TREE;
24274
24275 return deduced_args;
24276 }
24277
24278 // Compare two function templates T1 and T2 by deducing bindings
24279 // from one against the other. If both deductions succeed, compare
24280 // constraints to see which is more constrained.
24281 static int
24282 more_specialized_inst (tree t1, tree t2)
24283 {
24284 int fate = 0;
24285 int count = 0;
24286
24287 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24288 {
24289 --fate;
24290 ++count;
24291 }
24292
24293 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24294 {
24295 ++fate;
24296 ++count;
24297 }
24298
24299 // If both deductions succeed, then one may be more constrained.
24300 if (count == 2 && fate == 0)
24301 fate = more_constrained (t1, t2);
24302
24303 return fate;
24304 }
24305
24306 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24307 Return the TREE_LIST node with the most specialized template, if
24308 any. If there is no most specialized template, the error_mark_node
24309 is returned.
24310
24311 Note that this function does not look at, or modify, the
24312 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24313 returned is one of the elements of INSTANTIATIONS, callers may
24314 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24315 and retrieve it from the value returned. */
24316
24317 tree
24318 most_specialized_instantiation (tree templates)
24319 {
24320 tree fn, champ;
24321
24322 ++processing_template_decl;
24323
24324 champ = templates;
24325 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24326 {
24327 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24328 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24329 if (fate == -1)
24330 champ = fn;
24331 else if (!fate)
24332 {
24333 /* Equally specialized, move to next function. If there
24334 is no next function, nothing's most specialized. */
24335 fn = TREE_CHAIN (fn);
24336 champ = fn;
24337 if (!fn)
24338 break;
24339 }
24340 }
24341
24342 if (champ)
24343 /* Now verify that champ is better than everything earlier in the
24344 instantiation list. */
24345 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24346 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24347 {
24348 champ = NULL_TREE;
24349 break;
24350 }
24351 }
24352
24353 processing_template_decl--;
24354
24355 if (!champ)
24356 return error_mark_node;
24357
24358 return champ;
24359 }
24360
24361 /* If DECL is a specialization of some template, return the most
24362 general such template. Otherwise, returns NULL_TREE.
24363
24364 For example, given:
24365
24366 template <class T> struct S { template <class U> void f(U); };
24367
24368 if TMPL is `template <class U> void S<int>::f(U)' this will return
24369 the full template. This function will not trace past partial
24370 specializations, however. For example, given in addition:
24371
24372 template <class T> struct S<T*> { template <class U> void f(U); };
24373
24374 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24375 `template <class T> template <class U> S<T*>::f(U)'. */
24376
24377 tree
24378 most_general_template (tree decl)
24379 {
24380 if (TREE_CODE (decl) != TEMPLATE_DECL)
24381 {
24382 if (tree tinfo = get_template_info (decl))
24383 decl = TI_TEMPLATE (tinfo);
24384 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24385 template friend, or a FIELD_DECL for a capture pack. */
24386 if (TREE_CODE (decl) != TEMPLATE_DECL)
24387 return NULL_TREE;
24388 }
24389
24390 /* Look for more and more general templates. */
24391 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24392 {
24393 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24394 (See cp-tree.h for details.) */
24395 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24396 break;
24397
24398 if (CLASS_TYPE_P (TREE_TYPE (decl))
24399 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24400 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24401 break;
24402
24403 /* Stop if we run into an explicitly specialized class template. */
24404 if (!DECL_NAMESPACE_SCOPE_P (decl)
24405 && DECL_CONTEXT (decl)
24406 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24407 break;
24408
24409 decl = DECL_TI_TEMPLATE (decl);
24410 }
24411
24412 return decl;
24413 }
24414
24415 /* Return the most specialized of the template partial specializations
24416 which can produce TARGET, a specialization of some class or variable
24417 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24418 a TEMPLATE_DECL node corresponding to the partial specialization, while
24419 the TREE_PURPOSE is the set of template arguments that must be
24420 substituted into the template pattern in order to generate TARGET.
24421
24422 If the choice of partial specialization is ambiguous, a diagnostic
24423 is issued, and the error_mark_node is returned. If there are no
24424 partial specializations matching TARGET, then NULL_TREE is
24425 returned, indicating that the primary template should be used. */
24426
24427 tree
24428 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24429 {
24430 tree list = NULL_TREE;
24431 tree t;
24432 tree champ;
24433 int fate;
24434 bool ambiguous_p;
24435 tree outer_args = NULL_TREE;
24436 tree tmpl, args;
24437
24438 if (TYPE_P (target))
24439 {
24440 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24441 tmpl = TI_TEMPLATE (tinfo);
24442 args = TI_ARGS (tinfo);
24443 }
24444 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24445 {
24446 tmpl = TREE_OPERAND (target, 0);
24447 args = TREE_OPERAND (target, 1);
24448 }
24449 else if (VAR_P (target))
24450 {
24451 tree tinfo = DECL_TEMPLATE_INFO (target);
24452 tmpl = TI_TEMPLATE (tinfo);
24453 args = TI_ARGS (tinfo);
24454 }
24455 else
24456 gcc_unreachable ();
24457
24458 tree main_tmpl = most_general_template (tmpl);
24459
24460 /* For determining which partial specialization to use, only the
24461 innermost args are interesting. */
24462 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24463 {
24464 outer_args = strip_innermost_template_args (args, 1);
24465 args = INNERMOST_TEMPLATE_ARGS (args);
24466 }
24467
24468 /* The caller hasn't called push_to_top_level yet, but we need
24469 get_partial_spec_bindings to be done in non-template context so that we'll
24470 fully resolve everything. */
24471 processing_template_decl_sentinel ptds;
24472
24473 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24474 {
24475 tree spec_args;
24476 tree spec_tmpl = TREE_VALUE (t);
24477
24478 if (outer_args)
24479 {
24480 /* Substitute in the template args from the enclosing class. */
24481 ++processing_template_decl;
24482 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
24483 --processing_template_decl;
24484 }
24485
24486 if (spec_tmpl == error_mark_node)
24487 return error_mark_node;
24488
24489 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24490 if (spec_args)
24491 {
24492 if (outer_args)
24493 spec_args = add_to_template_args (outer_args, spec_args);
24494
24495 /* Keep the candidate only if the constraints are satisfied,
24496 or if we're not compiling with concepts. */
24497 if (!flag_concepts
24498 || constraints_satisfied_p (spec_tmpl, spec_args))
24499 {
24500 list = tree_cons (spec_args, TREE_VALUE (t), list);
24501 TREE_TYPE (list) = TREE_TYPE (t);
24502 }
24503 }
24504 }
24505
24506 if (! list)
24507 return NULL_TREE;
24508
24509 ambiguous_p = false;
24510 t = list;
24511 champ = t;
24512 t = TREE_CHAIN (t);
24513 for (; t; t = TREE_CHAIN (t))
24514 {
24515 fate = more_specialized_partial_spec (tmpl, champ, t);
24516 if (fate == 1)
24517 ;
24518 else
24519 {
24520 if (fate == 0)
24521 {
24522 t = TREE_CHAIN (t);
24523 if (! t)
24524 {
24525 ambiguous_p = true;
24526 break;
24527 }
24528 }
24529 champ = t;
24530 }
24531 }
24532
24533 if (!ambiguous_p)
24534 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24535 {
24536 fate = more_specialized_partial_spec (tmpl, champ, t);
24537 if (fate != 1)
24538 {
24539 ambiguous_p = true;
24540 break;
24541 }
24542 }
24543
24544 if (ambiguous_p)
24545 {
24546 const char *str;
24547 char *spaces = NULL;
24548 if (!(complain & tf_error))
24549 return error_mark_node;
24550 if (TYPE_P (target))
24551 error ("ambiguous template instantiation for %q#T", target);
24552 else
24553 error ("ambiguous template instantiation for %q#D", target);
24554 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24555 for (t = list; t; t = TREE_CHAIN (t))
24556 {
24557 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24558 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24559 "%s %#qS", spaces ? spaces : str, subst);
24560 spaces = spaces ? spaces : get_spaces (str);
24561 }
24562 free (spaces);
24563 return error_mark_node;
24564 }
24565
24566 return champ;
24567 }
24568
24569 /* Explicitly instantiate DECL. */
24570
24571 void
24572 do_decl_instantiation (tree decl, tree storage)
24573 {
24574 tree result = NULL_TREE;
24575 int extern_p = 0;
24576
24577 if (!decl || decl == error_mark_node)
24578 /* An error occurred, for which grokdeclarator has already issued
24579 an appropriate message. */
24580 return;
24581 else if (! DECL_LANG_SPECIFIC (decl))
24582 {
24583 error ("explicit instantiation of non-template %q#D", decl);
24584 return;
24585 }
24586 else if (DECL_DECLARED_CONCEPT_P (decl))
24587 {
24588 if (VAR_P (decl))
24589 error ("explicit instantiation of variable concept %q#D", decl);
24590 else
24591 error ("explicit instantiation of function concept %q#D", decl);
24592 return;
24593 }
24594
24595 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24596 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24597
24598 if (VAR_P (decl) && !var_templ)
24599 {
24600 /* There is an asymmetry here in the way VAR_DECLs and
24601 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24602 the latter, the DECL we get back will be marked as a
24603 template instantiation, and the appropriate
24604 DECL_TEMPLATE_INFO will be set up. This does not happen for
24605 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24606 should handle VAR_DECLs as it currently handles
24607 FUNCTION_DECLs. */
24608 if (!DECL_CLASS_SCOPE_P (decl))
24609 {
24610 error ("%qD is not a static data member of a class template", decl);
24611 return;
24612 }
24613 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24614 if (!result || !VAR_P (result))
24615 {
24616 error ("no matching template for %qD found", decl);
24617 return;
24618 }
24619 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24620 {
24621 error ("type %qT for explicit instantiation %qD does not match "
24622 "declared type %qT", TREE_TYPE (result), decl,
24623 TREE_TYPE (decl));
24624 return;
24625 }
24626 }
24627 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24628 {
24629 error ("explicit instantiation of %q#D", decl);
24630 return;
24631 }
24632 else
24633 result = decl;
24634
24635 /* Check for various error cases. Note that if the explicit
24636 instantiation is valid the RESULT will currently be marked as an
24637 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24638 until we get here. */
24639
24640 if (DECL_TEMPLATE_SPECIALIZATION (result))
24641 {
24642 /* DR 259 [temp.spec].
24643
24644 Both an explicit instantiation and a declaration of an explicit
24645 specialization shall not appear in a program unless the explicit
24646 instantiation follows a declaration of the explicit specialization.
24647
24648 For a given set of template parameters, if an explicit
24649 instantiation of a template appears after a declaration of an
24650 explicit specialization for that template, the explicit
24651 instantiation has no effect. */
24652 return;
24653 }
24654 else if (DECL_EXPLICIT_INSTANTIATION (result))
24655 {
24656 /* [temp.spec]
24657
24658 No program shall explicitly instantiate any template more
24659 than once.
24660
24661 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24662 the first instantiation was `extern' and the second is not,
24663 and EXTERN_P for the opposite case. */
24664 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24665 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24666 /* If an "extern" explicit instantiation follows an ordinary
24667 explicit instantiation, the template is instantiated. */
24668 if (extern_p)
24669 return;
24670 }
24671 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24672 {
24673 error ("no matching template for %qD found", result);
24674 return;
24675 }
24676 else if (!DECL_TEMPLATE_INFO (result))
24677 {
24678 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24679 return;
24680 }
24681
24682 if (storage == NULL_TREE)
24683 ;
24684 else if (storage == ridpointers[(int) RID_EXTERN])
24685 {
24686 if (cxx_dialect == cxx98)
24687 pedwarn (input_location, OPT_Wpedantic,
24688 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24689 "instantiations");
24690 extern_p = 1;
24691 }
24692 else
24693 error ("storage class %qD applied to template instantiation", storage);
24694
24695 check_explicit_instantiation_namespace (result);
24696 mark_decl_instantiated (result, extern_p);
24697 if (! extern_p)
24698 instantiate_decl (result, /*defer_ok=*/true,
24699 /*expl_inst_class_mem_p=*/false);
24700 }
24701
24702 static void
24703 mark_class_instantiated (tree t, int extern_p)
24704 {
24705 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24706 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24707 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24708 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24709 if (! extern_p)
24710 {
24711 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24712 rest_of_type_compilation (t, 1);
24713 }
24714 }
24715
24716 /* Called from do_type_instantiation through binding_table_foreach to
24717 do recursive instantiation for the type bound in ENTRY. */
24718 static void
24719 bt_instantiate_type_proc (binding_entry entry, void *data)
24720 {
24721 tree storage = *(tree *) data;
24722
24723 if (MAYBE_CLASS_TYPE_P (entry->type)
24724 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24725 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24726 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24727 }
24728
24729 /* Perform an explicit instantiation of template class T. STORAGE, if
24730 non-null, is the RID for extern, inline or static. COMPLAIN is
24731 nonzero if this is called from the parser, zero if called recursively,
24732 since the standard is unclear (as detailed below). */
24733
24734 void
24735 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24736 {
24737 int extern_p = 0;
24738 int nomem_p = 0;
24739 int static_p = 0;
24740 int previous_instantiation_extern_p = 0;
24741
24742 if (TREE_CODE (t) == TYPE_DECL)
24743 t = TREE_TYPE (t);
24744
24745 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24746 {
24747 tree tmpl =
24748 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24749 if (tmpl)
24750 error ("explicit instantiation of non-class template %qD", tmpl);
24751 else
24752 error ("explicit instantiation of non-template type %qT", t);
24753 return;
24754 }
24755
24756 complete_type (t);
24757
24758 if (!COMPLETE_TYPE_P (t))
24759 {
24760 if (complain & tf_error)
24761 error ("explicit instantiation of %q#T before definition of template",
24762 t);
24763 return;
24764 }
24765
24766 if (storage != NULL_TREE)
24767 {
24768 if (storage == ridpointers[(int) RID_EXTERN])
24769 {
24770 if (cxx_dialect == cxx98)
24771 pedwarn (input_location, OPT_Wpedantic,
24772 "ISO C++ 1998 forbids the use of %<extern%> on "
24773 "explicit instantiations");
24774 }
24775 else
24776 pedwarn (input_location, OPT_Wpedantic,
24777 "ISO C++ forbids the use of %qE"
24778 " on explicit instantiations", storage);
24779
24780 if (storage == ridpointers[(int) RID_INLINE])
24781 nomem_p = 1;
24782 else if (storage == ridpointers[(int) RID_EXTERN])
24783 extern_p = 1;
24784 else if (storage == ridpointers[(int) RID_STATIC])
24785 static_p = 1;
24786 else
24787 {
24788 error ("storage class %qD applied to template instantiation",
24789 storage);
24790 extern_p = 0;
24791 }
24792 }
24793
24794 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24795 {
24796 /* DR 259 [temp.spec].
24797
24798 Both an explicit instantiation and a declaration of an explicit
24799 specialization shall not appear in a program unless the explicit
24800 instantiation follows a declaration of the explicit specialization.
24801
24802 For a given set of template parameters, if an explicit
24803 instantiation of a template appears after a declaration of an
24804 explicit specialization for that template, the explicit
24805 instantiation has no effect. */
24806 return;
24807 }
24808 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24809 {
24810 /* [temp.spec]
24811
24812 No program shall explicitly instantiate any template more
24813 than once.
24814
24815 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24816 instantiation was `extern'. If EXTERN_P then the second is.
24817 These cases are OK. */
24818 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
24819
24820 if (!previous_instantiation_extern_p && !extern_p
24821 && (complain & tf_error))
24822 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24823
24824 /* If we've already instantiated the template, just return now. */
24825 if (!CLASSTYPE_INTERFACE_ONLY (t))
24826 return;
24827 }
24828
24829 check_explicit_instantiation_namespace (TYPE_NAME (t));
24830 mark_class_instantiated (t, extern_p);
24831
24832 if (nomem_p)
24833 return;
24834
24835 /* In contrast to implicit instantiation, where only the
24836 declarations, and not the definitions, of members are
24837 instantiated, we have here:
24838
24839 [temp.explicit]
24840
24841 The explicit instantiation of a class template specialization
24842 implies the instantiation of all of its members not
24843 previously explicitly specialized in the translation unit
24844 containing the explicit instantiation.
24845
24846 Of course, we can't instantiate member template classes, since we
24847 don't have any arguments for them. Note that the standard is
24848 unclear on whether the instantiation of the members are
24849 *explicit* instantiations or not. However, the most natural
24850 interpretation is that it should be an explicit
24851 instantiation. */
24852 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24853 if ((VAR_P (fld)
24854 || (TREE_CODE (fld) == FUNCTION_DECL
24855 && !static_p
24856 && user_provided_p (fld)))
24857 && DECL_TEMPLATE_INSTANTIATION (fld))
24858 {
24859 mark_decl_instantiated (fld, extern_p);
24860 if (! extern_p)
24861 instantiate_decl (fld, /*defer_ok=*/true,
24862 /*expl_inst_class_mem_p=*/true);
24863 }
24864
24865 if (CLASSTYPE_NESTED_UTDS (t))
24866 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24867 bt_instantiate_type_proc, &storage);
24868 }
24869
24870 /* Given a function DECL, which is a specialization of TMPL, modify
24871 DECL to be a re-instantiation of TMPL with the same template
24872 arguments. TMPL should be the template into which tsubst'ing
24873 should occur for DECL, not the most general template.
24874
24875 One reason for doing this is a scenario like this:
24876
24877 template <class T>
24878 void f(const T&, int i);
24879
24880 void g() { f(3, 7); }
24881
24882 template <class T>
24883 void f(const T& t, const int i) { }
24884
24885 Note that when the template is first instantiated, with
24886 instantiate_template, the resulting DECL will have no name for the
24887 first parameter, and the wrong type for the second. So, when we go
24888 to instantiate the DECL, we regenerate it. */
24889
24890 static void
24891 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24892 {
24893 /* The arguments used to instantiate DECL, from the most general
24894 template. */
24895 tree code_pattern;
24896
24897 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24898
24899 /* Make sure that we can see identifiers, and compute access
24900 correctly. */
24901 push_access_scope (decl);
24902
24903 if (TREE_CODE (decl) == FUNCTION_DECL)
24904 {
24905 tree decl_parm;
24906 tree pattern_parm;
24907 tree specs;
24908 int args_depth;
24909 int parms_depth;
24910
24911 args_depth = TMPL_ARGS_DEPTH (args);
24912 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24913 if (args_depth > parms_depth)
24914 args = get_innermost_template_args (args, parms_depth);
24915
24916 /* Instantiate a dynamic exception-specification. noexcept will be
24917 handled below. */
24918 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24919 if (TREE_VALUE (raises))
24920 {
24921 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24922 args, tf_error, NULL_TREE,
24923 /*defer_ok*/false);
24924 if (specs && specs != error_mark_node)
24925 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24926 specs);
24927 }
24928
24929 /* Merge parameter declarations. */
24930 decl_parm = skip_artificial_parms_for (decl,
24931 DECL_ARGUMENTS (decl));
24932 pattern_parm
24933 = skip_artificial_parms_for (code_pattern,
24934 DECL_ARGUMENTS (code_pattern));
24935 while (decl_parm && !DECL_PACK_P (pattern_parm))
24936 {
24937 tree parm_type;
24938 tree attributes;
24939
24940 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24941 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24942 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24943 NULL_TREE);
24944 parm_type = type_decays_to (parm_type);
24945 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24946 TREE_TYPE (decl_parm) = parm_type;
24947 attributes = DECL_ATTRIBUTES (pattern_parm);
24948 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24949 {
24950 DECL_ATTRIBUTES (decl_parm) = attributes;
24951 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24952 }
24953 decl_parm = DECL_CHAIN (decl_parm);
24954 pattern_parm = DECL_CHAIN (pattern_parm);
24955 }
24956 /* Merge any parameters that match with the function parameter
24957 pack. */
24958 if (pattern_parm && DECL_PACK_P (pattern_parm))
24959 {
24960 int i, len;
24961 tree expanded_types;
24962 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24963 the parameters in this function parameter pack. */
24964 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24965 args, tf_error, NULL_TREE);
24966 len = TREE_VEC_LENGTH (expanded_types);
24967 for (i = 0; i < len; i++)
24968 {
24969 tree parm_type;
24970 tree attributes;
24971
24972 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24973 /* Rename the parameter to include the index. */
24974 DECL_NAME (decl_parm) =
24975 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24976 parm_type = TREE_VEC_ELT (expanded_types, i);
24977 parm_type = type_decays_to (parm_type);
24978 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24979 TREE_TYPE (decl_parm) = parm_type;
24980 attributes = DECL_ATTRIBUTES (pattern_parm);
24981 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24982 {
24983 DECL_ATTRIBUTES (decl_parm) = attributes;
24984 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24985 }
24986 decl_parm = DECL_CHAIN (decl_parm);
24987 }
24988 }
24989 /* Merge additional specifiers from the CODE_PATTERN. */
24990 if (DECL_DECLARED_INLINE_P (code_pattern)
24991 && !DECL_DECLARED_INLINE_P (decl))
24992 DECL_DECLARED_INLINE_P (decl) = 1;
24993
24994 maybe_instantiate_noexcept (decl, tf_error);
24995 }
24996 else if (VAR_P (decl))
24997 {
24998 start_lambda_scope (decl);
24999 DECL_INITIAL (decl) =
25000 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25001 tf_error, DECL_TI_TEMPLATE (decl));
25002 finish_lambda_scope ();
25003 if (VAR_HAD_UNKNOWN_BOUND (decl))
25004 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25005 tf_error, DECL_TI_TEMPLATE (decl));
25006 }
25007 else
25008 gcc_unreachable ();
25009
25010 pop_access_scope (decl);
25011 }
25012
25013 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25014 substituted to get DECL. */
25015
25016 tree
25017 template_for_substitution (tree decl)
25018 {
25019 tree tmpl = DECL_TI_TEMPLATE (decl);
25020
25021 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25022 for the instantiation. This is not always the most general
25023 template. Consider, for example:
25024
25025 template <class T>
25026 struct S { template <class U> void f();
25027 template <> void f<int>(); };
25028
25029 and an instantiation of S<double>::f<int>. We want TD to be the
25030 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25031 while (/* An instantiation cannot have a definition, so we need a
25032 more general template. */
25033 DECL_TEMPLATE_INSTANTIATION (tmpl)
25034 /* We must also deal with friend templates. Given:
25035
25036 template <class T> struct S {
25037 template <class U> friend void f() {};
25038 };
25039
25040 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25041 so far as the language is concerned, but that's still
25042 where we get the pattern for the instantiation from. On
25043 other hand, if the definition comes outside the class, say:
25044
25045 template <class T> struct S {
25046 template <class U> friend void f();
25047 };
25048 template <class U> friend void f() {}
25049
25050 we don't need to look any further. That's what the check for
25051 DECL_INITIAL is for. */
25052 || (TREE_CODE (decl) == FUNCTION_DECL
25053 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25054 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25055 {
25056 /* The present template, TD, should not be a definition. If it
25057 were a definition, we should be using it! Note that we
25058 cannot restructure the loop to just keep going until we find
25059 a template with a definition, since that might go too far if
25060 a specialization was declared, but not defined. */
25061
25062 /* Fetch the more general template. */
25063 tmpl = DECL_TI_TEMPLATE (tmpl);
25064 }
25065
25066 return tmpl;
25067 }
25068
25069 /* Returns true if we need to instantiate this template instance even if we
25070 know we aren't going to emit it. */
25071
25072 bool
25073 always_instantiate_p (tree decl)
25074 {
25075 /* We always instantiate inline functions so that we can inline them. An
25076 explicit instantiation declaration prohibits implicit instantiation of
25077 non-inline functions. With high levels of optimization, we would
25078 normally inline non-inline functions -- but we're not allowed to do
25079 that for "extern template" functions. Therefore, we check
25080 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25081 return ((TREE_CODE (decl) == FUNCTION_DECL
25082 && (DECL_DECLARED_INLINE_P (decl)
25083 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25084 /* And we need to instantiate static data members so that
25085 their initializers are available in integral constant
25086 expressions. */
25087 || (VAR_P (decl)
25088 && decl_maybe_constant_var_p (decl)));
25089 }
25090
25091 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25092 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25093 error, true otherwise. */
25094
25095 bool
25096 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25097 {
25098 tree fntype, spec, noex;
25099
25100 /* Don't instantiate a noexcept-specification from template context. */
25101 if (processing_template_decl
25102 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25103 return true;
25104
25105 if (DECL_MAYBE_DELETED (fn))
25106 {
25107 if (fn == current_function_decl)
25108 /* We're in start_preparsed_function, keep going. */
25109 return true;
25110
25111 ++function_depth;
25112 synthesize_method (fn);
25113 --function_depth;
25114 return !DECL_MAYBE_DELETED (fn);
25115 }
25116
25117 fntype = TREE_TYPE (fn);
25118 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25119
25120 if (!spec || !TREE_PURPOSE (spec))
25121 return true;
25122
25123 noex = TREE_PURPOSE (spec);
25124 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25125 && TREE_CODE (noex) != DEFERRED_PARSE)
25126 return true;
25127
25128 tree orig_fn = NULL_TREE;
25129 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25130 its FUNCTION_DECL for the rest of this function -- push_access_scope
25131 doesn't accept TEMPLATE_DECLs. */
25132 if (DECL_FUNCTION_TEMPLATE_P (fn))
25133 {
25134 orig_fn = fn;
25135 fn = DECL_TEMPLATE_RESULT (fn);
25136 }
25137
25138 if (DECL_CLONED_FUNCTION_P (fn))
25139 {
25140 tree prime = DECL_CLONED_FUNCTION (fn);
25141 if (!maybe_instantiate_noexcept (prime, complain))
25142 return false;
25143 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25144 }
25145 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25146 {
25147 static hash_set<tree>* fns = new hash_set<tree>;
25148 bool added = false;
25149 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25150 {
25151 spec = get_defaulted_eh_spec (fn, complain);
25152 if (spec == error_mark_node)
25153 /* This might have failed because of an unparsed DMI, so
25154 let's try again later. */
25155 return false;
25156 }
25157 else if (!(added = !fns->add (fn)))
25158 {
25159 /* If hash_set::add returns true, the element was already there. */
25160 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25161 DECL_SOURCE_LOCATION (fn));
25162 error_at (loc,
25163 "exception specification of %qD depends on itself",
25164 fn);
25165 spec = noexcept_false_spec;
25166 }
25167 else if (push_tinst_level (fn))
25168 {
25169 push_to_top_level ();
25170 push_access_scope (fn);
25171 push_deferring_access_checks (dk_no_deferred);
25172 input_location = DECL_SOURCE_LOCATION (fn);
25173
25174 /* If needed, set current_class_ptr for the benefit of
25175 tsubst_copy/PARM_DECL. */
25176 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25177 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25178 {
25179 tree this_parm = DECL_ARGUMENTS (tdecl);
25180 current_class_ptr = NULL_TREE;
25181 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25182 current_class_ptr = this_parm;
25183 }
25184
25185 /* If this function is represented by a TEMPLATE_DECL, then
25186 the deferred noexcept-specification might still contain
25187 dependent types, even after substitution. And we need the
25188 dependency check functions to work in build_noexcept_spec. */
25189 if (orig_fn)
25190 ++processing_template_decl;
25191
25192 /* Do deferred instantiation of the noexcept-specifier. */
25193 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25194 DEFERRED_NOEXCEPT_ARGS (noex),
25195 tf_warning_or_error, fn,
25196 /*function_p=*/false,
25197 /*i_c_e_p=*/true);
25198
25199 /* Build up the noexcept-specification. */
25200 spec = build_noexcept_spec (noex, tf_warning_or_error);
25201
25202 if (orig_fn)
25203 --processing_template_decl;
25204
25205 pop_deferring_access_checks ();
25206 pop_access_scope (fn);
25207 pop_tinst_level ();
25208 pop_from_top_level ();
25209 }
25210 else
25211 spec = noexcept_false_spec;
25212
25213 if (added)
25214 fns->remove (fn);
25215 }
25216
25217 if (spec == error_mark_node)
25218 {
25219 /* This failed with a hard error, so let's go with false. */
25220 gcc_assert (seen_error ());
25221 spec = noexcept_false_spec;
25222 }
25223
25224 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25225 if (orig_fn)
25226 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25227
25228 return true;
25229 }
25230
25231 /* We're starting to process the function INST, an instantiation of PATTERN;
25232 add their parameters to local_specializations. */
25233
25234 static void
25235 register_parameter_specializations (tree pattern, tree inst)
25236 {
25237 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25238 tree spec_parm = DECL_ARGUMENTS (inst);
25239 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25240 {
25241 register_local_specialization (spec_parm, tmpl_parm);
25242 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25243 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25244 }
25245 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25246 {
25247 if (!DECL_PACK_P (tmpl_parm)
25248 || (spec_parm && DECL_PACK_P (spec_parm)))
25249 {
25250 register_local_specialization (spec_parm, tmpl_parm);
25251 spec_parm = DECL_CHAIN (spec_parm);
25252 }
25253 else
25254 {
25255 /* Register the (value) argument pack as a specialization of
25256 TMPL_PARM, then move on. */
25257 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25258 register_local_specialization (argpack, tmpl_parm);
25259 }
25260 }
25261 gcc_assert (!spec_parm);
25262 }
25263
25264 /* Produce the definition of D, a _DECL generated from a template. If
25265 DEFER_OK is true, then we don't have to actually do the
25266 instantiation now; we just have to do it sometime. Normally it is
25267 an error if this is an explicit instantiation but D is undefined.
25268 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25269 instantiated class template. */
25270
25271 tree
25272 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25273 {
25274 tree tmpl = DECL_TI_TEMPLATE (d);
25275 tree gen_args;
25276 tree args;
25277 tree td;
25278 tree code_pattern;
25279 tree spec;
25280 tree gen_tmpl;
25281 bool pattern_defined;
25282 location_t saved_loc = input_location;
25283 int saved_unevaluated_operand = cp_unevaluated_operand;
25284 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25285 bool external_p;
25286 bool deleted_p;
25287
25288 /* This function should only be used to instantiate templates for
25289 functions and static member variables. */
25290 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25291
25292 /* A concept is never instantiated. */
25293 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25294
25295 /* Variables are never deferred; if instantiation is required, they
25296 are instantiated right away. That allows for better code in the
25297 case that an expression refers to the value of the variable --
25298 if the variable has a constant value the referring expression can
25299 take advantage of that fact. */
25300 if (VAR_P (d))
25301 defer_ok = false;
25302
25303 /* Don't instantiate cloned functions. Instead, instantiate the
25304 functions they cloned. */
25305 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25306 d = DECL_CLONED_FUNCTION (d);
25307
25308 if (DECL_TEMPLATE_INSTANTIATED (d)
25309 || (TREE_CODE (d) == FUNCTION_DECL
25310 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25311 || DECL_TEMPLATE_SPECIALIZATION (d))
25312 /* D has already been instantiated or explicitly specialized, so
25313 there's nothing for us to do here.
25314
25315 It might seem reasonable to check whether or not D is an explicit
25316 instantiation, and, if so, stop here. But when an explicit
25317 instantiation is deferred until the end of the compilation,
25318 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25319 the instantiation. */
25320 return d;
25321
25322 /* Check to see whether we know that this template will be
25323 instantiated in some other file, as with "extern template"
25324 extension. */
25325 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25326
25327 /* In general, we do not instantiate such templates. */
25328 if (external_p && !always_instantiate_p (d))
25329 return d;
25330
25331 gen_tmpl = most_general_template (tmpl);
25332 gen_args = DECL_TI_ARGS (d);
25333
25334 /* We should already have the extra args. */
25335 gcc_checking_assert (tmpl == gen_tmpl
25336 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25337 == TMPL_ARGS_DEPTH (gen_args)));
25338 /* And what's in the hash table should match D. */
25339 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25340 == d
25341 || spec == NULL_TREE);
25342
25343 /* This needs to happen before any tsubsting. */
25344 if (! push_tinst_level (d))
25345 return d;
25346
25347 timevar_push (TV_TEMPLATE_INST);
25348
25349 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25350 for the instantiation. */
25351 td = template_for_substitution (d);
25352 args = gen_args;
25353
25354 if (VAR_P (d))
25355 {
25356 /* Look up an explicit specialization, if any. */
25357 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25358 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25359 if (elt && elt != error_mark_node)
25360 {
25361 td = TREE_VALUE (elt);
25362 args = TREE_PURPOSE (elt);
25363 }
25364 }
25365
25366 code_pattern = DECL_TEMPLATE_RESULT (td);
25367
25368 /* We should never be trying to instantiate a member of a class
25369 template or partial specialization. */
25370 gcc_assert (d != code_pattern);
25371
25372 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25373 || DECL_TEMPLATE_SPECIALIZATION (td))
25374 /* In the case of a friend template whose definition is provided
25375 outside the class, we may have too many arguments. Drop the
25376 ones we don't need. The same is true for specializations. */
25377 args = get_innermost_template_args
25378 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25379
25380 if (TREE_CODE (d) == FUNCTION_DECL)
25381 {
25382 deleted_p = DECL_DELETED_FN (code_pattern);
25383 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25384 && DECL_INITIAL (code_pattern) != error_mark_node)
25385 || DECL_DEFAULTED_FN (code_pattern)
25386 || deleted_p);
25387 }
25388 else
25389 {
25390 deleted_p = false;
25391 if (DECL_CLASS_SCOPE_P (code_pattern))
25392 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25393 else
25394 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25395 }
25396
25397 /* We may be in the middle of deferred access check. Disable it now. */
25398 push_deferring_access_checks (dk_no_deferred);
25399
25400 /* Unless an explicit instantiation directive has already determined
25401 the linkage of D, remember that a definition is available for
25402 this entity. */
25403 if (pattern_defined
25404 && !DECL_INTERFACE_KNOWN (d)
25405 && !DECL_NOT_REALLY_EXTERN (d))
25406 mark_definable (d);
25407
25408 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25409 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25410 input_location = DECL_SOURCE_LOCATION (d);
25411
25412 /* If D is a member of an explicitly instantiated class template,
25413 and no definition is available, treat it like an implicit
25414 instantiation. */
25415 if (!pattern_defined && expl_inst_class_mem_p
25416 && DECL_EXPLICIT_INSTANTIATION (d))
25417 {
25418 /* Leave linkage flags alone on instantiations with anonymous
25419 visibility. */
25420 if (TREE_PUBLIC (d))
25421 {
25422 DECL_NOT_REALLY_EXTERN (d) = 0;
25423 DECL_INTERFACE_KNOWN (d) = 0;
25424 }
25425 SET_DECL_IMPLICIT_INSTANTIATION (d);
25426 }
25427
25428 /* Defer all other templates, unless we have been explicitly
25429 forbidden from doing so. */
25430 if (/* If there is no definition, we cannot instantiate the
25431 template. */
25432 ! pattern_defined
25433 /* If it's OK to postpone instantiation, do so. */
25434 || defer_ok
25435 /* If this is a static data member that will be defined
25436 elsewhere, we don't want to instantiate the entire data
25437 member, but we do want to instantiate the initializer so that
25438 we can substitute that elsewhere. */
25439 || (external_p && VAR_P (d))
25440 /* Handle here a deleted function too, avoid generating
25441 its body (c++/61080). */
25442 || deleted_p)
25443 {
25444 /* The definition of the static data member is now required so
25445 we must substitute the initializer. */
25446 if (VAR_P (d)
25447 && !DECL_INITIAL (d)
25448 && DECL_INITIAL (code_pattern))
25449 {
25450 tree ns;
25451 tree init;
25452 bool const_init = false;
25453 bool enter_context = DECL_CLASS_SCOPE_P (d);
25454
25455 ns = decl_namespace_context (d);
25456 push_nested_namespace (ns);
25457 if (enter_context)
25458 push_nested_class (DECL_CONTEXT (d));
25459 init = tsubst_expr (DECL_INITIAL (code_pattern),
25460 args,
25461 tf_warning_or_error, NULL_TREE,
25462 /*integral_constant_expression_p=*/false);
25463 /* If instantiating the initializer involved instantiating this
25464 again, don't call cp_finish_decl twice. */
25465 if (!DECL_INITIAL (d))
25466 {
25467 /* Make sure the initializer is still constant, in case of
25468 circular dependency (template/instantiate6.C). */
25469 const_init
25470 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25471 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25472 /*asmspec_tree=*/NULL_TREE,
25473 LOOKUP_ONLYCONVERTING);
25474 }
25475 if (enter_context)
25476 pop_nested_class ();
25477 pop_nested_namespace (ns);
25478 }
25479
25480 /* We restore the source position here because it's used by
25481 add_pending_template. */
25482 input_location = saved_loc;
25483
25484 if (at_eof && !pattern_defined
25485 && DECL_EXPLICIT_INSTANTIATION (d)
25486 && DECL_NOT_REALLY_EXTERN (d))
25487 /* [temp.explicit]
25488
25489 The definition of a non-exported function template, a
25490 non-exported member function template, or a non-exported
25491 member function or static data member of a class template
25492 shall be present in every translation unit in which it is
25493 explicitly instantiated. */
25494 permerror (input_location, "explicit instantiation of %qD "
25495 "but no definition available", d);
25496
25497 /* If we're in unevaluated context, we just wanted to get the
25498 constant value; this isn't an odr use, so don't queue
25499 a full instantiation. */
25500 if (cp_unevaluated_operand != 0)
25501 goto out;
25502 /* ??? Historically, we have instantiated inline functions, even
25503 when marked as "extern template". */
25504 if (!(external_p && VAR_P (d)))
25505 add_pending_template (d);
25506 goto out;
25507 }
25508
25509 bool push_to_top, nested;
25510 tree fn_context;
25511 fn_context = decl_function_context (d);
25512 if (LAMBDA_FUNCTION_P (d))
25513 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25514 fn_context = NULL_TREE;
25515 nested = current_function_decl != NULL_TREE;
25516 push_to_top = !(nested && fn_context == current_function_decl);
25517
25518 vec<tree> omp_privatization_save;
25519 if (nested)
25520 save_omp_privatization_clauses (omp_privatization_save);
25521
25522 if (push_to_top)
25523 push_to_top_level ();
25524 else
25525 {
25526 gcc_assert (!processing_template_decl);
25527 push_function_context ();
25528 cp_unevaluated_operand = 0;
25529 c_inhibit_evaluation_warnings = 0;
25530 }
25531
25532 if (VAR_P (d))
25533 {
25534 /* The variable might be a lambda's extra scope, and that
25535 lambda's visibility depends on D's. */
25536 maybe_commonize_var (d);
25537 determine_visibility (d);
25538 }
25539
25540 /* Mark D as instantiated so that recursive calls to
25541 instantiate_decl do not try to instantiate it again. */
25542 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25543
25544 /* Regenerate the declaration in case the template has been modified
25545 by a subsequent redeclaration. */
25546 regenerate_decl_from_template (d, td, args);
25547
25548 /* We already set the file and line above. Reset them now in case
25549 they changed as a result of calling regenerate_decl_from_template. */
25550 input_location = DECL_SOURCE_LOCATION (d);
25551
25552 if (VAR_P (d))
25553 {
25554 tree init;
25555 bool const_init = false;
25556
25557 /* Clear out DECL_RTL; whatever was there before may not be right
25558 since we've reset the type of the declaration. */
25559 SET_DECL_RTL (d, NULL);
25560 DECL_IN_AGGR_P (d) = 0;
25561
25562 /* The initializer is placed in DECL_INITIAL by
25563 regenerate_decl_from_template so we don't need to
25564 push/pop_access_scope again here. Pull it out so that
25565 cp_finish_decl can process it. */
25566 init = DECL_INITIAL (d);
25567 DECL_INITIAL (d) = NULL_TREE;
25568 DECL_INITIALIZED_P (d) = 0;
25569
25570 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25571 initializer. That function will defer actual emission until
25572 we have a chance to determine linkage. */
25573 DECL_EXTERNAL (d) = 0;
25574
25575 /* Enter the scope of D so that access-checking works correctly. */
25576 bool enter_context = DECL_CLASS_SCOPE_P (d);
25577 if (enter_context)
25578 push_nested_class (DECL_CONTEXT (d));
25579
25580 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25581 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
25582 ? LOOKUP_CONSTINIT : 0);
25583 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
25584
25585 if (enter_context)
25586 pop_nested_class ();
25587
25588 if (variable_template_p (gen_tmpl))
25589 note_variable_template_instantiation (d);
25590 }
25591 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25592 synthesize_method (d);
25593 else if (TREE_CODE (d) == FUNCTION_DECL)
25594 {
25595 /* Set up the list of local specializations. */
25596 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25597 tree block = NULL_TREE;
25598
25599 /* Set up context. */
25600 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25601 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25602 block = push_stmt_list ();
25603 else
25604 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25605
25606 /* Some typedefs referenced from within the template code need to be
25607 access checked at template instantiation time, i.e now. These
25608 types were added to the template at parsing time. Let's get those
25609 and perform the access checks then. */
25610 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
25611 args);
25612
25613 /* Create substitution entries for the parameters. */
25614 register_parameter_specializations (code_pattern, d);
25615
25616 /* Substitute into the body of the function. */
25617 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25618 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25619 tf_warning_or_error, tmpl);
25620 else
25621 {
25622 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25623 tf_warning_or_error, tmpl,
25624 /*integral_constant_expression_p=*/false);
25625
25626 /* Set the current input_location to the end of the function
25627 so that finish_function knows where we are. */
25628 input_location
25629 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25630
25631 /* Remember if we saw an infinite loop in the template. */
25632 current_function_infinite_loop
25633 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25634 }
25635
25636 /* Finish the function. */
25637 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25638 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25639 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25640 else
25641 {
25642 d = finish_function (/*inline_p=*/false);
25643 expand_or_defer_fn (d);
25644 }
25645
25646 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25647 cp_check_omp_declare_reduction (d);
25648 }
25649
25650 /* We're not deferring instantiation any more. */
25651 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25652
25653 if (push_to_top)
25654 pop_from_top_level ();
25655 else
25656 pop_function_context ();
25657
25658 if (nested)
25659 restore_omp_privatization_clauses (omp_privatization_save);
25660
25661 out:
25662 pop_deferring_access_checks ();
25663 timevar_pop (TV_TEMPLATE_INST);
25664 pop_tinst_level ();
25665 input_location = saved_loc;
25666 cp_unevaluated_operand = saved_unevaluated_operand;
25667 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25668
25669 return d;
25670 }
25671
25672 /* Run through the list of templates that we wish we could
25673 instantiate, and instantiate any we can. RETRIES is the
25674 number of times we retry pending template instantiation. */
25675
25676 void
25677 instantiate_pending_templates (int retries)
25678 {
25679 int reconsider;
25680 location_t saved_loc = input_location;
25681
25682 /* Instantiating templates may trigger vtable generation. This in turn
25683 may require further template instantiations. We place a limit here
25684 to avoid infinite loop. */
25685 if (pending_templates && retries >= max_tinst_depth)
25686 {
25687 tree decl = pending_templates->tinst->maybe_get_node ();
25688
25689 fatal_error (input_location,
25690 "template instantiation depth exceeds maximum of %d"
25691 " instantiating %q+D, possibly from virtual table generation"
25692 " (use %<-ftemplate-depth=%> to increase the maximum)",
25693 max_tinst_depth, decl);
25694 if (TREE_CODE (decl) == FUNCTION_DECL)
25695 /* Pretend that we defined it. */
25696 DECL_INITIAL (decl) = error_mark_node;
25697 return;
25698 }
25699
25700 do
25701 {
25702 struct pending_template **t = &pending_templates;
25703 struct pending_template *last = NULL;
25704 reconsider = 0;
25705 while (*t)
25706 {
25707 tree instantiation = reopen_tinst_level ((*t)->tinst);
25708 bool complete = false;
25709
25710 if (TYPE_P (instantiation))
25711 {
25712 if (!COMPLETE_TYPE_P (instantiation))
25713 {
25714 instantiate_class_template (instantiation);
25715 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25716 for (tree fld = TYPE_FIELDS (instantiation);
25717 fld; fld = TREE_CHAIN (fld))
25718 if ((VAR_P (fld)
25719 || (TREE_CODE (fld) == FUNCTION_DECL
25720 && !DECL_ARTIFICIAL (fld)))
25721 && DECL_TEMPLATE_INSTANTIATION (fld))
25722 instantiate_decl (fld,
25723 /*defer_ok=*/false,
25724 /*expl_inst_class_mem_p=*/false);
25725
25726 if (COMPLETE_TYPE_P (instantiation))
25727 reconsider = 1;
25728 }
25729
25730 complete = COMPLETE_TYPE_P (instantiation);
25731 }
25732 else
25733 {
25734 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25735 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25736 {
25737 instantiation
25738 = instantiate_decl (instantiation,
25739 /*defer_ok=*/false,
25740 /*expl_inst_class_mem_p=*/false);
25741 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25742 reconsider = 1;
25743 }
25744
25745 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25746 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25747 }
25748
25749 if (complete)
25750 {
25751 /* If INSTANTIATION has been instantiated, then we don't
25752 need to consider it again in the future. */
25753 struct pending_template *drop = *t;
25754 *t = (*t)->next;
25755 set_refcount_ptr (drop->tinst);
25756 pending_template_freelist ().free (drop);
25757 }
25758 else
25759 {
25760 last = *t;
25761 t = &(*t)->next;
25762 }
25763 tinst_depth = 0;
25764 set_refcount_ptr (current_tinst_level);
25765 }
25766 last_pending_template = last;
25767 }
25768 while (reconsider);
25769
25770 input_location = saved_loc;
25771 }
25772
25773 /* Substitute ARGVEC into T, which is a list of initializers for
25774 either base class or a non-static data member. The TREE_PURPOSEs
25775 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25776 instantiate_decl. */
25777
25778 static tree
25779 tsubst_initializer_list (tree t, tree argvec)
25780 {
25781 tree inits = NULL_TREE;
25782 tree target_ctor = error_mark_node;
25783
25784 for (; t; t = TREE_CHAIN (t))
25785 {
25786 tree decl;
25787 tree init;
25788 tree expanded_bases = NULL_TREE;
25789 tree expanded_arguments = NULL_TREE;
25790 int i, len = 1;
25791
25792 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25793 {
25794 tree expr;
25795 tree arg;
25796
25797 /* Expand the base class expansion type into separate base
25798 classes. */
25799 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25800 tf_warning_or_error,
25801 NULL_TREE);
25802 if (expanded_bases == error_mark_node)
25803 continue;
25804
25805 /* We'll be building separate TREE_LISTs of arguments for
25806 each base. */
25807 len = TREE_VEC_LENGTH (expanded_bases);
25808 expanded_arguments = make_tree_vec (len);
25809 for (i = 0; i < len; i++)
25810 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
25811
25812 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25813 expand each argument in the TREE_VALUE of t. */
25814 expr = make_node (EXPR_PACK_EXPANSION);
25815 PACK_EXPANSION_LOCAL_P (expr) = true;
25816 PACK_EXPANSION_PARAMETER_PACKS (expr) =
25817 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
25818
25819 if (TREE_VALUE (t) == void_type_node)
25820 /* VOID_TYPE_NODE is used to indicate
25821 value-initialization. */
25822 {
25823 for (i = 0; i < len; i++)
25824 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
25825 }
25826 else
25827 {
25828 /* Substitute parameter packs into each argument in the
25829 TREE_LIST. */
25830 in_base_initializer = 1;
25831 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
25832 {
25833 tree expanded_exprs;
25834
25835 /* Expand the argument. */
25836 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25837 expanded_exprs
25838 = tsubst_pack_expansion (expr, argvec,
25839 tf_warning_or_error,
25840 NULL_TREE);
25841 if (expanded_exprs == error_mark_node)
25842 continue;
25843
25844 /* Prepend each of the expanded expressions to the
25845 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25846 for (i = 0; i < len; i++)
25847 {
25848 TREE_VEC_ELT (expanded_arguments, i) =
25849 tree_cons (NULL_TREE,
25850 TREE_VEC_ELT (expanded_exprs, i),
25851 TREE_VEC_ELT (expanded_arguments, i));
25852 }
25853 }
25854 in_base_initializer = 0;
25855
25856 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25857 since we built them backwards. */
25858 for (i = 0; i < len; i++)
25859 {
25860 TREE_VEC_ELT (expanded_arguments, i) =
25861 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25862 }
25863 }
25864 }
25865
25866 for (i = 0; i < len; ++i)
25867 {
25868 if (expanded_bases)
25869 {
25870 decl = TREE_VEC_ELT (expanded_bases, i);
25871 decl = expand_member_init (decl);
25872 init = TREE_VEC_ELT (expanded_arguments, i);
25873 }
25874 else
25875 {
25876 tree tmp;
25877 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25878 tf_warning_or_error, NULL_TREE);
25879
25880 decl = expand_member_init (decl);
25881 if (decl && !DECL_P (decl))
25882 in_base_initializer = 1;
25883
25884 init = TREE_VALUE (t);
25885 tmp = init;
25886 if (init != void_type_node)
25887 init = tsubst_expr (init, argvec,
25888 tf_warning_or_error, NULL_TREE,
25889 /*integral_constant_expression_p=*/false);
25890 if (init == NULL_TREE && tmp != NULL_TREE)
25891 /* If we had an initializer but it instantiated to nothing,
25892 value-initialize the object. This will only occur when
25893 the initializer was a pack expansion where the parameter
25894 packs used in that expansion were of length zero. */
25895 init = void_type_node;
25896 in_base_initializer = 0;
25897 }
25898
25899 if (target_ctor != error_mark_node
25900 && init != error_mark_node)
25901 {
25902 error ("mem-initializer for %qD follows constructor delegation",
25903 decl);
25904 return inits;
25905 }
25906 /* Look for a target constructor. */
25907 if (init != error_mark_node
25908 && decl && CLASS_TYPE_P (decl)
25909 && same_type_p (decl, current_class_type))
25910 {
25911 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25912 if (inits)
25913 {
25914 error ("constructor delegation follows mem-initializer for %qD",
25915 TREE_PURPOSE (inits));
25916 continue;
25917 }
25918 target_ctor = init;
25919 }
25920
25921 if (decl)
25922 {
25923 init = build_tree_list (decl, init);
25924 TREE_CHAIN (init) = inits;
25925 inits = init;
25926 }
25927 }
25928 }
25929 return inits;
25930 }
25931
25932 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25933
25934 static void
25935 set_current_access_from_decl (tree decl)
25936 {
25937 if (TREE_PRIVATE (decl))
25938 current_access_specifier = access_private_node;
25939 else if (TREE_PROTECTED (decl))
25940 current_access_specifier = access_protected_node;
25941 else
25942 current_access_specifier = access_public_node;
25943 }
25944
25945 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25946 is the instantiation (which should have been created with
25947 start_enum) and ARGS are the template arguments to use. */
25948
25949 static void
25950 tsubst_enum (tree tag, tree newtag, tree args)
25951 {
25952 tree e;
25953
25954 if (SCOPED_ENUM_P (newtag))
25955 begin_scope (sk_scoped_enum, newtag);
25956
25957 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25958 {
25959 tree value;
25960 tree decl;
25961
25962 decl = TREE_VALUE (e);
25963 /* Note that in a template enum, the TREE_VALUE is the
25964 CONST_DECL, not the corresponding INTEGER_CST. */
25965 value = tsubst_expr (DECL_INITIAL (decl),
25966 args, tf_warning_or_error, NULL_TREE,
25967 /*integral_constant_expression_p=*/true);
25968
25969 /* Give this enumeration constant the correct access. */
25970 set_current_access_from_decl (decl);
25971
25972 /* Actually build the enumerator itself. Here we're assuming that
25973 enumerators can't have dependent attributes. */
25974 build_enumerator (DECL_NAME (decl), value, newtag,
25975 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25976 }
25977
25978 if (SCOPED_ENUM_P (newtag))
25979 finish_scope ();
25980
25981 finish_enum_value_list (newtag);
25982 finish_enum (newtag);
25983
25984 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25985 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25986 }
25987
25988 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25989 its type -- but without substituting the innermost set of template
25990 arguments. So, innermost set of template parameters will appear in
25991 the type. */
25992
25993 tree
25994 get_mostly_instantiated_function_type (tree decl)
25995 {
25996 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25997 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25998 }
25999
26000 /* Return truthvalue if we're processing a template different from
26001 the last one involved in diagnostics. */
26002 bool
26003 problematic_instantiation_changed (void)
26004 {
26005 return current_tinst_level != last_error_tinst_level;
26006 }
26007
26008 /* Remember current template involved in diagnostics. */
26009 void
26010 record_last_problematic_instantiation (void)
26011 {
26012 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26013 }
26014
26015 struct tinst_level *
26016 current_instantiation (void)
26017 {
26018 return current_tinst_level;
26019 }
26020
26021 /* Return TRUE if current_function_decl is being instantiated, false
26022 otherwise. */
26023
26024 bool
26025 instantiating_current_function_p (void)
26026 {
26027 return (current_instantiation ()
26028 && (current_instantiation ()->maybe_get_node ()
26029 == current_function_decl));
26030 }
26031
26032 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26033 type. Return false for ok, true for disallowed. Issue error and
26034 inform messages under control of COMPLAIN. */
26035
26036 static bool
26037 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26038 {
26039 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26040 return false;
26041 else if (TYPE_PTR_P (type))
26042 return false;
26043 else if (TYPE_REF_P (type)
26044 && !TYPE_REF_IS_RVALUE (type))
26045 return false;
26046 else if (TYPE_PTRMEM_P (type))
26047 return false;
26048 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26049 {
26050 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26051 {
26052 if (complain & tf_error)
26053 error ("non-type template parameters of deduced class type only "
26054 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26055 return true;
26056 }
26057 return false;
26058 }
26059 else if (TREE_CODE (type) == TYPENAME_TYPE)
26060 return false;
26061 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26062 return false;
26063 else if (TREE_CODE (type) == NULLPTR_TYPE)
26064 return false;
26065 /* A bound template template parm could later be instantiated to have a valid
26066 nontype parm type via an alias template. */
26067 else if (cxx_dialect >= cxx11
26068 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26069 return false;
26070 else if (CLASS_TYPE_P (type))
26071 {
26072 if (cxx_dialect < cxx20)
26073 {
26074 if (complain & tf_error)
26075 error ("non-type template parameters of class type only available "
26076 "with %<-std=c++20%> or %<-std=gnu++20%>");
26077 return true;
26078 }
26079 if (dependent_type_p (type))
26080 return false;
26081 if (!complete_type_or_else (type, NULL_TREE))
26082 return true;
26083 if (!structural_type_p (type))
26084 {
26085 if (complain & tf_error)
26086 {
26087 auto_diagnostic_group d;
26088 error ("%qT is not a valid type for a template non-type "
26089 "parameter because it is not structural", type);
26090 structural_type_p (type, true);
26091 }
26092 return true;
26093 }
26094 return false;
26095 }
26096
26097 if (complain & tf_error)
26098 {
26099 if (type == error_mark_node)
26100 inform (input_location, "invalid template non-type parameter");
26101 else
26102 error ("%q#T is not a valid type for a template non-type parameter",
26103 type);
26104 }
26105 return true;
26106 }
26107
26108 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26109 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26110
26111 static bool
26112 dependent_type_p_r (tree type)
26113 {
26114 tree scope;
26115
26116 /* [temp.dep.type]
26117
26118 A type is dependent if it is:
26119
26120 -- a template parameter. Template template parameters are types
26121 for us (since TYPE_P holds true for them) so we handle
26122 them here. */
26123 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26124 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26125 return true;
26126 /* -- a qualified-id with a nested-name-specifier which contains a
26127 class-name that names a dependent type or whose unqualified-id
26128 names a dependent type. */
26129 if (TREE_CODE (type) == TYPENAME_TYPE)
26130 return true;
26131
26132 /* An alias template specialization can be dependent even if the
26133 resulting type is not. */
26134 if (dependent_alias_template_spec_p (type, nt_transparent))
26135 return true;
26136
26137 /* -- a cv-qualified type where the cv-unqualified type is
26138 dependent.
26139 No code is necessary for this bullet; the code below handles
26140 cv-qualified types, and we don't want to strip aliases with
26141 TYPE_MAIN_VARIANT because of DR 1558. */
26142 /* -- a compound type constructed from any dependent type. */
26143 if (TYPE_PTRMEM_P (type))
26144 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26145 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26146 (type)));
26147 else if (INDIRECT_TYPE_P (type))
26148 return dependent_type_p (TREE_TYPE (type));
26149 else if (FUNC_OR_METHOD_TYPE_P (type))
26150 {
26151 tree arg_type;
26152
26153 if (dependent_type_p (TREE_TYPE (type)))
26154 return true;
26155 for (arg_type = TYPE_ARG_TYPES (type);
26156 arg_type;
26157 arg_type = TREE_CHAIN (arg_type))
26158 if (dependent_type_p (TREE_VALUE (arg_type)))
26159 return true;
26160 if (cxx_dialect >= cxx17)
26161 /* A value-dependent noexcept-specifier makes the type dependent. */
26162 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26163 if (tree noex = TREE_PURPOSE (spec))
26164 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26165 affect overload resolution and treating it as dependent breaks
26166 things. Same for an unparsed noexcept expression. */
26167 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26168 && TREE_CODE (noex) != DEFERRED_PARSE
26169 && value_dependent_expression_p (noex))
26170 return true;
26171 return false;
26172 }
26173 /* -- an array type constructed from any dependent type or whose
26174 size is specified by a constant expression that is
26175 value-dependent.
26176
26177 We checked for type- and value-dependence of the bounds in
26178 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26179 if (TREE_CODE (type) == ARRAY_TYPE)
26180 {
26181 if (TYPE_DOMAIN (type)
26182 && dependent_type_p (TYPE_DOMAIN (type)))
26183 return true;
26184 return dependent_type_p (TREE_TYPE (type));
26185 }
26186
26187 /* -- a template-id in which either the template name is a template
26188 parameter ... */
26189 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26190 return true;
26191 /* ... or any of the template arguments is a dependent type or
26192 an expression that is type-dependent or value-dependent. */
26193 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26194 && (any_dependent_template_arguments_p
26195 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26196 return true;
26197
26198 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26199 dependent; if the argument of the `typeof' expression is not
26200 type-dependent, then it should already been have resolved. */
26201 if (TREE_CODE (type) == TYPEOF_TYPE
26202 || TREE_CODE (type) == DECLTYPE_TYPE
26203 || TREE_CODE (type) == UNDERLYING_TYPE)
26204 return true;
26205
26206 /* A template argument pack is dependent if any of its packed
26207 arguments are. */
26208 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26209 {
26210 tree args = ARGUMENT_PACK_ARGS (type);
26211 int i, len = TREE_VEC_LENGTH (args);
26212 for (i = 0; i < len; ++i)
26213 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26214 return true;
26215 }
26216
26217 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26218 be template parameters. */
26219 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26220 return true;
26221
26222 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26223 return true;
26224
26225 /* The standard does not specifically mention types that are local
26226 to template functions or local classes, but they should be
26227 considered dependent too. For example:
26228
26229 template <int I> void f() {
26230 enum E { a = I };
26231 S<sizeof (E)> s;
26232 }
26233
26234 The size of `E' cannot be known until the value of `I' has been
26235 determined. Therefore, `E' must be considered dependent. */
26236 scope = TYPE_CONTEXT (type);
26237 if (scope && TYPE_P (scope))
26238 return dependent_type_p (scope);
26239 /* Don't use type_dependent_expression_p here, as it can lead
26240 to infinite recursion trying to determine whether a lambda
26241 nested in a lambda is dependent (c++/47687). */
26242 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26243 && DECL_LANG_SPECIFIC (scope)
26244 && DECL_TEMPLATE_INFO (scope)
26245 && (any_dependent_template_arguments_p
26246 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26247 return true;
26248
26249 /* Other types are non-dependent. */
26250 return false;
26251 }
26252
26253 /* Returns TRUE if TYPE is dependent, in the sense of
26254 [temp.dep.type]. Note that a NULL type is considered dependent. */
26255
26256 bool
26257 dependent_type_p (tree type)
26258 {
26259 /* If there are no template parameters in scope, then there can't be
26260 any dependent types. */
26261 if (!processing_template_decl)
26262 {
26263 /* If we are not processing a template, then nobody should be
26264 providing us with a dependent type. */
26265 gcc_assert (type);
26266 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26267 return false;
26268 }
26269
26270 /* If the type is NULL, we have not computed a type for the entity
26271 in question; in that case, the type is dependent. */
26272 if (!type)
26273 return true;
26274
26275 /* Erroneous types can be considered non-dependent. */
26276 if (type == error_mark_node)
26277 return false;
26278
26279 /* Getting here with global_type_node means we improperly called this
26280 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26281 gcc_checking_assert (type != global_type_node);
26282
26283 /* If we have not already computed the appropriate value for TYPE,
26284 do so now. */
26285 if (!TYPE_DEPENDENT_P_VALID (type))
26286 {
26287 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26288 TYPE_DEPENDENT_P_VALID (type) = 1;
26289 }
26290
26291 return TYPE_DEPENDENT_P (type);
26292 }
26293
26294 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26295 lookup. In other words, a dependent type that is not the current
26296 instantiation. */
26297
26298 bool
26299 dependent_scope_p (tree scope)
26300 {
26301 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26302 && !currently_open_class (scope));
26303 }
26304
26305 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26306 an unknown base of 'this' (and is therefore instantiation-dependent). */
26307
26308 static bool
26309 unknown_base_ref_p (tree t)
26310 {
26311 if (!current_class_ptr)
26312 return false;
26313
26314 tree mem = TREE_OPERAND (t, 1);
26315 if (shared_member_p (mem))
26316 return false;
26317
26318 tree cur = current_nonlambda_class_type ();
26319 if (!any_dependent_bases_p (cur))
26320 return false;
26321
26322 tree ctx = TREE_OPERAND (t, 0);
26323 if (DERIVED_FROM_P (ctx, cur))
26324 return false;
26325
26326 return true;
26327 }
26328
26329 /* T is a SCOPE_REF; return whether we need to consider it
26330 instantiation-dependent so that we can check access at instantiation
26331 time even though we know which member it resolves to. */
26332
26333 static bool
26334 instantiation_dependent_scope_ref_p (tree t)
26335 {
26336 if (DECL_P (TREE_OPERAND (t, 1))
26337 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26338 && !unknown_base_ref_p (t)
26339 && accessible_in_template_p (TREE_OPERAND (t, 0),
26340 TREE_OPERAND (t, 1)))
26341 return false;
26342 else
26343 return true;
26344 }
26345
26346 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26347 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26348 expression. */
26349
26350 /* Note that this predicate is not appropriate for general expressions;
26351 only constant expressions (that satisfy potential_constant_expression)
26352 can be tested for value dependence. */
26353
26354 bool
26355 value_dependent_expression_p (tree expression)
26356 {
26357 if (!processing_template_decl || expression == NULL_TREE)
26358 return false;
26359
26360 /* A type-dependent expression is also value-dependent. */
26361 if (type_dependent_expression_p (expression))
26362 return true;
26363
26364 switch (TREE_CODE (expression))
26365 {
26366 case BASELINK:
26367 /* A dependent member function of the current instantiation. */
26368 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26369
26370 case FUNCTION_DECL:
26371 /* A dependent member function of the current instantiation. */
26372 if (DECL_CLASS_SCOPE_P (expression)
26373 && dependent_type_p (DECL_CONTEXT (expression)))
26374 return true;
26375 break;
26376
26377 case IDENTIFIER_NODE:
26378 /* A name that has not been looked up -- must be dependent. */
26379 return true;
26380
26381 case TEMPLATE_PARM_INDEX:
26382 /* A non-type template parm. */
26383 return true;
26384
26385 case CONST_DECL:
26386 /* A non-type template parm. */
26387 if (DECL_TEMPLATE_PARM_P (expression))
26388 return true;
26389 return value_dependent_expression_p (DECL_INITIAL (expression));
26390
26391 case VAR_DECL:
26392 /* A constant with literal type and is initialized
26393 with an expression that is value-dependent. */
26394 if (DECL_DEPENDENT_INIT_P (expression)
26395 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26396 || TYPE_REF_P (TREE_TYPE (expression)))
26397 return true;
26398 if (DECL_HAS_VALUE_EXPR_P (expression))
26399 {
26400 tree value_expr = DECL_VALUE_EXPR (expression);
26401 if (value_dependent_expression_p (value_expr)
26402 /* __PRETTY_FUNCTION__ inside a template function is dependent
26403 on the name of the function. */
26404 || (DECL_PRETTY_FUNCTION_P (expression)
26405 /* It might be used in a template, but not a template
26406 function, in which case its DECL_VALUE_EXPR will be
26407 "top level". */
26408 && value_expr == error_mark_node))
26409 return true;
26410 }
26411 return false;
26412
26413 case DYNAMIC_CAST_EXPR:
26414 case STATIC_CAST_EXPR:
26415 case CONST_CAST_EXPR:
26416 case REINTERPRET_CAST_EXPR:
26417 case CAST_EXPR:
26418 case IMPLICIT_CONV_EXPR:
26419 /* These expressions are value-dependent if the type to which
26420 the cast occurs is dependent or the expression being casted
26421 is value-dependent. */
26422 {
26423 tree type = TREE_TYPE (expression);
26424
26425 if (dependent_type_p (type))
26426 return true;
26427
26428 /* A functional cast has a list of operands. */
26429 expression = TREE_OPERAND (expression, 0);
26430 if (!expression)
26431 {
26432 /* If there are no operands, it must be an expression such
26433 as "int()". This should not happen for aggregate types
26434 because it would form non-constant expressions. */
26435 gcc_assert (cxx_dialect >= cxx11
26436 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26437
26438 return false;
26439 }
26440
26441 if (TREE_CODE (expression) == TREE_LIST)
26442 return any_value_dependent_elements_p (expression);
26443
26444 return value_dependent_expression_p (expression);
26445 }
26446
26447 case SIZEOF_EXPR:
26448 if (SIZEOF_EXPR_TYPE_P (expression))
26449 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26450 /* FALLTHRU */
26451 case ALIGNOF_EXPR:
26452 case TYPEID_EXPR:
26453 /* A `sizeof' expression is value-dependent if the operand is
26454 type-dependent or is a pack expansion. */
26455 expression = TREE_OPERAND (expression, 0);
26456 if (PACK_EXPANSION_P (expression))
26457 return true;
26458 else if (TYPE_P (expression))
26459 return dependent_type_p (expression);
26460 return instantiation_dependent_uneval_expression_p (expression);
26461
26462 case AT_ENCODE_EXPR:
26463 /* An 'encode' expression is value-dependent if the operand is
26464 type-dependent. */
26465 expression = TREE_OPERAND (expression, 0);
26466 return dependent_type_p (expression);
26467
26468 case NOEXCEPT_EXPR:
26469 expression = TREE_OPERAND (expression, 0);
26470 return instantiation_dependent_uneval_expression_p (expression);
26471
26472 case SCOPE_REF:
26473 /* All instantiation-dependent expressions should also be considered
26474 value-dependent. */
26475 return instantiation_dependent_scope_ref_p (expression);
26476
26477 case COMPONENT_REF:
26478 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26479 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26480
26481 case NONTYPE_ARGUMENT_PACK:
26482 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26483 is value-dependent. */
26484 {
26485 tree values = ARGUMENT_PACK_ARGS (expression);
26486 int i, len = TREE_VEC_LENGTH (values);
26487
26488 for (i = 0; i < len; ++i)
26489 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26490 return true;
26491
26492 return false;
26493 }
26494
26495 case TRAIT_EXPR:
26496 {
26497 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26498
26499 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26500 return true;
26501
26502 if (!type2)
26503 return false;
26504
26505 if (TREE_CODE (type2) != TREE_LIST)
26506 return dependent_type_p (type2);
26507
26508 for (; type2; type2 = TREE_CHAIN (type2))
26509 if (dependent_type_p (TREE_VALUE (type2)))
26510 return true;
26511
26512 return false;
26513 }
26514
26515 case MODOP_EXPR:
26516 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26517 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26518
26519 case ARRAY_REF:
26520 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26521 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26522
26523 case ADDR_EXPR:
26524 {
26525 tree op = TREE_OPERAND (expression, 0);
26526 return (value_dependent_expression_p (op)
26527 || has_value_dependent_address (op));
26528 }
26529
26530 case REQUIRES_EXPR:
26531 /* Treat all requires-expressions as value-dependent so
26532 we don't try to fold them. */
26533 return true;
26534
26535 case TYPE_REQ:
26536 return dependent_type_p (TREE_OPERAND (expression, 0));
26537
26538 case CALL_EXPR:
26539 {
26540 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26541 return true;
26542 tree fn = get_callee_fndecl (expression);
26543 int i, nargs;
26544 nargs = call_expr_nargs (expression);
26545 for (i = 0; i < nargs; ++i)
26546 {
26547 tree op = CALL_EXPR_ARG (expression, i);
26548 /* In a call to a constexpr member function, look through the
26549 implicit ADDR_EXPR on the object argument so that it doesn't
26550 cause the call to be considered value-dependent. We also
26551 look through it in potential_constant_expression. */
26552 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26553 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26554 && TREE_CODE (op) == ADDR_EXPR)
26555 op = TREE_OPERAND (op, 0);
26556 if (value_dependent_expression_p (op))
26557 return true;
26558 }
26559 return false;
26560 }
26561
26562 case TEMPLATE_ID_EXPR:
26563 return concept_definition_p (TREE_OPERAND (expression, 0));
26564
26565 case CONSTRUCTOR:
26566 {
26567 unsigned ix;
26568 tree val;
26569 if (dependent_type_p (TREE_TYPE (expression)))
26570 return true;
26571 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26572 if (value_dependent_expression_p (val))
26573 return true;
26574 return false;
26575 }
26576
26577 case STMT_EXPR:
26578 /* Treat a GNU statement expression as dependent to avoid crashing
26579 under instantiate_non_dependent_expr; it can't be constant. */
26580 return true;
26581
26582 default:
26583 /* A constant expression is value-dependent if any subexpression is
26584 value-dependent. */
26585 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26586 {
26587 case tcc_reference:
26588 case tcc_unary:
26589 case tcc_comparison:
26590 case tcc_binary:
26591 case tcc_expression:
26592 case tcc_vl_exp:
26593 {
26594 int i, len = cp_tree_operand_length (expression);
26595
26596 for (i = 0; i < len; i++)
26597 {
26598 tree t = TREE_OPERAND (expression, i);
26599
26600 /* In some cases, some of the operands may be missing.
26601 (For example, in the case of PREDECREMENT_EXPR, the
26602 amount to increment by may be missing.) That doesn't
26603 make the expression dependent. */
26604 if (t && value_dependent_expression_p (t))
26605 return true;
26606 }
26607 }
26608 break;
26609 default:
26610 break;
26611 }
26612 break;
26613 }
26614
26615 /* The expression is not value-dependent. */
26616 return false;
26617 }
26618
26619 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26620 [temp.dep.expr]. Note that an expression with no type is
26621 considered dependent. Other parts of the compiler arrange for an
26622 expression with type-dependent subexpressions to have no type, so
26623 this function doesn't have to be fully recursive. */
26624
26625 bool
26626 type_dependent_expression_p (tree expression)
26627 {
26628 if (!processing_template_decl)
26629 return false;
26630
26631 if (expression == NULL_TREE || expression == error_mark_node)
26632 return false;
26633
26634 STRIP_ANY_LOCATION_WRAPPER (expression);
26635
26636 /* An unresolved name is always dependent. */
26637 if (identifier_p (expression)
26638 || TREE_CODE (expression) == USING_DECL
26639 || TREE_CODE (expression) == WILDCARD_DECL)
26640 return true;
26641
26642 /* A lambda-expression in template context is dependent. dependent_type_p is
26643 true for a lambda in the scope of a class or function template, but that
26644 doesn't cover all template contexts, like a default template argument. */
26645 if (TREE_CODE (expression) == LAMBDA_EXPR)
26646 return true;
26647
26648 /* A fold expression is type-dependent. */
26649 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26650 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26651 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26652 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26653 return true;
26654
26655 /* Some expression forms are never type-dependent. */
26656 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
26657 || TREE_CODE (expression) == SIZEOF_EXPR
26658 || TREE_CODE (expression) == ALIGNOF_EXPR
26659 || TREE_CODE (expression) == AT_ENCODE_EXPR
26660 || TREE_CODE (expression) == NOEXCEPT_EXPR
26661 || TREE_CODE (expression) == TRAIT_EXPR
26662 || TREE_CODE (expression) == TYPEID_EXPR
26663 || TREE_CODE (expression) == DELETE_EXPR
26664 || TREE_CODE (expression) == VEC_DELETE_EXPR
26665 || TREE_CODE (expression) == THROW_EXPR
26666 || TREE_CODE (expression) == REQUIRES_EXPR)
26667 return false;
26668
26669 /* The types of these expressions depends only on the type to which
26670 the cast occurs. */
26671 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26672 || TREE_CODE (expression) == STATIC_CAST_EXPR
26673 || TREE_CODE (expression) == CONST_CAST_EXPR
26674 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26675 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26676 || TREE_CODE (expression) == CAST_EXPR)
26677 return dependent_type_p (TREE_TYPE (expression));
26678
26679 /* The types of these expressions depends only on the type created
26680 by the expression. */
26681 if (TREE_CODE (expression) == NEW_EXPR
26682 || TREE_CODE (expression) == VEC_NEW_EXPR)
26683 {
26684 /* For NEW_EXPR tree nodes created inside a template, either
26685 the object type itself or a TREE_LIST may appear as the
26686 operand 1. */
26687 tree type = TREE_OPERAND (expression, 1);
26688 if (TREE_CODE (type) == TREE_LIST)
26689 /* This is an array type. We need to check array dimensions
26690 as well. */
26691 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26692 || value_dependent_expression_p
26693 (TREE_OPERAND (TREE_VALUE (type), 1));
26694 else
26695 return dependent_type_p (type);
26696 }
26697
26698 if (TREE_CODE (expression) == SCOPE_REF)
26699 {
26700 tree scope = TREE_OPERAND (expression, 0);
26701 tree name = TREE_OPERAND (expression, 1);
26702
26703 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26704 contains an identifier associated by name lookup with one or more
26705 declarations declared with a dependent type, or...a
26706 nested-name-specifier or qualified-id that names a member of an
26707 unknown specialization. */
26708 return (type_dependent_expression_p (name)
26709 || dependent_scope_p (scope));
26710 }
26711
26712 if (TREE_CODE (expression) == TEMPLATE_DECL
26713 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26714 return uses_outer_template_parms (expression);
26715
26716 if (TREE_CODE (expression) == STMT_EXPR)
26717 expression = stmt_expr_value_expr (expression);
26718
26719 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26720 {
26721 tree elt;
26722 unsigned i;
26723
26724 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26725 {
26726 if (type_dependent_expression_p (elt))
26727 return true;
26728 }
26729 return false;
26730 }
26731
26732 /* A static data member of the current instantiation with incomplete
26733 array type is type-dependent, as the definition and specializations
26734 can have different bounds. */
26735 if (VAR_P (expression)
26736 && DECL_CLASS_SCOPE_P (expression)
26737 && dependent_type_p (DECL_CONTEXT (expression))
26738 && VAR_HAD_UNKNOWN_BOUND (expression))
26739 return true;
26740
26741 /* An array of unknown bound depending on a variadic parameter, eg:
26742
26743 template<typename... Args>
26744 void foo (Args... args)
26745 {
26746 int arr[] = { args... };
26747 }
26748
26749 template<int... vals>
26750 void bar ()
26751 {
26752 int arr[] = { vals... };
26753 }
26754
26755 If the array has no length and has an initializer, it must be that
26756 we couldn't determine its length in cp_complete_array_type because
26757 it is dependent. */
26758 if (VAR_P (expression)
26759 && TREE_TYPE (expression) != NULL_TREE
26760 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26761 && !TYPE_DOMAIN (TREE_TYPE (expression))
26762 && DECL_INITIAL (expression))
26763 return true;
26764
26765 /* A function or variable template-id is type-dependent if it has any
26766 dependent template arguments. */
26767 if (VAR_OR_FUNCTION_DECL_P (expression)
26768 && DECL_LANG_SPECIFIC (expression)
26769 && DECL_TEMPLATE_INFO (expression))
26770 {
26771 /* Consider the innermost template arguments, since those are the ones
26772 that come from the template-id; the template arguments for the
26773 enclosing class do not make it type-dependent unless they are used in
26774 the type of the decl. */
26775 if (instantiates_primary_template_p (expression)
26776 && (any_dependent_template_arguments_p
26777 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26778 return true;
26779 }
26780
26781 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26782 type-dependent. Checking this is important for functions with auto return
26783 type, which looks like a dependent type. */
26784 if (TREE_CODE (expression) == FUNCTION_DECL
26785 && !(DECL_CLASS_SCOPE_P (expression)
26786 && dependent_type_p (DECL_CONTEXT (expression)))
26787 && !(DECL_LANG_SPECIFIC (expression)
26788 && DECL_FRIEND_P (expression)
26789 && (!DECL_FRIEND_CONTEXT (expression)
26790 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26791 && !DECL_LOCAL_FUNCTION_P (expression))
26792 {
26793 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26794 || undeduced_auto_decl (expression));
26795 return false;
26796 }
26797
26798 /* Always dependent, on the number of arguments if nothing else. */
26799 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
26800 return true;
26801
26802 if (TREE_TYPE (expression) == unknown_type_node)
26803 {
26804 if (TREE_CODE (expression) == ADDR_EXPR)
26805 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
26806 if (TREE_CODE (expression) == COMPONENT_REF
26807 || TREE_CODE (expression) == OFFSET_REF)
26808 {
26809 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
26810 return true;
26811 expression = TREE_OPERAND (expression, 1);
26812 if (identifier_p (expression))
26813 return false;
26814 }
26815 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26816 if (TREE_CODE (expression) == SCOPE_REF)
26817 return false;
26818
26819 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
26820 if (TREE_CODE (expression) == CO_AWAIT_EXPR
26821 || TREE_CODE (expression) == CO_YIELD_EXPR)
26822 return true;
26823
26824 if (BASELINK_P (expression))
26825 {
26826 if (BASELINK_OPTYPE (expression)
26827 && dependent_type_p (BASELINK_OPTYPE (expression)))
26828 return true;
26829 expression = BASELINK_FUNCTIONS (expression);
26830 }
26831
26832 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
26833 {
26834 if (any_dependent_template_arguments_p
26835 (TREE_OPERAND (expression, 1)))
26836 return true;
26837 expression = TREE_OPERAND (expression, 0);
26838 if (identifier_p (expression))
26839 return true;
26840 }
26841
26842 gcc_assert (OVL_P (expression));
26843
26844 for (lkp_iterator iter (expression); iter; ++iter)
26845 if (type_dependent_expression_p (*iter))
26846 return true;
26847
26848 return false;
26849 }
26850
26851 /* The type of a non-type template parm declared with a placeholder type
26852 depends on the corresponding template argument, even though
26853 placeholders are not normally considered dependent. */
26854 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26855 && is_auto (TREE_TYPE (expression)))
26856 return true;
26857
26858 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26859
26860 /* Dependent type attributes might not have made it from the decl to
26861 the type yet. */
26862 if (DECL_P (expression)
26863 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26864 return true;
26865
26866 return (dependent_type_p (TREE_TYPE (expression)));
26867 }
26868
26869 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26870 type-dependent if the expression refers to a member of the current
26871 instantiation and the type of the referenced member is dependent, or the
26872 class member access expression refers to a member of an unknown
26873 specialization.
26874
26875 This function returns true if the OBJECT in such a class member access
26876 expression is of an unknown specialization. */
26877
26878 bool
26879 type_dependent_object_expression_p (tree object)
26880 {
26881 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26882 dependent. */
26883 if (TREE_CODE (object) == IDENTIFIER_NODE)
26884 return true;
26885 tree scope = TREE_TYPE (object);
26886 return (!scope || dependent_scope_p (scope));
26887 }
26888
26889 /* walk_tree callback function for instantiation_dependent_expression_p,
26890 below. Returns non-zero if a dependent subexpression is found. */
26891
26892 static tree
26893 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26894 void * /*data*/)
26895 {
26896 if (TYPE_P (*tp))
26897 {
26898 /* We don't have to worry about decltype currently because decltype
26899 of an instantiation-dependent expr is a dependent type. This
26900 might change depending on the resolution of DR 1172. */
26901 *walk_subtrees = false;
26902 return NULL_TREE;
26903 }
26904 enum tree_code code = TREE_CODE (*tp);
26905 switch (code)
26906 {
26907 /* Don't treat an argument list as dependent just because it has no
26908 TREE_TYPE. */
26909 case TREE_LIST:
26910 case TREE_VEC:
26911 case NONTYPE_ARGUMENT_PACK:
26912 return NULL_TREE;
26913
26914 case TEMPLATE_PARM_INDEX:
26915 if (dependent_type_p (TREE_TYPE (*tp)))
26916 return *tp;
26917 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26918 return *tp;
26919 /* We'll check value-dependence separately. */
26920 return NULL_TREE;
26921
26922 /* Handle expressions with type operands. */
26923 case SIZEOF_EXPR:
26924 case ALIGNOF_EXPR:
26925 case TYPEID_EXPR:
26926 case AT_ENCODE_EXPR:
26927 {
26928 tree op = TREE_OPERAND (*tp, 0);
26929 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26930 op = TREE_TYPE (op);
26931 if (TYPE_P (op))
26932 {
26933 if (dependent_type_p (op))
26934 return *tp;
26935 else
26936 {
26937 *walk_subtrees = false;
26938 return NULL_TREE;
26939 }
26940 }
26941 break;
26942 }
26943
26944 case COMPONENT_REF:
26945 if (identifier_p (TREE_OPERAND (*tp, 1)))
26946 /* In a template, finish_class_member_access_expr creates a
26947 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26948 type-dependent, so that we can check access control at
26949 instantiation time (PR 42277). See also Core issue 1273. */
26950 return *tp;
26951 break;
26952
26953 case SCOPE_REF:
26954 if (instantiation_dependent_scope_ref_p (*tp))
26955 return *tp;
26956 else
26957 break;
26958
26959 /* Treat statement-expressions as dependent. */
26960 case BIND_EXPR:
26961 return *tp;
26962
26963 /* Treat requires-expressions as dependent. */
26964 case REQUIRES_EXPR:
26965 return *tp;
26966
26967 case CALL_EXPR:
26968 /* Treat concept checks as dependent. */
26969 if (concept_check_p (*tp))
26970 return *tp;
26971 break;
26972
26973 case TEMPLATE_ID_EXPR:
26974 /* Treat concept checks as dependent. */
26975 if (concept_check_p (*tp))
26976 return *tp;
26977 break;
26978
26979 case CONSTRUCTOR:
26980 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26981 return *tp;
26982 break;
26983
26984 default:
26985 break;
26986 }
26987
26988 if (type_dependent_expression_p (*tp))
26989 return *tp;
26990 else
26991 return NULL_TREE;
26992 }
26993
26994 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26995 sense defined by the ABI:
26996
26997 "An expression is instantiation-dependent if it is type-dependent
26998 or value-dependent, or it has a subexpression that is type-dependent
26999 or value-dependent."
27000
27001 Except don't actually check value-dependence for unevaluated expressions,
27002 because in sizeof(i) we don't care about the value of i. Checking
27003 type-dependence will in turn check value-dependence of array bounds/template
27004 arguments as needed. */
27005
27006 bool
27007 instantiation_dependent_uneval_expression_p (tree expression)
27008 {
27009 tree result;
27010
27011 if (!processing_template_decl)
27012 return false;
27013
27014 if (expression == error_mark_node)
27015 return false;
27016
27017 result = cp_walk_tree_without_duplicates (&expression,
27018 instantiation_dependent_r, NULL);
27019 return result != NULL_TREE;
27020 }
27021
27022 /* As above, but also check value-dependence of the expression as a whole. */
27023
27024 bool
27025 instantiation_dependent_expression_p (tree expression)
27026 {
27027 return (instantiation_dependent_uneval_expression_p (expression)
27028 || value_dependent_expression_p (expression));
27029 }
27030
27031 /* Like type_dependent_expression_p, but it also works while not processing
27032 a template definition, i.e. during substitution or mangling. */
27033
27034 bool
27035 type_dependent_expression_p_push (tree expr)
27036 {
27037 bool b;
27038 ++processing_template_decl;
27039 b = type_dependent_expression_p (expr);
27040 --processing_template_decl;
27041 return b;
27042 }
27043
27044 /* Returns TRUE if ARGS contains a type-dependent expression. */
27045
27046 bool
27047 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27048 {
27049 unsigned int i;
27050 tree arg;
27051
27052 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27053 {
27054 if (type_dependent_expression_p (arg))
27055 return true;
27056 }
27057 return false;
27058 }
27059
27060 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27061 expressions) contains any type-dependent expressions. */
27062
27063 bool
27064 any_type_dependent_elements_p (const_tree list)
27065 {
27066 for (; list; list = TREE_CHAIN (list))
27067 if (type_dependent_expression_p (TREE_VALUE (list)))
27068 return true;
27069
27070 return false;
27071 }
27072
27073 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27074 expressions) contains any value-dependent expressions. */
27075
27076 bool
27077 any_value_dependent_elements_p (const_tree list)
27078 {
27079 for (; list; list = TREE_CHAIN (list))
27080 if (value_dependent_expression_p (TREE_VALUE (list)))
27081 return true;
27082
27083 return false;
27084 }
27085
27086 /* Returns TRUE if the ARG (a template argument) is dependent. */
27087
27088 bool
27089 dependent_template_arg_p (tree arg)
27090 {
27091 if (!processing_template_decl)
27092 return false;
27093
27094 /* Assume a template argument that was wrongly written by the user
27095 is dependent. This is consistent with what
27096 any_dependent_template_arguments_p [that calls this function]
27097 does. */
27098 if (!arg || arg == error_mark_node)
27099 return true;
27100
27101 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27102 arg = argument_pack_select_arg (arg);
27103
27104 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27105 return true;
27106 if (TREE_CODE (arg) == TEMPLATE_DECL)
27107 {
27108 if (DECL_TEMPLATE_PARM_P (arg))
27109 return true;
27110 /* A member template of a dependent class is not necessarily
27111 type-dependent, but it is a dependent template argument because it
27112 will be a member of an unknown specialization to that template. */
27113 tree scope = CP_DECL_CONTEXT (arg);
27114 return TYPE_P (scope) && dependent_type_p (scope);
27115 }
27116 else if (ARGUMENT_PACK_P (arg))
27117 {
27118 tree args = ARGUMENT_PACK_ARGS (arg);
27119 int i, len = TREE_VEC_LENGTH (args);
27120 for (i = 0; i < len; ++i)
27121 {
27122 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27123 return true;
27124 }
27125
27126 return false;
27127 }
27128 else if (TYPE_P (arg))
27129 return dependent_type_p (arg);
27130 else
27131 return value_dependent_expression_p (arg);
27132 }
27133
27134 /* Returns true if ARGS (a collection of template arguments) contains
27135 any types that require structural equality testing. */
27136
27137 bool
27138 any_template_arguments_need_structural_equality_p (tree args)
27139 {
27140 int i;
27141 int j;
27142
27143 if (!args)
27144 return false;
27145 if (args == error_mark_node)
27146 return true;
27147
27148 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27149 {
27150 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27151 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27152 {
27153 tree arg = TREE_VEC_ELT (level, j);
27154 tree packed_args = NULL_TREE;
27155 int k, len = 1;
27156
27157 if (ARGUMENT_PACK_P (arg))
27158 {
27159 /* Look inside the argument pack. */
27160 packed_args = ARGUMENT_PACK_ARGS (arg);
27161 len = TREE_VEC_LENGTH (packed_args);
27162 }
27163
27164 for (k = 0; k < len; ++k)
27165 {
27166 if (packed_args)
27167 arg = TREE_VEC_ELT (packed_args, k);
27168
27169 if (error_operand_p (arg))
27170 return true;
27171 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27172 continue;
27173 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27174 return true;
27175 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27176 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27177 return true;
27178 }
27179 }
27180 }
27181
27182 return false;
27183 }
27184
27185 /* Returns true if ARGS (a collection of template arguments) contains
27186 any dependent arguments. */
27187
27188 bool
27189 any_dependent_template_arguments_p (const_tree args)
27190 {
27191 int i;
27192 int j;
27193
27194 if (!args)
27195 return false;
27196 if (args == error_mark_node)
27197 return true;
27198
27199 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27200 {
27201 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27202 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27203 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27204 return true;
27205 }
27206
27207 return false;
27208 }
27209
27210 /* Returns true if ARGS contains any errors. */
27211
27212 bool
27213 any_erroneous_template_args_p (const_tree args)
27214 {
27215 int i;
27216 int j;
27217
27218 if (args == error_mark_node)
27219 return true;
27220
27221 if (args && TREE_CODE (args) != TREE_VEC)
27222 {
27223 if (tree ti = get_template_info (args))
27224 args = TI_ARGS (ti);
27225 else
27226 args = NULL_TREE;
27227 }
27228
27229 if (!args)
27230 return false;
27231
27232 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27233 {
27234 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27235 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27236 if (error_operand_p (TREE_VEC_ELT (level, j)))
27237 return true;
27238 }
27239
27240 return false;
27241 }
27242
27243 /* Returns TRUE if the template TMPL is type-dependent. */
27244
27245 bool
27246 dependent_template_p (tree tmpl)
27247 {
27248 if (TREE_CODE (tmpl) == OVERLOAD)
27249 {
27250 for (lkp_iterator iter (tmpl); iter; ++iter)
27251 if (dependent_template_p (*iter))
27252 return true;
27253 return false;
27254 }
27255
27256 /* Template template parameters are dependent. */
27257 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27258 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27259 return true;
27260 /* So are names that have not been looked up. */
27261 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27262 return true;
27263 return false;
27264 }
27265
27266 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27267
27268 bool
27269 dependent_template_id_p (tree tmpl, tree args)
27270 {
27271 return (dependent_template_p (tmpl)
27272 || any_dependent_template_arguments_p (args));
27273 }
27274
27275 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27276 are dependent. */
27277
27278 bool
27279 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27280 {
27281 int i;
27282
27283 if (!processing_template_decl)
27284 return false;
27285
27286 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27287 {
27288 tree decl = TREE_VEC_ELT (declv, i);
27289 tree init = TREE_VEC_ELT (initv, i);
27290 tree cond = TREE_VEC_ELT (condv, i);
27291 tree incr = TREE_VEC_ELT (incrv, i);
27292
27293 if (type_dependent_expression_p (decl)
27294 || TREE_CODE (decl) == SCOPE_REF)
27295 return true;
27296
27297 if (init && type_dependent_expression_p (init))
27298 return true;
27299
27300 if (cond == global_namespace)
27301 return true;
27302
27303 if (type_dependent_expression_p (cond))
27304 return true;
27305
27306 if (COMPARISON_CLASS_P (cond)
27307 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27308 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27309 return true;
27310
27311 if (TREE_CODE (incr) == MODOP_EXPR)
27312 {
27313 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27314 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27315 return true;
27316 }
27317 else if (type_dependent_expression_p (incr))
27318 return true;
27319 else if (TREE_CODE (incr) == MODIFY_EXPR)
27320 {
27321 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27322 return true;
27323 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27324 {
27325 tree t = TREE_OPERAND (incr, 1);
27326 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27327 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27328 return true;
27329
27330 /* If this loop has a class iterator with != comparison
27331 with increment other than i++/++i/i--/--i, make sure the
27332 increment is constant. */
27333 if (CLASS_TYPE_P (TREE_TYPE (decl))
27334 && TREE_CODE (cond) == NE_EXPR)
27335 {
27336 if (TREE_OPERAND (t, 0) == decl)
27337 t = TREE_OPERAND (t, 1);
27338 else
27339 t = TREE_OPERAND (t, 0);
27340 if (TREE_CODE (t) != INTEGER_CST)
27341 return true;
27342 }
27343 }
27344 }
27345 }
27346
27347 return false;
27348 }
27349
27350 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27351 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27352 no such TYPE can be found. Note that this function peers inside
27353 uninstantiated templates and therefore should be used only in
27354 extremely limited situations. ONLY_CURRENT_P restricts this
27355 peering to the currently open classes hierarchy (which is required
27356 when comparing types). */
27357
27358 tree
27359 resolve_typename_type (tree type, bool only_current_p)
27360 {
27361 tree scope;
27362 tree name;
27363 tree decl;
27364 int quals;
27365 tree pushed_scope;
27366 tree result;
27367
27368 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27369
27370 scope = TYPE_CONTEXT (type);
27371 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27372 gcc_checking_assert (uses_template_parms (scope));
27373
27374 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27375 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27376 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27377 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27378 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27379 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27380 the TYPENAME_TYPE instead, we avoid messing up with a possible
27381 typedef variant case. */
27382 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27383
27384 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27385 it first before we can figure out what NAME refers to. */
27386 if (TREE_CODE (scope) == TYPENAME_TYPE)
27387 {
27388 if (TYPENAME_IS_RESOLVING_P (scope))
27389 /* Given a class template A with a dependent base with nested type C,
27390 typedef typename A::C::C C will land us here, as trying to resolve
27391 the initial A::C leads to the local C typedef, which leads back to
27392 A::C::C. So we break the recursion now. */
27393 return type;
27394 else
27395 scope = resolve_typename_type (scope, only_current_p);
27396 }
27397 /* If we don't know what SCOPE refers to, then we cannot resolve the
27398 TYPENAME_TYPE. */
27399 if (!CLASS_TYPE_P (scope))
27400 return type;
27401 /* If this is a typedef, we don't want to look inside (c++/11987). */
27402 if (typedef_variant_p (type))
27403 return type;
27404 /* If SCOPE isn't the template itself, it will not have a valid
27405 TYPE_FIELDS list. */
27406 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27407 /* scope is either the template itself or a compatible instantiation
27408 like X<T>, so look up the name in the original template. */
27409 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27410 /* If scope has no fields, it can't be a current instantiation. Check this
27411 before currently_open_class to avoid infinite recursion (71515). */
27412 if (!TYPE_FIELDS (scope))
27413 return type;
27414 /* If the SCOPE is not the current instantiation, there's no reason
27415 to look inside it. */
27416 if (only_current_p && !currently_open_class (scope))
27417 return type;
27418 /* Enter the SCOPE so that name lookup will be resolved as if we
27419 were in the class definition. In particular, SCOPE will no
27420 longer be considered a dependent type. */
27421 pushed_scope = push_scope (scope);
27422 /* Look up the declaration. */
27423 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27424 tf_warning_or_error);
27425
27426 result = NULL_TREE;
27427
27428 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27429 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27430 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27431 if (!decl)
27432 /*nop*/;
27433 else if (identifier_p (fullname)
27434 && TREE_CODE (decl) == TYPE_DECL)
27435 {
27436 result = TREE_TYPE (decl);
27437 if (result == error_mark_node)
27438 result = NULL_TREE;
27439 }
27440 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27441 && DECL_CLASS_TEMPLATE_P (decl))
27442 {
27443 /* Obtain the template and the arguments. */
27444 tree tmpl = TREE_OPERAND (fullname, 0);
27445 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27446 {
27447 /* We get here with a plain identifier because a previous tentative
27448 parse of the nested-name-specifier as part of a ptr-operator saw
27449 ::template X<A>. The use of ::template is necessary in a
27450 ptr-operator, but wrong in a declarator-id.
27451
27452 [temp.names]: In a qualified-id of a declarator-id, the keyword
27453 template shall not appear at the top level. */
27454 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27455 "keyword %<template%> not allowed in declarator-id");
27456 tmpl = decl;
27457 }
27458 tree args = TREE_OPERAND (fullname, 1);
27459 /* Instantiate the template. */
27460 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27461 /*entering_scope=*/true,
27462 tf_error | tf_user);
27463 if (result == error_mark_node)
27464 result = NULL_TREE;
27465 }
27466
27467 /* Leave the SCOPE. */
27468 if (pushed_scope)
27469 pop_scope (pushed_scope);
27470
27471 /* If we failed to resolve it, return the original typename. */
27472 if (!result)
27473 return type;
27474
27475 /* If lookup found a typename type, resolve that too. */
27476 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27477 {
27478 /* Ill-formed programs can cause infinite recursion here, so we
27479 must catch that. */
27480 TYPENAME_IS_RESOLVING_P (result) = 1;
27481 result = resolve_typename_type (result, only_current_p);
27482 TYPENAME_IS_RESOLVING_P (result) = 0;
27483 }
27484
27485 /* Qualify the resulting type. */
27486 quals = cp_type_quals (type);
27487 if (quals)
27488 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27489
27490 return result;
27491 }
27492
27493 /* EXPR is an expression which is not type-dependent. Return a proxy
27494 for EXPR that can be used to compute the types of larger
27495 expressions containing EXPR. */
27496
27497 tree
27498 build_non_dependent_expr (tree expr)
27499 {
27500 tree orig_expr = expr;
27501 tree inner_expr;
27502
27503 /* When checking, try to get a constant value for all non-dependent
27504 expressions in order to expose bugs in *_dependent_expression_p
27505 and constexpr. This can affect code generation, see PR70704, so
27506 only do this for -fchecking=2. */
27507 if (flag_checking > 1
27508 && cxx_dialect >= cxx11
27509 /* Don't do this during nsdmi parsing as it can lead to
27510 unexpected recursive instantiations. */
27511 && !parsing_nsdmi ()
27512 /* Don't do this during concept processing either and for
27513 the same reason. */
27514 && !processing_constraint_expression_p ())
27515 fold_non_dependent_expr (expr, tf_none);
27516
27517 STRIP_ANY_LOCATION_WRAPPER (expr);
27518
27519 /* Preserve OVERLOADs; the functions must be available to resolve
27520 types. */
27521 inner_expr = expr;
27522 if (TREE_CODE (inner_expr) == STMT_EXPR)
27523 inner_expr = stmt_expr_value_expr (inner_expr);
27524 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27525 inner_expr = TREE_OPERAND (inner_expr, 0);
27526 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27527 inner_expr = TREE_OPERAND (inner_expr, 1);
27528 if (is_overloaded_fn (inner_expr)
27529 || TREE_CODE (inner_expr) == OFFSET_REF)
27530 return orig_expr;
27531 /* There is no need to return a proxy for a variable or enumerator. */
27532 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27533 return orig_expr;
27534 /* Preserve string constants; conversions from string constants to
27535 "char *" are allowed, even though normally a "const char *"
27536 cannot be used to initialize a "char *". */
27537 if (TREE_CODE (expr) == STRING_CST)
27538 return orig_expr;
27539 /* Preserve void and arithmetic constants, as an optimization -- there is no
27540 reason to create a new node. */
27541 if (TREE_CODE (expr) == VOID_CST
27542 || TREE_CODE (expr) == INTEGER_CST
27543 || TREE_CODE (expr) == REAL_CST)
27544 return orig_expr;
27545 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27546 There is at least one place where we want to know that a
27547 particular expression is a throw-expression: when checking a ?:
27548 expression, there are special rules if the second or third
27549 argument is a throw-expression. */
27550 if (TREE_CODE (expr) == THROW_EXPR)
27551 return orig_expr;
27552
27553 /* Don't wrap an initializer list, we need to be able to look inside. */
27554 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27555 return orig_expr;
27556
27557 /* Don't wrap a dummy object, we need to be able to test for it. */
27558 if (is_dummy_object (expr))
27559 return orig_expr;
27560
27561 if (TREE_CODE (expr) == COND_EXPR)
27562 return build3 (COND_EXPR,
27563 TREE_TYPE (expr),
27564 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27565 (TREE_OPERAND (expr, 1)
27566 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27567 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27568 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27569 if (TREE_CODE (expr) == COMPOUND_EXPR
27570 && !COMPOUND_EXPR_OVERLOADED (expr))
27571 return build2 (COMPOUND_EXPR,
27572 TREE_TYPE (expr),
27573 TREE_OPERAND (expr, 0),
27574 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27575
27576 /* If the type is unknown, it can't really be non-dependent */
27577 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27578
27579 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27580 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27581 TREE_TYPE (expr), expr);
27582 }
27583
27584 /* ARGS is a vector of expressions as arguments to a function call.
27585 Replace the arguments with equivalent non-dependent expressions.
27586 This modifies ARGS in place. */
27587
27588 void
27589 make_args_non_dependent (vec<tree, va_gc> *args)
27590 {
27591 unsigned int ix;
27592 tree arg;
27593
27594 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27595 {
27596 tree newarg = build_non_dependent_expr (arg);
27597 if (newarg != arg)
27598 (*args)[ix] = newarg;
27599 }
27600 }
27601
27602 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27603 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27604 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27605
27606 static tree
27607 make_auto_1 (tree name, bool set_canonical)
27608 {
27609 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27610 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27611 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27612 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27613 (0, processing_template_decl + 1, processing_template_decl + 1,
27614 TYPE_NAME (au), NULL_TREE);
27615 if (set_canonical)
27616 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27617 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27618 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27619 if (name == decltype_auto_identifier)
27620 AUTO_IS_DECLTYPE (au) = true;
27621
27622 return au;
27623 }
27624
27625 tree
27626 make_decltype_auto (void)
27627 {
27628 return make_auto_1 (decltype_auto_identifier, true);
27629 }
27630
27631 tree
27632 make_auto (void)
27633 {
27634 return make_auto_1 (auto_identifier, true);
27635 }
27636
27637 /* Return a C++17 deduction placeholder for class template TMPL. */
27638
27639 tree
27640 make_template_placeholder (tree tmpl)
27641 {
27642 tree t = make_auto_1 (auto_identifier, false);
27643 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27644 /* Our canonical type depends on the placeholder. */
27645 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27646 return t;
27647 }
27648
27649 /* True iff T is a C++17 class template deduction placeholder. */
27650
27651 bool
27652 template_placeholder_p (tree t)
27653 {
27654 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27655 }
27656
27657 /* Make a "constrained auto" type-specifier. This is an auto or
27658 decltype(auto) type with constraints that must be associated after
27659 deduction. The constraint is formed from the given concept CON
27660 and its optional sequence of template arguments ARGS.
27661
27662 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27663
27664 static tree
27665 make_constrained_placeholder_type (tree type, tree con, tree args)
27666 {
27667 /* Build the constraint. */
27668 tree tmpl = DECL_TI_TEMPLATE (con);
27669 tree expr = tmpl;
27670 if (TREE_CODE (con) == FUNCTION_DECL)
27671 expr = ovl_make (tmpl);
27672 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27673
27674 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27675
27676 /* Our canonical type depends on the constraint. */
27677 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27678
27679 /* Attach the constraint to the type declaration. */
27680 return TYPE_NAME (type);
27681 }
27682
27683 /* Make a "constrained auto" type-specifier. */
27684
27685 tree
27686 make_constrained_auto (tree con, tree args)
27687 {
27688 tree type = make_auto_1 (auto_identifier, false);
27689 return make_constrained_placeholder_type (type, con, args);
27690 }
27691
27692 /* Make a "constrained decltype(auto)" type-specifier. */
27693
27694 tree
27695 make_constrained_decltype_auto (tree con, tree args)
27696 {
27697 tree type = make_auto_1 (decltype_auto_identifier, false);
27698 return make_constrained_placeholder_type (type, con, args);
27699 }
27700
27701 /* Build and return a concept definition. Like other templates, the
27702 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27703 the TEMPLATE_DECL. */
27704
27705 tree
27706 finish_concept_definition (cp_expr id, tree init)
27707 {
27708 gcc_assert (identifier_p (id));
27709 gcc_assert (processing_template_decl);
27710
27711 location_t loc = id.get_location();
27712
27713 /* A concept-definition shall not have associated constraints. */
27714 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27715 {
27716 error_at (loc, "a concept cannot be constrained");
27717 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27718 }
27719
27720 /* A concept-definition shall appear in namespace scope. Templates
27721 aren't allowed in block scope, so we only need to check for class
27722 scope. */
27723 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27724 {
27725 error_at (loc, "concept %qE not in namespace scope", *id);
27726 return error_mark_node;
27727 }
27728
27729 /* Initially build the concept declaration; its type is bool. */
27730 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27731 DECL_CONTEXT (decl) = current_scope ();
27732 DECL_INITIAL (decl) = init;
27733
27734 /* Push the enclosing template. */
27735 return push_template_decl (decl);
27736 }
27737
27738 /* Given type ARG, return std::initializer_list<ARG>. */
27739
27740 static tree
27741 listify (tree arg)
27742 {
27743 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27744
27745 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27746 {
27747 gcc_rich_location richloc (input_location);
27748 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27749 error_at (&richloc,
27750 "deducing from brace-enclosed initializer list"
27751 " requires %<#include <initializer_list>%>");
27752
27753 return error_mark_node;
27754 }
27755 tree argvec = make_tree_vec (1);
27756 TREE_VEC_ELT (argvec, 0) = arg;
27757
27758 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27759 NULL_TREE, 0, tf_warning_or_error);
27760 }
27761
27762 /* Replace auto in TYPE with std::initializer_list<auto>. */
27763
27764 static tree
27765 listify_autos (tree type, tree auto_node)
27766 {
27767 tree init_auto = listify (strip_top_quals (auto_node));
27768 tree argvec = make_tree_vec (1);
27769 TREE_VEC_ELT (argvec, 0) = init_auto;
27770 if (processing_template_decl)
27771 argvec = add_to_template_args (current_template_args (), argvec);
27772 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27773 }
27774
27775 /* Hash traits for hashing possibly constrained 'auto'
27776 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27777
27778 struct auto_hash : default_hash_traits<tree>
27779 {
27780 static inline hashval_t hash (tree);
27781 static inline bool equal (tree, tree);
27782 };
27783
27784 /* Hash the 'auto' T. */
27785
27786 inline hashval_t
27787 auto_hash::hash (tree t)
27788 {
27789 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27790 /* Matching constrained-type-specifiers denote the same template
27791 parameter, so hash the constraint. */
27792 return hash_placeholder_constraint (c);
27793 else
27794 /* But unconstrained autos are all separate, so just hash the pointer. */
27795 return iterative_hash_object (t, 0);
27796 }
27797
27798 /* Compare two 'auto's. */
27799
27800 inline bool
27801 auto_hash::equal (tree t1, tree t2)
27802 {
27803 if (t1 == t2)
27804 return true;
27805
27806 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
27807 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
27808
27809 /* Two unconstrained autos are distinct. */
27810 if (!c1 || !c2)
27811 return false;
27812
27813 return equivalent_placeholder_constraints (c1, c2);
27814 }
27815
27816 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27817 constrained) auto, add it to the vector. */
27818
27819 static int
27820 extract_autos_r (tree t, void *data)
27821 {
27822 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
27823 if (is_auto (t))
27824 {
27825 /* All the autos were built with index 0; fix that up now. */
27826 tree *p = hash.find_slot (t, INSERT);
27827 unsigned idx;
27828 if (*p)
27829 /* If this is a repeated constrained-type-specifier, use the index we
27830 chose before. */
27831 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
27832 else
27833 {
27834 /* Otherwise this is new, so use the current count. */
27835 *p = t;
27836 idx = hash.elements () - 1;
27837 }
27838 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
27839 }
27840
27841 /* Always keep walking. */
27842 return 0;
27843 }
27844
27845 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27846 says they can appear anywhere in the type. */
27847
27848 static tree
27849 extract_autos (tree type)
27850 {
27851 hash_set<tree> visited;
27852 hash_table<auto_hash> hash (2);
27853
27854 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
27855
27856 tree tree_vec = make_tree_vec (hash.elements());
27857 for (hash_table<auto_hash>::iterator iter = hash.begin();
27858 iter != hash.end(); ++iter)
27859 {
27860 tree elt = *iter;
27861 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
27862 TREE_VEC_ELT (tree_vec, i)
27863 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
27864 }
27865
27866 return tree_vec;
27867 }
27868
27869 /* The stem for deduction guide names. */
27870 const char *const dguide_base = "__dguide_";
27871
27872 /* Return the name for a deduction guide for class template TMPL. */
27873
27874 tree
27875 dguide_name (tree tmpl)
27876 {
27877 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
27878 tree tname = TYPE_IDENTIFIER (type);
27879 char *buf = (char *) alloca (1 + strlen (dguide_base)
27880 + IDENTIFIER_LENGTH (tname));
27881 memcpy (buf, dguide_base, strlen (dguide_base));
27882 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
27883 IDENTIFIER_LENGTH (tname) + 1);
27884 tree dname = get_identifier (buf);
27885 TREE_TYPE (dname) = type;
27886 return dname;
27887 }
27888
27889 /* True if NAME is the name of a deduction guide. */
27890
27891 bool
27892 dguide_name_p (tree name)
27893 {
27894 return (TREE_CODE (name) == IDENTIFIER_NODE
27895 && TREE_TYPE (name)
27896 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
27897 strlen (dguide_base)));
27898 }
27899
27900 /* True if FN is a deduction guide. */
27901
27902 bool
27903 deduction_guide_p (const_tree fn)
27904 {
27905 if (DECL_P (fn))
27906 if (tree name = DECL_NAME (fn))
27907 return dguide_name_p (name);
27908 return false;
27909 }
27910
27911 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27912
27913 bool
27914 copy_guide_p (const_tree fn)
27915 {
27916 gcc_assert (deduction_guide_p (fn));
27917 if (!DECL_ARTIFICIAL (fn))
27918 return false;
27919 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27920 return (TREE_CHAIN (parms) == void_list_node
27921 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27922 }
27923
27924 /* True if FN is a guide generated from a constructor template. */
27925
27926 bool
27927 template_guide_p (const_tree fn)
27928 {
27929 gcc_assert (deduction_guide_p (fn));
27930 if (!DECL_ARTIFICIAL (fn))
27931 return false;
27932 tree tmpl = DECL_TI_TEMPLATE (fn);
27933 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27934 return PRIMARY_TEMPLATE_P (org);
27935 return false;
27936 }
27937
27938 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27939 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27940 template parameter types. Note that the handling of template template
27941 parameters relies on current_template_parms being set appropriately for the
27942 new template. */
27943
27944 static tree
27945 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27946 tree tsubst_args, tsubst_flags_t complain)
27947 {
27948 if (olddecl == error_mark_node)
27949 return error_mark_node;
27950
27951 tree oldidx = get_template_parm_index (olddecl);
27952
27953 tree newtype;
27954 if (TREE_CODE (olddecl) == TYPE_DECL
27955 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27956 {
27957 tree oldtype = TREE_TYPE (olddecl);
27958 newtype = cxx_make_type (TREE_CODE (oldtype));
27959 TYPE_MAIN_VARIANT (newtype) = newtype;
27960 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27961 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27962 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27963 }
27964 else
27965 {
27966 newtype = TREE_TYPE (olddecl);
27967 if (type_uses_auto (newtype))
27968 {
27969 // Substitute once to fix references to other template parameters.
27970 newtype = tsubst (newtype, tsubst_args,
27971 complain|tf_partial, NULL_TREE);
27972 // Now substitute again to reduce the level of the auto.
27973 newtype = tsubst (newtype, current_template_args (),
27974 complain, NULL_TREE);
27975 }
27976 else
27977 newtype = tsubst (newtype, tsubst_args,
27978 complain, NULL_TREE);
27979 }
27980
27981 tree newdecl
27982 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27983 DECL_NAME (olddecl), newtype);
27984 SET_DECL_TEMPLATE_PARM_P (newdecl);
27985
27986 tree newidx;
27987 if (TREE_CODE (olddecl) == TYPE_DECL
27988 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27989 {
27990 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27991 = build_template_parm_index (index, level, level,
27992 newdecl, newtype);
27993 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27994 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27995 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27996 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
27997 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
27998 else
27999 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28000
28001 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28002 {
28003 DECL_TEMPLATE_RESULT (newdecl)
28004 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28005 DECL_NAME (olddecl), newtype);
28006 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28007 // First create a copy (ttargs) of tsubst_args with an
28008 // additional level for the template template parameter's own
28009 // template parameters (ttparms).
28010 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28011 (DECL_TEMPLATE_PARMS (olddecl)));
28012 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28013 tree ttargs = make_tree_vec (depth + 1);
28014 for (int i = 0; i < depth; ++i)
28015 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28016 TREE_VEC_ELT (ttargs, depth)
28017 = template_parms_level_to_args (ttparms);
28018 // Substitute ttargs into ttparms to fix references to
28019 // other template parameters.
28020 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28021 complain|tf_partial);
28022 // Now substitute again with args based on tparms, to reduce
28023 // the level of the ttparms.
28024 ttargs = current_template_args ();
28025 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28026 complain);
28027 // Finally, tack the adjusted parms onto tparms.
28028 ttparms = tree_cons (size_int (depth), ttparms,
28029 current_template_parms);
28030 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28031 }
28032 }
28033 else
28034 {
28035 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28036 tree newconst
28037 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28038 TREE_CODE (oldconst),
28039 DECL_NAME (oldconst), newtype);
28040 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28041 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28042 SET_DECL_TEMPLATE_PARM_P (newconst);
28043 newidx = build_template_parm_index (index, level, level,
28044 newconst, newtype);
28045 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28046 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28047 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28048 }
28049
28050 return newdecl;
28051 }
28052
28053 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28054 template parameter. */
28055
28056 static tree
28057 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28058 tree targs, unsigned targs_index, tsubst_flags_t complain)
28059 {
28060 tree olddecl = TREE_VALUE (oldelt);
28061 tree newdecl = rewrite_template_parm (olddecl, index, level,
28062 targs, complain);
28063 if (newdecl == error_mark_node)
28064 return error_mark_node;
28065 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28066 targs, complain, NULL_TREE);
28067 tree list = build_tree_list (newdef, newdecl);
28068 TEMPLATE_PARM_CONSTRAINTS (list)
28069 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28070 targs, complain, NULL_TREE);
28071 int depth = TMPL_ARGS_DEPTH (targs);
28072 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28073 return list;
28074 }
28075
28076 /* Returns a C++17 class deduction guide template based on the constructor
28077 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28078 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28079 aggregate initialization guide. */
28080
28081 static tree
28082 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28083 {
28084 tree tparms, targs, fparms, fargs, ci;
28085 bool memtmpl = false;
28086 bool explicit_p;
28087 location_t loc;
28088 tree fn_tmpl = NULL_TREE;
28089
28090 if (outer_args)
28091 {
28092 ++processing_template_decl;
28093 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28094 --processing_template_decl;
28095 }
28096
28097 if (!DECL_DECLARES_FUNCTION_P (ctor))
28098 {
28099 if (TYPE_P (ctor))
28100 {
28101 bool copy_p = TYPE_REF_P (ctor);
28102 if (copy_p)
28103 fparms = tree_cons (NULL_TREE, type, void_list_node);
28104 else
28105 fparms = void_list_node;
28106 }
28107 else if (TREE_CODE (ctor) == TREE_LIST)
28108 fparms = ctor;
28109 else
28110 gcc_unreachable ();
28111
28112 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28113 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28114 targs = CLASSTYPE_TI_ARGS (type);
28115 ci = NULL_TREE;
28116 fargs = NULL_TREE;
28117 loc = DECL_SOURCE_LOCATION (ctmpl);
28118 explicit_p = false;
28119 }
28120 else
28121 {
28122 ++processing_template_decl;
28123 bool ok = true;
28124
28125 fn_tmpl
28126 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28127 : DECL_TI_TEMPLATE (ctor));
28128 if (outer_args)
28129 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28130 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28131
28132 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28133 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28134 fully specialized args for the enclosing class. Strip those off, as
28135 the deduction guide won't have those template parameters. */
28136 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28137 TMPL_PARMS_DEPTH (tparms));
28138 /* Discard the 'this' parameter. */
28139 fparms = FUNCTION_ARG_CHAIN (ctor);
28140 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28141 ci = get_constraints (ctor);
28142 loc = DECL_SOURCE_LOCATION (ctor);
28143 explicit_p = DECL_NONCONVERTING_P (ctor);
28144
28145 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28146 {
28147 memtmpl = true;
28148
28149 /* For a member template constructor, we need to flatten the two
28150 template parameter lists into one, and then adjust the function
28151 signature accordingly. This gets...complicated. */
28152 tree save_parms = current_template_parms;
28153
28154 /* For a member template we should have two levels of parms/args, one
28155 for the class and one for the constructor. We stripped
28156 specialized args for further enclosing classes above. */
28157 const int depth = 2;
28158 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28159
28160 /* Template args for translating references to the two-level template
28161 parameters into references to the one-level template parameters we
28162 are creating. */
28163 tree tsubst_args = copy_node (targs);
28164 TMPL_ARGS_LEVEL (tsubst_args, depth)
28165 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28166
28167 /* Template parms for the constructor template. */
28168 tree ftparms = TREE_VALUE (tparms);
28169 unsigned flen = TREE_VEC_LENGTH (ftparms);
28170 /* Template parms for the class template. */
28171 tparms = TREE_CHAIN (tparms);
28172 tree ctparms = TREE_VALUE (tparms);
28173 unsigned clen = TREE_VEC_LENGTH (ctparms);
28174 /* Template parms for the deduction guide start as a copy of the
28175 template parms for the class. We set current_template_parms for
28176 lookup_template_class_1. */
28177 current_template_parms = tparms = copy_node (tparms);
28178 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28179 for (unsigned i = 0; i < clen; ++i)
28180 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28181
28182 /* Now we need to rewrite the constructor parms to append them to the
28183 class parms. */
28184 for (unsigned i = 0; i < flen; ++i)
28185 {
28186 unsigned index = i + clen;
28187 unsigned level = 1;
28188 tree oldelt = TREE_VEC_ELT (ftparms, i);
28189 tree newelt
28190 = rewrite_tparm_list (oldelt, index, level,
28191 tsubst_args, i, complain);
28192 if (newelt == error_mark_node)
28193 ok = false;
28194 TREE_VEC_ELT (new_vec, index) = newelt;
28195 }
28196
28197 /* Now we have a final set of template parms to substitute into the
28198 function signature. */
28199 targs = template_parms_to_args (tparms);
28200 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28201 complain, ctor);
28202 if (fparms == error_mark_node)
28203 ok = false;
28204 if (ci)
28205 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28206
28207 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28208 cp_unevaluated_operand. */
28209 cp_evaluated ev;
28210 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28211 current_template_parms = save_parms;
28212 }
28213
28214 --processing_template_decl;
28215 if (!ok)
28216 return error_mark_node;
28217 }
28218
28219 if (!memtmpl)
28220 {
28221 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28222 tparms = copy_node (tparms);
28223 INNERMOST_TEMPLATE_PARMS (tparms)
28224 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28225 }
28226
28227 tree fntype = build_function_type (type, fparms);
28228 tree ded_fn = build_lang_decl_loc (loc,
28229 FUNCTION_DECL,
28230 dguide_name (type), fntype);
28231 DECL_ARGUMENTS (ded_fn) = fargs;
28232 DECL_ARTIFICIAL (ded_fn) = true;
28233 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28234 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28235 DECL_ARTIFICIAL (ded_tmpl) = true;
28236 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28237 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28238 if (DECL_P (ctor))
28239 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28240 if (ci)
28241 set_constraints (ded_tmpl, ci);
28242
28243 return ded_tmpl;
28244 }
28245
28246 /* Add to LIST the member types for the reshaped initializer CTOR. */
28247
28248 static tree
28249 collect_ctor_idx_types (tree ctor, tree list)
28250 {
28251 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28252 tree idx, val; unsigned i;
28253 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28254 {
28255 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28256 && CONSTRUCTOR_NELTS (val))
28257 if (tree subidx = CONSTRUCTOR_ELT (val, 0)->index)
28258 if (TREE_CODE (subidx) == FIELD_DECL)
28259 {
28260 list = collect_ctor_idx_types (val, list);
28261 continue;
28262 }
28263 tree ftype = finish_decltype_type (idx, true, tf_none);
28264 list = tree_cons (NULL_TREE, ftype, list);
28265 }
28266
28267 return list;
28268 }
28269
28270 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28271
28272 static bool
28273 is_spec_or_derived (tree etype, tree tmpl)
28274 {
28275 if (!etype || !CLASS_TYPE_P (etype))
28276 return false;
28277
28278 tree type = TREE_TYPE (tmpl);
28279 tree tparms = (INNERMOST_TEMPLATE_PARMS
28280 (DECL_TEMPLATE_PARMS (tmpl)));
28281 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28282 int err = unify (tparms, targs, type, etype,
28283 UNIFY_ALLOW_DERIVED, /*explain*/false);
28284 ggc_free (targs);
28285 return !err;
28286 }
28287
28288 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28289 INIT. */
28290
28291 static tree
28292 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28293 {
28294 if (cxx_dialect < cxx20)
28295 return NULL_TREE;
28296
28297 if (init == NULL_TREE)
28298 return NULL_TREE;
28299
28300 tree type = TREE_TYPE (tmpl);
28301 if (!CP_AGGREGATE_TYPE_P (type))
28302 return NULL_TREE;
28303
28304 /* No aggregate candidate for copy-initialization. */
28305 if (args->length() == 1)
28306 {
28307 tree val = (*args)[0];
28308 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28309 return NULL_TREE;
28310 }
28311
28312 /* If we encounter a problem, we just won't add the candidate. */
28313 tsubst_flags_t complain = tf_none;
28314
28315 tree parms = NULL_TREE;
28316 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28317 {
28318 init = reshape_init (type, init, complain);
28319 if (init == error_mark_node)
28320 return NULL_TREE;
28321 parms = collect_ctor_idx_types (init, parms);
28322 }
28323 else if (TREE_CODE (init) == TREE_LIST)
28324 {
28325 int len = list_length (init);
28326 for (tree field = TYPE_FIELDS (type);
28327 len;
28328 --len, field = DECL_CHAIN (field))
28329 {
28330 field = next_initializable_field (field);
28331 if (!field)
28332 return NULL_TREE;
28333 tree ftype = finish_decltype_type (field, true, complain);
28334 parms = tree_cons (NULL_TREE, ftype, parms);
28335 }
28336 }
28337 else
28338 /* Aggregate initialization doesn't apply to an initializer expression. */
28339 return NULL_TREE;
28340
28341 if (parms)
28342 {
28343 tree last = parms;
28344 parms = nreverse (parms);
28345 TREE_CHAIN (last) = void_list_node;
28346 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28347 return guide;
28348 }
28349
28350 return NULL_TREE;
28351 }
28352
28353 /* UGUIDES are the deduction guides for the underlying template of alias
28354 template TMPL; adjust them to be deduction guides for TMPL. */
28355
28356 static tree
28357 alias_ctad_tweaks (tree tmpl, tree uguides)
28358 {
28359 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28360 class type (9.2.8.2) where the template-name names an alias template A,
28361 the defining-type-id of A must be of the form
28362
28363 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28364
28365 as specified in 9.2.8.2. The guides of A are the set of functions or
28366 function templates formed as follows. For each function or function
28367 template f in the guides of the template named by the simple-template-id
28368 of the defining-type-id, the template arguments of the return type of f
28369 are deduced from the defining-type-id of A according to the process in
28370 13.10.2.5 with the exception that deduction does not fail if not all
28371 template arguments are deduced. Let g denote the result of substituting
28372 these deductions into f. If substitution succeeds, form a function or
28373 function template f' with the following properties and add it to the set
28374 of guides of A:
28375
28376 * The function type of f' is the function type of g.
28377
28378 * If f is a function template, f' is a function template whose template
28379 parameter list consists of all the template parameters of A (including
28380 their default template arguments) that appear in the above deductions or
28381 (recursively) in their default template arguments, followed by the
28382 template parameters of f that were not deduced (including their default
28383 template arguments), otherwise f' is not a function template.
28384
28385 * The associated constraints (13.5.2) are the conjunction of the
28386 associated constraints of g and a constraint that is satisfied if and only
28387 if the arguments of A are deducible (see below) from the return type.
28388
28389 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28390 be so as well.
28391
28392 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28393 considered to be so as well.
28394
28395 * The explicit-specifier of f' is the explicit-specifier of g (if
28396 any). */
28397
28398 /* This implementation differs from the above in two significant ways:
28399
28400 1) We include all template parameters of A, not just some.
28401 2) The added constraint is same_type instead of deducible.
28402
28403 I believe that while it's probably possible to construct a testcase that
28404 behaves differently with this simplification, it should have the same
28405 effect for real uses. Including all template parameters means that we
28406 deduce all parameters of A when resolving the call, so when we're in the
28407 constraint we don't need to deduce them again, we can just check whether
28408 the deduction produced the desired result. */
28409
28410 tsubst_flags_t complain = tf_warning_or_error;
28411 tree atype = TREE_TYPE (tmpl);
28412 tree aguides = NULL_TREE;
28413 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28414 unsigned natparms = TREE_VEC_LENGTH (atparms);
28415 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28416 for (ovl_iterator iter (uguides); iter; ++iter)
28417 {
28418 tree f = *iter;
28419 tree in_decl = f;
28420 location_t loc = DECL_SOURCE_LOCATION (f);
28421 tree ret = TREE_TYPE (TREE_TYPE (f));
28422 tree fprime = f;
28423 if (TREE_CODE (f) == TEMPLATE_DECL)
28424 {
28425 processing_template_decl_sentinel ptds (/*reset*/false);
28426 ++processing_template_decl;
28427
28428 /* Deduce template arguments for f from the type-id of A. */
28429 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28430 unsigned len = TREE_VEC_LENGTH (ftparms);
28431 tree targs = make_tree_vec (len);
28432 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28433 gcc_assert (!err);
28434
28435 /* The number of parms for f' is the number of parms for A plus
28436 non-deduced parms of f. */
28437 unsigned ndlen = 0;
28438 unsigned j;
28439 for (unsigned i = 0; i < len; ++i)
28440 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28441 ++ndlen;
28442 tree gtparms = make_tree_vec (natparms + ndlen);
28443
28444 /* First copy over the parms of A. */
28445 for (j = 0; j < natparms; ++j)
28446 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28447 /* Now rewrite the non-deduced parms of f. */
28448 for (unsigned i = 0; ndlen && i < len; ++i)
28449 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28450 {
28451 --ndlen;
28452 unsigned index = j++;
28453 unsigned level = 1;
28454 tree oldlist = TREE_VEC_ELT (ftparms, i);
28455 tree list = rewrite_tparm_list (oldlist, index, level,
28456 targs, i, complain);
28457 TREE_VEC_ELT (gtparms, index) = list;
28458 }
28459 gtparms = build_tree_list (size_one_node, gtparms);
28460
28461 /* Substitute the deduced arguments plus the rewritten template
28462 parameters into f to get g. This covers the type, copyness,
28463 guideness, and explicit-specifier. */
28464 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28465 if (g == error_mark_node)
28466 return error_mark_node;
28467 DECL_USE_TEMPLATE (g) = 0;
28468 fprime = build_template_decl (g, gtparms, false);
28469 DECL_TEMPLATE_RESULT (fprime) = g;
28470 TREE_TYPE (fprime) = TREE_TYPE (g);
28471 tree gtargs = template_parms_to_args (gtparms);
28472 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28473 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28474
28475 /* Substitute the associated constraints. */
28476 tree ci = get_constraints (f);
28477 if (ci)
28478 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28479 if (ci == error_mark_node)
28480 return error_mark_node;
28481
28482 /* Add a constraint that the return type matches the instantiation of
28483 A with the same template arguments. */
28484 ret = TREE_TYPE (TREE_TYPE (fprime));
28485 if (!same_type_p (atype, ret)
28486 /* FIXME this should mean they don't compare as equivalent. */
28487 || dependent_alias_template_spec_p (atype, nt_opaque))
28488 {
28489 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28490 ci = append_constraint (ci, same);
28491 }
28492
28493 if (ci)
28494 set_constraints (fprime, ci);
28495 }
28496 else
28497 {
28498 /* For a non-template deduction guide, if the arguments of A aren't
28499 deducible from the return type, don't add the candidate. */
28500 tree targs = make_tree_vec (natparms);
28501 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28502 for (unsigned i = 0; !err && i < natparms; ++i)
28503 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28504 err = true;
28505 if (err)
28506 continue;
28507 }
28508
28509 aguides = lookup_add (fprime, aguides);
28510 }
28511
28512 return aguides;
28513 }
28514
28515 /* Return artificial deduction guides built from the constructors of class
28516 template TMPL. */
28517
28518 static tree
28519 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28520 {
28521 tree type = TREE_TYPE (tmpl);
28522 tree outer_args = NULL_TREE;
28523 if (DECL_CLASS_SCOPE_P (tmpl)
28524 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28525 {
28526 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28527 type = TREE_TYPE (most_general_template (tmpl));
28528 }
28529
28530 tree cands = NULL_TREE;
28531
28532 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28533 {
28534 /* Skip inherited constructors. */
28535 if (iter.using_p ())
28536 continue;
28537
28538 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28539 cands = lookup_add (guide, cands);
28540 }
28541
28542 /* Add implicit default constructor deduction guide. */
28543 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28544 {
28545 tree guide = build_deduction_guide (type, type, outer_args,
28546 complain);
28547 cands = lookup_add (guide, cands);
28548 }
28549
28550 /* Add copy guide. */
28551 {
28552 tree gtype = build_reference_type (type);
28553 tree guide = build_deduction_guide (type, gtype, outer_args,
28554 complain);
28555 cands = lookup_add (guide, cands);
28556 }
28557
28558 return cands;
28559 }
28560
28561 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28562
28563 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28564 aggregate candidate is added separately because it depends on the
28565 initializer. */
28566
28567 static tree
28568 deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28569 {
28570 tree guides = NULL_TREE;
28571 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28572 {
28573 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28574 tree tinfo = get_template_info (under);
28575 guides = deduction_guides_for (TI_TEMPLATE (tinfo), complain);
28576 }
28577 else
28578 {
28579 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28580 dguide_name (tmpl),
28581 /*type*/false, /*complain*/false,
28582 /*hidden*/false);
28583 if (guides == error_mark_node)
28584 guides = NULL_TREE;
28585 }
28586
28587 /* Cache the deduction guides for a template. We also remember the result of
28588 lookup, and rebuild everything if it changes; should be very rare. */
28589 tree_pair_p cache = NULL;
28590 if (tree_pair_p &r
28591 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28592 {
28593 cache = r;
28594 if (cache->purpose == guides)
28595 return cache->value;
28596 }
28597 else
28598 {
28599 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28600 cache->purpose = guides;
28601 }
28602
28603 tree cands = NULL_TREE;
28604 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28605 cands = alias_ctad_tweaks (tmpl, guides);
28606 else
28607 {
28608 cands = ctor_deduction_guides_for (tmpl, complain);
28609 for (ovl_iterator it (guides); it; ++it)
28610 cands = lookup_add (*it, cands);
28611 }
28612
28613 cache->value = cands;
28614 return cands;
28615 }
28616
28617 /* Return whether TMPL is a (class template argument-) deducible template. */
28618
28619 bool
28620 ctad_template_p (tree tmpl)
28621 {
28622 /* A deducible template is either a class template or is an alias template
28623 whose defining-type-id is of the form
28624
28625 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28626
28627 where the nested-name-specifier (if any) is non-dependent and the
28628 template-name of the simple-template-id names a deducible template. */
28629
28630 if (DECL_CLASS_TEMPLATE_P (tmpl)
28631 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28632 return true;
28633 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28634 return false;
28635 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28636 if (tree tinfo = get_template_info (orig))
28637 return ctad_template_p (TI_TEMPLATE (tinfo));
28638 return false;
28639 }
28640
28641 /* Deduce template arguments for the class template placeholder PTYPE for
28642 template TMPL based on the initializer INIT, and return the resulting
28643 type. */
28644
28645 static tree
28646 do_class_deduction (tree ptype, tree tmpl, tree init,
28647 int flags, tsubst_flags_t complain)
28648 {
28649 /* We should have handled this in the caller. */
28650 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28651 return ptype;
28652
28653 /* Look through alias templates that just rename another template. */
28654 tmpl = get_underlying_template (tmpl);
28655 if (!ctad_template_p (tmpl))
28656 {
28657 if (complain & tf_error)
28658 error ("non-deducible template %qT used without template arguments", tmpl);
28659 return error_mark_node;
28660 }
28661 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
28662 {
28663 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28664 if (complain & tf_warning_or_error)
28665 pedwarn (input_location, 0, "alias template deduction only available "
28666 "with %<-std=c++20%> or %<-std=gnu++20%>");
28667 }
28668
28669 if (init && TREE_TYPE (init) == ptype)
28670 /* Using the template parm as its own argument. */
28671 return ptype;
28672
28673 tree type = TREE_TYPE (tmpl);
28674
28675 bool try_list_ctor = false;
28676
28677 releasing_vec rv_args = NULL;
28678 vec<tree,va_gc> *&args = *&rv_args;
28679 if (init == NULL_TREE)
28680 args = make_tree_vector ();
28681 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28682 {
28683 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28684 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28685 {
28686 /* As an exception, the first phase in 16.3.1.7 (considering the
28687 initializer list as a single argument) is omitted if the
28688 initializer list consists of a single expression of type cv U,
28689 where U is a specialization of C or a class derived from a
28690 specialization of C. */
28691 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28692 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28693 try_list_ctor = false;
28694 }
28695 if (try_list_ctor || is_std_init_list (type))
28696 args = make_tree_vector_single (init);
28697 else
28698 args = make_tree_vector_from_ctor (init);
28699 }
28700 else if (TREE_CODE (init) == TREE_LIST)
28701 args = make_tree_vector_from_list (init);
28702 else
28703 args = make_tree_vector_single (init);
28704
28705 /* Do this now to avoid problems with erroneous args later on. */
28706 args = resolve_args (args, complain);
28707 if (args == NULL)
28708 return error_mark_node;
28709
28710 tree cands = deduction_guides_for (tmpl, complain);
28711 if (cands == error_mark_node)
28712 return error_mark_node;
28713
28714 /* Prune explicit deduction guides in copy-initialization context. */
28715 bool elided = false;
28716 if (flags & LOOKUP_ONLYCONVERTING)
28717 {
28718 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28719 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28720 elided = true;
28721
28722 if (elided)
28723 {
28724 /* Found a nonconverting guide, prune the candidates. */
28725 tree pruned = NULL_TREE;
28726 for (lkp_iterator iter (cands); iter; ++iter)
28727 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28728 pruned = lookup_add (*iter, pruned);
28729
28730 cands = pruned;
28731 }
28732 }
28733
28734 if (tree guide = maybe_aggr_guide (tmpl, init, args))
28735 cands = lookup_add (guide, cands);
28736
28737 tree call = error_mark_node;
28738
28739 /* If this is list-initialization and the class has a list constructor, first
28740 try deducing from the list as a single argument, as [over.match.list]. */
28741 tree list_cands = NULL_TREE;
28742 if (try_list_ctor && cands)
28743 for (lkp_iterator iter (cands); iter; ++iter)
28744 {
28745 tree dg = *iter;
28746 if (is_list_ctor (dg))
28747 list_cands = lookup_add (dg, list_cands);
28748 }
28749 if (list_cands)
28750 {
28751 ++cp_unevaluated_operand;
28752 call = build_new_function_call (list_cands, &args, tf_decltype);
28753 --cp_unevaluated_operand;
28754
28755 if (call == error_mark_node)
28756 {
28757 /* That didn't work, now try treating the list as a sequence of
28758 arguments. */
28759 release_tree_vector (args);
28760 args = make_tree_vector_from_ctor (init);
28761 }
28762 }
28763
28764 if (elided && !cands)
28765 {
28766 error ("cannot deduce template arguments for copy-initialization"
28767 " of %qT, as it has no non-explicit deduction guides or "
28768 "user-declared constructors", type);
28769 return error_mark_node;
28770 }
28771 else if (!cands && call == error_mark_node)
28772 {
28773 error ("cannot deduce template arguments of %qT, as it has no viable "
28774 "deduction guides", type);
28775 return error_mark_node;
28776 }
28777
28778 if (call == error_mark_node)
28779 {
28780 ++cp_unevaluated_operand;
28781 call = build_new_function_call (cands, &args, tf_decltype);
28782 --cp_unevaluated_operand;
28783 }
28784
28785 if (call == error_mark_node
28786 && (complain & tf_warning_or_error))
28787 {
28788 error ("class template argument deduction failed:");
28789
28790 ++cp_unevaluated_operand;
28791 call = build_new_function_call (cands, &args, complain | tf_decltype);
28792 --cp_unevaluated_operand;
28793
28794 if (elided)
28795 inform (input_location, "explicit deduction guides not considered "
28796 "for copy-initialization");
28797 }
28798
28799 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
28800 }
28801
28802 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28803 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28804 The CONTEXT determines the context in which auto deduction is performed
28805 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28806 OUTER_TARGS are used during template argument deduction
28807 (context == adc_unify) to properly substitute the result, and is ignored
28808 in other contexts.
28809
28810 For partial-concept-ids, extra args may be appended to the list of deduced
28811 template arguments prior to determining constraint satisfaction. */
28812
28813 tree
28814 do_auto_deduction (tree type, tree init, tree auto_node,
28815 tsubst_flags_t complain, auto_deduction_context context,
28816 tree outer_targs, int flags)
28817 {
28818 tree targs;
28819
28820 if (init == error_mark_node)
28821 return error_mark_node;
28822
28823 if (init && type_dependent_expression_p (init)
28824 && context != adc_unify)
28825 /* Defining a subset of type-dependent expressions that we can deduce
28826 from ahead of time isn't worth the trouble. */
28827 return type;
28828
28829 /* Similarly, we can't deduce from another undeduced decl. */
28830 if (init && undeduced_auto_decl (init))
28831 return type;
28832
28833 /* We may be doing a partial substitution, but we still want to replace
28834 auto_node. */
28835 complain &= ~tf_partial;
28836
28837 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
28838 /* C++17 class template argument deduction. */
28839 return do_class_deduction (type, tmpl, init, flags, complain);
28840
28841 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
28842 /* Nothing we can do with this, even in deduction context. */
28843 return type;
28844
28845 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28846 with either a new invented type template parameter U or, if the
28847 initializer is a braced-init-list (8.5.4), with
28848 std::initializer_list<U>. */
28849 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28850 {
28851 if (!DIRECT_LIST_INIT_P (init))
28852 type = listify_autos (type, auto_node);
28853 else if (CONSTRUCTOR_NELTS (init) == 1)
28854 init = CONSTRUCTOR_ELT (init, 0)->value;
28855 else
28856 {
28857 if (complain & tf_warning_or_error)
28858 {
28859 if (permerror (input_location, "direct-list-initialization of "
28860 "%<auto%> requires exactly one element"))
28861 inform (input_location,
28862 "for deduction to %<std::initializer_list%>, use copy-"
28863 "list-initialization (i.e. add %<=%> before the %<{%>)");
28864 }
28865 type = listify_autos (type, auto_node);
28866 }
28867 }
28868
28869 if (type == error_mark_node)
28870 return error_mark_node;
28871
28872 init = resolve_nondeduced_context (init, complain);
28873
28874 if (context == adc_decomp_type
28875 && auto_node == type
28876 && init != error_mark_node
28877 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
28878 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28879 and initializer has array type, deduce cv-qualified array type. */
28880 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
28881 complain);
28882 else if (AUTO_IS_DECLTYPE (auto_node))
28883 {
28884 tree stripped_init = tree_strip_any_location_wrapper (init);
28885 bool id = (DECL_P (stripped_init)
28886 || ((TREE_CODE (init) == COMPONENT_REF
28887 || TREE_CODE (init) == SCOPE_REF)
28888 && !REF_PARENTHESIZED_P (init)));
28889 targs = make_tree_vec (1);
28890 TREE_VEC_ELT (targs, 0)
28891 = finish_decltype_type (init, id, tf_warning_or_error);
28892 if (type != auto_node)
28893 {
28894 if (complain & tf_error)
28895 error ("%qT as type rather than plain %<decltype(auto)%>", type);
28896 return error_mark_node;
28897 }
28898 }
28899 else
28900 {
28901 if (error_operand_p (init))
28902 return error_mark_node;
28903
28904 tree parms = build_tree_list (NULL_TREE, type);
28905 tree tparms;
28906
28907 if (flag_concepts)
28908 tparms = extract_autos (type);
28909 else
28910 {
28911 tparms = make_tree_vec (1);
28912 TREE_VEC_ELT (tparms, 0)
28913 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
28914 }
28915
28916 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28917 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
28918 DEDUCE_CALL,
28919 NULL, /*explain_p=*/false);
28920 if (val > 0)
28921 {
28922 if (processing_template_decl)
28923 /* Try again at instantiation time. */
28924 return type;
28925 if (type && type != error_mark_node
28926 && (complain & tf_error))
28927 /* If type is error_mark_node a diagnostic must have been
28928 emitted by now. Also, having a mention to '<type error>'
28929 in the diagnostic is not really useful to the user. */
28930 {
28931 if (cfun
28932 && FNDECL_USED_AUTO (current_function_decl)
28933 && (auto_node
28934 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
28935 && LAMBDA_FUNCTION_P (current_function_decl))
28936 error ("unable to deduce lambda return type from %qE", init);
28937 else
28938 error ("unable to deduce %qT from %qE", type, init);
28939 type_unification_real (tparms, targs, parms, &init, 1, 0,
28940 DEDUCE_CALL,
28941 NULL, /*explain_p=*/true);
28942 }
28943 return error_mark_node;
28944 }
28945 }
28946
28947 /* Check any placeholder constraints against the deduced type. */
28948 if (flag_concepts && !processing_template_decl)
28949 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
28950 {
28951 /* Use the deduced type to check the associated constraints. If we
28952 have a partial-concept-id, rebuild the argument list so that
28953 we check using the extra arguments. */
28954 check = unpack_concept_check (check);
28955 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
28956 tree cdecl = TREE_OPERAND (check, 0);
28957 if (OVL_P (cdecl))
28958 cdecl = OVL_FIRST (cdecl);
28959 tree cargs = TREE_OPERAND (check, 1);
28960 if (TREE_VEC_LENGTH (cargs) > 1)
28961 {
28962 cargs = copy_node (cargs);
28963 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
28964 }
28965 else
28966 cargs = targs;
28967
28968 /* Rebuild the check using the deduced arguments. */
28969 check = build_concept_check (cdecl, cargs, tf_none);
28970
28971 if (!constraints_satisfied_p (check))
28972 {
28973 if (complain & tf_warning_or_error)
28974 {
28975 auto_diagnostic_group d;
28976 switch (context)
28977 {
28978 case adc_unspecified:
28979 case adc_unify:
28980 error("placeholder constraints not satisfied");
28981 break;
28982 case adc_variable_type:
28983 case adc_decomp_type:
28984 error ("deduced initializer does not satisfy "
28985 "placeholder constraints");
28986 break;
28987 case adc_return_type:
28988 error ("deduced return type does not satisfy "
28989 "placeholder constraints");
28990 break;
28991 case adc_requirement:
28992 error ("deduced expression type does not satisfy "
28993 "placeholder constraints");
28994 break;
28995 }
28996 diagnose_constraints (input_location, check, targs);
28997 }
28998 return error_mark_node;
28999 }
29000 }
29001
29002 if (processing_template_decl && context != adc_unify)
29003 outer_targs = current_template_args ();
29004 targs = add_to_template_args (outer_targs, targs);
29005 return tsubst (type, targs, complain, NULL_TREE);
29006 }
29007
29008 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29009 result. */
29010
29011 tree
29012 splice_late_return_type (tree type, tree late_return_type)
29013 {
29014 if (late_return_type)
29015 {
29016 gcc_assert (is_auto (type) || seen_error ());
29017 return late_return_type;
29018 }
29019
29020 if (tree *auto_node = find_type_usage (&type, is_auto))
29021 {
29022 tree idx = get_template_parm_index (*auto_node);
29023 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29024 {
29025 /* In an abbreviated function template we didn't know we were dealing
29026 with a function template when we saw the auto return type, so update
29027 it to have the correct level. */
29028 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29029 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29030 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29031 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29032 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29033 *auto_node = new_auto;
29034 }
29035 }
29036 return type;
29037 }
29038
29039 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29040 'decltype(auto)' or a deduced class template. */
29041
29042 bool
29043 is_auto (const_tree type)
29044 {
29045 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29046 && (TYPE_IDENTIFIER (type) == auto_identifier
29047 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29048 return true;
29049 else
29050 return false;
29051 }
29052
29053 /* for_each_template_parm callback for type_uses_auto. */
29054
29055 int
29056 is_auto_r (tree tp, void */*data*/)
29057 {
29058 return is_auto (tp);
29059 }
29060
29061 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29062 a use of `auto'. Returns NULL_TREE otherwise. */
29063
29064 tree
29065 type_uses_auto (tree type)
29066 {
29067 if (type == NULL_TREE)
29068 return NULL_TREE;
29069 else if (flag_concepts)
29070 {
29071 /* The Concepts TS allows multiple autos in one type-specifier; just
29072 return the first one we find, do_auto_deduction will collect all of
29073 them. */
29074 if (uses_template_parms (type))
29075 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29076 /*visited*/NULL, /*nondeduced*/false);
29077 else
29078 return NULL_TREE;
29079 }
29080 else if (tree *tp = find_type_usage (&type, is_auto))
29081 return *tp;
29082 else
29083 return NULL_TREE;
29084 }
29085
29086 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29087 concepts are enabled, auto is acceptable in template arguments, but
29088 only when TEMPL identifies a template class. Return TRUE if any
29089 such errors were reported. */
29090
29091 bool
29092 check_auto_in_tmpl_args (tree tmpl, tree args)
29093 {
29094 /* If there were previous errors, nevermind. */
29095 if (!args || TREE_CODE (args) != TREE_VEC)
29096 return false;
29097
29098 /* If TMPL is an identifier, we're parsing and we can't tell yet
29099 whether TMPL is supposed to be a type, a function or a variable.
29100 We'll only be able to tell during template substitution, so we
29101 expect to be called again then. If concepts are enabled and we
29102 know we have a type, we're ok. */
29103 if (flag_concepts
29104 && (identifier_p (tmpl)
29105 || (DECL_P (tmpl)
29106 && (DECL_TYPE_TEMPLATE_P (tmpl)
29107 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29108 return false;
29109
29110 /* Quickly search for any occurrences of auto; usually there won't
29111 be any, and then we'll avoid allocating the vector. */
29112 if (!type_uses_auto (args))
29113 return false;
29114
29115 bool errors = false;
29116
29117 tree vec = extract_autos (args);
29118 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29119 {
29120 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29121 error_at (DECL_SOURCE_LOCATION (xauto),
29122 "invalid use of %qT in template argument", xauto);
29123 errors = true;
29124 }
29125
29126 return errors;
29127 }
29128
29129 /* For a given template T, return the vector of typedefs referenced
29130 in T for which access check is needed at T instantiation time.
29131 T is either a FUNCTION_DECL or a RECORD_TYPE.
29132 Those typedefs were added to T by the function
29133 append_type_to_template_for_access_check. */
29134
29135 vec<qualified_typedef_usage_t, va_gc> *
29136 get_types_needing_access_check (tree t)
29137 {
29138 gcc_checking_assert ((CLASS_TYPE_P (t) || TREE_CODE (t) == FUNCTION_DECL));
29139
29140 if (tree ti = get_template_info (t))
29141 if (TI_TEMPLATE (ti))
29142 return TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
29143
29144 return NULL;
29145 }
29146
29147 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
29148 tied to T. That list of typedefs will be access checked at
29149 T instantiation time.
29150 T is either a FUNCTION_DECL or a RECORD_TYPE.
29151 TYPE_DECL is a TYPE_DECL node representing a typedef.
29152 SCOPE is the scope through which TYPE_DECL is accessed.
29153 LOCATION is the location of the usage point of TYPE_DECL.
29154
29155 This function is a subroutine of
29156 append_type_to_template_for_access_check. */
29157
29158 static void
29159 append_type_to_template_for_access_check_1 (tree t,
29160 tree type_decl,
29161 tree scope,
29162 location_t location)
29163 {
29164 qualified_typedef_usage_t typedef_usage;
29165 tree ti;
29166
29167 if (!t || t == error_mark_node)
29168 return;
29169
29170 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
29171 || CLASS_TYPE_P (t))
29172 && type_decl
29173 && TREE_CODE (type_decl) == TYPE_DECL
29174 && scope);
29175
29176 if (!(ti = get_template_info (t)))
29177 return;
29178
29179 gcc_assert (TI_TEMPLATE (ti));
29180
29181 typedef_usage.typedef_decl = type_decl;
29182 typedef_usage.context = scope;
29183 typedef_usage.locus = location;
29184
29185 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
29186 }
29187
29188 /* Append TYPE_DECL to the template TEMPL.
29189 TEMPL is either a class type, a FUNCTION_DECL or a TEMPLATE_DECL.
29190 At TEMPL instanciation time, TYPE_DECL will be checked to see
29191 if it can be accessed through SCOPE.
29192 LOCATION is the location of the usage point of TYPE_DECL.
29193
29194 e.g. consider the following code snippet:
29195
29196 class C
29197 {
29198 typedef int myint;
29199 };
29200
29201 template<class U> struct S
29202 {
29203 C::myint mi; // <-- usage point of the typedef C::myint
29204 };
29205
29206 S<char> s;
29207
29208 At S<char> instantiation time, we need to check the access of C::myint
29209 In other words, we need to check the access of the myint typedef through
29210 the C scope. For that purpose, this function will add the myint typedef
29211 and the scope C through which its being accessed to a list of typedefs
29212 tied to the template S. That list will be walked at template instantiation
29213 time and access check performed on each typedefs it contains.
29214 Note that this particular code snippet should yield an error because
29215 myint is private to C. */
29216
29217 void
29218 append_type_to_template_for_access_check (tree templ,
29219 tree type_decl,
29220 tree scope,
29221 location_t location)
29222 {
29223 qualified_typedef_usage_t *iter;
29224 unsigned i;
29225
29226 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
29227
29228 /* Make sure we don't append the type to the template twice. */
29229 if (vec<qualified_typedef_usage_t, va_gc> *tdefs
29230 = get_types_needing_access_check (templ))
29231 FOR_EACH_VEC_ELT (*tdefs, i, iter)
29232 if (iter->typedef_decl == type_decl && scope == iter->context)
29233 return;
29234
29235 append_type_to_template_for_access_check_1 (templ, type_decl,
29236 scope, location);
29237 }
29238
29239 /* Recursively walk over && expressions searching for EXPR. Return a reference
29240 to that expression. */
29241
29242 static tree *find_template_requirement (tree *t, tree key)
29243 {
29244 if (*t == key)
29245 return t;
29246 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29247 {
29248 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29249 return p;
29250 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29251 return p;
29252 }
29253 return 0;
29254 }
29255
29256 /* Convert the generic type parameters in PARM that match the types given in the
29257 range [START_IDX, END_IDX) from the current_template_parms into generic type
29258 packs. */
29259
29260 tree
29261 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29262 {
29263 tree current = current_template_parms;
29264 int depth = TMPL_PARMS_DEPTH (current);
29265 current = INNERMOST_TEMPLATE_PARMS (current);
29266 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29267
29268 for (int i = 0; i < start_idx; ++i)
29269 TREE_VEC_ELT (replacement, i)
29270 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29271
29272 for (int i = start_idx; i < end_idx; ++i)
29273 {
29274 /* Create a distinct parameter pack type from the current parm and add it
29275 to the replacement args to tsubst below into the generic function
29276 parameter. */
29277 tree node = TREE_VEC_ELT (current, i);
29278 tree o = TREE_TYPE (TREE_VALUE (node));
29279 tree t = copy_type (o);
29280 TEMPLATE_TYPE_PARM_INDEX (t)
29281 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29282 t, 0, 0, tf_none);
29283 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29284 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29285 TYPE_MAIN_VARIANT (t) = t;
29286 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29287 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29288 TREE_VEC_ELT (replacement, i) = t;
29289
29290 /* Replace the current template parameter with new pack. */
29291 TREE_VALUE (node) = TREE_CHAIN (t);
29292
29293 /* Surgically adjust the associated constraint of adjusted parameter
29294 and it's corresponding contribution to the current template
29295 requirements. */
29296 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29297 {
29298 tree id = unpack_concept_check (constr);
29299 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
29300 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29301 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29302
29303 /* If there was a constraint, we also need to replace that in
29304 the template requirements, which we've already built. */
29305 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29306 reqs = find_template_requirement (reqs, constr);
29307 *reqs = fold;
29308 }
29309 }
29310
29311 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29312 TREE_VEC_ELT (replacement, i)
29313 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29314
29315 /* If there are more levels then build up the replacement with the outer
29316 template parms. */
29317 if (depth > 1)
29318 replacement = add_to_template_args (template_parms_to_args
29319 (TREE_CHAIN (current_template_parms)),
29320 replacement);
29321
29322 return tsubst (parm, replacement, tf_none, NULL_TREE);
29323 }
29324
29325 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29326 0..N-1. */
29327
29328 void
29329 declare_integer_pack (void)
29330 {
29331 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29332 build_function_type_list (integer_type_node,
29333 integer_type_node,
29334 NULL_TREE),
29335 NULL_TREE, ECF_CONST);
29336 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29337 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29338 CP_BUILT_IN_INTEGER_PACK);
29339 }
29340
29341 /* Set up the hash tables for template instantiations. */
29342
29343 void
29344 init_template_processing (void)
29345 {
29346 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29347 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29348
29349 if (cxx_dialect >= cxx11)
29350 declare_integer_pack ();
29351 }
29352
29353 /* Print stats about the template hash tables for -fstats. */
29354
29355 void
29356 print_template_statistics (void)
29357 {
29358 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29359 "%f collisions\n", (long) decl_specializations->size (),
29360 (long) decl_specializations->elements (),
29361 decl_specializations->collisions ());
29362 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29363 "%f collisions\n", (long) type_specializations->size (),
29364 (long) type_specializations->elements (),
29365 type_specializations->collisions ());
29366 }
29367
29368 #if CHECKING_P
29369
29370 namespace selftest {
29371
29372 /* Verify that build_non_dependent_expr () works, for various expressions,
29373 and that location wrappers don't affect the results. */
29374
29375 static void
29376 test_build_non_dependent_expr ()
29377 {
29378 location_t loc = BUILTINS_LOCATION;
29379
29380 /* Verify constants, without and with location wrappers. */
29381 tree int_cst = build_int_cst (integer_type_node, 42);
29382 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29383
29384 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29385 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29386 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29387
29388 tree string_lit = build_string (4, "foo");
29389 TREE_TYPE (string_lit) = char_array_type_node;
29390 string_lit = fix_string_type (string_lit);
29391 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29392
29393 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29394 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29395 ASSERT_EQ (wrapped_string_lit,
29396 build_non_dependent_expr (wrapped_string_lit));
29397 }
29398
29399 /* Verify that type_dependent_expression_p () works correctly, even
29400 in the presence of location wrapper nodes. */
29401
29402 static void
29403 test_type_dependent_expression_p ()
29404 {
29405 location_t loc = BUILTINS_LOCATION;
29406
29407 tree name = get_identifier ("foo");
29408
29409 /* If no templates are involved, nothing is type-dependent. */
29410 gcc_assert (!processing_template_decl);
29411 ASSERT_FALSE (type_dependent_expression_p (name));
29412
29413 ++processing_template_decl;
29414
29415 /* Within a template, an unresolved name is always type-dependent. */
29416 ASSERT_TRUE (type_dependent_expression_p (name));
29417
29418 /* Ensure it copes with NULL_TREE and errors. */
29419 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29420 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29421
29422 /* A USING_DECL in a template should be type-dependent, even if wrapped
29423 with a location wrapper (PR c++/83799). */
29424 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29425 TREE_TYPE (using_decl) = integer_type_node;
29426 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29427 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29428 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29429 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29430
29431 --processing_template_decl;
29432 }
29433
29434 /* Run all of the selftests within this file. */
29435
29436 void
29437 cp_pt_c_tests ()
29438 {
29439 test_build_non_dependent_expr ();
29440 test_type_dependent_expression_p ();
29441 }
29442
29443 } // namespace selftest
29444
29445 #endif /* #if CHECKING_P */
29446
29447 #include "gt-cp-pt.h"
This page took 1.325644 seconds and 6 git commands to generate.