c++: local-scope OMP UDR reductions have no template head
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45 #include "target.h"
46
47 /* The type of functions taking a tree, and some additional data, and
48 returning an int. */
49 typedef int (*tree_fn_t) (tree, void*);
50
51 /* The PENDING_TEMPLATES is a list of templates whose instantiations
52 have been deferred, either because their definitions were not yet
53 available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
55 {
56 struct pending_template *next;
57 struct tinst_level *tinst;
58 };
59
60 static GTY(()) struct pending_template *pending_templates;
61 static GTY(()) struct pending_template *last_pending_template;
62
63 int processing_template_parmlist;
64 static int template_header_count;
65
66 static GTY(()) tree saved_trees;
67 static vec<int> inline_parm_levels;
68
69 static GTY(()) struct tinst_level *current_tinst_level;
70
71 static GTY(()) vec<tree, va_gc> *saved_access_scope;
72
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
77
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
80 //
81 // Implementation of the RAII helper for creating new local
82 // specializations.
83 local_specialization_stack::local_specialization_stack (lss_policy policy)
84 : saved (local_specializations)
85 {
86 if (policy == lss_nop)
87 ;
88 else if (policy == lss_blank || !saved)
89 local_specializations = new hash_map<tree, tree>;
90 else
91 local_specializations = new hash_map<tree, tree>(*saved);
92 }
93
94 local_specialization_stack::~local_specialization_stack ()
95 {
96 if (local_specializations != saved)
97 {
98 delete local_specializations;
99 local_specializations = saved;
100 }
101 }
102
103 /* True if we've recursed into fn_type_unification too many times. */
104 static bool excessive_deduction_depth;
105
106 struct GTY((for_user)) spec_entry
107 {
108 tree tmpl;
109 tree args;
110 tree spec;
111 };
112
113 struct spec_hasher : ggc_ptr_hash<spec_entry>
114 {
115 static hashval_t hash (spec_entry *);
116 static bool equal (spec_entry *, spec_entry *);
117 };
118
119 /* The general template is not in these tables. */
120 typedef hash_table<spec_hasher> spec_hash_table;
121 static GTY (()) spec_hash_table *decl_specializations;
122 static GTY (()) spec_hash_table *type_specializations;
123
124 /* Contains canonical template parameter types. The vector is indexed by
125 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
126 TREE_LIST, whose TREE_VALUEs contain the canonical template
127 parameters of various types and levels. */
128 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
129
130 #define UNIFY_ALLOW_NONE 0
131 #define UNIFY_ALLOW_MORE_CV_QUAL 1
132 #define UNIFY_ALLOW_LESS_CV_QUAL 2
133 #define UNIFY_ALLOW_DERIVED 4
134 #define UNIFY_ALLOW_INTEGER 8
135 #define UNIFY_ALLOW_OUTER_LEVEL 16
136 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
137 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
138
139 enum template_base_result {
140 tbr_incomplete_type,
141 tbr_ambiguous_baseclass,
142 tbr_success
143 };
144
145 static bool resolve_overloaded_unification (tree, tree, tree, tree,
146 unification_kind_t, int,
147 bool);
148 static int try_one_overload (tree, tree, tree, tree, tree,
149 unification_kind_t, int, bool, bool);
150 static int unify (tree, tree, tree, tree, int, bool);
151 static void add_pending_template (tree);
152 static tree reopen_tinst_level (struct tinst_level *);
153 static tree tsubst_initializer_list (tree, tree);
154 static tree get_partial_spec_bindings (tree, tree, tree);
155 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
156 bool, bool);
157 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
158 bool, bool);
159 static void tsubst_enum (tree, tree, tree);
160 static tree add_to_template_args (tree, tree);
161 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
162 static int check_non_deducible_conversion (tree, tree, int, int,
163 struct conversion **, bool);
164 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
165 tree);
166 static int type_unification_real (tree, tree, tree, const tree *,
167 unsigned int, int, unification_kind_t,
168 vec<deferred_access_check, va_gc> **,
169 bool);
170 static void note_template_header (int);
171 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
172 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
173 static tree convert_template_argument (tree, tree, tree,
174 tsubst_flags_t, int, tree);
175 static tree for_each_template_parm (tree, tree_fn_t, void*,
176 hash_set<tree> *, bool, tree_fn_t = NULL);
177 static tree expand_template_argument_pack (tree);
178 static tree build_template_parm_index (int, int, int, tree, tree);
179 static bool inline_needs_template_parms (tree, bool);
180 static void push_inline_template_parms_recursive (tree, int);
181 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
182 static int mark_template_parm (tree, void *);
183 static int template_parm_this_level_p (tree, void *);
184 static tree tsubst_friend_function (tree, tree);
185 static tree tsubst_friend_class (tree, tree);
186 static int can_complete_type_without_circularity (tree);
187 static tree get_bindings (tree, tree, tree, bool);
188 static int template_decl_level (tree);
189 static int check_cv_quals_for_unify (int, tree, tree);
190 static int unify_pack_expansion (tree, tree, tree,
191 tree, unification_kind_t, bool, bool);
192 static tree copy_template_args (tree);
193 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
194 tree most_specialized_partial_spec (tree, tsubst_flags_t);
195 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
196 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
197 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
198 static bool check_specialization_scope (void);
199 static tree process_partial_specialization (tree);
200 static void set_current_access_from_decl (tree);
201 static enum template_base_result get_template_base (tree, tree, tree, tree,
202 bool , tree *);
203 static tree try_class_unification (tree, tree, tree, tree, bool);
204 static bool class_nttp_const_wrapper_p (tree t);
205 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
206 tree, tree);
207 static bool template_template_parm_bindings_ok_p (tree, tree);
208 static void tsubst_default_arguments (tree, tsubst_flags_t);
209 static tree for_each_template_parm_r (tree *, int *, void *);
210 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
211 static void copy_default_args_to_explicit_spec (tree);
212 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
213 static bool dependent_template_arg_p (tree);
214 static bool any_template_arguments_need_structural_equality_p (tree);
215 static bool dependent_type_p_r (tree);
216 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
217 static tree tsubst_decl (tree, tree, tsubst_flags_t);
218 static void perform_instantiation_time_access_checks (tree, tree);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
223 static bool complex_alias_template_p (const_tree tmpl);
224 static tree get_underlying_template (tree);
225 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
226 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
227 static tree make_argument_pack (tree);
228 static void register_parameter_specializations (tree, tree);
229 static tree enclosing_instantiation_of (tree tctx);
230 static void instantiate_body (tree pattern, tree args, tree d, bool nested);
231
232 /* Make the current scope suitable for access checking when we are
233 processing T. T can be FUNCTION_DECL for instantiated function
234 template, VAR_DECL for static member variable, or TYPE_DECL for
235 alias template (needed by instantiate_decl). */
236
237 void
238 push_access_scope (tree t)
239 {
240 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
241 || TREE_CODE (t) == TYPE_DECL);
242
243 if (DECL_FRIEND_CONTEXT (t))
244 push_nested_class (DECL_FRIEND_CONTEXT (t));
245 else if (DECL_CLASS_SCOPE_P (t))
246 push_nested_class (DECL_CONTEXT (t));
247 else
248 push_to_top_level ();
249
250 if (TREE_CODE (t) == FUNCTION_DECL)
251 {
252 vec_safe_push (saved_access_scope, current_function_decl);
253 current_function_decl = t;
254 }
255 }
256
257 /* Restore the scope set up by push_access_scope. T is the node we
258 are processing. */
259
260 void
261 pop_access_scope (tree t)
262 {
263 if (TREE_CODE (t) == FUNCTION_DECL)
264 current_function_decl = saved_access_scope->pop();
265
266 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
267 pop_nested_class ();
268 else
269 pop_from_top_level ();
270 }
271
272 /* Do any processing required when DECL (a member template
273 declaration) is finished. Returns the TEMPLATE_DECL corresponding
274 to DECL, unless it is a specialization, in which case the DECL
275 itself is returned. */
276
277 tree
278 finish_member_template_decl (tree decl)
279 {
280 if (decl == error_mark_node)
281 return error_mark_node;
282
283 gcc_assert (DECL_P (decl));
284
285 if (TREE_CODE (decl) == TYPE_DECL)
286 {
287 tree type;
288
289 type = TREE_TYPE (decl);
290 if (type == error_mark_node)
291 return error_mark_node;
292 if (MAYBE_CLASS_TYPE_P (type)
293 && CLASSTYPE_TEMPLATE_INFO (type)
294 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
295 {
296 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
297 check_member_template (tmpl);
298 return tmpl;
299 }
300 return NULL_TREE;
301 }
302 else if (TREE_CODE (decl) == FIELD_DECL)
303 error_at (DECL_SOURCE_LOCATION (decl),
304 "data member %qD cannot be a member template", decl);
305 else if (DECL_TEMPLATE_INFO (decl))
306 {
307 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
308 {
309 check_member_template (DECL_TI_TEMPLATE (decl));
310 return DECL_TI_TEMPLATE (decl);
311 }
312 else
313 return decl;
314 }
315 else
316 error_at (DECL_SOURCE_LOCATION (decl),
317 "invalid member template declaration %qD", decl);
318
319 return error_mark_node;
320 }
321
322 /* Create a template info node. */
323
324 tree
325 build_template_info (tree template_decl, tree template_args)
326 {
327 tree result = make_node (TEMPLATE_INFO);
328 TI_TEMPLATE (result) = template_decl;
329 TI_ARGS (result) = template_args;
330 return result;
331 }
332
333 /* Return the template info node corresponding to T, whatever T is. */
334
335 tree
336 get_template_info (const_tree t)
337 {
338 tree tinfo = NULL_TREE;
339
340 if (!t || t == error_mark_node)
341 return NULL;
342
343 if (TREE_CODE (t) == NAMESPACE_DECL
344 || TREE_CODE (t) == PARM_DECL)
345 return NULL;
346
347 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
348 tinfo = DECL_TEMPLATE_INFO (t);
349
350 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
351 t = TREE_TYPE (t);
352
353 if (OVERLOAD_TYPE_P (t))
354 tinfo = TYPE_TEMPLATE_INFO (t);
355 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
356 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
357
358 return tinfo;
359 }
360
361 /* Returns the template nesting level of the indicated class TYPE.
362
363 For example, in:
364 template <class T>
365 struct A
366 {
367 template <class U>
368 struct B {};
369 };
370
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
374
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
377 always safe. */
378
379 int
380 template_class_depth (tree type)
381 {
382 int depth;
383
384 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
385 {
386 tree tinfo = get_template_info (type);
387
388 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
390 ++depth;
391
392 if (DECL_P (type))
393 {
394 if (tree fctx = DECL_FRIEND_CONTEXT (type))
395 type = fctx;
396 else
397 type = CP_DECL_CONTEXT (type);
398 }
399 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
400 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
401 else
402 type = CP_TYPE_CONTEXT (type);
403 }
404
405 return depth;
406 }
407
408 /* Return TRUE if NODE instantiates a template that has arguments of
409 its own, be it directly a primary template or indirectly through a
410 partial specializations. */
411 static bool
412 instantiates_primary_template_p (tree node)
413 {
414 tree tinfo = get_template_info (node);
415 if (!tinfo)
416 return false;
417
418 tree tmpl = TI_TEMPLATE (tinfo);
419 if (PRIMARY_TEMPLATE_P (tmpl))
420 return true;
421
422 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
423 return false;
424
425 /* So now we know we have a specialization, but it could be a full
426 or a partial specialization. To tell which, compare the depth of
427 its template arguments with those of its context. */
428
429 tree ctxt = DECL_CONTEXT (tmpl);
430 tree ctinfo = get_template_info (ctxt);
431 if (!ctinfo)
432 return true;
433
434 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
435 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
436 }
437
438 /* Subroutine of maybe_begin_member_template_processing.
439 Returns true if processing DECL needs us to push template parms. */
440
441 static bool
442 inline_needs_template_parms (tree decl, bool nsdmi)
443 {
444 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
445 return false;
446
447 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
448 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
449 }
450
451 /* Subroutine of maybe_begin_member_template_processing.
452 Push the template parms in PARMS, starting from LEVELS steps into the
453 chain, and ending at the beginning, since template parms are listed
454 innermost first. */
455
456 static void
457 push_inline_template_parms_recursive (tree parmlist, int levels)
458 {
459 tree parms = TREE_VALUE (parmlist);
460 int i;
461
462 if (levels > 1)
463 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
464
465 ++processing_template_decl;
466 current_template_parms
467 = tree_cons (size_int (processing_template_decl),
468 parms, current_template_parms);
469 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
470
471 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
472 NULL);
473 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
474 {
475 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
476
477 if (error_operand_p (parm))
478 continue;
479
480 gcc_assert (DECL_P (parm));
481
482 switch (TREE_CODE (parm))
483 {
484 case TYPE_DECL:
485 case TEMPLATE_DECL:
486 pushdecl (parm);
487 break;
488
489 case PARM_DECL:
490 /* Push the CONST_DECL. */
491 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
492 break;
493
494 default:
495 gcc_unreachable ();
496 }
497 }
498 }
499
500 /* Restore the template parameter context for a member template, a
501 friend template defined in a class definition, or a non-template
502 member of template class. */
503
504 void
505 maybe_begin_member_template_processing (tree decl)
506 {
507 tree parms;
508 int levels = 0;
509 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
510
511 if (nsdmi)
512 {
513 tree ctx = DECL_CONTEXT (decl);
514 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
515 /* Disregard full specializations (c++/60999). */
516 && uses_template_parms (ctx)
517 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
518 }
519
520 if (inline_needs_template_parms (decl, nsdmi))
521 {
522 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
523 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
524
525 if (DECL_TEMPLATE_SPECIALIZATION (decl))
526 {
527 --levels;
528 parms = TREE_CHAIN (parms);
529 }
530
531 push_inline_template_parms_recursive (parms, levels);
532 }
533
534 /* Remember how many levels of template parameters we pushed so that
535 we can pop them later. */
536 inline_parm_levels.safe_push (levels);
537 }
538
539 /* Undo the effects of maybe_begin_member_template_processing. */
540
541 void
542 maybe_end_member_template_processing (void)
543 {
544 int i;
545 int last;
546
547 if (inline_parm_levels.length () == 0)
548 return;
549
550 last = inline_parm_levels.pop ();
551 for (i = 0; i < last; ++i)
552 {
553 --processing_template_decl;
554 current_template_parms = TREE_CHAIN (current_template_parms);
555 poplevel (0, 0, 0);
556 }
557 }
558
559 /* Return a new template argument vector which contains all of ARGS,
560 but has as its innermost set of arguments the EXTRA_ARGS. */
561
562 static tree
563 add_to_template_args (tree args, tree extra_args)
564 {
565 tree new_args;
566 int extra_depth;
567 int i;
568 int j;
569
570 if (args == NULL_TREE || extra_args == error_mark_node)
571 return extra_args;
572
573 extra_depth = TMPL_ARGS_DEPTH (extra_args);
574 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
575
576 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
577 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
578
579 for (j = 1; j <= extra_depth; ++j, ++i)
580 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
581
582 return new_args;
583 }
584
585 /* Like add_to_template_args, but only the outermost ARGS are added to
586 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
587 (EXTRA_ARGS) levels are added. This function is used to combine
588 the template arguments from a partial instantiation with the
589 template arguments used to attain the full instantiation from the
590 partial instantiation.
591
592 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
593
594 tree
595 add_outermost_template_args (tree args, tree extra_args)
596 {
597 tree new_args;
598
599 if (!args)
600 return extra_args;
601 if (TREE_CODE (args) == TEMPLATE_DECL)
602 {
603 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
604 args = TI_ARGS (ti);
605 }
606
607 /* If there are more levels of EXTRA_ARGS than there are ARGS,
608 something very fishy is going on. */
609 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
610
611 /* If *all* the new arguments will be the EXTRA_ARGS, just return
612 them. */
613 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
614 return extra_args;
615
616 /* For the moment, we make ARGS look like it contains fewer levels. */
617 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
618
619 new_args = add_to_template_args (args, extra_args);
620
621 /* Now, we restore ARGS to its full dimensions. */
622 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
623
624 return new_args;
625 }
626
627 /* Return the N levels of innermost template arguments from the ARGS. */
628
629 tree
630 get_innermost_template_args (tree args, int n)
631 {
632 tree new_args;
633 int extra_levels;
634 int i;
635
636 gcc_assert (n >= 0);
637
638 /* If N is 1, just return the innermost set of template arguments. */
639 if (n == 1)
640 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
641
642 /* If we're not removing anything, just return the arguments we were
643 given. */
644 extra_levels = TMPL_ARGS_DEPTH (args) - n;
645 gcc_assert (extra_levels >= 0);
646 if (extra_levels == 0)
647 return args;
648
649 /* Make a new set of arguments, not containing the outer arguments. */
650 new_args = make_tree_vec (n);
651 for (i = 1; i <= n; ++i)
652 SET_TMPL_ARGS_LEVEL (new_args, i,
653 TMPL_ARGS_LEVEL (args, i + extra_levels));
654
655 return new_args;
656 }
657
658 /* The inverse of get_innermost_template_args: Return all but the innermost
659 EXTRA_LEVELS levels of template arguments from the ARGS. */
660
661 static tree
662 strip_innermost_template_args (tree args, int extra_levels)
663 {
664 tree new_args;
665 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
666 int i;
667
668 gcc_assert (n >= 0);
669
670 /* If N is 1, just return the outermost set of template arguments. */
671 if (n == 1)
672 return TMPL_ARGS_LEVEL (args, 1);
673
674 /* If we're not removing anything, just return the arguments we were
675 given. */
676 gcc_assert (extra_levels >= 0);
677 if (extra_levels == 0)
678 return args;
679
680 /* Make a new set of arguments, not containing the inner arguments. */
681 new_args = make_tree_vec (n);
682 for (i = 1; i <= n; ++i)
683 SET_TMPL_ARGS_LEVEL (new_args, i,
684 TMPL_ARGS_LEVEL (args, i));
685
686 return new_args;
687 }
688
689 /* We've got a template header coming up; push to a new level for storing
690 the parms. */
691
692 void
693 begin_template_parm_list (void)
694 {
695 /* We use a non-tag-transparent scope here, which causes pushtag to
696 put tags in this scope, rather than in the enclosing class or
697 namespace scope. This is the right thing, since we want
698 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
699 global template class, push_template_decl handles putting the
700 TEMPLATE_DECL into top-level scope. For a nested template class,
701 e.g.:
702
703 template <class T> struct S1 {
704 template <class T> struct S2 {};
705 };
706
707 pushtag contains special code to insert the TEMPLATE_DECL for S2
708 at the right scope. */
709 begin_scope (sk_template_parms, NULL);
710 ++processing_template_decl;
711 ++processing_template_parmlist;
712 note_template_header (0);
713
714 /* Add a dummy parameter level while we process the parameter list. */
715 current_template_parms
716 = tree_cons (size_int (processing_template_decl),
717 make_tree_vec (0),
718 current_template_parms);
719 }
720
721 /* This routine is called when a specialization is declared. If it is
722 invalid to declare a specialization here, an error is reported and
723 false is returned, otherwise this routine will return true. */
724
725 static bool
726 check_specialization_scope (void)
727 {
728 tree scope = current_scope ();
729
730 /* [temp.expl.spec]
731
732 An explicit specialization shall be declared in the namespace of
733 which the template is a member, or, for member templates, in the
734 namespace of which the enclosing class or enclosing class
735 template is a member. An explicit specialization of a member
736 function, member class or static data member of a class template
737 shall be declared in the namespace of which the class template
738 is a member. */
739 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
740 {
741 error ("explicit specialization in non-namespace scope %qD", scope);
742 return false;
743 }
744
745 /* [temp.expl.spec]
746
747 In an explicit specialization declaration for a member of a class
748 template or a member template that appears in namespace scope,
749 the member template and some of its enclosing class templates may
750 remain unspecialized, except that the declaration shall not
751 explicitly specialize a class member template if its enclosing
752 class templates are not explicitly specialized as well. */
753 if (current_template_parms)
754 {
755 error ("enclosing class templates are not explicitly specialized");
756 return false;
757 }
758
759 return true;
760 }
761
762 /* We've just seen template <>. */
763
764 bool
765 begin_specialization (void)
766 {
767 begin_scope (sk_template_spec, NULL);
768 note_template_header (1);
769 return check_specialization_scope ();
770 }
771
772 /* Called at then end of processing a declaration preceded by
773 template<>. */
774
775 void
776 end_specialization (void)
777 {
778 finish_scope ();
779 reset_specialization ();
780 }
781
782 /* Any template <>'s that we have seen thus far are not referring to a
783 function specialization. */
784
785 void
786 reset_specialization (void)
787 {
788 processing_specialization = 0;
789 template_header_count = 0;
790 }
791
792 /* We've just seen a template header. If SPECIALIZATION is nonzero,
793 it was of the form template <>. */
794
795 static void
796 note_template_header (int specialization)
797 {
798 processing_specialization = specialization;
799 template_header_count++;
800 }
801
802 /* We're beginning an explicit instantiation. */
803
804 void
805 begin_explicit_instantiation (void)
806 {
807 gcc_assert (!processing_explicit_instantiation);
808 processing_explicit_instantiation = true;
809 }
810
811
812 void
813 end_explicit_instantiation (void)
814 {
815 gcc_assert (processing_explicit_instantiation);
816 processing_explicit_instantiation = false;
817 }
818
819 /* An explicit specialization or partial specialization of TMPL is being
820 declared. Check that the namespace in which the specialization is
821 occurring is permissible. Returns false iff it is invalid to
822 specialize TMPL in the current namespace. */
823
824 static bool
825 check_specialization_namespace (tree tmpl)
826 {
827 tree tpl_ns = decl_namespace_context (tmpl);
828
829 /* [tmpl.expl.spec]
830
831 An explicit specialization shall be declared in a namespace enclosing the
832 specialized template. An explicit specialization whose declarator-id is
833 not qualified shall be declared in the nearest enclosing namespace of the
834 template, or, if the namespace is inline (7.3.1), any namespace from its
835 enclosing namespace set. */
836 if (current_scope() != DECL_CONTEXT (tmpl)
837 && !at_namespace_scope_p ())
838 {
839 error ("specialization of %qD must appear at namespace scope", tmpl);
840 return false;
841 }
842
843 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
844 /* Same or enclosing namespace. */
845 return true;
846 else
847 {
848 auto_diagnostic_group d;
849 if (permerror (input_location,
850 "specialization of %qD in different namespace", tmpl))
851 inform (DECL_SOURCE_LOCATION (tmpl),
852 " from definition of %q#D", tmpl);
853 return false;
854 }
855 }
856
857 /* SPEC is an explicit instantiation. Check that it is valid to
858 perform this explicit instantiation in the current namespace. */
859
860 static void
861 check_explicit_instantiation_namespace (tree spec)
862 {
863 tree ns;
864
865 /* DR 275: An explicit instantiation shall appear in an enclosing
866 namespace of its template. */
867 ns = decl_namespace_context (spec);
868 if (!is_nested_namespace (current_namespace, ns))
869 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
870 "(which does not enclose namespace %qD)",
871 spec, current_namespace, ns);
872 }
873
874 /* Returns the type of a template specialization only if that
875 specialization needs to be defined. Otherwise (e.g., if the type has
876 already been defined), the function returns NULL_TREE. */
877
878 static tree
879 maybe_new_partial_specialization (tree type)
880 {
881 /* An implicit instantiation of an incomplete type implies
882 the definition of a new class template.
883
884 template<typename T>
885 struct S;
886
887 template<typename T>
888 struct S<T*>;
889
890 Here, S<T*> is an implicit instantiation of S whose type
891 is incomplete. */
892 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
893 return type;
894
895 /* It can also be the case that TYPE is a completed specialization.
896 Continuing the previous example, suppose we also declare:
897
898 template<typename T>
899 requires Integral<T>
900 struct S<T*>;
901
902 Here, S<T*> refers to the specialization S<T*> defined
903 above. However, we need to differentiate definitions because
904 we intend to define a new partial specialization. In this case,
905 we rely on the fact that the constraints are different for
906 this declaration than that above.
907
908 Note that we also get here for injected class names and
909 late-parsed template definitions. We must ensure that we
910 do not create new type declarations for those cases. */
911 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
912 {
913 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
914 tree args = CLASSTYPE_TI_ARGS (type);
915
916 /* If there are no template parameters, this cannot be a new
917 partial template specialization? */
918 if (!current_template_parms)
919 return NULL_TREE;
920
921 /* The injected-class-name is not a new partial specialization. */
922 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
923 return NULL_TREE;
924
925 /* If the constraints are not the same as those of the primary
926 then, we can probably create a new specialization. */
927 tree type_constr = current_template_constraints ();
928
929 if (type == TREE_TYPE (tmpl))
930 {
931 tree main_constr = get_constraints (tmpl);
932 if (equivalent_constraints (type_constr, main_constr))
933 return NULL_TREE;
934 }
935
936 /* Also, if there's a pre-existing specialization with matching
937 constraints, then this also isn't new. */
938 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
939 while (specs)
940 {
941 tree spec_tmpl = TREE_VALUE (specs);
942 tree spec_args = TREE_PURPOSE (specs);
943 tree spec_constr = get_constraints (spec_tmpl);
944 if (comp_template_args (args, spec_args)
945 && equivalent_constraints (type_constr, spec_constr))
946 return NULL_TREE;
947 specs = TREE_CHAIN (specs);
948 }
949
950 /* Create a new type node (and corresponding type decl)
951 for the newly declared specialization. */
952 tree t = make_class_type (TREE_CODE (type));
953 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
954 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
955
956 /* We only need a separate type node for storing the definition of this
957 partial specialization; uses of S<T*> are unconstrained, so all are
958 equivalent. So keep TYPE_CANONICAL the same. */
959 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
960
961 /* Build the corresponding type decl. */
962 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
963 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
964 DECL_SOURCE_LOCATION (d) = input_location;
965 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
966 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
967
968 return t;
969 }
970
971 return NULL_TREE;
972 }
973
974 /* The TYPE is being declared. If it is a template type, that means it
975 is a partial specialization. Do appropriate error-checking. */
976
977 tree
978 maybe_process_partial_specialization (tree type)
979 {
980 tree context;
981
982 if (type == error_mark_node)
983 return error_mark_node;
984
985 /* A lambda that appears in specialization context is not itself a
986 specialization. */
987 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
988 return type;
989
990 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
991 {
992 error ("name of class shadows template template parameter %qD",
993 TYPE_NAME (type));
994 return error_mark_node;
995 }
996
997 context = TYPE_CONTEXT (type);
998
999 if (TYPE_ALIAS_P (type))
1000 {
1001 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
1002
1003 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
1004 error ("specialization of alias template %qD",
1005 TI_TEMPLATE (tinfo));
1006 else
1007 error ("explicit specialization of non-template %qT", type);
1008 return error_mark_node;
1009 }
1010 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1011 {
1012 /* This is for ordinary explicit specialization and partial
1013 specialization of a template class such as:
1014
1015 template <> class C<int>;
1016
1017 or:
1018
1019 template <class T> class C<T*>;
1020
1021 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1022
1023 if (tree t = maybe_new_partial_specialization (type))
1024 {
1025 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1026 && !at_namespace_scope_p ())
1027 return error_mark_node;
1028 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1029 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1030 if (processing_template_decl)
1031 {
1032 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1033 if (decl == error_mark_node)
1034 return error_mark_node;
1035 return TREE_TYPE (decl);
1036 }
1037 }
1038 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1039 error ("specialization of %qT after instantiation", type);
1040 else if (errorcount && !processing_specialization
1041 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1042 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1043 /* Trying to define a specialization either without a template<> header
1044 or in an inappropriate place. We've already given an error, so just
1045 bail now so we don't actually define the specialization. */
1046 return error_mark_node;
1047 }
1048 else if (CLASS_TYPE_P (type)
1049 && !CLASSTYPE_USE_TEMPLATE (type)
1050 && CLASSTYPE_TEMPLATE_INFO (type)
1051 && context && CLASS_TYPE_P (context)
1052 && CLASSTYPE_TEMPLATE_INFO (context))
1053 {
1054 /* This is for an explicit specialization of member class
1055 template according to [temp.expl.spec/18]:
1056
1057 template <> template <class U> class C<int>::D;
1058
1059 The context `C<int>' must be an implicit instantiation.
1060 Otherwise this is just a member class template declared
1061 earlier like:
1062
1063 template <> class C<int> { template <class U> class D; };
1064 template <> template <class U> class C<int>::D;
1065
1066 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1067 while in the second case, `C<int>::D' is a primary template
1068 and `C<T>::D' may not exist. */
1069
1070 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1071 && !COMPLETE_TYPE_P (type))
1072 {
1073 tree t;
1074 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1075
1076 if (current_namespace
1077 != decl_namespace_context (tmpl))
1078 {
1079 if (permerror (input_location,
1080 "specialization of %qD in different namespace",
1081 type))
1082 inform (DECL_SOURCE_LOCATION (tmpl),
1083 "from definition of %q#D", tmpl);
1084 }
1085
1086 /* Check for invalid specialization after instantiation:
1087
1088 template <> template <> class C<int>::D<int>;
1089 template <> template <class U> class C<int>::D; */
1090
1091 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1092 t; t = TREE_CHAIN (t))
1093 {
1094 tree inst = TREE_VALUE (t);
1095 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1096 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1097 {
1098 /* We already have a full specialization of this partial
1099 instantiation, or a full specialization has been
1100 looked up but not instantiated. Reassign it to the
1101 new member specialization template. */
1102 spec_entry elt;
1103 spec_entry *entry;
1104
1105 elt.tmpl = most_general_template (tmpl);
1106 elt.args = CLASSTYPE_TI_ARGS (inst);
1107 elt.spec = inst;
1108
1109 type_specializations->remove_elt (&elt);
1110
1111 elt.tmpl = tmpl;
1112 CLASSTYPE_TI_ARGS (inst)
1113 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1114
1115 spec_entry **slot
1116 = type_specializations->find_slot (&elt, INSERT);
1117 entry = ggc_alloc<spec_entry> ();
1118 *entry = elt;
1119 *slot = entry;
1120 }
1121 else
1122 /* But if we've had an implicit instantiation, that's a
1123 problem ([temp.expl.spec]/6). */
1124 error ("specialization %qT after instantiation %qT",
1125 type, inst);
1126 }
1127
1128 /* Mark TYPE as a specialization. And as a result, we only
1129 have one level of template argument for the innermost
1130 class template. */
1131 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1132 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1133 CLASSTYPE_TI_ARGS (type)
1134 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1135 }
1136 }
1137 else if (processing_specialization)
1138 {
1139 /* Someday C++0x may allow for enum template specialization. */
1140 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1141 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1142 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1143 "of %qD not allowed by ISO C++", type);
1144 else
1145 {
1146 error ("explicit specialization of non-template %qT", type);
1147 return error_mark_node;
1148 }
1149 }
1150
1151 return type;
1152 }
1153
1154 /* Returns nonzero if we can optimize the retrieval of specializations
1155 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1156 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1157
1158 static inline bool
1159 optimize_specialization_lookup_p (tree tmpl)
1160 {
1161 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1162 && DECL_CLASS_SCOPE_P (tmpl)
1163 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1164 parameter. */
1165 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1166 /* The optimized lookup depends on the fact that the
1167 template arguments for the member function template apply
1168 purely to the containing class, which is not true if the
1169 containing class is an explicit or partial
1170 specialization. */
1171 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1172 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1173 && !DECL_CONV_FN_P (tmpl)
1174 /* It is possible to have a template that is not a member
1175 template and is not a member of a template class:
1176
1177 template <typename T>
1178 struct S { friend A::f(); };
1179
1180 Here, the friend function is a template, but the context does
1181 not have template information. The optimized lookup relies
1182 on having ARGS be the template arguments for both the class
1183 and the function template. */
1184 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1185 }
1186
1187 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1188 gone through coerce_template_parms by now. */
1189
1190 static void
1191 verify_unstripped_args_1 (tree inner)
1192 {
1193 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1194 {
1195 tree arg = TREE_VEC_ELT (inner, i);
1196 if (TREE_CODE (arg) == TEMPLATE_DECL)
1197 /* OK */;
1198 else if (TYPE_P (arg))
1199 gcc_assert (strip_typedefs (arg, NULL) == arg);
1200 else if (ARGUMENT_PACK_P (arg))
1201 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1202 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1203 /* Allow typedefs on the type of a non-type argument, since a
1204 parameter can have them. */;
1205 else
1206 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1207 }
1208 }
1209
1210 static void
1211 verify_unstripped_args (tree args)
1212 {
1213 ++processing_template_decl;
1214 if (!any_dependent_template_arguments_p (args))
1215 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1216 --processing_template_decl;
1217 }
1218
1219 /* Retrieve the specialization (in the sense of [temp.spec] - a
1220 specialization is either an instantiation or an explicit
1221 specialization) of TMPL for the given template ARGS. If there is
1222 no such specialization, return NULL_TREE. The ARGS are a vector of
1223 arguments, or a vector of vectors of arguments, in the case of
1224 templates with more than one level of parameters.
1225
1226 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1227 then we search for a partial specialization matching ARGS. This
1228 parameter is ignored if TMPL is not a class template.
1229
1230 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1231 result is a NONTYPE_ARGUMENT_PACK. */
1232
1233 static tree
1234 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1235 {
1236 if (tmpl == NULL_TREE)
1237 return NULL_TREE;
1238
1239 if (args == error_mark_node)
1240 return NULL_TREE;
1241
1242 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1243 || TREE_CODE (tmpl) == FIELD_DECL);
1244
1245 /* There should be as many levels of arguments as there are
1246 levels of parameters. */
1247 gcc_assert (TMPL_ARGS_DEPTH (args)
1248 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1249 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1250 : template_class_depth (DECL_CONTEXT (tmpl))));
1251
1252 if (flag_checking)
1253 verify_unstripped_args (args);
1254
1255 /* Lambda functions in templates aren't instantiated normally, but through
1256 tsubst_lambda_expr. */
1257 if (lambda_fn_in_template_p (tmpl))
1258 return NULL_TREE;
1259
1260 if (optimize_specialization_lookup_p (tmpl))
1261 {
1262 /* The template arguments actually apply to the containing
1263 class. Find the class specialization with those
1264 arguments. */
1265 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1266 tree class_specialization
1267 = retrieve_specialization (class_template, args, 0);
1268 if (!class_specialization)
1269 return NULL_TREE;
1270
1271 /* Find the instance of TMPL. */
1272 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1273 for (ovl_iterator iter (fns); iter; ++iter)
1274 {
1275 tree fn = *iter;
1276 if (tree ti = get_template_info (fn))
1277 if (TI_TEMPLATE (ti) == tmpl
1278 /* using-declarations can bring in a different
1279 instantiation of tmpl as a member of a different
1280 instantiation of tmpl's class. We don't want those
1281 here. */
1282 && DECL_CONTEXT (fn) == class_specialization)
1283 return fn;
1284 }
1285 return NULL_TREE;
1286 }
1287 else
1288 {
1289 spec_entry *found;
1290 spec_entry elt;
1291 spec_hash_table *specializations;
1292
1293 elt.tmpl = tmpl;
1294 elt.args = args;
1295 elt.spec = NULL_TREE;
1296
1297 if (DECL_CLASS_TEMPLATE_P (tmpl))
1298 specializations = type_specializations;
1299 else
1300 specializations = decl_specializations;
1301
1302 if (hash == 0)
1303 hash = spec_hasher::hash (&elt);
1304 found = specializations->find_with_hash (&elt, hash);
1305 if (found)
1306 return found->spec;
1307 }
1308
1309 return NULL_TREE;
1310 }
1311
1312 /* Like retrieve_specialization, but for local declarations. */
1313
1314 tree
1315 retrieve_local_specialization (tree tmpl)
1316 {
1317 if (local_specializations == NULL)
1318 return NULL_TREE;
1319
1320 tree *slot = local_specializations->get (tmpl);
1321 return slot ? *slot : NULL_TREE;
1322 }
1323
1324 /* Returns nonzero iff DECL is a specialization of TMPL. */
1325
1326 int
1327 is_specialization_of (tree decl, tree tmpl)
1328 {
1329 tree t;
1330
1331 if (TREE_CODE (decl) == FUNCTION_DECL)
1332 {
1333 for (t = decl;
1334 t != NULL_TREE;
1335 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1336 if (t == tmpl)
1337 return 1;
1338 }
1339 else
1340 {
1341 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1342
1343 for (t = TREE_TYPE (decl);
1344 t != NULL_TREE;
1345 t = CLASSTYPE_USE_TEMPLATE (t)
1346 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1347 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1348 return 1;
1349 }
1350
1351 return 0;
1352 }
1353
1354 /* Returns nonzero iff DECL is a specialization of friend declaration
1355 FRIEND_DECL according to [temp.friend]. */
1356
1357 bool
1358 is_specialization_of_friend (tree decl, tree friend_decl)
1359 {
1360 bool need_template = true;
1361 int template_depth;
1362
1363 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1364 || TREE_CODE (decl) == TYPE_DECL);
1365
1366 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1367 of a template class, we want to check if DECL is a specialization
1368 if this. */
1369 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1370 && DECL_TEMPLATE_INFO (friend_decl)
1371 && !DECL_USE_TEMPLATE (friend_decl))
1372 {
1373 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1374 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1375 need_template = false;
1376 }
1377 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1378 && !PRIMARY_TEMPLATE_P (friend_decl))
1379 need_template = false;
1380
1381 /* There is nothing to do if this is not a template friend. */
1382 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1383 return false;
1384
1385 if (is_specialization_of (decl, friend_decl))
1386 return true;
1387
1388 /* [temp.friend/6]
1389 A member of a class template may be declared to be a friend of a
1390 non-template class. In this case, the corresponding member of
1391 every specialization of the class template is a friend of the
1392 class granting friendship.
1393
1394 For example, given a template friend declaration
1395
1396 template <class T> friend void A<T>::f();
1397
1398 the member function below is considered a friend
1399
1400 template <> struct A<int> {
1401 void f();
1402 };
1403
1404 For this type of template friend, TEMPLATE_DEPTH below will be
1405 nonzero. To determine if DECL is a friend of FRIEND, we first
1406 check if the enclosing class is a specialization of another. */
1407
1408 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1409 if (template_depth
1410 && DECL_CLASS_SCOPE_P (decl)
1411 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1412 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1413 {
1414 /* Next, we check the members themselves. In order to handle
1415 a few tricky cases, such as when FRIEND_DECL's are
1416
1417 template <class T> friend void A<T>::g(T t);
1418 template <class T> template <T t> friend void A<T>::h();
1419
1420 and DECL's are
1421
1422 void A<int>::g(int);
1423 template <int> void A<int>::h();
1424
1425 we need to figure out ARGS, the template arguments from
1426 the context of DECL. This is required for template substitution
1427 of `T' in the function parameter of `g' and template parameter
1428 of `h' in the above examples. Here ARGS corresponds to `int'. */
1429
1430 tree context = DECL_CONTEXT (decl);
1431 tree args = NULL_TREE;
1432 int current_depth = 0;
1433
1434 while (current_depth < template_depth)
1435 {
1436 if (CLASSTYPE_TEMPLATE_INFO (context))
1437 {
1438 if (current_depth == 0)
1439 args = TYPE_TI_ARGS (context);
1440 else
1441 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1442 current_depth++;
1443 }
1444 context = TYPE_CONTEXT (context);
1445 }
1446
1447 if (TREE_CODE (decl) == FUNCTION_DECL)
1448 {
1449 bool is_template;
1450 tree friend_type;
1451 tree decl_type;
1452 tree friend_args_type;
1453 tree decl_args_type;
1454
1455 /* Make sure that both DECL and FRIEND_DECL are templates or
1456 non-templates. */
1457 is_template = DECL_TEMPLATE_INFO (decl)
1458 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1459 if (need_template ^ is_template)
1460 return false;
1461 else if (is_template)
1462 {
1463 /* If both are templates, check template parameter list. */
1464 tree friend_parms
1465 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1466 args, tf_none);
1467 if (!comp_template_parms
1468 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1469 friend_parms))
1470 return false;
1471
1472 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1473 }
1474 else
1475 decl_type = TREE_TYPE (decl);
1476
1477 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1478 tf_none, NULL_TREE);
1479 if (friend_type == error_mark_node)
1480 return false;
1481
1482 /* Check if return types match. */
1483 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1484 return false;
1485
1486 /* Check if function parameter types match, ignoring the
1487 `this' parameter. */
1488 friend_args_type = TYPE_ARG_TYPES (friend_type);
1489 decl_args_type = TYPE_ARG_TYPES (decl_type);
1490 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1491 friend_args_type = TREE_CHAIN (friend_args_type);
1492 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1493 decl_args_type = TREE_CHAIN (decl_args_type);
1494
1495 return compparms (decl_args_type, friend_args_type);
1496 }
1497 else
1498 {
1499 /* DECL is a TYPE_DECL */
1500 bool is_template;
1501 tree decl_type = TREE_TYPE (decl);
1502
1503 /* Make sure that both DECL and FRIEND_DECL are templates or
1504 non-templates. */
1505 is_template
1506 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1507 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1508
1509 if (need_template ^ is_template)
1510 return false;
1511 else if (is_template)
1512 {
1513 tree friend_parms;
1514 /* If both are templates, check the name of the two
1515 TEMPLATE_DECL's first because is_friend didn't. */
1516 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1517 != DECL_NAME (friend_decl))
1518 return false;
1519
1520 /* Now check template parameter list. */
1521 friend_parms
1522 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1523 args, tf_none);
1524 return comp_template_parms
1525 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1526 friend_parms);
1527 }
1528 else
1529 return (DECL_NAME (decl)
1530 == DECL_NAME (friend_decl));
1531 }
1532 }
1533 return false;
1534 }
1535
1536 /* Register the specialization SPEC as a specialization of TMPL with
1537 the indicated ARGS. IS_FRIEND indicates whether the specialization
1538 is actually just a friend declaration. ATTRLIST is the list of
1539 attributes that the specialization is declared with or NULL when
1540 it isn't. Returns SPEC, or an equivalent prior declaration, if
1541 available.
1542
1543 We also store instantiations of field packs in the hash table, even
1544 though they are not themselves templates, to make lookup easier. */
1545
1546 static tree
1547 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1548 hashval_t hash)
1549 {
1550 tree fn;
1551 spec_entry **slot = NULL;
1552 spec_entry elt;
1553
1554 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1555 || (TREE_CODE (tmpl) == FIELD_DECL
1556 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1557
1558 if (TREE_CODE (spec) == FUNCTION_DECL
1559 && uses_template_parms (DECL_TI_ARGS (spec)))
1560 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1561 register it; we want the corresponding TEMPLATE_DECL instead.
1562 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1563 the more obvious `uses_template_parms (spec)' to avoid problems
1564 with default function arguments. In particular, given
1565 something like this:
1566
1567 template <class T> void f(T t1, T t = T())
1568
1569 the default argument expression is not substituted for in an
1570 instantiation unless and until it is actually needed. */
1571 return spec;
1572
1573 if (optimize_specialization_lookup_p (tmpl))
1574 /* We don't put these specializations in the hash table, but we might
1575 want to give an error about a mismatch. */
1576 fn = retrieve_specialization (tmpl, args, 0);
1577 else
1578 {
1579 elt.tmpl = tmpl;
1580 elt.args = args;
1581 elt.spec = spec;
1582
1583 if (hash == 0)
1584 hash = spec_hasher::hash (&elt);
1585
1586 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1587 if (*slot)
1588 fn = (*slot)->spec;
1589 else
1590 fn = NULL_TREE;
1591 }
1592
1593 /* We can sometimes try to re-register a specialization that we've
1594 already got. In particular, regenerate_decl_from_template calls
1595 duplicate_decls which will update the specialization list. But,
1596 we'll still get called again here anyhow. It's more convenient
1597 to simply allow this than to try to prevent it. */
1598 if (fn == spec)
1599 return spec;
1600 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1601 {
1602 if (DECL_TEMPLATE_INSTANTIATION (fn))
1603 {
1604 if (DECL_ODR_USED (fn)
1605 || DECL_EXPLICIT_INSTANTIATION (fn))
1606 {
1607 error ("specialization of %qD after instantiation",
1608 fn);
1609 return error_mark_node;
1610 }
1611 else
1612 {
1613 tree clone;
1614 /* This situation should occur only if the first
1615 specialization is an implicit instantiation, the
1616 second is an explicit specialization, and the
1617 implicit instantiation has not yet been used. That
1618 situation can occur if we have implicitly
1619 instantiated a member function and then specialized
1620 it later.
1621
1622 We can also wind up here if a friend declaration that
1623 looked like an instantiation turns out to be a
1624 specialization:
1625
1626 template <class T> void foo(T);
1627 class S { friend void foo<>(int) };
1628 template <> void foo(int);
1629
1630 We transform the existing DECL in place so that any
1631 pointers to it become pointers to the updated
1632 declaration.
1633
1634 If there was a definition for the template, but not
1635 for the specialization, we want this to look as if
1636 there were no definition, and vice versa. */
1637 DECL_INITIAL (fn) = NULL_TREE;
1638 duplicate_decls (spec, fn, is_friend);
1639 /* The call to duplicate_decls will have applied
1640 [temp.expl.spec]:
1641
1642 An explicit specialization of a function template
1643 is inline only if it is explicitly declared to be,
1644 and independently of whether its function template
1645 is.
1646
1647 to the primary function; now copy the inline bits to
1648 the various clones. */
1649 FOR_EACH_CLONE (clone, fn)
1650 {
1651 DECL_DECLARED_INLINE_P (clone)
1652 = DECL_DECLARED_INLINE_P (fn);
1653 DECL_SOURCE_LOCATION (clone)
1654 = DECL_SOURCE_LOCATION (fn);
1655 DECL_DELETED_FN (clone)
1656 = DECL_DELETED_FN (fn);
1657 }
1658 check_specialization_namespace (tmpl);
1659
1660 return fn;
1661 }
1662 }
1663 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1664 {
1665 tree dd = duplicate_decls (spec, fn, is_friend);
1666 if (dd == error_mark_node)
1667 /* We've already complained in duplicate_decls. */
1668 return error_mark_node;
1669
1670 if (dd == NULL_TREE && DECL_INITIAL (spec))
1671 /* Dup decl failed, but this is a new definition. Set the
1672 line number so any errors match this new
1673 definition. */
1674 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1675
1676 return fn;
1677 }
1678 }
1679 else if (fn)
1680 return duplicate_decls (spec, fn, is_friend);
1681
1682 /* A specialization must be declared in the same namespace as the
1683 template it is specializing. */
1684 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1685 && !check_specialization_namespace (tmpl))
1686 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1687
1688 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1689 {
1690 spec_entry *entry = ggc_alloc<spec_entry> ();
1691 gcc_assert (tmpl && args && spec);
1692 *entry = elt;
1693 *slot = entry;
1694 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1695 && PRIMARY_TEMPLATE_P (tmpl)
1696 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1697 || variable_template_p (tmpl))
1698 /* If TMPL is a forward declaration of a template function, keep a list
1699 of all specializations in case we need to reassign them to a friend
1700 template later in tsubst_friend_function.
1701
1702 Also keep a list of all variable template instantiations so that
1703 process_partial_specialization can check whether a later partial
1704 specialization would have used it. */
1705 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1706 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1707 }
1708
1709 return spec;
1710 }
1711
1712 /* Returns true iff two spec_entry nodes are equivalent. */
1713
1714 int comparing_specializations;
1715
1716 bool
1717 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1718 {
1719 int equal;
1720
1721 ++comparing_specializations;
1722 equal = (e1->tmpl == e2->tmpl
1723 && comp_template_args (e1->args, e2->args));
1724 if (equal && flag_concepts
1725 /* tmpl could be a FIELD_DECL for a capture pack. */
1726 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1727 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1728 && uses_template_parms (e1->args))
1729 {
1730 /* Partial specializations of a variable template can be distinguished by
1731 constraints. */
1732 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1733 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1734 equal = equivalent_constraints (c1, c2);
1735 }
1736 --comparing_specializations;
1737
1738 return equal;
1739 }
1740
1741 /* Returns a hash for a template TMPL and template arguments ARGS. */
1742
1743 static hashval_t
1744 hash_tmpl_and_args (tree tmpl, tree args)
1745 {
1746 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1747 return iterative_hash_template_arg (args, val);
1748 }
1749
1750 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1751 ignoring SPEC. */
1752
1753 hashval_t
1754 spec_hasher::hash (spec_entry *e)
1755 {
1756 return hash_tmpl_and_args (e->tmpl, e->args);
1757 }
1758
1759 /* Recursively calculate a hash value for a template argument ARG, for use
1760 in the hash tables of template specializations. We must be
1761 careful to (at least) skip the same entities template_args_equal
1762 does. */
1763
1764 hashval_t
1765 iterative_hash_template_arg (tree arg, hashval_t val)
1766 {
1767 if (arg == NULL_TREE)
1768 return iterative_hash_object (arg, val);
1769
1770 if (!TYPE_P (arg))
1771 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1772 while (CONVERT_EXPR_P (arg)
1773 || TREE_CODE (arg) == NON_LVALUE_EXPR
1774 || class_nttp_const_wrapper_p (arg))
1775 arg = TREE_OPERAND (arg, 0);
1776
1777 enum tree_code code = TREE_CODE (arg);
1778
1779 val = iterative_hash_object (code, val);
1780
1781 switch (code)
1782 {
1783 case ARGUMENT_PACK_SELECT:
1784 gcc_unreachable ();
1785
1786 case ERROR_MARK:
1787 return val;
1788
1789 case IDENTIFIER_NODE:
1790 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1791
1792 case TREE_VEC:
1793 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1794 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1795 return val;
1796
1797 case TYPE_PACK_EXPANSION:
1798 case EXPR_PACK_EXPANSION:
1799 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1800 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1801
1802 case TYPE_ARGUMENT_PACK:
1803 case NONTYPE_ARGUMENT_PACK:
1804 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1805
1806 case TREE_LIST:
1807 for (; arg; arg = TREE_CHAIN (arg))
1808 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1809 return val;
1810
1811 case OVERLOAD:
1812 for (lkp_iterator iter (arg); iter; ++iter)
1813 val = iterative_hash_template_arg (*iter, val);
1814 return val;
1815
1816 case CONSTRUCTOR:
1817 {
1818 tree field, value;
1819 unsigned i;
1820 iterative_hash_template_arg (TREE_TYPE (arg), val);
1821 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1822 {
1823 val = iterative_hash_template_arg (field, val);
1824 val = iterative_hash_template_arg (value, val);
1825 }
1826 return val;
1827 }
1828
1829 case PARM_DECL:
1830 if (!DECL_ARTIFICIAL (arg))
1831 {
1832 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1833 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1834 }
1835 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1836
1837 case TARGET_EXPR:
1838 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1839
1840 case PTRMEM_CST:
1841 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1842 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1843
1844 case TEMPLATE_PARM_INDEX:
1845 val = iterative_hash_template_arg
1846 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1847 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1848 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1849
1850 case TRAIT_EXPR:
1851 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1852 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1853 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1854
1855 case BASELINK:
1856 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1857 val);
1858 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1859 val);
1860
1861 case MODOP_EXPR:
1862 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1863 code = TREE_CODE (TREE_OPERAND (arg, 1));
1864 val = iterative_hash_object (code, val);
1865 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1866
1867 case LAMBDA_EXPR:
1868 /* [temp.over.link] Two lambda-expressions are never considered
1869 equivalent.
1870
1871 So just hash the closure type. */
1872 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1873
1874 case CAST_EXPR:
1875 case IMPLICIT_CONV_EXPR:
1876 case STATIC_CAST_EXPR:
1877 case REINTERPRET_CAST_EXPR:
1878 case CONST_CAST_EXPR:
1879 case DYNAMIC_CAST_EXPR:
1880 case NEW_EXPR:
1881 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1882 /* Now hash operands as usual. */
1883 break;
1884
1885 case CALL_EXPR:
1886 {
1887 tree fn = CALL_EXPR_FN (arg);
1888 if (tree name = dependent_name (fn))
1889 {
1890 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1891 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1892 fn = name;
1893 }
1894 val = iterative_hash_template_arg (fn, val);
1895 call_expr_arg_iterator ai;
1896 for (tree x = first_call_expr_arg (arg, &ai); x;
1897 x = next_call_expr_arg (&ai))
1898 val = iterative_hash_template_arg (x, val);
1899 return val;
1900 }
1901
1902 default:
1903 break;
1904 }
1905
1906 char tclass = TREE_CODE_CLASS (code);
1907 switch (tclass)
1908 {
1909 case tcc_type:
1910 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1911 {
1912 // We want an alias specialization that survived strip_typedefs
1913 // to hash differently from its TYPE_CANONICAL, to avoid hash
1914 // collisions that compare as different in template_args_equal.
1915 // These could be dependent specializations that strip_typedefs
1916 // left alone, or untouched specializations because
1917 // coerce_template_parms returns the unconverted template
1918 // arguments if it sees incomplete argument packs.
1919 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1920 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1921 }
1922
1923 switch (TREE_CODE (arg))
1924 {
1925 case TEMPLATE_TEMPLATE_PARM:
1926 {
1927 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1928
1929 /* Do not recurse with TPI directly, as that is unbounded
1930 recursion. */
1931 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1932 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1933 }
1934 break;
1935
1936 case DECLTYPE_TYPE:
1937 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1938 break;
1939
1940 default:
1941 if (tree canonical = TYPE_CANONICAL (arg))
1942 val = iterative_hash_object (TYPE_HASH (canonical), val);
1943 break;
1944 }
1945
1946 return val;
1947
1948 case tcc_declaration:
1949 case tcc_constant:
1950 return iterative_hash_expr (arg, val);
1951
1952 default:
1953 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1954 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1955 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1956 return val;
1957 }
1958
1959 gcc_unreachable ();
1960 return 0;
1961 }
1962
1963 /* Unregister the specialization SPEC as a specialization of TMPL.
1964 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1965 if the SPEC was listed as a specialization of TMPL.
1966
1967 Note that SPEC has been ggc_freed, so we can't look inside it. */
1968
1969 bool
1970 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1971 {
1972 spec_entry *entry;
1973 spec_entry elt;
1974
1975 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1976 elt.args = TI_ARGS (tinfo);
1977 elt.spec = NULL_TREE;
1978
1979 entry = decl_specializations->find (&elt);
1980 if (entry != NULL)
1981 {
1982 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1983 gcc_assert (new_spec != NULL_TREE);
1984 entry->spec = new_spec;
1985 return 1;
1986 }
1987
1988 return 0;
1989 }
1990
1991 /* Like register_specialization, but for local declarations. We are
1992 registering SPEC, an instantiation of TMPL. */
1993
1994 void
1995 register_local_specialization (tree spec, tree tmpl)
1996 {
1997 gcc_assert (tmpl != spec);
1998 local_specializations->put (tmpl, spec);
1999 }
2000
2001 /* TYPE is a class type. Returns true if TYPE is an explicitly
2002 specialized class. */
2003
2004 bool
2005 explicit_class_specialization_p (tree type)
2006 {
2007 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2008 return false;
2009 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2010 }
2011
2012 /* Print the list of functions at FNS, going through all the overloads
2013 for each element of the list. Alternatively, FNS cannot be a
2014 TREE_LIST, in which case it will be printed together with all the
2015 overloads.
2016
2017 MORE and *STR should respectively be FALSE and NULL when the function
2018 is called from the outside. They are used internally on recursive
2019 calls. print_candidates manages the two parameters and leaves NULL
2020 in *STR when it ends. */
2021
2022 static void
2023 print_candidates_1 (tree fns, char **str, bool more = false)
2024 {
2025 if (TREE_CODE (fns) == TREE_LIST)
2026 for (; fns; fns = TREE_CHAIN (fns))
2027 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2028 else
2029 for (lkp_iterator iter (fns); iter;)
2030 {
2031 tree cand = *iter;
2032 ++iter;
2033
2034 const char *pfx = *str;
2035 if (!pfx)
2036 {
2037 if (more || iter)
2038 pfx = _("candidates are:");
2039 else
2040 pfx = _("candidate is:");
2041 *str = get_spaces (pfx);
2042 }
2043 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2044 }
2045 }
2046
2047 /* Print the list of candidate FNS in an error message. FNS can also
2048 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2049
2050 void
2051 print_candidates (tree fns)
2052 {
2053 char *str = NULL;
2054 print_candidates_1 (fns, &str);
2055 free (str);
2056 }
2057
2058 /* Get a (possibly) constrained template declaration for the
2059 purpose of ordering candidates. */
2060 static tree
2061 get_template_for_ordering (tree list)
2062 {
2063 gcc_assert (TREE_CODE (list) == TREE_LIST);
2064 tree f = TREE_VALUE (list);
2065 if (tree ti = DECL_TEMPLATE_INFO (f))
2066 return TI_TEMPLATE (ti);
2067 return f;
2068 }
2069
2070 /* Among candidates having the same signature, return the
2071 most constrained or NULL_TREE if there is no best candidate.
2072 If the signatures of candidates vary (e.g., template
2073 specialization vs. member function), then there can be no
2074 most constrained.
2075
2076 Note that we don't compare constraints on the functions
2077 themselves, but rather those of their templates. */
2078 static tree
2079 most_constrained_function (tree candidates)
2080 {
2081 // Try to find the best candidate in a first pass.
2082 tree champ = candidates;
2083 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2084 {
2085 int winner = more_constrained (get_template_for_ordering (champ),
2086 get_template_for_ordering (c));
2087 if (winner == -1)
2088 champ = c; // The candidate is more constrained
2089 else if (winner == 0)
2090 return NULL_TREE; // Neither is more constrained
2091 }
2092
2093 // Verify that the champ is better than previous candidates.
2094 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2095 if (!more_constrained (get_template_for_ordering (champ),
2096 get_template_for_ordering (c)))
2097 return NULL_TREE;
2098 }
2099
2100 return champ;
2101 }
2102
2103
2104 /* Returns the template (one of the functions given by TEMPLATE_ID)
2105 which can be specialized to match the indicated DECL with the
2106 explicit template args given in TEMPLATE_ID. The DECL may be
2107 NULL_TREE if none is available. In that case, the functions in
2108 TEMPLATE_ID are non-members.
2109
2110 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2111 specialization of a member template.
2112
2113 The TEMPLATE_COUNT is the number of references to qualifying
2114 template classes that appeared in the name of the function. See
2115 check_explicit_specialization for a more accurate description.
2116
2117 TSK indicates what kind of template declaration (if any) is being
2118 declared. TSK_TEMPLATE indicates that the declaration given by
2119 DECL, though a FUNCTION_DECL, has template parameters, and is
2120 therefore a template function.
2121
2122 The template args (those explicitly specified and those deduced)
2123 are output in a newly created vector *TARGS_OUT.
2124
2125 If it is impossible to determine the result, an error message is
2126 issued. The error_mark_node is returned to indicate failure. */
2127
2128 static tree
2129 determine_specialization (tree template_id,
2130 tree decl,
2131 tree* targs_out,
2132 int need_member_template,
2133 int template_count,
2134 tmpl_spec_kind tsk)
2135 {
2136 tree fns;
2137 tree targs;
2138 tree explicit_targs;
2139 tree candidates = NULL_TREE;
2140
2141 /* A TREE_LIST of templates of which DECL may be a specialization.
2142 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2143 corresponding TREE_PURPOSE is the set of template arguments that,
2144 when used to instantiate the template, would produce a function
2145 with the signature of DECL. */
2146 tree templates = NULL_TREE;
2147 int header_count;
2148 cp_binding_level *b;
2149
2150 *targs_out = NULL_TREE;
2151
2152 if (template_id == error_mark_node || decl == error_mark_node)
2153 return error_mark_node;
2154
2155 /* We shouldn't be specializing a member template of an
2156 unspecialized class template; we already gave an error in
2157 check_specialization_scope, now avoid crashing. */
2158 if (!VAR_P (decl)
2159 && template_count && DECL_CLASS_SCOPE_P (decl)
2160 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2161 {
2162 gcc_assert (errorcount);
2163 return error_mark_node;
2164 }
2165
2166 fns = TREE_OPERAND (template_id, 0);
2167 explicit_targs = TREE_OPERAND (template_id, 1);
2168
2169 if (fns == error_mark_node)
2170 return error_mark_node;
2171
2172 /* Check for baselinks. */
2173 if (BASELINK_P (fns))
2174 fns = BASELINK_FUNCTIONS (fns);
2175
2176 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2177 {
2178 error_at (DECL_SOURCE_LOCATION (decl),
2179 "%qD is not a function template", fns);
2180 return error_mark_node;
2181 }
2182 else if (VAR_P (decl) && !variable_template_p (fns))
2183 {
2184 error ("%qD is not a variable template", fns);
2185 return error_mark_node;
2186 }
2187
2188 /* Count the number of template headers specified for this
2189 specialization. */
2190 header_count = 0;
2191 for (b = current_binding_level;
2192 b->kind == sk_template_parms;
2193 b = b->level_chain)
2194 ++header_count;
2195
2196 tree orig_fns = fns;
2197
2198 if (variable_template_p (fns))
2199 {
2200 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2201 targs = coerce_template_parms (parms, explicit_targs, fns,
2202 tf_warning_or_error,
2203 /*req_all*/true, /*use_defarg*/true);
2204 if (targs != error_mark_node)
2205 templates = tree_cons (targs, fns, templates);
2206 }
2207 else for (lkp_iterator iter (fns); iter; ++iter)
2208 {
2209 tree fn = *iter;
2210
2211 if (TREE_CODE (fn) == TEMPLATE_DECL)
2212 {
2213 tree decl_arg_types;
2214 tree fn_arg_types;
2215 tree insttype;
2216
2217 /* In case of explicit specialization, we need to check if
2218 the number of template headers appearing in the specialization
2219 is correct. This is usually done in check_explicit_specialization,
2220 but the check done there cannot be exhaustive when specializing
2221 member functions. Consider the following code:
2222
2223 template <> void A<int>::f(int);
2224 template <> template <> void A<int>::f(int);
2225
2226 Assuming that A<int> is not itself an explicit specialization
2227 already, the first line specializes "f" which is a non-template
2228 member function, whilst the second line specializes "f" which
2229 is a template member function. So both lines are syntactically
2230 correct, and check_explicit_specialization does not reject
2231 them.
2232
2233 Here, we can do better, as we are matching the specialization
2234 against the declarations. We count the number of template
2235 headers, and we check if they match TEMPLATE_COUNT + 1
2236 (TEMPLATE_COUNT is the number of qualifying template classes,
2237 plus there must be another header for the member template
2238 itself).
2239
2240 Notice that if header_count is zero, this is not a
2241 specialization but rather a template instantiation, so there
2242 is no check we can perform here. */
2243 if (header_count && header_count != template_count + 1)
2244 continue;
2245
2246 /* Check that the number of template arguments at the
2247 innermost level for DECL is the same as for FN. */
2248 if (current_binding_level->kind == sk_template_parms
2249 && !current_binding_level->explicit_spec_p
2250 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2251 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2252 (current_template_parms))))
2253 continue;
2254
2255 /* DECL might be a specialization of FN. */
2256 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2257 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2258
2259 /* For a non-static member function, we need to make sure
2260 that the const qualification is the same. Since
2261 get_bindings does not try to merge the "this" parameter,
2262 we must do the comparison explicitly. */
2263 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2264 {
2265 if (!same_type_p (TREE_VALUE (fn_arg_types),
2266 TREE_VALUE (decl_arg_types)))
2267 continue;
2268
2269 /* And the ref-qualification. */
2270 if (type_memfn_rqual (TREE_TYPE (decl))
2271 != type_memfn_rqual (TREE_TYPE (fn)))
2272 continue;
2273 }
2274
2275 /* Skip the "this" parameter and, for constructors of
2276 classes with virtual bases, the VTT parameter. A
2277 full specialization of a constructor will have a VTT
2278 parameter, but a template never will. */
2279 decl_arg_types
2280 = skip_artificial_parms_for (decl, decl_arg_types);
2281 fn_arg_types
2282 = skip_artificial_parms_for (fn, fn_arg_types);
2283
2284 /* Function templates cannot be specializations; there are
2285 no partial specializations of functions. Therefore, if
2286 the type of DECL does not match FN, there is no
2287 match.
2288
2289 Note that it should never be the case that we have both
2290 candidates added here, and for regular member functions
2291 below. */
2292 if (tsk == tsk_template)
2293 {
2294 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2295 current_template_parms))
2296 continue;
2297 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2298 TREE_TYPE (TREE_TYPE (fn))))
2299 continue;
2300 if (!compparms (fn_arg_types, decl_arg_types))
2301 continue;
2302
2303 tree freq = get_trailing_function_requirements (fn);
2304 tree dreq = get_trailing_function_requirements (decl);
2305 if (!freq != !dreq)
2306 continue;
2307 if (freq)
2308 {
2309 tree fargs = DECL_TI_ARGS (fn);
2310 tsubst_flags_t complain = tf_none;
2311 freq = tsubst_constraint (freq, fargs, complain, fn);
2312 if (!cp_tree_equal (freq, dreq))
2313 continue;
2314 }
2315
2316 candidates = tree_cons (NULL_TREE, fn, candidates);
2317 continue;
2318 }
2319
2320 /* See whether this function might be a specialization of this
2321 template. Suppress access control because we might be trying
2322 to make this specialization a friend, and we have already done
2323 access control for the declaration of the specialization. */
2324 push_deferring_access_checks (dk_no_check);
2325 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2326 pop_deferring_access_checks ();
2327
2328 if (!targs)
2329 /* We cannot deduce template arguments that when used to
2330 specialize TMPL will produce DECL. */
2331 continue;
2332
2333 if (uses_template_parms (targs))
2334 /* We deduced something involving 'auto', which isn't a valid
2335 template argument. */
2336 continue;
2337
2338 /* Remove, from the set of candidates, all those functions
2339 whose constraints are not satisfied. */
2340 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2341 continue;
2342
2343 // Then, try to form the new function type.
2344 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2345 if (insttype == error_mark_node)
2346 continue;
2347 fn_arg_types
2348 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2349 if (!compparms (fn_arg_types, decl_arg_types))
2350 continue;
2351
2352 /* Save this template, and the arguments deduced. */
2353 templates = tree_cons (targs, fn, templates);
2354 }
2355 else if (need_member_template)
2356 /* FN is an ordinary member function, and we need a
2357 specialization of a member template. */
2358 ;
2359 else if (TREE_CODE (fn) != FUNCTION_DECL)
2360 /* We can get IDENTIFIER_NODEs here in certain erroneous
2361 cases. */
2362 ;
2363 else if (!DECL_FUNCTION_MEMBER_P (fn))
2364 /* This is just an ordinary non-member function. Nothing can
2365 be a specialization of that. */
2366 ;
2367 else if (DECL_ARTIFICIAL (fn))
2368 /* Cannot specialize functions that are created implicitly. */
2369 ;
2370 else
2371 {
2372 tree decl_arg_types;
2373
2374 /* This is an ordinary member function. However, since
2375 we're here, we can assume its enclosing class is a
2376 template class. For example,
2377
2378 template <typename T> struct S { void f(); };
2379 template <> void S<int>::f() {}
2380
2381 Here, S<int>::f is a non-template, but S<int> is a
2382 template class. If FN has the same type as DECL, we
2383 might be in business. */
2384
2385 if (!DECL_TEMPLATE_INFO (fn))
2386 /* Its enclosing class is an explicit specialization
2387 of a template class. This is not a candidate. */
2388 continue;
2389
2390 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2391 TREE_TYPE (TREE_TYPE (fn))))
2392 /* The return types differ. */
2393 continue;
2394
2395 /* Adjust the type of DECL in case FN is a static member. */
2396 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2397 if (DECL_STATIC_FUNCTION_P (fn)
2398 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2399 decl_arg_types = TREE_CHAIN (decl_arg_types);
2400
2401 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2402 decl_arg_types))
2403 continue;
2404
2405 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2406 && (type_memfn_rqual (TREE_TYPE (decl))
2407 != type_memfn_rqual (TREE_TYPE (fn))))
2408 continue;
2409
2410 // If the deduced arguments do not satisfy the constraints,
2411 // this is not a candidate.
2412 if (flag_concepts && !constraints_satisfied_p (fn))
2413 continue;
2414
2415 // Add the candidate.
2416 candidates = tree_cons (NULL_TREE, fn, candidates);
2417 }
2418 }
2419
2420 if (templates && TREE_CHAIN (templates))
2421 {
2422 /* We have:
2423
2424 [temp.expl.spec]
2425
2426 It is possible for a specialization with a given function
2427 signature to be instantiated from more than one function
2428 template. In such cases, explicit specification of the
2429 template arguments must be used to uniquely identify the
2430 function template specialization being specialized.
2431
2432 Note that here, there's no suggestion that we're supposed to
2433 determine which of the candidate templates is most
2434 specialized. However, we, also have:
2435
2436 [temp.func.order]
2437
2438 Partial ordering of overloaded function template
2439 declarations is used in the following contexts to select
2440 the function template to which a function template
2441 specialization refers:
2442
2443 -- when an explicit specialization refers to a function
2444 template.
2445
2446 So, we do use the partial ordering rules, at least for now.
2447 This extension can only serve to make invalid programs valid,
2448 so it's safe. And, there is strong anecdotal evidence that
2449 the committee intended the partial ordering rules to apply;
2450 the EDG front end has that behavior, and John Spicer claims
2451 that the committee simply forgot to delete the wording in
2452 [temp.expl.spec]. */
2453 tree tmpl = most_specialized_instantiation (templates);
2454 if (tmpl != error_mark_node)
2455 {
2456 templates = tmpl;
2457 TREE_CHAIN (templates) = NULL_TREE;
2458 }
2459 }
2460
2461 // Concepts allows multiple declarations of member functions
2462 // with the same signature. Like above, we need to rely on
2463 // on the partial ordering of those candidates to determine which
2464 // is the best.
2465 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2466 {
2467 if (tree cand = most_constrained_function (candidates))
2468 {
2469 candidates = cand;
2470 TREE_CHAIN (cand) = NULL_TREE;
2471 }
2472 }
2473
2474 if (templates == NULL_TREE && candidates == NULL_TREE)
2475 {
2476 error ("template-id %qD for %q+D does not match any template "
2477 "declaration", template_id, decl);
2478 if (header_count && header_count != template_count + 1)
2479 inform (DECL_SOURCE_LOCATION (decl),
2480 "saw %d %<template<>%>, need %d for "
2481 "specializing a member function template",
2482 header_count, template_count + 1);
2483 else
2484 print_candidates (orig_fns);
2485 return error_mark_node;
2486 }
2487 else if ((templates && TREE_CHAIN (templates))
2488 || (candidates && TREE_CHAIN (candidates))
2489 || (templates && candidates))
2490 {
2491 error ("ambiguous template specialization %qD for %q+D",
2492 template_id, decl);
2493 candidates = chainon (candidates, templates);
2494 print_candidates (candidates);
2495 return error_mark_node;
2496 }
2497
2498 /* We have one, and exactly one, match. */
2499 if (candidates)
2500 {
2501 tree fn = TREE_VALUE (candidates);
2502 *targs_out = copy_node (DECL_TI_ARGS (fn));
2503
2504 /* Propagate the candidate's constraints to the declaration. */
2505 if (tsk != tsk_template)
2506 set_constraints (decl, get_constraints (fn));
2507
2508 /* DECL is a re-declaration or partial instantiation of a template
2509 function. */
2510 if (TREE_CODE (fn) == TEMPLATE_DECL)
2511 return fn;
2512 /* It was a specialization of an ordinary member function in a
2513 template class. */
2514 return DECL_TI_TEMPLATE (fn);
2515 }
2516
2517 /* It was a specialization of a template. */
2518 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2519 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2520 {
2521 *targs_out = copy_node (targs);
2522 SET_TMPL_ARGS_LEVEL (*targs_out,
2523 TMPL_ARGS_DEPTH (*targs_out),
2524 TREE_PURPOSE (templates));
2525 }
2526 else
2527 *targs_out = TREE_PURPOSE (templates);
2528 return TREE_VALUE (templates);
2529 }
2530
2531 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2532 but with the default argument values filled in from those in the
2533 TMPL_TYPES. */
2534
2535 static tree
2536 copy_default_args_to_explicit_spec_1 (tree spec_types,
2537 tree tmpl_types)
2538 {
2539 tree new_spec_types;
2540
2541 if (!spec_types)
2542 return NULL_TREE;
2543
2544 if (spec_types == void_list_node)
2545 return void_list_node;
2546
2547 /* Substitute into the rest of the list. */
2548 new_spec_types =
2549 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2550 TREE_CHAIN (tmpl_types));
2551
2552 /* Add the default argument for this parameter. */
2553 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2554 TREE_VALUE (spec_types),
2555 new_spec_types);
2556 }
2557
2558 /* DECL is an explicit specialization. Replicate default arguments
2559 from the template it specializes. (That way, code like:
2560
2561 template <class T> void f(T = 3);
2562 template <> void f(double);
2563 void g () { f (); }
2564
2565 works, as required.) An alternative approach would be to look up
2566 the correct default arguments at the call-site, but this approach
2567 is consistent with how implicit instantiations are handled. */
2568
2569 static void
2570 copy_default_args_to_explicit_spec (tree decl)
2571 {
2572 tree tmpl;
2573 tree spec_types;
2574 tree tmpl_types;
2575 tree new_spec_types;
2576 tree old_type;
2577 tree new_type;
2578 tree t;
2579 tree object_type = NULL_TREE;
2580 tree in_charge = NULL_TREE;
2581 tree vtt = NULL_TREE;
2582
2583 /* See if there's anything we need to do. */
2584 tmpl = DECL_TI_TEMPLATE (decl);
2585 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2586 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2587 if (TREE_PURPOSE (t))
2588 break;
2589 if (!t)
2590 return;
2591
2592 old_type = TREE_TYPE (decl);
2593 spec_types = TYPE_ARG_TYPES (old_type);
2594
2595 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2596 {
2597 /* Remove the this pointer, but remember the object's type for
2598 CV quals. */
2599 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2600 spec_types = TREE_CHAIN (spec_types);
2601 tmpl_types = TREE_CHAIN (tmpl_types);
2602
2603 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2604 {
2605 /* DECL may contain more parameters than TMPL due to the extra
2606 in-charge parameter in constructors and destructors. */
2607 in_charge = spec_types;
2608 spec_types = TREE_CHAIN (spec_types);
2609 }
2610 if (DECL_HAS_VTT_PARM_P (decl))
2611 {
2612 vtt = spec_types;
2613 spec_types = TREE_CHAIN (spec_types);
2614 }
2615 }
2616
2617 /* Compute the merged default arguments. */
2618 new_spec_types =
2619 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2620
2621 /* Compute the new FUNCTION_TYPE. */
2622 if (object_type)
2623 {
2624 if (vtt)
2625 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2626 TREE_VALUE (vtt),
2627 new_spec_types);
2628
2629 if (in_charge)
2630 /* Put the in-charge parameter back. */
2631 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2632 TREE_VALUE (in_charge),
2633 new_spec_types);
2634
2635 new_type = build_method_type_directly (object_type,
2636 TREE_TYPE (old_type),
2637 new_spec_types);
2638 }
2639 else
2640 new_type = build_function_type (TREE_TYPE (old_type),
2641 new_spec_types);
2642 new_type = cp_build_type_attribute_variant (new_type,
2643 TYPE_ATTRIBUTES (old_type));
2644 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2645
2646 TREE_TYPE (decl) = new_type;
2647 }
2648
2649 /* Return the number of template headers we expect to see for a definition
2650 or specialization of CTYPE or one of its non-template members. */
2651
2652 int
2653 num_template_headers_for_class (tree ctype)
2654 {
2655 int num_templates = 0;
2656
2657 while (ctype && CLASS_TYPE_P (ctype))
2658 {
2659 /* You're supposed to have one `template <...>' for every
2660 template class, but you don't need one for a full
2661 specialization. For example:
2662
2663 template <class T> struct S{};
2664 template <> struct S<int> { void f(); };
2665 void S<int>::f () {}
2666
2667 is correct; there shouldn't be a `template <>' for the
2668 definition of `S<int>::f'. */
2669 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2670 /* If CTYPE does not have template information of any
2671 kind, then it is not a template, nor is it nested
2672 within a template. */
2673 break;
2674 if (explicit_class_specialization_p (ctype))
2675 break;
2676 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2677 ++num_templates;
2678
2679 ctype = TYPE_CONTEXT (ctype);
2680 }
2681
2682 return num_templates;
2683 }
2684
2685 /* Do a simple sanity check on the template headers that precede the
2686 variable declaration DECL. */
2687
2688 void
2689 check_template_variable (tree decl)
2690 {
2691 tree ctx = CP_DECL_CONTEXT (decl);
2692 int wanted = num_template_headers_for_class (ctx);
2693 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2694 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2695 {
2696 if (cxx_dialect < cxx14)
2697 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2698 "variable templates only available with "
2699 "%<-std=c++14%> or %<-std=gnu++14%>");
2700
2701 // Namespace-scope variable templates should have a template header.
2702 ++wanted;
2703 }
2704 if (template_header_count > wanted)
2705 {
2706 auto_diagnostic_group d;
2707 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2708 "too many template headers for %qD "
2709 "(should be %d)",
2710 decl, wanted);
2711 if (warned && CLASS_TYPE_P (ctx)
2712 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2713 inform (DECL_SOURCE_LOCATION (decl),
2714 "members of an explicitly specialized class are defined "
2715 "without a template header");
2716 }
2717 }
2718
2719 /* An explicit specialization whose declarator-id or class-head-name is not
2720 qualified shall be declared in the nearest enclosing namespace of the
2721 template, or, if the namespace is inline (7.3.1), any namespace from its
2722 enclosing namespace set.
2723
2724 If the name declared in the explicit instantiation is an unqualified name,
2725 the explicit instantiation shall appear in the namespace where its template
2726 is declared or, if that namespace is inline (7.3.1), any namespace from its
2727 enclosing namespace set. */
2728
2729 void
2730 check_unqualified_spec_or_inst (tree t, location_t loc)
2731 {
2732 tree tmpl = most_general_template (t);
2733 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2734 && !is_nested_namespace (current_namespace,
2735 CP_DECL_CONTEXT (tmpl), true))
2736 {
2737 if (processing_specialization)
2738 permerror (loc, "explicit specialization of %qD outside its "
2739 "namespace must use a nested-name-specifier", tmpl);
2740 else if (processing_explicit_instantiation
2741 && cxx_dialect >= cxx11)
2742 /* This was allowed in C++98, so only pedwarn. */
2743 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2744 "outside its namespace must use a nested-name-"
2745 "specifier", tmpl);
2746 }
2747 }
2748
2749 /* Warn for a template specialization SPEC that is missing some of a set
2750 of function or type attributes that the template TEMPL is declared with.
2751 ATTRLIST is a list of additional attributes that SPEC should be taken
2752 to ultimately be declared with. */
2753
2754 static void
2755 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2756 {
2757 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2758 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2759
2760 /* Avoid warning if the difference between the primary and
2761 the specialization is not in one of the attributes below. */
2762 const char* const blacklist[] = {
2763 "alloc_align", "alloc_size", "assume_aligned", "format",
2764 "format_arg", "malloc", "nonnull", NULL
2765 };
2766
2767 /* Put together a list of the black listed attributes that the primary
2768 template is declared with that the specialization is not, in case
2769 it's not apparent from the most recent declaration of the primary. */
2770 pretty_printer str;
2771 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2772 blacklist, &str);
2773
2774 if (!nattrs)
2775 return;
2776
2777 auto_diagnostic_group d;
2778 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2779 "explicit specialization %q#D may be missing attributes",
2780 spec))
2781 inform (DECL_SOURCE_LOCATION (tmpl),
2782 nattrs > 1
2783 ? G_("missing primary template attributes %s")
2784 : G_("missing primary template attribute %s"),
2785 pp_formatted_text (&str));
2786 }
2787
2788 /* Check to see if the function just declared, as indicated in
2789 DECLARATOR, and in DECL, is a specialization of a function
2790 template. We may also discover that the declaration is an explicit
2791 instantiation at this point.
2792
2793 Returns DECL, or an equivalent declaration that should be used
2794 instead if all goes well. Issues an error message if something is
2795 amiss. Returns error_mark_node if the error is not easily
2796 recoverable.
2797
2798 FLAGS is a bitmask consisting of the following flags:
2799
2800 2: The function has a definition.
2801 4: The function is a friend.
2802
2803 The TEMPLATE_COUNT is the number of references to qualifying
2804 template classes that appeared in the name of the function. For
2805 example, in
2806
2807 template <class T> struct S { void f(); };
2808 void S<int>::f();
2809
2810 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2811 classes are not counted in the TEMPLATE_COUNT, so that in
2812
2813 template <class T> struct S {};
2814 template <> struct S<int> { void f(); }
2815 template <> void S<int>::f();
2816
2817 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2818 invalid; there should be no template <>.)
2819
2820 If the function is a specialization, it is marked as such via
2821 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2822 is set up correctly, and it is added to the list of specializations
2823 for that template. */
2824
2825 tree
2826 check_explicit_specialization (tree declarator,
2827 tree decl,
2828 int template_count,
2829 int flags,
2830 tree attrlist)
2831 {
2832 int have_def = flags & 2;
2833 int is_friend = flags & 4;
2834 bool is_concept = flags & 8;
2835 int specialization = 0;
2836 int explicit_instantiation = 0;
2837 int member_specialization = 0;
2838 tree ctype = DECL_CLASS_CONTEXT (decl);
2839 tree dname = DECL_NAME (decl);
2840 tmpl_spec_kind tsk;
2841
2842 if (is_friend)
2843 {
2844 if (!processing_specialization)
2845 tsk = tsk_none;
2846 else
2847 tsk = tsk_excessive_parms;
2848 }
2849 else
2850 tsk = current_tmpl_spec_kind (template_count);
2851
2852 switch (tsk)
2853 {
2854 case tsk_none:
2855 if (processing_specialization && !VAR_P (decl))
2856 {
2857 specialization = 1;
2858 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2859 }
2860 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2861 {
2862 if (is_friend)
2863 /* This could be something like:
2864
2865 template <class T> void f(T);
2866 class S { friend void f<>(int); } */
2867 specialization = 1;
2868 else
2869 {
2870 /* This case handles bogus declarations like template <>
2871 template <class T> void f<int>(); */
2872
2873 error_at (cp_expr_loc_or_input_loc (declarator),
2874 "template-id %qE in declaration of primary template",
2875 declarator);
2876 return decl;
2877 }
2878 }
2879 break;
2880
2881 case tsk_invalid_member_spec:
2882 /* The error has already been reported in
2883 check_specialization_scope. */
2884 return error_mark_node;
2885
2886 case tsk_invalid_expl_inst:
2887 error ("template parameter list used in explicit instantiation");
2888
2889 /* Fall through. */
2890
2891 case tsk_expl_inst:
2892 if (have_def)
2893 error ("definition provided for explicit instantiation");
2894
2895 explicit_instantiation = 1;
2896 break;
2897
2898 case tsk_excessive_parms:
2899 case tsk_insufficient_parms:
2900 if (tsk == tsk_excessive_parms)
2901 error ("too many template parameter lists in declaration of %qD",
2902 decl);
2903 else if (template_header_count)
2904 error("too few template parameter lists in declaration of %qD", decl);
2905 else
2906 error("explicit specialization of %qD must be introduced by "
2907 "%<template <>%>", decl);
2908
2909 /* Fall through. */
2910 case tsk_expl_spec:
2911 if (is_concept)
2912 error ("explicit specialization declared %<concept%>");
2913
2914 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2915 /* In cases like template<> constexpr bool v = true;
2916 We'll give an error in check_template_variable. */
2917 break;
2918
2919 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2920 if (ctype)
2921 member_specialization = 1;
2922 else
2923 specialization = 1;
2924 break;
2925
2926 case tsk_template:
2927 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2928 {
2929 /* This case handles bogus declarations like template <>
2930 template <class T> void f<int>(); */
2931
2932 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2933 error_at (cp_expr_loc_or_input_loc (declarator),
2934 "template-id %qE in declaration of primary template",
2935 declarator);
2936 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2937 {
2938 /* Partial specialization of variable template. */
2939 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2940 specialization = 1;
2941 goto ok;
2942 }
2943 else if (cxx_dialect < cxx14)
2944 error_at (cp_expr_loc_or_input_loc (declarator),
2945 "non-type partial specialization %qE "
2946 "is not allowed", declarator);
2947 else
2948 error_at (cp_expr_loc_or_input_loc (declarator),
2949 "non-class, non-variable partial specialization %qE "
2950 "is not allowed", declarator);
2951 return decl;
2952 ok:;
2953 }
2954
2955 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2956 /* This is a specialization of a member template, without
2957 specialization the containing class. Something like:
2958
2959 template <class T> struct S {
2960 template <class U> void f (U);
2961 };
2962 template <> template <class U> void S<int>::f(U) {}
2963
2964 That's a specialization -- but of the entire template. */
2965 specialization = 1;
2966 break;
2967
2968 default:
2969 gcc_unreachable ();
2970 }
2971
2972 if ((specialization || member_specialization)
2973 /* This doesn't apply to variable templates. */
2974 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2975 {
2976 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2977 for (; t; t = TREE_CHAIN (t))
2978 if (TREE_PURPOSE (t))
2979 {
2980 permerror (input_location,
2981 "default argument specified in explicit specialization");
2982 break;
2983 }
2984 }
2985
2986 if (specialization || member_specialization || explicit_instantiation)
2987 {
2988 tree tmpl = NULL_TREE;
2989 tree targs = NULL_TREE;
2990 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2991
2992 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2993 if (!was_template_id)
2994 {
2995 tree fns;
2996
2997 gcc_assert (identifier_p (declarator));
2998 if (ctype)
2999 fns = dname;
3000 else
3001 {
3002 /* If there is no class context, the explicit instantiation
3003 must be at namespace scope. */
3004 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
3005
3006 /* Find the namespace binding, using the declaration
3007 context. */
3008 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3009 LOOK_want::NORMAL, true);
3010 if (fns == error_mark_node)
3011 /* If lookup fails, look for a friend declaration so we can
3012 give a better diagnostic. */
3013 fns = (lookup_qualified_name
3014 (CP_DECL_CONTEXT (decl), dname,
3015 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3016 /*complain*/true));
3017
3018 if (fns == error_mark_node || !is_overloaded_fn (fns))
3019 {
3020 error ("%qD is not a template function", dname);
3021 fns = error_mark_node;
3022 }
3023 }
3024
3025 declarator = lookup_template_function (fns, NULL_TREE);
3026 }
3027
3028 if (declarator == error_mark_node)
3029 return error_mark_node;
3030
3031 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3032 {
3033 if (!explicit_instantiation)
3034 /* A specialization in class scope. This is invalid,
3035 but the error will already have been flagged by
3036 check_specialization_scope. */
3037 return error_mark_node;
3038 else
3039 {
3040 /* It's not valid to write an explicit instantiation in
3041 class scope, e.g.:
3042
3043 class C { template void f(); }
3044
3045 This case is caught by the parser. However, on
3046 something like:
3047
3048 template class C { void f(); };
3049
3050 (which is invalid) we can get here. The error will be
3051 issued later. */
3052 ;
3053 }
3054
3055 return decl;
3056 }
3057 else if (ctype != NULL_TREE
3058 && (identifier_p (TREE_OPERAND (declarator, 0))))
3059 {
3060 // We'll match variable templates in start_decl.
3061 if (VAR_P (decl))
3062 return decl;
3063
3064 /* Find the list of functions in ctype that have the same
3065 name as the declared function. */
3066 tree name = TREE_OPERAND (declarator, 0);
3067
3068 if (constructor_name_p (name, ctype))
3069 {
3070 if (DECL_CONSTRUCTOR_P (decl)
3071 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3072 : !CLASSTYPE_DESTRUCTOR (ctype))
3073 {
3074 /* From [temp.expl.spec]:
3075
3076 If such an explicit specialization for the member
3077 of a class template names an implicitly-declared
3078 special member function (clause _special_), the
3079 program is ill-formed.
3080
3081 Similar language is found in [temp.explicit]. */
3082 error ("specialization of implicitly-declared special member function");
3083 return error_mark_node;
3084 }
3085
3086 name = DECL_NAME (decl);
3087 }
3088
3089 /* For a type-conversion operator, We might be looking for
3090 `operator int' which will be a specialization of
3091 `operator T'. Grab all the conversion operators, and
3092 then select from them. */
3093 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3094 ? conv_op_identifier : name);
3095
3096 if (fns == NULL_TREE)
3097 {
3098 error ("no member function %qD declared in %qT", name, ctype);
3099 return error_mark_node;
3100 }
3101 else
3102 TREE_OPERAND (declarator, 0) = fns;
3103 }
3104
3105 /* Figure out what exactly is being specialized at this point.
3106 Note that for an explicit instantiation, even one for a
3107 member function, we cannot tell a priori whether the
3108 instantiation is for a member template, or just a member
3109 function of a template class. Even if a member template is
3110 being instantiated, the member template arguments may be
3111 elided if they can be deduced from the rest of the
3112 declaration. */
3113 tmpl = determine_specialization (declarator, decl,
3114 &targs,
3115 member_specialization,
3116 template_count,
3117 tsk);
3118
3119 if (!tmpl || tmpl == error_mark_node)
3120 /* We couldn't figure out what this declaration was
3121 specializing. */
3122 return error_mark_node;
3123 else
3124 {
3125 if (TREE_CODE (decl) == FUNCTION_DECL
3126 && DECL_HIDDEN_FRIEND_P (tmpl))
3127 {
3128 auto_diagnostic_group d;
3129 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3130 "friend declaration %qD is not visible to "
3131 "explicit specialization", tmpl))
3132 inform (DECL_SOURCE_LOCATION (tmpl),
3133 "friend declaration here");
3134 }
3135 else if (!ctype && !is_friend
3136 && CP_DECL_CONTEXT (decl) == current_namespace)
3137 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3138
3139 tree gen_tmpl = most_general_template (tmpl);
3140
3141 if (explicit_instantiation)
3142 {
3143 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3144 is done by do_decl_instantiation later. */
3145
3146 int arg_depth = TMPL_ARGS_DEPTH (targs);
3147 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3148
3149 if (arg_depth > parm_depth)
3150 {
3151 /* If TMPL is not the most general template (for
3152 example, if TMPL is a friend template that is
3153 injected into namespace scope), then there will
3154 be too many levels of TARGS. Remove some of them
3155 here. */
3156 int i;
3157 tree new_targs;
3158
3159 new_targs = make_tree_vec (parm_depth);
3160 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3161 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3162 = TREE_VEC_ELT (targs, i);
3163 targs = new_targs;
3164 }
3165
3166 return instantiate_template (tmpl, targs, tf_error);
3167 }
3168
3169 /* If we thought that the DECL was a member function, but it
3170 turns out to be specializing a static member function,
3171 make DECL a static member function as well. */
3172 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3173 && DECL_STATIC_FUNCTION_P (tmpl)
3174 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3175 revert_static_member_fn (decl);
3176
3177 /* If this is a specialization of a member template of a
3178 template class, we want to return the TEMPLATE_DECL, not
3179 the specialization of it. */
3180 if (tsk == tsk_template && !was_template_id)
3181 {
3182 tree result = DECL_TEMPLATE_RESULT (tmpl);
3183 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3184 DECL_INITIAL (result) = NULL_TREE;
3185 if (have_def)
3186 {
3187 tree parm;
3188 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3189 DECL_SOURCE_LOCATION (result)
3190 = DECL_SOURCE_LOCATION (decl);
3191 /* We want to use the argument list specified in the
3192 definition, not in the original declaration. */
3193 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3194 for (parm = DECL_ARGUMENTS (result); parm;
3195 parm = DECL_CHAIN (parm))
3196 DECL_CONTEXT (parm) = result;
3197 }
3198 return register_specialization (tmpl, gen_tmpl, targs,
3199 is_friend, 0);
3200 }
3201
3202 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3203 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3204
3205 if (was_template_id)
3206 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3207
3208 /* Inherit default function arguments from the template
3209 DECL is specializing. */
3210 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3211 copy_default_args_to_explicit_spec (decl);
3212
3213 /* This specialization has the same protection as the
3214 template it specializes. */
3215 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3216 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3217
3218 /* 7.1.1-1 [dcl.stc]
3219
3220 A storage-class-specifier shall not be specified in an
3221 explicit specialization...
3222
3223 The parser rejects these, so unless action is taken here,
3224 explicit function specializations will always appear with
3225 global linkage.
3226
3227 The action recommended by the C++ CWG in response to C++
3228 defect report 605 is to make the storage class and linkage
3229 of the explicit specialization match the templated function:
3230
3231 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3232 */
3233 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3234 {
3235 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3236 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3237
3238 /* A concept cannot be specialized. */
3239 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3240 {
3241 error ("explicit specialization of function concept %qD",
3242 gen_tmpl);
3243 return error_mark_node;
3244 }
3245
3246 /* This specialization has the same linkage and visibility as
3247 the function template it specializes. */
3248 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3249 if (! TREE_PUBLIC (decl))
3250 {
3251 DECL_INTERFACE_KNOWN (decl) = 1;
3252 DECL_NOT_REALLY_EXTERN (decl) = 1;
3253 }
3254 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3255 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3256 {
3257 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3258 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3259 }
3260 }
3261
3262 /* If DECL is a friend declaration, declared using an
3263 unqualified name, the namespace associated with DECL may
3264 have been set incorrectly. For example, in:
3265
3266 template <typename T> void f(T);
3267 namespace N {
3268 struct S { friend void f<int>(int); }
3269 }
3270
3271 we will have set the DECL_CONTEXT for the friend
3272 declaration to N, rather than to the global namespace. */
3273 if (DECL_NAMESPACE_SCOPE_P (decl))
3274 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3275
3276 if (is_friend && !have_def)
3277 /* This is not really a declaration of a specialization.
3278 It's just the name of an instantiation. But, it's not
3279 a request for an instantiation, either. */
3280 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3281 else if (TREE_CODE (decl) == FUNCTION_DECL)
3282 /* A specialization is not necessarily COMDAT. */
3283 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3284 && DECL_DECLARED_INLINE_P (decl));
3285 else if (VAR_P (decl))
3286 DECL_COMDAT (decl) = false;
3287
3288 /* If this is a full specialization, register it so that we can find
3289 it again. Partial specializations will be registered in
3290 process_partial_specialization. */
3291 if (!processing_template_decl)
3292 {
3293 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3294
3295 decl = register_specialization (decl, gen_tmpl, targs,
3296 is_friend, 0);
3297 }
3298
3299
3300 /* A 'structor should already have clones. */
3301 gcc_assert (decl == error_mark_node
3302 || variable_template_p (tmpl)
3303 || !(DECL_CONSTRUCTOR_P (decl)
3304 || DECL_DESTRUCTOR_P (decl))
3305 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3306 }
3307 }
3308
3309 return decl;
3310 }
3311
3312 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3313 parameters. These are represented in the same format used for
3314 DECL_TEMPLATE_PARMS. */
3315
3316 int
3317 comp_template_parms (const_tree parms1, const_tree parms2)
3318 {
3319 const_tree p1;
3320 const_tree p2;
3321
3322 if (parms1 == parms2)
3323 return 1;
3324
3325 for (p1 = parms1, p2 = parms2;
3326 p1 != NULL_TREE && p2 != NULL_TREE;
3327 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3328 {
3329 tree t1 = TREE_VALUE (p1);
3330 tree t2 = TREE_VALUE (p2);
3331 int i;
3332
3333 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3334 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3335
3336 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3337 return 0;
3338
3339 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3340 {
3341 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3342 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3343
3344 /* If either of the template parameters are invalid, assume
3345 they match for the sake of error recovery. */
3346 if (error_operand_p (parm1) || error_operand_p (parm2))
3347 return 1;
3348
3349 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3350 return 0;
3351
3352 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3353 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3354 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3355 continue;
3356 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3357 return 0;
3358 }
3359 }
3360
3361 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3362 /* One set of parameters has more parameters lists than the
3363 other. */
3364 return 0;
3365
3366 return 1;
3367 }
3368
3369 /* Returns true if two template parameters are declared with
3370 equivalent constraints. */
3371
3372 static bool
3373 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3374 {
3375 tree req1 = TREE_TYPE (parm1);
3376 tree req2 = TREE_TYPE (parm2);
3377 if (!req1 != !req2)
3378 return false;
3379 if (req1)
3380 return cp_tree_equal (req1, req2);
3381 return true;
3382 }
3383
3384 /* Returns true when two template parameters are equivalent. */
3385
3386 static bool
3387 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3388 {
3389 tree decl1 = TREE_VALUE (parm1);
3390 tree decl2 = TREE_VALUE (parm2);
3391
3392 /* If either of the template parameters are invalid, assume
3393 they match for the sake of error recovery. */
3394 if (error_operand_p (decl1) || error_operand_p (decl2))
3395 return true;
3396
3397 /* ... they declare parameters of the same kind. */
3398 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3399 return false;
3400
3401 /* ... one parameter was introduced by a parameter declaration, then
3402 both are. This case arises as a result of eagerly rewriting declarations
3403 during parsing. */
3404 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3405 return false;
3406
3407 /* ... if either declares a pack, they both do. */
3408 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3409 return false;
3410
3411 if (TREE_CODE (decl1) == PARM_DECL)
3412 {
3413 /* ... if they declare non-type parameters, the types are equivalent. */
3414 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3415 return false;
3416 }
3417 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3418 {
3419 /* ... if they declare template template parameters, their template
3420 parameter lists are equivalent. */
3421 if (!template_heads_equivalent_p (decl1, decl2))
3422 return false;
3423 }
3424
3425 /* ... if they are declared with a qualified-concept name, they both
3426 are, and those names are equivalent. */
3427 return template_parameter_constraints_equivalent_p (parm1, parm2);
3428 }
3429
3430 /* Returns true if two template parameters lists are equivalent.
3431 Two template parameter lists are equivalent if they have the
3432 same length and their corresponding parameters are equivalent.
3433
3434 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3435 data structure returned by DECL_TEMPLATE_PARMS.
3436
3437 This is generally the same implementation as comp_template_parms
3438 except that it also the concept names and arguments used to
3439 introduce parameters. */
3440
3441 static bool
3442 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3443 {
3444 if (parms1 == parms2)
3445 return true;
3446
3447 const_tree p1 = parms1;
3448 const_tree p2 = parms2;
3449 while (p1 != NULL_TREE && p2 != NULL_TREE)
3450 {
3451 tree list1 = TREE_VALUE (p1);
3452 tree list2 = TREE_VALUE (p2);
3453
3454 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3455 return 0;
3456
3457 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3458 {
3459 tree parm1 = TREE_VEC_ELT (list1, i);
3460 tree parm2 = TREE_VEC_ELT (list2, i);
3461 if (!template_parameters_equivalent_p (parm1, parm2))
3462 return false;
3463 }
3464
3465 p1 = TREE_CHAIN (p1);
3466 p2 = TREE_CHAIN (p2);
3467 }
3468
3469 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3470 return false;
3471
3472 return true;
3473 }
3474
3475 /* Return true if the requires-clause of the template parameter lists are
3476 equivalent and false otherwise. */
3477 static bool
3478 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3479 {
3480 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3481 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3482 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3483 return false;
3484 if (!cp_tree_equal (req1, req2))
3485 return false;
3486 return true;
3487 }
3488
3489 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3490 Two template heads are equivalent if their template parameter
3491 lists are equivalent and their requires clauses are equivalent.
3492
3493 In pre-C++20, this is equivalent to calling comp_template_parms
3494 for the template parameters of TMPL1 and TMPL2. */
3495
3496 bool
3497 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3498 {
3499 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3500 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3501
3502 /* Don't change the matching rules for pre-C++20. */
3503 if (cxx_dialect < cxx20)
3504 return comp_template_parms (parms1, parms2);
3505
3506 /* ... have the same number of template parameters, and their
3507 corresponding parameters are equivalent. */
3508 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3509 return false;
3510
3511 /* ... if either has a requires-clause, they both do and their
3512 corresponding constraint-expressions are equivalent. */
3513 return template_requirements_equivalent_p (parms1, parms2);
3514 }
3515
3516 /* Determine whether PARM is a parameter pack. */
3517
3518 bool
3519 template_parameter_pack_p (const_tree parm)
3520 {
3521 /* Determine if we have a non-type template parameter pack. */
3522 if (TREE_CODE (parm) == PARM_DECL)
3523 return (DECL_TEMPLATE_PARM_P (parm)
3524 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3525 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3526 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3527
3528 /* If this is a list of template parameters, we could get a
3529 TYPE_DECL or a TEMPLATE_DECL. */
3530 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3531 parm = TREE_TYPE (parm);
3532
3533 /* Otherwise it must be a type template parameter. */
3534 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3535 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3536 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3537 }
3538
3539 /* Determine if T is a function parameter pack. */
3540
3541 bool
3542 function_parameter_pack_p (const_tree t)
3543 {
3544 if (t && TREE_CODE (t) == PARM_DECL)
3545 return DECL_PACK_P (t);
3546 return false;
3547 }
3548
3549 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3550 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3551
3552 tree
3553 get_function_template_decl (const_tree primary_func_tmpl_inst)
3554 {
3555 if (! primary_func_tmpl_inst
3556 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3557 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3558 return NULL;
3559
3560 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3561 }
3562
3563 /* Return true iff the function parameter PARAM_DECL was expanded
3564 from the function parameter pack PACK. */
3565
3566 bool
3567 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3568 {
3569 if (DECL_ARTIFICIAL (param_decl)
3570 || !function_parameter_pack_p (pack))
3571 return false;
3572
3573 /* The parameter pack and its pack arguments have the same
3574 DECL_PARM_INDEX. */
3575 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3576 }
3577
3578 /* Determine whether ARGS describes a variadic template args list,
3579 i.e., one that is terminated by a template argument pack. */
3580
3581 static bool
3582 template_args_variadic_p (tree args)
3583 {
3584 int nargs;
3585 tree last_parm;
3586
3587 if (args == NULL_TREE)
3588 return false;
3589
3590 args = INNERMOST_TEMPLATE_ARGS (args);
3591 nargs = TREE_VEC_LENGTH (args);
3592
3593 if (nargs == 0)
3594 return false;
3595
3596 last_parm = TREE_VEC_ELT (args, nargs - 1);
3597
3598 return ARGUMENT_PACK_P (last_parm);
3599 }
3600
3601 /* Generate a new name for the parameter pack name NAME (an
3602 IDENTIFIER_NODE) that incorporates its */
3603
3604 static tree
3605 make_ith_pack_parameter_name (tree name, int i)
3606 {
3607 /* Munge the name to include the parameter index. */
3608 #define NUMBUF_LEN 128
3609 char numbuf[NUMBUF_LEN];
3610 char* newname;
3611 int newname_len;
3612
3613 if (name == NULL_TREE)
3614 return name;
3615 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3616 newname_len = IDENTIFIER_LENGTH (name)
3617 + strlen (numbuf) + 2;
3618 newname = (char*)alloca (newname_len);
3619 snprintf (newname, newname_len,
3620 "%s#%i", IDENTIFIER_POINTER (name), i);
3621 return get_identifier (newname);
3622 }
3623
3624 /* Return true if T is a primary function, class or alias template
3625 specialization, not including the template pattern. */
3626
3627 bool
3628 primary_template_specialization_p (const_tree t)
3629 {
3630 if (!t)
3631 return false;
3632
3633 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3634 return (DECL_LANG_SPECIFIC (t)
3635 && DECL_USE_TEMPLATE (t)
3636 && DECL_TEMPLATE_INFO (t)
3637 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3638 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3639 return (CLASSTYPE_TEMPLATE_INFO (t)
3640 && CLASSTYPE_USE_TEMPLATE (t)
3641 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3642 else if (alias_template_specialization_p (t, nt_transparent))
3643 return true;
3644 return false;
3645 }
3646
3647 /* Return true if PARM is a template template parameter. */
3648
3649 bool
3650 template_template_parameter_p (const_tree parm)
3651 {
3652 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3653 }
3654
3655 /* Return true iff PARM is a DECL representing a type template
3656 parameter. */
3657
3658 bool
3659 template_type_parameter_p (const_tree parm)
3660 {
3661 return (parm
3662 && (TREE_CODE (parm) == TYPE_DECL
3663 || TREE_CODE (parm) == TEMPLATE_DECL)
3664 && DECL_TEMPLATE_PARM_P (parm));
3665 }
3666
3667 /* Return the template parameters of T if T is a
3668 primary template instantiation, NULL otherwise. */
3669
3670 tree
3671 get_primary_template_innermost_parameters (const_tree t)
3672 {
3673 tree parms = NULL, template_info = NULL;
3674
3675 if ((template_info = get_template_info (t))
3676 && primary_template_specialization_p (t))
3677 parms = INNERMOST_TEMPLATE_PARMS
3678 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3679
3680 return parms;
3681 }
3682
3683 /* Return the template parameters of the LEVELth level from the full list
3684 of template parameters PARMS. */
3685
3686 tree
3687 get_template_parms_at_level (tree parms, int level)
3688 {
3689 tree p;
3690 if (!parms
3691 || TREE_CODE (parms) != TREE_LIST
3692 || level > TMPL_PARMS_DEPTH (parms))
3693 return NULL_TREE;
3694
3695 for (p = parms; p; p = TREE_CHAIN (p))
3696 if (TMPL_PARMS_DEPTH (p) == level)
3697 return p;
3698
3699 return NULL_TREE;
3700 }
3701
3702 /* Returns the template arguments of T if T is a template instantiation,
3703 NULL otherwise. */
3704
3705 tree
3706 get_template_innermost_arguments (const_tree t)
3707 {
3708 tree args = NULL, template_info = NULL;
3709
3710 if ((template_info = get_template_info (t))
3711 && TI_ARGS (template_info))
3712 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3713
3714 return args;
3715 }
3716
3717 /* Return the argument pack elements of T if T is a template argument pack,
3718 NULL otherwise. */
3719
3720 tree
3721 get_template_argument_pack_elems (const_tree t)
3722 {
3723 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3724 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3725 return NULL;
3726
3727 return ARGUMENT_PACK_ARGS (t);
3728 }
3729
3730 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3731 ARGUMENT_PACK_SELECT represents. */
3732
3733 static tree
3734 argument_pack_select_arg (tree t)
3735 {
3736 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3737 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3738
3739 /* If the selected argument is an expansion E, that most likely means we were
3740 called from gen_elem_of_pack_expansion_instantiation during the
3741 substituting of an argument pack (of which the Ith element is a pack
3742 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3743 In this case, the Ith element resulting from this substituting is going to
3744 be a pack expansion, which pattern is the pattern of E. Let's return the
3745 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3746 resulting pack expansion from it. */
3747 if (PACK_EXPANSION_P (arg))
3748 {
3749 /* Make sure we aren't throwing away arg info. */
3750 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3751 arg = PACK_EXPANSION_PATTERN (arg);
3752 }
3753
3754 return arg;
3755 }
3756
3757
3758 /* True iff FN is a function representing a built-in variadic parameter
3759 pack. */
3760
3761 bool
3762 builtin_pack_fn_p (tree fn)
3763 {
3764 if (!fn
3765 || TREE_CODE (fn) != FUNCTION_DECL
3766 || !DECL_IS_BUILTIN (fn))
3767 return false;
3768
3769 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3770 return true;
3771
3772 return false;
3773 }
3774
3775 /* True iff CALL is a call to a function representing a built-in variadic
3776 parameter pack. */
3777
3778 static bool
3779 builtin_pack_call_p (tree call)
3780 {
3781 if (TREE_CODE (call) != CALL_EXPR)
3782 return false;
3783 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3784 }
3785
3786 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3787
3788 static tree
3789 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3790 tree in_decl)
3791 {
3792 tree ohi = CALL_EXPR_ARG (call, 0);
3793 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3794 false/*fn*/, true/*int_cst*/);
3795
3796 if (value_dependent_expression_p (hi))
3797 {
3798 if (hi != ohi)
3799 {
3800 call = copy_node (call);
3801 CALL_EXPR_ARG (call, 0) = hi;
3802 }
3803 tree ex = make_pack_expansion (call, complain);
3804 tree vec = make_tree_vec (1);
3805 TREE_VEC_ELT (vec, 0) = ex;
3806 return vec;
3807 }
3808 else
3809 {
3810 hi = cxx_constant_value (hi);
3811 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3812
3813 /* Calculate the largest value of len that won't make the size of the vec
3814 overflow an int. The compiler will exceed resource limits long before
3815 this, but it seems a decent place to diagnose. */
3816 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3817
3818 if (len < 0 || len > max)
3819 {
3820 if ((complain & tf_error)
3821 && hi != error_mark_node)
3822 error ("argument to %<__integer_pack%> must be between 0 and %d",
3823 max);
3824 return error_mark_node;
3825 }
3826
3827 tree vec = make_tree_vec (len);
3828
3829 for (int i = 0; i < len; ++i)
3830 TREE_VEC_ELT (vec, i) = size_int (i);
3831
3832 return vec;
3833 }
3834 }
3835
3836 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3837 CALL. */
3838
3839 static tree
3840 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3841 tree in_decl)
3842 {
3843 if (!builtin_pack_call_p (call))
3844 return NULL_TREE;
3845
3846 tree fn = CALL_EXPR_FN (call);
3847
3848 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3849 return expand_integer_pack (call, args, complain, in_decl);
3850
3851 return NULL_TREE;
3852 }
3853
3854 /* Structure used to track the progress of find_parameter_packs_r. */
3855 struct find_parameter_pack_data
3856 {
3857 /* TREE_LIST that will contain all of the parameter packs found by
3858 the traversal. */
3859 tree* parameter_packs;
3860
3861 /* Set of AST nodes that have been visited by the traversal. */
3862 hash_set<tree> *visited;
3863
3864 /* True iff we're making a type pack expansion. */
3865 bool type_pack_expansion_p;
3866 };
3867
3868 /* Identifies all of the argument packs that occur in a template
3869 argument and appends them to the TREE_LIST inside DATA, which is a
3870 find_parameter_pack_data structure. This is a subroutine of
3871 make_pack_expansion and uses_parameter_packs. */
3872 static tree
3873 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3874 {
3875 tree t = *tp;
3876 struct find_parameter_pack_data* ppd =
3877 (struct find_parameter_pack_data*)data;
3878 bool parameter_pack_p = false;
3879
3880 /* Don't look through typedefs; we are interested in whether a
3881 parameter pack is actually written in the expression/type we're
3882 looking at, not the target type. */
3883 if (TYPE_P (t) && typedef_variant_p (t))
3884 {
3885 /* But do look at arguments for an alias template. */
3886 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3887 cp_walk_tree (&TI_ARGS (tinfo),
3888 &find_parameter_packs_r,
3889 ppd, ppd->visited);
3890 *walk_subtrees = 0;
3891 return NULL_TREE;
3892 }
3893
3894 /* Identify whether this is a parameter pack or not. */
3895 switch (TREE_CODE (t))
3896 {
3897 case TEMPLATE_PARM_INDEX:
3898 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3899 parameter_pack_p = true;
3900 break;
3901
3902 case TEMPLATE_TYPE_PARM:
3903 t = TYPE_MAIN_VARIANT (t);
3904 /* FALLTHRU */
3905 case TEMPLATE_TEMPLATE_PARM:
3906 /* If the placeholder appears in the decl-specifier-seq of a function
3907 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3908 is a pack expansion, the invented template parameter is a template
3909 parameter pack. */
3910 if (ppd->type_pack_expansion_p && is_auto (t))
3911 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3912 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3913 parameter_pack_p = true;
3914 break;
3915
3916 case FIELD_DECL:
3917 case PARM_DECL:
3918 if (DECL_PACK_P (t))
3919 {
3920 /* We don't want to walk into the type of a PARM_DECL,
3921 because we don't want to see the type parameter pack. */
3922 *walk_subtrees = 0;
3923 parameter_pack_p = true;
3924 }
3925 break;
3926
3927 case VAR_DECL:
3928 if (DECL_PACK_P (t))
3929 {
3930 /* We don't want to walk into the type of a variadic capture proxy,
3931 because we don't want to see the type parameter pack. */
3932 *walk_subtrees = 0;
3933 parameter_pack_p = true;
3934 }
3935 else if (variable_template_specialization_p (t))
3936 {
3937 cp_walk_tree (&DECL_TI_ARGS (t),
3938 find_parameter_packs_r,
3939 ppd, ppd->visited);
3940 *walk_subtrees = 0;
3941 }
3942 break;
3943
3944 case CALL_EXPR:
3945 if (builtin_pack_call_p (t))
3946 parameter_pack_p = true;
3947 break;
3948
3949 case BASES:
3950 parameter_pack_p = true;
3951 break;
3952 default:
3953 /* Not a parameter pack. */
3954 break;
3955 }
3956
3957 if (parameter_pack_p)
3958 {
3959 /* Add this parameter pack to the list. */
3960 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3961 }
3962
3963 if (TYPE_P (t))
3964 cp_walk_tree (&TYPE_CONTEXT (t),
3965 &find_parameter_packs_r, ppd, ppd->visited);
3966
3967 /* This switch statement will return immediately if we don't find a
3968 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3969 switch (TREE_CODE (t))
3970 {
3971 case BOUND_TEMPLATE_TEMPLATE_PARM:
3972 /* Check the template itself. */
3973 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3974 &find_parameter_packs_r, ppd, ppd->visited);
3975 return NULL_TREE;
3976
3977 case DECL_EXPR:
3978 {
3979 tree decl = DECL_EXPR_DECL (t);
3980 /* Ignore the declaration of a capture proxy for a parameter pack. */
3981 if (is_capture_proxy (decl))
3982 *walk_subtrees = 0;
3983 if (is_typedef_decl (decl))
3984 /* Since we stop at typedefs above, we need to look through them at
3985 the point of the DECL_EXPR. */
3986 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3987 &find_parameter_packs_r, ppd, ppd->visited);
3988 return NULL_TREE;
3989 }
3990
3991 case TEMPLATE_DECL:
3992 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3993 return NULL_TREE;
3994 cp_walk_tree (&TREE_TYPE (t),
3995 &find_parameter_packs_r, ppd, ppd->visited);
3996 return NULL_TREE;
3997
3998 case TYPE_PACK_EXPANSION:
3999 case EXPR_PACK_EXPANSION:
4000 *walk_subtrees = 0;
4001 return NULL_TREE;
4002
4003 case INTEGER_TYPE:
4004 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4005 ppd, ppd->visited);
4006 *walk_subtrees = 0;
4007 return NULL_TREE;
4008
4009 case IDENTIFIER_NODE:
4010 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4011 ppd->visited);
4012 *walk_subtrees = 0;
4013 return NULL_TREE;
4014
4015 case LAMBDA_EXPR:
4016 {
4017 /* Since we defer implicit capture, look in the parms and body. */
4018 tree fn = lambda_function (t);
4019 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4020 ppd->visited);
4021 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4022 ppd->visited);
4023 return NULL_TREE;
4024 }
4025
4026 case DECLTYPE_TYPE:
4027 {
4028 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4029 type_pack_expansion_p to false so that any placeholders
4030 within the expression don't get marked as parameter packs. */
4031 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4032 ppd->type_pack_expansion_p = false;
4033 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4034 ppd, ppd->visited);
4035 ppd->type_pack_expansion_p = type_pack_expansion_p;
4036 *walk_subtrees = 0;
4037 return NULL_TREE;
4038 }
4039
4040 case IF_STMT:
4041 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4042 ppd, ppd->visited);
4043 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4044 ppd, ppd->visited);
4045 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4046 ppd, ppd->visited);
4047 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4048 *walk_subtrees = 0;
4049 return NULL_TREE;
4050
4051 default:
4052 return NULL_TREE;
4053 }
4054
4055 return NULL_TREE;
4056 }
4057
4058 /* Determines if the expression or type T uses any parameter packs. */
4059 tree
4060 uses_parameter_packs (tree t)
4061 {
4062 tree parameter_packs = NULL_TREE;
4063 struct find_parameter_pack_data ppd;
4064 ppd.parameter_packs = &parameter_packs;
4065 ppd.visited = new hash_set<tree>;
4066 ppd.type_pack_expansion_p = false;
4067 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4068 delete ppd.visited;
4069 return parameter_packs;
4070 }
4071
4072 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4073 representation a base-class initializer into a parameter pack
4074 expansion. If all goes well, the resulting node will be an
4075 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4076 respectively. */
4077 tree
4078 make_pack_expansion (tree arg, tsubst_flags_t complain)
4079 {
4080 tree result;
4081 tree parameter_packs = NULL_TREE;
4082 bool for_types = false;
4083 struct find_parameter_pack_data ppd;
4084
4085 if (!arg || arg == error_mark_node)
4086 return arg;
4087
4088 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4089 {
4090 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4091 class initializer. In this case, the TREE_PURPOSE will be a
4092 _TYPE node (representing the base class expansion we're
4093 initializing) and the TREE_VALUE will be a TREE_LIST
4094 containing the initialization arguments.
4095
4096 The resulting expansion looks somewhat different from most
4097 expansions. Rather than returning just one _EXPANSION, we
4098 return a TREE_LIST whose TREE_PURPOSE is a
4099 TYPE_PACK_EXPANSION containing the bases that will be
4100 initialized. The TREE_VALUE will be identical to the
4101 original TREE_VALUE, which is a list of arguments that will
4102 be passed to each base. We do not introduce any new pack
4103 expansion nodes into the TREE_VALUE (although it is possible
4104 that some already exist), because the TREE_PURPOSE and
4105 TREE_VALUE all need to be expanded together with the same
4106 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4107 resulting TREE_PURPOSE will mention the parameter packs in
4108 both the bases and the arguments to the bases. */
4109 tree purpose;
4110 tree value;
4111 tree parameter_packs = NULL_TREE;
4112
4113 /* Determine which parameter packs will be used by the base
4114 class expansion. */
4115 ppd.visited = new hash_set<tree>;
4116 ppd.parameter_packs = &parameter_packs;
4117 ppd.type_pack_expansion_p = false;
4118 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4119 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4120 &ppd, ppd.visited);
4121
4122 if (parameter_packs == NULL_TREE)
4123 {
4124 if (complain & tf_error)
4125 error ("base initializer expansion %qT contains no parameter packs",
4126 arg);
4127 delete ppd.visited;
4128 return error_mark_node;
4129 }
4130
4131 if (TREE_VALUE (arg) != void_type_node)
4132 {
4133 /* Collect the sets of parameter packs used in each of the
4134 initialization arguments. */
4135 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4136 {
4137 /* Determine which parameter packs will be expanded in this
4138 argument. */
4139 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4140 &ppd, ppd.visited);
4141 }
4142 }
4143
4144 delete ppd.visited;
4145
4146 /* Create the pack expansion type for the base type. */
4147 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4148 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4149 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4150 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4151
4152 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4153 they will rarely be compared to anything. */
4154 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4155
4156 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4157 }
4158
4159 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4160 for_types = true;
4161
4162 /* Build the PACK_EXPANSION_* node. */
4163 result = for_types
4164 ? cxx_make_type (TYPE_PACK_EXPANSION)
4165 : make_node (EXPR_PACK_EXPANSION);
4166 SET_PACK_EXPANSION_PATTERN (result, arg);
4167 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4168 {
4169 /* Propagate type and const-expression information. */
4170 TREE_TYPE (result) = TREE_TYPE (arg);
4171 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4172 /* Mark this read now, since the expansion might be length 0. */
4173 mark_exp_read (arg);
4174 }
4175 else
4176 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4177 they will rarely be compared to anything. */
4178 SET_TYPE_STRUCTURAL_EQUALITY (result);
4179
4180 /* Determine which parameter packs will be expanded. */
4181 ppd.parameter_packs = &parameter_packs;
4182 ppd.visited = new hash_set<tree>;
4183 ppd.type_pack_expansion_p = TYPE_P (arg);
4184 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4185 delete ppd.visited;
4186
4187 /* Make sure we found some parameter packs. */
4188 if (parameter_packs == NULL_TREE)
4189 {
4190 if (complain & tf_error)
4191 {
4192 if (TYPE_P (arg))
4193 error ("expansion pattern %qT contains no parameter packs", arg);
4194 else
4195 error ("expansion pattern %qE contains no parameter packs", arg);
4196 }
4197 return error_mark_node;
4198 }
4199 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4200
4201 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4202
4203 return result;
4204 }
4205
4206 /* Checks T for any "bare" parameter packs, which have not yet been
4207 expanded, and issues an error if any are found. This operation can
4208 only be done on full expressions or types (e.g., an expression
4209 statement, "if" condition, etc.), because we could have expressions like:
4210
4211 foo(f(g(h(args)))...)
4212
4213 where "args" is a parameter pack. check_for_bare_parameter_packs
4214 should not be called for the subexpressions args, h(args),
4215 g(h(args)), or f(g(h(args))), because we would produce erroneous
4216 error messages.
4217
4218 Returns TRUE and emits an error if there were bare parameter packs,
4219 returns FALSE otherwise. */
4220 bool
4221 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4222 {
4223 tree parameter_packs = NULL_TREE;
4224 struct find_parameter_pack_data ppd;
4225
4226 if (!processing_template_decl || !t || t == error_mark_node)
4227 return false;
4228
4229 /* A lambda might use a parameter pack from the containing context. */
4230 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4231 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4232 return false;
4233
4234 if (TREE_CODE (t) == TYPE_DECL)
4235 t = TREE_TYPE (t);
4236
4237 ppd.parameter_packs = &parameter_packs;
4238 ppd.visited = new hash_set<tree>;
4239 ppd.type_pack_expansion_p = false;
4240 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4241 delete ppd.visited;
4242
4243 if (parameter_packs)
4244 {
4245 if (loc == UNKNOWN_LOCATION)
4246 loc = cp_expr_loc_or_input_loc (t);
4247 error_at (loc, "parameter packs not expanded with %<...%>:");
4248 while (parameter_packs)
4249 {
4250 tree pack = TREE_VALUE (parameter_packs);
4251 tree name = NULL_TREE;
4252
4253 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4254 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4255 name = TYPE_NAME (pack);
4256 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4257 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4258 else if (TREE_CODE (pack) == CALL_EXPR)
4259 name = DECL_NAME (CALL_EXPR_FN (pack));
4260 else
4261 name = DECL_NAME (pack);
4262
4263 if (name)
4264 inform (loc, " %qD", name);
4265 else
4266 inform (loc, " %s", "<anonymous>");
4267
4268 parameter_packs = TREE_CHAIN (parameter_packs);
4269 }
4270
4271 return true;
4272 }
4273
4274 return false;
4275 }
4276
4277 /* Expand any parameter packs that occur in the template arguments in
4278 ARGS. */
4279 tree
4280 expand_template_argument_pack (tree args)
4281 {
4282 if (args == error_mark_node)
4283 return error_mark_node;
4284
4285 tree result_args = NULL_TREE;
4286 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4287 int num_result_args = -1;
4288 int non_default_args_count = -1;
4289
4290 /* First, determine if we need to expand anything, and the number of
4291 slots we'll need. */
4292 for (in_arg = 0; in_arg < nargs; ++in_arg)
4293 {
4294 tree arg = TREE_VEC_ELT (args, in_arg);
4295 if (arg == NULL_TREE)
4296 return args;
4297 if (ARGUMENT_PACK_P (arg))
4298 {
4299 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4300 if (num_result_args < 0)
4301 num_result_args = in_arg + num_packed;
4302 else
4303 num_result_args += num_packed;
4304 }
4305 else
4306 {
4307 if (num_result_args >= 0)
4308 num_result_args++;
4309 }
4310 }
4311
4312 /* If no expansion is necessary, we're done. */
4313 if (num_result_args < 0)
4314 return args;
4315
4316 /* Expand arguments. */
4317 result_args = make_tree_vec (num_result_args);
4318 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4319 non_default_args_count =
4320 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4321 for (in_arg = 0; in_arg < nargs; ++in_arg)
4322 {
4323 tree arg = TREE_VEC_ELT (args, in_arg);
4324 if (ARGUMENT_PACK_P (arg))
4325 {
4326 tree packed = ARGUMENT_PACK_ARGS (arg);
4327 int i, num_packed = TREE_VEC_LENGTH (packed);
4328 for (i = 0; i < num_packed; ++i, ++out_arg)
4329 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4330 if (non_default_args_count > 0)
4331 non_default_args_count += num_packed - 1;
4332 }
4333 else
4334 {
4335 TREE_VEC_ELT (result_args, out_arg) = arg;
4336 ++out_arg;
4337 }
4338 }
4339 if (non_default_args_count >= 0)
4340 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4341 return result_args;
4342 }
4343
4344 /* Checks if DECL shadows a template parameter.
4345
4346 [temp.local]: A template-parameter shall not be redeclared within its
4347 scope (including nested scopes).
4348
4349 Emits an error and returns TRUE if the DECL shadows a parameter,
4350 returns FALSE otherwise. */
4351
4352 bool
4353 check_template_shadow (tree decl)
4354 {
4355 tree olddecl;
4356
4357 /* If we're not in a template, we can't possibly shadow a template
4358 parameter. */
4359 if (!current_template_parms)
4360 return true;
4361
4362 /* Figure out what we're shadowing. */
4363 decl = OVL_FIRST (decl);
4364 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4365
4366 /* If there's no previous binding for this name, we're not shadowing
4367 anything, let alone a template parameter. */
4368 if (!olddecl)
4369 return true;
4370
4371 /* If we're not shadowing a template parameter, we're done. Note
4372 that OLDDECL might be an OVERLOAD (or perhaps even an
4373 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4374 node. */
4375 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4376 return true;
4377
4378 /* We check for decl != olddecl to avoid bogus errors for using a
4379 name inside a class. We check TPFI to avoid duplicate errors for
4380 inline member templates. */
4381 if (decl == olddecl
4382 || (DECL_TEMPLATE_PARM_P (decl)
4383 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4384 return true;
4385
4386 /* Don't complain about the injected class name, as we've already
4387 complained about the class itself. */
4388 if (DECL_SELF_REFERENCE_P (decl))
4389 return false;
4390
4391 if (DECL_TEMPLATE_PARM_P (decl))
4392 error ("declaration of template parameter %q+D shadows "
4393 "template parameter", decl);
4394 else
4395 error ("declaration of %q+#D shadows template parameter", decl);
4396 inform (DECL_SOURCE_LOCATION (olddecl),
4397 "template parameter %qD declared here", olddecl);
4398 return false;
4399 }
4400
4401 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4402 ORIG_LEVEL, DECL, and TYPE. */
4403
4404 static tree
4405 build_template_parm_index (int index,
4406 int level,
4407 int orig_level,
4408 tree decl,
4409 tree type)
4410 {
4411 tree t = make_node (TEMPLATE_PARM_INDEX);
4412 TEMPLATE_PARM_IDX (t) = index;
4413 TEMPLATE_PARM_LEVEL (t) = level;
4414 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4415 TEMPLATE_PARM_DECL (t) = decl;
4416 TREE_TYPE (t) = type;
4417 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4418 TREE_READONLY (t) = TREE_READONLY (decl);
4419
4420 return t;
4421 }
4422
4423 /* Find the canonical type parameter for the given template type
4424 parameter. Returns the canonical type parameter, which may be TYPE
4425 if no such parameter existed. */
4426
4427 static tree
4428 canonical_type_parameter (tree type)
4429 {
4430 int idx = TEMPLATE_TYPE_IDX (type);
4431
4432 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4433
4434 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4435 vec_safe_grow_cleared (canonical_template_parms, idx + 1, true);
4436
4437 for (tree list = (*canonical_template_parms)[idx];
4438 list; list = TREE_CHAIN (list))
4439 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4440 return TREE_VALUE (list);
4441
4442 (*canonical_template_parms)[idx]
4443 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4444 return type;
4445 }
4446
4447 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4448 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4449 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4450 new one is created. */
4451
4452 static tree
4453 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4454 tsubst_flags_t complain)
4455 {
4456 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4457 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4458 != TEMPLATE_PARM_LEVEL (index) - levels)
4459 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4460 {
4461 tree orig_decl = TEMPLATE_PARM_DECL (index);
4462
4463 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4464 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4465 type);
4466 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4467 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4468 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4469 DECL_ARTIFICIAL (decl) = 1;
4470 SET_DECL_TEMPLATE_PARM_P (decl);
4471
4472 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4473 TEMPLATE_PARM_LEVEL (index) - levels,
4474 TEMPLATE_PARM_ORIG_LEVEL (index),
4475 decl, type);
4476 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4477 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4478 = TEMPLATE_PARM_PARAMETER_PACK (index);
4479
4480 /* Template template parameters need this. */
4481 tree inner = decl;
4482 if (TREE_CODE (decl) == TEMPLATE_DECL)
4483 {
4484 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4485 TYPE_DECL, DECL_NAME (decl), type);
4486 DECL_TEMPLATE_RESULT (decl) = inner;
4487 DECL_ARTIFICIAL (inner) = true;
4488 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4489 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4490 }
4491
4492 /* Attach the TPI to the decl. */
4493 if (TREE_CODE (inner) == TYPE_DECL)
4494 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4495 else
4496 DECL_INITIAL (decl) = tpi;
4497 }
4498
4499 return TEMPLATE_PARM_DESCENDANTS (index);
4500 }
4501
4502 /* Process information from new template parameter PARM and append it
4503 to the LIST being built. This new parameter is a non-type
4504 parameter iff IS_NON_TYPE is true. This new parameter is a
4505 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4506 is in PARM_LOC. */
4507
4508 tree
4509 process_template_parm (tree list, location_t parm_loc, tree parm,
4510 bool is_non_type, bool is_parameter_pack)
4511 {
4512 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4513 tree prev = NULL_TREE;
4514 int idx = 0;
4515
4516 if (list)
4517 {
4518 prev = tree_last (list);
4519
4520 tree p = TREE_VALUE (prev);
4521 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4522 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4523 else if (TREE_CODE (p) == PARM_DECL)
4524 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4525
4526 ++idx;
4527 }
4528
4529 tree decl = NULL_TREE;
4530 tree defval = TREE_PURPOSE (parm);
4531 tree constr = TREE_TYPE (parm);
4532
4533 if (is_non_type)
4534 {
4535 parm = TREE_VALUE (parm);
4536
4537 SET_DECL_TEMPLATE_PARM_P (parm);
4538
4539 if (TREE_TYPE (parm) != error_mark_node)
4540 {
4541 /* [temp.param]
4542
4543 The top-level cv-qualifiers on the template-parameter are
4544 ignored when determining its type. */
4545 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4546 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4547 TREE_TYPE (parm) = error_mark_node;
4548 else if (uses_parameter_packs (TREE_TYPE (parm))
4549 && !is_parameter_pack
4550 /* If we're in a nested template parameter list, the template
4551 template parameter could be a parameter pack. */
4552 && processing_template_parmlist == 1)
4553 {
4554 /* This template parameter is not a parameter pack, but it
4555 should be. Complain about "bare" parameter packs. */
4556 check_for_bare_parameter_packs (TREE_TYPE (parm));
4557
4558 /* Recover by calling this a parameter pack. */
4559 is_parameter_pack = true;
4560 }
4561 }
4562
4563 /* A template parameter is not modifiable. */
4564 TREE_CONSTANT (parm) = 1;
4565 TREE_READONLY (parm) = 1;
4566 decl = build_decl (parm_loc,
4567 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4568 TREE_CONSTANT (decl) = 1;
4569 TREE_READONLY (decl) = 1;
4570 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4571 = build_template_parm_index (idx, processing_template_decl,
4572 processing_template_decl,
4573 decl, TREE_TYPE (parm));
4574
4575 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4576 = is_parameter_pack;
4577 }
4578 else
4579 {
4580 tree t;
4581 parm = TREE_VALUE (TREE_VALUE (parm));
4582
4583 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4584 {
4585 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4586 /* This is for distinguishing between real templates and template
4587 template parameters */
4588 TREE_TYPE (parm) = t;
4589
4590 /* any_template_parm_r expects to be able to get the targs of a
4591 DECL_TEMPLATE_RESULT. */
4592 tree result = DECL_TEMPLATE_RESULT (parm);
4593 TREE_TYPE (result) = t;
4594 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4595 tree tinfo = build_template_info (parm, args);
4596 retrofit_lang_decl (result);
4597 DECL_TEMPLATE_INFO (result) = tinfo;
4598
4599 decl = parm;
4600 }
4601 else
4602 {
4603 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4604 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4605 decl = build_decl (parm_loc,
4606 TYPE_DECL, parm, t);
4607 }
4608
4609 TYPE_NAME (t) = decl;
4610 TYPE_STUB_DECL (t) = decl;
4611 parm = decl;
4612 TEMPLATE_TYPE_PARM_INDEX (t)
4613 = build_template_parm_index (idx, processing_template_decl,
4614 processing_template_decl,
4615 decl, TREE_TYPE (parm));
4616 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4617 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4618 SET_TYPE_STRUCTURAL_EQUALITY (t);
4619 else
4620 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4621 }
4622 DECL_ARTIFICIAL (decl) = 1;
4623 SET_DECL_TEMPLATE_PARM_P (decl);
4624
4625 /* Build requirements for the type/template parameter.
4626 This must be done after SET_DECL_TEMPLATE_PARM_P or
4627 process_template_parm could fail. */
4628 tree reqs = finish_shorthand_constraint (parm, constr);
4629
4630 decl = pushdecl (decl);
4631 if (!is_non_type)
4632 parm = decl;
4633
4634 /* Build the parameter node linking the parameter declaration,
4635 its default argument (if any), and its constraints (if any). */
4636 parm = build_tree_list (defval, parm);
4637 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4638
4639 if (prev)
4640 TREE_CHAIN (prev) = parm;
4641 else
4642 list = parm;
4643
4644 return list;
4645 }
4646
4647 /* The end of a template parameter list has been reached. Process the
4648 tree list into a parameter vector, converting each parameter into a more
4649 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4650 as PARM_DECLs. */
4651
4652 tree
4653 end_template_parm_list (tree parms)
4654 {
4655 tree saved_parmlist = make_tree_vec (list_length (parms));
4656
4657 /* Pop the dummy parameter level and add the real one. We do not
4658 morph the dummy parameter in place, as it might have been
4659 captured by a (nested) template-template-parm. */
4660 current_template_parms = TREE_CHAIN (current_template_parms);
4661
4662 current_template_parms
4663 = tree_cons (size_int (processing_template_decl),
4664 saved_parmlist, current_template_parms);
4665
4666 for (unsigned ix = 0; parms; ix++)
4667 {
4668 tree parm = parms;
4669 parms = TREE_CHAIN (parms);
4670 TREE_CHAIN (parm) = NULL_TREE;
4671
4672 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4673 }
4674
4675 --processing_template_parmlist;
4676
4677 return saved_parmlist;
4678 }
4679
4680 // Explicitly indicate the end of the template parameter list. We assume
4681 // that the current template parameters have been constructed and/or
4682 // managed explicitly, as when creating new template template parameters
4683 // from a shorthand constraint.
4684 void
4685 end_template_parm_list ()
4686 {
4687 --processing_template_parmlist;
4688 }
4689
4690 /* end_template_decl is called after a template declaration is seen. */
4691
4692 void
4693 end_template_decl (void)
4694 {
4695 reset_specialization ();
4696
4697 if (! processing_template_decl)
4698 return;
4699
4700 /* This matches the pushlevel in begin_template_parm_list. */
4701 finish_scope ();
4702
4703 --processing_template_decl;
4704 current_template_parms = TREE_CHAIN (current_template_parms);
4705 }
4706
4707 /* Takes a TREE_LIST representing a template parameter and convert it
4708 into an argument suitable to be passed to the type substitution
4709 functions. Note that If the TREE_LIST contains an error_mark
4710 node, the returned argument is error_mark_node. */
4711
4712 tree
4713 template_parm_to_arg (tree t)
4714 {
4715
4716 if (t == NULL_TREE
4717 || TREE_CODE (t) != TREE_LIST)
4718 return t;
4719
4720 if (error_operand_p (TREE_VALUE (t)))
4721 return error_mark_node;
4722
4723 t = TREE_VALUE (t);
4724
4725 if (TREE_CODE (t) == TYPE_DECL
4726 || TREE_CODE (t) == TEMPLATE_DECL)
4727 {
4728 t = TREE_TYPE (t);
4729
4730 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4731 {
4732 /* Turn this argument into a TYPE_ARGUMENT_PACK
4733 with a single element, which expands T. */
4734 tree vec = make_tree_vec (1);
4735 if (CHECKING_P)
4736 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4737
4738 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4739
4740 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4741 SET_ARGUMENT_PACK_ARGS (t, vec);
4742 }
4743 }
4744 else
4745 {
4746 t = DECL_INITIAL (t);
4747
4748 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4749 {
4750 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4751 with a single element, which expands T. */
4752 tree vec = make_tree_vec (1);
4753 if (CHECKING_P)
4754 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4755
4756 t = convert_from_reference (t);
4757 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4758
4759 t = make_node (NONTYPE_ARGUMENT_PACK);
4760 SET_ARGUMENT_PACK_ARGS (t, vec);
4761 }
4762 else
4763 t = convert_from_reference (t);
4764 }
4765 return t;
4766 }
4767
4768 /* Given a single level of template parameters (a TREE_VEC), return it
4769 as a set of template arguments. */
4770
4771 tree
4772 template_parms_level_to_args (tree parms)
4773 {
4774 tree a = copy_node (parms);
4775 TREE_TYPE (a) = NULL_TREE;
4776 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4777 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4778
4779 if (CHECKING_P)
4780 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4781
4782 return a;
4783 }
4784
4785 /* Given a set of template parameters, return them as a set of template
4786 arguments. The template parameters are represented as a TREE_VEC, in
4787 the form documented in cp-tree.h for template arguments. */
4788
4789 tree
4790 template_parms_to_args (tree parms)
4791 {
4792 tree header;
4793 tree args = NULL_TREE;
4794 int length = TMPL_PARMS_DEPTH (parms);
4795 int l = length;
4796
4797 /* If there is only one level of template parameters, we do not
4798 create a TREE_VEC of TREE_VECs. Instead, we return a single
4799 TREE_VEC containing the arguments. */
4800 if (length > 1)
4801 args = make_tree_vec (length);
4802
4803 for (header = parms; header; header = TREE_CHAIN (header))
4804 {
4805 tree a = template_parms_level_to_args (TREE_VALUE (header));
4806
4807 if (length > 1)
4808 TREE_VEC_ELT (args, --l) = a;
4809 else
4810 args = a;
4811 }
4812
4813 return args;
4814 }
4815
4816 /* Within the declaration of a template, return the currently active
4817 template parameters as an argument TREE_VEC. */
4818
4819 static tree
4820 current_template_args (void)
4821 {
4822 return template_parms_to_args (current_template_parms);
4823 }
4824
4825 /* Return the fully generic arguments for of TMPL, i.e. what
4826 current_template_args would be while parsing it. */
4827
4828 tree
4829 generic_targs_for (tree tmpl)
4830 {
4831 if (tmpl == NULL_TREE)
4832 return NULL_TREE;
4833 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4834 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4835 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4836 template parameter, it has no TEMPLATE_INFO; for a partial
4837 specialization, it has the arguments for the primary template, and we
4838 want the arguments for the partial specialization. */;
4839 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4840 if (tree ti = get_template_info (result))
4841 return TI_ARGS (ti);
4842 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4843 }
4844
4845 /* Update the declared TYPE by doing any lookups which were thought to be
4846 dependent, but are not now that we know the SCOPE of the declarator. */
4847
4848 tree
4849 maybe_update_decl_type (tree orig_type, tree scope)
4850 {
4851 tree type = orig_type;
4852
4853 if (type == NULL_TREE)
4854 return type;
4855
4856 if (TREE_CODE (orig_type) == TYPE_DECL)
4857 type = TREE_TYPE (type);
4858
4859 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4860 && dependent_type_p (type)
4861 /* Don't bother building up the args in this case. */
4862 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4863 {
4864 /* tsubst in the args corresponding to the template parameters,
4865 including auto if present. Most things will be unchanged, but
4866 make_typename_type and tsubst_qualified_id will resolve
4867 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4868 tree args = current_template_args ();
4869 tree auto_node = type_uses_auto (type);
4870 tree pushed;
4871 if (auto_node)
4872 {
4873 tree auto_vec = make_tree_vec (1);
4874 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4875 args = add_to_template_args (args, auto_vec);
4876 }
4877 pushed = push_scope (scope);
4878 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4879 if (pushed)
4880 pop_scope (scope);
4881 }
4882
4883 if (type == error_mark_node)
4884 return orig_type;
4885
4886 if (TREE_CODE (orig_type) == TYPE_DECL)
4887 {
4888 if (same_type_p (type, TREE_TYPE (orig_type)))
4889 type = orig_type;
4890 else
4891 type = TYPE_NAME (type);
4892 }
4893 return type;
4894 }
4895
4896 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4897 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4898 the new template is a member template. */
4899
4900 static tree
4901 build_template_decl (tree decl, tree parms, bool member_template_p)
4902 {
4903 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4904 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4905 DECL_TEMPLATE_PARMS (tmpl) = parms;
4906 DECL_TEMPLATE_RESULT (tmpl) = decl;
4907 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4908 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4909 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4910 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4911
4912 return tmpl;
4913 }
4914
4915 struct template_parm_data
4916 {
4917 /* The level of the template parameters we are currently
4918 processing. */
4919 int level;
4920
4921 /* The index of the specialization argument we are currently
4922 processing. */
4923 int current_arg;
4924
4925 /* An array whose size is the number of template parameters. The
4926 elements are nonzero if the parameter has been used in any one
4927 of the arguments processed so far. */
4928 int* parms;
4929
4930 /* An array whose size is the number of template arguments. The
4931 elements are nonzero if the argument makes use of template
4932 parameters of this level. */
4933 int* arg_uses_template_parms;
4934 };
4935
4936 /* Subroutine of push_template_decl used to see if each template
4937 parameter in a partial specialization is used in the explicit
4938 argument list. If T is of the LEVEL given in DATA (which is
4939 treated as a template_parm_data*), then DATA->PARMS is marked
4940 appropriately. */
4941
4942 static int
4943 mark_template_parm (tree t, void* data)
4944 {
4945 int level;
4946 int idx;
4947 struct template_parm_data* tpd = (struct template_parm_data*) data;
4948
4949 template_parm_level_and_index (t, &level, &idx);
4950
4951 if (level == tpd->level)
4952 {
4953 tpd->parms[idx] = 1;
4954 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4955 }
4956
4957 /* In C++17 the type of a non-type argument is a deduced context. */
4958 if (cxx_dialect >= cxx17
4959 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4960 for_each_template_parm (TREE_TYPE (t),
4961 &mark_template_parm,
4962 data,
4963 NULL,
4964 /*include_nondeduced_p=*/false);
4965
4966 /* Return zero so that for_each_template_parm will continue the
4967 traversal of the tree; we want to mark *every* template parm. */
4968 return 0;
4969 }
4970
4971 /* Process the partial specialization DECL. */
4972
4973 static tree
4974 process_partial_specialization (tree decl)
4975 {
4976 tree type = TREE_TYPE (decl);
4977 tree tinfo = get_template_info (decl);
4978 tree maintmpl = TI_TEMPLATE (tinfo);
4979 tree specargs = TI_ARGS (tinfo);
4980 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4981 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4982 tree inner_parms;
4983 tree inst;
4984 int nargs = TREE_VEC_LENGTH (inner_args);
4985 int ntparms;
4986 int i;
4987 bool did_error_intro = false;
4988 struct template_parm_data tpd;
4989 struct template_parm_data tpd2;
4990
4991 gcc_assert (current_template_parms);
4992
4993 /* A concept cannot be specialized. */
4994 if (flag_concepts && variable_concept_p (maintmpl))
4995 {
4996 error ("specialization of variable concept %q#D", maintmpl);
4997 return error_mark_node;
4998 }
4999
5000 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5001 ntparms = TREE_VEC_LENGTH (inner_parms);
5002
5003 /* We check that each of the template parameters given in the
5004 partial specialization is used in the argument list to the
5005 specialization. For example:
5006
5007 template <class T> struct S;
5008 template <class T> struct S<T*>;
5009
5010 The second declaration is OK because `T*' uses the template
5011 parameter T, whereas
5012
5013 template <class T> struct S<int>;
5014
5015 is no good. Even trickier is:
5016
5017 template <class T>
5018 struct S1
5019 {
5020 template <class U>
5021 struct S2;
5022 template <class U>
5023 struct S2<T>;
5024 };
5025
5026 The S2<T> declaration is actually invalid; it is a
5027 full-specialization. Of course,
5028
5029 template <class U>
5030 struct S2<T (*)(U)>;
5031
5032 or some such would have been OK. */
5033 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5034 tpd.parms = XALLOCAVEC (int, ntparms);
5035 memset (tpd.parms, 0, sizeof (int) * ntparms);
5036
5037 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5038 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5039 for (i = 0; i < nargs; ++i)
5040 {
5041 tpd.current_arg = i;
5042 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5043 &mark_template_parm,
5044 &tpd,
5045 NULL,
5046 /*include_nondeduced_p=*/false);
5047 }
5048 for (i = 0; i < ntparms; ++i)
5049 if (tpd.parms[i] == 0)
5050 {
5051 /* One of the template parms was not used in a deduced context in the
5052 specialization. */
5053 if (!did_error_intro)
5054 {
5055 error ("template parameters not deducible in "
5056 "partial specialization:");
5057 did_error_intro = true;
5058 }
5059
5060 inform (input_location, " %qD",
5061 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5062 }
5063
5064 if (did_error_intro)
5065 return error_mark_node;
5066
5067 /* [temp.class.spec]
5068
5069 The argument list of the specialization shall not be identical to
5070 the implicit argument list of the primary template. */
5071 tree main_args
5072 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5073 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5074 && (!flag_concepts
5075 || !strictly_subsumes (current_template_constraints (),
5076 main_args, maintmpl)))
5077 {
5078 if (!flag_concepts)
5079 error ("partial specialization %q+D does not specialize "
5080 "any template arguments; to define the primary template, "
5081 "remove the template argument list", decl);
5082 else
5083 error ("partial specialization %q+D does not specialize any "
5084 "template arguments and is not more constrained than "
5085 "the primary template; to define the primary template, "
5086 "remove the template argument list", decl);
5087 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5088 }
5089
5090 /* A partial specialization that replaces multiple parameters of the
5091 primary template with a pack expansion is less specialized for those
5092 parameters. */
5093 if (nargs < DECL_NTPARMS (maintmpl))
5094 {
5095 error ("partial specialization is not more specialized than the "
5096 "primary template because it replaces multiple parameters "
5097 "with a pack expansion");
5098 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5099 /* Avoid crash in process_partial_specialization. */
5100 return decl;
5101 }
5102
5103 else if (nargs > DECL_NTPARMS (maintmpl))
5104 {
5105 error ("too many arguments for partial specialization %qT", type);
5106 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5107 /* Avoid crash below. */
5108 return decl;
5109 }
5110
5111 /* If we aren't in a dependent class, we can actually try deduction. */
5112 else if (tpd.level == 1
5113 /* FIXME we should be able to handle a partial specialization of a
5114 partial instantiation, but currently we can't (c++/41727). */
5115 && TMPL_ARGS_DEPTH (specargs) == 1
5116 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5117 {
5118 auto_diagnostic_group d;
5119 if (permerror (input_location, "partial specialization %qD is not "
5120 "more specialized than", decl))
5121 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5122 maintmpl);
5123 }
5124
5125 /* [temp.class.spec]
5126
5127 A partially specialized non-type argument expression shall not
5128 involve template parameters of the partial specialization except
5129 when the argument expression is a simple identifier.
5130
5131 The type of a template parameter corresponding to a specialized
5132 non-type argument shall not be dependent on a parameter of the
5133 specialization.
5134
5135 Also, we verify that pack expansions only occur at the
5136 end of the argument list. */
5137 tpd2.parms = 0;
5138 for (i = 0; i < nargs; ++i)
5139 {
5140 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5141 tree arg = TREE_VEC_ELT (inner_args, i);
5142 tree packed_args = NULL_TREE;
5143 int j, len = 1;
5144
5145 if (ARGUMENT_PACK_P (arg))
5146 {
5147 /* Extract the arguments from the argument pack. We'll be
5148 iterating over these in the following loop. */
5149 packed_args = ARGUMENT_PACK_ARGS (arg);
5150 len = TREE_VEC_LENGTH (packed_args);
5151 }
5152
5153 for (j = 0; j < len; j++)
5154 {
5155 if (packed_args)
5156 /* Get the Jth argument in the parameter pack. */
5157 arg = TREE_VEC_ELT (packed_args, j);
5158
5159 if (PACK_EXPANSION_P (arg))
5160 {
5161 /* Pack expansions must come at the end of the
5162 argument list. */
5163 if ((packed_args && j < len - 1)
5164 || (!packed_args && i < nargs - 1))
5165 {
5166 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5167 error ("parameter pack argument %qE must be at the "
5168 "end of the template argument list", arg);
5169 else
5170 error ("parameter pack argument %qT must be at the "
5171 "end of the template argument list", arg);
5172 }
5173 }
5174
5175 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5176 /* We only care about the pattern. */
5177 arg = PACK_EXPANSION_PATTERN (arg);
5178
5179 if (/* These first two lines are the `non-type' bit. */
5180 !TYPE_P (arg)
5181 && TREE_CODE (arg) != TEMPLATE_DECL
5182 /* This next two lines are the `argument expression is not just a
5183 simple identifier' condition and also the `specialized
5184 non-type argument' bit. */
5185 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5186 && !((REFERENCE_REF_P (arg)
5187 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5188 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5189 {
5190 if ((!packed_args && tpd.arg_uses_template_parms[i])
5191 || (packed_args && uses_template_parms (arg)))
5192 error_at (cp_expr_loc_or_input_loc (arg),
5193 "template argument %qE involves template "
5194 "parameter(s)", arg);
5195 else
5196 {
5197 /* Look at the corresponding template parameter,
5198 marking which template parameters its type depends
5199 upon. */
5200 tree type = TREE_TYPE (parm);
5201
5202 if (!tpd2.parms)
5203 {
5204 /* We haven't yet initialized TPD2. Do so now. */
5205 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5206 /* The number of parameters here is the number in the
5207 main template, which, as checked in the assertion
5208 above, is NARGS. */
5209 tpd2.parms = XALLOCAVEC (int, nargs);
5210 tpd2.level =
5211 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5212 }
5213
5214 /* Mark the template parameters. But this time, we're
5215 looking for the template parameters of the main
5216 template, not in the specialization. */
5217 tpd2.current_arg = i;
5218 tpd2.arg_uses_template_parms[i] = 0;
5219 memset (tpd2.parms, 0, sizeof (int) * nargs);
5220 for_each_template_parm (type,
5221 &mark_template_parm,
5222 &tpd2,
5223 NULL,
5224 /*include_nondeduced_p=*/false);
5225
5226 if (tpd2.arg_uses_template_parms [i])
5227 {
5228 /* The type depended on some template parameters.
5229 If they are fully specialized in the
5230 specialization, that's OK. */
5231 int j;
5232 int count = 0;
5233 for (j = 0; j < nargs; ++j)
5234 if (tpd2.parms[j] != 0
5235 && tpd.arg_uses_template_parms [j])
5236 ++count;
5237 if (count != 0)
5238 error_n (input_location, count,
5239 "type %qT of template argument %qE depends "
5240 "on a template parameter",
5241 "type %qT of template argument %qE depends "
5242 "on template parameters",
5243 type,
5244 arg);
5245 }
5246 }
5247 }
5248 }
5249 }
5250
5251 /* We should only get here once. */
5252 if (TREE_CODE (decl) == TYPE_DECL)
5253 gcc_assert (!COMPLETE_TYPE_P (type));
5254
5255 // Build the template decl.
5256 tree tmpl = build_template_decl (decl, current_template_parms,
5257 DECL_MEMBER_TEMPLATE_P (maintmpl));
5258 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5259 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5260 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5261
5262 /* Give template template parms a DECL_CONTEXT of the template
5263 for which they are a parameter. */
5264 for (i = 0; i < ntparms; ++i)
5265 {
5266 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5267 if (TREE_CODE (parm) == TEMPLATE_DECL)
5268 DECL_CONTEXT (parm) = tmpl;
5269 }
5270
5271 if (VAR_P (decl))
5272 /* We didn't register this in check_explicit_specialization so we could
5273 wait until the constraints were set. */
5274 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5275 else
5276 associate_classtype_constraints (type);
5277
5278 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5279 = tree_cons (specargs, tmpl,
5280 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5281 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5282
5283 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5284 inst = TREE_CHAIN (inst))
5285 {
5286 tree instance = TREE_VALUE (inst);
5287 if (TYPE_P (instance)
5288 ? (COMPLETE_TYPE_P (instance)
5289 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5290 : DECL_TEMPLATE_INSTANTIATION (instance))
5291 {
5292 tree spec = most_specialized_partial_spec (instance, tf_none);
5293 tree inst_decl = (DECL_P (instance)
5294 ? instance : TYPE_NAME (instance));
5295 if (!spec)
5296 /* OK */;
5297 else if (spec == error_mark_node)
5298 permerror (input_location,
5299 "declaration of %qD ambiguates earlier template "
5300 "instantiation for %qD", decl, inst_decl);
5301 else if (TREE_VALUE (spec) == tmpl)
5302 permerror (input_location,
5303 "partial specialization of %qD after instantiation "
5304 "of %qD", decl, inst_decl);
5305 }
5306 }
5307
5308 return decl;
5309 }
5310
5311 /* PARM is a template parameter of some form; return the corresponding
5312 TEMPLATE_PARM_INDEX. */
5313
5314 static tree
5315 get_template_parm_index (tree parm)
5316 {
5317 if (TREE_CODE (parm) == PARM_DECL
5318 || TREE_CODE (parm) == CONST_DECL)
5319 parm = DECL_INITIAL (parm);
5320 else if (TREE_CODE (parm) == TYPE_DECL
5321 || TREE_CODE (parm) == TEMPLATE_DECL)
5322 parm = TREE_TYPE (parm);
5323 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5324 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5325 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5326 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5327 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5328 return parm;
5329 }
5330
5331 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5332 parameter packs used by the template parameter PARM. */
5333
5334 static void
5335 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5336 {
5337 /* A type parm can't refer to another parm. */
5338 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5339 return;
5340 else if (TREE_CODE (parm) == PARM_DECL)
5341 {
5342 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5343 ppd, ppd->visited);
5344 return;
5345 }
5346
5347 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5348
5349 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5350 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5351 {
5352 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5353 if (template_parameter_pack_p (p))
5354 /* Any packs in the type are expanded by this parameter. */;
5355 else
5356 fixed_parameter_pack_p_1 (p, ppd);
5357 }
5358 }
5359
5360 /* PARM is a template parameter pack. Return any parameter packs used in
5361 its type or the type of any of its template parameters. If there are
5362 any such packs, it will be instantiated into a fixed template parameter
5363 list by partial instantiation rather than be fully deduced. */
5364
5365 tree
5366 fixed_parameter_pack_p (tree parm)
5367 {
5368 /* This can only be true in a member template. */
5369 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5370 return NULL_TREE;
5371 /* This can only be true for a parameter pack. */
5372 if (!template_parameter_pack_p (parm))
5373 return NULL_TREE;
5374 /* A type parm can't refer to another parm. */
5375 if (TREE_CODE (parm) == TYPE_DECL)
5376 return NULL_TREE;
5377
5378 tree parameter_packs = NULL_TREE;
5379 struct find_parameter_pack_data ppd;
5380 ppd.parameter_packs = &parameter_packs;
5381 ppd.visited = new hash_set<tree>;
5382 ppd.type_pack_expansion_p = false;
5383
5384 fixed_parameter_pack_p_1 (parm, &ppd);
5385
5386 delete ppd.visited;
5387 return parameter_packs;
5388 }
5389
5390 /* Check that a template declaration's use of default arguments and
5391 parameter packs is not invalid. Here, PARMS are the template
5392 parameters. IS_PRIMARY is true if DECL is the thing declared by
5393 a primary template. IS_PARTIAL is true if DECL is a partial
5394 specialization.
5395
5396 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5397 function template declaration or a friend class template
5398 declaration. In the function case, 1 indicates a declaration, 2
5399 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5400 emitted for extraneous default arguments.
5401
5402 Returns TRUE if there were no errors found, FALSE otherwise. */
5403
5404 bool
5405 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5406 bool is_partial, int is_friend_decl)
5407 {
5408 const char *msg;
5409 int last_level_to_check;
5410 tree parm_level;
5411 bool no_errors = true;
5412
5413 /* [temp.param]
5414
5415 A default template-argument shall not be specified in a
5416 function template declaration or a function template definition, nor
5417 in the template-parameter-list of the definition of a member of a
5418 class template. */
5419
5420 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5421 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5422 /* You can't have a function template declaration in a local
5423 scope, nor you can you define a member of a class template in a
5424 local scope. */
5425 return true;
5426
5427 if ((TREE_CODE (decl) == TYPE_DECL
5428 && TREE_TYPE (decl)
5429 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5430 || (TREE_CODE (decl) == FUNCTION_DECL
5431 && LAMBDA_FUNCTION_P (decl)))
5432 /* A lambda doesn't have an explicit declaration; don't complain
5433 about the parms of the enclosing class. */
5434 return true;
5435
5436 if (current_class_type
5437 && !TYPE_BEING_DEFINED (current_class_type)
5438 && DECL_LANG_SPECIFIC (decl)
5439 && DECL_DECLARES_FUNCTION_P (decl)
5440 /* If this is either a friend defined in the scope of the class
5441 or a member function. */
5442 && (DECL_FUNCTION_MEMBER_P (decl)
5443 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5444 : DECL_FRIEND_CONTEXT (decl)
5445 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5446 : false)
5447 /* And, if it was a member function, it really was defined in
5448 the scope of the class. */
5449 && (!DECL_FUNCTION_MEMBER_P (decl)
5450 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5451 /* We already checked these parameters when the template was
5452 declared, so there's no need to do it again now. This function
5453 was defined in class scope, but we're processing its body now
5454 that the class is complete. */
5455 return true;
5456
5457 /* Core issue 226 (C++0x only): the following only applies to class
5458 templates. */
5459 if (is_primary
5460 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5461 {
5462 /* [temp.param]
5463
5464 If a template-parameter has a default template-argument, all
5465 subsequent template-parameters shall have a default
5466 template-argument supplied. */
5467 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5468 {
5469 tree inner_parms = TREE_VALUE (parm_level);
5470 int ntparms = TREE_VEC_LENGTH (inner_parms);
5471 int seen_def_arg_p = 0;
5472 int i;
5473
5474 for (i = 0; i < ntparms; ++i)
5475 {
5476 tree parm = TREE_VEC_ELT (inner_parms, i);
5477
5478 if (parm == error_mark_node)
5479 continue;
5480
5481 if (TREE_PURPOSE (parm))
5482 seen_def_arg_p = 1;
5483 else if (seen_def_arg_p
5484 && !template_parameter_pack_p (TREE_VALUE (parm)))
5485 {
5486 error ("no default argument for %qD", TREE_VALUE (parm));
5487 /* For better subsequent error-recovery, we indicate that
5488 there should have been a default argument. */
5489 TREE_PURPOSE (parm) = error_mark_node;
5490 no_errors = false;
5491 }
5492 else if (!is_partial
5493 && !is_friend_decl
5494 /* Don't complain about an enclosing partial
5495 specialization. */
5496 && parm_level == parms
5497 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5498 && i < ntparms - 1
5499 && template_parameter_pack_p (TREE_VALUE (parm))
5500 /* A fixed parameter pack will be partially
5501 instantiated into a fixed length list. */
5502 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5503 {
5504 /* A primary class template, primary variable template
5505 (DR 2032), or alias template can only have one
5506 parameter pack, at the end of the template
5507 parameter list. */
5508
5509 error ("parameter pack %q+D must be at the end of the"
5510 " template parameter list", TREE_VALUE (parm));
5511
5512 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5513 = error_mark_node;
5514 no_errors = false;
5515 }
5516 }
5517 }
5518 }
5519
5520 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5521 || is_partial
5522 || !is_primary
5523 || is_friend_decl)
5524 /* For an ordinary class template, default template arguments are
5525 allowed at the innermost level, e.g.:
5526 template <class T = int>
5527 struct S {};
5528 but, in a partial specialization, they're not allowed even
5529 there, as we have in [temp.class.spec]:
5530
5531 The template parameter list of a specialization shall not
5532 contain default template argument values.
5533
5534 So, for a partial specialization, or for a function template
5535 (in C++98/C++03), we look at all of them. */
5536 ;
5537 else
5538 /* But, for a primary class template that is not a partial
5539 specialization we look at all template parameters except the
5540 innermost ones. */
5541 parms = TREE_CHAIN (parms);
5542
5543 /* Figure out what error message to issue. */
5544 if (is_friend_decl == 2)
5545 msg = G_("default template arguments may not be used in function template "
5546 "friend re-declaration");
5547 else if (is_friend_decl)
5548 msg = G_("default template arguments may not be used in template "
5549 "friend declarations");
5550 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5551 msg = G_("default template arguments may not be used in function templates "
5552 "without %<-std=c++11%> or %<-std=gnu++11%>");
5553 else if (is_partial)
5554 msg = G_("default template arguments may not be used in "
5555 "partial specializations");
5556 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5557 msg = G_("default argument for template parameter for class enclosing %qD");
5558 else
5559 /* Per [temp.param]/9, "A default template-argument shall not be
5560 specified in the template-parameter-lists of the definition of
5561 a member of a class template that appears outside of the member's
5562 class.", thus if we aren't handling a member of a class template
5563 there is no need to examine the parameters. */
5564 return true;
5565
5566 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5567 /* If we're inside a class definition, there's no need to
5568 examine the parameters to the class itself. On the one
5569 hand, they will be checked when the class is defined, and,
5570 on the other, default arguments are valid in things like:
5571 template <class T = double>
5572 struct S { template <class U> void f(U); };
5573 Here the default argument for `S' has no bearing on the
5574 declaration of `f'. */
5575 last_level_to_check = template_class_depth (current_class_type) + 1;
5576 else
5577 /* Check everything. */
5578 last_level_to_check = 0;
5579
5580 for (parm_level = parms;
5581 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5582 parm_level = TREE_CHAIN (parm_level))
5583 {
5584 tree inner_parms = TREE_VALUE (parm_level);
5585 int i;
5586 int ntparms;
5587
5588 ntparms = TREE_VEC_LENGTH (inner_parms);
5589 for (i = 0; i < ntparms; ++i)
5590 {
5591 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5592 continue;
5593
5594 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5595 {
5596 if (msg)
5597 {
5598 no_errors = false;
5599 if (is_friend_decl == 2)
5600 return no_errors;
5601
5602 error (msg, decl);
5603 msg = 0;
5604 }
5605
5606 /* Clear out the default argument so that we are not
5607 confused later. */
5608 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5609 }
5610 }
5611
5612 /* At this point, if we're still interested in issuing messages,
5613 they must apply to classes surrounding the object declared. */
5614 if (msg)
5615 msg = G_("default argument for template parameter for class "
5616 "enclosing %qD");
5617 }
5618
5619 return no_errors;
5620 }
5621
5622 /* Worker for push_template_decl_real, called via
5623 for_each_template_parm. DATA is really an int, indicating the
5624 level of the parameters we are interested in. If T is a template
5625 parameter of that level, return nonzero. */
5626
5627 static int
5628 template_parm_this_level_p (tree t, void* data)
5629 {
5630 int this_level = *(int *)data;
5631 int level;
5632
5633 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5634 level = TEMPLATE_PARM_LEVEL (t);
5635 else
5636 level = TEMPLATE_TYPE_LEVEL (t);
5637 return level == this_level;
5638 }
5639
5640 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5641 DATA is really an int, indicating the innermost outer level of parameters.
5642 If T is a template parameter of that level or further out, return
5643 nonzero. */
5644
5645 static int
5646 template_parm_outer_level (tree t, void *data)
5647 {
5648 int this_level = *(int *)data;
5649 int level;
5650
5651 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5652 level = TEMPLATE_PARM_LEVEL (t);
5653 else
5654 level = TEMPLATE_TYPE_LEVEL (t);
5655 return level <= this_level;
5656 }
5657
5658 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5659 parameters given by current_template_args, or reuses a
5660 previously existing one, if appropriate. Returns the DECL, or an
5661 equivalent one, if it is replaced via a call to duplicate_decls.
5662
5663 If IS_FRIEND is true, DECL is a friend declaration. */
5664
5665 tree
5666 push_template_decl_real (tree decl, bool is_friend)
5667 {
5668 tree tmpl;
5669 tree args;
5670 tree info;
5671 tree ctx;
5672 bool is_primary;
5673 bool is_partial;
5674 int new_template_p = 0;
5675 /* True if the template is a member template, in the sense of
5676 [temp.mem]. */
5677 bool member_template_p = false;
5678
5679 if (decl == error_mark_node || !current_template_parms)
5680 return error_mark_node;
5681
5682 /* See if this is a partial specialization. */
5683 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5684 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5685 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5686 || (VAR_P (decl)
5687 && DECL_LANG_SPECIFIC (decl)
5688 && DECL_TEMPLATE_SPECIALIZATION (decl)
5689 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5690
5691 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5692 is_friend = true;
5693
5694 if (is_friend)
5695 /* For a friend, we want the context of the friend, not
5696 the type of which it is a friend. */
5697 ctx = CP_DECL_CONTEXT (decl);
5698 else if (CP_DECL_CONTEXT (decl)
5699 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5700 /* In the case of a virtual function, we want the class in which
5701 it is defined. */
5702 ctx = CP_DECL_CONTEXT (decl);
5703 else
5704 /* Otherwise, if we're currently defining some class, the DECL
5705 is assumed to be a member of the class. */
5706 ctx = current_scope ();
5707
5708 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5709 ctx = NULL_TREE;
5710
5711 if (!DECL_CONTEXT (decl))
5712 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5713
5714 /* See if this is a primary template. */
5715 if (is_friend && ctx
5716 && uses_template_parms_level (ctx, processing_template_decl))
5717 /* A friend template that specifies a class context, i.e.
5718 template <typename T> friend void A<T>::f();
5719 is not primary. */
5720 is_primary = false;
5721 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5722 is_primary = false;
5723 else
5724 is_primary = template_parm_scope_p ();
5725
5726 if (is_primary)
5727 {
5728 warning (OPT_Wtemplates, "template %qD declared", decl);
5729
5730 if (DECL_CLASS_SCOPE_P (decl))
5731 member_template_p = true;
5732 if (TREE_CODE (decl) == TYPE_DECL
5733 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5734 {
5735 error ("template class without a name");
5736 return error_mark_node;
5737 }
5738 else if (TREE_CODE (decl) == FUNCTION_DECL)
5739 {
5740 if (member_template_p)
5741 {
5742 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5743 error ("member template %qD may not have virt-specifiers", decl);
5744 }
5745 if (DECL_DESTRUCTOR_P (decl))
5746 {
5747 /* [temp.mem]
5748
5749 A destructor shall not be a member template. */
5750 error_at (DECL_SOURCE_LOCATION (decl),
5751 "destructor %qD declared as member template", decl);
5752 return error_mark_node;
5753 }
5754 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5755 && (!prototype_p (TREE_TYPE (decl))
5756 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5757 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5758 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5759 == void_list_node)))
5760 {
5761 /* [basic.stc.dynamic.allocation]
5762
5763 An allocation function can be a function
5764 template. ... Template allocation functions shall
5765 have two or more parameters. */
5766 error ("invalid template declaration of %qD", decl);
5767 return error_mark_node;
5768 }
5769 }
5770 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5771 && CLASS_TYPE_P (TREE_TYPE (decl)))
5772 {
5773 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5774 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5775 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5776 {
5777 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5778 if (TREE_CODE (t) == TYPE_DECL)
5779 t = TREE_TYPE (t);
5780 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5781 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5782 }
5783 }
5784 else if (TREE_CODE (decl) == TYPE_DECL
5785 && TYPE_DECL_ALIAS_P (decl))
5786 /* alias-declaration */
5787 gcc_assert (!DECL_ARTIFICIAL (decl));
5788 else if (VAR_P (decl))
5789 /* C++14 variable template. */;
5790 else if (TREE_CODE (decl) == CONCEPT_DECL)
5791 /* C++20 concept definitions. */;
5792 else
5793 {
5794 error ("template declaration of %q#D", decl);
5795 return error_mark_node;
5796 }
5797 }
5798
5799 /* Check to see that the rules regarding the use of default
5800 arguments are not being violated. We check args for a friend
5801 functions when we know whether it's a definition, introducing
5802 declaration or re-declaration. */
5803 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5804 check_default_tmpl_args (decl, current_template_parms,
5805 is_primary, is_partial, is_friend);
5806
5807 /* Ensure that there are no parameter packs in the type of this
5808 declaration that have not been expanded. */
5809 if (TREE_CODE (decl) == FUNCTION_DECL)
5810 {
5811 /* Check each of the arguments individually to see if there are
5812 any bare parameter packs. */
5813 tree type = TREE_TYPE (decl);
5814 tree arg = DECL_ARGUMENTS (decl);
5815 tree argtype = TYPE_ARG_TYPES (type);
5816
5817 while (arg && argtype)
5818 {
5819 if (!DECL_PACK_P (arg)
5820 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5821 {
5822 /* This is a PARM_DECL that contains unexpanded parameter
5823 packs. We have already complained about this in the
5824 check_for_bare_parameter_packs call, so just replace
5825 these types with ERROR_MARK_NODE. */
5826 TREE_TYPE (arg) = error_mark_node;
5827 TREE_VALUE (argtype) = error_mark_node;
5828 }
5829
5830 arg = DECL_CHAIN (arg);
5831 argtype = TREE_CHAIN (argtype);
5832 }
5833
5834 /* Check for bare parameter packs in the return type and the
5835 exception specifiers. */
5836 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5837 /* Errors were already issued, set return type to int
5838 as the frontend doesn't expect error_mark_node as
5839 the return type. */
5840 TREE_TYPE (type) = integer_type_node;
5841 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5842 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5843 }
5844 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5845 ? DECL_ORIGINAL_TYPE (decl)
5846 : TREE_TYPE (decl)))
5847 {
5848 TREE_TYPE (decl) = error_mark_node;
5849 return error_mark_node;
5850 }
5851
5852 if (is_partial)
5853 return process_partial_specialization (decl);
5854
5855 args = current_template_args ();
5856
5857 if (!ctx
5858 || TREE_CODE (ctx) == FUNCTION_DECL
5859 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5860 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5861 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5862 {
5863 if (DECL_LANG_SPECIFIC (decl)
5864 && DECL_TEMPLATE_INFO (decl)
5865 && DECL_TI_TEMPLATE (decl))
5866 tmpl = DECL_TI_TEMPLATE (decl);
5867 /* If DECL is a TYPE_DECL for a class-template, then there won't
5868 be DECL_LANG_SPECIFIC. The information equivalent to
5869 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5870 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5871 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5872 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5873 {
5874 /* Since a template declaration already existed for this
5875 class-type, we must be redeclaring it here. Make sure
5876 that the redeclaration is valid. */
5877 redeclare_class_template (TREE_TYPE (decl),
5878 current_template_parms,
5879 current_template_constraints ());
5880 /* We don't need to create a new TEMPLATE_DECL; just use the
5881 one we already had. */
5882 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5883 }
5884 else
5885 {
5886 tmpl = build_template_decl (decl, current_template_parms,
5887 member_template_p);
5888 new_template_p = 1;
5889
5890 if (DECL_LANG_SPECIFIC (decl)
5891 && DECL_TEMPLATE_SPECIALIZATION (decl))
5892 {
5893 /* A specialization of a member template of a template
5894 class. */
5895 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5896 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5897 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5898 }
5899 }
5900 }
5901 else
5902 {
5903 tree a, t, current, parms;
5904 int i;
5905 tree tinfo = get_template_info (decl);
5906
5907 if (!tinfo)
5908 {
5909 error ("template definition of non-template %q#D", decl);
5910 return error_mark_node;
5911 }
5912
5913 tmpl = TI_TEMPLATE (tinfo);
5914
5915 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5916 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5917 && DECL_TEMPLATE_SPECIALIZATION (decl)
5918 && DECL_MEMBER_TEMPLATE_P (tmpl))
5919 {
5920 tree new_tmpl;
5921
5922 /* The declaration is a specialization of a member
5923 template, declared outside the class. Therefore, the
5924 innermost template arguments will be NULL, so we
5925 replace them with the arguments determined by the
5926 earlier call to check_explicit_specialization. */
5927 args = DECL_TI_ARGS (decl);
5928
5929 new_tmpl
5930 = build_template_decl (decl, current_template_parms,
5931 member_template_p);
5932 DECL_TI_TEMPLATE (decl) = new_tmpl;
5933 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5934 DECL_TEMPLATE_INFO (new_tmpl)
5935 = build_template_info (tmpl, args);
5936
5937 register_specialization (new_tmpl,
5938 most_general_template (tmpl),
5939 args,
5940 is_friend, 0);
5941 return decl;
5942 }
5943
5944 /* Make sure the template headers we got make sense. */
5945
5946 parms = DECL_TEMPLATE_PARMS (tmpl);
5947 i = TMPL_PARMS_DEPTH (parms);
5948 if (TMPL_ARGS_DEPTH (args) != i)
5949 {
5950 error ("expected %d levels of template parms for %q#D, got %d",
5951 i, decl, TMPL_ARGS_DEPTH (args));
5952 DECL_INTERFACE_KNOWN (decl) = 1;
5953 return error_mark_node;
5954 }
5955 else
5956 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5957 {
5958 a = TMPL_ARGS_LEVEL (args, i);
5959 t = INNERMOST_TEMPLATE_PARMS (parms);
5960
5961 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5962 {
5963 if (current == decl)
5964 error ("got %d template parameters for %q#D",
5965 TREE_VEC_LENGTH (a), decl);
5966 else
5967 error ("got %d template parameters for %q#T",
5968 TREE_VEC_LENGTH (a), current);
5969 error (" but %d required", TREE_VEC_LENGTH (t));
5970 /* Avoid crash in import_export_decl. */
5971 DECL_INTERFACE_KNOWN (decl) = 1;
5972 return error_mark_node;
5973 }
5974
5975 if (current == decl)
5976 current = ctx;
5977 else if (current == NULL_TREE)
5978 /* Can happen in erroneous input. */
5979 break;
5980 else
5981 current = get_containing_scope (current);
5982 }
5983
5984 /* Check that the parms are used in the appropriate qualifying scopes
5985 in the declarator. */
5986 if (!comp_template_args
5987 (TI_ARGS (tinfo),
5988 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5989 {
5990 error ("template arguments to %qD do not match original "
5991 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5992 if (!uses_template_parms (TI_ARGS (tinfo)))
5993 inform (input_location, "use %<template<>%> for"
5994 " an explicit specialization");
5995 /* Avoid crash in import_export_decl. */
5996 DECL_INTERFACE_KNOWN (decl) = 1;
5997 return error_mark_node;
5998 }
5999 }
6000
6001 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl) == decl);
6002
6003 if (new_template_p)
6004 {
6005 /* Push template declarations for global functions and types.
6006 Note that we do not try to push a global template friend
6007 declared in a template class; such a thing may well depend on
6008 the template parameters of the class and we'll push it when
6009 instantiating the befriending class. */
6010 if (!ctx
6011 && !(is_friend && template_class_depth (current_class_type) > 0))
6012 {
6013 tmpl = pushdecl_namespace_level (tmpl, is_friend);
6014 if (tmpl == error_mark_node)
6015 return error_mark_node;
6016
6017 /* Hide template friend classes that haven't been declared yet. */
6018 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
6019 {
6020 DECL_ANTICIPATED (tmpl) = 1;
6021 DECL_FRIEND_P (tmpl) = 1;
6022 }
6023 }
6024 }
6025 else
6026 /* The type may have been completed, or (erroneously) changed. */
6027 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6028
6029 if (is_primary)
6030 {
6031 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6032
6033 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6034
6035 /* Give template template parms a DECL_CONTEXT of the template
6036 for which they are a parameter. */
6037 parms = INNERMOST_TEMPLATE_PARMS (parms);
6038 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6039 {
6040 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6041 if (TREE_CODE (parm) == TEMPLATE_DECL)
6042 DECL_CONTEXT (parm) = tmpl;
6043 }
6044
6045 if (TREE_CODE (decl) == TYPE_DECL
6046 && TYPE_DECL_ALIAS_P (decl))
6047 {
6048 if (tree constr
6049 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6050 {
6051 /* ??? Why don't we do this here for all templates? */
6052 constr = build_constraints (constr, NULL_TREE);
6053 set_constraints (decl, constr);
6054 }
6055 if (complex_alias_template_p (tmpl))
6056 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6057 }
6058 }
6059
6060 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6061 back to its most general template. If TMPL is a specialization,
6062 ARGS may only have the innermost set of arguments. Add the missing
6063 argument levels if necessary. */
6064 if (DECL_TEMPLATE_INFO (tmpl))
6065 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6066
6067 info = build_template_info (tmpl, args);
6068
6069 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6070 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6071 else
6072 {
6073 if (is_primary)
6074 retrofit_lang_decl (decl);
6075 if (DECL_LANG_SPECIFIC (decl)
6076 && !(VAR_OR_FUNCTION_DECL_P (decl)
6077 && DECL_LOCAL_DECL_P (decl)))
6078 DECL_TEMPLATE_INFO (decl) = info;
6079 }
6080
6081 if (flag_implicit_templates
6082 && !is_friend
6083 && TREE_PUBLIC (decl)
6084 && VAR_OR_FUNCTION_DECL_P (decl))
6085 /* Set DECL_COMDAT on template instantiations; if we force
6086 them to be emitted by explicit instantiation,
6087 mark_needed will tell cgraph to do the right thing. */
6088 DECL_COMDAT (decl) = true;
6089
6090 return DECL_TEMPLATE_RESULT (tmpl);
6091 }
6092
6093 tree
6094 push_template_decl (tree decl)
6095 {
6096 return push_template_decl_real (decl, false);
6097 }
6098
6099 /* FN is an inheriting constructor that inherits from the constructor
6100 template INHERITED; turn FN into a constructor template with a matching
6101 template header. */
6102
6103 tree
6104 add_inherited_template_parms (tree fn, tree inherited)
6105 {
6106 tree inner_parms
6107 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6108 inner_parms = copy_node (inner_parms);
6109 tree parms
6110 = tree_cons (size_int (processing_template_decl + 1),
6111 inner_parms, current_template_parms);
6112 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6113 tree args = template_parms_to_args (parms);
6114 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6115 DECL_ARTIFICIAL (tmpl) = true;
6116 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6117 return tmpl;
6118 }
6119
6120 /* Called when a class template TYPE is redeclared with the indicated
6121 template PARMS, e.g.:
6122
6123 template <class T> struct S;
6124 template <class T> struct S {}; */
6125
6126 bool
6127 redeclare_class_template (tree type, tree parms, tree cons)
6128 {
6129 tree tmpl;
6130 tree tmpl_parms;
6131 int i;
6132
6133 if (!TYPE_TEMPLATE_INFO (type))
6134 {
6135 error ("%qT is not a template type", type);
6136 return false;
6137 }
6138
6139 tmpl = TYPE_TI_TEMPLATE (type);
6140 if (!PRIMARY_TEMPLATE_P (tmpl))
6141 /* The type is nested in some template class. Nothing to worry
6142 about here; there are no new template parameters for the nested
6143 type. */
6144 return true;
6145
6146 if (!parms)
6147 {
6148 error ("template specifiers not specified in declaration of %qD",
6149 tmpl);
6150 return false;
6151 }
6152
6153 parms = INNERMOST_TEMPLATE_PARMS (parms);
6154 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6155
6156 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6157 {
6158 error_n (input_location, TREE_VEC_LENGTH (parms),
6159 "redeclared with %d template parameter",
6160 "redeclared with %d template parameters",
6161 TREE_VEC_LENGTH (parms));
6162 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6163 "previous declaration %qD used %d template parameter",
6164 "previous declaration %qD used %d template parameters",
6165 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6166 return false;
6167 }
6168
6169 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6170 {
6171 tree tmpl_parm;
6172 tree parm;
6173 tree tmpl_default;
6174 tree parm_default;
6175
6176 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6177 || TREE_VEC_ELT (parms, i) == error_mark_node)
6178 continue;
6179
6180 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6181 if (error_operand_p (tmpl_parm))
6182 return false;
6183
6184 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6185 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6186 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6187
6188 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6189 TEMPLATE_DECL. */
6190 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6191 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6192 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6193 || (TREE_CODE (tmpl_parm) != PARM_DECL
6194 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6195 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6196 || (TREE_CODE (tmpl_parm) == PARM_DECL
6197 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6198 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6199 {
6200 auto_diagnostic_group d;
6201 error ("template parameter %q+#D", tmpl_parm);
6202 inform (input_location, "redeclared here as %q#D", parm);
6203 return false;
6204 }
6205
6206 /* The parameters can be declared to introduce different
6207 constraints. */
6208 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6209 tree p2 = TREE_VEC_ELT (parms, i);
6210 if (!template_parameter_constraints_equivalent_p (p1, p2))
6211 {
6212 auto_diagnostic_group d;
6213 error ("declaration of template parameter %q+#D with different "
6214 "constraints", parm);
6215 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6216 "original declaration appeared here");
6217 return false;
6218 }
6219
6220 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6221 {
6222 /* We have in [temp.param]:
6223
6224 A template-parameter may not be given default arguments
6225 by two different declarations in the same scope. */
6226 auto_diagnostic_group d;
6227 error_at (input_location, "redefinition of default argument for %q#D", parm);
6228 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6229 "original definition appeared here");
6230 return false;
6231 }
6232
6233 if (parm_default != NULL_TREE)
6234 /* Update the previous template parameters (which are the ones
6235 that will really count) with the new default value. */
6236 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6237 else if (tmpl_default != NULL_TREE)
6238 /* Update the new parameters, too; they'll be used as the
6239 parameters for any members. */
6240 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6241
6242 /* Give each template template parm in this redeclaration a
6243 DECL_CONTEXT of the template for which they are a parameter. */
6244 if (TREE_CODE (parm) == TEMPLATE_DECL)
6245 {
6246 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6247 DECL_CONTEXT (parm) = tmpl;
6248 }
6249
6250 if (TREE_CODE (parm) == TYPE_DECL)
6251 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6252 }
6253
6254 tree ci = get_constraints (tmpl);
6255 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6256 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6257
6258 /* Two classes with different constraints declare different entities. */
6259 if (!cp_tree_equal (req1, req2))
6260 {
6261 auto_diagnostic_group d;
6262 error_at (input_location, "redeclaration %q#D with different "
6263 "constraints", tmpl);
6264 inform (DECL_SOURCE_LOCATION (tmpl),
6265 "original declaration appeared here");
6266 return false;
6267 }
6268
6269 return true;
6270 }
6271
6272 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6273 to be used when the caller has already checked
6274 (processing_template_decl
6275 && !instantiation_dependent_expression_p (expr)
6276 && potential_constant_expression (expr))
6277 and cleared processing_template_decl. */
6278
6279 tree
6280 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6281 {
6282 return tsubst_copy_and_build (expr,
6283 /*args=*/NULL_TREE,
6284 complain,
6285 /*in_decl=*/NULL_TREE,
6286 /*function_p=*/false,
6287 /*integral_constant_expression_p=*/true);
6288 }
6289
6290 /* Simplify EXPR if it is a non-dependent expression. Returns the
6291 (possibly simplified) expression. */
6292
6293 tree
6294 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6295 {
6296 if (expr == NULL_TREE)
6297 return NULL_TREE;
6298
6299 /* If we're in a template, but EXPR isn't value dependent, simplify
6300 it. We're supposed to treat:
6301
6302 template <typename T> void f(T[1 + 1]);
6303 template <typename T> void f(T[2]);
6304
6305 as two declarations of the same function, for example. */
6306 if (processing_template_decl
6307 && is_nondependent_constant_expression (expr))
6308 {
6309 processing_template_decl_sentinel s;
6310 expr = instantiate_non_dependent_expr_internal (expr, complain);
6311 }
6312 return expr;
6313 }
6314
6315 tree
6316 instantiate_non_dependent_expr (tree expr)
6317 {
6318 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6319 }
6320
6321 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6322 an uninstantiated expression. */
6323
6324 tree
6325 instantiate_non_dependent_or_null (tree expr)
6326 {
6327 if (expr == NULL_TREE)
6328 return NULL_TREE;
6329 if (processing_template_decl)
6330 {
6331 if (!is_nondependent_constant_expression (expr))
6332 expr = NULL_TREE;
6333 else
6334 {
6335 processing_template_decl_sentinel s;
6336 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6337 }
6338 }
6339 return expr;
6340 }
6341
6342 /* True iff T is a specialization of a variable template. */
6343
6344 bool
6345 variable_template_specialization_p (tree t)
6346 {
6347 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6348 return false;
6349 tree tmpl = DECL_TI_TEMPLATE (t);
6350 return variable_template_p (tmpl);
6351 }
6352
6353 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6354 template declaration, or a TYPE_DECL for an alias declaration. */
6355
6356 bool
6357 alias_type_or_template_p (tree t)
6358 {
6359 if (t == NULL_TREE)
6360 return false;
6361 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6362 || (TYPE_P (t)
6363 && TYPE_NAME (t)
6364 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6365 || DECL_ALIAS_TEMPLATE_P (t));
6366 }
6367
6368 /* If T is a specialization of an alias template, return it; otherwise return
6369 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6370
6371 tree
6372 alias_template_specialization_p (const_tree t,
6373 bool transparent_typedefs)
6374 {
6375 if (!TYPE_P (t))
6376 return NULL_TREE;
6377
6378 /* It's an alias template specialization if it's an alias and its
6379 TYPE_NAME is a specialization of a primary template. */
6380 if (typedef_variant_p (t))
6381 {
6382 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6383 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6384 return CONST_CAST_TREE (t);
6385 if (transparent_typedefs)
6386 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6387 (TYPE_NAME (t)),
6388 transparent_typedefs);
6389 }
6390
6391 return NULL_TREE;
6392 }
6393
6394 /* An alias template is complex from a SFINAE perspective if a template-id
6395 using that alias can be ill-formed when the expansion is not, as with
6396 the void_t template. We determine this by checking whether the
6397 expansion for the alias template uses all its template parameters. */
6398
6399 struct uses_all_template_parms_data
6400 {
6401 int level;
6402 bool *seen;
6403 };
6404
6405 static int
6406 uses_all_template_parms_r (tree t, void *data_)
6407 {
6408 struct uses_all_template_parms_data &data
6409 = *(struct uses_all_template_parms_data*)data_;
6410 tree idx = get_template_parm_index (t);
6411
6412 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6413 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6414 return 0;
6415 }
6416
6417 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6418
6419 static int
6420 complex_pack_expansion_r (tree t, void *data_)
6421 {
6422 /* An alias template with a pack expansion that expands a pack from the
6423 enclosing class needs to be considered complex, to avoid confusion with
6424 the same pack being used as an argument to the alias's own template
6425 parameter (91966). */
6426 if (!PACK_EXPANSION_P (t))
6427 return 0;
6428 struct uses_all_template_parms_data &data
6429 = *(struct uses_all_template_parms_data*)data_;
6430 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6431 pack = TREE_CHAIN (pack))
6432 {
6433 tree parm_pack = TREE_VALUE (pack);
6434 if (!TEMPLATE_PARM_P (parm_pack))
6435 continue;
6436 int idx, level;
6437 template_parm_level_and_index (parm_pack, &level, &idx);
6438 if (level < data.level)
6439 return 1;
6440 }
6441 return 0;
6442 }
6443
6444 static bool
6445 complex_alias_template_p (const_tree tmpl)
6446 {
6447 /* A renaming alias isn't complex. */
6448 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6449 return false;
6450
6451 /* Any other constrained alias is complex. */
6452 if (get_constraints (tmpl))
6453 return true;
6454
6455 struct uses_all_template_parms_data data;
6456 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6457 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6458 data.level = TMPL_PARMS_DEPTH (parms);
6459 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6460 data.seen = XALLOCAVEC (bool, len);
6461 for (int i = 0; i < len; ++i)
6462 data.seen[i] = false;
6463
6464 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6465 NULL, true, complex_pack_expansion_r))
6466 return true;
6467 for (int i = 0; i < len; ++i)
6468 if (!data.seen[i])
6469 return true;
6470 return false;
6471 }
6472
6473 /* If T is a specialization of a complex alias template with dependent
6474 template-arguments, return it; otherwise return NULL_TREE. If T is a
6475 typedef to such a specialization, return the specialization. */
6476
6477 tree
6478 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6479 {
6480 if (!TYPE_P (t) || !typedef_variant_p (t))
6481 return NULL_TREE;
6482
6483 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6484 if (tinfo
6485 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6486 && (any_dependent_template_arguments_p
6487 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6488 return CONST_CAST_TREE (t);
6489
6490 if (transparent_typedefs)
6491 {
6492 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6493 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6494 }
6495
6496 return NULL_TREE;
6497 }
6498
6499 /* Return the number of innermost template parameters in TMPL. */
6500
6501 static int
6502 num_innermost_template_parms (const_tree tmpl)
6503 {
6504 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6505 return TREE_VEC_LENGTH (parms);
6506 }
6507
6508 /* Return either TMPL or another template that it is equivalent to under DR
6509 1286: An alias that just changes the name of a template is equivalent to
6510 the other template. */
6511
6512 static tree
6513 get_underlying_template (tree tmpl)
6514 {
6515 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6516 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6517 {
6518 /* Determine if the alias is equivalent to an underlying template. */
6519 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6520 /* The underlying type may have been ill-formed. Don't proceed. */
6521 if (!orig_type)
6522 break;
6523 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6524 if (!tinfo)
6525 break;
6526
6527 tree underlying = TI_TEMPLATE (tinfo);
6528 if (!PRIMARY_TEMPLATE_P (underlying)
6529 || (num_innermost_template_parms (tmpl)
6530 != num_innermost_template_parms (underlying)))
6531 break;
6532
6533 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6534 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6535 break;
6536
6537 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6538 it's appropriate to treat a less-constrained alias as equivalent. */
6539 if (!at_least_as_constrained (underlying, tmpl))
6540 break;
6541
6542 /* Alias is equivalent. Strip it and repeat. */
6543 tmpl = underlying;
6544 }
6545
6546 return tmpl;
6547 }
6548
6549 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6550 must be a reference-to-function or a pointer-to-function type, as specified
6551 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6552 and check that the resulting function has external linkage. */
6553
6554 static tree
6555 convert_nontype_argument_function (tree type, tree expr,
6556 tsubst_flags_t complain)
6557 {
6558 tree fns = expr;
6559 tree fn, fn_no_ptr;
6560 linkage_kind linkage;
6561
6562 fn = instantiate_type (type, fns, tf_none);
6563 if (fn == error_mark_node)
6564 return error_mark_node;
6565
6566 if (value_dependent_expression_p (fn))
6567 goto accept;
6568
6569 fn_no_ptr = strip_fnptr_conv (fn);
6570 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6571 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6572 if (BASELINK_P (fn_no_ptr))
6573 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6574
6575 /* [temp.arg.nontype]/1
6576
6577 A template-argument for a non-type, non-template template-parameter
6578 shall be one of:
6579 [...]
6580 -- the address of an object or function with external [C++11: or
6581 internal] linkage. */
6582
6583 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6584 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6585 {
6586 if (complain & tf_error)
6587 {
6588 location_t loc = cp_expr_loc_or_input_loc (expr);
6589 error_at (loc, "%qE is not a valid template argument for type %qT",
6590 expr, type);
6591 if (TYPE_PTR_P (type))
6592 inform (loc, "it must be the address of a function "
6593 "with external linkage");
6594 else
6595 inform (loc, "it must be the name of a function with "
6596 "external linkage");
6597 }
6598 return NULL_TREE;
6599 }
6600
6601 linkage = decl_linkage (fn_no_ptr);
6602 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6603 {
6604 if (complain & tf_error)
6605 {
6606 location_t loc = cp_expr_loc_or_input_loc (expr);
6607 if (cxx_dialect >= cxx11)
6608 error_at (loc, "%qE is not a valid template argument for type "
6609 "%qT because %qD has no linkage",
6610 expr, type, fn_no_ptr);
6611 else
6612 error_at (loc, "%qE is not a valid template argument for type "
6613 "%qT because %qD does not have external linkage",
6614 expr, type, fn_no_ptr);
6615 }
6616 return NULL_TREE;
6617 }
6618
6619 accept:
6620 if (TYPE_REF_P (type))
6621 {
6622 if (REFERENCE_REF_P (fn))
6623 fn = TREE_OPERAND (fn, 0);
6624 else
6625 fn = build_address (fn);
6626 }
6627 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6628 fn = build_nop (type, fn);
6629
6630 return fn;
6631 }
6632
6633 /* Subroutine of convert_nontype_argument.
6634 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6635 Emit an error otherwise. */
6636
6637 static bool
6638 check_valid_ptrmem_cst_expr (tree type, tree expr,
6639 tsubst_flags_t complain)
6640 {
6641 tree orig_expr = expr;
6642 STRIP_NOPS (expr);
6643 if (null_ptr_cst_p (expr))
6644 return true;
6645 if (TREE_CODE (expr) == PTRMEM_CST
6646 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6647 PTRMEM_CST_CLASS (expr)))
6648 return true;
6649 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6650 return true;
6651 if (processing_template_decl
6652 && TREE_CODE (expr) == ADDR_EXPR
6653 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6654 return true;
6655 if (complain & tf_error)
6656 {
6657 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6658 error_at (loc, "%qE is not a valid template argument for type %qT",
6659 orig_expr, type);
6660 if (TREE_CODE (expr) != PTRMEM_CST)
6661 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6662 else
6663 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6664 }
6665 return false;
6666 }
6667
6668 /* Returns TRUE iff the address of OP is value-dependent.
6669
6670 14.6.2.4 [temp.dep.temp]:
6671 A non-integral non-type template-argument is dependent if its type is
6672 dependent or it has either of the following forms
6673 qualified-id
6674 & qualified-id
6675 and contains a nested-name-specifier which specifies a class-name that
6676 names a dependent type.
6677
6678 We generalize this to just say that the address of a member of a
6679 dependent class is value-dependent; the above doesn't cover the
6680 address of a static data member named with an unqualified-id. */
6681
6682 static bool
6683 has_value_dependent_address (tree op)
6684 {
6685 STRIP_ANY_LOCATION_WRAPPER (op);
6686
6687 /* We could use get_inner_reference here, but there's no need;
6688 this is only relevant for template non-type arguments, which
6689 can only be expressed as &id-expression. */
6690 if (DECL_P (op))
6691 {
6692 tree ctx = CP_DECL_CONTEXT (op);
6693 if (TYPE_P (ctx) && dependent_type_p (ctx))
6694 return true;
6695 }
6696
6697 return false;
6698 }
6699
6700 /* The next set of functions are used for providing helpful explanatory
6701 diagnostics for failed overload resolution. Their messages should be
6702 indented by two spaces for consistency with the messages in
6703 call.c */
6704
6705 static int
6706 unify_success (bool /*explain_p*/)
6707 {
6708 return 0;
6709 }
6710
6711 /* Other failure functions should call this one, to provide a single function
6712 for setting a breakpoint on. */
6713
6714 static int
6715 unify_invalid (bool /*explain_p*/)
6716 {
6717 return 1;
6718 }
6719
6720 static int
6721 unify_parameter_deduction_failure (bool explain_p, tree parm)
6722 {
6723 if (explain_p)
6724 inform (input_location,
6725 " couldn%'t deduce template parameter %qD", parm);
6726 return unify_invalid (explain_p);
6727 }
6728
6729 static int
6730 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6731 {
6732 if (explain_p)
6733 inform (input_location,
6734 " types %qT and %qT have incompatible cv-qualifiers",
6735 parm, arg);
6736 return unify_invalid (explain_p);
6737 }
6738
6739 static int
6740 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6741 {
6742 if (explain_p)
6743 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6744 return unify_invalid (explain_p);
6745 }
6746
6747 static int
6748 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6749 {
6750 if (explain_p)
6751 inform (input_location,
6752 " template parameter %qD is not a parameter pack, but "
6753 "argument %qD is",
6754 parm, arg);
6755 return unify_invalid (explain_p);
6756 }
6757
6758 static int
6759 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6760 {
6761 if (explain_p)
6762 inform (input_location,
6763 " template argument %qE does not match "
6764 "pointer-to-member constant %qE",
6765 arg, parm);
6766 return unify_invalid (explain_p);
6767 }
6768
6769 static int
6770 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6771 {
6772 if (explain_p)
6773 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6774 return unify_invalid (explain_p);
6775 }
6776
6777 static int
6778 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6779 {
6780 if (explain_p)
6781 inform (input_location,
6782 " inconsistent parameter pack deduction with %qT and %qT",
6783 old_arg, new_arg);
6784 return unify_invalid (explain_p);
6785 }
6786
6787 static int
6788 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6789 {
6790 if (explain_p)
6791 {
6792 if (TYPE_P (parm))
6793 inform (input_location,
6794 " deduced conflicting types for parameter %qT (%qT and %qT)",
6795 parm, first, second);
6796 else
6797 inform (input_location,
6798 " deduced conflicting values for non-type parameter "
6799 "%qE (%qE and %qE)", parm, first, second);
6800 }
6801 return unify_invalid (explain_p);
6802 }
6803
6804 static int
6805 unify_vla_arg (bool explain_p, tree arg)
6806 {
6807 if (explain_p)
6808 inform (input_location,
6809 " variable-sized array type %qT is not "
6810 "a valid template argument",
6811 arg);
6812 return unify_invalid (explain_p);
6813 }
6814
6815 static int
6816 unify_method_type_error (bool explain_p, tree arg)
6817 {
6818 if (explain_p)
6819 inform (input_location,
6820 " member function type %qT is not a valid template argument",
6821 arg);
6822 return unify_invalid (explain_p);
6823 }
6824
6825 static int
6826 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6827 {
6828 if (explain_p)
6829 {
6830 if (least_p)
6831 inform_n (input_location, wanted,
6832 " candidate expects at least %d argument, %d provided",
6833 " candidate expects at least %d arguments, %d provided",
6834 wanted, have);
6835 else
6836 inform_n (input_location, wanted,
6837 " candidate expects %d argument, %d provided",
6838 " candidate expects %d arguments, %d provided",
6839 wanted, have);
6840 }
6841 return unify_invalid (explain_p);
6842 }
6843
6844 static int
6845 unify_too_many_arguments (bool explain_p, int have, int wanted)
6846 {
6847 return unify_arity (explain_p, have, wanted);
6848 }
6849
6850 static int
6851 unify_too_few_arguments (bool explain_p, int have, int wanted,
6852 bool least_p = false)
6853 {
6854 return unify_arity (explain_p, have, wanted, least_p);
6855 }
6856
6857 static int
6858 unify_arg_conversion (bool explain_p, tree to_type,
6859 tree from_type, tree arg)
6860 {
6861 if (explain_p)
6862 inform (cp_expr_loc_or_input_loc (arg),
6863 " cannot convert %qE (type %qT) to type %qT",
6864 arg, from_type, to_type);
6865 return unify_invalid (explain_p);
6866 }
6867
6868 static int
6869 unify_no_common_base (bool explain_p, enum template_base_result r,
6870 tree parm, tree arg)
6871 {
6872 if (explain_p)
6873 switch (r)
6874 {
6875 case tbr_ambiguous_baseclass:
6876 inform (input_location, " %qT is an ambiguous base class of %qT",
6877 parm, arg);
6878 break;
6879 default:
6880 inform (input_location, " %qT is not derived from %qT", arg, parm);
6881 break;
6882 }
6883 return unify_invalid (explain_p);
6884 }
6885
6886 static int
6887 unify_inconsistent_template_template_parameters (bool explain_p)
6888 {
6889 if (explain_p)
6890 inform (input_location,
6891 " template parameters of a template template argument are "
6892 "inconsistent with other deduced template arguments");
6893 return unify_invalid (explain_p);
6894 }
6895
6896 static int
6897 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6898 {
6899 if (explain_p)
6900 inform (input_location,
6901 " cannot deduce a template for %qT from non-template type %qT",
6902 parm, arg);
6903 return unify_invalid (explain_p);
6904 }
6905
6906 static int
6907 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6908 {
6909 if (explain_p)
6910 inform (input_location,
6911 " template argument %qE does not match %qE", arg, parm);
6912 return unify_invalid (explain_p);
6913 }
6914
6915 /* True if T is a C++20 template parameter object to store the argument for a
6916 template parameter of class type. */
6917
6918 bool
6919 template_parm_object_p (const_tree t)
6920 {
6921 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6922 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6923 }
6924
6925 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6926 argument for TYPE, points to an unsuitable object.
6927
6928 Also adjust the type of the index in C++20 array subobject references. */
6929
6930 static bool
6931 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6932 {
6933 switch (TREE_CODE (expr))
6934 {
6935 CASE_CONVERT:
6936 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6937 complain);
6938
6939 case TARGET_EXPR:
6940 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6941 complain);
6942
6943 case CONSTRUCTOR:
6944 {
6945 unsigned i; tree elt;
6946 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6947 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6948 return true;
6949 }
6950 break;
6951
6952 case ADDR_EXPR:
6953 {
6954 tree decl = TREE_OPERAND (expr, 0);
6955
6956 if (cxx_dialect >= cxx20)
6957 while (TREE_CODE (decl) == COMPONENT_REF
6958 || TREE_CODE (decl) == ARRAY_REF)
6959 {
6960 tree &op = TREE_OPERAND (decl, 1);
6961 if (TREE_CODE (decl) == ARRAY_REF
6962 && TREE_CODE (op) == INTEGER_CST)
6963 /* Canonicalize array offsets to ptrdiff_t; how they were
6964 written doesn't matter for subobject identity. */
6965 op = fold_convert (ptrdiff_type_node, op);
6966 decl = TREE_OPERAND (decl, 0);
6967 }
6968
6969 if (!VAR_P (decl))
6970 {
6971 if (complain & tf_error)
6972 error_at (cp_expr_loc_or_input_loc (expr),
6973 "%qE is not a valid template argument of type %qT "
6974 "because %qE is not a variable", expr, type, decl);
6975 return true;
6976 }
6977 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6978 {
6979 if (complain & tf_error)
6980 error_at (cp_expr_loc_or_input_loc (expr),
6981 "%qE is not a valid template argument of type %qT "
6982 "in C++98 because %qD does not have external linkage",
6983 expr, type, decl);
6984 return true;
6985 }
6986 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6987 && decl_linkage (decl) == lk_none)
6988 {
6989 if (complain & tf_error)
6990 error_at (cp_expr_loc_or_input_loc (expr),
6991 "%qE is not a valid template argument of type %qT "
6992 "because %qD has no linkage", expr, type, decl);
6993 return true;
6994 }
6995 /* C++17: For a non-type template-parameter of reference or pointer
6996 type, the value of the constant expression shall not refer to (or
6997 for a pointer type, shall not be the address of):
6998 * a subobject (4.5),
6999 * a temporary object (15.2),
7000 * a string literal (5.13.5),
7001 * the result of a typeid expression (8.2.8), or
7002 * a predefined __func__ variable (11.4.1). */
7003 else if (DECL_ARTIFICIAL (decl))
7004 {
7005 if (complain & tf_error)
7006 error ("the address of %qD is not a valid template argument",
7007 decl);
7008 return true;
7009 }
7010 else if (cxx_dialect < cxx20
7011 && !(same_type_ignoring_top_level_qualifiers_p
7012 (strip_array_types (TREE_TYPE (type)),
7013 strip_array_types (TREE_TYPE (decl)))))
7014 {
7015 if (complain & tf_error)
7016 error ("the address of the %qT subobject of %qD is not a "
7017 "valid template argument", TREE_TYPE (type), decl);
7018 return true;
7019 }
7020 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7021 {
7022 if (complain & tf_error)
7023 error ("the address of %qD is not a valid template argument "
7024 "because it does not have static storage duration",
7025 decl);
7026 return true;
7027 }
7028 }
7029 break;
7030
7031 default:
7032 if (!INDIRECT_TYPE_P (type))
7033 /* We're only concerned about pointers and references here. */;
7034 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7035 /* Null pointer values are OK in C++11. */;
7036 else
7037 {
7038 if (VAR_P (expr))
7039 {
7040 if (complain & tf_error)
7041 error ("%qD is not a valid template argument "
7042 "because %qD is a variable, not the address of "
7043 "a variable", expr, expr);
7044 return true;
7045 }
7046 else
7047 {
7048 if (complain & tf_error)
7049 error ("%qE is not a valid template argument for %qT "
7050 "because it is not the address of a variable",
7051 expr, type);
7052 return true;
7053 }
7054 }
7055 }
7056 return false;
7057
7058 }
7059
7060 /* The template arguments corresponding to template parameter objects of types
7061 that contain pointers to members. */
7062
7063 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7064
7065 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7066 template argument EXPR. */
7067
7068 static tree
7069 get_template_parm_object (tree expr, tsubst_flags_t complain)
7070 {
7071 if (TREE_CODE (expr) == TARGET_EXPR)
7072 expr = TARGET_EXPR_INITIAL (expr);
7073
7074 if (!TREE_CONSTANT (expr))
7075 {
7076 if ((complain & tf_error)
7077 && require_rvalue_constant_expression (expr))
7078 cxx_constant_value (expr);
7079 return error_mark_node;
7080 }
7081 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7082 return error_mark_node;
7083
7084 tree name = mangle_template_parm_object (expr);
7085 tree decl = get_global_binding (name);
7086 if (decl)
7087 return decl;
7088
7089 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7090 decl = create_temporary_var (type);
7091 TREE_STATIC (decl) = true;
7092 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7093 TREE_READONLY (decl) = true;
7094 DECL_NAME (decl) = name;
7095 SET_DECL_ASSEMBLER_NAME (decl, name);
7096 DECL_CONTEXT (decl) = global_namespace;
7097 comdat_linkage (decl);
7098
7099 if (!zero_init_p (type))
7100 {
7101 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7102 lower_var_init before we're done mangling. So store the original
7103 value elsewhere. */
7104 tree copy = unshare_constructor (expr);
7105 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7106 }
7107
7108 pushdecl_top_level_and_finish (decl, expr);
7109
7110 return decl;
7111 }
7112
7113 /* Return the actual template argument corresponding to template parameter
7114 object VAR. */
7115
7116 tree
7117 tparm_object_argument (tree var)
7118 {
7119 if (zero_init_p (TREE_TYPE (var)))
7120 return DECL_INITIAL (var);
7121 return *(tparm_obj_values->get (var));
7122 }
7123
7124 /* Attempt to convert the non-type template parameter EXPR to the
7125 indicated TYPE. If the conversion is successful, return the
7126 converted value. If the conversion is unsuccessful, return
7127 NULL_TREE if we issued an error message, or error_mark_node if we
7128 did not. We issue error messages for out-and-out bad template
7129 parameters, but not simply because the conversion failed, since we
7130 might be just trying to do argument deduction. Both TYPE and EXPR
7131 must be non-dependent.
7132
7133 The conversion follows the special rules described in
7134 [temp.arg.nontype], and it is much more strict than an implicit
7135 conversion.
7136
7137 This function is called twice for each template argument (see
7138 lookup_template_class for a more accurate description of this
7139 problem). This means that we need to handle expressions which
7140 are not valid in a C++ source, but can be created from the
7141 first call (for instance, casts to perform conversions). These
7142 hacks can go away after we fix the double coercion problem. */
7143
7144 static tree
7145 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7146 {
7147 tree expr_type;
7148 location_t loc = cp_expr_loc_or_input_loc (expr);
7149
7150 /* Detect immediately string literals as invalid non-type argument.
7151 This special-case is not needed for correctness (we would easily
7152 catch this later), but only to provide better diagnostic for this
7153 common user mistake. As suggested by DR 100, we do not mention
7154 linkage issues in the diagnostic as this is not the point. */
7155 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7156 {
7157 if (complain & tf_error)
7158 error ("%qE is not a valid template argument for type %qT "
7159 "because string literals can never be used in this context",
7160 expr, type);
7161 return NULL_TREE;
7162 }
7163
7164 /* Add the ADDR_EXPR now for the benefit of
7165 value_dependent_expression_p. */
7166 if (TYPE_PTROBV_P (type)
7167 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7168 {
7169 expr = decay_conversion (expr, complain);
7170 if (expr == error_mark_node)
7171 return error_mark_node;
7172 }
7173
7174 /* If we are in a template, EXPR may be non-dependent, but still
7175 have a syntactic, rather than semantic, form. For example, EXPR
7176 might be a SCOPE_REF, rather than the VAR_DECL to which the
7177 SCOPE_REF refers. Preserving the qualifying scope is necessary
7178 so that access checking can be performed when the template is
7179 instantiated -- but here we need the resolved form so that we can
7180 convert the argument. */
7181 bool non_dep = false;
7182 if (TYPE_REF_OBJ_P (type)
7183 && has_value_dependent_address (expr))
7184 /* If we want the address and it's value-dependent, don't fold. */;
7185 else if (processing_template_decl
7186 && is_nondependent_constant_expression (expr))
7187 non_dep = true;
7188 if (error_operand_p (expr))
7189 return error_mark_node;
7190 expr_type = TREE_TYPE (expr);
7191
7192 /* If the argument is non-dependent, perform any conversions in
7193 non-dependent context as well. */
7194 processing_template_decl_sentinel s (non_dep);
7195 if (non_dep)
7196 expr = instantiate_non_dependent_expr_internal (expr, complain);
7197
7198 const bool val_dep_p = value_dependent_expression_p (expr);
7199 if (val_dep_p)
7200 expr = canonicalize_expr_argument (expr, complain);
7201
7202 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7203 to a non-type argument of "nullptr". */
7204 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7205 expr = fold_simple (convert (type, expr));
7206
7207 /* In C++11, integral or enumeration non-type template arguments can be
7208 arbitrary constant expressions. Pointer and pointer to
7209 member arguments can be general constant expressions that evaluate
7210 to a null value, but otherwise still need to be of a specific form. */
7211 if (cxx_dialect >= cxx11)
7212 {
7213 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7214 /* A PTRMEM_CST is already constant, and a valid template
7215 argument for a parameter of pointer to member type, we just want
7216 to leave it in that form rather than lower it to a
7217 CONSTRUCTOR. */;
7218 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7219 || cxx_dialect >= cxx17)
7220 {
7221 /* C++17: A template-argument for a non-type template-parameter shall
7222 be a converted constant expression (8.20) of the type of the
7223 template-parameter. */
7224 expr = build_converted_constant_expr (type, expr, complain);
7225 if (expr == error_mark_node)
7226 /* Make sure we return NULL_TREE only if we have really issued
7227 an error, as described above. */
7228 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7229 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7230 {
7231 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7232 return expr;
7233 }
7234 expr = maybe_constant_value (expr, NULL_TREE,
7235 /*manifestly_const_eval=*/true);
7236 expr = convert_from_reference (expr);
7237 }
7238 else if (TYPE_PTR_OR_PTRMEM_P (type))
7239 {
7240 tree folded = maybe_constant_value (expr, NULL_TREE,
7241 /*manifestly_const_eval=*/true);
7242 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7243 : null_member_pointer_value_p (folded))
7244 expr = folded;
7245 }
7246 }
7247
7248 if (TYPE_REF_P (type))
7249 expr = mark_lvalue_use (expr);
7250 else
7251 expr = mark_rvalue_use (expr);
7252
7253 /* HACK: Due to double coercion, we can get a
7254 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7255 which is the tree that we built on the first call (see
7256 below when coercing to reference to object or to reference to
7257 function). We just strip everything and get to the arg.
7258 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7259 for examples. */
7260 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7261 {
7262 tree probe_type, probe = expr;
7263 if (REFERENCE_REF_P (probe))
7264 probe = TREE_OPERAND (probe, 0);
7265 probe_type = TREE_TYPE (probe);
7266 if (TREE_CODE (probe) == NOP_EXPR)
7267 {
7268 /* ??? Maybe we could use convert_from_reference here, but we
7269 would need to relax its constraints because the NOP_EXPR
7270 could actually change the type to something more cv-qualified,
7271 and this is not folded by convert_from_reference. */
7272 tree addr = TREE_OPERAND (probe, 0);
7273 if (TYPE_REF_P (probe_type)
7274 && TREE_CODE (addr) == ADDR_EXPR
7275 && TYPE_PTR_P (TREE_TYPE (addr))
7276 && (same_type_ignoring_top_level_qualifiers_p
7277 (TREE_TYPE (probe_type),
7278 TREE_TYPE (TREE_TYPE (addr)))))
7279 {
7280 expr = TREE_OPERAND (addr, 0);
7281 expr_type = TREE_TYPE (probe_type);
7282 }
7283 }
7284 }
7285
7286 /* [temp.arg.nontype]/5, bullet 1
7287
7288 For a non-type template-parameter of integral or enumeration type,
7289 integral promotions (_conv.prom_) and integral conversions
7290 (_conv.integral_) are applied. */
7291 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7292 || TREE_CODE (type) == REAL_TYPE)
7293 {
7294 if (cxx_dialect < cxx11)
7295 {
7296 tree t = build_converted_constant_expr (type, expr, complain);
7297 t = maybe_constant_value (t);
7298 if (t != error_mark_node)
7299 expr = t;
7300 }
7301
7302 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7303 return error_mark_node;
7304
7305 /* Notice that there are constant expressions like '4 % 0' which
7306 do not fold into integer constants. */
7307 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7308 {
7309 if (complain & tf_error)
7310 {
7311 int errs = errorcount, warns = warningcount + werrorcount;
7312 if (!require_potential_constant_expression (expr))
7313 expr = error_mark_node;
7314 else
7315 expr = cxx_constant_value (expr);
7316 if (errorcount > errs || warningcount + werrorcount > warns)
7317 inform (loc, "in template argument for type %qT", type);
7318 if (expr == error_mark_node)
7319 return NULL_TREE;
7320 /* else cxx_constant_value complained but gave us
7321 a real constant, so go ahead. */
7322 if (!CONSTANT_CLASS_P (expr))
7323 {
7324 /* Some assemble time constant expressions like
7325 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7326 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7327 as we can emit them into .rodata initializers of
7328 variables, yet they can't fold into an INTEGER_CST at
7329 compile time. Refuse them here. */
7330 gcc_checking_assert (reduced_constant_expression_p (expr));
7331 error_at (loc, "template argument %qE for type %qT not "
7332 "a compile-time constant", expr, type);
7333 return NULL_TREE;
7334 }
7335 }
7336 else
7337 return NULL_TREE;
7338 }
7339
7340 /* Avoid typedef problems. */
7341 if (TREE_TYPE (expr) != type)
7342 expr = fold_convert (type, expr);
7343 }
7344 /* [temp.arg.nontype]/5, bullet 2
7345
7346 For a non-type template-parameter of type pointer to object,
7347 qualification conversions (_conv.qual_) and the array-to-pointer
7348 conversion (_conv.array_) are applied. */
7349 else if (TYPE_PTROBV_P (type))
7350 {
7351 tree decayed = expr;
7352
7353 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7354 decay_conversion or an explicit cast. If it's a problematic cast,
7355 we'll complain about it below. */
7356 if (TREE_CODE (expr) == NOP_EXPR)
7357 {
7358 tree probe = expr;
7359 STRIP_NOPS (probe);
7360 if (TREE_CODE (probe) == ADDR_EXPR
7361 && TYPE_PTR_P (TREE_TYPE (probe)))
7362 {
7363 expr = probe;
7364 expr_type = TREE_TYPE (expr);
7365 }
7366 }
7367
7368 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7369
7370 A template-argument for a non-type, non-template template-parameter
7371 shall be one of: [...]
7372
7373 -- the name of a non-type template-parameter;
7374 -- the address of an object or function with external linkage, [...]
7375 expressed as "& id-expression" where the & is optional if the name
7376 refers to a function or array, or if the corresponding
7377 template-parameter is a reference.
7378
7379 Here, we do not care about functions, as they are invalid anyway
7380 for a parameter of type pointer-to-object. */
7381
7382 if (val_dep_p)
7383 /* Non-type template parameters are OK. */
7384 ;
7385 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7386 /* Null pointer values are OK in C++11. */;
7387 else if (TREE_CODE (expr) != ADDR_EXPR
7388 && !INDIRECT_TYPE_P (expr_type))
7389 /* Other values, like integer constants, might be valid
7390 non-type arguments of some other type. */
7391 return error_mark_node;
7392 else if (invalid_tparm_referent_p (type, expr, complain))
7393 return NULL_TREE;
7394
7395 expr = decayed;
7396
7397 expr = perform_qualification_conversions (type, expr);
7398 if (expr == error_mark_node)
7399 return error_mark_node;
7400 }
7401 /* [temp.arg.nontype]/5, bullet 3
7402
7403 For a non-type template-parameter of type reference to object, no
7404 conversions apply. The type referred to by the reference may be more
7405 cv-qualified than the (otherwise identical) type of the
7406 template-argument. The template-parameter is bound directly to the
7407 template-argument, which must be an lvalue. */
7408 else if (TYPE_REF_OBJ_P (type))
7409 {
7410 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7411 expr_type))
7412 return error_mark_node;
7413
7414 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7415 {
7416 if (complain & tf_error)
7417 error ("%qE is not a valid template argument for type %qT "
7418 "because of conflicts in cv-qualification", expr, type);
7419 return NULL_TREE;
7420 }
7421
7422 if (!lvalue_p (expr))
7423 {
7424 if (complain & tf_error)
7425 error ("%qE is not a valid template argument for type %qT "
7426 "because it is not an lvalue", expr, type);
7427 return NULL_TREE;
7428 }
7429
7430 /* [temp.arg.nontype]/1
7431
7432 A template-argument for a non-type, non-template template-parameter
7433 shall be one of: [...]
7434
7435 -- the address of an object or function with external linkage. */
7436 if (INDIRECT_REF_P (expr)
7437 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7438 {
7439 expr = TREE_OPERAND (expr, 0);
7440 if (DECL_P (expr))
7441 {
7442 if (complain & tf_error)
7443 error ("%q#D is not a valid template argument for type %qT "
7444 "because a reference variable does not have a constant "
7445 "address", expr, type);
7446 return NULL_TREE;
7447 }
7448 }
7449
7450 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7451 /* OK, dependent reference. We don't want to ask whether a DECL is
7452 itself value-dependent, since what we want here is its address. */;
7453 else
7454 {
7455 expr = build_address (expr);
7456
7457 if (invalid_tparm_referent_p (type, expr, complain))
7458 return NULL_TREE;
7459 }
7460
7461 if (!same_type_p (type, TREE_TYPE (expr)))
7462 expr = build_nop (type, expr);
7463 }
7464 /* [temp.arg.nontype]/5, bullet 4
7465
7466 For a non-type template-parameter of type pointer to function, only
7467 the function-to-pointer conversion (_conv.func_) is applied. If the
7468 template-argument represents a set of overloaded functions (or a
7469 pointer to such), the matching function is selected from the set
7470 (_over.over_). */
7471 else if (TYPE_PTRFN_P (type))
7472 {
7473 /* If the argument is a template-id, we might not have enough
7474 context information to decay the pointer. */
7475 if (!type_unknown_p (expr_type))
7476 {
7477 expr = decay_conversion (expr, complain);
7478 if (expr == error_mark_node)
7479 return error_mark_node;
7480 }
7481
7482 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7483 /* Null pointer values are OK in C++11. */
7484 return perform_qualification_conversions (type, expr);
7485
7486 expr = convert_nontype_argument_function (type, expr, complain);
7487 if (!expr || expr == error_mark_node)
7488 return expr;
7489 }
7490 /* [temp.arg.nontype]/5, bullet 5
7491
7492 For a non-type template-parameter of type reference to function, no
7493 conversions apply. If the template-argument represents a set of
7494 overloaded functions, the matching function is selected from the set
7495 (_over.over_). */
7496 else if (TYPE_REFFN_P (type))
7497 {
7498 if (TREE_CODE (expr) == ADDR_EXPR)
7499 {
7500 if (complain & tf_error)
7501 {
7502 error ("%qE is not a valid template argument for type %qT "
7503 "because it is a pointer", expr, type);
7504 inform (input_location, "try using %qE instead",
7505 TREE_OPERAND (expr, 0));
7506 }
7507 return NULL_TREE;
7508 }
7509
7510 expr = convert_nontype_argument_function (type, expr, complain);
7511 if (!expr || expr == error_mark_node)
7512 return expr;
7513 }
7514 /* [temp.arg.nontype]/5, bullet 6
7515
7516 For a non-type template-parameter of type pointer to member function,
7517 no conversions apply. If the template-argument represents a set of
7518 overloaded member functions, the matching member function is selected
7519 from the set (_over.over_). */
7520 else if (TYPE_PTRMEMFUNC_P (type))
7521 {
7522 expr = instantiate_type (type, expr, tf_none);
7523 if (expr == error_mark_node)
7524 return error_mark_node;
7525
7526 /* [temp.arg.nontype] bullet 1 says the pointer to member
7527 expression must be a pointer-to-member constant. */
7528 if (!val_dep_p
7529 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7530 return NULL_TREE;
7531
7532 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7533 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7534 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7535 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7536 }
7537 /* [temp.arg.nontype]/5, bullet 7
7538
7539 For a non-type template-parameter of type pointer to data member,
7540 qualification conversions (_conv.qual_) are applied. */
7541 else if (TYPE_PTRDATAMEM_P (type))
7542 {
7543 /* [temp.arg.nontype] bullet 1 says the pointer to member
7544 expression must be a pointer-to-member constant. */
7545 if (!val_dep_p
7546 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7547 return NULL_TREE;
7548
7549 expr = perform_qualification_conversions (type, expr);
7550 if (expr == error_mark_node)
7551 return expr;
7552 }
7553 else if (NULLPTR_TYPE_P (type))
7554 {
7555 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7556 {
7557 if (complain & tf_error)
7558 error ("%qE is not a valid template argument for type %qT "
7559 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7560 return NULL_TREE;
7561 }
7562 return expr;
7563 }
7564 else if (CLASS_TYPE_P (type))
7565 {
7566 /* Replace the argument with a reference to the corresponding template
7567 parameter object. */
7568 if (!val_dep_p)
7569 expr = get_template_parm_object (expr, complain);
7570 if (expr == error_mark_node)
7571 return NULL_TREE;
7572 }
7573 /* A template non-type parameter must be one of the above. */
7574 else
7575 gcc_unreachable ();
7576
7577 /* Sanity check: did we actually convert the argument to the
7578 right type? */
7579 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7580 (type, TREE_TYPE (expr)));
7581 return convert_from_reference (expr);
7582 }
7583
7584 /* Subroutine of coerce_template_template_parms, which returns 1 if
7585 PARM_PARM and ARG_PARM match using the rule for the template
7586 parameters of template template parameters. Both PARM and ARG are
7587 template parameters; the rest of the arguments are the same as for
7588 coerce_template_template_parms.
7589 */
7590 static int
7591 coerce_template_template_parm (tree parm,
7592 tree arg,
7593 tsubst_flags_t complain,
7594 tree in_decl,
7595 tree outer_args)
7596 {
7597 if (arg == NULL_TREE || error_operand_p (arg)
7598 || parm == NULL_TREE || error_operand_p (parm))
7599 return 0;
7600
7601 if (TREE_CODE (arg) != TREE_CODE (parm))
7602 return 0;
7603
7604 switch (TREE_CODE (parm))
7605 {
7606 case TEMPLATE_DECL:
7607 /* We encounter instantiations of templates like
7608 template <template <template <class> class> class TT>
7609 class C; */
7610 {
7611 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7612 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7613
7614 if (!coerce_template_template_parms
7615 (parmparm, argparm, complain, in_decl, outer_args))
7616 return 0;
7617 }
7618 /* Fall through. */
7619
7620 case TYPE_DECL:
7621 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7622 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7623 /* Argument is a parameter pack but parameter is not. */
7624 return 0;
7625 break;
7626
7627 case PARM_DECL:
7628 /* The tsubst call is used to handle cases such as
7629
7630 template <int> class C {};
7631 template <class T, template <T> class TT> class D {};
7632 D<int, C> d;
7633
7634 i.e. the parameter list of TT depends on earlier parameters. */
7635 if (!uses_template_parms (TREE_TYPE (arg)))
7636 {
7637 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7638 if (!uses_template_parms (t)
7639 && !same_type_p (t, TREE_TYPE (arg)))
7640 return 0;
7641 }
7642
7643 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7644 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7645 /* Argument is a parameter pack but parameter is not. */
7646 return 0;
7647
7648 break;
7649
7650 default:
7651 gcc_unreachable ();
7652 }
7653
7654 return 1;
7655 }
7656
7657 /* Coerce template argument list ARGLIST for use with template
7658 template-parameter TEMPL. */
7659
7660 static tree
7661 coerce_template_args_for_ttp (tree templ, tree arglist,
7662 tsubst_flags_t complain)
7663 {
7664 /* Consider an example where a template template parameter declared as
7665
7666 template <class T, class U = std::allocator<T> > class TT
7667
7668 The template parameter level of T and U are one level larger than
7669 of TT. To proper process the default argument of U, say when an
7670 instantiation `TT<int>' is seen, we need to build the full
7671 arguments containing {int} as the innermost level. Outer levels,
7672 available when not appearing as default template argument, can be
7673 obtained from the arguments of the enclosing template.
7674
7675 Suppose that TT is later substituted with std::vector. The above
7676 instantiation is `TT<int, std::allocator<T> >' with TT at
7677 level 1, and T at level 2, while the template arguments at level 1
7678 becomes {std::vector} and the inner level 2 is {int}. */
7679
7680 tree outer = DECL_CONTEXT (templ);
7681 if (outer)
7682 outer = generic_targs_for (outer);
7683 else if (current_template_parms)
7684 {
7685 /* This is an argument of the current template, so we haven't set
7686 DECL_CONTEXT yet. */
7687 tree relevant_template_parms;
7688
7689 /* Parameter levels that are greater than the level of the given
7690 template template parm are irrelevant. */
7691 relevant_template_parms = current_template_parms;
7692 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7693 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7694 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7695
7696 outer = template_parms_to_args (relevant_template_parms);
7697 }
7698
7699 if (outer)
7700 arglist = add_to_template_args (outer, arglist);
7701
7702 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7703 return coerce_template_parms (parmlist, arglist, templ,
7704 complain,
7705 /*require_all_args=*/true,
7706 /*use_default_args=*/true);
7707 }
7708
7709 /* A cache of template template parameters with match-all default
7710 arguments. */
7711 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7712
7713 /* T is a bound template template-parameter. Copy its arguments into default
7714 arguments of the template template-parameter's template parameters. */
7715
7716 static tree
7717 add_defaults_to_ttp (tree otmpl)
7718 {
7719 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7720 return *c;
7721
7722 tree ntmpl = copy_node (otmpl);
7723
7724 tree ntype = copy_node (TREE_TYPE (otmpl));
7725 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7726 TYPE_MAIN_VARIANT (ntype) = ntype;
7727 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7728 TYPE_NAME (ntype) = ntmpl;
7729 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7730
7731 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7732 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7733 TEMPLATE_PARM_DECL (idx) = ntmpl;
7734 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7735
7736 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7737 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7738 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7739 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7740 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7741 {
7742 tree o = TREE_VEC_ELT (vec, i);
7743 if (!template_parameter_pack_p (TREE_VALUE (o)))
7744 {
7745 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7746 TREE_PURPOSE (n) = any_targ_node;
7747 }
7748 }
7749
7750 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7751 return ntmpl;
7752 }
7753
7754 /* ARG is a bound potential template template-argument, and PARGS is a list
7755 of arguments for the corresponding template template-parameter. Adjust
7756 PARGS as appropriate for application to ARG's template, and if ARG is a
7757 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7758 arguments to the template template parameter. */
7759
7760 static tree
7761 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7762 {
7763 ++processing_template_decl;
7764 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7765 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7766 {
7767 /* When comparing two template template-parameters in partial ordering,
7768 rewrite the one currently being used as an argument to have default
7769 arguments for all parameters. */
7770 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7771 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7772 if (pargs != error_mark_node)
7773 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7774 TYPE_TI_ARGS (arg));
7775 }
7776 else
7777 {
7778 tree aparms
7779 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7780 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7781 /*require_all*/true,
7782 /*use_default*/true);
7783 }
7784 --processing_template_decl;
7785 return pargs;
7786 }
7787
7788 /* Subroutine of unify for the case when PARM is a
7789 BOUND_TEMPLATE_TEMPLATE_PARM. */
7790
7791 static int
7792 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7793 bool explain_p)
7794 {
7795 tree parmvec = TYPE_TI_ARGS (parm);
7796 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7797
7798 /* The template template parm might be variadic and the argument
7799 not, so flatten both argument lists. */
7800 parmvec = expand_template_argument_pack (parmvec);
7801 argvec = expand_template_argument_pack (argvec);
7802
7803 if (flag_new_ttp)
7804 {
7805 /* In keeping with P0522R0, adjust P's template arguments
7806 to apply to A's template; then flatten it again. */
7807 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7808 nparmvec = expand_template_argument_pack (nparmvec);
7809
7810 if (unify (tparms, targs, nparmvec, argvec,
7811 UNIFY_ALLOW_NONE, explain_p))
7812 return 1;
7813
7814 /* If the P0522 adjustment eliminated a pack expansion, deduce
7815 empty packs. */
7816 if (flag_new_ttp
7817 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7818 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7819 DEDUCE_EXACT, /*sub*/true, explain_p))
7820 return 1;
7821 }
7822 else
7823 {
7824 /* Deduce arguments T, i from TT<T> or TT<i>.
7825 We check each element of PARMVEC and ARGVEC individually
7826 rather than the whole TREE_VEC since they can have
7827 different number of elements, which is allowed under N2555. */
7828
7829 int len = TREE_VEC_LENGTH (parmvec);
7830
7831 /* Check if the parameters end in a pack, making them
7832 variadic. */
7833 int parm_variadic_p = 0;
7834 if (len > 0
7835 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7836 parm_variadic_p = 1;
7837
7838 for (int i = 0; i < len - parm_variadic_p; ++i)
7839 /* If the template argument list of P contains a pack
7840 expansion that is not the last template argument, the
7841 entire template argument list is a non-deduced
7842 context. */
7843 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7844 return unify_success (explain_p);
7845
7846 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7847 return unify_too_few_arguments (explain_p,
7848 TREE_VEC_LENGTH (argvec), len);
7849
7850 for (int i = 0; i < len - parm_variadic_p; ++i)
7851 if (unify (tparms, targs,
7852 TREE_VEC_ELT (parmvec, i),
7853 TREE_VEC_ELT (argvec, i),
7854 UNIFY_ALLOW_NONE, explain_p))
7855 return 1;
7856
7857 if (parm_variadic_p
7858 && unify_pack_expansion (tparms, targs,
7859 parmvec, argvec,
7860 DEDUCE_EXACT,
7861 /*subr=*/true, explain_p))
7862 return 1;
7863 }
7864
7865 return 0;
7866 }
7867
7868 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7869 template template parameters. Both PARM_PARMS and ARG_PARMS are
7870 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7871 or PARM_DECL.
7872
7873 Consider the example:
7874 template <class T> class A;
7875 template<template <class U> class TT> class B;
7876
7877 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7878 the parameters to A, and OUTER_ARGS contains A. */
7879
7880 static int
7881 coerce_template_template_parms (tree parm_parms,
7882 tree arg_parms,
7883 tsubst_flags_t complain,
7884 tree in_decl,
7885 tree outer_args)
7886 {
7887 int nparms, nargs, i;
7888 tree parm, arg;
7889 int variadic_p = 0;
7890
7891 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7892 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7893
7894 nparms = TREE_VEC_LENGTH (parm_parms);
7895 nargs = TREE_VEC_LENGTH (arg_parms);
7896
7897 if (flag_new_ttp)
7898 {
7899 /* P0522R0: A template template-parameter P is at least as specialized as
7900 a template template-argument A if, given the following rewrite to two
7901 function templates, the function template corresponding to P is at
7902 least as specialized as the function template corresponding to A
7903 according to the partial ordering rules for function templates
7904 ([temp.func.order]). Given an invented class template X with the
7905 template parameter list of A (including default arguments):
7906
7907 * Each of the two function templates has the same template parameters,
7908 respectively, as P or A.
7909
7910 * Each function template has a single function parameter whose type is
7911 a specialization of X with template arguments corresponding to the
7912 template parameters from the respective function template where, for
7913 each template parameter PP in the template parameter list of the
7914 function template, a corresponding template argument AA is formed. If
7915 PP declares a parameter pack, then AA is the pack expansion
7916 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7917
7918 If the rewrite produces an invalid type, then P is not at least as
7919 specialized as A. */
7920
7921 /* So coerce P's args to apply to A's parms, and then deduce between A's
7922 args and the converted args. If that succeeds, A is at least as
7923 specialized as P, so they match.*/
7924 tree pargs = template_parms_level_to_args (parm_parms);
7925 pargs = add_outermost_template_args (outer_args, pargs);
7926 ++processing_template_decl;
7927 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7928 /*require_all*/true, /*use_default*/true);
7929 --processing_template_decl;
7930 if (pargs != error_mark_node)
7931 {
7932 tree targs = make_tree_vec (nargs);
7933 tree aargs = template_parms_level_to_args (arg_parms);
7934 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7935 /*explain*/false))
7936 return 1;
7937 }
7938 }
7939
7940 /* Determine whether we have a parameter pack at the end of the
7941 template template parameter's template parameter list. */
7942 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7943 {
7944 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7945
7946 if (error_operand_p (parm))
7947 return 0;
7948
7949 switch (TREE_CODE (parm))
7950 {
7951 case TEMPLATE_DECL:
7952 case TYPE_DECL:
7953 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7954 variadic_p = 1;
7955 break;
7956
7957 case PARM_DECL:
7958 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7959 variadic_p = 1;
7960 break;
7961
7962 default:
7963 gcc_unreachable ();
7964 }
7965 }
7966
7967 if (nargs != nparms
7968 && !(variadic_p && nargs >= nparms - 1))
7969 return 0;
7970
7971 /* Check all of the template parameters except the parameter pack at
7972 the end (if any). */
7973 for (i = 0; i < nparms - variadic_p; ++i)
7974 {
7975 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7976 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7977 continue;
7978
7979 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7980 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7981
7982 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7983 outer_args))
7984 return 0;
7985
7986 }
7987
7988 if (variadic_p)
7989 {
7990 /* Check each of the template parameters in the template
7991 argument against the template parameter pack at the end of
7992 the template template parameter. */
7993 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7994 return 0;
7995
7996 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7997
7998 for (; i < nargs; ++i)
7999 {
8000 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8001 continue;
8002
8003 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8004
8005 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8006 outer_args))
8007 return 0;
8008 }
8009 }
8010
8011 return 1;
8012 }
8013
8014 /* Verifies that the deduced template arguments (in TARGS) for the
8015 template template parameters (in TPARMS) represent valid bindings,
8016 by comparing the template parameter list of each template argument
8017 to the template parameter list of its corresponding template
8018 template parameter, in accordance with DR150. This
8019 routine can only be called after all template arguments have been
8020 deduced. It will return TRUE if all of the template template
8021 parameter bindings are okay, FALSE otherwise. */
8022 bool
8023 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8024 {
8025 int i, ntparms = TREE_VEC_LENGTH (tparms);
8026 bool ret = true;
8027
8028 /* We're dealing with template parms in this process. */
8029 ++processing_template_decl;
8030
8031 targs = INNERMOST_TEMPLATE_ARGS (targs);
8032
8033 for (i = 0; i < ntparms; ++i)
8034 {
8035 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8036 tree targ = TREE_VEC_ELT (targs, i);
8037
8038 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8039 {
8040 tree packed_args = NULL_TREE;
8041 int idx, len = 1;
8042
8043 if (ARGUMENT_PACK_P (targ))
8044 {
8045 /* Look inside the argument pack. */
8046 packed_args = ARGUMENT_PACK_ARGS (targ);
8047 len = TREE_VEC_LENGTH (packed_args);
8048 }
8049
8050 for (idx = 0; idx < len; ++idx)
8051 {
8052 tree targ_parms = NULL_TREE;
8053
8054 if (packed_args)
8055 /* Extract the next argument from the argument
8056 pack. */
8057 targ = TREE_VEC_ELT (packed_args, idx);
8058
8059 if (PACK_EXPANSION_P (targ))
8060 /* Look at the pattern of the pack expansion. */
8061 targ = PACK_EXPANSION_PATTERN (targ);
8062
8063 /* Extract the template parameters from the template
8064 argument. */
8065 if (TREE_CODE (targ) == TEMPLATE_DECL)
8066 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8067 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8068 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8069
8070 /* Verify that we can coerce the template template
8071 parameters from the template argument to the template
8072 parameter. This requires an exact match. */
8073 if (targ_parms
8074 && !coerce_template_template_parms
8075 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8076 targ_parms,
8077 tf_none,
8078 tparm,
8079 targs))
8080 {
8081 ret = false;
8082 goto out;
8083 }
8084 }
8085 }
8086 }
8087
8088 out:
8089
8090 --processing_template_decl;
8091 return ret;
8092 }
8093
8094 /* Since type attributes aren't mangled, we need to strip them from
8095 template type arguments. */
8096
8097 tree
8098 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8099 {
8100 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8101 return arg;
8102 bool removed_attributes = false;
8103 tree canon = strip_typedefs (arg, &removed_attributes);
8104 if (removed_attributes
8105 && (complain & tf_warning))
8106 warning (OPT_Wignored_attributes,
8107 "ignoring attributes on template argument %qT", arg);
8108 return canon;
8109 }
8110
8111 /* And from inside dependent non-type arguments like sizeof(Type). */
8112
8113 static tree
8114 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8115 {
8116 if (!arg || arg == error_mark_node)
8117 return arg;
8118 bool removed_attributes = false;
8119 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8120 if (removed_attributes
8121 && (complain & tf_warning))
8122 warning (OPT_Wignored_attributes,
8123 "ignoring attributes in template argument %qE", arg);
8124 return canon;
8125 }
8126
8127 // A template declaration can be substituted for a constrained
8128 // template template parameter only when the argument is more
8129 // constrained than the parameter.
8130 static bool
8131 is_compatible_template_arg (tree parm, tree arg)
8132 {
8133 tree parm_cons = get_constraints (parm);
8134
8135 /* For now, allow constrained template template arguments
8136 and unconstrained template template parameters. */
8137 if (parm_cons == NULL_TREE)
8138 return true;
8139
8140 /* If the template parameter is constrained, we need to rewrite its
8141 constraints in terms of the ARG's template parameters. This ensures
8142 that all of the template parameter types will have the same depth.
8143
8144 Note that this is only valid when coerce_template_template_parm is
8145 true for the innermost template parameters of PARM and ARG. In other
8146 words, because coercion is successful, this conversion will be valid. */
8147 tree new_args = NULL_TREE;
8148 if (parm_cons)
8149 {
8150 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8151 new_args = template_parms_level_to_args (aparms);
8152 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8153 tf_none, NULL_TREE);
8154 if (parm_cons == error_mark_node)
8155 return false;
8156 }
8157
8158 return weakly_subsumes (parm_cons, new_args, arg);
8159 }
8160
8161 // Convert a placeholder argument into a binding to the original
8162 // parameter. The original parameter is saved as the TREE_TYPE of
8163 // ARG.
8164 static inline tree
8165 convert_wildcard_argument (tree parm, tree arg)
8166 {
8167 TREE_TYPE (arg) = parm;
8168 return arg;
8169 }
8170
8171 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8172 because one of them is dependent. But we need to represent the
8173 conversion for the benefit of cp_tree_equal. */
8174
8175 static tree
8176 maybe_convert_nontype_argument (tree type, tree arg)
8177 {
8178 /* Auto parms get no conversion. */
8179 if (type_uses_auto (type))
8180 return arg;
8181 /* We don't need or want to add this conversion now if we're going to use the
8182 argument for deduction. */
8183 if (value_dependent_expression_p (arg))
8184 return arg;
8185
8186 type = cv_unqualified (type);
8187 tree argtype = TREE_TYPE (arg);
8188 if (same_type_p (type, argtype))
8189 return arg;
8190
8191 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8192 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8193 return arg;
8194 }
8195
8196 /* Convert the indicated template ARG as necessary to match the
8197 indicated template PARM. Returns the converted ARG, or
8198 error_mark_node if the conversion was unsuccessful. Error and
8199 warning messages are issued under control of COMPLAIN. This
8200 conversion is for the Ith parameter in the parameter list. ARGS is
8201 the full set of template arguments deduced so far. */
8202
8203 static tree
8204 convert_template_argument (tree parm,
8205 tree arg,
8206 tree args,
8207 tsubst_flags_t complain,
8208 int i,
8209 tree in_decl)
8210 {
8211 tree orig_arg;
8212 tree val;
8213 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8214
8215 if (parm == error_mark_node || error_operand_p (arg))
8216 return error_mark_node;
8217
8218 /* Trivially convert placeholders. */
8219 if (TREE_CODE (arg) == WILDCARD_DECL)
8220 return convert_wildcard_argument (parm, arg);
8221
8222 if (arg == any_targ_node)
8223 return arg;
8224
8225 if (TREE_CODE (arg) == TREE_LIST
8226 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8227 {
8228 /* The template argument was the name of some
8229 member function. That's usually
8230 invalid, but static members are OK. In any
8231 case, grab the underlying fields/functions
8232 and issue an error later if required. */
8233 TREE_TYPE (arg) = unknown_type_node;
8234 }
8235
8236 orig_arg = arg;
8237
8238 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8239 requires_type = (TREE_CODE (parm) == TYPE_DECL
8240 || requires_tmpl_type);
8241
8242 /* When determining whether an argument pack expansion is a template,
8243 look at the pattern. */
8244 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8245 arg = PACK_EXPANSION_PATTERN (arg);
8246
8247 /* Deal with an injected-class-name used as a template template arg. */
8248 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8249 {
8250 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8251 if (TREE_CODE (t) == TEMPLATE_DECL)
8252 {
8253 if (cxx_dialect >= cxx11)
8254 /* OK under DR 1004. */;
8255 else if (complain & tf_warning_or_error)
8256 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8257 " used as template template argument", TYPE_NAME (arg));
8258 else if (flag_pedantic_errors)
8259 t = arg;
8260
8261 arg = t;
8262 }
8263 }
8264
8265 is_tmpl_type =
8266 ((TREE_CODE (arg) == TEMPLATE_DECL
8267 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8268 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8269 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8270 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8271
8272 if (is_tmpl_type
8273 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8274 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8275 arg = TYPE_STUB_DECL (arg);
8276
8277 is_type = TYPE_P (arg) || is_tmpl_type;
8278
8279 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8280 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8281 {
8282 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8283 {
8284 if (complain & tf_error)
8285 error ("invalid use of destructor %qE as a type", orig_arg);
8286 return error_mark_node;
8287 }
8288
8289 permerror (input_location,
8290 "to refer to a type member of a template parameter, "
8291 "use %<typename %E%>", orig_arg);
8292
8293 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8294 TREE_OPERAND (arg, 1),
8295 typename_type,
8296 complain);
8297 arg = orig_arg;
8298 is_type = 1;
8299 }
8300 if (is_type != requires_type)
8301 {
8302 if (in_decl)
8303 {
8304 if (complain & tf_error)
8305 {
8306 error ("type/value mismatch at argument %d in template "
8307 "parameter list for %qD",
8308 i + 1, in_decl);
8309 if (is_type)
8310 {
8311 /* The template argument is a type, but we're expecting
8312 an expression. */
8313 inform (input_location,
8314 " expected a constant of type %qT, got %qT",
8315 TREE_TYPE (parm),
8316 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8317 /* [temp.arg]/2: "In a template-argument, an ambiguity
8318 between a type-id and an expression is resolved to a
8319 type-id, regardless of the form of the corresponding
8320 template-parameter." So give the user a clue. */
8321 if (TREE_CODE (arg) == FUNCTION_TYPE)
8322 inform (input_location, " ambiguous template argument "
8323 "for non-type template parameter is treated as "
8324 "function type");
8325 }
8326 else if (requires_tmpl_type)
8327 inform (input_location,
8328 " expected a class template, got %qE", orig_arg);
8329 else
8330 inform (input_location,
8331 " expected a type, got %qE", orig_arg);
8332 }
8333 }
8334 return error_mark_node;
8335 }
8336 if (is_tmpl_type ^ requires_tmpl_type)
8337 {
8338 if (in_decl && (complain & tf_error))
8339 {
8340 error ("type/value mismatch at argument %d in template "
8341 "parameter list for %qD",
8342 i + 1, in_decl);
8343 if (is_tmpl_type)
8344 inform (input_location,
8345 " expected a type, got %qT", DECL_NAME (arg));
8346 else
8347 inform (input_location,
8348 " expected a class template, got %qT", orig_arg);
8349 }
8350 return error_mark_node;
8351 }
8352
8353 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8354 /* We already did the appropriate conversion when packing args. */
8355 val = orig_arg;
8356 else if (is_type)
8357 {
8358 if (requires_tmpl_type)
8359 {
8360 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8361 /* The number of argument required is not known yet.
8362 Just accept it for now. */
8363 val = orig_arg;
8364 else
8365 {
8366 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8367 tree argparm;
8368
8369 /* Strip alias templates that are equivalent to another
8370 template. */
8371 arg = get_underlying_template (arg);
8372 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8373
8374 if (coerce_template_template_parms (parmparm, argparm,
8375 complain, in_decl,
8376 args))
8377 {
8378 val = arg;
8379
8380 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8381 TEMPLATE_DECL. */
8382 if (val != error_mark_node)
8383 {
8384 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8385 val = TREE_TYPE (val);
8386 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8387 val = make_pack_expansion (val, complain);
8388 }
8389 }
8390 else
8391 {
8392 if (in_decl && (complain & tf_error))
8393 {
8394 error ("type/value mismatch at argument %d in "
8395 "template parameter list for %qD",
8396 i + 1, in_decl);
8397 inform (input_location,
8398 " expected a template of type %qD, got %qT",
8399 parm, orig_arg);
8400 }
8401
8402 val = error_mark_node;
8403 }
8404
8405 // Check that the constraints are compatible before allowing the
8406 // substitution.
8407 if (val != error_mark_node)
8408 if (!is_compatible_template_arg (parm, arg))
8409 {
8410 if (in_decl && (complain & tf_error))
8411 {
8412 error ("constraint mismatch at argument %d in "
8413 "template parameter list for %qD",
8414 i + 1, in_decl);
8415 inform (input_location, " expected %qD but got %qD",
8416 parm, arg);
8417 }
8418 val = error_mark_node;
8419 }
8420 }
8421 }
8422 else
8423 val = orig_arg;
8424 /* We only form one instance of each template specialization.
8425 Therefore, if we use a non-canonical variant (i.e., a
8426 typedef), any future messages referring to the type will use
8427 the typedef, which is confusing if those future uses do not
8428 themselves also use the typedef. */
8429 if (TYPE_P (val))
8430 val = canonicalize_type_argument (val, complain);
8431 }
8432 else
8433 {
8434 tree t = TREE_TYPE (parm);
8435
8436 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8437 > TMPL_ARGS_DEPTH (args))
8438 /* We don't have enough levels of args to do any substitution. This
8439 can happen in the context of -fnew-ttp-matching. */;
8440 else if (tree a = type_uses_auto (t))
8441 {
8442 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8443 if (t == error_mark_node)
8444 return error_mark_node;
8445 }
8446 else
8447 t = tsubst (t, args, complain, in_decl);
8448
8449 if (invalid_nontype_parm_type_p (t, complain))
8450 return error_mark_node;
8451
8452 if (t != TREE_TYPE (parm))
8453 t = canonicalize_type_argument (t, complain);
8454
8455 if (!type_dependent_expression_p (orig_arg)
8456 && !uses_template_parms (t))
8457 /* We used to call digest_init here. However, digest_init
8458 will report errors, which we don't want when complain
8459 is zero. More importantly, digest_init will try too
8460 hard to convert things: for example, `0' should not be
8461 converted to pointer type at this point according to
8462 the standard. Accepting this is not merely an
8463 extension, since deciding whether or not these
8464 conversions can occur is part of determining which
8465 function template to call, or whether a given explicit
8466 argument specification is valid. */
8467 val = convert_nontype_argument (t, orig_arg, complain);
8468 else
8469 {
8470 val = canonicalize_expr_argument (orig_arg, complain);
8471 val = maybe_convert_nontype_argument (t, val);
8472 }
8473
8474
8475 if (val == NULL_TREE)
8476 val = error_mark_node;
8477 else if (val == error_mark_node && (complain & tf_error))
8478 error_at (cp_expr_loc_or_input_loc (orig_arg),
8479 "could not convert template argument %qE from %qT to %qT",
8480 orig_arg, TREE_TYPE (orig_arg), t);
8481
8482 if (INDIRECT_REF_P (val))
8483 {
8484 /* Reject template arguments that are references to built-in
8485 functions with no library fallbacks. */
8486 const_tree inner = TREE_OPERAND (val, 0);
8487 const_tree innertype = TREE_TYPE (inner);
8488 if (innertype
8489 && TYPE_REF_P (innertype)
8490 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8491 && TREE_OPERAND_LENGTH (inner) > 0
8492 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8493 return error_mark_node;
8494 }
8495
8496 if (TREE_CODE (val) == SCOPE_REF)
8497 {
8498 /* Strip typedefs from the SCOPE_REF. */
8499 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8500 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8501 complain);
8502 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8503 QUALIFIED_NAME_IS_TEMPLATE (val));
8504 }
8505 }
8506
8507 return val;
8508 }
8509
8510 /* Coerces the remaining template arguments in INNER_ARGS (from
8511 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8512 Returns the coerced argument pack. PARM_IDX is the position of this
8513 parameter in the template parameter list. ARGS is the original
8514 template argument list. */
8515 static tree
8516 coerce_template_parameter_pack (tree parms,
8517 int parm_idx,
8518 tree args,
8519 tree inner_args,
8520 int arg_idx,
8521 tree new_args,
8522 int* lost,
8523 tree in_decl,
8524 tsubst_flags_t complain)
8525 {
8526 tree parm = TREE_VEC_ELT (parms, parm_idx);
8527 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8528 tree packed_args;
8529 tree argument_pack;
8530 tree packed_parms = NULL_TREE;
8531
8532 if (arg_idx > nargs)
8533 arg_idx = nargs;
8534
8535 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8536 {
8537 /* When the template parameter is a non-type template parameter pack
8538 or template template parameter pack whose type or template
8539 parameters use parameter packs, we know exactly how many arguments
8540 we are looking for. Build a vector of the instantiated decls for
8541 these template parameters in PACKED_PARMS. */
8542 /* We can't use make_pack_expansion here because it would interpret a
8543 _DECL as a use rather than a declaration. */
8544 tree decl = TREE_VALUE (parm);
8545 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8546 SET_PACK_EXPANSION_PATTERN (exp, decl);
8547 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8548 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8549
8550 TREE_VEC_LENGTH (args)--;
8551 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8552 TREE_VEC_LENGTH (args)++;
8553
8554 if (packed_parms == error_mark_node)
8555 return error_mark_node;
8556
8557 /* If we're doing a partial instantiation of a member template,
8558 verify that all of the types used for the non-type
8559 template parameter pack are, in fact, valid for non-type
8560 template parameters. */
8561 if (arg_idx < nargs
8562 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8563 {
8564 int j, len = TREE_VEC_LENGTH (packed_parms);
8565 for (j = 0; j < len; ++j)
8566 {
8567 tree t = TREE_VEC_ELT (packed_parms, j);
8568 if (TREE_CODE (t) == PARM_DECL
8569 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8570 return error_mark_node;
8571 }
8572 /* We don't know how many args we have yet, just
8573 use the unconverted ones for now. */
8574 return NULL_TREE;
8575 }
8576
8577 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8578 }
8579 /* Check if we have a placeholder pack, which indicates we're
8580 in the context of a introduction list. In that case we want
8581 to match this pack to the single placeholder. */
8582 else if (arg_idx < nargs
8583 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8584 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8585 {
8586 nargs = arg_idx + 1;
8587 packed_args = make_tree_vec (1);
8588 }
8589 else
8590 packed_args = make_tree_vec (nargs - arg_idx);
8591
8592 /* Convert the remaining arguments, which will be a part of the
8593 parameter pack "parm". */
8594 int first_pack_arg = arg_idx;
8595 for (; arg_idx < nargs; ++arg_idx)
8596 {
8597 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8598 tree actual_parm = TREE_VALUE (parm);
8599 int pack_idx = arg_idx - first_pack_arg;
8600
8601 if (packed_parms)
8602 {
8603 /* Once we've packed as many args as we have types, stop. */
8604 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8605 break;
8606 else if (PACK_EXPANSION_P (arg))
8607 /* We don't know how many args we have yet, just
8608 use the unconverted ones for now. */
8609 return NULL_TREE;
8610 else
8611 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8612 }
8613
8614 if (arg == error_mark_node)
8615 {
8616 if (complain & tf_error)
8617 error ("template argument %d is invalid", arg_idx + 1);
8618 }
8619 else
8620 arg = convert_template_argument (actual_parm,
8621 arg, new_args, complain, parm_idx,
8622 in_decl);
8623 if (arg == error_mark_node)
8624 (*lost)++;
8625 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8626 }
8627
8628 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8629 && TREE_VEC_LENGTH (packed_args) > 0)
8630 {
8631 if (complain & tf_error)
8632 error ("wrong number of template arguments (%d, should be %d)",
8633 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8634 return error_mark_node;
8635 }
8636
8637 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8638 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8639 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8640 else
8641 {
8642 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8643 TREE_CONSTANT (argument_pack) = 1;
8644 }
8645
8646 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8647 if (CHECKING_P)
8648 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8649 TREE_VEC_LENGTH (packed_args));
8650 return argument_pack;
8651 }
8652
8653 /* Returns the number of pack expansions in the template argument vector
8654 ARGS. */
8655
8656 static int
8657 pack_expansion_args_count (tree args)
8658 {
8659 int i;
8660 int count = 0;
8661 if (args)
8662 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8663 {
8664 tree elt = TREE_VEC_ELT (args, i);
8665 if (elt && PACK_EXPANSION_P (elt))
8666 ++count;
8667 }
8668 return count;
8669 }
8670
8671 /* Convert all template arguments to their appropriate types, and
8672 return a vector containing the innermost resulting template
8673 arguments. If any error occurs, return error_mark_node. Error and
8674 warning messages are issued under control of COMPLAIN.
8675
8676 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8677 for arguments not specified in ARGS. Otherwise, if
8678 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8679 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8680 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8681 ARGS. */
8682
8683 static tree
8684 coerce_template_parms (tree parms,
8685 tree args,
8686 tree in_decl,
8687 tsubst_flags_t complain,
8688 bool require_all_args,
8689 bool use_default_args)
8690 {
8691 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8692 tree orig_inner_args;
8693 tree inner_args;
8694 tree new_args;
8695 tree new_inner_args;
8696
8697 /* When used as a boolean value, indicates whether this is a
8698 variadic template parameter list. Since it's an int, we can also
8699 subtract it from nparms to get the number of non-variadic
8700 parameters. */
8701 int variadic_p = 0;
8702 int variadic_args_p = 0;
8703 int post_variadic_parms = 0;
8704
8705 /* Adjustment to nparms for fixed parameter packs. */
8706 int fixed_pack_adjust = 0;
8707 int fixed_packs = 0;
8708 int missing = 0;
8709
8710 /* Likewise for parameters with default arguments. */
8711 int default_p = 0;
8712
8713 if (args == error_mark_node)
8714 return error_mark_node;
8715
8716 nparms = TREE_VEC_LENGTH (parms);
8717
8718 /* Determine if there are any parameter packs or default arguments. */
8719 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8720 {
8721 tree parm = TREE_VEC_ELT (parms, parm_idx);
8722 if (variadic_p)
8723 ++post_variadic_parms;
8724 if (template_parameter_pack_p (TREE_VALUE (parm)))
8725 ++variadic_p;
8726 if (TREE_PURPOSE (parm))
8727 ++default_p;
8728 }
8729
8730 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8731 /* If there are no parameters that follow a parameter pack, we need to
8732 expand any argument packs so that we can deduce a parameter pack from
8733 some non-packed args followed by an argument pack, as in variadic85.C.
8734 If there are such parameters, we need to leave argument packs intact
8735 so the arguments are assigned properly. This can happen when dealing
8736 with a nested class inside a partial specialization of a class
8737 template, as in variadic92.C, or when deducing a template parameter pack
8738 from a sub-declarator, as in variadic114.C. */
8739 if (!post_variadic_parms)
8740 inner_args = expand_template_argument_pack (inner_args);
8741
8742 /* Count any pack expansion args. */
8743 variadic_args_p = pack_expansion_args_count (inner_args);
8744
8745 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8746 if ((nargs - variadic_args_p > nparms && !variadic_p)
8747 || (nargs < nparms - variadic_p
8748 && require_all_args
8749 && !variadic_args_p
8750 && (!use_default_args
8751 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8752 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8753 {
8754 bad_nargs:
8755 if (complain & tf_error)
8756 {
8757 if (variadic_p || default_p)
8758 {
8759 nparms -= variadic_p + default_p;
8760 error ("wrong number of template arguments "
8761 "(%d, should be at least %d)", nargs, nparms);
8762 }
8763 else
8764 error ("wrong number of template arguments "
8765 "(%d, should be %d)", nargs, nparms);
8766
8767 if (in_decl)
8768 inform (DECL_SOURCE_LOCATION (in_decl),
8769 "provided for %qD", in_decl);
8770 }
8771
8772 return error_mark_node;
8773 }
8774 /* We can't pass a pack expansion to a non-pack parameter of an alias
8775 template (DR 1430). */
8776 else if (in_decl
8777 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8778 || concept_definition_p (in_decl))
8779 && variadic_args_p
8780 && nargs - variadic_args_p < nparms - variadic_p)
8781 {
8782 if (complain & tf_error)
8783 {
8784 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8785 {
8786 tree arg = TREE_VEC_ELT (inner_args, i);
8787 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8788
8789 if (PACK_EXPANSION_P (arg)
8790 && !template_parameter_pack_p (parm))
8791 {
8792 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8793 error_at (location_of (arg),
8794 "pack expansion argument for non-pack parameter "
8795 "%qD of alias template %qD", parm, in_decl);
8796 else
8797 error_at (location_of (arg),
8798 "pack expansion argument for non-pack parameter "
8799 "%qD of concept %qD", parm, in_decl);
8800 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8801 goto found;
8802 }
8803 }
8804 gcc_unreachable ();
8805 found:;
8806 }
8807 return error_mark_node;
8808 }
8809
8810 /* We need to evaluate the template arguments, even though this
8811 template-id may be nested within a "sizeof". */
8812 cp_evaluated ev;
8813
8814 new_inner_args = make_tree_vec (nparms);
8815 new_args = add_outermost_template_args (args, new_inner_args);
8816 int pack_adjust = 0;
8817 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8818 {
8819 tree arg;
8820 tree parm;
8821
8822 /* Get the Ith template parameter. */
8823 parm = TREE_VEC_ELT (parms, parm_idx);
8824
8825 if (parm == error_mark_node)
8826 {
8827 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8828 continue;
8829 }
8830
8831 /* Calculate the next argument. */
8832 if (arg_idx < nargs)
8833 arg = TREE_VEC_ELT (inner_args, arg_idx);
8834 else
8835 arg = NULL_TREE;
8836
8837 if (template_parameter_pack_p (TREE_VALUE (parm))
8838 && (arg || require_all_args || !(complain & tf_partial))
8839 && !(arg && ARGUMENT_PACK_P (arg)))
8840 {
8841 /* Some arguments will be placed in the
8842 template parameter pack PARM. */
8843 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8844 inner_args, arg_idx,
8845 new_args, &lost,
8846 in_decl, complain);
8847
8848 if (arg == NULL_TREE)
8849 {
8850 /* We don't know how many args we have yet, just use the
8851 unconverted (and still packed) ones for now. */
8852 new_inner_args = orig_inner_args;
8853 arg_idx = nargs;
8854 break;
8855 }
8856
8857 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8858
8859 /* Store this argument. */
8860 if (arg == error_mark_node)
8861 {
8862 lost++;
8863 /* We are done with all of the arguments. */
8864 arg_idx = nargs;
8865 break;
8866 }
8867 else
8868 {
8869 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8870 arg_idx += pack_adjust;
8871 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8872 {
8873 ++fixed_packs;
8874 fixed_pack_adjust += pack_adjust;
8875 }
8876 }
8877
8878 continue;
8879 }
8880 else if (arg)
8881 {
8882 if (PACK_EXPANSION_P (arg))
8883 {
8884 /* "If every valid specialization of a variadic template
8885 requires an empty template parameter pack, the template is
8886 ill-formed, no diagnostic required." So check that the
8887 pattern works with this parameter. */
8888 tree pattern = PACK_EXPANSION_PATTERN (arg);
8889 tree conv = convert_template_argument (TREE_VALUE (parm),
8890 pattern, new_args,
8891 complain, parm_idx,
8892 in_decl);
8893 if (conv == error_mark_node)
8894 {
8895 if (complain & tf_error)
8896 inform (input_location, "so any instantiation with a "
8897 "non-empty parameter pack would be ill-formed");
8898 ++lost;
8899 }
8900 else if (TYPE_P (conv) && !TYPE_P (pattern))
8901 /* Recover from missing typename. */
8902 TREE_VEC_ELT (inner_args, arg_idx)
8903 = make_pack_expansion (conv, complain);
8904
8905 /* We don't know how many args we have yet, just
8906 use the unconverted ones for now. */
8907 new_inner_args = inner_args;
8908 arg_idx = nargs;
8909 break;
8910 }
8911 }
8912 else if (require_all_args)
8913 {
8914 /* There must be a default arg in this case. */
8915 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8916 complain, in_decl);
8917 /* The position of the first default template argument,
8918 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8919 Record that. */
8920 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8921 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8922 arg_idx - pack_adjust);
8923 }
8924 else
8925 break;
8926
8927 if (arg == error_mark_node)
8928 {
8929 if (complain & tf_error)
8930 error ("template argument %d is invalid", arg_idx + 1);
8931 }
8932 else if (!arg)
8933 {
8934 /* This can occur if there was an error in the template
8935 parameter list itself (which we would already have
8936 reported) that we are trying to recover from, e.g., a class
8937 template with a parameter list such as
8938 template<typename..., typename> (cpp0x/variadic150.C). */
8939 ++lost;
8940
8941 /* This can also happen with a fixed parameter pack (71834). */
8942 if (arg_idx >= nargs)
8943 ++missing;
8944 }
8945 else
8946 arg = convert_template_argument (TREE_VALUE (parm),
8947 arg, new_args, complain,
8948 parm_idx, in_decl);
8949
8950 if (arg == error_mark_node)
8951 lost++;
8952
8953 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8954 }
8955
8956 if (missing || arg_idx < nargs - variadic_args_p)
8957 {
8958 /* If we had fixed parameter packs, we didn't know how many arguments we
8959 actually needed earlier; now we do. */
8960 nparms += fixed_pack_adjust;
8961 variadic_p -= fixed_packs;
8962 goto bad_nargs;
8963 }
8964
8965 if (arg_idx < nargs)
8966 {
8967 /* We had some pack expansion arguments that will only work if the packs
8968 are empty, but wait until instantiation time to complain.
8969 See variadic-ttp3.C. */
8970
8971 /* Except that we can't provide empty packs to alias templates or
8972 concepts when there are no corresponding parameters. Basically,
8973 we can get here with this:
8974
8975 template<typename T> concept C = true;
8976
8977 template<typename... Args>
8978 requires C<Args...>
8979 void f();
8980
8981 When parsing C<Args...>, we try to form a concept check of
8982 C<?, Args...>. Without the extra check for substituting an empty
8983 pack past the last parameter, we can accept the check as valid.
8984
8985 FIXME: This may be valid for alias templates (but I doubt it).
8986
8987 FIXME: The error could be better also. */
8988 if (in_decl && concept_definition_p (in_decl))
8989 {
8990 if (complain & tf_error)
8991 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8992 "too many arguments");
8993 return error_mark_node;
8994 }
8995
8996 int len = nparms + (nargs - arg_idx);
8997 tree args = make_tree_vec (len);
8998 int i = 0;
8999 for (; i < nparms; ++i)
9000 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9001 for (; i < len; ++i, ++arg_idx)
9002 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9003 arg_idx - pack_adjust);
9004 new_inner_args = args;
9005 }
9006
9007 if (lost)
9008 {
9009 gcc_assert (!(complain & tf_error) || seen_error ());
9010 return error_mark_node;
9011 }
9012
9013 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9014 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9015 TREE_VEC_LENGTH (new_inner_args));
9016
9017 return new_inner_args;
9018 }
9019
9020 /* Convert all template arguments to their appropriate types, and
9021 return a vector containing the innermost resulting template
9022 arguments. If any error occurs, return error_mark_node. Error and
9023 warning messages are not issued.
9024
9025 Note that no function argument deduction is performed, and default
9026 arguments are used to fill in unspecified arguments. */
9027 tree
9028 coerce_template_parms (tree parms, tree args, tree in_decl)
9029 {
9030 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9031 }
9032
9033 /* Convert all template arguments to their appropriate type, and
9034 instantiate default arguments as needed. This returns a vector
9035 containing the innermost resulting template arguments, or
9036 error_mark_node if unsuccessful. */
9037 tree
9038 coerce_template_parms (tree parms, tree args, tree in_decl,
9039 tsubst_flags_t complain)
9040 {
9041 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9042 }
9043
9044 /* Like coerce_template_parms. If PARMS represents all template
9045 parameters levels, this function returns a vector of vectors
9046 representing all the resulting argument levels. Note that in this
9047 case, only the innermost arguments are coerced because the
9048 outermost ones are supposed to have been coerced already.
9049
9050 Otherwise, if PARMS represents only (the innermost) vector of
9051 parameters, this function returns a vector containing just the
9052 innermost resulting arguments. */
9053
9054 static tree
9055 coerce_innermost_template_parms (tree parms,
9056 tree args,
9057 tree in_decl,
9058 tsubst_flags_t complain,
9059 bool require_all_args,
9060 bool use_default_args)
9061 {
9062 int parms_depth = TMPL_PARMS_DEPTH (parms);
9063 int args_depth = TMPL_ARGS_DEPTH (args);
9064 tree coerced_args;
9065
9066 if (parms_depth > 1)
9067 {
9068 coerced_args = make_tree_vec (parms_depth);
9069 tree level;
9070 int cur_depth;
9071
9072 for (level = parms, cur_depth = parms_depth;
9073 parms_depth > 0 && level != NULL_TREE;
9074 level = TREE_CHAIN (level), --cur_depth)
9075 {
9076 tree l;
9077 if (cur_depth == args_depth)
9078 l = coerce_template_parms (TREE_VALUE (level),
9079 args, in_decl, complain,
9080 require_all_args,
9081 use_default_args);
9082 else
9083 l = TMPL_ARGS_LEVEL (args, cur_depth);
9084
9085 if (l == error_mark_node)
9086 return error_mark_node;
9087
9088 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9089 }
9090 }
9091 else
9092 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9093 args, in_decl, complain,
9094 require_all_args,
9095 use_default_args);
9096 return coerced_args;
9097 }
9098
9099 /* Returns true if T is a wrapper to make a C++20 template parameter
9100 object const. */
9101
9102 static bool
9103 class_nttp_const_wrapper_p (tree t)
9104 {
9105 if (cxx_dialect < cxx20)
9106 return false;
9107 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9108 && CP_TYPE_CONST_P (TREE_TYPE (t))
9109 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9110 }
9111
9112 /* Returns 1 if template args OT and NT are equivalent. */
9113
9114 int
9115 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9116 {
9117 if (nt == ot)
9118 return 1;
9119 if (nt == NULL_TREE || ot == NULL_TREE)
9120 return false;
9121 if (nt == any_targ_node || ot == any_targ_node)
9122 return true;
9123
9124 if (class_nttp_const_wrapper_p (nt))
9125 nt = TREE_OPERAND (nt, 0);
9126 if (class_nttp_const_wrapper_p (ot))
9127 ot = TREE_OPERAND (ot, 0);
9128
9129 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9130 /* For member templates */
9131 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9132 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9133 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9134 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9135 PACK_EXPANSION_PATTERN (nt))
9136 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9137 PACK_EXPANSION_EXTRA_ARGS (nt)));
9138 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9139 return cp_tree_equal (ot, nt);
9140 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9141 gcc_unreachable ();
9142 else if (TYPE_P (nt) || TYPE_P (ot))
9143 {
9144 if (!(TYPE_P (nt) && TYPE_P (ot)))
9145 return false;
9146 /* Don't treat an alias template specialization with dependent
9147 arguments as equivalent to its underlying type when used as a
9148 template argument; we need them to be distinct so that we
9149 substitute into the specialization arguments at instantiation
9150 time. And aliases can't be equivalent without being ==, so
9151 we don't need to look any deeper.
9152
9153 During partial ordering, however, we need to treat them normally so
9154 that we can order uses of the same alias with different
9155 cv-qualification (79960). */
9156 if (!partial_order
9157 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9158 return false;
9159 else
9160 return same_type_p (ot, nt);
9161 }
9162 else
9163 {
9164 /* Try to treat a template non-type argument that has been converted
9165 to the parameter type as equivalent to one that hasn't yet. */
9166 for (enum tree_code code1 = TREE_CODE (ot);
9167 CONVERT_EXPR_CODE_P (code1)
9168 || code1 == NON_LVALUE_EXPR;
9169 code1 = TREE_CODE (ot))
9170 ot = TREE_OPERAND (ot, 0);
9171
9172 for (enum tree_code code2 = TREE_CODE (nt);
9173 CONVERT_EXPR_CODE_P (code2)
9174 || code2 == NON_LVALUE_EXPR;
9175 code2 = TREE_CODE (nt))
9176 nt = TREE_OPERAND (nt, 0);
9177
9178 return cp_tree_equal (ot, nt);
9179 }
9180 }
9181
9182 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9183 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9184 NEWARG_PTR with the offending arguments if they are non-NULL. */
9185
9186 int
9187 comp_template_args (tree oldargs, tree newargs,
9188 tree *oldarg_ptr, tree *newarg_ptr,
9189 bool partial_order)
9190 {
9191 int i;
9192
9193 if (oldargs == newargs)
9194 return 1;
9195
9196 if (!oldargs || !newargs)
9197 return 0;
9198
9199 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9200 return 0;
9201
9202 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9203 {
9204 tree nt = TREE_VEC_ELT (newargs, i);
9205 tree ot = TREE_VEC_ELT (oldargs, i);
9206
9207 if (! template_args_equal (ot, nt, partial_order))
9208 {
9209 if (oldarg_ptr != NULL)
9210 *oldarg_ptr = ot;
9211 if (newarg_ptr != NULL)
9212 *newarg_ptr = nt;
9213 return 0;
9214 }
9215 }
9216 return 1;
9217 }
9218
9219 inline bool
9220 comp_template_args_porder (tree oargs, tree nargs)
9221 {
9222 return comp_template_args (oargs, nargs, NULL, NULL, true);
9223 }
9224
9225 /* Implement a freelist interface for objects of type T.
9226
9227 Head is a separate object, rather than a regular member, so that we
9228 can define it as a GTY deletable pointer, which is highly
9229 desirable. A data member could be declared that way, but then the
9230 containing object would implicitly get GTY((user)), which would
9231 prevent us from instantiating freelists as global objects.
9232 Although this way we can create freelist global objects, they're
9233 such thin wrappers that instantiating temporaries at every use
9234 loses nothing and saves permanent storage for the freelist object.
9235
9236 Member functions next, anew, poison and reinit have default
9237 implementations that work for most of the types we're interested
9238 in, but if they don't work for some type, they should be explicitly
9239 specialized. See the comments before them for requirements, and
9240 the example specializations for the tree_list_freelist. */
9241 template <typename T>
9242 class freelist
9243 {
9244 /* Return the next object in a chain. We could just do type
9245 punning, but if we access the object with its underlying type, we
9246 avoid strict-aliasing trouble. This needs only work between
9247 poison and reinit. */
9248 static T *&next (T *obj) { return obj->next; }
9249
9250 /* Return a newly allocated, uninitialized or minimally-initialized
9251 object of type T. Any initialization performed by anew should
9252 either remain across the life of the object and the execution of
9253 poison, or be redone by reinit. */
9254 static T *anew () { return ggc_alloc<T> (); }
9255
9256 /* Optionally scribble all over the bits holding the object, so that
9257 they become (mostly?) uninitialized memory. This is called while
9258 preparing to make the object part of the free list. */
9259 static void poison (T *obj) {
9260 T *p ATTRIBUTE_UNUSED = obj;
9261 T **q ATTRIBUTE_UNUSED = &next (obj);
9262
9263 #ifdef ENABLE_GC_CHECKING
9264 /* Poison the data, to indicate the data is garbage. */
9265 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9266 memset (p, 0xa5, sizeof (*p));
9267 #endif
9268 /* Let valgrind know the object is free. */
9269 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9270
9271 /* Let valgrind know the next portion of the object is available,
9272 but uninitialized. */
9273 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9274 }
9275
9276 /* Bring an object that underwent at least one lifecycle after anew
9277 and before the most recent free and poison, back to a usable
9278 state, reinitializing whatever is needed for it to be
9279 functionally equivalent to an object just allocated and returned
9280 by anew. This may poison or clear the next field, used by
9281 freelist housekeeping after poison was called. */
9282 static void reinit (T *obj) {
9283 T **q ATTRIBUTE_UNUSED = &next (obj);
9284
9285 #ifdef ENABLE_GC_CHECKING
9286 memset (q, 0xa5, sizeof (*q));
9287 #endif
9288 /* Let valgrind know the entire object is available, but
9289 uninitialized. */
9290 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9291 }
9292
9293 /* Reference a GTY-deletable pointer that points to the first object
9294 in the free list proper. */
9295 T *&head;
9296 public:
9297 /* Construct a freelist object chaining objects off of HEAD. */
9298 freelist (T *&head) : head(head) {}
9299
9300 /* Add OBJ to the free object list. The former head becomes OBJ's
9301 successor. */
9302 void free (T *obj)
9303 {
9304 poison (obj);
9305 next (obj) = head;
9306 head = obj;
9307 }
9308
9309 /* Take an object from the free list, if one is available, or
9310 allocate a new one. Objects taken from the free list should be
9311 regarded as filled with garbage, except for bits that are
9312 configured to be preserved across free and alloc. */
9313 T *alloc ()
9314 {
9315 if (head)
9316 {
9317 T *obj = head;
9318 head = next (head);
9319 reinit (obj);
9320 return obj;
9321 }
9322 else
9323 return anew ();
9324 }
9325 };
9326
9327 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9328 want to allocate a TREE_LIST using the usual interface, and ensure
9329 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9330 build_tree_list logic in reinit, so this could go out of sync. */
9331 template <>
9332 inline tree &
9333 freelist<tree_node>::next (tree obj)
9334 {
9335 return TREE_CHAIN (obj);
9336 }
9337 template <>
9338 inline tree
9339 freelist<tree_node>::anew ()
9340 {
9341 return build_tree_list (NULL, NULL);
9342 }
9343 template <>
9344 inline void
9345 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9346 {
9347 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9348 tree p ATTRIBUTE_UNUSED = obj;
9349 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9350 tree *q ATTRIBUTE_UNUSED = &next (obj);
9351
9352 #ifdef ENABLE_GC_CHECKING
9353 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9354
9355 /* Poison the data, to indicate the data is garbage. */
9356 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9357 memset (p, 0xa5, size);
9358 #endif
9359 /* Let valgrind know the object is free. */
9360 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9361 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9362 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9363 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9364
9365 #ifdef ENABLE_GC_CHECKING
9366 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9367 /* Keep TREE_CHAIN functional. */
9368 TREE_SET_CODE (obj, TREE_LIST);
9369 #else
9370 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9371 #endif
9372 }
9373 template <>
9374 inline void
9375 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9376 {
9377 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9378
9379 #ifdef ENABLE_GC_CHECKING
9380 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9381 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9382 memset (obj, 0, sizeof (tree_list));
9383 #endif
9384
9385 /* Let valgrind know the entire object is available, but
9386 uninitialized. */
9387 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9388
9389 #ifdef ENABLE_GC_CHECKING
9390 TREE_SET_CODE (obj, TREE_LIST);
9391 #else
9392 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9393 #endif
9394 }
9395
9396 /* Point to the first object in the TREE_LIST freelist. */
9397 static GTY((deletable)) tree tree_list_freelist_head;
9398 /* Return the/an actual TREE_LIST freelist. */
9399 static inline freelist<tree_node>
9400 tree_list_freelist ()
9401 {
9402 return tree_list_freelist_head;
9403 }
9404
9405 /* Point to the first object in the tinst_level freelist. */
9406 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9407 /* Return the/an actual tinst_level freelist. */
9408 static inline freelist<tinst_level>
9409 tinst_level_freelist ()
9410 {
9411 return tinst_level_freelist_head;
9412 }
9413
9414 /* Point to the first object in the pending_template freelist. */
9415 static GTY((deletable)) pending_template *pending_template_freelist_head;
9416 /* Return the/an actual pending_template freelist. */
9417 static inline freelist<pending_template>
9418 pending_template_freelist ()
9419 {
9420 return pending_template_freelist_head;
9421 }
9422
9423 /* Build the TREE_LIST object out of a split list, store it
9424 permanently, and return it. */
9425 tree
9426 tinst_level::to_list ()
9427 {
9428 gcc_assert (split_list_p ());
9429 tree ret = tree_list_freelist ().alloc ();
9430 TREE_PURPOSE (ret) = tldcl;
9431 TREE_VALUE (ret) = targs;
9432 tldcl = ret;
9433 targs = NULL;
9434 gcc_assert (tree_list_p ());
9435 return ret;
9436 }
9437
9438 const unsigned short tinst_level::refcount_infinity;
9439
9440 /* Increment OBJ's refcount unless it is already infinite. */
9441 static tinst_level *
9442 inc_refcount_use (tinst_level *obj)
9443 {
9444 if (obj && obj->refcount != tinst_level::refcount_infinity)
9445 ++obj->refcount;
9446 return obj;
9447 }
9448
9449 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9450 void
9451 tinst_level::free (tinst_level *obj)
9452 {
9453 if (obj->tree_list_p ())
9454 tree_list_freelist ().free (obj->get_node ());
9455 tinst_level_freelist ().free (obj);
9456 }
9457
9458 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9459 OBJ's DECL and OBJ, and start over with the tinst_level object that
9460 used to be referenced by OBJ's NEXT. */
9461 static void
9462 dec_refcount_use (tinst_level *obj)
9463 {
9464 while (obj
9465 && obj->refcount != tinst_level::refcount_infinity
9466 && !--obj->refcount)
9467 {
9468 tinst_level *next = obj->next;
9469 tinst_level::free (obj);
9470 obj = next;
9471 }
9472 }
9473
9474 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9475 and of the former PTR. Omitting the second argument is equivalent
9476 to passing (T*)NULL; this is allowed because passing the
9477 zero-valued integral constant NULL confuses type deduction and/or
9478 overload resolution. */
9479 template <typename T>
9480 static void
9481 set_refcount_ptr (T *& ptr, T *obj = NULL)
9482 {
9483 T *save = ptr;
9484 ptr = inc_refcount_use (obj);
9485 dec_refcount_use (save);
9486 }
9487
9488 static void
9489 add_pending_template (tree d)
9490 {
9491 tree ti = (TYPE_P (d)
9492 ? CLASSTYPE_TEMPLATE_INFO (d)
9493 : DECL_TEMPLATE_INFO (d));
9494 struct pending_template *pt;
9495 int level;
9496
9497 if (TI_PENDING_TEMPLATE_FLAG (ti))
9498 return;
9499
9500 /* We are called both from instantiate_decl, where we've already had a
9501 tinst_level pushed, and instantiate_template, where we haven't.
9502 Compensate. */
9503 gcc_assert (TREE_CODE (d) != TREE_LIST);
9504 level = !current_tinst_level
9505 || current_tinst_level->maybe_get_node () != d;
9506
9507 if (level)
9508 push_tinst_level (d);
9509
9510 pt = pending_template_freelist ().alloc ();
9511 pt->next = NULL;
9512 pt->tinst = NULL;
9513 set_refcount_ptr (pt->tinst, current_tinst_level);
9514 if (last_pending_template)
9515 last_pending_template->next = pt;
9516 else
9517 pending_templates = pt;
9518
9519 last_pending_template = pt;
9520
9521 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9522
9523 if (level)
9524 pop_tinst_level ();
9525 }
9526
9527
9528 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9529 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9530 documentation for TEMPLATE_ID_EXPR. */
9531
9532 tree
9533 lookup_template_function (tree fns, tree arglist)
9534 {
9535 if (fns == error_mark_node || arglist == error_mark_node)
9536 return error_mark_node;
9537
9538 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9539
9540 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9541 {
9542 error ("%q#D is not a function template", fns);
9543 return error_mark_node;
9544 }
9545
9546 if (BASELINK_P (fns))
9547 {
9548 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9549 unknown_type_node,
9550 BASELINK_FUNCTIONS (fns),
9551 arglist);
9552 return fns;
9553 }
9554
9555 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9556 }
9557
9558 /* Within the scope of a template class S<T>, the name S gets bound
9559 (in build_self_reference) to a TYPE_DECL for the class, not a
9560 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9561 or one of its enclosing classes, and that type is a template,
9562 return the associated TEMPLATE_DECL. Otherwise, the original
9563 DECL is returned.
9564
9565 Also handle the case when DECL is a TREE_LIST of ambiguous
9566 injected-class-names from different bases. */
9567
9568 tree
9569 maybe_get_template_decl_from_type_decl (tree decl)
9570 {
9571 if (decl == NULL_TREE)
9572 return decl;
9573
9574 /* DR 176: A lookup that finds an injected-class-name (10.2
9575 [class.member.lookup]) can result in an ambiguity in certain cases
9576 (for example, if it is found in more than one base class). If all of
9577 the injected-class-names that are found refer to specializations of
9578 the same class template, and if the name is followed by a
9579 template-argument-list, the reference refers to the class template
9580 itself and not a specialization thereof, and is not ambiguous. */
9581 if (TREE_CODE (decl) == TREE_LIST)
9582 {
9583 tree t, tmpl = NULL_TREE;
9584 for (t = decl; t; t = TREE_CHAIN (t))
9585 {
9586 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9587 if (!tmpl)
9588 tmpl = elt;
9589 else if (tmpl != elt)
9590 break;
9591 }
9592 if (tmpl && t == NULL_TREE)
9593 return tmpl;
9594 else
9595 return decl;
9596 }
9597
9598 return (decl != NULL_TREE
9599 && DECL_SELF_REFERENCE_P (decl)
9600 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9601 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9602 }
9603
9604 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9605 parameters, find the desired type.
9606
9607 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9608
9609 IN_DECL, if non-NULL, is the template declaration we are trying to
9610 instantiate.
9611
9612 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9613 the class we are looking up.
9614
9615 Issue error and warning messages under control of COMPLAIN.
9616
9617 If the template class is really a local class in a template
9618 function, then the FUNCTION_CONTEXT is the function in which it is
9619 being instantiated.
9620
9621 ??? Note that this function is currently called *twice* for each
9622 template-id: the first time from the parser, while creating the
9623 incomplete type (finish_template_type), and the second type during the
9624 real instantiation (instantiate_template_class). This is surely something
9625 that we want to avoid. It also causes some problems with argument
9626 coercion (see convert_nontype_argument for more information on this). */
9627
9628 static tree
9629 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9630 int entering_scope, tsubst_flags_t complain)
9631 {
9632 tree templ = NULL_TREE, parmlist;
9633 tree t;
9634 spec_entry **slot;
9635 spec_entry *entry;
9636 spec_entry elt;
9637 hashval_t hash;
9638
9639 if (identifier_p (d1))
9640 {
9641 tree value = innermost_non_namespace_value (d1);
9642 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9643 templ = value;
9644 else
9645 {
9646 if (context)
9647 push_decl_namespace (context);
9648 templ = lookup_name (d1);
9649 templ = maybe_get_template_decl_from_type_decl (templ);
9650 if (context)
9651 pop_decl_namespace ();
9652 }
9653 if (templ)
9654 context = DECL_CONTEXT (templ);
9655 }
9656 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9657 {
9658 tree type = TREE_TYPE (d1);
9659
9660 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9661 an implicit typename for the second A. Deal with it. */
9662 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9663 type = TREE_TYPE (type);
9664
9665 if (CLASSTYPE_TEMPLATE_INFO (type))
9666 {
9667 templ = CLASSTYPE_TI_TEMPLATE (type);
9668 d1 = DECL_NAME (templ);
9669 }
9670 }
9671 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9672 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9673 {
9674 templ = TYPE_TI_TEMPLATE (d1);
9675 d1 = DECL_NAME (templ);
9676 }
9677 else if (DECL_TYPE_TEMPLATE_P (d1))
9678 {
9679 templ = d1;
9680 d1 = DECL_NAME (templ);
9681 context = DECL_CONTEXT (templ);
9682 }
9683 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9684 {
9685 templ = d1;
9686 d1 = DECL_NAME (templ);
9687 }
9688
9689 /* Issue an error message if we didn't find a template. */
9690 if (! templ)
9691 {
9692 if (complain & tf_error)
9693 error ("%qT is not a template", d1);
9694 return error_mark_node;
9695 }
9696
9697 if (TREE_CODE (templ) != TEMPLATE_DECL
9698 /* Make sure it's a user visible template, if it was named by
9699 the user. */
9700 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9701 && !PRIMARY_TEMPLATE_P (templ)))
9702 {
9703 if (complain & tf_error)
9704 {
9705 error ("non-template type %qT used as a template", d1);
9706 if (in_decl)
9707 error ("for template declaration %q+D", in_decl);
9708 }
9709 return error_mark_node;
9710 }
9711
9712 complain &= ~tf_user;
9713
9714 /* An alias that just changes the name of a template is equivalent to the
9715 other template, so if any of the arguments are pack expansions, strip
9716 the alias to avoid problems with a pack expansion passed to a non-pack
9717 alias template parameter (DR 1430). */
9718 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9719 templ = get_underlying_template (templ);
9720
9721 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9722 {
9723 tree parm;
9724 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9725 if (arglist2 == error_mark_node
9726 || (!uses_template_parms (arglist2)
9727 && check_instantiated_args (templ, arglist2, complain)))
9728 return error_mark_node;
9729
9730 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9731 return parm;
9732 }
9733 else
9734 {
9735 tree template_type = TREE_TYPE (templ);
9736 tree gen_tmpl;
9737 tree type_decl;
9738 tree found = NULL_TREE;
9739 int arg_depth;
9740 int parm_depth;
9741 int is_dependent_type;
9742 int use_partial_inst_tmpl = false;
9743
9744 if (template_type == error_mark_node)
9745 /* An error occurred while building the template TEMPL, and a
9746 diagnostic has most certainly been emitted for that
9747 already. Let's propagate that error. */
9748 return error_mark_node;
9749
9750 gen_tmpl = most_general_template (templ);
9751 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9752 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9753 arg_depth = TMPL_ARGS_DEPTH (arglist);
9754
9755 if (arg_depth == 1 && parm_depth > 1)
9756 {
9757 /* We've been given an incomplete set of template arguments.
9758 For example, given:
9759
9760 template <class T> struct S1 {
9761 template <class U> struct S2 {};
9762 template <class U> struct S2<U*> {};
9763 };
9764
9765 we will be called with an ARGLIST of `U*', but the
9766 TEMPLATE will be `template <class T> template
9767 <class U> struct S1<T>::S2'. We must fill in the missing
9768 arguments. */
9769 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9770 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9771 arg_depth = TMPL_ARGS_DEPTH (arglist);
9772 }
9773
9774 /* Now we should have enough arguments. */
9775 gcc_assert (parm_depth == arg_depth);
9776
9777 /* From here on, we're only interested in the most general
9778 template. */
9779
9780 /* Calculate the BOUND_ARGS. These will be the args that are
9781 actually tsubst'd into the definition to create the
9782 instantiation. */
9783 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9784 complain,
9785 /*require_all_args=*/true,
9786 /*use_default_args=*/true);
9787
9788 if (arglist == error_mark_node)
9789 /* We were unable to bind the arguments. */
9790 return error_mark_node;
9791
9792 /* In the scope of a template class, explicit references to the
9793 template class refer to the type of the template, not any
9794 instantiation of it. For example, in:
9795
9796 template <class T> class C { void f(C<T>); }
9797
9798 the `C<T>' is just the same as `C'. Outside of the
9799 class, however, such a reference is an instantiation. */
9800 if (entering_scope
9801 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9802 || currently_open_class (template_type))
9803 {
9804 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9805
9806 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9807 return template_type;
9808 }
9809
9810 /* If we already have this specialization, return it. */
9811 elt.tmpl = gen_tmpl;
9812 elt.args = arglist;
9813 elt.spec = NULL_TREE;
9814 hash = spec_hasher::hash (&elt);
9815 entry = type_specializations->find_with_hash (&elt, hash);
9816
9817 if (entry)
9818 return entry->spec;
9819
9820 /* If the template's constraints are not satisfied,
9821 then we cannot form a valid type.
9822
9823 Note that the check is deferred until after the hash
9824 lookup. This prevents redundant checks on previously
9825 instantiated specializations. */
9826 if (flag_concepts
9827 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9828 && !constraints_satisfied_p (gen_tmpl, arglist))
9829 {
9830 if (complain & tf_error)
9831 {
9832 auto_diagnostic_group d;
9833 error ("template constraint failure for %qD", gen_tmpl);
9834 diagnose_constraints (input_location, gen_tmpl, arglist);
9835 }
9836 return error_mark_node;
9837 }
9838
9839 is_dependent_type = uses_template_parms (arglist);
9840
9841 /* If the deduced arguments are invalid, then the binding
9842 failed. */
9843 if (!is_dependent_type
9844 && check_instantiated_args (gen_tmpl,
9845 INNERMOST_TEMPLATE_ARGS (arglist),
9846 complain))
9847 return error_mark_node;
9848
9849 if (!is_dependent_type
9850 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9851 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9852 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9853 {
9854 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9855 DECL_NAME (gen_tmpl),
9856 /*tag_scope=*/ts_global);
9857 return found;
9858 }
9859
9860 context = DECL_CONTEXT (gen_tmpl);
9861 if (context && TYPE_P (context))
9862 {
9863 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9864 context = complete_type (context);
9865 }
9866 else
9867 context = tsubst (context, arglist, complain, in_decl);
9868
9869 if (context == error_mark_node)
9870 return error_mark_node;
9871
9872 if (!context)
9873 context = global_namespace;
9874
9875 /* Create the type. */
9876 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9877 {
9878 /* The user referred to a specialization of an alias
9879 template represented by GEN_TMPL.
9880
9881 [temp.alias]/2 says:
9882
9883 When a template-id refers to the specialization of an
9884 alias template, it is equivalent to the associated
9885 type obtained by substitution of its
9886 template-arguments for the template-parameters in the
9887 type-id of the alias template. */
9888
9889 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9890 /* Note that the call above (by indirectly calling
9891 register_specialization in tsubst_decl) registers the
9892 TYPE_DECL representing the specialization of the alias
9893 template. So next time someone substitutes ARGLIST for
9894 the template parms into the alias template (GEN_TMPL),
9895 she'll get that TYPE_DECL back. */
9896
9897 if (t == error_mark_node)
9898 return t;
9899 }
9900 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9901 {
9902 if (!is_dependent_type)
9903 {
9904 set_current_access_from_decl (TYPE_NAME (template_type));
9905 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9906 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9907 arglist, complain, in_decl),
9908 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9909 arglist, complain, in_decl),
9910 SCOPED_ENUM_P (template_type), NULL);
9911
9912 if (t == error_mark_node)
9913 return t;
9914 }
9915 else
9916 {
9917 /* We don't want to call start_enum for this type, since
9918 the values for the enumeration constants may involve
9919 template parameters. And, no one should be interested
9920 in the enumeration constants for such a type. */
9921 t = cxx_make_type (ENUMERAL_TYPE);
9922 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9923 }
9924 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9925 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9926 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9927 }
9928 else if (CLASS_TYPE_P (template_type))
9929 {
9930 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9931 instantiated here. */
9932 gcc_assert (!LAMBDA_TYPE_P (template_type));
9933
9934 t = make_class_type (TREE_CODE (template_type));
9935 CLASSTYPE_DECLARED_CLASS (t)
9936 = CLASSTYPE_DECLARED_CLASS (template_type);
9937 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9938
9939 /* A local class. Make sure the decl gets registered properly. */
9940 if (context == current_function_decl)
9941 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9942 == error_mark_node)
9943 return error_mark_node;
9944
9945 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9946 /* This instantiation is another name for the primary
9947 template type. Set the TYPE_CANONICAL field
9948 appropriately. */
9949 TYPE_CANONICAL (t) = template_type;
9950 else if (any_template_arguments_need_structural_equality_p (arglist))
9951 /* Some of the template arguments require structural
9952 equality testing, so this template class requires
9953 structural equality testing. */
9954 SET_TYPE_STRUCTURAL_EQUALITY (t);
9955 }
9956 else
9957 gcc_unreachable ();
9958
9959 /* If we called start_enum or pushtag above, this information
9960 will already be set up. */
9961 type_decl = TYPE_NAME (t);
9962 if (!type_decl)
9963 {
9964 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9965
9966 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9967 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9968 DECL_SOURCE_LOCATION (type_decl)
9969 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9970 }
9971
9972 if (CLASS_TYPE_P (template_type))
9973 {
9974 TREE_PRIVATE (type_decl)
9975 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9976 TREE_PROTECTED (type_decl)
9977 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9978 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9979 {
9980 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9981 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9982 }
9983 }
9984
9985 if (OVERLOAD_TYPE_P (t)
9986 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9987 {
9988 static const char *tags[] = {"abi_tag", "may_alias"};
9989
9990 for (unsigned ix = 0; ix != 2; ix++)
9991 {
9992 tree attributes
9993 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9994
9995 if (attributes)
9996 TYPE_ATTRIBUTES (t)
9997 = tree_cons (TREE_PURPOSE (attributes),
9998 TREE_VALUE (attributes),
9999 TYPE_ATTRIBUTES (t));
10000 }
10001 }
10002
10003 /* Let's consider the explicit specialization of a member
10004 of a class template specialization that is implicitly instantiated,
10005 e.g.:
10006 template<class T>
10007 struct S
10008 {
10009 template<class U> struct M {}; //#0
10010 };
10011
10012 template<>
10013 template<>
10014 struct S<int>::M<char> //#1
10015 {
10016 int i;
10017 };
10018 [temp.expl.spec]/4 says this is valid.
10019
10020 In this case, when we write:
10021 S<int>::M<char> m;
10022
10023 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10024 the one of #0.
10025
10026 When we encounter #1, we want to store the partial instantiation
10027 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10028
10029 For all cases other than this "explicit specialization of member of a
10030 class template", we just want to store the most general template into
10031 the CLASSTYPE_TI_TEMPLATE of M.
10032
10033 This case of "explicit specialization of member of a class template"
10034 only happens when:
10035 1/ the enclosing class is an instantiation of, and therefore not
10036 the same as, the context of the most general template, and
10037 2/ we aren't looking at the partial instantiation itself, i.e.
10038 the innermost arguments are not the same as the innermost parms of
10039 the most general template.
10040
10041 So it's only when 1/ and 2/ happens that we want to use the partial
10042 instantiation of the member template in lieu of its most general
10043 template. */
10044
10045 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10046 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10047 /* the enclosing class must be an instantiation... */
10048 && CLASS_TYPE_P (context)
10049 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10050 {
10051 TREE_VEC_LENGTH (arglist)--;
10052 ++processing_template_decl;
10053 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10054 tree partial_inst_args =
10055 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10056 arglist, complain, NULL_TREE);
10057 --processing_template_decl;
10058 TREE_VEC_LENGTH (arglist)++;
10059 if (partial_inst_args == error_mark_node)
10060 return error_mark_node;
10061 use_partial_inst_tmpl =
10062 /*...and we must not be looking at the partial instantiation
10063 itself. */
10064 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10065 partial_inst_args);
10066 }
10067
10068 if (!use_partial_inst_tmpl)
10069 /* This case is easy; there are no member templates involved. */
10070 found = gen_tmpl;
10071 else
10072 {
10073 /* This is a full instantiation of a member template. Find
10074 the partial instantiation of which this is an instance. */
10075
10076 /* Temporarily reduce by one the number of levels in the ARGLIST
10077 so as to avoid comparing the last set of arguments. */
10078 TREE_VEC_LENGTH (arglist)--;
10079 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
10080 TREE_VEC_LENGTH (arglist)++;
10081 /* FOUND is either a proper class type, or an alias
10082 template specialization. In the later case, it's a
10083 TYPE_DECL, resulting from the substituting of arguments
10084 for parameters in the TYPE_DECL of the alias template
10085 done earlier. So be careful while getting the template
10086 of FOUND. */
10087 found = (TREE_CODE (found) == TEMPLATE_DECL
10088 ? found
10089 : (TREE_CODE (found) == TYPE_DECL
10090 ? DECL_TI_TEMPLATE (found)
10091 : CLASSTYPE_TI_TEMPLATE (found)));
10092
10093 if (DECL_CLASS_TEMPLATE_P (found)
10094 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10095 {
10096 /* If this partial instantiation is specialized, we want to
10097 use it for hash table lookup. */
10098 elt.tmpl = found;
10099 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10100 hash = spec_hasher::hash (&elt);
10101 }
10102 }
10103
10104 /* Build template info for the new specialization. */
10105 if (TYPE_ALIAS_P (t))
10106 {
10107 /* This is constructed during instantiation of the alias
10108 decl. But for member templates of template classes, that
10109 is not correct as we need to refer to the partially
10110 instantiated template, not the most general template.
10111 The incorrect knowledge will not have escaped this
10112 instantiation process, so we're good just updating the
10113 template_info we made then. */
10114 tree ti = DECL_TEMPLATE_INFO (TYPE_NAME (t));
10115 gcc_checking_assert (template_args_equal (TI_ARGS (ti), arglist));
10116 if (TI_TEMPLATE (ti) != found)
10117 {
10118 gcc_checking_assert (DECL_TI_TEMPLATE (found) == TI_TEMPLATE (ti));
10119 TI_TEMPLATE (ti) = found;
10120 }
10121 }
10122 else
10123 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10124
10125 elt.spec = t;
10126 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10127 gcc_checking_assert (*slot == NULL);
10128 entry = ggc_alloc<spec_entry> ();
10129 *entry = elt;
10130 *slot = entry;
10131
10132 /* Note this use of the partial instantiation so we can check it
10133 later in maybe_process_partial_specialization. */
10134 DECL_TEMPLATE_INSTANTIATIONS (found)
10135 = tree_cons (arglist, t,
10136 DECL_TEMPLATE_INSTANTIATIONS (found));
10137
10138 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10139 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10140 /* Now that the type has been registered on the instantiations
10141 list, we set up the enumerators. Because the enumeration
10142 constants may involve the enumeration type itself, we make
10143 sure to register the type first, and then create the
10144 constants. That way, doing tsubst_expr for the enumeration
10145 constants won't result in recursive calls here; we'll find
10146 the instantiation and exit above. */
10147 tsubst_enum (template_type, t, arglist);
10148
10149 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10150 /* If the type makes use of template parameters, the
10151 code that generates debugging information will crash. */
10152 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10153
10154 /* Possibly limit visibility based on template args. */
10155 TREE_PUBLIC (type_decl) = 1;
10156 determine_visibility (type_decl);
10157
10158 inherit_targ_abi_tags (t);
10159
10160 return t;
10161 }
10162 }
10163
10164 /* Wrapper for lookup_template_class_1. */
10165
10166 tree
10167 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10168 int entering_scope, tsubst_flags_t complain)
10169 {
10170 tree ret;
10171 timevar_push (TV_TEMPLATE_INST);
10172 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10173 entering_scope, complain);
10174 timevar_pop (TV_TEMPLATE_INST);
10175 return ret;
10176 }
10177
10178 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10179
10180 tree
10181 lookup_template_variable (tree templ, tree arglist)
10182 {
10183 if (flag_concepts && variable_concept_p (templ))
10184 return build_concept_check (templ, arglist, tf_none);
10185
10186 /* The type of the expression is NULL_TREE since the template-id could refer
10187 to an explicit or partial specialization. */
10188 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10189 }
10190
10191 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10192
10193 tree
10194 finish_template_variable (tree var, tsubst_flags_t complain)
10195 {
10196 tree templ = TREE_OPERAND (var, 0);
10197 tree arglist = TREE_OPERAND (var, 1);
10198
10199 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10200 arglist = add_outermost_template_args (tmpl_args, arglist);
10201
10202 templ = most_general_template (templ);
10203 tree parms = DECL_TEMPLATE_PARMS (templ);
10204 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10205 /*req_all*/true,
10206 /*use_default*/true);
10207 if (arglist == error_mark_node)
10208 return error_mark_node;
10209
10210 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10211 {
10212 if (complain & tf_error)
10213 {
10214 auto_diagnostic_group d;
10215 error ("use of invalid variable template %qE", var);
10216 diagnose_constraints (location_of (var), templ, arglist);
10217 }
10218 return error_mark_node;
10219 }
10220
10221 return instantiate_template (templ, arglist, complain);
10222 }
10223
10224 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10225 TARGS template args, and instantiate it if it's not dependent. */
10226
10227 tree
10228 lookup_and_finish_template_variable (tree templ, tree targs,
10229 tsubst_flags_t complain)
10230 {
10231 templ = lookup_template_variable (templ, targs);
10232 if (!any_dependent_template_arguments_p (targs))
10233 {
10234 templ = finish_template_variable (templ, complain);
10235 mark_used (templ);
10236 }
10237
10238 return convert_from_reference (templ);
10239 }
10240
10241 \f
10242 struct pair_fn_data
10243 {
10244 tree_fn_t fn;
10245 tree_fn_t any_fn;
10246 void *data;
10247 /* True when we should also visit template parameters that occur in
10248 non-deduced contexts. */
10249 bool include_nondeduced_p;
10250 hash_set<tree> *visited;
10251 };
10252
10253 /* Called from for_each_template_parm via walk_tree. */
10254
10255 static tree
10256 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10257 {
10258 tree t = *tp;
10259 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10260 tree_fn_t fn = pfd->fn;
10261 void *data = pfd->data;
10262 tree result = NULL_TREE;
10263
10264 #define WALK_SUBTREE(NODE) \
10265 do \
10266 { \
10267 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10268 pfd->include_nondeduced_p, \
10269 pfd->any_fn); \
10270 if (result) goto out; \
10271 } \
10272 while (0)
10273
10274 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10275 return t;
10276
10277 if (TYPE_P (t)
10278 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10279 WALK_SUBTREE (TYPE_CONTEXT (t));
10280
10281 switch (TREE_CODE (t))
10282 {
10283 case RECORD_TYPE:
10284 if (TYPE_PTRMEMFUNC_P (t))
10285 break;
10286 /* Fall through. */
10287
10288 case UNION_TYPE:
10289 case ENUMERAL_TYPE:
10290 if (!TYPE_TEMPLATE_INFO (t))
10291 *walk_subtrees = 0;
10292 else
10293 WALK_SUBTREE (TYPE_TI_ARGS (t));
10294 break;
10295
10296 case INTEGER_TYPE:
10297 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10298 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10299 break;
10300
10301 case METHOD_TYPE:
10302 /* Since we're not going to walk subtrees, we have to do this
10303 explicitly here. */
10304 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10305 /* Fall through. */
10306
10307 case FUNCTION_TYPE:
10308 /* Check the return type. */
10309 WALK_SUBTREE (TREE_TYPE (t));
10310
10311 /* Check the parameter types. Since default arguments are not
10312 instantiated until they are needed, the TYPE_ARG_TYPES may
10313 contain expressions that involve template parameters. But,
10314 no-one should be looking at them yet. And, once they're
10315 instantiated, they don't contain template parameters, so
10316 there's no point in looking at them then, either. */
10317 {
10318 tree parm;
10319
10320 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10321 WALK_SUBTREE (TREE_VALUE (parm));
10322
10323 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10324 want walk_tree walking into them itself. */
10325 *walk_subtrees = 0;
10326 }
10327
10328 if (flag_noexcept_type)
10329 {
10330 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10331 if (spec)
10332 WALK_SUBTREE (TREE_PURPOSE (spec));
10333 }
10334 break;
10335
10336 case TYPEOF_TYPE:
10337 case DECLTYPE_TYPE:
10338 case UNDERLYING_TYPE:
10339 if (pfd->include_nondeduced_p
10340 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10341 pfd->visited,
10342 pfd->include_nondeduced_p,
10343 pfd->any_fn))
10344 return error_mark_node;
10345 *walk_subtrees = false;
10346 break;
10347
10348 case FUNCTION_DECL:
10349 case VAR_DECL:
10350 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10351 WALK_SUBTREE (DECL_TI_ARGS (t));
10352 /* Fall through. */
10353
10354 case PARM_DECL:
10355 case CONST_DECL:
10356 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10357 WALK_SUBTREE (DECL_INITIAL (t));
10358 if (DECL_CONTEXT (t)
10359 && pfd->include_nondeduced_p)
10360 WALK_SUBTREE (DECL_CONTEXT (t));
10361 break;
10362
10363 case BOUND_TEMPLATE_TEMPLATE_PARM:
10364 /* Record template parameters such as `T' inside `TT<T>'. */
10365 WALK_SUBTREE (TYPE_TI_ARGS (t));
10366 /* Fall through. */
10367
10368 case TEMPLATE_TEMPLATE_PARM:
10369 case TEMPLATE_TYPE_PARM:
10370 case TEMPLATE_PARM_INDEX:
10371 if (fn && (*fn)(t, data))
10372 return t;
10373 else if (!fn)
10374 return t;
10375 break;
10376
10377 case TEMPLATE_DECL:
10378 /* A template template parameter is encountered. */
10379 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10380 WALK_SUBTREE (TREE_TYPE (t));
10381
10382 /* Already substituted template template parameter */
10383 *walk_subtrees = 0;
10384 break;
10385
10386 case TYPENAME_TYPE:
10387 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10388 partial instantiation. */
10389 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10390 *walk_subtrees = 0;
10391 break;
10392
10393 case CONSTRUCTOR:
10394 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10395 && pfd->include_nondeduced_p)
10396 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10397 break;
10398
10399 case INDIRECT_REF:
10400 case COMPONENT_REF:
10401 /* If there's no type, then this thing must be some expression
10402 involving template parameters. */
10403 if (!fn && !TREE_TYPE (t))
10404 return error_mark_node;
10405 break;
10406
10407 case MODOP_EXPR:
10408 case CAST_EXPR:
10409 case IMPLICIT_CONV_EXPR:
10410 case REINTERPRET_CAST_EXPR:
10411 case CONST_CAST_EXPR:
10412 case STATIC_CAST_EXPR:
10413 case DYNAMIC_CAST_EXPR:
10414 case ARROW_EXPR:
10415 case DOTSTAR_EXPR:
10416 case TYPEID_EXPR:
10417 case PSEUDO_DTOR_EXPR:
10418 if (!fn)
10419 return error_mark_node;
10420 break;
10421
10422 case SCOPE_REF:
10423 if (pfd->include_nondeduced_p)
10424 WALK_SUBTREE (TREE_OPERAND (t, 0));
10425 break;
10426
10427 case REQUIRES_EXPR:
10428 {
10429 if (!fn)
10430 return error_mark_node;
10431
10432 /* Recursively walk the type of each constraint variable. */
10433 tree p = TREE_OPERAND (t, 0);
10434 while (p)
10435 {
10436 WALK_SUBTREE (TREE_TYPE (p));
10437 p = TREE_CHAIN (p);
10438 }
10439 }
10440 break;
10441
10442 default:
10443 break;
10444 }
10445
10446 #undef WALK_SUBTREE
10447
10448 /* We didn't find any template parameters we liked. */
10449 out:
10450 return result;
10451 }
10452
10453 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10454 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10455 call FN with the parameter and the DATA.
10456 If FN returns nonzero, the iteration is terminated, and
10457 for_each_template_parm returns 1. Otherwise, the iteration
10458 continues. If FN never returns a nonzero value, the value
10459 returned by for_each_template_parm is 0. If FN is NULL, it is
10460 considered to be the function which always returns 1.
10461
10462 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10463 parameters that occur in non-deduced contexts. When false, only
10464 visits those template parameters that can be deduced. */
10465
10466 static tree
10467 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10468 hash_set<tree> *visited,
10469 bool include_nondeduced_p,
10470 tree_fn_t any_fn)
10471 {
10472 struct pair_fn_data pfd;
10473 tree result;
10474
10475 /* Set up. */
10476 pfd.fn = fn;
10477 pfd.any_fn = any_fn;
10478 pfd.data = data;
10479 pfd.include_nondeduced_p = include_nondeduced_p;
10480
10481 /* Walk the tree. (Conceptually, we would like to walk without
10482 duplicates, but for_each_template_parm_r recursively calls
10483 for_each_template_parm, so we would need to reorganize a fair
10484 bit to use walk_tree_without_duplicates, so we keep our own
10485 visited list.) */
10486 if (visited)
10487 pfd.visited = visited;
10488 else
10489 pfd.visited = new hash_set<tree>;
10490 result = cp_walk_tree (&t,
10491 for_each_template_parm_r,
10492 &pfd,
10493 pfd.visited);
10494
10495 /* Clean up. */
10496 if (!visited)
10497 {
10498 delete pfd.visited;
10499 pfd.visited = 0;
10500 }
10501
10502 return result;
10503 }
10504
10505 struct find_template_parameter_info
10506 {
10507 explicit find_template_parameter_info (tree ctx_parms)
10508 : parm_list (NULL_TREE),
10509 ctx_parms (ctx_parms),
10510 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10511 {}
10512
10513 hash_set<tree> visited;
10514 hash_set<tree> parms;
10515 tree parm_list;
10516 tree ctx_parms;
10517 int max_depth;
10518 };
10519
10520 /* Appends the declaration of T to the list in DATA. */
10521
10522 static int
10523 keep_template_parm (tree t, void* data)
10524 {
10525 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10526
10527 /* Template parameters declared within the expression are not part of
10528 the parameter mapping. For example, in this concept:
10529
10530 template<typename T>
10531 concept C = requires { <expr> } -> same_as<int>;
10532
10533 the return specifier same_as<int> declares a new decltype parameter
10534 that must not be part of the parameter mapping. The same is true
10535 for generic lambda parameters, lambda template parameters, etc. */
10536 int level;
10537 int index;
10538 template_parm_level_and_index (t, &level, &index);
10539 if (level > ftpi->max_depth)
10540 return 0;
10541
10542 /* Arguments like const T yield parameters like const T. This means that
10543 a template-id like X<T, const T> would yield two distinct parameters:
10544 T and const T. Adjust types to their unqualified versions. */
10545 if (TYPE_P (t))
10546 t = TYPE_MAIN_VARIANT (t);
10547 if (!ftpi->parms.add (t))
10548 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10549
10550 return 0;
10551 }
10552
10553 /* Ensure that we recursively examine certain terms that are not normally
10554 visited in for_each_template_parm_r. */
10555
10556 static int
10557 any_template_parm_r (tree t, void *data)
10558 {
10559 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10560
10561 #define WALK_SUBTREE(NODE) \
10562 do \
10563 { \
10564 for_each_template_parm (NODE, keep_template_parm, data, \
10565 &ftpi->visited, true, \
10566 any_template_parm_r); \
10567 } \
10568 while (0)
10569
10570 /* A mention of a member alias/typedef is a use of all of its template
10571 arguments, including those from the enclosing class, so we don't use
10572 alias_template_specialization_p here. */
10573 if (TYPE_P (t) && typedef_variant_p (t))
10574 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10575 WALK_SUBTREE (TI_ARGS (tinfo));
10576
10577 switch (TREE_CODE (t))
10578 {
10579 case TEMPLATE_TYPE_PARM:
10580 /* Type constraints of a placeholder type may contain parameters. */
10581 if (is_auto (t))
10582 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10583 WALK_SUBTREE (constr);
10584 break;
10585
10586 case TEMPLATE_ID_EXPR:
10587 /* Search through references to variable templates. */
10588 WALK_SUBTREE (TREE_OPERAND (t, 0));
10589 WALK_SUBTREE (TREE_OPERAND (t, 1));
10590 break;
10591
10592 case TEMPLATE_PARM_INDEX:
10593 case PARM_DECL:
10594 /* A parameter or constraint variable may also depend on a template
10595 parameter without explicitly naming it. */
10596 WALK_SUBTREE (TREE_TYPE (t));
10597 break;
10598
10599 case TEMPLATE_DECL:
10600 {
10601 /* If T is a member template that shares template parameters with
10602 ctx_parms, we need to mark all those parameters for mapping. */
10603 tree dparms = DECL_TEMPLATE_PARMS (t);
10604 tree cparms = ftpi->ctx_parms;
10605 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10606 dparms = TREE_CHAIN (dparms);
10607 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10608 cparms = TREE_CHAIN (cparms);
10609 while (dparms
10610 && (TREE_TYPE (TREE_VALUE (dparms))
10611 != TREE_TYPE (TREE_VALUE (cparms))))
10612 dparms = TREE_CHAIN (dparms),
10613 cparms = TREE_CHAIN (cparms);
10614 if (dparms)
10615 {
10616 int ddepth = TMPL_PARMS_DEPTH (dparms);
10617 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10618 for (int i = 0; i < ddepth; ++i)
10619 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10620 }
10621 }
10622 break;
10623
10624 case LAMBDA_EXPR:
10625 {
10626 /* Look in the parms and body. */
10627 tree fn = lambda_function (t);
10628 WALK_SUBTREE (TREE_TYPE (fn));
10629 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10630 }
10631 break;
10632
10633 case IDENTIFIER_NODE:
10634 if (IDENTIFIER_CONV_OP_P (t))
10635 /* The conversion-type-id of a conversion operator may be dependent. */
10636 WALK_SUBTREE (TREE_TYPE (t));
10637 break;
10638
10639 default:
10640 break;
10641 }
10642
10643 /* Keep walking. */
10644 return 0;
10645 }
10646
10647 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10648 are the template parameters in scope. */
10649
10650 tree
10651 find_template_parameters (tree t, tree ctx_parms)
10652 {
10653 if (!ctx_parms)
10654 return NULL_TREE;
10655
10656 find_template_parameter_info ftpi (ctx_parms);
10657 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10658 /*include_nondeduced*/true, any_template_parm_r);
10659 return ftpi.parm_list;
10660 }
10661
10662 /* Returns true if T depends on any template parameter. */
10663
10664 int
10665 uses_template_parms (tree t)
10666 {
10667 if (t == NULL_TREE)
10668 return false;
10669
10670 bool dependent_p;
10671 int saved_processing_template_decl;
10672
10673 saved_processing_template_decl = processing_template_decl;
10674 if (!saved_processing_template_decl)
10675 processing_template_decl = 1;
10676 if (TYPE_P (t))
10677 dependent_p = dependent_type_p (t);
10678 else if (TREE_CODE (t) == TREE_VEC)
10679 dependent_p = any_dependent_template_arguments_p (t);
10680 else if (TREE_CODE (t) == TREE_LIST)
10681 dependent_p = (uses_template_parms (TREE_VALUE (t))
10682 || uses_template_parms (TREE_CHAIN (t)));
10683 else if (TREE_CODE (t) == TYPE_DECL)
10684 dependent_p = dependent_type_p (TREE_TYPE (t));
10685 else if (t == error_mark_node)
10686 dependent_p = false;
10687 else
10688 dependent_p = value_dependent_expression_p (t);
10689
10690 processing_template_decl = saved_processing_template_decl;
10691
10692 return dependent_p;
10693 }
10694
10695 /* Returns true iff current_function_decl is an incompletely instantiated
10696 template. Useful instead of processing_template_decl because the latter
10697 is set to 0 during instantiate_non_dependent_expr. */
10698
10699 bool
10700 in_template_function (void)
10701 {
10702 tree fn = current_function_decl;
10703 bool ret;
10704 ++processing_template_decl;
10705 ret = (fn && DECL_LANG_SPECIFIC (fn)
10706 && DECL_TEMPLATE_INFO (fn)
10707 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10708 --processing_template_decl;
10709 return ret;
10710 }
10711
10712 /* Returns true if T depends on any template parameter with level LEVEL. */
10713
10714 bool
10715 uses_template_parms_level (tree t, int level)
10716 {
10717 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10718 /*include_nondeduced_p=*/true);
10719 }
10720
10721 /* Returns true if the signature of DECL depends on any template parameter from
10722 its enclosing class. */
10723
10724 bool
10725 uses_outer_template_parms (tree decl)
10726 {
10727 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10728 if (depth == 0)
10729 return false;
10730 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10731 &depth, NULL, /*include_nondeduced_p=*/true))
10732 return true;
10733 if (PRIMARY_TEMPLATE_P (decl)
10734 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10735 (DECL_TEMPLATE_PARMS (decl)),
10736 template_parm_outer_level,
10737 &depth, NULL, /*include_nondeduced_p=*/true))
10738 return true;
10739 tree ci = get_constraints (decl);
10740 if (ci)
10741 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10742 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10743 &depth, NULL, /*nondeduced*/true))
10744 return true;
10745 return false;
10746 }
10747
10748 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10749 ill-formed translation unit, i.e. a variable or function that isn't
10750 usable in a constant expression. */
10751
10752 static inline bool
10753 neglectable_inst_p (tree d)
10754 {
10755 return (d && DECL_P (d)
10756 && !undeduced_auto_decl (d)
10757 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10758 : decl_maybe_constant_var_p (d)));
10759 }
10760
10761 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10762 neglectable and instantiated from within an erroneous instantiation. */
10763
10764 static bool
10765 limit_bad_template_recursion (tree decl)
10766 {
10767 struct tinst_level *lev = current_tinst_level;
10768 int errs = errorcount + sorrycount;
10769 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10770 return false;
10771
10772 for (; lev; lev = lev->next)
10773 if (neglectable_inst_p (lev->maybe_get_node ()))
10774 break;
10775
10776 return (lev && errs > lev->errors);
10777 }
10778
10779 static int tinst_depth;
10780 extern int max_tinst_depth;
10781 int depth_reached;
10782
10783 static GTY(()) struct tinst_level *last_error_tinst_level;
10784
10785 /* We're starting to instantiate D; record the template instantiation context
10786 at LOC for diagnostics and to restore it later. */
10787
10788 bool
10789 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10790 {
10791 struct tinst_level *new_level;
10792
10793 if (tinst_depth >= max_tinst_depth)
10794 {
10795 /* Tell error.c not to try to instantiate any templates. */
10796 at_eof = 2;
10797 fatal_error (input_location,
10798 "template instantiation depth exceeds maximum of %d"
10799 " (use %<-ftemplate-depth=%> to increase the maximum)",
10800 max_tinst_depth);
10801 return false;
10802 }
10803
10804 /* If the current instantiation caused problems, don't let it instantiate
10805 anything else. Do allow deduction substitution and decls usable in
10806 constant expressions. */
10807 if (!targs && limit_bad_template_recursion (tldcl))
10808 {
10809 /* Avoid no_linkage_errors and unused function warnings for this
10810 decl. */
10811 TREE_NO_WARNING (tldcl) = 1;
10812 return false;
10813 }
10814
10815 /* When not -quiet, dump template instantiations other than functions, since
10816 announce_function will take care of those. */
10817 if (!quiet_flag && !targs
10818 && TREE_CODE (tldcl) != TREE_LIST
10819 && TREE_CODE (tldcl) != FUNCTION_DECL)
10820 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10821
10822 new_level = tinst_level_freelist ().alloc ();
10823 new_level->tldcl = tldcl;
10824 new_level->targs = targs;
10825 new_level->locus = loc;
10826 new_level->errors = errorcount + sorrycount;
10827 new_level->next = NULL;
10828 new_level->refcount = 0;
10829 set_refcount_ptr (new_level->next, current_tinst_level);
10830 set_refcount_ptr (current_tinst_level, new_level);
10831
10832 ++tinst_depth;
10833 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10834 depth_reached = tinst_depth;
10835
10836 return true;
10837 }
10838
10839 /* We're starting substitution of TMPL<ARGS>; record the template
10840 substitution context for diagnostics and to restore it later. */
10841
10842 bool
10843 push_tinst_level (tree tmpl, tree args)
10844 {
10845 return push_tinst_level_loc (tmpl, args, input_location);
10846 }
10847
10848 /* We're starting to instantiate D; record INPUT_LOCATION and the
10849 template instantiation context for diagnostics and to restore it
10850 later. */
10851
10852 bool
10853 push_tinst_level (tree d)
10854 {
10855 return push_tinst_level_loc (d, input_location);
10856 }
10857
10858 /* Likewise, but record LOC as the program location. */
10859
10860 bool
10861 push_tinst_level_loc (tree d, location_t loc)
10862 {
10863 gcc_assert (TREE_CODE (d) != TREE_LIST);
10864 return push_tinst_level_loc (d, NULL, loc);
10865 }
10866
10867 /* We're done instantiating this template; return to the instantiation
10868 context. */
10869
10870 void
10871 pop_tinst_level (void)
10872 {
10873 /* Restore the filename and line number stashed away when we started
10874 this instantiation. */
10875 input_location = current_tinst_level->locus;
10876 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10877 --tinst_depth;
10878 }
10879
10880 /* We're instantiating a deferred template; restore the template
10881 instantiation context in which the instantiation was requested, which
10882 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10883
10884 static tree
10885 reopen_tinst_level (struct tinst_level *level)
10886 {
10887 struct tinst_level *t;
10888
10889 tinst_depth = 0;
10890 for (t = level; t; t = t->next)
10891 ++tinst_depth;
10892
10893 set_refcount_ptr (current_tinst_level, level);
10894 pop_tinst_level ();
10895 if (current_tinst_level)
10896 current_tinst_level->errors = errorcount+sorrycount;
10897 return level->maybe_get_node ();
10898 }
10899
10900 /* Returns the TINST_LEVEL which gives the original instantiation
10901 context. */
10902
10903 struct tinst_level *
10904 outermost_tinst_level (void)
10905 {
10906 struct tinst_level *level = current_tinst_level;
10907 if (level)
10908 while (level->next)
10909 level = level->next;
10910 return level;
10911 }
10912
10913 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10914 vector of template arguments, as for tsubst.
10915
10916 Returns an appropriate tsubst'd friend declaration. */
10917
10918 static tree
10919 tsubst_friend_function (tree decl, tree args)
10920 {
10921 tree new_friend;
10922
10923 if (TREE_CODE (decl) == FUNCTION_DECL
10924 && DECL_TEMPLATE_INSTANTIATION (decl)
10925 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10926 /* This was a friend declared with an explicit template
10927 argument list, e.g.:
10928
10929 friend void f<>(T);
10930
10931 to indicate that f was a template instantiation, not a new
10932 function declaration. Now, we have to figure out what
10933 instantiation of what template. */
10934 {
10935 tree template_id, arglist, fns;
10936 tree new_args;
10937 tree tmpl;
10938 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10939
10940 /* Friend functions are looked up in the containing namespace scope.
10941 We must enter that scope, to avoid finding member functions of the
10942 current class with same name. */
10943 push_nested_namespace (ns);
10944 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10945 tf_warning_or_error, NULL_TREE,
10946 /*integral_constant_expression_p=*/false);
10947 pop_nested_namespace (ns);
10948 arglist = tsubst (DECL_TI_ARGS (decl), args,
10949 tf_warning_or_error, NULL_TREE);
10950 template_id = lookup_template_function (fns, arglist);
10951
10952 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10953 tmpl = determine_specialization (template_id, new_friend,
10954 &new_args,
10955 /*need_member_template=*/0,
10956 TREE_VEC_LENGTH (args),
10957 tsk_none);
10958 return instantiate_template (tmpl, new_args, tf_error);
10959 }
10960
10961 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10962 if (new_friend == error_mark_node)
10963 return error_mark_node;
10964
10965 /* The NEW_FRIEND will look like an instantiation, to the
10966 compiler, but is not an instantiation from the point of view of
10967 the language. For example, we might have had:
10968
10969 template <class T> struct S {
10970 template <class U> friend void f(T, U);
10971 };
10972
10973 Then, in S<int>, template <class U> void f(int, U) is not an
10974 instantiation of anything. */
10975
10976 DECL_USE_TEMPLATE (new_friend) = 0;
10977 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10978 {
10979 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10980 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10981 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10982
10983 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10984 match in decls_match. */
10985 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10986 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
10987 treqs = maybe_substitute_reqs_for (treqs, new_friend);
10988 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10989 }
10990
10991 /* The mangled name for the NEW_FRIEND is incorrect. The function
10992 is not a template instantiation and should not be mangled like
10993 one. Therefore, we forget the mangling here; we'll recompute it
10994 later if we need it. */
10995 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10996 {
10997 SET_DECL_RTL (new_friend, NULL);
10998 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10999 }
11000
11001 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11002 {
11003 tree old_decl;
11004 tree ns;
11005
11006 /* We must save some information from NEW_FRIEND before calling
11007 duplicate decls since that function will free NEW_FRIEND if
11008 possible. */
11009 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11010 tree new_friend_result_template_info = NULL_TREE;
11011 bool new_friend_is_defn =
11012 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11013 (template_for_substitution (new_friend)))
11014 != NULL_TREE);
11015 tree not_tmpl = new_friend;
11016
11017 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11018 {
11019 /* This declaration is a `primary' template. */
11020 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11021
11022 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11023 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11024 }
11025
11026 /* Inside pushdecl_namespace_level, we will push into the
11027 current namespace. However, the friend function should go
11028 into the namespace of the template. */
11029 ns = decl_namespace_context (new_friend);
11030 push_nested_namespace (ns);
11031 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
11032 pop_nested_namespace (ns);
11033
11034 if (old_decl == error_mark_node)
11035 return error_mark_node;
11036
11037 if (old_decl != new_friend)
11038 {
11039 /* This new friend declaration matched an existing
11040 declaration. For example, given:
11041
11042 template <class T> void f(T);
11043 template <class U> class C {
11044 template <class T> friend void f(T) {}
11045 };
11046
11047 the friend declaration actually provides the definition
11048 of `f', once C has been instantiated for some type. So,
11049 old_decl will be the out-of-class template declaration,
11050 while new_friend is the in-class definition.
11051
11052 But, if `f' was called before this point, the
11053 instantiation of `f' will have DECL_TI_ARGS corresponding
11054 to `T' but not to `U', references to which might appear
11055 in the definition of `f'. Previously, the most general
11056 template for an instantiation of `f' was the out-of-class
11057 version; now it is the in-class version. Therefore, we
11058 run through all specialization of `f', adding to their
11059 DECL_TI_ARGS appropriately. In particular, they need a
11060 new set of outer arguments, corresponding to the
11061 arguments for this class instantiation.
11062
11063 The same situation can arise with something like this:
11064
11065 friend void f(int);
11066 template <class T> class C {
11067 friend void f(T) {}
11068 };
11069
11070 when `C<int>' is instantiated. Now, `f(int)' is defined
11071 in the class. */
11072
11073 if (!new_friend_is_defn)
11074 /* On the other hand, if the in-class declaration does
11075 *not* provide a definition, then we don't want to alter
11076 existing definitions. We can just leave everything
11077 alone. */
11078 ;
11079 else
11080 {
11081 tree new_template = TI_TEMPLATE (new_friend_template_info);
11082 tree new_args = TI_ARGS (new_friend_template_info);
11083
11084 /* Overwrite whatever template info was there before, if
11085 any, with the new template information pertaining to
11086 the declaration. */
11087 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11088
11089 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11090 {
11091 /* We should have called reregister_specialization in
11092 duplicate_decls. */
11093 gcc_assert (retrieve_specialization (new_template,
11094 new_args, 0)
11095 == old_decl);
11096
11097 /* Instantiate it if the global has already been used. */
11098 if (DECL_ODR_USED (old_decl))
11099 instantiate_decl (old_decl, /*defer_ok=*/true,
11100 /*expl_inst_class_mem_p=*/false);
11101 }
11102 else
11103 {
11104 tree t;
11105
11106 /* Indicate that the old function template is a partial
11107 instantiation. */
11108 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11109 = new_friend_result_template_info;
11110
11111 gcc_assert (new_template
11112 == most_general_template (new_template));
11113 gcc_assert (new_template != old_decl);
11114
11115 /* Reassign any specializations already in the hash table
11116 to the new more general template, and add the
11117 additional template args. */
11118 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11119 t != NULL_TREE;
11120 t = TREE_CHAIN (t))
11121 {
11122 tree spec = TREE_VALUE (t);
11123 spec_entry elt;
11124
11125 elt.tmpl = old_decl;
11126 elt.args = DECL_TI_ARGS (spec);
11127 elt.spec = NULL_TREE;
11128
11129 decl_specializations->remove_elt (&elt);
11130
11131 DECL_TI_ARGS (spec)
11132 = add_outermost_template_args (new_args,
11133 DECL_TI_ARGS (spec));
11134
11135 register_specialization
11136 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11137
11138 }
11139 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11140 }
11141 }
11142
11143 /* The information from NEW_FRIEND has been merged into OLD_DECL
11144 by duplicate_decls. */
11145 new_friend = old_decl;
11146 }
11147 }
11148 else
11149 {
11150 tree context = DECL_CONTEXT (new_friend);
11151 bool dependent_p;
11152
11153 /* In the code
11154 template <class T> class C {
11155 template <class U> friend void C1<U>::f (); // case 1
11156 friend void C2<T>::f (); // case 2
11157 };
11158 we only need to make sure CONTEXT is a complete type for
11159 case 2. To distinguish between the two cases, we note that
11160 CONTEXT of case 1 remains dependent type after tsubst while
11161 this isn't true for case 2. */
11162 ++processing_template_decl;
11163 dependent_p = dependent_type_p (context);
11164 --processing_template_decl;
11165
11166 if (!dependent_p
11167 && !complete_type_or_else (context, NULL_TREE))
11168 return error_mark_node;
11169
11170 if (COMPLETE_TYPE_P (context))
11171 {
11172 tree fn = new_friend;
11173 /* do_friend adds the TEMPLATE_DECL for any member friend
11174 template even if it isn't a member template, i.e.
11175 template <class T> friend A<T>::f();
11176 Look through it in that case. */
11177 if (TREE_CODE (fn) == TEMPLATE_DECL
11178 && !PRIMARY_TEMPLATE_P (fn))
11179 fn = DECL_TEMPLATE_RESULT (fn);
11180 /* Check to see that the declaration is really present, and,
11181 possibly obtain an improved declaration. */
11182 fn = check_classfn (context, fn, NULL_TREE);
11183
11184 if (fn)
11185 new_friend = fn;
11186 }
11187 }
11188
11189 return new_friend;
11190 }
11191
11192 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11193 template arguments, as for tsubst.
11194
11195 Returns an appropriate tsubst'd friend type or error_mark_node on
11196 failure. */
11197
11198 static tree
11199 tsubst_friend_class (tree friend_tmpl, tree args)
11200 {
11201 tree tmpl;
11202
11203 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11204 {
11205 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11206 return TREE_TYPE (tmpl);
11207 }
11208
11209 tree context = CP_DECL_CONTEXT (friend_tmpl);
11210 if (TREE_CODE (context) == NAMESPACE_DECL)
11211 push_nested_namespace (context);
11212 else
11213 {
11214 context = tsubst (context, args, tf_error, NULL_TREE);
11215 push_nested_class (context);
11216 }
11217
11218 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11219 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11220
11221 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11222 {
11223 /* The friend template has already been declared. Just
11224 check to see that the declarations match, and install any new
11225 default parameters. We must tsubst the default parameters,
11226 of course. We only need the innermost template parameters
11227 because that is all that redeclare_class_template will look
11228 at. */
11229 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11230 > TMPL_ARGS_DEPTH (args))
11231 {
11232 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11233 args, tf_warning_or_error);
11234 location_t saved_input_location = input_location;
11235 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11236 tree cons = get_constraints (tmpl);
11237 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11238 input_location = saved_input_location;
11239 }
11240 }
11241 else
11242 {
11243 /* The friend template has not already been declared. In this
11244 case, the instantiation of the template class will cause the
11245 injection of this template into the namespace scope. */
11246 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11247
11248 if (tmpl != error_mark_node)
11249 {
11250 /* The new TMPL is not an instantiation of anything, so we
11251 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11252 for the new type because that is supposed to be the
11253 corresponding template decl, i.e., TMPL. */
11254 DECL_USE_TEMPLATE (tmpl) = 0;
11255 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11256 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11257 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11258 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11259
11260 /* It is hidden. */
11261 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11262 DECL_ANTICIPATED (tmpl)
11263 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11264
11265 /* Substitute into and set the constraints on the new declaration. */
11266 if (tree ci = get_constraints (friend_tmpl))
11267 {
11268 ++processing_template_decl;
11269 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11270 DECL_FRIEND_CONTEXT (friend_tmpl));
11271 --processing_template_decl;
11272 set_constraints (tmpl, ci);
11273 }
11274
11275 /* Inject this template into the enclosing namspace scope. */
11276 tmpl = pushdecl_namespace_level (tmpl, true);
11277 }
11278 }
11279
11280 if (TREE_CODE (context) == NAMESPACE_DECL)
11281 pop_nested_namespace (context);
11282 else
11283 pop_nested_class ();
11284
11285 return TREE_TYPE (tmpl);
11286 }
11287
11288 /* Returns zero if TYPE cannot be completed later due to circularity.
11289 Otherwise returns one. */
11290
11291 static int
11292 can_complete_type_without_circularity (tree type)
11293 {
11294 if (type == NULL_TREE || type == error_mark_node)
11295 return 0;
11296 else if (COMPLETE_TYPE_P (type))
11297 return 1;
11298 else if (TREE_CODE (type) == ARRAY_TYPE)
11299 return can_complete_type_without_circularity (TREE_TYPE (type));
11300 else if (CLASS_TYPE_P (type)
11301 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11302 return 0;
11303 else
11304 return 1;
11305 }
11306
11307 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11308 tsubst_flags_t, tree);
11309
11310 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11311 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11312
11313 static tree
11314 tsubst_attribute (tree t, tree *decl_p, tree args,
11315 tsubst_flags_t complain, tree in_decl)
11316 {
11317 gcc_assert (ATTR_IS_DEPENDENT (t));
11318
11319 tree val = TREE_VALUE (t);
11320 if (val == NULL_TREE)
11321 /* Nothing to do. */;
11322 else if ((flag_openmp || flag_openmp_simd)
11323 && is_attribute_p ("omp declare simd",
11324 get_attribute_name (t)))
11325 {
11326 tree clauses = TREE_VALUE (val);
11327 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11328 complain, in_decl);
11329 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11330 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11331 tree parms = DECL_ARGUMENTS (*decl_p);
11332 clauses
11333 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11334 if (clauses)
11335 val = build_tree_list (NULL_TREE, clauses);
11336 else
11337 val = NULL_TREE;
11338 }
11339 else if (flag_openmp
11340 && is_attribute_p ("omp declare variant base",
11341 get_attribute_name (t)))
11342 {
11343 ++cp_unevaluated_operand;
11344 tree varid
11345 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11346 in_decl, /*integral_constant_expression_p=*/false);
11347 --cp_unevaluated_operand;
11348 tree chain = TREE_CHAIN (val);
11349 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11350 tree ctx = copy_list (TREE_VALUE (val));
11351 tree simd = get_identifier ("simd");
11352 tree score = get_identifier (" score");
11353 tree condition = get_identifier ("condition");
11354 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11355 {
11356 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11357 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11358 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11359 {
11360 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11361 {
11362 tree clauses = TREE_VALUE (t2);
11363 clauses = tsubst_omp_clauses (clauses,
11364 C_ORT_OMP_DECLARE_SIMD, args,
11365 complain, in_decl);
11366 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11367 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11368 TREE_VALUE (t2) = clauses;
11369 }
11370 else
11371 {
11372 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11373 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11374 if (TREE_VALUE (t3))
11375 {
11376 bool allow_string
11377 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11378 && TREE_PURPOSE (t3) != score);
11379 tree v = TREE_VALUE (t3);
11380 if (TREE_CODE (v) == STRING_CST && allow_string)
11381 continue;
11382 v = tsubst_expr (v, args, complain, in_decl, true);
11383 v = fold_non_dependent_expr (v);
11384 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11385 || (TREE_PURPOSE (t3) == score
11386 ? TREE_CODE (v) != INTEGER_CST
11387 : !tree_fits_shwi_p (v)))
11388 {
11389 location_t loc
11390 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11391 match_loc);
11392 if (TREE_PURPOSE (t3) == score)
11393 error_at (loc, "score argument must be "
11394 "constant integer expression");
11395 else if (allow_string)
11396 error_at (loc, "property must be constant "
11397 "integer expression or string "
11398 "literal");
11399 else
11400 error_at (loc, "property must be constant "
11401 "integer expression");
11402 return NULL_TREE;
11403 }
11404 else if (TREE_PURPOSE (t3) == score
11405 && tree_int_cst_sgn (v) < 0)
11406 {
11407 location_t loc
11408 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11409 match_loc);
11410 error_at (loc, "score argument must be "
11411 "non-negative");
11412 return NULL_TREE;
11413 }
11414 TREE_VALUE (t3) = v;
11415 }
11416 }
11417 }
11418 }
11419 val = tree_cons (varid, ctx, chain);
11420 }
11421 /* If the first attribute argument is an identifier, don't
11422 pass it through tsubst. Attributes like mode, format,
11423 cleanup and several target specific attributes expect it
11424 unmodified. */
11425 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11426 {
11427 tree chain
11428 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11429 /*integral_constant_expression_p=*/false);
11430 if (chain != TREE_CHAIN (val))
11431 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11432 }
11433 else if (PACK_EXPANSION_P (val))
11434 {
11435 /* An attribute pack expansion. */
11436 tree purp = TREE_PURPOSE (t);
11437 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11438 if (pack == error_mark_node)
11439 return error_mark_node;
11440 int len = TREE_VEC_LENGTH (pack);
11441 tree list = NULL_TREE;
11442 tree *q = &list;
11443 for (int i = 0; i < len; ++i)
11444 {
11445 tree elt = TREE_VEC_ELT (pack, i);
11446 *q = build_tree_list (purp, elt);
11447 q = &TREE_CHAIN (*q);
11448 }
11449 return list;
11450 }
11451 else
11452 val = tsubst_expr (val, args, complain, in_decl,
11453 /*integral_constant_expression_p=*/false);
11454
11455 if (val != TREE_VALUE (t))
11456 return build_tree_list (TREE_PURPOSE (t), val);
11457 return t;
11458 }
11459
11460 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11461 unchanged or a new TREE_LIST chain. */
11462
11463 static tree
11464 tsubst_attributes (tree attributes, tree args,
11465 tsubst_flags_t complain, tree in_decl)
11466 {
11467 tree last_dep = NULL_TREE;
11468
11469 for (tree t = attributes; t; t = TREE_CHAIN (t))
11470 if (ATTR_IS_DEPENDENT (t))
11471 {
11472 last_dep = t;
11473 attributes = copy_list (attributes);
11474 break;
11475 }
11476
11477 if (last_dep)
11478 for (tree *p = &attributes; *p; )
11479 {
11480 tree t = *p;
11481 if (ATTR_IS_DEPENDENT (t))
11482 {
11483 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11484 if (subst != t)
11485 {
11486 *p = subst;
11487 while (*p)
11488 p = &TREE_CHAIN (*p);
11489 *p = TREE_CHAIN (t);
11490 continue;
11491 }
11492 }
11493 p = &TREE_CHAIN (*p);
11494 }
11495
11496 return attributes;
11497 }
11498
11499 /* Apply any attributes which had to be deferred until instantiation
11500 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11501 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11502
11503 static void
11504 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11505 tree args, tsubst_flags_t complain, tree in_decl)
11506 {
11507 tree last_dep = NULL_TREE;
11508 tree t;
11509 tree *p;
11510
11511 if (attributes == NULL_TREE)
11512 return;
11513
11514 if (DECL_P (*decl_p))
11515 {
11516 if (TREE_TYPE (*decl_p) == error_mark_node)
11517 return;
11518 p = &DECL_ATTRIBUTES (*decl_p);
11519 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11520 to our attributes parameter. */
11521 gcc_assert (*p == attributes);
11522 }
11523 else
11524 {
11525 p = &TYPE_ATTRIBUTES (*decl_p);
11526 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11527 lookup_template_class_1, and should be preserved. */
11528 gcc_assert (*p != attributes);
11529 while (*p)
11530 p = &TREE_CHAIN (*p);
11531 }
11532
11533 for (t = attributes; t; t = TREE_CHAIN (t))
11534 if (ATTR_IS_DEPENDENT (t))
11535 {
11536 last_dep = t;
11537 attributes = copy_list (attributes);
11538 break;
11539 }
11540
11541 *p = attributes;
11542 if (last_dep)
11543 {
11544 tree late_attrs = NULL_TREE;
11545 tree *q = &late_attrs;
11546
11547 for (; *p; )
11548 {
11549 t = *p;
11550 if (ATTR_IS_DEPENDENT (t))
11551 {
11552 *p = TREE_CHAIN (t);
11553 TREE_CHAIN (t) = NULL_TREE;
11554 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11555 while (*q)
11556 q = &TREE_CHAIN (*q);
11557 }
11558 else
11559 p = &TREE_CHAIN (t);
11560 }
11561
11562 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11563 }
11564 }
11565
11566 /* The template TMPL is being instantiated with the template arguments TARGS.
11567 Perform the access checks that we deferred when parsing the template. */
11568
11569 static void
11570 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11571 {
11572 unsigned i;
11573 deferred_access_check *chk;
11574
11575 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11576 return;
11577
11578 if (vec<deferred_access_check, va_gc> *access_checks
11579 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11580 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11581 {
11582 tree decl = chk->decl;
11583 tree diag_decl = chk->diag_decl;
11584 tree type_scope = TREE_TYPE (chk->binfo);
11585
11586 if (uses_template_parms (type_scope))
11587 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11588
11589 /* Make access check error messages point to the location
11590 of the use of the typedef. */
11591 iloc_sentinel ils (chk->loc);
11592 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11593 decl, diag_decl, tf_warning_or_error);
11594 }
11595 }
11596
11597 static tree
11598 instantiate_class_template_1 (tree type)
11599 {
11600 tree templ, args, pattern, t, member;
11601 tree typedecl;
11602 tree pbinfo;
11603 tree base_list;
11604 unsigned int saved_maximum_field_alignment;
11605 tree fn_context;
11606
11607 if (type == error_mark_node)
11608 return error_mark_node;
11609
11610 if (COMPLETE_OR_OPEN_TYPE_P (type)
11611 || uses_template_parms (type))
11612 return type;
11613
11614 /* Figure out which template is being instantiated. */
11615 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11616 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11617
11618 /* Mark the type as in the process of being defined. */
11619 TYPE_BEING_DEFINED (type) = 1;
11620
11621 /* We may be in the middle of deferred access check. Disable
11622 it now. */
11623 deferring_access_check_sentinel acs (dk_no_deferred);
11624
11625 /* Determine what specialization of the original template to
11626 instantiate. */
11627 t = most_specialized_partial_spec (type, tf_warning_or_error);
11628 if (t == error_mark_node)
11629 return error_mark_node;
11630 else if (t)
11631 {
11632 /* This TYPE is actually an instantiation of a partial
11633 specialization. We replace the innermost set of ARGS with
11634 the arguments appropriate for substitution. For example,
11635 given:
11636
11637 template <class T> struct S {};
11638 template <class T> struct S<T*> {};
11639
11640 and supposing that we are instantiating S<int*>, ARGS will
11641 presently be {int*} -- but we need {int}. */
11642 pattern = TREE_TYPE (t);
11643 args = TREE_PURPOSE (t);
11644 }
11645 else
11646 {
11647 pattern = TREE_TYPE (templ);
11648 args = CLASSTYPE_TI_ARGS (type);
11649 }
11650
11651 /* If the template we're instantiating is incomplete, then clearly
11652 there's nothing we can do. */
11653 if (!COMPLETE_TYPE_P (pattern))
11654 {
11655 /* We can try again later. */
11656 TYPE_BEING_DEFINED (type) = 0;
11657 return type;
11658 }
11659
11660 /* If we've recursively instantiated too many templates, stop. */
11661 if (! push_tinst_level (type))
11662 return type;
11663
11664 int saved_unevaluated_operand = cp_unevaluated_operand;
11665 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11666
11667 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11668 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11669 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11670 fn_context = error_mark_node;
11671 if (!fn_context)
11672 push_to_top_level ();
11673 else
11674 {
11675 cp_unevaluated_operand = 0;
11676 c_inhibit_evaluation_warnings = 0;
11677 }
11678 /* Use #pragma pack from the template context. */
11679 saved_maximum_field_alignment = maximum_field_alignment;
11680 maximum_field_alignment = TYPE_PRECISION (pattern);
11681
11682 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11683
11684 /* Set the input location to the most specialized template definition.
11685 This is needed if tsubsting causes an error. */
11686 typedecl = TYPE_MAIN_DECL (pattern);
11687 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11688 DECL_SOURCE_LOCATION (typedecl);
11689
11690 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11691 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11692 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11693 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11694 if (ANON_AGGR_TYPE_P (pattern))
11695 SET_ANON_AGGR_TYPE_P (type);
11696 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11697 {
11698 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11699 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11700 /* Adjust visibility for template arguments. */
11701 determine_visibility (TYPE_MAIN_DECL (type));
11702 }
11703 if (CLASS_TYPE_P (type))
11704 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11705
11706 pbinfo = TYPE_BINFO (pattern);
11707
11708 /* We should never instantiate a nested class before its enclosing
11709 class; we need to look up the nested class by name before we can
11710 instantiate it, and that lookup should instantiate the enclosing
11711 class. */
11712 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11713 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11714
11715 base_list = NULL_TREE;
11716 if (BINFO_N_BASE_BINFOS (pbinfo))
11717 {
11718 tree pbase_binfo;
11719 tree pushed_scope;
11720 int i;
11721
11722 /* We must enter the scope containing the type, as that is where
11723 the accessibility of types named in dependent bases are
11724 looked up from. */
11725 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11726
11727 /* Substitute into each of the bases to determine the actual
11728 basetypes. */
11729 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11730 {
11731 tree base;
11732 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11733 tree expanded_bases = NULL_TREE;
11734 int idx, len = 1;
11735
11736 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11737 {
11738 expanded_bases =
11739 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11740 args, tf_error, NULL_TREE);
11741 if (expanded_bases == error_mark_node)
11742 continue;
11743
11744 len = TREE_VEC_LENGTH (expanded_bases);
11745 }
11746
11747 for (idx = 0; idx < len; idx++)
11748 {
11749 if (expanded_bases)
11750 /* Extract the already-expanded base class. */
11751 base = TREE_VEC_ELT (expanded_bases, idx);
11752 else
11753 /* Substitute to figure out the base class. */
11754 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11755 NULL_TREE);
11756
11757 if (base == error_mark_node)
11758 continue;
11759
11760 base_list = tree_cons (access, base, base_list);
11761 if (BINFO_VIRTUAL_P (pbase_binfo))
11762 TREE_TYPE (base_list) = integer_type_node;
11763 }
11764 }
11765
11766 /* The list is now in reverse order; correct that. */
11767 base_list = nreverse (base_list);
11768
11769 if (pushed_scope)
11770 pop_scope (pushed_scope);
11771 }
11772 /* Now call xref_basetypes to set up all the base-class
11773 information. */
11774 xref_basetypes (type, base_list);
11775
11776 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11777 (int) ATTR_FLAG_TYPE_IN_PLACE,
11778 args, tf_error, NULL_TREE);
11779 fixup_attribute_variants (type);
11780
11781 /* Now that our base classes are set up, enter the scope of the
11782 class, so that name lookups into base classes, etc. will work
11783 correctly. This is precisely analogous to what we do in
11784 begin_class_definition when defining an ordinary non-template
11785 class, except we also need to push the enclosing classes. */
11786 push_nested_class (type);
11787
11788 /* Now members are processed in the order of declaration. */
11789 for (member = CLASSTYPE_DECL_LIST (pattern);
11790 member; member = TREE_CHAIN (member))
11791 {
11792 tree t = TREE_VALUE (member);
11793
11794 if (TREE_PURPOSE (member))
11795 {
11796 if (TYPE_P (t))
11797 {
11798 if (LAMBDA_TYPE_P (t))
11799 /* A closure type for a lambda in an NSDMI or default argument.
11800 Ignore it; it will be regenerated when needed. */
11801 continue;
11802
11803 /* Build new CLASSTYPE_NESTED_UTDS. */
11804 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11805 && TYPE_LANG_SPECIFIC (t)
11806 && CLASSTYPE_IS_TEMPLATE (t));
11807
11808 /* If the member is a class template, then -- even after
11809 substitution -- there may be dependent types in the
11810 template argument list for the class. We increment
11811 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11812 that function will assume that no types are dependent
11813 when outside of a template. */
11814 if (class_template_p)
11815 ++processing_template_decl;
11816 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11817 if (class_template_p)
11818 --processing_template_decl;
11819 if (newtag == error_mark_node)
11820 continue;
11821
11822 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11823 {
11824 tree name = TYPE_IDENTIFIER (t);
11825
11826 if (class_template_p)
11827 /* Unfortunately, lookup_template_class sets
11828 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11829 instantiation (i.e., for the type of a member
11830 template class nested within a template class.)
11831 This behavior is required for
11832 maybe_process_partial_specialization to work
11833 correctly, but is not accurate in this case;
11834 the TAG is not an instantiation of anything.
11835 (The corresponding TEMPLATE_DECL is an
11836 instantiation, but the TYPE is not.) */
11837 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11838
11839 /* Now, we call pushtag to put this NEWTAG into the scope of
11840 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11841 pushtag calling push_template_decl. We don't have to do
11842 this for enums because it will already have been done in
11843 tsubst_enum. */
11844 if (name)
11845 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11846 pushtag (name, newtag, /*tag_scope=*/ts_current);
11847 }
11848 }
11849 else if (DECL_DECLARES_FUNCTION_P (t))
11850 {
11851 tree r;
11852
11853 if (TREE_CODE (t) == TEMPLATE_DECL)
11854 ++processing_template_decl;
11855 r = tsubst (t, args, tf_error, NULL_TREE);
11856 if (TREE_CODE (t) == TEMPLATE_DECL)
11857 --processing_template_decl;
11858 set_current_access_from_decl (r);
11859 finish_member_declaration (r);
11860 /* Instantiate members marked with attribute used. */
11861 if (r != error_mark_node && DECL_PRESERVE_P (r))
11862 mark_used (r);
11863 if (TREE_CODE (r) == FUNCTION_DECL
11864 && DECL_OMP_DECLARE_REDUCTION_P (r))
11865 cp_check_omp_declare_reduction (r);
11866 }
11867 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11868 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11869 /* A closure type for a lambda in an NSDMI or default argument.
11870 Ignore it; it will be regenerated when needed. */;
11871 else
11872 {
11873 /* Build new TYPE_FIELDS. */
11874 if (TREE_CODE (t) == STATIC_ASSERT)
11875 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11876 /*integral_constant_expression_p=*/true);
11877 else if (TREE_CODE (t) != CONST_DECL)
11878 {
11879 tree r;
11880 tree vec = NULL_TREE;
11881 int len = 1;
11882
11883 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11884 /* The file and line for this declaration, to
11885 assist in error message reporting. Since we
11886 called push_tinst_level above, we don't need to
11887 restore these. */
11888 input_location = DECL_SOURCE_LOCATION (t);
11889
11890 if (TREE_CODE (t) == TEMPLATE_DECL)
11891 ++processing_template_decl;
11892 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11893 if (TREE_CODE (t) == TEMPLATE_DECL)
11894 --processing_template_decl;
11895
11896 if (TREE_CODE (r) == TREE_VEC)
11897 {
11898 /* A capture pack became multiple fields. */
11899 vec = r;
11900 len = TREE_VEC_LENGTH (vec);
11901 }
11902
11903 for (int i = 0; i < len; ++i)
11904 {
11905 if (vec)
11906 r = TREE_VEC_ELT (vec, i);
11907 if (VAR_P (r))
11908 {
11909 /* In [temp.inst]:
11910
11911 [t]he initialization (and any associated
11912 side-effects) of a static data member does
11913 not occur unless the static data member is
11914 itself used in a way that requires the
11915 definition of the static data member to
11916 exist.
11917
11918 Therefore, we do not substitute into the
11919 initialized for the static data member here. */
11920 finish_static_data_member_decl
11921 (r,
11922 /*init=*/NULL_TREE,
11923 /*init_const_expr_p=*/false,
11924 /*asmspec_tree=*/NULL_TREE,
11925 /*flags=*/0);
11926 /* Instantiate members marked with attribute used. */
11927 if (r != error_mark_node && DECL_PRESERVE_P (r))
11928 mark_used (r);
11929 }
11930 else if (TREE_CODE (r) == FIELD_DECL)
11931 {
11932 /* Determine whether R has a valid type and can be
11933 completed later. If R is invalid, then its type
11934 is replaced by error_mark_node. */
11935 tree rtype = TREE_TYPE (r);
11936 if (can_complete_type_without_circularity (rtype))
11937 complete_type (rtype);
11938
11939 if (!complete_or_array_type_p (rtype))
11940 {
11941 /* If R's type couldn't be completed and
11942 it isn't a flexible array member (whose
11943 type is incomplete by definition) give
11944 an error. */
11945 cxx_incomplete_type_error (r, rtype);
11946 TREE_TYPE (r) = error_mark_node;
11947 }
11948 else if (TREE_CODE (rtype) == ARRAY_TYPE
11949 && TYPE_DOMAIN (rtype) == NULL_TREE
11950 && (TREE_CODE (type) == UNION_TYPE
11951 || TREE_CODE (type) == QUAL_UNION_TYPE))
11952 {
11953 error ("flexible array member %qD in union", r);
11954 TREE_TYPE (r) = error_mark_node;
11955 }
11956 else if (!verify_type_context (input_location,
11957 TCTX_FIELD, rtype))
11958 TREE_TYPE (r) = error_mark_node;
11959 }
11960
11961 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11962 such a thing will already have been added to the field
11963 list by tsubst_enum in finish_member_declaration in the
11964 CLASSTYPE_NESTED_UTDS case above. */
11965 if (!(TREE_CODE (r) == TYPE_DECL
11966 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11967 && DECL_ARTIFICIAL (r)))
11968 {
11969 set_current_access_from_decl (r);
11970 finish_member_declaration (r);
11971 }
11972 }
11973 }
11974 }
11975 }
11976 else
11977 {
11978 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11979 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11980 {
11981 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11982
11983 tree friend_type = t;
11984 bool adjust_processing_template_decl = false;
11985
11986 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11987 {
11988 /* template <class T> friend class C; */
11989 friend_type = tsubst_friend_class (friend_type, args);
11990 adjust_processing_template_decl = true;
11991 }
11992 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11993 {
11994 /* template <class T> friend class C::D; */
11995 friend_type = tsubst (friend_type, args,
11996 tf_warning_or_error, NULL_TREE);
11997 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11998 friend_type = TREE_TYPE (friend_type);
11999 adjust_processing_template_decl = true;
12000 }
12001 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12002 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12003 {
12004 /* This could be either
12005
12006 friend class T::C;
12007
12008 when dependent_type_p is false or
12009
12010 template <class U> friend class T::C;
12011
12012 otherwise. */
12013 /* Bump processing_template_decl in case this is something like
12014 template <class T> friend struct A<T>::B. */
12015 ++processing_template_decl;
12016 friend_type = tsubst (friend_type, args,
12017 tf_warning_or_error, NULL_TREE);
12018 if (dependent_type_p (friend_type))
12019 adjust_processing_template_decl = true;
12020 --processing_template_decl;
12021 }
12022 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
12023 && !CLASSTYPE_USE_TEMPLATE (friend_type)
12024 && TYPE_HIDDEN_P (friend_type))
12025 {
12026 /* friend class C;
12027
12028 where C hasn't been declared yet. Let's lookup name
12029 from namespace scope directly, bypassing any name that
12030 come from dependent base class. */
12031 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
12032
12033 /* The call to xref_tag_from_type does injection for friend
12034 classes. */
12035 push_nested_namespace (ns);
12036 friend_type =
12037 xref_tag_from_type (friend_type, NULL_TREE,
12038 /*tag_scope=*/ts_current);
12039 pop_nested_namespace (ns);
12040 }
12041 else if (uses_template_parms (friend_type))
12042 /* friend class C<T>; */
12043 friend_type = tsubst (friend_type, args,
12044 tf_warning_or_error, NULL_TREE);
12045 /* Otherwise it's
12046
12047 friend class C;
12048
12049 where C is already declared or
12050
12051 friend class C<int>;
12052
12053 We don't have to do anything in these cases. */
12054
12055 if (adjust_processing_template_decl)
12056 /* Trick make_friend_class into realizing that the friend
12057 we're adding is a template, not an ordinary class. It's
12058 important that we use make_friend_class since it will
12059 perform some error-checking and output cross-reference
12060 information. */
12061 ++processing_template_decl;
12062
12063 if (friend_type != error_mark_node)
12064 make_friend_class (type, friend_type, /*complain=*/false);
12065
12066 if (adjust_processing_template_decl)
12067 --processing_template_decl;
12068 }
12069 else
12070 {
12071 /* Build new DECL_FRIENDLIST. */
12072 tree r;
12073
12074 /* The file and line for this declaration, to
12075 assist in error message reporting. Since we
12076 called push_tinst_level above, we don't need to
12077 restore these. */
12078 input_location = DECL_SOURCE_LOCATION (t);
12079
12080 if (TREE_CODE (t) == TEMPLATE_DECL)
12081 {
12082 ++processing_template_decl;
12083 push_deferring_access_checks (dk_no_check);
12084 }
12085
12086 r = tsubst_friend_function (t, args);
12087 add_friend (type, r, /*complain=*/false);
12088 if (TREE_CODE (t) == TEMPLATE_DECL)
12089 {
12090 pop_deferring_access_checks ();
12091 --processing_template_decl;
12092 }
12093 }
12094 }
12095 }
12096
12097 if (fn_context)
12098 {
12099 /* Restore these before substituting into the lambda capture
12100 initializers. */
12101 cp_unevaluated_operand = saved_unevaluated_operand;
12102 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12103 }
12104
12105 /* Set the file and line number information to whatever is given for
12106 the class itself. This puts error messages involving generated
12107 implicit functions at a predictable point, and the same point
12108 that would be used for non-template classes. */
12109 input_location = DECL_SOURCE_LOCATION (typedecl);
12110
12111 unreverse_member_declarations (type);
12112 finish_struct_1 (type);
12113 TYPE_BEING_DEFINED (type) = 0;
12114
12115 /* We don't instantiate default arguments for member functions. 14.7.1:
12116
12117 The implicit instantiation of a class template specialization causes
12118 the implicit instantiation of the declarations, but not of the
12119 definitions or default arguments, of the class member functions,
12120 member classes, static data members and member templates.... */
12121
12122 perform_instantiation_time_access_checks (pattern, args);
12123 perform_deferred_access_checks (tf_warning_or_error);
12124 pop_nested_class ();
12125 maximum_field_alignment = saved_maximum_field_alignment;
12126 if (!fn_context)
12127 pop_from_top_level ();
12128 pop_tinst_level ();
12129
12130 /* The vtable for a template class can be emitted in any translation
12131 unit in which the class is instantiated. When there is no key
12132 method, however, finish_struct_1 will already have added TYPE to
12133 the keyed_classes. */
12134 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12135 vec_safe_push (keyed_classes, type);
12136
12137 return type;
12138 }
12139
12140 /* Wrapper for instantiate_class_template_1. */
12141
12142 tree
12143 instantiate_class_template (tree type)
12144 {
12145 tree ret;
12146 timevar_push (TV_TEMPLATE_INST);
12147 ret = instantiate_class_template_1 (type);
12148 timevar_pop (TV_TEMPLATE_INST);
12149 return ret;
12150 }
12151
12152 tree
12153 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12154 {
12155 tree r;
12156
12157 if (!t)
12158 r = t;
12159 else if (TYPE_P (t))
12160 r = tsubst (t, args, complain, in_decl);
12161 else
12162 {
12163 if (!(complain & tf_warning))
12164 ++c_inhibit_evaluation_warnings;
12165 r = tsubst_expr (t, args, complain, in_decl,
12166 /*integral_constant_expression_p=*/true);
12167 if (!(complain & tf_warning))
12168 --c_inhibit_evaluation_warnings;
12169 }
12170
12171 return r;
12172 }
12173
12174 /* Given a function parameter pack TMPL_PARM and some function parameters
12175 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12176 and set *SPEC_P to point at the next point in the list. */
12177
12178 tree
12179 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12180 {
12181 /* Collect all of the extra "packed" parameters into an
12182 argument pack. */
12183 tree parmvec;
12184 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12185 tree spec_parm = *spec_p;
12186 int i, len;
12187
12188 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12189 if (tmpl_parm
12190 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12191 break;
12192
12193 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12194 parmvec = make_tree_vec (len);
12195 spec_parm = *spec_p;
12196 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12197 {
12198 tree elt = spec_parm;
12199 if (DECL_PACK_P (elt))
12200 elt = make_pack_expansion (elt);
12201 TREE_VEC_ELT (parmvec, i) = elt;
12202 }
12203
12204 /* Build the argument packs. */
12205 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12206 *spec_p = spec_parm;
12207
12208 return argpack;
12209 }
12210
12211 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12212 NONTYPE_ARGUMENT_PACK. */
12213
12214 static tree
12215 make_fnparm_pack (tree spec_parm)
12216 {
12217 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12218 }
12219
12220 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12221 pack expansion with no extra args, 2 if it has extra args, or 0
12222 if it is not a pack expansion. */
12223
12224 static int
12225 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12226 {
12227 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12228 /* We're being called before this happens in tsubst_pack_expansion. */
12229 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12230 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12231 if (i >= TREE_VEC_LENGTH (vec))
12232 return 0;
12233 tree elt = TREE_VEC_ELT (vec, i);
12234 if (DECL_P (elt))
12235 /* A decl pack is itself an expansion. */
12236 elt = TREE_TYPE (elt);
12237 if (!PACK_EXPANSION_P (elt))
12238 return 0;
12239 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12240 return 2;
12241 return 1;
12242 }
12243
12244
12245 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12246
12247 static tree
12248 make_argument_pack_select (tree arg_pack, unsigned index)
12249 {
12250 tree aps = make_node (ARGUMENT_PACK_SELECT);
12251
12252 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12253 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12254
12255 return aps;
12256 }
12257
12258 /* This is a subroutine of tsubst_pack_expansion.
12259
12260 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12261 mechanism to store the (non complete list of) arguments of the
12262 substitution and return a non substituted pack expansion, in order
12263 to wait for when we have enough arguments to really perform the
12264 substitution. */
12265
12266 static bool
12267 use_pack_expansion_extra_args_p (tree parm_packs,
12268 int arg_pack_len,
12269 bool has_empty_arg)
12270 {
12271 /* If one pack has an expansion and another pack has a normal
12272 argument or if one pack has an empty argument and an another
12273 one hasn't then tsubst_pack_expansion cannot perform the
12274 substitution and need to fall back on the
12275 PACK_EXPANSION_EXTRA mechanism. */
12276 if (parm_packs == NULL_TREE)
12277 return false;
12278 else if (has_empty_arg)
12279 {
12280 /* If all the actual packs are pack expansions, we can still
12281 subsitute directly. */
12282 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12283 {
12284 tree a = TREE_VALUE (p);
12285 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12286 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12287 a = ARGUMENT_PACK_ARGS (a);
12288 if (TREE_VEC_LENGTH (a) == 1)
12289 a = TREE_VEC_ELT (a, 0);
12290 if (PACK_EXPANSION_P (a))
12291 continue;
12292 return true;
12293 }
12294 return false;
12295 }
12296
12297 bool has_expansion_arg = false;
12298 for (int i = 0 ; i < arg_pack_len; ++i)
12299 {
12300 bool has_non_expansion_arg = false;
12301 for (tree parm_pack = parm_packs;
12302 parm_pack;
12303 parm_pack = TREE_CHAIN (parm_pack))
12304 {
12305 tree arg = TREE_VALUE (parm_pack);
12306
12307 int exp = argument_pack_element_is_expansion_p (arg, i);
12308 if (exp == 2)
12309 /* We can't substitute a pack expansion with extra args into
12310 our pattern. */
12311 return true;
12312 else if (exp)
12313 has_expansion_arg = true;
12314 else
12315 has_non_expansion_arg = true;
12316 }
12317
12318 if (has_expansion_arg && has_non_expansion_arg)
12319 return true;
12320 }
12321 return false;
12322 }
12323
12324 /* [temp.variadic]/6 says that:
12325
12326 The instantiation of a pack expansion [...]
12327 produces a list E1,E2, ..., En, where N is the number of elements
12328 in the pack expansion parameters.
12329
12330 This subroutine of tsubst_pack_expansion produces one of these Ei.
12331
12332 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12333 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12334 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12335 INDEX is the index 'i' of the element Ei to produce. ARGS,
12336 COMPLAIN, and IN_DECL are the same parameters as for the
12337 tsubst_pack_expansion function.
12338
12339 The function returns the resulting Ei upon successful completion,
12340 or error_mark_node.
12341
12342 Note that this function possibly modifies the ARGS parameter, so
12343 it's the responsibility of the caller to restore it. */
12344
12345 static tree
12346 gen_elem_of_pack_expansion_instantiation (tree pattern,
12347 tree parm_packs,
12348 unsigned index,
12349 tree args /* This parm gets
12350 modified. */,
12351 tsubst_flags_t complain,
12352 tree in_decl)
12353 {
12354 tree t;
12355 bool ith_elem_is_expansion = false;
12356
12357 /* For each parameter pack, change the substitution of the parameter
12358 pack to the ith argument in its argument pack, then expand the
12359 pattern. */
12360 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12361 {
12362 tree parm = TREE_PURPOSE (pack);
12363 tree arg_pack = TREE_VALUE (pack);
12364 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12365
12366 ith_elem_is_expansion |=
12367 argument_pack_element_is_expansion_p (arg_pack, index);
12368
12369 /* Select the Ith argument from the pack. */
12370 if (TREE_CODE (parm) == PARM_DECL
12371 || VAR_P (parm)
12372 || TREE_CODE (parm) == FIELD_DECL)
12373 {
12374 if (index == 0)
12375 {
12376 aps = make_argument_pack_select (arg_pack, index);
12377 if (!mark_used (parm, complain) && !(complain & tf_error))
12378 return error_mark_node;
12379 register_local_specialization (aps, parm);
12380 }
12381 else
12382 aps = retrieve_local_specialization (parm);
12383 }
12384 else
12385 {
12386 int idx, level;
12387 template_parm_level_and_index (parm, &level, &idx);
12388
12389 if (index == 0)
12390 {
12391 aps = make_argument_pack_select (arg_pack, index);
12392 /* Update the corresponding argument. */
12393 TMPL_ARG (args, level, idx) = aps;
12394 }
12395 else
12396 /* Re-use the ARGUMENT_PACK_SELECT. */
12397 aps = TMPL_ARG (args, level, idx);
12398 }
12399 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12400 }
12401
12402 /* Substitute into the PATTERN with the (possibly altered)
12403 arguments. */
12404 if (pattern == in_decl)
12405 /* Expanding a fixed parameter pack from
12406 coerce_template_parameter_pack. */
12407 t = tsubst_decl (pattern, args, complain);
12408 else if (pattern == error_mark_node)
12409 t = error_mark_node;
12410 else if (!TYPE_P (pattern))
12411 t = tsubst_expr (pattern, args, complain, in_decl,
12412 /*integral_constant_expression_p=*/false);
12413 else
12414 t = tsubst (pattern, args, complain, in_decl);
12415
12416 /* If the Ith argument pack element is a pack expansion, then
12417 the Ith element resulting from the substituting is going to
12418 be a pack expansion as well. */
12419 if (ith_elem_is_expansion)
12420 t = make_pack_expansion (t, complain);
12421
12422 return t;
12423 }
12424
12425 /* When the unexpanded parameter pack in a fold expression expands to an empty
12426 sequence, the value of the expression is as follows; the program is
12427 ill-formed if the operator is not listed in this table.
12428
12429 && true
12430 || false
12431 , void() */
12432
12433 tree
12434 expand_empty_fold (tree t, tsubst_flags_t complain)
12435 {
12436 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12437 if (!FOLD_EXPR_MODIFY_P (t))
12438 switch (code)
12439 {
12440 case TRUTH_ANDIF_EXPR:
12441 return boolean_true_node;
12442 case TRUTH_ORIF_EXPR:
12443 return boolean_false_node;
12444 case COMPOUND_EXPR:
12445 return void_node;
12446 default:
12447 break;
12448 }
12449
12450 if (complain & tf_error)
12451 error_at (location_of (t),
12452 "fold of empty expansion over %O", code);
12453 return error_mark_node;
12454 }
12455
12456 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12457 form an expression that combines the two terms using the
12458 operator of T. */
12459
12460 static tree
12461 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12462 {
12463 tree op = FOLD_EXPR_OP (t);
12464 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12465
12466 // Handle compound assignment operators.
12467 if (FOLD_EXPR_MODIFY_P (t))
12468 return build_x_modify_expr (input_location, left, code, right, complain);
12469
12470 warning_sentinel s(warn_parentheses);
12471 switch (code)
12472 {
12473 case COMPOUND_EXPR:
12474 return build_x_compound_expr (input_location, left, right, complain);
12475 default:
12476 return build_x_binary_op (input_location, code,
12477 left, TREE_CODE (left),
12478 right, TREE_CODE (right),
12479 /*overload=*/NULL,
12480 complain);
12481 }
12482 }
12483
12484 /* Substitute ARGS into the pack of a fold expression T. */
12485
12486 static inline tree
12487 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12488 {
12489 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12490 }
12491
12492 /* Substitute ARGS into the pack of a fold expression T. */
12493
12494 static inline tree
12495 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12496 {
12497 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12498 }
12499
12500 /* Expand a PACK of arguments into a grouped as left fold.
12501 Given a pack containing elements A0, A1, ..., An and an
12502 operator @, this builds the expression:
12503
12504 ((A0 @ A1) @ A2) ... @ An
12505
12506 Note that PACK must not be empty.
12507
12508 The operator is defined by the original fold expression T. */
12509
12510 static tree
12511 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12512 {
12513 tree left = TREE_VEC_ELT (pack, 0);
12514 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12515 {
12516 tree right = TREE_VEC_ELT (pack, i);
12517 left = fold_expression (t, left, right, complain);
12518 }
12519 return left;
12520 }
12521
12522 /* Substitute into a unary left fold expression. */
12523
12524 static tree
12525 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12526 tree in_decl)
12527 {
12528 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12529 if (pack == error_mark_node)
12530 return error_mark_node;
12531 if (PACK_EXPANSION_P (pack))
12532 {
12533 tree r = copy_node (t);
12534 FOLD_EXPR_PACK (r) = pack;
12535 return r;
12536 }
12537 if (TREE_VEC_LENGTH (pack) == 0)
12538 return expand_empty_fold (t, complain);
12539 else
12540 return expand_left_fold (t, pack, complain);
12541 }
12542
12543 /* Substitute into a binary left fold expression.
12544
12545 Do ths by building a single (non-empty) vector of argumnts and
12546 building the expression from those elements. */
12547
12548 static tree
12549 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12550 tree in_decl)
12551 {
12552 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12553 if (pack == error_mark_node)
12554 return error_mark_node;
12555 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12556 if (init == error_mark_node)
12557 return error_mark_node;
12558
12559 if (PACK_EXPANSION_P (pack))
12560 {
12561 tree r = copy_node (t);
12562 FOLD_EXPR_PACK (r) = pack;
12563 FOLD_EXPR_INIT (r) = init;
12564 return r;
12565 }
12566
12567 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12568 TREE_VEC_ELT (vec, 0) = init;
12569 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12570 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12571
12572 return expand_left_fold (t, vec, complain);
12573 }
12574
12575 /* Expand a PACK of arguments into a grouped as right fold.
12576 Given a pack containing elementns A0, A1, ..., and an
12577 operator @, this builds the expression:
12578
12579 A0@ ... (An-2 @ (An-1 @ An))
12580
12581 Note that PACK must not be empty.
12582
12583 The operator is defined by the original fold expression T. */
12584
12585 tree
12586 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12587 {
12588 // Build the expression.
12589 int n = TREE_VEC_LENGTH (pack);
12590 tree right = TREE_VEC_ELT (pack, n - 1);
12591 for (--n; n != 0; --n)
12592 {
12593 tree left = TREE_VEC_ELT (pack, n - 1);
12594 right = fold_expression (t, left, right, complain);
12595 }
12596 return right;
12597 }
12598
12599 /* Substitute into a unary right fold expression. */
12600
12601 static tree
12602 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12603 tree in_decl)
12604 {
12605 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12606 if (pack == error_mark_node)
12607 return error_mark_node;
12608 if (PACK_EXPANSION_P (pack))
12609 {
12610 tree r = copy_node (t);
12611 FOLD_EXPR_PACK (r) = pack;
12612 return r;
12613 }
12614 if (TREE_VEC_LENGTH (pack) == 0)
12615 return expand_empty_fold (t, complain);
12616 else
12617 return expand_right_fold (t, pack, complain);
12618 }
12619
12620 /* Substitute into a binary right fold expression.
12621
12622 Do ths by building a single (non-empty) vector of arguments and
12623 building the expression from those elements. */
12624
12625 static tree
12626 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12627 tree in_decl)
12628 {
12629 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12630 if (pack == error_mark_node)
12631 return error_mark_node;
12632 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12633 if (init == error_mark_node)
12634 return error_mark_node;
12635
12636 if (PACK_EXPANSION_P (pack))
12637 {
12638 tree r = copy_node (t);
12639 FOLD_EXPR_PACK (r) = pack;
12640 FOLD_EXPR_INIT (r) = init;
12641 return r;
12642 }
12643
12644 int n = TREE_VEC_LENGTH (pack);
12645 tree vec = make_tree_vec (n + 1);
12646 for (int i = 0; i < n; ++i)
12647 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12648 TREE_VEC_ELT (vec, n) = init;
12649
12650 return expand_right_fold (t, vec, complain);
12651 }
12652
12653 /* Walk through the pattern of a pack expansion, adding everything in
12654 local_specializations to a list. */
12655
12656 class el_data
12657 {
12658 public:
12659 hash_set<tree> internal;
12660 tree extra;
12661 tsubst_flags_t complain;
12662
12663 el_data (tsubst_flags_t c)
12664 : extra (NULL_TREE), complain (c) {}
12665 };
12666 static tree
12667 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12668 {
12669 el_data &data = *reinterpret_cast<el_data*>(data_);
12670 tree *extra = &data.extra;
12671 tsubst_flags_t complain = data.complain;
12672
12673 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12674 /* Remember local typedefs (85214). */
12675 tp = &TYPE_NAME (*tp);
12676
12677 if (TREE_CODE (*tp) == DECL_EXPR)
12678 data.internal.add (DECL_EXPR_DECL (*tp));
12679 else if (tree spec = retrieve_local_specialization (*tp))
12680 {
12681 if (data.internal.contains (*tp))
12682 /* Don't mess with variables declared within the pattern. */
12683 return NULL_TREE;
12684 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12685 {
12686 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12687 tree args = ARGUMENT_PACK_ARGS (spec);
12688 if (TREE_VEC_LENGTH (args) == 1)
12689 {
12690 tree elt = TREE_VEC_ELT (args, 0);
12691 if (PACK_EXPANSION_P (elt))
12692 elt = PACK_EXPANSION_PATTERN (elt);
12693 if (DECL_PACK_P (elt))
12694 spec = elt;
12695 }
12696 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12697 {
12698 /* Handle lambda capture here, since we aren't doing any
12699 substitution now, and so tsubst_copy won't call
12700 process_outer_var_ref. */
12701 tree args = ARGUMENT_PACK_ARGS (spec);
12702 int len = TREE_VEC_LENGTH (args);
12703 for (int i = 0; i < len; ++i)
12704 {
12705 tree arg = TREE_VEC_ELT (args, i);
12706 tree carg = arg;
12707 if (outer_automatic_var_p (arg))
12708 carg = process_outer_var_ref (arg, complain);
12709 if (carg != arg)
12710 {
12711 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12712 proxies. */
12713 if (i == 0)
12714 {
12715 spec = copy_node (spec);
12716 args = copy_node (args);
12717 SET_ARGUMENT_PACK_ARGS (spec, args);
12718 register_local_specialization (spec, *tp);
12719 }
12720 TREE_VEC_ELT (args, i) = carg;
12721 }
12722 }
12723 }
12724 }
12725 if (outer_automatic_var_p (spec))
12726 spec = process_outer_var_ref (spec, complain);
12727 *extra = tree_cons (*tp, spec, *extra);
12728 }
12729 return NULL_TREE;
12730 }
12731 static tree
12732 extract_local_specs (tree pattern, tsubst_flags_t complain)
12733 {
12734 el_data data (complain);
12735 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12736 return data.extra;
12737 }
12738
12739 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12740 for use in PACK_EXPANSION_EXTRA_ARGS. */
12741
12742 tree
12743 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12744 {
12745 tree extra = args;
12746 if (local_specializations)
12747 if (tree locals = extract_local_specs (pattern, complain))
12748 extra = tree_cons (NULL_TREE, extra, locals);
12749 return extra;
12750 }
12751
12752 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12753 normal template args to ARGS. */
12754
12755 tree
12756 add_extra_args (tree extra, tree args)
12757 {
12758 if (extra && TREE_CODE (extra) == TREE_LIST)
12759 {
12760 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12761 {
12762 /* The partial instantiation involved local declarations collected in
12763 extract_local_specs; map from the general template to our local
12764 context. */
12765 tree gen = TREE_PURPOSE (elt);
12766 tree inst = TREE_VALUE (elt);
12767 if (DECL_P (inst))
12768 if (tree local = retrieve_local_specialization (inst))
12769 inst = local;
12770 /* else inst is already a full instantiation of the pack. */
12771 register_local_specialization (inst, gen);
12772 }
12773 gcc_assert (!TREE_PURPOSE (extra));
12774 extra = TREE_VALUE (extra);
12775 }
12776 #if 1
12777 /* I think we should always be able to substitute dependent args into the
12778 pattern. If that turns out to be incorrect in some cases, enable the
12779 alternate code (and add complain/in_decl parms to this function). */
12780 gcc_checking_assert (!uses_template_parms (extra));
12781 #else
12782 if (!uses_template_parms (extra))
12783 {
12784 gcc_unreachable ();
12785 extra = tsubst_template_args (extra, args, complain, in_decl);
12786 args = add_outermost_template_args (args, extra);
12787 }
12788 else
12789 #endif
12790 args = add_to_template_args (extra, args);
12791 return args;
12792 }
12793
12794 /* Substitute ARGS into T, which is an pack expansion
12795 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12796 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12797 (if only a partial substitution could be performed) or
12798 ERROR_MARK_NODE if there was an error. */
12799 tree
12800 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12801 tree in_decl)
12802 {
12803 tree pattern;
12804 tree pack, packs = NULL_TREE;
12805 bool unsubstituted_packs = false;
12806 int i, len = -1;
12807 tree result;
12808 bool need_local_specializations = false;
12809 int levels;
12810
12811 gcc_assert (PACK_EXPANSION_P (t));
12812 pattern = PACK_EXPANSION_PATTERN (t);
12813
12814 /* Add in any args remembered from an earlier partial instantiation. */
12815 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12816
12817 levels = TMPL_ARGS_DEPTH (args);
12818
12819 /* Determine the argument packs that will instantiate the parameter
12820 packs used in the expansion expression. While we're at it,
12821 compute the number of arguments to be expanded and make sure it
12822 is consistent. */
12823 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12824 pack = TREE_CHAIN (pack))
12825 {
12826 tree parm_pack = TREE_VALUE (pack);
12827 tree arg_pack = NULL_TREE;
12828 tree orig_arg = NULL_TREE;
12829 int level = 0;
12830
12831 if (TREE_CODE (parm_pack) == BASES)
12832 {
12833 gcc_assert (parm_pack == pattern);
12834 if (BASES_DIRECT (parm_pack))
12835 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12836 args, complain,
12837 in_decl, false),
12838 complain);
12839 else
12840 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12841 args, complain, in_decl,
12842 false), complain);
12843 }
12844 else if (builtin_pack_call_p (parm_pack))
12845 {
12846 if (parm_pack != pattern)
12847 {
12848 if (complain & tf_error)
12849 sorry ("%qE is not the entire pattern of the pack expansion",
12850 parm_pack);
12851 return error_mark_node;
12852 }
12853 return expand_builtin_pack_call (parm_pack, args,
12854 complain, in_decl);
12855 }
12856 else if (TREE_CODE (parm_pack) == PARM_DECL)
12857 {
12858 /* We know we have correct local_specializations if this
12859 expansion is at function scope, or if we're dealing with a
12860 local parameter in a requires expression; for the latter,
12861 tsubst_requires_expr set it up appropriately. */
12862 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12863 arg_pack = retrieve_local_specialization (parm_pack);
12864 else
12865 /* We can't rely on local_specializations for a parameter
12866 name used later in a function declaration (such as in a
12867 late-specified return type). Even if it exists, it might
12868 have the wrong value for a recursive call. */
12869 need_local_specializations = true;
12870
12871 if (!arg_pack)
12872 {
12873 /* This parameter pack was used in an unevaluated context. Just
12874 make a dummy decl, since it's only used for its type. */
12875 ++cp_unevaluated_operand;
12876 arg_pack = tsubst_decl (parm_pack, args, complain);
12877 --cp_unevaluated_operand;
12878 if (arg_pack && DECL_PACK_P (arg_pack))
12879 /* Partial instantiation of the parm_pack, we can't build
12880 up an argument pack yet. */
12881 arg_pack = NULL_TREE;
12882 else
12883 arg_pack = make_fnparm_pack (arg_pack);
12884 }
12885 else if (DECL_PACK_P (arg_pack))
12886 /* This argument pack isn't fully instantiated yet. */
12887 arg_pack = NULL_TREE;
12888 }
12889 else if (is_capture_proxy (parm_pack))
12890 {
12891 arg_pack = retrieve_local_specialization (parm_pack);
12892 if (DECL_PACK_P (arg_pack))
12893 arg_pack = NULL_TREE;
12894 }
12895 else
12896 {
12897 int idx;
12898 template_parm_level_and_index (parm_pack, &level, &idx);
12899 if (level <= levels)
12900 arg_pack = TMPL_ARG (args, level, idx);
12901
12902 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
12903 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
12904 arg_pack = NULL_TREE;
12905 }
12906
12907 orig_arg = arg_pack;
12908 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12909 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12910
12911 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12912 /* This can only happen if we forget to expand an argument
12913 pack somewhere else. Just return an error, silently. */
12914 {
12915 result = make_tree_vec (1);
12916 TREE_VEC_ELT (result, 0) = error_mark_node;
12917 return result;
12918 }
12919
12920 if (arg_pack)
12921 {
12922 int my_len =
12923 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12924
12925 /* Don't bother trying to do a partial substitution with
12926 incomplete packs; we'll try again after deduction. */
12927 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12928 return t;
12929
12930 if (len < 0)
12931 len = my_len;
12932 else if (len != my_len)
12933 {
12934 if (!(complain & tf_error))
12935 /* Fail quietly. */;
12936 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12937 error ("mismatched argument pack lengths while expanding %qT",
12938 pattern);
12939 else
12940 error ("mismatched argument pack lengths while expanding %qE",
12941 pattern);
12942 return error_mark_node;
12943 }
12944
12945 /* Keep track of the parameter packs and their corresponding
12946 argument packs. */
12947 packs = tree_cons (parm_pack, arg_pack, packs);
12948 TREE_TYPE (packs) = orig_arg;
12949 }
12950 else
12951 {
12952 /* We can't substitute for this parameter pack. We use a flag as
12953 well as the missing_level counter because function parameter
12954 packs don't have a level. */
12955 gcc_assert (processing_template_decl || is_auto (parm_pack));
12956 unsubstituted_packs = true;
12957 }
12958 }
12959
12960 /* If the expansion is just T..., return the matching argument pack, unless
12961 we need to call convert_from_reference on all the elements. This is an
12962 important optimization; see c++/68422. */
12963 if (!unsubstituted_packs
12964 && TREE_PURPOSE (packs) == pattern)
12965 {
12966 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12967
12968 /* If the argument pack is a single pack expansion, pull it out. */
12969 if (TREE_VEC_LENGTH (args) == 1
12970 && pack_expansion_args_count (args))
12971 return TREE_VEC_ELT (args, 0);
12972
12973 /* Types need no adjustment, nor does sizeof..., and if we still have
12974 some pack expansion args we won't do anything yet. */
12975 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12976 || PACK_EXPANSION_SIZEOF_P (t)
12977 || pack_expansion_args_count (args))
12978 return args;
12979 /* Also optimize expression pack expansions if we can tell that the
12980 elements won't have reference type. */
12981 tree type = TREE_TYPE (pattern);
12982 if (type && !TYPE_REF_P (type)
12983 && !PACK_EXPANSION_P (type)
12984 && !WILDCARD_TYPE_P (type))
12985 return args;
12986 /* Otherwise use the normal path so we get convert_from_reference. */
12987 }
12988
12989 /* We cannot expand this expansion expression, because we don't have
12990 all of the argument packs we need. */
12991 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12992 {
12993 /* We got some full packs, but we can't substitute them in until we
12994 have values for all the packs. So remember these until then. */
12995
12996 t = make_pack_expansion (pattern, complain);
12997 PACK_EXPANSION_EXTRA_ARGS (t)
12998 = build_extra_args (pattern, args, complain);
12999 return t;
13000 }
13001
13002 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13003 type, so create our own local specializations map; the current map is
13004 either NULL or (in the case of recursive unification) might have
13005 bindings that we don't want to use or alter. */
13006 local_specialization_stack lss (need_local_specializations
13007 ? lss_blank : lss_nop);
13008
13009 if (unsubstituted_packs)
13010 {
13011 /* There were no real arguments, we're just replacing a parameter
13012 pack with another version of itself. Substitute into the
13013 pattern and return a PACK_EXPANSION_*. The caller will need to
13014 deal with that. */
13015 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13016 t = tsubst_expr (pattern, args, complain, in_decl,
13017 /*integral_constant_expression_p=*/false);
13018 else
13019 t = tsubst (pattern, args, complain, in_decl);
13020 t = make_pack_expansion (t, complain);
13021 return t;
13022 }
13023
13024 gcc_assert (len >= 0);
13025
13026 /* For each argument in each argument pack, substitute into the
13027 pattern. */
13028 result = make_tree_vec (len);
13029 tree elem_args = copy_template_args (args);
13030 for (i = 0; i < len; ++i)
13031 {
13032 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13033 i,
13034 elem_args, complain,
13035 in_decl);
13036 TREE_VEC_ELT (result, i) = t;
13037 if (t == error_mark_node)
13038 {
13039 result = error_mark_node;
13040 break;
13041 }
13042 }
13043
13044 /* Update ARGS to restore the substitution from parameter packs to
13045 their argument packs. */
13046 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13047 {
13048 tree parm = TREE_PURPOSE (pack);
13049
13050 if (TREE_CODE (parm) == PARM_DECL
13051 || VAR_P (parm)
13052 || TREE_CODE (parm) == FIELD_DECL)
13053 register_local_specialization (TREE_TYPE (pack), parm);
13054 else
13055 {
13056 int idx, level;
13057
13058 if (TREE_VALUE (pack) == NULL_TREE)
13059 continue;
13060
13061 template_parm_level_and_index (parm, &level, &idx);
13062
13063 /* Update the corresponding argument. */
13064 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13065 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13066 TREE_TYPE (pack);
13067 else
13068 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13069 }
13070 }
13071
13072 /* If the dependent pack arguments were such that we end up with only a
13073 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13074 if (len == 1 && TREE_CODE (result) == TREE_VEC
13075 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13076 return TREE_VEC_ELT (result, 0);
13077
13078 return result;
13079 }
13080
13081 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13082 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13083 parameter packs; all parms generated from a function parameter pack will
13084 have the same DECL_PARM_INDEX. */
13085
13086 tree
13087 get_pattern_parm (tree parm, tree tmpl)
13088 {
13089 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13090 tree patparm;
13091
13092 if (DECL_ARTIFICIAL (parm))
13093 {
13094 for (patparm = DECL_ARGUMENTS (pattern);
13095 patparm; patparm = DECL_CHAIN (patparm))
13096 if (DECL_ARTIFICIAL (patparm)
13097 && DECL_NAME (parm) == DECL_NAME (patparm))
13098 break;
13099 }
13100 else
13101 {
13102 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13103 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13104 gcc_assert (DECL_PARM_INDEX (patparm)
13105 == DECL_PARM_INDEX (parm));
13106 }
13107
13108 return patparm;
13109 }
13110
13111 /* Make an argument pack out of the TREE_VEC VEC. */
13112
13113 static tree
13114 make_argument_pack (tree vec)
13115 {
13116 tree pack;
13117
13118 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13119 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13120 else
13121 {
13122 pack = make_node (NONTYPE_ARGUMENT_PACK);
13123 TREE_CONSTANT (pack) = 1;
13124 }
13125 SET_ARGUMENT_PACK_ARGS (pack, vec);
13126 return pack;
13127 }
13128
13129 /* Return an exact copy of template args T that can be modified
13130 independently. */
13131
13132 static tree
13133 copy_template_args (tree t)
13134 {
13135 if (t == error_mark_node)
13136 return t;
13137
13138 int len = TREE_VEC_LENGTH (t);
13139 tree new_vec = make_tree_vec (len);
13140
13141 for (int i = 0; i < len; ++i)
13142 {
13143 tree elt = TREE_VEC_ELT (t, i);
13144 if (elt && TREE_CODE (elt) == TREE_VEC)
13145 elt = copy_template_args (elt);
13146 TREE_VEC_ELT (new_vec, i) = elt;
13147 }
13148
13149 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13150 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13151
13152 return new_vec;
13153 }
13154
13155 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13156
13157 tree
13158 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13159 tree in_decl)
13160 {
13161 /* Substitute into each of the arguments. */
13162 tree new_arg = TYPE_P (orig_arg)
13163 ? cxx_make_type (TREE_CODE (orig_arg))
13164 : make_node (TREE_CODE (orig_arg));
13165
13166 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13167 args, complain, in_decl);
13168 if (pack_args == error_mark_node)
13169 new_arg = error_mark_node;
13170 else
13171 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13172
13173 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13174 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13175
13176 return new_arg;
13177 }
13178
13179 /* Substitute ARGS into the vector or list of template arguments T. */
13180
13181 tree
13182 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13183 {
13184 tree orig_t = t;
13185 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13186 tree *elts;
13187
13188 if (t == error_mark_node)
13189 return error_mark_node;
13190
13191 len = TREE_VEC_LENGTH (t);
13192 elts = XALLOCAVEC (tree, len);
13193
13194 for (i = 0; i < len; i++)
13195 {
13196 tree orig_arg = TREE_VEC_ELT (t, i);
13197 tree new_arg;
13198
13199 if (TREE_CODE (orig_arg) == TREE_VEC)
13200 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13201 else if (PACK_EXPANSION_P (orig_arg))
13202 {
13203 /* Substitute into an expansion expression. */
13204 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13205
13206 if (TREE_CODE (new_arg) == TREE_VEC)
13207 /* Add to the expanded length adjustment the number of
13208 expanded arguments. We subtract one from this
13209 measurement, because the argument pack expression
13210 itself is already counted as 1 in
13211 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13212 the argument pack is empty. */
13213 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13214 }
13215 else if (ARGUMENT_PACK_P (orig_arg))
13216 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13217 else
13218 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13219
13220 if (new_arg == error_mark_node)
13221 return error_mark_node;
13222
13223 elts[i] = new_arg;
13224 if (new_arg != orig_arg)
13225 need_new = 1;
13226 }
13227
13228 if (!need_new)
13229 return t;
13230
13231 /* Make space for the expanded arguments coming from template
13232 argument packs. */
13233 t = make_tree_vec (len + expanded_len_adjust);
13234 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13235 arguments for a member template.
13236 In that case each TREE_VEC in ORIG_T represents a level of template
13237 arguments, and ORIG_T won't carry any non defaulted argument count.
13238 It will rather be the nested TREE_VECs that will carry one.
13239 In other words, ORIG_T carries a non defaulted argument count only
13240 if it doesn't contain any nested TREE_VEC. */
13241 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13242 {
13243 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13244 count += expanded_len_adjust;
13245 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13246 }
13247 for (i = 0, out = 0; i < len; i++)
13248 {
13249 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13250 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13251 && TREE_CODE (elts[i]) == TREE_VEC)
13252 {
13253 int idx;
13254
13255 /* Now expand the template argument pack "in place". */
13256 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13257 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13258 }
13259 else
13260 {
13261 TREE_VEC_ELT (t, out) = elts[i];
13262 out++;
13263 }
13264 }
13265
13266 return t;
13267 }
13268
13269 /* Substitute ARGS into one level PARMS of template parameters. */
13270
13271 static tree
13272 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13273 {
13274 if (parms == error_mark_node)
13275 return error_mark_node;
13276
13277 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13278
13279 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13280 {
13281 tree tuple = TREE_VEC_ELT (parms, i);
13282
13283 if (tuple == error_mark_node)
13284 continue;
13285
13286 TREE_VEC_ELT (new_vec, i) =
13287 tsubst_template_parm (tuple, args, complain);
13288 }
13289
13290 return new_vec;
13291 }
13292
13293 /* Return the result of substituting ARGS into the template parameters
13294 given by PARMS. If there are m levels of ARGS and m + n levels of
13295 PARMS, then the result will contain n levels of PARMS. For
13296 example, if PARMS is `template <class T> template <class U>
13297 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13298 result will be `template <int*, double, class V>'. */
13299
13300 static tree
13301 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13302 {
13303 tree r = NULL_TREE;
13304 tree* new_parms;
13305
13306 /* When substituting into a template, we must set
13307 PROCESSING_TEMPLATE_DECL as the template parameters may be
13308 dependent if they are based on one-another, and the dependency
13309 predicates are short-circuit outside of templates. */
13310 ++processing_template_decl;
13311
13312 for (new_parms = &r;
13313 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13314 new_parms = &(TREE_CHAIN (*new_parms)),
13315 parms = TREE_CHAIN (parms))
13316 {
13317 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13318 args, complain);
13319 *new_parms =
13320 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13321 - TMPL_ARGS_DEPTH (args)),
13322 new_vec, NULL_TREE);
13323 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13324 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13325 }
13326
13327 --processing_template_decl;
13328
13329 return r;
13330 }
13331
13332 /* Return the result of substituting ARGS into one template parameter
13333 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13334 parameter and which TREE_PURPOSE is the default argument of the
13335 template parameter. */
13336
13337 static tree
13338 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13339 {
13340 tree default_value, parm_decl;
13341
13342 if (args == NULL_TREE
13343 || t == NULL_TREE
13344 || t == error_mark_node)
13345 return t;
13346
13347 gcc_assert (TREE_CODE (t) == TREE_LIST);
13348
13349 default_value = TREE_PURPOSE (t);
13350 parm_decl = TREE_VALUE (t);
13351 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13352
13353 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13354 if (TREE_CODE (parm_decl) == PARM_DECL
13355 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13356 parm_decl = error_mark_node;
13357 default_value = tsubst_template_arg (default_value, args,
13358 complain, NULL_TREE);
13359 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13360
13361 tree r = build_tree_list (default_value, parm_decl);
13362 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13363 return r;
13364 }
13365
13366 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13367 type T. If T is not an aggregate or enumeration type, it is
13368 handled as if by tsubst. IN_DECL is as for tsubst. If
13369 ENTERING_SCOPE is nonzero, T is the context for a template which
13370 we are presently tsubst'ing. Return the substituted value. */
13371
13372 static tree
13373 tsubst_aggr_type (tree t,
13374 tree args,
13375 tsubst_flags_t complain,
13376 tree in_decl,
13377 int entering_scope)
13378 {
13379 if (t == NULL_TREE)
13380 return NULL_TREE;
13381
13382 switch (TREE_CODE (t))
13383 {
13384 case RECORD_TYPE:
13385 if (TYPE_PTRMEMFUNC_P (t))
13386 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13387
13388 /* Fall through. */
13389 case ENUMERAL_TYPE:
13390 case UNION_TYPE:
13391 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13392 {
13393 tree argvec;
13394 tree context;
13395 tree r;
13396
13397 /* In "sizeof(X<I>)" we need to evaluate "I". */
13398 cp_evaluated ev;
13399
13400 /* First, determine the context for the type we are looking
13401 up. */
13402 context = TYPE_CONTEXT (t);
13403 if (context && TYPE_P (context))
13404 {
13405 context = tsubst_aggr_type (context, args, complain,
13406 in_decl, /*entering_scope=*/1);
13407 /* If context is a nested class inside a class template,
13408 it may still need to be instantiated (c++/33959). */
13409 context = complete_type (context);
13410 }
13411
13412 /* Then, figure out what arguments are appropriate for the
13413 type we are trying to find. For example, given:
13414
13415 template <class T> struct S;
13416 template <class T, class U> void f(T, U) { S<U> su; }
13417
13418 and supposing that we are instantiating f<int, double>,
13419 then our ARGS will be {int, double}, but, when looking up
13420 S we only want {double}. */
13421 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13422 complain, in_decl);
13423 if (argvec == error_mark_node)
13424 r = error_mark_node;
13425 else if (cxx_dialect >= cxx17 && dependent_scope_p (context))
13426 {
13427 /* See maybe_dependent_member_ref. */
13428 tree name = TYPE_IDENTIFIER (t);
13429 tree fullname = name;
13430 if (instantiates_primary_template_p (t))
13431 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13432 INNERMOST_TEMPLATE_ARGS (argvec));
13433 return build_typename_type (context, name, fullname,
13434 typename_type);
13435 }
13436 else
13437 {
13438 r = lookup_template_class (t, argvec, in_decl, context,
13439 entering_scope, complain);
13440 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13441 }
13442
13443 return r;
13444 }
13445 else
13446 /* This is not a template type, so there's nothing to do. */
13447 return t;
13448
13449 default:
13450 return tsubst (t, args, complain, in_decl);
13451 }
13452 }
13453
13454 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13455
13456 /* Substitute into the default argument ARG (a default argument for
13457 FN), which has the indicated TYPE. */
13458
13459 tree
13460 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13461 tsubst_flags_t complain)
13462 {
13463 int errs = errorcount + sorrycount;
13464
13465 /* This can happen in invalid code. */
13466 if (TREE_CODE (arg) == DEFERRED_PARSE)
13467 return arg;
13468
13469 /* Shortcut {}. */
13470 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13471 && CONSTRUCTOR_NELTS (arg) == 0)
13472 return arg;
13473
13474 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13475 parm = chain_index (parmnum, parm);
13476 tree parmtype = TREE_TYPE (parm);
13477 if (DECL_BY_REFERENCE (parm))
13478 parmtype = TREE_TYPE (parmtype);
13479 if (parmtype == error_mark_node)
13480 return error_mark_node;
13481
13482 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13483
13484 tree *slot;
13485 if (defarg_inst && (slot = defarg_inst->get (parm)))
13486 return *slot;
13487
13488 /* This default argument came from a template. Instantiate the
13489 default argument here, not in tsubst. In the case of
13490 something like:
13491
13492 template <class T>
13493 struct S {
13494 static T t();
13495 void f(T = t());
13496 };
13497
13498 we must be careful to do name lookup in the scope of S<T>,
13499 rather than in the current class. */
13500 push_to_top_level ();
13501 push_access_scope (fn);
13502 push_deferring_access_checks (dk_no_deferred);
13503 start_lambda_scope (parm);
13504
13505 /* The default argument expression may cause implicitly defined
13506 member functions to be synthesized, which will result in garbage
13507 collection. We must treat this situation as if we were within
13508 the body of function so as to avoid collecting live data on the
13509 stack. */
13510 ++function_depth;
13511 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13512 complain, NULL_TREE,
13513 /*integral_constant_expression_p=*/false);
13514 --function_depth;
13515
13516 finish_lambda_scope ();
13517
13518 /* Make sure the default argument is reasonable. */
13519 arg = check_default_argument (type, arg, complain);
13520
13521 if (errorcount+sorrycount > errs
13522 && (complain & tf_warning_or_error))
13523 inform (input_location,
13524 " when instantiating default argument for call to %qD", fn);
13525
13526 pop_deferring_access_checks ();
13527 pop_access_scope (fn);
13528 pop_from_top_level ();
13529
13530 if (arg != error_mark_node && !cp_unevaluated_operand)
13531 {
13532 if (!defarg_inst)
13533 defarg_inst = decl_tree_cache_map::create_ggc (37);
13534 defarg_inst->put (parm, arg);
13535 }
13536
13537 return arg;
13538 }
13539
13540 /* Substitute into all the default arguments for FN. */
13541
13542 static void
13543 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13544 {
13545 tree arg;
13546 tree tmpl_args;
13547
13548 tmpl_args = DECL_TI_ARGS (fn);
13549
13550 /* If this function is not yet instantiated, we certainly don't need
13551 its default arguments. */
13552 if (uses_template_parms (tmpl_args))
13553 return;
13554 /* Don't do this again for clones. */
13555 if (DECL_CLONED_FUNCTION_P (fn))
13556 return;
13557
13558 int i = 0;
13559 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13560 arg;
13561 arg = TREE_CHAIN (arg), ++i)
13562 if (TREE_PURPOSE (arg))
13563 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13564 TREE_VALUE (arg),
13565 TREE_PURPOSE (arg),
13566 complain);
13567 }
13568
13569 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13570 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13571
13572 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13573
13574 void
13575 store_explicit_specifier (tree v, tree t)
13576 {
13577 if (!explicit_specifier_map)
13578 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13579 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13580 explicit_specifier_map->put (v, t);
13581 }
13582
13583 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13584
13585 static tree
13586 lookup_explicit_specifier (tree v)
13587 {
13588 return *explicit_specifier_map->get (v);
13589 }
13590
13591 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13592 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13593 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13594 identical to T. */
13595
13596 static tree
13597 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13598 tree raises, tsubst_flags_t complain)
13599 {
13600 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13601
13602 tree new_type;
13603 if (TREE_CODE (t) == FUNCTION_TYPE)
13604 {
13605 new_type = build_function_type (return_type, arg_types);
13606 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13607 }
13608 else
13609 {
13610 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13611 /* Don't pick up extra function qualifiers from the basetype. */
13612 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13613 if (! MAYBE_CLASS_TYPE_P (r))
13614 {
13615 /* [temp.deduct]
13616
13617 Type deduction may fail for any of the following
13618 reasons:
13619
13620 -- Attempting to create "pointer to member of T" when T
13621 is not a class type. */
13622 if (complain & tf_error)
13623 error ("creating pointer to member function of non-class type %qT",
13624 r);
13625 return error_mark_node;
13626 }
13627
13628 new_type = build_method_type_directly (r, return_type,
13629 TREE_CHAIN (arg_types));
13630 }
13631 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13632
13633 cp_ref_qualifier rqual = type_memfn_rqual (t);
13634 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13635 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13636 }
13637
13638 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13639 each of its formal parameters. If there is a disagreement then rebuild
13640 DECL's function type according to its formal parameter types, as part of a
13641 resolution for Core issues 1001/1322. */
13642
13643 static void
13644 maybe_rebuild_function_decl_type (tree decl)
13645 {
13646 bool function_type_needs_rebuilding = false;
13647 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13648 {
13649 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13650 while (parm_type_list && parm_type_list != void_list_node)
13651 {
13652 tree parm_type = TREE_VALUE (parm_type_list);
13653 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13654 if (!same_type_p (parm_type, formal_parm_type_unqual))
13655 {
13656 function_type_needs_rebuilding = true;
13657 break;
13658 }
13659
13660 parm_list = DECL_CHAIN (parm_list);
13661 parm_type_list = TREE_CHAIN (parm_type_list);
13662 }
13663 }
13664
13665 if (!function_type_needs_rebuilding)
13666 return;
13667
13668 const tree fntype = TREE_TYPE (decl);
13669 tree parm_list = DECL_ARGUMENTS (decl);
13670 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13671 tree new_parm_type_list = NULL_TREE;
13672 tree *q = &new_parm_type_list;
13673 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13674 {
13675 *q = copy_node (old_parm_type_list);
13676 parm_list = DECL_CHAIN (parm_list);
13677 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13678 q = &TREE_CHAIN (*q);
13679 }
13680 while (old_parm_type_list && old_parm_type_list != void_list_node)
13681 {
13682 *q = copy_node (old_parm_type_list);
13683 tree *new_parm_type = &TREE_VALUE (*q);
13684 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13685 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13686 *new_parm_type = formal_parm_type_unqual;
13687
13688 parm_list = DECL_CHAIN (parm_list);
13689 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13690 q = &TREE_CHAIN (*q);
13691 }
13692 if (old_parm_type_list == void_list_node)
13693 *q = void_list_node;
13694
13695 TREE_TYPE (decl)
13696 = rebuild_function_or_method_type (fntype,
13697 TREE_TYPE (fntype), new_parm_type_list,
13698 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13699 }
13700
13701 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13702
13703 static tree
13704 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13705 tree lambda_fntype)
13706 {
13707 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13708 hashval_t hash = 0;
13709 tree in_decl = t;
13710
13711 /* Nobody should be tsubst'ing into non-template functions. */
13712 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13713 || DECL_LOCAL_DECL_P (t));
13714
13715 if (DECL_LOCAL_DECL_P (t))
13716 {
13717 if (tree spec = retrieve_local_specialization (t))
13718 return spec;
13719 }
13720 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13721 {
13722 /* If T is not dependent, just return it. */
13723 if (!uses_template_parms (DECL_TI_ARGS (t))
13724 && !LAMBDA_FUNCTION_P (t))
13725 return t;
13726
13727 /* Calculate the most general template of which R is a
13728 specialization. */
13729 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13730
13731 /* We're substituting a lambda function under tsubst_lambda_expr but not
13732 directly from it; find the matching function we're already inside.
13733 But don't do this if T is a generic lambda with a single level of
13734 template parms, as in that case we're doing a normal instantiation. */
13735 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13736 && (!generic_lambda_fn_p (t)
13737 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13738 return enclosing_instantiation_of (t);
13739
13740 /* Calculate the complete set of arguments used to
13741 specialize R. */
13742 argvec = tsubst_template_args (DECL_TI_ARGS
13743 (DECL_TEMPLATE_RESULT
13744 (DECL_TI_TEMPLATE (t))),
13745 args, complain, in_decl);
13746 if (argvec == error_mark_node)
13747 return error_mark_node;
13748
13749 /* Check to see if we already have this specialization. */
13750 if (!lambda_fntype)
13751 {
13752 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13753 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13754 return spec;
13755 }
13756
13757 /* We can see more levels of arguments than parameters if
13758 there was a specialization of a member template, like
13759 this:
13760
13761 template <class T> struct S { template <class U> void f(); }
13762 template <> template <class U> void S<int>::f(U);
13763
13764 Here, we'll be substituting into the specialization,
13765 because that's where we can find the code we actually
13766 want to generate, but we'll have enough arguments for
13767 the most general template.
13768
13769 We also deal with the peculiar case:
13770
13771 template <class T> struct S {
13772 template <class U> friend void f();
13773 };
13774 template <class U> void f() {}
13775 template S<int>;
13776 template void f<double>();
13777
13778 Here, the ARGS for the instantiation of will be {int,
13779 double}. But, we only need as many ARGS as there are
13780 levels of template parameters in CODE_PATTERN. We are
13781 careful not to get fooled into reducing the ARGS in
13782 situations like:
13783
13784 template <class T> struct S { template <class U> void f(U); }
13785 template <class T> template <> void S<T>::f(int) {}
13786
13787 which we can spot because the pattern will be a
13788 specialization in this case. */
13789 int args_depth = TMPL_ARGS_DEPTH (args);
13790 int parms_depth =
13791 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13792
13793 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13794 args = get_innermost_template_args (args, parms_depth);
13795 }
13796 else
13797 {
13798 /* This special case arises when we have something like this:
13799
13800 template <class T> struct S {
13801 friend void f<int>(int, double);
13802 };
13803
13804 Here, the DECL_TI_TEMPLATE for the friend declaration
13805 will be an IDENTIFIER_NODE. We are being called from
13806 tsubst_friend_function, and we want only to create a
13807 new decl (R) with appropriate types so that we can call
13808 determine_specialization. */
13809 gen_tmpl = NULL_TREE;
13810 argvec = NULL_TREE;
13811 }
13812
13813 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13814 : NULL_TREE);
13815 tree ctx = closure ? closure : DECL_CONTEXT (t);
13816 bool member = ctx && TYPE_P (ctx);
13817
13818 if (member && !closure)
13819 ctx = tsubst_aggr_type (ctx, args,
13820 complain, t, /*entering_scope=*/1);
13821
13822 tree type = (lambda_fntype ? lambda_fntype
13823 : tsubst (TREE_TYPE (t), args,
13824 complain | tf_fndecl_type, in_decl));
13825 if (type == error_mark_node)
13826 return error_mark_node;
13827
13828 /* If we hit excessive deduction depth, the type is bogus even if
13829 it isn't error_mark_node, so don't build a decl. */
13830 if (excessive_deduction_depth)
13831 return error_mark_node;
13832
13833 /* We do NOT check for matching decls pushed separately at this
13834 point, as they may not represent instantiations of this
13835 template, and in any case are considered separate under the
13836 discrete model. */
13837 tree r = copy_decl (t);
13838 DECL_USE_TEMPLATE (r) = 0;
13839 TREE_TYPE (r) = type;
13840 /* Clear out the mangled name and RTL for the instantiation. */
13841 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13842 SET_DECL_RTL (r, NULL);
13843 /* Leave DECL_INITIAL set on deleted instantiations. */
13844 if (!DECL_DELETED_FN (r))
13845 DECL_INITIAL (r) = NULL_TREE;
13846 DECL_CONTEXT (r) = ctx;
13847
13848 /* Handle explicit(dependent-expr). */
13849 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13850 {
13851 tree spec = lookup_explicit_specifier (t);
13852 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13853 /*function_p=*/false,
13854 /*i_c_e_p=*/true);
13855 spec = build_explicit_specifier (spec, complain);
13856 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13857 }
13858
13859 /* OpenMP UDRs have the only argument a reference to the declared
13860 type. We want to diagnose if the declared type is a reference,
13861 which is invalid, but as references to references are usually
13862 quietly merged, diagnose it here. */
13863 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13864 {
13865 tree argtype
13866 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13867 argtype = tsubst (argtype, args, complain, in_decl);
13868 if (TYPE_REF_P (argtype))
13869 error_at (DECL_SOURCE_LOCATION (t),
13870 "reference type %qT in "
13871 "%<#pragma omp declare reduction%>", argtype);
13872 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13873 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13874 argtype);
13875 }
13876
13877 if (member && DECL_CONV_FN_P (r))
13878 /* Type-conversion operator. Reconstruct the name, in
13879 case it's the name of one of the template's parameters. */
13880 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13881
13882 tree parms = DECL_ARGUMENTS (t);
13883 if (closure)
13884 parms = DECL_CHAIN (parms);
13885 parms = tsubst (parms, args, complain, t);
13886 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13887 DECL_CONTEXT (parm) = r;
13888 if (closure)
13889 {
13890 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13891 DECL_NAME (tparm) = closure_identifier;
13892 DECL_CHAIN (tparm) = parms;
13893 parms = tparm;
13894 }
13895 DECL_ARGUMENTS (r) = parms;
13896 DECL_RESULT (r) = NULL_TREE;
13897
13898 maybe_rebuild_function_decl_type (r);
13899
13900 TREE_STATIC (r) = 0;
13901 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13902 DECL_EXTERNAL (r) = 1;
13903 /* If this is an instantiation of a function with internal
13904 linkage, we already know what object file linkage will be
13905 assigned to the instantiation. */
13906 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13907 DECL_DEFER_OUTPUT (r) = 0;
13908 DECL_CHAIN (r) = NULL_TREE;
13909 DECL_PENDING_INLINE_INFO (r) = 0;
13910 DECL_PENDING_INLINE_P (r) = 0;
13911 DECL_SAVED_TREE (r) = NULL_TREE;
13912 DECL_STRUCT_FUNCTION (r) = NULL;
13913 TREE_USED (r) = 0;
13914 /* We'll re-clone as appropriate in instantiate_template. */
13915 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13916
13917 /* If we aren't complaining now, return on error before we register
13918 the specialization so that we'll complain eventually. */
13919 if ((complain & tf_error) == 0
13920 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13921 && !grok_op_properties (r, /*complain=*/false))
13922 return error_mark_node;
13923
13924 /* Associate the constraints directly with the instantiation. We
13925 don't substitute through the constraints; that's only done when
13926 they are checked. */
13927 if (tree ci = get_constraints (t))
13928 /* Unless we're regenerating a lambda, in which case we'll set the
13929 lambda's constraints in tsubst_lambda_expr. */
13930 if (!lambda_fntype)
13931 set_constraints (r, ci);
13932
13933 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13934 SET_DECL_FRIEND_CONTEXT (r,
13935 tsubst (DECL_FRIEND_CONTEXT (t),
13936 args, complain, in_decl));
13937
13938 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13939 this in the special friend case mentioned above where
13940 GEN_TMPL is NULL. */
13941 if (gen_tmpl && !closure)
13942 {
13943 DECL_TEMPLATE_INFO (r)
13944 = build_template_info (gen_tmpl, argvec);
13945 SET_DECL_IMPLICIT_INSTANTIATION (r);
13946
13947 tree new_r
13948 = register_specialization (r, gen_tmpl, argvec, false, hash);
13949 if (new_r != r)
13950 /* We instantiated this while substituting into
13951 the type earlier (template/friend54.C). */
13952 return new_r;
13953
13954 /* We're not supposed to instantiate default arguments
13955 until they are called, for a template. But, for a
13956 declaration like:
13957
13958 template <class T> void f ()
13959 { extern void g(int i = T()); }
13960
13961 we should do the substitution when the template is
13962 instantiated. We handle the member function case in
13963 instantiate_class_template since the default arguments
13964 might refer to other members of the class. */
13965 if (!member
13966 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13967 && !uses_template_parms (argvec))
13968 tsubst_default_arguments (r, complain);
13969 }
13970 else if (DECL_LOCAL_DECL_P (r))
13971 {
13972 if (!cp_unevaluated_operand)
13973 register_local_specialization (r, t);
13974 }
13975 else
13976 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13977
13978 /* Copy the list of befriending classes. */
13979 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13980 *friends;
13981 friends = &TREE_CHAIN (*friends))
13982 {
13983 *friends = copy_node (*friends);
13984 TREE_VALUE (*friends)
13985 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13986 }
13987
13988 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13989 {
13990 maybe_retrofit_in_chrg (r);
13991 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13992 return error_mark_node;
13993 /* If this is an instantiation of a member template, clone it.
13994 If it isn't, that'll be handled by
13995 clone_constructors_and_destructors. */
13996 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13997 clone_cdtor (r, /*update_methods=*/false);
13998 }
13999 else if ((complain & tf_error) != 0
14000 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14001 && !grok_op_properties (r, /*complain=*/true))
14002 return error_mark_node;
14003
14004 /* Possibly limit visibility based on template args. */
14005 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14006 if (DECL_VISIBILITY_SPECIFIED (t))
14007 {
14008 DECL_VISIBILITY_SPECIFIED (r) = 0;
14009 DECL_ATTRIBUTES (r)
14010 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14011 }
14012 determine_visibility (r);
14013 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14014 && !processing_template_decl)
14015 defaulted_late_check (r);
14016
14017 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14018 args, complain, in_decl);
14019 if (flag_openmp)
14020 if (tree attr = lookup_attribute ("omp declare variant base",
14021 DECL_ATTRIBUTES (r)))
14022 omp_declare_variant_finalize (r, attr);
14023
14024 return r;
14025 }
14026
14027 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14028
14029 static tree
14030 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14031 tree lambda_fntype)
14032 {
14033 /* We can get here when processing a member function template,
14034 member class template, or template template parameter. */
14035 tree decl = DECL_TEMPLATE_RESULT (t);
14036 tree in_decl = t;
14037 tree spec;
14038 tree tmpl_args;
14039 tree full_args;
14040 tree r;
14041 hashval_t hash = 0;
14042
14043 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14044 {
14045 /* Template template parameter is treated here. */
14046 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14047 if (new_type == error_mark_node)
14048 r = error_mark_node;
14049 /* If we get a real template back, return it. This can happen in
14050 the context of most_specialized_partial_spec. */
14051 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14052 r = new_type;
14053 else
14054 /* The new TEMPLATE_DECL was built in
14055 reduce_template_parm_level. */
14056 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14057 return r;
14058 }
14059
14060 if (!lambda_fntype)
14061 {
14062 /* We might already have an instance of this template.
14063 The ARGS are for the surrounding class type, so the
14064 full args contain the tsubst'd args for the context,
14065 plus the innermost args from the template decl. */
14066 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14067 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14068 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14069 /* Because this is a template, the arguments will still be
14070 dependent, even after substitution. If
14071 PROCESSING_TEMPLATE_DECL is not set, the dependency
14072 predicates will short-circuit. */
14073 ++processing_template_decl;
14074 full_args = tsubst_template_args (tmpl_args, args,
14075 complain, in_decl);
14076 --processing_template_decl;
14077 if (full_args == error_mark_node)
14078 return error_mark_node;
14079
14080 /* If this is a default template template argument,
14081 tsubst might not have changed anything. */
14082 if (full_args == tmpl_args)
14083 return t;
14084
14085 hash = hash_tmpl_and_args (t, full_args);
14086 spec = retrieve_specialization (t, full_args, hash);
14087 if (spec != NULL_TREE)
14088 {
14089 if (TYPE_P (spec))
14090 /* Type partial instantiations are stored as the type by
14091 lookup_template_class_1, not here as the template. */
14092 spec = CLASSTYPE_TI_TEMPLATE (spec);
14093 return spec;
14094 }
14095 }
14096
14097 /* Make a new template decl. It will be similar to the
14098 original, but will record the current template arguments.
14099 We also create a new function declaration, which is just
14100 like the old one, but points to this new template, rather
14101 than the old one. */
14102 r = copy_decl (t);
14103 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14104 DECL_CHAIN (r) = NULL_TREE;
14105
14106 // Build new template info linking to the original template decl.
14107 if (!lambda_fntype)
14108 {
14109 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14110 SET_DECL_IMPLICIT_INSTANTIATION (r);
14111 }
14112 else
14113 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14114
14115 /* The template parameters for this new template are all the
14116 template parameters for the old template, except the
14117 outermost level of parameters. */
14118 DECL_TEMPLATE_PARMS (r)
14119 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14120 complain);
14121
14122 bool class_p = false;
14123 tree inner = decl;
14124 ++processing_template_decl;
14125 if (TREE_CODE (inner) == FUNCTION_DECL)
14126 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14127 else
14128 {
14129 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14130 {
14131 class_p = true;
14132 inner = TREE_TYPE (inner);
14133 }
14134 inner = tsubst (inner, args, complain, in_decl);
14135 }
14136 --processing_template_decl;
14137 if (inner == error_mark_node)
14138 return error_mark_node;
14139
14140 if (class_p)
14141 {
14142 /* For a partial specialization, we need to keep pointing to
14143 the primary template. */
14144 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14145 CLASSTYPE_TI_TEMPLATE (inner) = r;
14146
14147 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14148 inner = TYPE_MAIN_DECL (inner);
14149 }
14150 else if (lambda_fntype)
14151 {
14152 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14153 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14154 }
14155 else
14156 {
14157 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14158 DECL_TI_TEMPLATE (inner) = r;
14159 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14160 }
14161
14162 DECL_TEMPLATE_RESULT (r) = inner;
14163 TREE_TYPE (r) = TREE_TYPE (inner);
14164 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14165
14166 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14167 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14168
14169 if (PRIMARY_TEMPLATE_P (t))
14170 DECL_PRIMARY_TEMPLATE (r) = r;
14171
14172 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
14173 && !lambda_fntype)
14174 /* Record this non-type partial instantiation. */
14175 register_specialization (r, t,
14176 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14177 false, hash);
14178
14179 return r;
14180 }
14181
14182 /* True if FN is the op() for a lambda in an uninstantiated template. */
14183
14184 bool
14185 lambda_fn_in_template_p (tree fn)
14186 {
14187 if (!fn || !LAMBDA_FUNCTION_P (fn))
14188 return false;
14189 tree closure = DECL_CONTEXT (fn);
14190 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14191 }
14192
14193 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14194 which the above is true. */
14195
14196 bool
14197 instantiated_lambda_fn_p (tree fn)
14198 {
14199 if (!fn || !LAMBDA_FUNCTION_P (fn))
14200 return false;
14201 tree closure = DECL_CONTEXT (fn);
14202 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14203 return LAMBDA_EXPR_INSTANTIATED (lam);
14204 }
14205
14206 /* We're instantiating a variable from template function TCTX. Return the
14207 corresponding current enclosing scope. This gets complicated because lambda
14208 functions in templates are regenerated rather than instantiated, but generic
14209 lambda functions are subsequently instantiated. */
14210
14211 static tree
14212 enclosing_instantiation_of (tree otctx)
14213 {
14214 tree tctx = otctx;
14215 tree fn = current_function_decl;
14216 int lambda_count = 0;
14217
14218 for (; tctx && (lambda_fn_in_template_p (tctx)
14219 || instantiated_lambda_fn_p (tctx));
14220 tctx = decl_function_context (tctx))
14221 ++lambda_count;
14222 for (; fn; fn = decl_function_context (fn))
14223 {
14224 tree ofn = fn;
14225 int flambda_count = 0;
14226 for (; fn && instantiated_lambda_fn_p (fn);
14227 fn = decl_function_context (fn))
14228 ++flambda_count;
14229 if ((fn && DECL_TEMPLATE_INFO (fn))
14230 ? most_general_template (fn) != most_general_template (tctx)
14231 : fn != tctx)
14232 continue;
14233 if (flambda_count != lambda_count)
14234 {
14235 gcc_assert (flambda_count > lambda_count);
14236 for (; flambda_count > lambda_count; --flambda_count)
14237 ofn = decl_function_context (ofn);
14238 }
14239 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14240 || DECL_CONV_FN_P (ofn));
14241 return ofn;
14242 }
14243 gcc_unreachable ();
14244 }
14245
14246 /* Substitute the ARGS into the T, which is a _DECL. Return the
14247 result of the substitution. Issue error and warning messages under
14248 control of COMPLAIN. */
14249
14250 static tree
14251 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14252 {
14253 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14254 location_t saved_loc;
14255 tree r = NULL_TREE;
14256 tree in_decl = t;
14257 hashval_t hash = 0;
14258
14259 /* Set the filename and linenumber to improve error-reporting. */
14260 saved_loc = input_location;
14261 input_location = DECL_SOURCE_LOCATION (t);
14262
14263 switch (TREE_CODE (t))
14264 {
14265 case TEMPLATE_DECL:
14266 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14267 break;
14268
14269 case FUNCTION_DECL:
14270 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14271 break;
14272
14273 case PARM_DECL:
14274 {
14275 tree type = NULL_TREE;
14276 int i, len = 1;
14277 tree expanded_types = NULL_TREE;
14278 tree prev_r = NULL_TREE;
14279 tree first_r = NULL_TREE;
14280
14281 if (DECL_PACK_P (t))
14282 {
14283 /* If there is a local specialization that isn't a
14284 parameter pack, it means that we're doing a "simple"
14285 substitution from inside tsubst_pack_expansion. Just
14286 return the local specialization (which will be a single
14287 parm). */
14288 tree spec = retrieve_local_specialization (t);
14289 if (spec
14290 && TREE_CODE (spec) == PARM_DECL
14291 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14292 RETURN (spec);
14293
14294 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14295 the parameters in this function parameter pack. */
14296 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14297 complain, in_decl);
14298 if (TREE_CODE (expanded_types) == TREE_VEC)
14299 {
14300 len = TREE_VEC_LENGTH (expanded_types);
14301
14302 /* Zero-length parameter packs are boring. Just substitute
14303 into the chain. */
14304 if (len == 0 && !cp_unevaluated_operand)
14305 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14306 TREE_CHAIN (t)));
14307 }
14308 else
14309 {
14310 /* All we did was update the type. Make a note of that. */
14311 type = expanded_types;
14312 expanded_types = NULL_TREE;
14313 }
14314 }
14315
14316 /* Loop through all of the parameters we'll build. When T is
14317 a function parameter pack, LEN is the number of expanded
14318 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14319 r = NULL_TREE;
14320 for (i = 0; i < len; ++i)
14321 {
14322 prev_r = r;
14323 r = copy_node (t);
14324 if (DECL_TEMPLATE_PARM_P (t))
14325 SET_DECL_TEMPLATE_PARM_P (r);
14326
14327 if (expanded_types)
14328 /* We're on the Ith parameter of the function parameter
14329 pack. */
14330 {
14331 /* Get the Ith type. */
14332 type = TREE_VEC_ELT (expanded_types, i);
14333
14334 /* Rename the parameter to include the index. */
14335 DECL_NAME (r)
14336 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14337 }
14338 else if (!type)
14339 /* We're dealing with a normal parameter. */
14340 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14341
14342 type = type_decays_to (type);
14343 TREE_TYPE (r) = type;
14344 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14345
14346 if (DECL_INITIAL (r))
14347 {
14348 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14349 DECL_INITIAL (r) = TREE_TYPE (r);
14350 else
14351 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14352 complain, in_decl);
14353 }
14354
14355 DECL_CONTEXT (r) = NULL_TREE;
14356
14357 if (!DECL_TEMPLATE_PARM_P (r))
14358 DECL_ARG_TYPE (r) = type_passed_as (type);
14359
14360 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14361 args, complain, in_decl);
14362
14363 /* Keep track of the first new parameter we
14364 generate. That's what will be returned to the
14365 caller. */
14366 if (!first_r)
14367 first_r = r;
14368
14369 /* Build a proper chain of parameters when substituting
14370 into a function parameter pack. */
14371 if (prev_r)
14372 DECL_CHAIN (prev_r) = r;
14373 }
14374
14375 /* If cp_unevaluated_operand is set, we're just looking for a
14376 single dummy parameter, so don't keep going. */
14377 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14378 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14379 complain, DECL_CHAIN (t));
14380
14381 /* FIRST_R contains the start of the chain we've built. */
14382 r = first_r;
14383 }
14384 break;
14385
14386 case FIELD_DECL:
14387 {
14388 tree type = NULL_TREE;
14389 tree vec = NULL_TREE;
14390 tree expanded_types = NULL_TREE;
14391 int len = 1;
14392
14393 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14394 {
14395 /* This field is a lambda capture pack. Return a TREE_VEC of
14396 the expanded fields to instantiate_class_template_1. */
14397 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14398 complain, in_decl);
14399 if (TREE_CODE (expanded_types) == TREE_VEC)
14400 {
14401 len = TREE_VEC_LENGTH (expanded_types);
14402 vec = make_tree_vec (len);
14403 }
14404 else
14405 {
14406 /* All we did was update the type. Make a note of that. */
14407 type = expanded_types;
14408 expanded_types = NULL_TREE;
14409 }
14410 }
14411
14412 for (int i = 0; i < len; ++i)
14413 {
14414 r = copy_decl (t);
14415 if (expanded_types)
14416 {
14417 type = TREE_VEC_ELT (expanded_types, i);
14418 DECL_NAME (r)
14419 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14420 }
14421 else if (!type)
14422 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14423
14424 if (type == error_mark_node)
14425 RETURN (error_mark_node);
14426 TREE_TYPE (r) = type;
14427 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14428
14429 if (DECL_C_BIT_FIELD (r))
14430 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14431 number of bits. */
14432 DECL_BIT_FIELD_REPRESENTATIVE (r)
14433 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14434 complain, in_decl,
14435 /*integral_constant_expression_p=*/true);
14436 if (DECL_INITIAL (t))
14437 {
14438 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14439 NSDMI in perform_member_init. Still set DECL_INITIAL
14440 so that we know there is one. */
14441 DECL_INITIAL (r) = void_node;
14442 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14443 retrofit_lang_decl (r);
14444 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14445 }
14446 /* We don't have to set DECL_CONTEXT here; it is set by
14447 finish_member_declaration. */
14448 DECL_CHAIN (r) = NULL_TREE;
14449
14450 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14451 args, complain, in_decl);
14452
14453 if (vec)
14454 TREE_VEC_ELT (vec, i) = r;
14455 }
14456
14457 if (vec)
14458 r = vec;
14459 }
14460 break;
14461
14462 case USING_DECL:
14463 /* We reach here only for member using decls. We also need to check
14464 uses_template_parms because DECL_DEPENDENT_P is not set for a
14465 using-declaration that designates a member of the current
14466 instantiation (c++/53549). */
14467 if (DECL_DEPENDENT_P (t)
14468 || uses_template_parms (USING_DECL_SCOPE (t)))
14469 {
14470 tree scope = USING_DECL_SCOPE (t);
14471 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14472 if (PACK_EXPANSION_P (scope))
14473 {
14474 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14475 int len = TREE_VEC_LENGTH (vec);
14476 r = make_tree_vec (len);
14477 for (int i = 0; i < len; ++i)
14478 {
14479 tree escope = TREE_VEC_ELT (vec, i);
14480 tree elt = do_class_using_decl (escope, name);
14481 if (!elt)
14482 {
14483 r = error_mark_node;
14484 break;
14485 }
14486 else
14487 {
14488 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14489 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14490 }
14491 TREE_VEC_ELT (r, i) = elt;
14492 }
14493 }
14494 else
14495 {
14496 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14497 complain, in_decl);
14498 r = do_class_using_decl (inst_scope, name);
14499 if (!r)
14500 r = error_mark_node;
14501 else
14502 {
14503 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14504 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14505 }
14506 }
14507 }
14508 else
14509 {
14510 r = copy_node (t);
14511 DECL_CHAIN (r) = NULL_TREE;
14512 }
14513 break;
14514
14515 case TYPE_DECL:
14516 case VAR_DECL:
14517 {
14518 tree argvec = NULL_TREE;
14519 tree gen_tmpl = NULL_TREE;
14520 tree tmpl = NULL_TREE;
14521 tree type = NULL_TREE;
14522
14523 if (TREE_TYPE (t) == error_mark_node)
14524 RETURN (error_mark_node);
14525
14526 if (TREE_CODE (t) == TYPE_DECL
14527 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14528 {
14529 /* If this is the canonical decl, we don't have to
14530 mess with instantiations, and often we can't (for
14531 typename, template type parms and such). Note that
14532 TYPE_NAME is not correct for the above test if
14533 we've copied the type for a typedef. */
14534 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14535 if (type == error_mark_node)
14536 RETURN (error_mark_node);
14537 r = TYPE_NAME (type);
14538 break;
14539 }
14540
14541 /* Check to see if we already have the specialization we
14542 need. */
14543 tree spec = NULL_TREE;
14544 bool local_p = false;
14545 tree ctx = DECL_CONTEXT (t);
14546 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14547 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14548 {
14549 local_p = false;
14550 if (DECL_CLASS_SCOPE_P (t))
14551 {
14552 ctx = tsubst_aggr_type (ctx, args,
14553 complain,
14554 in_decl, /*entering_scope=*/1);
14555 /* If CTX is unchanged, then T is in fact the
14556 specialization we want. That situation occurs when
14557 referencing a static data member within in its own
14558 class. We can use pointer equality, rather than
14559 same_type_p, because DECL_CONTEXT is always
14560 canonical... */
14561 if (ctx == DECL_CONTEXT (t)
14562 /* ... unless T is a member template; in which
14563 case our caller can be willing to create a
14564 specialization of that template represented
14565 by T. */
14566 && !(DECL_TI_TEMPLATE (t)
14567 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14568 spec = t;
14569 }
14570
14571 if (!spec)
14572 {
14573 tmpl = DECL_TI_TEMPLATE (t);
14574 gen_tmpl = most_general_template (tmpl);
14575 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14576 if (argvec != error_mark_node)
14577 argvec = (coerce_innermost_template_parms
14578 (DECL_TEMPLATE_PARMS (gen_tmpl),
14579 argvec, t, complain,
14580 /*all*/true, /*defarg*/true));
14581 if (argvec == error_mark_node)
14582 RETURN (error_mark_node);
14583 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14584 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14585 }
14586 }
14587 else
14588 {
14589 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14590 /* Subsequent calls to pushdecl will fill this in. */
14591 ctx = NULL_TREE;
14592 /* A local variable. */
14593 local_p = true;
14594 /* Unless this is a reference to a static variable from an
14595 enclosing function, in which case we need to fill it in now. */
14596 if (TREE_STATIC (t))
14597 {
14598 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14599 if (fn != current_function_decl)
14600 ctx = fn;
14601 }
14602 spec = retrieve_local_specialization (t);
14603 }
14604 /* If we already have the specialization we need, there is
14605 nothing more to do. */
14606 if (spec)
14607 {
14608 r = spec;
14609 break;
14610 }
14611
14612 /* Create a new node for the specialization we need. */
14613 if (type == NULL_TREE)
14614 {
14615 if (is_typedef_decl (t))
14616 type = DECL_ORIGINAL_TYPE (t);
14617 else
14618 type = TREE_TYPE (t);
14619 if (VAR_P (t)
14620 && VAR_HAD_UNKNOWN_BOUND (t)
14621 && type != error_mark_node)
14622 type = strip_array_domain (type);
14623 tree sub_args = args;
14624 if (tree auto_node = type_uses_auto (type))
14625 {
14626 /* Mask off any template args past the variable's context so we
14627 don't replace the auto with an unrelated argument. */
14628 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14629 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14630 if (extra > 0)
14631 /* This should never happen with the new lambda instantiation
14632 model, but keep the handling just in case. */
14633 gcc_assert (!CHECKING_P),
14634 sub_args = strip_innermost_template_args (args, extra);
14635 }
14636 type = tsubst (type, sub_args, complain, in_decl);
14637 /* Substituting the type might have recursively instantiated this
14638 same alias (c++/86171). */
14639 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14640 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14641 {
14642 r = spec;
14643 break;
14644 }
14645 }
14646 r = copy_decl (t);
14647 if (VAR_P (r))
14648 {
14649 DECL_INITIALIZED_P (r) = 0;
14650 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14651 if (type == error_mark_node)
14652 RETURN (error_mark_node);
14653 if (TREE_CODE (type) == FUNCTION_TYPE)
14654 {
14655 /* It may seem that this case cannot occur, since:
14656
14657 typedef void f();
14658 void g() { f x; }
14659
14660 declares a function, not a variable. However:
14661
14662 typedef void f();
14663 template <typename T> void g() { T t; }
14664 template void g<f>();
14665
14666 is an attempt to declare a variable with function
14667 type. */
14668 error ("variable %qD has function type",
14669 /* R is not yet sufficiently initialized, so we
14670 just use its name. */
14671 DECL_NAME (r));
14672 RETURN (error_mark_node);
14673 }
14674 type = complete_type (type);
14675 /* Wait until cp_finish_decl to set this again, to handle
14676 circular dependency (template/instantiate6.C). */
14677 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14678 type = check_var_type (DECL_NAME (r), type,
14679 DECL_SOURCE_LOCATION (r));
14680 if (DECL_HAS_VALUE_EXPR_P (t))
14681 {
14682 tree ve = DECL_VALUE_EXPR (t);
14683 /* If the DECL_VALUE_EXPR is converted to the declared type,
14684 preserve the identity so that gimplify_type_sizes works. */
14685 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14686 if (nop)
14687 ve = TREE_OPERAND (ve, 0);
14688 ve = tsubst_expr (ve, args, complain, in_decl,
14689 /*constant_expression_p=*/false);
14690 if (REFERENCE_REF_P (ve))
14691 {
14692 gcc_assert (TYPE_REF_P (type));
14693 ve = TREE_OPERAND (ve, 0);
14694 }
14695 if (nop)
14696 ve = build_nop (type, ve);
14697 else if (DECL_LANG_SPECIFIC (t)
14698 && DECL_OMP_PRIVATIZED_MEMBER (t)
14699 && TREE_CODE (ve) == COMPONENT_REF
14700 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14701 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14702 type = TREE_TYPE (ve);
14703 else
14704 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14705 == TYPE_MAIN_VARIANT (type));
14706 SET_DECL_VALUE_EXPR (r, ve);
14707 }
14708 if (CP_DECL_THREAD_LOCAL_P (r)
14709 && !processing_template_decl)
14710 set_decl_tls_model (r, decl_default_tls_model (r));
14711 }
14712 else if (DECL_SELF_REFERENCE_P (t))
14713 SET_DECL_SELF_REFERENCE_P (r);
14714 TREE_TYPE (r) = type;
14715 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14716 DECL_CONTEXT (r) = ctx;
14717 /* Clear out the mangled name and RTL for the instantiation. */
14718 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14719 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14720 SET_DECL_RTL (r, NULL);
14721 /* The initializer must not be expanded until it is required;
14722 see [temp.inst]. */
14723 DECL_INITIAL (r) = NULL_TREE;
14724 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14725 if (VAR_P (r))
14726 {
14727 if (DECL_LANG_SPECIFIC (r))
14728 SET_DECL_DEPENDENT_INIT_P (r, false);
14729
14730 SET_DECL_MODE (r, VOIDmode);
14731
14732 /* Possibly limit visibility based on template args. */
14733 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14734 if (DECL_VISIBILITY_SPECIFIED (t))
14735 {
14736 DECL_VISIBILITY_SPECIFIED (r) = 0;
14737 DECL_ATTRIBUTES (r)
14738 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14739 }
14740 determine_visibility (r);
14741 }
14742
14743 if (!local_p)
14744 {
14745 /* A static data member declaration is always marked
14746 external when it is declared in-class, even if an
14747 initializer is present. We mimic the non-template
14748 processing here. */
14749 DECL_EXTERNAL (r) = 1;
14750 if (DECL_NAMESPACE_SCOPE_P (t))
14751 DECL_NOT_REALLY_EXTERN (r) = 1;
14752
14753 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14754 SET_DECL_IMPLICIT_INSTANTIATION (r);
14755 if (!error_operand_p (r) || (complain & tf_error))
14756 register_specialization (r, gen_tmpl, argvec, false, hash);
14757 }
14758 else
14759 {
14760 if (DECL_LANG_SPECIFIC (r))
14761 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14762 if (!cp_unevaluated_operand)
14763 register_local_specialization (r, t);
14764 }
14765
14766 DECL_CHAIN (r) = NULL_TREE;
14767
14768 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14769 /*flags=*/0,
14770 args, complain, in_decl);
14771
14772 /* Preserve a typedef that names a type. */
14773 if (is_typedef_decl (r) && type != error_mark_node)
14774 {
14775 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14776 set_underlying_type (r);
14777 if (TYPE_DECL_ALIAS_P (r))
14778 /* An alias template specialization can be dependent
14779 even if its underlying type is not. */
14780 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14781 }
14782
14783 layout_decl (r, 0);
14784 }
14785 break;
14786
14787 default:
14788 gcc_unreachable ();
14789 }
14790 #undef RETURN
14791
14792 out:
14793 /* Restore the file and line information. */
14794 input_location = saved_loc;
14795
14796 return r;
14797 }
14798
14799 /* Substitute into the complete parameter type list PARMS. */
14800
14801 tree
14802 tsubst_function_parms (tree parms,
14803 tree args,
14804 tsubst_flags_t complain,
14805 tree in_decl)
14806 {
14807 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14808 }
14809
14810 /* Substitute into the ARG_TYPES of a function type.
14811 If END is a TREE_CHAIN, leave it and any following types
14812 un-substituted. */
14813
14814 static tree
14815 tsubst_arg_types (tree arg_types,
14816 tree args,
14817 tree end,
14818 tsubst_flags_t complain,
14819 tree in_decl)
14820 {
14821 tree remaining_arg_types;
14822 tree type = NULL_TREE;
14823 int i = 1;
14824 tree expanded_args = NULL_TREE;
14825 tree default_arg;
14826
14827 if (!arg_types || arg_types == void_list_node || arg_types == end)
14828 return arg_types;
14829
14830 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14831 args, end, complain, in_decl);
14832 if (remaining_arg_types == error_mark_node)
14833 return error_mark_node;
14834
14835 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14836 {
14837 /* For a pack expansion, perform substitution on the
14838 entire expression. Later on, we'll handle the arguments
14839 one-by-one. */
14840 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14841 args, complain, in_decl);
14842
14843 if (TREE_CODE (expanded_args) == TREE_VEC)
14844 /* So that we'll spin through the parameters, one by one. */
14845 i = TREE_VEC_LENGTH (expanded_args);
14846 else
14847 {
14848 /* We only partially substituted into the parameter
14849 pack. Our type is TYPE_PACK_EXPANSION. */
14850 type = expanded_args;
14851 expanded_args = NULL_TREE;
14852 }
14853 }
14854
14855 while (i > 0) {
14856 --i;
14857
14858 if (expanded_args)
14859 type = TREE_VEC_ELT (expanded_args, i);
14860 else if (!type)
14861 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14862
14863 if (type == error_mark_node)
14864 return error_mark_node;
14865 if (VOID_TYPE_P (type))
14866 {
14867 if (complain & tf_error)
14868 {
14869 error ("invalid parameter type %qT", type);
14870 if (in_decl)
14871 error ("in declaration %q+D", in_decl);
14872 }
14873 return error_mark_node;
14874 }
14875 /* DR 657. */
14876 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14877 return error_mark_node;
14878
14879 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14880 top-level qualifiers as required. */
14881 type = cv_unqualified (type_decays_to (type));
14882
14883 /* We do not substitute into default arguments here. The standard
14884 mandates that they be instantiated only when needed, which is
14885 done in build_over_call. */
14886 default_arg = TREE_PURPOSE (arg_types);
14887
14888 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14889 since the new op() won't have any associated template arguments for us
14890 to refer to later. */
14891 if (lambda_fn_in_template_p (in_decl))
14892 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14893 false/*fn*/, false/*constexpr*/);
14894
14895 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14896 {
14897 /* We've instantiated a template before its default arguments
14898 have been parsed. This can happen for a nested template
14899 class, and is not an error unless we require the default
14900 argument in a call of this function. */
14901 remaining_arg_types =
14902 tree_cons (default_arg, type, remaining_arg_types);
14903 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14904 remaining_arg_types);
14905 }
14906 else
14907 remaining_arg_types =
14908 hash_tree_cons (default_arg, type, remaining_arg_types);
14909 }
14910
14911 return remaining_arg_types;
14912 }
14913
14914 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14915 *not* handle the exception-specification for FNTYPE, because the
14916 initial substitution of explicitly provided template parameters
14917 during argument deduction forbids substitution into the
14918 exception-specification:
14919
14920 [temp.deduct]
14921
14922 All references in the function type of the function template to the
14923 corresponding template parameters are replaced by the specified tem-
14924 plate argument values. If a substitution in a template parameter or
14925 in the function type of the function template results in an invalid
14926 type, type deduction fails. [Note: The equivalent substitution in
14927 exception specifications is done only when the function is instanti-
14928 ated, at which point a program is ill-formed if the substitution
14929 results in an invalid type.] */
14930
14931 static tree
14932 tsubst_function_type (tree t,
14933 tree args,
14934 tsubst_flags_t complain,
14935 tree in_decl)
14936 {
14937 tree return_type;
14938 tree arg_types = NULL_TREE;
14939
14940 /* The TYPE_CONTEXT is not used for function/method types. */
14941 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14942
14943 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14944 failure. */
14945 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14946
14947 if (late_return_type_p)
14948 {
14949 /* Substitute the argument types. */
14950 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14951 complain, in_decl);
14952 if (arg_types == error_mark_node)
14953 return error_mark_node;
14954
14955 tree save_ccp = current_class_ptr;
14956 tree save_ccr = current_class_ref;
14957 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14958 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14959 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14960 if (do_inject)
14961 {
14962 /* DR 1207: 'this' is in scope in the trailing return type. */
14963 inject_this_parameter (this_type, cp_type_quals (this_type));
14964 }
14965
14966 /* Substitute the return type. */
14967 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14968
14969 if (do_inject)
14970 {
14971 current_class_ptr = save_ccp;
14972 current_class_ref = save_ccr;
14973 }
14974 }
14975 else
14976 /* Substitute the return type. */
14977 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14978
14979 if (return_type == error_mark_node)
14980 return error_mark_node;
14981 /* DR 486 clarifies that creation of a function type with an
14982 invalid return type is a deduction failure. */
14983 if (TREE_CODE (return_type) == ARRAY_TYPE
14984 || TREE_CODE (return_type) == FUNCTION_TYPE)
14985 {
14986 if (complain & tf_error)
14987 {
14988 if (TREE_CODE (return_type) == ARRAY_TYPE)
14989 error ("function returning an array");
14990 else
14991 error ("function returning a function");
14992 }
14993 return error_mark_node;
14994 }
14995 /* And DR 657. */
14996 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14997 return error_mark_node;
14998
14999 if (!late_return_type_p)
15000 {
15001 /* Substitute the argument types. */
15002 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15003 complain, in_decl);
15004 if (arg_types == error_mark_node)
15005 return error_mark_node;
15006 }
15007
15008 /* Construct a new type node and return it. */
15009 return rebuild_function_or_method_type (t, return_type, arg_types,
15010 /*raises=*/NULL_TREE, complain);
15011 }
15012
15013 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15014 ARGS into that specification, and return the substituted
15015 specification. If there is no specification, return NULL_TREE. */
15016
15017 static tree
15018 tsubst_exception_specification (tree fntype,
15019 tree args,
15020 tsubst_flags_t complain,
15021 tree in_decl,
15022 bool defer_ok)
15023 {
15024 tree specs;
15025 tree new_specs;
15026
15027 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15028 new_specs = NULL_TREE;
15029 if (specs && TREE_PURPOSE (specs))
15030 {
15031 /* A noexcept-specifier. */
15032 tree expr = TREE_PURPOSE (specs);
15033 if (TREE_CODE (expr) == INTEGER_CST)
15034 new_specs = expr;
15035 else if (defer_ok)
15036 {
15037 /* Defer instantiation of noexcept-specifiers to avoid
15038 excessive instantiations (c++/49107). */
15039 new_specs = make_node (DEFERRED_NOEXCEPT);
15040 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15041 {
15042 /* We already partially instantiated this member template,
15043 so combine the new args with the old. */
15044 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15045 = DEFERRED_NOEXCEPT_PATTERN (expr);
15046 DEFERRED_NOEXCEPT_ARGS (new_specs)
15047 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15048 }
15049 else
15050 {
15051 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15052 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15053 }
15054 }
15055 else
15056 {
15057 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15058 {
15059 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15060 args);
15061 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15062 }
15063 new_specs = tsubst_copy_and_build
15064 (expr, args, complain, in_decl, /*function_p=*/false,
15065 /*integral_constant_expression_p=*/true);
15066 }
15067 new_specs = build_noexcept_spec (new_specs, complain);
15068 }
15069 else if (specs)
15070 {
15071 if (! TREE_VALUE (specs))
15072 new_specs = specs;
15073 else
15074 while (specs)
15075 {
15076 tree spec;
15077 int i, len = 1;
15078 tree expanded_specs = NULL_TREE;
15079
15080 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15081 {
15082 /* Expand the pack expansion type. */
15083 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15084 args, complain,
15085 in_decl);
15086
15087 if (expanded_specs == error_mark_node)
15088 return error_mark_node;
15089 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15090 len = TREE_VEC_LENGTH (expanded_specs);
15091 else
15092 {
15093 /* We're substituting into a member template, so
15094 we got a TYPE_PACK_EXPANSION back. Add that
15095 expansion and move on. */
15096 gcc_assert (TREE_CODE (expanded_specs)
15097 == TYPE_PACK_EXPANSION);
15098 new_specs = add_exception_specifier (new_specs,
15099 expanded_specs,
15100 complain);
15101 specs = TREE_CHAIN (specs);
15102 continue;
15103 }
15104 }
15105
15106 for (i = 0; i < len; ++i)
15107 {
15108 if (expanded_specs)
15109 spec = TREE_VEC_ELT (expanded_specs, i);
15110 else
15111 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15112 if (spec == error_mark_node)
15113 return spec;
15114 new_specs = add_exception_specifier (new_specs, spec,
15115 complain);
15116 }
15117
15118 specs = TREE_CHAIN (specs);
15119 }
15120 }
15121 return new_specs;
15122 }
15123
15124 /* Substitute through a TREE_LIST of types or expressions, handling pack
15125 expansions. */
15126
15127 tree
15128 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15129 {
15130 if (t == void_list_node)
15131 return t;
15132
15133 tree purpose = TREE_PURPOSE (t);
15134 tree purposevec = NULL_TREE;
15135 if (!purpose)
15136 ;
15137 else if (PACK_EXPANSION_P (purpose))
15138 {
15139 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15140 if (TREE_CODE (purpose) == TREE_VEC)
15141 purposevec = purpose;
15142 }
15143 else if (TYPE_P (purpose))
15144 purpose = tsubst (purpose, args, complain, in_decl);
15145 else
15146 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15147 if (purpose == error_mark_node || purposevec == error_mark_node)
15148 return error_mark_node;
15149
15150 tree value = TREE_VALUE (t);
15151 tree valuevec = NULL_TREE;
15152 if (!value)
15153 ;
15154 else if (PACK_EXPANSION_P (value))
15155 {
15156 value = tsubst_pack_expansion (value, args, complain, in_decl);
15157 if (TREE_CODE (value) == TREE_VEC)
15158 valuevec = value;
15159 }
15160 else if (TYPE_P (value))
15161 value = tsubst (value, args, complain, in_decl);
15162 else
15163 value = tsubst_copy_and_build (value, args, complain, in_decl);
15164 if (value == error_mark_node || valuevec == error_mark_node)
15165 return error_mark_node;
15166
15167 tree chain = TREE_CHAIN (t);
15168 if (!chain)
15169 ;
15170 else if (TREE_CODE (chain) == TREE_LIST)
15171 chain = tsubst_tree_list (chain, args, complain, in_decl);
15172 else if (TYPE_P (chain))
15173 chain = tsubst (chain, args, complain, in_decl);
15174 else
15175 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15176 if (chain == error_mark_node)
15177 return error_mark_node;
15178
15179 if (purpose == TREE_PURPOSE (t)
15180 && value == TREE_VALUE (t)
15181 && chain == TREE_CHAIN (t))
15182 return t;
15183
15184 int len;
15185 /* Determine the number of arguments. */
15186 if (purposevec)
15187 {
15188 len = TREE_VEC_LENGTH (purposevec);
15189 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15190 }
15191 else if (valuevec)
15192 len = TREE_VEC_LENGTH (valuevec);
15193 else
15194 len = 1;
15195
15196 for (int i = len; i-- > 0; )
15197 {
15198 if (purposevec)
15199 purpose = TREE_VEC_ELT (purposevec, i);
15200 if (valuevec)
15201 value = TREE_VEC_ELT (valuevec, i);
15202
15203 if (value && TYPE_P (value))
15204 chain = hash_tree_cons (purpose, value, chain);
15205 else
15206 chain = tree_cons (purpose, value, chain);
15207 }
15208
15209 return chain;
15210 }
15211
15212 /* Take the tree structure T and replace template parameters used
15213 therein with the argument vector ARGS. IN_DECL is an associated
15214 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15215 Issue error and warning messages under control of COMPLAIN. Note
15216 that we must be relatively non-tolerant of extensions here, in
15217 order to preserve conformance; if we allow substitutions that
15218 should not be allowed, we may allow argument deductions that should
15219 not succeed, and therefore report ambiguous overload situations
15220 where there are none. In theory, we could allow the substitution,
15221 but indicate that it should have failed, and allow our caller to
15222 make sure that the right thing happens, but we don't try to do this
15223 yet.
15224
15225 This function is used for dealing with types, decls and the like;
15226 for expressions, use tsubst_expr or tsubst_copy. */
15227
15228 tree
15229 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15230 {
15231 enum tree_code code;
15232 tree type, r = NULL_TREE;
15233
15234 if (t == NULL_TREE || t == error_mark_node
15235 || t == integer_type_node
15236 || t == void_type_node
15237 || t == char_type_node
15238 || t == unknown_type_node
15239 || TREE_CODE (t) == NAMESPACE_DECL
15240 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15241 return t;
15242
15243 if (DECL_P (t))
15244 return tsubst_decl (t, args, complain);
15245
15246 if (args == NULL_TREE)
15247 return t;
15248
15249 code = TREE_CODE (t);
15250
15251 if (code == IDENTIFIER_NODE)
15252 type = IDENTIFIER_TYPE_VALUE (t);
15253 else
15254 type = TREE_TYPE (t);
15255
15256 gcc_assert (type != unknown_type_node);
15257
15258 /* Reuse typedefs. We need to do this to handle dependent attributes,
15259 such as attribute aligned. */
15260 if (TYPE_P (t)
15261 && typedef_variant_p (t))
15262 {
15263 tree decl = TYPE_NAME (t);
15264
15265 if (alias_template_specialization_p (t, nt_opaque))
15266 {
15267 /* DECL represents an alias template and we want to
15268 instantiate it. */
15269 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15270 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15271 r = instantiate_alias_template (tmpl, gen_args, complain);
15272 }
15273 else if (DECL_CLASS_SCOPE_P (decl)
15274 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15275 && uses_template_parms (DECL_CONTEXT (decl)))
15276 {
15277 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15278 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15279 r = retrieve_specialization (tmpl, gen_args, 0);
15280 }
15281 else if (DECL_FUNCTION_SCOPE_P (decl)
15282 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15283 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15284 r = retrieve_local_specialization (decl);
15285 else
15286 /* The typedef is from a non-template context. */
15287 return t;
15288
15289 if (r)
15290 {
15291 r = TREE_TYPE (r);
15292 r = cp_build_qualified_type_real
15293 (r, cp_type_quals (t) | cp_type_quals (r),
15294 complain | tf_ignore_bad_quals);
15295 return r;
15296 }
15297 else
15298 {
15299 /* We don't have an instantiation yet, so drop the typedef. */
15300 int quals = cp_type_quals (t);
15301 t = DECL_ORIGINAL_TYPE (decl);
15302 t = cp_build_qualified_type_real (t, quals,
15303 complain | tf_ignore_bad_quals);
15304 }
15305 }
15306
15307 bool fndecl_type = (complain & tf_fndecl_type);
15308 complain &= ~tf_fndecl_type;
15309
15310 if (type
15311 && code != TYPENAME_TYPE
15312 && code != TEMPLATE_TYPE_PARM
15313 && code != TEMPLATE_PARM_INDEX
15314 && code != IDENTIFIER_NODE
15315 && code != FUNCTION_TYPE
15316 && code != METHOD_TYPE)
15317 type = tsubst (type, args, complain, in_decl);
15318 if (type == error_mark_node)
15319 return error_mark_node;
15320
15321 switch (code)
15322 {
15323 case RECORD_TYPE:
15324 case UNION_TYPE:
15325 case ENUMERAL_TYPE:
15326 return tsubst_aggr_type (t, args, complain, in_decl,
15327 /*entering_scope=*/0);
15328
15329 case ERROR_MARK:
15330 case IDENTIFIER_NODE:
15331 case VOID_TYPE:
15332 case REAL_TYPE:
15333 case COMPLEX_TYPE:
15334 case VECTOR_TYPE:
15335 case BOOLEAN_TYPE:
15336 case NULLPTR_TYPE:
15337 case LANG_TYPE:
15338 return t;
15339
15340 case INTEGER_TYPE:
15341 if (t == integer_type_node)
15342 return t;
15343
15344 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15345 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15346 return t;
15347
15348 {
15349 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15350
15351 max = tsubst_expr (omax, args, complain, in_decl,
15352 /*integral_constant_expression_p=*/false);
15353
15354 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15355 needed. */
15356 if (TREE_CODE (max) == NOP_EXPR
15357 && TREE_SIDE_EFFECTS (omax)
15358 && !TREE_TYPE (max))
15359 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15360
15361 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15362 with TREE_SIDE_EFFECTS that indicates this is not an integral
15363 constant expression. */
15364 if (processing_template_decl
15365 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15366 {
15367 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15368 TREE_SIDE_EFFECTS (max) = 1;
15369 }
15370
15371 return compute_array_index_type (NULL_TREE, max, complain);
15372 }
15373
15374 case TEMPLATE_TYPE_PARM:
15375 case TEMPLATE_TEMPLATE_PARM:
15376 case BOUND_TEMPLATE_TEMPLATE_PARM:
15377 case TEMPLATE_PARM_INDEX:
15378 {
15379 int idx;
15380 int level;
15381 int levels;
15382 tree arg = NULL_TREE;
15383
15384 r = NULL_TREE;
15385
15386 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15387 template_parm_level_and_index (t, &level, &idx);
15388
15389 levels = TMPL_ARGS_DEPTH (args);
15390 if (level <= levels
15391 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15392 {
15393 arg = TMPL_ARG (args, level, idx);
15394
15395 /* See through ARGUMENT_PACK_SELECT arguments. */
15396 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15397 arg = argument_pack_select_arg (arg);
15398 }
15399
15400 if (arg == error_mark_node)
15401 return error_mark_node;
15402 else if (arg != NULL_TREE)
15403 {
15404 if (ARGUMENT_PACK_P (arg))
15405 /* If ARG is an argument pack, we don't actually want to
15406 perform a substitution here, because substitutions
15407 for argument packs are only done
15408 element-by-element. We can get to this point when
15409 substituting the type of a non-type template
15410 parameter pack, when that type actually contains
15411 template parameter packs from an outer template, e.g.,
15412
15413 template<typename... Types> struct A {
15414 template<Types... Values> struct B { };
15415 }; */
15416 return t;
15417
15418 if (code == TEMPLATE_TYPE_PARM)
15419 {
15420 int quals;
15421
15422 /* When building concept checks for the purpose of
15423 deducing placeholders, we can end up with wildcards
15424 where types are expected. Adjust this to the deduced
15425 value. */
15426 if (TREE_CODE (arg) == WILDCARD_DECL)
15427 arg = TREE_TYPE (TREE_TYPE (arg));
15428
15429 gcc_assert (TYPE_P (arg));
15430
15431 quals = cp_type_quals (arg) | cp_type_quals (t);
15432
15433 return cp_build_qualified_type_real
15434 (arg, quals, complain | tf_ignore_bad_quals);
15435 }
15436 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15437 {
15438 /* We are processing a type constructed from a
15439 template template parameter. */
15440 tree argvec = tsubst (TYPE_TI_ARGS (t),
15441 args, complain, in_decl);
15442 if (argvec == error_mark_node)
15443 return error_mark_node;
15444
15445 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15446 || TREE_CODE (arg) == TEMPLATE_DECL
15447 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15448
15449 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15450 /* Consider this code:
15451
15452 template <template <class> class Template>
15453 struct Internal {
15454 template <class Arg> using Bind = Template<Arg>;
15455 };
15456
15457 template <template <class> class Template, class Arg>
15458 using Instantiate = Template<Arg>; //#0
15459
15460 template <template <class> class Template,
15461 class Argument>
15462 using Bind =
15463 Instantiate<Internal<Template>::template Bind,
15464 Argument>; //#1
15465
15466 When #1 is parsed, the
15467 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15468 parameter `Template' in #0 matches the
15469 UNBOUND_CLASS_TEMPLATE representing the argument
15470 `Internal<Template>::template Bind'; We then want
15471 to assemble the type `Bind<Argument>' that can't
15472 be fully created right now, because
15473 `Internal<Template>' not being complete, the Bind
15474 template cannot be looked up in that context. So
15475 we need to "store" `Bind<Argument>' for later
15476 when the context of Bind becomes complete. Let's
15477 store that in a TYPENAME_TYPE. */
15478 return make_typename_type (TYPE_CONTEXT (arg),
15479 build_nt (TEMPLATE_ID_EXPR,
15480 TYPE_IDENTIFIER (arg),
15481 argvec),
15482 typename_type,
15483 complain);
15484
15485 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15486 are resolving nested-types in the signature of a
15487 member function templates. Otherwise ARG is a
15488 TEMPLATE_DECL and is the real template to be
15489 instantiated. */
15490 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15491 arg = TYPE_NAME (arg);
15492
15493 r = lookup_template_class (arg,
15494 argvec, in_decl,
15495 DECL_CONTEXT (arg),
15496 /*entering_scope=*/0,
15497 complain);
15498 return cp_build_qualified_type_real
15499 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15500 }
15501 else if (code == TEMPLATE_TEMPLATE_PARM)
15502 return arg;
15503 else
15504 /* TEMPLATE_PARM_INDEX. */
15505 return convert_from_reference (unshare_expr (arg));
15506 }
15507
15508 if (level == 1)
15509 /* This can happen during the attempted tsubst'ing in
15510 unify. This means that we don't yet have any information
15511 about the template parameter in question. */
15512 return t;
15513
15514 /* Early in template argument deduction substitution, we don't
15515 want to reduce the level of 'auto', or it will be confused
15516 with a normal template parm in subsequent deduction.
15517 Similarly, don't reduce the level of template parameters to
15518 avoid mismatches when deducing their types. */
15519 if (complain & tf_partial)
15520 return t;
15521
15522 /* If we get here, we must have been looking at a parm for a
15523 more deeply nested template. Make a new version of this
15524 template parameter, but with a lower level. */
15525 switch (code)
15526 {
15527 case TEMPLATE_TYPE_PARM:
15528 case TEMPLATE_TEMPLATE_PARM:
15529 case BOUND_TEMPLATE_TEMPLATE_PARM:
15530 if (cp_type_quals (t))
15531 {
15532 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15533 r = cp_build_qualified_type_real
15534 (r, cp_type_quals (t),
15535 complain | (code == TEMPLATE_TYPE_PARM
15536 ? tf_ignore_bad_quals : 0));
15537 }
15538 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15539 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15540 && (r = (TEMPLATE_PARM_DESCENDANTS
15541 (TEMPLATE_TYPE_PARM_INDEX (t))))
15542 && (r = TREE_TYPE (r))
15543 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15544 /* Break infinite recursion when substituting the constraints
15545 of a constrained placeholder. */;
15546 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15547 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15548 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15549 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15550 r = TEMPLATE_PARM_DESCENDANTS (arg))
15551 && (TEMPLATE_PARM_LEVEL (r)
15552 == TEMPLATE_PARM_LEVEL (arg) - levels))
15553 /* Cache the simple case of lowering a type parameter. */
15554 r = TREE_TYPE (r);
15555 else
15556 {
15557 r = copy_type (t);
15558 TEMPLATE_TYPE_PARM_INDEX (r)
15559 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15560 r, levels, args, complain);
15561 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15562 TYPE_MAIN_VARIANT (r) = r;
15563 TYPE_POINTER_TO (r) = NULL_TREE;
15564 TYPE_REFERENCE_TO (r) = NULL_TREE;
15565
15566 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15567 {
15568 /* Propagate constraints on placeholders since they are
15569 only instantiated during satisfaction. */
15570 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15571 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15572 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15573 {
15574 pl = tsubst_copy (pl, args, complain, in_decl);
15575 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15576 }
15577 }
15578
15579 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15580 /* We have reduced the level of the template
15581 template parameter, but not the levels of its
15582 template parameters, so canonical_type_parameter
15583 will not be able to find the canonical template
15584 template parameter for this level. Thus, we
15585 require structural equality checking to compare
15586 TEMPLATE_TEMPLATE_PARMs. */
15587 SET_TYPE_STRUCTURAL_EQUALITY (r);
15588 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15589 SET_TYPE_STRUCTURAL_EQUALITY (r);
15590 else
15591 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15592
15593 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15594 {
15595 tree tinfo = TYPE_TEMPLATE_INFO (t);
15596 /* We might need to substitute into the types of non-type
15597 template parameters. */
15598 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15599 complain, in_decl);
15600 if (tmpl == error_mark_node)
15601 return error_mark_node;
15602 tree argvec = tsubst (TI_ARGS (tinfo), args,
15603 complain, in_decl);
15604 if (argvec == error_mark_node)
15605 return error_mark_node;
15606
15607 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15608 = build_template_info (tmpl, argvec);
15609 }
15610 }
15611 break;
15612
15613 case TEMPLATE_PARM_INDEX:
15614 /* OK, now substitute the type of the non-type parameter. We
15615 couldn't do it earlier because it might be an auto parameter,
15616 and we wouldn't need to if we had an argument. */
15617 type = tsubst (type, args, complain, in_decl);
15618 if (type == error_mark_node)
15619 return error_mark_node;
15620 r = reduce_template_parm_level (t, type, levels, args, complain);
15621 break;
15622
15623 default:
15624 gcc_unreachable ();
15625 }
15626
15627 return r;
15628 }
15629
15630 case TREE_LIST:
15631 return tsubst_tree_list (t, args, complain, in_decl);
15632
15633 case TREE_BINFO:
15634 /* We should never be tsubsting a binfo. */
15635 gcc_unreachable ();
15636
15637 case TREE_VEC:
15638 /* A vector of template arguments. */
15639 gcc_assert (!type);
15640 return tsubst_template_args (t, args, complain, in_decl);
15641
15642 case POINTER_TYPE:
15643 case REFERENCE_TYPE:
15644 {
15645 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15646 return t;
15647
15648 /* [temp.deduct]
15649
15650 Type deduction may fail for any of the following
15651 reasons:
15652
15653 -- Attempting to create a pointer to reference type.
15654 -- Attempting to create a reference to a reference type or
15655 a reference to void.
15656
15657 Core issue 106 says that creating a reference to a reference
15658 during instantiation is no longer a cause for failure. We
15659 only enforce this check in strict C++98 mode. */
15660 if ((TYPE_REF_P (type)
15661 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15662 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15663 {
15664 static location_t last_loc;
15665
15666 /* We keep track of the last time we issued this error
15667 message to avoid spewing a ton of messages during a
15668 single bad template instantiation. */
15669 if (complain & tf_error
15670 && last_loc != input_location)
15671 {
15672 if (VOID_TYPE_P (type))
15673 error ("forming reference to void");
15674 else if (code == POINTER_TYPE)
15675 error ("forming pointer to reference type %qT", type);
15676 else
15677 error ("forming reference to reference type %qT", type);
15678 last_loc = input_location;
15679 }
15680
15681 return error_mark_node;
15682 }
15683 else if (TREE_CODE (type) == FUNCTION_TYPE
15684 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15685 || type_memfn_rqual (type) != REF_QUAL_NONE))
15686 {
15687 if (complain & tf_error)
15688 {
15689 if (code == POINTER_TYPE)
15690 error ("forming pointer to qualified function type %qT",
15691 type);
15692 else
15693 error ("forming reference to qualified function type %qT",
15694 type);
15695 }
15696 return error_mark_node;
15697 }
15698 else if (code == POINTER_TYPE)
15699 {
15700 r = build_pointer_type (type);
15701 if (TREE_CODE (type) == METHOD_TYPE)
15702 r = build_ptrmemfunc_type (r);
15703 }
15704 else if (TYPE_REF_P (type))
15705 /* In C++0x, during template argument substitution, when there is an
15706 attempt to create a reference to a reference type, reference
15707 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15708
15709 "If a template-argument for a template-parameter T names a type
15710 that is a reference to a type A, an attempt to create the type
15711 'lvalue reference to cv T' creates the type 'lvalue reference to
15712 A,' while an attempt to create the type type rvalue reference to
15713 cv T' creates the type T"
15714 */
15715 r = cp_build_reference_type
15716 (TREE_TYPE (type),
15717 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15718 else
15719 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15720 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15721
15722 if (r != error_mark_node)
15723 /* Will this ever be needed for TYPE_..._TO values? */
15724 layout_type (r);
15725
15726 return r;
15727 }
15728 case OFFSET_TYPE:
15729 {
15730 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15731 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15732 {
15733 /* [temp.deduct]
15734
15735 Type deduction may fail for any of the following
15736 reasons:
15737
15738 -- Attempting to create "pointer to member of T" when T
15739 is not a class type. */
15740 if (complain & tf_error)
15741 error ("creating pointer to member of non-class type %qT", r);
15742 return error_mark_node;
15743 }
15744 if (TYPE_REF_P (type))
15745 {
15746 if (complain & tf_error)
15747 error ("creating pointer to member reference type %qT", type);
15748 return error_mark_node;
15749 }
15750 if (VOID_TYPE_P (type))
15751 {
15752 if (complain & tf_error)
15753 error ("creating pointer to member of type void");
15754 return error_mark_node;
15755 }
15756 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15757 if (TREE_CODE (type) == FUNCTION_TYPE)
15758 {
15759 /* The type of the implicit object parameter gets its
15760 cv-qualifiers from the FUNCTION_TYPE. */
15761 tree memptr;
15762 tree method_type
15763 = build_memfn_type (type, r, type_memfn_quals (type),
15764 type_memfn_rqual (type));
15765 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15766 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15767 complain);
15768 }
15769 else
15770 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15771 cp_type_quals (t),
15772 complain);
15773 }
15774 case FUNCTION_TYPE:
15775 case METHOD_TYPE:
15776 {
15777 tree fntype;
15778 tree specs;
15779 fntype = tsubst_function_type (t, args, complain, in_decl);
15780 if (fntype == error_mark_node)
15781 return error_mark_node;
15782
15783 /* Substitute the exception specification. */
15784 specs = tsubst_exception_specification (t, args, complain, in_decl,
15785 /*defer_ok*/fndecl_type);
15786 if (specs == error_mark_node)
15787 return error_mark_node;
15788 if (specs)
15789 fntype = build_exception_variant (fntype, specs);
15790 return fntype;
15791 }
15792 case ARRAY_TYPE:
15793 {
15794 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15795 if (domain == error_mark_node)
15796 return error_mark_node;
15797
15798 /* As an optimization, we avoid regenerating the array type if
15799 it will obviously be the same as T. */
15800 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15801 return t;
15802
15803 /* These checks should match the ones in create_array_type_for_decl.
15804
15805 [temp.deduct]
15806
15807 The deduction may fail for any of the following reasons:
15808
15809 -- Attempting to create an array with an element type that
15810 is void, a function type, or a reference type, or [DR337]
15811 an abstract class type. */
15812 if (VOID_TYPE_P (type)
15813 || TREE_CODE (type) == FUNCTION_TYPE
15814 || (TREE_CODE (type) == ARRAY_TYPE
15815 && TYPE_DOMAIN (type) == NULL_TREE)
15816 || TYPE_REF_P (type))
15817 {
15818 if (complain & tf_error)
15819 error ("creating array of %qT", type);
15820 return error_mark_node;
15821 }
15822
15823 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15824 return error_mark_node;
15825
15826 r = build_cplus_array_type (type, domain);
15827
15828 if (!valid_array_size_p (input_location, r, in_decl,
15829 (complain & tf_error)))
15830 return error_mark_node;
15831
15832 if (TYPE_USER_ALIGN (t))
15833 {
15834 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15835 TYPE_USER_ALIGN (r) = 1;
15836 }
15837
15838 return r;
15839 }
15840
15841 case TYPENAME_TYPE:
15842 {
15843 tree ctx = TYPE_CONTEXT (t);
15844 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15845 {
15846 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15847 if (ctx == error_mark_node
15848 || TREE_VEC_LENGTH (ctx) > 1)
15849 return error_mark_node;
15850 if (TREE_VEC_LENGTH (ctx) == 0)
15851 {
15852 if (complain & tf_error)
15853 error ("%qD is instantiated for an empty pack",
15854 TYPENAME_TYPE_FULLNAME (t));
15855 return error_mark_node;
15856 }
15857 ctx = TREE_VEC_ELT (ctx, 0);
15858 }
15859 else
15860 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15861 /*entering_scope=*/1);
15862 if (ctx == error_mark_node)
15863 return error_mark_node;
15864
15865 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15866 complain, in_decl);
15867 if (f == error_mark_node)
15868 return error_mark_node;
15869
15870 if (!MAYBE_CLASS_TYPE_P (ctx))
15871 {
15872 if (complain & tf_error)
15873 error ("%qT is not a class, struct, or union type", ctx);
15874 return error_mark_node;
15875 }
15876 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15877 {
15878 /* Normally, make_typename_type does not require that the CTX
15879 have complete type in order to allow things like:
15880
15881 template <class T> struct S { typename S<T>::X Y; };
15882
15883 But, such constructs have already been resolved by this
15884 point, so here CTX really should have complete type, unless
15885 it's a partial instantiation. */
15886 ctx = complete_type (ctx);
15887 if (!COMPLETE_TYPE_P (ctx))
15888 {
15889 if (complain & tf_error)
15890 cxx_incomplete_type_error (NULL_TREE, ctx);
15891 return error_mark_node;
15892 }
15893 }
15894
15895 f = make_typename_type (ctx, f, typename_type,
15896 complain | tf_keep_type_decl);
15897 if (f == error_mark_node)
15898 return f;
15899 if (TREE_CODE (f) == TYPE_DECL)
15900 {
15901 complain |= tf_ignore_bad_quals;
15902 f = TREE_TYPE (f);
15903 }
15904
15905 if (TREE_CODE (f) != TYPENAME_TYPE)
15906 {
15907 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15908 {
15909 if (complain & tf_error)
15910 error ("%qT resolves to %qT, which is not an enumeration type",
15911 t, f);
15912 else
15913 return error_mark_node;
15914 }
15915 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15916 {
15917 if (complain & tf_error)
15918 error ("%qT resolves to %qT, which is not a class type",
15919 t, f);
15920 else
15921 return error_mark_node;
15922 }
15923 }
15924
15925 return cp_build_qualified_type_real
15926 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15927 }
15928
15929 case UNBOUND_CLASS_TEMPLATE:
15930 {
15931 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15932 in_decl, /*entering_scope=*/1);
15933 tree name = TYPE_IDENTIFIER (t);
15934 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15935
15936 if (ctx == error_mark_node || name == error_mark_node)
15937 return error_mark_node;
15938
15939 if (parm_list)
15940 parm_list = tsubst_template_parms (parm_list, args, complain);
15941 return make_unbound_class_template (ctx, name, parm_list, complain);
15942 }
15943
15944 case TYPEOF_TYPE:
15945 {
15946 tree type;
15947
15948 ++cp_unevaluated_operand;
15949 ++c_inhibit_evaluation_warnings;
15950
15951 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15952 complain, in_decl,
15953 /*integral_constant_expression_p=*/false);
15954
15955 --cp_unevaluated_operand;
15956 --c_inhibit_evaluation_warnings;
15957
15958 type = finish_typeof (type);
15959 return cp_build_qualified_type_real (type,
15960 cp_type_quals (t)
15961 | cp_type_quals (type),
15962 complain);
15963 }
15964
15965 case DECLTYPE_TYPE:
15966 {
15967 tree type;
15968
15969 ++cp_unevaluated_operand;
15970 ++c_inhibit_evaluation_warnings;
15971
15972 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15973 complain|tf_decltype, in_decl,
15974 /*function_p*/false,
15975 /*integral_constant_expression*/false);
15976
15977 --cp_unevaluated_operand;
15978 --c_inhibit_evaluation_warnings;
15979
15980 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15981 type = lambda_capture_field_type (type,
15982 false /*explicit_init*/,
15983 DECLTYPE_FOR_REF_CAPTURE (t));
15984 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15985 type = lambda_proxy_type (type);
15986 else
15987 {
15988 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15989 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15990 && EXPR_P (type))
15991 /* In a template ~id could be either a complement expression
15992 or an unqualified-id naming a destructor; if instantiating
15993 it produces an expression, it's not an id-expression or
15994 member access. */
15995 id = false;
15996 type = finish_decltype_type (type, id, complain);
15997 }
15998 return cp_build_qualified_type_real (type,
15999 cp_type_quals (t)
16000 | cp_type_quals (type),
16001 complain | tf_ignore_bad_quals);
16002 }
16003
16004 case UNDERLYING_TYPE:
16005 {
16006 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16007 complain, in_decl);
16008 return finish_underlying_type (type);
16009 }
16010
16011 case TYPE_ARGUMENT_PACK:
16012 case NONTYPE_ARGUMENT_PACK:
16013 {
16014 tree r;
16015
16016 if (code == NONTYPE_ARGUMENT_PACK)
16017 r = make_node (code);
16018 else
16019 r = cxx_make_type (code);
16020
16021 tree pack_args = ARGUMENT_PACK_ARGS (t);
16022 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
16023 SET_ARGUMENT_PACK_ARGS (r, pack_args);
16024
16025 return r;
16026 }
16027
16028 case VOID_CST:
16029 case INTEGER_CST:
16030 case REAL_CST:
16031 case STRING_CST:
16032 case PLUS_EXPR:
16033 case MINUS_EXPR:
16034 case NEGATE_EXPR:
16035 case NOP_EXPR:
16036 case INDIRECT_REF:
16037 case ADDR_EXPR:
16038 case CALL_EXPR:
16039 case ARRAY_REF:
16040 case SCOPE_REF:
16041 /* We should use one of the expression tsubsts for these codes. */
16042 gcc_unreachable ();
16043
16044 default:
16045 sorry ("use of %qs in template", get_tree_code_name (code));
16046 return error_mark_node;
16047 }
16048 }
16049
16050 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16051 expression on the left-hand side of the "." or "->" operator. We
16052 only do the lookup if we had a dependent BASELINK. Otherwise we
16053 adjust it onto the instantiated heirarchy. */
16054
16055 static tree
16056 tsubst_baselink (tree baselink, tree object_type,
16057 tree args, tsubst_flags_t complain, tree in_decl)
16058 {
16059 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16060 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16061 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16062
16063 tree optype = BASELINK_OPTYPE (baselink);
16064 optype = tsubst (optype, args, complain, in_decl);
16065
16066 tree template_args = NULL_TREE;
16067 bool template_id_p = false;
16068 tree fns = BASELINK_FUNCTIONS (baselink);
16069 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16070 {
16071 template_id_p = true;
16072 template_args = TREE_OPERAND (fns, 1);
16073 fns = TREE_OPERAND (fns, 0);
16074 if (template_args)
16075 template_args = tsubst_template_args (template_args, args,
16076 complain, in_decl);
16077 }
16078
16079 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16080 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16081 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16082
16083 if (dependent_p)
16084 {
16085 tree name = OVL_NAME (fns);
16086 if (IDENTIFIER_CONV_OP_P (name))
16087 name = make_conv_op_name (optype);
16088
16089 if (name == complete_dtor_identifier)
16090 /* Treat as-if non-dependent below. */
16091 dependent_p = false;
16092
16093 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16094 complain);
16095 if (!baselink)
16096 {
16097 if ((complain & tf_error)
16098 && constructor_name_p (name, qualifying_scope))
16099 error ("cannot call constructor %<%T::%D%> directly",
16100 qualifying_scope, name);
16101 return error_mark_node;
16102 }
16103
16104 if (BASELINK_P (baselink))
16105 fns = BASELINK_FUNCTIONS (baselink);
16106 }
16107 else
16108 /* We're going to overwrite pieces below, make a duplicate. */
16109 baselink = copy_node (baselink);
16110
16111 /* If lookup found a single function, mark it as used at this point.
16112 (If lookup found multiple functions the one selected later by
16113 overload resolution will be marked as used at that point.) */
16114 if (!template_id_p && !really_overloaded_fn (fns))
16115 {
16116 tree fn = OVL_FIRST (fns);
16117 bool ok = mark_used (fn, complain);
16118 if (!ok && !(complain & tf_error))
16119 return error_mark_node;
16120 if (ok && BASELINK_P (baselink))
16121 /* We might have instantiated an auto function. */
16122 TREE_TYPE (baselink) = TREE_TYPE (fn);
16123 }
16124
16125 if (BASELINK_P (baselink))
16126 {
16127 /* Add back the template arguments, if present. */
16128 if (template_id_p)
16129 BASELINK_FUNCTIONS (baselink)
16130 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16131
16132 /* Update the conversion operator type. */
16133 BASELINK_OPTYPE (baselink) = optype;
16134 }
16135
16136 if (!object_type)
16137 object_type = current_class_type;
16138
16139 if (qualified_p || !dependent_p)
16140 {
16141 baselink = adjust_result_of_qualified_name_lookup (baselink,
16142 qualifying_scope,
16143 object_type);
16144 if (!qualified_p)
16145 /* We need to call adjust_result_of_qualified_name_lookup in case the
16146 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16147 so that we still get virtual function binding. */
16148 BASELINK_QUALIFIED_P (baselink) = false;
16149 }
16150
16151 return baselink;
16152 }
16153
16154 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16155 true if the qualified-id will be a postfix-expression in-and-of
16156 itself; false if more of the postfix-expression follows the
16157 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16158 of "&". */
16159
16160 static tree
16161 tsubst_qualified_id (tree qualified_id, tree args,
16162 tsubst_flags_t complain, tree in_decl,
16163 bool done, bool address_p)
16164 {
16165 tree expr;
16166 tree scope;
16167 tree name;
16168 bool is_template;
16169 tree template_args;
16170 location_t loc = UNKNOWN_LOCATION;
16171
16172 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16173
16174 /* Figure out what name to look up. */
16175 name = TREE_OPERAND (qualified_id, 1);
16176 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16177 {
16178 is_template = true;
16179 loc = EXPR_LOCATION (name);
16180 template_args = TREE_OPERAND (name, 1);
16181 if (template_args)
16182 template_args = tsubst_template_args (template_args, args,
16183 complain, in_decl);
16184 if (template_args == error_mark_node)
16185 return error_mark_node;
16186 name = TREE_OPERAND (name, 0);
16187 }
16188 else
16189 {
16190 is_template = false;
16191 template_args = NULL_TREE;
16192 }
16193
16194 /* Substitute into the qualifying scope. When there are no ARGS, we
16195 are just trying to simplify a non-dependent expression. In that
16196 case the qualifying scope may be dependent, and, in any case,
16197 substituting will not help. */
16198 scope = TREE_OPERAND (qualified_id, 0);
16199 if (args)
16200 {
16201 scope = tsubst (scope, args, complain, in_decl);
16202 expr = tsubst_copy (name, args, complain, in_decl);
16203 }
16204 else
16205 expr = name;
16206
16207 if (dependent_scope_p (scope))
16208 {
16209 if (is_template)
16210 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16211 tree r = build_qualified_name (NULL_TREE, scope, expr,
16212 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16213 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16214 return r;
16215 }
16216
16217 if (!BASELINK_P (name) && !DECL_P (expr))
16218 {
16219 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16220 {
16221 /* A BIT_NOT_EXPR is used to represent a destructor. */
16222 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16223 {
16224 error ("qualifying type %qT does not match destructor name ~%qT",
16225 scope, TREE_OPERAND (expr, 0));
16226 expr = error_mark_node;
16227 }
16228 else
16229 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16230 LOOK_want::NORMAL, false);
16231 }
16232 else
16233 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16234 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16235 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16236 {
16237 if (complain & tf_error)
16238 {
16239 error ("dependent-name %qE is parsed as a non-type, but "
16240 "instantiation yields a type", qualified_id);
16241 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16242 }
16243 return error_mark_node;
16244 }
16245 }
16246
16247 if (DECL_P (expr))
16248 {
16249 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16250 scope, complain))
16251 return error_mark_node;
16252 /* Remember that there was a reference to this entity. */
16253 if (!mark_used (expr, complain) && !(complain & tf_error))
16254 return error_mark_node;
16255 }
16256
16257 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16258 {
16259 if (complain & tf_error)
16260 qualified_name_lookup_error (scope,
16261 TREE_OPERAND (qualified_id, 1),
16262 expr, input_location);
16263 return error_mark_node;
16264 }
16265
16266 if (is_template)
16267 {
16268 /* We may be repeating a check already done during parsing, but
16269 if it was well-formed and passed then, it will pass again
16270 now, and if it didn't, we wouldn't have got here. The case
16271 we want to catch is when we couldn't tell then, and can now,
16272 namely when templ prior to substitution was an
16273 identifier. */
16274 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16275 return error_mark_node;
16276
16277 if (variable_template_p (expr))
16278 expr = lookup_and_finish_template_variable (expr, template_args,
16279 complain);
16280 else
16281 expr = lookup_template_function (expr, template_args);
16282 }
16283
16284 if (expr == error_mark_node && complain & tf_error)
16285 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16286 expr, input_location);
16287 else if (TYPE_P (scope))
16288 {
16289 expr = (adjust_result_of_qualified_name_lookup
16290 (expr, scope, current_nonlambda_class_type ()));
16291 expr = (finish_qualified_id_expr
16292 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16293 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16294 /*template_arg_p=*/false, complain));
16295 }
16296
16297 /* Expressions do not generally have reference type. */
16298 if (TREE_CODE (expr) != SCOPE_REF
16299 /* However, if we're about to form a pointer-to-member, we just
16300 want the referenced member referenced. */
16301 && TREE_CODE (expr) != OFFSET_REF)
16302 expr = convert_from_reference (expr);
16303
16304 if (REF_PARENTHESIZED_P (qualified_id))
16305 expr = force_paren_expr (expr);
16306
16307 return expr;
16308 }
16309
16310 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16311 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16312 for tsubst. */
16313
16314 static tree
16315 tsubst_init (tree init, tree decl, tree args,
16316 tsubst_flags_t complain, tree in_decl)
16317 {
16318 if (!init)
16319 return NULL_TREE;
16320
16321 init = tsubst_expr (init, args, complain, in_decl, false);
16322
16323 tree type = TREE_TYPE (decl);
16324
16325 if (!init && type != error_mark_node)
16326 {
16327 if (tree auto_node = type_uses_auto (type))
16328 {
16329 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16330 {
16331 if (complain & tf_error)
16332 error ("initializer for %q#D expands to an empty list "
16333 "of expressions", decl);
16334 return error_mark_node;
16335 }
16336 }
16337 else if (!dependent_type_p (type))
16338 {
16339 /* If we had an initializer but it
16340 instantiated to nothing,
16341 value-initialize the object. This will
16342 only occur when the initializer was a
16343 pack expansion where the parameter packs
16344 used in that expansion were of length
16345 zero. */
16346 init = build_value_init (type, complain);
16347 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16348 init = get_target_expr_sfinae (init, complain);
16349 if (TREE_CODE (init) == TARGET_EXPR)
16350 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16351 }
16352 }
16353
16354 return init;
16355 }
16356
16357 /* If T is a reference to a dependent member of the current instantiation C and
16358 we are trying to refer to that member in a partial instantiation of C,
16359 return a SCOPE_REF; otherwise, return NULL_TREE.
16360
16361 This can happen when forming a C++17 deduction guide, as in PR96199. */
16362
16363 static tree
16364 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16365 tree in_decl)
16366 {
16367 if (cxx_dialect < cxx17)
16368 return NULL_TREE;
16369
16370 tree ctx = context_for_name_lookup (t);
16371 if (!CLASS_TYPE_P (ctx))
16372 return NULL_TREE;
16373
16374 ctx = tsubst (ctx, args, complain, in_decl);
16375 if (dependent_scope_p (ctx))
16376 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16377 /*template_p=*/false);
16378
16379 return NULL_TREE;
16380 }
16381
16382 /* Like tsubst, but deals with expressions. This function just replaces
16383 template parms; to finish processing the resultant expression, use
16384 tsubst_copy_and_build or tsubst_expr. */
16385
16386 static tree
16387 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16388 {
16389 enum tree_code code;
16390 tree r;
16391
16392 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16393 return t;
16394
16395 code = TREE_CODE (t);
16396
16397 switch (code)
16398 {
16399 case PARM_DECL:
16400 r = retrieve_local_specialization (t);
16401
16402 if (r == NULL_TREE)
16403 {
16404 /* We get here for a use of 'this' in an NSDMI. */
16405 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16406 return current_class_ptr;
16407
16408 /* This can happen for a parameter name used later in a function
16409 declaration (such as in a late-specified return type). Just
16410 make a dummy decl, since it's only used for its type. */
16411 gcc_assert (cp_unevaluated_operand != 0);
16412 r = tsubst_decl (t, args, complain);
16413 /* Give it the template pattern as its context; its true context
16414 hasn't been instantiated yet and this is good enough for
16415 mangling. */
16416 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16417 }
16418
16419 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16420 r = argument_pack_select_arg (r);
16421 if (!mark_used (r, complain) && !(complain & tf_error))
16422 return error_mark_node;
16423 return r;
16424
16425 case CONST_DECL:
16426 {
16427 tree enum_type;
16428 tree v;
16429
16430 if (DECL_TEMPLATE_PARM_P (t))
16431 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16432 /* There is no need to substitute into namespace-scope
16433 enumerators. */
16434 if (DECL_NAMESPACE_SCOPE_P (t))
16435 return t;
16436 /* If ARGS is NULL, then T is known to be non-dependent. */
16437 if (args == NULL_TREE)
16438 return scalar_constant_value (t);
16439
16440 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16441 return ref;
16442
16443 /* Unfortunately, we cannot just call lookup_name here.
16444 Consider:
16445
16446 template <int I> int f() {
16447 enum E { a = I };
16448 struct S { void g() { E e = a; } };
16449 };
16450
16451 When we instantiate f<7>::S::g(), say, lookup_name is not
16452 clever enough to find f<7>::a. */
16453 enum_type
16454 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16455 /*entering_scope=*/0);
16456
16457 for (v = TYPE_VALUES (enum_type);
16458 v != NULL_TREE;
16459 v = TREE_CHAIN (v))
16460 if (TREE_PURPOSE (v) == DECL_NAME (t))
16461 return TREE_VALUE (v);
16462
16463 /* We didn't find the name. That should never happen; if
16464 name-lookup found it during preliminary parsing, we
16465 should find it again here during instantiation. */
16466 gcc_unreachable ();
16467 }
16468 return t;
16469
16470 case FIELD_DECL:
16471 if (DECL_CONTEXT (t))
16472 {
16473 tree ctx;
16474
16475 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16476 /*entering_scope=*/1);
16477 if (ctx != DECL_CONTEXT (t))
16478 {
16479 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16480 if (!r)
16481 {
16482 if (complain & tf_error)
16483 error ("using invalid field %qD", t);
16484 return error_mark_node;
16485 }
16486 return r;
16487 }
16488 }
16489
16490 return t;
16491
16492 case VAR_DECL:
16493 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16494 return ref;
16495 gcc_fallthrough();
16496 case FUNCTION_DECL:
16497 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16498 r = tsubst (t, args, complain, in_decl);
16499 else if (local_variable_p (t)
16500 && uses_template_parms (DECL_CONTEXT (t)))
16501 {
16502 r = retrieve_local_specialization (t);
16503 if (r == NULL_TREE)
16504 {
16505 /* First try name lookup to find the instantiation. */
16506 r = lookup_name (DECL_NAME (t));
16507 if (r)
16508 {
16509 if (!VAR_P (r))
16510 {
16511 /* During error-recovery we may find a non-variable,
16512 even an OVERLOAD: just bail out and avoid ICEs and
16513 duplicate diagnostics (c++/62207). */
16514 gcc_assert (seen_error ());
16515 return error_mark_node;
16516 }
16517 if (!is_capture_proxy (r))
16518 {
16519 /* Make sure the one we found is the one we want. */
16520 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16521 if (ctx != DECL_CONTEXT (r))
16522 r = NULL_TREE;
16523 }
16524 }
16525
16526 if (r)
16527 /* OK */;
16528 else
16529 {
16530 /* This can happen for a variable used in a
16531 late-specified return type of a local lambda, or for a
16532 local static or constant. Building a new VAR_DECL
16533 should be OK in all those cases. */
16534 r = tsubst_decl (t, args, complain);
16535 if (local_specializations)
16536 /* Avoid infinite recursion (79640). */
16537 register_local_specialization (r, t);
16538 if (decl_maybe_constant_var_p (r))
16539 {
16540 /* We can't call cp_finish_decl, so handle the
16541 initializer by hand. */
16542 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16543 complain, in_decl);
16544 if (!processing_template_decl)
16545 init = maybe_constant_init (init);
16546 if (processing_template_decl
16547 ? potential_constant_expression (init)
16548 : reduced_constant_expression_p (init))
16549 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16550 = TREE_CONSTANT (r) = true;
16551 DECL_INITIAL (r) = init;
16552 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16553 TREE_TYPE (r)
16554 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16555 complain, adc_variable_type);
16556 }
16557 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16558 || decl_constant_var_p (r)
16559 || seen_error ());
16560 if (!processing_template_decl
16561 && !TREE_STATIC (r))
16562 r = process_outer_var_ref (r, complain);
16563 }
16564 /* Remember this for subsequent uses. */
16565 if (local_specializations)
16566 register_local_specialization (r, t);
16567 }
16568 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16569 r = argument_pack_select_arg (r);
16570 }
16571 else
16572 r = t;
16573 if (!mark_used (r, complain))
16574 return error_mark_node;
16575 return r;
16576
16577 case NAMESPACE_DECL:
16578 return t;
16579
16580 case OVERLOAD:
16581 return t;
16582
16583 case BASELINK:
16584 return tsubst_baselink (t, current_nonlambda_class_type (),
16585 args, complain, in_decl);
16586
16587 case TEMPLATE_DECL:
16588 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16589 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16590 args, complain, in_decl);
16591 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16592 return tsubst (t, args, complain, in_decl);
16593 else if (DECL_CLASS_SCOPE_P (t)
16594 && uses_template_parms (DECL_CONTEXT (t)))
16595 {
16596 /* Template template argument like the following example need
16597 special treatment:
16598
16599 template <template <class> class TT> struct C {};
16600 template <class T> struct D {
16601 template <class U> struct E {};
16602 C<E> c; // #1
16603 };
16604 D<int> d; // #2
16605
16606 We are processing the template argument `E' in #1 for
16607 the template instantiation #2. Originally, `E' is a
16608 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16609 have to substitute this with one having context `D<int>'. */
16610
16611 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16612 if (dependent_scope_p (context))
16613 {
16614 /* When rewriting a constructor into a deduction guide, a
16615 non-dependent name can become dependent, so memtmpl<args>
16616 becomes context::template memtmpl<args>. */
16617 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16618 return build_qualified_name (type, context, DECL_NAME (t),
16619 /*template*/true);
16620 }
16621 return lookup_field (context, DECL_NAME(t), 0, false);
16622 }
16623 else
16624 /* Ordinary template template argument. */
16625 return t;
16626
16627 case NON_LVALUE_EXPR:
16628 case VIEW_CONVERT_EXPR:
16629 {
16630 /* Handle location wrappers by substituting the wrapped node
16631 first, *then* reusing the resulting type. Doing the type
16632 first ensures that we handle template parameters and
16633 parameter pack expansions. */
16634 if (location_wrapper_p (t))
16635 {
16636 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16637 complain, in_decl);
16638 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16639 }
16640 tree op = TREE_OPERAND (t, 0);
16641 if (code == VIEW_CONVERT_EXPR
16642 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16643 {
16644 /* Wrapper to make a C++20 template parameter object const. */
16645 op = tsubst_copy (op, args, complain, in_decl);
16646 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16647 {
16648 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16649 return build1 (code, type, op);
16650 }
16651 else
16652 {
16653 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16654 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16655 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16656 return op;
16657 }
16658 }
16659 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16660 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16661 {
16662 op = tsubst_copy (op, args, complain, in_decl);
16663 op = build1 (code, TREE_TYPE (op), op);
16664 REF_PARENTHESIZED_P (op) = true;
16665 return op;
16666 }
16667 /* We shouldn't see any other uses of these in templates. */
16668 gcc_unreachable ();
16669 }
16670
16671 case CAST_EXPR:
16672 case REINTERPRET_CAST_EXPR:
16673 case CONST_CAST_EXPR:
16674 case STATIC_CAST_EXPR:
16675 case DYNAMIC_CAST_EXPR:
16676 case IMPLICIT_CONV_EXPR:
16677 case CONVERT_EXPR:
16678 case NOP_EXPR:
16679 {
16680 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16681 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16682 return build1 (code, type, op0);
16683 }
16684
16685 case SIZEOF_EXPR:
16686 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16687 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16688 {
16689 tree expanded, op = TREE_OPERAND (t, 0);
16690 int len = 0;
16691
16692 if (SIZEOF_EXPR_TYPE_P (t))
16693 op = TREE_TYPE (op);
16694
16695 ++cp_unevaluated_operand;
16696 ++c_inhibit_evaluation_warnings;
16697 /* We only want to compute the number of arguments. */
16698 if (PACK_EXPANSION_P (op))
16699 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16700 else
16701 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16702 args, complain, in_decl);
16703 --cp_unevaluated_operand;
16704 --c_inhibit_evaluation_warnings;
16705
16706 if (TREE_CODE (expanded) == TREE_VEC)
16707 {
16708 len = TREE_VEC_LENGTH (expanded);
16709 /* Set TREE_USED for the benefit of -Wunused. */
16710 for (int i = 0; i < len; i++)
16711 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16712 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16713 }
16714
16715 if (expanded == error_mark_node)
16716 return error_mark_node;
16717 else if (PACK_EXPANSION_P (expanded)
16718 || (TREE_CODE (expanded) == TREE_VEC
16719 && pack_expansion_args_count (expanded)))
16720
16721 {
16722 if (PACK_EXPANSION_P (expanded))
16723 /* OK. */;
16724 else if (TREE_VEC_LENGTH (expanded) == 1)
16725 expanded = TREE_VEC_ELT (expanded, 0);
16726 else
16727 expanded = make_argument_pack (expanded);
16728
16729 if (TYPE_P (expanded))
16730 return cxx_sizeof_or_alignof_type (input_location,
16731 expanded, SIZEOF_EXPR,
16732 false,
16733 complain & tf_error);
16734 else
16735 return cxx_sizeof_or_alignof_expr (input_location,
16736 expanded, SIZEOF_EXPR,
16737 complain & tf_error);
16738 }
16739 else
16740 return build_int_cst (size_type_node, len);
16741 }
16742 if (SIZEOF_EXPR_TYPE_P (t))
16743 {
16744 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16745 args, complain, in_decl);
16746 r = build1 (NOP_EXPR, r, error_mark_node);
16747 r = build1 (SIZEOF_EXPR,
16748 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16749 SIZEOF_EXPR_TYPE_P (r) = 1;
16750 return r;
16751 }
16752 /* Fall through */
16753
16754 case INDIRECT_REF:
16755 case NEGATE_EXPR:
16756 case TRUTH_NOT_EXPR:
16757 case BIT_NOT_EXPR:
16758 case ADDR_EXPR:
16759 case UNARY_PLUS_EXPR: /* Unary + */
16760 case ALIGNOF_EXPR:
16761 case AT_ENCODE_EXPR:
16762 case ARROW_EXPR:
16763 case THROW_EXPR:
16764 case TYPEID_EXPR:
16765 case REALPART_EXPR:
16766 case IMAGPART_EXPR:
16767 case PAREN_EXPR:
16768 {
16769 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16770 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16771 r = build1 (code, type, op0);
16772 if (code == ALIGNOF_EXPR)
16773 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16774 return r;
16775 }
16776
16777 case COMPONENT_REF:
16778 {
16779 tree object;
16780 tree name;
16781
16782 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16783 name = TREE_OPERAND (t, 1);
16784 if (TREE_CODE (name) == BIT_NOT_EXPR)
16785 {
16786 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16787 complain, in_decl);
16788 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16789 }
16790 else if (TREE_CODE (name) == SCOPE_REF
16791 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16792 {
16793 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16794 complain, in_decl);
16795 name = TREE_OPERAND (name, 1);
16796 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16797 complain, in_decl);
16798 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16799 name = build_qualified_name (/*type=*/NULL_TREE,
16800 base, name,
16801 /*template_p=*/false);
16802 }
16803 else if (BASELINK_P (name))
16804 name = tsubst_baselink (name,
16805 non_reference (TREE_TYPE (object)),
16806 args, complain,
16807 in_decl);
16808 else
16809 name = tsubst_copy (name, args, complain, in_decl);
16810 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16811 }
16812
16813 case PLUS_EXPR:
16814 case MINUS_EXPR:
16815 case MULT_EXPR:
16816 case TRUNC_DIV_EXPR:
16817 case CEIL_DIV_EXPR:
16818 case FLOOR_DIV_EXPR:
16819 case ROUND_DIV_EXPR:
16820 case EXACT_DIV_EXPR:
16821 case BIT_AND_EXPR:
16822 case BIT_IOR_EXPR:
16823 case BIT_XOR_EXPR:
16824 case TRUNC_MOD_EXPR:
16825 case FLOOR_MOD_EXPR:
16826 case TRUTH_ANDIF_EXPR:
16827 case TRUTH_ORIF_EXPR:
16828 case TRUTH_AND_EXPR:
16829 case TRUTH_OR_EXPR:
16830 case RSHIFT_EXPR:
16831 case LSHIFT_EXPR:
16832 case EQ_EXPR:
16833 case NE_EXPR:
16834 case MAX_EXPR:
16835 case MIN_EXPR:
16836 case LE_EXPR:
16837 case GE_EXPR:
16838 case LT_EXPR:
16839 case GT_EXPR:
16840 case COMPOUND_EXPR:
16841 case DOTSTAR_EXPR:
16842 case MEMBER_REF:
16843 case PREDECREMENT_EXPR:
16844 case PREINCREMENT_EXPR:
16845 case POSTDECREMENT_EXPR:
16846 case POSTINCREMENT_EXPR:
16847 {
16848 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16849 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16850 return build_nt (code, op0, op1);
16851 }
16852
16853 case SCOPE_REF:
16854 {
16855 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16856 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16857 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16858 QUALIFIED_NAME_IS_TEMPLATE (t));
16859 }
16860
16861 case ARRAY_REF:
16862 {
16863 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16864 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16865 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16866 }
16867
16868 case CALL_EXPR:
16869 {
16870 int n = VL_EXP_OPERAND_LENGTH (t);
16871 tree result = build_vl_exp (CALL_EXPR, n);
16872 int i;
16873 for (i = 0; i < n; i++)
16874 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16875 complain, in_decl);
16876 return result;
16877 }
16878
16879 case COND_EXPR:
16880 case MODOP_EXPR:
16881 case PSEUDO_DTOR_EXPR:
16882 case VEC_PERM_EXPR:
16883 {
16884 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16885 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16886 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16887 r = build_nt (code, op0, op1, op2);
16888 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16889 return r;
16890 }
16891
16892 case NEW_EXPR:
16893 {
16894 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16895 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16896 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16897 r = build_nt (code, op0, op1, op2);
16898 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16899 return r;
16900 }
16901
16902 case DELETE_EXPR:
16903 {
16904 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16905 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16906 r = build_nt (code, op0, op1);
16907 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16908 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16909 return r;
16910 }
16911
16912 case TEMPLATE_ID_EXPR:
16913 {
16914 /* Substituted template arguments */
16915 tree fn = TREE_OPERAND (t, 0);
16916 tree targs = TREE_OPERAND (t, 1);
16917
16918 fn = tsubst_copy (fn, args, complain, in_decl);
16919 if (targs)
16920 targs = tsubst_template_args (targs, args, complain, in_decl);
16921
16922 return lookup_template_function (fn, targs);
16923 }
16924
16925 case TREE_LIST:
16926 {
16927 tree purpose, value, chain;
16928
16929 if (t == void_list_node)
16930 return t;
16931
16932 purpose = TREE_PURPOSE (t);
16933 if (purpose)
16934 purpose = tsubst_copy (purpose, args, complain, in_decl);
16935 value = TREE_VALUE (t);
16936 if (value)
16937 value = tsubst_copy (value, args, complain, in_decl);
16938 chain = TREE_CHAIN (t);
16939 if (chain && chain != void_type_node)
16940 chain = tsubst_copy (chain, args, complain, in_decl);
16941 if (purpose == TREE_PURPOSE (t)
16942 && value == TREE_VALUE (t)
16943 && chain == TREE_CHAIN (t))
16944 return t;
16945 return tree_cons (purpose, value, chain);
16946 }
16947
16948 case RECORD_TYPE:
16949 case UNION_TYPE:
16950 case ENUMERAL_TYPE:
16951 case INTEGER_TYPE:
16952 case TEMPLATE_TYPE_PARM:
16953 case TEMPLATE_TEMPLATE_PARM:
16954 case BOUND_TEMPLATE_TEMPLATE_PARM:
16955 case TEMPLATE_PARM_INDEX:
16956 case POINTER_TYPE:
16957 case REFERENCE_TYPE:
16958 case OFFSET_TYPE:
16959 case FUNCTION_TYPE:
16960 case METHOD_TYPE:
16961 case ARRAY_TYPE:
16962 case TYPENAME_TYPE:
16963 case UNBOUND_CLASS_TEMPLATE:
16964 case TYPEOF_TYPE:
16965 case DECLTYPE_TYPE:
16966 case TYPE_DECL:
16967 return tsubst (t, args, complain, in_decl);
16968
16969 case USING_DECL:
16970 t = DECL_NAME (t);
16971 /* Fall through. */
16972 case IDENTIFIER_NODE:
16973 if (IDENTIFIER_CONV_OP_P (t))
16974 {
16975 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16976 return make_conv_op_name (new_type);
16977 }
16978 else
16979 return t;
16980
16981 case CONSTRUCTOR:
16982 /* This is handled by tsubst_copy_and_build. */
16983 gcc_unreachable ();
16984
16985 case VA_ARG_EXPR:
16986 {
16987 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16988 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16989 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16990 }
16991
16992 case CLEANUP_POINT_EXPR:
16993 /* We shouldn't have built any of these during initial template
16994 generation. Instead, they should be built during instantiation
16995 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16996 gcc_unreachable ();
16997
16998 case OFFSET_REF:
16999 {
17000 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17001 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17002 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17003 r = build2 (code, type, op0, op1);
17004 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17005 if (!mark_used (TREE_OPERAND (r, 1), complain)
17006 && !(complain & tf_error))
17007 return error_mark_node;
17008 return r;
17009 }
17010
17011 case EXPR_PACK_EXPANSION:
17012 error ("invalid use of pack expansion expression");
17013 return error_mark_node;
17014
17015 case NONTYPE_ARGUMENT_PACK:
17016 error ("use %<...%> to expand argument pack");
17017 return error_mark_node;
17018
17019 case VOID_CST:
17020 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17021 return t;
17022
17023 case INTEGER_CST:
17024 case REAL_CST:
17025 case COMPLEX_CST:
17026 {
17027 /* Instantiate any typedefs in the type. */
17028 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17029 r = fold_convert (type, t);
17030 gcc_assert (TREE_CODE (r) == code);
17031 return r;
17032 }
17033
17034 case STRING_CST:
17035 {
17036 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17037 r = t;
17038 if (type != TREE_TYPE (t))
17039 {
17040 r = copy_node (t);
17041 TREE_TYPE (r) = type;
17042 }
17043 return r;
17044 }
17045
17046 case PTRMEM_CST:
17047 /* These can sometimes show up in a partial instantiation, but never
17048 involve template parms. */
17049 gcc_assert (!uses_template_parms (t));
17050 return t;
17051
17052 case UNARY_LEFT_FOLD_EXPR:
17053 return tsubst_unary_left_fold (t, args, complain, in_decl);
17054 case UNARY_RIGHT_FOLD_EXPR:
17055 return tsubst_unary_right_fold (t, args, complain, in_decl);
17056 case BINARY_LEFT_FOLD_EXPR:
17057 return tsubst_binary_left_fold (t, args, complain, in_decl);
17058 case BINARY_RIGHT_FOLD_EXPR:
17059 return tsubst_binary_right_fold (t, args, complain, in_decl);
17060 case PREDICT_EXPR:
17061 return t;
17062
17063 case DEBUG_BEGIN_STMT:
17064 /* ??? There's no point in copying it for now, but maybe some
17065 day it will contain more information, such as a pointer back
17066 to the containing function, inlined copy or so. */
17067 return t;
17068
17069 case CO_AWAIT_EXPR:
17070 return tsubst_expr (t, args, complain, in_decl,
17071 /*integral_constant_expression_p=*/false);
17072 break;
17073
17074 default:
17075 /* We shouldn't get here, but keep going if !flag_checking. */
17076 if (flag_checking)
17077 gcc_unreachable ();
17078 return t;
17079 }
17080 }
17081
17082 /* Helper function for tsubst_omp_clauses, used for instantiation of
17083 OMP_CLAUSE_DECL of clauses. */
17084
17085 static tree
17086 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17087 tree in_decl, tree *iterator_cache)
17088 {
17089 if (decl == NULL_TREE)
17090 return NULL_TREE;
17091
17092 /* Handle OpenMP iterators. */
17093 if (TREE_CODE (decl) == TREE_LIST
17094 && TREE_PURPOSE (decl)
17095 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17096 {
17097 tree ret;
17098 if (iterator_cache[0] == TREE_PURPOSE (decl))
17099 ret = iterator_cache[1];
17100 else
17101 {
17102 tree *tp = &ret;
17103 begin_scope (sk_omp, NULL);
17104 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17105 {
17106 *tp = copy_node (it);
17107 TREE_VEC_ELT (*tp, 0)
17108 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17109 TREE_VEC_ELT (*tp, 1)
17110 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17111 /*integral_constant_expression_p=*/false);
17112 TREE_VEC_ELT (*tp, 2)
17113 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17114 /*integral_constant_expression_p=*/false);
17115 TREE_VEC_ELT (*tp, 3)
17116 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17117 /*integral_constant_expression_p=*/false);
17118 TREE_CHAIN (*tp) = NULL_TREE;
17119 tp = &TREE_CHAIN (*tp);
17120 }
17121 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17122 iterator_cache[0] = TREE_PURPOSE (decl);
17123 iterator_cache[1] = ret;
17124 }
17125 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17126 args, complain,
17127 in_decl, NULL));
17128 }
17129
17130 /* Handle an OpenMP array section represented as a TREE_LIST (or
17131 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17132 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17133 TREE_LIST. We can handle it exactly the same as an array section
17134 (purpose, value, and a chain), even though the nomenclature
17135 (low_bound, length, etc) is different. */
17136 if (TREE_CODE (decl) == TREE_LIST)
17137 {
17138 tree low_bound
17139 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17140 /*integral_constant_expression_p=*/false);
17141 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17142 /*integral_constant_expression_p=*/false);
17143 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17144 in_decl, NULL);
17145 if (TREE_PURPOSE (decl) == low_bound
17146 && TREE_VALUE (decl) == length
17147 && TREE_CHAIN (decl) == chain)
17148 return decl;
17149 tree ret = tree_cons (low_bound, length, chain);
17150 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17151 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17152 return ret;
17153 }
17154 tree ret = tsubst_expr (decl, args, complain, in_decl,
17155 /*integral_constant_expression_p=*/false);
17156 /* Undo convert_from_reference tsubst_expr could have called. */
17157 if (decl
17158 && REFERENCE_REF_P (ret)
17159 && !REFERENCE_REF_P (decl))
17160 ret = TREE_OPERAND (ret, 0);
17161 return ret;
17162 }
17163
17164 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17165
17166 static tree
17167 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17168 tree args, tsubst_flags_t complain, tree in_decl)
17169 {
17170 tree new_clauses = NULL_TREE, nc, oc;
17171 tree linear_no_step = NULL_TREE;
17172 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17173
17174 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17175 {
17176 nc = copy_node (oc);
17177 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17178 new_clauses = nc;
17179
17180 switch (OMP_CLAUSE_CODE (nc))
17181 {
17182 case OMP_CLAUSE_LASTPRIVATE:
17183 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17184 {
17185 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17186 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17187 in_decl, /*integral_constant_expression_p=*/false);
17188 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17189 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17190 }
17191 /* FALLTHRU */
17192 case OMP_CLAUSE_PRIVATE:
17193 case OMP_CLAUSE_SHARED:
17194 case OMP_CLAUSE_FIRSTPRIVATE:
17195 case OMP_CLAUSE_COPYIN:
17196 case OMP_CLAUSE_COPYPRIVATE:
17197 case OMP_CLAUSE_UNIFORM:
17198 case OMP_CLAUSE_DEPEND:
17199 case OMP_CLAUSE_FROM:
17200 case OMP_CLAUSE_TO:
17201 case OMP_CLAUSE_MAP:
17202 case OMP_CLAUSE_NONTEMPORAL:
17203 case OMP_CLAUSE_USE_DEVICE_PTR:
17204 case OMP_CLAUSE_USE_DEVICE_ADDR:
17205 case OMP_CLAUSE_IS_DEVICE_PTR:
17206 case OMP_CLAUSE_INCLUSIVE:
17207 case OMP_CLAUSE_EXCLUSIVE:
17208 OMP_CLAUSE_DECL (nc)
17209 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17210 in_decl, iterator_cache);
17211 break;
17212 case OMP_CLAUSE_TILE:
17213 case OMP_CLAUSE_IF:
17214 case OMP_CLAUSE_NUM_THREADS:
17215 case OMP_CLAUSE_SCHEDULE:
17216 case OMP_CLAUSE_COLLAPSE:
17217 case OMP_CLAUSE_FINAL:
17218 case OMP_CLAUSE_DEVICE:
17219 case OMP_CLAUSE_DIST_SCHEDULE:
17220 case OMP_CLAUSE_NUM_TEAMS:
17221 case OMP_CLAUSE_THREAD_LIMIT:
17222 case OMP_CLAUSE_SAFELEN:
17223 case OMP_CLAUSE_SIMDLEN:
17224 case OMP_CLAUSE_NUM_TASKS:
17225 case OMP_CLAUSE_GRAINSIZE:
17226 case OMP_CLAUSE_PRIORITY:
17227 case OMP_CLAUSE_ORDERED:
17228 case OMP_CLAUSE_HINT:
17229 case OMP_CLAUSE_NUM_GANGS:
17230 case OMP_CLAUSE_NUM_WORKERS:
17231 case OMP_CLAUSE_VECTOR_LENGTH:
17232 case OMP_CLAUSE_WORKER:
17233 case OMP_CLAUSE_VECTOR:
17234 case OMP_CLAUSE_ASYNC:
17235 case OMP_CLAUSE_WAIT:
17236 OMP_CLAUSE_OPERAND (nc, 0)
17237 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17238 in_decl, /*integral_constant_expression_p=*/false);
17239 break;
17240 case OMP_CLAUSE_REDUCTION:
17241 case OMP_CLAUSE_IN_REDUCTION:
17242 case OMP_CLAUSE_TASK_REDUCTION:
17243 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17244 {
17245 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17246 if (TREE_CODE (placeholder) == SCOPE_REF)
17247 {
17248 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17249 complain, in_decl);
17250 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17251 = build_qualified_name (NULL_TREE, scope,
17252 TREE_OPERAND (placeholder, 1),
17253 false);
17254 }
17255 else
17256 gcc_assert (identifier_p (placeholder));
17257 }
17258 OMP_CLAUSE_DECL (nc)
17259 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17260 in_decl, NULL);
17261 break;
17262 case OMP_CLAUSE_GANG:
17263 case OMP_CLAUSE_ALIGNED:
17264 OMP_CLAUSE_DECL (nc)
17265 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17266 in_decl, NULL);
17267 OMP_CLAUSE_OPERAND (nc, 1)
17268 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17269 in_decl, /*integral_constant_expression_p=*/false);
17270 break;
17271 case OMP_CLAUSE_LINEAR:
17272 OMP_CLAUSE_DECL (nc)
17273 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17274 in_decl, NULL);
17275 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17276 {
17277 gcc_assert (!linear_no_step);
17278 linear_no_step = nc;
17279 }
17280 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17281 OMP_CLAUSE_LINEAR_STEP (nc)
17282 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17283 complain, in_decl, NULL);
17284 else
17285 OMP_CLAUSE_LINEAR_STEP (nc)
17286 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17287 in_decl,
17288 /*integral_constant_expression_p=*/false);
17289 break;
17290 case OMP_CLAUSE_NOWAIT:
17291 case OMP_CLAUSE_DEFAULT:
17292 case OMP_CLAUSE_UNTIED:
17293 case OMP_CLAUSE_MERGEABLE:
17294 case OMP_CLAUSE_INBRANCH:
17295 case OMP_CLAUSE_NOTINBRANCH:
17296 case OMP_CLAUSE_PROC_BIND:
17297 case OMP_CLAUSE_FOR:
17298 case OMP_CLAUSE_PARALLEL:
17299 case OMP_CLAUSE_SECTIONS:
17300 case OMP_CLAUSE_TASKGROUP:
17301 case OMP_CLAUSE_NOGROUP:
17302 case OMP_CLAUSE_THREADS:
17303 case OMP_CLAUSE_SIMD:
17304 case OMP_CLAUSE_DEFAULTMAP:
17305 case OMP_CLAUSE_ORDER:
17306 case OMP_CLAUSE_BIND:
17307 case OMP_CLAUSE_INDEPENDENT:
17308 case OMP_CLAUSE_AUTO:
17309 case OMP_CLAUSE_SEQ:
17310 case OMP_CLAUSE_IF_PRESENT:
17311 case OMP_CLAUSE_FINALIZE:
17312 break;
17313 default:
17314 gcc_unreachable ();
17315 }
17316 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17317 switch (OMP_CLAUSE_CODE (nc))
17318 {
17319 case OMP_CLAUSE_SHARED:
17320 case OMP_CLAUSE_PRIVATE:
17321 case OMP_CLAUSE_FIRSTPRIVATE:
17322 case OMP_CLAUSE_LASTPRIVATE:
17323 case OMP_CLAUSE_COPYPRIVATE:
17324 case OMP_CLAUSE_LINEAR:
17325 case OMP_CLAUSE_REDUCTION:
17326 case OMP_CLAUSE_IN_REDUCTION:
17327 case OMP_CLAUSE_TASK_REDUCTION:
17328 case OMP_CLAUSE_USE_DEVICE_PTR:
17329 case OMP_CLAUSE_USE_DEVICE_ADDR:
17330 case OMP_CLAUSE_IS_DEVICE_PTR:
17331 case OMP_CLAUSE_INCLUSIVE:
17332 case OMP_CLAUSE_EXCLUSIVE:
17333 /* tsubst_expr on SCOPE_REF results in returning
17334 finish_non_static_data_member result. Undo that here. */
17335 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17336 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17337 == IDENTIFIER_NODE))
17338 {
17339 tree t = OMP_CLAUSE_DECL (nc);
17340 tree v = t;
17341 while (v)
17342 switch (TREE_CODE (v))
17343 {
17344 case COMPONENT_REF:
17345 case MEM_REF:
17346 case INDIRECT_REF:
17347 CASE_CONVERT:
17348 case POINTER_PLUS_EXPR:
17349 v = TREE_OPERAND (v, 0);
17350 continue;
17351 case PARM_DECL:
17352 if (DECL_CONTEXT (v) == current_function_decl
17353 && DECL_ARTIFICIAL (v)
17354 && DECL_NAME (v) == this_identifier)
17355 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17356 /* FALLTHRU */
17357 default:
17358 v = NULL_TREE;
17359 break;
17360 }
17361 }
17362 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17363 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17364 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17365 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17366 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17367 {
17368 tree decl = OMP_CLAUSE_DECL (nc);
17369 if (VAR_P (decl))
17370 {
17371 retrofit_lang_decl (decl);
17372 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17373 }
17374 }
17375 break;
17376 default:
17377 break;
17378 }
17379 }
17380
17381 new_clauses = nreverse (new_clauses);
17382 if (ort != C_ORT_OMP_DECLARE_SIMD)
17383 {
17384 new_clauses = finish_omp_clauses (new_clauses, ort);
17385 if (linear_no_step)
17386 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17387 if (nc == linear_no_step)
17388 {
17389 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17390 break;
17391 }
17392 }
17393 return new_clauses;
17394 }
17395
17396 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17397
17398 static tree
17399 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17400 tree in_decl)
17401 {
17402 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17403
17404 tree purpose, value, chain;
17405
17406 if (t == NULL)
17407 return t;
17408
17409 if (TREE_CODE (t) != TREE_LIST)
17410 return tsubst_copy_and_build (t, args, complain, in_decl,
17411 /*function_p=*/false,
17412 /*integral_constant_expression_p=*/false);
17413
17414 if (t == void_list_node)
17415 return t;
17416
17417 purpose = TREE_PURPOSE (t);
17418 if (purpose)
17419 purpose = RECUR (purpose);
17420 value = TREE_VALUE (t);
17421 if (value)
17422 {
17423 if (TREE_CODE (value) != LABEL_DECL)
17424 value = RECUR (value);
17425 else
17426 {
17427 value = lookup_label (DECL_NAME (value));
17428 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17429 TREE_USED (value) = 1;
17430 }
17431 }
17432 chain = TREE_CHAIN (t);
17433 if (chain && chain != void_type_node)
17434 chain = RECUR (chain);
17435 return tree_cons (purpose, value, chain);
17436 #undef RECUR
17437 }
17438
17439 /* Used to temporarily communicate the list of #pragma omp parallel
17440 clauses to #pragma omp for instantiation if they are combined
17441 together. */
17442
17443 static tree *omp_parallel_combined_clauses;
17444
17445 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17446 tree *, unsigned int *);
17447
17448 /* Substitute one OMP_FOR iterator. */
17449
17450 static bool
17451 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17452 tree initv, tree condv, tree incrv, tree *clauses,
17453 tree args, tsubst_flags_t complain, tree in_decl,
17454 bool integral_constant_expression_p)
17455 {
17456 #define RECUR(NODE) \
17457 tsubst_expr ((NODE), args, complain, in_decl, \
17458 integral_constant_expression_p)
17459 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17460 bool ret = false;
17461
17462 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17463 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17464
17465 decl = TREE_OPERAND (init, 0);
17466 init = TREE_OPERAND (init, 1);
17467 tree decl_expr = NULL_TREE;
17468 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17469 if (range_for)
17470 {
17471 bool decomp = false;
17472 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17473 {
17474 tree v = DECL_VALUE_EXPR (decl);
17475 if (TREE_CODE (v) == ARRAY_REF
17476 && VAR_P (TREE_OPERAND (v, 0))
17477 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17478 {
17479 tree decomp_first = NULL_TREE;
17480 unsigned decomp_cnt = 0;
17481 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17482 maybe_push_decl (d);
17483 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17484 in_decl, &decomp_first, &decomp_cnt);
17485 decomp = true;
17486 if (d == error_mark_node)
17487 decl = error_mark_node;
17488 else
17489 for (unsigned int i = 0; i < decomp_cnt; i++)
17490 {
17491 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17492 {
17493 tree v = build_nt (ARRAY_REF, d,
17494 size_int (decomp_cnt - i - 1),
17495 NULL_TREE, NULL_TREE);
17496 SET_DECL_VALUE_EXPR (decomp_first, v);
17497 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17498 }
17499 fit_decomposition_lang_decl (decomp_first, d);
17500 decomp_first = DECL_CHAIN (decomp_first);
17501 }
17502 }
17503 }
17504 decl = tsubst_decl (decl, args, complain);
17505 if (!decomp)
17506 maybe_push_decl (decl);
17507 }
17508 else if (init && TREE_CODE (init) == DECL_EXPR)
17509 {
17510 /* We need to jump through some hoops to handle declarations in the
17511 init-statement, since we might need to handle auto deduction,
17512 but we need to keep control of initialization. */
17513 decl_expr = init;
17514 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17515 decl = tsubst_decl (decl, args, complain);
17516 }
17517 else
17518 {
17519 if (TREE_CODE (decl) == SCOPE_REF)
17520 {
17521 decl = RECUR (decl);
17522 if (TREE_CODE (decl) == COMPONENT_REF)
17523 {
17524 tree v = decl;
17525 while (v)
17526 switch (TREE_CODE (v))
17527 {
17528 case COMPONENT_REF:
17529 case MEM_REF:
17530 case INDIRECT_REF:
17531 CASE_CONVERT:
17532 case POINTER_PLUS_EXPR:
17533 v = TREE_OPERAND (v, 0);
17534 continue;
17535 case PARM_DECL:
17536 if (DECL_CONTEXT (v) == current_function_decl
17537 && DECL_ARTIFICIAL (v)
17538 && DECL_NAME (v) == this_identifier)
17539 {
17540 decl = TREE_OPERAND (decl, 1);
17541 decl = omp_privatize_field (decl, false);
17542 }
17543 /* FALLTHRU */
17544 default:
17545 v = NULL_TREE;
17546 break;
17547 }
17548 }
17549 }
17550 else
17551 decl = RECUR (decl);
17552 }
17553 if (init && TREE_CODE (init) == TREE_VEC)
17554 {
17555 init = copy_node (init);
17556 TREE_VEC_ELT (init, 0)
17557 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17558 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17559 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17560 }
17561 else
17562 init = RECUR (init);
17563
17564 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17565 {
17566 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17567 if (TREE_CODE (o) == TREE_LIST)
17568 TREE_VEC_ELT (orig_declv, i)
17569 = tree_cons (RECUR (TREE_PURPOSE (o)),
17570 RECUR (TREE_VALUE (o)),
17571 NULL_TREE);
17572 else
17573 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17574 }
17575
17576 if (range_for)
17577 {
17578 tree this_pre_body = NULL_TREE;
17579 tree orig_init = NULL_TREE;
17580 tree orig_decl = NULL_TREE;
17581 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17582 orig_init, cond, incr);
17583 if (orig_decl)
17584 {
17585 if (orig_declv == NULL_TREE)
17586 orig_declv = copy_node (declv);
17587 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17588 ret = true;
17589 }
17590 else if (orig_declv)
17591 TREE_VEC_ELT (orig_declv, i) = decl;
17592 }
17593
17594 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17595 if (!range_for && auto_node && init)
17596 TREE_TYPE (decl)
17597 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17598
17599 gcc_assert (!type_dependent_expression_p (decl));
17600
17601 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17602 {
17603 if (decl_expr)
17604 {
17605 /* Declare the variable, but don't let that initialize it. */
17606 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17607 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17608 RECUR (decl_expr);
17609 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17610 }
17611
17612 if (!range_for)
17613 {
17614 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17615 if (COMPARISON_CLASS_P (cond)
17616 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17617 {
17618 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17619 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17620 TREE_VEC_ELT (rhs, 0)
17621 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17622 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17623 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17624 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17625 lhs, rhs);
17626 }
17627 else
17628 cond = RECUR (cond);
17629 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17630 if (TREE_CODE (incr) == MODIFY_EXPR)
17631 {
17632 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17633 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17634 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17635 NOP_EXPR, rhs, complain);
17636 }
17637 else
17638 incr = RECUR (incr);
17639 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17640 TREE_VEC_ELT (orig_declv, i) = decl;
17641 }
17642 TREE_VEC_ELT (declv, i) = decl;
17643 TREE_VEC_ELT (initv, i) = init;
17644 TREE_VEC_ELT (condv, i) = cond;
17645 TREE_VEC_ELT (incrv, i) = incr;
17646 return ret;
17647 }
17648
17649 if (decl_expr)
17650 {
17651 /* Declare and initialize the variable. */
17652 RECUR (decl_expr);
17653 init = NULL_TREE;
17654 }
17655 else if (init)
17656 {
17657 tree *pc;
17658 int j;
17659 for (j = ((omp_parallel_combined_clauses == NULL
17660 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17661 {
17662 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17663 {
17664 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17665 && OMP_CLAUSE_DECL (*pc) == decl)
17666 break;
17667 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17668 && OMP_CLAUSE_DECL (*pc) == decl)
17669 {
17670 if (j)
17671 break;
17672 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17673 tree c = *pc;
17674 *pc = OMP_CLAUSE_CHAIN (c);
17675 OMP_CLAUSE_CHAIN (c) = *clauses;
17676 *clauses = c;
17677 }
17678 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17679 && OMP_CLAUSE_DECL (*pc) == decl)
17680 {
17681 error ("iteration variable %qD should not be firstprivate",
17682 decl);
17683 *pc = OMP_CLAUSE_CHAIN (*pc);
17684 }
17685 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17686 && OMP_CLAUSE_DECL (*pc) == decl)
17687 {
17688 error ("iteration variable %qD should not be reduction",
17689 decl);
17690 *pc = OMP_CLAUSE_CHAIN (*pc);
17691 }
17692 else
17693 pc = &OMP_CLAUSE_CHAIN (*pc);
17694 }
17695 if (*pc)
17696 break;
17697 }
17698 if (*pc == NULL_TREE)
17699 {
17700 tree c = build_omp_clause (input_location,
17701 TREE_CODE (t) == OMP_LOOP
17702 ? OMP_CLAUSE_LASTPRIVATE
17703 : OMP_CLAUSE_PRIVATE);
17704 OMP_CLAUSE_DECL (c) = decl;
17705 c = finish_omp_clauses (c, C_ORT_OMP);
17706 if (c)
17707 {
17708 OMP_CLAUSE_CHAIN (c) = *clauses;
17709 *clauses = c;
17710 }
17711 }
17712 }
17713 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17714 if (COMPARISON_CLASS_P (cond))
17715 {
17716 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17717 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17718 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17719 }
17720 else
17721 cond = RECUR (cond);
17722 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17723 switch (TREE_CODE (incr))
17724 {
17725 case PREINCREMENT_EXPR:
17726 case PREDECREMENT_EXPR:
17727 case POSTINCREMENT_EXPR:
17728 case POSTDECREMENT_EXPR:
17729 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17730 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17731 break;
17732 case MODIFY_EXPR:
17733 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17734 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17735 {
17736 tree rhs = TREE_OPERAND (incr, 1);
17737 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17738 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17739 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17740 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17741 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17742 rhs0, rhs1));
17743 }
17744 else
17745 incr = RECUR (incr);
17746 break;
17747 case MODOP_EXPR:
17748 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17749 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17750 {
17751 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17752 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17753 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17754 TREE_TYPE (decl), lhs,
17755 RECUR (TREE_OPERAND (incr, 2))));
17756 }
17757 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17758 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17759 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17760 {
17761 tree rhs = TREE_OPERAND (incr, 2);
17762 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17763 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17764 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17765 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17766 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17767 rhs0, rhs1));
17768 }
17769 else
17770 incr = RECUR (incr);
17771 break;
17772 default:
17773 incr = RECUR (incr);
17774 break;
17775 }
17776
17777 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17778 TREE_VEC_ELT (orig_declv, i) = decl;
17779 TREE_VEC_ELT (declv, i) = decl;
17780 TREE_VEC_ELT (initv, i) = init;
17781 TREE_VEC_ELT (condv, i) = cond;
17782 TREE_VEC_ELT (incrv, i) = incr;
17783 return false;
17784 #undef RECUR
17785 }
17786
17787 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17788 of OMP_TARGET's body. */
17789
17790 static tree
17791 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17792 {
17793 *walk_subtrees = 0;
17794 switch (TREE_CODE (*tp))
17795 {
17796 case OMP_TEAMS:
17797 return *tp;
17798 case BIND_EXPR:
17799 case STATEMENT_LIST:
17800 *walk_subtrees = 1;
17801 break;
17802 default:
17803 break;
17804 }
17805 return NULL_TREE;
17806 }
17807
17808 /* Helper function for tsubst_expr. For decomposition declaration
17809 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17810 also the corresponding decls representing the identifiers
17811 of the decomposition declaration. Return DECL if successful
17812 or error_mark_node otherwise, set *FIRST to the first decl
17813 in the list chained through DECL_CHAIN and *CNT to the number
17814 of such decls. */
17815
17816 static tree
17817 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17818 tsubst_flags_t complain, tree in_decl, tree *first,
17819 unsigned int *cnt)
17820 {
17821 tree decl2, decl3, prev = decl;
17822 *cnt = 0;
17823 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17824 for (decl2 = DECL_CHAIN (pattern_decl);
17825 decl2
17826 && VAR_P (decl2)
17827 && DECL_DECOMPOSITION_P (decl2)
17828 && DECL_NAME (decl2);
17829 decl2 = DECL_CHAIN (decl2))
17830 {
17831 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17832 {
17833 gcc_assert (errorcount);
17834 return error_mark_node;
17835 }
17836 (*cnt)++;
17837 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17838 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17839 tree v = DECL_VALUE_EXPR (decl2);
17840 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17841 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17842 decl3 = tsubst (decl2, args, complain, in_decl);
17843 SET_DECL_VALUE_EXPR (decl2, v);
17844 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17845 if (VAR_P (decl3))
17846 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17847 else
17848 {
17849 gcc_assert (errorcount);
17850 decl = error_mark_node;
17851 continue;
17852 }
17853 maybe_push_decl (decl3);
17854 if (error_operand_p (decl3))
17855 decl = error_mark_node;
17856 else if (decl != error_mark_node
17857 && DECL_CHAIN (decl3) != prev
17858 && decl != prev)
17859 {
17860 gcc_assert (errorcount);
17861 decl = error_mark_node;
17862 }
17863 else
17864 prev = decl3;
17865 }
17866 *first = prev;
17867 return decl;
17868 }
17869
17870 /* Return the proper local_specialization for init-capture pack DECL. */
17871
17872 static tree
17873 lookup_init_capture_pack (tree decl)
17874 {
17875 /* We handle normal pack captures by forwarding to the specialization of the
17876 captured parameter. We can't do that for pack init-captures; we need them
17877 to have their own local_specialization. We created the individual
17878 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17879 when we process the DECL_EXPR for the pack init-capture in the template.
17880 So, how do we find them? We don't know the capture proxy pack when
17881 building the individual resulting proxies, and we don't know the
17882 individual proxies when instantiating the pack. What we have in common is
17883 the FIELD_DECL.
17884
17885 So...when we instantiate the FIELD_DECL, we stick the result in
17886 local_specializations. Then at the DECL_EXPR we look up that result, see
17887 how many elements it has, synthesize the names, and look them up. */
17888
17889 tree cname = DECL_NAME (decl);
17890 tree val = DECL_VALUE_EXPR (decl);
17891 tree field = TREE_OPERAND (val, 1);
17892 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17893 tree fpack = retrieve_local_specialization (field);
17894 if (fpack == error_mark_node)
17895 return error_mark_node;
17896
17897 int len = 1;
17898 tree vec = NULL_TREE;
17899 tree r = NULL_TREE;
17900 if (TREE_CODE (fpack) == TREE_VEC)
17901 {
17902 len = TREE_VEC_LENGTH (fpack);
17903 vec = make_tree_vec (len);
17904 r = make_node (NONTYPE_ARGUMENT_PACK);
17905 SET_ARGUMENT_PACK_ARGS (r, vec);
17906 }
17907 for (int i = 0; i < len; ++i)
17908 {
17909 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17910 tree elt = lookup_name (ename);
17911 if (vec)
17912 TREE_VEC_ELT (vec, i) = elt;
17913 else
17914 r = elt;
17915 }
17916 return r;
17917 }
17918
17919 /* Like tsubst_copy for expressions, etc. but also does semantic
17920 processing. */
17921
17922 tree
17923 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17924 bool integral_constant_expression_p)
17925 {
17926 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17927 #define RECUR(NODE) \
17928 tsubst_expr ((NODE), args, complain, in_decl, \
17929 integral_constant_expression_p)
17930
17931 tree stmt, tmp;
17932 tree r;
17933 location_t loc;
17934
17935 if (t == NULL_TREE || t == error_mark_node)
17936 return t;
17937
17938 loc = input_location;
17939 if (location_t eloc = cp_expr_location (t))
17940 input_location = eloc;
17941 if (STATEMENT_CODE_P (TREE_CODE (t)))
17942 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17943
17944 switch (TREE_CODE (t))
17945 {
17946 case STATEMENT_LIST:
17947 {
17948 tree_stmt_iterator i;
17949 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17950 RECUR (tsi_stmt (i));
17951 break;
17952 }
17953
17954 case CTOR_INITIALIZER:
17955 finish_mem_initializers (tsubst_initializer_list
17956 (TREE_OPERAND (t, 0), args));
17957 break;
17958
17959 case RETURN_EXPR:
17960 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17961 break;
17962
17963 case CO_RETURN_EXPR:
17964 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
17965 break;
17966
17967 case CO_YIELD_EXPR:
17968 stmt = finish_co_yield_expr (input_location,
17969 RECUR (TREE_OPERAND (t, 0)));
17970 RETURN (stmt);
17971 break;
17972
17973 case CO_AWAIT_EXPR:
17974 stmt = finish_co_await_expr (input_location,
17975 RECUR (TREE_OPERAND (t, 0)));
17976 RETURN (stmt);
17977 break;
17978
17979 case EXPR_STMT:
17980 tmp = RECUR (EXPR_STMT_EXPR (t));
17981 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17982 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17983 else
17984 finish_expr_stmt (tmp);
17985 break;
17986
17987 case USING_STMT:
17988 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17989 break;
17990
17991 case DECL_EXPR:
17992 {
17993 tree decl, pattern_decl;
17994 tree init;
17995
17996 pattern_decl = decl = DECL_EXPR_DECL (t);
17997 if (TREE_CODE (decl) == LABEL_DECL)
17998 finish_label_decl (DECL_NAME (decl));
17999 else if (TREE_CODE (decl) == USING_DECL)
18000 {
18001 tree scope = USING_DECL_SCOPE (decl);
18002 tree name = DECL_NAME (decl);
18003
18004 scope = tsubst (scope, args, complain, in_decl);
18005 finish_nonmember_using_decl (scope, name);
18006 }
18007 else if (is_capture_proxy (decl)
18008 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18009 {
18010 /* We're in tsubst_lambda_expr, we've already inserted a new
18011 capture proxy, so look it up and register it. */
18012 tree inst;
18013 if (!DECL_PACK_P (decl))
18014 {
18015 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18016 LOOK_want::HIDDEN_LAMBDA);
18017 gcc_assert (inst != decl && is_capture_proxy (inst));
18018 }
18019 else if (is_normal_capture_proxy (decl))
18020 {
18021 inst = (retrieve_local_specialization
18022 (DECL_CAPTURED_VARIABLE (decl)));
18023 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18024 || DECL_PACK_P (inst));
18025 }
18026 else
18027 inst = lookup_init_capture_pack (decl);
18028
18029 register_local_specialization (inst, decl);
18030 break;
18031 }
18032 else if (DECL_PRETTY_FUNCTION_P (decl))
18033 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18034 DECL_NAME (decl),
18035 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18036 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18037 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18038 /* Don't copy the old closure; we'll create a new one in
18039 tsubst_lambda_expr. */
18040 break;
18041 else
18042 {
18043 init = DECL_INITIAL (decl);
18044 decl = tsubst (decl, args, complain, in_decl);
18045 if (decl != error_mark_node)
18046 {
18047 /* By marking the declaration as instantiated, we avoid
18048 trying to instantiate it. Since instantiate_decl can't
18049 handle local variables, and since we've already done
18050 all that needs to be done, that's the right thing to
18051 do. */
18052 if (VAR_P (decl))
18053 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18054 if (VAR_P (decl) && !DECL_NAME (decl)
18055 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18056 /* Anonymous aggregates are a special case. */
18057 finish_anon_union (decl);
18058 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18059 {
18060 DECL_CONTEXT (decl) = current_function_decl;
18061 if (DECL_NAME (decl) == this_identifier)
18062 {
18063 tree lam = DECL_CONTEXT (current_function_decl);
18064 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18065 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18066 }
18067 insert_capture_proxy (decl);
18068 }
18069 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18070 /* We already did a pushtag. */;
18071 else if (TREE_CODE (decl) == FUNCTION_DECL
18072 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18073 && DECL_FUNCTION_SCOPE_P (pattern_decl))
18074 {
18075 /* We pretend this is regular local extern decl of
18076 a namespace-scope fn. Then we make it really
18077 local, it is a nested function. */
18078 gcc_checking_assert (DECL_LOCAL_DECL_P (decl));
18079 DECL_CONTEXT (decl) = global_namespace;
18080 pushdecl (decl);
18081 DECL_CONTEXT (decl) = current_function_decl;
18082 if (cp_check_omp_declare_reduction (decl))
18083 instantiate_body (pattern_decl, args, decl, true);
18084 }
18085 else
18086 {
18087 bool const_init = false;
18088 unsigned int cnt = 0;
18089 tree first = NULL_TREE, ndecl = error_mark_node;
18090 maybe_push_decl (decl);
18091
18092 if (VAR_P (decl)
18093 && DECL_DECOMPOSITION_P (decl)
18094 && TREE_TYPE (pattern_decl) != error_mark_node)
18095 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18096 complain, in_decl, &first,
18097 &cnt);
18098
18099 init = tsubst_init (init, decl, args, complain, in_decl);
18100
18101 if (VAR_P (decl))
18102 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18103 (pattern_decl));
18104
18105 if (ndecl != error_mark_node)
18106 cp_maybe_mangle_decomp (ndecl, first, cnt);
18107
18108 /* In a non-template function, VLA type declarations are
18109 handled in grokdeclarator; for templates, handle them
18110 now. */
18111 predeclare_vla (decl);
18112
18113 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
18114
18115 if (ndecl != error_mark_node)
18116 cp_finish_decomp (ndecl, first, cnt);
18117 }
18118 }
18119 }
18120
18121 break;
18122 }
18123
18124 case FOR_STMT:
18125 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18126 RECUR (FOR_INIT_STMT (t));
18127 finish_init_stmt (stmt);
18128 tmp = RECUR (FOR_COND (t));
18129 finish_for_cond (tmp, stmt, false, 0);
18130 tmp = RECUR (FOR_EXPR (t));
18131 finish_for_expr (tmp, stmt);
18132 {
18133 bool prev = note_iteration_stmt_body_start ();
18134 RECUR (FOR_BODY (t));
18135 note_iteration_stmt_body_end (prev);
18136 }
18137 finish_for_stmt (stmt);
18138 break;
18139
18140 case RANGE_FOR_STMT:
18141 {
18142 /* Construct another range_for, if this is not a final
18143 substitution (for inside a generic lambda of a
18144 template). Otherwise convert to a regular for. */
18145 tree decl, expr;
18146 stmt = (processing_template_decl
18147 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18148 : begin_for_stmt (NULL_TREE, NULL_TREE));
18149 RECUR (RANGE_FOR_INIT_STMT (t));
18150 decl = RANGE_FOR_DECL (t);
18151 decl = tsubst (decl, args, complain, in_decl);
18152 maybe_push_decl (decl);
18153 expr = RECUR (RANGE_FOR_EXPR (t));
18154
18155 tree decomp_first = NULL_TREE;
18156 unsigned decomp_cnt = 0;
18157 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18158 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18159 complain, in_decl,
18160 &decomp_first, &decomp_cnt);
18161
18162 if (processing_template_decl)
18163 {
18164 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18165 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18166 finish_range_for_decl (stmt, decl, expr);
18167 if (decomp_first && decl != error_mark_node)
18168 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18169 }
18170 else
18171 {
18172 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18173 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18174 stmt = cp_convert_range_for (stmt, decl, expr,
18175 decomp_first, decomp_cnt,
18176 RANGE_FOR_IVDEP (t), unroll);
18177 }
18178
18179 bool prev = note_iteration_stmt_body_start ();
18180 RECUR (RANGE_FOR_BODY (t));
18181 note_iteration_stmt_body_end (prev);
18182 finish_for_stmt (stmt);
18183 }
18184 break;
18185
18186 case WHILE_STMT:
18187 stmt = begin_while_stmt ();
18188 tmp = RECUR (WHILE_COND (t));
18189 finish_while_stmt_cond (tmp, stmt, false, 0);
18190 {
18191 bool prev = note_iteration_stmt_body_start ();
18192 RECUR (WHILE_BODY (t));
18193 note_iteration_stmt_body_end (prev);
18194 }
18195 finish_while_stmt (stmt);
18196 break;
18197
18198 case DO_STMT:
18199 stmt = begin_do_stmt ();
18200 {
18201 bool prev = note_iteration_stmt_body_start ();
18202 RECUR (DO_BODY (t));
18203 note_iteration_stmt_body_end (prev);
18204 }
18205 finish_do_body (stmt);
18206 tmp = RECUR (DO_COND (t));
18207 finish_do_stmt (tmp, stmt, false, 0);
18208 break;
18209
18210 case IF_STMT:
18211 stmt = begin_if_stmt ();
18212 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18213 if (IF_STMT_CONSTEXPR_P (t))
18214 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18215 tmp = RECUR (IF_COND (t));
18216 tmp = finish_if_stmt_cond (tmp, stmt);
18217 if (IF_STMT_CONSTEXPR_P (t)
18218 && instantiation_dependent_expression_p (tmp))
18219 {
18220 /* We're partially instantiating a generic lambda, but the condition
18221 of the constexpr if is still dependent. Don't substitute into the
18222 branches now, just remember the template arguments. */
18223 do_poplevel (IF_SCOPE (stmt));
18224 IF_COND (stmt) = IF_COND (t);
18225 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18226 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18227 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18228 add_stmt (stmt);
18229 break;
18230 }
18231 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18232 /* Don't instantiate the THEN_CLAUSE. */;
18233 else
18234 {
18235 tree folded = fold_non_dependent_expr (tmp, complain);
18236 bool inhibit = integer_zerop (folded);
18237 if (inhibit)
18238 ++c_inhibit_evaluation_warnings;
18239 RECUR (THEN_CLAUSE (t));
18240 if (inhibit)
18241 --c_inhibit_evaluation_warnings;
18242 }
18243 finish_then_clause (stmt);
18244
18245 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18246 /* Don't instantiate the ELSE_CLAUSE. */;
18247 else if (ELSE_CLAUSE (t))
18248 {
18249 tree folded = fold_non_dependent_expr (tmp, complain);
18250 bool inhibit = integer_nonzerop (folded);
18251 begin_else_clause (stmt);
18252 if (inhibit)
18253 ++c_inhibit_evaluation_warnings;
18254 RECUR (ELSE_CLAUSE (t));
18255 if (inhibit)
18256 --c_inhibit_evaluation_warnings;
18257 finish_else_clause (stmt);
18258 }
18259
18260 finish_if_stmt (stmt);
18261 break;
18262
18263 case BIND_EXPR:
18264 if (BIND_EXPR_BODY_BLOCK (t))
18265 stmt = begin_function_body ();
18266 else
18267 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18268 ? BCS_TRY_BLOCK : 0);
18269
18270 RECUR (BIND_EXPR_BODY (t));
18271
18272 if (BIND_EXPR_BODY_BLOCK (t))
18273 finish_function_body (stmt);
18274 else
18275 finish_compound_stmt (stmt);
18276 break;
18277
18278 case BREAK_STMT:
18279 finish_break_stmt ();
18280 break;
18281
18282 case CONTINUE_STMT:
18283 finish_continue_stmt ();
18284 break;
18285
18286 case SWITCH_STMT:
18287 stmt = begin_switch_stmt ();
18288 tmp = RECUR (SWITCH_STMT_COND (t));
18289 finish_switch_cond (tmp, stmt);
18290 RECUR (SWITCH_STMT_BODY (t));
18291 finish_switch_stmt (stmt);
18292 break;
18293
18294 case CASE_LABEL_EXPR:
18295 {
18296 tree decl = CASE_LABEL (t);
18297 tree low = RECUR (CASE_LOW (t));
18298 tree high = RECUR (CASE_HIGH (t));
18299 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18300 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18301 {
18302 tree label = CASE_LABEL (l);
18303 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18304 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18305 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18306 }
18307 }
18308 break;
18309
18310 case LABEL_EXPR:
18311 {
18312 tree decl = LABEL_EXPR_LABEL (t);
18313 tree label;
18314
18315 label = finish_label_stmt (DECL_NAME (decl));
18316 if (TREE_CODE (label) == LABEL_DECL)
18317 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18318 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18319 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18320 }
18321 break;
18322
18323 case GOTO_EXPR:
18324 tmp = GOTO_DESTINATION (t);
18325 if (TREE_CODE (tmp) != LABEL_DECL)
18326 /* Computed goto's must be tsubst'd into. On the other hand,
18327 non-computed gotos must not be; the identifier in question
18328 will have no binding. */
18329 tmp = RECUR (tmp);
18330 else
18331 tmp = DECL_NAME (tmp);
18332 finish_goto_stmt (tmp);
18333 break;
18334
18335 case ASM_EXPR:
18336 {
18337 tree string = RECUR (ASM_STRING (t));
18338 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18339 complain, in_decl);
18340 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18341 complain, in_decl);
18342 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18343 complain, in_decl);
18344 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18345 complain, in_decl);
18346 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18347 outputs, inputs, clobbers, labels,
18348 ASM_INLINE_P (t));
18349 tree asm_expr = tmp;
18350 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18351 asm_expr = TREE_OPERAND (asm_expr, 0);
18352 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18353 }
18354 break;
18355
18356 case TRY_BLOCK:
18357 if (CLEANUP_P (t))
18358 {
18359 stmt = begin_try_block ();
18360 RECUR (TRY_STMTS (t));
18361 finish_cleanup_try_block (stmt);
18362 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18363 }
18364 else
18365 {
18366 tree compound_stmt = NULL_TREE;
18367
18368 if (FN_TRY_BLOCK_P (t))
18369 stmt = begin_function_try_block (&compound_stmt);
18370 else
18371 stmt = begin_try_block ();
18372
18373 RECUR (TRY_STMTS (t));
18374
18375 if (FN_TRY_BLOCK_P (t))
18376 finish_function_try_block (stmt);
18377 else
18378 finish_try_block (stmt);
18379
18380 RECUR (TRY_HANDLERS (t));
18381 if (FN_TRY_BLOCK_P (t))
18382 finish_function_handler_sequence (stmt, compound_stmt);
18383 else
18384 finish_handler_sequence (stmt);
18385 }
18386 break;
18387
18388 case HANDLER:
18389 {
18390 tree decl = HANDLER_PARMS (t);
18391
18392 if (decl)
18393 {
18394 decl = tsubst (decl, args, complain, in_decl);
18395 /* Prevent instantiate_decl from trying to instantiate
18396 this variable. We've already done all that needs to be
18397 done. */
18398 if (decl != error_mark_node)
18399 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18400 }
18401 stmt = begin_handler ();
18402 finish_handler_parms (decl, stmt);
18403 RECUR (HANDLER_BODY (t));
18404 finish_handler (stmt);
18405 }
18406 break;
18407
18408 case TAG_DEFN:
18409 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18410 if (CLASS_TYPE_P (tmp))
18411 {
18412 /* Local classes are not independent templates; they are
18413 instantiated along with their containing function. And this
18414 way we don't have to deal with pushing out of one local class
18415 to instantiate a member of another local class. */
18416 /* Closures are handled by the LAMBDA_EXPR. */
18417 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18418 complete_type (tmp);
18419 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18420 if ((VAR_P (fld)
18421 || (TREE_CODE (fld) == FUNCTION_DECL
18422 && !DECL_ARTIFICIAL (fld)))
18423 && DECL_TEMPLATE_INSTANTIATION (fld))
18424 instantiate_decl (fld, /*defer_ok=*/false,
18425 /*expl_inst_class=*/false);
18426 }
18427 break;
18428
18429 case STATIC_ASSERT:
18430 {
18431 tree condition;
18432
18433 ++c_inhibit_evaluation_warnings;
18434 condition =
18435 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18436 args,
18437 complain, in_decl,
18438 /*integral_constant_expression_p=*/true);
18439 --c_inhibit_evaluation_warnings;
18440
18441 finish_static_assert (condition,
18442 STATIC_ASSERT_MESSAGE (t),
18443 STATIC_ASSERT_SOURCE_LOCATION (t),
18444 /*member_p=*/false);
18445 }
18446 break;
18447
18448 case OACC_KERNELS:
18449 case OACC_PARALLEL:
18450 case OACC_SERIAL:
18451 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18452 in_decl);
18453 stmt = begin_omp_parallel ();
18454 RECUR (OMP_BODY (t));
18455 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18456 break;
18457
18458 case OMP_PARALLEL:
18459 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18460 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18461 complain, in_decl);
18462 if (OMP_PARALLEL_COMBINED (t))
18463 omp_parallel_combined_clauses = &tmp;
18464 stmt = begin_omp_parallel ();
18465 RECUR (OMP_PARALLEL_BODY (t));
18466 gcc_assert (omp_parallel_combined_clauses == NULL);
18467 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18468 = OMP_PARALLEL_COMBINED (t);
18469 pop_omp_privatization_clauses (r);
18470 break;
18471
18472 case OMP_TASK:
18473 if (OMP_TASK_BODY (t) == NULL_TREE)
18474 {
18475 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18476 complain, in_decl);
18477 t = copy_node (t);
18478 OMP_TASK_CLAUSES (t) = tmp;
18479 add_stmt (t);
18480 break;
18481 }
18482 r = push_omp_privatization_clauses (false);
18483 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18484 complain, in_decl);
18485 stmt = begin_omp_task ();
18486 RECUR (OMP_TASK_BODY (t));
18487 finish_omp_task (tmp, stmt);
18488 pop_omp_privatization_clauses (r);
18489 break;
18490
18491 case OMP_FOR:
18492 case OMP_LOOP:
18493 case OMP_SIMD:
18494 case OMP_DISTRIBUTE:
18495 case OMP_TASKLOOP:
18496 case OACC_LOOP:
18497 {
18498 tree clauses, body, pre_body;
18499 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18500 tree orig_declv = NULL_TREE;
18501 tree incrv = NULL_TREE;
18502 enum c_omp_region_type ort = C_ORT_OMP;
18503 bool any_range_for = false;
18504 int i;
18505
18506 if (TREE_CODE (t) == OACC_LOOP)
18507 ort = C_ORT_ACC;
18508
18509 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18510 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18511 in_decl);
18512 if (OMP_FOR_INIT (t) != NULL_TREE)
18513 {
18514 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18515 if (OMP_FOR_ORIG_DECLS (t))
18516 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18517 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18518 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18519 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18520 }
18521
18522 keep_next_level (true);
18523 stmt = begin_omp_structured_block ();
18524
18525 pre_body = push_stmt_list ();
18526 RECUR (OMP_FOR_PRE_BODY (t));
18527 pre_body = pop_stmt_list (pre_body);
18528
18529 if (OMP_FOR_INIT (t) != NULL_TREE)
18530 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18531 any_range_for
18532 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18533 condv, incrv, &clauses, args,
18534 complain, in_decl,
18535 integral_constant_expression_p);
18536 omp_parallel_combined_clauses = NULL;
18537
18538 if (any_range_for)
18539 {
18540 gcc_assert (orig_declv);
18541 body = begin_omp_structured_block ();
18542 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18543 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18544 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18545 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18546 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18547 TREE_VEC_ELT (declv, i));
18548 }
18549 else
18550 body = push_stmt_list ();
18551 RECUR (OMP_FOR_BODY (t));
18552 if (any_range_for)
18553 body = finish_omp_structured_block (body);
18554 else
18555 body = pop_stmt_list (body);
18556
18557 if (OMP_FOR_INIT (t) != NULL_TREE)
18558 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18559 orig_declv, initv, condv, incrv, body, pre_body,
18560 NULL, clauses);
18561 else
18562 {
18563 t = make_node (TREE_CODE (t));
18564 TREE_TYPE (t) = void_type_node;
18565 OMP_FOR_BODY (t) = body;
18566 OMP_FOR_PRE_BODY (t) = pre_body;
18567 OMP_FOR_CLAUSES (t) = clauses;
18568 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18569 add_stmt (t);
18570 }
18571
18572 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18573 t));
18574 pop_omp_privatization_clauses (r);
18575 }
18576 break;
18577
18578 case OMP_SECTIONS:
18579 omp_parallel_combined_clauses = NULL;
18580 /* FALLTHRU */
18581 case OMP_SINGLE:
18582 case OMP_TEAMS:
18583 case OMP_CRITICAL:
18584 case OMP_TASKGROUP:
18585 case OMP_SCAN:
18586 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18587 && OMP_TEAMS_COMBINED (t));
18588 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18589 in_decl);
18590 if (TREE_CODE (t) == OMP_TEAMS)
18591 {
18592 keep_next_level (true);
18593 stmt = begin_omp_structured_block ();
18594 RECUR (OMP_BODY (t));
18595 stmt = finish_omp_structured_block (stmt);
18596 }
18597 else
18598 {
18599 stmt = push_stmt_list ();
18600 RECUR (OMP_BODY (t));
18601 stmt = pop_stmt_list (stmt);
18602 }
18603
18604 if (TREE_CODE (t) == OMP_CRITICAL
18605 && tmp != NULL_TREE
18606 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18607 {
18608 error_at (OMP_CLAUSE_LOCATION (tmp),
18609 "%<#pragma omp critical%> with %<hint%> clause requires "
18610 "a name, except when %<omp_sync_hint_none%> is used");
18611 RETURN (error_mark_node);
18612 }
18613 t = copy_node (t);
18614 OMP_BODY (t) = stmt;
18615 OMP_CLAUSES (t) = tmp;
18616 add_stmt (t);
18617 pop_omp_privatization_clauses (r);
18618 break;
18619
18620 case OMP_DEPOBJ:
18621 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18622 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18623 {
18624 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18625 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18626 {
18627 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18628 args, complain, in_decl);
18629 if (tmp == NULL_TREE)
18630 tmp = error_mark_node;
18631 }
18632 else
18633 {
18634 kind = (enum omp_clause_depend_kind)
18635 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18636 tmp = NULL_TREE;
18637 }
18638 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18639 }
18640 else
18641 finish_omp_depobj (EXPR_LOCATION (t), r,
18642 OMP_CLAUSE_DEPEND_SOURCE,
18643 OMP_DEPOBJ_CLAUSES (t));
18644 break;
18645
18646 case OACC_DATA:
18647 case OMP_TARGET_DATA:
18648 case OMP_TARGET:
18649 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18650 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18651 in_decl);
18652 keep_next_level (true);
18653 stmt = begin_omp_structured_block ();
18654
18655 RECUR (OMP_BODY (t));
18656 stmt = finish_omp_structured_block (stmt);
18657
18658 t = copy_node (t);
18659 OMP_BODY (t) = stmt;
18660 OMP_CLAUSES (t) = tmp;
18661 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18662 {
18663 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18664 if (teams)
18665 {
18666 /* For combined target teams, ensure the num_teams and
18667 thread_limit clause expressions are evaluated on the host,
18668 before entering the target construct. */
18669 tree c;
18670 for (c = OMP_TEAMS_CLAUSES (teams);
18671 c; c = OMP_CLAUSE_CHAIN (c))
18672 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18673 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18674 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18675 {
18676 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18677 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18678 if (expr == error_mark_node)
18679 continue;
18680 tmp = TARGET_EXPR_SLOT (expr);
18681 add_stmt (expr);
18682 OMP_CLAUSE_OPERAND (c, 0) = expr;
18683 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18684 OMP_CLAUSE_FIRSTPRIVATE);
18685 OMP_CLAUSE_DECL (tc) = tmp;
18686 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18687 OMP_TARGET_CLAUSES (t) = tc;
18688 }
18689 }
18690 }
18691 add_stmt (t);
18692 break;
18693
18694 case OACC_DECLARE:
18695 t = copy_node (t);
18696 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18697 complain, in_decl);
18698 OACC_DECLARE_CLAUSES (t) = tmp;
18699 add_stmt (t);
18700 break;
18701
18702 case OMP_TARGET_UPDATE:
18703 case OMP_TARGET_ENTER_DATA:
18704 case OMP_TARGET_EXIT_DATA:
18705 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18706 complain, in_decl);
18707 t = copy_node (t);
18708 OMP_STANDALONE_CLAUSES (t) = tmp;
18709 add_stmt (t);
18710 break;
18711
18712 case OACC_ENTER_DATA:
18713 case OACC_EXIT_DATA:
18714 case OACC_UPDATE:
18715 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18716 complain, in_decl);
18717 t = copy_node (t);
18718 OMP_STANDALONE_CLAUSES (t) = tmp;
18719 add_stmt (t);
18720 break;
18721
18722 case OMP_ORDERED:
18723 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18724 complain, in_decl);
18725 stmt = push_stmt_list ();
18726 RECUR (OMP_BODY (t));
18727 stmt = pop_stmt_list (stmt);
18728
18729 t = copy_node (t);
18730 OMP_BODY (t) = stmt;
18731 OMP_ORDERED_CLAUSES (t) = tmp;
18732 add_stmt (t);
18733 break;
18734
18735 case OMP_MASTER:
18736 omp_parallel_combined_clauses = NULL;
18737 /* FALLTHRU */
18738 case OMP_SECTION:
18739 stmt = push_stmt_list ();
18740 RECUR (OMP_BODY (t));
18741 stmt = pop_stmt_list (stmt);
18742
18743 t = copy_node (t);
18744 OMP_BODY (t) = stmt;
18745 add_stmt (t);
18746 break;
18747
18748 case OMP_ATOMIC:
18749 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18750 tmp = NULL_TREE;
18751 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18752 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18753 complain, in_decl);
18754 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18755 {
18756 tree op1 = TREE_OPERAND (t, 1);
18757 tree rhs1 = NULL_TREE;
18758 tree lhs, rhs;
18759 if (TREE_CODE (op1) == COMPOUND_EXPR)
18760 {
18761 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18762 op1 = TREE_OPERAND (op1, 1);
18763 }
18764 lhs = RECUR (TREE_OPERAND (op1, 0));
18765 rhs = RECUR (TREE_OPERAND (op1, 1));
18766 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18767 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18768 OMP_ATOMIC_MEMORY_ORDER (t));
18769 }
18770 else
18771 {
18772 tree op1 = TREE_OPERAND (t, 1);
18773 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18774 tree rhs1 = NULL_TREE;
18775 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18776 enum tree_code opcode = NOP_EXPR;
18777 if (code == OMP_ATOMIC_READ)
18778 {
18779 v = RECUR (TREE_OPERAND (op1, 0));
18780 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18781 }
18782 else if (code == OMP_ATOMIC_CAPTURE_OLD
18783 || code == OMP_ATOMIC_CAPTURE_NEW)
18784 {
18785 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18786 v = RECUR (TREE_OPERAND (op1, 0));
18787 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18788 if (TREE_CODE (op11) == COMPOUND_EXPR)
18789 {
18790 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18791 op11 = TREE_OPERAND (op11, 1);
18792 }
18793 lhs = RECUR (TREE_OPERAND (op11, 0));
18794 rhs = RECUR (TREE_OPERAND (op11, 1));
18795 opcode = TREE_CODE (op11);
18796 if (opcode == MODIFY_EXPR)
18797 opcode = NOP_EXPR;
18798 }
18799 else
18800 {
18801 code = OMP_ATOMIC;
18802 lhs = RECUR (TREE_OPERAND (op1, 0));
18803 rhs = RECUR (TREE_OPERAND (op1, 1));
18804 }
18805 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18806 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18807 }
18808 break;
18809
18810 case TRANSACTION_EXPR:
18811 {
18812 int flags = 0;
18813 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18814 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18815
18816 if (TRANSACTION_EXPR_IS_STMT (t))
18817 {
18818 tree body = TRANSACTION_EXPR_BODY (t);
18819 tree noex = NULL_TREE;
18820 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18821 {
18822 noex = MUST_NOT_THROW_COND (body);
18823 if (noex == NULL_TREE)
18824 noex = boolean_true_node;
18825 body = TREE_OPERAND (body, 0);
18826 }
18827 stmt = begin_transaction_stmt (input_location, NULL, flags);
18828 RECUR (body);
18829 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18830 }
18831 else
18832 {
18833 stmt = build_transaction_expr (EXPR_LOCATION (t),
18834 RECUR (TRANSACTION_EXPR_BODY (t)),
18835 flags, NULL_TREE);
18836 RETURN (stmt);
18837 }
18838 }
18839 break;
18840
18841 case MUST_NOT_THROW_EXPR:
18842 {
18843 tree op0 = RECUR (TREE_OPERAND (t, 0));
18844 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18845 RETURN (build_must_not_throw_expr (op0, cond));
18846 }
18847
18848 case EXPR_PACK_EXPANSION:
18849 error ("invalid use of pack expansion expression");
18850 RETURN (error_mark_node);
18851
18852 case NONTYPE_ARGUMENT_PACK:
18853 error ("use %<...%> to expand argument pack");
18854 RETURN (error_mark_node);
18855
18856 case COMPOUND_EXPR:
18857 tmp = RECUR (TREE_OPERAND (t, 0));
18858 if (tmp == NULL_TREE)
18859 /* If the first operand was a statement, we're done with it. */
18860 RETURN (RECUR (TREE_OPERAND (t, 1)));
18861 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18862 RECUR (TREE_OPERAND (t, 1)),
18863 complain));
18864
18865 case ANNOTATE_EXPR:
18866 tmp = RECUR (TREE_OPERAND (t, 0));
18867 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18868 TREE_TYPE (tmp), tmp,
18869 RECUR (TREE_OPERAND (t, 1)),
18870 RECUR (TREE_OPERAND (t, 2))));
18871
18872 case PREDICT_EXPR:
18873 RETURN (add_stmt (copy_node (t)));
18874
18875 default:
18876 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18877
18878 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18879 /*function_p=*/false,
18880 integral_constant_expression_p));
18881 }
18882
18883 RETURN (NULL_TREE);
18884 out:
18885 input_location = loc;
18886 return r;
18887 #undef RECUR
18888 #undef RETURN
18889 }
18890
18891 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18892 function. For description of the body see comment above
18893 cp_parser_omp_declare_reduction_exprs. */
18894
18895 static void
18896 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18897 {
18898 if (t == NULL_TREE || t == error_mark_node)
18899 return;
18900
18901 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
18902
18903 tree_stmt_iterator tsi;
18904 int i;
18905 tree stmts[7];
18906 memset (stmts, 0, sizeof stmts);
18907 for (i = 0, tsi = tsi_start (t);
18908 i < 7 && !tsi_end_p (tsi);
18909 i++, tsi_next (&tsi))
18910 stmts[i] = tsi_stmt (tsi);
18911 gcc_assert (tsi_end_p (tsi));
18912
18913 if (i >= 3)
18914 {
18915 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18916 && TREE_CODE (stmts[1]) == DECL_EXPR);
18917 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18918 args, complain, in_decl);
18919 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18920 args, complain, in_decl);
18921 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
18922 expect to be pushing it. */
18923 DECL_CONTEXT (omp_out) = current_function_decl;
18924 DECL_CONTEXT (omp_in) = current_function_decl;
18925 keep_next_level (true);
18926 tree block = begin_omp_structured_block ();
18927 tsubst_expr (stmts[2], args, complain, in_decl, false);
18928 block = finish_omp_structured_block (block);
18929 block = maybe_cleanup_point_expr_void (block);
18930 add_decl_expr (omp_out);
18931 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18932 TREE_NO_WARNING (omp_out) = 1;
18933 add_decl_expr (omp_in);
18934 finish_expr_stmt (block);
18935 }
18936 if (i >= 6)
18937 {
18938 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18939 && TREE_CODE (stmts[4]) == DECL_EXPR);
18940 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18941 args, complain, in_decl);
18942 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18943 args, complain, in_decl);
18944 DECL_CONTEXT (omp_priv) = current_function_decl;
18945 DECL_CONTEXT (omp_orig) = current_function_decl;
18946 keep_next_level (true);
18947 tree block = begin_omp_structured_block ();
18948 tsubst_expr (stmts[5], args, complain, in_decl, false);
18949 block = finish_omp_structured_block (block);
18950 block = maybe_cleanup_point_expr_void (block);
18951 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18952 add_decl_expr (omp_priv);
18953 add_decl_expr (omp_orig);
18954 finish_expr_stmt (block);
18955 if (i == 7)
18956 add_decl_expr (omp_orig);
18957 }
18958 }
18959
18960 /* T is a postfix-expression that is not being used in a function
18961 call. Return the substituted version of T. */
18962
18963 static tree
18964 tsubst_non_call_postfix_expression (tree t, tree args,
18965 tsubst_flags_t complain,
18966 tree in_decl)
18967 {
18968 if (TREE_CODE (t) == SCOPE_REF)
18969 t = tsubst_qualified_id (t, args, complain, in_decl,
18970 /*done=*/false, /*address_p=*/false);
18971 else
18972 t = tsubst_copy_and_build (t, args, complain, in_decl,
18973 /*function_p=*/false,
18974 /*integral_constant_expression_p=*/false);
18975
18976 return t;
18977 }
18978
18979 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18980 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18981 dependent init-capture. */
18982
18983 static void
18984 prepend_one_capture (tree field, tree init, tree &list,
18985 tsubst_flags_t complain)
18986 {
18987 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18988 {
18989 tree type = NULL_TREE;
18990 if (!init)
18991 {
18992 if (complain & tf_error)
18993 error ("empty initializer in lambda init-capture");
18994 init = error_mark_node;
18995 }
18996 else if (TREE_CODE (init) == TREE_LIST)
18997 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18998 if (!type)
18999 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19000 TREE_TYPE (field) = type;
19001 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19002 }
19003 list = tree_cons (field, init, list);
19004 }
19005
19006 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19007 instantiation context. Instantiating a pack expansion containing a lambda
19008 might result in multiple lambdas all based on the same lambda in the
19009 template. */
19010
19011 tree
19012 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19013 {
19014 tree oldfn = lambda_function (t);
19015 in_decl = oldfn;
19016
19017 tree r = build_lambda_expr ();
19018
19019 LAMBDA_EXPR_LOCATION (r)
19020 = LAMBDA_EXPR_LOCATION (t);
19021 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19022 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19023 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19024 LAMBDA_EXPR_INSTANTIATED (r) = true;
19025
19026 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19027 /* A lambda in a default argument outside a class gets no
19028 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19029 tsubst_default_argument calls start_lambda_scope, so we need to
19030 specifically ignore it here, and use the global scope. */
19031 record_null_lambda_scope (r);
19032 else
19033 record_lambda_scope (r);
19034
19035 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19036 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19037
19038 vec<tree,va_gc>* field_packs = NULL;
19039
19040 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19041 cap = TREE_CHAIN (cap))
19042 {
19043 tree ofield = TREE_PURPOSE (cap);
19044 tree init = TREE_VALUE (cap);
19045 if (PACK_EXPANSION_P (init))
19046 init = tsubst_pack_expansion (init, args, complain, in_decl);
19047 else
19048 init = tsubst_copy_and_build (init, args, complain, in_decl,
19049 /*fn*/false, /*constexpr*/false);
19050
19051 if (init == error_mark_node)
19052 return error_mark_node;
19053
19054 if (init && TREE_CODE (init) == TREE_LIST)
19055 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19056
19057 if (!processing_template_decl
19058 && init && TREE_CODE (init) != TREE_VEC
19059 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19060 {
19061 /* For a VLA, simply tsubsting the field type won't work, we need to
19062 go through add_capture again. XXX do we want to do this for all
19063 captures? */
19064 tree name = (get_identifier
19065 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19066 tree ftype = TREE_TYPE (ofield);
19067 bool by_ref = (TYPE_REF_P (ftype)
19068 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19069 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19070 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19071 continue;
19072 }
19073
19074 if (PACK_EXPANSION_P (ofield))
19075 ofield = PACK_EXPANSION_PATTERN (ofield);
19076 tree field = tsubst_decl (ofield, args, complain);
19077
19078 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19079 {
19080 /* Remember these for when we've pushed local_specializations. */
19081 vec_safe_push (field_packs, ofield);
19082 vec_safe_push (field_packs, field);
19083 }
19084
19085 if (field == error_mark_node)
19086 return error_mark_node;
19087
19088 if (TREE_CODE (field) == TREE_VEC)
19089 {
19090 int len = TREE_VEC_LENGTH (field);
19091 gcc_assert (TREE_CODE (init) == TREE_VEC
19092 && TREE_VEC_LENGTH (init) == len);
19093 for (int i = 0; i < len; ++i)
19094 prepend_one_capture (TREE_VEC_ELT (field, i),
19095 TREE_VEC_ELT (init, i),
19096 LAMBDA_EXPR_CAPTURE_LIST (r),
19097 complain);
19098 }
19099 else
19100 {
19101 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19102 complain);
19103
19104 if (id_equal (DECL_NAME (field), "__this"))
19105 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19106 }
19107 }
19108
19109 tree type = begin_lambda_type (r);
19110 if (type == error_mark_node)
19111 return error_mark_node;
19112
19113 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19114 determine_visibility (TYPE_NAME (type));
19115
19116 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19117
19118 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19119 ? DECL_TI_TEMPLATE (oldfn)
19120 : NULL_TREE);
19121
19122 tree fntype = static_fn_type (oldfn);
19123 if (oldtmpl)
19124 ++processing_template_decl;
19125 fntype = tsubst (fntype, args, complain, in_decl);
19126 if (oldtmpl)
19127 --processing_template_decl;
19128
19129 if (fntype == error_mark_node)
19130 r = error_mark_node;
19131 else
19132 {
19133 /* The body of a lambda-expression is not a subexpression of the
19134 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19135 which would be skipped if cp_unevaluated_operand. */
19136 cp_evaluated ev;
19137
19138 /* Fix the type of 'this'. */
19139 fntype = build_memfn_type (fntype, type,
19140 type_memfn_quals (fntype),
19141 type_memfn_rqual (fntype));
19142 tree fn, tmpl;
19143 if (oldtmpl)
19144 {
19145 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19146 if (tmpl == error_mark_node)
19147 {
19148 r = error_mark_node;
19149 goto out;
19150 }
19151 fn = DECL_TEMPLATE_RESULT (tmpl);
19152 finish_member_declaration (tmpl);
19153 }
19154 else
19155 {
19156 tmpl = NULL_TREE;
19157 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19158 if (fn == error_mark_node)
19159 {
19160 r = error_mark_node;
19161 goto out;
19162 }
19163 finish_member_declaration (fn);
19164 }
19165
19166 if (tree ci = get_constraints (oldfn))
19167 {
19168 /* Substitute into the lambda's constraints. */
19169 if (oldtmpl)
19170 ++processing_template_decl;
19171 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19172 if (oldtmpl)
19173 --processing_template_decl;
19174 set_constraints (fn, ci);
19175 }
19176
19177 /* Let finish_function set this. */
19178 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19179
19180 bool nested = cfun;
19181 if (nested)
19182 push_function_context ();
19183 else
19184 /* Still increment function_depth so that we don't GC in the
19185 middle of an expression. */
19186 ++function_depth;
19187
19188 local_specialization_stack s (lss_copy);
19189
19190 tree body = start_lambda_function (fn, r);
19191
19192 /* Now record them for lookup_init_capture_pack. */
19193 int fplen = vec_safe_length (field_packs);
19194 for (int i = 0; i < fplen; )
19195 {
19196 tree pack = (*field_packs)[i++];
19197 tree inst = (*field_packs)[i++];
19198 register_local_specialization (inst, pack);
19199 }
19200 release_tree_vector (field_packs);
19201
19202 register_parameter_specializations (oldfn, fn);
19203
19204 if (oldtmpl)
19205 {
19206 /* We might not partially instantiate some parts of the function, so
19207 copy these flags from the original template. */
19208 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19209 current_function_returns_value = ol->returns_value;
19210 current_function_returns_null = ol->returns_null;
19211 current_function_returns_abnormally = ol->returns_abnormally;
19212 current_function_infinite_loop = ol->infinite_loop;
19213 }
19214
19215 /* [temp.deduct] A lambda-expression appearing in a function type or a
19216 template parameter is not considered part of the immediate context for
19217 the purposes of template argument deduction. */
19218 complain = tf_warning_or_error;
19219
19220 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19221 /*constexpr*/false);
19222
19223 finish_lambda_function (body);
19224
19225 if (nested)
19226 pop_function_context ();
19227 else
19228 --function_depth;
19229
19230 /* The capture list was built up in reverse order; fix that now. */
19231 LAMBDA_EXPR_CAPTURE_LIST (r)
19232 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19233
19234 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19235
19236 maybe_add_lambda_conv_op (type);
19237 }
19238
19239 out:
19240 finish_struct (type, /*attr*/NULL_TREE);
19241
19242 insert_pending_capture_proxies ();
19243
19244 return r;
19245 }
19246
19247 /* Like tsubst but deals with expressions and performs semantic
19248 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19249
19250 tree
19251 tsubst_copy_and_build (tree t,
19252 tree args,
19253 tsubst_flags_t complain,
19254 tree in_decl,
19255 bool function_p,
19256 bool integral_constant_expression_p)
19257 {
19258 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19259 #define RECUR(NODE) \
19260 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19261 /*function_p=*/false, \
19262 integral_constant_expression_p)
19263
19264 tree retval, op1;
19265 location_t save_loc;
19266
19267 if (t == NULL_TREE || t == error_mark_node)
19268 return t;
19269
19270 save_loc = input_location;
19271 if (location_t eloc = cp_expr_location (t))
19272 input_location = eloc;
19273
19274 /* N3276 decltype magic only applies to calls at the top level or on the
19275 right side of a comma. */
19276 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19277 complain &= ~tf_decltype;
19278
19279 switch (TREE_CODE (t))
19280 {
19281 case USING_DECL:
19282 t = DECL_NAME (t);
19283 /* Fall through. */
19284 case IDENTIFIER_NODE:
19285 {
19286 tree decl;
19287 cp_id_kind idk;
19288 bool non_integral_constant_expression_p;
19289 const char *error_msg;
19290
19291 if (IDENTIFIER_CONV_OP_P (t))
19292 {
19293 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19294 t = make_conv_op_name (new_type);
19295 }
19296
19297 /* Look up the name. */
19298 decl = lookup_name (t);
19299
19300 /* By convention, expressions use ERROR_MARK_NODE to indicate
19301 failure, not NULL_TREE. */
19302 if (decl == NULL_TREE)
19303 decl = error_mark_node;
19304
19305 decl = finish_id_expression (t, decl, NULL_TREE,
19306 &idk,
19307 integral_constant_expression_p,
19308 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19309 &non_integral_constant_expression_p,
19310 /*template_p=*/false,
19311 /*done=*/true,
19312 /*address_p=*/false,
19313 /*template_arg_p=*/false,
19314 &error_msg,
19315 input_location);
19316 if (error_msg)
19317 error (error_msg);
19318 if (!function_p && identifier_p (decl))
19319 {
19320 if (complain & tf_error)
19321 unqualified_name_lookup_error (decl);
19322 decl = error_mark_node;
19323 }
19324 RETURN (decl);
19325 }
19326
19327 case TEMPLATE_ID_EXPR:
19328 {
19329 tree object;
19330 tree templ = RECUR (TREE_OPERAND (t, 0));
19331 tree targs = TREE_OPERAND (t, 1);
19332
19333 if (targs)
19334 targs = tsubst_template_args (targs, args, complain, in_decl);
19335 if (targs == error_mark_node)
19336 RETURN (error_mark_node);
19337
19338 if (TREE_CODE (templ) == SCOPE_REF)
19339 {
19340 tree name = TREE_OPERAND (templ, 1);
19341 tree tid = lookup_template_function (name, targs);
19342 TREE_OPERAND (templ, 1) = tid;
19343 RETURN (templ);
19344 }
19345
19346 if (concept_definition_p (templ))
19347 {
19348 tree check = build_concept_check (templ, targs, complain);
19349 if (check == error_mark_node)
19350 RETURN (error_mark_node);
19351
19352 tree id = unpack_concept_check (check);
19353
19354 /* If we built a function concept check, return the underlying
19355 template-id. So we can evaluate it as a function call. */
19356 if (function_concept_p (TREE_OPERAND (id, 0)))
19357 RETURN (id);
19358
19359 RETURN (check);
19360 }
19361
19362 if (variable_template_p (templ))
19363 {
19364 tree r = lookup_and_finish_template_variable (templ, targs,
19365 complain);
19366 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19367 RETURN (r);
19368 }
19369
19370 if (TREE_CODE (templ) == COMPONENT_REF)
19371 {
19372 object = TREE_OPERAND (templ, 0);
19373 templ = TREE_OPERAND (templ, 1);
19374 }
19375 else
19376 object = NULL_TREE;
19377 templ = lookup_template_function (templ, targs);
19378
19379 if (object)
19380 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
19381 object, templ, NULL_TREE));
19382 else
19383 RETURN (baselink_for_fns (templ));
19384 }
19385
19386 case INDIRECT_REF:
19387 {
19388 tree r = RECUR (TREE_OPERAND (t, 0));
19389
19390 if (REFERENCE_REF_P (t))
19391 {
19392 /* A type conversion to reference type will be enclosed in
19393 such an indirect ref, but the substitution of the cast
19394 will have also added such an indirect ref. */
19395 r = convert_from_reference (r);
19396 }
19397 else
19398 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19399 complain|decltype_flag);
19400
19401 if (REF_PARENTHESIZED_P (t))
19402 r = force_paren_expr (r);
19403
19404 RETURN (r);
19405 }
19406
19407 case NOP_EXPR:
19408 {
19409 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19410 tree op0 = RECUR (TREE_OPERAND (t, 0));
19411 RETURN (build_nop (type, op0));
19412 }
19413
19414 case IMPLICIT_CONV_EXPR:
19415 {
19416 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19417 tree expr = RECUR (TREE_OPERAND (t, 0));
19418 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19419 {
19420 retval = copy_node (t);
19421 TREE_TYPE (retval) = type;
19422 TREE_OPERAND (retval, 0) = expr;
19423 RETURN (retval);
19424 }
19425 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19426 /* We'll pass this to convert_nontype_argument again, we don't need
19427 to actually perform any conversion here. */
19428 RETURN (expr);
19429 int flags = LOOKUP_IMPLICIT;
19430 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19431 flags = LOOKUP_NORMAL;
19432 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19433 flags |= LOOKUP_NO_NARROWING;
19434 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19435 flags));
19436 }
19437
19438 case CONVERT_EXPR:
19439 {
19440 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19441 tree op0 = RECUR (TREE_OPERAND (t, 0));
19442 if (op0 == error_mark_node)
19443 RETURN (error_mark_node);
19444 RETURN (build1 (CONVERT_EXPR, type, op0));
19445 }
19446
19447 case CAST_EXPR:
19448 case REINTERPRET_CAST_EXPR:
19449 case CONST_CAST_EXPR:
19450 case DYNAMIC_CAST_EXPR:
19451 case STATIC_CAST_EXPR:
19452 {
19453 tree type;
19454 tree op, r = NULL_TREE;
19455
19456 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19457 if (integral_constant_expression_p
19458 && !cast_valid_in_integral_constant_expression_p (type))
19459 {
19460 if (complain & tf_error)
19461 error ("a cast to a type other than an integral or "
19462 "enumeration type cannot appear in a constant-expression");
19463 RETURN (error_mark_node);
19464 }
19465
19466 op = RECUR (TREE_OPERAND (t, 0));
19467
19468 warning_sentinel s(warn_useless_cast);
19469 warning_sentinel s2(warn_ignored_qualifiers);
19470 switch (TREE_CODE (t))
19471 {
19472 case CAST_EXPR:
19473 r = build_functional_cast (input_location, type, op, complain);
19474 break;
19475 case REINTERPRET_CAST_EXPR:
19476 r = build_reinterpret_cast (input_location, type, op, complain);
19477 break;
19478 case CONST_CAST_EXPR:
19479 r = build_const_cast (input_location, type, op, complain);
19480 break;
19481 case DYNAMIC_CAST_EXPR:
19482 r = build_dynamic_cast (input_location, type, op, complain);
19483 break;
19484 case STATIC_CAST_EXPR:
19485 r = build_static_cast (input_location, type, op, complain);
19486 if (IMPLICIT_RVALUE_P (t))
19487 set_implicit_rvalue_p (r);
19488 break;
19489 default:
19490 gcc_unreachable ();
19491 }
19492
19493 RETURN (r);
19494 }
19495
19496 case POSTDECREMENT_EXPR:
19497 case POSTINCREMENT_EXPR:
19498 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19499 args, complain, in_decl);
19500 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19501 complain|decltype_flag));
19502
19503 case PREDECREMENT_EXPR:
19504 case PREINCREMENT_EXPR:
19505 case NEGATE_EXPR:
19506 case BIT_NOT_EXPR:
19507 case ABS_EXPR:
19508 case TRUTH_NOT_EXPR:
19509 case UNARY_PLUS_EXPR: /* Unary + */
19510 case REALPART_EXPR:
19511 case IMAGPART_EXPR:
19512 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19513 RECUR (TREE_OPERAND (t, 0)),
19514 complain|decltype_flag));
19515
19516 case FIX_TRUNC_EXPR:
19517 gcc_unreachable ();
19518
19519 case ADDR_EXPR:
19520 op1 = TREE_OPERAND (t, 0);
19521 if (TREE_CODE (op1) == LABEL_DECL)
19522 RETURN (finish_label_address_expr (DECL_NAME (op1),
19523 EXPR_LOCATION (op1)));
19524 if (TREE_CODE (op1) == SCOPE_REF)
19525 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19526 /*done=*/true, /*address_p=*/true);
19527 else
19528 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19529 in_decl);
19530 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19531 complain|decltype_flag));
19532
19533 case PLUS_EXPR:
19534 case MINUS_EXPR:
19535 case MULT_EXPR:
19536 case TRUNC_DIV_EXPR:
19537 case CEIL_DIV_EXPR:
19538 case FLOOR_DIV_EXPR:
19539 case ROUND_DIV_EXPR:
19540 case EXACT_DIV_EXPR:
19541 case BIT_AND_EXPR:
19542 case BIT_IOR_EXPR:
19543 case BIT_XOR_EXPR:
19544 case TRUNC_MOD_EXPR:
19545 case FLOOR_MOD_EXPR:
19546 case TRUTH_ANDIF_EXPR:
19547 case TRUTH_ORIF_EXPR:
19548 case TRUTH_AND_EXPR:
19549 case TRUTH_OR_EXPR:
19550 case RSHIFT_EXPR:
19551 case LSHIFT_EXPR:
19552 case EQ_EXPR:
19553 case NE_EXPR:
19554 case MAX_EXPR:
19555 case MIN_EXPR:
19556 case LE_EXPR:
19557 case GE_EXPR:
19558 case LT_EXPR:
19559 case GT_EXPR:
19560 case SPACESHIP_EXPR:
19561 case MEMBER_REF:
19562 case DOTSTAR_EXPR:
19563 {
19564 /* If T was type-dependent, suppress warnings that depend on the range
19565 of the types involved. */
19566 bool was_dep = type_dependent_expression_p_push (t);
19567
19568 tree op0 = RECUR (TREE_OPERAND (t, 0));
19569 tree op1 = RECUR (TREE_OPERAND (t, 1));
19570
19571 warning_sentinel s1(warn_type_limits, was_dep);
19572 warning_sentinel s2(warn_div_by_zero, was_dep);
19573 warning_sentinel s3(warn_logical_op, was_dep);
19574 warning_sentinel s4(warn_tautological_compare, was_dep);
19575
19576 tree r = build_x_binary_op
19577 (input_location, TREE_CODE (t),
19578 op0,
19579 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19580 ? ERROR_MARK
19581 : TREE_CODE (TREE_OPERAND (t, 0))),
19582 op1,
19583 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19584 ? ERROR_MARK
19585 : TREE_CODE (TREE_OPERAND (t, 1))),
19586 /*overload=*/NULL,
19587 complain|decltype_flag);
19588 if (EXPR_P (r) && TREE_NO_WARNING (t))
19589 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19590
19591 RETURN (r);
19592 }
19593
19594 case POINTER_PLUS_EXPR:
19595 {
19596 tree op0 = RECUR (TREE_OPERAND (t, 0));
19597 if (op0 == error_mark_node)
19598 RETURN (error_mark_node);
19599 tree op1 = RECUR (TREE_OPERAND (t, 1));
19600 if (op1 == error_mark_node)
19601 RETURN (error_mark_node);
19602 RETURN (fold_build_pointer_plus (op0, op1));
19603 }
19604
19605 case SCOPE_REF:
19606 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19607 /*address_p=*/false));
19608 case ARRAY_REF:
19609 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19610 args, complain, in_decl);
19611 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19612 RECUR (TREE_OPERAND (t, 1)),
19613 complain|decltype_flag));
19614
19615 case SIZEOF_EXPR:
19616 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19617 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19618 RETURN (tsubst_copy (t, args, complain, in_decl));
19619 /* Fall through */
19620
19621 case ALIGNOF_EXPR:
19622 {
19623 tree r;
19624
19625 op1 = TREE_OPERAND (t, 0);
19626 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19627 op1 = TREE_TYPE (op1);
19628 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19629 && ALIGNOF_EXPR_STD_P (t));
19630 if (!args)
19631 {
19632 /* When there are no ARGS, we are trying to evaluate a
19633 non-dependent expression from the parser. Trying to do
19634 the substitutions may not work. */
19635 if (!TYPE_P (op1))
19636 op1 = TREE_TYPE (op1);
19637 }
19638 else
19639 {
19640 ++cp_unevaluated_operand;
19641 ++c_inhibit_evaluation_warnings;
19642 if (TYPE_P (op1))
19643 op1 = tsubst (op1, args, complain, in_decl);
19644 else
19645 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19646 /*function_p=*/false,
19647 /*integral_constant_expression_p=*/
19648 false);
19649 --cp_unevaluated_operand;
19650 --c_inhibit_evaluation_warnings;
19651 }
19652 if (TYPE_P (op1))
19653 r = cxx_sizeof_or_alignof_type (input_location,
19654 op1, TREE_CODE (t), std_alignof,
19655 complain & tf_error);
19656 else
19657 r = cxx_sizeof_or_alignof_expr (input_location,
19658 op1, TREE_CODE (t),
19659 complain & tf_error);
19660 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19661 {
19662 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19663 {
19664 if (!processing_template_decl && TYPE_P (op1))
19665 {
19666 r = build_min (SIZEOF_EXPR, size_type_node,
19667 build1 (NOP_EXPR, op1, error_mark_node));
19668 SIZEOF_EXPR_TYPE_P (r) = 1;
19669 }
19670 else
19671 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19672 TREE_SIDE_EFFECTS (r) = 0;
19673 TREE_READONLY (r) = 1;
19674 }
19675 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19676 }
19677 RETURN (r);
19678 }
19679
19680 case AT_ENCODE_EXPR:
19681 {
19682 op1 = TREE_OPERAND (t, 0);
19683 ++cp_unevaluated_operand;
19684 ++c_inhibit_evaluation_warnings;
19685 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19686 /*function_p=*/false,
19687 /*integral_constant_expression_p=*/false);
19688 --cp_unevaluated_operand;
19689 --c_inhibit_evaluation_warnings;
19690 RETURN (objc_build_encode_expr (op1));
19691 }
19692
19693 case NOEXCEPT_EXPR:
19694 op1 = TREE_OPERAND (t, 0);
19695 ++cp_unevaluated_operand;
19696 ++c_inhibit_evaluation_warnings;
19697 ++cp_noexcept_operand;
19698 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19699 /*function_p=*/false,
19700 /*integral_constant_expression_p=*/false);
19701 --cp_unevaluated_operand;
19702 --c_inhibit_evaluation_warnings;
19703 --cp_noexcept_operand;
19704 RETURN (finish_noexcept_expr (op1, complain));
19705
19706 case MODOP_EXPR:
19707 {
19708 warning_sentinel s(warn_div_by_zero);
19709 tree lhs = RECUR (TREE_OPERAND (t, 0));
19710 tree rhs = RECUR (TREE_OPERAND (t, 2));
19711 tree r = build_x_modify_expr
19712 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19713 complain|decltype_flag);
19714 /* TREE_NO_WARNING must be set if either the expression was
19715 parenthesized or it uses an operator such as >>= rather
19716 than plain assignment. In the former case, it was already
19717 set and must be copied. In the latter case,
19718 build_x_modify_expr sets it and it must not be reset
19719 here. */
19720 if (TREE_NO_WARNING (t))
19721 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19722
19723 RETURN (r);
19724 }
19725
19726 case ARROW_EXPR:
19727 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19728 args, complain, in_decl);
19729 /* Remember that there was a reference to this entity. */
19730 if (DECL_P (op1)
19731 && !mark_used (op1, complain) && !(complain & tf_error))
19732 RETURN (error_mark_node);
19733 RETURN (build_x_arrow (input_location, op1, complain));
19734
19735 case NEW_EXPR:
19736 {
19737 tree placement = RECUR (TREE_OPERAND (t, 0));
19738 tree init = RECUR (TREE_OPERAND (t, 3));
19739 vec<tree, va_gc> *placement_vec;
19740 vec<tree, va_gc> *init_vec;
19741 tree ret;
19742 location_t loc = EXPR_LOCATION (t);
19743
19744 if (placement == NULL_TREE)
19745 placement_vec = NULL;
19746 else if (placement == error_mark_node)
19747 RETURN (error_mark_node);
19748 else
19749 {
19750 placement_vec = make_tree_vector ();
19751 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19752 vec_safe_push (placement_vec, TREE_VALUE (placement));
19753 }
19754
19755 /* If there was an initializer in the original tree, but it
19756 instantiated to an empty list, then we should pass a
19757 non-NULL empty vector to tell build_new that it was an
19758 empty initializer() rather than no initializer. This can
19759 only happen when the initializer is a pack expansion whose
19760 parameter packs are of length zero. */
19761 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19762 init_vec = NULL;
19763 else
19764 {
19765 init_vec = make_tree_vector ();
19766 if (init == void_node)
19767 gcc_assert (init_vec != NULL);
19768 else
19769 {
19770 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19771 vec_safe_push (init_vec, TREE_VALUE (init));
19772 }
19773 }
19774
19775 /* Avoid passing an enclosing decl to valid_array_size_p. */
19776 in_decl = NULL_TREE;
19777
19778 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19779 tree op2 = RECUR (TREE_OPERAND (t, 2));
19780 ret = build_new (loc, &placement_vec, op1, op2,
19781 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19782 complain);
19783
19784 if (placement_vec != NULL)
19785 release_tree_vector (placement_vec);
19786 if (init_vec != NULL)
19787 release_tree_vector (init_vec);
19788
19789 RETURN (ret);
19790 }
19791
19792 case DELETE_EXPR:
19793 {
19794 tree op0 = RECUR (TREE_OPERAND (t, 0));
19795 tree op1 = RECUR (TREE_OPERAND (t, 1));
19796 RETURN (delete_sanity (input_location, op0, op1,
19797 DELETE_EXPR_USE_VEC (t),
19798 DELETE_EXPR_USE_GLOBAL (t),
19799 complain));
19800 }
19801
19802 case COMPOUND_EXPR:
19803 {
19804 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19805 complain & ~tf_decltype, in_decl,
19806 /*function_p=*/false,
19807 integral_constant_expression_p);
19808 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19809 op0,
19810 RECUR (TREE_OPERAND (t, 1)),
19811 complain|decltype_flag));
19812 }
19813
19814 case CALL_EXPR:
19815 {
19816 tree function;
19817 unsigned int nargs, i;
19818 bool qualified_p;
19819 bool koenig_p;
19820 tree ret;
19821
19822 function = CALL_EXPR_FN (t);
19823 /* Internal function with no arguments. */
19824 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19825 RETURN (t);
19826
19827 /* When we parsed the expression, we determined whether or
19828 not Koenig lookup should be performed. */
19829 koenig_p = KOENIG_LOOKUP_P (t);
19830 if (function == NULL_TREE)
19831 {
19832 koenig_p = false;
19833 qualified_p = false;
19834 }
19835 else if (TREE_CODE (function) == SCOPE_REF)
19836 {
19837 qualified_p = true;
19838 function = tsubst_qualified_id (function, args, complain, in_decl,
19839 /*done=*/false,
19840 /*address_p=*/false);
19841 }
19842 else if (koenig_p && identifier_p (function))
19843 {
19844 /* Do nothing; calling tsubst_copy_and_build on an identifier
19845 would incorrectly perform unqualified lookup again.
19846
19847 Note that we can also have an IDENTIFIER_NODE if the earlier
19848 unqualified lookup found a member function; in that case
19849 koenig_p will be false and we do want to do the lookup
19850 again to find the instantiated member function.
19851
19852 FIXME but doing that causes c++/15272, so we need to stop
19853 using IDENTIFIER_NODE in that situation. */
19854 qualified_p = false;
19855 }
19856 else
19857 {
19858 if (TREE_CODE (function) == COMPONENT_REF)
19859 {
19860 tree op = TREE_OPERAND (function, 1);
19861
19862 qualified_p = (TREE_CODE (op) == SCOPE_REF
19863 || (BASELINK_P (op)
19864 && BASELINK_QUALIFIED_P (op)));
19865 }
19866 else
19867 qualified_p = false;
19868
19869 if (TREE_CODE (function) == ADDR_EXPR
19870 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19871 /* Avoid error about taking the address of a constructor. */
19872 function = TREE_OPERAND (function, 0);
19873
19874 function = tsubst_copy_and_build (function, args, complain,
19875 in_decl,
19876 !qualified_p,
19877 integral_constant_expression_p);
19878
19879 if (BASELINK_P (function))
19880 qualified_p = true;
19881 }
19882
19883 nargs = call_expr_nargs (t);
19884 releasing_vec call_args;
19885 for (i = 0; i < nargs; ++i)
19886 {
19887 tree arg = CALL_EXPR_ARG (t, i);
19888
19889 if (!PACK_EXPANSION_P (arg))
19890 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19891 else
19892 {
19893 /* Expand the pack expansion and push each entry onto
19894 CALL_ARGS. */
19895 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19896 if (TREE_CODE (arg) == TREE_VEC)
19897 {
19898 unsigned int len, j;
19899
19900 len = TREE_VEC_LENGTH (arg);
19901 for (j = 0; j < len; ++j)
19902 {
19903 tree value = TREE_VEC_ELT (arg, j);
19904 if (value != NULL_TREE)
19905 value = convert_from_reference (value);
19906 vec_safe_push (call_args, value);
19907 }
19908 }
19909 else
19910 {
19911 /* A partial substitution. Add one entry. */
19912 vec_safe_push (call_args, arg);
19913 }
19914 }
19915 }
19916
19917 /* Stripped-down processing for a call in a thunk. Specifically, in
19918 the thunk template for a generic lambda. */
19919 if (CALL_FROM_THUNK_P (t))
19920 {
19921 /* Now that we've expanded any packs, the number of call args
19922 might be different. */
19923 unsigned int cargs = call_args->length ();
19924 tree thisarg = NULL_TREE;
19925 if (TREE_CODE (function) == COMPONENT_REF)
19926 {
19927 thisarg = TREE_OPERAND (function, 0);
19928 if (TREE_CODE (thisarg) == INDIRECT_REF)
19929 thisarg = TREE_OPERAND (thisarg, 0);
19930 function = TREE_OPERAND (function, 1);
19931 if (TREE_CODE (function) == BASELINK)
19932 function = BASELINK_FUNCTIONS (function);
19933 }
19934 /* We aren't going to do normal overload resolution, so force the
19935 template-id to resolve. */
19936 function = resolve_nondeduced_context (function, complain);
19937 for (unsigned i = 0; i < cargs; ++i)
19938 {
19939 /* In a thunk, pass through args directly, without any
19940 conversions. */
19941 tree arg = (*call_args)[i];
19942 while (TREE_CODE (arg) != PARM_DECL)
19943 arg = TREE_OPERAND (arg, 0);
19944 (*call_args)[i] = arg;
19945 }
19946 if (thisarg)
19947 {
19948 /* If there are no other args, just push 'this'. */
19949 if (cargs == 0)
19950 vec_safe_push (call_args, thisarg);
19951 else
19952 {
19953 /* Otherwise, shift the other args over to make room. */
19954 tree last = (*call_args)[cargs - 1];
19955 vec_safe_push (call_args, last);
19956 for (int i = cargs - 1; i > 0; --i)
19957 (*call_args)[i] = (*call_args)[i - 1];
19958 (*call_args)[0] = thisarg;
19959 }
19960 }
19961 ret = build_call_a (function, call_args->length (),
19962 call_args->address ());
19963 /* The thunk location is not interesting. */
19964 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19965 CALL_FROM_THUNK_P (ret) = true;
19966 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19967 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19968
19969 RETURN (ret);
19970 }
19971
19972 /* We do not perform argument-dependent lookup if normal
19973 lookup finds a non-function, in accordance with the
19974 expected resolution of DR 218. */
19975 if (koenig_p
19976 && ((is_overloaded_fn (function)
19977 /* If lookup found a member function, the Koenig lookup is
19978 not appropriate, even if an unqualified-name was used
19979 to denote the function. */
19980 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19981 || identifier_p (function))
19982 /* Only do this when substitution turns a dependent call
19983 into a non-dependent call. */
19984 && type_dependent_expression_p_push (t)
19985 && !any_type_dependent_arguments_p (call_args))
19986 function = perform_koenig_lookup (function, call_args, tf_none);
19987
19988 if (function != NULL_TREE
19989 && identifier_p (function)
19990 && !any_type_dependent_arguments_p (call_args))
19991 {
19992 if (koenig_p && (complain & tf_warning_or_error))
19993 {
19994 /* For backwards compatibility and good diagnostics, try
19995 the unqualified lookup again if we aren't in SFINAE
19996 context. */
19997 tree unq = (tsubst_copy_and_build
19998 (function, args, complain, in_decl, true,
19999 integral_constant_expression_p));
20000 if (unq == error_mark_node)
20001 RETURN (error_mark_node);
20002
20003 if (unq != function)
20004 {
20005 /* In a lambda fn, we have to be careful to not
20006 introduce new this captures. Legacy code can't
20007 be using lambdas anyway, so it's ok to be
20008 stricter. */
20009 bool in_lambda = (current_class_type
20010 && LAMBDA_TYPE_P (current_class_type));
20011 char const *const msg
20012 = G_("%qD was not declared in this scope, "
20013 "and no declarations were found by "
20014 "argument-dependent lookup at the point "
20015 "of instantiation");
20016
20017 bool diag = true;
20018 if (in_lambda)
20019 error_at (cp_expr_loc_or_input_loc (t),
20020 msg, function);
20021 else
20022 diag = permerror (cp_expr_loc_or_input_loc (t),
20023 msg, function);
20024 if (diag)
20025 {
20026 tree fn = unq;
20027
20028 if (INDIRECT_REF_P (fn))
20029 fn = TREE_OPERAND (fn, 0);
20030 if (is_overloaded_fn (fn))
20031 fn = get_first_fn (fn);
20032
20033 if (!DECL_P (fn))
20034 /* Can't say anything more. */;
20035 else if (DECL_CLASS_SCOPE_P (fn))
20036 {
20037 location_t loc = cp_expr_loc_or_input_loc (t);
20038 inform (loc,
20039 "declarations in dependent base %qT are "
20040 "not found by unqualified lookup",
20041 DECL_CLASS_CONTEXT (fn));
20042 if (current_class_ptr)
20043 inform (loc,
20044 "use %<this->%D%> instead", function);
20045 else
20046 inform (loc,
20047 "use %<%T::%D%> instead",
20048 current_class_name, function);
20049 }
20050 else
20051 inform (DECL_SOURCE_LOCATION (fn),
20052 "%qD declared here, later in the "
20053 "translation unit", fn);
20054 if (in_lambda)
20055 RETURN (error_mark_node);
20056 }
20057
20058 function = unq;
20059 }
20060 }
20061 if (identifier_p (function))
20062 {
20063 if (complain & tf_error)
20064 unqualified_name_lookup_error (function);
20065 RETURN (error_mark_node);
20066 }
20067 }
20068
20069 /* Remember that there was a reference to this entity. */
20070 if (function != NULL_TREE
20071 && DECL_P (function)
20072 && !mark_used (function, complain) && !(complain & tf_error))
20073 RETURN (error_mark_node);
20074
20075 /* Put back tf_decltype for the actual call. */
20076 complain |= decltype_flag;
20077
20078 if (function == NULL_TREE)
20079 switch (CALL_EXPR_IFN (t))
20080 {
20081 case IFN_LAUNDER:
20082 gcc_assert (nargs == 1);
20083 if (vec_safe_length (call_args) != 1)
20084 {
20085 error_at (cp_expr_loc_or_input_loc (t),
20086 "wrong number of arguments to "
20087 "%<__builtin_launder%>");
20088 ret = error_mark_node;
20089 }
20090 else
20091 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20092 (*call_args)[0], complain);
20093 break;
20094
20095 case IFN_VEC_CONVERT:
20096 gcc_assert (nargs == 1);
20097 if (vec_safe_length (call_args) != 1)
20098 {
20099 error_at (cp_expr_loc_or_input_loc (t),
20100 "wrong number of arguments to "
20101 "%<__builtin_convertvector%>");
20102 ret = error_mark_node;
20103 break;
20104 }
20105 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20106 tsubst (TREE_TYPE (t), args,
20107 complain, in_decl),
20108 complain);
20109 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20110 RETURN (ret);
20111 break;
20112
20113 default:
20114 /* Unsupported internal function with arguments. */
20115 gcc_unreachable ();
20116 }
20117 else if (TREE_CODE (function) == OFFSET_REF
20118 || TREE_CODE (function) == DOTSTAR_EXPR
20119 || TREE_CODE (function) == MEMBER_REF)
20120 ret = build_offset_ref_call_from_tree (function, &call_args,
20121 complain);
20122 else if (TREE_CODE (function) == COMPONENT_REF)
20123 {
20124 tree instance = TREE_OPERAND (function, 0);
20125 tree fn = TREE_OPERAND (function, 1);
20126
20127 if (processing_template_decl
20128 && (type_dependent_expression_p (instance)
20129 || (!BASELINK_P (fn)
20130 && TREE_CODE (fn) != FIELD_DECL)
20131 || type_dependent_expression_p (fn)
20132 || any_type_dependent_arguments_p (call_args)))
20133 ret = build_min_nt_call_vec (function, call_args);
20134 else if (!BASELINK_P (fn))
20135 ret = finish_call_expr (function, &call_args,
20136 /*disallow_virtual=*/false,
20137 /*koenig_p=*/false,
20138 complain);
20139 else
20140 ret = (build_new_method_call
20141 (instance, fn,
20142 &call_args, NULL_TREE,
20143 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20144 /*fn_p=*/NULL,
20145 complain));
20146 }
20147 else if (concept_check_p (function))
20148 {
20149 /* FUNCTION is a template-id referring to a concept definition. */
20150 tree id = unpack_concept_check (function);
20151 tree tmpl = TREE_OPERAND (id, 0);
20152 tree args = TREE_OPERAND (id, 1);
20153
20154 /* Calls to standard and variable concepts should have been
20155 previously diagnosed. */
20156 gcc_assert (function_concept_p (tmpl));
20157
20158 /* Ensure the result is wrapped as a call expression. */
20159 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20160 }
20161 else
20162 ret = finish_call_expr (function, &call_args,
20163 /*disallow_virtual=*/qualified_p,
20164 koenig_p,
20165 complain);
20166
20167 if (ret != error_mark_node)
20168 {
20169 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20170 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20171 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20172 if (op || ord || rev)
20173 {
20174 function = extract_call_expr (ret);
20175 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20176 CALL_EXPR_ORDERED_ARGS (function) = ord;
20177 CALL_EXPR_REVERSE_ARGS (function) = rev;
20178 }
20179 }
20180
20181 RETURN (ret);
20182 }
20183
20184 case COND_EXPR:
20185 {
20186 tree cond = RECUR (TREE_OPERAND (t, 0));
20187 cond = mark_rvalue_use (cond);
20188 tree folded_cond = fold_non_dependent_expr (cond, complain);
20189 tree exp1, exp2;
20190
20191 if (TREE_CODE (folded_cond) == INTEGER_CST)
20192 {
20193 if (integer_zerop (folded_cond))
20194 {
20195 ++c_inhibit_evaluation_warnings;
20196 exp1 = RECUR (TREE_OPERAND (t, 1));
20197 --c_inhibit_evaluation_warnings;
20198 exp2 = RECUR (TREE_OPERAND (t, 2));
20199 }
20200 else
20201 {
20202 exp1 = RECUR (TREE_OPERAND (t, 1));
20203 ++c_inhibit_evaluation_warnings;
20204 exp2 = RECUR (TREE_OPERAND (t, 2));
20205 --c_inhibit_evaluation_warnings;
20206 }
20207 cond = folded_cond;
20208 }
20209 else
20210 {
20211 exp1 = RECUR (TREE_OPERAND (t, 1));
20212 exp2 = RECUR (TREE_OPERAND (t, 2));
20213 }
20214
20215 warning_sentinel s(warn_duplicated_branches);
20216 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20217 cond, exp1, exp2, complain));
20218 }
20219
20220 case PSEUDO_DTOR_EXPR:
20221 {
20222 tree op0 = RECUR (TREE_OPERAND (t, 0));
20223 tree op1 = RECUR (TREE_OPERAND (t, 1));
20224 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20225 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20226 input_location));
20227 }
20228
20229 case TREE_LIST:
20230 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20231
20232 case COMPONENT_REF:
20233 {
20234 tree object;
20235 tree object_type;
20236 tree member;
20237 tree r;
20238
20239 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20240 args, complain, in_decl);
20241 /* Remember that there was a reference to this entity. */
20242 if (DECL_P (object)
20243 && !mark_used (object, complain) && !(complain & tf_error))
20244 RETURN (error_mark_node);
20245 object_type = TREE_TYPE (object);
20246
20247 member = TREE_OPERAND (t, 1);
20248 if (BASELINK_P (member))
20249 member = tsubst_baselink (member,
20250 non_reference (TREE_TYPE (object)),
20251 args, complain, in_decl);
20252 else
20253 member = tsubst_copy (member, args, complain, in_decl);
20254 if (member == error_mark_node)
20255 RETURN (error_mark_node);
20256
20257 if (TREE_CODE (member) == FIELD_DECL)
20258 {
20259 r = finish_non_static_data_member (member, object, NULL_TREE);
20260 if (TREE_CODE (r) == COMPONENT_REF)
20261 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20262 RETURN (r);
20263 }
20264 else if (type_dependent_expression_p (object))
20265 /* We can't do much here. */;
20266 else if (!CLASS_TYPE_P (object_type))
20267 {
20268 if (scalarish_type_p (object_type))
20269 {
20270 tree s = NULL_TREE;
20271 tree dtor = member;
20272
20273 if (TREE_CODE (dtor) == SCOPE_REF)
20274 {
20275 s = TREE_OPERAND (dtor, 0);
20276 dtor = TREE_OPERAND (dtor, 1);
20277 }
20278 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20279 {
20280 dtor = TREE_OPERAND (dtor, 0);
20281 if (TYPE_P (dtor))
20282 RETURN (finish_pseudo_destructor_expr
20283 (object, s, dtor, input_location));
20284 }
20285 }
20286 }
20287 else if (TREE_CODE (member) == SCOPE_REF
20288 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20289 {
20290 /* Lookup the template functions now that we know what the
20291 scope is. */
20292 tree scope = TREE_OPERAND (member, 0);
20293 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20294 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20295 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20296 /*complain=*/false);
20297 if (BASELINK_P (member))
20298 {
20299 BASELINK_FUNCTIONS (member)
20300 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20301 args);
20302 member = (adjust_result_of_qualified_name_lookup
20303 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20304 object_type));
20305 }
20306 else
20307 {
20308 qualified_name_lookup_error (scope, tmpl, member,
20309 input_location);
20310 RETURN (error_mark_node);
20311 }
20312 }
20313 else if (TREE_CODE (member) == SCOPE_REF
20314 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20315 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20316 {
20317 if (complain & tf_error)
20318 {
20319 if (TYPE_P (TREE_OPERAND (member, 0)))
20320 error ("%qT is not a class or namespace",
20321 TREE_OPERAND (member, 0));
20322 else
20323 error ("%qD is not a class or namespace",
20324 TREE_OPERAND (member, 0));
20325 }
20326 RETURN (error_mark_node);
20327 }
20328
20329 r = finish_class_member_access_expr (object, member,
20330 /*template_p=*/false,
20331 complain);
20332 if (TREE_CODE (r) == COMPONENT_REF)
20333 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20334 RETURN (r);
20335 }
20336
20337 case THROW_EXPR:
20338 RETURN (build_throw
20339 (input_location, RECUR (TREE_OPERAND (t, 0))));
20340
20341 case CONSTRUCTOR:
20342 {
20343 vec<constructor_elt, va_gc> *n;
20344 constructor_elt *ce;
20345 unsigned HOST_WIDE_INT idx;
20346 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20347 bool process_index_p;
20348 int newlen;
20349 bool need_copy_p = false;
20350 tree r;
20351
20352 if (type == error_mark_node)
20353 RETURN (error_mark_node);
20354
20355 /* We do not want to process the index of aggregate
20356 initializers as they are identifier nodes which will be
20357 looked up by digest_init. */
20358 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20359
20360 if (null_member_pointer_value_p (t))
20361 {
20362 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20363 RETURN (t);
20364 }
20365
20366 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20367 newlen = vec_safe_length (n);
20368 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20369 {
20370 if (ce->index && process_index_p
20371 /* An identifier index is looked up in the type
20372 being initialized, not the current scope. */
20373 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20374 ce->index = RECUR (ce->index);
20375
20376 if (PACK_EXPANSION_P (ce->value))
20377 {
20378 /* Substitute into the pack expansion. */
20379 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20380 in_decl);
20381
20382 if (ce->value == error_mark_node
20383 || PACK_EXPANSION_P (ce->value))
20384 ;
20385 else if (TREE_VEC_LENGTH (ce->value) == 1)
20386 /* Just move the argument into place. */
20387 ce->value = TREE_VEC_ELT (ce->value, 0);
20388 else
20389 {
20390 /* Update the length of the final CONSTRUCTOR
20391 arguments vector, and note that we will need to
20392 copy.*/
20393 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20394 need_copy_p = true;
20395 }
20396 }
20397 else
20398 ce->value = RECUR (ce->value);
20399 }
20400
20401 if (need_copy_p)
20402 {
20403 vec<constructor_elt, va_gc> *old_n = n;
20404
20405 vec_alloc (n, newlen);
20406 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20407 {
20408 if (TREE_CODE (ce->value) == TREE_VEC)
20409 {
20410 int i, len = TREE_VEC_LENGTH (ce->value);
20411 for (i = 0; i < len; ++i)
20412 CONSTRUCTOR_APPEND_ELT (n, 0,
20413 TREE_VEC_ELT (ce->value, i));
20414 }
20415 else
20416 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20417 }
20418 }
20419
20420 r = build_constructor (init_list_type_node, n);
20421 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20422 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20423 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20424
20425 if (TREE_HAS_CONSTRUCTOR (t))
20426 {
20427 fcl_t cl = fcl_functional;
20428 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20429 cl = fcl_c99;
20430 RETURN (finish_compound_literal (type, r, complain, cl));
20431 }
20432
20433 TREE_TYPE (r) = type;
20434 RETURN (r);
20435 }
20436
20437 case TYPEID_EXPR:
20438 {
20439 tree operand_0 = TREE_OPERAND (t, 0);
20440 if (TYPE_P (operand_0))
20441 {
20442 operand_0 = tsubst (operand_0, args, complain, in_decl);
20443 RETURN (get_typeid (operand_0, complain));
20444 }
20445 else
20446 {
20447 operand_0 = RECUR (operand_0);
20448 RETURN (build_typeid (operand_0, complain));
20449 }
20450 }
20451
20452 case VAR_DECL:
20453 if (!args)
20454 RETURN (t);
20455 /* Fall through */
20456
20457 case PARM_DECL:
20458 {
20459 tree r = tsubst_copy (t, args, complain, in_decl);
20460 /* ??? We're doing a subset of finish_id_expression here. */
20461 if (tree wrap = maybe_get_tls_wrapper_call (r))
20462 /* Replace an evaluated use of the thread_local variable with
20463 a call to its wrapper. */
20464 r = wrap;
20465 else if (outer_automatic_var_p (r))
20466 r = process_outer_var_ref (r, complain);
20467
20468 if (!TYPE_REF_P (TREE_TYPE (t)))
20469 /* If the original type was a reference, we'll be wrapped in
20470 the appropriate INDIRECT_REF. */
20471 r = convert_from_reference (r);
20472 RETURN (r);
20473 }
20474
20475 case VA_ARG_EXPR:
20476 {
20477 tree op0 = RECUR (TREE_OPERAND (t, 0));
20478 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20479 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20480 }
20481
20482 case OFFSETOF_EXPR:
20483 {
20484 tree object_ptr
20485 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20486 in_decl, /*function_p=*/false,
20487 /*integral_constant_expression_p=*/false);
20488 RETURN (finish_offsetof (object_ptr,
20489 RECUR (TREE_OPERAND (t, 0)),
20490 EXPR_LOCATION (t)));
20491 }
20492
20493 case ADDRESSOF_EXPR:
20494 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20495 RECUR (TREE_OPERAND (t, 0)), complain));
20496
20497 case TRAIT_EXPR:
20498 {
20499 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20500 complain, in_decl);
20501 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20502 complain, in_decl);
20503 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20504 TRAIT_EXPR_KIND (t), type1, type2));
20505 }
20506
20507 case STMT_EXPR:
20508 {
20509 tree old_stmt_expr = cur_stmt_expr;
20510 tree stmt_expr = begin_stmt_expr ();
20511
20512 cur_stmt_expr = stmt_expr;
20513 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20514 integral_constant_expression_p);
20515 stmt_expr = finish_stmt_expr (stmt_expr, false);
20516 cur_stmt_expr = old_stmt_expr;
20517
20518 /* If the resulting list of expression statement is empty,
20519 fold it further into void_node. */
20520 if (empty_expr_stmt_p (stmt_expr))
20521 stmt_expr = void_node;
20522
20523 RETURN (stmt_expr);
20524 }
20525
20526 case LAMBDA_EXPR:
20527 {
20528 if (complain & tf_partial)
20529 {
20530 /* We don't have a full set of template arguments yet; don't touch
20531 the lambda at all. */
20532 gcc_assert (processing_template_decl);
20533 return t;
20534 }
20535 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20536
20537 RETURN (build_lambda_object (r));
20538 }
20539
20540 case TARGET_EXPR:
20541 /* We can get here for a constant initializer of non-dependent type.
20542 FIXME stop folding in cp_parser_initializer_clause. */
20543 {
20544 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20545 complain);
20546 RETURN (r);
20547 }
20548
20549 case TRANSACTION_EXPR:
20550 RETURN (tsubst_expr(t, args, complain, in_decl,
20551 integral_constant_expression_p));
20552
20553 case PAREN_EXPR:
20554 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20555
20556 case VEC_PERM_EXPR:
20557 {
20558 tree op0 = RECUR (TREE_OPERAND (t, 0));
20559 tree op1 = RECUR (TREE_OPERAND (t, 1));
20560 tree op2 = RECUR (TREE_OPERAND (t, 2));
20561 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20562 complain));
20563 }
20564
20565 case REQUIRES_EXPR:
20566 {
20567 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20568 RETURN (r);
20569 }
20570
20571 case RANGE_EXPR:
20572 /* No need to substitute further, a RANGE_EXPR will always be built
20573 with constant operands. */
20574 RETURN (t);
20575
20576 case NON_LVALUE_EXPR:
20577 case VIEW_CONVERT_EXPR:
20578 if (location_wrapper_p (t))
20579 /* We need to do this here as well as in tsubst_copy so we get the
20580 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20581 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20582 EXPR_LOCATION (t)));
20583 /* fallthrough. */
20584
20585 default:
20586 /* Handle Objective-C++ constructs, if appropriate. */
20587 {
20588 tree subst
20589 = objcp_tsubst_copy_and_build (t, args, complain,
20590 in_decl, /*function_p=*/false);
20591 if (subst)
20592 RETURN (subst);
20593 }
20594 RETURN (tsubst_copy (t, args, complain, in_decl));
20595 }
20596
20597 #undef RECUR
20598 #undef RETURN
20599 out:
20600 input_location = save_loc;
20601 return retval;
20602 }
20603
20604 /* Verify that the instantiated ARGS are valid. For type arguments,
20605 make sure that the type's linkage is ok. For non-type arguments,
20606 make sure they are constants if they are integral or enumerations.
20607 Emit an error under control of COMPLAIN, and return TRUE on error. */
20608
20609 static bool
20610 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20611 {
20612 if (dependent_template_arg_p (t))
20613 return false;
20614 if (ARGUMENT_PACK_P (t))
20615 {
20616 tree vec = ARGUMENT_PACK_ARGS (t);
20617 int len = TREE_VEC_LENGTH (vec);
20618 bool result = false;
20619 int i;
20620
20621 for (i = 0; i < len; ++i)
20622 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20623 result = true;
20624 return result;
20625 }
20626 else if (TYPE_P (t))
20627 {
20628 /* [basic.link]: A name with no linkage (notably, the name
20629 of a class or enumeration declared in a local scope)
20630 shall not be used to declare an entity with linkage.
20631 This implies that names with no linkage cannot be used as
20632 template arguments
20633
20634 DR 757 relaxes this restriction for C++0x. */
20635 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20636 : no_linkage_check (t, /*relaxed_p=*/false));
20637
20638 if (nt)
20639 {
20640 /* DR 488 makes use of a type with no linkage cause
20641 type deduction to fail. */
20642 if (complain & tf_error)
20643 {
20644 if (TYPE_UNNAMED_P (nt))
20645 error ("%qT is/uses unnamed type", t);
20646 else
20647 error ("template argument for %qD uses local type %qT",
20648 tmpl, t);
20649 }
20650 return true;
20651 }
20652 /* In order to avoid all sorts of complications, we do not
20653 allow variably-modified types as template arguments. */
20654 else if (variably_modified_type_p (t, NULL_TREE))
20655 {
20656 if (complain & tf_error)
20657 error ("%qT is a variably modified type", t);
20658 return true;
20659 }
20660 }
20661 /* Class template and alias template arguments should be OK. */
20662 else if (DECL_TYPE_TEMPLATE_P (t))
20663 ;
20664 /* A non-type argument of integral or enumerated type must be a
20665 constant. */
20666 else if (TREE_TYPE (t)
20667 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20668 && !REFERENCE_REF_P (t)
20669 && !TREE_CONSTANT (t))
20670 {
20671 if (complain & tf_error)
20672 error ("integral expression %qE is not constant", t);
20673 return true;
20674 }
20675 return false;
20676 }
20677
20678 static bool
20679 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20680 {
20681 int ix, len = DECL_NTPARMS (tmpl);
20682 bool result = false;
20683
20684 for (ix = 0; ix != len; ix++)
20685 {
20686 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20687 result = true;
20688 }
20689 if (result && (complain & tf_error))
20690 error (" trying to instantiate %qD", tmpl);
20691 return result;
20692 }
20693
20694 /* We're out of SFINAE context now, so generate diagnostics for the access
20695 errors we saw earlier when instantiating D from TMPL and ARGS. */
20696
20697 static void
20698 recheck_decl_substitution (tree d, tree tmpl, tree args)
20699 {
20700 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20701 tree type = TREE_TYPE (pattern);
20702 location_t loc = input_location;
20703
20704 push_access_scope (d);
20705 push_deferring_access_checks (dk_no_deferred);
20706 input_location = DECL_SOURCE_LOCATION (pattern);
20707 tsubst (type, args, tf_warning_or_error, d);
20708 input_location = loc;
20709 pop_deferring_access_checks ();
20710 pop_access_scope (d);
20711 }
20712
20713 /* Instantiate the indicated variable, function, or alias template TMPL with
20714 the template arguments in TARG_PTR. */
20715
20716 static tree
20717 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20718 {
20719 tree targ_ptr = orig_args;
20720 tree fndecl;
20721 tree gen_tmpl;
20722 tree spec;
20723 bool access_ok = true;
20724
20725 if (tmpl == error_mark_node)
20726 return error_mark_node;
20727
20728 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20729
20730 /* If this function is a clone, handle it specially. */
20731 if (DECL_CLONED_FUNCTION_P (tmpl))
20732 {
20733 tree spec;
20734 tree clone;
20735
20736 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20737 DECL_CLONED_FUNCTION. */
20738 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20739 targ_ptr, complain);
20740 if (spec == error_mark_node)
20741 return error_mark_node;
20742
20743 /* Look for the clone. */
20744 FOR_EACH_CLONE (clone, spec)
20745 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20746 return clone;
20747 /* We should always have found the clone by now. */
20748 gcc_unreachable ();
20749 return NULL_TREE;
20750 }
20751
20752 if (targ_ptr == error_mark_node)
20753 return error_mark_node;
20754
20755 /* Check to see if we already have this specialization. */
20756 gen_tmpl = most_general_template (tmpl);
20757 if (TMPL_ARGS_DEPTH (targ_ptr)
20758 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20759 /* targ_ptr only has the innermost template args, so add the outer ones
20760 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20761 the case of a non-dependent call within a template definition). */
20762 targ_ptr = (add_outermost_template_args
20763 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20764 targ_ptr));
20765
20766 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20767 but it doesn't seem to be on the hot path. */
20768 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20769
20770 gcc_checking_assert (tmpl == gen_tmpl
20771 || ((fndecl
20772 = retrieve_specialization (tmpl, orig_args, 0))
20773 == spec)
20774 || fndecl == NULL_TREE);
20775
20776 if (spec != NULL_TREE)
20777 {
20778 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20779 {
20780 if (complain & tf_error)
20781 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20782 return error_mark_node;
20783 }
20784 return spec;
20785 }
20786
20787 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20788 complain))
20789 return error_mark_node;
20790
20791 /* We are building a FUNCTION_DECL, during which the access of its
20792 parameters and return types have to be checked. However this
20793 FUNCTION_DECL which is the desired context for access checking
20794 is not built yet. We solve this chicken-and-egg problem by
20795 deferring all checks until we have the FUNCTION_DECL. */
20796 push_deferring_access_checks (dk_deferred);
20797
20798 /* Instantiation of the function happens in the context of the function
20799 template, not the context of the overload resolution we're doing. */
20800 push_to_top_level ();
20801 /* If there are dependent arguments, e.g. because we're doing partial
20802 ordering, make sure processing_template_decl stays set. */
20803 if (uses_template_parms (targ_ptr))
20804 ++processing_template_decl;
20805 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20806 {
20807 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20808 complain, gen_tmpl, true);
20809 push_nested_class (ctx);
20810 }
20811
20812 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20813
20814 fndecl = NULL_TREE;
20815 if (VAR_P (pattern))
20816 {
20817 /* We need to determine if we're using a partial or explicit
20818 specialization now, because the type of the variable could be
20819 different. */
20820 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20821 tree elt = most_specialized_partial_spec (tid, complain);
20822 if (elt == error_mark_node)
20823 pattern = error_mark_node;
20824 else if (elt)
20825 {
20826 tree partial_tmpl = TREE_VALUE (elt);
20827 tree partial_args = TREE_PURPOSE (elt);
20828 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20829 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20830 }
20831 }
20832
20833 /* Substitute template parameters to obtain the specialization. */
20834 if (fndecl == NULL_TREE)
20835 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20836 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20837 pop_nested_class ();
20838 pop_from_top_level ();
20839
20840 if (fndecl == error_mark_node)
20841 {
20842 pop_deferring_access_checks ();
20843 return error_mark_node;
20844 }
20845
20846 /* The DECL_TI_TEMPLATE should always be the immediate parent
20847 template, not the most general template. */
20848 DECL_TI_TEMPLATE (fndecl) = tmpl;
20849 DECL_TI_ARGS (fndecl) = targ_ptr;
20850
20851 /* Now we know the specialization, compute access previously
20852 deferred. Do no access control for inheriting constructors,
20853 as we already checked access for the inherited constructor. */
20854 if (!(flag_new_inheriting_ctors
20855 && DECL_INHERITED_CTOR (fndecl)))
20856 {
20857 push_access_scope (fndecl);
20858 if (!perform_deferred_access_checks (complain))
20859 access_ok = false;
20860 pop_access_scope (fndecl);
20861 }
20862 pop_deferring_access_checks ();
20863
20864 /* If we've just instantiated the main entry point for a function,
20865 instantiate all the alternate entry points as well. We do this
20866 by cloning the instantiation of the main entry point, not by
20867 instantiating the template clones. */
20868 if (tree chain = DECL_CHAIN (gen_tmpl))
20869 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20870 clone_cdtor (fndecl, /*update_methods=*/false);
20871
20872 if (!access_ok)
20873 {
20874 if (!(complain & tf_error))
20875 {
20876 /* Remember to reinstantiate when we're out of SFINAE so the user
20877 can see the errors. */
20878 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20879 }
20880 return error_mark_node;
20881 }
20882 return fndecl;
20883 }
20884
20885 /* Wrapper for instantiate_template_1. */
20886
20887 tree
20888 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20889 {
20890 tree ret;
20891 timevar_push (TV_TEMPLATE_INST);
20892 ret = instantiate_template_1 (tmpl, orig_args, complain);
20893 timevar_pop (TV_TEMPLATE_INST);
20894 return ret;
20895 }
20896
20897 /* Instantiate the alias template TMPL with ARGS. Also push a template
20898 instantiation level, which instantiate_template doesn't do because
20899 functions and variables have sufficient context established by the
20900 callers. */
20901
20902 static tree
20903 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20904 {
20905 if (tmpl == error_mark_node || args == error_mark_node)
20906 return error_mark_node;
20907
20908 args =
20909 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20910 args, tmpl, complain,
20911 /*require_all_args=*/true,
20912 /*use_default_args=*/true);
20913
20914 /* FIXME check for satisfaction in check_instantiated_args. */
20915 if (flag_concepts
20916 && !any_dependent_template_arguments_p (args)
20917 && !constraints_satisfied_p (tmpl, args))
20918 {
20919 if (complain & tf_error)
20920 {
20921 auto_diagnostic_group d;
20922 error ("template constraint failure for %qD", tmpl);
20923 diagnose_constraints (input_location, tmpl, args);
20924 }
20925 return error_mark_node;
20926 }
20927
20928 if (!push_tinst_level (tmpl, args))
20929 return error_mark_node;
20930 tree r = instantiate_template (tmpl, args, complain);
20931 pop_tinst_level ();
20932
20933 return r;
20934 }
20935
20936 /* PARM is a template parameter pack for FN. Returns true iff
20937 PARM is used in a deducible way in the argument list of FN. */
20938
20939 static bool
20940 pack_deducible_p (tree parm, tree fn)
20941 {
20942 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20943 for (; t; t = TREE_CHAIN (t))
20944 {
20945 tree type = TREE_VALUE (t);
20946 tree packs;
20947 if (!PACK_EXPANSION_P (type))
20948 continue;
20949 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20950 packs; packs = TREE_CHAIN (packs))
20951 if (template_args_equal (TREE_VALUE (packs), parm))
20952 {
20953 /* The template parameter pack is used in a function parameter
20954 pack. If this is the end of the parameter list, the
20955 template parameter pack is deducible. */
20956 if (TREE_CHAIN (t) == void_list_node)
20957 return true;
20958 else
20959 /* Otherwise, not. Well, it could be deduced from
20960 a non-pack parameter, but doing so would end up with
20961 a deduction mismatch, so don't bother. */
20962 return false;
20963 }
20964 }
20965 /* The template parameter pack isn't used in any function parameter
20966 packs, but it might be used deeper, e.g. tuple<Args...>. */
20967 return true;
20968 }
20969
20970 /* Subroutine of fn_type_unification: check non-dependent parms for
20971 convertibility. */
20972
20973 static int
20974 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20975 tree fn, unification_kind_t strict, int flags,
20976 struct conversion **convs, bool explain_p)
20977 {
20978 /* Non-constructor methods need to leave a conversion for 'this', which
20979 isn't included in nargs here. */
20980 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20981 && !DECL_CONSTRUCTOR_P (fn));
20982
20983 for (unsigned ia = 0;
20984 parms && parms != void_list_node && ia < nargs; )
20985 {
20986 tree parm = TREE_VALUE (parms);
20987
20988 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20989 && (!TREE_CHAIN (parms)
20990 || TREE_CHAIN (parms) == void_list_node))
20991 /* For a function parameter pack that occurs at the end of the
20992 parameter-declaration-list, the type A of each remaining
20993 argument of the call is compared with the type P of the
20994 declarator-id of the function parameter pack. */
20995 break;
20996
20997 parms = TREE_CHAIN (parms);
20998
20999 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21000 /* For a function parameter pack that does not occur at the
21001 end of the parameter-declaration-list, the type of the
21002 parameter pack is a non-deduced context. */
21003 continue;
21004
21005 if (!uses_template_parms (parm))
21006 {
21007 tree arg = args[ia];
21008 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21009 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21010
21011 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21012 conv_p, explain_p))
21013 return 1;
21014 }
21015
21016 ++ia;
21017 }
21018
21019 return 0;
21020 }
21021
21022 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21023 NARGS elements of the arguments that are being used when calling
21024 it. TARGS is a vector into which the deduced template arguments
21025 are placed.
21026
21027 Returns either a FUNCTION_DECL for the matching specialization of FN or
21028 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21029 true, diagnostics will be printed to explain why it failed.
21030
21031 If FN is a conversion operator, or we are trying to produce a specific
21032 specialization, RETURN_TYPE is the return type desired.
21033
21034 The EXPLICIT_TARGS are explicit template arguments provided via a
21035 template-id.
21036
21037 The parameter STRICT is one of:
21038
21039 DEDUCE_CALL:
21040 We are deducing arguments for a function call, as in
21041 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21042 deducing arguments for a call to the result of a conversion
21043 function template, as in [over.call.object].
21044
21045 DEDUCE_CONV:
21046 We are deducing arguments for a conversion function, as in
21047 [temp.deduct.conv].
21048
21049 DEDUCE_EXACT:
21050 We are deducing arguments when doing an explicit instantiation
21051 as in [temp.explicit], when determining an explicit specialization
21052 as in [temp.expl.spec], or when taking the address of a function
21053 template, as in [temp.deduct.funcaddr]. */
21054
21055 tree
21056 fn_type_unification (tree fn,
21057 tree explicit_targs,
21058 tree targs,
21059 const tree *args,
21060 unsigned int nargs,
21061 tree return_type,
21062 unification_kind_t strict,
21063 int flags,
21064 struct conversion **convs,
21065 bool explain_p,
21066 bool decltype_p)
21067 {
21068 tree parms;
21069 tree fntype;
21070 tree decl = NULL_TREE;
21071 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21072 bool ok;
21073 static int deduction_depth;
21074 /* type_unification_real will pass back any access checks from default
21075 template argument substitution. */
21076 vec<deferred_access_check, va_gc> *checks = NULL;
21077 /* We don't have all the template args yet. */
21078 bool incomplete = true;
21079
21080 tree orig_fn = fn;
21081 if (flag_new_inheriting_ctors)
21082 fn = strip_inheriting_ctors (fn);
21083
21084 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21085 tree r = error_mark_node;
21086
21087 tree full_targs = targs;
21088 if (TMPL_ARGS_DEPTH (targs)
21089 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21090 full_targs = (add_outermost_template_args
21091 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21092 targs));
21093
21094 if (decltype_p)
21095 complain |= tf_decltype;
21096
21097 /* In C++0x, it's possible to have a function template whose type depends
21098 on itself recursively. This is most obvious with decltype, but can also
21099 occur with enumeration scope (c++/48969). So we need to catch infinite
21100 recursion and reject the substitution at deduction time; this function
21101 will return error_mark_node for any repeated substitution.
21102
21103 This also catches excessive recursion such as when f<N> depends on
21104 f<N-1> across all integers, and returns error_mark_node for all the
21105 substitutions back up to the initial one.
21106
21107 This is, of course, not reentrant. */
21108 if (excessive_deduction_depth)
21109 return error_mark_node;
21110 ++deduction_depth;
21111
21112 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21113
21114 fntype = TREE_TYPE (fn);
21115 if (explicit_targs)
21116 {
21117 /* [temp.deduct]
21118
21119 The specified template arguments must match the template
21120 parameters in kind (i.e., type, nontype, template), and there
21121 must not be more arguments than there are parameters;
21122 otherwise type deduction fails.
21123
21124 Nontype arguments must match the types of the corresponding
21125 nontype template parameters, or must be convertible to the
21126 types of the corresponding nontype parameters as specified in
21127 _temp.arg.nontype_, otherwise type deduction fails.
21128
21129 All references in the function type of the function template
21130 to the corresponding template parameters are replaced by the
21131 specified template argument values. If a substitution in a
21132 template parameter or in the function type of the function
21133 template results in an invalid type, type deduction fails. */
21134 int i, len = TREE_VEC_LENGTH (tparms);
21135 location_t loc = input_location;
21136 incomplete = false;
21137
21138 if (explicit_targs == error_mark_node)
21139 goto fail;
21140
21141 if (TMPL_ARGS_DEPTH (explicit_targs)
21142 < TMPL_ARGS_DEPTH (full_targs))
21143 explicit_targs = add_outermost_template_args (full_targs,
21144 explicit_targs);
21145
21146 /* Adjust any explicit template arguments before entering the
21147 substitution context. */
21148 explicit_targs
21149 = (coerce_template_parms (tparms, explicit_targs, fn,
21150 complain|tf_partial,
21151 /*require_all_args=*/false,
21152 /*use_default_args=*/false));
21153 if (explicit_targs == error_mark_node)
21154 goto fail;
21155
21156 /* Substitute the explicit args into the function type. This is
21157 necessary so that, for instance, explicitly declared function
21158 arguments can match null pointed constants. If we were given
21159 an incomplete set of explicit args, we must not do semantic
21160 processing during substitution as we could create partial
21161 instantiations. */
21162 for (i = 0; i < len; i++)
21163 {
21164 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21165 bool parameter_pack = false;
21166 tree targ = TREE_VEC_ELT (explicit_targs, i);
21167
21168 /* Dig out the actual parm. */
21169 if (TREE_CODE (parm) == TYPE_DECL
21170 || TREE_CODE (parm) == TEMPLATE_DECL)
21171 {
21172 parm = TREE_TYPE (parm);
21173 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21174 }
21175 else if (TREE_CODE (parm) == PARM_DECL)
21176 {
21177 parm = DECL_INITIAL (parm);
21178 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21179 }
21180
21181 if (targ == NULL_TREE)
21182 /* No explicit argument for this template parameter. */
21183 incomplete = true;
21184 else if (parameter_pack && pack_deducible_p (parm, fn))
21185 {
21186 /* Mark the argument pack as "incomplete". We could
21187 still deduce more arguments during unification.
21188 We remove this mark in type_unification_real. */
21189 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21190 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21191 = ARGUMENT_PACK_ARGS (targ);
21192
21193 /* We have some incomplete argument packs. */
21194 incomplete = true;
21195 }
21196 }
21197
21198 if (incomplete)
21199 {
21200 if (!push_tinst_level (fn, explicit_targs))
21201 {
21202 excessive_deduction_depth = true;
21203 goto fail;
21204 }
21205 ++processing_template_decl;
21206 input_location = DECL_SOURCE_LOCATION (fn);
21207 /* Ignore any access checks; we'll see them again in
21208 instantiate_template and they might have the wrong
21209 access path at this point. */
21210 push_deferring_access_checks (dk_deferred);
21211 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21212 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21213 pop_deferring_access_checks ();
21214 input_location = loc;
21215 --processing_template_decl;
21216 pop_tinst_level ();
21217
21218 if (fntype == error_mark_node)
21219 goto fail;
21220 }
21221
21222 /* Place the explicitly specified arguments in TARGS. */
21223 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21224 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21225 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21226 if (!incomplete && CHECKING_P
21227 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21228 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21229 (targs, NUM_TMPL_ARGS (explicit_targs));
21230 }
21231
21232 if (return_type && strict != DEDUCE_CALL)
21233 {
21234 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21235 new_args[0] = return_type;
21236 memcpy (new_args + 1, args, nargs * sizeof (tree));
21237 args = new_args;
21238 ++nargs;
21239 }
21240
21241 if (!incomplete)
21242 goto deduced;
21243
21244 /* Never do unification on the 'this' parameter. */
21245 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21246
21247 if (return_type && strict == DEDUCE_CALL)
21248 {
21249 /* We're deducing for a call to the result of a template conversion
21250 function. The parms we really want are in return_type. */
21251 if (INDIRECT_TYPE_P (return_type))
21252 return_type = TREE_TYPE (return_type);
21253 parms = TYPE_ARG_TYPES (return_type);
21254 }
21255 else if (return_type)
21256 {
21257 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21258 }
21259
21260 /* We allow incomplete unification without an error message here
21261 because the standard doesn't seem to explicitly prohibit it. Our
21262 callers must be ready to deal with unification failures in any
21263 event. */
21264
21265 /* If we aren't explaining yet, push tinst context so we can see where
21266 any errors (e.g. from class instantiations triggered by instantiation
21267 of default template arguments) come from. If we are explaining, this
21268 context is redundant. */
21269 if (!explain_p && !push_tinst_level (fn, targs))
21270 {
21271 excessive_deduction_depth = true;
21272 goto fail;
21273 }
21274
21275 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21276 full_targs, parms, args, nargs, /*subr=*/0,
21277 strict, &checks, explain_p);
21278 if (!explain_p)
21279 pop_tinst_level ();
21280 if (!ok)
21281 goto fail;
21282
21283 /* Now that we have bindings for all of the template arguments,
21284 ensure that the arguments deduced for the template template
21285 parameters have compatible template parameter lists. We cannot
21286 check this property before we have deduced all template
21287 arguments, because the template parameter types of a template
21288 template parameter might depend on prior template parameters
21289 deduced after the template template parameter. The following
21290 ill-formed example illustrates this issue:
21291
21292 template<typename T, template<T> class C> void f(C<5>, T);
21293
21294 template<int N> struct X {};
21295
21296 void g() {
21297 f(X<5>(), 5l); // error: template argument deduction fails
21298 }
21299
21300 The template parameter list of 'C' depends on the template type
21301 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21302 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21303 time that we deduce 'C'. */
21304 if (!template_template_parm_bindings_ok_p
21305 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21306 {
21307 unify_inconsistent_template_template_parameters (explain_p);
21308 goto fail;
21309 }
21310
21311 deduced:
21312
21313 /* CWG2369: Check satisfaction before non-deducible conversions. */
21314 if (!constraints_satisfied_p (fn, targs))
21315 {
21316 if (explain_p)
21317 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21318 goto fail;
21319 }
21320
21321 /* DR 1391: All parameters have args, now check non-dependent parms for
21322 convertibility. We don't do this if all args were explicitly specified,
21323 as the standard says that we substitute explicit args immediately. */
21324 if (incomplete
21325 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21326 convs, explain_p))
21327 goto fail;
21328
21329 /* All is well so far. Now, check:
21330
21331 [temp.deduct]
21332
21333 When all template arguments have been deduced, all uses of
21334 template parameters in nondeduced contexts are replaced with
21335 the corresponding deduced argument values. If the
21336 substitution results in an invalid type, as described above,
21337 type deduction fails. */
21338 if (!push_tinst_level (fn, targs))
21339 {
21340 excessive_deduction_depth = true;
21341 goto fail;
21342 }
21343
21344 /* Also collect access checks from the instantiation. */
21345 reopen_deferring_access_checks (checks);
21346
21347 decl = instantiate_template (fn, targs, complain);
21348
21349 checks = get_deferred_access_checks ();
21350 pop_deferring_access_checks ();
21351
21352 pop_tinst_level ();
21353
21354 if (decl == error_mark_node)
21355 goto fail;
21356
21357 /* Now perform any access checks encountered during substitution. */
21358 push_access_scope (decl);
21359 ok = perform_access_checks (checks, complain);
21360 pop_access_scope (decl);
21361 if (!ok)
21362 goto fail;
21363
21364 /* If we're looking for an exact match, check that what we got
21365 is indeed an exact match. It might not be if some template
21366 parameters are used in non-deduced contexts. But don't check
21367 for an exact match if we have dependent template arguments;
21368 in that case we're doing partial ordering, and we already know
21369 that we have two candidates that will provide the actual type. */
21370 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21371 {
21372 tree substed = TREE_TYPE (decl);
21373 unsigned int i;
21374
21375 tree sarg
21376 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21377 if (return_type)
21378 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21379 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21380 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21381 {
21382 unify_type_mismatch (explain_p, args[i],
21383 TREE_VALUE (sarg));
21384 goto fail;
21385 }
21386 }
21387
21388 /* After doing deduction with the inherited constructor, actually return an
21389 instantiation of the inheriting constructor. */
21390 if (orig_fn != fn)
21391 decl = instantiate_template (orig_fn, targs, complain);
21392
21393 r = decl;
21394
21395 fail:
21396 --deduction_depth;
21397 if (excessive_deduction_depth)
21398 {
21399 if (deduction_depth == 0)
21400 /* Reset once we're all the way out. */
21401 excessive_deduction_depth = false;
21402 }
21403
21404 return r;
21405 }
21406
21407 /* Adjust types before performing type deduction, as described in
21408 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21409 sections are symmetric. PARM is the type of a function parameter
21410 or the return type of the conversion function. ARG is the type of
21411 the argument passed to the call, or the type of the value
21412 initialized with the result of the conversion function.
21413 ARG_EXPR is the original argument expression, which may be null. */
21414
21415 static int
21416 maybe_adjust_types_for_deduction (unification_kind_t strict,
21417 tree* parm,
21418 tree* arg,
21419 tree arg_expr)
21420 {
21421 int result = 0;
21422
21423 switch (strict)
21424 {
21425 case DEDUCE_CALL:
21426 break;
21427
21428 case DEDUCE_CONV:
21429 /* Swap PARM and ARG throughout the remainder of this
21430 function; the handling is precisely symmetric since PARM
21431 will initialize ARG rather than vice versa. */
21432 std::swap (parm, arg);
21433 break;
21434
21435 case DEDUCE_EXACT:
21436 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21437 too, but here handle it by stripping the reference from PARM
21438 rather than by adding it to ARG. */
21439 if (TYPE_REF_P (*parm)
21440 && TYPE_REF_IS_RVALUE (*parm)
21441 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21442 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21443 && TYPE_REF_P (*arg)
21444 && !TYPE_REF_IS_RVALUE (*arg))
21445 *parm = TREE_TYPE (*parm);
21446 /* Nothing else to do in this case. */
21447 return 0;
21448
21449 default:
21450 gcc_unreachable ();
21451 }
21452
21453 if (!TYPE_REF_P (*parm))
21454 {
21455 /* [temp.deduct.call]
21456
21457 If P is not a reference type:
21458
21459 --If A is an array type, the pointer type produced by the
21460 array-to-pointer standard conversion (_conv.array_) is
21461 used in place of A for type deduction; otherwise,
21462
21463 --If A is a function type, the pointer type produced by
21464 the function-to-pointer standard conversion
21465 (_conv.func_) is used in place of A for type deduction;
21466 otherwise,
21467
21468 --If A is a cv-qualified type, the top level
21469 cv-qualifiers of A's type are ignored for type
21470 deduction. */
21471 if (TREE_CODE (*arg) == ARRAY_TYPE)
21472 *arg = build_pointer_type (TREE_TYPE (*arg));
21473 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21474 *arg = build_pointer_type (*arg);
21475 else
21476 *arg = TYPE_MAIN_VARIANT (*arg);
21477 }
21478
21479 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21480 reference to a cv-unqualified template parameter that does not represent a
21481 template parameter of a class template (during class template argument
21482 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21483 an lvalue, the type "lvalue reference to A" is used in place of A for type
21484 deduction. */
21485 if (TYPE_REF_P (*parm)
21486 && TYPE_REF_IS_RVALUE (*parm)
21487 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21488 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21489 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21490 && (arg_expr ? lvalue_p (arg_expr)
21491 /* try_one_overload doesn't provide an arg_expr, but
21492 functions are always lvalues. */
21493 : TREE_CODE (*arg) == FUNCTION_TYPE))
21494 *arg = build_reference_type (*arg);
21495
21496 /* [temp.deduct.call]
21497
21498 If P is a cv-qualified type, the top level cv-qualifiers
21499 of P's type are ignored for type deduction. If P is a
21500 reference type, the type referred to by P is used for
21501 type deduction. */
21502 *parm = TYPE_MAIN_VARIANT (*parm);
21503 if (TYPE_REF_P (*parm))
21504 {
21505 *parm = TREE_TYPE (*parm);
21506 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21507 }
21508
21509 /* DR 322. For conversion deduction, remove a reference type on parm
21510 too (which has been swapped into ARG). */
21511 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21512 *arg = TREE_TYPE (*arg);
21513
21514 return result;
21515 }
21516
21517 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21518 template which doesn't contain any deducible template parameters; check if
21519 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21520 unify_one_argument. */
21521
21522 static int
21523 check_non_deducible_conversion (tree parm, tree arg, int strict,
21524 int flags, struct conversion **conv_p,
21525 bool explain_p)
21526 {
21527 tree type;
21528
21529 if (!TYPE_P (arg))
21530 type = TREE_TYPE (arg);
21531 else
21532 type = arg;
21533
21534 if (same_type_p (parm, type))
21535 return unify_success (explain_p);
21536
21537 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21538 if (strict == DEDUCE_CONV)
21539 {
21540 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21541 return unify_success (explain_p);
21542 }
21543 else if (strict != DEDUCE_EXACT)
21544 {
21545 bool ok = false;
21546 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21547 if (conv_p)
21548 /* Avoid recalculating this in add_function_candidate. */
21549 ok = (*conv_p
21550 = good_conversion (parm, type, conv_arg, flags, complain));
21551 else
21552 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21553 if (ok)
21554 return unify_success (explain_p);
21555 }
21556
21557 if (strict == DEDUCE_EXACT)
21558 return unify_type_mismatch (explain_p, parm, arg);
21559 else
21560 return unify_arg_conversion (explain_p, parm, type, arg);
21561 }
21562
21563 static bool uses_deducible_template_parms (tree type);
21564
21565 /* Returns true iff the expression EXPR is one from which a template
21566 argument can be deduced. In other words, if it's an undecorated
21567 use of a template non-type parameter. */
21568
21569 static bool
21570 deducible_expression (tree expr)
21571 {
21572 /* Strip implicit conversions. */
21573 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21574 expr = TREE_OPERAND (expr, 0);
21575 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21576 }
21577
21578 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21579 deducible way; that is, if it has a max value of <PARM> - 1. */
21580
21581 static bool
21582 deducible_array_bound (tree domain)
21583 {
21584 if (domain == NULL_TREE)
21585 return false;
21586
21587 tree max = TYPE_MAX_VALUE (domain);
21588 if (TREE_CODE (max) != MINUS_EXPR)
21589 return false;
21590
21591 return deducible_expression (TREE_OPERAND (max, 0));
21592 }
21593
21594 /* Returns true iff the template arguments ARGS use a template parameter
21595 in a deducible way. */
21596
21597 static bool
21598 deducible_template_args (tree args)
21599 {
21600 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21601 {
21602 bool deducible;
21603 tree elt = TREE_VEC_ELT (args, i);
21604 if (ARGUMENT_PACK_P (elt))
21605 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21606 else
21607 {
21608 if (PACK_EXPANSION_P (elt))
21609 elt = PACK_EXPANSION_PATTERN (elt);
21610 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21611 deducible = true;
21612 else if (TYPE_P (elt))
21613 deducible = uses_deducible_template_parms (elt);
21614 else
21615 deducible = deducible_expression (elt);
21616 }
21617 if (deducible)
21618 return true;
21619 }
21620 return false;
21621 }
21622
21623 /* Returns true iff TYPE contains any deducible references to template
21624 parameters, as per 14.8.2.5. */
21625
21626 static bool
21627 uses_deducible_template_parms (tree type)
21628 {
21629 if (PACK_EXPANSION_P (type))
21630 type = PACK_EXPANSION_PATTERN (type);
21631
21632 /* T
21633 cv-list T
21634 TT<T>
21635 TT<i>
21636 TT<> */
21637 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21638 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21639 return true;
21640
21641 /* T*
21642 T&
21643 T&& */
21644 if (INDIRECT_TYPE_P (type))
21645 return uses_deducible_template_parms (TREE_TYPE (type));
21646
21647 /* T[integer-constant ]
21648 type [i] */
21649 if (TREE_CODE (type) == ARRAY_TYPE)
21650 return (uses_deducible_template_parms (TREE_TYPE (type))
21651 || deducible_array_bound (TYPE_DOMAIN (type)));
21652
21653 /* T type ::*
21654 type T::*
21655 T T::*
21656 T (type ::*)()
21657 type (T::*)()
21658 type (type ::*)(T)
21659 type (T::*)(T)
21660 T (type ::*)(T)
21661 T (T::*)()
21662 T (T::*)(T) */
21663 if (TYPE_PTRMEM_P (type))
21664 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21665 || (uses_deducible_template_parms
21666 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21667
21668 /* template-name <T> (where template-name refers to a class template)
21669 template-name <i> (where template-name refers to a class template) */
21670 if (CLASS_TYPE_P (type)
21671 && CLASSTYPE_TEMPLATE_INFO (type)
21672 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21673 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21674 (CLASSTYPE_TI_ARGS (type)));
21675
21676 /* type (T)
21677 T()
21678 T(T) */
21679 if (FUNC_OR_METHOD_TYPE_P (type))
21680 {
21681 if (uses_deducible_template_parms (TREE_TYPE (type)))
21682 return true;
21683 tree parm = TYPE_ARG_TYPES (type);
21684 if (TREE_CODE (type) == METHOD_TYPE)
21685 parm = TREE_CHAIN (parm);
21686 for (; parm; parm = TREE_CHAIN (parm))
21687 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21688 return true;
21689 }
21690
21691 return false;
21692 }
21693
21694 /* Subroutine of type_unification_real and unify_pack_expansion to
21695 handle unification of a single P/A pair. Parameters are as
21696 for those functions. */
21697
21698 static int
21699 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21700 int subr, unification_kind_t strict,
21701 bool explain_p)
21702 {
21703 tree arg_expr = NULL_TREE;
21704 int arg_strict;
21705
21706 if (arg == error_mark_node || parm == error_mark_node)
21707 return unify_invalid (explain_p);
21708 if (arg == unknown_type_node)
21709 /* We can't deduce anything from this, but we might get all the
21710 template args from other function args. */
21711 return unify_success (explain_p);
21712
21713 /* Implicit conversions (Clause 4) will be performed on a function
21714 argument to convert it to the type of the corresponding function
21715 parameter if the parameter type contains no template-parameters that
21716 participate in template argument deduction. */
21717 if (strict != DEDUCE_EXACT
21718 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21719 /* For function parameters with no deducible template parameters,
21720 just return. We'll check non-dependent conversions later. */
21721 return unify_success (explain_p);
21722
21723 switch (strict)
21724 {
21725 case DEDUCE_CALL:
21726 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21727 | UNIFY_ALLOW_MORE_CV_QUAL
21728 | UNIFY_ALLOW_DERIVED);
21729 break;
21730
21731 case DEDUCE_CONV:
21732 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21733 break;
21734
21735 case DEDUCE_EXACT:
21736 arg_strict = UNIFY_ALLOW_NONE;
21737 break;
21738
21739 default:
21740 gcc_unreachable ();
21741 }
21742
21743 /* We only do these transformations if this is the top-level
21744 parameter_type_list in a call or declaration matching; in other
21745 situations (nested function declarators, template argument lists) we
21746 won't be comparing a type to an expression, and we don't do any type
21747 adjustments. */
21748 if (!subr)
21749 {
21750 if (!TYPE_P (arg))
21751 {
21752 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21753 if (type_unknown_p (arg))
21754 {
21755 /* [temp.deduct.type] A template-argument can be
21756 deduced from a pointer to function or pointer
21757 to member function argument if the set of
21758 overloaded functions does not contain function
21759 templates and at most one of a set of
21760 overloaded functions provides a unique
21761 match. */
21762 resolve_overloaded_unification (tparms, targs, parm,
21763 arg, strict,
21764 arg_strict, explain_p);
21765 /* If a unique match was not found, this is a
21766 non-deduced context, so we still succeed. */
21767 return unify_success (explain_p);
21768 }
21769
21770 arg_expr = arg;
21771 arg = unlowered_expr_type (arg);
21772 if (arg == error_mark_node)
21773 return unify_invalid (explain_p);
21774 }
21775
21776 arg_strict |=
21777 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21778 }
21779 else
21780 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21781 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21782 return unify_template_argument_mismatch (explain_p, parm, arg);
21783
21784 /* For deduction from an init-list we need the actual list. */
21785 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21786 arg = arg_expr;
21787 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21788 }
21789
21790 /* for_each_template_parm callback that always returns 0. */
21791
21792 static int
21793 zero_r (tree, void *)
21794 {
21795 return 0;
21796 }
21797
21798 /* for_each_template_parm any_fn callback to handle deduction of a template
21799 type argument from the type of an array bound. */
21800
21801 static int
21802 array_deduction_r (tree t, void *data)
21803 {
21804 tree_pair_p d = (tree_pair_p)data;
21805 tree &tparms = d->purpose;
21806 tree &targs = d->value;
21807
21808 if (TREE_CODE (t) == ARRAY_TYPE)
21809 if (tree dom = TYPE_DOMAIN (t))
21810 if (tree max = TYPE_MAX_VALUE (dom))
21811 {
21812 if (TREE_CODE (max) == MINUS_EXPR)
21813 max = TREE_OPERAND (max, 0);
21814 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21815 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21816 UNIFY_ALLOW_NONE, /*explain*/false);
21817 }
21818
21819 /* Keep walking. */
21820 return 0;
21821 }
21822
21823 /* Try to deduce any not-yet-deduced template type arguments from the type of
21824 an array bound. This is handled separately from unify because 14.8.2.5 says
21825 "The type of a type parameter is only deduced from an array bound if it is
21826 not otherwise deduced." */
21827
21828 static void
21829 try_array_deduction (tree tparms, tree targs, tree parm)
21830 {
21831 tree_pair_s data = { tparms, targs };
21832 hash_set<tree> visited;
21833 for_each_template_parm (parm, zero_r, &data, &visited,
21834 /*nondeduced*/false, array_deduction_r);
21835 }
21836
21837 /* Most parms like fn_type_unification.
21838
21839 If SUBR is 1, we're being called recursively (to unify the
21840 arguments of a function or method parameter of a function
21841 template).
21842
21843 CHECKS is a pointer to a vector of access checks encountered while
21844 substituting default template arguments. */
21845
21846 static int
21847 type_unification_real (tree tparms,
21848 tree full_targs,
21849 tree xparms,
21850 const tree *xargs,
21851 unsigned int xnargs,
21852 int subr,
21853 unification_kind_t strict,
21854 vec<deferred_access_check, va_gc> **checks,
21855 bool explain_p)
21856 {
21857 tree parm, arg;
21858 int i;
21859 int ntparms = TREE_VEC_LENGTH (tparms);
21860 int saw_undeduced = 0;
21861 tree parms;
21862 const tree *args;
21863 unsigned int nargs;
21864 unsigned int ia;
21865
21866 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21867 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21868 gcc_assert (ntparms > 0);
21869
21870 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21871
21872 /* Reset the number of non-defaulted template arguments contained
21873 in TARGS. */
21874 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21875
21876 again:
21877 parms = xparms;
21878 args = xargs;
21879 nargs = xnargs;
21880
21881 ia = 0;
21882 while (parms && parms != void_list_node
21883 && ia < nargs)
21884 {
21885 parm = TREE_VALUE (parms);
21886
21887 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21888 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21889 /* For a function parameter pack that occurs at the end of the
21890 parameter-declaration-list, the type A of each remaining
21891 argument of the call is compared with the type P of the
21892 declarator-id of the function parameter pack. */
21893 break;
21894
21895 parms = TREE_CHAIN (parms);
21896
21897 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21898 /* For a function parameter pack that does not occur at the
21899 end of the parameter-declaration-list, the type of the
21900 parameter pack is a non-deduced context. */
21901 continue;
21902
21903 arg = args[ia];
21904 ++ia;
21905
21906 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21907 explain_p))
21908 return 1;
21909 }
21910
21911 if (parms
21912 && parms != void_list_node
21913 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21914 {
21915 /* Unify the remaining arguments with the pack expansion type. */
21916 tree argvec;
21917 tree parmvec = make_tree_vec (1);
21918
21919 /* Allocate a TREE_VEC and copy in all of the arguments */
21920 argvec = make_tree_vec (nargs - ia);
21921 for (i = 0; ia < nargs; ++ia, ++i)
21922 TREE_VEC_ELT (argvec, i) = args[ia];
21923
21924 /* Copy the parameter into parmvec. */
21925 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21926 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21927 /*subr=*/subr, explain_p))
21928 return 1;
21929
21930 /* Advance to the end of the list of parameters. */
21931 parms = TREE_CHAIN (parms);
21932 }
21933
21934 /* Fail if we've reached the end of the parm list, and more args
21935 are present, and the parm list isn't variadic. */
21936 if (ia < nargs && parms == void_list_node)
21937 return unify_too_many_arguments (explain_p, nargs, ia);
21938 /* Fail if parms are left and they don't have default values and
21939 they aren't all deduced as empty packs (c++/57397). This is
21940 consistent with sufficient_parms_p. */
21941 if (parms && parms != void_list_node
21942 && TREE_PURPOSE (parms) == NULL_TREE)
21943 {
21944 unsigned int count = nargs;
21945 tree p = parms;
21946 bool type_pack_p;
21947 do
21948 {
21949 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21950 if (!type_pack_p)
21951 count++;
21952 p = TREE_CHAIN (p);
21953 }
21954 while (p && p != void_list_node);
21955 if (count != nargs)
21956 return unify_too_few_arguments (explain_p, ia, count,
21957 type_pack_p);
21958 }
21959
21960 if (!subr)
21961 {
21962 tsubst_flags_t complain = (explain_p
21963 ? tf_warning_or_error
21964 : tf_none);
21965 bool tried_array_deduction = (cxx_dialect < cxx17);
21966
21967 for (i = 0; i < ntparms; i++)
21968 {
21969 tree targ = TREE_VEC_ELT (targs, i);
21970 tree tparm = TREE_VEC_ELT (tparms, i);
21971
21972 /* Clear the "incomplete" flags on all argument packs now so that
21973 substituting them into later default arguments works. */
21974 if (targ && ARGUMENT_PACK_P (targ))
21975 {
21976 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21977 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21978 }
21979
21980 if (targ || tparm == error_mark_node)
21981 continue;
21982 tparm = TREE_VALUE (tparm);
21983
21984 if (TREE_CODE (tparm) == TYPE_DECL
21985 && !tried_array_deduction)
21986 {
21987 try_array_deduction (tparms, targs, xparms);
21988 tried_array_deduction = true;
21989 if (TREE_VEC_ELT (targs, i))
21990 continue;
21991 }
21992
21993 /* If this is an undeduced nontype parameter that depends on
21994 a type parameter, try another pass; its type may have been
21995 deduced from a later argument than the one from which
21996 this parameter can be deduced. */
21997 if (TREE_CODE (tparm) == PARM_DECL
21998 && uses_template_parms (TREE_TYPE (tparm))
21999 && saw_undeduced < 2)
22000 {
22001 saw_undeduced = 1;
22002 continue;
22003 }
22004
22005 /* Core issue #226 (C++0x) [temp.deduct]:
22006
22007 If a template argument has not been deduced, its
22008 default template argument, if any, is used.
22009
22010 When we are in C++98 mode, TREE_PURPOSE will either
22011 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22012 to explicitly check cxx_dialect here. */
22013 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22014 /* OK, there is a default argument. Wait until after the
22015 conversion check to do substitution. */
22016 continue;
22017
22018 /* If the type parameter is a parameter pack, then it will
22019 be deduced to an empty parameter pack. */
22020 if (template_parameter_pack_p (tparm))
22021 {
22022 tree arg;
22023
22024 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22025 {
22026 arg = make_node (NONTYPE_ARGUMENT_PACK);
22027 TREE_CONSTANT (arg) = 1;
22028 }
22029 else
22030 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22031
22032 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22033
22034 TREE_VEC_ELT (targs, i) = arg;
22035 continue;
22036 }
22037
22038 return unify_parameter_deduction_failure (explain_p, tparm);
22039 }
22040
22041 /* Now substitute into the default template arguments. */
22042 for (i = 0; i < ntparms; i++)
22043 {
22044 tree targ = TREE_VEC_ELT (targs, i);
22045 tree tparm = TREE_VEC_ELT (tparms, i);
22046
22047 if (targ || tparm == error_mark_node)
22048 continue;
22049 tree parm = TREE_VALUE (tparm);
22050 tree arg = TREE_PURPOSE (tparm);
22051 reopen_deferring_access_checks (*checks);
22052 location_t save_loc = input_location;
22053 if (DECL_P (parm))
22054 input_location = DECL_SOURCE_LOCATION (parm);
22055
22056 if (saw_undeduced == 1
22057 && TREE_CODE (parm) == PARM_DECL
22058 && uses_template_parms (TREE_TYPE (parm)))
22059 {
22060 /* The type of this non-type parameter depends on undeduced
22061 parameters. Don't try to use its default argument yet,
22062 since we might deduce an argument for it on the next pass,
22063 but do check whether the arguments we already have cause
22064 substitution failure, so that that happens before we try
22065 later default arguments (78489). */
22066 ++processing_template_decl;
22067 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22068 NULL_TREE);
22069 --processing_template_decl;
22070 if (type == error_mark_node)
22071 arg = error_mark_node;
22072 else
22073 arg = NULL_TREE;
22074 }
22075 else
22076 {
22077 /* Even if the call is happening in template context, getting
22078 here means it's non-dependent, and a default argument is
22079 considered a separate definition under [temp.decls], so we can
22080 do this substitution without processing_template_decl. This
22081 is important if the default argument contains something that
22082 might be instantiation-dependent like access (87480). */
22083 processing_template_decl_sentinel s;
22084 tree substed = NULL_TREE;
22085 if (saw_undeduced == 1)
22086 {
22087 /* First instatiate in template context, in case we still
22088 depend on undeduced template parameters. */
22089 ++processing_template_decl;
22090 substed = tsubst_template_arg (arg, full_targs, complain,
22091 NULL_TREE);
22092 --processing_template_decl;
22093 if (substed != error_mark_node
22094 && !uses_template_parms (substed))
22095 /* We replaced all the tparms, substitute again out of
22096 template context. */
22097 substed = NULL_TREE;
22098 }
22099 if (!substed)
22100 substed = tsubst_template_arg (arg, full_targs, complain,
22101 NULL_TREE);
22102
22103 if (!uses_template_parms (substed))
22104 arg = convert_template_argument (parm, substed, full_targs,
22105 complain, i, NULL_TREE);
22106 else if (saw_undeduced == 1)
22107 arg = NULL_TREE;
22108 else
22109 arg = error_mark_node;
22110 }
22111
22112 input_location = save_loc;
22113 *checks = get_deferred_access_checks ();
22114 pop_deferring_access_checks ();
22115
22116 if (arg == error_mark_node)
22117 return 1;
22118 else if (arg)
22119 {
22120 TREE_VEC_ELT (targs, i) = arg;
22121 /* The position of the first default template argument,
22122 is also the number of non-defaulted arguments in TARGS.
22123 Record that. */
22124 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22125 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22126 }
22127 }
22128
22129 if (saw_undeduced++ == 1)
22130 goto again;
22131 }
22132
22133 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22134 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22135
22136 return unify_success (explain_p);
22137 }
22138
22139 /* Subroutine of type_unification_real. Args are like the variables
22140 at the call site. ARG is an overloaded function (or template-id);
22141 we try deducing template args from each of the overloads, and if
22142 only one succeeds, we go with that. Modifies TARGS and returns
22143 true on success. */
22144
22145 static bool
22146 resolve_overloaded_unification (tree tparms,
22147 tree targs,
22148 tree parm,
22149 tree arg,
22150 unification_kind_t strict,
22151 int sub_strict,
22152 bool explain_p)
22153 {
22154 tree tempargs = copy_node (targs);
22155 int good = 0;
22156 tree goodfn = NULL_TREE;
22157 bool addr_p;
22158
22159 if (TREE_CODE (arg) == ADDR_EXPR)
22160 {
22161 arg = TREE_OPERAND (arg, 0);
22162 addr_p = true;
22163 }
22164 else
22165 addr_p = false;
22166
22167 if (TREE_CODE (arg) == COMPONENT_REF)
22168 /* Handle `&x' where `x' is some static or non-static member
22169 function name. */
22170 arg = TREE_OPERAND (arg, 1);
22171
22172 if (TREE_CODE (arg) == OFFSET_REF)
22173 arg = TREE_OPERAND (arg, 1);
22174
22175 /* Strip baselink information. */
22176 if (BASELINK_P (arg))
22177 arg = BASELINK_FUNCTIONS (arg);
22178
22179 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22180 {
22181 /* If we got some explicit template args, we need to plug them into
22182 the affected templates before we try to unify, in case the
22183 explicit args will completely resolve the templates in question. */
22184
22185 int ok = 0;
22186 tree expl_subargs = TREE_OPERAND (arg, 1);
22187 arg = TREE_OPERAND (arg, 0);
22188
22189 for (lkp_iterator iter (arg); iter; ++iter)
22190 {
22191 tree fn = *iter;
22192 tree subargs, elem;
22193
22194 if (TREE_CODE (fn) != TEMPLATE_DECL)
22195 continue;
22196
22197 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22198 expl_subargs, NULL_TREE, tf_none,
22199 /*require_all_args=*/true,
22200 /*use_default_args=*/true);
22201 if (subargs != error_mark_node
22202 && !any_dependent_template_arguments_p (subargs))
22203 {
22204 fn = instantiate_template (fn, subargs, tf_none);
22205 if (!constraints_satisfied_p (fn))
22206 continue;
22207 if (undeduced_auto_decl (fn))
22208 {
22209 /* Instantiate the function to deduce its return type. */
22210 ++function_depth;
22211 instantiate_decl (fn, /*defer*/false, /*class*/false);
22212 --function_depth;
22213 }
22214
22215 elem = TREE_TYPE (fn);
22216 if (try_one_overload (tparms, targs, tempargs, parm,
22217 elem, strict, sub_strict, addr_p, explain_p)
22218 && (!goodfn || !same_type_p (goodfn, elem)))
22219 {
22220 goodfn = elem;
22221 ++good;
22222 }
22223 }
22224 else if (subargs)
22225 ++ok;
22226 }
22227 /* If no templates (or more than one) are fully resolved by the
22228 explicit arguments, this template-id is a non-deduced context; it
22229 could still be OK if we deduce all template arguments for the
22230 enclosing call through other arguments. */
22231 if (good != 1)
22232 good = ok;
22233 }
22234 else if (!OVL_P (arg))
22235 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22236 -- but the deduction does not succeed because the expression is
22237 not just the function on its own. */
22238 return false;
22239 else
22240 for (lkp_iterator iter (arg); iter; ++iter)
22241 {
22242 tree fn = *iter;
22243 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22244 strict, sub_strict, addr_p, explain_p)
22245 && (!goodfn || !decls_match (goodfn, fn)))
22246 {
22247 goodfn = fn;
22248 ++good;
22249 }
22250 }
22251
22252 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22253 to function or pointer to member function argument if the set of
22254 overloaded functions does not contain function templates and at most
22255 one of a set of overloaded functions provides a unique match.
22256
22257 So if we found multiple possibilities, we return success but don't
22258 deduce anything. */
22259
22260 if (good == 1)
22261 {
22262 int i = TREE_VEC_LENGTH (targs);
22263 for (; i--; )
22264 if (TREE_VEC_ELT (tempargs, i))
22265 {
22266 tree old = TREE_VEC_ELT (targs, i);
22267 tree new_ = TREE_VEC_ELT (tempargs, i);
22268 if (new_ && old && ARGUMENT_PACK_P (old)
22269 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22270 /* Don't forget explicit template arguments in a pack. */
22271 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22272 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22273 TREE_VEC_ELT (targs, i) = new_;
22274 }
22275 }
22276 if (good)
22277 return true;
22278
22279 return false;
22280 }
22281
22282 /* Core DR 115: In contexts where deduction is done and fails, or in
22283 contexts where deduction is not done, if a template argument list is
22284 specified and it, along with any default template arguments, identifies
22285 a single function template specialization, then the template-id is an
22286 lvalue for the function template specialization. */
22287
22288 tree
22289 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22290 {
22291 tree expr, offset, baselink;
22292 bool addr;
22293
22294 if (!type_unknown_p (orig_expr))
22295 return orig_expr;
22296
22297 expr = orig_expr;
22298 addr = false;
22299 offset = NULL_TREE;
22300 baselink = NULL_TREE;
22301
22302 if (TREE_CODE (expr) == ADDR_EXPR)
22303 {
22304 expr = TREE_OPERAND (expr, 0);
22305 addr = true;
22306 }
22307 if (TREE_CODE (expr) == OFFSET_REF)
22308 {
22309 offset = expr;
22310 expr = TREE_OPERAND (expr, 1);
22311 }
22312 if (BASELINK_P (expr))
22313 {
22314 baselink = expr;
22315 expr = BASELINK_FUNCTIONS (expr);
22316 }
22317
22318 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22319 {
22320 int good = 0;
22321 tree goodfn = NULL_TREE;
22322
22323 /* If we got some explicit template args, we need to plug them into
22324 the affected templates before we try to unify, in case the
22325 explicit args will completely resolve the templates in question. */
22326
22327 tree expl_subargs = TREE_OPERAND (expr, 1);
22328 tree arg = TREE_OPERAND (expr, 0);
22329 tree badfn = NULL_TREE;
22330 tree badargs = NULL_TREE;
22331
22332 for (lkp_iterator iter (arg); iter; ++iter)
22333 {
22334 tree fn = *iter;
22335 tree subargs, elem;
22336
22337 if (TREE_CODE (fn) != TEMPLATE_DECL)
22338 continue;
22339
22340 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22341 expl_subargs, NULL_TREE, tf_none,
22342 /*require_all_args=*/true,
22343 /*use_default_args=*/true);
22344 if (subargs != error_mark_node
22345 && !any_dependent_template_arguments_p (subargs))
22346 {
22347 elem = instantiate_template (fn, subargs, tf_none);
22348 if (elem == error_mark_node)
22349 {
22350 badfn = fn;
22351 badargs = subargs;
22352 }
22353 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22354 && constraints_satisfied_p (elem))
22355 {
22356 goodfn = elem;
22357 ++good;
22358 }
22359 }
22360 }
22361 if (good == 1)
22362 {
22363 mark_used (goodfn);
22364 expr = goodfn;
22365 if (baselink)
22366 expr = build_baselink (BASELINK_BINFO (baselink),
22367 BASELINK_ACCESS_BINFO (baselink),
22368 expr, BASELINK_OPTYPE (baselink));
22369 if (offset)
22370 {
22371 tree base
22372 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22373 expr = build_offset_ref (base, expr, addr, complain);
22374 }
22375 if (addr)
22376 expr = cp_build_addr_expr (expr, complain);
22377 return expr;
22378 }
22379 else if (good == 0 && badargs && (complain & tf_error))
22380 /* There were no good options and at least one bad one, so let the
22381 user know what the problem is. */
22382 instantiate_template (badfn, badargs, complain);
22383 }
22384 return orig_expr;
22385 }
22386
22387 /* As above, but error out if the expression remains overloaded. */
22388
22389 tree
22390 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22391 {
22392 exp = resolve_nondeduced_context (exp, complain);
22393 if (type_unknown_p (exp))
22394 {
22395 if (complain & tf_error)
22396 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22397 return error_mark_node;
22398 }
22399 return exp;
22400 }
22401
22402 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22403 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22404 different overloads deduce different arguments for a given parm.
22405 ADDR_P is true if the expression for which deduction is being
22406 performed was of the form "& fn" rather than simply "fn".
22407
22408 Returns 1 on success. */
22409
22410 static int
22411 try_one_overload (tree tparms,
22412 tree orig_targs,
22413 tree targs,
22414 tree parm,
22415 tree arg,
22416 unification_kind_t strict,
22417 int sub_strict,
22418 bool addr_p,
22419 bool explain_p)
22420 {
22421 int nargs;
22422 tree tempargs;
22423 int i;
22424
22425 if (arg == error_mark_node)
22426 return 0;
22427
22428 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22429 to function or pointer to member function argument if the set of
22430 overloaded functions does not contain function templates and at most
22431 one of a set of overloaded functions provides a unique match.
22432
22433 So if this is a template, just return success. */
22434
22435 if (uses_template_parms (arg))
22436 return 1;
22437
22438 if (TREE_CODE (arg) == METHOD_TYPE)
22439 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22440 else if (addr_p)
22441 arg = build_pointer_type (arg);
22442
22443 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22444
22445 /* We don't copy orig_targs for this because if we have already deduced
22446 some template args from previous args, unify would complain when we
22447 try to deduce a template parameter for the same argument, even though
22448 there isn't really a conflict. */
22449 nargs = TREE_VEC_LENGTH (targs);
22450 tempargs = make_tree_vec (nargs);
22451
22452 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22453 return 0;
22454
22455 /* First make sure we didn't deduce anything that conflicts with
22456 explicitly specified args. */
22457 for (i = nargs; i--; )
22458 {
22459 tree elt = TREE_VEC_ELT (tempargs, i);
22460 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22461
22462 if (!elt)
22463 /*NOP*/;
22464 else if (uses_template_parms (elt))
22465 /* Since we're unifying against ourselves, we will fill in
22466 template args used in the function parm list with our own
22467 template parms. Discard them. */
22468 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22469 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22470 {
22471 /* Check that the argument at each index of the deduced argument pack
22472 is equivalent to the corresponding explicitly specified argument.
22473 We may have deduced more arguments than were explicitly specified,
22474 and that's OK. */
22475
22476 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22477 that's wrong if we deduce the same argument pack from multiple
22478 function arguments: it's only incomplete the first time. */
22479
22480 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22481 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22482
22483 if (TREE_VEC_LENGTH (deduced_pack)
22484 < TREE_VEC_LENGTH (explicit_pack))
22485 return 0;
22486
22487 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22488 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22489 TREE_VEC_ELT (deduced_pack, j)))
22490 return 0;
22491 }
22492 else if (oldelt && !template_args_equal (oldelt, elt))
22493 return 0;
22494 }
22495
22496 for (i = nargs; i--; )
22497 {
22498 tree elt = TREE_VEC_ELT (tempargs, i);
22499
22500 if (elt)
22501 TREE_VEC_ELT (targs, i) = elt;
22502 }
22503
22504 return 1;
22505 }
22506
22507 /* PARM is a template class (perhaps with unbound template
22508 parameters). ARG is a fully instantiated type. If ARG can be
22509 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22510 TARGS are as for unify. */
22511
22512 static tree
22513 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22514 bool explain_p)
22515 {
22516 tree copy_of_targs;
22517
22518 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22519 return NULL_TREE;
22520 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22521 /* Matches anything. */;
22522 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22523 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22524 return NULL_TREE;
22525
22526 /* We need to make a new template argument vector for the call to
22527 unify. If we used TARGS, we'd clutter it up with the result of
22528 the attempted unification, even if this class didn't work out.
22529 We also don't want to commit ourselves to all the unifications
22530 we've already done, since unification is supposed to be done on
22531 an argument-by-argument basis. In other words, consider the
22532 following pathological case:
22533
22534 template <int I, int J, int K>
22535 struct S {};
22536
22537 template <int I, int J>
22538 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22539
22540 template <int I, int J, int K>
22541 void f(S<I, J, K>, S<I, I, I>);
22542
22543 void g() {
22544 S<0, 0, 0> s0;
22545 S<0, 1, 2> s2;
22546
22547 f(s0, s2);
22548 }
22549
22550 Now, by the time we consider the unification involving `s2', we
22551 already know that we must have `f<0, 0, 0>'. But, even though
22552 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22553 because there are two ways to unify base classes of S<0, 1, 2>
22554 with S<I, I, I>. If we kept the already deduced knowledge, we
22555 would reject the possibility I=1. */
22556 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22557
22558 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22559 {
22560 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22561 return NULL_TREE;
22562 return arg;
22563 }
22564
22565 /* If unification failed, we're done. */
22566 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22567 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22568 return NULL_TREE;
22569
22570 return arg;
22571 }
22572
22573 /* Given a template type PARM and a class type ARG, find the unique
22574 base type in ARG that is an instance of PARM. We do not examine
22575 ARG itself; only its base-classes. If there is not exactly one
22576 appropriate base class, return NULL_TREE. PARM may be the type of
22577 a partial specialization, as well as a plain template type. Used
22578 by unify. */
22579
22580 static enum template_base_result
22581 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22582 bool explain_p, tree *result)
22583 {
22584 tree rval = NULL_TREE;
22585 tree binfo;
22586
22587 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22588
22589 binfo = TYPE_BINFO (complete_type (arg));
22590 if (!binfo)
22591 {
22592 /* The type could not be completed. */
22593 *result = NULL_TREE;
22594 return tbr_incomplete_type;
22595 }
22596
22597 /* Walk in inheritance graph order. The search order is not
22598 important, and this avoids multiple walks of virtual bases. */
22599 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22600 {
22601 tree r = try_class_unification (tparms, targs, parm,
22602 BINFO_TYPE (binfo), explain_p);
22603
22604 if (r)
22605 {
22606 /* If there is more than one satisfactory baseclass, then:
22607
22608 [temp.deduct.call]
22609
22610 If they yield more than one possible deduced A, the type
22611 deduction fails.
22612
22613 applies. */
22614 if (rval && !same_type_p (r, rval))
22615 {
22616 *result = NULL_TREE;
22617 return tbr_ambiguous_baseclass;
22618 }
22619
22620 rval = r;
22621 }
22622 }
22623
22624 *result = rval;
22625 return tbr_success;
22626 }
22627
22628 /* Returns the level of DECL, which declares a template parameter. */
22629
22630 static int
22631 template_decl_level (tree decl)
22632 {
22633 switch (TREE_CODE (decl))
22634 {
22635 case TYPE_DECL:
22636 case TEMPLATE_DECL:
22637 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22638
22639 case PARM_DECL:
22640 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22641
22642 default:
22643 gcc_unreachable ();
22644 }
22645 return 0;
22646 }
22647
22648 /* Decide whether ARG can be unified with PARM, considering only the
22649 cv-qualifiers of each type, given STRICT as documented for unify.
22650 Returns nonzero iff the unification is OK on that basis. */
22651
22652 static int
22653 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22654 {
22655 int arg_quals = cp_type_quals (arg);
22656 int parm_quals = cp_type_quals (parm);
22657
22658 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22659 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22660 {
22661 /* Although a CVR qualifier is ignored when being applied to a
22662 substituted template parameter ([8.3.2]/1 for example), that
22663 does not allow us to unify "const T" with "int&" because both
22664 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22665 It is ok when we're allowing additional CV qualifiers
22666 at the outer level [14.8.2.1]/3,1st bullet. */
22667 if ((TYPE_REF_P (arg)
22668 || FUNC_OR_METHOD_TYPE_P (arg))
22669 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22670 return 0;
22671
22672 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22673 && (parm_quals & TYPE_QUAL_RESTRICT))
22674 return 0;
22675 }
22676
22677 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22678 && (arg_quals & parm_quals) != parm_quals)
22679 return 0;
22680
22681 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22682 && (parm_quals & arg_quals) != arg_quals)
22683 return 0;
22684
22685 return 1;
22686 }
22687
22688 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22689 void
22690 template_parm_level_and_index (tree parm, int* level, int* index)
22691 {
22692 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22693 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22694 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22695 {
22696 *index = TEMPLATE_TYPE_IDX (parm);
22697 *level = TEMPLATE_TYPE_LEVEL (parm);
22698 }
22699 else
22700 {
22701 *index = TEMPLATE_PARM_IDX (parm);
22702 *level = TEMPLATE_PARM_LEVEL (parm);
22703 }
22704 }
22705
22706 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22707 do { \
22708 if (unify (TP, TA, P, A, S, EP)) \
22709 return 1; \
22710 } while (0)
22711
22712 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22713 expansion at the end of PACKED_PARMS. Returns 0 if the type
22714 deduction succeeds, 1 otherwise. STRICT is the same as in
22715 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22716 function call argument list. We'll need to adjust the arguments to make them
22717 types. SUBR tells us if this is from a recursive call to
22718 type_unification_real, or for comparing two template argument
22719 lists. */
22720
22721 static int
22722 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22723 tree packed_args, unification_kind_t strict,
22724 bool subr, bool explain_p)
22725 {
22726 tree parm
22727 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22728 tree pattern = PACK_EXPANSION_PATTERN (parm);
22729 tree pack, packs = NULL_TREE;
22730 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22731
22732 /* Add in any args remembered from an earlier partial instantiation. */
22733 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22734 int levels = TMPL_ARGS_DEPTH (targs);
22735
22736 packed_args = expand_template_argument_pack (packed_args);
22737
22738 int len = TREE_VEC_LENGTH (packed_args);
22739
22740 /* Determine the parameter packs we will be deducing from the
22741 pattern, and record their current deductions. */
22742 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22743 pack; pack = TREE_CHAIN (pack))
22744 {
22745 tree parm_pack = TREE_VALUE (pack);
22746 int idx, level;
22747
22748 /* Only template parameter packs can be deduced, not e.g. function
22749 parameter packs or __bases or __integer_pack. */
22750 if (!TEMPLATE_PARM_P (parm_pack))
22751 continue;
22752
22753 /* Determine the index and level of this parameter pack. */
22754 template_parm_level_and_index (parm_pack, &level, &idx);
22755 if (level < levels)
22756 continue;
22757
22758 /* Keep track of the parameter packs and their corresponding
22759 argument packs. */
22760 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22761 TREE_TYPE (packs) = make_tree_vec (len - start);
22762 }
22763
22764 /* Loop through all of the arguments that have not yet been
22765 unified and unify each with the pattern. */
22766 for (i = start; i < len; i++)
22767 {
22768 tree parm;
22769 bool any_explicit = false;
22770 tree arg = TREE_VEC_ELT (packed_args, i);
22771
22772 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22773 or the element of its argument pack at the current index if
22774 this argument was explicitly specified. */
22775 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22776 {
22777 int idx, level;
22778 tree arg, pargs;
22779 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22780
22781 arg = NULL_TREE;
22782 if (TREE_VALUE (pack)
22783 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22784 && (i - start < TREE_VEC_LENGTH (pargs)))
22785 {
22786 any_explicit = true;
22787 arg = TREE_VEC_ELT (pargs, i - start);
22788 }
22789 TMPL_ARG (targs, level, idx) = arg;
22790 }
22791
22792 /* If we had explicit template arguments, substitute them into the
22793 pattern before deduction. */
22794 if (any_explicit)
22795 {
22796 /* Some arguments might still be unspecified or dependent. */
22797 bool dependent;
22798 ++processing_template_decl;
22799 dependent = any_dependent_template_arguments_p (targs);
22800 if (!dependent)
22801 --processing_template_decl;
22802 parm = tsubst (pattern, targs,
22803 explain_p ? tf_warning_or_error : tf_none,
22804 NULL_TREE);
22805 if (dependent)
22806 --processing_template_decl;
22807 if (parm == error_mark_node)
22808 return 1;
22809 }
22810 else
22811 parm = pattern;
22812
22813 /* Unify the pattern with the current argument. */
22814 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22815 explain_p))
22816 return 1;
22817
22818 /* For each parameter pack, collect the deduced value. */
22819 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22820 {
22821 int idx, level;
22822 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22823
22824 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22825 TMPL_ARG (targs, level, idx);
22826 }
22827 }
22828
22829 /* Verify that the results of unification with the parameter packs
22830 produce results consistent with what we've seen before, and make
22831 the deduced argument packs available. */
22832 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22833 {
22834 tree old_pack = TREE_VALUE (pack);
22835 tree new_args = TREE_TYPE (pack);
22836 int i, len = TREE_VEC_LENGTH (new_args);
22837 int idx, level;
22838 bool nondeduced_p = false;
22839
22840 /* By default keep the original deduced argument pack.
22841 If necessary, more specific code is going to update the
22842 resulting deduced argument later down in this function. */
22843 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22844 TMPL_ARG (targs, level, idx) = old_pack;
22845
22846 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22847 actually deduce anything. */
22848 for (i = 0; i < len && !nondeduced_p; ++i)
22849 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22850 nondeduced_p = true;
22851 if (nondeduced_p)
22852 continue;
22853
22854 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22855 {
22856 /* If we had fewer function args than explicit template args,
22857 just use the explicits. */
22858 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22859 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22860 if (len < explicit_len)
22861 new_args = explicit_args;
22862 }
22863
22864 if (!old_pack)
22865 {
22866 tree result;
22867 /* Build the deduced *_ARGUMENT_PACK. */
22868 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22869 {
22870 result = make_node (NONTYPE_ARGUMENT_PACK);
22871 TREE_CONSTANT (result) = 1;
22872 }
22873 else
22874 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22875
22876 SET_ARGUMENT_PACK_ARGS (result, new_args);
22877
22878 /* Note the deduced argument packs for this parameter
22879 pack. */
22880 TMPL_ARG (targs, level, idx) = result;
22881 }
22882 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22883 && (ARGUMENT_PACK_ARGS (old_pack)
22884 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22885 {
22886 /* We only had the explicitly-provided arguments before, but
22887 now we have a complete set of arguments. */
22888 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22889
22890 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22891 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22892 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22893 }
22894 else
22895 {
22896 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22897 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22898 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
22899 /* During template argument deduction for the aggregate deduction
22900 candidate, the number of elements in a trailing parameter pack
22901 is only deduced from the number of remaining function
22902 arguments if it is not otherwise deduced. */
22903 if (cxx_dialect >= cxx20
22904 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
22905 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
22906 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
22907 if (!comp_template_args (old_args, new_args,
22908 &bad_old_arg, &bad_new_arg))
22909 /* Inconsistent unification of this parameter pack. */
22910 return unify_parameter_pack_inconsistent (explain_p,
22911 bad_old_arg,
22912 bad_new_arg);
22913 }
22914 }
22915
22916 return unify_success (explain_p);
22917 }
22918
22919 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22920 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22921 parameters and return value are as for unify. */
22922
22923 static int
22924 unify_array_domain (tree tparms, tree targs,
22925 tree parm_dom, tree arg_dom,
22926 bool explain_p)
22927 {
22928 tree parm_max;
22929 tree arg_max;
22930 bool parm_cst;
22931 bool arg_cst;
22932
22933 /* Our representation of array types uses "N - 1" as the
22934 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22935 not an integer constant. We cannot unify arbitrarily
22936 complex expressions, so we eliminate the MINUS_EXPRs
22937 here. */
22938 parm_max = TYPE_MAX_VALUE (parm_dom);
22939 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22940 if (!parm_cst)
22941 {
22942 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22943 parm_max = TREE_OPERAND (parm_max, 0);
22944 }
22945 arg_max = TYPE_MAX_VALUE (arg_dom);
22946 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22947 if (!arg_cst)
22948 {
22949 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22950 trying to unify the type of a variable with the type
22951 of a template parameter. For example:
22952
22953 template <unsigned int N>
22954 void f (char (&) [N]);
22955 int g();
22956 void h(int i) {
22957 char a[g(i)];
22958 f(a);
22959 }
22960
22961 Here, the type of the ARG will be "int [g(i)]", and
22962 may be a SAVE_EXPR, etc. */
22963 if (TREE_CODE (arg_max) != MINUS_EXPR)
22964 return unify_vla_arg (explain_p, arg_dom);
22965 arg_max = TREE_OPERAND (arg_max, 0);
22966 }
22967
22968 /* If only one of the bounds used a MINUS_EXPR, compensate
22969 by adding one to the other bound. */
22970 if (parm_cst && !arg_cst)
22971 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22972 integer_type_node,
22973 parm_max,
22974 integer_one_node);
22975 else if (arg_cst && !parm_cst)
22976 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22977 integer_type_node,
22978 arg_max,
22979 integer_one_node);
22980
22981 return unify (tparms, targs, parm_max, arg_max,
22982 UNIFY_ALLOW_INTEGER, explain_p);
22983 }
22984
22985 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22986
22987 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22988
22989 static pa_kind_t
22990 pa_kind (tree t)
22991 {
22992 if (PACK_EXPANSION_P (t))
22993 t = PACK_EXPANSION_PATTERN (t);
22994 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22995 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22996 || DECL_TYPE_TEMPLATE_P (t))
22997 return pa_tmpl;
22998 else if (TYPE_P (t))
22999 return pa_type;
23000 else
23001 return pa_expr;
23002 }
23003
23004 /* Deduce the value of template parameters. TPARMS is the (innermost)
23005 set of template parameters to a template. TARGS is the bindings
23006 for those template parameters, as determined thus far; TARGS may
23007 include template arguments for outer levels of template parameters
23008 as well. PARM is a parameter to a template function, or a
23009 subcomponent of that parameter; ARG is the corresponding argument.
23010 This function attempts to match PARM with ARG in a manner
23011 consistent with the existing assignments in TARGS. If more values
23012 are deduced, then TARGS is updated.
23013
23014 Returns 0 if the type deduction succeeds, 1 otherwise. The
23015 parameter STRICT is a bitwise or of the following flags:
23016
23017 UNIFY_ALLOW_NONE:
23018 Require an exact match between PARM and ARG.
23019 UNIFY_ALLOW_MORE_CV_QUAL:
23020 Allow the deduced ARG to be more cv-qualified (by qualification
23021 conversion) than ARG.
23022 UNIFY_ALLOW_LESS_CV_QUAL:
23023 Allow the deduced ARG to be less cv-qualified than ARG.
23024 UNIFY_ALLOW_DERIVED:
23025 Allow the deduced ARG to be a template base class of ARG,
23026 or a pointer to a template base class of the type pointed to by
23027 ARG.
23028 UNIFY_ALLOW_INTEGER:
23029 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23030 case for more information.
23031 UNIFY_ALLOW_OUTER_LEVEL:
23032 This is the outermost level of a deduction. Used to determine validity
23033 of qualification conversions. A valid qualification conversion must
23034 have const qualified pointers leading up to the inner type which
23035 requires additional CV quals, except at the outer level, where const
23036 is not required [conv.qual]. It would be normal to set this flag in
23037 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23038 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23039 This is the outermost level of a deduction, and PARM can be more CV
23040 qualified at this point.
23041 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23042 This is the outermost level of a deduction, and PARM can be less CV
23043 qualified at this point. */
23044
23045 static int
23046 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23047 bool explain_p)
23048 {
23049 int idx;
23050 tree targ;
23051 tree tparm;
23052 int strict_in = strict;
23053 tsubst_flags_t complain = (explain_p
23054 ? tf_warning_or_error
23055 : tf_none);
23056
23057 /* I don't think this will do the right thing with respect to types.
23058 But the only case I've seen it in so far has been array bounds, where
23059 signedness is the only information lost, and I think that will be
23060 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23061 finish_id_expression_1, and are also OK. */
23062 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23063 parm = TREE_OPERAND (parm, 0);
23064
23065 if (arg == error_mark_node)
23066 return unify_invalid (explain_p);
23067 if (arg == unknown_type_node
23068 || arg == init_list_type_node)
23069 /* We can't deduce anything from this, but we might get all the
23070 template args from other function args. */
23071 return unify_success (explain_p);
23072
23073 if (parm == any_targ_node || arg == any_targ_node)
23074 return unify_success (explain_p);
23075
23076 /* If PARM uses template parameters, then we can't bail out here,
23077 even if ARG == PARM, since we won't record unifications for the
23078 template parameters. We might need them if we're trying to
23079 figure out which of two things is more specialized. */
23080 if (arg == parm && !uses_template_parms (parm))
23081 return unify_success (explain_p);
23082
23083 /* Handle init lists early, so the rest of the function can assume
23084 we're dealing with a type. */
23085 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23086 {
23087 tree elt, elttype;
23088 unsigned i;
23089 tree orig_parm = parm;
23090
23091 if (!is_std_init_list (parm)
23092 && TREE_CODE (parm) != ARRAY_TYPE)
23093 /* We can only deduce from an initializer list argument if the
23094 parameter is std::initializer_list or an array; otherwise this
23095 is a non-deduced context. */
23096 return unify_success (explain_p);
23097
23098 if (TREE_CODE (parm) == ARRAY_TYPE)
23099 elttype = TREE_TYPE (parm);
23100 else
23101 {
23102 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23103 /* Deduction is defined in terms of a single type, so just punt
23104 on the (bizarre) std::initializer_list<T...>. */
23105 if (PACK_EXPANSION_P (elttype))
23106 return unify_success (explain_p);
23107 }
23108
23109 if (strict != DEDUCE_EXACT
23110 && TYPE_P (elttype)
23111 && !uses_deducible_template_parms (elttype))
23112 /* If ELTTYPE has no deducible template parms, skip deduction from
23113 the list elements. */;
23114 else
23115 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23116 {
23117 int elt_strict = strict;
23118
23119 if (elt == error_mark_node)
23120 return unify_invalid (explain_p);
23121
23122 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23123 {
23124 tree type = TREE_TYPE (elt);
23125 if (type == error_mark_node)
23126 return unify_invalid (explain_p);
23127 /* It should only be possible to get here for a call. */
23128 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23129 elt_strict |= maybe_adjust_types_for_deduction
23130 (DEDUCE_CALL, &elttype, &type, elt);
23131 elt = type;
23132 }
23133
23134 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23135 explain_p);
23136 }
23137
23138 if (TREE_CODE (parm) == ARRAY_TYPE
23139 && deducible_array_bound (TYPE_DOMAIN (parm)))
23140 {
23141 /* Also deduce from the length of the initializer list. */
23142 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23143 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23144 if (idx == error_mark_node)
23145 return unify_invalid (explain_p);
23146 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23147 idx, explain_p);
23148 }
23149
23150 /* If the std::initializer_list<T> deduction worked, replace the
23151 deduced A with std::initializer_list<A>. */
23152 if (orig_parm != parm)
23153 {
23154 idx = TEMPLATE_TYPE_IDX (orig_parm);
23155 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23156 targ = listify (targ);
23157 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23158 }
23159 return unify_success (explain_p);
23160 }
23161
23162 /* If parm and arg aren't the same kind of thing (template, type, or
23163 expression), fail early. */
23164 if (pa_kind (parm) != pa_kind (arg))
23165 return unify_invalid (explain_p);
23166
23167 /* Immediately reject some pairs that won't unify because of
23168 cv-qualification mismatches. */
23169 if (TREE_CODE (arg) == TREE_CODE (parm)
23170 && TYPE_P (arg)
23171 /* It is the elements of the array which hold the cv quals of an array
23172 type, and the elements might be template type parms. We'll check
23173 when we recurse. */
23174 && TREE_CODE (arg) != ARRAY_TYPE
23175 /* We check the cv-qualifiers when unifying with template type
23176 parameters below. We want to allow ARG `const T' to unify with
23177 PARM `T' for example, when computing which of two templates
23178 is more specialized, for example. */
23179 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23180 && !check_cv_quals_for_unify (strict_in, arg, parm))
23181 return unify_cv_qual_mismatch (explain_p, parm, arg);
23182
23183 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23184 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23185 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23186 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23187 strict &= ~UNIFY_ALLOW_DERIVED;
23188 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23189 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23190
23191 switch (TREE_CODE (parm))
23192 {
23193 case TYPENAME_TYPE:
23194 case SCOPE_REF:
23195 case UNBOUND_CLASS_TEMPLATE:
23196 /* In a type which contains a nested-name-specifier, template
23197 argument values cannot be deduced for template parameters used
23198 within the nested-name-specifier. */
23199 return unify_success (explain_p);
23200
23201 case TEMPLATE_TYPE_PARM:
23202 case TEMPLATE_TEMPLATE_PARM:
23203 case BOUND_TEMPLATE_TEMPLATE_PARM:
23204 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23205 if (error_operand_p (tparm))
23206 return unify_invalid (explain_p);
23207
23208 if (TEMPLATE_TYPE_LEVEL (parm)
23209 != template_decl_level (tparm))
23210 /* The PARM is not one we're trying to unify. Just check
23211 to see if it matches ARG. */
23212 {
23213 if (TREE_CODE (arg) == TREE_CODE (parm)
23214 && (is_auto (parm) ? is_auto (arg)
23215 : same_type_p (parm, arg)))
23216 return unify_success (explain_p);
23217 else
23218 return unify_type_mismatch (explain_p, parm, arg);
23219 }
23220 idx = TEMPLATE_TYPE_IDX (parm);
23221 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23222 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23223 if (error_operand_p (tparm))
23224 return unify_invalid (explain_p);
23225
23226 /* Check for mixed types and values. */
23227 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23228 && TREE_CODE (tparm) != TYPE_DECL)
23229 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23230 && TREE_CODE (tparm) != TEMPLATE_DECL))
23231 gcc_unreachable ();
23232
23233 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23234 {
23235 if ((strict_in & UNIFY_ALLOW_DERIVED)
23236 && CLASS_TYPE_P (arg))
23237 {
23238 /* First try to match ARG directly. */
23239 tree t = try_class_unification (tparms, targs, parm, arg,
23240 explain_p);
23241 if (!t)
23242 {
23243 /* Otherwise, look for a suitable base of ARG, as below. */
23244 enum template_base_result r;
23245 r = get_template_base (tparms, targs, parm, arg,
23246 explain_p, &t);
23247 if (!t)
23248 return unify_no_common_base (explain_p, r, parm, arg);
23249 arg = t;
23250 }
23251 }
23252 /* ARG must be constructed from a template class or a template
23253 template parameter. */
23254 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23255 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23256 return unify_template_deduction_failure (explain_p, parm, arg);
23257
23258 /* Deduce arguments T, i from TT<T> or TT<i>. */
23259 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23260 return 1;
23261
23262 arg = TYPE_TI_TEMPLATE (arg);
23263
23264 /* Fall through to deduce template name. */
23265 }
23266
23267 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23268 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23269 {
23270 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23271
23272 /* Simple cases: Value already set, does match or doesn't. */
23273 if (targ != NULL_TREE && template_args_equal (targ, arg))
23274 return unify_success (explain_p);
23275 else if (targ)
23276 return unify_inconsistency (explain_p, parm, targ, arg);
23277 }
23278 else
23279 {
23280 /* If PARM is `const T' and ARG is only `int', we don't have
23281 a match unless we are allowing additional qualification.
23282 If ARG is `const int' and PARM is just `T' that's OK;
23283 that binds `const int' to `T'. */
23284 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23285 arg, parm))
23286 return unify_cv_qual_mismatch (explain_p, parm, arg);
23287
23288 /* Consider the case where ARG is `const volatile int' and
23289 PARM is `const T'. Then, T should be `volatile int'. */
23290 arg = cp_build_qualified_type_real
23291 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23292 if (arg == error_mark_node)
23293 return unify_invalid (explain_p);
23294
23295 /* Simple cases: Value already set, does match or doesn't. */
23296 if (targ != NULL_TREE && same_type_p (targ, arg))
23297 return unify_success (explain_p);
23298 else if (targ)
23299 return unify_inconsistency (explain_p, parm, targ, arg);
23300
23301 /* Make sure that ARG is not a variable-sized array. (Note
23302 that were talking about variable-sized arrays (like
23303 `int[n]'), rather than arrays of unknown size (like
23304 `int[]').) We'll get very confused by such a type since
23305 the bound of the array is not constant, and therefore
23306 not mangleable. Besides, such types are not allowed in
23307 ISO C++, so we can do as we please here. We do allow
23308 them for 'auto' deduction, since that isn't ABI-exposed. */
23309 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23310 return unify_vla_arg (explain_p, arg);
23311
23312 /* Strip typedefs as in convert_template_argument. */
23313 arg = canonicalize_type_argument (arg, tf_none);
23314 }
23315
23316 /* If ARG is a parameter pack or an expansion, we cannot unify
23317 against it unless PARM is also a parameter pack. */
23318 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23319 && !template_parameter_pack_p (parm))
23320 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23321
23322 /* If the argument deduction results is a METHOD_TYPE,
23323 then there is a problem.
23324 METHOD_TYPE doesn't map to any real C++ type the result of
23325 the deduction cannot be of that type. */
23326 if (TREE_CODE (arg) == METHOD_TYPE)
23327 return unify_method_type_error (explain_p, arg);
23328
23329 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23330 return unify_success (explain_p);
23331
23332 case TEMPLATE_PARM_INDEX:
23333 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23334 if (error_operand_p (tparm))
23335 return unify_invalid (explain_p);
23336
23337 if (TEMPLATE_PARM_LEVEL (parm)
23338 != template_decl_level (tparm))
23339 {
23340 /* The PARM is not one we're trying to unify. Just check
23341 to see if it matches ARG. */
23342 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23343 && cp_tree_equal (parm, arg));
23344 if (result)
23345 unify_expression_unequal (explain_p, parm, arg);
23346 return result;
23347 }
23348
23349 idx = TEMPLATE_PARM_IDX (parm);
23350 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23351
23352 if (targ)
23353 {
23354 if ((strict & UNIFY_ALLOW_INTEGER)
23355 && TREE_TYPE (targ) && TREE_TYPE (arg)
23356 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23357 /* We're deducing from an array bound, the type doesn't matter. */
23358 arg = fold_convert (TREE_TYPE (targ), arg);
23359 int x = !cp_tree_equal (targ, arg);
23360 if (x)
23361 unify_inconsistency (explain_p, parm, targ, arg);
23362 return x;
23363 }
23364
23365 /* [temp.deduct.type] If, in the declaration of a function template
23366 with a non-type template-parameter, the non-type
23367 template-parameter is used in an expression in the function
23368 parameter-list and, if the corresponding template-argument is
23369 deduced, the template-argument type shall match the type of the
23370 template-parameter exactly, except that a template-argument
23371 deduced from an array bound may be of any integral type.
23372 The non-type parameter might use already deduced type parameters. */
23373 tparm = TREE_TYPE (parm);
23374 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23375 /* We don't have enough levels of args to do any substitution. This
23376 can happen in the context of -fnew-ttp-matching. */;
23377 else
23378 {
23379 ++processing_template_decl;
23380 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23381 --processing_template_decl;
23382
23383 if (tree a = type_uses_auto (tparm))
23384 {
23385 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23386 if (tparm == error_mark_node)
23387 return 1;
23388 }
23389 }
23390
23391 if (!TREE_TYPE (arg))
23392 /* Template-parameter dependent expression. Just accept it for now.
23393 It will later be processed in convert_template_argument. */
23394 ;
23395 else if (same_type_ignoring_top_level_qualifiers_p
23396 (non_reference (TREE_TYPE (arg)),
23397 non_reference (tparm)))
23398 /* OK. Ignore top-level quals here because a class-type template
23399 parameter object is const. */;
23400 else if ((strict & UNIFY_ALLOW_INTEGER)
23401 && CP_INTEGRAL_TYPE_P (tparm))
23402 /* Convert the ARG to the type of PARM; the deduced non-type
23403 template argument must exactly match the types of the
23404 corresponding parameter. */
23405 arg = fold (build_nop (tparm, arg));
23406 else if (uses_template_parms (tparm))
23407 {
23408 /* We haven't deduced the type of this parameter yet. */
23409 if (cxx_dialect >= cxx17
23410 /* We deduce from array bounds in try_array_deduction. */
23411 && !(strict & UNIFY_ALLOW_INTEGER))
23412 {
23413 /* Deduce it from the non-type argument. */
23414 tree atype = TREE_TYPE (arg);
23415 RECUR_AND_CHECK_FAILURE (tparms, targs,
23416 tparm, atype,
23417 UNIFY_ALLOW_NONE, explain_p);
23418 }
23419 else
23420 /* Try again later. */
23421 return unify_success (explain_p);
23422 }
23423 else
23424 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23425
23426 /* If ARG is a parameter pack or an expansion, we cannot unify
23427 against it unless PARM is also a parameter pack. */
23428 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23429 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23430 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23431
23432 {
23433 bool removed_attr = false;
23434 arg = strip_typedefs_expr (arg, &removed_attr);
23435 }
23436 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23437 return unify_success (explain_p);
23438
23439 case PTRMEM_CST:
23440 {
23441 /* A pointer-to-member constant can be unified only with
23442 another constant. */
23443 if (TREE_CODE (arg) != PTRMEM_CST)
23444 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23445
23446 /* Just unify the class member. It would be useless (and possibly
23447 wrong, depending on the strict flags) to unify also
23448 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23449 arg refer to the same variable, even if through different
23450 classes. For instance:
23451
23452 struct A { int x; };
23453 struct B : A { };
23454
23455 Unification of &A::x and &B::x must succeed. */
23456 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23457 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23458 }
23459
23460 case POINTER_TYPE:
23461 {
23462 if (!TYPE_PTR_P (arg))
23463 return unify_type_mismatch (explain_p, parm, arg);
23464
23465 /* [temp.deduct.call]
23466
23467 A can be another pointer or pointer to member type that can
23468 be converted to the deduced A via a qualification
23469 conversion (_conv.qual_).
23470
23471 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23472 This will allow for additional cv-qualification of the
23473 pointed-to types if appropriate. */
23474
23475 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23476 /* The derived-to-base conversion only persists through one
23477 level of pointers. */
23478 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23479
23480 return unify (tparms, targs, TREE_TYPE (parm),
23481 TREE_TYPE (arg), strict, explain_p);
23482 }
23483
23484 case REFERENCE_TYPE:
23485 if (!TYPE_REF_P (arg))
23486 return unify_type_mismatch (explain_p, parm, arg);
23487 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23488 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23489
23490 case ARRAY_TYPE:
23491 if (TREE_CODE (arg) != ARRAY_TYPE)
23492 return unify_type_mismatch (explain_p, parm, arg);
23493 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23494 != (TYPE_DOMAIN (arg) == NULL_TREE))
23495 return unify_type_mismatch (explain_p, parm, arg);
23496 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23497 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23498 if (TYPE_DOMAIN (parm) != NULL_TREE)
23499 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23500 TYPE_DOMAIN (arg), explain_p);
23501 return unify_success (explain_p);
23502
23503 case REAL_TYPE:
23504 case COMPLEX_TYPE:
23505 case VECTOR_TYPE:
23506 case INTEGER_TYPE:
23507 case BOOLEAN_TYPE:
23508 case ENUMERAL_TYPE:
23509 case VOID_TYPE:
23510 case NULLPTR_TYPE:
23511 if (TREE_CODE (arg) != TREE_CODE (parm))
23512 return unify_type_mismatch (explain_p, parm, arg);
23513
23514 /* We have already checked cv-qualification at the top of the
23515 function. */
23516 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23517 return unify_type_mismatch (explain_p, parm, arg);
23518
23519 /* As far as unification is concerned, this wins. Later checks
23520 will invalidate it if necessary. */
23521 return unify_success (explain_p);
23522
23523 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23524 /* Type INTEGER_CST can come from ordinary constant template args. */
23525 case INTEGER_CST:
23526 while (CONVERT_EXPR_P (arg))
23527 arg = TREE_OPERAND (arg, 0);
23528
23529 if (TREE_CODE (arg) != INTEGER_CST)
23530 return unify_template_argument_mismatch (explain_p, parm, arg);
23531 return (tree_int_cst_equal (parm, arg)
23532 ? unify_success (explain_p)
23533 : unify_template_argument_mismatch (explain_p, parm, arg));
23534
23535 case TREE_VEC:
23536 {
23537 int i, len, argslen;
23538 int parm_variadic_p = 0;
23539
23540 if (TREE_CODE (arg) != TREE_VEC)
23541 return unify_template_argument_mismatch (explain_p, parm, arg);
23542
23543 len = TREE_VEC_LENGTH (parm);
23544 argslen = TREE_VEC_LENGTH (arg);
23545
23546 /* Check for pack expansions in the parameters. */
23547 for (i = 0; i < len; ++i)
23548 {
23549 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23550 {
23551 if (i == len - 1)
23552 /* We can unify against something with a trailing
23553 parameter pack. */
23554 parm_variadic_p = 1;
23555 else
23556 /* [temp.deduct.type]/9: If the template argument list of
23557 P contains a pack expansion that is not the last
23558 template argument, the entire template argument list
23559 is a non-deduced context. */
23560 return unify_success (explain_p);
23561 }
23562 }
23563
23564 /* If we don't have enough arguments to satisfy the parameters
23565 (not counting the pack expression at the end), or we have
23566 too many arguments for a parameter list that doesn't end in
23567 a pack expression, we can't unify. */
23568 if (parm_variadic_p
23569 ? argslen < len - parm_variadic_p
23570 : argslen != len)
23571 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23572
23573 /* Unify all of the parameters that precede the (optional)
23574 pack expression. */
23575 for (i = 0; i < len - parm_variadic_p; ++i)
23576 {
23577 RECUR_AND_CHECK_FAILURE (tparms, targs,
23578 TREE_VEC_ELT (parm, i),
23579 TREE_VEC_ELT (arg, i),
23580 UNIFY_ALLOW_NONE, explain_p);
23581 }
23582 if (parm_variadic_p)
23583 return unify_pack_expansion (tparms, targs, parm, arg,
23584 DEDUCE_EXACT,
23585 /*subr=*/true, explain_p);
23586 return unify_success (explain_p);
23587 }
23588
23589 case RECORD_TYPE:
23590 case UNION_TYPE:
23591 if (TREE_CODE (arg) != TREE_CODE (parm))
23592 return unify_type_mismatch (explain_p, parm, arg);
23593
23594 if (TYPE_PTRMEMFUNC_P (parm))
23595 {
23596 if (!TYPE_PTRMEMFUNC_P (arg))
23597 return unify_type_mismatch (explain_p, parm, arg);
23598
23599 return unify (tparms, targs,
23600 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23601 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23602 strict, explain_p);
23603 }
23604 else if (TYPE_PTRMEMFUNC_P (arg))
23605 return unify_type_mismatch (explain_p, parm, arg);
23606
23607 if (CLASSTYPE_TEMPLATE_INFO (parm))
23608 {
23609 tree t = NULL_TREE;
23610
23611 if (strict_in & UNIFY_ALLOW_DERIVED)
23612 {
23613 /* First, we try to unify the PARM and ARG directly. */
23614 t = try_class_unification (tparms, targs,
23615 parm, arg, explain_p);
23616
23617 if (!t)
23618 {
23619 /* Fallback to the special case allowed in
23620 [temp.deduct.call]:
23621
23622 If P is a class, and P has the form
23623 template-id, then A can be a derived class of
23624 the deduced A. Likewise, if P is a pointer to
23625 a class of the form template-id, A can be a
23626 pointer to a derived class pointed to by the
23627 deduced A. */
23628 enum template_base_result r;
23629 r = get_template_base (tparms, targs, parm, arg,
23630 explain_p, &t);
23631
23632 if (!t)
23633 {
23634 /* Don't give the derived diagnostic if we're
23635 already dealing with the same template. */
23636 bool same_template
23637 = (CLASSTYPE_TEMPLATE_INFO (arg)
23638 && (CLASSTYPE_TI_TEMPLATE (parm)
23639 == CLASSTYPE_TI_TEMPLATE (arg)));
23640 return unify_no_common_base (explain_p && !same_template,
23641 r, parm, arg);
23642 }
23643 }
23644 }
23645 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23646 && (CLASSTYPE_TI_TEMPLATE (parm)
23647 == CLASSTYPE_TI_TEMPLATE (arg)))
23648 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23649 Then, we should unify `int' and `U'. */
23650 t = arg;
23651 else
23652 /* There's no chance of unification succeeding. */
23653 return unify_type_mismatch (explain_p, parm, arg);
23654
23655 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23656 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23657 }
23658 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23659 return unify_type_mismatch (explain_p, parm, arg);
23660 return unify_success (explain_p);
23661
23662 case METHOD_TYPE:
23663 case FUNCTION_TYPE:
23664 {
23665 unsigned int nargs;
23666 tree *args;
23667 tree a;
23668 unsigned int i;
23669
23670 if (TREE_CODE (arg) != TREE_CODE (parm))
23671 return unify_type_mismatch (explain_p, parm, arg);
23672
23673 /* CV qualifications for methods can never be deduced, they must
23674 match exactly. We need to check them explicitly here,
23675 because type_unification_real treats them as any other
23676 cv-qualified parameter. */
23677 if (TREE_CODE (parm) == METHOD_TYPE
23678 && (!check_cv_quals_for_unify
23679 (UNIFY_ALLOW_NONE,
23680 class_of_this_parm (arg),
23681 class_of_this_parm (parm))))
23682 return unify_cv_qual_mismatch (explain_p, parm, arg);
23683 if (TREE_CODE (arg) == FUNCTION_TYPE
23684 && type_memfn_quals (parm) != type_memfn_quals (arg))
23685 return unify_cv_qual_mismatch (explain_p, parm, arg);
23686 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23687 return unify_type_mismatch (explain_p, parm, arg);
23688
23689 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23690 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23691
23692 nargs = list_length (TYPE_ARG_TYPES (arg));
23693 args = XALLOCAVEC (tree, nargs);
23694 for (a = TYPE_ARG_TYPES (arg), i = 0;
23695 a != NULL_TREE && a != void_list_node;
23696 a = TREE_CHAIN (a), ++i)
23697 args[i] = TREE_VALUE (a);
23698 nargs = i;
23699
23700 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23701 args, nargs, 1, DEDUCE_EXACT,
23702 NULL, explain_p))
23703 return 1;
23704
23705 if (flag_noexcept_type)
23706 {
23707 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23708 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23709 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23710 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23711 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23712 && uses_template_parms (TREE_PURPOSE (pspec)))
23713 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23714 TREE_PURPOSE (aspec),
23715 UNIFY_ALLOW_NONE, explain_p);
23716 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23717 return unify_type_mismatch (explain_p, parm, arg);
23718 }
23719
23720 return 0;
23721 }
23722
23723 case OFFSET_TYPE:
23724 /* Unify a pointer to member with a pointer to member function, which
23725 deduces the type of the member as a function type. */
23726 if (TYPE_PTRMEMFUNC_P (arg))
23727 {
23728 /* Check top-level cv qualifiers */
23729 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23730 return unify_cv_qual_mismatch (explain_p, parm, arg);
23731
23732 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23733 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23734 UNIFY_ALLOW_NONE, explain_p);
23735
23736 /* Determine the type of the function we are unifying against. */
23737 tree fntype = static_fn_type (arg);
23738
23739 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23740 }
23741
23742 if (TREE_CODE (arg) != OFFSET_TYPE)
23743 return unify_type_mismatch (explain_p, parm, arg);
23744 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23745 TYPE_OFFSET_BASETYPE (arg),
23746 UNIFY_ALLOW_NONE, explain_p);
23747 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23748 strict, explain_p);
23749
23750 case CONST_DECL:
23751 if (DECL_TEMPLATE_PARM_P (parm))
23752 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23753 if (arg != scalar_constant_value (parm))
23754 return unify_template_argument_mismatch (explain_p, parm, arg);
23755 return unify_success (explain_p);
23756
23757 case FIELD_DECL:
23758 case TEMPLATE_DECL:
23759 /* Matched cases are handled by the ARG == PARM test above. */
23760 return unify_template_argument_mismatch (explain_p, parm, arg);
23761
23762 case VAR_DECL:
23763 /* We might get a variable as a non-type template argument in parm if the
23764 corresponding parameter is type-dependent. Make any necessary
23765 adjustments based on whether arg is a reference. */
23766 if (CONSTANT_CLASS_P (arg))
23767 parm = fold_non_dependent_expr (parm, complain);
23768 else if (REFERENCE_REF_P (arg))
23769 {
23770 tree sub = TREE_OPERAND (arg, 0);
23771 STRIP_NOPS (sub);
23772 if (TREE_CODE (sub) == ADDR_EXPR)
23773 arg = TREE_OPERAND (sub, 0);
23774 }
23775 /* Now use the normal expression code to check whether they match. */
23776 goto expr;
23777
23778 case TYPE_ARGUMENT_PACK:
23779 case NONTYPE_ARGUMENT_PACK:
23780 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23781 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23782
23783 case TYPEOF_TYPE:
23784 case DECLTYPE_TYPE:
23785 case UNDERLYING_TYPE:
23786 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23787 or UNDERLYING_TYPE nodes. */
23788 return unify_success (explain_p);
23789
23790 case ERROR_MARK:
23791 /* Unification fails if we hit an error node. */
23792 return unify_invalid (explain_p);
23793
23794 case INDIRECT_REF:
23795 if (REFERENCE_REF_P (parm))
23796 {
23797 bool pexp = PACK_EXPANSION_P (arg);
23798 if (pexp)
23799 arg = PACK_EXPANSION_PATTERN (arg);
23800 if (REFERENCE_REF_P (arg))
23801 arg = TREE_OPERAND (arg, 0);
23802 if (pexp)
23803 arg = make_pack_expansion (arg, complain);
23804 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23805 strict, explain_p);
23806 }
23807 /* FALLTHRU */
23808
23809 default:
23810 /* An unresolved overload is a nondeduced context. */
23811 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23812 return unify_success (explain_p);
23813 gcc_assert (EXPR_P (parm)
23814 || COMPOUND_LITERAL_P (parm)
23815 || TREE_CODE (parm) == TRAIT_EXPR);
23816 expr:
23817 /* We must be looking at an expression. This can happen with
23818 something like:
23819
23820 template <int I>
23821 void foo(S<I>, S<I + 2>);
23822
23823 or
23824
23825 template<typename T>
23826 void foo(A<T, T{}>);
23827
23828 This is a "non-deduced context":
23829
23830 [deduct.type]
23831
23832 The non-deduced contexts are:
23833
23834 --A non-type template argument or an array bound in which
23835 a subexpression references a template parameter.
23836
23837 In these cases, we assume deduction succeeded, but don't
23838 actually infer any unifications. */
23839
23840 if (!uses_template_parms (parm)
23841 && !template_args_equal (parm, arg))
23842 return unify_expression_unequal (explain_p, parm, arg);
23843 else
23844 return unify_success (explain_p);
23845 }
23846 }
23847 #undef RECUR_AND_CHECK_FAILURE
23848 \f
23849 /* Note that DECL can be defined in this translation unit, if
23850 required. */
23851
23852 static void
23853 mark_definable (tree decl)
23854 {
23855 tree clone;
23856 DECL_NOT_REALLY_EXTERN (decl) = 1;
23857 FOR_EACH_CLONE (clone, decl)
23858 DECL_NOT_REALLY_EXTERN (clone) = 1;
23859 }
23860
23861 /* Called if RESULT is explicitly instantiated, or is a member of an
23862 explicitly instantiated class. */
23863
23864 void
23865 mark_decl_instantiated (tree result, int extern_p)
23866 {
23867 SET_DECL_EXPLICIT_INSTANTIATION (result);
23868
23869 /* If this entity has already been written out, it's too late to
23870 make any modifications. */
23871 if (TREE_ASM_WRITTEN (result))
23872 return;
23873
23874 /* For anonymous namespace we don't need to do anything. */
23875 if (decl_anon_ns_mem_p (result))
23876 {
23877 gcc_assert (!TREE_PUBLIC (result));
23878 return;
23879 }
23880
23881 if (TREE_CODE (result) != FUNCTION_DECL)
23882 /* The TREE_PUBLIC flag for function declarations will have been
23883 set correctly by tsubst. */
23884 TREE_PUBLIC (result) = 1;
23885
23886 /* This might have been set by an earlier implicit instantiation. */
23887 DECL_COMDAT (result) = 0;
23888
23889 if (extern_p)
23890 DECL_NOT_REALLY_EXTERN (result) = 0;
23891 else
23892 {
23893 mark_definable (result);
23894 mark_needed (result);
23895 /* Always make artificials weak. */
23896 if (DECL_ARTIFICIAL (result) && flag_weak)
23897 comdat_linkage (result);
23898 /* For WIN32 we also want to put explicit instantiations in
23899 linkonce sections. */
23900 else if (TREE_PUBLIC (result))
23901 maybe_make_one_only (result);
23902 if (TREE_CODE (result) == FUNCTION_DECL
23903 && DECL_TEMPLATE_INSTANTIATED (result))
23904 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23905 since start_preparsed_function wouldn't have if we had an earlier
23906 extern explicit instantiation. */
23907 DECL_EXTERNAL (result) = 0;
23908 }
23909
23910 /* If EXTERN_P, then this function will not be emitted -- unless
23911 followed by an explicit instantiation, at which point its linkage
23912 will be adjusted. If !EXTERN_P, then this function will be
23913 emitted here. In neither circumstance do we want
23914 import_export_decl to adjust the linkage. */
23915 DECL_INTERFACE_KNOWN (result) = 1;
23916 }
23917
23918 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23919 important template arguments. If any are missing, we check whether
23920 they're important by using error_mark_node for substituting into any
23921 args that were used for partial ordering (the ones between ARGS and END)
23922 and seeing if it bubbles up. */
23923
23924 static bool
23925 check_undeduced_parms (tree targs, tree args, tree end)
23926 {
23927 bool found = false;
23928 int i;
23929 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23930 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23931 {
23932 found = true;
23933 TREE_VEC_ELT (targs, i) = error_mark_node;
23934 }
23935 if (found)
23936 {
23937 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23938 if (substed == error_mark_node)
23939 return true;
23940 }
23941 return false;
23942 }
23943
23944 /* Given two function templates PAT1 and PAT2, return:
23945
23946 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23947 -1 if PAT2 is more specialized than PAT1.
23948 0 if neither is more specialized.
23949
23950 LEN indicates the number of parameters we should consider
23951 (defaulted parameters should not be considered).
23952
23953 The 1998 std underspecified function template partial ordering, and
23954 DR214 addresses the issue. We take pairs of arguments, one from
23955 each of the templates, and deduce them against each other. One of
23956 the templates will be more specialized if all the *other*
23957 template's arguments deduce against its arguments and at least one
23958 of its arguments *does* *not* deduce against the other template's
23959 corresponding argument. Deduction is done as for class templates.
23960 The arguments used in deduction have reference and top level cv
23961 qualifiers removed. Iff both arguments were originally reference
23962 types *and* deduction succeeds in both directions, an lvalue reference
23963 wins against an rvalue reference and otherwise the template
23964 with the more cv-qualified argument wins for that pairing (if
23965 neither is more cv-qualified, they both are equal). Unlike regular
23966 deduction, after all the arguments have been deduced in this way,
23967 we do *not* verify the deduced template argument values can be
23968 substituted into non-deduced contexts.
23969
23970 The logic can be a bit confusing here, because we look at deduce1 and
23971 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23972 can find template arguments for pat1 to make arg1 look like arg2, that
23973 means that arg2 is at least as specialized as arg1. */
23974
23975 int
23976 more_specialized_fn (tree pat1, tree pat2, int len)
23977 {
23978 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23979 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23980 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23981 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23982 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23983 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23984 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23985 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23986 tree origs1, origs2;
23987 bool lose1 = false;
23988 bool lose2 = false;
23989
23990 /* Remove the this parameter from non-static member functions. If
23991 one is a non-static member function and the other is not a static
23992 member function, remove the first parameter from that function
23993 also. This situation occurs for operator functions where we
23994 locate both a member function (with this pointer) and non-member
23995 operator (with explicit first operand). */
23996 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23997 {
23998 len--; /* LEN is the number of significant arguments for DECL1 */
23999 args1 = TREE_CHAIN (args1);
24000 if (!DECL_STATIC_FUNCTION_P (decl2))
24001 args2 = TREE_CHAIN (args2);
24002 }
24003 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24004 {
24005 args2 = TREE_CHAIN (args2);
24006 if (!DECL_STATIC_FUNCTION_P (decl1))
24007 {
24008 len--;
24009 args1 = TREE_CHAIN (args1);
24010 }
24011 }
24012
24013 /* If only one is a conversion operator, they are unordered. */
24014 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24015 return 0;
24016
24017 /* Consider the return type for a conversion function */
24018 if (DECL_CONV_FN_P (decl1))
24019 {
24020 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24021 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24022 len++;
24023 }
24024
24025 processing_template_decl++;
24026
24027 origs1 = args1;
24028 origs2 = args2;
24029
24030 while (len--
24031 /* Stop when an ellipsis is seen. */
24032 && args1 != NULL_TREE && args2 != NULL_TREE)
24033 {
24034 tree arg1 = TREE_VALUE (args1);
24035 tree arg2 = TREE_VALUE (args2);
24036 int deduce1, deduce2;
24037 int quals1 = -1;
24038 int quals2 = -1;
24039 int ref1 = 0;
24040 int ref2 = 0;
24041
24042 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24043 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24044 {
24045 /* When both arguments are pack expansions, we need only
24046 unify the patterns themselves. */
24047 arg1 = PACK_EXPANSION_PATTERN (arg1);
24048 arg2 = PACK_EXPANSION_PATTERN (arg2);
24049
24050 /* This is the last comparison we need to do. */
24051 len = 0;
24052 }
24053
24054 if (TYPE_REF_P (arg1))
24055 {
24056 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24057 arg1 = TREE_TYPE (arg1);
24058 quals1 = cp_type_quals (arg1);
24059 }
24060
24061 if (TYPE_REF_P (arg2))
24062 {
24063 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24064 arg2 = TREE_TYPE (arg2);
24065 quals2 = cp_type_quals (arg2);
24066 }
24067
24068 arg1 = TYPE_MAIN_VARIANT (arg1);
24069 arg2 = TYPE_MAIN_VARIANT (arg2);
24070
24071 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24072 {
24073 int i, len2 = remaining_arguments (args2);
24074 tree parmvec = make_tree_vec (1);
24075 tree argvec = make_tree_vec (len2);
24076 tree ta = args2;
24077
24078 /* Setup the parameter vector, which contains only ARG1. */
24079 TREE_VEC_ELT (parmvec, 0) = arg1;
24080
24081 /* Setup the argument vector, which contains the remaining
24082 arguments. */
24083 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24084 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24085
24086 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24087 argvec, DEDUCE_EXACT,
24088 /*subr=*/true, /*explain_p=*/false)
24089 == 0);
24090
24091 /* We cannot deduce in the other direction, because ARG1 is
24092 a pack expansion but ARG2 is not. */
24093 deduce2 = 0;
24094 }
24095 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24096 {
24097 int i, len1 = remaining_arguments (args1);
24098 tree parmvec = make_tree_vec (1);
24099 tree argvec = make_tree_vec (len1);
24100 tree ta = args1;
24101
24102 /* Setup the parameter vector, which contains only ARG1. */
24103 TREE_VEC_ELT (parmvec, 0) = arg2;
24104
24105 /* Setup the argument vector, which contains the remaining
24106 arguments. */
24107 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24108 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24109
24110 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24111 argvec, DEDUCE_EXACT,
24112 /*subr=*/true, /*explain_p=*/false)
24113 == 0);
24114
24115 /* We cannot deduce in the other direction, because ARG2 is
24116 a pack expansion but ARG1 is not.*/
24117 deduce1 = 0;
24118 }
24119
24120 else
24121 {
24122 /* The normal case, where neither argument is a pack
24123 expansion. */
24124 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24125 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24126 == 0);
24127 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24128 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24129 == 0);
24130 }
24131
24132 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24133 arg2, then arg2 is not as specialized as arg1. */
24134 if (!deduce1)
24135 lose2 = true;
24136 if (!deduce2)
24137 lose1 = true;
24138
24139 /* "If, for a given type, deduction succeeds in both directions
24140 (i.e., the types are identical after the transformations above)
24141 and both P and A were reference types (before being replaced with
24142 the type referred to above):
24143 - if the type from the argument template was an lvalue reference and
24144 the type from the parameter template was not, the argument type is
24145 considered to be more specialized than the other; otherwise,
24146 - if the type from the argument template is more cv-qualified
24147 than the type from the parameter template (as described above),
24148 the argument type is considered to be more specialized than the other;
24149 otherwise,
24150 - neither type is more specialized than the other." */
24151
24152 if (deduce1 && deduce2)
24153 {
24154 if (ref1 && ref2 && ref1 != ref2)
24155 {
24156 if (ref1 > ref2)
24157 lose1 = true;
24158 else
24159 lose2 = true;
24160 }
24161 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24162 {
24163 if ((quals1 & quals2) == quals2)
24164 lose2 = true;
24165 if ((quals1 & quals2) == quals1)
24166 lose1 = true;
24167 }
24168 }
24169
24170 if (lose1 && lose2)
24171 /* We've failed to deduce something in either direction.
24172 These must be unordered. */
24173 break;
24174
24175 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24176 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24177 /* We have already processed all of the arguments in our
24178 handing of the pack expansion type. */
24179 len = 0;
24180
24181 args1 = TREE_CHAIN (args1);
24182 args2 = TREE_CHAIN (args2);
24183 }
24184
24185 /* "In most cases, all template parameters must have values in order for
24186 deduction to succeed, but for partial ordering purposes a template
24187 parameter may remain without a value provided it is not used in the
24188 types being used for partial ordering."
24189
24190 Thus, if we are missing any of the targs1 we need to substitute into
24191 origs1, then pat2 is not as specialized as pat1. This can happen when
24192 there is a nondeduced context. */
24193 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24194 lose2 = true;
24195 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24196 lose1 = true;
24197
24198 processing_template_decl--;
24199
24200 /* If both deductions succeed, the partial ordering selects the more
24201 constrained template. */
24202 /* P2113: If the corresponding template-parameters of the
24203 template-parameter-lists are not equivalent ([temp.over.link]) or if
24204 the function parameters that positionally correspond between the two
24205 templates are not of the same type, neither template is more
24206 specialized than the other. */
24207 if (!lose1 && !lose2
24208 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24209 DECL_TEMPLATE_PARMS (pat2))
24210 && compparms (origs1, origs2))
24211 {
24212 int winner = more_constrained (decl1, decl2);
24213 if (winner > 0)
24214 lose2 = true;
24215 else if (winner < 0)
24216 lose1 = true;
24217 }
24218
24219 /* All things being equal, if the next argument is a pack expansion
24220 for one function but not for the other, prefer the
24221 non-variadic function. FIXME this is bogus; see c++/41958. */
24222 if (lose1 == lose2
24223 && args1 && TREE_VALUE (args1)
24224 && args2 && TREE_VALUE (args2))
24225 {
24226 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24227 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24228 }
24229
24230 if (lose1 == lose2)
24231 return 0;
24232 else if (!lose1)
24233 return 1;
24234 else
24235 return -1;
24236 }
24237
24238 /* Determine which of two partial specializations of TMPL is more
24239 specialized.
24240
24241 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24242 to the first partial specialization. The TREE_PURPOSE is the
24243 innermost set of template parameters for the partial
24244 specialization. PAT2 is similar, but for the second template.
24245
24246 Return 1 if the first partial specialization is more specialized;
24247 -1 if the second is more specialized; 0 if neither is more
24248 specialized.
24249
24250 See [temp.class.order] for information about determining which of
24251 two templates is more specialized. */
24252
24253 static int
24254 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24255 {
24256 tree targs;
24257 int winner = 0;
24258 bool any_deductions = false;
24259
24260 tree tmpl1 = TREE_VALUE (pat1);
24261 tree tmpl2 = TREE_VALUE (pat2);
24262 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24263 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24264
24265 /* Just like what happens for functions, if we are ordering between
24266 different template specializations, we may encounter dependent
24267 types in the arguments, and we need our dependency check functions
24268 to behave correctly. */
24269 ++processing_template_decl;
24270 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24271 if (targs)
24272 {
24273 --winner;
24274 any_deductions = true;
24275 }
24276
24277 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24278 if (targs)
24279 {
24280 ++winner;
24281 any_deductions = true;
24282 }
24283 --processing_template_decl;
24284
24285 /* If both deductions succeed, the partial ordering selects the more
24286 constrained template. */
24287 if (!winner && any_deductions)
24288 winner = more_constrained (tmpl1, tmpl2);
24289
24290 /* In the case of a tie where at least one of the templates
24291 has a parameter pack at the end, the template with the most
24292 non-packed parameters wins. */
24293 if (winner == 0
24294 && any_deductions
24295 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24296 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24297 {
24298 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24299 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24300 int len1 = TREE_VEC_LENGTH (args1);
24301 int len2 = TREE_VEC_LENGTH (args2);
24302
24303 /* We don't count the pack expansion at the end. */
24304 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24305 --len1;
24306 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24307 --len2;
24308
24309 if (len1 > len2)
24310 return 1;
24311 else if (len1 < len2)
24312 return -1;
24313 }
24314
24315 return winner;
24316 }
24317
24318 /* Return the template arguments that will produce the function signature
24319 DECL from the function template FN, with the explicit template
24320 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24321 also match. Return NULL_TREE if no satisfactory arguments could be
24322 found. */
24323
24324 static tree
24325 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24326 {
24327 int ntparms = DECL_NTPARMS (fn);
24328 tree targs = make_tree_vec (ntparms);
24329 tree decl_type = TREE_TYPE (decl);
24330 tree decl_arg_types;
24331 tree *args;
24332 unsigned int nargs, ix;
24333 tree arg;
24334
24335 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24336
24337 /* Never do unification on the 'this' parameter. */
24338 decl_arg_types = skip_artificial_parms_for (decl,
24339 TYPE_ARG_TYPES (decl_type));
24340
24341 nargs = list_length (decl_arg_types);
24342 args = XALLOCAVEC (tree, nargs);
24343 for (arg = decl_arg_types, ix = 0;
24344 arg != NULL_TREE && arg != void_list_node;
24345 arg = TREE_CHAIN (arg), ++ix)
24346 args[ix] = TREE_VALUE (arg);
24347
24348 if (fn_type_unification (fn, explicit_args, targs,
24349 args, ix,
24350 (check_rettype || DECL_CONV_FN_P (fn)
24351 ? TREE_TYPE (decl_type) : NULL_TREE),
24352 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24353 /*explain_p=*/false,
24354 /*decltype*/false)
24355 == error_mark_node)
24356 return NULL_TREE;
24357
24358 return targs;
24359 }
24360
24361 /* Return the innermost template arguments that, when applied to a partial
24362 specialization SPEC_TMPL of TMPL, yield the ARGS.
24363
24364 For example, suppose we have:
24365
24366 template <class T, class U> struct S {};
24367 template <class T> struct S<T*, int> {};
24368
24369 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24370 partial specialization and the ARGS will be {double*, int}. The resulting
24371 vector will be {double}, indicating that `T' is bound to `double'. */
24372
24373 static tree
24374 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24375 {
24376 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24377 tree spec_args
24378 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24379 int i, ntparms = TREE_VEC_LENGTH (tparms);
24380 tree deduced_args;
24381 tree innermost_deduced_args;
24382
24383 innermost_deduced_args = make_tree_vec (ntparms);
24384 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24385 {
24386 deduced_args = copy_node (args);
24387 SET_TMPL_ARGS_LEVEL (deduced_args,
24388 TMPL_ARGS_DEPTH (deduced_args),
24389 innermost_deduced_args);
24390 }
24391 else
24392 deduced_args = innermost_deduced_args;
24393
24394 bool tried_array_deduction = (cxx_dialect < cxx17);
24395 again:
24396 if (unify (tparms, deduced_args,
24397 INNERMOST_TEMPLATE_ARGS (spec_args),
24398 INNERMOST_TEMPLATE_ARGS (args),
24399 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24400 return NULL_TREE;
24401
24402 for (i = 0; i < ntparms; ++i)
24403 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24404 {
24405 if (!tried_array_deduction)
24406 {
24407 try_array_deduction (tparms, innermost_deduced_args,
24408 INNERMOST_TEMPLATE_ARGS (spec_args));
24409 tried_array_deduction = true;
24410 if (TREE_VEC_ELT (innermost_deduced_args, i))
24411 goto again;
24412 }
24413 return NULL_TREE;
24414 }
24415
24416 if (!push_tinst_level (spec_tmpl, deduced_args))
24417 {
24418 excessive_deduction_depth = true;
24419 return NULL_TREE;
24420 }
24421
24422 /* Verify that nondeduced template arguments agree with the type
24423 obtained from argument deduction.
24424
24425 For example:
24426
24427 struct A { typedef int X; };
24428 template <class T, class U> struct C {};
24429 template <class T> struct C<T, typename T::X> {};
24430
24431 Then with the instantiation `C<A, int>', we can deduce that
24432 `T' is `A' but unify () does not check whether `typename T::X'
24433 is `int'. */
24434 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24435
24436 if (spec_args != error_mark_node)
24437 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24438 INNERMOST_TEMPLATE_ARGS (spec_args),
24439 tmpl, tf_none, false, false);
24440
24441 pop_tinst_level ();
24442
24443 if (spec_args == error_mark_node
24444 /* We only need to check the innermost arguments; the other
24445 arguments will always agree. */
24446 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24447 INNERMOST_TEMPLATE_ARGS (args)))
24448 return NULL_TREE;
24449
24450 /* Now that we have bindings for all of the template arguments,
24451 ensure that the arguments deduced for the template template
24452 parameters have compatible template parameter lists. See the use
24453 of template_template_parm_bindings_ok_p in fn_type_unification
24454 for more information. */
24455 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24456 return NULL_TREE;
24457
24458 return deduced_args;
24459 }
24460
24461 // Compare two function templates T1 and T2 by deducing bindings
24462 // from one against the other. If both deductions succeed, compare
24463 // constraints to see which is more constrained.
24464 static int
24465 more_specialized_inst (tree t1, tree t2)
24466 {
24467 int fate = 0;
24468 int count = 0;
24469
24470 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24471 {
24472 --fate;
24473 ++count;
24474 }
24475
24476 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24477 {
24478 ++fate;
24479 ++count;
24480 }
24481
24482 // If both deductions succeed, then one may be more constrained.
24483 if (count == 2 && fate == 0)
24484 fate = more_constrained (t1, t2);
24485
24486 return fate;
24487 }
24488
24489 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24490 Return the TREE_LIST node with the most specialized template, if
24491 any. If there is no most specialized template, the error_mark_node
24492 is returned.
24493
24494 Note that this function does not look at, or modify, the
24495 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24496 returned is one of the elements of INSTANTIATIONS, callers may
24497 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24498 and retrieve it from the value returned. */
24499
24500 tree
24501 most_specialized_instantiation (tree templates)
24502 {
24503 tree fn, champ;
24504
24505 ++processing_template_decl;
24506
24507 champ = templates;
24508 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24509 {
24510 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24511 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24512 if (fate == -1)
24513 champ = fn;
24514 else if (!fate)
24515 {
24516 /* Equally specialized, move to next function. If there
24517 is no next function, nothing's most specialized. */
24518 fn = TREE_CHAIN (fn);
24519 champ = fn;
24520 if (!fn)
24521 break;
24522 }
24523 }
24524
24525 if (champ)
24526 /* Now verify that champ is better than everything earlier in the
24527 instantiation list. */
24528 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24529 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24530 {
24531 champ = NULL_TREE;
24532 break;
24533 }
24534 }
24535
24536 processing_template_decl--;
24537
24538 if (!champ)
24539 return error_mark_node;
24540
24541 return champ;
24542 }
24543
24544 /* If DECL is a specialization of some template, return the most
24545 general such template. Otherwise, returns NULL_TREE.
24546
24547 For example, given:
24548
24549 template <class T> struct S { template <class U> void f(U); };
24550
24551 if TMPL is `template <class U> void S<int>::f(U)' this will return
24552 the full template. This function will not trace past partial
24553 specializations, however. For example, given in addition:
24554
24555 template <class T> struct S<T*> { template <class U> void f(U); };
24556
24557 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24558 `template <class T> template <class U> S<T*>::f(U)'. */
24559
24560 tree
24561 most_general_template (tree decl)
24562 {
24563 if (TREE_CODE (decl) != TEMPLATE_DECL)
24564 {
24565 if (tree tinfo = get_template_info (decl))
24566 decl = TI_TEMPLATE (tinfo);
24567 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24568 template friend, or a FIELD_DECL for a capture pack. */
24569 if (TREE_CODE (decl) != TEMPLATE_DECL)
24570 return NULL_TREE;
24571 }
24572
24573 /* Look for more and more general templates. */
24574 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24575 {
24576 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24577 (See cp-tree.h for details.) */
24578 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24579 break;
24580
24581 if (CLASS_TYPE_P (TREE_TYPE (decl))
24582 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24583 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24584 break;
24585
24586 /* Stop if we run into an explicitly specialized class template. */
24587 if (!DECL_NAMESPACE_SCOPE_P (decl)
24588 && DECL_CONTEXT (decl)
24589 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24590 break;
24591
24592 decl = DECL_TI_TEMPLATE (decl);
24593 }
24594
24595 return decl;
24596 }
24597
24598 /* Return the most specialized of the template partial specializations
24599 which can produce TARGET, a specialization of some class or variable
24600 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24601 a TEMPLATE_DECL node corresponding to the partial specialization, while
24602 the TREE_PURPOSE is the set of template arguments that must be
24603 substituted into the template pattern in order to generate TARGET.
24604
24605 If the choice of partial specialization is ambiguous, a diagnostic
24606 is issued, and the error_mark_node is returned. If there are no
24607 partial specializations matching TARGET, then NULL_TREE is
24608 returned, indicating that the primary template should be used. */
24609
24610 tree
24611 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24612 {
24613 tree list = NULL_TREE;
24614 tree t;
24615 tree champ;
24616 int fate;
24617 bool ambiguous_p;
24618 tree outer_args = NULL_TREE;
24619 tree tmpl, args;
24620
24621 if (TYPE_P (target))
24622 {
24623 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24624 tmpl = TI_TEMPLATE (tinfo);
24625 args = TI_ARGS (tinfo);
24626 }
24627 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24628 {
24629 tmpl = TREE_OPERAND (target, 0);
24630 args = TREE_OPERAND (target, 1);
24631 }
24632 else if (VAR_P (target))
24633 {
24634 tree tinfo = DECL_TEMPLATE_INFO (target);
24635 tmpl = TI_TEMPLATE (tinfo);
24636 args = TI_ARGS (tinfo);
24637 }
24638 else
24639 gcc_unreachable ();
24640
24641 tree main_tmpl = most_general_template (tmpl);
24642
24643 /* For determining which partial specialization to use, only the
24644 innermost args are interesting. */
24645 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24646 {
24647 outer_args = strip_innermost_template_args (args, 1);
24648 args = INNERMOST_TEMPLATE_ARGS (args);
24649 }
24650
24651 /* The caller hasn't called push_to_top_level yet, but we need
24652 get_partial_spec_bindings to be done in non-template context so that we'll
24653 fully resolve everything. */
24654 processing_template_decl_sentinel ptds;
24655
24656 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24657 {
24658 const tree ospec_tmpl = TREE_VALUE (t);
24659
24660 tree spec_tmpl;
24661 if (outer_args)
24662 {
24663 /* Substitute in the template args from the enclosing class. */
24664 ++processing_template_decl;
24665 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
24666 --processing_template_decl;
24667 if (spec_tmpl == error_mark_node)
24668 return error_mark_node;
24669 }
24670 else
24671 spec_tmpl = ospec_tmpl;
24672
24673 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24674 if (spec_args)
24675 {
24676 if (outer_args)
24677 spec_args = add_to_template_args (outer_args, spec_args);
24678
24679 /* Keep the candidate only if the constraints are satisfied,
24680 or if we're not compiling with concepts. */
24681 if (!flag_concepts
24682 || constraints_satisfied_p (ospec_tmpl, spec_args))
24683 {
24684 list = tree_cons (spec_args, ospec_tmpl, list);
24685 TREE_TYPE (list) = TREE_TYPE (t);
24686 }
24687 }
24688 }
24689
24690 if (! list)
24691 return NULL_TREE;
24692
24693 ambiguous_p = false;
24694 t = list;
24695 champ = t;
24696 t = TREE_CHAIN (t);
24697 for (; t; t = TREE_CHAIN (t))
24698 {
24699 fate = more_specialized_partial_spec (tmpl, champ, t);
24700 if (fate == 1)
24701 ;
24702 else
24703 {
24704 if (fate == 0)
24705 {
24706 t = TREE_CHAIN (t);
24707 if (! t)
24708 {
24709 ambiguous_p = true;
24710 break;
24711 }
24712 }
24713 champ = t;
24714 }
24715 }
24716
24717 if (!ambiguous_p)
24718 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24719 {
24720 fate = more_specialized_partial_spec (tmpl, champ, t);
24721 if (fate != 1)
24722 {
24723 ambiguous_p = true;
24724 break;
24725 }
24726 }
24727
24728 if (ambiguous_p)
24729 {
24730 const char *str;
24731 char *spaces = NULL;
24732 if (!(complain & tf_error))
24733 return error_mark_node;
24734 if (TYPE_P (target))
24735 error ("ambiguous template instantiation for %q#T", target);
24736 else
24737 error ("ambiguous template instantiation for %q#D", target);
24738 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24739 for (t = list; t; t = TREE_CHAIN (t))
24740 {
24741 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24742 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24743 "%s %#qS", spaces ? spaces : str, subst);
24744 spaces = spaces ? spaces : get_spaces (str);
24745 }
24746 free (spaces);
24747 return error_mark_node;
24748 }
24749
24750 return champ;
24751 }
24752
24753 /* Explicitly instantiate DECL. */
24754
24755 void
24756 do_decl_instantiation (tree decl, tree storage)
24757 {
24758 tree result = NULL_TREE;
24759 int extern_p = 0;
24760
24761 if (!decl || decl == error_mark_node)
24762 /* An error occurred, for which grokdeclarator has already issued
24763 an appropriate message. */
24764 return;
24765 else if (! DECL_LANG_SPECIFIC (decl))
24766 {
24767 error ("explicit instantiation of non-template %q#D", decl);
24768 return;
24769 }
24770 else if (DECL_DECLARED_CONCEPT_P (decl))
24771 {
24772 if (VAR_P (decl))
24773 error ("explicit instantiation of variable concept %q#D", decl);
24774 else
24775 error ("explicit instantiation of function concept %q#D", decl);
24776 return;
24777 }
24778
24779 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24780 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24781
24782 if (VAR_P (decl) && !var_templ)
24783 {
24784 /* There is an asymmetry here in the way VAR_DECLs and
24785 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24786 the latter, the DECL we get back will be marked as a
24787 template instantiation, and the appropriate
24788 DECL_TEMPLATE_INFO will be set up. This does not happen for
24789 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24790 should handle VAR_DECLs as it currently handles
24791 FUNCTION_DECLs. */
24792 if (!DECL_CLASS_SCOPE_P (decl))
24793 {
24794 error ("%qD is not a static data member of a class template", decl);
24795 return;
24796 }
24797 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24798 if (!result || !VAR_P (result))
24799 {
24800 error ("no matching template for %qD found", decl);
24801 return;
24802 }
24803 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24804 {
24805 error ("type %qT for explicit instantiation %qD does not match "
24806 "declared type %qT", TREE_TYPE (result), decl,
24807 TREE_TYPE (decl));
24808 return;
24809 }
24810 }
24811 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24812 {
24813 error ("explicit instantiation of %q#D", decl);
24814 return;
24815 }
24816 else
24817 result = decl;
24818
24819 /* Check for various error cases. Note that if the explicit
24820 instantiation is valid the RESULT will currently be marked as an
24821 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24822 until we get here. */
24823
24824 if (DECL_TEMPLATE_SPECIALIZATION (result))
24825 {
24826 /* DR 259 [temp.spec].
24827
24828 Both an explicit instantiation and a declaration of an explicit
24829 specialization shall not appear in a program unless the explicit
24830 instantiation follows a declaration of the explicit specialization.
24831
24832 For a given set of template parameters, if an explicit
24833 instantiation of a template appears after a declaration of an
24834 explicit specialization for that template, the explicit
24835 instantiation has no effect. */
24836 return;
24837 }
24838 else if (DECL_EXPLICIT_INSTANTIATION (result))
24839 {
24840 /* [temp.spec]
24841
24842 No program shall explicitly instantiate any template more
24843 than once.
24844
24845 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24846 the first instantiation was `extern' and the second is not,
24847 and EXTERN_P for the opposite case. */
24848 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24849 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24850 /* If an "extern" explicit instantiation follows an ordinary
24851 explicit instantiation, the template is instantiated. */
24852 if (extern_p)
24853 return;
24854 }
24855 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24856 {
24857 error ("no matching template for %qD found", result);
24858 return;
24859 }
24860 else if (!DECL_TEMPLATE_INFO (result))
24861 {
24862 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24863 return;
24864 }
24865
24866 if (storage == NULL_TREE)
24867 ;
24868 else if (storage == ridpointers[(int) RID_EXTERN])
24869 {
24870 if (cxx_dialect == cxx98)
24871 pedwarn (input_location, OPT_Wpedantic,
24872 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24873 "instantiations");
24874 extern_p = 1;
24875 }
24876 else
24877 error ("storage class %qD applied to template instantiation", storage);
24878
24879 check_explicit_instantiation_namespace (result);
24880 mark_decl_instantiated (result, extern_p);
24881 if (! extern_p)
24882 instantiate_decl (result, /*defer_ok=*/true,
24883 /*expl_inst_class_mem_p=*/false);
24884 }
24885
24886 static void
24887 mark_class_instantiated (tree t, int extern_p)
24888 {
24889 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24890 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24891 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24892 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24893 if (! extern_p)
24894 {
24895 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24896 rest_of_type_compilation (t, 1);
24897 }
24898 }
24899
24900 /* Called from do_type_instantiation through binding_table_foreach to
24901 do recursive instantiation for the type bound in ENTRY. */
24902 static void
24903 bt_instantiate_type_proc (binding_entry entry, void *data)
24904 {
24905 tree storage = *(tree *) data;
24906
24907 if (MAYBE_CLASS_TYPE_P (entry->type)
24908 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24909 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24910 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24911 }
24912
24913 /* Perform an explicit instantiation of template class T. STORAGE, if
24914 non-null, is the RID for extern, inline or static. COMPLAIN is
24915 nonzero if this is called from the parser, zero if called recursively,
24916 since the standard is unclear (as detailed below). */
24917
24918 void
24919 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24920 {
24921 int extern_p = 0;
24922 int nomem_p = 0;
24923 int static_p = 0;
24924 int previous_instantiation_extern_p = 0;
24925
24926 if (TREE_CODE (t) == TYPE_DECL)
24927 t = TREE_TYPE (t);
24928
24929 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24930 {
24931 tree tmpl =
24932 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24933 if (tmpl)
24934 error ("explicit instantiation of non-class template %qD", tmpl);
24935 else
24936 error ("explicit instantiation of non-template type %qT", t);
24937 return;
24938 }
24939
24940 complete_type (t);
24941
24942 if (!COMPLETE_TYPE_P (t))
24943 {
24944 if (complain & tf_error)
24945 error ("explicit instantiation of %q#T before definition of template",
24946 t);
24947 return;
24948 }
24949
24950 if (storage != NULL_TREE)
24951 {
24952 if (storage == ridpointers[(int) RID_EXTERN])
24953 {
24954 if (cxx_dialect == cxx98)
24955 pedwarn (input_location, OPT_Wpedantic,
24956 "ISO C++ 1998 forbids the use of %<extern%> on "
24957 "explicit instantiations");
24958 }
24959 else
24960 pedwarn (input_location, OPT_Wpedantic,
24961 "ISO C++ forbids the use of %qE"
24962 " on explicit instantiations", storage);
24963
24964 if (storage == ridpointers[(int) RID_INLINE])
24965 nomem_p = 1;
24966 else if (storage == ridpointers[(int) RID_EXTERN])
24967 extern_p = 1;
24968 else if (storage == ridpointers[(int) RID_STATIC])
24969 static_p = 1;
24970 else
24971 {
24972 error ("storage class %qD applied to template instantiation",
24973 storage);
24974 extern_p = 0;
24975 }
24976 }
24977
24978 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24979 {
24980 /* DR 259 [temp.spec].
24981
24982 Both an explicit instantiation and a declaration of an explicit
24983 specialization shall not appear in a program unless the explicit
24984 instantiation follows a declaration of the explicit specialization.
24985
24986 For a given set of template parameters, if an explicit
24987 instantiation of a template appears after a declaration of an
24988 explicit specialization for that template, the explicit
24989 instantiation has no effect. */
24990 return;
24991 }
24992 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24993 {
24994 /* [temp.spec]
24995
24996 No program shall explicitly instantiate any template more
24997 than once.
24998
24999 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
25000 instantiation was `extern'. If EXTERN_P then the second is.
25001 These cases are OK. */
25002 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
25003
25004 if (!previous_instantiation_extern_p && !extern_p
25005 && (complain & tf_error))
25006 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
25007
25008 /* If we've already instantiated the template, just return now. */
25009 if (!CLASSTYPE_INTERFACE_ONLY (t))
25010 return;
25011 }
25012
25013 check_explicit_instantiation_namespace (TYPE_NAME (t));
25014 mark_class_instantiated (t, extern_p);
25015
25016 if (nomem_p)
25017 return;
25018
25019 /* In contrast to implicit instantiation, where only the
25020 declarations, and not the definitions, of members are
25021 instantiated, we have here:
25022
25023 [temp.explicit]
25024
25025 An explicit instantiation that names a class template
25026 specialization is also an explicit instantiation of the same
25027 kind (declaration or definition) of each of its members (not
25028 including members inherited from base classes and members
25029 that are templates) that has not been previously explicitly
25030 specialized in the translation unit containing the explicit
25031 instantiation, provided that the associated constraints, if
25032 any, of that member are satisfied by the template arguments
25033 of the explicit instantiation. */
25034 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25035 if ((VAR_P (fld)
25036 || (TREE_CODE (fld) == FUNCTION_DECL
25037 && !static_p
25038 && user_provided_p (fld)))
25039 && DECL_TEMPLATE_INSTANTIATION (fld)
25040 && constraints_satisfied_p (fld))
25041 {
25042 mark_decl_instantiated (fld, extern_p);
25043 if (! extern_p)
25044 instantiate_decl (fld, /*defer_ok=*/true,
25045 /*expl_inst_class_mem_p=*/true);
25046 }
25047
25048 if (CLASSTYPE_NESTED_UTDS (t))
25049 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
25050 bt_instantiate_type_proc, &storage);
25051 }
25052
25053 /* Given a function DECL, which is a specialization of TMPL, modify
25054 DECL to be a re-instantiation of TMPL with the same template
25055 arguments. TMPL should be the template into which tsubst'ing
25056 should occur for DECL, not the most general template.
25057
25058 One reason for doing this is a scenario like this:
25059
25060 template <class T>
25061 void f(const T&, int i);
25062
25063 void g() { f(3, 7); }
25064
25065 template <class T>
25066 void f(const T& t, const int i) { }
25067
25068 Note that when the template is first instantiated, with
25069 instantiate_template, the resulting DECL will have no name for the
25070 first parameter, and the wrong type for the second. So, when we go
25071 to instantiate the DECL, we regenerate it. */
25072
25073 static void
25074 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25075 {
25076 /* The arguments used to instantiate DECL, from the most general
25077 template. */
25078 tree code_pattern;
25079
25080 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25081
25082 /* Make sure that we can see identifiers, and compute access
25083 correctly. */
25084 push_access_scope (decl);
25085
25086 if (TREE_CODE (decl) == FUNCTION_DECL)
25087 {
25088 tree decl_parm;
25089 tree pattern_parm;
25090 tree specs;
25091 int args_depth;
25092 int parms_depth;
25093
25094 args_depth = TMPL_ARGS_DEPTH (args);
25095 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25096 if (args_depth > parms_depth)
25097 args = get_innermost_template_args (args, parms_depth);
25098
25099 /* Instantiate a dynamic exception-specification. noexcept will be
25100 handled below. */
25101 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25102 if (TREE_VALUE (raises))
25103 {
25104 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25105 args, tf_error, NULL_TREE,
25106 /*defer_ok*/false);
25107 if (specs && specs != error_mark_node)
25108 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25109 specs);
25110 }
25111
25112 /* Merge parameter declarations. */
25113 decl_parm = skip_artificial_parms_for (decl,
25114 DECL_ARGUMENTS (decl));
25115 pattern_parm
25116 = skip_artificial_parms_for (code_pattern,
25117 DECL_ARGUMENTS (code_pattern));
25118 while (decl_parm && !DECL_PACK_P (pattern_parm))
25119 {
25120 tree parm_type;
25121 tree attributes;
25122
25123 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25124 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25125 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25126 NULL_TREE);
25127 parm_type = type_decays_to (parm_type);
25128 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25129 TREE_TYPE (decl_parm) = parm_type;
25130 attributes = DECL_ATTRIBUTES (pattern_parm);
25131 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25132 {
25133 DECL_ATTRIBUTES (decl_parm) = attributes;
25134 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25135 }
25136 decl_parm = DECL_CHAIN (decl_parm);
25137 pattern_parm = DECL_CHAIN (pattern_parm);
25138 }
25139 /* Merge any parameters that match with the function parameter
25140 pack. */
25141 if (pattern_parm && DECL_PACK_P (pattern_parm))
25142 {
25143 int i, len;
25144 tree expanded_types;
25145 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25146 the parameters in this function parameter pack. */
25147 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25148 args, tf_error, NULL_TREE);
25149 len = TREE_VEC_LENGTH (expanded_types);
25150 for (i = 0; i < len; i++)
25151 {
25152 tree parm_type;
25153 tree attributes;
25154
25155 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25156 /* Rename the parameter to include the index. */
25157 DECL_NAME (decl_parm) =
25158 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25159 parm_type = TREE_VEC_ELT (expanded_types, i);
25160 parm_type = type_decays_to (parm_type);
25161 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25162 TREE_TYPE (decl_parm) = parm_type;
25163 attributes = DECL_ATTRIBUTES (pattern_parm);
25164 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25165 {
25166 DECL_ATTRIBUTES (decl_parm) = attributes;
25167 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25168 }
25169 decl_parm = DECL_CHAIN (decl_parm);
25170 }
25171 }
25172 /* Merge additional specifiers from the CODE_PATTERN. */
25173 if (DECL_DECLARED_INLINE_P (code_pattern)
25174 && !DECL_DECLARED_INLINE_P (decl))
25175 DECL_DECLARED_INLINE_P (decl) = 1;
25176
25177 maybe_instantiate_noexcept (decl, tf_error);
25178 }
25179 else if (VAR_P (decl))
25180 {
25181 start_lambda_scope (decl);
25182 DECL_INITIAL (decl) =
25183 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25184 tf_error, DECL_TI_TEMPLATE (decl));
25185 finish_lambda_scope ();
25186 if (VAR_HAD_UNKNOWN_BOUND (decl))
25187 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25188 tf_error, DECL_TI_TEMPLATE (decl));
25189 }
25190 else
25191 gcc_unreachable ();
25192
25193 pop_access_scope (decl);
25194 }
25195
25196 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25197 substituted to get DECL. */
25198
25199 tree
25200 template_for_substitution (tree decl)
25201 {
25202 tree tmpl = DECL_TI_TEMPLATE (decl);
25203
25204 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25205 for the instantiation. This is not always the most general
25206 template. Consider, for example:
25207
25208 template <class T>
25209 struct S { template <class U> void f();
25210 template <> void f<int>(); };
25211
25212 and an instantiation of S<double>::f<int>. We want TD to be the
25213 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25214 while (/* An instantiation cannot have a definition, so we need a
25215 more general template. */
25216 DECL_TEMPLATE_INSTANTIATION (tmpl)
25217 /* We must also deal with friend templates. Given:
25218
25219 template <class T> struct S {
25220 template <class U> friend void f() {};
25221 };
25222
25223 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25224 so far as the language is concerned, but that's still
25225 where we get the pattern for the instantiation from. On
25226 other hand, if the definition comes outside the class, say:
25227
25228 template <class T> struct S {
25229 template <class U> friend void f();
25230 };
25231 template <class U> friend void f() {}
25232
25233 we don't need to look any further. That's what the check for
25234 DECL_INITIAL is for. */
25235 || (TREE_CODE (decl) == FUNCTION_DECL
25236 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25237 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25238 {
25239 /* The present template, TD, should not be a definition. If it
25240 were a definition, we should be using it! Note that we
25241 cannot restructure the loop to just keep going until we find
25242 a template with a definition, since that might go too far if
25243 a specialization was declared, but not defined. */
25244
25245 /* Fetch the more general template. */
25246 tmpl = DECL_TI_TEMPLATE (tmpl);
25247 }
25248
25249 return tmpl;
25250 }
25251
25252 /* Returns true if we need to instantiate this template instance even if we
25253 know we aren't going to emit it. */
25254
25255 bool
25256 always_instantiate_p (tree decl)
25257 {
25258 /* We always instantiate inline functions so that we can inline them. An
25259 explicit instantiation declaration prohibits implicit instantiation of
25260 non-inline functions. With high levels of optimization, we would
25261 normally inline non-inline functions -- but we're not allowed to do
25262 that for "extern template" functions. Therefore, we check
25263 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25264 return ((TREE_CODE (decl) == FUNCTION_DECL
25265 && (DECL_DECLARED_INLINE_P (decl)
25266 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25267 /* And we need to instantiate static data members so that
25268 their initializers are available in integral constant
25269 expressions. */
25270 || (VAR_P (decl)
25271 && decl_maybe_constant_var_p (decl)));
25272 }
25273
25274 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25275 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25276 error, true otherwise. */
25277
25278 bool
25279 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25280 {
25281 tree fntype, spec, noex;
25282
25283 /* Don't instantiate a noexcept-specification from template context. */
25284 if (processing_template_decl
25285 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25286 return true;
25287
25288 if (DECL_MAYBE_DELETED (fn))
25289 {
25290 if (fn == current_function_decl)
25291 /* We're in start_preparsed_function, keep going. */
25292 return true;
25293
25294 ++function_depth;
25295 synthesize_method (fn);
25296 --function_depth;
25297 return !DECL_MAYBE_DELETED (fn);
25298 }
25299
25300 fntype = TREE_TYPE (fn);
25301 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25302
25303 if (!spec || !TREE_PURPOSE (spec))
25304 return true;
25305
25306 noex = TREE_PURPOSE (spec);
25307 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25308 && TREE_CODE (noex) != DEFERRED_PARSE)
25309 return true;
25310
25311 tree orig_fn = NULL_TREE;
25312 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25313 its FUNCTION_DECL for the rest of this function -- push_access_scope
25314 doesn't accept TEMPLATE_DECLs. */
25315 if (DECL_FUNCTION_TEMPLATE_P (fn))
25316 {
25317 orig_fn = fn;
25318 fn = DECL_TEMPLATE_RESULT (fn);
25319 }
25320
25321 if (DECL_CLONED_FUNCTION_P (fn))
25322 {
25323 tree prime = DECL_CLONED_FUNCTION (fn);
25324 if (!maybe_instantiate_noexcept (prime, complain))
25325 return false;
25326 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25327 }
25328 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25329 {
25330 static hash_set<tree>* fns = new hash_set<tree>;
25331 bool added = false;
25332 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25333 {
25334 spec = get_defaulted_eh_spec (fn, complain);
25335 if (spec == error_mark_node)
25336 /* This might have failed because of an unparsed DMI, so
25337 let's try again later. */
25338 return false;
25339 }
25340 else if (!(added = !fns->add (fn)))
25341 {
25342 /* If hash_set::add returns true, the element was already there. */
25343 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25344 DECL_SOURCE_LOCATION (fn));
25345 error_at (loc,
25346 "exception specification of %qD depends on itself",
25347 fn);
25348 spec = noexcept_false_spec;
25349 }
25350 else if (push_tinst_level (fn))
25351 {
25352 push_to_top_level ();
25353 push_access_scope (fn);
25354 push_deferring_access_checks (dk_no_deferred);
25355 input_location = DECL_SOURCE_LOCATION (fn);
25356
25357 /* If needed, set current_class_ptr for the benefit of
25358 tsubst_copy/PARM_DECL. */
25359 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25360 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25361 {
25362 tree this_parm = DECL_ARGUMENTS (tdecl);
25363 current_class_ptr = NULL_TREE;
25364 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25365 current_class_ptr = this_parm;
25366 }
25367
25368 /* If this function is represented by a TEMPLATE_DECL, then
25369 the deferred noexcept-specification might still contain
25370 dependent types, even after substitution. And we need the
25371 dependency check functions to work in build_noexcept_spec. */
25372 if (orig_fn)
25373 ++processing_template_decl;
25374
25375 /* Do deferred instantiation of the noexcept-specifier. */
25376 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25377 DEFERRED_NOEXCEPT_ARGS (noex),
25378 tf_warning_or_error, fn,
25379 /*function_p=*/false,
25380 /*i_c_e_p=*/true);
25381
25382 /* Build up the noexcept-specification. */
25383 spec = build_noexcept_spec (noex, tf_warning_or_error);
25384
25385 if (orig_fn)
25386 --processing_template_decl;
25387
25388 pop_deferring_access_checks ();
25389 pop_access_scope (fn);
25390 pop_tinst_level ();
25391 pop_from_top_level ();
25392 }
25393 else
25394 spec = noexcept_false_spec;
25395
25396 if (added)
25397 fns->remove (fn);
25398 }
25399
25400 if (spec == error_mark_node)
25401 {
25402 /* This failed with a hard error, so let's go with false. */
25403 gcc_assert (seen_error ());
25404 spec = noexcept_false_spec;
25405 }
25406
25407 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25408 if (orig_fn)
25409 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25410
25411 return true;
25412 }
25413
25414 /* We're starting to process the function INST, an instantiation of PATTERN;
25415 add their parameters to local_specializations. */
25416
25417 static void
25418 register_parameter_specializations (tree pattern, tree inst)
25419 {
25420 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25421 tree spec_parm = DECL_ARGUMENTS (inst);
25422 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25423 {
25424 register_local_specialization (spec_parm, tmpl_parm);
25425 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25426 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25427 }
25428 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25429 {
25430 if (!DECL_PACK_P (tmpl_parm)
25431 || (spec_parm && DECL_PACK_P (spec_parm)))
25432 {
25433 register_local_specialization (spec_parm, tmpl_parm);
25434 spec_parm = DECL_CHAIN (spec_parm);
25435 }
25436 else
25437 {
25438 /* Register the (value) argument pack as a specialization of
25439 TMPL_PARM, then move on. */
25440 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25441 register_local_specialization (argpack, tmpl_parm);
25442 }
25443 }
25444 gcc_assert (!spec_parm);
25445 }
25446
25447 /* Instantiate the body of D using PATTERN with ARGS. We have
25448 already determined PATTERN is the correct template to use.
25449 NESTED_P is true if this is a nested function, in which case
25450 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25451
25452 static void
25453 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
25454 {
25455 tree td = NULL_TREE;
25456 tree code_pattern = pattern;
25457
25458 if (!nested_p)
25459 {
25460 td = pattern;
25461 code_pattern = DECL_TEMPLATE_RESULT (td);
25462 }
25463 else
25464 /* Only OMP reductions are nested. */
25465 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
25466
25467 vec<tree> omp_privatization_save;
25468 if (current_function_decl)
25469 save_omp_privatization_clauses (omp_privatization_save);
25470
25471 bool push_to_top
25472 = !(current_function_decl
25473 && !LAMBDA_FUNCTION_P (d)
25474 && decl_function_context (d) == current_function_decl);
25475
25476 if (push_to_top)
25477 push_to_top_level ();
25478 else
25479 {
25480 gcc_assert (!processing_template_decl);
25481 push_function_context ();
25482 cp_unevaluated_operand = 0;
25483 c_inhibit_evaluation_warnings = 0;
25484 }
25485
25486 if (VAR_P (d))
25487 {
25488 /* The variable might be a lambda's extra scope, and that
25489 lambda's visibility depends on D's. */
25490 maybe_commonize_var (d);
25491 determine_visibility (d);
25492 }
25493
25494 /* Mark D as instantiated so that recursive calls to
25495 instantiate_decl do not try to instantiate it again. */
25496 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25497
25498 if (td)
25499 /* Regenerate the declaration in case the template has been modified
25500 by a subsequent redeclaration. */
25501 regenerate_decl_from_template (d, td, args);
25502
25503 /* We already set the file and line above. Reset them now in case
25504 they changed as a result of calling regenerate_decl_from_template. */
25505 input_location = DECL_SOURCE_LOCATION (d);
25506
25507 if (VAR_P (d))
25508 {
25509 tree init;
25510 bool const_init = false;
25511
25512 /* Clear out DECL_RTL; whatever was there before may not be right
25513 since we've reset the type of the declaration. */
25514 SET_DECL_RTL (d, NULL);
25515 DECL_IN_AGGR_P (d) = 0;
25516
25517 /* The initializer is placed in DECL_INITIAL by
25518 regenerate_decl_from_template so we don't need to
25519 push/pop_access_scope again here. Pull it out so that
25520 cp_finish_decl can process it. */
25521 init = DECL_INITIAL (d);
25522 DECL_INITIAL (d) = NULL_TREE;
25523 DECL_INITIALIZED_P (d) = 0;
25524
25525 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25526 initializer. That function will defer actual emission until
25527 we have a chance to determine linkage. */
25528 DECL_EXTERNAL (d) = 0;
25529
25530 /* Enter the scope of D so that access-checking works correctly. */
25531 bool enter_context = DECL_CLASS_SCOPE_P (d);
25532 if (enter_context)
25533 push_nested_class (DECL_CONTEXT (d));
25534
25535 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25536 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25537
25538 if (enter_context)
25539 pop_nested_class ();
25540 }
25541 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25542 synthesize_method (d);
25543 else if (TREE_CODE (d) == FUNCTION_DECL)
25544 {
25545 /* Set up the list of local specializations. */
25546 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25547 tree block = NULL_TREE;
25548
25549 /* Set up context. */
25550 if (nested_p)
25551 block = push_stmt_list ();
25552 else
25553 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25554
25555 perform_instantiation_time_access_checks (code_pattern, args);
25556
25557 /* Create substitution entries for the parameters. */
25558 register_parameter_specializations (code_pattern, d);
25559
25560 /* Substitute into the body of the function. */
25561 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25562 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25563 tf_warning_or_error, d);
25564 else
25565 {
25566 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25567 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25568 /*integral_constant_expression_p=*/false);
25569
25570 /* Set the current input_location to the end of the function
25571 so that finish_function knows where we are. */
25572 input_location
25573 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25574
25575 /* Remember if we saw an infinite loop in the template. */
25576 current_function_infinite_loop
25577 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25578 }
25579
25580 /* Finish the function. */
25581 if (nested_p)
25582 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25583 else
25584 {
25585 d = finish_function (/*inline_p=*/false);
25586 expand_or_defer_fn (d);
25587 }
25588
25589 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25590 cp_check_omp_declare_reduction (d);
25591 }
25592
25593 /* We're not deferring instantiation any more. */
25594 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25595
25596 if (push_to_top)
25597 pop_from_top_level ();
25598 else
25599 pop_function_context ();
25600
25601 if (current_function_decl)
25602 restore_omp_privatization_clauses (omp_privatization_save);
25603 }
25604
25605 /* Produce the definition of D, a _DECL generated from a template. If
25606 DEFER_OK is true, then we don't have to actually do the
25607 instantiation now; we just have to do it sometime. Normally it is
25608 an error if this is an explicit instantiation but D is undefined.
25609 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25610 instantiated class template. */
25611
25612 tree
25613 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25614 {
25615 tree tmpl = DECL_TI_TEMPLATE (d);
25616 tree gen_args;
25617 tree args;
25618 tree td;
25619 tree code_pattern;
25620 tree spec;
25621 tree gen_tmpl;
25622 bool pattern_defined;
25623 location_t saved_loc = input_location;
25624 int saved_unevaluated_operand = cp_unevaluated_operand;
25625 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25626 bool external_p;
25627 bool deleted_p;
25628
25629 /* This function should only be used to instantiate templates for
25630 functions and static member variables. */
25631 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25632
25633 /* A concept is never instantiated. */
25634 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25635
25636 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
25637
25638 /* Variables are never deferred; if instantiation is required, they
25639 are instantiated right away. That allows for better code in the
25640 case that an expression refers to the value of the variable --
25641 if the variable has a constant value the referring expression can
25642 take advantage of that fact. */
25643 if (VAR_P (d))
25644 defer_ok = false;
25645
25646 /* Don't instantiate cloned functions. Instead, instantiate the
25647 functions they cloned. */
25648 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25649 d = DECL_CLONED_FUNCTION (d);
25650
25651 if (DECL_TEMPLATE_INSTANTIATED (d)
25652 || TREE_TYPE (d) == error_mark_node
25653 || (TREE_CODE (d) == FUNCTION_DECL
25654 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25655 || DECL_TEMPLATE_SPECIALIZATION (d))
25656 /* D has already been instantiated or explicitly specialized, so
25657 there's nothing for us to do here.
25658
25659 It might seem reasonable to check whether or not D is an explicit
25660 instantiation, and, if so, stop here. But when an explicit
25661 instantiation is deferred until the end of the compilation,
25662 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25663 the instantiation. */
25664 return d;
25665
25666 /* Check to see whether we know that this template will be
25667 instantiated in some other file, as with "extern template"
25668 extension. */
25669 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25670
25671 /* In general, we do not instantiate such templates. */
25672 if (external_p && !always_instantiate_p (d))
25673 return d;
25674
25675 gen_tmpl = most_general_template (tmpl);
25676 gen_args = DECL_TI_ARGS (d);
25677
25678 /* We should already have the extra args. */
25679 gcc_checking_assert (tmpl == gen_tmpl
25680 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25681 == TMPL_ARGS_DEPTH (gen_args)));
25682 /* And what's in the hash table should match D. */
25683 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25684 == d
25685 || spec == NULL_TREE);
25686
25687 /* This needs to happen before any tsubsting. */
25688 if (! push_tinst_level (d))
25689 return d;
25690
25691 timevar_push (TV_TEMPLATE_INST);
25692
25693 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25694 for the instantiation. */
25695 td = template_for_substitution (d);
25696 args = gen_args;
25697
25698 if (VAR_P (d))
25699 {
25700 /* Look up an explicit specialization, if any. */
25701 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25702 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25703 if (elt && elt != error_mark_node)
25704 {
25705 td = TREE_VALUE (elt);
25706 args = TREE_PURPOSE (elt);
25707 }
25708 }
25709
25710 code_pattern = DECL_TEMPLATE_RESULT (td);
25711
25712 /* We should never be trying to instantiate a member of a class
25713 template or partial specialization. */
25714 gcc_assert (d != code_pattern);
25715
25716 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25717 || DECL_TEMPLATE_SPECIALIZATION (td))
25718 /* In the case of a friend template whose definition is provided
25719 outside the class, we may have too many arguments. Drop the
25720 ones we don't need. The same is true for specializations. */
25721 args = get_innermost_template_args
25722 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25723
25724 if (TREE_CODE (d) == FUNCTION_DECL)
25725 {
25726 deleted_p = DECL_DELETED_FN (code_pattern);
25727 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25728 && DECL_INITIAL (code_pattern) != error_mark_node)
25729 || DECL_DEFAULTED_FN (code_pattern)
25730 || deleted_p);
25731 }
25732 else
25733 {
25734 deleted_p = false;
25735 if (DECL_CLASS_SCOPE_P (code_pattern))
25736 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25737 else
25738 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25739 }
25740
25741 /* We may be in the middle of deferred access check. Disable it now. */
25742 push_deferring_access_checks (dk_no_deferred);
25743
25744 /* Unless an explicit instantiation directive has already determined
25745 the linkage of D, remember that a definition is available for
25746 this entity. */
25747 if (pattern_defined
25748 && !DECL_INTERFACE_KNOWN (d)
25749 && !DECL_NOT_REALLY_EXTERN (d))
25750 mark_definable (d);
25751
25752 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25753 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25754 input_location = DECL_SOURCE_LOCATION (d);
25755
25756 /* If D is a member of an explicitly instantiated class template,
25757 and no definition is available, treat it like an implicit
25758 instantiation. */
25759 if (!pattern_defined && expl_inst_class_mem_p
25760 && DECL_EXPLICIT_INSTANTIATION (d))
25761 {
25762 /* Leave linkage flags alone on instantiations with anonymous
25763 visibility. */
25764 if (TREE_PUBLIC (d))
25765 {
25766 DECL_NOT_REALLY_EXTERN (d) = 0;
25767 DECL_INTERFACE_KNOWN (d) = 0;
25768 }
25769 SET_DECL_IMPLICIT_INSTANTIATION (d);
25770 }
25771
25772 /* Defer all other templates, unless we have been explicitly
25773 forbidden from doing so. */
25774 if (/* If there is no definition, we cannot instantiate the
25775 template. */
25776 ! pattern_defined
25777 /* If it's OK to postpone instantiation, do so. */
25778 || defer_ok
25779 /* If this is a static data member that will be defined
25780 elsewhere, we don't want to instantiate the entire data
25781 member, but we do want to instantiate the initializer so that
25782 we can substitute that elsewhere. */
25783 || (external_p && VAR_P (d))
25784 /* Handle here a deleted function too, avoid generating
25785 its body (c++/61080). */
25786 || deleted_p)
25787 {
25788 /* The definition of the static data member is now required so
25789 we must substitute the initializer. */
25790 if (VAR_P (d)
25791 && !DECL_INITIAL (d)
25792 && DECL_INITIAL (code_pattern))
25793 {
25794 tree ns;
25795 tree init;
25796 bool const_init = false;
25797 bool enter_context = DECL_CLASS_SCOPE_P (d);
25798
25799 ns = decl_namespace_context (d);
25800 push_nested_namespace (ns);
25801 if (enter_context)
25802 push_nested_class (DECL_CONTEXT (d));
25803 init = tsubst_expr (DECL_INITIAL (code_pattern),
25804 args,
25805 tf_warning_or_error, NULL_TREE,
25806 /*integral_constant_expression_p=*/false);
25807 /* If instantiating the initializer involved instantiating this
25808 again, don't call cp_finish_decl twice. */
25809 if (!DECL_INITIAL (d))
25810 {
25811 /* Make sure the initializer is still constant, in case of
25812 circular dependency (template/instantiate6.C). */
25813 const_init
25814 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25815 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25816 /*asmspec_tree=*/NULL_TREE,
25817 LOOKUP_ONLYCONVERTING);
25818 }
25819 if (enter_context)
25820 pop_nested_class ();
25821 pop_nested_namespace (ns);
25822 }
25823
25824 /* We restore the source position here because it's used by
25825 add_pending_template. */
25826 input_location = saved_loc;
25827
25828 if (at_eof && !pattern_defined
25829 && DECL_EXPLICIT_INSTANTIATION (d)
25830 && DECL_NOT_REALLY_EXTERN (d))
25831 /* [temp.explicit]
25832
25833 The definition of a non-exported function template, a
25834 non-exported member function template, or a non-exported
25835 member function or static data member of a class template
25836 shall be present in every translation unit in which it is
25837 explicitly instantiated. */
25838 permerror (input_location, "explicit instantiation of %qD "
25839 "but no definition available", d);
25840
25841 /* If we're in unevaluated context, we just wanted to get the
25842 constant value; this isn't an odr use, so don't queue
25843 a full instantiation. */
25844 if (!cp_unevaluated_operand
25845 /* ??? Historically, we have instantiated inline functions, even
25846 when marked as "extern template". */
25847 && !(external_p && VAR_P (d)))
25848 add_pending_template (d);
25849 }
25850 else
25851 {
25852 if (variable_template_p (gen_tmpl))
25853 note_variable_template_instantiation (d);
25854 instantiate_body (td, args, d, false);
25855 }
25856
25857 pop_deferring_access_checks ();
25858 timevar_pop (TV_TEMPLATE_INST);
25859 pop_tinst_level ();
25860 input_location = saved_loc;
25861 cp_unevaluated_operand = saved_unevaluated_operand;
25862 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25863
25864 return d;
25865 }
25866
25867 /* Run through the list of templates that we wish we could
25868 instantiate, and instantiate any we can. RETRIES is the
25869 number of times we retry pending template instantiation. */
25870
25871 void
25872 instantiate_pending_templates (int retries)
25873 {
25874 int reconsider;
25875 location_t saved_loc = input_location;
25876
25877 /* Instantiating templates may trigger vtable generation. This in turn
25878 may require further template instantiations. We place a limit here
25879 to avoid infinite loop. */
25880 if (pending_templates && retries >= max_tinst_depth)
25881 {
25882 tree decl = pending_templates->tinst->maybe_get_node ();
25883
25884 fatal_error (input_location,
25885 "template instantiation depth exceeds maximum of %d"
25886 " instantiating %q+D, possibly from virtual table generation"
25887 " (use %<-ftemplate-depth=%> to increase the maximum)",
25888 max_tinst_depth, decl);
25889 if (TREE_CODE (decl) == FUNCTION_DECL)
25890 /* Pretend that we defined it. */
25891 DECL_INITIAL (decl) = error_mark_node;
25892 return;
25893 }
25894
25895 do
25896 {
25897 struct pending_template **t = &pending_templates;
25898 struct pending_template *last = NULL;
25899 reconsider = 0;
25900 while (*t)
25901 {
25902 tree instantiation = reopen_tinst_level ((*t)->tinst);
25903 bool complete = false;
25904
25905 if (TYPE_P (instantiation))
25906 {
25907 if (!COMPLETE_TYPE_P (instantiation))
25908 {
25909 instantiate_class_template (instantiation);
25910 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25911 for (tree fld = TYPE_FIELDS (instantiation);
25912 fld; fld = TREE_CHAIN (fld))
25913 if ((VAR_P (fld)
25914 || (TREE_CODE (fld) == FUNCTION_DECL
25915 && !DECL_ARTIFICIAL (fld)))
25916 && DECL_TEMPLATE_INSTANTIATION (fld))
25917 instantiate_decl (fld,
25918 /*defer_ok=*/false,
25919 /*expl_inst_class_mem_p=*/false);
25920
25921 if (COMPLETE_TYPE_P (instantiation))
25922 reconsider = 1;
25923 }
25924
25925 complete = COMPLETE_TYPE_P (instantiation);
25926 }
25927 else
25928 {
25929 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25930 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25931 {
25932 instantiation
25933 = instantiate_decl (instantiation,
25934 /*defer_ok=*/false,
25935 /*expl_inst_class_mem_p=*/false);
25936 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25937 reconsider = 1;
25938 }
25939
25940 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25941 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25942 }
25943
25944 if (complete)
25945 {
25946 /* If INSTANTIATION has been instantiated, then we don't
25947 need to consider it again in the future. */
25948 struct pending_template *drop = *t;
25949 *t = (*t)->next;
25950 set_refcount_ptr (drop->tinst);
25951 pending_template_freelist ().free (drop);
25952 }
25953 else
25954 {
25955 last = *t;
25956 t = &(*t)->next;
25957 }
25958 tinst_depth = 0;
25959 set_refcount_ptr (current_tinst_level);
25960 }
25961 last_pending_template = last;
25962 }
25963 while (reconsider);
25964
25965 input_location = saved_loc;
25966 }
25967
25968 /* Substitute ARGVEC into T, which is a list of initializers for
25969 either base class or a non-static data member. The TREE_PURPOSEs
25970 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25971 instantiate_decl. */
25972
25973 static tree
25974 tsubst_initializer_list (tree t, tree argvec)
25975 {
25976 tree inits = NULL_TREE;
25977 tree target_ctor = error_mark_node;
25978
25979 for (; t; t = TREE_CHAIN (t))
25980 {
25981 tree decl;
25982 tree init;
25983 tree expanded_bases = NULL_TREE;
25984 tree expanded_arguments = NULL_TREE;
25985 int i, len = 1;
25986
25987 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25988 {
25989 tree expr;
25990 tree arg;
25991
25992 /* Expand the base class expansion type into separate base
25993 classes. */
25994 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25995 tf_warning_or_error,
25996 NULL_TREE);
25997 if (expanded_bases == error_mark_node)
25998 continue;
25999
26000 /* We'll be building separate TREE_LISTs of arguments for
26001 each base. */
26002 len = TREE_VEC_LENGTH (expanded_bases);
26003 expanded_arguments = make_tree_vec (len);
26004 for (i = 0; i < len; i++)
26005 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26006
26007 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26008 expand each argument in the TREE_VALUE of t. */
26009 expr = make_node (EXPR_PACK_EXPANSION);
26010 PACK_EXPANSION_LOCAL_P (expr) = true;
26011 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26012 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26013
26014 if (TREE_VALUE (t) == void_type_node)
26015 /* VOID_TYPE_NODE is used to indicate
26016 value-initialization. */
26017 {
26018 for (i = 0; i < len; i++)
26019 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26020 }
26021 else
26022 {
26023 /* Substitute parameter packs into each argument in the
26024 TREE_LIST. */
26025 in_base_initializer = 1;
26026 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26027 {
26028 tree expanded_exprs;
26029
26030 /* Expand the argument. */
26031 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
26032 expanded_exprs
26033 = tsubst_pack_expansion (expr, argvec,
26034 tf_warning_or_error,
26035 NULL_TREE);
26036 if (expanded_exprs == error_mark_node)
26037 continue;
26038
26039 /* Prepend each of the expanded expressions to the
26040 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26041 for (i = 0; i < len; i++)
26042 {
26043 TREE_VEC_ELT (expanded_arguments, i) =
26044 tree_cons (NULL_TREE,
26045 TREE_VEC_ELT (expanded_exprs, i),
26046 TREE_VEC_ELT (expanded_arguments, i));
26047 }
26048 }
26049 in_base_initializer = 0;
26050
26051 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26052 since we built them backwards. */
26053 for (i = 0; i < len; i++)
26054 {
26055 TREE_VEC_ELT (expanded_arguments, i) =
26056 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26057 }
26058 }
26059 }
26060
26061 for (i = 0; i < len; ++i)
26062 {
26063 if (expanded_bases)
26064 {
26065 decl = TREE_VEC_ELT (expanded_bases, i);
26066 decl = expand_member_init (decl);
26067 init = TREE_VEC_ELT (expanded_arguments, i);
26068 }
26069 else
26070 {
26071 tree tmp;
26072 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26073 tf_warning_or_error, NULL_TREE);
26074
26075 decl = expand_member_init (decl);
26076 if (decl && !DECL_P (decl))
26077 in_base_initializer = 1;
26078
26079 init = TREE_VALUE (t);
26080 tmp = init;
26081 if (init != void_type_node)
26082 init = tsubst_expr (init, argvec,
26083 tf_warning_or_error, NULL_TREE,
26084 /*integral_constant_expression_p=*/false);
26085 if (init == NULL_TREE && tmp != NULL_TREE)
26086 /* If we had an initializer but it instantiated to nothing,
26087 value-initialize the object. This will only occur when
26088 the initializer was a pack expansion where the parameter
26089 packs used in that expansion were of length zero. */
26090 init = void_type_node;
26091 in_base_initializer = 0;
26092 }
26093
26094 if (target_ctor != error_mark_node
26095 && init != error_mark_node)
26096 {
26097 error ("mem-initializer for %qD follows constructor delegation",
26098 decl);
26099 return inits;
26100 }
26101 /* Look for a target constructor. */
26102 if (init != error_mark_node
26103 && decl && CLASS_TYPE_P (decl)
26104 && same_type_p (decl, current_class_type))
26105 {
26106 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26107 if (inits)
26108 {
26109 error ("constructor delegation follows mem-initializer for %qD",
26110 TREE_PURPOSE (inits));
26111 continue;
26112 }
26113 target_ctor = init;
26114 }
26115
26116 if (decl)
26117 {
26118 init = build_tree_list (decl, init);
26119 /* Carry over the dummy TREE_TYPE node containing the source
26120 location. */
26121 TREE_TYPE (init) = TREE_TYPE (t);
26122 TREE_CHAIN (init) = inits;
26123 inits = init;
26124 }
26125 }
26126 }
26127 return inits;
26128 }
26129
26130 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26131
26132 static void
26133 set_current_access_from_decl (tree decl)
26134 {
26135 if (TREE_PRIVATE (decl))
26136 current_access_specifier = access_private_node;
26137 else if (TREE_PROTECTED (decl))
26138 current_access_specifier = access_protected_node;
26139 else
26140 current_access_specifier = access_public_node;
26141 }
26142
26143 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26144 is the instantiation (which should have been created with
26145 start_enum) and ARGS are the template arguments to use. */
26146
26147 static void
26148 tsubst_enum (tree tag, tree newtag, tree args)
26149 {
26150 tree e;
26151
26152 if (SCOPED_ENUM_P (newtag))
26153 begin_scope (sk_scoped_enum, newtag);
26154
26155 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26156 {
26157 tree value;
26158 tree decl;
26159
26160 decl = TREE_VALUE (e);
26161 /* Note that in a template enum, the TREE_VALUE is the
26162 CONST_DECL, not the corresponding INTEGER_CST. */
26163 value = tsubst_expr (DECL_INITIAL (decl),
26164 args, tf_warning_or_error, NULL_TREE,
26165 /*integral_constant_expression_p=*/true);
26166
26167 /* Give this enumeration constant the correct access. */
26168 set_current_access_from_decl (decl);
26169
26170 /* Actually build the enumerator itself. Here we're assuming that
26171 enumerators can't have dependent attributes. */
26172 build_enumerator (DECL_NAME (decl), value, newtag,
26173 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26174 }
26175
26176 if (SCOPED_ENUM_P (newtag))
26177 finish_scope ();
26178
26179 finish_enum_value_list (newtag);
26180 finish_enum (newtag);
26181
26182 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26183 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26184 }
26185
26186 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26187 its type -- but without substituting the innermost set of template
26188 arguments. So, innermost set of template parameters will appear in
26189 the type. */
26190
26191 tree
26192 get_mostly_instantiated_function_type (tree decl)
26193 {
26194 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26195 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26196 }
26197
26198 /* Return truthvalue if we're processing a template different from
26199 the last one involved in diagnostics. */
26200 bool
26201 problematic_instantiation_changed (void)
26202 {
26203 return current_tinst_level != last_error_tinst_level;
26204 }
26205
26206 /* Remember current template involved in diagnostics. */
26207 void
26208 record_last_problematic_instantiation (void)
26209 {
26210 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26211 }
26212
26213 struct tinst_level *
26214 current_instantiation (void)
26215 {
26216 return current_tinst_level;
26217 }
26218
26219 /* Return TRUE if current_function_decl is being instantiated, false
26220 otherwise. */
26221
26222 bool
26223 instantiating_current_function_p (void)
26224 {
26225 return (current_instantiation ()
26226 && (current_instantiation ()->maybe_get_node ()
26227 == current_function_decl));
26228 }
26229
26230 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26231 type. Return false for ok, true for disallowed. Issue error and
26232 inform messages under control of COMPLAIN. */
26233
26234 static bool
26235 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26236 {
26237 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26238 return false;
26239 else if (TYPE_PTR_P (type))
26240 return false;
26241 else if (TYPE_REF_P (type)
26242 && !TYPE_REF_IS_RVALUE (type))
26243 return false;
26244 else if (TYPE_PTRMEM_P (type))
26245 return false;
26246 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26247 {
26248 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26249 {
26250 if (complain & tf_error)
26251 error ("non-type template parameters of deduced class type only "
26252 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26253 return true;
26254 }
26255 return false;
26256 }
26257 else if (TREE_CODE (type) == TYPENAME_TYPE)
26258 return false;
26259 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26260 return false;
26261 else if (TREE_CODE (type) == NULLPTR_TYPE)
26262 return false;
26263 /* A bound template template parm could later be instantiated to have a valid
26264 nontype parm type via an alias template. */
26265 else if (cxx_dialect >= cxx11
26266 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26267 return false;
26268 else if (VOID_TYPE_P (type))
26269 /* Fall through. */;
26270 else if (cxx_dialect >= cxx20)
26271 {
26272 if (dependent_type_p (type))
26273 return false;
26274 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26275 return true;
26276 if (structural_type_p (type))
26277 return false;
26278 if (complain & tf_error)
26279 {
26280 auto_diagnostic_group d;
26281 error ("%qT is not a valid type for a template non-type "
26282 "parameter because it is not structural", type);
26283 structural_type_p (type, true);
26284 }
26285 return true;
26286 }
26287 else if (CLASS_TYPE_P (type))
26288 {
26289 if (complain & tf_error)
26290 error ("non-type template parameters of class type only available "
26291 "with %<-std=c++20%> or %<-std=gnu++20%>");
26292 return true;
26293 }
26294
26295 if (complain & tf_error)
26296 {
26297 if (type == error_mark_node)
26298 inform (input_location, "invalid template non-type parameter");
26299 else
26300 error ("%q#T is not a valid type for a template non-type parameter",
26301 type);
26302 }
26303 return true;
26304 }
26305
26306 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26307 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26308
26309 static bool
26310 dependent_type_p_r (tree type)
26311 {
26312 tree scope;
26313
26314 /* [temp.dep.type]
26315
26316 A type is dependent if it is:
26317
26318 -- a template parameter. Template template parameters are types
26319 for us (since TYPE_P holds true for them) so we handle
26320 them here. */
26321 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26322 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26323 return true;
26324 /* -- a qualified-id with a nested-name-specifier which contains a
26325 class-name that names a dependent type or whose unqualified-id
26326 names a dependent type. */
26327 if (TREE_CODE (type) == TYPENAME_TYPE)
26328 return true;
26329
26330 /* An alias template specialization can be dependent even if the
26331 resulting type is not. */
26332 if (dependent_alias_template_spec_p (type, nt_transparent))
26333 return true;
26334
26335 /* -- a cv-qualified type where the cv-unqualified type is
26336 dependent.
26337 No code is necessary for this bullet; the code below handles
26338 cv-qualified types, and we don't want to strip aliases with
26339 TYPE_MAIN_VARIANT because of DR 1558. */
26340 /* -- a compound type constructed from any dependent type. */
26341 if (TYPE_PTRMEM_P (type))
26342 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26343 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26344 (type)));
26345 else if (INDIRECT_TYPE_P (type))
26346 return dependent_type_p (TREE_TYPE (type));
26347 else if (FUNC_OR_METHOD_TYPE_P (type))
26348 {
26349 tree arg_type;
26350
26351 if (dependent_type_p (TREE_TYPE (type)))
26352 return true;
26353 for (arg_type = TYPE_ARG_TYPES (type);
26354 arg_type;
26355 arg_type = TREE_CHAIN (arg_type))
26356 if (dependent_type_p (TREE_VALUE (arg_type)))
26357 return true;
26358 if (cxx_dialect >= cxx17)
26359 /* A value-dependent noexcept-specifier makes the type dependent. */
26360 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26361 if (tree noex = TREE_PURPOSE (spec))
26362 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26363 affect overload resolution and treating it as dependent breaks
26364 things. Same for an unparsed noexcept expression. */
26365 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26366 && TREE_CODE (noex) != DEFERRED_PARSE
26367 && value_dependent_expression_p (noex))
26368 return true;
26369 return false;
26370 }
26371 /* -- an array type constructed from any dependent type or whose
26372 size is specified by a constant expression that is
26373 value-dependent.
26374
26375 We checked for type- and value-dependence of the bounds in
26376 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26377 if (TREE_CODE (type) == ARRAY_TYPE)
26378 {
26379 if (TYPE_DOMAIN (type)
26380 && dependent_type_p (TYPE_DOMAIN (type)))
26381 return true;
26382 return dependent_type_p (TREE_TYPE (type));
26383 }
26384
26385 /* -- a template-id in which either the template name is a template
26386 parameter ... */
26387 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26388 return true;
26389 /* ... or any of the template arguments is a dependent type or
26390 an expression that is type-dependent or value-dependent. */
26391 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26392 && (any_dependent_template_arguments_p
26393 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26394 return true;
26395
26396 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26397 dependent; if the argument of the `typeof' expression is not
26398 type-dependent, then it should already been have resolved. */
26399 if (TREE_CODE (type) == TYPEOF_TYPE
26400 || TREE_CODE (type) == DECLTYPE_TYPE
26401 || TREE_CODE (type) == UNDERLYING_TYPE)
26402 return true;
26403
26404 /* A template argument pack is dependent if any of its packed
26405 arguments are. */
26406 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26407 {
26408 tree args = ARGUMENT_PACK_ARGS (type);
26409 int i, len = TREE_VEC_LENGTH (args);
26410 for (i = 0; i < len; ++i)
26411 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26412 return true;
26413 }
26414
26415 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26416 be template parameters. */
26417 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26418 return true;
26419
26420 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26421 return true;
26422
26423 /* The standard does not specifically mention types that are local
26424 to template functions or local classes, but they should be
26425 considered dependent too. For example:
26426
26427 template <int I> void f() {
26428 enum E { a = I };
26429 S<sizeof (E)> s;
26430 }
26431
26432 The size of `E' cannot be known until the value of `I' has been
26433 determined. Therefore, `E' must be considered dependent. */
26434 scope = TYPE_CONTEXT (type);
26435 if (scope && TYPE_P (scope))
26436 return dependent_type_p (scope);
26437 /* Don't use type_dependent_expression_p here, as it can lead
26438 to infinite recursion trying to determine whether a lambda
26439 nested in a lambda is dependent (c++/47687). */
26440 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26441 && DECL_LANG_SPECIFIC (scope)
26442 && DECL_TEMPLATE_INFO (scope)
26443 && (any_dependent_template_arguments_p
26444 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26445 return true;
26446
26447 /* Other types are non-dependent. */
26448 return false;
26449 }
26450
26451 /* Returns TRUE if TYPE is dependent, in the sense of
26452 [temp.dep.type]. Note that a NULL type is considered dependent. */
26453
26454 bool
26455 dependent_type_p (tree type)
26456 {
26457 /* If there are no template parameters in scope, then there can't be
26458 any dependent types. */
26459 if (!processing_template_decl)
26460 {
26461 /* If we are not processing a template, then nobody should be
26462 providing us with a dependent type. */
26463 gcc_assert (type);
26464 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26465 return false;
26466 }
26467
26468 /* If the type is NULL, we have not computed a type for the entity
26469 in question; in that case, the type is dependent. */
26470 if (!type)
26471 return true;
26472
26473 /* Erroneous types can be considered non-dependent. */
26474 if (type == error_mark_node)
26475 return false;
26476
26477 /* Getting here with global_type_node means we improperly called this
26478 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26479 gcc_checking_assert (type != global_type_node);
26480
26481 /* If we have not already computed the appropriate value for TYPE,
26482 do so now. */
26483 if (!TYPE_DEPENDENT_P_VALID (type))
26484 {
26485 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26486 TYPE_DEPENDENT_P_VALID (type) = 1;
26487 }
26488
26489 return TYPE_DEPENDENT_P (type);
26490 }
26491
26492 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26493 lookup. In other words, a dependent type that is not the current
26494 instantiation. */
26495
26496 bool
26497 dependent_scope_p (tree scope)
26498 {
26499 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26500 && !currently_open_class (scope));
26501 }
26502
26503 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26504 an unknown base of 'this' (and is therefore instantiation-dependent). */
26505
26506 static bool
26507 unknown_base_ref_p (tree t)
26508 {
26509 if (!current_class_ptr)
26510 return false;
26511
26512 tree mem = TREE_OPERAND (t, 1);
26513 if (shared_member_p (mem))
26514 return false;
26515
26516 tree cur = current_nonlambda_class_type ();
26517 if (!any_dependent_bases_p (cur))
26518 return false;
26519
26520 tree ctx = TREE_OPERAND (t, 0);
26521 if (DERIVED_FROM_P (ctx, cur))
26522 return false;
26523
26524 return true;
26525 }
26526
26527 /* T is a SCOPE_REF; return whether we need to consider it
26528 instantiation-dependent so that we can check access at instantiation
26529 time even though we know which member it resolves to. */
26530
26531 static bool
26532 instantiation_dependent_scope_ref_p (tree t)
26533 {
26534 if (DECL_P (TREE_OPERAND (t, 1))
26535 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26536 && !unknown_base_ref_p (t)
26537 && accessible_in_template_p (TREE_OPERAND (t, 0),
26538 TREE_OPERAND (t, 1)))
26539 return false;
26540 else
26541 return true;
26542 }
26543
26544 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26545 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26546 expression. */
26547
26548 /* Note that this predicate is not appropriate for general expressions;
26549 only constant expressions (that satisfy potential_constant_expression)
26550 can be tested for value dependence. */
26551
26552 bool
26553 value_dependent_expression_p (tree expression)
26554 {
26555 if (!processing_template_decl || expression == NULL_TREE)
26556 return false;
26557
26558 /* A type-dependent expression is also value-dependent. */
26559 if (type_dependent_expression_p (expression))
26560 return true;
26561
26562 switch (TREE_CODE (expression))
26563 {
26564 case BASELINK:
26565 /* A dependent member function of the current instantiation. */
26566 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26567
26568 case FUNCTION_DECL:
26569 /* A dependent member function of the current instantiation. */
26570 if (DECL_CLASS_SCOPE_P (expression)
26571 && dependent_type_p (DECL_CONTEXT (expression)))
26572 return true;
26573 break;
26574
26575 case IDENTIFIER_NODE:
26576 /* A name that has not been looked up -- must be dependent. */
26577 return true;
26578
26579 case TEMPLATE_PARM_INDEX:
26580 /* A non-type template parm. */
26581 return true;
26582
26583 case CONST_DECL:
26584 /* A non-type template parm. */
26585 if (DECL_TEMPLATE_PARM_P (expression))
26586 return true;
26587 return value_dependent_expression_p (DECL_INITIAL (expression));
26588
26589 case VAR_DECL:
26590 /* A constant with literal type and is initialized
26591 with an expression that is value-dependent. */
26592 if (DECL_DEPENDENT_INIT_P (expression)
26593 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26594 || TYPE_REF_P (TREE_TYPE (expression)))
26595 return true;
26596 if (DECL_HAS_VALUE_EXPR_P (expression))
26597 {
26598 tree value_expr = DECL_VALUE_EXPR (expression);
26599 if (value_dependent_expression_p (value_expr)
26600 /* __PRETTY_FUNCTION__ inside a template function is dependent
26601 on the name of the function. */
26602 || (DECL_PRETTY_FUNCTION_P (expression)
26603 /* It might be used in a template, but not a template
26604 function, in which case its DECL_VALUE_EXPR will be
26605 "top level". */
26606 && value_expr == error_mark_node))
26607 return true;
26608 }
26609 return false;
26610
26611 case DYNAMIC_CAST_EXPR:
26612 case STATIC_CAST_EXPR:
26613 case CONST_CAST_EXPR:
26614 case REINTERPRET_CAST_EXPR:
26615 case CAST_EXPR:
26616 case IMPLICIT_CONV_EXPR:
26617 /* These expressions are value-dependent if the type to which
26618 the cast occurs is dependent or the expression being casted
26619 is value-dependent. */
26620 {
26621 tree type = TREE_TYPE (expression);
26622
26623 if (dependent_type_p (type))
26624 return true;
26625
26626 /* A functional cast has a list of operands. */
26627 expression = TREE_OPERAND (expression, 0);
26628 if (!expression)
26629 {
26630 /* If there are no operands, it must be an expression such
26631 as "int()". This should not happen for aggregate types
26632 because it would form non-constant expressions. */
26633 gcc_assert (cxx_dialect >= cxx11
26634 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26635
26636 return false;
26637 }
26638
26639 if (TREE_CODE (expression) == TREE_LIST)
26640 return any_value_dependent_elements_p (expression);
26641
26642 return value_dependent_expression_p (expression);
26643 }
26644
26645 case SIZEOF_EXPR:
26646 if (SIZEOF_EXPR_TYPE_P (expression))
26647 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26648 /* FALLTHRU */
26649 case ALIGNOF_EXPR:
26650 case TYPEID_EXPR:
26651 /* A `sizeof' expression is value-dependent if the operand is
26652 type-dependent or is a pack expansion. */
26653 expression = TREE_OPERAND (expression, 0);
26654 if (PACK_EXPANSION_P (expression))
26655 return true;
26656 else if (TYPE_P (expression))
26657 return dependent_type_p (expression);
26658 return instantiation_dependent_uneval_expression_p (expression);
26659
26660 case AT_ENCODE_EXPR:
26661 /* An 'encode' expression is value-dependent if the operand is
26662 type-dependent. */
26663 expression = TREE_OPERAND (expression, 0);
26664 return dependent_type_p (expression);
26665
26666 case NOEXCEPT_EXPR:
26667 expression = TREE_OPERAND (expression, 0);
26668 return instantiation_dependent_uneval_expression_p (expression);
26669
26670 case SCOPE_REF:
26671 /* All instantiation-dependent expressions should also be considered
26672 value-dependent. */
26673 return instantiation_dependent_scope_ref_p (expression);
26674
26675 case COMPONENT_REF:
26676 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26677 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26678
26679 case NONTYPE_ARGUMENT_PACK:
26680 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26681 is value-dependent. */
26682 {
26683 tree values = ARGUMENT_PACK_ARGS (expression);
26684 int i, len = TREE_VEC_LENGTH (values);
26685
26686 for (i = 0; i < len; ++i)
26687 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26688 return true;
26689
26690 return false;
26691 }
26692
26693 case TRAIT_EXPR:
26694 {
26695 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26696
26697 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26698 return true;
26699
26700 if (!type2)
26701 return false;
26702
26703 if (TREE_CODE (type2) != TREE_LIST)
26704 return dependent_type_p (type2);
26705
26706 for (; type2; type2 = TREE_CHAIN (type2))
26707 if (dependent_type_p (TREE_VALUE (type2)))
26708 return true;
26709
26710 return false;
26711 }
26712
26713 case MODOP_EXPR:
26714 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26715 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26716
26717 case ARRAY_REF:
26718 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26719 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26720
26721 case ADDR_EXPR:
26722 {
26723 tree op = TREE_OPERAND (expression, 0);
26724 return (value_dependent_expression_p (op)
26725 || has_value_dependent_address (op));
26726 }
26727
26728 case REQUIRES_EXPR:
26729 /* Treat all requires-expressions as value-dependent so
26730 we don't try to fold them. */
26731 return true;
26732
26733 case TYPE_REQ:
26734 return dependent_type_p (TREE_OPERAND (expression, 0));
26735
26736 case CALL_EXPR:
26737 {
26738 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26739 return true;
26740 tree fn = get_callee_fndecl (expression);
26741 int i, nargs;
26742 nargs = call_expr_nargs (expression);
26743 for (i = 0; i < nargs; ++i)
26744 {
26745 tree op = CALL_EXPR_ARG (expression, i);
26746 /* In a call to a constexpr member function, look through the
26747 implicit ADDR_EXPR on the object argument so that it doesn't
26748 cause the call to be considered value-dependent. We also
26749 look through it in potential_constant_expression. */
26750 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26751 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26752 && TREE_CODE (op) == ADDR_EXPR)
26753 op = TREE_OPERAND (op, 0);
26754 if (value_dependent_expression_p (op))
26755 return true;
26756 }
26757 return false;
26758 }
26759
26760 case TEMPLATE_ID_EXPR:
26761 return concept_definition_p (TREE_OPERAND (expression, 0));
26762
26763 case CONSTRUCTOR:
26764 {
26765 unsigned ix;
26766 tree val;
26767 if (dependent_type_p (TREE_TYPE (expression)))
26768 return true;
26769 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26770 if (value_dependent_expression_p (val))
26771 return true;
26772 return false;
26773 }
26774
26775 case STMT_EXPR:
26776 /* Treat a GNU statement expression as dependent to avoid crashing
26777 under instantiate_non_dependent_expr; it can't be constant. */
26778 return true;
26779
26780 default:
26781 /* A constant expression is value-dependent if any subexpression is
26782 value-dependent. */
26783 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26784 {
26785 case tcc_reference:
26786 case tcc_unary:
26787 case tcc_comparison:
26788 case tcc_binary:
26789 case tcc_expression:
26790 case tcc_vl_exp:
26791 {
26792 int i, len = cp_tree_operand_length (expression);
26793
26794 for (i = 0; i < len; i++)
26795 {
26796 tree t = TREE_OPERAND (expression, i);
26797
26798 /* In some cases, some of the operands may be missing.
26799 (For example, in the case of PREDECREMENT_EXPR, the
26800 amount to increment by may be missing.) That doesn't
26801 make the expression dependent. */
26802 if (t && value_dependent_expression_p (t))
26803 return true;
26804 }
26805 }
26806 break;
26807 default:
26808 break;
26809 }
26810 break;
26811 }
26812
26813 /* The expression is not value-dependent. */
26814 return false;
26815 }
26816
26817 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26818 [temp.dep.expr]. Note that an expression with no type is
26819 considered dependent. Other parts of the compiler arrange for an
26820 expression with type-dependent subexpressions to have no type, so
26821 this function doesn't have to be fully recursive. */
26822
26823 bool
26824 type_dependent_expression_p (tree expression)
26825 {
26826 if (!processing_template_decl)
26827 return false;
26828
26829 if (expression == NULL_TREE || expression == error_mark_node)
26830 return false;
26831
26832 STRIP_ANY_LOCATION_WRAPPER (expression);
26833
26834 /* An unresolved name is always dependent. */
26835 if (identifier_p (expression)
26836 || TREE_CODE (expression) == USING_DECL
26837 || TREE_CODE (expression) == WILDCARD_DECL)
26838 return true;
26839
26840 /* A lambda-expression in template context is dependent. dependent_type_p is
26841 true for a lambda in the scope of a class or function template, but that
26842 doesn't cover all template contexts, like a default template argument. */
26843 if (TREE_CODE (expression) == LAMBDA_EXPR)
26844 return true;
26845
26846 /* A fold expression is type-dependent. */
26847 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26848 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26849 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26850 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26851 return true;
26852
26853 /* Some expression forms are never type-dependent. */
26854 if (TREE_CODE (expression) == SIZEOF_EXPR
26855 || TREE_CODE (expression) == ALIGNOF_EXPR
26856 || TREE_CODE (expression) == AT_ENCODE_EXPR
26857 || TREE_CODE (expression) == NOEXCEPT_EXPR
26858 || TREE_CODE (expression) == TRAIT_EXPR
26859 || TREE_CODE (expression) == TYPEID_EXPR
26860 || TREE_CODE (expression) == DELETE_EXPR
26861 || TREE_CODE (expression) == VEC_DELETE_EXPR
26862 || TREE_CODE (expression) == THROW_EXPR
26863 || TREE_CODE (expression) == REQUIRES_EXPR)
26864 return false;
26865
26866 /* The types of these expressions depends only on the type to which
26867 the cast occurs. */
26868 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26869 || TREE_CODE (expression) == STATIC_CAST_EXPR
26870 || TREE_CODE (expression) == CONST_CAST_EXPR
26871 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26872 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26873 || TREE_CODE (expression) == CAST_EXPR)
26874 return dependent_type_p (TREE_TYPE (expression));
26875
26876 /* The types of these expressions depends only on the type created
26877 by the expression. */
26878 if (TREE_CODE (expression) == NEW_EXPR
26879 || TREE_CODE (expression) == VEC_NEW_EXPR)
26880 {
26881 /* For NEW_EXPR tree nodes created inside a template, either
26882 the object type itself or a TREE_LIST may appear as the
26883 operand 1. */
26884 tree type = TREE_OPERAND (expression, 1);
26885 if (TREE_CODE (type) == TREE_LIST)
26886 /* This is an array type. We need to check array dimensions
26887 as well. */
26888 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26889 || value_dependent_expression_p
26890 (TREE_OPERAND (TREE_VALUE (type), 1));
26891 /* Array type whose dimension has to be deduced. */
26892 else if (TREE_CODE (type) == ARRAY_TYPE
26893 && TREE_OPERAND (expression, 2) == NULL_TREE)
26894 return true;
26895 else
26896 return dependent_type_p (type);
26897 }
26898
26899 if (TREE_CODE (expression) == SCOPE_REF)
26900 {
26901 tree scope = TREE_OPERAND (expression, 0);
26902 tree name = TREE_OPERAND (expression, 1);
26903
26904 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26905 contains an identifier associated by name lookup with one or more
26906 declarations declared with a dependent type, or...a
26907 nested-name-specifier or qualified-id that names a member of an
26908 unknown specialization. */
26909 return (type_dependent_expression_p (name)
26910 || dependent_scope_p (scope));
26911 }
26912
26913 if (TREE_CODE (expression) == TEMPLATE_DECL
26914 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26915 return uses_outer_template_parms (expression);
26916
26917 if (TREE_CODE (expression) == STMT_EXPR)
26918 expression = stmt_expr_value_expr (expression);
26919
26920 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26921 {
26922 tree elt;
26923 unsigned i;
26924
26925 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26926 {
26927 if (type_dependent_expression_p (elt))
26928 return true;
26929 }
26930 return false;
26931 }
26932
26933 /* A static data member of the current instantiation with incomplete
26934 array type is type-dependent, as the definition and specializations
26935 can have different bounds. */
26936 if (VAR_P (expression)
26937 && DECL_CLASS_SCOPE_P (expression)
26938 && dependent_type_p (DECL_CONTEXT (expression))
26939 && VAR_HAD_UNKNOWN_BOUND (expression))
26940 return true;
26941
26942 /* An array of unknown bound depending on a variadic parameter, eg:
26943
26944 template<typename... Args>
26945 void foo (Args... args)
26946 {
26947 int arr[] = { args... };
26948 }
26949
26950 template<int... vals>
26951 void bar ()
26952 {
26953 int arr[] = { vals... };
26954 }
26955
26956 If the array has no length and has an initializer, it must be that
26957 we couldn't determine its length in cp_complete_array_type because
26958 it is dependent. */
26959 if (VAR_P (expression)
26960 && TREE_TYPE (expression) != NULL_TREE
26961 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26962 && !TYPE_DOMAIN (TREE_TYPE (expression))
26963 && DECL_INITIAL (expression))
26964 return true;
26965
26966 /* A function or variable template-id is type-dependent if it has any
26967 dependent template arguments. */
26968 if (VAR_OR_FUNCTION_DECL_P (expression)
26969 && DECL_LANG_SPECIFIC (expression)
26970 && DECL_TEMPLATE_INFO (expression))
26971 {
26972 /* Consider the innermost template arguments, since those are the ones
26973 that come from the template-id; the template arguments for the
26974 enclosing class do not make it type-dependent unless they are used in
26975 the type of the decl. */
26976 if (instantiates_primary_template_p (expression)
26977 && (any_dependent_template_arguments_p
26978 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26979 return true;
26980 }
26981
26982 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26983 type-dependent. Checking this is important for functions with auto return
26984 type, which looks like a dependent type. */
26985 if (TREE_CODE (expression) == FUNCTION_DECL
26986 && !(DECL_CLASS_SCOPE_P (expression)
26987 && dependent_type_p (DECL_CONTEXT (expression)))
26988 && !(DECL_LANG_SPECIFIC (expression)
26989 && DECL_FRIEND_P (expression)
26990 && (!DECL_FRIEND_CONTEXT (expression)
26991 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26992 && !DECL_LOCAL_DECL_P (expression))
26993 {
26994 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26995 || undeduced_auto_decl (expression));
26996 return false;
26997 }
26998
26999 /* Always dependent, on the number of arguments if nothing else. */
27000 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27001 return true;
27002
27003 if (TREE_TYPE (expression) == unknown_type_node)
27004 {
27005 if (TREE_CODE (expression) == ADDR_EXPR)
27006 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27007 if (TREE_CODE (expression) == COMPONENT_REF
27008 || TREE_CODE (expression) == OFFSET_REF)
27009 {
27010 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27011 return true;
27012 expression = TREE_OPERAND (expression, 1);
27013 if (identifier_p (expression))
27014 return false;
27015 }
27016 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27017 if (TREE_CODE (expression) == SCOPE_REF)
27018 return false;
27019
27020 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27021 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27022 || TREE_CODE (expression) == CO_YIELD_EXPR)
27023 return true;
27024
27025 if (BASELINK_P (expression))
27026 {
27027 if (BASELINK_OPTYPE (expression)
27028 && dependent_type_p (BASELINK_OPTYPE (expression)))
27029 return true;
27030 expression = BASELINK_FUNCTIONS (expression);
27031 }
27032
27033 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27034 {
27035 if (any_dependent_template_arguments_p
27036 (TREE_OPERAND (expression, 1)))
27037 return true;
27038 expression = TREE_OPERAND (expression, 0);
27039 if (identifier_p (expression))
27040 return true;
27041 }
27042
27043 gcc_assert (OVL_P (expression));
27044
27045 for (lkp_iterator iter (expression); iter; ++iter)
27046 if (type_dependent_expression_p (*iter))
27047 return true;
27048
27049 return false;
27050 }
27051
27052 /* The type of a non-type template parm declared with a placeholder type
27053 depends on the corresponding template argument, even though
27054 placeholders are not normally considered dependent. */
27055 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27056 && is_auto (TREE_TYPE (expression)))
27057 return true;
27058
27059 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27060
27061 /* Dependent type attributes might not have made it from the decl to
27062 the type yet. */
27063 if (DECL_P (expression)
27064 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27065 return true;
27066
27067 return (dependent_type_p (TREE_TYPE (expression)));
27068 }
27069
27070 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27071 type-dependent if the expression refers to a member of the current
27072 instantiation and the type of the referenced member is dependent, or the
27073 class member access expression refers to a member of an unknown
27074 specialization.
27075
27076 This function returns true if the OBJECT in such a class member access
27077 expression is of an unknown specialization. */
27078
27079 bool
27080 type_dependent_object_expression_p (tree object)
27081 {
27082 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27083 dependent. */
27084 if (TREE_CODE (object) == IDENTIFIER_NODE)
27085 return true;
27086 tree scope = TREE_TYPE (object);
27087 return (!scope || dependent_scope_p (scope));
27088 }
27089
27090 /* walk_tree callback function for instantiation_dependent_expression_p,
27091 below. Returns non-zero if a dependent subexpression is found. */
27092
27093 static tree
27094 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27095 void * /*data*/)
27096 {
27097 if (TYPE_P (*tp))
27098 {
27099 /* We don't have to worry about decltype currently because decltype
27100 of an instantiation-dependent expr is a dependent type. This
27101 might change depending on the resolution of DR 1172. */
27102 *walk_subtrees = false;
27103 return NULL_TREE;
27104 }
27105 enum tree_code code = TREE_CODE (*tp);
27106 switch (code)
27107 {
27108 /* Don't treat an argument list as dependent just because it has no
27109 TREE_TYPE. */
27110 case TREE_LIST:
27111 case TREE_VEC:
27112 case NONTYPE_ARGUMENT_PACK:
27113 return NULL_TREE;
27114
27115 case TEMPLATE_PARM_INDEX:
27116 if (dependent_type_p (TREE_TYPE (*tp)))
27117 return *tp;
27118 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27119 return *tp;
27120 /* We'll check value-dependence separately. */
27121 return NULL_TREE;
27122
27123 /* Handle expressions with type operands. */
27124 case SIZEOF_EXPR:
27125 case ALIGNOF_EXPR:
27126 case TYPEID_EXPR:
27127 case AT_ENCODE_EXPR:
27128 {
27129 tree op = TREE_OPERAND (*tp, 0);
27130 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27131 op = TREE_TYPE (op);
27132 if (TYPE_P (op))
27133 {
27134 if (dependent_type_p (op))
27135 return *tp;
27136 else
27137 {
27138 *walk_subtrees = false;
27139 return NULL_TREE;
27140 }
27141 }
27142 break;
27143 }
27144
27145 case COMPONENT_REF:
27146 if (identifier_p (TREE_OPERAND (*tp, 1)))
27147 /* In a template, finish_class_member_access_expr creates a
27148 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27149 type-dependent, so that we can check access control at
27150 instantiation time (PR 42277). See also Core issue 1273. */
27151 return *tp;
27152 break;
27153
27154 case SCOPE_REF:
27155 if (instantiation_dependent_scope_ref_p (*tp))
27156 return *tp;
27157 else
27158 break;
27159
27160 /* Treat statement-expressions as dependent. */
27161 case BIND_EXPR:
27162 return *tp;
27163
27164 /* Treat requires-expressions as dependent. */
27165 case REQUIRES_EXPR:
27166 return *tp;
27167
27168 case CALL_EXPR:
27169 /* Treat concept checks as dependent. */
27170 if (concept_check_p (*tp))
27171 return *tp;
27172 break;
27173
27174 case TEMPLATE_ID_EXPR:
27175 /* Treat concept checks as dependent. */
27176 if (concept_check_p (*tp))
27177 return *tp;
27178 break;
27179
27180 case CONSTRUCTOR:
27181 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27182 return *tp;
27183 break;
27184
27185 default:
27186 break;
27187 }
27188
27189 if (type_dependent_expression_p (*tp))
27190 return *tp;
27191 else
27192 return NULL_TREE;
27193 }
27194
27195 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27196 sense defined by the ABI:
27197
27198 "An expression is instantiation-dependent if it is type-dependent
27199 or value-dependent, or it has a subexpression that is type-dependent
27200 or value-dependent."
27201
27202 Except don't actually check value-dependence for unevaluated expressions,
27203 because in sizeof(i) we don't care about the value of i. Checking
27204 type-dependence will in turn check value-dependence of array bounds/template
27205 arguments as needed. */
27206
27207 bool
27208 instantiation_dependent_uneval_expression_p (tree expression)
27209 {
27210 tree result;
27211
27212 if (!processing_template_decl)
27213 return false;
27214
27215 if (expression == error_mark_node)
27216 return false;
27217
27218 result = cp_walk_tree_without_duplicates (&expression,
27219 instantiation_dependent_r, NULL);
27220 return result != NULL_TREE;
27221 }
27222
27223 /* As above, but also check value-dependence of the expression as a whole. */
27224
27225 bool
27226 instantiation_dependent_expression_p (tree expression)
27227 {
27228 return (instantiation_dependent_uneval_expression_p (expression)
27229 || value_dependent_expression_p (expression));
27230 }
27231
27232 /* Like type_dependent_expression_p, but it also works while not processing
27233 a template definition, i.e. during substitution or mangling. */
27234
27235 bool
27236 type_dependent_expression_p_push (tree expr)
27237 {
27238 bool b;
27239 ++processing_template_decl;
27240 b = type_dependent_expression_p (expr);
27241 --processing_template_decl;
27242 return b;
27243 }
27244
27245 /* Returns TRUE if ARGS contains a type-dependent expression. */
27246
27247 bool
27248 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27249 {
27250 unsigned int i;
27251 tree arg;
27252
27253 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27254 {
27255 if (type_dependent_expression_p (arg))
27256 return true;
27257 }
27258 return false;
27259 }
27260
27261 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27262 expressions) contains any type-dependent expressions. */
27263
27264 bool
27265 any_type_dependent_elements_p (const_tree list)
27266 {
27267 for (; list; list = TREE_CHAIN (list))
27268 if (type_dependent_expression_p (TREE_VALUE (list)))
27269 return true;
27270
27271 return false;
27272 }
27273
27274 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27275 expressions) contains any value-dependent expressions. */
27276
27277 bool
27278 any_value_dependent_elements_p (const_tree list)
27279 {
27280 for (; list; list = TREE_CHAIN (list))
27281 if (value_dependent_expression_p (TREE_VALUE (list)))
27282 return true;
27283
27284 return false;
27285 }
27286
27287 /* Returns TRUE if the ARG (a template argument) is dependent. */
27288
27289 bool
27290 dependent_template_arg_p (tree arg)
27291 {
27292 if (!processing_template_decl)
27293 return false;
27294
27295 /* Assume a template argument that was wrongly written by the user
27296 is dependent. This is consistent with what
27297 any_dependent_template_arguments_p [that calls this function]
27298 does. */
27299 if (!arg || arg == error_mark_node)
27300 return true;
27301
27302 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27303 arg = argument_pack_select_arg (arg);
27304
27305 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27306 return true;
27307 if (TREE_CODE (arg) == TEMPLATE_DECL)
27308 {
27309 if (DECL_TEMPLATE_PARM_P (arg))
27310 return true;
27311 /* A member template of a dependent class is not necessarily
27312 type-dependent, but it is a dependent template argument because it
27313 will be a member of an unknown specialization to that template. */
27314 tree scope = CP_DECL_CONTEXT (arg);
27315 return TYPE_P (scope) && dependent_type_p (scope);
27316 }
27317 else if (ARGUMENT_PACK_P (arg))
27318 {
27319 tree args = ARGUMENT_PACK_ARGS (arg);
27320 int i, len = TREE_VEC_LENGTH (args);
27321 for (i = 0; i < len; ++i)
27322 {
27323 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27324 return true;
27325 }
27326
27327 return false;
27328 }
27329 else if (TYPE_P (arg))
27330 return dependent_type_p (arg);
27331 else
27332 return value_dependent_expression_p (arg);
27333 }
27334
27335 /* Returns true if ARGS (a collection of template arguments) contains
27336 any types that require structural equality testing. */
27337
27338 bool
27339 any_template_arguments_need_structural_equality_p (tree args)
27340 {
27341 int i;
27342 int j;
27343
27344 if (!args)
27345 return false;
27346 if (args == error_mark_node)
27347 return true;
27348
27349 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27350 {
27351 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27352 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27353 {
27354 tree arg = TREE_VEC_ELT (level, j);
27355 tree packed_args = NULL_TREE;
27356 int k, len = 1;
27357
27358 if (ARGUMENT_PACK_P (arg))
27359 {
27360 /* Look inside the argument pack. */
27361 packed_args = ARGUMENT_PACK_ARGS (arg);
27362 len = TREE_VEC_LENGTH (packed_args);
27363 }
27364
27365 for (k = 0; k < len; ++k)
27366 {
27367 if (packed_args)
27368 arg = TREE_VEC_ELT (packed_args, k);
27369
27370 if (error_operand_p (arg))
27371 return true;
27372 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27373 continue;
27374 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27375 return true;
27376 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27377 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27378 return true;
27379 }
27380 }
27381 }
27382
27383 return false;
27384 }
27385
27386 /* Returns true if ARGS (a collection of template arguments) contains
27387 any dependent arguments. */
27388
27389 bool
27390 any_dependent_template_arguments_p (const_tree args)
27391 {
27392 int i;
27393 int j;
27394
27395 if (!args)
27396 return false;
27397 if (args == error_mark_node)
27398 return true;
27399
27400 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27401 {
27402 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27403 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27404 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27405 return true;
27406 }
27407
27408 return false;
27409 }
27410
27411 /* Returns true if ARGS contains any errors. */
27412
27413 bool
27414 any_erroneous_template_args_p (const_tree args)
27415 {
27416 int i;
27417 int j;
27418
27419 if (args == error_mark_node)
27420 return true;
27421
27422 if (args && TREE_CODE (args) != TREE_VEC)
27423 {
27424 if (tree ti = get_template_info (args))
27425 args = TI_ARGS (ti);
27426 else
27427 args = NULL_TREE;
27428 }
27429
27430 if (!args)
27431 return false;
27432
27433 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27434 {
27435 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27436 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27437 if (error_operand_p (TREE_VEC_ELT (level, j)))
27438 return true;
27439 }
27440
27441 return false;
27442 }
27443
27444 /* Returns TRUE if the template TMPL is type-dependent. */
27445
27446 bool
27447 dependent_template_p (tree tmpl)
27448 {
27449 if (TREE_CODE (tmpl) == OVERLOAD)
27450 {
27451 for (lkp_iterator iter (tmpl); iter; ++iter)
27452 if (dependent_template_p (*iter))
27453 return true;
27454 return false;
27455 }
27456
27457 /* Template template parameters are dependent. */
27458 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27459 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27460 return true;
27461 /* So are names that have not been looked up. */
27462 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27463 return true;
27464 return false;
27465 }
27466
27467 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27468
27469 bool
27470 dependent_template_id_p (tree tmpl, tree args)
27471 {
27472 return (dependent_template_p (tmpl)
27473 || any_dependent_template_arguments_p (args));
27474 }
27475
27476 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27477 are dependent. */
27478
27479 bool
27480 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27481 {
27482 int i;
27483
27484 if (!processing_template_decl)
27485 return false;
27486
27487 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27488 {
27489 tree decl = TREE_VEC_ELT (declv, i);
27490 tree init = TREE_VEC_ELT (initv, i);
27491 tree cond = TREE_VEC_ELT (condv, i);
27492 tree incr = TREE_VEC_ELT (incrv, i);
27493
27494 if (type_dependent_expression_p (decl)
27495 || TREE_CODE (decl) == SCOPE_REF)
27496 return true;
27497
27498 if (init && type_dependent_expression_p (init))
27499 return true;
27500
27501 if (cond == global_namespace)
27502 return true;
27503
27504 if (type_dependent_expression_p (cond))
27505 return true;
27506
27507 if (COMPARISON_CLASS_P (cond)
27508 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27509 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27510 return true;
27511
27512 if (TREE_CODE (incr) == MODOP_EXPR)
27513 {
27514 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27515 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27516 return true;
27517 }
27518 else if (type_dependent_expression_p (incr))
27519 return true;
27520 else if (TREE_CODE (incr) == MODIFY_EXPR)
27521 {
27522 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27523 return true;
27524 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27525 {
27526 tree t = TREE_OPERAND (incr, 1);
27527 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27528 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27529 return true;
27530
27531 /* If this loop has a class iterator with != comparison
27532 with increment other than i++/++i/i--/--i, make sure the
27533 increment is constant. */
27534 if (CLASS_TYPE_P (TREE_TYPE (decl))
27535 && TREE_CODE (cond) == NE_EXPR)
27536 {
27537 if (TREE_OPERAND (t, 0) == decl)
27538 t = TREE_OPERAND (t, 1);
27539 else
27540 t = TREE_OPERAND (t, 0);
27541 if (TREE_CODE (t) != INTEGER_CST)
27542 return true;
27543 }
27544 }
27545 }
27546 }
27547
27548 return false;
27549 }
27550
27551 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27552 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27553 no such TYPE can be found. Note that this function peers inside
27554 uninstantiated templates and therefore should be used only in
27555 extremely limited situations. ONLY_CURRENT_P restricts this
27556 peering to the currently open classes hierarchy (which is required
27557 when comparing types). */
27558
27559 tree
27560 resolve_typename_type (tree type, bool only_current_p)
27561 {
27562 tree scope;
27563 tree name;
27564 tree decl;
27565 int quals;
27566 tree pushed_scope;
27567 tree result;
27568
27569 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27570
27571 scope = TYPE_CONTEXT (type);
27572 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27573 gcc_checking_assert (uses_template_parms (scope));
27574
27575 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27576 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27577 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27578 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27579 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27580 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27581 the TYPENAME_TYPE instead, we avoid messing up with a possible
27582 typedef variant case. */
27583 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27584
27585 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27586 it first before we can figure out what NAME refers to. */
27587 if (TREE_CODE (scope) == TYPENAME_TYPE)
27588 {
27589 if (TYPENAME_IS_RESOLVING_P (scope))
27590 /* Given a class template A with a dependent base with nested type C,
27591 typedef typename A::C::C C will land us here, as trying to resolve
27592 the initial A::C leads to the local C typedef, which leads back to
27593 A::C::C. So we break the recursion now. */
27594 return type;
27595 else
27596 scope = resolve_typename_type (scope, only_current_p);
27597 }
27598 /* If we don't know what SCOPE refers to, then we cannot resolve the
27599 TYPENAME_TYPE. */
27600 if (!CLASS_TYPE_P (scope))
27601 return type;
27602 /* If this is a typedef, we don't want to look inside (c++/11987). */
27603 if (typedef_variant_p (type))
27604 return type;
27605 /* If SCOPE isn't the template itself, it will not have a valid
27606 TYPE_FIELDS list. */
27607 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27608 /* scope is either the template itself or a compatible instantiation
27609 like X<T>, so look up the name in the original template. */
27610 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27611 /* If scope has no fields, it can't be a current instantiation. Check this
27612 before currently_open_class to avoid infinite recursion (71515). */
27613 if (!TYPE_FIELDS (scope))
27614 return type;
27615 /* If the SCOPE is not the current instantiation, there's no reason
27616 to look inside it. */
27617 if (only_current_p && !currently_open_class (scope))
27618 return type;
27619 /* Enter the SCOPE so that name lookup will be resolved as if we
27620 were in the class definition. In particular, SCOPE will no
27621 longer be considered a dependent type. */
27622 pushed_scope = push_scope (scope);
27623 /* Look up the declaration. */
27624 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27625 tf_warning_or_error);
27626
27627 result = NULL_TREE;
27628
27629 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27630 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27631 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27632 if (!decl)
27633 /*nop*/;
27634 else if (identifier_p (fullname)
27635 && TREE_CODE (decl) == TYPE_DECL)
27636 {
27637 result = TREE_TYPE (decl);
27638 if (result == error_mark_node)
27639 result = NULL_TREE;
27640 }
27641 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27642 && DECL_CLASS_TEMPLATE_P (decl))
27643 {
27644 /* Obtain the template and the arguments. */
27645 tree tmpl = TREE_OPERAND (fullname, 0);
27646 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27647 {
27648 /* We get here with a plain identifier because a previous tentative
27649 parse of the nested-name-specifier as part of a ptr-operator saw
27650 ::template X<A>. The use of ::template is necessary in a
27651 ptr-operator, but wrong in a declarator-id.
27652
27653 [temp.names]: In a qualified-id of a declarator-id, the keyword
27654 template shall not appear at the top level. */
27655 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27656 "keyword %<template%> not allowed in declarator-id");
27657 tmpl = decl;
27658 }
27659 tree args = TREE_OPERAND (fullname, 1);
27660 /* Instantiate the template. */
27661 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27662 /*entering_scope=*/true,
27663 tf_error | tf_user);
27664 if (result == error_mark_node)
27665 result = NULL_TREE;
27666 }
27667
27668 /* Leave the SCOPE. */
27669 if (pushed_scope)
27670 pop_scope (pushed_scope);
27671
27672 /* If we failed to resolve it, return the original typename. */
27673 if (!result)
27674 return type;
27675
27676 /* If lookup found a typename type, resolve that too. */
27677 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27678 {
27679 /* Ill-formed programs can cause infinite recursion here, so we
27680 must catch that. */
27681 TYPENAME_IS_RESOLVING_P (result) = 1;
27682 result = resolve_typename_type (result, only_current_p);
27683 TYPENAME_IS_RESOLVING_P (result) = 0;
27684 }
27685
27686 /* Qualify the resulting type. */
27687 quals = cp_type_quals (type);
27688 if (quals)
27689 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27690
27691 return result;
27692 }
27693
27694 /* EXPR is an expression which is not type-dependent. Return a proxy
27695 for EXPR that can be used to compute the types of larger
27696 expressions containing EXPR. */
27697
27698 tree
27699 build_non_dependent_expr (tree expr)
27700 {
27701 tree orig_expr = expr;
27702 tree inner_expr;
27703
27704 /* When checking, try to get a constant value for all non-dependent
27705 expressions in order to expose bugs in *_dependent_expression_p
27706 and constexpr. This can affect code generation, see PR70704, so
27707 only do this for -fchecking=2. */
27708 if (flag_checking > 1
27709 && cxx_dialect >= cxx11
27710 /* Don't do this during nsdmi parsing as it can lead to
27711 unexpected recursive instantiations. */
27712 && !parsing_nsdmi ()
27713 /* Don't do this during concept processing either and for
27714 the same reason. */
27715 && !processing_constraint_expression_p ())
27716 fold_non_dependent_expr (expr, tf_none);
27717
27718 STRIP_ANY_LOCATION_WRAPPER (expr);
27719
27720 /* Preserve OVERLOADs; the functions must be available to resolve
27721 types. */
27722 inner_expr = expr;
27723 if (TREE_CODE (inner_expr) == STMT_EXPR)
27724 inner_expr = stmt_expr_value_expr (inner_expr);
27725 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27726 inner_expr = TREE_OPERAND (inner_expr, 0);
27727 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27728 inner_expr = TREE_OPERAND (inner_expr, 1);
27729 if (is_overloaded_fn (inner_expr)
27730 || TREE_CODE (inner_expr) == OFFSET_REF)
27731 return orig_expr;
27732 /* There is no need to return a proxy for a variable or enumerator. */
27733 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27734 return orig_expr;
27735 /* Preserve string constants; conversions from string constants to
27736 "char *" are allowed, even though normally a "const char *"
27737 cannot be used to initialize a "char *". */
27738 if (TREE_CODE (expr) == STRING_CST)
27739 return orig_expr;
27740 /* Preserve void and arithmetic constants, as an optimization -- there is no
27741 reason to create a new node. */
27742 if (TREE_CODE (expr) == VOID_CST
27743 || TREE_CODE (expr) == INTEGER_CST
27744 || TREE_CODE (expr) == REAL_CST)
27745 return orig_expr;
27746 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27747 There is at least one place where we want to know that a
27748 particular expression is a throw-expression: when checking a ?:
27749 expression, there are special rules if the second or third
27750 argument is a throw-expression. */
27751 if (TREE_CODE (expr) == THROW_EXPR)
27752 return orig_expr;
27753
27754 /* Don't wrap an initializer list, we need to be able to look inside. */
27755 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27756 return orig_expr;
27757
27758 /* Don't wrap a dummy object, we need to be able to test for it. */
27759 if (is_dummy_object (expr))
27760 return orig_expr;
27761
27762 if (TREE_CODE (expr) == COND_EXPR)
27763 return build3 (COND_EXPR,
27764 TREE_TYPE (expr),
27765 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27766 (TREE_OPERAND (expr, 1)
27767 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27768 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27769 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27770 if (TREE_CODE (expr) == COMPOUND_EXPR
27771 && !COMPOUND_EXPR_OVERLOADED (expr))
27772 return build2 (COMPOUND_EXPR,
27773 TREE_TYPE (expr),
27774 TREE_OPERAND (expr, 0),
27775 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27776
27777 /* If the type is unknown, it can't really be non-dependent */
27778 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27779
27780 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27781 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27782 TREE_TYPE (expr), expr);
27783 }
27784
27785 /* ARGS is a vector of expressions as arguments to a function call.
27786 Replace the arguments with equivalent non-dependent expressions.
27787 This modifies ARGS in place. */
27788
27789 void
27790 make_args_non_dependent (vec<tree, va_gc> *args)
27791 {
27792 unsigned int ix;
27793 tree arg;
27794
27795 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27796 {
27797 tree newarg = build_non_dependent_expr (arg);
27798 if (newarg != arg)
27799 (*args)[ix] = newarg;
27800 }
27801 }
27802
27803 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27804 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27805 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27806
27807 static tree
27808 make_auto_1 (tree name, bool set_canonical)
27809 {
27810 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27811 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27812 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27813 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27814 (0, processing_template_decl + 1, processing_template_decl + 1,
27815 TYPE_NAME (au), NULL_TREE);
27816 if (set_canonical)
27817 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27818 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27819 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27820 if (name == decltype_auto_identifier)
27821 AUTO_IS_DECLTYPE (au) = true;
27822
27823 return au;
27824 }
27825
27826 tree
27827 make_decltype_auto (void)
27828 {
27829 return make_auto_1 (decltype_auto_identifier, true);
27830 }
27831
27832 tree
27833 make_auto (void)
27834 {
27835 return make_auto_1 (auto_identifier, true);
27836 }
27837
27838 /* Return a C++17 deduction placeholder for class template TMPL. */
27839
27840 tree
27841 make_template_placeholder (tree tmpl)
27842 {
27843 tree t = make_auto_1 (auto_identifier, false);
27844 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27845 /* Our canonical type depends on the placeholder. */
27846 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27847 return t;
27848 }
27849
27850 /* True iff T is a C++17 class template deduction placeholder. */
27851
27852 bool
27853 template_placeholder_p (tree t)
27854 {
27855 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27856 }
27857
27858 /* Make a "constrained auto" type-specifier. This is an auto or
27859 decltype(auto) type with constraints that must be associated after
27860 deduction. The constraint is formed from the given concept CON
27861 and its optional sequence of template arguments ARGS.
27862
27863 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27864
27865 static tree
27866 make_constrained_placeholder_type (tree type, tree con, tree args)
27867 {
27868 /* Build the constraint. */
27869 tree tmpl = DECL_TI_TEMPLATE (con);
27870 tree expr = tmpl;
27871 if (TREE_CODE (con) == FUNCTION_DECL)
27872 expr = ovl_make (tmpl);
27873 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27874
27875 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27876
27877 /* Our canonical type depends on the constraint. */
27878 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27879
27880 /* Attach the constraint to the type declaration. */
27881 return TYPE_NAME (type);
27882 }
27883
27884 /* Make a "constrained auto" type-specifier. */
27885
27886 tree
27887 make_constrained_auto (tree con, tree args)
27888 {
27889 tree type = make_auto_1 (auto_identifier, false);
27890 return make_constrained_placeholder_type (type, con, args);
27891 }
27892
27893 /* Make a "constrained decltype(auto)" type-specifier. */
27894
27895 tree
27896 make_constrained_decltype_auto (tree con, tree args)
27897 {
27898 tree type = make_auto_1 (decltype_auto_identifier, false);
27899 return make_constrained_placeholder_type (type, con, args);
27900 }
27901
27902 /* Build and return a concept definition. Like other templates, the
27903 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27904 the TEMPLATE_DECL. */
27905
27906 tree
27907 finish_concept_definition (cp_expr id, tree init)
27908 {
27909 gcc_assert (identifier_p (id));
27910 gcc_assert (processing_template_decl);
27911
27912 location_t loc = id.get_location();
27913
27914 /* A concept-definition shall not have associated constraints. */
27915 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27916 {
27917 error_at (loc, "a concept cannot be constrained");
27918 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27919 }
27920
27921 /* A concept-definition shall appear in namespace scope. Templates
27922 aren't allowed in block scope, so we only need to check for class
27923 scope. */
27924 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27925 {
27926 error_at (loc, "concept %qE not in namespace scope", *id);
27927 return error_mark_node;
27928 }
27929
27930 /* Initially build the concept declaration; its type is bool. */
27931 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27932 DECL_CONTEXT (decl) = current_scope ();
27933 DECL_INITIAL (decl) = init;
27934
27935 /* Push the enclosing template. */
27936 return push_template_decl (decl);
27937 }
27938
27939 /* Given type ARG, return std::initializer_list<ARG>. */
27940
27941 static tree
27942 listify (tree arg)
27943 {
27944 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27945
27946 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27947 {
27948 gcc_rich_location richloc (input_location);
27949 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27950 error_at (&richloc,
27951 "deducing from brace-enclosed initializer list"
27952 " requires %<#include <initializer_list>%>");
27953
27954 return error_mark_node;
27955 }
27956 tree argvec = make_tree_vec (1);
27957 TREE_VEC_ELT (argvec, 0) = arg;
27958
27959 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27960 NULL_TREE, 0, tf_warning_or_error);
27961 }
27962
27963 /* Replace auto in TYPE with std::initializer_list<auto>. */
27964
27965 static tree
27966 listify_autos (tree type, tree auto_node)
27967 {
27968 tree init_auto = listify (strip_top_quals (auto_node));
27969 tree argvec = make_tree_vec (1);
27970 TREE_VEC_ELT (argvec, 0) = init_auto;
27971 if (processing_template_decl)
27972 argvec = add_to_template_args (current_template_args (), argvec);
27973 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27974 }
27975
27976 /* Hash traits for hashing possibly constrained 'auto'
27977 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27978
27979 struct auto_hash : default_hash_traits<tree>
27980 {
27981 static inline hashval_t hash (tree);
27982 static inline bool equal (tree, tree);
27983 };
27984
27985 /* Hash the 'auto' T. */
27986
27987 inline hashval_t
27988 auto_hash::hash (tree t)
27989 {
27990 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27991 /* Matching constrained-type-specifiers denote the same template
27992 parameter, so hash the constraint. */
27993 return hash_placeholder_constraint (c);
27994 else
27995 /* But unconstrained autos are all separate, so just hash the pointer. */
27996 return iterative_hash_object (t, 0);
27997 }
27998
27999 /* Compare two 'auto's. */
28000
28001 inline bool
28002 auto_hash::equal (tree t1, tree t2)
28003 {
28004 if (t1 == t2)
28005 return true;
28006
28007 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28008 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28009
28010 /* Two unconstrained autos are distinct. */
28011 if (!c1 || !c2)
28012 return false;
28013
28014 return equivalent_placeholder_constraints (c1, c2);
28015 }
28016
28017 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28018 constrained) auto, add it to the vector. */
28019
28020 static int
28021 extract_autos_r (tree t, void *data)
28022 {
28023 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28024 if (is_auto (t))
28025 {
28026 /* All the autos were built with index 0; fix that up now. */
28027 tree *p = hash.find_slot (t, INSERT);
28028 unsigned idx;
28029 if (*p)
28030 /* If this is a repeated constrained-type-specifier, use the index we
28031 chose before. */
28032 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28033 else
28034 {
28035 /* Otherwise this is new, so use the current count. */
28036 *p = t;
28037 idx = hash.elements () - 1;
28038 }
28039 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28040 }
28041
28042 /* Always keep walking. */
28043 return 0;
28044 }
28045
28046 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28047 says they can appear anywhere in the type. */
28048
28049 static tree
28050 extract_autos (tree type)
28051 {
28052 hash_set<tree> visited;
28053 hash_table<auto_hash> hash (2);
28054
28055 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28056
28057 tree tree_vec = make_tree_vec (hash.elements());
28058 for (hash_table<auto_hash>::iterator iter = hash.begin();
28059 iter != hash.end(); ++iter)
28060 {
28061 tree elt = *iter;
28062 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28063 TREE_VEC_ELT (tree_vec, i)
28064 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28065 }
28066
28067 return tree_vec;
28068 }
28069
28070 /* The stem for deduction guide names. */
28071 const char *const dguide_base = "__dguide_";
28072
28073 /* Return the name for a deduction guide for class template TMPL. */
28074
28075 tree
28076 dguide_name (tree tmpl)
28077 {
28078 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28079 tree tname = TYPE_IDENTIFIER (type);
28080 char *buf = (char *) alloca (1 + strlen (dguide_base)
28081 + IDENTIFIER_LENGTH (tname));
28082 memcpy (buf, dguide_base, strlen (dguide_base));
28083 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28084 IDENTIFIER_LENGTH (tname) + 1);
28085 tree dname = get_identifier (buf);
28086 TREE_TYPE (dname) = type;
28087 return dname;
28088 }
28089
28090 /* True if NAME is the name of a deduction guide. */
28091
28092 bool
28093 dguide_name_p (tree name)
28094 {
28095 return (TREE_CODE (name) == IDENTIFIER_NODE
28096 && TREE_TYPE (name)
28097 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
28098 strlen (dguide_base)));
28099 }
28100
28101 /* True if FN is a deduction guide. */
28102
28103 bool
28104 deduction_guide_p (const_tree fn)
28105 {
28106 if (DECL_P (fn))
28107 if (tree name = DECL_NAME (fn))
28108 return dguide_name_p (name);
28109 return false;
28110 }
28111
28112 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28113
28114 bool
28115 copy_guide_p (const_tree fn)
28116 {
28117 gcc_assert (deduction_guide_p (fn));
28118 if (!DECL_ARTIFICIAL (fn))
28119 return false;
28120 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28121 return (TREE_CHAIN (parms) == void_list_node
28122 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28123 }
28124
28125 /* True if FN is a guide generated from a constructor template. */
28126
28127 bool
28128 template_guide_p (const_tree fn)
28129 {
28130 gcc_assert (deduction_guide_p (fn));
28131 if (!DECL_ARTIFICIAL (fn))
28132 return false;
28133 tree tmpl = DECL_TI_TEMPLATE (fn);
28134 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28135 return PRIMARY_TEMPLATE_P (org);
28136 return false;
28137 }
28138
28139 /* True if FN is an aggregate initialization guide or the copy deduction
28140 guide. */
28141
28142 bool
28143 builtin_guide_p (const_tree fn)
28144 {
28145 if (!deduction_guide_p (fn))
28146 return false;
28147 if (!DECL_ARTIFICIAL (fn))
28148 /* Explicitly declared. */
28149 return false;
28150 if (DECL_ABSTRACT_ORIGIN (fn))
28151 /* Derived from a constructor. */
28152 return false;
28153 return true;
28154 }
28155
28156 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28157 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28158 template parameter types. Note that the handling of template template
28159 parameters relies on current_template_parms being set appropriately for the
28160 new template. */
28161
28162 static tree
28163 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28164 tree tsubst_args, tsubst_flags_t complain)
28165 {
28166 if (olddecl == error_mark_node)
28167 return error_mark_node;
28168
28169 tree oldidx = get_template_parm_index (olddecl);
28170
28171 tree newtype;
28172 if (TREE_CODE (olddecl) == TYPE_DECL
28173 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28174 {
28175 tree oldtype = TREE_TYPE (olddecl);
28176 newtype = cxx_make_type (TREE_CODE (oldtype));
28177 TYPE_MAIN_VARIANT (newtype) = newtype;
28178 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28179 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28180 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28181 }
28182 else
28183 {
28184 newtype = TREE_TYPE (olddecl);
28185 if (type_uses_auto (newtype))
28186 {
28187 // Substitute once to fix references to other template parameters.
28188 newtype = tsubst (newtype, tsubst_args,
28189 complain|tf_partial, NULL_TREE);
28190 // Now substitute again to reduce the level of the auto.
28191 newtype = tsubst (newtype, current_template_args (),
28192 complain, NULL_TREE);
28193 }
28194 else
28195 newtype = tsubst (newtype, tsubst_args,
28196 complain, NULL_TREE);
28197 }
28198
28199 tree newdecl
28200 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28201 DECL_NAME (olddecl), newtype);
28202 SET_DECL_TEMPLATE_PARM_P (newdecl);
28203
28204 tree newidx;
28205 if (TREE_CODE (olddecl) == TYPE_DECL
28206 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28207 {
28208 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28209 = build_template_parm_index (index, level, level,
28210 newdecl, newtype);
28211 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28212 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28213 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28214 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28215 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28216 else
28217 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28218
28219 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28220 {
28221 DECL_TEMPLATE_RESULT (newdecl)
28222 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28223 DECL_NAME (olddecl), newtype);
28224 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28225 // First create a copy (ttargs) of tsubst_args with an
28226 // additional level for the template template parameter's own
28227 // template parameters (ttparms).
28228 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28229 (DECL_TEMPLATE_PARMS (olddecl)));
28230 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28231 tree ttargs = make_tree_vec (depth + 1);
28232 for (int i = 0; i < depth; ++i)
28233 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28234 TREE_VEC_ELT (ttargs, depth)
28235 = template_parms_level_to_args (ttparms);
28236 // Substitute ttargs into ttparms to fix references to
28237 // other template parameters.
28238 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28239 complain|tf_partial);
28240 // Now substitute again with args based on tparms, to reduce
28241 // the level of the ttparms.
28242 ttargs = current_template_args ();
28243 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28244 complain);
28245 // Finally, tack the adjusted parms onto tparms.
28246 ttparms = tree_cons (size_int (depth), ttparms,
28247 current_template_parms);
28248 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28249 }
28250 }
28251 else
28252 {
28253 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28254 tree newconst
28255 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28256 TREE_CODE (oldconst),
28257 DECL_NAME (oldconst), newtype);
28258 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28259 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28260 SET_DECL_TEMPLATE_PARM_P (newconst);
28261 newidx = build_template_parm_index (index, level, level,
28262 newconst, newtype);
28263 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28264 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28265 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28266 }
28267
28268 return newdecl;
28269 }
28270
28271 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28272 template parameter. */
28273
28274 static tree
28275 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28276 tree targs, unsigned targs_index, tsubst_flags_t complain)
28277 {
28278 tree olddecl = TREE_VALUE (oldelt);
28279 tree newdecl = rewrite_template_parm (olddecl, index, level,
28280 targs, complain);
28281 if (newdecl == error_mark_node)
28282 return error_mark_node;
28283 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28284 targs, complain, NULL_TREE);
28285 tree list = build_tree_list (newdef, newdecl);
28286 TEMPLATE_PARM_CONSTRAINTS (list)
28287 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28288 targs, complain, NULL_TREE);
28289 int depth = TMPL_ARGS_DEPTH (targs);
28290 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28291 return list;
28292 }
28293
28294 /* Returns a C++17 class deduction guide template based on the constructor
28295 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28296 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28297 aggregate initialization guide. */
28298
28299 static tree
28300 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28301 {
28302 tree tparms, targs, fparms, fargs, ci;
28303 bool memtmpl = false;
28304 bool explicit_p;
28305 location_t loc;
28306 tree fn_tmpl = NULL_TREE;
28307
28308 if (outer_args)
28309 {
28310 ++processing_template_decl;
28311 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28312 --processing_template_decl;
28313 }
28314
28315 if (!DECL_DECLARES_FUNCTION_P (ctor))
28316 {
28317 if (TYPE_P (ctor))
28318 {
28319 bool copy_p = TYPE_REF_P (ctor);
28320 if (copy_p)
28321 fparms = tree_cons (NULL_TREE, type, void_list_node);
28322 else
28323 fparms = void_list_node;
28324 }
28325 else if (TREE_CODE (ctor) == TREE_LIST)
28326 fparms = ctor;
28327 else
28328 gcc_unreachable ();
28329
28330 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28331 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28332 targs = CLASSTYPE_TI_ARGS (type);
28333 ci = NULL_TREE;
28334 fargs = NULL_TREE;
28335 loc = DECL_SOURCE_LOCATION (ctmpl);
28336 explicit_p = false;
28337 }
28338 else
28339 {
28340 ++processing_template_decl;
28341 bool ok = true;
28342
28343 fn_tmpl
28344 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28345 : DECL_TI_TEMPLATE (ctor));
28346 if (outer_args)
28347 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28348 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28349
28350 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28351 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28352 fully specialized args for the enclosing class. Strip those off, as
28353 the deduction guide won't have those template parameters. */
28354 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28355 TMPL_PARMS_DEPTH (tparms));
28356 /* Discard the 'this' parameter. */
28357 fparms = FUNCTION_ARG_CHAIN (ctor);
28358 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28359 ci = get_constraints (ctor);
28360 loc = DECL_SOURCE_LOCATION (ctor);
28361 explicit_p = DECL_NONCONVERTING_P (ctor);
28362
28363 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28364 {
28365 memtmpl = true;
28366
28367 /* For a member template constructor, we need to flatten the two
28368 template parameter lists into one, and then adjust the function
28369 signature accordingly. This gets...complicated. */
28370 tree save_parms = current_template_parms;
28371
28372 /* For a member template we should have two levels of parms/args, one
28373 for the class and one for the constructor. We stripped
28374 specialized args for further enclosing classes above. */
28375 const int depth = 2;
28376 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28377
28378 /* Template args for translating references to the two-level template
28379 parameters into references to the one-level template parameters we
28380 are creating. */
28381 tree tsubst_args = copy_node (targs);
28382 TMPL_ARGS_LEVEL (tsubst_args, depth)
28383 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28384
28385 /* Template parms for the constructor template. */
28386 tree ftparms = TREE_VALUE (tparms);
28387 unsigned flen = TREE_VEC_LENGTH (ftparms);
28388 /* Template parms for the class template. */
28389 tparms = TREE_CHAIN (tparms);
28390 tree ctparms = TREE_VALUE (tparms);
28391 unsigned clen = TREE_VEC_LENGTH (ctparms);
28392 /* Template parms for the deduction guide start as a copy of the
28393 template parms for the class. We set current_template_parms for
28394 lookup_template_class_1. */
28395 current_template_parms = tparms = copy_node (tparms);
28396 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28397 for (unsigned i = 0; i < clen; ++i)
28398 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28399
28400 /* Now we need to rewrite the constructor parms to append them to the
28401 class parms. */
28402 for (unsigned i = 0; i < flen; ++i)
28403 {
28404 unsigned index = i + clen;
28405 unsigned level = 1;
28406 tree oldelt = TREE_VEC_ELT (ftparms, i);
28407 tree newelt
28408 = rewrite_tparm_list (oldelt, index, level,
28409 tsubst_args, i, complain);
28410 if (newelt == error_mark_node)
28411 ok = false;
28412 TREE_VEC_ELT (new_vec, index) = newelt;
28413 }
28414
28415 /* Now we have a final set of template parms to substitute into the
28416 function signature. */
28417 targs = template_parms_to_args (tparms);
28418 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28419 complain, ctor);
28420 if (fparms == error_mark_node)
28421 ok = false;
28422 if (ci)
28423 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28424
28425 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28426 cp_unevaluated_operand. */
28427 cp_evaluated ev;
28428 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28429 current_template_parms = save_parms;
28430 }
28431 else
28432 {
28433 /* Substitute in the same arguments to rewrite class members into
28434 references to members of an unknown specialization. */
28435 cp_evaluated ev;
28436 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28437 fargs = tsubst (fargs, targs, complain, ctor);
28438 if (ci)
28439 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28440 }
28441
28442 --processing_template_decl;
28443 if (!ok)
28444 return error_mark_node;
28445 }
28446
28447 if (!memtmpl)
28448 {
28449 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28450 tparms = copy_node (tparms);
28451 INNERMOST_TEMPLATE_PARMS (tparms)
28452 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28453 }
28454
28455 tree fntype = build_function_type (type, fparms);
28456 tree ded_fn = build_lang_decl_loc (loc,
28457 FUNCTION_DECL,
28458 dguide_name (type), fntype);
28459 DECL_ARGUMENTS (ded_fn) = fargs;
28460 DECL_ARTIFICIAL (ded_fn) = true;
28461 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28462 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28463 DECL_ARTIFICIAL (ded_tmpl) = true;
28464 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28465 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28466 if (DECL_P (ctor))
28467 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28468 if (ci)
28469 set_constraints (ded_tmpl, ci);
28470
28471 return ded_tmpl;
28472 }
28473
28474 /* Add to LIST the member types for the reshaped initializer CTOR. */
28475
28476 static tree
28477 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28478 {
28479 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28480 tree idx, val; unsigned i;
28481 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28482 {
28483 tree ftype = elt ? elt : TREE_TYPE (idx);
28484 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28485 && CONSTRUCTOR_NELTS (val)
28486 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28487 type gets a single initializer. */
28488 && CP_AGGREGATE_TYPE_P (ftype)
28489 && !(TREE_CODE (ftype) == ARRAY_TYPE
28490 && uses_template_parms (TYPE_DOMAIN (ftype))))
28491 {
28492 tree subelt = NULL_TREE;
28493 if (TREE_CODE (ftype) == ARRAY_TYPE)
28494 subelt = TREE_TYPE (ftype);
28495 list = collect_ctor_idx_types (val, list, subelt);
28496 continue;
28497 }
28498 tree arg = NULL_TREE;
28499 if (i == v->length() - 1
28500 && PACK_EXPANSION_P (ftype))
28501 /* Give the trailing pack expansion parameter a default argument to
28502 match aggregate initialization behavior, even if we deduce the
28503 length of the pack separately to more than we have initializers. */
28504 arg = build_constructor (init_list_type_node, NULL);
28505 /* if ei is of array type and xi is a braced-init-list or string literal,
28506 Ti is an rvalue reference to the declared type of ei */
28507 STRIP_ANY_LOCATION_WRAPPER (val);
28508 if (TREE_CODE (ftype) == ARRAY_TYPE
28509 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28510 || TREE_CODE (val) == STRING_CST))
28511 {
28512 if (TREE_CODE (val) == STRING_CST)
28513 ftype = cp_build_qualified_type
28514 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28515 ftype = (cp_build_reference_type
28516 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28517 }
28518 list = tree_cons (arg, ftype, list);
28519 }
28520
28521 return list;
28522 }
28523
28524 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28525
28526 static bool
28527 is_spec_or_derived (tree etype, tree tmpl)
28528 {
28529 if (!etype || !CLASS_TYPE_P (etype))
28530 return false;
28531
28532 tree type = TREE_TYPE (tmpl);
28533 tree tparms = (INNERMOST_TEMPLATE_PARMS
28534 (DECL_TEMPLATE_PARMS (tmpl)));
28535 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28536 int err = unify (tparms, targs, type, etype,
28537 UNIFY_ALLOW_DERIVED, /*explain*/false);
28538 ggc_free (targs);
28539 return !err;
28540 }
28541
28542 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28543 INIT. */
28544
28545 static tree
28546 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28547 {
28548 if (cxx_dialect < cxx20)
28549 return NULL_TREE;
28550
28551 if (init == NULL_TREE)
28552 return NULL_TREE;
28553
28554 tree type = TREE_TYPE (tmpl);
28555 if (!CP_AGGREGATE_TYPE_P (type))
28556 return NULL_TREE;
28557
28558 /* No aggregate candidate for copy-initialization. */
28559 if (args->length() == 1)
28560 {
28561 tree val = (*args)[0];
28562 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28563 return NULL_TREE;
28564 }
28565
28566 /* If we encounter a problem, we just won't add the candidate. */
28567 tsubst_flags_t complain = tf_none;
28568
28569 tree parms = NULL_TREE;
28570 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28571 {
28572 init = reshape_init (type, init, complain);
28573 if (init == error_mark_node)
28574 return NULL_TREE;
28575 parms = collect_ctor_idx_types (init, parms);
28576 }
28577 else if (TREE_CODE (init) == TREE_LIST)
28578 {
28579 int len = list_length (init);
28580 for (tree field = TYPE_FIELDS (type);
28581 len;
28582 --len, field = DECL_CHAIN (field))
28583 {
28584 field = next_initializable_field (field);
28585 if (!field)
28586 return NULL_TREE;
28587 tree ftype = finish_decltype_type (field, true, complain);
28588 parms = tree_cons (NULL_TREE, ftype, parms);
28589 }
28590 }
28591 else
28592 /* Aggregate initialization doesn't apply to an initializer expression. */
28593 return NULL_TREE;
28594
28595 if (parms)
28596 {
28597 tree last = parms;
28598 parms = nreverse (parms);
28599 TREE_CHAIN (last) = void_list_node;
28600 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28601 return guide;
28602 }
28603
28604 return NULL_TREE;
28605 }
28606
28607 /* UGUIDES are the deduction guides for the underlying template of alias
28608 template TMPL; adjust them to be deduction guides for TMPL. */
28609
28610 static tree
28611 alias_ctad_tweaks (tree tmpl, tree uguides)
28612 {
28613 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28614 class type (9.2.8.2) where the template-name names an alias template A,
28615 the defining-type-id of A must be of the form
28616
28617 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28618
28619 as specified in 9.2.8.2. The guides of A are the set of functions or
28620 function templates formed as follows. For each function or function
28621 template f in the guides of the template named by the simple-template-id
28622 of the defining-type-id, the template arguments of the return type of f
28623 are deduced from the defining-type-id of A according to the process in
28624 13.10.2.5 with the exception that deduction does not fail if not all
28625 template arguments are deduced. Let g denote the result of substituting
28626 these deductions into f. If substitution succeeds, form a function or
28627 function template f' with the following properties and add it to the set
28628 of guides of A:
28629
28630 * The function type of f' is the function type of g.
28631
28632 * If f is a function template, f' is a function template whose template
28633 parameter list consists of all the template parameters of A (including
28634 their default template arguments) that appear in the above deductions or
28635 (recursively) in their default template arguments, followed by the
28636 template parameters of f that were not deduced (including their default
28637 template arguments), otherwise f' is not a function template.
28638
28639 * The associated constraints (13.5.2) are the conjunction of the
28640 associated constraints of g and a constraint that is satisfied if and only
28641 if the arguments of A are deducible (see below) from the return type.
28642
28643 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28644 be so as well.
28645
28646 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28647 considered to be so as well.
28648
28649 * The explicit-specifier of f' is the explicit-specifier of g (if
28650 any). */
28651
28652 /* This implementation differs from the above in two significant ways:
28653
28654 1) We include all template parameters of A, not just some.
28655 2) The added constraint is same_type instead of deducible.
28656
28657 I believe that while it's probably possible to construct a testcase that
28658 behaves differently with this simplification, it should have the same
28659 effect for real uses. Including all template parameters means that we
28660 deduce all parameters of A when resolving the call, so when we're in the
28661 constraint we don't need to deduce them again, we can just check whether
28662 the deduction produced the desired result. */
28663
28664 tsubst_flags_t complain = tf_warning_or_error;
28665 tree atype = TREE_TYPE (tmpl);
28666 tree aguides = NULL_TREE;
28667 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28668 unsigned natparms = TREE_VEC_LENGTH (atparms);
28669 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28670 for (ovl_iterator iter (uguides); iter; ++iter)
28671 {
28672 tree f = *iter;
28673 tree in_decl = f;
28674 location_t loc = DECL_SOURCE_LOCATION (f);
28675 tree ret = TREE_TYPE (TREE_TYPE (f));
28676 tree fprime = f;
28677 if (TREE_CODE (f) == TEMPLATE_DECL)
28678 {
28679 processing_template_decl_sentinel ptds (/*reset*/false);
28680 ++processing_template_decl;
28681
28682 /* Deduce template arguments for f from the type-id of A. */
28683 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28684 unsigned len = TREE_VEC_LENGTH (ftparms);
28685 tree targs = make_tree_vec (len);
28686 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28687 gcc_assert (!err);
28688
28689 /* The number of parms for f' is the number of parms for A plus
28690 non-deduced parms of f. */
28691 unsigned ndlen = 0;
28692 unsigned j;
28693 for (unsigned i = 0; i < len; ++i)
28694 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28695 ++ndlen;
28696 tree gtparms = make_tree_vec (natparms + ndlen);
28697
28698 /* First copy over the parms of A. */
28699 for (j = 0; j < natparms; ++j)
28700 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28701 /* Now rewrite the non-deduced parms of f. */
28702 for (unsigned i = 0; ndlen && i < len; ++i)
28703 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28704 {
28705 --ndlen;
28706 unsigned index = j++;
28707 unsigned level = 1;
28708 tree oldlist = TREE_VEC_ELT (ftparms, i);
28709 tree list = rewrite_tparm_list (oldlist, index, level,
28710 targs, i, complain);
28711 TREE_VEC_ELT (gtparms, index) = list;
28712 }
28713 gtparms = build_tree_list (size_one_node, gtparms);
28714
28715 /* Substitute the deduced arguments plus the rewritten template
28716 parameters into f to get g. This covers the type, copyness,
28717 guideness, and explicit-specifier. */
28718 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28719 if (g == error_mark_node)
28720 return error_mark_node;
28721 DECL_USE_TEMPLATE (g) = 0;
28722 fprime = build_template_decl (g, gtparms, false);
28723 DECL_TEMPLATE_RESULT (fprime) = g;
28724 TREE_TYPE (fprime) = TREE_TYPE (g);
28725 tree gtargs = template_parms_to_args (gtparms);
28726 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28727 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28728
28729 /* Substitute the associated constraints. */
28730 tree ci = get_constraints (f);
28731 if (ci)
28732 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28733 if (ci == error_mark_node)
28734 return error_mark_node;
28735
28736 /* Add a constraint that the return type matches the instantiation of
28737 A with the same template arguments. */
28738 ret = TREE_TYPE (TREE_TYPE (fprime));
28739 if (!same_type_p (atype, ret)
28740 /* FIXME this should mean they don't compare as equivalent. */
28741 || dependent_alias_template_spec_p (atype, nt_opaque))
28742 {
28743 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28744 ci = append_constraint (ci, same);
28745 }
28746
28747 if (ci)
28748 {
28749 remove_constraints (fprime);
28750 set_constraints (fprime, ci);
28751 }
28752 }
28753 else
28754 {
28755 /* For a non-template deduction guide, if the arguments of A aren't
28756 deducible from the return type, don't add the candidate. */
28757 tree targs = make_tree_vec (natparms);
28758 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28759 for (unsigned i = 0; !err && i < natparms; ++i)
28760 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28761 err = true;
28762 if (err)
28763 continue;
28764 }
28765
28766 aguides = lookup_add (fprime, aguides);
28767 }
28768
28769 return aguides;
28770 }
28771
28772 /* Return artificial deduction guides built from the constructors of class
28773 template TMPL. */
28774
28775 static tree
28776 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28777 {
28778 tree type = TREE_TYPE (tmpl);
28779 tree outer_args = NULL_TREE;
28780 if (DECL_CLASS_SCOPE_P (tmpl)
28781 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28782 {
28783 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28784 type = TREE_TYPE (most_general_template (tmpl));
28785 }
28786
28787 tree cands = NULL_TREE;
28788
28789 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28790 {
28791 /* Skip inherited constructors. */
28792 if (iter.using_p ())
28793 continue;
28794
28795 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28796 cands = lookup_add (guide, cands);
28797 }
28798
28799 /* Add implicit default constructor deduction guide. */
28800 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28801 {
28802 tree guide = build_deduction_guide (type, type, outer_args,
28803 complain);
28804 cands = lookup_add (guide, cands);
28805 }
28806
28807 /* Add copy guide. */
28808 {
28809 tree gtype = build_reference_type (type);
28810 tree guide = build_deduction_guide (type, gtype, outer_args,
28811 complain);
28812 cands = lookup_add (guide, cands);
28813 }
28814
28815 return cands;
28816 }
28817
28818 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28819
28820 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28821 aggregate candidate is added separately because it depends on the
28822 initializer. */
28823
28824 static tree
28825 deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28826 {
28827 tree guides = NULL_TREE;
28828 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28829 {
28830 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28831 tree tinfo = get_template_info (under);
28832 guides = deduction_guides_for (TI_TEMPLATE (tinfo), complain);
28833 }
28834 else
28835 {
28836 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28837 dguide_name (tmpl),
28838 LOOK_want::NORMAL, /*complain*/false);
28839 if (guides == error_mark_node)
28840 guides = NULL_TREE;
28841 }
28842
28843 /* Cache the deduction guides for a template. We also remember the result of
28844 lookup, and rebuild everything if it changes; should be very rare. */
28845 tree_pair_p cache = NULL;
28846 if (tree_pair_p &r
28847 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28848 {
28849 cache = r;
28850 if (cache->purpose == guides)
28851 return cache->value;
28852 }
28853 else
28854 {
28855 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28856 cache->purpose = guides;
28857 }
28858
28859 tree cands = NULL_TREE;
28860 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28861 cands = alias_ctad_tweaks (tmpl, guides);
28862 else
28863 {
28864 cands = ctor_deduction_guides_for (tmpl, complain);
28865 for (ovl_iterator it (guides); it; ++it)
28866 cands = lookup_add (*it, cands);
28867 }
28868
28869 cache->value = cands;
28870 return cands;
28871 }
28872
28873 /* Return whether TMPL is a (class template argument-) deducible template. */
28874
28875 bool
28876 ctad_template_p (tree tmpl)
28877 {
28878 /* A deducible template is either a class template or is an alias template
28879 whose defining-type-id is of the form
28880
28881 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28882
28883 where the nested-name-specifier (if any) is non-dependent and the
28884 template-name of the simple-template-id names a deducible template. */
28885
28886 if (DECL_CLASS_TEMPLATE_P (tmpl)
28887 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28888 return true;
28889 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28890 return false;
28891 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28892 if (tree tinfo = get_template_info (orig))
28893 return ctad_template_p (TI_TEMPLATE (tinfo));
28894 return false;
28895 }
28896
28897 /* Deduce template arguments for the class template placeholder PTYPE for
28898 template TMPL based on the initializer INIT, and return the resulting
28899 type. */
28900
28901 static tree
28902 do_class_deduction (tree ptype, tree tmpl, tree init,
28903 int flags, tsubst_flags_t complain)
28904 {
28905 /* We should have handled this in the caller. */
28906 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28907 return ptype;
28908
28909 /* Look through alias templates that just rename another template. */
28910 tmpl = get_underlying_template (tmpl);
28911 if (!ctad_template_p (tmpl))
28912 {
28913 if (complain & tf_error)
28914 error ("non-deducible template %qT used without template arguments", tmpl);
28915 return error_mark_node;
28916 }
28917 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
28918 {
28919 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28920 if (complain & tf_warning_or_error)
28921 pedwarn (input_location, 0, "alias template deduction only available "
28922 "with %<-std=c++20%> or %<-std=gnu++20%>");
28923 }
28924
28925 if (init && TREE_TYPE (init) == ptype)
28926 /* Using the template parm as its own argument. */
28927 return ptype;
28928
28929 tree type = TREE_TYPE (tmpl);
28930
28931 bool try_list_ctor = false;
28932
28933 releasing_vec rv_args = NULL;
28934 vec<tree,va_gc> *&args = *&rv_args;
28935 if (init == NULL_TREE)
28936 args = make_tree_vector ();
28937 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28938 {
28939 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28940 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28941 {
28942 /* As an exception, the first phase in 16.3.1.7 (considering the
28943 initializer list as a single argument) is omitted if the
28944 initializer list consists of a single expression of type cv U,
28945 where U is a specialization of C or a class derived from a
28946 specialization of C. */
28947 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28948 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28949 try_list_ctor = false;
28950 }
28951 if (try_list_ctor || is_std_init_list (type))
28952 args = make_tree_vector_single (init);
28953 else
28954 args = make_tree_vector_from_ctor (init);
28955 }
28956 else if (TREE_CODE (init) == TREE_LIST)
28957 args = make_tree_vector_from_list (init);
28958 else
28959 args = make_tree_vector_single (init);
28960
28961 /* Do this now to avoid problems with erroneous args later on. */
28962 args = resolve_args (args, complain);
28963 if (args == NULL)
28964 return error_mark_node;
28965
28966 tree cands = deduction_guides_for (tmpl, complain);
28967 if (cands == error_mark_node)
28968 return error_mark_node;
28969
28970 /* Prune explicit deduction guides in copy-initialization context. */
28971 bool elided = false;
28972 if (flags & LOOKUP_ONLYCONVERTING)
28973 {
28974 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28975 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28976 elided = true;
28977
28978 if (elided)
28979 {
28980 /* Found a nonconverting guide, prune the candidates. */
28981 tree pruned = NULL_TREE;
28982 for (lkp_iterator iter (cands); iter; ++iter)
28983 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28984 pruned = lookup_add (*iter, pruned);
28985
28986 cands = pruned;
28987 }
28988 }
28989
28990 if (tree guide = maybe_aggr_guide (tmpl, init, args))
28991 cands = lookup_add (guide, cands);
28992
28993 tree call = error_mark_node;
28994
28995 /* If this is list-initialization and the class has a list constructor, first
28996 try deducing from the list as a single argument, as [over.match.list]. */
28997 tree list_cands = NULL_TREE;
28998 if (try_list_ctor && cands)
28999 for (lkp_iterator iter (cands); iter; ++iter)
29000 {
29001 tree dg = *iter;
29002 if (is_list_ctor (dg))
29003 list_cands = lookup_add (dg, list_cands);
29004 }
29005 if (list_cands)
29006 {
29007 ++cp_unevaluated_operand;
29008 call = build_new_function_call (list_cands, &args, tf_decltype);
29009 --cp_unevaluated_operand;
29010
29011 if (call == error_mark_node)
29012 {
29013 /* That didn't work, now try treating the list as a sequence of
29014 arguments. */
29015 release_tree_vector (args);
29016 args = make_tree_vector_from_ctor (init);
29017 }
29018 }
29019
29020 if (elided && !cands)
29021 {
29022 error ("cannot deduce template arguments for copy-initialization"
29023 " of %qT, as it has no non-explicit deduction guides or "
29024 "user-declared constructors", type);
29025 return error_mark_node;
29026 }
29027 else if (!cands && call == error_mark_node)
29028 {
29029 error ("cannot deduce template arguments of %qT, as it has no viable "
29030 "deduction guides", type);
29031 return error_mark_node;
29032 }
29033
29034 if (call == error_mark_node)
29035 {
29036 ++cp_unevaluated_operand;
29037 call = build_new_function_call (cands, &args, tf_decltype);
29038 --cp_unevaluated_operand;
29039 }
29040
29041 if (call == error_mark_node
29042 && (complain & tf_warning_or_error))
29043 {
29044 error ("class template argument deduction failed:");
29045
29046 ++cp_unevaluated_operand;
29047 call = build_new_function_call (cands, &args, complain | tf_decltype);
29048 --cp_unevaluated_operand;
29049
29050 if (elided)
29051 inform (input_location, "explicit deduction guides not considered "
29052 "for copy-initialization");
29053 }
29054
29055 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
29056 }
29057
29058 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29059 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29060 The CONTEXT determines the context in which auto deduction is performed
29061 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29062 OUTER_TARGS are used during template argument deduction
29063 (context == adc_unify) to properly substitute the result, and is ignored
29064 in other contexts.
29065
29066 For partial-concept-ids, extra args may be appended to the list of deduced
29067 template arguments prior to determining constraint satisfaction. */
29068
29069 tree
29070 do_auto_deduction (tree type, tree init, tree auto_node,
29071 tsubst_flags_t complain, auto_deduction_context context,
29072 tree outer_targs, int flags)
29073 {
29074 tree targs;
29075
29076 if (init == error_mark_node)
29077 return error_mark_node;
29078
29079 if (init && type_dependent_expression_p (init)
29080 && context != adc_unify)
29081 /* Defining a subset of type-dependent expressions that we can deduce
29082 from ahead of time isn't worth the trouble. */
29083 return type;
29084
29085 /* Similarly, we can't deduce from another undeduced decl. */
29086 if (init && undeduced_auto_decl (init))
29087 return type;
29088
29089 /* We may be doing a partial substitution, but we still want to replace
29090 auto_node. */
29091 complain &= ~tf_partial;
29092
29093 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29094 /* C++17 class template argument deduction. */
29095 return do_class_deduction (type, tmpl, init, flags, complain);
29096
29097 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29098 /* Nothing we can do with this, even in deduction context. */
29099 return type;
29100
29101 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29102 with either a new invented type template parameter U or, if the
29103 initializer is a braced-init-list (8.5.4), with
29104 std::initializer_list<U>. */
29105 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29106 {
29107 if (!DIRECT_LIST_INIT_P (init))
29108 type = listify_autos (type, auto_node);
29109 else if (CONSTRUCTOR_NELTS (init) == 1)
29110 init = CONSTRUCTOR_ELT (init, 0)->value;
29111 else
29112 {
29113 if (complain & tf_warning_or_error)
29114 {
29115 if (permerror (input_location, "direct-list-initialization of "
29116 "%<auto%> requires exactly one element"))
29117 inform (input_location,
29118 "for deduction to %<std::initializer_list%>, use copy-"
29119 "list-initialization (i.e. add %<=%> before the %<{%>)");
29120 }
29121 type = listify_autos (type, auto_node);
29122 }
29123 }
29124
29125 if (type == error_mark_node)
29126 return error_mark_node;
29127
29128 init = resolve_nondeduced_context (init, complain);
29129
29130 if (context == adc_decomp_type
29131 && auto_node == type
29132 && init != error_mark_node
29133 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29134 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29135 and initializer has array type, deduce cv-qualified array type. */
29136 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
29137 complain);
29138 else if (AUTO_IS_DECLTYPE (auto_node))
29139 {
29140 tree stripped_init = tree_strip_any_location_wrapper (init);
29141 bool id = (DECL_P (stripped_init)
29142 || ((TREE_CODE (init) == COMPONENT_REF
29143 || TREE_CODE (init) == SCOPE_REF)
29144 && !REF_PARENTHESIZED_P (init)));
29145 targs = make_tree_vec (1);
29146 TREE_VEC_ELT (targs, 0)
29147 = finish_decltype_type (init, id, tf_warning_or_error);
29148 if (type != auto_node)
29149 {
29150 if (complain & tf_error)
29151 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29152 return error_mark_node;
29153 }
29154 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29155 {
29156 if (complain & tf_error)
29157 error ("%<decltype(auto)%> cannot be cv-qualified");
29158 return error_mark_node;
29159 }
29160 }
29161 else
29162 {
29163 if (error_operand_p (init))
29164 return error_mark_node;
29165
29166 tree parms = build_tree_list (NULL_TREE, type);
29167 tree tparms;
29168
29169 if (flag_concepts)
29170 tparms = extract_autos (type);
29171 else
29172 {
29173 tparms = make_tree_vec (1);
29174 TREE_VEC_ELT (tparms, 0)
29175 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29176 }
29177
29178 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29179 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29180 DEDUCE_CALL,
29181 NULL, /*explain_p=*/false);
29182 if (val > 0)
29183 {
29184 if (processing_template_decl)
29185 /* Try again at instantiation time. */
29186 return type;
29187 if (type && type != error_mark_node
29188 && (complain & tf_error))
29189 /* If type is error_mark_node a diagnostic must have been
29190 emitted by now. Also, having a mention to '<type error>'
29191 in the diagnostic is not really useful to the user. */
29192 {
29193 if (cfun
29194 && FNDECL_USED_AUTO (current_function_decl)
29195 && (auto_node
29196 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29197 && LAMBDA_FUNCTION_P (current_function_decl))
29198 error ("unable to deduce lambda return type from %qE", init);
29199 else
29200 error ("unable to deduce %qT from %qE", type, init);
29201 type_unification_real (tparms, targs, parms, &init, 1, 0,
29202 DEDUCE_CALL,
29203 NULL, /*explain_p=*/true);
29204 }
29205 return error_mark_node;
29206 }
29207 }
29208
29209 /* Check any placeholder constraints against the deduced type. */
29210 if (flag_concepts && !processing_template_decl)
29211 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29212 {
29213 /* Use the deduced type to check the associated constraints. If we
29214 have a partial-concept-id, rebuild the argument list so that
29215 we check using the extra arguments. */
29216 check = unpack_concept_check (check);
29217 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29218 tree cdecl = TREE_OPERAND (check, 0);
29219 if (OVL_P (cdecl))
29220 cdecl = OVL_FIRST (cdecl);
29221 tree cargs = TREE_OPERAND (check, 1);
29222 if (TREE_VEC_LENGTH (cargs) > 1)
29223 {
29224 cargs = copy_node (cargs);
29225 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29226 }
29227 else
29228 cargs = targs;
29229
29230 /* Rebuild the check using the deduced arguments. */
29231 check = build_concept_check (cdecl, cargs, tf_none);
29232
29233 if (!constraints_satisfied_p (check))
29234 {
29235 if (complain & tf_warning_or_error)
29236 {
29237 auto_diagnostic_group d;
29238 switch (context)
29239 {
29240 case adc_unspecified:
29241 case adc_unify:
29242 error("placeholder constraints not satisfied");
29243 break;
29244 case adc_variable_type:
29245 case adc_decomp_type:
29246 error ("deduced initializer does not satisfy "
29247 "placeholder constraints");
29248 break;
29249 case adc_return_type:
29250 error ("deduced return type does not satisfy "
29251 "placeholder constraints");
29252 break;
29253 case adc_requirement:
29254 error ("deduced expression type does not satisfy "
29255 "placeholder constraints");
29256 break;
29257 }
29258 diagnose_constraints (input_location, check, targs);
29259 }
29260 return error_mark_node;
29261 }
29262 }
29263
29264 if (processing_template_decl && context != adc_unify)
29265 outer_targs = current_template_args ();
29266 targs = add_to_template_args (outer_targs, targs);
29267 return tsubst (type, targs, complain, NULL_TREE);
29268 }
29269
29270 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29271 result. */
29272
29273 tree
29274 splice_late_return_type (tree type, tree late_return_type)
29275 {
29276 if (late_return_type)
29277 {
29278 gcc_assert (is_auto (type) || seen_error ());
29279 return late_return_type;
29280 }
29281
29282 if (tree *auto_node = find_type_usage (&type, is_auto))
29283 {
29284 tree idx = get_template_parm_index (*auto_node);
29285 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29286 {
29287 /* In an abbreviated function template we didn't know we were dealing
29288 with a function template when we saw the auto return type, so update
29289 it to have the correct level. */
29290 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29291 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29292 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29293 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29294 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29295 *auto_node = new_auto;
29296 }
29297 }
29298 return type;
29299 }
29300
29301 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29302 'decltype(auto)' or a deduced class template. */
29303
29304 bool
29305 is_auto (const_tree type)
29306 {
29307 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29308 && (TYPE_IDENTIFIER (type) == auto_identifier
29309 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29310 return true;
29311 else
29312 return false;
29313 }
29314
29315 /* for_each_template_parm callback for type_uses_auto. */
29316
29317 int
29318 is_auto_r (tree tp, void */*data*/)
29319 {
29320 return is_auto (tp);
29321 }
29322
29323 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29324 a use of `auto'. Returns NULL_TREE otherwise. */
29325
29326 tree
29327 type_uses_auto (tree type)
29328 {
29329 if (type == NULL_TREE)
29330 return NULL_TREE;
29331 else if (flag_concepts)
29332 {
29333 /* The Concepts TS allows multiple autos in one type-specifier; just
29334 return the first one we find, do_auto_deduction will collect all of
29335 them. */
29336 if (uses_template_parms (type))
29337 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29338 /*visited*/NULL, /*nondeduced*/false);
29339 else
29340 return NULL_TREE;
29341 }
29342 else if (tree *tp = find_type_usage (&type, is_auto))
29343 return *tp;
29344 else
29345 return NULL_TREE;
29346 }
29347
29348 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29349 concepts are enabled, auto is acceptable in template arguments, but
29350 only when TEMPL identifies a template class. Return TRUE if any
29351 such errors were reported. */
29352
29353 bool
29354 check_auto_in_tmpl_args (tree tmpl, tree args)
29355 {
29356 /* If there were previous errors, nevermind. */
29357 if (!args || TREE_CODE (args) != TREE_VEC)
29358 return false;
29359
29360 /* If TMPL is an identifier, we're parsing and we can't tell yet
29361 whether TMPL is supposed to be a type, a function or a variable.
29362 We'll only be able to tell during template substitution, so we
29363 expect to be called again then. If concepts are enabled and we
29364 know we have a type, we're ok. */
29365 if (flag_concepts
29366 && (identifier_p (tmpl)
29367 || (DECL_P (tmpl)
29368 && (DECL_TYPE_TEMPLATE_P (tmpl)
29369 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29370 return false;
29371
29372 /* Quickly search for any occurrences of auto; usually there won't
29373 be any, and then we'll avoid allocating the vector. */
29374 if (!type_uses_auto (args))
29375 return false;
29376
29377 bool errors = false;
29378
29379 tree vec = extract_autos (args);
29380 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29381 {
29382 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29383 error_at (DECL_SOURCE_LOCATION (xauto),
29384 "invalid use of %qT in template argument", xauto);
29385 errors = true;
29386 }
29387
29388 return errors;
29389 }
29390
29391 /* Recursively walk over && expressions searching for EXPR. Return a reference
29392 to that expression. */
29393
29394 static tree *find_template_requirement (tree *t, tree key)
29395 {
29396 if (*t == key)
29397 return t;
29398 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29399 {
29400 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29401 return p;
29402 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29403 return p;
29404 }
29405 return 0;
29406 }
29407
29408 /* Convert the generic type parameters in PARM that match the types given in the
29409 range [START_IDX, END_IDX) from the current_template_parms into generic type
29410 packs. */
29411
29412 tree
29413 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29414 {
29415 tree current = current_template_parms;
29416 int depth = TMPL_PARMS_DEPTH (current);
29417 current = INNERMOST_TEMPLATE_PARMS (current);
29418 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29419
29420 for (int i = 0; i < start_idx; ++i)
29421 TREE_VEC_ELT (replacement, i)
29422 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29423
29424 for (int i = start_idx; i < end_idx; ++i)
29425 {
29426 /* Create a distinct parameter pack type from the current parm and add it
29427 to the replacement args to tsubst below into the generic function
29428 parameter. */
29429 tree node = TREE_VEC_ELT (current, i);
29430 tree o = TREE_TYPE (TREE_VALUE (node));
29431 tree t = copy_type (o);
29432 TEMPLATE_TYPE_PARM_INDEX (t)
29433 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29434 t, 0, 0, tf_none);
29435 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29436 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29437 TYPE_MAIN_VARIANT (t) = t;
29438 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29439 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29440 TREE_VEC_ELT (replacement, i) = t;
29441
29442 /* Replace the current template parameter with new pack. */
29443 TREE_VALUE (node) = TREE_CHAIN (t);
29444
29445 /* Surgically adjust the associated constraint of adjusted parameter
29446 and it's corresponding contribution to the current template
29447 requirements. */
29448 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29449 {
29450 tree id = unpack_concept_check (constr);
29451 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
29452 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29453 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29454
29455 /* If there was a constraint, we also need to replace that in
29456 the template requirements, which we've already built. */
29457 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29458 reqs = find_template_requirement (reqs, constr);
29459 *reqs = fold;
29460 }
29461 }
29462
29463 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29464 TREE_VEC_ELT (replacement, i)
29465 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29466
29467 /* If there are more levels then build up the replacement with the outer
29468 template parms. */
29469 if (depth > 1)
29470 replacement = add_to_template_args (template_parms_to_args
29471 (TREE_CHAIN (current_template_parms)),
29472 replacement);
29473
29474 return tsubst (parm, replacement, tf_none, NULL_TREE);
29475 }
29476
29477 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29478 0..N-1. */
29479
29480 void
29481 declare_integer_pack (void)
29482 {
29483 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29484 build_function_type_list (integer_type_node,
29485 integer_type_node,
29486 NULL_TREE),
29487 NULL_TREE, ECF_CONST);
29488 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29489 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29490 CP_BUILT_IN_INTEGER_PACK);
29491 }
29492
29493 /* Set up the hash tables for template instantiations. */
29494
29495 void
29496 init_template_processing (void)
29497 {
29498 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29499 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29500
29501 if (cxx_dialect >= cxx11)
29502 declare_integer_pack ();
29503 }
29504
29505 /* Print stats about the template hash tables for -fstats. */
29506
29507 void
29508 print_template_statistics (void)
29509 {
29510 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29511 "%f collisions\n", (long) decl_specializations->size (),
29512 (long) decl_specializations->elements (),
29513 decl_specializations->collisions ());
29514 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29515 "%f collisions\n", (long) type_specializations->size (),
29516 (long) type_specializations->elements (),
29517 type_specializations->collisions ());
29518 }
29519
29520 #if CHECKING_P
29521
29522 namespace selftest {
29523
29524 /* Verify that build_non_dependent_expr () works, for various expressions,
29525 and that location wrappers don't affect the results. */
29526
29527 static void
29528 test_build_non_dependent_expr ()
29529 {
29530 location_t loc = BUILTINS_LOCATION;
29531
29532 /* Verify constants, without and with location wrappers. */
29533 tree int_cst = build_int_cst (integer_type_node, 42);
29534 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29535
29536 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29537 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29538 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29539
29540 tree string_lit = build_string (4, "foo");
29541 TREE_TYPE (string_lit) = char_array_type_node;
29542 string_lit = fix_string_type (string_lit);
29543 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29544
29545 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29546 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29547 ASSERT_EQ (wrapped_string_lit,
29548 build_non_dependent_expr (wrapped_string_lit));
29549 }
29550
29551 /* Verify that type_dependent_expression_p () works correctly, even
29552 in the presence of location wrapper nodes. */
29553
29554 static void
29555 test_type_dependent_expression_p ()
29556 {
29557 location_t loc = BUILTINS_LOCATION;
29558
29559 tree name = get_identifier ("foo");
29560
29561 /* If no templates are involved, nothing is type-dependent. */
29562 gcc_assert (!processing_template_decl);
29563 ASSERT_FALSE (type_dependent_expression_p (name));
29564
29565 ++processing_template_decl;
29566
29567 /* Within a template, an unresolved name is always type-dependent. */
29568 ASSERT_TRUE (type_dependent_expression_p (name));
29569
29570 /* Ensure it copes with NULL_TREE and errors. */
29571 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29572 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29573
29574 /* A USING_DECL in a template should be type-dependent, even if wrapped
29575 with a location wrapper (PR c++/83799). */
29576 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29577 TREE_TYPE (using_decl) = integer_type_node;
29578 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29579 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29580 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29581 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29582
29583 --processing_template_decl;
29584 }
29585
29586 /* Run all of the selftests within this file. */
29587
29588 void
29589 cp_pt_c_tests ()
29590 {
29591 test_build_non_dependent_expr ();
29592 test_type_dependent_expression_p ();
29593 }
29594
29595 } // namespace selftest
29596
29597 #endif /* #if CHECKING_P */
29598
29599 #include "gt-cp-pt.h"