c++: Return only in-scope tparms in keep_template_parm [PR95310]
[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 TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4708 thereof, and converts it into an argument suitable to be passed to
4709 the type substitution functions. Note that if the TREE_LIST contains
4710 an error_mark node, the returned argument is error_mark_node. */
4711
4712 tree
4713 template_parm_to_arg (tree t)
4714 {
4715 if (!t)
4716 return NULL_TREE;
4717
4718 if (TREE_CODE (t) == TREE_LIST)
4719 t = TREE_VALUE (t);
4720
4721 if (error_operand_p (t))
4722 return error_mark_node;
4723
4724 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4725 {
4726 if (TREE_CODE (t) == TYPE_DECL
4727 || TREE_CODE (t) == TEMPLATE_DECL)
4728 t = TREE_TYPE (t);
4729 else
4730 t = DECL_INITIAL (t);
4731 }
4732
4733 gcc_assert (TEMPLATE_PARM_P (t));
4734
4735 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4736 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4737 {
4738 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4739 {
4740 /* Turn this argument into a TYPE_ARGUMENT_PACK
4741 with a single element, which expands T. */
4742 tree vec = make_tree_vec (1);
4743 if (CHECKING_P)
4744 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4745
4746 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4747
4748 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4749 SET_ARGUMENT_PACK_ARGS (t, vec);
4750 }
4751 }
4752 else
4753 {
4754 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4755 {
4756 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4757 with a single element, which expands T. */
4758 tree vec = make_tree_vec (1);
4759 if (CHECKING_P)
4760 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4761
4762 t = convert_from_reference (t);
4763 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4764
4765 t = make_node (NONTYPE_ARGUMENT_PACK);
4766 SET_ARGUMENT_PACK_ARGS (t, vec);
4767 }
4768 else
4769 t = convert_from_reference (t);
4770 }
4771 return t;
4772 }
4773
4774 /* Given a single level of template parameters (a TREE_VEC), return it
4775 as a set of template arguments. */
4776
4777 tree
4778 template_parms_level_to_args (tree parms)
4779 {
4780 tree a = copy_node (parms);
4781 TREE_TYPE (a) = NULL_TREE;
4782 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4783 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4784
4785 if (CHECKING_P)
4786 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4787
4788 return a;
4789 }
4790
4791 /* Given a set of template parameters, return them as a set of template
4792 arguments. The template parameters are represented as a TREE_VEC, in
4793 the form documented in cp-tree.h for template arguments. */
4794
4795 tree
4796 template_parms_to_args (tree parms)
4797 {
4798 tree header;
4799 tree args = NULL_TREE;
4800 int length = TMPL_PARMS_DEPTH (parms);
4801 int l = length;
4802
4803 /* If there is only one level of template parameters, we do not
4804 create a TREE_VEC of TREE_VECs. Instead, we return a single
4805 TREE_VEC containing the arguments. */
4806 if (length > 1)
4807 args = make_tree_vec (length);
4808
4809 for (header = parms; header; header = TREE_CHAIN (header))
4810 {
4811 tree a = template_parms_level_to_args (TREE_VALUE (header));
4812
4813 if (length > 1)
4814 TREE_VEC_ELT (args, --l) = a;
4815 else
4816 args = a;
4817 }
4818
4819 return args;
4820 }
4821
4822 /* Within the declaration of a template, return the currently active
4823 template parameters as an argument TREE_VEC. */
4824
4825 static tree
4826 current_template_args (void)
4827 {
4828 return template_parms_to_args (current_template_parms);
4829 }
4830
4831 /* Return the fully generic arguments for of TMPL, i.e. what
4832 current_template_args would be while parsing it. */
4833
4834 tree
4835 generic_targs_for (tree tmpl)
4836 {
4837 if (tmpl == NULL_TREE)
4838 return NULL_TREE;
4839 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4840 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4841 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4842 template parameter, it has no TEMPLATE_INFO; for a partial
4843 specialization, it has the arguments for the primary template, and we
4844 want the arguments for the partial specialization. */;
4845 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4846 if (tree ti = get_template_info (result))
4847 return TI_ARGS (ti);
4848 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4849 }
4850
4851 /* Update the declared TYPE by doing any lookups which were thought to be
4852 dependent, but are not now that we know the SCOPE of the declarator. */
4853
4854 tree
4855 maybe_update_decl_type (tree orig_type, tree scope)
4856 {
4857 tree type = orig_type;
4858
4859 if (type == NULL_TREE)
4860 return type;
4861
4862 if (TREE_CODE (orig_type) == TYPE_DECL)
4863 type = TREE_TYPE (type);
4864
4865 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4866 && dependent_type_p (type)
4867 /* Don't bother building up the args in this case. */
4868 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4869 {
4870 /* tsubst in the args corresponding to the template parameters,
4871 including auto if present. Most things will be unchanged, but
4872 make_typename_type and tsubst_qualified_id will resolve
4873 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4874 tree args = current_template_args ();
4875 tree auto_node = type_uses_auto (type);
4876 tree pushed;
4877 if (auto_node)
4878 {
4879 tree auto_vec = make_tree_vec (1);
4880 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4881 args = add_to_template_args (args, auto_vec);
4882 }
4883 pushed = push_scope (scope);
4884 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4885 if (pushed)
4886 pop_scope (scope);
4887 }
4888
4889 if (type == error_mark_node)
4890 return orig_type;
4891
4892 if (TREE_CODE (orig_type) == TYPE_DECL)
4893 {
4894 if (same_type_p (type, TREE_TYPE (orig_type)))
4895 type = orig_type;
4896 else
4897 type = TYPE_NAME (type);
4898 }
4899 return type;
4900 }
4901
4902 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4903 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4904 the new template is a member template. */
4905
4906 static tree
4907 build_template_decl (tree decl, tree parms, bool member_template_p)
4908 {
4909 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4910 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4911 DECL_TEMPLATE_PARMS (tmpl) = parms;
4912 DECL_TEMPLATE_RESULT (tmpl) = decl;
4913 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4914 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4915 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4916 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4917
4918 return tmpl;
4919 }
4920
4921 struct template_parm_data
4922 {
4923 /* The level of the template parameters we are currently
4924 processing. */
4925 int level;
4926
4927 /* The index of the specialization argument we are currently
4928 processing. */
4929 int current_arg;
4930
4931 /* An array whose size is the number of template parameters. The
4932 elements are nonzero if the parameter has been used in any one
4933 of the arguments processed so far. */
4934 int* parms;
4935
4936 /* An array whose size is the number of template arguments. The
4937 elements are nonzero if the argument makes use of template
4938 parameters of this level. */
4939 int* arg_uses_template_parms;
4940 };
4941
4942 /* Subroutine of push_template_decl used to see if each template
4943 parameter in a partial specialization is used in the explicit
4944 argument list. If T is of the LEVEL given in DATA (which is
4945 treated as a template_parm_data*), then DATA->PARMS is marked
4946 appropriately. */
4947
4948 static int
4949 mark_template_parm (tree t, void* data)
4950 {
4951 int level;
4952 int idx;
4953 struct template_parm_data* tpd = (struct template_parm_data*) data;
4954
4955 template_parm_level_and_index (t, &level, &idx);
4956
4957 if (level == tpd->level)
4958 {
4959 tpd->parms[idx] = 1;
4960 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4961 }
4962
4963 /* In C++17 the type of a non-type argument is a deduced context. */
4964 if (cxx_dialect >= cxx17
4965 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4966 for_each_template_parm (TREE_TYPE (t),
4967 &mark_template_parm,
4968 data,
4969 NULL,
4970 /*include_nondeduced_p=*/false);
4971
4972 /* Return zero so that for_each_template_parm will continue the
4973 traversal of the tree; we want to mark *every* template parm. */
4974 return 0;
4975 }
4976
4977 /* Process the partial specialization DECL. */
4978
4979 static tree
4980 process_partial_specialization (tree decl)
4981 {
4982 tree type = TREE_TYPE (decl);
4983 tree tinfo = get_template_info (decl);
4984 tree maintmpl = TI_TEMPLATE (tinfo);
4985 tree specargs = TI_ARGS (tinfo);
4986 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4987 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4988 tree inner_parms;
4989 tree inst;
4990 int nargs = TREE_VEC_LENGTH (inner_args);
4991 int ntparms;
4992 int i;
4993 bool did_error_intro = false;
4994 struct template_parm_data tpd;
4995 struct template_parm_data tpd2;
4996
4997 gcc_assert (current_template_parms);
4998
4999 /* A concept cannot be specialized. */
5000 if (flag_concepts && variable_concept_p (maintmpl))
5001 {
5002 error ("specialization of variable concept %q#D", maintmpl);
5003 return error_mark_node;
5004 }
5005
5006 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5007 ntparms = TREE_VEC_LENGTH (inner_parms);
5008
5009 /* We check that each of the template parameters given in the
5010 partial specialization is used in the argument list to the
5011 specialization. For example:
5012
5013 template <class T> struct S;
5014 template <class T> struct S<T*>;
5015
5016 The second declaration is OK because `T*' uses the template
5017 parameter T, whereas
5018
5019 template <class T> struct S<int>;
5020
5021 is no good. Even trickier is:
5022
5023 template <class T>
5024 struct S1
5025 {
5026 template <class U>
5027 struct S2;
5028 template <class U>
5029 struct S2<T>;
5030 };
5031
5032 The S2<T> declaration is actually invalid; it is a
5033 full-specialization. Of course,
5034
5035 template <class U>
5036 struct S2<T (*)(U)>;
5037
5038 or some such would have been OK. */
5039 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5040 tpd.parms = XALLOCAVEC (int, ntparms);
5041 memset (tpd.parms, 0, sizeof (int) * ntparms);
5042
5043 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5044 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5045 for (i = 0; i < nargs; ++i)
5046 {
5047 tpd.current_arg = i;
5048 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5049 &mark_template_parm,
5050 &tpd,
5051 NULL,
5052 /*include_nondeduced_p=*/false);
5053 }
5054 for (i = 0; i < ntparms; ++i)
5055 if (tpd.parms[i] == 0)
5056 {
5057 /* One of the template parms was not used in a deduced context in the
5058 specialization. */
5059 if (!did_error_intro)
5060 {
5061 error ("template parameters not deducible in "
5062 "partial specialization:");
5063 did_error_intro = true;
5064 }
5065
5066 inform (input_location, " %qD",
5067 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5068 }
5069
5070 if (did_error_intro)
5071 return error_mark_node;
5072
5073 /* [temp.class.spec]
5074
5075 The argument list of the specialization shall not be identical to
5076 the implicit argument list of the primary template. */
5077 tree main_args
5078 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5079 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5080 && (!flag_concepts
5081 || !strictly_subsumes (current_template_constraints (),
5082 main_args, maintmpl)))
5083 {
5084 if (!flag_concepts)
5085 error ("partial specialization %q+D does not specialize "
5086 "any template arguments; to define the primary template, "
5087 "remove the template argument list", decl);
5088 else
5089 error ("partial specialization %q+D does not specialize any "
5090 "template arguments and is not more constrained than "
5091 "the primary template; to define the primary template, "
5092 "remove the template argument list", decl);
5093 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5094 }
5095
5096 /* A partial specialization that replaces multiple parameters of the
5097 primary template with a pack expansion is less specialized for those
5098 parameters. */
5099 if (nargs < DECL_NTPARMS (maintmpl))
5100 {
5101 error ("partial specialization is not more specialized than the "
5102 "primary template because it replaces multiple parameters "
5103 "with a pack expansion");
5104 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5105 /* Avoid crash in process_partial_specialization. */
5106 return decl;
5107 }
5108
5109 else if (nargs > DECL_NTPARMS (maintmpl))
5110 {
5111 error ("too many arguments for partial specialization %qT", type);
5112 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5113 /* Avoid crash below. */
5114 return decl;
5115 }
5116
5117 /* If we aren't in a dependent class, we can actually try deduction. */
5118 else if (tpd.level == 1
5119 /* FIXME we should be able to handle a partial specialization of a
5120 partial instantiation, but currently we can't (c++/41727). */
5121 && TMPL_ARGS_DEPTH (specargs) == 1
5122 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5123 {
5124 auto_diagnostic_group d;
5125 if (permerror (input_location, "partial specialization %qD is not "
5126 "more specialized than", decl))
5127 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5128 maintmpl);
5129 }
5130
5131 /* [temp.class.spec]
5132
5133 A partially specialized non-type argument expression shall not
5134 involve template parameters of the partial specialization except
5135 when the argument expression is a simple identifier.
5136
5137 The type of a template parameter corresponding to a specialized
5138 non-type argument shall not be dependent on a parameter of the
5139 specialization.
5140
5141 Also, we verify that pack expansions only occur at the
5142 end of the argument list. */
5143 tpd2.parms = 0;
5144 for (i = 0; i < nargs; ++i)
5145 {
5146 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5147 tree arg = TREE_VEC_ELT (inner_args, i);
5148 tree packed_args = NULL_TREE;
5149 int j, len = 1;
5150
5151 if (ARGUMENT_PACK_P (arg))
5152 {
5153 /* Extract the arguments from the argument pack. We'll be
5154 iterating over these in the following loop. */
5155 packed_args = ARGUMENT_PACK_ARGS (arg);
5156 len = TREE_VEC_LENGTH (packed_args);
5157 }
5158
5159 for (j = 0; j < len; j++)
5160 {
5161 if (packed_args)
5162 /* Get the Jth argument in the parameter pack. */
5163 arg = TREE_VEC_ELT (packed_args, j);
5164
5165 if (PACK_EXPANSION_P (arg))
5166 {
5167 /* Pack expansions must come at the end of the
5168 argument list. */
5169 if ((packed_args && j < len - 1)
5170 || (!packed_args && i < nargs - 1))
5171 {
5172 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5173 error ("parameter pack argument %qE must be at the "
5174 "end of the template argument list", arg);
5175 else
5176 error ("parameter pack argument %qT must be at the "
5177 "end of the template argument list", arg);
5178 }
5179 }
5180
5181 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5182 /* We only care about the pattern. */
5183 arg = PACK_EXPANSION_PATTERN (arg);
5184
5185 if (/* These first two lines are the `non-type' bit. */
5186 !TYPE_P (arg)
5187 && TREE_CODE (arg) != TEMPLATE_DECL
5188 /* This next two lines are the `argument expression is not just a
5189 simple identifier' condition and also the `specialized
5190 non-type argument' bit. */
5191 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5192 && !((REFERENCE_REF_P (arg)
5193 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5194 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5195 {
5196 if ((!packed_args && tpd.arg_uses_template_parms[i])
5197 || (packed_args && uses_template_parms (arg)))
5198 error_at (cp_expr_loc_or_input_loc (arg),
5199 "template argument %qE involves template "
5200 "parameter(s)", arg);
5201 else
5202 {
5203 /* Look at the corresponding template parameter,
5204 marking which template parameters its type depends
5205 upon. */
5206 tree type = TREE_TYPE (parm);
5207
5208 if (!tpd2.parms)
5209 {
5210 /* We haven't yet initialized TPD2. Do so now. */
5211 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5212 /* The number of parameters here is the number in the
5213 main template, which, as checked in the assertion
5214 above, is NARGS. */
5215 tpd2.parms = XALLOCAVEC (int, nargs);
5216 tpd2.level =
5217 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5218 }
5219
5220 /* Mark the template parameters. But this time, we're
5221 looking for the template parameters of the main
5222 template, not in the specialization. */
5223 tpd2.current_arg = i;
5224 tpd2.arg_uses_template_parms[i] = 0;
5225 memset (tpd2.parms, 0, sizeof (int) * nargs);
5226 for_each_template_parm (type,
5227 &mark_template_parm,
5228 &tpd2,
5229 NULL,
5230 /*include_nondeduced_p=*/false);
5231
5232 if (tpd2.arg_uses_template_parms [i])
5233 {
5234 /* The type depended on some template parameters.
5235 If they are fully specialized in the
5236 specialization, that's OK. */
5237 int j;
5238 int count = 0;
5239 for (j = 0; j < nargs; ++j)
5240 if (tpd2.parms[j] != 0
5241 && tpd.arg_uses_template_parms [j])
5242 ++count;
5243 if (count != 0)
5244 error_n (input_location, count,
5245 "type %qT of template argument %qE depends "
5246 "on a template parameter",
5247 "type %qT of template argument %qE depends "
5248 "on template parameters",
5249 type,
5250 arg);
5251 }
5252 }
5253 }
5254 }
5255 }
5256
5257 /* We should only get here once. */
5258 if (TREE_CODE (decl) == TYPE_DECL)
5259 gcc_assert (!COMPLETE_TYPE_P (type));
5260
5261 // Build the template decl.
5262 tree tmpl = build_template_decl (decl, current_template_parms,
5263 DECL_MEMBER_TEMPLATE_P (maintmpl));
5264 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5265 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5266 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5267
5268 /* Give template template parms a DECL_CONTEXT of the template
5269 for which they are a parameter. */
5270 for (i = 0; i < ntparms; ++i)
5271 {
5272 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5273 if (TREE_CODE (parm) == TEMPLATE_DECL)
5274 DECL_CONTEXT (parm) = tmpl;
5275 }
5276
5277 if (VAR_P (decl))
5278 /* We didn't register this in check_explicit_specialization so we could
5279 wait until the constraints were set. */
5280 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5281 else
5282 associate_classtype_constraints (type);
5283
5284 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5285 = tree_cons (specargs, tmpl,
5286 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5287 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5288
5289 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5290 inst = TREE_CHAIN (inst))
5291 {
5292 tree instance = TREE_VALUE (inst);
5293 if (TYPE_P (instance)
5294 ? (COMPLETE_TYPE_P (instance)
5295 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5296 : DECL_TEMPLATE_INSTANTIATION (instance))
5297 {
5298 tree spec = most_specialized_partial_spec (instance, tf_none);
5299 tree inst_decl = (DECL_P (instance)
5300 ? instance : TYPE_NAME (instance));
5301 if (!spec)
5302 /* OK */;
5303 else if (spec == error_mark_node)
5304 permerror (input_location,
5305 "declaration of %qD ambiguates earlier template "
5306 "instantiation for %qD", decl, inst_decl);
5307 else if (TREE_VALUE (spec) == tmpl)
5308 permerror (input_location,
5309 "partial specialization of %qD after instantiation "
5310 "of %qD", decl, inst_decl);
5311 }
5312 }
5313
5314 return decl;
5315 }
5316
5317 /* PARM is a template parameter of some form; return the corresponding
5318 TEMPLATE_PARM_INDEX. */
5319
5320 static tree
5321 get_template_parm_index (tree parm)
5322 {
5323 if (TREE_CODE (parm) == PARM_DECL
5324 || TREE_CODE (parm) == CONST_DECL)
5325 parm = DECL_INITIAL (parm);
5326 else if (TREE_CODE (parm) == TYPE_DECL
5327 || TREE_CODE (parm) == TEMPLATE_DECL)
5328 parm = TREE_TYPE (parm);
5329 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5330 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5331 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5332 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5333 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5334 return parm;
5335 }
5336
5337 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5338 parameter packs used by the template parameter PARM. */
5339
5340 static void
5341 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5342 {
5343 /* A type parm can't refer to another parm. */
5344 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5345 return;
5346 else if (TREE_CODE (parm) == PARM_DECL)
5347 {
5348 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5349 ppd, ppd->visited);
5350 return;
5351 }
5352
5353 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5354
5355 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5356 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5357 {
5358 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5359 if (template_parameter_pack_p (p))
5360 /* Any packs in the type are expanded by this parameter. */;
5361 else
5362 fixed_parameter_pack_p_1 (p, ppd);
5363 }
5364 }
5365
5366 /* PARM is a template parameter pack. Return any parameter packs used in
5367 its type or the type of any of its template parameters. If there are
5368 any such packs, it will be instantiated into a fixed template parameter
5369 list by partial instantiation rather than be fully deduced. */
5370
5371 tree
5372 fixed_parameter_pack_p (tree parm)
5373 {
5374 /* This can only be true in a member template. */
5375 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5376 return NULL_TREE;
5377 /* This can only be true for a parameter pack. */
5378 if (!template_parameter_pack_p (parm))
5379 return NULL_TREE;
5380 /* A type parm can't refer to another parm. */
5381 if (TREE_CODE (parm) == TYPE_DECL)
5382 return NULL_TREE;
5383
5384 tree parameter_packs = NULL_TREE;
5385 struct find_parameter_pack_data ppd;
5386 ppd.parameter_packs = &parameter_packs;
5387 ppd.visited = new hash_set<tree>;
5388 ppd.type_pack_expansion_p = false;
5389
5390 fixed_parameter_pack_p_1 (parm, &ppd);
5391
5392 delete ppd.visited;
5393 return parameter_packs;
5394 }
5395
5396 /* Check that a template declaration's use of default arguments and
5397 parameter packs is not invalid. Here, PARMS are the template
5398 parameters. IS_PRIMARY is true if DECL is the thing declared by
5399 a primary template. IS_PARTIAL is true if DECL is a partial
5400 specialization.
5401
5402 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5403 function template declaration or a friend class template
5404 declaration. In the function case, 1 indicates a declaration, 2
5405 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5406 emitted for extraneous default arguments.
5407
5408 Returns TRUE if there were no errors found, FALSE otherwise. */
5409
5410 bool
5411 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5412 bool is_partial, int is_friend_decl)
5413 {
5414 const char *msg;
5415 int last_level_to_check;
5416 tree parm_level;
5417 bool no_errors = true;
5418
5419 /* [temp.param]
5420
5421 A default template-argument shall not be specified in a
5422 function template declaration or a function template definition, nor
5423 in the template-parameter-list of the definition of a member of a
5424 class template. */
5425
5426 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5427 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5428 /* You can't have a function template declaration in a local
5429 scope, nor you can you define a member of a class template in a
5430 local scope. */
5431 return true;
5432
5433 if ((TREE_CODE (decl) == TYPE_DECL
5434 && TREE_TYPE (decl)
5435 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5436 || (TREE_CODE (decl) == FUNCTION_DECL
5437 && LAMBDA_FUNCTION_P (decl)))
5438 /* A lambda doesn't have an explicit declaration; don't complain
5439 about the parms of the enclosing class. */
5440 return true;
5441
5442 if (current_class_type
5443 && !TYPE_BEING_DEFINED (current_class_type)
5444 && DECL_LANG_SPECIFIC (decl)
5445 && DECL_DECLARES_FUNCTION_P (decl)
5446 /* If this is either a friend defined in the scope of the class
5447 or a member function. */
5448 && (DECL_FUNCTION_MEMBER_P (decl)
5449 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5450 : DECL_FRIEND_CONTEXT (decl)
5451 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5452 : false)
5453 /* And, if it was a member function, it really was defined in
5454 the scope of the class. */
5455 && (!DECL_FUNCTION_MEMBER_P (decl)
5456 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5457 /* We already checked these parameters when the template was
5458 declared, so there's no need to do it again now. This function
5459 was defined in class scope, but we're processing its body now
5460 that the class is complete. */
5461 return true;
5462
5463 /* Core issue 226 (C++0x only): the following only applies to class
5464 templates. */
5465 if (is_primary
5466 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5467 {
5468 /* [temp.param]
5469
5470 If a template-parameter has a default template-argument, all
5471 subsequent template-parameters shall have a default
5472 template-argument supplied. */
5473 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5474 {
5475 tree inner_parms = TREE_VALUE (parm_level);
5476 int ntparms = TREE_VEC_LENGTH (inner_parms);
5477 int seen_def_arg_p = 0;
5478 int i;
5479
5480 for (i = 0; i < ntparms; ++i)
5481 {
5482 tree parm = TREE_VEC_ELT (inner_parms, i);
5483
5484 if (parm == error_mark_node)
5485 continue;
5486
5487 if (TREE_PURPOSE (parm))
5488 seen_def_arg_p = 1;
5489 else if (seen_def_arg_p
5490 && !template_parameter_pack_p (TREE_VALUE (parm)))
5491 {
5492 error ("no default argument for %qD", TREE_VALUE (parm));
5493 /* For better subsequent error-recovery, we indicate that
5494 there should have been a default argument. */
5495 TREE_PURPOSE (parm) = error_mark_node;
5496 no_errors = false;
5497 }
5498 else if (!is_partial
5499 && !is_friend_decl
5500 /* Don't complain about an enclosing partial
5501 specialization. */
5502 && parm_level == parms
5503 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5504 && i < ntparms - 1
5505 && template_parameter_pack_p (TREE_VALUE (parm))
5506 /* A fixed parameter pack will be partially
5507 instantiated into a fixed length list. */
5508 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5509 {
5510 /* A primary class template, primary variable template
5511 (DR 2032), or alias template can only have one
5512 parameter pack, at the end of the template
5513 parameter list. */
5514
5515 error ("parameter pack %q+D must be at the end of the"
5516 " template parameter list", TREE_VALUE (parm));
5517
5518 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5519 = error_mark_node;
5520 no_errors = false;
5521 }
5522 }
5523 }
5524 }
5525
5526 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5527 || is_partial
5528 || !is_primary
5529 || is_friend_decl)
5530 /* For an ordinary class template, default template arguments are
5531 allowed at the innermost level, e.g.:
5532 template <class T = int>
5533 struct S {};
5534 but, in a partial specialization, they're not allowed even
5535 there, as we have in [temp.class.spec]:
5536
5537 The template parameter list of a specialization shall not
5538 contain default template argument values.
5539
5540 So, for a partial specialization, or for a function template
5541 (in C++98/C++03), we look at all of them. */
5542 ;
5543 else
5544 /* But, for a primary class template that is not a partial
5545 specialization we look at all template parameters except the
5546 innermost ones. */
5547 parms = TREE_CHAIN (parms);
5548
5549 /* Figure out what error message to issue. */
5550 if (is_friend_decl == 2)
5551 msg = G_("default template arguments may not be used in function template "
5552 "friend re-declaration");
5553 else if (is_friend_decl)
5554 msg = G_("default template arguments may not be used in template "
5555 "friend declarations");
5556 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5557 msg = G_("default template arguments may not be used in function templates "
5558 "without %<-std=c++11%> or %<-std=gnu++11%>");
5559 else if (is_partial)
5560 msg = G_("default template arguments may not be used in "
5561 "partial specializations");
5562 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5563 msg = G_("default argument for template parameter for class enclosing %qD");
5564 else
5565 /* Per [temp.param]/9, "A default template-argument shall not be
5566 specified in the template-parameter-lists of the definition of
5567 a member of a class template that appears outside of the member's
5568 class.", thus if we aren't handling a member of a class template
5569 there is no need to examine the parameters. */
5570 return true;
5571
5572 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5573 /* If we're inside a class definition, there's no need to
5574 examine the parameters to the class itself. On the one
5575 hand, they will be checked when the class is defined, and,
5576 on the other, default arguments are valid in things like:
5577 template <class T = double>
5578 struct S { template <class U> void f(U); };
5579 Here the default argument for `S' has no bearing on the
5580 declaration of `f'. */
5581 last_level_to_check = template_class_depth (current_class_type) + 1;
5582 else
5583 /* Check everything. */
5584 last_level_to_check = 0;
5585
5586 for (parm_level = parms;
5587 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5588 parm_level = TREE_CHAIN (parm_level))
5589 {
5590 tree inner_parms = TREE_VALUE (parm_level);
5591 int i;
5592 int ntparms;
5593
5594 ntparms = TREE_VEC_LENGTH (inner_parms);
5595 for (i = 0; i < ntparms; ++i)
5596 {
5597 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5598 continue;
5599
5600 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5601 {
5602 if (msg)
5603 {
5604 no_errors = false;
5605 if (is_friend_decl == 2)
5606 return no_errors;
5607
5608 error (msg, decl);
5609 msg = 0;
5610 }
5611
5612 /* Clear out the default argument so that we are not
5613 confused later. */
5614 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5615 }
5616 }
5617
5618 /* At this point, if we're still interested in issuing messages,
5619 they must apply to classes surrounding the object declared. */
5620 if (msg)
5621 msg = G_("default argument for template parameter for class "
5622 "enclosing %qD");
5623 }
5624
5625 return no_errors;
5626 }
5627
5628 /* Worker for push_template_decl_real, called via
5629 for_each_template_parm. DATA is really an int, indicating the
5630 level of the parameters we are interested in. If T is a template
5631 parameter of that level, return nonzero. */
5632
5633 static int
5634 template_parm_this_level_p (tree t, void* data)
5635 {
5636 int this_level = *(int *)data;
5637 int level;
5638
5639 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5640 level = TEMPLATE_PARM_LEVEL (t);
5641 else
5642 level = TEMPLATE_TYPE_LEVEL (t);
5643 return level == this_level;
5644 }
5645
5646 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5647 DATA is really an int, indicating the innermost outer level of parameters.
5648 If T is a template parameter of that level or further out, return
5649 nonzero. */
5650
5651 static int
5652 template_parm_outer_level (tree t, void *data)
5653 {
5654 int this_level = *(int *)data;
5655 int level;
5656
5657 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5658 level = TEMPLATE_PARM_LEVEL (t);
5659 else
5660 level = TEMPLATE_TYPE_LEVEL (t);
5661 return level <= this_level;
5662 }
5663
5664 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5665 parameters given by current_template_args, or reuses a
5666 previously existing one, if appropriate. Returns the DECL, or an
5667 equivalent one, if it is replaced via a call to duplicate_decls.
5668
5669 If IS_FRIEND is true, DECL is a friend declaration. */
5670
5671 tree
5672 push_template_decl_real (tree decl, bool is_friend)
5673 {
5674 tree tmpl;
5675 tree args;
5676 tree info;
5677 tree ctx;
5678 bool is_primary;
5679 bool is_partial;
5680 int new_template_p = 0;
5681 /* True if the template is a member template, in the sense of
5682 [temp.mem]. */
5683 bool member_template_p = false;
5684
5685 if (decl == error_mark_node || !current_template_parms)
5686 return error_mark_node;
5687
5688 /* See if this is a partial specialization. */
5689 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5690 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5691 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5692 || (VAR_P (decl)
5693 && DECL_LANG_SPECIFIC (decl)
5694 && DECL_TEMPLATE_SPECIALIZATION (decl)
5695 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5696
5697 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5698 is_friend = true;
5699
5700 if (is_friend)
5701 /* For a friend, we want the context of the friend, not
5702 the type of which it is a friend. */
5703 ctx = CP_DECL_CONTEXT (decl);
5704 else if (CP_DECL_CONTEXT (decl)
5705 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5706 /* In the case of a virtual function, we want the class in which
5707 it is defined. */
5708 ctx = CP_DECL_CONTEXT (decl);
5709 else
5710 /* Otherwise, if we're currently defining some class, the DECL
5711 is assumed to be a member of the class. */
5712 ctx = current_scope ();
5713
5714 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5715 ctx = NULL_TREE;
5716
5717 if (!DECL_CONTEXT (decl))
5718 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5719
5720 /* See if this is a primary template. */
5721 if (is_friend && ctx
5722 && uses_template_parms_level (ctx, processing_template_decl))
5723 /* A friend template that specifies a class context, i.e.
5724 template <typename T> friend void A<T>::f();
5725 is not primary. */
5726 is_primary = false;
5727 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5728 is_primary = false;
5729 else
5730 is_primary = template_parm_scope_p ();
5731
5732 if (is_primary)
5733 {
5734 warning (OPT_Wtemplates, "template %qD declared", decl);
5735
5736 if (DECL_CLASS_SCOPE_P (decl))
5737 member_template_p = true;
5738 if (TREE_CODE (decl) == TYPE_DECL
5739 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5740 {
5741 error ("template class without a name");
5742 return error_mark_node;
5743 }
5744 else if (TREE_CODE (decl) == FUNCTION_DECL)
5745 {
5746 if (member_template_p)
5747 {
5748 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5749 error ("member template %qD may not have virt-specifiers", decl);
5750 }
5751 if (DECL_DESTRUCTOR_P (decl))
5752 {
5753 /* [temp.mem]
5754
5755 A destructor shall not be a member template. */
5756 error_at (DECL_SOURCE_LOCATION (decl),
5757 "destructor %qD declared as member template", decl);
5758 return error_mark_node;
5759 }
5760 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5761 && (!prototype_p (TREE_TYPE (decl))
5762 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5763 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5764 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5765 == void_list_node)))
5766 {
5767 /* [basic.stc.dynamic.allocation]
5768
5769 An allocation function can be a function
5770 template. ... Template allocation functions shall
5771 have two or more parameters. */
5772 error ("invalid template declaration of %qD", decl);
5773 return error_mark_node;
5774 }
5775 }
5776 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5777 && CLASS_TYPE_P (TREE_TYPE (decl)))
5778 {
5779 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5780 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5781 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5782 {
5783 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5784 if (TREE_CODE (t) == TYPE_DECL)
5785 t = TREE_TYPE (t);
5786 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5787 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5788 }
5789 }
5790 else if (TREE_CODE (decl) == TYPE_DECL
5791 && TYPE_DECL_ALIAS_P (decl))
5792 /* alias-declaration */
5793 gcc_assert (!DECL_ARTIFICIAL (decl));
5794 else if (VAR_P (decl))
5795 /* C++14 variable template. */;
5796 else if (TREE_CODE (decl) == CONCEPT_DECL)
5797 /* C++20 concept definitions. */;
5798 else
5799 {
5800 error ("template declaration of %q#D", decl);
5801 return error_mark_node;
5802 }
5803 }
5804
5805 /* Check to see that the rules regarding the use of default
5806 arguments are not being violated. We check args for a friend
5807 functions when we know whether it's a definition, introducing
5808 declaration or re-declaration. */
5809 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5810 check_default_tmpl_args (decl, current_template_parms,
5811 is_primary, is_partial, is_friend);
5812
5813 /* Ensure that there are no parameter packs in the type of this
5814 declaration that have not been expanded. */
5815 if (TREE_CODE (decl) == FUNCTION_DECL)
5816 {
5817 /* Check each of the arguments individually to see if there are
5818 any bare parameter packs. */
5819 tree type = TREE_TYPE (decl);
5820 tree arg = DECL_ARGUMENTS (decl);
5821 tree argtype = TYPE_ARG_TYPES (type);
5822
5823 while (arg && argtype)
5824 {
5825 if (!DECL_PACK_P (arg)
5826 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5827 {
5828 /* This is a PARM_DECL that contains unexpanded parameter
5829 packs. We have already complained about this in the
5830 check_for_bare_parameter_packs call, so just replace
5831 these types with ERROR_MARK_NODE. */
5832 TREE_TYPE (arg) = error_mark_node;
5833 TREE_VALUE (argtype) = error_mark_node;
5834 }
5835
5836 arg = DECL_CHAIN (arg);
5837 argtype = TREE_CHAIN (argtype);
5838 }
5839
5840 /* Check for bare parameter packs in the return type and the
5841 exception specifiers. */
5842 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5843 /* Errors were already issued, set return type to int
5844 as the frontend doesn't expect error_mark_node as
5845 the return type. */
5846 TREE_TYPE (type) = integer_type_node;
5847 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5848 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5849 }
5850 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5851 ? DECL_ORIGINAL_TYPE (decl)
5852 : TREE_TYPE (decl)))
5853 {
5854 TREE_TYPE (decl) = error_mark_node;
5855 return error_mark_node;
5856 }
5857
5858 if (is_partial)
5859 return process_partial_specialization (decl);
5860
5861 args = current_template_args ();
5862
5863 if (!ctx
5864 || TREE_CODE (ctx) == FUNCTION_DECL
5865 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5866 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5867 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5868 {
5869 if (DECL_LANG_SPECIFIC (decl)
5870 && DECL_TEMPLATE_INFO (decl)
5871 && DECL_TI_TEMPLATE (decl))
5872 tmpl = DECL_TI_TEMPLATE (decl);
5873 /* If DECL is a TYPE_DECL for a class-template, then there won't
5874 be DECL_LANG_SPECIFIC. The information equivalent to
5875 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5876 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5877 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5878 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5879 {
5880 /* Since a template declaration already existed for this
5881 class-type, we must be redeclaring it here. Make sure
5882 that the redeclaration is valid. */
5883 redeclare_class_template (TREE_TYPE (decl),
5884 current_template_parms,
5885 current_template_constraints ());
5886 /* We don't need to create a new TEMPLATE_DECL; just use the
5887 one we already had. */
5888 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5889 }
5890 else
5891 {
5892 tmpl = build_template_decl (decl, current_template_parms,
5893 member_template_p);
5894 new_template_p = 1;
5895
5896 if (DECL_LANG_SPECIFIC (decl)
5897 && DECL_TEMPLATE_SPECIALIZATION (decl))
5898 {
5899 /* A specialization of a member template of a template
5900 class. */
5901 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5902 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5903 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5904 }
5905 }
5906 }
5907 else
5908 {
5909 tree a, t, current, parms;
5910 int i;
5911 tree tinfo = get_template_info (decl);
5912
5913 if (!tinfo)
5914 {
5915 error ("template definition of non-template %q#D", decl);
5916 return error_mark_node;
5917 }
5918
5919 tmpl = TI_TEMPLATE (tinfo);
5920
5921 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5922 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5923 && DECL_TEMPLATE_SPECIALIZATION (decl)
5924 && DECL_MEMBER_TEMPLATE_P (tmpl))
5925 {
5926 tree new_tmpl;
5927
5928 /* The declaration is a specialization of a member
5929 template, declared outside the class. Therefore, the
5930 innermost template arguments will be NULL, so we
5931 replace them with the arguments determined by the
5932 earlier call to check_explicit_specialization. */
5933 args = DECL_TI_ARGS (decl);
5934
5935 new_tmpl
5936 = build_template_decl (decl, current_template_parms,
5937 member_template_p);
5938 DECL_TI_TEMPLATE (decl) = new_tmpl;
5939 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5940 DECL_TEMPLATE_INFO (new_tmpl)
5941 = build_template_info (tmpl, args);
5942
5943 register_specialization (new_tmpl,
5944 most_general_template (tmpl),
5945 args,
5946 is_friend, 0);
5947 return decl;
5948 }
5949
5950 /* Make sure the template headers we got make sense. */
5951
5952 parms = DECL_TEMPLATE_PARMS (tmpl);
5953 i = TMPL_PARMS_DEPTH (parms);
5954 if (TMPL_ARGS_DEPTH (args) != i)
5955 {
5956 error ("expected %d levels of template parms for %q#D, got %d",
5957 i, decl, TMPL_ARGS_DEPTH (args));
5958 DECL_INTERFACE_KNOWN (decl) = 1;
5959 return error_mark_node;
5960 }
5961 else
5962 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5963 {
5964 a = TMPL_ARGS_LEVEL (args, i);
5965 t = INNERMOST_TEMPLATE_PARMS (parms);
5966
5967 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5968 {
5969 if (current == decl)
5970 error ("got %d template parameters for %q#D",
5971 TREE_VEC_LENGTH (a), decl);
5972 else
5973 error ("got %d template parameters for %q#T",
5974 TREE_VEC_LENGTH (a), current);
5975 error (" but %d required", TREE_VEC_LENGTH (t));
5976 /* Avoid crash in import_export_decl. */
5977 DECL_INTERFACE_KNOWN (decl) = 1;
5978 return error_mark_node;
5979 }
5980
5981 if (current == decl)
5982 current = ctx;
5983 else if (current == NULL_TREE)
5984 /* Can happen in erroneous input. */
5985 break;
5986 else
5987 current = get_containing_scope (current);
5988 }
5989
5990 /* Check that the parms are used in the appropriate qualifying scopes
5991 in the declarator. */
5992 if (!comp_template_args
5993 (TI_ARGS (tinfo),
5994 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5995 {
5996 error ("template arguments to %qD do not match original "
5997 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5998 if (!uses_template_parms (TI_ARGS (tinfo)))
5999 inform (input_location, "use %<template<>%> for"
6000 " an explicit specialization");
6001 /* Avoid crash in import_export_decl. */
6002 DECL_INTERFACE_KNOWN (decl) = 1;
6003 return error_mark_node;
6004 }
6005 }
6006
6007 gcc_checking_assert (DECL_TEMPLATE_RESULT (tmpl) == decl);
6008
6009 if (new_template_p)
6010 {
6011 /* Push template declarations for global functions and types.
6012 Note that we do not try to push a global template friend
6013 declared in a template class; such a thing may well depend on
6014 the template parameters of the class and we'll push it when
6015 instantiating the befriending class. */
6016 if (!ctx
6017 && !(is_friend && template_class_depth (current_class_type) > 0))
6018 {
6019 tmpl = pushdecl_namespace_level (tmpl, is_friend);
6020 if (tmpl == error_mark_node)
6021 return error_mark_node;
6022
6023 /* Hide template friend classes that haven't been declared yet. */
6024 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
6025 {
6026 DECL_ANTICIPATED (tmpl) = 1;
6027 DECL_FRIEND_P (tmpl) = 1;
6028 }
6029 }
6030 }
6031 else
6032 /* The type may have been completed, or (erroneously) changed. */
6033 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6034
6035 if (is_primary)
6036 {
6037 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6038
6039 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6040
6041 /* Give template template parms a DECL_CONTEXT of the template
6042 for which they are a parameter. */
6043 parms = INNERMOST_TEMPLATE_PARMS (parms);
6044 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6045 {
6046 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6047 if (TREE_CODE (parm) == TEMPLATE_DECL)
6048 DECL_CONTEXT (parm) = tmpl;
6049 }
6050
6051 if (TREE_CODE (decl) == TYPE_DECL
6052 && TYPE_DECL_ALIAS_P (decl))
6053 {
6054 if (tree constr
6055 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6056 {
6057 /* ??? Why don't we do this here for all templates? */
6058 constr = build_constraints (constr, NULL_TREE);
6059 set_constraints (decl, constr);
6060 }
6061 if (complex_alias_template_p (tmpl))
6062 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6063 }
6064 }
6065
6066 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6067 back to its most general template. If TMPL is a specialization,
6068 ARGS may only have the innermost set of arguments. Add the missing
6069 argument levels if necessary. */
6070 if (DECL_TEMPLATE_INFO (tmpl))
6071 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6072
6073 info = build_template_info (tmpl, args);
6074
6075 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6076 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6077 else
6078 {
6079 if (is_primary)
6080 retrofit_lang_decl (decl);
6081 if (DECL_LANG_SPECIFIC (decl)
6082 && !(VAR_OR_FUNCTION_DECL_P (decl)
6083 && DECL_LOCAL_DECL_P (decl)))
6084 DECL_TEMPLATE_INFO (decl) = info;
6085 }
6086
6087 if (flag_implicit_templates
6088 && !is_friend
6089 && TREE_PUBLIC (decl)
6090 && VAR_OR_FUNCTION_DECL_P (decl))
6091 /* Set DECL_COMDAT on template instantiations; if we force
6092 them to be emitted by explicit instantiation,
6093 mark_needed will tell cgraph to do the right thing. */
6094 DECL_COMDAT (decl) = true;
6095
6096 return DECL_TEMPLATE_RESULT (tmpl);
6097 }
6098
6099 tree
6100 push_template_decl (tree decl)
6101 {
6102 return push_template_decl_real (decl, false);
6103 }
6104
6105 /* FN is an inheriting constructor that inherits from the constructor
6106 template INHERITED; turn FN into a constructor template with a matching
6107 template header. */
6108
6109 tree
6110 add_inherited_template_parms (tree fn, tree inherited)
6111 {
6112 tree inner_parms
6113 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6114 inner_parms = copy_node (inner_parms);
6115 tree parms
6116 = tree_cons (size_int (processing_template_decl + 1),
6117 inner_parms, current_template_parms);
6118 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6119 tree args = template_parms_to_args (parms);
6120 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6121 DECL_ARTIFICIAL (tmpl) = true;
6122 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6123 return tmpl;
6124 }
6125
6126 /* Called when a class template TYPE is redeclared with the indicated
6127 template PARMS, e.g.:
6128
6129 template <class T> struct S;
6130 template <class T> struct S {}; */
6131
6132 bool
6133 redeclare_class_template (tree type, tree parms, tree cons)
6134 {
6135 tree tmpl;
6136 tree tmpl_parms;
6137 int i;
6138
6139 if (!TYPE_TEMPLATE_INFO (type))
6140 {
6141 error ("%qT is not a template type", type);
6142 return false;
6143 }
6144
6145 tmpl = TYPE_TI_TEMPLATE (type);
6146 if (!PRIMARY_TEMPLATE_P (tmpl))
6147 /* The type is nested in some template class. Nothing to worry
6148 about here; there are no new template parameters for the nested
6149 type. */
6150 return true;
6151
6152 if (!parms)
6153 {
6154 error ("template specifiers not specified in declaration of %qD",
6155 tmpl);
6156 return false;
6157 }
6158
6159 parms = INNERMOST_TEMPLATE_PARMS (parms);
6160 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6161
6162 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6163 {
6164 error_n (input_location, TREE_VEC_LENGTH (parms),
6165 "redeclared with %d template parameter",
6166 "redeclared with %d template parameters",
6167 TREE_VEC_LENGTH (parms));
6168 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6169 "previous declaration %qD used %d template parameter",
6170 "previous declaration %qD used %d template parameters",
6171 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6172 return false;
6173 }
6174
6175 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6176 {
6177 tree tmpl_parm;
6178 tree parm;
6179 tree tmpl_default;
6180 tree parm_default;
6181
6182 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6183 || TREE_VEC_ELT (parms, i) == error_mark_node)
6184 continue;
6185
6186 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6187 if (error_operand_p (tmpl_parm))
6188 return false;
6189
6190 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6191 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6192 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6193
6194 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6195 TEMPLATE_DECL. */
6196 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6197 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6198 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6199 || (TREE_CODE (tmpl_parm) != PARM_DECL
6200 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6201 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6202 || (TREE_CODE (tmpl_parm) == PARM_DECL
6203 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6204 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6205 {
6206 auto_diagnostic_group d;
6207 error ("template parameter %q+#D", tmpl_parm);
6208 inform (input_location, "redeclared here as %q#D", parm);
6209 return false;
6210 }
6211
6212 /* The parameters can be declared to introduce different
6213 constraints. */
6214 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6215 tree p2 = TREE_VEC_ELT (parms, i);
6216 if (!template_parameter_constraints_equivalent_p (p1, p2))
6217 {
6218 auto_diagnostic_group d;
6219 error ("declaration of template parameter %q+#D with different "
6220 "constraints", parm);
6221 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6222 "original declaration appeared here");
6223 return false;
6224 }
6225
6226 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6227 {
6228 /* We have in [temp.param]:
6229
6230 A template-parameter may not be given default arguments
6231 by two different declarations in the same scope. */
6232 auto_diagnostic_group d;
6233 error_at (input_location, "redefinition of default argument for %q#D", parm);
6234 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6235 "original definition appeared here");
6236 return false;
6237 }
6238
6239 if (parm_default != NULL_TREE)
6240 /* Update the previous template parameters (which are the ones
6241 that will really count) with the new default value. */
6242 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6243 else if (tmpl_default != NULL_TREE)
6244 /* Update the new parameters, too; they'll be used as the
6245 parameters for any members. */
6246 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6247
6248 /* Give each template template parm in this redeclaration a
6249 DECL_CONTEXT of the template for which they are a parameter. */
6250 if (TREE_CODE (parm) == TEMPLATE_DECL)
6251 {
6252 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6253 DECL_CONTEXT (parm) = tmpl;
6254 }
6255
6256 if (TREE_CODE (parm) == TYPE_DECL)
6257 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6258 }
6259
6260 tree ci = get_constraints (tmpl);
6261 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6262 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6263
6264 /* Two classes with different constraints declare different entities. */
6265 if (!cp_tree_equal (req1, req2))
6266 {
6267 auto_diagnostic_group d;
6268 error_at (input_location, "redeclaration %q#D with different "
6269 "constraints", tmpl);
6270 inform (DECL_SOURCE_LOCATION (tmpl),
6271 "original declaration appeared here");
6272 return false;
6273 }
6274
6275 return true;
6276 }
6277
6278 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6279 to be used when the caller has already checked
6280 (processing_template_decl
6281 && !instantiation_dependent_expression_p (expr)
6282 && potential_constant_expression (expr))
6283 and cleared processing_template_decl. */
6284
6285 tree
6286 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6287 {
6288 return tsubst_copy_and_build (expr,
6289 /*args=*/NULL_TREE,
6290 complain,
6291 /*in_decl=*/NULL_TREE,
6292 /*function_p=*/false,
6293 /*integral_constant_expression_p=*/true);
6294 }
6295
6296 /* Simplify EXPR if it is a non-dependent expression. Returns the
6297 (possibly simplified) expression. */
6298
6299 tree
6300 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6301 {
6302 if (expr == NULL_TREE)
6303 return NULL_TREE;
6304
6305 /* If we're in a template, but EXPR isn't value dependent, simplify
6306 it. We're supposed to treat:
6307
6308 template <typename T> void f(T[1 + 1]);
6309 template <typename T> void f(T[2]);
6310
6311 as two declarations of the same function, for example. */
6312 if (processing_template_decl
6313 && is_nondependent_constant_expression (expr))
6314 {
6315 processing_template_decl_sentinel s;
6316 expr = instantiate_non_dependent_expr_internal (expr, complain);
6317 }
6318 return expr;
6319 }
6320
6321 tree
6322 instantiate_non_dependent_expr (tree expr)
6323 {
6324 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6325 }
6326
6327 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6328 an uninstantiated expression. */
6329
6330 tree
6331 instantiate_non_dependent_or_null (tree expr)
6332 {
6333 if (expr == NULL_TREE)
6334 return NULL_TREE;
6335 if (processing_template_decl)
6336 {
6337 if (!is_nondependent_constant_expression (expr))
6338 expr = NULL_TREE;
6339 else
6340 {
6341 processing_template_decl_sentinel s;
6342 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6343 }
6344 }
6345 return expr;
6346 }
6347
6348 /* True iff T is a specialization of a variable template. */
6349
6350 bool
6351 variable_template_specialization_p (tree t)
6352 {
6353 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6354 return false;
6355 tree tmpl = DECL_TI_TEMPLATE (t);
6356 return variable_template_p (tmpl);
6357 }
6358
6359 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6360 template declaration, or a TYPE_DECL for an alias declaration. */
6361
6362 bool
6363 alias_type_or_template_p (tree t)
6364 {
6365 if (t == NULL_TREE)
6366 return false;
6367 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6368 || (TYPE_P (t)
6369 && TYPE_NAME (t)
6370 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6371 || DECL_ALIAS_TEMPLATE_P (t));
6372 }
6373
6374 /* If T is a specialization of an alias template, return it; otherwise return
6375 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6376
6377 tree
6378 alias_template_specialization_p (const_tree t,
6379 bool transparent_typedefs)
6380 {
6381 if (!TYPE_P (t))
6382 return NULL_TREE;
6383
6384 /* It's an alias template specialization if it's an alias and its
6385 TYPE_NAME is a specialization of a primary template. */
6386 if (typedef_variant_p (t))
6387 {
6388 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6389 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6390 return CONST_CAST_TREE (t);
6391 if (transparent_typedefs)
6392 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6393 (TYPE_NAME (t)),
6394 transparent_typedefs);
6395 }
6396
6397 return NULL_TREE;
6398 }
6399
6400 /* An alias template is complex from a SFINAE perspective if a template-id
6401 using that alias can be ill-formed when the expansion is not, as with
6402 the void_t template. We determine this by checking whether the
6403 expansion for the alias template uses all its template parameters. */
6404
6405 struct uses_all_template_parms_data
6406 {
6407 int level;
6408 bool *seen;
6409 };
6410
6411 static int
6412 uses_all_template_parms_r (tree t, void *data_)
6413 {
6414 struct uses_all_template_parms_data &data
6415 = *(struct uses_all_template_parms_data*)data_;
6416 tree idx = get_template_parm_index (t);
6417
6418 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6419 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6420 return 0;
6421 }
6422
6423 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6424
6425 static int
6426 complex_pack_expansion_r (tree t, void *data_)
6427 {
6428 /* An alias template with a pack expansion that expands a pack from the
6429 enclosing class needs to be considered complex, to avoid confusion with
6430 the same pack being used as an argument to the alias's own template
6431 parameter (91966). */
6432 if (!PACK_EXPANSION_P (t))
6433 return 0;
6434 struct uses_all_template_parms_data &data
6435 = *(struct uses_all_template_parms_data*)data_;
6436 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6437 pack = TREE_CHAIN (pack))
6438 {
6439 tree parm_pack = TREE_VALUE (pack);
6440 if (!TEMPLATE_PARM_P (parm_pack))
6441 continue;
6442 int idx, level;
6443 template_parm_level_and_index (parm_pack, &level, &idx);
6444 if (level < data.level)
6445 return 1;
6446 }
6447 return 0;
6448 }
6449
6450 static bool
6451 complex_alias_template_p (const_tree tmpl)
6452 {
6453 /* A renaming alias isn't complex. */
6454 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6455 return false;
6456
6457 /* Any other constrained alias is complex. */
6458 if (get_constraints (tmpl))
6459 return true;
6460
6461 struct uses_all_template_parms_data data;
6462 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6463 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6464 data.level = TMPL_PARMS_DEPTH (parms);
6465 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6466 data.seen = XALLOCAVEC (bool, len);
6467 for (int i = 0; i < len; ++i)
6468 data.seen[i] = false;
6469
6470 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6471 NULL, true, complex_pack_expansion_r))
6472 return true;
6473 for (int i = 0; i < len; ++i)
6474 if (!data.seen[i])
6475 return true;
6476 return false;
6477 }
6478
6479 /* If T is a specialization of a complex alias template with dependent
6480 template-arguments, return it; otherwise return NULL_TREE. If T is a
6481 typedef to such a specialization, return the specialization. */
6482
6483 tree
6484 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6485 {
6486 if (!TYPE_P (t) || !typedef_variant_p (t))
6487 return NULL_TREE;
6488
6489 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6490 if (tinfo
6491 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6492 && (any_dependent_template_arguments_p
6493 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6494 return CONST_CAST_TREE (t);
6495
6496 if (transparent_typedefs)
6497 {
6498 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6499 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6500 }
6501
6502 return NULL_TREE;
6503 }
6504
6505 /* Return the number of innermost template parameters in TMPL. */
6506
6507 static int
6508 num_innermost_template_parms (const_tree tmpl)
6509 {
6510 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6511 return TREE_VEC_LENGTH (parms);
6512 }
6513
6514 /* Return either TMPL or another template that it is equivalent to under DR
6515 1286: An alias that just changes the name of a template is equivalent to
6516 the other template. */
6517
6518 static tree
6519 get_underlying_template (tree tmpl)
6520 {
6521 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6522 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6523 {
6524 /* Determine if the alias is equivalent to an underlying template. */
6525 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6526 /* The underlying type may have been ill-formed. Don't proceed. */
6527 if (!orig_type)
6528 break;
6529 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6530 if (!tinfo)
6531 break;
6532
6533 tree underlying = TI_TEMPLATE (tinfo);
6534 if (!PRIMARY_TEMPLATE_P (underlying)
6535 || (num_innermost_template_parms (tmpl)
6536 != num_innermost_template_parms (underlying)))
6537 break;
6538
6539 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6540 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6541 break;
6542
6543 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6544 it's appropriate to treat a less-constrained alias as equivalent. */
6545 if (!at_least_as_constrained (underlying, tmpl))
6546 break;
6547
6548 /* Alias is equivalent. Strip it and repeat. */
6549 tmpl = underlying;
6550 }
6551
6552 return tmpl;
6553 }
6554
6555 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6556 must be a reference-to-function or a pointer-to-function type, as specified
6557 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6558 and check that the resulting function has external linkage. */
6559
6560 static tree
6561 convert_nontype_argument_function (tree type, tree expr,
6562 tsubst_flags_t complain)
6563 {
6564 tree fns = expr;
6565 tree fn, fn_no_ptr;
6566 linkage_kind linkage;
6567
6568 fn = instantiate_type (type, fns, tf_none);
6569 if (fn == error_mark_node)
6570 return error_mark_node;
6571
6572 if (value_dependent_expression_p (fn))
6573 goto accept;
6574
6575 fn_no_ptr = strip_fnptr_conv (fn);
6576 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6577 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6578 if (BASELINK_P (fn_no_ptr))
6579 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6580
6581 /* [temp.arg.nontype]/1
6582
6583 A template-argument for a non-type, non-template template-parameter
6584 shall be one of:
6585 [...]
6586 -- the address of an object or function with external [C++11: or
6587 internal] linkage. */
6588
6589 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6590 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6591 {
6592 if (complain & tf_error)
6593 {
6594 location_t loc = cp_expr_loc_or_input_loc (expr);
6595 error_at (loc, "%qE is not a valid template argument for type %qT",
6596 expr, type);
6597 if (TYPE_PTR_P (type))
6598 inform (loc, "it must be the address of a function "
6599 "with external linkage");
6600 else
6601 inform (loc, "it must be the name of a function with "
6602 "external linkage");
6603 }
6604 return NULL_TREE;
6605 }
6606
6607 linkage = decl_linkage (fn_no_ptr);
6608 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6609 {
6610 if (complain & tf_error)
6611 {
6612 location_t loc = cp_expr_loc_or_input_loc (expr);
6613 if (cxx_dialect >= cxx11)
6614 error_at (loc, "%qE is not a valid template argument for type "
6615 "%qT because %qD has no linkage",
6616 expr, type, fn_no_ptr);
6617 else
6618 error_at (loc, "%qE is not a valid template argument for type "
6619 "%qT because %qD does not have external linkage",
6620 expr, type, fn_no_ptr);
6621 }
6622 return NULL_TREE;
6623 }
6624
6625 accept:
6626 if (TYPE_REF_P (type))
6627 {
6628 if (REFERENCE_REF_P (fn))
6629 fn = TREE_OPERAND (fn, 0);
6630 else
6631 fn = build_address (fn);
6632 }
6633 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6634 fn = build_nop (type, fn);
6635
6636 return fn;
6637 }
6638
6639 /* Subroutine of convert_nontype_argument.
6640 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6641 Emit an error otherwise. */
6642
6643 static bool
6644 check_valid_ptrmem_cst_expr (tree type, tree expr,
6645 tsubst_flags_t complain)
6646 {
6647 tree orig_expr = expr;
6648 STRIP_NOPS (expr);
6649 if (null_ptr_cst_p (expr))
6650 return true;
6651 if (TREE_CODE (expr) == PTRMEM_CST
6652 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6653 PTRMEM_CST_CLASS (expr)))
6654 return true;
6655 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6656 return true;
6657 if (processing_template_decl
6658 && TREE_CODE (expr) == ADDR_EXPR
6659 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6660 return true;
6661 if (complain & tf_error)
6662 {
6663 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6664 error_at (loc, "%qE is not a valid template argument for type %qT",
6665 orig_expr, type);
6666 if (TREE_CODE (expr) != PTRMEM_CST)
6667 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6668 else
6669 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6670 }
6671 return false;
6672 }
6673
6674 /* Returns TRUE iff the address of OP is value-dependent.
6675
6676 14.6.2.4 [temp.dep.temp]:
6677 A non-integral non-type template-argument is dependent if its type is
6678 dependent or it has either of the following forms
6679 qualified-id
6680 & qualified-id
6681 and contains a nested-name-specifier which specifies a class-name that
6682 names a dependent type.
6683
6684 We generalize this to just say that the address of a member of a
6685 dependent class is value-dependent; the above doesn't cover the
6686 address of a static data member named with an unqualified-id. */
6687
6688 static bool
6689 has_value_dependent_address (tree op)
6690 {
6691 STRIP_ANY_LOCATION_WRAPPER (op);
6692
6693 /* We could use get_inner_reference here, but there's no need;
6694 this is only relevant for template non-type arguments, which
6695 can only be expressed as &id-expression. */
6696 if (DECL_P (op))
6697 {
6698 tree ctx = CP_DECL_CONTEXT (op);
6699 if (TYPE_P (ctx) && dependent_type_p (ctx))
6700 return true;
6701 }
6702
6703 return false;
6704 }
6705
6706 /* The next set of functions are used for providing helpful explanatory
6707 diagnostics for failed overload resolution. Their messages should be
6708 indented by two spaces for consistency with the messages in
6709 call.c */
6710
6711 static int
6712 unify_success (bool /*explain_p*/)
6713 {
6714 return 0;
6715 }
6716
6717 /* Other failure functions should call this one, to provide a single function
6718 for setting a breakpoint on. */
6719
6720 static int
6721 unify_invalid (bool /*explain_p*/)
6722 {
6723 return 1;
6724 }
6725
6726 static int
6727 unify_parameter_deduction_failure (bool explain_p, tree parm)
6728 {
6729 if (explain_p)
6730 inform (input_location,
6731 " couldn%'t deduce template parameter %qD", parm);
6732 return unify_invalid (explain_p);
6733 }
6734
6735 static int
6736 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6737 {
6738 if (explain_p)
6739 inform (input_location,
6740 " types %qT and %qT have incompatible cv-qualifiers",
6741 parm, arg);
6742 return unify_invalid (explain_p);
6743 }
6744
6745 static int
6746 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6747 {
6748 if (explain_p)
6749 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6750 return unify_invalid (explain_p);
6751 }
6752
6753 static int
6754 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6755 {
6756 if (explain_p)
6757 inform (input_location,
6758 " template parameter %qD is not a parameter pack, but "
6759 "argument %qD is",
6760 parm, arg);
6761 return unify_invalid (explain_p);
6762 }
6763
6764 static int
6765 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6766 {
6767 if (explain_p)
6768 inform (input_location,
6769 " template argument %qE does not match "
6770 "pointer-to-member constant %qE",
6771 arg, parm);
6772 return unify_invalid (explain_p);
6773 }
6774
6775 static int
6776 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6777 {
6778 if (explain_p)
6779 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6780 return unify_invalid (explain_p);
6781 }
6782
6783 static int
6784 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6785 {
6786 if (explain_p)
6787 inform (input_location,
6788 " inconsistent parameter pack deduction with %qT and %qT",
6789 old_arg, new_arg);
6790 return unify_invalid (explain_p);
6791 }
6792
6793 static int
6794 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6795 {
6796 if (explain_p)
6797 {
6798 if (TYPE_P (parm))
6799 inform (input_location,
6800 " deduced conflicting types for parameter %qT (%qT and %qT)",
6801 parm, first, second);
6802 else
6803 inform (input_location,
6804 " deduced conflicting values for non-type parameter "
6805 "%qE (%qE and %qE)", parm, first, second);
6806 }
6807 return unify_invalid (explain_p);
6808 }
6809
6810 static int
6811 unify_vla_arg (bool explain_p, tree arg)
6812 {
6813 if (explain_p)
6814 inform (input_location,
6815 " variable-sized array type %qT is not "
6816 "a valid template argument",
6817 arg);
6818 return unify_invalid (explain_p);
6819 }
6820
6821 static int
6822 unify_method_type_error (bool explain_p, tree arg)
6823 {
6824 if (explain_p)
6825 inform (input_location,
6826 " member function type %qT is not a valid template argument",
6827 arg);
6828 return unify_invalid (explain_p);
6829 }
6830
6831 static int
6832 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6833 {
6834 if (explain_p)
6835 {
6836 if (least_p)
6837 inform_n (input_location, wanted,
6838 " candidate expects at least %d argument, %d provided",
6839 " candidate expects at least %d arguments, %d provided",
6840 wanted, have);
6841 else
6842 inform_n (input_location, wanted,
6843 " candidate expects %d argument, %d provided",
6844 " candidate expects %d arguments, %d provided",
6845 wanted, have);
6846 }
6847 return unify_invalid (explain_p);
6848 }
6849
6850 static int
6851 unify_too_many_arguments (bool explain_p, int have, int wanted)
6852 {
6853 return unify_arity (explain_p, have, wanted);
6854 }
6855
6856 static int
6857 unify_too_few_arguments (bool explain_p, int have, int wanted,
6858 bool least_p = false)
6859 {
6860 return unify_arity (explain_p, have, wanted, least_p);
6861 }
6862
6863 static int
6864 unify_arg_conversion (bool explain_p, tree to_type,
6865 tree from_type, tree arg)
6866 {
6867 if (explain_p)
6868 inform (cp_expr_loc_or_input_loc (arg),
6869 " cannot convert %qE (type %qT) to type %qT",
6870 arg, from_type, to_type);
6871 return unify_invalid (explain_p);
6872 }
6873
6874 static int
6875 unify_no_common_base (bool explain_p, enum template_base_result r,
6876 tree parm, tree arg)
6877 {
6878 if (explain_p)
6879 switch (r)
6880 {
6881 case tbr_ambiguous_baseclass:
6882 inform (input_location, " %qT is an ambiguous base class of %qT",
6883 parm, arg);
6884 break;
6885 default:
6886 inform (input_location, " %qT is not derived from %qT", arg, parm);
6887 break;
6888 }
6889 return unify_invalid (explain_p);
6890 }
6891
6892 static int
6893 unify_inconsistent_template_template_parameters (bool explain_p)
6894 {
6895 if (explain_p)
6896 inform (input_location,
6897 " template parameters of a template template argument are "
6898 "inconsistent with other deduced template arguments");
6899 return unify_invalid (explain_p);
6900 }
6901
6902 static int
6903 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6904 {
6905 if (explain_p)
6906 inform (input_location,
6907 " cannot deduce a template for %qT from non-template type %qT",
6908 parm, arg);
6909 return unify_invalid (explain_p);
6910 }
6911
6912 static int
6913 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6914 {
6915 if (explain_p)
6916 inform (input_location,
6917 " template argument %qE does not match %qE", arg, parm);
6918 return unify_invalid (explain_p);
6919 }
6920
6921 /* True if T is a C++20 template parameter object to store the argument for a
6922 template parameter of class type. */
6923
6924 bool
6925 template_parm_object_p (const_tree t)
6926 {
6927 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6928 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6929 }
6930
6931 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6932 argument for TYPE, points to an unsuitable object.
6933
6934 Also adjust the type of the index in C++20 array subobject references. */
6935
6936 static bool
6937 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6938 {
6939 switch (TREE_CODE (expr))
6940 {
6941 CASE_CONVERT:
6942 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6943 complain);
6944
6945 case TARGET_EXPR:
6946 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6947 complain);
6948
6949 case CONSTRUCTOR:
6950 {
6951 unsigned i; tree elt;
6952 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6953 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6954 return true;
6955 }
6956 break;
6957
6958 case ADDR_EXPR:
6959 {
6960 tree decl = TREE_OPERAND (expr, 0);
6961
6962 if (cxx_dialect >= cxx20)
6963 while (TREE_CODE (decl) == COMPONENT_REF
6964 || TREE_CODE (decl) == ARRAY_REF)
6965 {
6966 tree &op = TREE_OPERAND (decl, 1);
6967 if (TREE_CODE (decl) == ARRAY_REF
6968 && TREE_CODE (op) == INTEGER_CST)
6969 /* Canonicalize array offsets to ptrdiff_t; how they were
6970 written doesn't matter for subobject identity. */
6971 op = fold_convert (ptrdiff_type_node, op);
6972 decl = TREE_OPERAND (decl, 0);
6973 }
6974
6975 if (!VAR_P (decl))
6976 {
6977 if (complain & tf_error)
6978 error_at (cp_expr_loc_or_input_loc (expr),
6979 "%qE is not a valid template argument of type %qT "
6980 "because %qE is not a variable", expr, type, decl);
6981 return true;
6982 }
6983 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6984 {
6985 if (complain & tf_error)
6986 error_at (cp_expr_loc_or_input_loc (expr),
6987 "%qE is not a valid template argument of type %qT "
6988 "in C++98 because %qD does not have external linkage",
6989 expr, type, decl);
6990 return true;
6991 }
6992 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6993 && decl_linkage (decl) == lk_none)
6994 {
6995 if (complain & tf_error)
6996 error_at (cp_expr_loc_or_input_loc (expr),
6997 "%qE is not a valid template argument of type %qT "
6998 "because %qD has no linkage", expr, type, decl);
6999 return true;
7000 }
7001 /* C++17: For a non-type template-parameter of reference or pointer
7002 type, the value of the constant expression shall not refer to (or
7003 for a pointer type, shall not be the address of):
7004 * a subobject (4.5),
7005 * a temporary object (15.2),
7006 * a string literal (5.13.5),
7007 * the result of a typeid expression (8.2.8), or
7008 * a predefined __func__ variable (11.4.1). */
7009 else if (DECL_ARTIFICIAL (decl))
7010 {
7011 if (complain & tf_error)
7012 error ("the address of %qD is not a valid template argument",
7013 decl);
7014 return true;
7015 }
7016 else if (cxx_dialect < cxx20
7017 && !(same_type_ignoring_top_level_qualifiers_p
7018 (strip_array_types (TREE_TYPE (type)),
7019 strip_array_types (TREE_TYPE (decl)))))
7020 {
7021 if (complain & tf_error)
7022 error ("the address of the %qT subobject of %qD is not a "
7023 "valid template argument", TREE_TYPE (type), decl);
7024 return true;
7025 }
7026 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7027 {
7028 if (complain & tf_error)
7029 error ("the address of %qD is not a valid template argument "
7030 "because it does not have static storage duration",
7031 decl);
7032 return true;
7033 }
7034 }
7035 break;
7036
7037 default:
7038 if (!INDIRECT_TYPE_P (type))
7039 /* We're only concerned about pointers and references here. */;
7040 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7041 /* Null pointer values are OK in C++11. */;
7042 else
7043 {
7044 if (VAR_P (expr))
7045 {
7046 if (complain & tf_error)
7047 error ("%qD is not a valid template argument "
7048 "because %qD is a variable, not the address of "
7049 "a variable", expr, expr);
7050 return true;
7051 }
7052 else
7053 {
7054 if (complain & tf_error)
7055 error ("%qE is not a valid template argument for %qT "
7056 "because it is not the address of a variable",
7057 expr, type);
7058 return true;
7059 }
7060 }
7061 }
7062 return false;
7063
7064 }
7065
7066 /* The template arguments corresponding to template parameter objects of types
7067 that contain pointers to members. */
7068
7069 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7070
7071 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7072 template argument EXPR. */
7073
7074 static tree
7075 get_template_parm_object (tree expr, tsubst_flags_t complain)
7076 {
7077 if (TREE_CODE (expr) == TARGET_EXPR)
7078 expr = TARGET_EXPR_INITIAL (expr);
7079
7080 if (!TREE_CONSTANT (expr))
7081 {
7082 if ((complain & tf_error)
7083 && require_rvalue_constant_expression (expr))
7084 cxx_constant_value (expr);
7085 return error_mark_node;
7086 }
7087 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7088 return error_mark_node;
7089
7090 tree name = mangle_template_parm_object (expr);
7091 tree decl = get_global_binding (name);
7092 if (decl)
7093 return decl;
7094
7095 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7096 decl = create_temporary_var (type);
7097 TREE_STATIC (decl) = true;
7098 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7099 TREE_READONLY (decl) = true;
7100 DECL_NAME (decl) = name;
7101 SET_DECL_ASSEMBLER_NAME (decl, name);
7102 DECL_CONTEXT (decl) = global_namespace;
7103 comdat_linkage (decl);
7104
7105 if (!zero_init_p (type))
7106 {
7107 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7108 lower_var_init before we're done mangling. So store the original
7109 value elsewhere. */
7110 tree copy = unshare_constructor (expr);
7111 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7112 }
7113
7114 pushdecl_top_level_and_finish (decl, expr);
7115
7116 return decl;
7117 }
7118
7119 /* Return the actual template argument corresponding to template parameter
7120 object VAR. */
7121
7122 tree
7123 tparm_object_argument (tree var)
7124 {
7125 if (zero_init_p (TREE_TYPE (var)))
7126 return DECL_INITIAL (var);
7127 return *(tparm_obj_values->get (var));
7128 }
7129
7130 /* Attempt to convert the non-type template parameter EXPR to the
7131 indicated TYPE. If the conversion is successful, return the
7132 converted value. If the conversion is unsuccessful, return
7133 NULL_TREE if we issued an error message, or error_mark_node if we
7134 did not. We issue error messages for out-and-out bad template
7135 parameters, but not simply because the conversion failed, since we
7136 might be just trying to do argument deduction. Both TYPE and EXPR
7137 must be non-dependent.
7138
7139 The conversion follows the special rules described in
7140 [temp.arg.nontype], and it is much more strict than an implicit
7141 conversion.
7142
7143 This function is called twice for each template argument (see
7144 lookup_template_class for a more accurate description of this
7145 problem). This means that we need to handle expressions which
7146 are not valid in a C++ source, but can be created from the
7147 first call (for instance, casts to perform conversions). These
7148 hacks can go away after we fix the double coercion problem. */
7149
7150 static tree
7151 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7152 {
7153 tree expr_type;
7154 location_t loc = cp_expr_loc_or_input_loc (expr);
7155
7156 /* Detect immediately string literals as invalid non-type argument.
7157 This special-case is not needed for correctness (we would easily
7158 catch this later), but only to provide better diagnostic for this
7159 common user mistake. As suggested by DR 100, we do not mention
7160 linkage issues in the diagnostic as this is not the point. */
7161 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7162 {
7163 if (complain & tf_error)
7164 error ("%qE is not a valid template argument for type %qT "
7165 "because string literals can never be used in this context",
7166 expr, type);
7167 return NULL_TREE;
7168 }
7169
7170 /* Add the ADDR_EXPR now for the benefit of
7171 value_dependent_expression_p. */
7172 if (TYPE_PTROBV_P (type)
7173 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7174 {
7175 expr = decay_conversion (expr, complain);
7176 if (expr == error_mark_node)
7177 return error_mark_node;
7178 }
7179
7180 /* If we are in a template, EXPR may be non-dependent, but still
7181 have a syntactic, rather than semantic, form. For example, EXPR
7182 might be a SCOPE_REF, rather than the VAR_DECL to which the
7183 SCOPE_REF refers. Preserving the qualifying scope is necessary
7184 so that access checking can be performed when the template is
7185 instantiated -- but here we need the resolved form so that we can
7186 convert the argument. */
7187 bool non_dep = false;
7188 if (TYPE_REF_OBJ_P (type)
7189 && has_value_dependent_address (expr))
7190 /* If we want the address and it's value-dependent, don't fold. */;
7191 else if (processing_template_decl
7192 && is_nondependent_constant_expression (expr))
7193 non_dep = true;
7194 if (error_operand_p (expr))
7195 return error_mark_node;
7196 expr_type = TREE_TYPE (expr);
7197
7198 /* If the argument is non-dependent, perform any conversions in
7199 non-dependent context as well. */
7200 processing_template_decl_sentinel s (non_dep);
7201 if (non_dep)
7202 expr = instantiate_non_dependent_expr_internal (expr, complain);
7203
7204 const bool val_dep_p = value_dependent_expression_p (expr);
7205 if (val_dep_p)
7206 expr = canonicalize_expr_argument (expr, complain);
7207
7208 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7209 to a non-type argument of "nullptr". */
7210 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7211 expr = fold_simple (convert (type, expr));
7212
7213 /* In C++11, integral or enumeration non-type template arguments can be
7214 arbitrary constant expressions. Pointer and pointer to
7215 member arguments can be general constant expressions that evaluate
7216 to a null value, but otherwise still need to be of a specific form. */
7217 if (cxx_dialect >= cxx11)
7218 {
7219 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7220 /* A PTRMEM_CST is already constant, and a valid template
7221 argument for a parameter of pointer to member type, we just want
7222 to leave it in that form rather than lower it to a
7223 CONSTRUCTOR. */;
7224 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7225 || cxx_dialect >= cxx17)
7226 {
7227 /* C++17: A template-argument for a non-type template-parameter shall
7228 be a converted constant expression (8.20) of the type of the
7229 template-parameter. */
7230 expr = build_converted_constant_expr (type, expr, complain);
7231 if (expr == error_mark_node)
7232 /* Make sure we return NULL_TREE only if we have really issued
7233 an error, as described above. */
7234 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7235 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7236 {
7237 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7238 return expr;
7239 }
7240 expr = maybe_constant_value (expr, NULL_TREE,
7241 /*manifestly_const_eval=*/true);
7242 expr = convert_from_reference (expr);
7243 }
7244 else if (TYPE_PTR_OR_PTRMEM_P (type))
7245 {
7246 tree folded = maybe_constant_value (expr, NULL_TREE,
7247 /*manifestly_const_eval=*/true);
7248 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7249 : null_member_pointer_value_p (folded))
7250 expr = folded;
7251 }
7252 }
7253
7254 if (TYPE_REF_P (type))
7255 expr = mark_lvalue_use (expr);
7256 else
7257 expr = mark_rvalue_use (expr);
7258
7259 /* HACK: Due to double coercion, we can get a
7260 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7261 which is the tree that we built on the first call (see
7262 below when coercing to reference to object or to reference to
7263 function). We just strip everything and get to the arg.
7264 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7265 for examples. */
7266 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7267 {
7268 tree probe_type, probe = expr;
7269 if (REFERENCE_REF_P (probe))
7270 probe = TREE_OPERAND (probe, 0);
7271 probe_type = TREE_TYPE (probe);
7272 if (TREE_CODE (probe) == NOP_EXPR)
7273 {
7274 /* ??? Maybe we could use convert_from_reference here, but we
7275 would need to relax its constraints because the NOP_EXPR
7276 could actually change the type to something more cv-qualified,
7277 and this is not folded by convert_from_reference. */
7278 tree addr = TREE_OPERAND (probe, 0);
7279 if (TYPE_REF_P (probe_type)
7280 && TREE_CODE (addr) == ADDR_EXPR
7281 && TYPE_PTR_P (TREE_TYPE (addr))
7282 && (same_type_ignoring_top_level_qualifiers_p
7283 (TREE_TYPE (probe_type),
7284 TREE_TYPE (TREE_TYPE (addr)))))
7285 {
7286 expr = TREE_OPERAND (addr, 0);
7287 expr_type = TREE_TYPE (probe_type);
7288 }
7289 }
7290 }
7291
7292 /* [temp.arg.nontype]/5, bullet 1
7293
7294 For a non-type template-parameter of integral or enumeration type,
7295 integral promotions (_conv.prom_) and integral conversions
7296 (_conv.integral_) are applied. */
7297 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7298 || TREE_CODE (type) == REAL_TYPE)
7299 {
7300 if (cxx_dialect < cxx11)
7301 {
7302 tree t = build_converted_constant_expr (type, expr, complain);
7303 t = maybe_constant_value (t);
7304 if (t != error_mark_node)
7305 expr = t;
7306 }
7307
7308 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7309 return error_mark_node;
7310
7311 /* Notice that there are constant expressions like '4 % 0' which
7312 do not fold into integer constants. */
7313 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7314 {
7315 if (complain & tf_error)
7316 {
7317 int errs = errorcount, warns = warningcount + werrorcount;
7318 if (!require_potential_constant_expression (expr))
7319 expr = error_mark_node;
7320 else
7321 expr = cxx_constant_value (expr);
7322 if (errorcount > errs || warningcount + werrorcount > warns)
7323 inform (loc, "in template argument for type %qT", type);
7324 if (expr == error_mark_node)
7325 return NULL_TREE;
7326 /* else cxx_constant_value complained but gave us
7327 a real constant, so go ahead. */
7328 if (!CONSTANT_CLASS_P (expr))
7329 {
7330 /* Some assemble time constant expressions like
7331 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7332 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7333 as we can emit them into .rodata initializers of
7334 variables, yet they can't fold into an INTEGER_CST at
7335 compile time. Refuse them here. */
7336 gcc_checking_assert (reduced_constant_expression_p (expr));
7337 error_at (loc, "template argument %qE for type %qT not "
7338 "a compile-time constant", expr, type);
7339 return NULL_TREE;
7340 }
7341 }
7342 else
7343 return NULL_TREE;
7344 }
7345
7346 /* Avoid typedef problems. */
7347 if (TREE_TYPE (expr) != type)
7348 expr = fold_convert (type, expr);
7349 }
7350 /* [temp.arg.nontype]/5, bullet 2
7351
7352 For a non-type template-parameter of type pointer to object,
7353 qualification conversions (_conv.qual_) and the array-to-pointer
7354 conversion (_conv.array_) are applied. */
7355 else if (TYPE_PTROBV_P (type))
7356 {
7357 tree decayed = expr;
7358
7359 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7360 decay_conversion or an explicit cast. If it's a problematic cast,
7361 we'll complain about it below. */
7362 if (TREE_CODE (expr) == NOP_EXPR)
7363 {
7364 tree probe = expr;
7365 STRIP_NOPS (probe);
7366 if (TREE_CODE (probe) == ADDR_EXPR
7367 && TYPE_PTR_P (TREE_TYPE (probe)))
7368 {
7369 expr = probe;
7370 expr_type = TREE_TYPE (expr);
7371 }
7372 }
7373
7374 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7375
7376 A template-argument for a non-type, non-template template-parameter
7377 shall be one of: [...]
7378
7379 -- the name of a non-type template-parameter;
7380 -- the address of an object or function with external linkage, [...]
7381 expressed as "& id-expression" where the & is optional if the name
7382 refers to a function or array, or if the corresponding
7383 template-parameter is a reference.
7384
7385 Here, we do not care about functions, as they are invalid anyway
7386 for a parameter of type pointer-to-object. */
7387
7388 if (val_dep_p)
7389 /* Non-type template parameters are OK. */
7390 ;
7391 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7392 /* Null pointer values are OK in C++11. */;
7393 else if (TREE_CODE (expr) != ADDR_EXPR
7394 && !INDIRECT_TYPE_P (expr_type))
7395 /* Other values, like integer constants, might be valid
7396 non-type arguments of some other type. */
7397 return error_mark_node;
7398 else if (invalid_tparm_referent_p (type, expr, complain))
7399 return NULL_TREE;
7400
7401 expr = decayed;
7402
7403 expr = perform_qualification_conversions (type, expr);
7404 if (expr == error_mark_node)
7405 return error_mark_node;
7406 }
7407 /* [temp.arg.nontype]/5, bullet 3
7408
7409 For a non-type template-parameter of type reference to object, no
7410 conversions apply. The type referred to by the reference may be more
7411 cv-qualified than the (otherwise identical) type of the
7412 template-argument. The template-parameter is bound directly to the
7413 template-argument, which must be an lvalue. */
7414 else if (TYPE_REF_OBJ_P (type))
7415 {
7416 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7417 expr_type))
7418 return error_mark_node;
7419
7420 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7421 {
7422 if (complain & tf_error)
7423 error ("%qE is not a valid template argument for type %qT "
7424 "because of conflicts in cv-qualification", expr, type);
7425 return NULL_TREE;
7426 }
7427
7428 if (!lvalue_p (expr))
7429 {
7430 if (complain & tf_error)
7431 error ("%qE is not a valid template argument for type %qT "
7432 "because it is not an lvalue", expr, type);
7433 return NULL_TREE;
7434 }
7435
7436 /* [temp.arg.nontype]/1
7437
7438 A template-argument for a non-type, non-template template-parameter
7439 shall be one of: [...]
7440
7441 -- the address of an object or function with external linkage. */
7442 if (INDIRECT_REF_P (expr)
7443 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7444 {
7445 expr = TREE_OPERAND (expr, 0);
7446 if (DECL_P (expr))
7447 {
7448 if (complain & tf_error)
7449 error ("%q#D is not a valid template argument for type %qT "
7450 "because a reference variable does not have a constant "
7451 "address", expr, type);
7452 return NULL_TREE;
7453 }
7454 }
7455
7456 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7457 /* OK, dependent reference. We don't want to ask whether a DECL is
7458 itself value-dependent, since what we want here is its address. */;
7459 else
7460 {
7461 expr = build_address (expr);
7462
7463 if (invalid_tparm_referent_p (type, expr, complain))
7464 return NULL_TREE;
7465 }
7466
7467 if (!same_type_p (type, TREE_TYPE (expr)))
7468 expr = build_nop (type, expr);
7469 }
7470 /* [temp.arg.nontype]/5, bullet 4
7471
7472 For a non-type template-parameter of type pointer to function, only
7473 the function-to-pointer conversion (_conv.func_) is applied. If the
7474 template-argument represents a set of overloaded functions (or a
7475 pointer to such), the matching function is selected from the set
7476 (_over.over_). */
7477 else if (TYPE_PTRFN_P (type))
7478 {
7479 /* If the argument is a template-id, we might not have enough
7480 context information to decay the pointer. */
7481 if (!type_unknown_p (expr_type))
7482 {
7483 expr = decay_conversion (expr, complain);
7484 if (expr == error_mark_node)
7485 return error_mark_node;
7486 }
7487
7488 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7489 /* Null pointer values are OK in C++11. */
7490 return perform_qualification_conversions (type, expr);
7491
7492 expr = convert_nontype_argument_function (type, expr, complain);
7493 if (!expr || expr == error_mark_node)
7494 return expr;
7495 }
7496 /* [temp.arg.nontype]/5, bullet 5
7497
7498 For a non-type template-parameter of type reference to function, no
7499 conversions apply. If the template-argument represents a set of
7500 overloaded functions, the matching function is selected from the set
7501 (_over.over_). */
7502 else if (TYPE_REFFN_P (type))
7503 {
7504 if (TREE_CODE (expr) == ADDR_EXPR)
7505 {
7506 if (complain & tf_error)
7507 {
7508 error ("%qE is not a valid template argument for type %qT "
7509 "because it is a pointer", expr, type);
7510 inform (input_location, "try using %qE instead",
7511 TREE_OPERAND (expr, 0));
7512 }
7513 return NULL_TREE;
7514 }
7515
7516 expr = convert_nontype_argument_function (type, expr, complain);
7517 if (!expr || expr == error_mark_node)
7518 return expr;
7519 }
7520 /* [temp.arg.nontype]/5, bullet 6
7521
7522 For a non-type template-parameter of type pointer to member function,
7523 no conversions apply. If the template-argument represents a set of
7524 overloaded member functions, the matching member function is selected
7525 from the set (_over.over_). */
7526 else if (TYPE_PTRMEMFUNC_P (type))
7527 {
7528 expr = instantiate_type (type, expr, tf_none);
7529 if (expr == error_mark_node)
7530 return error_mark_node;
7531
7532 /* [temp.arg.nontype] bullet 1 says the pointer to member
7533 expression must be a pointer-to-member constant. */
7534 if (!val_dep_p
7535 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7536 return NULL_TREE;
7537
7538 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7539 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7540 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7541 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7542 }
7543 /* [temp.arg.nontype]/5, bullet 7
7544
7545 For a non-type template-parameter of type pointer to data member,
7546 qualification conversions (_conv.qual_) are applied. */
7547 else if (TYPE_PTRDATAMEM_P (type))
7548 {
7549 /* [temp.arg.nontype] bullet 1 says the pointer to member
7550 expression must be a pointer-to-member constant. */
7551 if (!val_dep_p
7552 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7553 return NULL_TREE;
7554
7555 expr = perform_qualification_conversions (type, expr);
7556 if (expr == error_mark_node)
7557 return expr;
7558 }
7559 else if (NULLPTR_TYPE_P (type))
7560 {
7561 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7562 {
7563 if (complain & tf_error)
7564 error ("%qE is not a valid template argument for type %qT "
7565 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7566 return NULL_TREE;
7567 }
7568 return expr;
7569 }
7570 else if (CLASS_TYPE_P (type))
7571 {
7572 /* Replace the argument with a reference to the corresponding template
7573 parameter object. */
7574 if (!val_dep_p)
7575 expr = get_template_parm_object (expr, complain);
7576 if (expr == error_mark_node)
7577 return NULL_TREE;
7578 }
7579 /* A template non-type parameter must be one of the above. */
7580 else
7581 gcc_unreachable ();
7582
7583 /* Sanity check: did we actually convert the argument to the
7584 right type? */
7585 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7586 (type, TREE_TYPE (expr)));
7587 return convert_from_reference (expr);
7588 }
7589
7590 /* Subroutine of coerce_template_template_parms, which returns 1 if
7591 PARM_PARM and ARG_PARM match using the rule for the template
7592 parameters of template template parameters. Both PARM and ARG are
7593 template parameters; the rest of the arguments are the same as for
7594 coerce_template_template_parms.
7595 */
7596 static int
7597 coerce_template_template_parm (tree parm,
7598 tree arg,
7599 tsubst_flags_t complain,
7600 tree in_decl,
7601 tree outer_args)
7602 {
7603 if (arg == NULL_TREE || error_operand_p (arg)
7604 || parm == NULL_TREE || error_operand_p (parm))
7605 return 0;
7606
7607 if (TREE_CODE (arg) != TREE_CODE (parm))
7608 return 0;
7609
7610 switch (TREE_CODE (parm))
7611 {
7612 case TEMPLATE_DECL:
7613 /* We encounter instantiations of templates like
7614 template <template <template <class> class> class TT>
7615 class C; */
7616 {
7617 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7618 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7619
7620 if (!coerce_template_template_parms
7621 (parmparm, argparm, complain, in_decl, outer_args))
7622 return 0;
7623 }
7624 /* Fall through. */
7625
7626 case TYPE_DECL:
7627 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7628 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7629 /* Argument is a parameter pack but parameter is not. */
7630 return 0;
7631 break;
7632
7633 case PARM_DECL:
7634 /* The tsubst call is used to handle cases such as
7635
7636 template <int> class C {};
7637 template <class T, template <T> class TT> class D {};
7638 D<int, C> d;
7639
7640 i.e. the parameter list of TT depends on earlier parameters. */
7641 if (!uses_template_parms (TREE_TYPE (arg)))
7642 {
7643 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7644 if (!uses_template_parms (t)
7645 && !same_type_p (t, TREE_TYPE (arg)))
7646 return 0;
7647 }
7648
7649 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7650 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7651 /* Argument is a parameter pack but parameter is not. */
7652 return 0;
7653
7654 break;
7655
7656 default:
7657 gcc_unreachable ();
7658 }
7659
7660 return 1;
7661 }
7662
7663 /* Coerce template argument list ARGLIST for use with template
7664 template-parameter TEMPL. */
7665
7666 static tree
7667 coerce_template_args_for_ttp (tree templ, tree arglist,
7668 tsubst_flags_t complain)
7669 {
7670 /* Consider an example where a template template parameter declared as
7671
7672 template <class T, class U = std::allocator<T> > class TT
7673
7674 The template parameter level of T and U are one level larger than
7675 of TT. To proper process the default argument of U, say when an
7676 instantiation `TT<int>' is seen, we need to build the full
7677 arguments containing {int} as the innermost level. Outer levels,
7678 available when not appearing as default template argument, can be
7679 obtained from the arguments of the enclosing template.
7680
7681 Suppose that TT is later substituted with std::vector. The above
7682 instantiation is `TT<int, std::allocator<T> >' with TT at
7683 level 1, and T at level 2, while the template arguments at level 1
7684 becomes {std::vector} and the inner level 2 is {int}. */
7685
7686 tree outer = DECL_CONTEXT (templ);
7687 if (outer)
7688 outer = generic_targs_for (outer);
7689 else if (current_template_parms)
7690 {
7691 /* This is an argument of the current template, so we haven't set
7692 DECL_CONTEXT yet. */
7693 tree relevant_template_parms;
7694
7695 /* Parameter levels that are greater than the level of the given
7696 template template parm are irrelevant. */
7697 relevant_template_parms = current_template_parms;
7698 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7699 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7700 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7701
7702 outer = template_parms_to_args (relevant_template_parms);
7703 }
7704
7705 if (outer)
7706 arglist = add_to_template_args (outer, arglist);
7707
7708 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7709 return coerce_template_parms (parmlist, arglist, templ,
7710 complain,
7711 /*require_all_args=*/true,
7712 /*use_default_args=*/true);
7713 }
7714
7715 /* A cache of template template parameters with match-all default
7716 arguments. */
7717 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7718
7719 /* T is a bound template template-parameter. Copy its arguments into default
7720 arguments of the template template-parameter's template parameters. */
7721
7722 static tree
7723 add_defaults_to_ttp (tree otmpl)
7724 {
7725 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7726 return *c;
7727
7728 tree ntmpl = copy_node (otmpl);
7729
7730 tree ntype = copy_node (TREE_TYPE (otmpl));
7731 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7732 TYPE_MAIN_VARIANT (ntype) = ntype;
7733 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7734 TYPE_NAME (ntype) = ntmpl;
7735 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7736
7737 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7738 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7739 TEMPLATE_PARM_DECL (idx) = ntmpl;
7740 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7741
7742 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7743 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7744 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7745 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7746 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7747 {
7748 tree o = TREE_VEC_ELT (vec, i);
7749 if (!template_parameter_pack_p (TREE_VALUE (o)))
7750 {
7751 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7752 TREE_PURPOSE (n) = any_targ_node;
7753 }
7754 }
7755
7756 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7757 return ntmpl;
7758 }
7759
7760 /* ARG is a bound potential template template-argument, and PARGS is a list
7761 of arguments for the corresponding template template-parameter. Adjust
7762 PARGS as appropriate for application to ARG's template, and if ARG is a
7763 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7764 arguments to the template template parameter. */
7765
7766 static tree
7767 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7768 {
7769 ++processing_template_decl;
7770 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7771 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7772 {
7773 /* When comparing two template template-parameters in partial ordering,
7774 rewrite the one currently being used as an argument to have default
7775 arguments for all parameters. */
7776 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7777 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7778 if (pargs != error_mark_node)
7779 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7780 TYPE_TI_ARGS (arg));
7781 }
7782 else
7783 {
7784 tree aparms
7785 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7786 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7787 /*require_all*/true,
7788 /*use_default*/true);
7789 }
7790 --processing_template_decl;
7791 return pargs;
7792 }
7793
7794 /* Subroutine of unify for the case when PARM is a
7795 BOUND_TEMPLATE_TEMPLATE_PARM. */
7796
7797 static int
7798 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7799 bool explain_p)
7800 {
7801 tree parmvec = TYPE_TI_ARGS (parm);
7802 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7803
7804 /* The template template parm might be variadic and the argument
7805 not, so flatten both argument lists. */
7806 parmvec = expand_template_argument_pack (parmvec);
7807 argvec = expand_template_argument_pack (argvec);
7808
7809 if (flag_new_ttp)
7810 {
7811 /* In keeping with P0522R0, adjust P's template arguments
7812 to apply to A's template; then flatten it again. */
7813 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7814 nparmvec = expand_template_argument_pack (nparmvec);
7815
7816 if (unify (tparms, targs, nparmvec, argvec,
7817 UNIFY_ALLOW_NONE, explain_p))
7818 return 1;
7819
7820 /* If the P0522 adjustment eliminated a pack expansion, deduce
7821 empty packs. */
7822 if (flag_new_ttp
7823 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7824 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7825 DEDUCE_EXACT, /*sub*/true, explain_p))
7826 return 1;
7827 }
7828 else
7829 {
7830 /* Deduce arguments T, i from TT<T> or TT<i>.
7831 We check each element of PARMVEC and ARGVEC individually
7832 rather than the whole TREE_VEC since they can have
7833 different number of elements, which is allowed under N2555. */
7834
7835 int len = TREE_VEC_LENGTH (parmvec);
7836
7837 /* Check if the parameters end in a pack, making them
7838 variadic. */
7839 int parm_variadic_p = 0;
7840 if (len > 0
7841 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7842 parm_variadic_p = 1;
7843
7844 for (int i = 0; i < len - parm_variadic_p; ++i)
7845 /* If the template argument list of P contains a pack
7846 expansion that is not the last template argument, the
7847 entire template argument list is a non-deduced
7848 context. */
7849 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7850 return unify_success (explain_p);
7851
7852 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7853 return unify_too_few_arguments (explain_p,
7854 TREE_VEC_LENGTH (argvec), len);
7855
7856 for (int i = 0; i < len - parm_variadic_p; ++i)
7857 if (unify (tparms, targs,
7858 TREE_VEC_ELT (parmvec, i),
7859 TREE_VEC_ELT (argvec, i),
7860 UNIFY_ALLOW_NONE, explain_p))
7861 return 1;
7862
7863 if (parm_variadic_p
7864 && unify_pack_expansion (tparms, targs,
7865 parmvec, argvec,
7866 DEDUCE_EXACT,
7867 /*subr=*/true, explain_p))
7868 return 1;
7869 }
7870
7871 return 0;
7872 }
7873
7874 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7875 template template parameters. Both PARM_PARMS and ARG_PARMS are
7876 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7877 or PARM_DECL.
7878
7879 Consider the example:
7880 template <class T> class A;
7881 template<template <class U> class TT> class B;
7882
7883 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7884 the parameters to A, and OUTER_ARGS contains A. */
7885
7886 static int
7887 coerce_template_template_parms (tree parm_parms,
7888 tree arg_parms,
7889 tsubst_flags_t complain,
7890 tree in_decl,
7891 tree outer_args)
7892 {
7893 int nparms, nargs, i;
7894 tree parm, arg;
7895 int variadic_p = 0;
7896
7897 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7898 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7899
7900 nparms = TREE_VEC_LENGTH (parm_parms);
7901 nargs = TREE_VEC_LENGTH (arg_parms);
7902
7903 if (flag_new_ttp)
7904 {
7905 /* P0522R0: A template template-parameter P is at least as specialized as
7906 a template template-argument A if, given the following rewrite to two
7907 function templates, the function template corresponding to P is at
7908 least as specialized as the function template corresponding to A
7909 according to the partial ordering rules for function templates
7910 ([temp.func.order]). Given an invented class template X with the
7911 template parameter list of A (including default arguments):
7912
7913 * Each of the two function templates has the same template parameters,
7914 respectively, as P or A.
7915
7916 * Each function template has a single function parameter whose type is
7917 a specialization of X with template arguments corresponding to the
7918 template parameters from the respective function template where, for
7919 each template parameter PP in the template parameter list of the
7920 function template, a corresponding template argument AA is formed. If
7921 PP declares a parameter pack, then AA is the pack expansion
7922 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7923
7924 If the rewrite produces an invalid type, then P is not at least as
7925 specialized as A. */
7926
7927 /* So coerce P's args to apply to A's parms, and then deduce between A's
7928 args and the converted args. If that succeeds, A is at least as
7929 specialized as P, so they match.*/
7930 tree pargs = template_parms_level_to_args (parm_parms);
7931 pargs = add_outermost_template_args (outer_args, pargs);
7932 ++processing_template_decl;
7933 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7934 /*require_all*/true, /*use_default*/true);
7935 --processing_template_decl;
7936 if (pargs != error_mark_node)
7937 {
7938 tree targs = make_tree_vec (nargs);
7939 tree aargs = template_parms_level_to_args (arg_parms);
7940 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7941 /*explain*/false))
7942 return 1;
7943 }
7944 }
7945
7946 /* Determine whether we have a parameter pack at the end of the
7947 template template parameter's template parameter list. */
7948 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7949 {
7950 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7951
7952 if (error_operand_p (parm))
7953 return 0;
7954
7955 switch (TREE_CODE (parm))
7956 {
7957 case TEMPLATE_DECL:
7958 case TYPE_DECL:
7959 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7960 variadic_p = 1;
7961 break;
7962
7963 case PARM_DECL:
7964 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7965 variadic_p = 1;
7966 break;
7967
7968 default:
7969 gcc_unreachable ();
7970 }
7971 }
7972
7973 if (nargs != nparms
7974 && !(variadic_p && nargs >= nparms - 1))
7975 return 0;
7976
7977 /* Check all of the template parameters except the parameter pack at
7978 the end (if any). */
7979 for (i = 0; i < nparms - variadic_p; ++i)
7980 {
7981 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7982 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7983 continue;
7984
7985 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7986 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7987
7988 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7989 outer_args))
7990 return 0;
7991
7992 }
7993
7994 if (variadic_p)
7995 {
7996 /* Check each of the template parameters in the template
7997 argument against the template parameter pack at the end of
7998 the template template parameter. */
7999 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8000 return 0;
8001
8002 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8003
8004 for (; i < nargs; ++i)
8005 {
8006 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8007 continue;
8008
8009 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8010
8011 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8012 outer_args))
8013 return 0;
8014 }
8015 }
8016
8017 return 1;
8018 }
8019
8020 /* Verifies that the deduced template arguments (in TARGS) for the
8021 template template parameters (in TPARMS) represent valid bindings,
8022 by comparing the template parameter list of each template argument
8023 to the template parameter list of its corresponding template
8024 template parameter, in accordance with DR150. This
8025 routine can only be called after all template arguments have been
8026 deduced. It will return TRUE if all of the template template
8027 parameter bindings are okay, FALSE otherwise. */
8028 bool
8029 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8030 {
8031 int i, ntparms = TREE_VEC_LENGTH (tparms);
8032 bool ret = true;
8033
8034 /* We're dealing with template parms in this process. */
8035 ++processing_template_decl;
8036
8037 targs = INNERMOST_TEMPLATE_ARGS (targs);
8038
8039 for (i = 0; i < ntparms; ++i)
8040 {
8041 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8042 tree targ = TREE_VEC_ELT (targs, i);
8043
8044 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8045 {
8046 tree packed_args = NULL_TREE;
8047 int idx, len = 1;
8048
8049 if (ARGUMENT_PACK_P (targ))
8050 {
8051 /* Look inside the argument pack. */
8052 packed_args = ARGUMENT_PACK_ARGS (targ);
8053 len = TREE_VEC_LENGTH (packed_args);
8054 }
8055
8056 for (idx = 0; idx < len; ++idx)
8057 {
8058 tree targ_parms = NULL_TREE;
8059
8060 if (packed_args)
8061 /* Extract the next argument from the argument
8062 pack. */
8063 targ = TREE_VEC_ELT (packed_args, idx);
8064
8065 if (PACK_EXPANSION_P (targ))
8066 /* Look at the pattern of the pack expansion. */
8067 targ = PACK_EXPANSION_PATTERN (targ);
8068
8069 /* Extract the template parameters from the template
8070 argument. */
8071 if (TREE_CODE (targ) == TEMPLATE_DECL)
8072 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8073 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8074 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8075
8076 /* Verify that we can coerce the template template
8077 parameters from the template argument to the template
8078 parameter. This requires an exact match. */
8079 if (targ_parms
8080 && !coerce_template_template_parms
8081 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8082 targ_parms,
8083 tf_none,
8084 tparm,
8085 targs))
8086 {
8087 ret = false;
8088 goto out;
8089 }
8090 }
8091 }
8092 }
8093
8094 out:
8095
8096 --processing_template_decl;
8097 return ret;
8098 }
8099
8100 /* Since type attributes aren't mangled, we need to strip them from
8101 template type arguments. */
8102
8103 tree
8104 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8105 {
8106 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8107 return arg;
8108 bool removed_attributes = false;
8109 tree canon = strip_typedefs (arg, &removed_attributes);
8110 if (removed_attributes
8111 && (complain & tf_warning))
8112 warning (OPT_Wignored_attributes,
8113 "ignoring attributes on template argument %qT", arg);
8114 return canon;
8115 }
8116
8117 /* And from inside dependent non-type arguments like sizeof(Type). */
8118
8119 static tree
8120 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8121 {
8122 if (!arg || arg == error_mark_node)
8123 return arg;
8124 bool removed_attributes = false;
8125 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8126 if (removed_attributes
8127 && (complain & tf_warning))
8128 warning (OPT_Wignored_attributes,
8129 "ignoring attributes in template argument %qE", arg);
8130 return canon;
8131 }
8132
8133 // A template declaration can be substituted for a constrained
8134 // template template parameter only when the argument is more
8135 // constrained than the parameter.
8136 static bool
8137 is_compatible_template_arg (tree parm, tree arg)
8138 {
8139 tree parm_cons = get_constraints (parm);
8140
8141 /* For now, allow constrained template template arguments
8142 and unconstrained template template parameters. */
8143 if (parm_cons == NULL_TREE)
8144 return true;
8145
8146 /* If the template parameter is constrained, we need to rewrite its
8147 constraints in terms of the ARG's template parameters. This ensures
8148 that all of the template parameter types will have the same depth.
8149
8150 Note that this is only valid when coerce_template_template_parm is
8151 true for the innermost template parameters of PARM and ARG. In other
8152 words, because coercion is successful, this conversion will be valid. */
8153 tree new_args = NULL_TREE;
8154 if (parm_cons)
8155 {
8156 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8157 new_args = template_parms_level_to_args (aparms);
8158 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8159 tf_none, NULL_TREE);
8160 if (parm_cons == error_mark_node)
8161 return false;
8162 }
8163
8164 return weakly_subsumes (parm_cons, new_args, arg);
8165 }
8166
8167 // Convert a placeholder argument into a binding to the original
8168 // parameter. The original parameter is saved as the TREE_TYPE of
8169 // ARG.
8170 static inline tree
8171 convert_wildcard_argument (tree parm, tree arg)
8172 {
8173 TREE_TYPE (arg) = parm;
8174 return arg;
8175 }
8176
8177 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8178 because one of them is dependent. But we need to represent the
8179 conversion for the benefit of cp_tree_equal. */
8180
8181 static tree
8182 maybe_convert_nontype_argument (tree type, tree arg)
8183 {
8184 /* Auto parms get no conversion. */
8185 if (type_uses_auto (type))
8186 return arg;
8187 /* We don't need or want to add this conversion now if we're going to use the
8188 argument for deduction. */
8189 if (value_dependent_expression_p (arg))
8190 return arg;
8191
8192 type = cv_unqualified (type);
8193 tree argtype = TREE_TYPE (arg);
8194 if (same_type_p (type, argtype))
8195 return arg;
8196
8197 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8198 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8199 return arg;
8200 }
8201
8202 /* Convert the indicated template ARG as necessary to match the
8203 indicated template PARM. Returns the converted ARG, or
8204 error_mark_node if the conversion was unsuccessful. Error and
8205 warning messages are issued under control of COMPLAIN. This
8206 conversion is for the Ith parameter in the parameter list. ARGS is
8207 the full set of template arguments deduced so far. */
8208
8209 static tree
8210 convert_template_argument (tree parm,
8211 tree arg,
8212 tree args,
8213 tsubst_flags_t complain,
8214 int i,
8215 tree in_decl)
8216 {
8217 tree orig_arg;
8218 tree val;
8219 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8220
8221 if (parm == error_mark_node || error_operand_p (arg))
8222 return error_mark_node;
8223
8224 /* Trivially convert placeholders. */
8225 if (TREE_CODE (arg) == WILDCARD_DECL)
8226 return convert_wildcard_argument (parm, arg);
8227
8228 if (arg == any_targ_node)
8229 return arg;
8230
8231 if (TREE_CODE (arg) == TREE_LIST
8232 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8233 {
8234 /* The template argument was the name of some
8235 member function. That's usually
8236 invalid, but static members are OK. In any
8237 case, grab the underlying fields/functions
8238 and issue an error later if required. */
8239 TREE_TYPE (arg) = unknown_type_node;
8240 }
8241
8242 orig_arg = arg;
8243
8244 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8245 requires_type = (TREE_CODE (parm) == TYPE_DECL
8246 || requires_tmpl_type);
8247
8248 /* When determining whether an argument pack expansion is a template,
8249 look at the pattern. */
8250 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8251 arg = PACK_EXPANSION_PATTERN (arg);
8252
8253 /* Deal with an injected-class-name used as a template template arg. */
8254 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8255 {
8256 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8257 if (TREE_CODE (t) == TEMPLATE_DECL)
8258 {
8259 if (cxx_dialect >= cxx11)
8260 /* OK under DR 1004. */;
8261 else if (complain & tf_warning_or_error)
8262 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8263 " used as template template argument", TYPE_NAME (arg));
8264 else if (flag_pedantic_errors)
8265 t = arg;
8266
8267 arg = t;
8268 }
8269 }
8270
8271 is_tmpl_type =
8272 ((TREE_CODE (arg) == TEMPLATE_DECL
8273 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8274 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8275 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8276 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8277
8278 if (is_tmpl_type
8279 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8280 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8281 arg = TYPE_STUB_DECL (arg);
8282
8283 is_type = TYPE_P (arg) || is_tmpl_type;
8284
8285 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8286 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8287 {
8288 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8289 {
8290 if (complain & tf_error)
8291 error ("invalid use of destructor %qE as a type", orig_arg);
8292 return error_mark_node;
8293 }
8294
8295 permerror (input_location,
8296 "to refer to a type member of a template parameter, "
8297 "use %<typename %E%>", orig_arg);
8298
8299 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8300 TREE_OPERAND (arg, 1),
8301 typename_type,
8302 complain);
8303 arg = orig_arg;
8304 is_type = 1;
8305 }
8306 if (is_type != requires_type)
8307 {
8308 if (in_decl)
8309 {
8310 if (complain & tf_error)
8311 {
8312 error ("type/value mismatch at argument %d in template "
8313 "parameter list for %qD",
8314 i + 1, in_decl);
8315 if (is_type)
8316 {
8317 /* The template argument is a type, but we're expecting
8318 an expression. */
8319 inform (input_location,
8320 " expected a constant of type %qT, got %qT",
8321 TREE_TYPE (parm),
8322 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8323 /* [temp.arg]/2: "In a template-argument, an ambiguity
8324 between a type-id and an expression is resolved to a
8325 type-id, regardless of the form of the corresponding
8326 template-parameter." So give the user a clue. */
8327 if (TREE_CODE (arg) == FUNCTION_TYPE)
8328 inform (input_location, " ambiguous template argument "
8329 "for non-type template parameter is treated as "
8330 "function type");
8331 }
8332 else if (requires_tmpl_type)
8333 inform (input_location,
8334 " expected a class template, got %qE", orig_arg);
8335 else
8336 inform (input_location,
8337 " expected a type, got %qE", orig_arg);
8338 }
8339 }
8340 return error_mark_node;
8341 }
8342 if (is_tmpl_type ^ requires_tmpl_type)
8343 {
8344 if (in_decl && (complain & tf_error))
8345 {
8346 error ("type/value mismatch at argument %d in template "
8347 "parameter list for %qD",
8348 i + 1, in_decl);
8349 if (is_tmpl_type)
8350 inform (input_location,
8351 " expected a type, got %qT", DECL_NAME (arg));
8352 else
8353 inform (input_location,
8354 " expected a class template, got %qT", orig_arg);
8355 }
8356 return error_mark_node;
8357 }
8358
8359 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8360 /* We already did the appropriate conversion when packing args. */
8361 val = orig_arg;
8362 else if (is_type)
8363 {
8364 if (requires_tmpl_type)
8365 {
8366 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8367 /* The number of argument required is not known yet.
8368 Just accept it for now. */
8369 val = orig_arg;
8370 else
8371 {
8372 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8373 tree argparm;
8374
8375 /* Strip alias templates that are equivalent to another
8376 template. */
8377 arg = get_underlying_template (arg);
8378 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8379
8380 if (coerce_template_template_parms (parmparm, argparm,
8381 complain, in_decl,
8382 args))
8383 {
8384 val = arg;
8385
8386 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8387 TEMPLATE_DECL. */
8388 if (val != error_mark_node)
8389 {
8390 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8391 val = TREE_TYPE (val);
8392 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8393 val = make_pack_expansion (val, complain);
8394 }
8395 }
8396 else
8397 {
8398 if (in_decl && (complain & tf_error))
8399 {
8400 error ("type/value mismatch at argument %d in "
8401 "template parameter list for %qD",
8402 i + 1, in_decl);
8403 inform (input_location,
8404 " expected a template of type %qD, got %qT",
8405 parm, orig_arg);
8406 }
8407
8408 val = error_mark_node;
8409 }
8410
8411 // Check that the constraints are compatible before allowing the
8412 // substitution.
8413 if (val != error_mark_node)
8414 if (!is_compatible_template_arg (parm, arg))
8415 {
8416 if (in_decl && (complain & tf_error))
8417 {
8418 error ("constraint mismatch at argument %d in "
8419 "template parameter list for %qD",
8420 i + 1, in_decl);
8421 inform (input_location, " expected %qD but got %qD",
8422 parm, arg);
8423 }
8424 val = error_mark_node;
8425 }
8426 }
8427 }
8428 else
8429 val = orig_arg;
8430 /* We only form one instance of each template specialization.
8431 Therefore, if we use a non-canonical variant (i.e., a
8432 typedef), any future messages referring to the type will use
8433 the typedef, which is confusing if those future uses do not
8434 themselves also use the typedef. */
8435 if (TYPE_P (val))
8436 val = canonicalize_type_argument (val, complain);
8437 }
8438 else
8439 {
8440 tree t = TREE_TYPE (parm);
8441
8442 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8443 > TMPL_ARGS_DEPTH (args))
8444 /* We don't have enough levels of args to do any substitution. This
8445 can happen in the context of -fnew-ttp-matching. */;
8446 else if (tree a = type_uses_auto (t))
8447 {
8448 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8449 if (t == error_mark_node)
8450 return error_mark_node;
8451 }
8452 else
8453 t = tsubst (t, args, complain, in_decl);
8454
8455 if (invalid_nontype_parm_type_p (t, complain))
8456 return error_mark_node;
8457
8458 if (t != TREE_TYPE (parm))
8459 t = canonicalize_type_argument (t, complain);
8460
8461 if (!type_dependent_expression_p (orig_arg)
8462 && !uses_template_parms (t))
8463 /* We used to call digest_init here. However, digest_init
8464 will report errors, which we don't want when complain
8465 is zero. More importantly, digest_init will try too
8466 hard to convert things: for example, `0' should not be
8467 converted to pointer type at this point according to
8468 the standard. Accepting this is not merely an
8469 extension, since deciding whether or not these
8470 conversions can occur is part of determining which
8471 function template to call, or whether a given explicit
8472 argument specification is valid. */
8473 val = convert_nontype_argument (t, orig_arg, complain);
8474 else
8475 {
8476 val = canonicalize_expr_argument (orig_arg, complain);
8477 val = maybe_convert_nontype_argument (t, val);
8478 }
8479
8480
8481 if (val == NULL_TREE)
8482 val = error_mark_node;
8483 else if (val == error_mark_node && (complain & tf_error))
8484 error_at (cp_expr_loc_or_input_loc (orig_arg),
8485 "could not convert template argument %qE from %qT to %qT",
8486 orig_arg, TREE_TYPE (orig_arg), t);
8487
8488 if (INDIRECT_REF_P (val))
8489 {
8490 /* Reject template arguments that are references to built-in
8491 functions with no library fallbacks. */
8492 const_tree inner = TREE_OPERAND (val, 0);
8493 const_tree innertype = TREE_TYPE (inner);
8494 if (innertype
8495 && TYPE_REF_P (innertype)
8496 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8497 && TREE_OPERAND_LENGTH (inner) > 0
8498 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8499 return error_mark_node;
8500 }
8501
8502 if (TREE_CODE (val) == SCOPE_REF)
8503 {
8504 /* Strip typedefs from the SCOPE_REF. */
8505 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8506 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8507 complain);
8508 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8509 QUALIFIED_NAME_IS_TEMPLATE (val));
8510 }
8511 }
8512
8513 return val;
8514 }
8515
8516 /* Coerces the remaining template arguments in INNER_ARGS (from
8517 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8518 Returns the coerced argument pack. PARM_IDX is the position of this
8519 parameter in the template parameter list. ARGS is the original
8520 template argument list. */
8521 static tree
8522 coerce_template_parameter_pack (tree parms,
8523 int parm_idx,
8524 tree args,
8525 tree inner_args,
8526 int arg_idx,
8527 tree new_args,
8528 int* lost,
8529 tree in_decl,
8530 tsubst_flags_t complain)
8531 {
8532 tree parm = TREE_VEC_ELT (parms, parm_idx);
8533 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8534 tree packed_args;
8535 tree argument_pack;
8536 tree packed_parms = NULL_TREE;
8537
8538 if (arg_idx > nargs)
8539 arg_idx = nargs;
8540
8541 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8542 {
8543 /* When the template parameter is a non-type template parameter pack
8544 or template template parameter pack whose type or template
8545 parameters use parameter packs, we know exactly how many arguments
8546 we are looking for. Build a vector of the instantiated decls for
8547 these template parameters in PACKED_PARMS. */
8548 /* We can't use make_pack_expansion here because it would interpret a
8549 _DECL as a use rather than a declaration. */
8550 tree decl = TREE_VALUE (parm);
8551 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8552 SET_PACK_EXPANSION_PATTERN (exp, decl);
8553 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8554 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8555
8556 TREE_VEC_LENGTH (args)--;
8557 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8558 TREE_VEC_LENGTH (args)++;
8559
8560 if (packed_parms == error_mark_node)
8561 return error_mark_node;
8562
8563 /* If we're doing a partial instantiation of a member template,
8564 verify that all of the types used for the non-type
8565 template parameter pack are, in fact, valid for non-type
8566 template parameters. */
8567 if (arg_idx < nargs
8568 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8569 {
8570 int j, len = TREE_VEC_LENGTH (packed_parms);
8571 for (j = 0; j < len; ++j)
8572 {
8573 tree t = TREE_VEC_ELT (packed_parms, j);
8574 if (TREE_CODE (t) == PARM_DECL
8575 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8576 return error_mark_node;
8577 }
8578 /* We don't know how many args we have yet, just
8579 use the unconverted ones for now. */
8580 return NULL_TREE;
8581 }
8582
8583 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8584 }
8585 /* Check if we have a placeholder pack, which indicates we're
8586 in the context of a introduction list. In that case we want
8587 to match this pack to the single placeholder. */
8588 else if (arg_idx < nargs
8589 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8590 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8591 {
8592 nargs = arg_idx + 1;
8593 packed_args = make_tree_vec (1);
8594 }
8595 else
8596 packed_args = make_tree_vec (nargs - arg_idx);
8597
8598 /* Convert the remaining arguments, which will be a part of the
8599 parameter pack "parm". */
8600 int first_pack_arg = arg_idx;
8601 for (; arg_idx < nargs; ++arg_idx)
8602 {
8603 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8604 tree actual_parm = TREE_VALUE (parm);
8605 int pack_idx = arg_idx - first_pack_arg;
8606
8607 if (packed_parms)
8608 {
8609 /* Once we've packed as many args as we have types, stop. */
8610 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8611 break;
8612 else if (PACK_EXPANSION_P (arg))
8613 /* We don't know how many args we have yet, just
8614 use the unconverted ones for now. */
8615 return NULL_TREE;
8616 else
8617 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8618 }
8619
8620 if (arg == error_mark_node)
8621 {
8622 if (complain & tf_error)
8623 error ("template argument %d is invalid", arg_idx + 1);
8624 }
8625 else
8626 arg = convert_template_argument (actual_parm,
8627 arg, new_args, complain, parm_idx,
8628 in_decl);
8629 if (arg == error_mark_node)
8630 (*lost)++;
8631 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8632 }
8633
8634 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8635 && TREE_VEC_LENGTH (packed_args) > 0)
8636 {
8637 if (complain & tf_error)
8638 error ("wrong number of template arguments (%d, should be %d)",
8639 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8640 return error_mark_node;
8641 }
8642
8643 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8644 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8645 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8646 else
8647 {
8648 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8649 TREE_CONSTANT (argument_pack) = 1;
8650 }
8651
8652 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8653 if (CHECKING_P)
8654 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8655 TREE_VEC_LENGTH (packed_args));
8656 return argument_pack;
8657 }
8658
8659 /* Returns the number of pack expansions in the template argument vector
8660 ARGS. */
8661
8662 static int
8663 pack_expansion_args_count (tree args)
8664 {
8665 int i;
8666 int count = 0;
8667 if (args)
8668 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8669 {
8670 tree elt = TREE_VEC_ELT (args, i);
8671 if (elt && PACK_EXPANSION_P (elt))
8672 ++count;
8673 }
8674 return count;
8675 }
8676
8677 /* Convert all template arguments to their appropriate types, and
8678 return a vector containing the innermost resulting template
8679 arguments. If any error occurs, return error_mark_node. Error and
8680 warning messages are issued under control of COMPLAIN.
8681
8682 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8683 for arguments not specified in ARGS. Otherwise, if
8684 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8685 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8686 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8687 ARGS. */
8688
8689 static tree
8690 coerce_template_parms (tree parms,
8691 tree args,
8692 tree in_decl,
8693 tsubst_flags_t complain,
8694 bool require_all_args,
8695 bool use_default_args)
8696 {
8697 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8698 tree orig_inner_args;
8699 tree inner_args;
8700 tree new_args;
8701 tree new_inner_args;
8702
8703 /* When used as a boolean value, indicates whether this is a
8704 variadic template parameter list. Since it's an int, we can also
8705 subtract it from nparms to get the number of non-variadic
8706 parameters. */
8707 int variadic_p = 0;
8708 int variadic_args_p = 0;
8709 int post_variadic_parms = 0;
8710
8711 /* Adjustment to nparms for fixed parameter packs. */
8712 int fixed_pack_adjust = 0;
8713 int fixed_packs = 0;
8714 int missing = 0;
8715
8716 /* Likewise for parameters with default arguments. */
8717 int default_p = 0;
8718
8719 if (args == error_mark_node)
8720 return error_mark_node;
8721
8722 nparms = TREE_VEC_LENGTH (parms);
8723
8724 /* Determine if there are any parameter packs or default arguments. */
8725 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8726 {
8727 tree parm = TREE_VEC_ELT (parms, parm_idx);
8728 if (variadic_p)
8729 ++post_variadic_parms;
8730 if (template_parameter_pack_p (TREE_VALUE (parm)))
8731 ++variadic_p;
8732 if (TREE_PURPOSE (parm))
8733 ++default_p;
8734 }
8735
8736 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8737 /* If there are no parameters that follow a parameter pack, we need to
8738 expand any argument packs so that we can deduce a parameter pack from
8739 some non-packed args followed by an argument pack, as in variadic85.C.
8740 If there are such parameters, we need to leave argument packs intact
8741 so the arguments are assigned properly. This can happen when dealing
8742 with a nested class inside a partial specialization of a class
8743 template, as in variadic92.C, or when deducing a template parameter pack
8744 from a sub-declarator, as in variadic114.C. */
8745 if (!post_variadic_parms)
8746 inner_args = expand_template_argument_pack (inner_args);
8747
8748 /* Count any pack expansion args. */
8749 variadic_args_p = pack_expansion_args_count (inner_args);
8750
8751 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8752 if ((nargs - variadic_args_p > nparms && !variadic_p)
8753 || (nargs < nparms - variadic_p
8754 && require_all_args
8755 && !variadic_args_p
8756 && (!use_default_args
8757 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8758 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8759 {
8760 bad_nargs:
8761 if (complain & tf_error)
8762 {
8763 if (variadic_p || default_p)
8764 {
8765 nparms -= variadic_p + default_p;
8766 error ("wrong number of template arguments "
8767 "(%d, should be at least %d)", nargs, nparms);
8768 }
8769 else
8770 error ("wrong number of template arguments "
8771 "(%d, should be %d)", nargs, nparms);
8772
8773 if (in_decl)
8774 inform (DECL_SOURCE_LOCATION (in_decl),
8775 "provided for %qD", in_decl);
8776 }
8777
8778 return error_mark_node;
8779 }
8780 /* We can't pass a pack expansion to a non-pack parameter of an alias
8781 template (DR 1430). */
8782 else if (in_decl
8783 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8784 || concept_definition_p (in_decl))
8785 && variadic_args_p
8786 && nargs - variadic_args_p < nparms - variadic_p)
8787 {
8788 if (complain & tf_error)
8789 {
8790 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8791 {
8792 tree arg = TREE_VEC_ELT (inner_args, i);
8793 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8794
8795 if (PACK_EXPANSION_P (arg)
8796 && !template_parameter_pack_p (parm))
8797 {
8798 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8799 error_at (location_of (arg),
8800 "pack expansion argument for non-pack parameter "
8801 "%qD of alias template %qD", parm, in_decl);
8802 else
8803 error_at (location_of (arg),
8804 "pack expansion argument for non-pack parameter "
8805 "%qD of concept %qD", parm, in_decl);
8806 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8807 goto found;
8808 }
8809 }
8810 gcc_unreachable ();
8811 found:;
8812 }
8813 return error_mark_node;
8814 }
8815
8816 /* We need to evaluate the template arguments, even though this
8817 template-id may be nested within a "sizeof". */
8818 cp_evaluated ev;
8819
8820 new_inner_args = make_tree_vec (nparms);
8821 new_args = add_outermost_template_args (args, new_inner_args);
8822 int pack_adjust = 0;
8823 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8824 {
8825 tree arg;
8826 tree parm;
8827
8828 /* Get the Ith template parameter. */
8829 parm = TREE_VEC_ELT (parms, parm_idx);
8830
8831 if (parm == error_mark_node)
8832 {
8833 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8834 continue;
8835 }
8836
8837 /* Calculate the next argument. */
8838 if (arg_idx < nargs)
8839 arg = TREE_VEC_ELT (inner_args, arg_idx);
8840 else
8841 arg = NULL_TREE;
8842
8843 if (template_parameter_pack_p (TREE_VALUE (parm))
8844 && (arg || require_all_args || !(complain & tf_partial))
8845 && !(arg && ARGUMENT_PACK_P (arg)))
8846 {
8847 /* Some arguments will be placed in the
8848 template parameter pack PARM. */
8849 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8850 inner_args, arg_idx,
8851 new_args, &lost,
8852 in_decl, complain);
8853
8854 if (arg == NULL_TREE)
8855 {
8856 /* We don't know how many args we have yet, just use the
8857 unconverted (and still packed) ones for now. */
8858 new_inner_args = orig_inner_args;
8859 arg_idx = nargs;
8860 break;
8861 }
8862
8863 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8864
8865 /* Store this argument. */
8866 if (arg == error_mark_node)
8867 {
8868 lost++;
8869 /* We are done with all of the arguments. */
8870 arg_idx = nargs;
8871 break;
8872 }
8873 else
8874 {
8875 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8876 arg_idx += pack_adjust;
8877 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8878 {
8879 ++fixed_packs;
8880 fixed_pack_adjust += pack_adjust;
8881 }
8882 }
8883
8884 continue;
8885 }
8886 else if (arg)
8887 {
8888 if (PACK_EXPANSION_P (arg))
8889 {
8890 /* "If every valid specialization of a variadic template
8891 requires an empty template parameter pack, the template is
8892 ill-formed, no diagnostic required." So check that the
8893 pattern works with this parameter. */
8894 tree pattern = PACK_EXPANSION_PATTERN (arg);
8895 tree conv = convert_template_argument (TREE_VALUE (parm),
8896 pattern, new_args,
8897 complain, parm_idx,
8898 in_decl);
8899 if (conv == error_mark_node)
8900 {
8901 if (complain & tf_error)
8902 inform (input_location, "so any instantiation with a "
8903 "non-empty parameter pack would be ill-formed");
8904 ++lost;
8905 }
8906 else if (TYPE_P (conv) && !TYPE_P (pattern))
8907 /* Recover from missing typename. */
8908 TREE_VEC_ELT (inner_args, arg_idx)
8909 = make_pack_expansion (conv, complain);
8910
8911 /* We don't know how many args we have yet, just
8912 use the unconverted ones for now. */
8913 new_inner_args = inner_args;
8914 arg_idx = nargs;
8915 break;
8916 }
8917 }
8918 else if (require_all_args)
8919 {
8920 /* There must be a default arg in this case. */
8921 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8922 complain, in_decl);
8923 /* The position of the first default template argument,
8924 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8925 Record that. */
8926 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8927 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8928 arg_idx - pack_adjust);
8929 }
8930 else
8931 break;
8932
8933 if (arg == error_mark_node)
8934 {
8935 if (complain & tf_error)
8936 error ("template argument %d is invalid", arg_idx + 1);
8937 }
8938 else if (!arg)
8939 {
8940 /* This can occur if there was an error in the template
8941 parameter list itself (which we would already have
8942 reported) that we are trying to recover from, e.g., a class
8943 template with a parameter list such as
8944 template<typename..., typename> (cpp0x/variadic150.C). */
8945 ++lost;
8946
8947 /* This can also happen with a fixed parameter pack (71834). */
8948 if (arg_idx >= nargs)
8949 ++missing;
8950 }
8951 else
8952 arg = convert_template_argument (TREE_VALUE (parm),
8953 arg, new_args, complain,
8954 parm_idx, in_decl);
8955
8956 if (arg == error_mark_node)
8957 lost++;
8958
8959 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8960 }
8961
8962 if (missing || arg_idx < nargs - variadic_args_p)
8963 {
8964 /* If we had fixed parameter packs, we didn't know how many arguments we
8965 actually needed earlier; now we do. */
8966 nparms += fixed_pack_adjust;
8967 variadic_p -= fixed_packs;
8968 goto bad_nargs;
8969 }
8970
8971 if (arg_idx < nargs)
8972 {
8973 /* We had some pack expansion arguments that will only work if the packs
8974 are empty, but wait until instantiation time to complain.
8975 See variadic-ttp3.C. */
8976
8977 /* Except that we can't provide empty packs to alias templates or
8978 concepts when there are no corresponding parameters. Basically,
8979 we can get here with this:
8980
8981 template<typename T> concept C = true;
8982
8983 template<typename... Args>
8984 requires C<Args...>
8985 void f();
8986
8987 When parsing C<Args...>, we try to form a concept check of
8988 C<?, Args...>. Without the extra check for substituting an empty
8989 pack past the last parameter, we can accept the check as valid.
8990
8991 FIXME: This may be valid for alias templates (but I doubt it).
8992
8993 FIXME: The error could be better also. */
8994 if (in_decl && concept_definition_p (in_decl))
8995 {
8996 if (complain & tf_error)
8997 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8998 "too many arguments");
8999 return error_mark_node;
9000 }
9001
9002 int len = nparms + (nargs - arg_idx);
9003 tree args = make_tree_vec (len);
9004 int i = 0;
9005 for (; i < nparms; ++i)
9006 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9007 for (; i < len; ++i, ++arg_idx)
9008 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9009 arg_idx - pack_adjust);
9010 new_inner_args = args;
9011 }
9012
9013 if (lost)
9014 {
9015 gcc_assert (!(complain & tf_error) || seen_error ());
9016 return error_mark_node;
9017 }
9018
9019 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9020 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9021 TREE_VEC_LENGTH (new_inner_args));
9022
9023 return new_inner_args;
9024 }
9025
9026 /* Convert all template arguments to their appropriate types, and
9027 return a vector containing the innermost resulting template
9028 arguments. If any error occurs, return error_mark_node. Error and
9029 warning messages are not issued.
9030
9031 Note that no function argument deduction is performed, and default
9032 arguments are used to fill in unspecified arguments. */
9033 tree
9034 coerce_template_parms (tree parms, tree args, tree in_decl)
9035 {
9036 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9037 }
9038
9039 /* Convert all template arguments to their appropriate type, and
9040 instantiate default arguments as needed. This returns a vector
9041 containing the innermost resulting template arguments, or
9042 error_mark_node if unsuccessful. */
9043 tree
9044 coerce_template_parms (tree parms, tree args, tree in_decl,
9045 tsubst_flags_t complain)
9046 {
9047 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9048 }
9049
9050 /* Like coerce_template_parms. If PARMS represents all template
9051 parameters levels, this function returns a vector of vectors
9052 representing all the resulting argument levels. Note that in this
9053 case, only the innermost arguments are coerced because the
9054 outermost ones are supposed to have been coerced already.
9055
9056 Otherwise, if PARMS represents only (the innermost) vector of
9057 parameters, this function returns a vector containing just the
9058 innermost resulting arguments. */
9059
9060 static tree
9061 coerce_innermost_template_parms (tree parms,
9062 tree args,
9063 tree in_decl,
9064 tsubst_flags_t complain,
9065 bool require_all_args,
9066 bool use_default_args)
9067 {
9068 int parms_depth = TMPL_PARMS_DEPTH (parms);
9069 int args_depth = TMPL_ARGS_DEPTH (args);
9070 tree coerced_args;
9071
9072 if (parms_depth > 1)
9073 {
9074 coerced_args = make_tree_vec (parms_depth);
9075 tree level;
9076 int cur_depth;
9077
9078 for (level = parms, cur_depth = parms_depth;
9079 parms_depth > 0 && level != NULL_TREE;
9080 level = TREE_CHAIN (level), --cur_depth)
9081 {
9082 tree l;
9083 if (cur_depth == args_depth)
9084 l = coerce_template_parms (TREE_VALUE (level),
9085 args, in_decl, complain,
9086 require_all_args,
9087 use_default_args);
9088 else
9089 l = TMPL_ARGS_LEVEL (args, cur_depth);
9090
9091 if (l == error_mark_node)
9092 return error_mark_node;
9093
9094 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9095 }
9096 }
9097 else
9098 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9099 args, in_decl, complain,
9100 require_all_args,
9101 use_default_args);
9102 return coerced_args;
9103 }
9104
9105 /* Returns true if T is a wrapper to make a C++20 template parameter
9106 object const. */
9107
9108 static bool
9109 class_nttp_const_wrapper_p (tree t)
9110 {
9111 if (cxx_dialect < cxx20)
9112 return false;
9113 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9114 && CP_TYPE_CONST_P (TREE_TYPE (t))
9115 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9116 }
9117
9118 /* Returns 1 if template args OT and NT are equivalent. */
9119
9120 int
9121 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9122 {
9123 if (nt == ot)
9124 return 1;
9125 if (nt == NULL_TREE || ot == NULL_TREE)
9126 return false;
9127 if (nt == any_targ_node || ot == any_targ_node)
9128 return true;
9129
9130 if (class_nttp_const_wrapper_p (nt))
9131 nt = TREE_OPERAND (nt, 0);
9132 if (class_nttp_const_wrapper_p (ot))
9133 ot = TREE_OPERAND (ot, 0);
9134
9135 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9136 /* For member templates */
9137 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9138 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9139 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9140 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9141 PACK_EXPANSION_PATTERN (nt))
9142 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9143 PACK_EXPANSION_EXTRA_ARGS (nt)));
9144 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9145 return cp_tree_equal (ot, nt);
9146 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9147 gcc_unreachable ();
9148 else if (TYPE_P (nt) || TYPE_P (ot))
9149 {
9150 if (!(TYPE_P (nt) && TYPE_P (ot)))
9151 return false;
9152 /* Don't treat an alias template specialization with dependent
9153 arguments as equivalent to its underlying type when used as a
9154 template argument; we need them to be distinct so that we
9155 substitute into the specialization arguments at instantiation
9156 time. And aliases can't be equivalent without being ==, so
9157 we don't need to look any deeper.
9158
9159 During partial ordering, however, we need to treat them normally so
9160 that we can order uses of the same alias with different
9161 cv-qualification (79960). */
9162 if (!partial_order
9163 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9164 return false;
9165 else
9166 return same_type_p (ot, nt);
9167 }
9168 else
9169 {
9170 /* Try to treat a template non-type argument that has been converted
9171 to the parameter type as equivalent to one that hasn't yet. */
9172 for (enum tree_code code1 = TREE_CODE (ot);
9173 CONVERT_EXPR_CODE_P (code1)
9174 || code1 == NON_LVALUE_EXPR;
9175 code1 = TREE_CODE (ot))
9176 ot = TREE_OPERAND (ot, 0);
9177
9178 for (enum tree_code code2 = TREE_CODE (nt);
9179 CONVERT_EXPR_CODE_P (code2)
9180 || code2 == NON_LVALUE_EXPR;
9181 code2 = TREE_CODE (nt))
9182 nt = TREE_OPERAND (nt, 0);
9183
9184 return cp_tree_equal (ot, nt);
9185 }
9186 }
9187
9188 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9189 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9190 NEWARG_PTR with the offending arguments if they are non-NULL. */
9191
9192 int
9193 comp_template_args (tree oldargs, tree newargs,
9194 tree *oldarg_ptr, tree *newarg_ptr,
9195 bool partial_order)
9196 {
9197 int i;
9198
9199 if (oldargs == newargs)
9200 return 1;
9201
9202 if (!oldargs || !newargs)
9203 return 0;
9204
9205 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9206 return 0;
9207
9208 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9209 {
9210 tree nt = TREE_VEC_ELT (newargs, i);
9211 tree ot = TREE_VEC_ELT (oldargs, i);
9212
9213 if (! template_args_equal (ot, nt, partial_order))
9214 {
9215 if (oldarg_ptr != NULL)
9216 *oldarg_ptr = ot;
9217 if (newarg_ptr != NULL)
9218 *newarg_ptr = nt;
9219 return 0;
9220 }
9221 }
9222 return 1;
9223 }
9224
9225 inline bool
9226 comp_template_args_porder (tree oargs, tree nargs)
9227 {
9228 return comp_template_args (oargs, nargs, NULL, NULL, true);
9229 }
9230
9231 /* Implement a freelist interface for objects of type T.
9232
9233 Head is a separate object, rather than a regular member, so that we
9234 can define it as a GTY deletable pointer, which is highly
9235 desirable. A data member could be declared that way, but then the
9236 containing object would implicitly get GTY((user)), which would
9237 prevent us from instantiating freelists as global objects.
9238 Although this way we can create freelist global objects, they're
9239 such thin wrappers that instantiating temporaries at every use
9240 loses nothing and saves permanent storage for the freelist object.
9241
9242 Member functions next, anew, poison and reinit have default
9243 implementations that work for most of the types we're interested
9244 in, but if they don't work for some type, they should be explicitly
9245 specialized. See the comments before them for requirements, and
9246 the example specializations for the tree_list_freelist. */
9247 template <typename T>
9248 class freelist
9249 {
9250 /* Return the next object in a chain. We could just do type
9251 punning, but if we access the object with its underlying type, we
9252 avoid strict-aliasing trouble. This needs only work between
9253 poison and reinit. */
9254 static T *&next (T *obj) { return obj->next; }
9255
9256 /* Return a newly allocated, uninitialized or minimally-initialized
9257 object of type T. Any initialization performed by anew should
9258 either remain across the life of the object and the execution of
9259 poison, or be redone by reinit. */
9260 static T *anew () { return ggc_alloc<T> (); }
9261
9262 /* Optionally scribble all over the bits holding the object, so that
9263 they become (mostly?) uninitialized memory. This is called while
9264 preparing to make the object part of the free list. */
9265 static void poison (T *obj) {
9266 T *p ATTRIBUTE_UNUSED = obj;
9267 T **q ATTRIBUTE_UNUSED = &next (obj);
9268
9269 #ifdef ENABLE_GC_CHECKING
9270 /* Poison the data, to indicate the data is garbage. */
9271 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9272 memset (p, 0xa5, sizeof (*p));
9273 #endif
9274 /* Let valgrind know the object is free. */
9275 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9276
9277 /* Let valgrind know the next portion of the object is available,
9278 but uninitialized. */
9279 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9280 }
9281
9282 /* Bring an object that underwent at least one lifecycle after anew
9283 and before the most recent free and poison, back to a usable
9284 state, reinitializing whatever is needed for it to be
9285 functionally equivalent to an object just allocated and returned
9286 by anew. This may poison or clear the next field, used by
9287 freelist housekeeping after poison was called. */
9288 static void reinit (T *obj) {
9289 T **q ATTRIBUTE_UNUSED = &next (obj);
9290
9291 #ifdef ENABLE_GC_CHECKING
9292 memset (q, 0xa5, sizeof (*q));
9293 #endif
9294 /* Let valgrind know the entire object is available, but
9295 uninitialized. */
9296 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9297 }
9298
9299 /* Reference a GTY-deletable pointer that points to the first object
9300 in the free list proper. */
9301 T *&head;
9302 public:
9303 /* Construct a freelist object chaining objects off of HEAD. */
9304 freelist (T *&head) : head(head) {}
9305
9306 /* Add OBJ to the free object list. The former head becomes OBJ's
9307 successor. */
9308 void free (T *obj)
9309 {
9310 poison (obj);
9311 next (obj) = head;
9312 head = obj;
9313 }
9314
9315 /* Take an object from the free list, if one is available, or
9316 allocate a new one. Objects taken from the free list should be
9317 regarded as filled with garbage, except for bits that are
9318 configured to be preserved across free and alloc. */
9319 T *alloc ()
9320 {
9321 if (head)
9322 {
9323 T *obj = head;
9324 head = next (head);
9325 reinit (obj);
9326 return obj;
9327 }
9328 else
9329 return anew ();
9330 }
9331 };
9332
9333 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9334 want to allocate a TREE_LIST using the usual interface, and ensure
9335 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9336 build_tree_list logic in reinit, so this could go out of sync. */
9337 template <>
9338 inline tree &
9339 freelist<tree_node>::next (tree obj)
9340 {
9341 return TREE_CHAIN (obj);
9342 }
9343 template <>
9344 inline tree
9345 freelist<tree_node>::anew ()
9346 {
9347 return build_tree_list (NULL, NULL);
9348 }
9349 template <>
9350 inline void
9351 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9352 {
9353 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9354 tree p ATTRIBUTE_UNUSED = obj;
9355 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9356 tree *q ATTRIBUTE_UNUSED = &next (obj);
9357
9358 #ifdef ENABLE_GC_CHECKING
9359 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9360
9361 /* Poison the data, to indicate the data is garbage. */
9362 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9363 memset (p, 0xa5, size);
9364 #endif
9365 /* Let valgrind know the object is free. */
9366 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9367 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9368 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9369 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9370
9371 #ifdef ENABLE_GC_CHECKING
9372 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9373 /* Keep TREE_CHAIN functional. */
9374 TREE_SET_CODE (obj, TREE_LIST);
9375 #else
9376 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9377 #endif
9378 }
9379 template <>
9380 inline void
9381 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9382 {
9383 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9384
9385 #ifdef ENABLE_GC_CHECKING
9386 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9387 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9388 memset (obj, 0, sizeof (tree_list));
9389 #endif
9390
9391 /* Let valgrind know the entire object is available, but
9392 uninitialized. */
9393 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9394
9395 #ifdef ENABLE_GC_CHECKING
9396 TREE_SET_CODE (obj, TREE_LIST);
9397 #else
9398 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9399 #endif
9400 }
9401
9402 /* Point to the first object in the TREE_LIST freelist. */
9403 static GTY((deletable)) tree tree_list_freelist_head;
9404 /* Return the/an actual TREE_LIST freelist. */
9405 static inline freelist<tree_node>
9406 tree_list_freelist ()
9407 {
9408 return tree_list_freelist_head;
9409 }
9410
9411 /* Point to the first object in the tinst_level freelist. */
9412 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9413 /* Return the/an actual tinst_level freelist. */
9414 static inline freelist<tinst_level>
9415 tinst_level_freelist ()
9416 {
9417 return tinst_level_freelist_head;
9418 }
9419
9420 /* Point to the first object in the pending_template freelist. */
9421 static GTY((deletable)) pending_template *pending_template_freelist_head;
9422 /* Return the/an actual pending_template freelist. */
9423 static inline freelist<pending_template>
9424 pending_template_freelist ()
9425 {
9426 return pending_template_freelist_head;
9427 }
9428
9429 /* Build the TREE_LIST object out of a split list, store it
9430 permanently, and return it. */
9431 tree
9432 tinst_level::to_list ()
9433 {
9434 gcc_assert (split_list_p ());
9435 tree ret = tree_list_freelist ().alloc ();
9436 TREE_PURPOSE (ret) = tldcl;
9437 TREE_VALUE (ret) = targs;
9438 tldcl = ret;
9439 targs = NULL;
9440 gcc_assert (tree_list_p ());
9441 return ret;
9442 }
9443
9444 const unsigned short tinst_level::refcount_infinity;
9445
9446 /* Increment OBJ's refcount unless it is already infinite. */
9447 static tinst_level *
9448 inc_refcount_use (tinst_level *obj)
9449 {
9450 if (obj && obj->refcount != tinst_level::refcount_infinity)
9451 ++obj->refcount;
9452 return obj;
9453 }
9454
9455 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9456 void
9457 tinst_level::free (tinst_level *obj)
9458 {
9459 if (obj->tree_list_p ())
9460 tree_list_freelist ().free (obj->get_node ());
9461 tinst_level_freelist ().free (obj);
9462 }
9463
9464 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9465 OBJ's DECL and OBJ, and start over with the tinst_level object that
9466 used to be referenced by OBJ's NEXT. */
9467 static void
9468 dec_refcount_use (tinst_level *obj)
9469 {
9470 while (obj
9471 && obj->refcount != tinst_level::refcount_infinity
9472 && !--obj->refcount)
9473 {
9474 tinst_level *next = obj->next;
9475 tinst_level::free (obj);
9476 obj = next;
9477 }
9478 }
9479
9480 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9481 and of the former PTR. Omitting the second argument is equivalent
9482 to passing (T*)NULL; this is allowed because passing the
9483 zero-valued integral constant NULL confuses type deduction and/or
9484 overload resolution. */
9485 template <typename T>
9486 static void
9487 set_refcount_ptr (T *& ptr, T *obj = NULL)
9488 {
9489 T *save = ptr;
9490 ptr = inc_refcount_use (obj);
9491 dec_refcount_use (save);
9492 }
9493
9494 static void
9495 add_pending_template (tree d)
9496 {
9497 tree ti = (TYPE_P (d)
9498 ? CLASSTYPE_TEMPLATE_INFO (d)
9499 : DECL_TEMPLATE_INFO (d));
9500 struct pending_template *pt;
9501 int level;
9502
9503 if (TI_PENDING_TEMPLATE_FLAG (ti))
9504 return;
9505
9506 /* We are called both from instantiate_decl, where we've already had a
9507 tinst_level pushed, and instantiate_template, where we haven't.
9508 Compensate. */
9509 gcc_assert (TREE_CODE (d) != TREE_LIST);
9510 level = !current_tinst_level
9511 || current_tinst_level->maybe_get_node () != d;
9512
9513 if (level)
9514 push_tinst_level (d);
9515
9516 pt = pending_template_freelist ().alloc ();
9517 pt->next = NULL;
9518 pt->tinst = NULL;
9519 set_refcount_ptr (pt->tinst, current_tinst_level);
9520 if (last_pending_template)
9521 last_pending_template->next = pt;
9522 else
9523 pending_templates = pt;
9524
9525 last_pending_template = pt;
9526
9527 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9528
9529 if (level)
9530 pop_tinst_level ();
9531 }
9532
9533
9534 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9535 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9536 documentation for TEMPLATE_ID_EXPR. */
9537
9538 tree
9539 lookup_template_function (tree fns, tree arglist)
9540 {
9541 if (fns == error_mark_node || arglist == error_mark_node)
9542 return error_mark_node;
9543
9544 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9545
9546 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9547 {
9548 error ("%q#D is not a function template", fns);
9549 return error_mark_node;
9550 }
9551
9552 if (BASELINK_P (fns))
9553 {
9554 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9555 unknown_type_node,
9556 BASELINK_FUNCTIONS (fns),
9557 arglist);
9558 return fns;
9559 }
9560
9561 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9562 }
9563
9564 /* Within the scope of a template class S<T>, the name S gets bound
9565 (in build_self_reference) to a TYPE_DECL for the class, not a
9566 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9567 or one of its enclosing classes, and that type is a template,
9568 return the associated TEMPLATE_DECL. Otherwise, the original
9569 DECL is returned.
9570
9571 Also handle the case when DECL is a TREE_LIST of ambiguous
9572 injected-class-names from different bases. */
9573
9574 tree
9575 maybe_get_template_decl_from_type_decl (tree decl)
9576 {
9577 if (decl == NULL_TREE)
9578 return decl;
9579
9580 /* DR 176: A lookup that finds an injected-class-name (10.2
9581 [class.member.lookup]) can result in an ambiguity in certain cases
9582 (for example, if it is found in more than one base class). If all of
9583 the injected-class-names that are found refer to specializations of
9584 the same class template, and if the name is followed by a
9585 template-argument-list, the reference refers to the class template
9586 itself and not a specialization thereof, and is not ambiguous. */
9587 if (TREE_CODE (decl) == TREE_LIST)
9588 {
9589 tree t, tmpl = NULL_TREE;
9590 for (t = decl; t; t = TREE_CHAIN (t))
9591 {
9592 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9593 if (!tmpl)
9594 tmpl = elt;
9595 else if (tmpl != elt)
9596 break;
9597 }
9598 if (tmpl && t == NULL_TREE)
9599 return tmpl;
9600 else
9601 return decl;
9602 }
9603
9604 return (decl != NULL_TREE
9605 && DECL_SELF_REFERENCE_P (decl)
9606 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9607 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9608 }
9609
9610 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9611 parameters, find the desired type.
9612
9613 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9614
9615 IN_DECL, if non-NULL, is the template declaration we are trying to
9616 instantiate.
9617
9618 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9619 the class we are looking up.
9620
9621 Issue error and warning messages under control of COMPLAIN.
9622
9623 If the template class is really a local class in a template
9624 function, then the FUNCTION_CONTEXT is the function in which it is
9625 being instantiated.
9626
9627 ??? Note that this function is currently called *twice* for each
9628 template-id: the first time from the parser, while creating the
9629 incomplete type (finish_template_type), and the second type during the
9630 real instantiation (instantiate_template_class). This is surely something
9631 that we want to avoid. It also causes some problems with argument
9632 coercion (see convert_nontype_argument for more information on this). */
9633
9634 static tree
9635 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9636 int entering_scope, tsubst_flags_t complain)
9637 {
9638 tree templ = NULL_TREE, parmlist;
9639 tree t;
9640 spec_entry **slot;
9641 spec_entry *entry;
9642 spec_entry elt;
9643 hashval_t hash;
9644
9645 if (identifier_p (d1))
9646 {
9647 tree value = innermost_non_namespace_value (d1);
9648 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9649 templ = value;
9650 else
9651 {
9652 if (context)
9653 push_decl_namespace (context);
9654 templ = lookup_name (d1);
9655 templ = maybe_get_template_decl_from_type_decl (templ);
9656 if (context)
9657 pop_decl_namespace ();
9658 }
9659 if (templ)
9660 context = DECL_CONTEXT (templ);
9661 }
9662 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9663 {
9664 tree type = TREE_TYPE (d1);
9665
9666 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9667 an implicit typename for the second A. Deal with it. */
9668 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9669 type = TREE_TYPE (type);
9670
9671 if (CLASSTYPE_TEMPLATE_INFO (type))
9672 {
9673 templ = CLASSTYPE_TI_TEMPLATE (type);
9674 d1 = DECL_NAME (templ);
9675 }
9676 }
9677 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9678 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9679 {
9680 templ = TYPE_TI_TEMPLATE (d1);
9681 d1 = DECL_NAME (templ);
9682 }
9683 else if (DECL_TYPE_TEMPLATE_P (d1))
9684 {
9685 templ = d1;
9686 d1 = DECL_NAME (templ);
9687 context = DECL_CONTEXT (templ);
9688 }
9689 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9690 {
9691 templ = d1;
9692 d1 = DECL_NAME (templ);
9693 }
9694
9695 /* Issue an error message if we didn't find a template. */
9696 if (! templ)
9697 {
9698 if (complain & tf_error)
9699 error ("%qT is not a template", d1);
9700 return error_mark_node;
9701 }
9702
9703 if (TREE_CODE (templ) != TEMPLATE_DECL
9704 /* Make sure it's a user visible template, if it was named by
9705 the user. */
9706 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9707 && !PRIMARY_TEMPLATE_P (templ)))
9708 {
9709 if (complain & tf_error)
9710 {
9711 error ("non-template type %qT used as a template", d1);
9712 if (in_decl)
9713 error ("for template declaration %q+D", in_decl);
9714 }
9715 return error_mark_node;
9716 }
9717
9718 complain &= ~tf_user;
9719
9720 /* An alias that just changes the name of a template is equivalent to the
9721 other template, so if any of the arguments are pack expansions, strip
9722 the alias to avoid problems with a pack expansion passed to a non-pack
9723 alias template parameter (DR 1430). */
9724 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9725 templ = get_underlying_template (templ);
9726
9727 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9728 {
9729 tree parm;
9730 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9731 if (arglist2 == error_mark_node
9732 || (!uses_template_parms (arglist2)
9733 && check_instantiated_args (templ, arglist2, complain)))
9734 return error_mark_node;
9735
9736 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9737 return parm;
9738 }
9739 else
9740 {
9741 tree template_type = TREE_TYPE (templ);
9742 tree gen_tmpl;
9743 tree type_decl;
9744 tree found = NULL_TREE;
9745 int arg_depth;
9746 int parm_depth;
9747 int is_dependent_type;
9748 int use_partial_inst_tmpl = false;
9749
9750 if (template_type == error_mark_node)
9751 /* An error occurred while building the template TEMPL, and a
9752 diagnostic has most certainly been emitted for that
9753 already. Let's propagate that error. */
9754 return error_mark_node;
9755
9756 gen_tmpl = most_general_template (templ);
9757 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9758 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9759 arg_depth = TMPL_ARGS_DEPTH (arglist);
9760
9761 if (arg_depth == 1 && parm_depth > 1)
9762 {
9763 /* We've been given an incomplete set of template arguments.
9764 For example, given:
9765
9766 template <class T> struct S1 {
9767 template <class U> struct S2 {};
9768 template <class U> struct S2<U*> {};
9769 };
9770
9771 we will be called with an ARGLIST of `U*', but the
9772 TEMPLATE will be `template <class T> template
9773 <class U> struct S1<T>::S2'. We must fill in the missing
9774 arguments. */
9775 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9776 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9777 arg_depth = TMPL_ARGS_DEPTH (arglist);
9778 }
9779
9780 /* Now we should have enough arguments. */
9781 gcc_assert (parm_depth == arg_depth);
9782
9783 /* From here on, we're only interested in the most general
9784 template. */
9785
9786 /* Calculate the BOUND_ARGS. These will be the args that are
9787 actually tsubst'd into the definition to create the
9788 instantiation. */
9789 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9790 complain,
9791 /*require_all_args=*/true,
9792 /*use_default_args=*/true);
9793
9794 if (arglist == error_mark_node)
9795 /* We were unable to bind the arguments. */
9796 return error_mark_node;
9797
9798 /* In the scope of a template class, explicit references to the
9799 template class refer to the type of the template, not any
9800 instantiation of it. For example, in:
9801
9802 template <class T> class C { void f(C<T>); }
9803
9804 the `C<T>' is just the same as `C'. Outside of the
9805 class, however, such a reference is an instantiation. */
9806 if (entering_scope
9807 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9808 || currently_open_class (template_type))
9809 {
9810 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9811
9812 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9813 return template_type;
9814 }
9815
9816 /* If we already have this specialization, return it. */
9817 elt.tmpl = gen_tmpl;
9818 elt.args = arglist;
9819 elt.spec = NULL_TREE;
9820 hash = spec_hasher::hash (&elt);
9821 entry = type_specializations->find_with_hash (&elt, hash);
9822
9823 if (entry)
9824 return entry->spec;
9825
9826 /* If the template's constraints are not satisfied,
9827 then we cannot form a valid type.
9828
9829 Note that the check is deferred until after the hash
9830 lookup. This prevents redundant checks on previously
9831 instantiated specializations. */
9832 if (flag_concepts
9833 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9834 && !constraints_satisfied_p (gen_tmpl, arglist))
9835 {
9836 if (complain & tf_error)
9837 {
9838 auto_diagnostic_group d;
9839 error ("template constraint failure for %qD", gen_tmpl);
9840 diagnose_constraints (input_location, gen_tmpl, arglist);
9841 }
9842 return error_mark_node;
9843 }
9844
9845 is_dependent_type = uses_template_parms (arglist);
9846
9847 /* If the deduced arguments are invalid, then the binding
9848 failed. */
9849 if (!is_dependent_type
9850 && check_instantiated_args (gen_tmpl,
9851 INNERMOST_TEMPLATE_ARGS (arglist),
9852 complain))
9853 return error_mark_node;
9854
9855 if (!is_dependent_type
9856 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9857 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9858 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9859 /* This occurs when the user has tried to define a tagged type
9860 in a scope that forbids it. We emitted an error during the
9861 parse. We didn't complete the bail out then, so here we
9862 are. */
9863 return error_mark_node;
9864
9865 context = DECL_CONTEXT (gen_tmpl);
9866 if (context && TYPE_P (context))
9867 {
9868 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9869 context = complete_type (context);
9870 }
9871 else
9872 context = tsubst (context, arglist, complain, in_decl);
9873
9874 if (context == error_mark_node)
9875 return error_mark_node;
9876
9877 if (!context)
9878 context = global_namespace;
9879
9880 /* Create the type. */
9881 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9882 {
9883 /* The user referred to a specialization of an alias
9884 template represented by GEN_TMPL.
9885
9886 [temp.alias]/2 says:
9887
9888 When a template-id refers to the specialization of an
9889 alias template, it is equivalent to the associated
9890 type obtained by substitution of its
9891 template-arguments for the template-parameters in the
9892 type-id of the alias template. */
9893
9894 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9895 /* Note that the call above (by indirectly calling
9896 register_specialization in tsubst_decl) registers the
9897 TYPE_DECL representing the specialization of the alias
9898 template. So next time someone substitutes ARGLIST for
9899 the template parms into the alias template (GEN_TMPL),
9900 she'll get that TYPE_DECL back. */
9901
9902 if (t == error_mark_node)
9903 return t;
9904 }
9905 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9906 {
9907 if (!is_dependent_type)
9908 {
9909 set_current_access_from_decl (TYPE_NAME (template_type));
9910 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9911 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9912 arglist, complain, in_decl),
9913 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9914 arglist, complain, in_decl),
9915 SCOPED_ENUM_P (template_type), NULL);
9916
9917 if (t == error_mark_node)
9918 return t;
9919 }
9920 else
9921 {
9922 /* We don't want to call start_enum for this type, since
9923 the values for the enumeration constants may involve
9924 template parameters. And, no one should be interested
9925 in the enumeration constants for such a type. */
9926 t = cxx_make_type (ENUMERAL_TYPE);
9927 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9928 }
9929 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9930 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9931 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9932 }
9933 else if (CLASS_TYPE_P (template_type))
9934 {
9935 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9936 instantiated here. */
9937 gcc_assert (!LAMBDA_TYPE_P (template_type));
9938
9939 t = make_class_type (TREE_CODE (template_type));
9940 CLASSTYPE_DECLARED_CLASS (t)
9941 = CLASSTYPE_DECLARED_CLASS (template_type);
9942 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9943
9944 /* A local class. Make sure the decl gets registered properly. */
9945 if (context == current_function_decl)
9946 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9947 == error_mark_node)
9948 return error_mark_node;
9949
9950 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9951 /* This instantiation is another name for the primary
9952 template type. Set the TYPE_CANONICAL field
9953 appropriately. */
9954 TYPE_CANONICAL (t) = template_type;
9955 else if (any_template_arguments_need_structural_equality_p (arglist))
9956 /* Some of the template arguments require structural
9957 equality testing, so this template class requires
9958 structural equality testing. */
9959 SET_TYPE_STRUCTURAL_EQUALITY (t);
9960 }
9961 else
9962 gcc_unreachable ();
9963
9964 /* If we called start_enum or pushtag above, this information
9965 will already be set up. */
9966 type_decl = TYPE_NAME (t);
9967 if (!type_decl)
9968 {
9969 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9970
9971 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9972 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9973 DECL_SOURCE_LOCATION (type_decl)
9974 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9975 }
9976
9977 if (CLASS_TYPE_P (template_type))
9978 {
9979 TREE_PRIVATE (type_decl)
9980 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9981 TREE_PROTECTED (type_decl)
9982 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9983 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9984 {
9985 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9986 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9987 }
9988 }
9989
9990 if (OVERLOAD_TYPE_P (t)
9991 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9992 {
9993 static const char *tags[] = {"abi_tag", "may_alias"};
9994
9995 for (unsigned ix = 0; ix != 2; ix++)
9996 {
9997 tree attributes
9998 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9999
10000 if (attributes)
10001 TYPE_ATTRIBUTES (t)
10002 = tree_cons (TREE_PURPOSE (attributes),
10003 TREE_VALUE (attributes),
10004 TYPE_ATTRIBUTES (t));
10005 }
10006 }
10007
10008 /* Let's consider the explicit specialization of a member
10009 of a class template specialization that is implicitly instantiated,
10010 e.g.:
10011 template<class T>
10012 struct S
10013 {
10014 template<class U> struct M {}; //#0
10015 };
10016
10017 template<>
10018 template<>
10019 struct S<int>::M<char> //#1
10020 {
10021 int i;
10022 };
10023 [temp.expl.spec]/4 says this is valid.
10024
10025 In this case, when we write:
10026 S<int>::M<char> m;
10027
10028 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10029 the one of #0.
10030
10031 When we encounter #1, we want to store the partial instantiation
10032 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10033
10034 For all cases other than this "explicit specialization of member of a
10035 class template", we just want to store the most general template into
10036 the CLASSTYPE_TI_TEMPLATE of M.
10037
10038 This case of "explicit specialization of member of a class template"
10039 only happens when:
10040 1/ the enclosing class is an instantiation of, and therefore not
10041 the same as, the context of the most general template, and
10042 2/ we aren't looking at the partial instantiation itself, i.e.
10043 the innermost arguments are not the same as the innermost parms of
10044 the most general template.
10045
10046 So it's only when 1/ and 2/ happens that we want to use the partial
10047 instantiation of the member template in lieu of its most general
10048 template. */
10049
10050 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10051 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10052 /* the enclosing class must be an instantiation... */
10053 && CLASS_TYPE_P (context)
10054 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10055 {
10056 TREE_VEC_LENGTH (arglist)--;
10057 ++processing_template_decl;
10058 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10059 tree partial_inst_args =
10060 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10061 arglist, complain, NULL_TREE);
10062 --processing_template_decl;
10063 TREE_VEC_LENGTH (arglist)++;
10064 if (partial_inst_args == error_mark_node)
10065 return error_mark_node;
10066 use_partial_inst_tmpl =
10067 /*...and we must not be looking at the partial instantiation
10068 itself. */
10069 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10070 partial_inst_args);
10071 }
10072
10073 if (!use_partial_inst_tmpl)
10074 /* This case is easy; there are no member templates involved. */
10075 found = gen_tmpl;
10076 else
10077 {
10078 /* This is a full instantiation of a member template. Find
10079 the partial instantiation of which this is an instance. */
10080
10081 /* Temporarily reduce by one the number of levels in the ARGLIST
10082 so as to avoid comparing the last set of arguments. */
10083 TREE_VEC_LENGTH (arglist)--;
10084 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
10085 TREE_VEC_LENGTH (arglist)++;
10086 /* FOUND is either a proper class type, or an alias
10087 template specialization. In the later case, it's a
10088 TYPE_DECL, resulting from the substituting of arguments
10089 for parameters in the TYPE_DECL of the alias template
10090 done earlier. So be careful while getting the template
10091 of FOUND. */
10092 found = (TREE_CODE (found) == TEMPLATE_DECL
10093 ? found
10094 : (TREE_CODE (found) == TYPE_DECL
10095 ? DECL_TI_TEMPLATE (found)
10096 : CLASSTYPE_TI_TEMPLATE (found)));
10097
10098 if (DECL_CLASS_TEMPLATE_P (found)
10099 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10100 {
10101 /* If this partial instantiation is specialized, we want to
10102 use it for hash table lookup. */
10103 elt.tmpl = found;
10104 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10105 hash = spec_hasher::hash (&elt);
10106 }
10107 }
10108
10109 /* Build template info for the new specialization. */
10110 if (TYPE_ALIAS_P (t))
10111 {
10112 /* This is constructed during instantiation of the alias
10113 decl. But for member templates of template classes, that
10114 is not correct as we need to refer to the partially
10115 instantiated template, not the most general template.
10116 The incorrect knowledge will not have escaped this
10117 instantiation process, so we're good just updating the
10118 template_info we made then. */
10119 tree ti = DECL_TEMPLATE_INFO (TYPE_NAME (t));
10120 gcc_checking_assert (template_args_equal (TI_ARGS (ti), arglist));
10121 if (TI_TEMPLATE (ti) != found)
10122 {
10123 gcc_checking_assert (DECL_TI_TEMPLATE (found) == TI_TEMPLATE (ti));
10124 TI_TEMPLATE (ti) = found;
10125 }
10126 }
10127 else
10128 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10129
10130 elt.spec = t;
10131 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10132 gcc_checking_assert (*slot == NULL);
10133 entry = ggc_alloc<spec_entry> ();
10134 *entry = elt;
10135 *slot = entry;
10136
10137 /* Note this use of the partial instantiation so we can check it
10138 later in maybe_process_partial_specialization. */
10139 DECL_TEMPLATE_INSTANTIATIONS (found)
10140 = tree_cons (arglist, t,
10141 DECL_TEMPLATE_INSTANTIATIONS (found));
10142
10143 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10144 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10145 /* Now that the type has been registered on the instantiations
10146 list, we set up the enumerators. Because the enumeration
10147 constants may involve the enumeration type itself, we make
10148 sure to register the type first, and then create the
10149 constants. That way, doing tsubst_expr for the enumeration
10150 constants won't result in recursive calls here; we'll find
10151 the instantiation and exit above. */
10152 tsubst_enum (template_type, t, arglist);
10153
10154 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10155 /* If the type makes use of template parameters, the
10156 code that generates debugging information will crash. */
10157 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10158
10159 /* Possibly limit visibility based on template args. */
10160 TREE_PUBLIC (type_decl) = 1;
10161 determine_visibility (type_decl);
10162
10163 inherit_targ_abi_tags (t);
10164
10165 return t;
10166 }
10167 }
10168
10169 /* Wrapper for lookup_template_class_1. */
10170
10171 tree
10172 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10173 int entering_scope, tsubst_flags_t complain)
10174 {
10175 tree ret;
10176 timevar_push (TV_TEMPLATE_INST);
10177 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10178 entering_scope, complain);
10179 timevar_pop (TV_TEMPLATE_INST);
10180 return ret;
10181 }
10182
10183 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10184
10185 tree
10186 lookup_template_variable (tree templ, tree arglist)
10187 {
10188 if (flag_concepts && variable_concept_p (templ))
10189 return build_concept_check (templ, arglist, tf_none);
10190
10191 /* The type of the expression is NULL_TREE since the template-id could refer
10192 to an explicit or partial specialization. */
10193 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10194 }
10195
10196 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10197
10198 tree
10199 finish_template_variable (tree var, tsubst_flags_t complain)
10200 {
10201 tree templ = TREE_OPERAND (var, 0);
10202 tree arglist = TREE_OPERAND (var, 1);
10203
10204 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10205 arglist = add_outermost_template_args (tmpl_args, arglist);
10206
10207 templ = most_general_template (templ);
10208 tree parms = DECL_TEMPLATE_PARMS (templ);
10209 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10210 /*req_all*/true,
10211 /*use_default*/true);
10212 if (arglist == error_mark_node)
10213 return error_mark_node;
10214
10215 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10216 {
10217 if (complain & tf_error)
10218 {
10219 auto_diagnostic_group d;
10220 error ("use of invalid variable template %qE", var);
10221 diagnose_constraints (location_of (var), templ, arglist);
10222 }
10223 return error_mark_node;
10224 }
10225
10226 return instantiate_template (templ, arglist, complain);
10227 }
10228
10229 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10230 TARGS template args, and instantiate it if it's not dependent. */
10231
10232 tree
10233 lookup_and_finish_template_variable (tree templ, tree targs,
10234 tsubst_flags_t complain)
10235 {
10236 templ = lookup_template_variable (templ, targs);
10237 if (!any_dependent_template_arguments_p (targs))
10238 {
10239 templ = finish_template_variable (templ, complain);
10240 mark_used (templ);
10241 }
10242
10243 return convert_from_reference (templ);
10244 }
10245
10246 /* If the set of template parameters PARMS contains a template parameter
10247 at the given LEVEL and INDEX, then return this parameter. Otherwise
10248 return NULL_TREE. */
10249
10250 static tree
10251 corresponding_template_parameter (tree parms, int level, int index)
10252 {
10253 while (TMPL_PARMS_DEPTH (parms) > level)
10254 parms = TREE_CHAIN (parms);
10255
10256 if (TMPL_PARMS_DEPTH (parms) != level
10257 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10258 return NULL_TREE;
10259
10260 tree t = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), index));
10261 /* As in template_parm_to_arg. */
10262 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10263 t = TREE_TYPE (t);
10264 else
10265 t = DECL_INITIAL (t);
10266
10267 gcc_assert (TEMPLATE_PARM_P (t));
10268 return t;
10269 }
10270
10271 /* Return the template parameter from PARMS that positionally corresponds
10272 to the template parameter PARM, or else return NULL_TREE. */
10273
10274 static tree
10275 corresponding_template_parameter (tree parms, tree parm)
10276 {
10277 int level, index;
10278 template_parm_level_and_index (parm, &level, &index);
10279 return corresponding_template_parameter (parms, level, index);
10280 }
10281
10282 \f
10283 struct pair_fn_data
10284 {
10285 tree_fn_t fn;
10286 tree_fn_t any_fn;
10287 void *data;
10288 /* True when we should also visit template parameters that occur in
10289 non-deduced contexts. */
10290 bool include_nondeduced_p;
10291 hash_set<tree> *visited;
10292 };
10293
10294 /* Called from for_each_template_parm via walk_tree. */
10295
10296 static tree
10297 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10298 {
10299 tree t = *tp;
10300 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10301 tree_fn_t fn = pfd->fn;
10302 void *data = pfd->data;
10303 tree result = NULL_TREE;
10304
10305 #define WALK_SUBTREE(NODE) \
10306 do \
10307 { \
10308 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10309 pfd->include_nondeduced_p, \
10310 pfd->any_fn); \
10311 if (result) goto out; \
10312 } \
10313 while (0)
10314
10315 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10316 return t;
10317
10318 if (TYPE_P (t)
10319 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10320 WALK_SUBTREE (TYPE_CONTEXT (t));
10321
10322 switch (TREE_CODE (t))
10323 {
10324 case RECORD_TYPE:
10325 if (TYPE_PTRMEMFUNC_P (t))
10326 break;
10327 /* Fall through. */
10328
10329 case UNION_TYPE:
10330 case ENUMERAL_TYPE:
10331 if (!TYPE_TEMPLATE_INFO (t))
10332 *walk_subtrees = 0;
10333 else
10334 WALK_SUBTREE (TYPE_TI_ARGS (t));
10335 break;
10336
10337 case INTEGER_TYPE:
10338 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10339 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10340 break;
10341
10342 case METHOD_TYPE:
10343 /* Since we're not going to walk subtrees, we have to do this
10344 explicitly here. */
10345 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10346 /* Fall through. */
10347
10348 case FUNCTION_TYPE:
10349 /* Check the return type. */
10350 WALK_SUBTREE (TREE_TYPE (t));
10351
10352 /* Check the parameter types. Since default arguments are not
10353 instantiated until they are needed, the TYPE_ARG_TYPES may
10354 contain expressions that involve template parameters. But,
10355 no-one should be looking at them yet. And, once they're
10356 instantiated, they don't contain template parameters, so
10357 there's no point in looking at them then, either. */
10358 {
10359 tree parm;
10360
10361 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10362 WALK_SUBTREE (TREE_VALUE (parm));
10363
10364 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10365 want walk_tree walking into them itself. */
10366 *walk_subtrees = 0;
10367 }
10368
10369 if (flag_noexcept_type)
10370 {
10371 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10372 if (spec)
10373 WALK_SUBTREE (TREE_PURPOSE (spec));
10374 }
10375 break;
10376
10377 case TYPEOF_TYPE:
10378 case DECLTYPE_TYPE:
10379 case UNDERLYING_TYPE:
10380 if (pfd->include_nondeduced_p
10381 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10382 pfd->visited,
10383 pfd->include_nondeduced_p,
10384 pfd->any_fn))
10385 return error_mark_node;
10386 *walk_subtrees = false;
10387 break;
10388
10389 case FUNCTION_DECL:
10390 case VAR_DECL:
10391 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10392 WALK_SUBTREE (DECL_TI_ARGS (t));
10393 /* Fall through. */
10394
10395 case PARM_DECL:
10396 case CONST_DECL:
10397 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10398 WALK_SUBTREE (DECL_INITIAL (t));
10399 if (DECL_CONTEXT (t)
10400 && pfd->include_nondeduced_p)
10401 WALK_SUBTREE (DECL_CONTEXT (t));
10402 break;
10403
10404 case BOUND_TEMPLATE_TEMPLATE_PARM:
10405 /* Record template parameters such as `T' inside `TT<T>'. */
10406 WALK_SUBTREE (TYPE_TI_ARGS (t));
10407 /* Fall through. */
10408
10409 case TEMPLATE_TEMPLATE_PARM:
10410 case TEMPLATE_TYPE_PARM:
10411 case TEMPLATE_PARM_INDEX:
10412 if (fn && (*fn)(t, data))
10413 return t;
10414 else if (!fn)
10415 return t;
10416 break;
10417
10418 case TEMPLATE_DECL:
10419 /* A template template parameter is encountered. */
10420 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10421 WALK_SUBTREE (TREE_TYPE (t));
10422
10423 /* Already substituted template template parameter */
10424 *walk_subtrees = 0;
10425 break;
10426
10427 case TYPENAME_TYPE:
10428 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10429 partial instantiation. */
10430 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10431 *walk_subtrees = 0;
10432 break;
10433
10434 case CONSTRUCTOR:
10435 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10436 && pfd->include_nondeduced_p)
10437 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10438 break;
10439
10440 case INDIRECT_REF:
10441 case COMPONENT_REF:
10442 /* If there's no type, then this thing must be some expression
10443 involving template parameters. */
10444 if (!fn && !TREE_TYPE (t))
10445 return error_mark_node;
10446 break;
10447
10448 case MODOP_EXPR:
10449 case CAST_EXPR:
10450 case IMPLICIT_CONV_EXPR:
10451 case REINTERPRET_CAST_EXPR:
10452 case CONST_CAST_EXPR:
10453 case STATIC_CAST_EXPR:
10454 case DYNAMIC_CAST_EXPR:
10455 case ARROW_EXPR:
10456 case DOTSTAR_EXPR:
10457 case TYPEID_EXPR:
10458 case PSEUDO_DTOR_EXPR:
10459 if (!fn)
10460 return error_mark_node;
10461 break;
10462
10463 case SCOPE_REF:
10464 if (pfd->include_nondeduced_p)
10465 WALK_SUBTREE (TREE_OPERAND (t, 0));
10466 break;
10467
10468 case REQUIRES_EXPR:
10469 {
10470 if (!fn)
10471 return error_mark_node;
10472
10473 /* Recursively walk the type of each constraint variable. */
10474 tree p = TREE_OPERAND (t, 0);
10475 while (p)
10476 {
10477 WALK_SUBTREE (TREE_TYPE (p));
10478 p = TREE_CHAIN (p);
10479 }
10480 }
10481 break;
10482
10483 default:
10484 break;
10485 }
10486
10487 #undef WALK_SUBTREE
10488
10489 /* We didn't find any template parameters we liked. */
10490 out:
10491 return result;
10492 }
10493
10494 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10495 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10496 call FN with the parameter and the DATA.
10497 If FN returns nonzero, the iteration is terminated, and
10498 for_each_template_parm returns 1. Otherwise, the iteration
10499 continues. If FN never returns a nonzero value, the value
10500 returned by for_each_template_parm is 0. If FN is NULL, it is
10501 considered to be the function which always returns 1.
10502
10503 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10504 parameters that occur in non-deduced contexts. When false, only
10505 visits those template parameters that can be deduced. */
10506
10507 static tree
10508 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10509 hash_set<tree> *visited,
10510 bool include_nondeduced_p,
10511 tree_fn_t any_fn)
10512 {
10513 struct pair_fn_data pfd;
10514 tree result;
10515
10516 /* Set up. */
10517 pfd.fn = fn;
10518 pfd.any_fn = any_fn;
10519 pfd.data = data;
10520 pfd.include_nondeduced_p = include_nondeduced_p;
10521
10522 /* Walk the tree. (Conceptually, we would like to walk without
10523 duplicates, but for_each_template_parm_r recursively calls
10524 for_each_template_parm, so we would need to reorganize a fair
10525 bit to use walk_tree_without_duplicates, so we keep our own
10526 visited list.) */
10527 if (visited)
10528 pfd.visited = visited;
10529 else
10530 pfd.visited = new hash_set<tree>;
10531 result = cp_walk_tree (&t,
10532 for_each_template_parm_r,
10533 &pfd,
10534 pfd.visited);
10535
10536 /* Clean up. */
10537 if (!visited)
10538 {
10539 delete pfd.visited;
10540 pfd.visited = 0;
10541 }
10542
10543 return result;
10544 }
10545
10546 struct find_template_parameter_info
10547 {
10548 explicit find_template_parameter_info (tree ctx_parms)
10549 : parm_list (NULL_TREE),
10550 ctx_parms (ctx_parms),
10551 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10552 {}
10553
10554 hash_set<tree> visited;
10555 hash_set<tree> parms;
10556 tree parm_list;
10557 tree ctx_parms;
10558 int max_depth;
10559 };
10560
10561 /* Appends the declaration of T to the list in DATA. */
10562
10563 static int
10564 keep_template_parm (tree t, void* data)
10565 {
10566 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10567
10568 /* Template parameters declared within the expression are not part of
10569 the parameter mapping. For example, in this concept:
10570
10571 template<typename T>
10572 concept C = requires { <expr> } -> same_as<int>;
10573
10574 the return specifier same_as<int> declares a new decltype parameter
10575 that must not be part of the parameter mapping. The same is true
10576 for generic lambda parameters, lambda template parameters, etc. */
10577 int level;
10578 int index;
10579 template_parm_level_and_index (t, &level, &index);
10580 if (level > ftpi->max_depth)
10581 return 0;
10582
10583 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10584 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10585 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10586 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10587
10588 /* This template parameter might be an argument to a cached dependent
10589 specalization that was formed earlier inside some other template, in
10590 which case the parameter is not among the ones that are in-scope.
10591 Look in CTX_PARMS to find the corresponding in-scope template
10592 parameter, and use it instead. */
10593 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
10594 t = in_scope;
10595
10596 /* Arguments like const T yield parameters like const T. This means that
10597 a template-id like X<T, const T> would yield two distinct parameters:
10598 T and const T. Adjust types to their unqualified versions. */
10599 if (TYPE_P (t))
10600 t = TYPE_MAIN_VARIANT (t);
10601 if (!ftpi->parms.add (t))
10602 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10603
10604 return 0;
10605 }
10606
10607 /* Ensure that we recursively examine certain terms that are not normally
10608 visited in for_each_template_parm_r. */
10609
10610 static int
10611 any_template_parm_r (tree t, void *data)
10612 {
10613 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10614
10615 #define WALK_SUBTREE(NODE) \
10616 do \
10617 { \
10618 for_each_template_parm (NODE, keep_template_parm, data, \
10619 &ftpi->visited, true, \
10620 any_template_parm_r); \
10621 } \
10622 while (0)
10623
10624 /* A mention of a member alias/typedef is a use of all of its template
10625 arguments, including those from the enclosing class, so we don't use
10626 alias_template_specialization_p here. */
10627 if (TYPE_P (t) && typedef_variant_p (t))
10628 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10629 WALK_SUBTREE (TI_ARGS (tinfo));
10630
10631 switch (TREE_CODE (t))
10632 {
10633 case TEMPLATE_TYPE_PARM:
10634 /* Type constraints of a placeholder type may contain parameters. */
10635 if (is_auto (t))
10636 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10637 WALK_SUBTREE (constr);
10638 break;
10639
10640 case TEMPLATE_ID_EXPR:
10641 /* Search through references to variable templates. */
10642 WALK_SUBTREE (TREE_OPERAND (t, 0));
10643 WALK_SUBTREE (TREE_OPERAND (t, 1));
10644 break;
10645
10646 case TEMPLATE_PARM_INDEX:
10647 case PARM_DECL:
10648 /* A parameter or constraint variable may also depend on a template
10649 parameter without explicitly naming it. */
10650 WALK_SUBTREE (TREE_TYPE (t));
10651 break;
10652
10653 case TEMPLATE_DECL:
10654 {
10655 /* If T is a member template that shares template parameters with
10656 ctx_parms, we need to mark all those parameters for mapping. */
10657 tree dparms = DECL_TEMPLATE_PARMS (t);
10658 tree cparms = ftpi->ctx_parms;
10659 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10660 dparms = TREE_CHAIN (dparms);
10661 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10662 cparms = TREE_CHAIN (cparms);
10663 while (dparms
10664 && (TREE_TYPE (TREE_VALUE (dparms))
10665 != TREE_TYPE (TREE_VALUE (cparms))))
10666 dparms = TREE_CHAIN (dparms),
10667 cparms = TREE_CHAIN (cparms);
10668 if (dparms)
10669 {
10670 int ddepth = TMPL_PARMS_DEPTH (dparms);
10671 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10672 for (int i = 0; i < ddepth; ++i)
10673 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10674 }
10675 }
10676 break;
10677
10678 case LAMBDA_EXPR:
10679 {
10680 /* Look in the parms and body. */
10681 tree fn = lambda_function (t);
10682 WALK_SUBTREE (TREE_TYPE (fn));
10683 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10684 }
10685 break;
10686
10687 case IDENTIFIER_NODE:
10688 if (IDENTIFIER_CONV_OP_P (t))
10689 /* The conversion-type-id of a conversion operator may be dependent. */
10690 WALK_SUBTREE (TREE_TYPE (t));
10691 break;
10692
10693 default:
10694 break;
10695 }
10696
10697 /* Keep walking. */
10698 return 0;
10699 }
10700
10701 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10702 are the template parameters in scope. */
10703
10704 tree
10705 find_template_parameters (tree t, tree ctx_parms)
10706 {
10707 if (!ctx_parms)
10708 return NULL_TREE;
10709
10710 find_template_parameter_info ftpi (ctx_parms);
10711 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10712 /*include_nondeduced*/true, any_template_parm_r);
10713 return ftpi.parm_list;
10714 }
10715
10716 /* Returns true if T depends on any template parameter. */
10717
10718 int
10719 uses_template_parms (tree t)
10720 {
10721 if (t == NULL_TREE)
10722 return false;
10723
10724 bool dependent_p;
10725 int saved_processing_template_decl;
10726
10727 saved_processing_template_decl = processing_template_decl;
10728 if (!saved_processing_template_decl)
10729 processing_template_decl = 1;
10730 if (TYPE_P (t))
10731 dependent_p = dependent_type_p (t);
10732 else if (TREE_CODE (t) == TREE_VEC)
10733 dependent_p = any_dependent_template_arguments_p (t);
10734 else if (TREE_CODE (t) == TREE_LIST)
10735 dependent_p = (uses_template_parms (TREE_VALUE (t))
10736 || uses_template_parms (TREE_CHAIN (t)));
10737 else if (TREE_CODE (t) == TYPE_DECL)
10738 dependent_p = dependent_type_p (TREE_TYPE (t));
10739 else if (t == error_mark_node)
10740 dependent_p = false;
10741 else
10742 dependent_p = value_dependent_expression_p (t);
10743
10744 processing_template_decl = saved_processing_template_decl;
10745
10746 return dependent_p;
10747 }
10748
10749 /* Returns true iff current_function_decl is an incompletely instantiated
10750 template. Useful instead of processing_template_decl because the latter
10751 is set to 0 during instantiate_non_dependent_expr. */
10752
10753 bool
10754 in_template_function (void)
10755 {
10756 tree fn = current_function_decl;
10757 bool ret;
10758 ++processing_template_decl;
10759 ret = (fn && DECL_LANG_SPECIFIC (fn)
10760 && DECL_TEMPLATE_INFO (fn)
10761 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10762 --processing_template_decl;
10763 return ret;
10764 }
10765
10766 /* Returns true if T depends on any template parameter with level LEVEL. */
10767
10768 bool
10769 uses_template_parms_level (tree t, int level)
10770 {
10771 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10772 /*include_nondeduced_p=*/true);
10773 }
10774
10775 /* Returns true if the signature of DECL depends on any template parameter from
10776 its enclosing class. */
10777
10778 bool
10779 uses_outer_template_parms (tree decl)
10780 {
10781 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10782 if (depth == 0)
10783 return false;
10784 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10785 &depth, NULL, /*include_nondeduced_p=*/true))
10786 return true;
10787 if (PRIMARY_TEMPLATE_P (decl)
10788 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10789 (DECL_TEMPLATE_PARMS (decl)),
10790 template_parm_outer_level,
10791 &depth, NULL, /*include_nondeduced_p=*/true))
10792 return true;
10793 tree ci = get_constraints (decl);
10794 if (ci)
10795 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10796 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10797 &depth, NULL, /*nondeduced*/true))
10798 return true;
10799 return false;
10800 }
10801
10802 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10803 ill-formed translation unit, i.e. a variable or function that isn't
10804 usable in a constant expression. */
10805
10806 static inline bool
10807 neglectable_inst_p (tree d)
10808 {
10809 return (d && DECL_P (d)
10810 && !undeduced_auto_decl (d)
10811 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10812 : decl_maybe_constant_var_p (d)));
10813 }
10814
10815 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10816 neglectable and instantiated from within an erroneous instantiation. */
10817
10818 static bool
10819 limit_bad_template_recursion (tree decl)
10820 {
10821 struct tinst_level *lev = current_tinst_level;
10822 int errs = errorcount + sorrycount;
10823 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10824 return false;
10825
10826 for (; lev; lev = lev->next)
10827 if (neglectable_inst_p (lev->maybe_get_node ()))
10828 break;
10829
10830 return (lev && errs > lev->errors);
10831 }
10832
10833 static int tinst_depth;
10834 extern int max_tinst_depth;
10835 int depth_reached;
10836
10837 static GTY(()) struct tinst_level *last_error_tinst_level;
10838
10839 /* We're starting to instantiate D; record the template instantiation context
10840 at LOC for diagnostics and to restore it later. */
10841
10842 bool
10843 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10844 {
10845 struct tinst_level *new_level;
10846
10847 if (tinst_depth >= max_tinst_depth)
10848 {
10849 /* Tell error.c not to try to instantiate any templates. */
10850 at_eof = 2;
10851 fatal_error (input_location,
10852 "template instantiation depth exceeds maximum of %d"
10853 " (use %<-ftemplate-depth=%> to increase the maximum)",
10854 max_tinst_depth);
10855 return false;
10856 }
10857
10858 /* If the current instantiation caused problems, don't let it instantiate
10859 anything else. Do allow deduction substitution and decls usable in
10860 constant expressions. */
10861 if (!targs && limit_bad_template_recursion (tldcl))
10862 {
10863 /* Avoid no_linkage_errors and unused function warnings for this
10864 decl. */
10865 TREE_NO_WARNING (tldcl) = 1;
10866 return false;
10867 }
10868
10869 /* When not -quiet, dump template instantiations other than functions, since
10870 announce_function will take care of those. */
10871 if (!quiet_flag && !targs
10872 && TREE_CODE (tldcl) != TREE_LIST
10873 && TREE_CODE (tldcl) != FUNCTION_DECL)
10874 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10875
10876 new_level = tinst_level_freelist ().alloc ();
10877 new_level->tldcl = tldcl;
10878 new_level->targs = targs;
10879 new_level->locus = loc;
10880 new_level->errors = errorcount + sorrycount;
10881 new_level->next = NULL;
10882 new_level->refcount = 0;
10883 set_refcount_ptr (new_level->next, current_tinst_level);
10884 set_refcount_ptr (current_tinst_level, new_level);
10885
10886 ++tinst_depth;
10887 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10888 depth_reached = tinst_depth;
10889
10890 return true;
10891 }
10892
10893 /* We're starting substitution of TMPL<ARGS>; record the template
10894 substitution context for diagnostics and to restore it later. */
10895
10896 bool
10897 push_tinst_level (tree tmpl, tree args)
10898 {
10899 return push_tinst_level_loc (tmpl, args, input_location);
10900 }
10901
10902 /* We're starting to instantiate D; record INPUT_LOCATION and the
10903 template instantiation context for diagnostics and to restore it
10904 later. */
10905
10906 bool
10907 push_tinst_level (tree d)
10908 {
10909 return push_tinst_level_loc (d, input_location);
10910 }
10911
10912 /* Likewise, but record LOC as the program location. */
10913
10914 bool
10915 push_tinst_level_loc (tree d, location_t loc)
10916 {
10917 gcc_assert (TREE_CODE (d) != TREE_LIST);
10918 return push_tinst_level_loc (d, NULL, loc);
10919 }
10920
10921 /* We're done instantiating this template; return to the instantiation
10922 context. */
10923
10924 void
10925 pop_tinst_level (void)
10926 {
10927 /* Restore the filename and line number stashed away when we started
10928 this instantiation. */
10929 input_location = current_tinst_level->locus;
10930 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10931 --tinst_depth;
10932 }
10933
10934 /* We're instantiating a deferred template; restore the template
10935 instantiation context in which the instantiation was requested, which
10936 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10937
10938 static tree
10939 reopen_tinst_level (struct tinst_level *level)
10940 {
10941 struct tinst_level *t;
10942
10943 tinst_depth = 0;
10944 for (t = level; t; t = t->next)
10945 ++tinst_depth;
10946
10947 set_refcount_ptr (current_tinst_level, level);
10948 pop_tinst_level ();
10949 if (current_tinst_level)
10950 current_tinst_level->errors = errorcount+sorrycount;
10951 return level->maybe_get_node ();
10952 }
10953
10954 /* Returns the TINST_LEVEL which gives the original instantiation
10955 context. */
10956
10957 struct tinst_level *
10958 outermost_tinst_level (void)
10959 {
10960 struct tinst_level *level = current_tinst_level;
10961 if (level)
10962 while (level->next)
10963 level = level->next;
10964 return level;
10965 }
10966
10967 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10968 vector of template arguments, as for tsubst.
10969
10970 Returns an appropriate tsubst'd friend declaration. */
10971
10972 static tree
10973 tsubst_friend_function (tree decl, tree args)
10974 {
10975 tree new_friend;
10976
10977 if (TREE_CODE (decl) == FUNCTION_DECL
10978 && DECL_TEMPLATE_INSTANTIATION (decl)
10979 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10980 /* This was a friend declared with an explicit template
10981 argument list, e.g.:
10982
10983 friend void f<>(T);
10984
10985 to indicate that f was a template instantiation, not a new
10986 function declaration. Now, we have to figure out what
10987 instantiation of what template. */
10988 {
10989 tree template_id, arglist, fns;
10990 tree new_args;
10991 tree tmpl;
10992 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10993
10994 /* Friend functions are looked up in the containing namespace scope.
10995 We must enter that scope, to avoid finding member functions of the
10996 current class with same name. */
10997 push_nested_namespace (ns);
10998 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10999 tf_warning_or_error, NULL_TREE,
11000 /*integral_constant_expression_p=*/false);
11001 pop_nested_namespace (ns);
11002 arglist = tsubst (DECL_TI_ARGS (decl), args,
11003 tf_warning_or_error, NULL_TREE);
11004 template_id = lookup_template_function (fns, arglist);
11005
11006 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11007 tmpl = determine_specialization (template_id, new_friend,
11008 &new_args,
11009 /*need_member_template=*/0,
11010 TREE_VEC_LENGTH (args),
11011 tsk_none);
11012 return instantiate_template (tmpl, new_args, tf_error);
11013 }
11014
11015 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11016 if (new_friend == error_mark_node)
11017 return error_mark_node;
11018
11019 /* The NEW_FRIEND will look like an instantiation, to the
11020 compiler, but is not an instantiation from the point of view of
11021 the language. For example, we might have had:
11022
11023 template <class T> struct S {
11024 template <class U> friend void f(T, U);
11025 };
11026
11027 Then, in S<int>, template <class U> void f(int, U) is not an
11028 instantiation of anything. */
11029
11030 DECL_USE_TEMPLATE (new_friend) = 0;
11031 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11032 {
11033 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11034 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11035 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11036
11037 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11038 match in decls_match. */
11039 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11040 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11041 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11042 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11043 }
11044
11045 /* The mangled name for the NEW_FRIEND is incorrect. The function
11046 is not a template instantiation and should not be mangled like
11047 one. Therefore, we forget the mangling here; we'll recompute it
11048 later if we need it. */
11049 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11050 {
11051 SET_DECL_RTL (new_friend, NULL);
11052 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11053 }
11054
11055 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11056 {
11057 tree old_decl;
11058 tree ns;
11059
11060 /* We must save some information from NEW_FRIEND before calling
11061 duplicate decls since that function will free NEW_FRIEND if
11062 possible. */
11063 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11064 tree new_friend_result_template_info = NULL_TREE;
11065 bool new_friend_is_defn =
11066 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11067 (template_for_substitution (new_friend)))
11068 != NULL_TREE);
11069 tree not_tmpl = new_friend;
11070
11071 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11072 {
11073 /* This declaration is a `primary' template. */
11074 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11075
11076 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11077 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11078 }
11079
11080 /* Inside pushdecl_namespace_level, we will push into the
11081 current namespace. However, the friend function should go
11082 into the namespace of the template. */
11083 ns = decl_namespace_context (new_friend);
11084 push_nested_namespace (ns);
11085 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
11086 pop_nested_namespace (ns);
11087
11088 if (old_decl == error_mark_node)
11089 return error_mark_node;
11090
11091 if (old_decl != new_friend)
11092 {
11093 /* This new friend declaration matched an existing
11094 declaration. For example, given:
11095
11096 template <class T> void f(T);
11097 template <class U> class C {
11098 template <class T> friend void f(T) {}
11099 };
11100
11101 the friend declaration actually provides the definition
11102 of `f', once C has been instantiated for some type. So,
11103 old_decl will be the out-of-class template declaration,
11104 while new_friend is the in-class definition.
11105
11106 But, if `f' was called before this point, the
11107 instantiation of `f' will have DECL_TI_ARGS corresponding
11108 to `T' but not to `U', references to which might appear
11109 in the definition of `f'. Previously, the most general
11110 template for an instantiation of `f' was the out-of-class
11111 version; now it is the in-class version. Therefore, we
11112 run through all specialization of `f', adding to their
11113 DECL_TI_ARGS appropriately. In particular, they need a
11114 new set of outer arguments, corresponding to the
11115 arguments for this class instantiation.
11116
11117 The same situation can arise with something like this:
11118
11119 friend void f(int);
11120 template <class T> class C {
11121 friend void f(T) {}
11122 };
11123
11124 when `C<int>' is instantiated. Now, `f(int)' is defined
11125 in the class. */
11126
11127 if (!new_friend_is_defn)
11128 /* On the other hand, if the in-class declaration does
11129 *not* provide a definition, then we don't want to alter
11130 existing definitions. We can just leave everything
11131 alone. */
11132 ;
11133 else
11134 {
11135 tree new_template = TI_TEMPLATE (new_friend_template_info);
11136 tree new_args = TI_ARGS (new_friend_template_info);
11137
11138 /* Overwrite whatever template info was there before, if
11139 any, with the new template information pertaining to
11140 the declaration. */
11141 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11142
11143 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11144 {
11145 /* We should have called reregister_specialization in
11146 duplicate_decls. */
11147 gcc_assert (retrieve_specialization (new_template,
11148 new_args, 0)
11149 == old_decl);
11150
11151 /* Instantiate it if the global has already been used. */
11152 if (DECL_ODR_USED (old_decl))
11153 instantiate_decl (old_decl, /*defer_ok=*/true,
11154 /*expl_inst_class_mem_p=*/false);
11155 }
11156 else
11157 {
11158 tree t;
11159
11160 /* Indicate that the old function template is a partial
11161 instantiation. */
11162 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11163 = new_friend_result_template_info;
11164
11165 gcc_assert (new_template
11166 == most_general_template (new_template));
11167 gcc_assert (new_template != old_decl);
11168
11169 /* Reassign any specializations already in the hash table
11170 to the new more general template, and add the
11171 additional template args. */
11172 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11173 t != NULL_TREE;
11174 t = TREE_CHAIN (t))
11175 {
11176 tree spec = TREE_VALUE (t);
11177 spec_entry elt;
11178
11179 elt.tmpl = old_decl;
11180 elt.args = DECL_TI_ARGS (spec);
11181 elt.spec = NULL_TREE;
11182
11183 decl_specializations->remove_elt (&elt);
11184
11185 DECL_TI_ARGS (spec)
11186 = add_outermost_template_args (new_args,
11187 DECL_TI_ARGS (spec));
11188
11189 register_specialization
11190 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11191
11192 }
11193 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11194 }
11195 }
11196
11197 /* The information from NEW_FRIEND has been merged into OLD_DECL
11198 by duplicate_decls. */
11199 new_friend = old_decl;
11200 }
11201 }
11202 else
11203 {
11204 tree context = DECL_CONTEXT (new_friend);
11205 bool dependent_p;
11206
11207 /* In the code
11208 template <class T> class C {
11209 template <class U> friend void C1<U>::f (); // case 1
11210 friend void C2<T>::f (); // case 2
11211 };
11212 we only need to make sure CONTEXT is a complete type for
11213 case 2. To distinguish between the two cases, we note that
11214 CONTEXT of case 1 remains dependent type after tsubst while
11215 this isn't true for case 2. */
11216 ++processing_template_decl;
11217 dependent_p = dependent_type_p (context);
11218 --processing_template_decl;
11219
11220 if (!dependent_p
11221 && !complete_type_or_else (context, NULL_TREE))
11222 return error_mark_node;
11223
11224 if (COMPLETE_TYPE_P (context))
11225 {
11226 tree fn = new_friend;
11227 /* do_friend adds the TEMPLATE_DECL for any member friend
11228 template even if it isn't a member template, i.e.
11229 template <class T> friend A<T>::f();
11230 Look through it in that case. */
11231 if (TREE_CODE (fn) == TEMPLATE_DECL
11232 && !PRIMARY_TEMPLATE_P (fn))
11233 fn = DECL_TEMPLATE_RESULT (fn);
11234 /* Check to see that the declaration is really present, and,
11235 possibly obtain an improved declaration. */
11236 fn = check_classfn (context, fn, NULL_TREE);
11237
11238 if (fn)
11239 new_friend = fn;
11240 }
11241 }
11242
11243 return new_friend;
11244 }
11245
11246 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11247 template arguments, as for tsubst.
11248
11249 Returns an appropriate tsubst'd friend type or error_mark_node on
11250 failure. */
11251
11252 static tree
11253 tsubst_friend_class (tree friend_tmpl, tree args)
11254 {
11255 tree tmpl;
11256
11257 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11258 {
11259 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11260 return TREE_TYPE (tmpl);
11261 }
11262
11263 tree context = CP_DECL_CONTEXT (friend_tmpl);
11264 if (TREE_CODE (context) == NAMESPACE_DECL)
11265 push_nested_namespace (context);
11266 else
11267 {
11268 context = tsubst (context, args, tf_error, NULL_TREE);
11269 push_nested_class (context);
11270 }
11271
11272 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11273 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11274
11275 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11276 {
11277 /* The friend template has already been declared. Just
11278 check to see that the declarations match, and install any new
11279 default parameters. We must tsubst the default parameters,
11280 of course. We only need the innermost template parameters
11281 because that is all that redeclare_class_template will look
11282 at. */
11283 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11284 > TMPL_ARGS_DEPTH (args))
11285 {
11286 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11287 args, tf_warning_or_error);
11288 location_t saved_input_location = input_location;
11289 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11290 tree cons = get_constraints (tmpl);
11291 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11292 input_location = saved_input_location;
11293 }
11294 }
11295 else
11296 {
11297 /* The friend template has not already been declared. In this
11298 case, the instantiation of the template class will cause the
11299 injection of this template into the namespace scope. */
11300 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11301
11302 if (tmpl != error_mark_node)
11303 {
11304 /* The new TMPL is not an instantiation of anything, so we
11305 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11306 for the new type because that is supposed to be the
11307 corresponding template decl, i.e., TMPL. */
11308 DECL_USE_TEMPLATE (tmpl) = 0;
11309 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11310 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11311 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11312 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11313
11314 /* It is hidden. */
11315 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11316 DECL_ANTICIPATED (tmpl)
11317 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11318
11319 /* Substitute into and set the constraints on the new declaration. */
11320 if (tree ci = get_constraints (friend_tmpl))
11321 {
11322 ++processing_template_decl;
11323 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11324 DECL_FRIEND_CONTEXT (friend_tmpl));
11325 --processing_template_decl;
11326 set_constraints (tmpl, ci);
11327 }
11328
11329 /* Inject this template into the enclosing namspace scope. */
11330 tmpl = pushdecl_namespace_level (tmpl, true);
11331 }
11332 }
11333
11334 if (TREE_CODE (context) == NAMESPACE_DECL)
11335 pop_nested_namespace (context);
11336 else
11337 pop_nested_class ();
11338
11339 return TREE_TYPE (tmpl);
11340 }
11341
11342 /* Returns zero if TYPE cannot be completed later due to circularity.
11343 Otherwise returns one. */
11344
11345 static int
11346 can_complete_type_without_circularity (tree type)
11347 {
11348 if (type == NULL_TREE || type == error_mark_node)
11349 return 0;
11350 else if (COMPLETE_TYPE_P (type))
11351 return 1;
11352 else if (TREE_CODE (type) == ARRAY_TYPE)
11353 return can_complete_type_without_circularity (TREE_TYPE (type));
11354 else if (CLASS_TYPE_P (type)
11355 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11356 return 0;
11357 else
11358 return 1;
11359 }
11360
11361 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11362 tsubst_flags_t, tree);
11363
11364 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11365 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11366
11367 static tree
11368 tsubst_attribute (tree t, tree *decl_p, tree args,
11369 tsubst_flags_t complain, tree in_decl)
11370 {
11371 gcc_assert (ATTR_IS_DEPENDENT (t));
11372
11373 tree val = TREE_VALUE (t);
11374 if (val == NULL_TREE)
11375 /* Nothing to do. */;
11376 else if ((flag_openmp || flag_openmp_simd)
11377 && is_attribute_p ("omp declare simd",
11378 get_attribute_name (t)))
11379 {
11380 tree clauses = TREE_VALUE (val);
11381 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11382 complain, in_decl);
11383 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11384 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11385 tree parms = DECL_ARGUMENTS (*decl_p);
11386 clauses
11387 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11388 if (clauses)
11389 val = build_tree_list (NULL_TREE, clauses);
11390 else
11391 val = NULL_TREE;
11392 }
11393 else if (flag_openmp
11394 && is_attribute_p ("omp declare variant base",
11395 get_attribute_name (t)))
11396 {
11397 ++cp_unevaluated_operand;
11398 tree varid
11399 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11400 in_decl, /*integral_constant_expression_p=*/false);
11401 --cp_unevaluated_operand;
11402 tree chain = TREE_CHAIN (val);
11403 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11404 tree ctx = copy_list (TREE_VALUE (val));
11405 tree simd = get_identifier ("simd");
11406 tree score = get_identifier (" score");
11407 tree condition = get_identifier ("condition");
11408 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11409 {
11410 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11411 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11412 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11413 {
11414 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11415 {
11416 tree clauses = TREE_VALUE (t2);
11417 clauses = tsubst_omp_clauses (clauses,
11418 C_ORT_OMP_DECLARE_SIMD, args,
11419 complain, in_decl);
11420 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11421 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11422 TREE_VALUE (t2) = clauses;
11423 }
11424 else
11425 {
11426 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11427 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11428 if (TREE_VALUE (t3))
11429 {
11430 bool allow_string
11431 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11432 && TREE_PURPOSE (t3) != score);
11433 tree v = TREE_VALUE (t3);
11434 if (TREE_CODE (v) == STRING_CST && allow_string)
11435 continue;
11436 v = tsubst_expr (v, args, complain, in_decl, true);
11437 v = fold_non_dependent_expr (v);
11438 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11439 || (TREE_PURPOSE (t3) == score
11440 ? TREE_CODE (v) != INTEGER_CST
11441 : !tree_fits_shwi_p (v)))
11442 {
11443 location_t loc
11444 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11445 match_loc);
11446 if (TREE_PURPOSE (t3) == score)
11447 error_at (loc, "score argument must be "
11448 "constant integer expression");
11449 else if (allow_string)
11450 error_at (loc, "property must be constant "
11451 "integer expression or string "
11452 "literal");
11453 else
11454 error_at (loc, "property must be constant "
11455 "integer expression");
11456 return NULL_TREE;
11457 }
11458 else if (TREE_PURPOSE (t3) == score
11459 && tree_int_cst_sgn (v) < 0)
11460 {
11461 location_t loc
11462 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11463 match_loc);
11464 error_at (loc, "score argument must be "
11465 "non-negative");
11466 return NULL_TREE;
11467 }
11468 TREE_VALUE (t3) = v;
11469 }
11470 }
11471 }
11472 }
11473 val = tree_cons (varid, ctx, chain);
11474 }
11475 /* If the first attribute argument is an identifier, don't
11476 pass it through tsubst. Attributes like mode, format,
11477 cleanup and several target specific attributes expect it
11478 unmodified. */
11479 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11480 {
11481 tree chain
11482 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11483 /*integral_constant_expression_p=*/false);
11484 if (chain != TREE_CHAIN (val))
11485 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11486 }
11487 else if (PACK_EXPANSION_P (val))
11488 {
11489 /* An attribute pack expansion. */
11490 tree purp = TREE_PURPOSE (t);
11491 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11492 if (pack == error_mark_node)
11493 return error_mark_node;
11494 int len = TREE_VEC_LENGTH (pack);
11495 tree list = NULL_TREE;
11496 tree *q = &list;
11497 for (int i = 0; i < len; ++i)
11498 {
11499 tree elt = TREE_VEC_ELT (pack, i);
11500 *q = build_tree_list (purp, elt);
11501 q = &TREE_CHAIN (*q);
11502 }
11503 return list;
11504 }
11505 else
11506 val = tsubst_expr (val, args, complain, in_decl,
11507 /*integral_constant_expression_p=*/false);
11508
11509 if (val != TREE_VALUE (t))
11510 return build_tree_list (TREE_PURPOSE (t), val);
11511 return t;
11512 }
11513
11514 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11515 unchanged or a new TREE_LIST chain. */
11516
11517 static tree
11518 tsubst_attributes (tree attributes, tree args,
11519 tsubst_flags_t complain, tree in_decl)
11520 {
11521 tree last_dep = NULL_TREE;
11522
11523 for (tree t = attributes; t; t = TREE_CHAIN (t))
11524 if (ATTR_IS_DEPENDENT (t))
11525 {
11526 last_dep = t;
11527 attributes = copy_list (attributes);
11528 break;
11529 }
11530
11531 if (last_dep)
11532 for (tree *p = &attributes; *p; )
11533 {
11534 tree t = *p;
11535 if (ATTR_IS_DEPENDENT (t))
11536 {
11537 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11538 if (subst != t)
11539 {
11540 *p = subst;
11541 while (*p)
11542 p = &TREE_CHAIN (*p);
11543 *p = TREE_CHAIN (t);
11544 continue;
11545 }
11546 }
11547 p = &TREE_CHAIN (*p);
11548 }
11549
11550 return attributes;
11551 }
11552
11553 /* Apply any attributes which had to be deferred until instantiation
11554 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11555 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11556
11557 static void
11558 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11559 tree args, tsubst_flags_t complain, tree in_decl)
11560 {
11561 tree last_dep = NULL_TREE;
11562 tree t;
11563 tree *p;
11564
11565 if (attributes == NULL_TREE)
11566 return;
11567
11568 if (DECL_P (*decl_p))
11569 {
11570 if (TREE_TYPE (*decl_p) == error_mark_node)
11571 return;
11572 p = &DECL_ATTRIBUTES (*decl_p);
11573 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11574 to our attributes parameter. */
11575 gcc_assert (*p == attributes);
11576 }
11577 else
11578 {
11579 p = &TYPE_ATTRIBUTES (*decl_p);
11580 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11581 lookup_template_class_1, and should be preserved. */
11582 gcc_assert (*p != attributes);
11583 while (*p)
11584 p = &TREE_CHAIN (*p);
11585 }
11586
11587 for (t = attributes; t; t = TREE_CHAIN (t))
11588 if (ATTR_IS_DEPENDENT (t))
11589 {
11590 last_dep = t;
11591 attributes = copy_list (attributes);
11592 break;
11593 }
11594
11595 *p = attributes;
11596 if (last_dep)
11597 {
11598 tree late_attrs = NULL_TREE;
11599 tree *q = &late_attrs;
11600
11601 for (; *p; )
11602 {
11603 t = *p;
11604 if (ATTR_IS_DEPENDENT (t))
11605 {
11606 *p = TREE_CHAIN (t);
11607 TREE_CHAIN (t) = NULL_TREE;
11608 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11609 while (*q)
11610 q = &TREE_CHAIN (*q);
11611 }
11612 else
11613 p = &TREE_CHAIN (t);
11614 }
11615
11616 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11617 }
11618 }
11619
11620 /* The template TMPL is being instantiated with the template arguments TARGS.
11621 Perform the access checks that we deferred when parsing the template. */
11622
11623 static void
11624 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11625 {
11626 unsigned i;
11627 deferred_access_check *chk;
11628
11629 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11630 return;
11631
11632 if (vec<deferred_access_check, va_gc> *access_checks
11633 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11634 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11635 {
11636 tree decl = chk->decl;
11637 tree diag_decl = chk->diag_decl;
11638 tree type_scope = TREE_TYPE (chk->binfo);
11639
11640 if (uses_template_parms (type_scope))
11641 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11642
11643 /* Make access check error messages point to the location
11644 of the use of the typedef. */
11645 iloc_sentinel ils (chk->loc);
11646 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11647 decl, diag_decl, tf_warning_or_error);
11648 }
11649 }
11650
11651 static tree
11652 instantiate_class_template_1 (tree type)
11653 {
11654 tree templ, args, pattern, t, member;
11655 tree typedecl;
11656 tree pbinfo;
11657 tree base_list;
11658 unsigned int saved_maximum_field_alignment;
11659 tree fn_context;
11660
11661 if (type == error_mark_node)
11662 return error_mark_node;
11663
11664 if (COMPLETE_OR_OPEN_TYPE_P (type)
11665 || uses_template_parms (type))
11666 return type;
11667
11668 /* Figure out which template is being instantiated. */
11669 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11670 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11671
11672 /* Mark the type as in the process of being defined. */
11673 TYPE_BEING_DEFINED (type) = 1;
11674
11675 /* We may be in the middle of deferred access check. Disable
11676 it now. */
11677 deferring_access_check_sentinel acs (dk_no_deferred);
11678
11679 /* Determine what specialization of the original template to
11680 instantiate. */
11681 t = most_specialized_partial_spec (type, tf_warning_or_error);
11682 if (t == error_mark_node)
11683 return error_mark_node;
11684 else if (t)
11685 {
11686 /* This TYPE is actually an instantiation of a partial
11687 specialization. We replace the innermost set of ARGS with
11688 the arguments appropriate for substitution. For example,
11689 given:
11690
11691 template <class T> struct S {};
11692 template <class T> struct S<T*> {};
11693
11694 and supposing that we are instantiating S<int*>, ARGS will
11695 presently be {int*} -- but we need {int}. */
11696 pattern = TREE_TYPE (t);
11697 args = TREE_PURPOSE (t);
11698 }
11699 else
11700 {
11701 pattern = TREE_TYPE (templ);
11702 args = CLASSTYPE_TI_ARGS (type);
11703 }
11704
11705 /* If the template we're instantiating is incomplete, then clearly
11706 there's nothing we can do. */
11707 if (!COMPLETE_TYPE_P (pattern))
11708 {
11709 /* We can try again later. */
11710 TYPE_BEING_DEFINED (type) = 0;
11711 return type;
11712 }
11713
11714 /* If we've recursively instantiated too many templates, stop. */
11715 if (! push_tinst_level (type))
11716 return type;
11717
11718 int saved_unevaluated_operand = cp_unevaluated_operand;
11719 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11720
11721 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11722 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11723 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11724 fn_context = error_mark_node;
11725 if (!fn_context)
11726 push_to_top_level ();
11727 else
11728 {
11729 cp_unevaluated_operand = 0;
11730 c_inhibit_evaluation_warnings = 0;
11731 }
11732 /* Use #pragma pack from the template context. */
11733 saved_maximum_field_alignment = maximum_field_alignment;
11734 maximum_field_alignment = TYPE_PRECISION (pattern);
11735
11736 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11737
11738 /* Set the input location to the most specialized template definition.
11739 This is needed if tsubsting causes an error. */
11740 typedecl = TYPE_MAIN_DECL (pattern);
11741 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11742 DECL_SOURCE_LOCATION (typedecl);
11743
11744 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11745 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11746 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11747 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11748 if (ANON_AGGR_TYPE_P (pattern))
11749 SET_ANON_AGGR_TYPE_P (type);
11750 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11751 {
11752 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11753 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11754 /* Adjust visibility for template arguments. */
11755 determine_visibility (TYPE_MAIN_DECL (type));
11756 }
11757 if (CLASS_TYPE_P (type))
11758 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11759
11760 pbinfo = TYPE_BINFO (pattern);
11761
11762 /* We should never instantiate a nested class before its enclosing
11763 class; we need to look up the nested class by name before we can
11764 instantiate it, and that lookup should instantiate the enclosing
11765 class. */
11766 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11767 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11768
11769 base_list = NULL_TREE;
11770 if (BINFO_N_BASE_BINFOS (pbinfo))
11771 {
11772 tree pbase_binfo;
11773 tree pushed_scope;
11774 int i;
11775
11776 /* We must enter the scope containing the type, as that is where
11777 the accessibility of types named in dependent bases are
11778 looked up from. */
11779 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11780
11781 /* Substitute into each of the bases to determine the actual
11782 basetypes. */
11783 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11784 {
11785 tree base;
11786 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11787 tree expanded_bases = NULL_TREE;
11788 int idx, len = 1;
11789
11790 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11791 {
11792 expanded_bases =
11793 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11794 args, tf_error, NULL_TREE);
11795 if (expanded_bases == error_mark_node)
11796 continue;
11797
11798 len = TREE_VEC_LENGTH (expanded_bases);
11799 }
11800
11801 for (idx = 0; idx < len; idx++)
11802 {
11803 if (expanded_bases)
11804 /* Extract the already-expanded base class. */
11805 base = TREE_VEC_ELT (expanded_bases, idx);
11806 else
11807 /* Substitute to figure out the base class. */
11808 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11809 NULL_TREE);
11810
11811 if (base == error_mark_node)
11812 continue;
11813
11814 base_list = tree_cons (access, base, base_list);
11815 if (BINFO_VIRTUAL_P (pbase_binfo))
11816 TREE_TYPE (base_list) = integer_type_node;
11817 }
11818 }
11819
11820 /* The list is now in reverse order; correct that. */
11821 base_list = nreverse (base_list);
11822
11823 if (pushed_scope)
11824 pop_scope (pushed_scope);
11825 }
11826 /* Now call xref_basetypes to set up all the base-class
11827 information. */
11828 xref_basetypes (type, base_list);
11829
11830 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11831 (int) ATTR_FLAG_TYPE_IN_PLACE,
11832 args, tf_error, NULL_TREE);
11833 fixup_attribute_variants (type);
11834
11835 /* Now that our base classes are set up, enter the scope of the
11836 class, so that name lookups into base classes, etc. will work
11837 correctly. This is precisely analogous to what we do in
11838 begin_class_definition when defining an ordinary non-template
11839 class, except we also need to push the enclosing classes. */
11840 push_nested_class (type);
11841
11842 /* Now members are processed in the order of declaration. */
11843 for (member = CLASSTYPE_DECL_LIST (pattern);
11844 member; member = TREE_CHAIN (member))
11845 {
11846 tree t = TREE_VALUE (member);
11847
11848 if (TREE_PURPOSE (member))
11849 {
11850 if (TYPE_P (t))
11851 {
11852 if (LAMBDA_TYPE_P (t))
11853 /* A closure type for a lambda in an NSDMI or default argument.
11854 Ignore it; it will be regenerated when needed. */
11855 continue;
11856
11857 /* Build new CLASSTYPE_NESTED_UTDS. */
11858 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11859 && TYPE_LANG_SPECIFIC (t)
11860 && CLASSTYPE_IS_TEMPLATE (t));
11861
11862 /* If the member is a class template, then -- even after
11863 substitution -- there may be dependent types in the
11864 template argument list for the class. We increment
11865 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11866 that function will assume that no types are dependent
11867 when outside of a template. */
11868 if (class_template_p)
11869 ++processing_template_decl;
11870 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11871 if (class_template_p)
11872 --processing_template_decl;
11873 if (newtag == error_mark_node)
11874 continue;
11875
11876 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11877 {
11878 tree name = TYPE_IDENTIFIER (t);
11879
11880 if (class_template_p)
11881 /* Unfortunately, lookup_template_class sets
11882 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11883 instantiation (i.e., for the type of a member
11884 template class nested within a template class.)
11885 This behavior is required for
11886 maybe_process_partial_specialization to work
11887 correctly, but is not accurate in this case;
11888 the TAG is not an instantiation of anything.
11889 (The corresponding TEMPLATE_DECL is an
11890 instantiation, but the TYPE is not.) */
11891 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11892
11893 /* Now, we call pushtag to put this NEWTAG into the scope of
11894 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11895 pushtag calling push_template_decl. We don't have to do
11896 this for enums because it will already have been done in
11897 tsubst_enum. */
11898 if (name)
11899 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11900 pushtag (name, newtag, /*tag_scope=*/ts_current);
11901 }
11902 }
11903 else if (DECL_DECLARES_FUNCTION_P (t))
11904 {
11905 tree r;
11906
11907 if (TREE_CODE (t) == TEMPLATE_DECL)
11908 ++processing_template_decl;
11909 r = tsubst (t, args, tf_error, NULL_TREE);
11910 if (TREE_CODE (t) == TEMPLATE_DECL)
11911 --processing_template_decl;
11912 set_current_access_from_decl (r);
11913 finish_member_declaration (r);
11914 /* Instantiate members marked with attribute used. */
11915 if (r != error_mark_node && DECL_PRESERVE_P (r))
11916 mark_used (r);
11917 if (TREE_CODE (r) == FUNCTION_DECL
11918 && DECL_OMP_DECLARE_REDUCTION_P (r))
11919 cp_check_omp_declare_reduction (r);
11920 }
11921 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11922 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11923 /* A closure type for a lambda in an NSDMI or default argument.
11924 Ignore it; it will be regenerated when needed. */;
11925 else
11926 {
11927 /* Build new TYPE_FIELDS. */
11928 if (TREE_CODE (t) == STATIC_ASSERT)
11929 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11930 /*integral_constant_expression_p=*/true);
11931 else if (TREE_CODE (t) != CONST_DECL)
11932 {
11933 tree r;
11934 tree vec = NULL_TREE;
11935 int len = 1;
11936
11937 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11938 /* The file and line for this declaration, to
11939 assist in error message reporting. Since we
11940 called push_tinst_level above, we don't need to
11941 restore these. */
11942 input_location = DECL_SOURCE_LOCATION (t);
11943
11944 if (TREE_CODE (t) == TEMPLATE_DECL)
11945 ++processing_template_decl;
11946 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11947 if (TREE_CODE (t) == TEMPLATE_DECL)
11948 --processing_template_decl;
11949
11950 if (TREE_CODE (r) == TREE_VEC)
11951 {
11952 /* A capture pack became multiple fields. */
11953 vec = r;
11954 len = TREE_VEC_LENGTH (vec);
11955 }
11956
11957 for (int i = 0; i < len; ++i)
11958 {
11959 if (vec)
11960 r = TREE_VEC_ELT (vec, i);
11961 if (VAR_P (r))
11962 {
11963 /* In [temp.inst]:
11964
11965 [t]he initialization (and any associated
11966 side-effects) of a static data member does
11967 not occur unless the static data member is
11968 itself used in a way that requires the
11969 definition of the static data member to
11970 exist.
11971
11972 Therefore, we do not substitute into the
11973 initialized for the static data member here. */
11974 finish_static_data_member_decl
11975 (r,
11976 /*init=*/NULL_TREE,
11977 /*init_const_expr_p=*/false,
11978 /*asmspec_tree=*/NULL_TREE,
11979 /*flags=*/0);
11980 /* Instantiate members marked with attribute used. */
11981 if (r != error_mark_node && DECL_PRESERVE_P (r))
11982 mark_used (r);
11983 }
11984 else if (TREE_CODE (r) == FIELD_DECL)
11985 {
11986 /* Determine whether R has a valid type and can be
11987 completed later. If R is invalid, then its type
11988 is replaced by error_mark_node. */
11989 tree rtype = TREE_TYPE (r);
11990 if (can_complete_type_without_circularity (rtype))
11991 complete_type (rtype);
11992
11993 if (!complete_or_array_type_p (rtype))
11994 {
11995 /* If R's type couldn't be completed and
11996 it isn't a flexible array member (whose
11997 type is incomplete by definition) give
11998 an error. */
11999 cxx_incomplete_type_error (r, rtype);
12000 TREE_TYPE (r) = error_mark_node;
12001 }
12002 else if (TREE_CODE (rtype) == ARRAY_TYPE
12003 && TYPE_DOMAIN (rtype) == NULL_TREE
12004 && (TREE_CODE (type) == UNION_TYPE
12005 || TREE_CODE (type) == QUAL_UNION_TYPE))
12006 {
12007 error ("flexible array member %qD in union", r);
12008 TREE_TYPE (r) = error_mark_node;
12009 }
12010 else if (!verify_type_context (input_location,
12011 TCTX_FIELD, rtype))
12012 TREE_TYPE (r) = error_mark_node;
12013 }
12014
12015 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
12016 such a thing will already have been added to the field
12017 list by tsubst_enum in finish_member_declaration in the
12018 CLASSTYPE_NESTED_UTDS case above. */
12019 if (!(TREE_CODE (r) == TYPE_DECL
12020 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12021 && DECL_ARTIFICIAL (r)))
12022 {
12023 set_current_access_from_decl (r);
12024 finish_member_declaration (r);
12025 }
12026 }
12027 }
12028 }
12029 }
12030 else
12031 {
12032 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12033 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12034 {
12035 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12036
12037 tree friend_type = t;
12038 bool adjust_processing_template_decl = false;
12039
12040 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12041 {
12042 /* template <class T> friend class C; */
12043 friend_type = tsubst_friend_class (friend_type, args);
12044 adjust_processing_template_decl = true;
12045 }
12046 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12047 {
12048 /* template <class T> friend class C::D; */
12049 friend_type = tsubst (friend_type, args,
12050 tf_warning_or_error, NULL_TREE);
12051 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12052 friend_type = TREE_TYPE (friend_type);
12053 adjust_processing_template_decl = true;
12054 }
12055 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12056 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12057 {
12058 /* This could be either
12059
12060 friend class T::C;
12061
12062 when dependent_type_p is false or
12063
12064 template <class U> friend class T::C;
12065
12066 otherwise. */
12067 /* Bump processing_template_decl in case this is something like
12068 template <class T> friend struct A<T>::B. */
12069 ++processing_template_decl;
12070 friend_type = tsubst (friend_type, args,
12071 tf_warning_or_error, NULL_TREE);
12072 if (dependent_type_p (friend_type))
12073 adjust_processing_template_decl = true;
12074 --processing_template_decl;
12075 }
12076 else if (uses_template_parms (friend_type))
12077 /* friend class C<T>; */
12078 friend_type = tsubst (friend_type, args,
12079 tf_warning_or_error, NULL_TREE);
12080 /* Otherwise it's
12081
12082 friend class C;
12083
12084 where C is already declared or
12085
12086 friend class C<int>;
12087
12088 We don't have to do anything in these cases. */
12089
12090 if (adjust_processing_template_decl)
12091 /* Trick make_friend_class into realizing that the friend
12092 we're adding is a template, not an ordinary class. It's
12093 important that we use make_friend_class since it will
12094 perform some error-checking and output cross-reference
12095 information. */
12096 ++processing_template_decl;
12097
12098 if (friend_type != error_mark_node)
12099 make_friend_class (type, friend_type, /*complain=*/false);
12100
12101 if (adjust_processing_template_decl)
12102 --processing_template_decl;
12103 }
12104 else
12105 {
12106 /* Build new DECL_FRIENDLIST. */
12107 tree r;
12108
12109 /* The file and line for this declaration, to
12110 assist in error message reporting. Since we
12111 called push_tinst_level above, we don't need to
12112 restore these. */
12113 input_location = DECL_SOURCE_LOCATION (t);
12114
12115 if (TREE_CODE (t) == TEMPLATE_DECL)
12116 {
12117 ++processing_template_decl;
12118 push_deferring_access_checks (dk_no_check);
12119 }
12120
12121 r = tsubst_friend_function (t, args);
12122 add_friend (type, r, /*complain=*/false);
12123 if (TREE_CODE (t) == TEMPLATE_DECL)
12124 {
12125 pop_deferring_access_checks ();
12126 --processing_template_decl;
12127 }
12128 }
12129 }
12130 }
12131
12132 if (fn_context)
12133 {
12134 /* Restore these before substituting into the lambda capture
12135 initializers. */
12136 cp_unevaluated_operand = saved_unevaluated_operand;
12137 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12138 }
12139
12140 /* Set the file and line number information to whatever is given for
12141 the class itself. This puts error messages involving generated
12142 implicit functions at a predictable point, and the same point
12143 that would be used for non-template classes. */
12144 input_location = DECL_SOURCE_LOCATION (typedecl);
12145
12146 unreverse_member_declarations (type);
12147 finish_struct_1 (type);
12148 TYPE_BEING_DEFINED (type) = 0;
12149
12150 /* We don't instantiate default arguments for member functions. 14.7.1:
12151
12152 The implicit instantiation of a class template specialization causes
12153 the implicit instantiation of the declarations, but not of the
12154 definitions or default arguments, of the class member functions,
12155 member classes, static data members and member templates.... */
12156
12157 perform_instantiation_time_access_checks (pattern, args);
12158 perform_deferred_access_checks (tf_warning_or_error);
12159 pop_nested_class ();
12160 maximum_field_alignment = saved_maximum_field_alignment;
12161 if (!fn_context)
12162 pop_from_top_level ();
12163 pop_tinst_level ();
12164
12165 /* The vtable for a template class can be emitted in any translation
12166 unit in which the class is instantiated. When there is no key
12167 method, however, finish_struct_1 will already have added TYPE to
12168 the keyed_classes. */
12169 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12170 vec_safe_push (keyed_classes, type);
12171
12172 return type;
12173 }
12174
12175 /* Wrapper for instantiate_class_template_1. */
12176
12177 tree
12178 instantiate_class_template (tree type)
12179 {
12180 tree ret;
12181 timevar_push (TV_TEMPLATE_INST);
12182 ret = instantiate_class_template_1 (type);
12183 timevar_pop (TV_TEMPLATE_INST);
12184 return ret;
12185 }
12186
12187 tree
12188 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12189 {
12190 tree r;
12191
12192 if (!t)
12193 r = t;
12194 else if (TYPE_P (t))
12195 r = tsubst (t, args, complain, in_decl);
12196 else
12197 {
12198 if (!(complain & tf_warning))
12199 ++c_inhibit_evaluation_warnings;
12200 r = tsubst_expr (t, args, complain, in_decl,
12201 /*integral_constant_expression_p=*/true);
12202 if (!(complain & tf_warning))
12203 --c_inhibit_evaluation_warnings;
12204 }
12205
12206 return r;
12207 }
12208
12209 /* Given a function parameter pack TMPL_PARM and some function parameters
12210 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12211 and set *SPEC_P to point at the next point in the list. */
12212
12213 tree
12214 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12215 {
12216 /* Collect all of the extra "packed" parameters into an
12217 argument pack. */
12218 tree parmvec;
12219 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12220 tree spec_parm = *spec_p;
12221 int i, len;
12222
12223 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12224 if (tmpl_parm
12225 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12226 break;
12227
12228 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12229 parmvec = make_tree_vec (len);
12230 spec_parm = *spec_p;
12231 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12232 {
12233 tree elt = spec_parm;
12234 if (DECL_PACK_P (elt))
12235 elt = make_pack_expansion (elt);
12236 TREE_VEC_ELT (parmvec, i) = elt;
12237 }
12238
12239 /* Build the argument packs. */
12240 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12241 *spec_p = spec_parm;
12242
12243 return argpack;
12244 }
12245
12246 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12247 NONTYPE_ARGUMENT_PACK. */
12248
12249 static tree
12250 make_fnparm_pack (tree spec_parm)
12251 {
12252 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12253 }
12254
12255 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12256 pack expansion with no extra args, 2 if it has extra args, or 0
12257 if it is not a pack expansion. */
12258
12259 static int
12260 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12261 {
12262 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12263 /* We're being called before this happens in tsubst_pack_expansion. */
12264 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12265 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12266 if (i >= TREE_VEC_LENGTH (vec))
12267 return 0;
12268 tree elt = TREE_VEC_ELT (vec, i);
12269 if (DECL_P (elt))
12270 /* A decl pack is itself an expansion. */
12271 elt = TREE_TYPE (elt);
12272 if (!PACK_EXPANSION_P (elt))
12273 return 0;
12274 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12275 return 2;
12276 return 1;
12277 }
12278
12279
12280 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12281
12282 static tree
12283 make_argument_pack_select (tree arg_pack, unsigned index)
12284 {
12285 tree aps = make_node (ARGUMENT_PACK_SELECT);
12286
12287 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12288 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12289
12290 return aps;
12291 }
12292
12293 /* This is a subroutine of tsubst_pack_expansion.
12294
12295 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12296 mechanism to store the (non complete list of) arguments of the
12297 substitution and return a non substituted pack expansion, in order
12298 to wait for when we have enough arguments to really perform the
12299 substitution. */
12300
12301 static bool
12302 use_pack_expansion_extra_args_p (tree parm_packs,
12303 int arg_pack_len,
12304 bool has_empty_arg)
12305 {
12306 /* If one pack has an expansion and another pack has a normal
12307 argument or if one pack has an empty argument and an another
12308 one hasn't then tsubst_pack_expansion cannot perform the
12309 substitution and need to fall back on the
12310 PACK_EXPANSION_EXTRA mechanism. */
12311 if (parm_packs == NULL_TREE)
12312 return false;
12313 else if (has_empty_arg)
12314 {
12315 /* If all the actual packs are pack expansions, we can still
12316 subsitute directly. */
12317 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12318 {
12319 tree a = TREE_VALUE (p);
12320 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12321 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12322 a = ARGUMENT_PACK_ARGS (a);
12323 if (TREE_VEC_LENGTH (a) == 1)
12324 a = TREE_VEC_ELT (a, 0);
12325 if (PACK_EXPANSION_P (a))
12326 continue;
12327 return true;
12328 }
12329 return false;
12330 }
12331
12332 bool has_expansion_arg = false;
12333 for (int i = 0 ; i < arg_pack_len; ++i)
12334 {
12335 bool has_non_expansion_arg = false;
12336 for (tree parm_pack = parm_packs;
12337 parm_pack;
12338 parm_pack = TREE_CHAIN (parm_pack))
12339 {
12340 tree arg = TREE_VALUE (parm_pack);
12341
12342 int exp = argument_pack_element_is_expansion_p (arg, i);
12343 if (exp == 2)
12344 /* We can't substitute a pack expansion with extra args into
12345 our pattern. */
12346 return true;
12347 else if (exp)
12348 has_expansion_arg = true;
12349 else
12350 has_non_expansion_arg = true;
12351 }
12352
12353 if (has_expansion_arg && has_non_expansion_arg)
12354 return true;
12355 }
12356 return false;
12357 }
12358
12359 /* [temp.variadic]/6 says that:
12360
12361 The instantiation of a pack expansion [...]
12362 produces a list E1,E2, ..., En, where N is the number of elements
12363 in the pack expansion parameters.
12364
12365 This subroutine of tsubst_pack_expansion produces one of these Ei.
12366
12367 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12368 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12369 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12370 INDEX is the index 'i' of the element Ei to produce. ARGS,
12371 COMPLAIN, and IN_DECL are the same parameters as for the
12372 tsubst_pack_expansion function.
12373
12374 The function returns the resulting Ei upon successful completion,
12375 or error_mark_node.
12376
12377 Note that this function possibly modifies the ARGS parameter, so
12378 it's the responsibility of the caller to restore it. */
12379
12380 static tree
12381 gen_elem_of_pack_expansion_instantiation (tree pattern,
12382 tree parm_packs,
12383 unsigned index,
12384 tree args /* This parm gets
12385 modified. */,
12386 tsubst_flags_t complain,
12387 tree in_decl)
12388 {
12389 tree t;
12390 bool ith_elem_is_expansion = false;
12391
12392 /* For each parameter pack, change the substitution of the parameter
12393 pack to the ith argument in its argument pack, then expand the
12394 pattern. */
12395 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12396 {
12397 tree parm = TREE_PURPOSE (pack);
12398 tree arg_pack = TREE_VALUE (pack);
12399 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12400
12401 ith_elem_is_expansion |=
12402 argument_pack_element_is_expansion_p (arg_pack, index);
12403
12404 /* Select the Ith argument from the pack. */
12405 if (TREE_CODE (parm) == PARM_DECL
12406 || VAR_P (parm)
12407 || TREE_CODE (parm) == FIELD_DECL)
12408 {
12409 if (index == 0)
12410 {
12411 aps = make_argument_pack_select (arg_pack, index);
12412 if (!mark_used (parm, complain) && !(complain & tf_error))
12413 return error_mark_node;
12414 register_local_specialization (aps, parm);
12415 }
12416 else
12417 aps = retrieve_local_specialization (parm);
12418 }
12419 else
12420 {
12421 int idx, level;
12422 template_parm_level_and_index (parm, &level, &idx);
12423
12424 if (index == 0)
12425 {
12426 aps = make_argument_pack_select (arg_pack, index);
12427 /* Update the corresponding argument. */
12428 TMPL_ARG (args, level, idx) = aps;
12429 }
12430 else
12431 /* Re-use the ARGUMENT_PACK_SELECT. */
12432 aps = TMPL_ARG (args, level, idx);
12433 }
12434 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12435 }
12436
12437 /* Substitute into the PATTERN with the (possibly altered)
12438 arguments. */
12439 if (pattern == in_decl)
12440 /* Expanding a fixed parameter pack from
12441 coerce_template_parameter_pack. */
12442 t = tsubst_decl (pattern, args, complain);
12443 else if (pattern == error_mark_node)
12444 t = error_mark_node;
12445 else if (!TYPE_P (pattern))
12446 t = tsubst_expr (pattern, args, complain, in_decl,
12447 /*integral_constant_expression_p=*/false);
12448 else
12449 t = tsubst (pattern, args, complain, in_decl);
12450
12451 /* If the Ith argument pack element is a pack expansion, then
12452 the Ith element resulting from the substituting is going to
12453 be a pack expansion as well. */
12454 if (ith_elem_is_expansion)
12455 t = make_pack_expansion (t, complain);
12456
12457 return t;
12458 }
12459
12460 /* When the unexpanded parameter pack in a fold expression expands to an empty
12461 sequence, the value of the expression is as follows; the program is
12462 ill-formed if the operator is not listed in this table.
12463
12464 && true
12465 || false
12466 , void() */
12467
12468 tree
12469 expand_empty_fold (tree t, tsubst_flags_t complain)
12470 {
12471 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12472 if (!FOLD_EXPR_MODIFY_P (t))
12473 switch (code)
12474 {
12475 case TRUTH_ANDIF_EXPR:
12476 return boolean_true_node;
12477 case TRUTH_ORIF_EXPR:
12478 return boolean_false_node;
12479 case COMPOUND_EXPR:
12480 return void_node;
12481 default:
12482 break;
12483 }
12484
12485 if (complain & tf_error)
12486 error_at (location_of (t),
12487 "fold of empty expansion over %O", code);
12488 return error_mark_node;
12489 }
12490
12491 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12492 form an expression that combines the two terms using the
12493 operator of T. */
12494
12495 static tree
12496 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12497 {
12498 tree op = FOLD_EXPR_OP (t);
12499 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12500
12501 // Handle compound assignment operators.
12502 if (FOLD_EXPR_MODIFY_P (t))
12503 return build_x_modify_expr (input_location, left, code, right, complain);
12504
12505 warning_sentinel s(warn_parentheses);
12506 switch (code)
12507 {
12508 case COMPOUND_EXPR:
12509 return build_x_compound_expr (input_location, left, right, complain);
12510 default:
12511 return build_x_binary_op (input_location, code,
12512 left, TREE_CODE (left),
12513 right, TREE_CODE (right),
12514 /*overload=*/NULL,
12515 complain);
12516 }
12517 }
12518
12519 /* Substitute ARGS into the pack of a fold expression T. */
12520
12521 static inline tree
12522 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12523 {
12524 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12525 }
12526
12527 /* Substitute ARGS into the pack of a fold expression T. */
12528
12529 static inline tree
12530 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12531 {
12532 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12533 }
12534
12535 /* Expand a PACK of arguments into a grouped as left fold.
12536 Given a pack containing elements A0, A1, ..., An and an
12537 operator @, this builds the expression:
12538
12539 ((A0 @ A1) @ A2) ... @ An
12540
12541 Note that PACK must not be empty.
12542
12543 The operator is defined by the original fold expression T. */
12544
12545 static tree
12546 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12547 {
12548 tree left = TREE_VEC_ELT (pack, 0);
12549 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12550 {
12551 tree right = TREE_VEC_ELT (pack, i);
12552 left = fold_expression (t, left, right, complain);
12553 }
12554 return left;
12555 }
12556
12557 /* Substitute into a unary left fold expression. */
12558
12559 static tree
12560 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12561 tree in_decl)
12562 {
12563 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12564 if (pack == error_mark_node)
12565 return error_mark_node;
12566 if (PACK_EXPANSION_P (pack))
12567 {
12568 tree r = copy_node (t);
12569 FOLD_EXPR_PACK (r) = pack;
12570 return r;
12571 }
12572 if (TREE_VEC_LENGTH (pack) == 0)
12573 return expand_empty_fold (t, complain);
12574 else
12575 return expand_left_fold (t, pack, complain);
12576 }
12577
12578 /* Substitute into a binary left fold expression.
12579
12580 Do ths by building a single (non-empty) vector of argumnts and
12581 building the expression from those elements. */
12582
12583 static tree
12584 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12585 tree in_decl)
12586 {
12587 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12588 if (pack == error_mark_node)
12589 return error_mark_node;
12590 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12591 if (init == error_mark_node)
12592 return error_mark_node;
12593
12594 if (PACK_EXPANSION_P (pack))
12595 {
12596 tree r = copy_node (t);
12597 FOLD_EXPR_PACK (r) = pack;
12598 FOLD_EXPR_INIT (r) = init;
12599 return r;
12600 }
12601
12602 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12603 TREE_VEC_ELT (vec, 0) = init;
12604 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12605 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12606
12607 return expand_left_fold (t, vec, complain);
12608 }
12609
12610 /* Expand a PACK of arguments into a grouped as right fold.
12611 Given a pack containing elementns A0, A1, ..., and an
12612 operator @, this builds the expression:
12613
12614 A0@ ... (An-2 @ (An-1 @ An))
12615
12616 Note that PACK must not be empty.
12617
12618 The operator is defined by the original fold expression T. */
12619
12620 tree
12621 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12622 {
12623 // Build the expression.
12624 int n = TREE_VEC_LENGTH (pack);
12625 tree right = TREE_VEC_ELT (pack, n - 1);
12626 for (--n; n != 0; --n)
12627 {
12628 tree left = TREE_VEC_ELT (pack, n - 1);
12629 right = fold_expression (t, left, right, complain);
12630 }
12631 return right;
12632 }
12633
12634 /* Substitute into a unary right fold expression. */
12635
12636 static tree
12637 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12638 tree in_decl)
12639 {
12640 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12641 if (pack == error_mark_node)
12642 return error_mark_node;
12643 if (PACK_EXPANSION_P (pack))
12644 {
12645 tree r = copy_node (t);
12646 FOLD_EXPR_PACK (r) = pack;
12647 return r;
12648 }
12649 if (TREE_VEC_LENGTH (pack) == 0)
12650 return expand_empty_fold (t, complain);
12651 else
12652 return expand_right_fold (t, pack, complain);
12653 }
12654
12655 /* Substitute into a binary right fold expression.
12656
12657 Do ths by building a single (non-empty) vector of arguments and
12658 building the expression from those elements. */
12659
12660 static tree
12661 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12662 tree in_decl)
12663 {
12664 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12665 if (pack == error_mark_node)
12666 return error_mark_node;
12667 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12668 if (init == error_mark_node)
12669 return error_mark_node;
12670
12671 if (PACK_EXPANSION_P (pack))
12672 {
12673 tree r = copy_node (t);
12674 FOLD_EXPR_PACK (r) = pack;
12675 FOLD_EXPR_INIT (r) = init;
12676 return r;
12677 }
12678
12679 int n = TREE_VEC_LENGTH (pack);
12680 tree vec = make_tree_vec (n + 1);
12681 for (int i = 0; i < n; ++i)
12682 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12683 TREE_VEC_ELT (vec, n) = init;
12684
12685 return expand_right_fold (t, vec, complain);
12686 }
12687
12688 /* Walk through the pattern of a pack expansion, adding everything in
12689 local_specializations to a list. */
12690
12691 class el_data
12692 {
12693 public:
12694 hash_set<tree> internal;
12695 tree extra;
12696 tsubst_flags_t complain;
12697
12698 el_data (tsubst_flags_t c)
12699 : extra (NULL_TREE), complain (c) {}
12700 };
12701 static tree
12702 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12703 {
12704 el_data &data = *reinterpret_cast<el_data*>(data_);
12705 tree *extra = &data.extra;
12706 tsubst_flags_t complain = data.complain;
12707
12708 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12709 /* Remember local typedefs (85214). */
12710 tp = &TYPE_NAME (*tp);
12711
12712 if (TREE_CODE (*tp) == DECL_EXPR)
12713 data.internal.add (DECL_EXPR_DECL (*tp));
12714 else if (tree spec = retrieve_local_specialization (*tp))
12715 {
12716 if (data.internal.contains (*tp))
12717 /* Don't mess with variables declared within the pattern. */
12718 return NULL_TREE;
12719 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12720 {
12721 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12722 tree args = ARGUMENT_PACK_ARGS (spec);
12723 if (TREE_VEC_LENGTH (args) == 1)
12724 {
12725 tree elt = TREE_VEC_ELT (args, 0);
12726 if (PACK_EXPANSION_P (elt))
12727 elt = PACK_EXPANSION_PATTERN (elt);
12728 if (DECL_PACK_P (elt))
12729 spec = elt;
12730 }
12731 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12732 {
12733 /* Handle lambda capture here, since we aren't doing any
12734 substitution now, and so tsubst_copy won't call
12735 process_outer_var_ref. */
12736 tree args = ARGUMENT_PACK_ARGS (spec);
12737 int len = TREE_VEC_LENGTH (args);
12738 for (int i = 0; i < len; ++i)
12739 {
12740 tree arg = TREE_VEC_ELT (args, i);
12741 tree carg = arg;
12742 if (outer_automatic_var_p (arg))
12743 carg = process_outer_var_ref (arg, complain);
12744 if (carg != arg)
12745 {
12746 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12747 proxies. */
12748 if (i == 0)
12749 {
12750 spec = copy_node (spec);
12751 args = copy_node (args);
12752 SET_ARGUMENT_PACK_ARGS (spec, args);
12753 register_local_specialization (spec, *tp);
12754 }
12755 TREE_VEC_ELT (args, i) = carg;
12756 }
12757 }
12758 }
12759 }
12760 if (outer_automatic_var_p (spec))
12761 spec = process_outer_var_ref (spec, complain);
12762 *extra = tree_cons (*tp, spec, *extra);
12763 }
12764 return NULL_TREE;
12765 }
12766 static tree
12767 extract_local_specs (tree pattern, tsubst_flags_t complain)
12768 {
12769 el_data data (complain);
12770 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12771 return data.extra;
12772 }
12773
12774 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12775 for use in PACK_EXPANSION_EXTRA_ARGS. */
12776
12777 tree
12778 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12779 {
12780 tree extra = args;
12781 if (local_specializations)
12782 if (tree locals = extract_local_specs (pattern, complain))
12783 extra = tree_cons (NULL_TREE, extra, locals);
12784 return extra;
12785 }
12786
12787 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12788 normal template args to ARGS. */
12789
12790 tree
12791 add_extra_args (tree extra, tree args)
12792 {
12793 if (extra && TREE_CODE (extra) == TREE_LIST)
12794 {
12795 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12796 {
12797 /* The partial instantiation involved local declarations collected in
12798 extract_local_specs; map from the general template to our local
12799 context. */
12800 tree gen = TREE_PURPOSE (elt);
12801 tree inst = TREE_VALUE (elt);
12802 if (DECL_P (inst))
12803 if (tree local = retrieve_local_specialization (inst))
12804 inst = local;
12805 /* else inst is already a full instantiation of the pack. */
12806 register_local_specialization (inst, gen);
12807 }
12808 gcc_assert (!TREE_PURPOSE (extra));
12809 extra = TREE_VALUE (extra);
12810 }
12811 #if 1
12812 /* I think we should always be able to substitute dependent args into the
12813 pattern. If that turns out to be incorrect in some cases, enable the
12814 alternate code (and add complain/in_decl parms to this function). */
12815 gcc_checking_assert (!uses_template_parms (extra));
12816 #else
12817 if (!uses_template_parms (extra))
12818 {
12819 gcc_unreachable ();
12820 extra = tsubst_template_args (extra, args, complain, in_decl);
12821 args = add_outermost_template_args (args, extra);
12822 }
12823 else
12824 #endif
12825 args = add_to_template_args (extra, args);
12826 return args;
12827 }
12828
12829 /* Substitute ARGS into T, which is an pack expansion
12830 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12831 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12832 (if only a partial substitution could be performed) or
12833 ERROR_MARK_NODE if there was an error. */
12834 tree
12835 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12836 tree in_decl)
12837 {
12838 tree pattern;
12839 tree pack, packs = NULL_TREE;
12840 bool unsubstituted_packs = false;
12841 int i, len = -1;
12842 tree result;
12843 bool need_local_specializations = false;
12844 int levels;
12845
12846 gcc_assert (PACK_EXPANSION_P (t));
12847 pattern = PACK_EXPANSION_PATTERN (t);
12848
12849 /* Add in any args remembered from an earlier partial instantiation. */
12850 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12851
12852 levels = TMPL_ARGS_DEPTH (args);
12853
12854 /* Determine the argument packs that will instantiate the parameter
12855 packs used in the expansion expression. While we're at it,
12856 compute the number of arguments to be expanded and make sure it
12857 is consistent. */
12858 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12859 pack = TREE_CHAIN (pack))
12860 {
12861 tree parm_pack = TREE_VALUE (pack);
12862 tree arg_pack = NULL_TREE;
12863 tree orig_arg = NULL_TREE;
12864 int level = 0;
12865
12866 if (TREE_CODE (parm_pack) == BASES)
12867 {
12868 gcc_assert (parm_pack == pattern);
12869 if (BASES_DIRECT (parm_pack))
12870 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12871 args, complain,
12872 in_decl, false),
12873 complain);
12874 else
12875 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12876 args, complain, in_decl,
12877 false), complain);
12878 }
12879 else if (builtin_pack_call_p (parm_pack))
12880 {
12881 if (parm_pack != pattern)
12882 {
12883 if (complain & tf_error)
12884 sorry ("%qE is not the entire pattern of the pack expansion",
12885 parm_pack);
12886 return error_mark_node;
12887 }
12888 return expand_builtin_pack_call (parm_pack, args,
12889 complain, in_decl);
12890 }
12891 else if (TREE_CODE (parm_pack) == PARM_DECL)
12892 {
12893 /* We know we have correct local_specializations if this
12894 expansion is at function scope, or if we're dealing with a
12895 local parameter in a requires expression; for the latter,
12896 tsubst_requires_expr set it up appropriately. */
12897 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12898 arg_pack = retrieve_local_specialization (parm_pack);
12899 else
12900 /* We can't rely on local_specializations for a parameter
12901 name used later in a function declaration (such as in a
12902 late-specified return type). Even if it exists, it might
12903 have the wrong value for a recursive call. */
12904 need_local_specializations = true;
12905
12906 if (!arg_pack)
12907 {
12908 /* This parameter pack was used in an unevaluated context. Just
12909 make a dummy decl, since it's only used for its type. */
12910 ++cp_unevaluated_operand;
12911 arg_pack = tsubst_decl (parm_pack, args, complain);
12912 --cp_unevaluated_operand;
12913 if (arg_pack && DECL_PACK_P (arg_pack))
12914 /* Partial instantiation of the parm_pack, we can't build
12915 up an argument pack yet. */
12916 arg_pack = NULL_TREE;
12917 else
12918 arg_pack = make_fnparm_pack (arg_pack);
12919 }
12920 else if (DECL_PACK_P (arg_pack))
12921 /* This argument pack isn't fully instantiated yet. */
12922 arg_pack = NULL_TREE;
12923 }
12924 else if (is_capture_proxy (parm_pack))
12925 {
12926 arg_pack = retrieve_local_specialization (parm_pack);
12927 if (DECL_PACK_P (arg_pack))
12928 arg_pack = NULL_TREE;
12929 }
12930 else
12931 {
12932 int idx;
12933 template_parm_level_and_index (parm_pack, &level, &idx);
12934 if (level <= levels)
12935 arg_pack = TMPL_ARG (args, level, idx);
12936
12937 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
12938 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
12939 arg_pack = NULL_TREE;
12940 }
12941
12942 orig_arg = arg_pack;
12943 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12944 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12945
12946 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12947 /* This can only happen if we forget to expand an argument
12948 pack somewhere else. Just return an error, silently. */
12949 {
12950 result = make_tree_vec (1);
12951 TREE_VEC_ELT (result, 0) = error_mark_node;
12952 return result;
12953 }
12954
12955 if (arg_pack)
12956 {
12957 int my_len =
12958 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12959
12960 /* Don't bother trying to do a partial substitution with
12961 incomplete packs; we'll try again after deduction. */
12962 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12963 return t;
12964
12965 if (len < 0)
12966 len = my_len;
12967 else if (len != my_len)
12968 {
12969 if (!(complain & tf_error))
12970 /* Fail quietly. */;
12971 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12972 error ("mismatched argument pack lengths while expanding %qT",
12973 pattern);
12974 else
12975 error ("mismatched argument pack lengths while expanding %qE",
12976 pattern);
12977 return error_mark_node;
12978 }
12979
12980 /* Keep track of the parameter packs and their corresponding
12981 argument packs. */
12982 packs = tree_cons (parm_pack, arg_pack, packs);
12983 TREE_TYPE (packs) = orig_arg;
12984 }
12985 else
12986 {
12987 /* We can't substitute for this parameter pack. We use a flag as
12988 well as the missing_level counter because function parameter
12989 packs don't have a level. */
12990 gcc_assert (processing_template_decl || is_auto (parm_pack));
12991 unsubstituted_packs = true;
12992 }
12993 }
12994
12995 /* If the expansion is just T..., return the matching argument pack, unless
12996 we need to call convert_from_reference on all the elements. This is an
12997 important optimization; see c++/68422. */
12998 if (!unsubstituted_packs
12999 && TREE_PURPOSE (packs) == pattern)
13000 {
13001 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13002
13003 /* If the argument pack is a single pack expansion, pull it out. */
13004 if (TREE_VEC_LENGTH (args) == 1
13005 && pack_expansion_args_count (args))
13006 return TREE_VEC_ELT (args, 0);
13007
13008 /* Types need no adjustment, nor does sizeof..., and if we still have
13009 some pack expansion args we won't do anything yet. */
13010 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13011 || PACK_EXPANSION_SIZEOF_P (t)
13012 || pack_expansion_args_count (args))
13013 return args;
13014 /* Also optimize expression pack expansions if we can tell that the
13015 elements won't have reference type. */
13016 tree type = TREE_TYPE (pattern);
13017 if (type && !TYPE_REF_P (type)
13018 && !PACK_EXPANSION_P (type)
13019 && !WILDCARD_TYPE_P (type))
13020 return args;
13021 /* Otherwise use the normal path so we get convert_from_reference. */
13022 }
13023
13024 /* We cannot expand this expansion expression, because we don't have
13025 all of the argument packs we need. */
13026 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
13027 {
13028 /* We got some full packs, but we can't substitute them in until we
13029 have values for all the packs. So remember these until then. */
13030
13031 t = make_pack_expansion (pattern, complain);
13032 PACK_EXPANSION_EXTRA_ARGS (t)
13033 = build_extra_args (pattern, args, complain);
13034 return t;
13035 }
13036
13037 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13038 type, so create our own local specializations map; the current map is
13039 either NULL or (in the case of recursive unification) might have
13040 bindings that we don't want to use or alter. */
13041 local_specialization_stack lss (need_local_specializations
13042 ? lss_blank : lss_nop);
13043
13044 if (unsubstituted_packs)
13045 {
13046 /* There were no real arguments, we're just replacing a parameter
13047 pack with another version of itself. Substitute into the
13048 pattern and return a PACK_EXPANSION_*. The caller will need to
13049 deal with that. */
13050 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13051 t = tsubst_expr (pattern, args, complain, in_decl,
13052 /*integral_constant_expression_p=*/false);
13053 else
13054 t = tsubst (pattern, args, complain, in_decl);
13055 t = make_pack_expansion (t, complain);
13056 return t;
13057 }
13058
13059 gcc_assert (len >= 0);
13060
13061 /* For each argument in each argument pack, substitute into the
13062 pattern. */
13063 result = make_tree_vec (len);
13064 tree elem_args = copy_template_args (args);
13065 for (i = 0; i < len; ++i)
13066 {
13067 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13068 i,
13069 elem_args, complain,
13070 in_decl);
13071 TREE_VEC_ELT (result, i) = t;
13072 if (t == error_mark_node)
13073 {
13074 result = error_mark_node;
13075 break;
13076 }
13077 }
13078
13079 /* Update ARGS to restore the substitution from parameter packs to
13080 their argument packs. */
13081 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13082 {
13083 tree parm = TREE_PURPOSE (pack);
13084
13085 if (TREE_CODE (parm) == PARM_DECL
13086 || VAR_P (parm)
13087 || TREE_CODE (parm) == FIELD_DECL)
13088 register_local_specialization (TREE_TYPE (pack), parm);
13089 else
13090 {
13091 int idx, level;
13092
13093 if (TREE_VALUE (pack) == NULL_TREE)
13094 continue;
13095
13096 template_parm_level_and_index (parm, &level, &idx);
13097
13098 /* Update the corresponding argument. */
13099 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13100 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13101 TREE_TYPE (pack);
13102 else
13103 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13104 }
13105 }
13106
13107 /* If the dependent pack arguments were such that we end up with only a
13108 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13109 if (len == 1 && TREE_CODE (result) == TREE_VEC
13110 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13111 return TREE_VEC_ELT (result, 0);
13112
13113 return result;
13114 }
13115
13116 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13117 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13118 parameter packs; all parms generated from a function parameter pack will
13119 have the same DECL_PARM_INDEX. */
13120
13121 tree
13122 get_pattern_parm (tree parm, tree tmpl)
13123 {
13124 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13125 tree patparm;
13126
13127 if (DECL_ARTIFICIAL (parm))
13128 {
13129 for (patparm = DECL_ARGUMENTS (pattern);
13130 patparm; patparm = DECL_CHAIN (patparm))
13131 if (DECL_ARTIFICIAL (patparm)
13132 && DECL_NAME (parm) == DECL_NAME (patparm))
13133 break;
13134 }
13135 else
13136 {
13137 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13138 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13139 gcc_assert (DECL_PARM_INDEX (patparm)
13140 == DECL_PARM_INDEX (parm));
13141 }
13142
13143 return patparm;
13144 }
13145
13146 /* Make an argument pack out of the TREE_VEC VEC. */
13147
13148 static tree
13149 make_argument_pack (tree vec)
13150 {
13151 tree pack;
13152
13153 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13154 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13155 else
13156 {
13157 pack = make_node (NONTYPE_ARGUMENT_PACK);
13158 TREE_CONSTANT (pack) = 1;
13159 }
13160 SET_ARGUMENT_PACK_ARGS (pack, vec);
13161 return pack;
13162 }
13163
13164 /* Return an exact copy of template args T that can be modified
13165 independently. */
13166
13167 static tree
13168 copy_template_args (tree t)
13169 {
13170 if (t == error_mark_node)
13171 return t;
13172
13173 int len = TREE_VEC_LENGTH (t);
13174 tree new_vec = make_tree_vec (len);
13175
13176 for (int i = 0; i < len; ++i)
13177 {
13178 tree elt = TREE_VEC_ELT (t, i);
13179 if (elt && TREE_CODE (elt) == TREE_VEC)
13180 elt = copy_template_args (elt);
13181 TREE_VEC_ELT (new_vec, i) = elt;
13182 }
13183
13184 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13185 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13186
13187 return new_vec;
13188 }
13189
13190 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13191
13192 tree
13193 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13194 tree in_decl)
13195 {
13196 /* Substitute into each of the arguments. */
13197 tree new_arg = TYPE_P (orig_arg)
13198 ? cxx_make_type (TREE_CODE (orig_arg))
13199 : make_node (TREE_CODE (orig_arg));
13200
13201 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13202 args, complain, in_decl);
13203 if (pack_args == error_mark_node)
13204 new_arg = error_mark_node;
13205 else
13206 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13207
13208 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13209 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13210
13211 return new_arg;
13212 }
13213
13214 /* Substitute ARGS into the vector or list of template arguments T. */
13215
13216 tree
13217 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13218 {
13219 tree orig_t = t;
13220 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13221 tree *elts;
13222
13223 if (t == error_mark_node)
13224 return error_mark_node;
13225
13226 len = TREE_VEC_LENGTH (t);
13227 elts = XALLOCAVEC (tree, len);
13228
13229 for (i = 0; i < len; i++)
13230 {
13231 tree orig_arg = TREE_VEC_ELT (t, i);
13232 tree new_arg;
13233
13234 if (TREE_CODE (orig_arg) == TREE_VEC)
13235 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13236 else if (PACK_EXPANSION_P (orig_arg))
13237 {
13238 /* Substitute into an expansion expression. */
13239 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13240
13241 if (TREE_CODE (new_arg) == TREE_VEC)
13242 /* Add to the expanded length adjustment the number of
13243 expanded arguments. We subtract one from this
13244 measurement, because the argument pack expression
13245 itself is already counted as 1 in
13246 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13247 the argument pack is empty. */
13248 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13249 }
13250 else if (ARGUMENT_PACK_P (orig_arg))
13251 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13252 else
13253 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13254
13255 if (new_arg == error_mark_node)
13256 return error_mark_node;
13257
13258 elts[i] = new_arg;
13259 if (new_arg != orig_arg)
13260 need_new = 1;
13261 }
13262
13263 if (!need_new)
13264 return t;
13265
13266 /* Make space for the expanded arguments coming from template
13267 argument packs. */
13268 t = make_tree_vec (len + expanded_len_adjust);
13269 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13270 arguments for a member template.
13271 In that case each TREE_VEC in ORIG_T represents a level of template
13272 arguments, and ORIG_T won't carry any non defaulted argument count.
13273 It will rather be the nested TREE_VECs that will carry one.
13274 In other words, ORIG_T carries a non defaulted argument count only
13275 if it doesn't contain any nested TREE_VEC. */
13276 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13277 {
13278 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13279 count += expanded_len_adjust;
13280 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13281 }
13282 for (i = 0, out = 0; i < len; i++)
13283 {
13284 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13285 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13286 && TREE_CODE (elts[i]) == TREE_VEC)
13287 {
13288 int idx;
13289
13290 /* Now expand the template argument pack "in place". */
13291 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13292 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13293 }
13294 else
13295 {
13296 TREE_VEC_ELT (t, out) = elts[i];
13297 out++;
13298 }
13299 }
13300
13301 return t;
13302 }
13303
13304 /* Substitute ARGS into one level PARMS of template parameters. */
13305
13306 static tree
13307 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13308 {
13309 if (parms == error_mark_node)
13310 return error_mark_node;
13311
13312 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13313
13314 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13315 {
13316 tree tuple = TREE_VEC_ELT (parms, i);
13317
13318 if (tuple == error_mark_node)
13319 continue;
13320
13321 TREE_VEC_ELT (new_vec, i) =
13322 tsubst_template_parm (tuple, args, complain);
13323 }
13324
13325 return new_vec;
13326 }
13327
13328 /* Return the result of substituting ARGS into the template parameters
13329 given by PARMS. If there are m levels of ARGS and m + n levels of
13330 PARMS, then the result will contain n levels of PARMS. For
13331 example, if PARMS is `template <class T> template <class U>
13332 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13333 result will be `template <int*, double, class V>'. */
13334
13335 static tree
13336 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13337 {
13338 tree r = NULL_TREE;
13339 tree* new_parms;
13340
13341 /* When substituting into a template, we must set
13342 PROCESSING_TEMPLATE_DECL as the template parameters may be
13343 dependent if they are based on one-another, and the dependency
13344 predicates are short-circuit outside of templates. */
13345 ++processing_template_decl;
13346
13347 for (new_parms = &r;
13348 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13349 new_parms = &(TREE_CHAIN (*new_parms)),
13350 parms = TREE_CHAIN (parms))
13351 {
13352 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13353 args, complain);
13354 *new_parms =
13355 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13356 - TMPL_ARGS_DEPTH (args)),
13357 new_vec, NULL_TREE);
13358 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13359 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13360 }
13361
13362 --processing_template_decl;
13363
13364 return r;
13365 }
13366
13367 /* Return the result of substituting ARGS into one template parameter
13368 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13369 parameter and which TREE_PURPOSE is the default argument of the
13370 template parameter. */
13371
13372 static tree
13373 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13374 {
13375 tree default_value, parm_decl;
13376
13377 if (args == NULL_TREE
13378 || t == NULL_TREE
13379 || t == error_mark_node)
13380 return t;
13381
13382 gcc_assert (TREE_CODE (t) == TREE_LIST);
13383
13384 default_value = TREE_PURPOSE (t);
13385 parm_decl = TREE_VALUE (t);
13386 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13387
13388 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13389 if (TREE_CODE (parm_decl) == PARM_DECL
13390 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13391 parm_decl = error_mark_node;
13392 default_value = tsubst_template_arg (default_value, args,
13393 complain, NULL_TREE);
13394 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13395
13396 tree r = build_tree_list (default_value, parm_decl);
13397 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13398 return r;
13399 }
13400
13401 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13402 type T. If T is not an aggregate or enumeration type, it is
13403 handled as if by tsubst. IN_DECL is as for tsubst. If
13404 ENTERING_SCOPE is nonzero, T is the context for a template which
13405 we are presently tsubst'ing. Return the substituted value. */
13406
13407 static tree
13408 tsubst_aggr_type (tree t,
13409 tree args,
13410 tsubst_flags_t complain,
13411 tree in_decl,
13412 int entering_scope)
13413 {
13414 if (t == NULL_TREE)
13415 return NULL_TREE;
13416
13417 switch (TREE_CODE (t))
13418 {
13419 case RECORD_TYPE:
13420 if (TYPE_PTRMEMFUNC_P (t))
13421 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13422
13423 /* Fall through. */
13424 case ENUMERAL_TYPE:
13425 case UNION_TYPE:
13426 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13427 {
13428 tree argvec;
13429 tree context;
13430 tree r;
13431
13432 /* In "sizeof(X<I>)" we need to evaluate "I". */
13433 cp_evaluated ev;
13434
13435 /* First, determine the context for the type we are looking
13436 up. */
13437 context = TYPE_CONTEXT (t);
13438 if (context && TYPE_P (context))
13439 {
13440 context = tsubst_aggr_type (context, args, complain,
13441 in_decl, /*entering_scope=*/1);
13442 /* If context is a nested class inside a class template,
13443 it may still need to be instantiated (c++/33959). */
13444 context = complete_type (context);
13445 }
13446
13447 /* Then, figure out what arguments are appropriate for the
13448 type we are trying to find. For example, given:
13449
13450 template <class T> struct S;
13451 template <class T, class U> void f(T, U) { S<U> su; }
13452
13453 and supposing that we are instantiating f<int, double>,
13454 then our ARGS will be {int, double}, but, when looking up
13455 S we only want {double}. */
13456 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13457 complain, in_decl);
13458 if (argvec == error_mark_node)
13459 r = error_mark_node;
13460 else if (cxx_dialect >= cxx17 && dependent_scope_p (context))
13461 {
13462 /* See maybe_dependent_member_ref. */
13463 tree name = TYPE_IDENTIFIER (t);
13464 tree fullname = name;
13465 if (instantiates_primary_template_p (t))
13466 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13467 INNERMOST_TEMPLATE_ARGS (argvec));
13468 return build_typename_type (context, name, fullname,
13469 typename_type);
13470 }
13471 else
13472 {
13473 r = lookup_template_class (t, argvec, in_decl, context,
13474 entering_scope, complain);
13475 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13476 }
13477
13478 return r;
13479 }
13480 else
13481 /* This is not a template type, so there's nothing to do. */
13482 return t;
13483
13484 default:
13485 return tsubst (t, args, complain, in_decl);
13486 }
13487 }
13488
13489 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13490
13491 /* Substitute into the default argument ARG (a default argument for
13492 FN), which has the indicated TYPE. */
13493
13494 tree
13495 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13496 tsubst_flags_t complain)
13497 {
13498 int errs = errorcount + sorrycount;
13499
13500 /* This can happen in invalid code. */
13501 if (TREE_CODE (arg) == DEFERRED_PARSE)
13502 return arg;
13503
13504 /* Shortcut {}. */
13505 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13506 && CONSTRUCTOR_NELTS (arg) == 0)
13507 return arg;
13508
13509 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13510 parm = chain_index (parmnum, parm);
13511 tree parmtype = TREE_TYPE (parm);
13512 if (DECL_BY_REFERENCE (parm))
13513 parmtype = TREE_TYPE (parmtype);
13514 if (parmtype == error_mark_node)
13515 return error_mark_node;
13516
13517 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13518
13519 tree *slot;
13520 if (defarg_inst && (slot = defarg_inst->get (parm)))
13521 return *slot;
13522
13523 /* This default argument came from a template. Instantiate the
13524 default argument here, not in tsubst. In the case of
13525 something like:
13526
13527 template <class T>
13528 struct S {
13529 static T t();
13530 void f(T = t());
13531 };
13532
13533 we must be careful to do name lookup in the scope of S<T>,
13534 rather than in the current class. */
13535 push_to_top_level ();
13536 push_access_scope (fn);
13537 push_deferring_access_checks (dk_no_deferred);
13538 start_lambda_scope (parm);
13539
13540 /* The default argument expression may cause implicitly defined
13541 member functions to be synthesized, which will result in garbage
13542 collection. We must treat this situation as if we were within
13543 the body of function so as to avoid collecting live data on the
13544 stack. */
13545 ++function_depth;
13546 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13547 complain, NULL_TREE,
13548 /*integral_constant_expression_p=*/false);
13549 --function_depth;
13550
13551 finish_lambda_scope ();
13552
13553 /* Make sure the default argument is reasonable. */
13554 arg = check_default_argument (type, arg, complain);
13555
13556 if (errorcount+sorrycount > errs
13557 && (complain & tf_warning_or_error))
13558 inform (input_location,
13559 " when instantiating default argument for call to %qD", fn);
13560
13561 pop_deferring_access_checks ();
13562 pop_access_scope (fn);
13563 pop_from_top_level ();
13564
13565 if (arg != error_mark_node && !cp_unevaluated_operand)
13566 {
13567 if (!defarg_inst)
13568 defarg_inst = decl_tree_cache_map::create_ggc (37);
13569 defarg_inst->put (parm, arg);
13570 }
13571
13572 return arg;
13573 }
13574
13575 /* Substitute into all the default arguments for FN. */
13576
13577 static void
13578 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13579 {
13580 tree arg;
13581 tree tmpl_args;
13582
13583 tmpl_args = DECL_TI_ARGS (fn);
13584
13585 /* If this function is not yet instantiated, we certainly don't need
13586 its default arguments. */
13587 if (uses_template_parms (tmpl_args))
13588 return;
13589 /* Don't do this again for clones. */
13590 if (DECL_CLONED_FUNCTION_P (fn))
13591 return;
13592
13593 int i = 0;
13594 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13595 arg;
13596 arg = TREE_CHAIN (arg), ++i)
13597 if (TREE_PURPOSE (arg))
13598 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13599 TREE_VALUE (arg),
13600 TREE_PURPOSE (arg),
13601 complain);
13602 }
13603
13604 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13605 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13606
13607 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13608
13609 void
13610 store_explicit_specifier (tree v, tree t)
13611 {
13612 if (!explicit_specifier_map)
13613 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13614 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13615 explicit_specifier_map->put (v, t);
13616 }
13617
13618 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13619
13620 static tree
13621 lookup_explicit_specifier (tree v)
13622 {
13623 return *explicit_specifier_map->get (v);
13624 }
13625
13626 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13627 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13628 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13629 identical to T. */
13630
13631 static tree
13632 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13633 tree raises, tsubst_flags_t complain)
13634 {
13635 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13636
13637 tree new_type;
13638 if (TREE_CODE (t) == FUNCTION_TYPE)
13639 {
13640 new_type = build_function_type (return_type, arg_types);
13641 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13642 }
13643 else
13644 {
13645 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13646 /* Don't pick up extra function qualifiers from the basetype. */
13647 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13648 if (! MAYBE_CLASS_TYPE_P (r))
13649 {
13650 /* [temp.deduct]
13651
13652 Type deduction may fail for any of the following
13653 reasons:
13654
13655 -- Attempting to create "pointer to member of T" when T
13656 is not a class type. */
13657 if (complain & tf_error)
13658 error ("creating pointer to member function of non-class type %qT",
13659 r);
13660 return error_mark_node;
13661 }
13662
13663 new_type = build_method_type_directly (r, return_type,
13664 TREE_CHAIN (arg_types));
13665 }
13666 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13667
13668 cp_ref_qualifier rqual = type_memfn_rqual (t);
13669 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13670 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13671 }
13672
13673 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13674 each of its formal parameters. If there is a disagreement then rebuild
13675 DECL's function type according to its formal parameter types, as part of a
13676 resolution for Core issues 1001/1322. */
13677
13678 static void
13679 maybe_rebuild_function_decl_type (tree decl)
13680 {
13681 bool function_type_needs_rebuilding = false;
13682 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13683 {
13684 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13685 while (parm_type_list && parm_type_list != void_list_node)
13686 {
13687 tree parm_type = TREE_VALUE (parm_type_list);
13688 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13689 if (!same_type_p (parm_type, formal_parm_type_unqual))
13690 {
13691 function_type_needs_rebuilding = true;
13692 break;
13693 }
13694
13695 parm_list = DECL_CHAIN (parm_list);
13696 parm_type_list = TREE_CHAIN (parm_type_list);
13697 }
13698 }
13699
13700 if (!function_type_needs_rebuilding)
13701 return;
13702
13703 const tree fntype = TREE_TYPE (decl);
13704 tree parm_list = DECL_ARGUMENTS (decl);
13705 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13706 tree new_parm_type_list = NULL_TREE;
13707 tree *q = &new_parm_type_list;
13708 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13709 {
13710 *q = copy_node (old_parm_type_list);
13711 parm_list = DECL_CHAIN (parm_list);
13712 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13713 q = &TREE_CHAIN (*q);
13714 }
13715 while (old_parm_type_list && old_parm_type_list != void_list_node)
13716 {
13717 *q = copy_node (old_parm_type_list);
13718 tree *new_parm_type = &TREE_VALUE (*q);
13719 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13720 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13721 *new_parm_type = formal_parm_type_unqual;
13722
13723 parm_list = DECL_CHAIN (parm_list);
13724 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13725 q = &TREE_CHAIN (*q);
13726 }
13727 if (old_parm_type_list == void_list_node)
13728 *q = void_list_node;
13729
13730 TREE_TYPE (decl)
13731 = rebuild_function_or_method_type (fntype,
13732 TREE_TYPE (fntype), new_parm_type_list,
13733 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13734 }
13735
13736 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13737
13738 static tree
13739 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13740 tree lambda_fntype)
13741 {
13742 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13743 hashval_t hash = 0;
13744 tree in_decl = t;
13745
13746 /* Nobody should be tsubst'ing into non-template functions. */
13747 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13748 || DECL_LOCAL_DECL_P (t));
13749
13750 if (DECL_LOCAL_DECL_P (t))
13751 {
13752 if (tree spec = retrieve_local_specialization (t))
13753 return spec;
13754 }
13755 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13756 {
13757 /* If T is not dependent, just return it. */
13758 if (!uses_template_parms (DECL_TI_ARGS (t))
13759 && !LAMBDA_FUNCTION_P (t))
13760 return t;
13761
13762 /* Calculate the most general template of which R is a
13763 specialization. */
13764 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13765
13766 /* We're substituting a lambda function under tsubst_lambda_expr but not
13767 directly from it; find the matching function we're already inside.
13768 But don't do this if T is a generic lambda with a single level of
13769 template parms, as in that case we're doing a normal instantiation. */
13770 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13771 && (!generic_lambda_fn_p (t)
13772 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13773 return enclosing_instantiation_of (t);
13774
13775 /* Calculate the complete set of arguments used to
13776 specialize R. */
13777 argvec = tsubst_template_args (DECL_TI_ARGS
13778 (DECL_TEMPLATE_RESULT
13779 (DECL_TI_TEMPLATE (t))),
13780 args, complain, in_decl);
13781 if (argvec == error_mark_node)
13782 return error_mark_node;
13783
13784 /* Check to see if we already have this specialization. */
13785 if (!lambda_fntype)
13786 {
13787 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13788 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13789 return spec;
13790 }
13791
13792 /* We can see more levels of arguments than parameters if
13793 there was a specialization of a member template, like
13794 this:
13795
13796 template <class T> struct S { template <class U> void f(); }
13797 template <> template <class U> void S<int>::f(U);
13798
13799 Here, we'll be substituting into the specialization,
13800 because that's where we can find the code we actually
13801 want to generate, but we'll have enough arguments for
13802 the most general template.
13803
13804 We also deal with the peculiar case:
13805
13806 template <class T> struct S {
13807 template <class U> friend void f();
13808 };
13809 template <class U> void f() {}
13810 template S<int>;
13811 template void f<double>();
13812
13813 Here, the ARGS for the instantiation of will be {int,
13814 double}. But, we only need as many ARGS as there are
13815 levels of template parameters in CODE_PATTERN. We are
13816 careful not to get fooled into reducing the ARGS in
13817 situations like:
13818
13819 template <class T> struct S { template <class U> void f(U); }
13820 template <class T> template <> void S<T>::f(int) {}
13821
13822 which we can spot because the pattern will be a
13823 specialization in this case. */
13824 int args_depth = TMPL_ARGS_DEPTH (args);
13825 int parms_depth =
13826 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13827
13828 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13829 args = get_innermost_template_args (args, parms_depth);
13830 }
13831 else
13832 {
13833 /* This special case arises when we have something like this:
13834
13835 template <class T> struct S {
13836 friend void f<int>(int, double);
13837 };
13838
13839 Here, the DECL_TI_TEMPLATE for the friend declaration
13840 will be an IDENTIFIER_NODE. We are being called from
13841 tsubst_friend_function, and we want only to create a
13842 new decl (R) with appropriate types so that we can call
13843 determine_specialization. */
13844 gen_tmpl = NULL_TREE;
13845 argvec = NULL_TREE;
13846 }
13847
13848 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13849 : NULL_TREE);
13850 tree ctx = closure ? closure : DECL_CONTEXT (t);
13851 bool member = ctx && TYPE_P (ctx);
13852
13853 if (member && !closure)
13854 ctx = tsubst_aggr_type (ctx, args,
13855 complain, t, /*entering_scope=*/1);
13856
13857 tree type = (lambda_fntype ? lambda_fntype
13858 : tsubst (TREE_TYPE (t), args,
13859 complain | tf_fndecl_type, in_decl));
13860 if (type == error_mark_node)
13861 return error_mark_node;
13862
13863 /* If we hit excessive deduction depth, the type is bogus even if
13864 it isn't error_mark_node, so don't build a decl. */
13865 if (excessive_deduction_depth)
13866 return error_mark_node;
13867
13868 /* We do NOT check for matching decls pushed separately at this
13869 point, as they may not represent instantiations of this
13870 template, and in any case are considered separate under the
13871 discrete model. */
13872 tree r = copy_decl (t);
13873 DECL_USE_TEMPLATE (r) = 0;
13874 TREE_TYPE (r) = type;
13875 /* Clear out the mangled name and RTL for the instantiation. */
13876 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13877 SET_DECL_RTL (r, NULL);
13878 /* Leave DECL_INITIAL set on deleted instantiations. */
13879 if (!DECL_DELETED_FN (r))
13880 DECL_INITIAL (r) = NULL_TREE;
13881 DECL_CONTEXT (r) = ctx;
13882
13883 /* Handle explicit(dependent-expr). */
13884 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13885 {
13886 tree spec = lookup_explicit_specifier (t);
13887 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13888 /*function_p=*/false,
13889 /*i_c_e_p=*/true);
13890 spec = build_explicit_specifier (spec, complain);
13891 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13892 }
13893
13894 /* OpenMP UDRs have the only argument a reference to the declared
13895 type. We want to diagnose if the declared type is a reference,
13896 which is invalid, but as references to references are usually
13897 quietly merged, diagnose it here. */
13898 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13899 {
13900 tree argtype
13901 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13902 argtype = tsubst (argtype, args, complain, in_decl);
13903 if (TYPE_REF_P (argtype))
13904 error_at (DECL_SOURCE_LOCATION (t),
13905 "reference type %qT in "
13906 "%<#pragma omp declare reduction%>", argtype);
13907 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13908 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13909 argtype);
13910 }
13911
13912 if (member && DECL_CONV_FN_P (r))
13913 /* Type-conversion operator. Reconstruct the name, in
13914 case it's the name of one of the template's parameters. */
13915 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13916
13917 tree parms = DECL_ARGUMENTS (t);
13918 if (closure)
13919 parms = DECL_CHAIN (parms);
13920 parms = tsubst (parms, args, complain, t);
13921 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13922 DECL_CONTEXT (parm) = r;
13923 if (closure)
13924 {
13925 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13926 DECL_NAME (tparm) = closure_identifier;
13927 DECL_CHAIN (tparm) = parms;
13928 parms = tparm;
13929 }
13930 DECL_ARGUMENTS (r) = parms;
13931 DECL_RESULT (r) = NULL_TREE;
13932
13933 maybe_rebuild_function_decl_type (r);
13934
13935 TREE_STATIC (r) = 0;
13936 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13937 DECL_EXTERNAL (r) = 1;
13938 /* If this is an instantiation of a function with internal
13939 linkage, we already know what object file linkage will be
13940 assigned to the instantiation. */
13941 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13942 DECL_DEFER_OUTPUT (r) = 0;
13943 DECL_CHAIN (r) = NULL_TREE;
13944 DECL_PENDING_INLINE_INFO (r) = 0;
13945 DECL_PENDING_INLINE_P (r) = 0;
13946 DECL_SAVED_TREE (r) = NULL_TREE;
13947 DECL_STRUCT_FUNCTION (r) = NULL;
13948 TREE_USED (r) = 0;
13949 /* We'll re-clone as appropriate in instantiate_template. */
13950 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13951
13952 /* If we aren't complaining now, return on error before we register
13953 the specialization so that we'll complain eventually. */
13954 if ((complain & tf_error) == 0
13955 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13956 && !grok_op_properties (r, /*complain=*/false))
13957 return error_mark_node;
13958
13959 /* Associate the constraints directly with the instantiation. We
13960 don't substitute through the constraints; that's only done when
13961 they are checked. */
13962 if (tree ci = get_constraints (t))
13963 /* Unless we're regenerating a lambda, in which case we'll set the
13964 lambda's constraints in tsubst_lambda_expr. */
13965 if (!lambda_fntype)
13966 set_constraints (r, ci);
13967
13968 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13969 SET_DECL_FRIEND_CONTEXT (r,
13970 tsubst (DECL_FRIEND_CONTEXT (t),
13971 args, complain, in_decl));
13972
13973 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13974 this in the special friend case mentioned above where
13975 GEN_TMPL is NULL. */
13976 if (gen_tmpl && !closure)
13977 {
13978 DECL_TEMPLATE_INFO (r)
13979 = build_template_info (gen_tmpl, argvec);
13980 SET_DECL_IMPLICIT_INSTANTIATION (r);
13981
13982 tree new_r
13983 = register_specialization (r, gen_tmpl, argvec, false, hash);
13984 if (new_r != r)
13985 /* We instantiated this while substituting into
13986 the type earlier (template/friend54.C). */
13987 return new_r;
13988
13989 /* We're not supposed to instantiate default arguments
13990 until they are called, for a template. But, for a
13991 declaration like:
13992
13993 template <class T> void f ()
13994 { extern void g(int i = T()); }
13995
13996 we should do the substitution when the template is
13997 instantiated. We handle the member function case in
13998 instantiate_class_template since the default arguments
13999 might refer to other members of the class. */
14000 if (!member
14001 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14002 && !uses_template_parms (argvec))
14003 tsubst_default_arguments (r, complain);
14004 }
14005 else if (DECL_LOCAL_DECL_P (r))
14006 {
14007 if (!cp_unevaluated_operand)
14008 register_local_specialization (r, t);
14009 }
14010 else
14011 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14012
14013 /* Copy the list of befriending classes. */
14014 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14015 *friends;
14016 friends = &TREE_CHAIN (*friends))
14017 {
14018 *friends = copy_node (*friends);
14019 TREE_VALUE (*friends)
14020 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14021 }
14022
14023 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14024 {
14025 maybe_retrofit_in_chrg (r);
14026 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14027 return error_mark_node;
14028 /* If this is an instantiation of a member template, clone it.
14029 If it isn't, that'll be handled by
14030 clone_constructors_and_destructors. */
14031 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14032 clone_cdtor (r, /*update_methods=*/false);
14033 }
14034 else if ((complain & tf_error) != 0
14035 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14036 && !grok_op_properties (r, /*complain=*/true))
14037 return error_mark_node;
14038
14039 /* Possibly limit visibility based on template args. */
14040 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14041 if (DECL_VISIBILITY_SPECIFIED (t))
14042 {
14043 DECL_VISIBILITY_SPECIFIED (r) = 0;
14044 DECL_ATTRIBUTES (r)
14045 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14046 }
14047 determine_visibility (r);
14048 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14049 && !processing_template_decl)
14050 defaulted_late_check (r);
14051
14052 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14053 args, complain, in_decl);
14054 if (flag_openmp)
14055 if (tree attr = lookup_attribute ("omp declare variant base",
14056 DECL_ATTRIBUTES (r)))
14057 omp_declare_variant_finalize (r, attr);
14058
14059 return r;
14060 }
14061
14062 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14063
14064 static tree
14065 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14066 tree lambda_fntype)
14067 {
14068 /* We can get here when processing a member function template,
14069 member class template, or template template parameter. */
14070 tree decl = DECL_TEMPLATE_RESULT (t);
14071 tree in_decl = t;
14072 tree spec;
14073 tree tmpl_args;
14074 tree full_args;
14075 tree r;
14076 hashval_t hash = 0;
14077
14078 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14079 {
14080 /* Template template parameter is treated here. */
14081 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14082 if (new_type == error_mark_node)
14083 r = error_mark_node;
14084 /* If we get a real template back, return it. This can happen in
14085 the context of most_specialized_partial_spec. */
14086 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14087 r = new_type;
14088 else
14089 /* The new TEMPLATE_DECL was built in
14090 reduce_template_parm_level. */
14091 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14092 return r;
14093 }
14094
14095 if (!lambda_fntype)
14096 {
14097 /* We might already have an instance of this template.
14098 The ARGS are for the surrounding class type, so the
14099 full args contain the tsubst'd args for the context,
14100 plus the innermost args from the template decl. */
14101 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14102 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14103 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14104 /* Because this is a template, the arguments will still be
14105 dependent, even after substitution. If
14106 PROCESSING_TEMPLATE_DECL is not set, the dependency
14107 predicates will short-circuit. */
14108 ++processing_template_decl;
14109 full_args = tsubst_template_args (tmpl_args, args,
14110 complain, in_decl);
14111 --processing_template_decl;
14112 if (full_args == error_mark_node)
14113 return error_mark_node;
14114
14115 /* If this is a default template template argument,
14116 tsubst might not have changed anything. */
14117 if (full_args == tmpl_args)
14118 return t;
14119
14120 hash = hash_tmpl_and_args (t, full_args);
14121 spec = retrieve_specialization (t, full_args, hash);
14122 if (spec != NULL_TREE)
14123 {
14124 if (TYPE_P (spec))
14125 /* Type partial instantiations are stored as the type by
14126 lookup_template_class_1, not here as the template. */
14127 spec = CLASSTYPE_TI_TEMPLATE (spec);
14128 return spec;
14129 }
14130 }
14131
14132 /* Make a new template decl. It will be similar to the
14133 original, but will record the current template arguments.
14134 We also create a new function declaration, which is just
14135 like the old one, but points to this new template, rather
14136 than the old one. */
14137 r = copy_decl (t);
14138 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14139 DECL_CHAIN (r) = NULL_TREE;
14140
14141 // Build new template info linking to the original template decl.
14142 if (!lambda_fntype)
14143 {
14144 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14145 SET_DECL_IMPLICIT_INSTANTIATION (r);
14146 }
14147 else
14148 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14149
14150 /* The template parameters for this new template are all the
14151 template parameters for the old template, except the
14152 outermost level of parameters. */
14153 DECL_TEMPLATE_PARMS (r)
14154 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14155 complain);
14156
14157 bool class_p = false;
14158 tree inner = decl;
14159 ++processing_template_decl;
14160 if (TREE_CODE (inner) == FUNCTION_DECL)
14161 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14162 else
14163 {
14164 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14165 {
14166 class_p = true;
14167 inner = TREE_TYPE (inner);
14168 }
14169 inner = tsubst (inner, args, complain, in_decl);
14170 }
14171 --processing_template_decl;
14172 if (inner == error_mark_node)
14173 return error_mark_node;
14174
14175 if (class_p)
14176 {
14177 /* For a partial specialization, we need to keep pointing to
14178 the primary template. */
14179 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14180 CLASSTYPE_TI_TEMPLATE (inner) = r;
14181
14182 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14183 inner = TYPE_MAIN_DECL (inner);
14184 }
14185 else if (lambda_fntype)
14186 {
14187 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14188 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14189 }
14190 else
14191 {
14192 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14193 DECL_TI_TEMPLATE (inner) = r;
14194 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14195 }
14196
14197 DECL_TEMPLATE_RESULT (r) = inner;
14198 TREE_TYPE (r) = TREE_TYPE (inner);
14199 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14200
14201 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14202 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14203
14204 if (PRIMARY_TEMPLATE_P (t))
14205 DECL_PRIMARY_TEMPLATE (r) = r;
14206
14207 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
14208 && !lambda_fntype)
14209 /* Record this non-type partial instantiation. */
14210 register_specialization (r, t,
14211 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14212 false, hash);
14213
14214 return r;
14215 }
14216
14217 /* True if FN is the op() for a lambda in an uninstantiated template. */
14218
14219 bool
14220 lambda_fn_in_template_p (tree fn)
14221 {
14222 if (!fn || !LAMBDA_FUNCTION_P (fn))
14223 return false;
14224 tree closure = DECL_CONTEXT (fn);
14225 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14226 }
14227
14228 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14229 which the above is true. */
14230
14231 bool
14232 instantiated_lambda_fn_p (tree fn)
14233 {
14234 if (!fn || !LAMBDA_FUNCTION_P (fn))
14235 return false;
14236 tree closure = DECL_CONTEXT (fn);
14237 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14238 return LAMBDA_EXPR_INSTANTIATED (lam);
14239 }
14240
14241 /* We're instantiating a variable from template function TCTX. Return the
14242 corresponding current enclosing scope. This gets complicated because lambda
14243 functions in templates are regenerated rather than instantiated, but generic
14244 lambda functions are subsequently instantiated. */
14245
14246 static tree
14247 enclosing_instantiation_of (tree otctx)
14248 {
14249 tree tctx = otctx;
14250 tree fn = current_function_decl;
14251 int lambda_count = 0;
14252
14253 for (; tctx && (lambda_fn_in_template_p (tctx)
14254 || instantiated_lambda_fn_p (tctx));
14255 tctx = decl_function_context (tctx))
14256 ++lambda_count;
14257 for (; fn; fn = decl_function_context (fn))
14258 {
14259 tree ofn = fn;
14260 int flambda_count = 0;
14261 for (; fn && instantiated_lambda_fn_p (fn);
14262 fn = decl_function_context (fn))
14263 ++flambda_count;
14264 if ((fn && DECL_TEMPLATE_INFO (fn))
14265 ? most_general_template (fn) != most_general_template (tctx)
14266 : fn != tctx)
14267 continue;
14268 if (flambda_count != lambda_count)
14269 {
14270 gcc_assert (flambda_count > lambda_count);
14271 for (; flambda_count > lambda_count; --flambda_count)
14272 ofn = decl_function_context (ofn);
14273 }
14274 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14275 || DECL_CONV_FN_P (ofn));
14276 return ofn;
14277 }
14278 gcc_unreachable ();
14279 }
14280
14281 /* Substitute the ARGS into the T, which is a _DECL. Return the
14282 result of the substitution. Issue error and warning messages under
14283 control of COMPLAIN. */
14284
14285 static tree
14286 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14287 {
14288 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14289 location_t saved_loc;
14290 tree r = NULL_TREE;
14291 tree in_decl = t;
14292 hashval_t hash = 0;
14293
14294 /* Set the filename and linenumber to improve error-reporting. */
14295 saved_loc = input_location;
14296 input_location = DECL_SOURCE_LOCATION (t);
14297
14298 switch (TREE_CODE (t))
14299 {
14300 case TEMPLATE_DECL:
14301 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14302 break;
14303
14304 case FUNCTION_DECL:
14305 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14306 break;
14307
14308 case PARM_DECL:
14309 {
14310 tree type = NULL_TREE;
14311 int i, len = 1;
14312 tree expanded_types = NULL_TREE;
14313 tree prev_r = NULL_TREE;
14314 tree first_r = NULL_TREE;
14315
14316 if (DECL_PACK_P (t))
14317 {
14318 /* If there is a local specialization that isn't a
14319 parameter pack, it means that we're doing a "simple"
14320 substitution from inside tsubst_pack_expansion. Just
14321 return the local specialization (which will be a single
14322 parm). */
14323 tree spec = retrieve_local_specialization (t);
14324 if (spec
14325 && TREE_CODE (spec) == PARM_DECL
14326 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14327 RETURN (spec);
14328
14329 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14330 the parameters in this function parameter pack. */
14331 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14332 complain, in_decl);
14333 if (TREE_CODE (expanded_types) == TREE_VEC)
14334 {
14335 len = TREE_VEC_LENGTH (expanded_types);
14336
14337 /* Zero-length parameter packs are boring. Just substitute
14338 into the chain. */
14339 if (len == 0 && !cp_unevaluated_operand)
14340 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14341 TREE_CHAIN (t)));
14342 }
14343 else
14344 {
14345 /* All we did was update the type. Make a note of that. */
14346 type = expanded_types;
14347 expanded_types = NULL_TREE;
14348 }
14349 }
14350
14351 /* Loop through all of the parameters we'll build. When T is
14352 a function parameter pack, LEN is the number of expanded
14353 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14354 r = NULL_TREE;
14355 for (i = 0; i < len; ++i)
14356 {
14357 prev_r = r;
14358 r = copy_node (t);
14359 if (DECL_TEMPLATE_PARM_P (t))
14360 SET_DECL_TEMPLATE_PARM_P (r);
14361
14362 if (expanded_types)
14363 /* We're on the Ith parameter of the function parameter
14364 pack. */
14365 {
14366 /* Get the Ith type. */
14367 type = TREE_VEC_ELT (expanded_types, i);
14368
14369 /* Rename the parameter to include the index. */
14370 DECL_NAME (r)
14371 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14372 }
14373 else if (!type)
14374 /* We're dealing with a normal parameter. */
14375 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14376
14377 type = type_decays_to (type);
14378 TREE_TYPE (r) = type;
14379 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14380
14381 if (DECL_INITIAL (r))
14382 {
14383 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14384 DECL_INITIAL (r) = TREE_TYPE (r);
14385 else
14386 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14387 complain, in_decl);
14388 }
14389
14390 DECL_CONTEXT (r) = NULL_TREE;
14391
14392 if (!DECL_TEMPLATE_PARM_P (r))
14393 DECL_ARG_TYPE (r) = type_passed_as (type);
14394
14395 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14396 args, complain, in_decl);
14397
14398 /* Keep track of the first new parameter we
14399 generate. That's what will be returned to the
14400 caller. */
14401 if (!first_r)
14402 first_r = r;
14403
14404 /* Build a proper chain of parameters when substituting
14405 into a function parameter pack. */
14406 if (prev_r)
14407 DECL_CHAIN (prev_r) = r;
14408 }
14409
14410 /* If cp_unevaluated_operand is set, we're just looking for a
14411 single dummy parameter, so don't keep going. */
14412 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14413 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14414 complain, DECL_CHAIN (t));
14415
14416 /* FIRST_R contains the start of the chain we've built. */
14417 r = first_r;
14418 }
14419 break;
14420
14421 case FIELD_DECL:
14422 {
14423 tree type = NULL_TREE;
14424 tree vec = NULL_TREE;
14425 tree expanded_types = NULL_TREE;
14426 int len = 1;
14427
14428 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14429 {
14430 /* This field is a lambda capture pack. Return a TREE_VEC of
14431 the expanded fields to instantiate_class_template_1. */
14432 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14433 complain, in_decl);
14434 if (TREE_CODE (expanded_types) == TREE_VEC)
14435 {
14436 len = TREE_VEC_LENGTH (expanded_types);
14437 vec = make_tree_vec (len);
14438 }
14439 else
14440 {
14441 /* All we did was update the type. Make a note of that. */
14442 type = expanded_types;
14443 expanded_types = NULL_TREE;
14444 }
14445 }
14446
14447 for (int i = 0; i < len; ++i)
14448 {
14449 r = copy_decl (t);
14450 if (expanded_types)
14451 {
14452 type = TREE_VEC_ELT (expanded_types, i);
14453 DECL_NAME (r)
14454 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14455 }
14456 else if (!type)
14457 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14458
14459 if (type == error_mark_node)
14460 RETURN (error_mark_node);
14461 TREE_TYPE (r) = type;
14462 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14463
14464 if (DECL_C_BIT_FIELD (r))
14465 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14466 number of bits. */
14467 DECL_BIT_FIELD_REPRESENTATIVE (r)
14468 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14469 complain, in_decl,
14470 /*integral_constant_expression_p=*/true);
14471 if (DECL_INITIAL (t))
14472 {
14473 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14474 NSDMI in perform_member_init. Still set DECL_INITIAL
14475 so that we know there is one. */
14476 DECL_INITIAL (r) = void_node;
14477 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14478 retrofit_lang_decl (r);
14479 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14480 }
14481 /* We don't have to set DECL_CONTEXT here; it is set by
14482 finish_member_declaration. */
14483 DECL_CHAIN (r) = NULL_TREE;
14484
14485 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14486 args, complain, in_decl);
14487
14488 if (vec)
14489 TREE_VEC_ELT (vec, i) = r;
14490 }
14491
14492 if (vec)
14493 r = vec;
14494 }
14495 break;
14496
14497 case USING_DECL:
14498 /* We reach here only for member using decls. We also need to check
14499 uses_template_parms because DECL_DEPENDENT_P is not set for a
14500 using-declaration that designates a member of the current
14501 instantiation (c++/53549). */
14502 if (DECL_DEPENDENT_P (t)
14503 || uses_template_parms (USING_DECL_SCOPE (t)))
14504 {
14505 tree scope = USING_DECL_SCOPE (t);
14506 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14507 if (PACK_EXPANSION_P (scope))
14508 {
14509 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14510 int len = TREE_VEC_LENGTH (vec);
14511 r = make_tree_vec (len);
14512 for (int i = 0; i < len; ++i)
14513 {
14514 tree escope = TREE_VEC_ELT (vec, i);
14515 tree elt = do_class_using_decl (escope, name);
14516 if (!elt)
14517 {
14518 r = error_mark_node;
14519 break;
14520 }
14521 else
14522 {
14523 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14524 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14525 }
14526 TREE_VEC_ELT (r, i) = elt;
14527 }
14528 }
14529 else
14530 {
14531 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14532 complain, in_decl);
14533 r = do_class_using_decl (inst_scope, name);
14534 if (!r)
14535 r = error_mark_node;
14536 else
14537 {
14538 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14539 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14540 }
14541 }
14542 }
14543 else
14544 {
14545 r = copy_node (t);
14546 DECL_CHAIN (r) = NULL_TREE;
14547 }
14548 break;
14549
14550 case TYPE_DECL:
14551 case VAR_DECL:
14552 {
14553 tree argvec = NULL_TREE;
14554 tree gen_tmpl = NULL_TREE;
14555 tree tmpl = NULL_TREE;
14556 tree type = NULL_TREE;
14557
14558 if (TREE_TYPE (t) == error_mark_node)
14559 RETURN (error_mark_node);
14560
14561 if (TREE_CODE (t) == TYPE_DECL
14562 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14563 {
14564 /* If this is the canonical decl, we don't have to
14565 mess with instantiations, and often we can't (for
14566 typename, template type parms and such). Note that
14567 TYPE_NAME is not correct for the above test if
14568 we've copied the type for a typedef. */
14569 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14570 if (type == error_mark_node)
14571 RETURN (error_mark_node);
14572 r = TYPE_NAME (type);
14573 break;
14574 }
14575
14576 /* Check to see if we already have the specialization we
14577 need. */
14578 tree spec = NULL_TREE;
14579 bool local_p = false;
14580 tree ctx = DECL_CONTEXT (t);
14581 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14582 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14583 {
14584 local_p = false;
14585 if (DECL_CLASS_SCOPE_P (t))
14586 {
14587 ctx = tsubst_aggr_type (ctx, args,
14588 complain,
14589 in_decl, /*entering_scope=*/1);
14590 /* If CTX is unchanged, then T is in fact the
14591 specialization we want. That situation occurs when
14592 referencing a static data member within in its own
14593 class. We can use pointer equality, rather than
14594 same_type_p, because DECL_CONTEXT is always
14595 canonical... */
14596 if (ctx == DECL_CONTEXT (t)
14597 /* ... unless T is a member template; in which
14598 case our caller can be willing to create a
14599 specialization of that template represented
14600 by T. */
14601 && !(DECL_TI_TEMPLATE (t)
14602 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14603 spec = t;
14604 }
14605
14606 if (!spec)
14607 {
14608 tmpl = DECL_TI_TEMPLATE (t);
14609 gen_tmpl = most_general_template (tmpl);
14610 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14611 if (argvec != error_mark_node)
14612 argvec = (coerce_innermost_template_parms
14613 (DECL_TEMPLATE_PARMS (gen_tmpl),
14614 argvec, t, complain,
14615 /*all*/true, /*defarg*/true));
14616 if (argvec == error_mark_node)
14617 RETURN (error_mark_node);
14618 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14619 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14620 }
14621 }
14622 else
14623 {
14624 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14625 /* Subsequent calls to pushdecl will fill this in. */
14626 ctx = NULL_TREE;
14627 /* A local variable. */
14628 local_p = true;
14629 /* Unless this is a reference to a static variable from an
14630 enclosing function, in which case we need to fill it in now. */
14631 if (TREE_STATIC (t))
14632 {
14633 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14634 if (fn != current_function_decl)
14635 ctx = fn;
14636 }
14637 spec = retrieve_local_specialization (t);
14638 }
14639 /* If we already have the specialization we need, there is
14640 nothing more to do. */
14641 if (spec)
14642 {
14643 r = spec;
14644 break;
14645 }
14646
14647 /* Create a new node for the specialization we need. */
14648 if (type == NULL_TREE)
14649 {
14650 if (is_typedef_decl (t))
14651 type = DECL_ORIGINAL_TYPE (t);
14652 else
14653 type = TREE_TYPE (t);
14654 if (VAR_P (t)
14655 && VAR_HAD_UNKNOWN_BOUND (t)
14656 && type != error_mark_node)
14657 type = strip_array_domain (type);
14658 tree sub_args = args;
14659 if (tree auto_node = type_uses_auto (type))
14660 {
14661 /* Mask off any template args past the variable's context so we
14662 don't replace the auto with an unrelated argument. */
14663 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14664 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14665 if (extra > 0)
14666 /* This should never happen with the new lambda instantiation
14667 model, but keep the handling just in case. */
14668 gcc_assert (!CHECKING_P),
14669 sub_args = strip_innermost_template_args (args, extra);
14670 }
14671 type = tsubst (type, sub_args, complain, in_decl);
14672 /* Substituting the type might have recursively instantiated this
14673 same alias (c++/86171). */
14674 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14675 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14676 {
14677 r = spec;
14678 break;
14679 }
14680 }
14681 r = copy_decl (t);
14682 if (VAR_P (r))
14683 {
14684 DECL_INITIALIZED_P (r) = 0;
14685 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14686 if (type == error_mark_node)
14687 RETURN (error_mark_node);
14688 if (TREE_CODE (type) == FUNCTION_TYPE)
14689 {
14690 /* It may seem that this case cannot occur, since:
14691
14692 typedef void f();
14693 void g() { f x; }
14694
14695 declares a function, not a variable. However:
14696
14697 typedef void f();
14698 template <typename T> void g() { T t; }
14699 template void g<f>();
14700
14701 is an attempt to declare a variable with function
14702 type. */
14703 error ("variable %qD has function type",
14704 /* R is not yet sufficiently initialized, so we
14705 just use its name. */
14706 DECL_NAME (r));
14707 RETURN (error_mark_node);
14708 }
14709 type = complete_type (type);
14710 /* Wait until cp_finish_decl to set this again, to handle
14711 circular dependency (template/instantiate6.C). */
14712 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14713 type = check_var_type (DECL_NAME (r), type,
14714 DECL_SOURCE_LOCATION (r));
14715 if (DECL_HAS_VALUE_EXPR_P (t))
14716 {
14717 tree ve = DECL_VALUE_EXPR (t);
14718 /* If the DECL_VALUE_EXPR is converted to the declared type,
14719 preserve the identity so that gimplify_type_sizes works. */
14720 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14721 if (nop)
14722 ve = TREE_OPERAND (ve, 0);
14723 ve = tsubst_expr (ve, args, complain, in_decl,
14724 /*constant_expression_p=*/false);
14725 if (REFERENCE_REF_P (ve))
14726 {
14727 gcc_assert (TYPE_REF_P (type));
14728 ve = TREE_OPERAND (ve, 0);
14729 }
14730 if (nop)
14731 ve = build_nop (type, ve);
14732 else if (DECL_LANG_SPECIFIC (t)
14733 && DECL_OMP_PRIVATIZED_MEMBER (t)
14734 && TREE_CODE (ve) == COMPONENT_REF
14735 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14736 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14737 type = TREE_TYPE (ve);
14738 else
14739 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14740 == TYPE_MAIN_VARIANT (type));
14741 SET_DECL_VALUE_EXPR (r, ve);
14742 }
14743 if (CP_DECL_THREAD_LOCAL_P (r)
14744 && !processing_template_decl)
14745 set_decl_tls_model (r, decl_default_tls_model (r));
14746 }
14747 else if (DECL_SELF_REFERENCE_P (t))
14748 SET_DECL_SELF_REFERENCE_P (r);
14749 TREE_TYPE (r) = type;
14750 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14751 DECL_CONTEXT (r) = ctx;
14752 /* Clear out the mangled name and RTL for the instantiation. */
14753 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14754 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14755 SET_DECL_RTL (r, NULL);
14756 /* The initializer must not be expanded until it is required;
14757 see [temp.inst]. */
14758 DECL_INITIAL (r) = NULL_TREE;
14759 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14760 if (VAR_P (r))
14761 {
14762 if (DECL_LANG_SPECIFIC (r))
14763 SET_DECL_DEPENDENT_INIT_P (r, false);
14764
14765 SET_DECL_MODE (r, VOIDmode);
14766
14767 /* Possibly limit visibility based on template args. */
14768 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14769 if (DECL_VISIBILITY_SPECIFIED (t))
14770 {
14771 DECL_VISIBILITY_SPECIFIED (r) = 0;
14772 DECL_ATTRIBUTES (r)
14773 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14774 }
14775 determine_visibility (r);
14776 }
14777
14778 if (!local_p)
14779 {
14780 /* A static data member declaration is always marked
14781 external when it is declared in-class, even if an
14782 initializer is present. We mimic the non-template
14783 processing here. */
14784 DECL_EXTERNAL (r) = 1;
14785 if (DECL_NAMESPACE_SCOPE_P (t))
14786 DECL_NOT_REALLY_EXTERN (r) = 1;
14787
14788 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14789 SET_DECL_IMPLICIT_INSTANTIATION (r);
14790 if (!error_operand_p (r) || (complain & tf_error))
14791 register_specialization (r, gen_tmpl, argvec, false, hash);
14792 }
14793 else
14794 {
14795 if (DECL_LANG_SPECIFIC (r))
14796 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14797 if (!cp_unevaluated_operand)
14798 register_local_specialization (r, t);
14799 }
14800
14801 DECL_CHAIN (r) = NULL_TREE;
14802
14803 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14804 /*flags=*/0,
14805 args, complain, in_decl);
14806
14807 /* Preserve a typedef that names a type. */
14808 if (is_typedef_decl (r) && type != error_mark_node)
14809 {
14810 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14811 set_underlying_type (r);
14812 if (TYPE_DECL_ALIAS_P (r))
14813 /* An alias template specialization can be dependent
14814 even if its underlying type is not. */
14815 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14816 }
14817
14818 layout_decl (r, 0);
14819 }
14820 break;
14821
14822 default:
14823 gcc_unreachable ();
14824 }
14825 #undef RETURN
14826
14827 out:
14828 /* Restore the file and line information. */
14829 input_location = saved_loc;
14830
14831 return r;
14832 }
14833
14834 /* Substitute into the complete parameter type list PARMS. */
14835
14836 tree
14837 tsubst_function_parms (tree parms,
14838 tree args,
14839 tsubst_flags_t complain,
14840 tree in_decl)
14841 {
14842 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14843 }
14844
14845 /* Substitute into the ARG_TYPES of a function type.
14846 If END is a TREE_CHAIN, leave it and any following types
14847 un-substituted. */
14848
14849 static tree
14850 tsubst_arg_types (tree arg_types,
14851 tree args,
14852 tree end,
14853 tsubst_flags_t complain,
14854 tree in_decl)
14855 {
14856 tree remaining_arg_types;
14857 tree type = NULL_TREE;
14858 int i = 1;
14859 tree expanded_args = NULL_TREE;
14860 tree default_arg;
14861
14862 if (!arg_types || arg_types == void_list_node || arg_types == end)
14863 return arg_types;
14864
14865 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14866 args, end, complain, in_decl);
14867 if (remaining_arg_types == error_mark_node)
14868 return error_mark_node;
14869
14870 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14871 {
14872 /* For a pack expansion, perform substitution on the
14873 entire expression. Later on, we'll handle the arguments
14874 one-by-one. */
14875 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14876 args, complain, in_decl);
14877
14878 if (TREE_CODE (expanded_args) == TREE_VEC)
14879 /* So that we'll spin through the parameters, one by one. */
14880 i = TREE_VEC_LENGTH (expanded_args);
14881 else
14882 {
14883 /* We only partially substituted into the parameter
14884 pack. Our type is TYPE_PACK_EXPANSION. */
14885 type = expanded_args;
14886 expanded_args = NULL_TREE;
14887 }
14888 }
14889
14890 while (i > 0) {
14891 --i;
14892
14893 if (expanded_args)
14894 type = TREE_VEC_ELT (expanded_args, i);
14895 else if (!type)
14896 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14897
14898 if (type == error_mark_node)
14899 return error_mark_node;
14900 if (VOID_TYPE_P (type))
14901 {
14902 if (complain & tf_error)
14903 {
14904 error ("invalid parameter type %qT", type);
14905 if (in_decl)
14906 error ("in declaration %q+D", in_decl);
14907 }
14908 return error_mark_node;
14909 }
14910 /* DR 657. */
14911 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14912 return error_mark_node;
14913
14914 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14915 top-level qualifiers as required. */
14916 type = cv_unqualified (type_decays_to (type));
14917
14918 /* We do not substitute into default arguments here. The standard
14919 mandates that they be instantiated only when needed, which is
14920 done in build_over_call. */
14921 default_arg = TREE_PURPOSE (arg_types);
14922
14923 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14924 since the new op() won't have any associated template arguments for us
14925 to refer to later. */
14926 if (lambda_fn_in_template_p (in_decl))
14927 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14928 false/*fn*/, false/*constexpr*/);
14929
14930 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14931 {
14932 /* We've instantiated a template before its default arguments
14933 have been parsed. This can happen for a nested template
14934 class, and is not an error unless we require the default
14935 argument in a call of this function. */
14936 remaining_arg_types =
14937 tree_cons (default_arg, type, remaining_arg_types);
14938 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14939 remaining_arg_types);
14940 }
14941 else
14942 remaining_arg_types =
14943 hash_tree_cons (default_arg, type, remaining_arg_types);
14944 }
14945
14946 return remaining_arg_types;
14947 }
14948
14949 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14950 *not* handle the exception-specification for FNTYPE, because the
14951 initial substitution of explicitly provided template parameters
14952 during argument deduction forbids substitution into the
14953 exception-specification:
14954
14955 [temp.deduct]
14956
14957 All references in the function type of the function template to the
14958 corresponding template parameters are replaced by the specified tem-
14959 plate argument values. If a substitution in a template parameter or
14960 in the function type of the function template results in an invalid
14961 type, type deduction fails. [Note: The equivalent substitution in
14962 exception specifications is done only when the function is instanti-
14963 ated, at which point a program is ill-formed if the substitution
14964 results in an invalid type.] */
14965
14966 static tree
14967 tsubst_function_type (tree t,
14968 tree args,
14969 tsubst_flags_t complain,
14970 tree in_decl)
14971 {
14972 tree return_type;
14973 tree arg_types = NULL_TREE;
14974
14975 /* The TYPE_CONTEXT is not used for function/method types. */
14976 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14977
14978 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14979 failure. */
14980 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14981
14982 if (late_return_type_p)
14983 {
14984 /* Substitute the argument types. */
14985 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14986 complain, in_decl);
14987 if (arg_types == error_mark_node)
14988 return error_mark_node;
14989
14990 tree save_ccp = current_class_ptr;
14991 tree save_ccr = current_class_ref;
14992 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14993 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14994 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14995 if (do_inject)
14996 {
14997 /* DR 1207: 'this' is in scope in the trailing return type. */
14998 inject_this_parameter (this_type, cp_type_quals (this_type));
14999 }
15000
15001 /* Substitute the return type. */
15002 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15003
15004 if (do_inject)
15005 {
15006 current_class_ptr = save_ccp;
15007 current_class_ref = save_ccr;
15008 }
15009 }
15010 else
15011 /* Substitute the return type. */
15012 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15013
15014 if (return_type == error_mark_node)
15015 return error_mark_node;
15016 /* DR 486 clarifies that creation of a function type with an
15017 invalid return type is a deduction failure. */
15018 if (TREE_CODE (return_type) == ARRAY_TYPE
15019 || TREE_CODE (return_type) == FUNCTION_TYPE)
15020 {
15021 if (complain & tf_error)
15022 {
15023 if (TREE_CODE (return_type) == ARRAY_TYPE)
15024 error ("function returning an array");
15025 else
15026 error ("function returning a function");
15027 }
15028 return error_mark_node;
15029 }
15030 /* And DR 657. */
15031 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
15032 return error_mark_node;
15033
15034 if (!late_return_type_p)
15035 {
15036 /* Substitute the argument types. */
15037 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15038 complain, in_decl);
15039 if (arg_types == error_mark_node)
15040 return error_mark_node;
15041 }
15042
15043 /* Construct a new type node and return it. */
15044 return rebuild_function_or_method_type (t, return_type, arg_types,
15045 /*raises=*/NULL_TREE, complain);
15046 }
15047
15048 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15049 ARGS into that specification, and return the substituted
15050 specification. If there is no specification, return NULL_TREE. */
15051
15052 static tree
15053 tsubst_exception_specification (tree fntype,
15054 tree args,
15055 tsubst_flags_t complain,
15056 tree in_decl,
15057 bool defer_ok)
15058 {
15059 tree specs;
15060 tree new_specs;
15061
15062 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15063 new_specs = NULL_TREE;
15064 if (specs && TREE_PURPOSE (specs))
15065 {
15066 /* A noexcept-specifier. */
15067 tree expr = TREE_PURPOSE (specs);
15068 if (TREE_CODE (expr) == INTEGER_CST)
15069 new_specs = expr;
15070 else if (defer_ok)
15071 {
15072 /* Defer instantiation of noexcept-specifiers to avoid
15073 excessive instantiations (c++/49107). */
15074 new_specs = make_node (DEFERRED_NOEXCEPT);
15075 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15076 {
15077 /* We already partially instantiated this member template,
15078 so combine the new args with the old. */
15079 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15080 = DEFERRED_NOEXCEPT_PATTERN (expr);
15081 DEFERRED_NOEXCEPT_ARGS (new_specs)
15082 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15083 }
15084 else
15085 {
15086 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15087 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15088 }
15089 }
15090 else
15091 {
15092 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15093 {
15094 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15095 args);
15096 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15097 }
15098 new_specs = tsubst_copy_and_build
15099 (expr, args, complain, in_decl, /*function_p=*/false,
15100 /*integral_constant_expression_p=*/true);
15101 }
15102 new_specs = build_noexcept_spec (new_specs, complain);
15103 }
15104 else if (specs)
15105 {
15106 if (! TREE_VALUE (specs))
15107 new_specs = specs;
15108 else
15109 while (specs)
15110 {
15111 tree spec;
15112 int i, len = 1;
15113 tree expanded_specs = NULL_TREE;
15114
15115 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15116 {
15117 /* Expand the pack expansion type. */
15118 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15119 args, complain,
15120 in_decl);
15121
15122 if (expanded_specs == error_mark_node)
15123 return error_mark_node;
15124 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15125 len = TREE_VEC_LENGTH (expanded_specs);
15126 else
15127 {
15128 /* We're substituting into a member template, so
15129 we got a TYPE_PACK_EXPANSION back. Add that
15130 expansion and move on. */
15131 gcc_assert (TREE_CODE (expanded_specs)
15132 == TYPE_PACK_EXPANSION);
15133 new_specs = add_exception_specifier (new_specs,
15134 expanded_specs,
15135 complain);
15136 specs = TREE_CHAIN (specs);
15137 continue;
15138 }
15139 }
15140
15141 for (i = 0; i < len; ++i)
15142 {
15143 if (expanded_specs)
15144 spec = TREE_VEC_ELT (expanded_specs, i);
15145 else
15146 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15147 if (spec == error_mark_node)
15148 return spec;
15149 new_specs = add_exception_specifier (new_specs, spec,
15150 complain);
15151 }
15152
15153 specs = TREE_CHAIN (specs);
15154 }
15155 }
15156 return new_specs;
15157 }
15158
15159 /* Substitute through a TREE_LIST of types or expressions, handling pack
15160 expansions. */
15161
15162 tree
15163 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15164 {
15165 if (t == void_list_node)
15166 return t;
15167
15168 tree purpose = TREE_PURPOSE (t);
15169 tree purposevec = NULL_TREE;
15170 if (!purpose)
15171 ;
15172 else if (PACK_EXPANSION_P (purpose))
15173 {
15174 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15175 if (TREE_CODE (purpose) == TREE_VEC)
15176 purposevec = purpose;
15177 }
15178 else if (TYPE_P (purpose))
15179 purpose = tsubst (purpose, args, complain, in_decl);
15180 else
15181 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15182 if (purpose == error_mark_node || purposevec == error_mark_node)
15183 return error_mark_node;
15184
15185 tree value = TREE_VALUE (t);
15186 tree valuevec = NULL_TREE;
15187 if (!value)
15188 ;
15189 else if (PACK_EXPANSION_P (value))
15190 {
15191 value = tsubst_pack_expansion (value, args, complain, in_decl);
15192 if (TREE_CODE (value) == TREE_VEC)
15193 valuevec = value;
15194 }
15195 else if (TYPE_P (value))
15196 value = tsubst (value, args, complain, in_decl);
15197 else
15198 value = tsubst_copy_and_build (value, args, complain, in_decl);
15199 if (value == error_mark_node || valuevec == error_mark_node)
15200 return error_mark_node;
15201
15202 tree chain = TREE_CHAIN (t);
15203 if (!chain)
15204 ;
15205 else if (TREE_CODE (chain) == TREE_LIST)
15206 chain = tsubst_tree_list (chain, args, complain, in_decl);
15207 else if (TYPE_P (chain))
15208 chain = tsubst (chain, args, complain, in_decl);
15209 else
15210 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15211 if (chain == error_mark_node)
15212 return error_mark_node;
15213
15214 if (purpose == TREE_PURPOSE (t)
15215 && value == TREE_VALUE (t)
15216 && chain == TREE_CHAIN (t))
15217 return t;
15218
15219 int len;
15220 /* Determine the number of arguments. */
15221 if (purposevec)
15222 {
15223 len = TREE_VEC_LENGTH (purposevec);
15224 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15225 }
15226 else if (valuevec)
15227 len = TREE_VEC_LENGTH (valuevec);
15228 else
15229 len = 1;
15230
15231 for (int i = len; i-- > 0; )
15232 {
15233 if (purposevec)
15234 purpose = TREE_VEC_ELT (purposevec, i);
15235 if (valuevec)
15236 value = TREE_VEC_ELT (valuevec, i);
15237
15238 if (value && TYPE_P (value))
15239 chain = hash_tree_cons (purpose, value, chain);
15240 else
15241 chain = tree_cons (purpose, value, chain);
15242 }
15243
15244 return chain;
15245 }
15246
15247 /* Take the tree structure T and replace template parameters used
15248 therein with the argument vector ARGS. IN_DECL is an associated
15249 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15250 Issue error and warning messages under control of COMPLAIN. Note
15251 that we must be relatively non-tolerant of extensions here, in
15252 order to preserve conformance; if we allow substitutions that
15253 should not be allowed, we may allow argument deductions that should
15254 not succeed, and therefore report ambiguous overload situations
15255 where there are none. In theory, we could allow the substitution,
15256 but indicate that it should have failed, and allow our caller to
15257 make sure that the right thing happens, but we don't try to do this
15258 yet.
15259
15260 This function is used for dealing with types, decls and the like;
15261 for expressions, use tsubst_expr or tsubst_copy. */
15262
15263 tree
15264 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15265 {
15266 enum tree_code code;
15267 tree type, r = NULL_TREE;
15268
15269 if (t == NULL_TREE || t == error_mark_node
15270 || t == integer_type_node
15271 || t == void_type_node
15272 || t == char_type_node
15273 || t == unknown_type_node
15274 || TREE_CODE (t) == NAMESPACE_DECL
15275 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15276 return t;
15277
15278 if (DECL_P (t))
15279 return tsubst_decl (t, args, complain);
15280
15281 if (args == NULL_TREE)
15282 return t;
15283
15284 code = TREE_CODE (t);
15285
15286 if (code == IDENTIFIER_NODE)
15287 type = IDENTIFIER_TYPE_VALUE (t);
15288 else
15289 type = TREE_TYPE (t);
15290
15291 gcc_assert (type != unknown_type_node);
15292
15293 /* Reuse typedefs. We need to do this to handle dependent attributes,
15294 such as attribute aligned. */
15295 if (TYPE_P (t)
15296 && typedef_variant_p (t))
15297 {
15298 tree decl = TYPE_NAME (t);
15299
15300 if (alias_template_specialization_p (t, nt_opaque))
15301 {
15302 /* DECL represents an alias template and we want to
15303 instantiate it. */
15304 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15305 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15306 r = instantiate_alias_template (tmpl, gen_args, complain);
15307 }
15308 else if (DECL_CLASS_SCOPE_P (decl)
15309 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15310 && uses_template_parms (DECL_CONTEXT (decl)))
15311 {
15312 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15313 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15314 r = retrieve_specialization (tmpl, gen_args, 0);
15315 }
15316 else if (DECL_FUNCTION_SCOPE_P (decl)
15317 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15318 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15319 r = retrieve_local_specialization (decl);
15320 else
15321 /* The typedef is from a non-template context. */
15322 return t;
15323
15324 if (r)
15325 {
15326 r = TREE_TYPE (r);
15327 r = cp_build_qualified_type_real
15328 (r, cp_type_quals (t) | cp_type_quals (r),
15329 complain | tf_ignore_bad_quals);
15330 return r;
15331 }
15332 else
15333 {
15334 /* We don't have an instantiation yet, so drop the typedef. */
15335 int quals = cp_type_quals (t);
15336 t = DECL_ORIGINAL_TYPE (decl);
15337 t = cp_build_qualified_type_real (t, quals,
15338 complain | tf_ignore_bad_quals);
15339 }
15340 }
15341
15342 bool fndecl_type = (complain & tf_fndecl_type);
15343 complain &= ~tf_fndecl_type;
15344
15345 if (type
15346 && code != TYPENAME_TYPE
15347 && code != TEMPLATE_TYPE_PARM
15348 && code != TEMPLATE_PARM_INDEX
15349 && code != IDENTIFIER_NODE
15350 && code != FUNCTION_TYPE
15351 && code != METHOD_TYPE)
15352 type = tsubst (type, args, complain, in_decl);
15353 if (type == error_mark_node)
15354 return error_mark_node;
15355
15356 switch (code)
15357 {
15358 case RECORD_TYPE:
15359 case UNION_TYPE:
15360 case ENUMERAL_TYPE:
15361 return tsubst_aggr_type (t, args, complain, in_decl,
15362 /*entering_scope=*/0);
15363
15364 case ERROR_MARK:
15365 case IDENTIFIER_NODE:
15366 case VOID_TYPE:
15367 case REAL_TYPE:
15368 case COMPLEX_TYPE:
15369 case VECTOR_TYPE:
15370 case BOOLEAN_TYPE:
15371 case NULLPTR_TYPE:
15372 case LANG_TYPE:
15373 return t;
15374
15375 case INTEGER_TYPE:
15376 if (t == integer_type_node)
15377 return t;
15378
15379 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15380 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15381 return t;
15382
15383 {
15384 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15385
15386 max = tsubst_expr (omax, args, complain, in_decl,
15387 /*integral_constant_expression_p=*/false);
15388
15389 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15390 needed. */
15391 if (TREE_CODE (max) == NOP_EXPR
15392 && TREE_SIDE_EFFECTS (omax)
15393 && !TREE_TYPE (max))
15394 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15395
15396 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15397 with TREE_SIDE_EFFECTS that indicates this is not an integral
15398 constant expression. */
15399 if (processing_template_decl
15400 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15401 {
15402 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15403 TREE_SIDE_EFFECTS (max) = 1;
15404 }
15405
15406 return compute_array_index_type (NULL_TREE, max, complain);
15407 }
15408
15409 case TEMPLATE_TYPE_PARM:
15410 case TEMPLATE_TEMPLATE_PARM:
15411 case BOUND_TEMPLATE_TEMPLATE_PARM:
15412 case TEMPLATE_PARM_INDEX:
15413 {
15414 int idx;
15415 int level;
15416 int levels;
15417 tree arg = NULL_TREE;
15418
15419 r = NULL_TREE;
15420
15421 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15422 template_parm_level_and_index (t, &level, &idx);
15423
15424 levels = TMPL_ARGS_DEPTH (args);
15425 if (level <= levels
15426 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15427 {
15428 arg = TMPL_ARG (args, level, idx);
15429
15430 /* See through ARGUMENT_PACK_SELECT arguments. */
15431 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15432 arg = argument_pack_select_arg (arg);
15433 }
15434
15435 if (arg == error_mark_node)
15436 return error_mark_node;
15437 else if (arg != NULL_TREE)
15438 {
15439 if (ARGUMENT_PACK_P (arg))
15440 /* If ARG is an argument pack, we don't actually want to
15441 perform a substitution here, because substitutions
15442 for argument packs are only done
15443 element-by-element. We can get to this point when
15444 substituting the type of a non-type template
15445 parameter pack, when that type actually contains
15446 template parameter packs from an outer template, e.g.,
15447
15448 template<typename... Types> struct A {
15449 template<Types... Values> struct B { };
15450 }; */
15451 return t;
15452
15453 if (code == TEMPLATE_TYPE_PARM)
15454 {
15455 int quals;
15456
15457 /* When building concept checks for the purpose of
15458 deducing placeholders, we can end up with wildcards
15459 where types are expected. Adjust this to the deduced
15460 value. */
15461 if (TREE_CODE (arg) == WILDCARD_DECL)
15462 arg = TREE_TYPE (TREE_TYPE (arg));
15463
15464 gcc_assert (TYPE_P (arg));
15465
15466 quals = cp_type_quals (arg) | cp_type_quals (t);
15467
15468 return cp_build_qualified_type_real
15469 (arg, quals, complain | tf_ignore_bad_quals);
15470 }
15471 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15472 {
15473 /* We are processing a type constructed from a
15474 template template parameter. */
15475 tree argvec = tsubst (TYPE_TI_ARGS (t),
15476 args, complain, in_decl);
15477 if (argvec == error_mark_node)
15478 return error_mark_node;
15479
15480 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15481 || TREE_CODE (arg) == TEMPLATE_DECL
15482 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15483
15484 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15485 /* Consider this code:
15486
15487 template <template <class> class Template>
15488 struct Internal {
15489 template <class Arg> using Bind = Template<Arg>;
15490 };
15491
15492 template <template <class> class Template, class Arg>
15493 using Instantiate = Template<Arg>; //#0
15494
15495 template <template <class> class Template,
15496 class Argument>
15497 using Bind =
15498 Instantiate<Internal<Template>::template Bind,
15499 Argument>; //#1
15500
15501 When #1 is parsed, the
15502 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15503 parameter `Template' in #0 matches the
15504 UNBOUND_CLASS_TEMPLATE representing the argument
15505 `Internal<Template>::template Bind'; We then want
15506 to assemble the type `Bind<Argument>' that can't
15507 be fully created right now, because
15508 `Internal<Template>' not being complete, the Bind
15509 template cannot be looked up in that context. So
15510 we need to "store" `Bind<Argument>' for later
15511 when the context of Bind becomes complete. Let's
15512 store that in a TYPENAME_TYPE. */
15513 return make_typename_type (TYPE_CONTEXT (arg),
15514 build_nt (TEMPLATE_ID_EXPR,
15515 TYPE_IDENTIFIER (arg),
15516 argvec),
15517 typename_type,
15518 complain);
15519
15520 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15521 are resolving nested-types in the signature of a
15522 member function templates. Otherwise ARG is a
15523 TEMPLATE_DECL and is the real template to be
15524 instantiated. */
15525 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15526 arg = TYPE_NAME (arg);
15527
15528 r = lookup_template_class (arg,
15529 argvec, in_decl,
15530 DECL_CONTEXT (arg),
15531 /*entering_scope=*/0,
15532 complain);
15533 return cp_build_qualified_type_real
15534 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15535 }
15536 else if (code == TEMPLATE_TEMPLATE_PARM)
15537 return arg;
15538 else
15539 /* TEMPLATE_PARM_INDEX. */
15540 return convert_from_reference (unshare_expr (arg));
15541 }
15542
15543 if (level == 1)
15544 /* This can happen during the attempted tsubst'ing in
15545 unify. This means that we don't yet have any information
15546 about the template parameter in question. */
15547 return t;
15548
15549 /* Early in template argument deduction substitution, we don't
15550 want to reduce the level of 'auto', or it will be confused
15551 with a normal template parm in subsequent deduction.
15552 Similarly, don't reduce the level of template parameters to
15553 avoid mismatches when deducing their types. */
15554 if (complain & tf_partial)
15555 return t;
15556
15557 /* If we get here, we must have been looking at a parm for a
15558 more deeply nested template. Make a new version of this
15559 template parameter, but with a lower level. */
15560 switch (code)
15561 {
15562 case TEMPLATE_TYPE_PARM:
15563 case TEMPLATE_TEMPLATE_PARM:
15564 case BOUND_TEMPLATE_TEMPLATE_PARM:
15565 if (cp_type_quals (t))
15566 {
15567 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15568 r = cp_build_qualified_type_real
15569 (r, cp_type_quals (t),
15570 complain | (code == TEMPLATE_TYPE_PARM
15571 ? tf_ignore_bad_quals : 0));
15572 }
15573 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15574 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15575 && (r = (TEMPLATE_PARM_DESCENDANTS
15576 (TEMPLATE_TYPE_PARM_INDEX (t))))
15577 && (r = TREE_TYPE (r))
15578 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15579 /* Break infinite recursion when substituting the constraints
15580 of a constrained placeholder. */;
15581 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15582 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15583 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15584 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15585 r = TEMPLATE_PARM_DESCENDANTS (arg))
15586 && (TEMPLATE_PARM_LEVEL (r)
15587 == TEMPLATE_PARM_LEVEL (arg) - levels))
15588 /* Cache the simple case of lowering a type parameter. */
15589 r = TREE_TYPE (r);
15590 else
15591 {
15592 r = copy_type (t);
15593 TEMPLATE_TYPE_PARM_INDEX (r)
15594 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15595 r, levels, args, complain);
15596 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15597 TYPE_MAIN_VARIANT (r) = r;
15598 TYPE_POINTER_TO (r) = NULL_TREE;
15599 TYPE_REFERENCE_TO (r) = NULL_TREE;
15600
15601 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15602 {
15603 /* Propagate constraints on placeholders since they are
15604 only instantiated during satisfaction. */
15605 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15606 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15607 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15608 {
15609 pl = tsubst_copy (pl, args, complain, in_decl);
15610 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15611 }
15612 }
15613
15614 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15615 /* We have reduced the level of the template
15616 template parameter, but not the levels of its
15617 template parameters, so canonical_type_parameter
15618 will not be able to find the canonical template
15619 template parameter for this level. Thus, we
15620 require structural equality checking to compare
15621 TEMPLATE_TEMPLATE_PARMs. */
15622 SET_TYPE_STRUCTURAL_EQUALITY (r);
15623 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15624 SET_TYPE_STRUCTURAL_EQUALITY (r);
15625 else
15626 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15627
15628 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15629 {
15630 tree tinfo = TYPE_TEMPLATE_INFO (t);
15631 /* We might need to substitute into the types of non-type
15632 template parameters. */
15633 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15634 complain, in_decl);
15635 if (tmpl == error_mark_node)
15636 return error_mark_node;
15637 tree argvec = tsubst (TI_ARGS (tinfo), args,
15638 complain, in_decl);
15639 if (argvec == error_mark_node)
15640 return error_mark_node;
15641
15642 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15643 = build_template_info (tmpl, argvec);
15644 }
15645 }
15646 break;
15647
15648 case TEMPLATE_PARM_INDEX:
15649 /* OK, now substitute the type of the non-type parameter. We
15650 couldn't do it earlier because it might be an auto parameter,
15651 and we wouldn't need to if we had an argument. */
15652 type = tsubst (type, args, complain, in_decl);
15653 if (type == error_mark_node)
15654 return error_mark_node;
15655 r = reduce_template_parm_level (t, type, levels, args, complain);
15656 break;
15657
15658 default:
15659 gcc_unreachable ();
15660 }
15661
15662 return r;
15663 }
15664
15665 case TREE_LIST:
15666 return tsubst_tree_list (t, args, complain, in_decl);
15667
15668 case TREE_BINFO:
15669 /* We should never be tsubsting a binfo. */
15670 gcc_unreachable ();
15671
15672 case TREE_VEC:
15673 /* A vector of template arguments. */
15674 gcc_assert (!type);
15675 return tsubst_template_args (t, args, complain, in_decl);
15676
15677 case POINTER_TYPE:
15678 case REFERENCE_TYPE:
15679 {
15680 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15681 return t;
15682
15683 /* [temp.deduct]
15684
15685 Type deduction may fail for any of the following
15686 reasons:
15687
15688 -- Attempting to create a pointer to reference type.
15689 -- Attempting to create a reference to a reference type or
15690 a reference to void.
15691
15692 Core issue 106 says that creating a reference to a reference
15693 during instantiation is no longer a cause for failure. We
15694 only enforce this check in strict C++98 mode. */
15695 if ((TYPE_REF_P (type)
15696 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15697 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15698 {
15699 static location_t last_loc;
15700
15701 /* We keep track of the last time we issued this error
15702 message to avoid spewing a ton of messages during a
15703 single bad template instantiation. */
15704 if (complain & tf_error
15705 && last_loc != input_location)
15706 {
15707 if (VOID_TYPE_P (type))
15708 error ("forming reference to void");
15709 else if (code == POINTER_TYPE)
15710 error ("forming pointer to reference type %qT", type);
15711 else
15712 error ("forming reference to reference type %qT", type);
15713 last_loc = input_location;
15714 }
15715
15716 return error_mark_node;
15717 }
15718 else if (TREE_CODE (type) == FUNCTION_TYPE
15719 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15720 || type_memfn_rqual (type) != REF_QUAL_NONE))
15721 {
15722 if (complain & tf_error)
15723 {
15724 if (code == POINTER_TYPE)
15725 error ("forming pointer to qualified function type %qT",
15726 type);
15727 else
15728 error ("forming reference to qualified function type %qT",
15729 type);
15730 }
15731 return error_mark_node;
15732 }
15733 else if (code == POINTER_TYPE)
15734 {
15735 r = build_pointer_type (type);
15736 if (TREE_CODE (type) == METHOD_TYPE)
15737 r = build_ptrmemfunc_type (r);
15738 }
15739 else if (TYPE_REF_P (type))
15740 /* In C++0x, during template argument substitution, when there is an
15741 attempt to create a reference to a reference type, reference
15742 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15743
15744 "If a template-argument for a template-parameter T names a type
15745 that is a reference to a type A, an attempt to create the type
15746 'lvalue reference to cv T' creates the type 'lvalue reference to
15747 A,' while an attempt to create the type type rvalue reference to
15748 cv T' creates the type T"
15749 */
15750 r = cp_build_reference_type
15751 (TREE_TYPE (type),
15752 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15753 else
15754 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15755 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15756
15757 if (r != error_mark_node)
15758 /* Will this ever be needed for TYPE_..._TO values? */
15759 layout_type (r);
15760
15761 return r;
15762 }
15763 case OFFSET_TYPE:
15764 {
15765 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15766 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15767 {
15768 /* [temp.deduct]
15769
15770 Type deduction may fail for any of the following
15771 reasons:
15772
15773 -- Attempting to create "pointer to member of T" when T
15774 is not a class type. */
15775 if (complain & tf_error)
15776 error ("creating pointer to member of non-class type %qT", r);
15777 return error_mark_node;
15778 }
15779 if (TYPE_REF_P (type))
15780 {
15781 if (complain & tf_error)
15782 error ("creating pointer to member reference type %qT", type);
15783 return error_mark_node;
15784 }
15785 if (VOID_TYPE_P (type))
15786 {
15787 if (complain & tf_error)
15788 error ("creating pointer to member of type void");
15789 return error_mark_node;
15790 }
15791 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15792 if (TREE_CODE (type) == FUNCTION_TYPE)
15793 {
15794 /* The type of the implicit object parameter gets its
15795 cv-qualifiers from the FUNCTION_TYPE. */
15796 tree memptr;
15797 tree method_type
15798 = build_memfn_type (type, r, type_memfn_quals (type),
15799 type_memfn_rqual (type));
15800 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15801 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15802 complain);
15803 }
15804 else
15805 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15806 cp_type_quals (t),
15807 complain);
15808 }
15809 case FUNCTION_TYPE:
15810 case METHOD_TYPE:
15811 {
15812 tree fntype;
15813 tree specs;
15814 fntype = tsubst_function_type (t, args, complain, in_decl);
15815 if (fntype == error_mark_node)
15816 return error_mark_node;
15817
15818 /* Substitute the exception specification. */
15819 specs = tsubst_exception_specification (t, args, complain, in_decl,
15820 /*defer_ok*/fndecl_type);
15821 if (specs == error_mark_node)
15822 return error_mark_node;
15823 if (specs)
15824 fntype = build_exception_variant (fntype, specs);
15825 return fntype;
15826 }
15827 case ARRAY_TYPE:
15828 {
15829 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15830 if (domain == error_mark_node)
15831 return error_mark_node;
15832
15833 /* As an optimization, we avoid regenerating the array type if
15834 it will obviously be the same as T. */
15835 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15836 return t;
15837
15838 /* These checks should match the ones in create_array_type_for_decl.
15839
15840 [temp.deduct]
15841
15842 The deduction may fail for any of the following reasons:
15843
15844 -- Attempting to create an array with an element type that
15845 is void, a function type, or a reference type, or [DR337]
15846 an abstract class type. */
15847 if (VOID_TYPE_P (type)
15848 || TREE_CODE (type) == FUNCTION_TYPE
15849 || (TREE_CODE (type) == ARRAY_TYPE
15850 && TYPE_DOMAIN (type) == NULL_TREE)
15851 || TYPE_REF_P (type))
15852 {
15853 if (complain & tf_error)
15854 error ("creating array of %qT", type);
15855 return error_mark_node;
15856 }
15857
15858 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15859 return error_mark_node;
15860
15861 r = build_cplus_array_type (type, domain);
15862
15863 if (!valid_array_size_p (input_location, r, in_decl,
15864 (complain & tf_error)))
15865 return error_mark_node;
15866
15867 if (TYPE_USER_ALIGN (t))
15868 {
15869 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15870 TYPE_USER_ALIGN (r) = 1;
15871 }
15872
15873 return r;
15874 }
15875
15876 case TYPENAME_TYPE:
15877 {
15878 tree ctx = TYPE_CONTEXT (t);
15879 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15880 {
15881 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15882 if (ctx == error_mark_node
15883 || TREE_VEC_LENGTH (ctx) > 1)
15884 return error_mark_node;
15885 if (TREE_VEC_LENGTH (ctx) == 0)
15886 {
15887 if (complain & tf_error)
15888 error ("%qD is instantiated for an empty pack",
15889 TYPENAME_TYPE_FULLNAME (t));
15890 return error_mark_node;
15891 }
15892 ctx = TREE_VEC_ELT (ctx, 0);
15893 }
15894 else
15895 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15896 /*entering_scope=*/1);
15897 if (ctx == error_mark_node)
15898 return error_mark_node;
15899
15900 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15901 complain, in_decl);
15902 if (f == error_mark_node)
15903 return error_mark_node;
15904
15905 if (!MAYBE_CLASS_TYPE_P (ctx))
15906 {
15907 if (complain & tf_error)
15908 error ("%qT is not a class, struct, or union type", ctx);
15909 return error_mark_node;
15910 }
15911 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15912 {
15913 /* Normally, make_typename_type does not require that the CTX
15914 have complete type in order to allow things like:
15915
15916 template <class T> struct S { typename S<T>::X Y; };
15917
15918 But, such constructs have already been resolved by this
15919 point, so here CTX really should have complete type, unless
15920 it's a partial instantiation. */
15921 ctx = complete_type (ctx);
15922 if (!COMPLETE_TYPE_P (ctx))
15923 {
15924 if (complain & tf_error)
15925 cxx_incomplete_type_error (NULL_TREE, ctx);
15926 return error_mark_node;
15927 }
15928 }
15929
15930 f = make_typename_type (ctx, f, typename_type,
15931 complain | tf_keep_type_decl);
15932 if (f == error_mark_node)
15933 return f;
15934 if (TREE_CODE (f) == TYPE_DECL)
15935 {
15936 complain |= tf_ignore_bad_quals;
15937 f = TREE_TYPE (f);
15938 }
15939
15940 if (TREE_CODE (f) != TYPENAME_TYPE)
15941 {
15942 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15943 {
15944 if (complain & tf_error)
15945 error ("%qT resolves to %qT, which is not an enumeration type",
15946 t, f);
15947 else
15948 return error_mark_node;
15949 }
15950 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15951 {
15952 if (complain & tf_error)
15953 error ("%qT resolves to %qT, which is not a class type",
15954 t, f);
15955 else
15956 return error_mark_node;
15957 }
15958 }
15959
15960 return cp_build_qualified_type_real
15961 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15962 }
15963
15964 case UNBOUND_CLASS_TEMPLATE:
15965 {
15966 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15967 in_decl, /*entering_scope=*/1);
15968 tree name = TYPE_IDENTIFIER (t);
15969 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15970
15971 if (ctx == error_mark_node || name == error_mark_node)
15972 return error_mark_node;
15973
15974 if (parm_list)
15975 parm_list = tsubst_template_parms (parm_list, args, complain);
15976 return make_unbound_class_template (ctx, name, parm_list, complain);
15977 }
15978
15979 case TYPEOF_TYPE:
15980 {
15981 tree type;
15982
15983 ++cp_unevaluated_operand;
15984 ++c_inhibit_evaluation_warnings;
15985
15986 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15987 complain, in_decl,
15988 /*integral_constant_expression_p=*/false);
15989
15990 --cp_unevaluated_operand;
15991 --c_inhibit_evaluation_warnings;
15992
15993 type = finish_typeof (type);
15994 return cp_build_qualified_type_real (type,
15995 cp_type_quals (t)
15996 | cp_type_quals (type),
15997 complain);
15998 }
15999
16000 case DECLTYPE_TYPE:
16001 {
16002 tree type;
16003
16004 ++cp_unevaluated_operand;
16005 ++c_inhibit_evaluation_warnings;
16006
16007 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16008 complain|tf_decltype, in_decl,
16009 /*function_p*/false,
16010 /*integral_constant_expression*/false);
16011
16012 --cp_unevaluated_operand;
16013 --c_inhibit_evaluation_warnings;
16014
16015 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16016 type = lambda_capture_field_type (type,
16017 false /*explicit_init*/,
16018 DECLTYPE_FOR_REF_CAPTURE (t));
16019 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16020 type = lambda_proxy_type (type);
16021 else
16022 {
16023 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16024 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16025 && EXPR_P (type))
16026 /* In a template ~id could be either a complement expression
16027 or an unqualified-id naming a destructor; if instantiating
16028 it produces an expression, it's not an id-expression or
16029 member access. */
16030 id = false;
16031 type = finish_decltype_type (type, id, complain);
16032 }
16033 return cp_build_qualified_type_real (type,
16034 cp_type_quals (t)
16035 | cp_type_quals (type),
16036 complain | tf_ignore_bad_quals);
16037 }
16038
16039 case UNDERLYING_TYPE:
16040 {
16041 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16042 complain, in_decl);
16043 return finish_underlying_type (type);
16044 }
16045
16046 case TYPE_ARGUMENT_PACK:
16047 case NONTYPE_ARGUMENT_PACK:
16048 {
16049 tree r;
16050
16051 if (code == NONTYPE_ARGUMENT_PACK)
16052 r = make_node (code);
16053 else
16054 r = cxx_make_type (code);
16055
16056 tree pack_args = ARGUMENT_PACK_ARGS (t);
16057 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
16058 SET_ARGUMENT_PACK_ARGS (r, pack_args);
16059
16060 return r;
16061 }
16062
16063 case VOID_CST:
16064 case INTEGER_CST:
16065 case REAL_CST:
16066 case STRING_CST:
16067 case PLUS_EXPR:
16068 case MINUS_EXPR:
16069 case NEGATE_EXPR:
16070 case NOP_EXPR:
16071 case INDIRECT_REF:
16072 case ADDR_EXPR:
16073 case CALL_EXPR:
16074 case ARRAY_REF:
16075 case SCOPE_REF:
16076 /* We should use one of the expression tsubsts for these codes. */
16077 gcc_unreachable ();
16078
16079 default:
16080 sorry ("use of %qs in template", get_tree_code_name (code));
16081 return error_mark_node;
16082 }
16083 }
16084
16085 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16086 expression on the left-hand side of the "." or "->" operator. We
16087 only do the lookup if we had a dependent BASELINK. Otherwise we
16088 adjust it onto the instantiated heirarchy. */
16089
16090 static tree
16091 tsubst_baselink (tree baselink, tree object_type,
16092 tree args, tsubst_flags_t complain, tree in_decl)
16093 {
16094 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16095 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16096 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16097
16098 tree optype = BASELINK_OPTYPE (baselink);
16099 optype = tsubst (optype, args, complain, in_decl);
16100
16101 tree template_args = NULL_TREE;
16102 bool template_id_p = false;
16103 tree fns = BASELINK_FUNCTIONS (baselink);
16104 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16105 {
16106 template_id_p = true;
16107 template_args = TREE_OPERAND (fns, 1);
16108 fns = TREE_OPERAND (fns, 0);
16109 if (template_args)
16110 template_args = tsubst_template_args (template_args, args,
16111 complain, in_decl);
16112 }
16113
16114 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16115 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16116 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16117
16118 if (dependent_p)
16119 {
16120 tree name = OVL_NAME (fns);
16121 if (IDENTIFIER_CONV_OP_P (name))
16122 name = make_conv_op_name (optype);
16123
16124 if (name == complete_dtor_identifier)
16125 /* Treat as-if non-dependent below. */
16126 dependent_p = false;
16127
16128 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16129 complain);
16130 if (!baselink)
16131 {
16132 if ((complain & tf_error)
16133 && constructor_name_p (name, qualifying_scope))
16134 error ("cannot call constructor %<%T::%D%> directly",
16135 qualifying_scope, name);
16136 return error_mark_node;
16137 }
16138
16139 if (BASELINK_P (baselink))
16140 fns = BASELINK_FUNCTIONS (baselink);
16141 }
16142 else
16143 /* We're going to overwrite pieces below, make a duplicate. */
16144 baselink = copy_node (baselink);
16145
16146 /* If lookup found a single function, mark it as used at this point.
16147 (If lookup found multiple functions the one selected later by
16148 overload resolution will be marked as used at that point.) */
16149 if (!template_id_p && !really_overloaded_fn (fns))
16150 {
16151 tree fn = OVL_FIRST (fns);
16152 bool ok = mark_used (fn, complain);
16153 if (!ok && !(complain & tf_error))
16154 return error_mark_node;
16155 if (ok && BASELINK_P (baselink))
16156 /* We might have instantiated an auto function. */
16157 TREE_TYPE (baselink) = TREE_TYPE (fn);
16158 }
16159
16160 if (BASELINK_P (baselink))
16161 {
16162 /* Add back the template arguments, if present. */
16163 if (template_id_p)
16164 BASELINK_FUNCTIONS (baselink)
16165 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16166
16167 /* Update the conversion operator type. */
16168 BASELINK_OPTYPE (baselink) = optype;
16169 }
16170
16171 if (!object_type)
16172 object_type = current_class_type;
16173
16174 if (qualified_p || !dependent_p)
16175 {
16176 baselink = adjust_result_of_qualified_name_lookup (baselink,
16177 qualifying_scope,
16178 object_type);
16179 if (!qualified_p)
16180 /* We need to call adjust_result_of_qualified_name_lookup in case the
16181 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16182 so that we still get virtual function binding. */
16183 BASELINK_QUALIFIED_P (baselink) = false;
16184 }
16185
16186 return baselink;
16187 }
16188
16189 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16190 true if the qualified-id will be a postfix-expression in-and-of
16191 itself; false if more of the postfix-expression follows the
16192 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16193 of "&". */
16194
16195 static tree
16196 tsubst_qualified_id (tree qualified_id, tree args,
16197 tsubst_flags_t complain, tree in_decl,
16198 bool done, bool address_p)
16199 {
16200 tree expr;
16201 tree scope;
16202 tree name;
16203 bool is_template;
16204 tree template_args;
16205 location_t loc = UNKNOWN_LOCATION;
16206
16207 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16208
16209 /* Figure out what name to look up. */
16210 name = TREE_OPERAND (qualified_id, 1);
16211 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16212 {
16213 is_template = true;
16214 loc = EXPR_LOCATION (name);
16215 template_args = TREE_OPERAND (name, 1);
16216 if (template_args)
16217 template_args = tsubst_template_args (template_args, args,
16218 complain, in_decl);
16219 if (template_args == error_mark_node)
16220 return error_mark_node;
16221 name = TREE_OPERAND (name, 0);
16222 }
16223 else
16224 {
16225 is_template = false;
16226 template_args = NULL_TREE;
16227 }
16228
16229 /* Substitute into the qualifying scope. When there are no ARGS, we
16230 are just trying to simplify a non-dependent expression. In that
16231 case the qualifying scope may be dependent, and, in any case,
16232 substituting will not help. */
16233 scope = TREE_OPERAND (qualified_id, 0);
16234 if (args)
16235 {
16236 scope = tsubst (scope, args, complain, in_decl);
16237 expr = tsubst_copy (name, args, complain, in_decl);
16238 }
16239 else
16240 expr = name;
16241
16242 if (dependent_scope_p (scope))
16243 {
16244 if (is_template)
16245 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16246 tree r = build_qualified_name (NULL_TREE, scope, expr,
16247 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16248 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16249 return r;
16250 }
16251
16252 if (!BASELINK_P (name) && !DECL_P (expr))
16253 {
16254 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16255 {
16256 /* A BIT_NOT_EXPR is used to represent a destructor. */
16257 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16258 {
16259 error ("qualifying type %qT does not match destructor name ~%qT",
16260 scope, TREE_OPERAND (expr, 0));
16261 expr = error_mark_node;
16262 }
16263 else
16264 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16265 LOOK_want::NORMAL, false);
16266 }
16267 else
16268 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16269 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16270 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16271 {
16272 if (complain & tf_error)
16273 {
16274 error ("dependent-name %qE is parsed as a non-type, but "
16275 "instantiation yields a type", qualified_id);
16276 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16277 }
16278 return error_mark_node;
16279 }
16280 }
16281
16282 if (DECL_P (expr))
16283 {
16284 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16285 scope, complain))
16286 return error_mark_node;
16287 /* Remember that there was a reference to this entity. */
16288 if (!mark_used (expr, complain) && !(complain & tf_error))
16289 return error_mark_node;
16290 }
16291
16292 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16293 {
16294 if (complain & tf_error)
16295 qualified_name_lookup_error (scope,
16296 TREE_OPERAND (qualified_id, 1),
16297 expr, input_location);
16298 return error_mark_node;
16299 }
16300
16301 if (is_template)
16302 {
16303 /* We may be repeating a check already done during parsing, but
16304 if it was well-formed and passed then, it will pass again
16305 now, and if it didn't, we wouldn't have got here. The case
16306 we want to catch is when we couldn't tell then, and can now,
16307 namely when templ prior to substitution was an
16308 identifier. */
16309 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16310 return error_mark_node;
16311
16312 if (variable_template_p (expr))
16313 expr = lookup_and_finish_template_variable (expr, template_args,
16314 complain);
16315 else
16316 expr = lookup_template_function (expr, template_args);
16317 }
16318
16319 if (expr == error_mark_node && complain & tf_error)
16320 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16321 expr, input_location);
16322 else if (TYPE_P (scope))
16323 {
16324 expr = (adjust_result_of_qualified_name_lookup
16325 (expr, scope, current_nonlambda_class_type ()));
16326 expr = (finish_qualified_id_expr
16327 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16328 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16329 /*template_arg_p=*/false, complain));
16330 }
16331
16332 /* Expressions do not generally have reference type. */
16333 if (TREE_CODE (expr) != SCOPE_REF
16334 /* However, if we're about to form a pointer-to-member, we just
16335 want the referenced member referenced. */
16336 && TREE_CODE (expr) != OFFSET_REF)
16337 expr = convert_from_reference (expr);
16338
16339 if (REF_PARENTHESIZED_P (qualified_id))
16340 expr = force_paren_expr (expr);
16341
16342 return expr;
16343 }
16344
16345 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16346 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16347 for tsubst. */
16348
16349 static tree
16350 tsubst_init (tree init, tree decl, tree args,
16351 tsubst_flags_t complain, tree in_decl)
16352 {
16353 if (!init)
16354 return NULL_TREE;
16355
16356 init = tsubst_expr (init, args, complain, in_decl, false);
16357
16358 tree type = TREE_TYPE (decl);
16359
16360 if (!init && type != error_mark_node)
16361 {
16362 if (tree auto_node = type_uses_auto (type))
16363 {
16364 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16365 {
16366 if (complain & tf_error)
16367 error ("initializer for %q#D expands to an empty list "
16368 "of expressions", decl);
16369 return error_mark_node;
16370 }
16371 }
16372 else if (!dependent_type_p (type))
16373 {
16374 /* If we had an initializer but it
16375 instantiated to nothing,
16376 value-initialize the object. This will
16377 only occur when the initializer was a
16378 pack expansion where the parameter packs
16379 used in that expansion were of length
16380 zero. */
16381 init = build_value_init (type, complain);
16382 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16383 init = get_target_expr_sfinae (init, complain);
16384 if (TREE_CODE (init) == TARGET_EXPR)
16385 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16386 }
16387 }
16388
16389 return init;
16390 }
16391
16392 /* If T is a reference to a dependent member of the current instantiation C and
16393 we are trying to refer to that member in a partial instantiation of C,
16394 return a SCOPE_REF; otherwise, return NULL_TREE.
16395
16396 This can happen when forming a C++17 deduction guide, as in PR96199. */
16397
16398 static tree
16399 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16400 tree in_decl)
16401 {
16402 if (cxx_dialect < cxx17)
16403 return NULL_TREE;
16404
16405 tree ctx = context_for_name_lookup (t);
16406 if (!CLASS_TYPE_P (ctx))
16407 return NULL_TREE;
16408
16409 ctx = tsubst (ctx, args, complain, in_decl);
16410 if (dependent_scope_p (ctx))
16411 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16412 /*template_p=*/false);
16413
16414 return NULL_TREE;
16415 }
16416
16417 /* Like tsubst, but deals with expressions. This function just replaces
16418 template parms; to finish processing the resultant expression, use
16419 tsubst_copy_and_build or tsubst_expr. */
16420
16421 static tree
16422 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16423 {
16424 enum tree_code code;
16425 tree r;
16426
16427 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16428 return t;
16429
16430 code = TREE_CODE (t);
16431
16432 switch (code)
16433 {
16434 case PARM_DECL:
16435 r = retrieve_local_specialization (t);
16436
16437 if (r == NULL_TREE)
16438 {
16439 /* We get here for a use of 'this' in an NSDMI. */
16440 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16441 return current_class_ptr;
16442
16443 /* This can happen for a parameter name used later in a function
16444 declaration (such as in a late-specified return type). Just
16445 make a dummy decl, since it's only used for its type. */
16446 gcc_assert (cp_unevaluated_operand != 0);
16447 r = tsubst_decl (t, args, complain);
16448 /* Give it the template pattern as its context; its true context
16449 hasn't been instantiated yet and this is good enough for
16450 mangling. */
16451 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16452 }
16453
16454 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16455 r = argument_pack_select_arg (r);
16456 if (!mark_used (r, complain) && !(complain & tf_error))
16457 return error_mark_node;
16458 return r;
16459
16460 case CONST_DECL:
16461 {
16462 tree enum_type;
16463 tree v;
16464
16465 if (DECL_TEMPLATE_PARM_P (t))
16466 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16467 /* There is no need to substitute into namespace-scope
16468 enumerators. */
16469 if (DECL_NAMESPACE_SCOPE_P (t))
16470 return t;
16471 /* If ARGS is NULL, then T is known to be non-dependent. */
16472 if (args == NULL_TREE)
16473 return scalar_constant_value (t);
16474
16475 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16476 return ref;
16477
16478 /* Unfortunately, we cannot just call lookup_name here.
16479 Consider:
16480
16481 template <int I> int f() {
16482 enum E { a = I };
16483 struct S { void g() { E e = a; } };
16484 };
16485
16486 When we instantiate f<7>::S::g(), say, lookup_name is not
16487 clever enough to find f<7>::a. */
16488 enum_type
16489 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16490 /*entering_scope=*/0);
16491
16492 for (v = TYPE_VALUES (enum_type);
16493 v != NULL_TREE;
16494 v = TREE_CHAIN (v))
16495 if (TREE_PURPOSE (v) == DECL_NAME (t))
16496 return TREE_VALUE (v);
16497
16498 /* We didn't find the name. That should never happen; if
16499 name-lookup found it during preliminary parsing, we
16500 should find it again here during instantiation. */
16501 gcc_unreachable ();
16502 }
16503 return t;
16504
16505 case FIELD_DECL:
16506 if (DECL_CONTEXT (t))
16507 {
16508 tree ctx;
16509
16510 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16511 /*entering_scope=*/1);
16512 if (ctx != DECL_CONTEXT (t))
16513 {
16514 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16515 if (!r)
16516 {
16517 if (complain & tf_error)
16518 error ("using invalid field %qD", t);
16519 return error_mark_node;
16520 }
16521 return r;
16522 }
16523 }
16524
16525 return t;
16526
16527 case VAR_DECL:
16528 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16529 return ref;
16530 gcc_fallthrough();
16531 case FUNCTION_DECL:
16532 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16533 r = tsubst (t, args, complain, in_decl);
16534 else if (local_variable_p (t)
16535 && uses_template_parms (DECL_CONTEXT (t)))
16536 {
16537 r = retrieve_local_specialization (t);
16538 if (r == NULL_TREE)
16539 {
16540 /* First try name lookup to find the instantiation. */
16541 r = lookup_name (DECL_NAME (t));
16542 if (r)
16543 {
16544 if (!VAR_P (r))
16545 {
16546 /* During error-recovery we may find a non-variable,
16547 even an OVERLOAD: just bail out and avoid ICEs and
16548 duplicate diagnostics (c++/62207). */
16549 gcc_assert (seen_error ());
16550 return error_mark_node;
16551 }
16552 if (!is_capture_proxy (r))
16553 {
16554 /* Make sure the one we found is the one we want. */
16555 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16556 if (ctx != DECL_CONTEXT (r))
16557 r = NULL_TREE;
16558 }
16559 }
16560
16561 if (r)
16562 /* OK */;
16563 else
16564 {
16565 /* This can happen for a variable used in a
16566 late-specified return type of a local lambda, or for a
16567 local static or constant. Building a new VAR_DECL
16568 should be OK in all those cases. */
16569 r = tsubst_decl (t, args, complain);
16570 if (local_specializations)
16571 /* Avoid infinite recursion (79640). */
16572 register_local_specialization (r, t);
16573 if (decl_maybe_constant_var_p (r))
16574 {
16575 /* We can't call cp_finish_decl, so handle the
16576 initializer by hand. */
16577 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16578 complain, in_decl);
16579 if (!processing_template_decl)
16580 init = maybe_constant_init (init);
16581 if (processing_template_decl
16582 ? potential_constant_expression (init)
16583 : reduced_constant_expression_p (init))
16584 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16585 = TREE_CONSTANT (r) = true;
16586 DECL_INITIAL (r) = init;
16587 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16588 TREE_TYPE (r)
16589 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16590 complain, adc_variable_type);
16591 }
16592 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16593 || decl_constant_var_p (r)
16594 || seen_error ());
16595 if (!processing_template_decl
16596 && !TREE_STATIC (r))
16597 r = process_outer_var_ref (r, complain);
16598 }
16599 /* Remember this for subsequent uses. */
16600 if (local_specializations)
16601 register_local_specialization (r, t);
16602 }
16603 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16604 r = argument_pack_select_arg (r);
16605 }
16606 else
16607 r = t;
16608 if (!mark_used (r, complain))
16609 return error_mark_node;
16610 return r;
16611
16612 case NAMESPACE_DECL:
16613 return t;
16614
16615 case OVERLOAD:
16616 return t;
16617
16618 case BASELINK:
16619 return tsubst_baselink (t, current_nonlambda_class_type (),
16620 args, complain, in_decl);
16621
16622 case TEMPLATE_DECL:
16623 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16624 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16625 args, complain, in_decl);
16626 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16627 return tsubst (t, args, complain, in_decl);
16628 else if (DECL_CLASS_SCOPE_P (t)
16629 && uses_template_parms (DECL_CONTEXT (t)))
16630 {
16631 /* Template template argument like the following example need
16632 special treatment:
16633
16634 template <template <class> class TT> struct C {};
16635 template <class T> struct D {
16636 template <class U> struct E {};
16637 C<E> c; // #1
16638 };
16639 D<int> d; // #2
16640
16641 We are processing the template argument `E' in #1 for
16642 the template instantiation #2. Originally, `E' is a
16643 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16644 have to substitute this with one having context `D<int>'. */
16645
16646 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16647 if (dependent_scope_p (context))
16648 {
16649 /* When rewriting a constructor into a deduction guide, a
16650 non-dependent name can become dependent, so memtmpl<args>
16651 becomes context::template memtmpl<args>. */
16652 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16653 return build_qualified_name (type, context, DECL_NAME (t),
16654 /*template*/true);
16655 }
16656 return lookup_field (context, DECL_NAME(t), 0, false);
16657 }
16658 else
16659 /* Ordinary template template argument. */
16660 return t;
16661
16662 case NON_LVALUE_EXPR:
16663 case VIEW_CONVERT_EXPR:
16664 {
16665 /* Handle location wrappers by substituting the wrapped node
16666 first, *then* reusing the resulting type. Doing the type
16667 first ensures that we handle template parameters and
16668 parameter pack expansions. */
16669 if (location_wrapper_p (t))
16670 {
16671 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16672 complain, in_decl);
16673 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16674 }
16675 tree op = TREE_OPERAND (t, 0);
16676 if (code == VIEW_CONVERT_EXPR
16677 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16678 {
16679 /* Wrapper to make a C++20 template parameter object const. */
16680 op = tsubst_copy (op, args, complain, in_decl);
16681 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16682 {
16683 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16684 return build1 (code, type, op);
16685 }
16686 else
16687 {
16688 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16689 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16690 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16691 return op;
16692 }
16693 }
16694 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16695 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16696 {
16697 op = tsubst_copy (op, args, complain, in_decl);
16698 op = build1 (code, TREE_TYPE (op), op);
16699 REF_PARENTHESIZED_P (op) = true;
16700 return op;
16701 }
16702 /* We shouldn't see any other uses of these in templates. */
16703 gcc_unreachable ();
16704 }
16705
16706 case CAST_EXPR:
16707 case REINTERPRET_CAST_EXPR:
16708 case CONST_CAST_EXPR:
16709 case STATIC_CAST_EXPR:
16710 case DYNAMIC_CAST_EXPR:
16711 case IMPLICIT_CONV_EXPR:
16712 case CONVERT_EXPR:
16713 case NOP_EXPR:
16714 {
16715 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16716 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16717 return build1 (code, type, op0);
16718 }
16719
16720 case SIZEOF_EXPR:
16721 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16722 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16723 {
16724 tree expanded, op = TREE_OPERAND (t, 0);
16725 int len = 0;
16726
16727 if (SIZEOF_EXPR_TYPE_P (t))
16728 op = TREE_TYPE (op);
16729
16730 ++cp_unevaluated_operand;
16731 ++c_inhibit_evaluation_warnings;
16732 /* We only want to compute the number of arguments. */
16733 if (PACK_EXPANSION_P (op))
16734 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16735 else
16736 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16737 args, complain, in_decl);
16738 --cp_unevaluated_operand;
16739 --c_inhibit_evaluation_warnings;
16740
16741 if (TREE_CODE (expanded) == TREE_VEC)
16742 {
16743 len = TREE_VEC_LENGTH (expanded);
16744 /* Set TREE_USED for the benefit of -Wunused. */
16745 for (int i = 0; i < len; i++)
16746 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16747 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16748 }
16749
16750 if (expanded == error_mark_node)
16751 return error_mark_node;
16752 else if (PACK_EXPANSION_P (expanded)
16753 || (TREE_CODE (expanded) == TREE_VEC
16754 && pack_expansion_args_count (expanded)))
16755
16756 {
16757 if (PACK_EXPANSION_P (expanded))
16758 /* OK. */;
16759 else if (TREE_VEC_LENGTH (expanded) == 1)
16760 expanded = TREE_VEC_ELT (expanded, 0);
16761 else
16762 expanded = make_argument_pack (expanded);
16763
16764 if (TYPE_P (expanded))
16765 return cxx_sizeof_or_alignof_type (input_location,
16766 expanded, SIZEOF_EXPR,
16767 false,
16768 complain & tf_error);
16769 else
16770 return cxx_sizeof_or_alignof_expr (input_location,
16771 expanded, SIZEOF_EXPR,
16772 complain & tf_error);
16773 }
16774 else
16775 return build_int_cst (size_type_node, len);
16776 }
16777 if (SIZEOF_EXPR_TYPE_P (t))
16778 {
16779 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16780 args, complain, in_decl);
16781 r = build1 (NOP_EXPR, r, error_mark_node);
16782 r = build1 (SIZEOF_EXPR,
16783 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16784 SIZEOF_EXPR_TYPE_P (r) = 1;
16785 return r;
16786 }
16787 /* Fall through */
16788
16789 case INDIRECT_REF:
16790 case NEGATE_EXPR:
16791 case TRUTH_NOT_EXPR:
16792 case BIT_NOT_EXPR:
16793 case ADDR_EXPR:
16794 case UNARY_PLUS_EXPR: /* Unary + */
16795 case ALIGNOF_EXPR:
16796 case AT_ENCODE_EXPR:
16797 case ARROW_EXPR:
16798 case THROW_EXPR:
16799 case TYPEID_EXPR:
16800 case REALPART_EXPR:
16801 case IMAGPART_EXPR:
16802 case PAREN_EXPR:
16803 {
16804 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16805 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16806 r = build1 (code, type, op0);
16807 if (code == ALIGNOF_EXPR)
16808 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16809 return r;
16810 }
16811
16812 case COMPONENT_REF:
16813 {
16814 tree object;
16815 tree name;
16816
16817 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16818 name = TREE_OPERAND (t, 1);
16819 if (TREE_CODE (name) == BIT_NOT_EXPR)
16820 {
16821 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16822 complain, in_decl);
16823 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16824 }
16825 else if (TREE_CODE (name) == SCOPE_REF
16826 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16827 {
16828 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16829 complain, in_decl);
16830 name = TREE_OPERAND (name, 1);
16831 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16832 complain, in_decl);
16833 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16834 name = build_qualified_name (/*type=*/NULL_TREE,
16835 base, name,
16836 /*template_p=*/false);
16837 }
16838 else if (BASELINK_P (name))
16839 name = tsubst_baselink (name,
16840 non_reference (TREE_TYPE (object)),
16841 args, complain,
16842 in_decl);
16843 else
16844 name = tsubst_copy (name, args, complain, in_decl);
16845 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16846 }
16847
16848 case PLUS_EXPR:
16849 case MINUS_EXPR:
16850 case MULT_EXPR:
16851 case TRUNC_DIV_EXPR:
16852 case CEIL_DIV_EXPR:
16853 case FLOOR_DIV_EXPR:
16854 case ROUND_DIV_EXPR:
16855 case EXACT_DIV_EXPR:
16856 case BIT_AND_EXPR:
16857 case BIT_IOR_EXPR:
16858 case BIT_XOR_EXPR:
16859 case TRUNC_MOD_EXPR:
16860 case FLOOR_MOD_EXPR:
16861 case TRUTH_ANDIF_EXPR:
16862 case TRUTH_ORIF_EXPR:
16863 case TRUTH_AND_EXPR:
16864 case TRUTH_OR_EXPR:
16865 case RSHIFT_EXPR:
16866 case LSHIFT_EXPR:
16867 case EQ_EXPR:
16868 case NE_EXPR:
16869 case MAX_EXPR:
16870 case MIN_EXPR:
16871 case LE_EXPR:
16872 case GE_EXPR:
16873 case LT_EXPR:
16874 case GT_EXPR:
16875 case COMPOUND_EXPR:
16876 case DOTSTAR_EXPR:
16877 case MEMBER_REF:
16878 case PREDECREMENT_EXPR:
16879 case PREINCREMENT_EXPR:
16880 case POSTDECREMENT_EXPR:
16881 case POSTINCREMENT_EXPR:
16882 {
16883 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16884 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16885 return build_nt (code, op0, op1);
16886 }
16887
16888 case SCOPE_REF:
16889 {
16890 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16891 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16892 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16893 QUALIFIED_NAME_IS_TEMPLATE (t));
16894 }
16895
16896 case ARRAY_REF:
16897 {
16898 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16899 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16900 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16901 }
16902
16903 case CALL_EXPR:
16904 {
16905 int n = VL_EXP_OPERAND_LENGTH (t);
16906 tree result = build_vl_exp (CALL_EXPR, n);
16907 int i;
16908 for (i = 0; i < n; i++)
16909 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16910 complain, in_decl);
16911 return result;
16912 }
16913
16914 case COND_EXPR:
16915 case MODOP_EXPR:
16916 case PSEUDO_DTOR_EXPR:
16917 case VEC_PERM_EXPR:
16918 {
16919 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16920 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16921 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16922 r = build_nt (code, op0, op1, op2);
16923 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16924 return r;
16925 }
16926
16927 case NEW_EXPR:
16928 {
16929 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16930 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16931 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16932 r = build_nt (code, op0, op1, op2);
16933 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16934 return r;
16935 }
16936
16937 case DELETE_EXPR:
16938 {
16939 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16940 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16941 r = build_nt (code, op0, op1);
16942 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16943 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16944 return r;
16945 }
16946
16947 case TEMPLATE_ID_EXPR:
16948 {
16949 /* Substituted template arguments */
16950 tree fn = TREE_OPERAND (t, 0);
16951 tree targs = TREE_OPERAND (t, 1);
16952
16953 fn = tsubst_copy (fn, args, complain, in_decl);
16954 if (targs)
16955 targs = tsubst_template_args (targs, args, complain, in_decl);
16956
16957 return lookup_template_function (fn, targs);
16958 }
16959
16960 case TREE_LIST:
16961 {
16962 tree purpose, value, chain;
16963
16964 if (t == void_list_node)
16965 return t;
16966
16967 purpose = TREE_PURPOSE (t);
16968 if (purpose)
16969 purpose = tsubst_copy (purpose, args, complain, in_decl);
16970 value = TREE_VALUE (t);
16971 if (value)
16972 value = tsubst_copy (value, args, complain, in_decl);
16973 chain = TREE_CHAIN (t);
16974 if (chain && chain != void_type_node)
16975 chain = tsubst_copy (chain, args, complain, in_decl);
16976 if (purpose == TREE_PURPOSE (t)
16977 && value == TREE_VALUE (t)
16978 && chain == TREE_CHAIN (t))
16979 return t;
16980 return tree_cons (purpose, value, chain);
16981 }
16982
16983 case RECORD_TYPE:
16984 case UNION_TYPE:
16985 case ENUMERAL_TYPE:
16986 case INTEGER_TYPE:
16987 case TEMPLATE_TYPE_PARM:
16988 case TEMPLATE_TEMPLATE_PARM:
16989 case BOUND_TEMPLATE_TEMPLATE_PARM:
16990 case TEMPLATE_PARM_INDEX:
16991 case POINTER_TYPE:
16992 case REFERENCE_TYPE:
16993 case OFFSET_TYPE:
16994 case FUNCTION_TYPE:
16995 case METHOD_TYPE:
16996 case ARRAY_TYPE:
16997 case TYPENAME_TYPE:
16998 case UNBOUND_CLASS_TEMPLATE:
16999 case TYPEOF_TYPE:
17000 case DECLTYPE_TYPE:
17001 case TYPE_DECL:
17002 return tsubst (t, args, complain, in_decl);
17003
17004 case USING_DECL:
17005 t = DECL_NAME (t);
17006 /* Fall through. */
17007 case IDENTIFIER_NODE:
17008 if (IDENTIFIER_CONV_OP_P (t))
17009 {
17010 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17011 return make_conv_op_name (new_type);
17012 }
17013 else
17014 return t;
17015
17016 case CONSTRUCTOR:
17017 /* This is handled by tsubst_copy_and_build. */
17018 gcc_unreachable ();
17019
17020 case VA_ARG_EXPR:
17021 {
17022 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17023 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17024 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17025 }
17026
17027 case CLEANUP_POINT_EXPR:
17028 /* We shouldn't have built any of these during initial template
17029 generation. Instead, they should be built during instantiation
17030 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17031 gcc_unreachable ();
17032
17033 case OFFSET_REF:
17034 {
17035 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17036 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17037 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17038 r = build2 (code, type, op0, op1);
17039 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17040 if (!mark_used (TREE_OPERAND (r, 1), complain)
17041 && !(complain & tf_error))
17042 return error_mark_node;
17043 return r;
17044 }
17045
17046 case EXPR_PACK_EXPANSION:
17047 error ("invalid use of pack expansion expression");
17048 return error_mark_node;
17049
17050 case NONTYPE_ARGUMENT_PACK:
17051 error ("use %<...%> to expand argument pack");
17052 return error_mark_node;
17053
17054 case VOID_CST:
17055 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17056 return t;
17057
17058 case INTEGER_CST:
17059 case REAL_CST:
17060 case COMPLEX_CST:
17061 {
17062 /* Instantiate any typedefs in the type. */
17063 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17064 r = fold_convert (type, t);
17065 gcc_assert (TREE_CODE (r) == code);
17066 return r;
17067 }
17068
17069 case STRING_CST:
17070 {
17071 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17072 r = t;
17073 if (type != TREE_TYPE (t))
17074 {
17075 r = copy_node (t);
17076 TREE_TYPE (r) = type;
17077 }
17078 return r;
17079 }
17080
17081 case PTRMEM_CST:
17082 /* These can sometimes show up in a partial instantiation, but never
17083 involve template parms. */
17084 gcc_assert (!uses_template_parms (t));
17085 return t;
17086
17087 case UNARY_LEFT_FOLD_EXPR:
17088 return tsubst_unary_left_fold (t, args, complain, in_decl);
17089 case UNARY_RIGHT_FOLD_EXPR:
17090 return tsubst_unary_right_fold (t, args, complain, in_decl);
17091 case BINARY_LEFT_FOLD_EXPR:
17092 return tsubst_binary_left_fold (t, args, complain, in_decl);
17093 case BINARY_RIGHT_FOLD_EXPR:
17094 return tsubst_binary_right_fold (t, args, complain, in_decl);
17095 case PREDICT_EXPR:
17096 return t;
17097
17098 case DEBUG_BEGIN_STMT:
17099 /* ??? There's no point in copying it for now, but maybe some
17100 day it will contain more information, such as a pointer back
17101 to the containing function, inlined copy or so. */
17102 return t;
17103
17104 case CO_AWAIT_EXPR:
17105 return tsubst_expr (t, args, complain, in_decl,
17106 /*integral_constant_expression_p=*/false);
17107 break;
17108
17109 default:
17110 /* We shouldn't get here, but keep going if !flag_checking. */
17111 if (flag_checking)
17112 gcc_unreachable ();
17113 return t;
17114 }
17115 }
17116
17117 /* Helper function for tsubst_omp_clauses, used for instantiation of
17118 OMP_CLAUSE_DECL of clauses. */
17119
17120 static tree
17121 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17122 tree in_decl, tree *iterator_cache)
17123 {
17124 if (decl == NULL_TREE)
17125 return NULL_TREE;
17126
17127 /* Handle OpenMP iterators. */
17128 if (TREE_CODE (decl) == TREE_LIST
17129 && TREE_PURPOSE (decl)
17130 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17131 {
17132 tree ret;
17133 if (iterator_cache[0] == TREE_PURPOSE (decl))
17134 ret = iterator_cache[1];
17135 else
17136 {
17137 tree *tp = &ret;
17138 begin_scope (sk_omp, NULL);
17139 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17140 {
17141 *tp = copy_node (it);
17142 TREE_VEC_ELT (*tp, 0)
17143 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17144 TREE_VEC_ELT (*tp, 1)
17145 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17146 /*integral_constant_expression_p=*/false);
17147 TREE_VEC_ELT (*tp, 2)
17148 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17149 /*integral_constant_expression_p=*/false);
17150 TREE_VEC_ELT (*tp, 3)
17151 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17152 /*integral_constant_expression_p=*/false);
17153 TREE_CHAIN (*tp) = NULL_TREE;
17154 tp = &TREE_CHAIN (*tp);
17155 }
17156 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17157 iterator_cache[0] = TREE_PURPOSE (decl);
17158 iterator_cache[1] = ret;
17159 }
17160 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17161 args, complain,
17162 in_decl, NULL));
17163 }
17164
17165 /* Handle an OpenMP array section represented as a TREE_LIST (or
17166 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17167 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17168 TREE_LIST. We can handle it exactly the same as an array section
17169 (purpose, value, and a chain), even though the nomenclature
17170 (low_bound, length, etc) is different. */
17171 if (TREE_CODE (decl) == TREE_LIST)
17172 {
17173 tree low_bound
17174 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17175 /*integral_constant_expression_p=*/false);
17176 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17177 /*integral_constant_expression_p=*/false);
17178 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17179 in_decl, NULL);
17180 if (TREE_PURPOSE (decl) == low_bound
17181 && TREE_VALUE (decl) == length
17182 && TREE_CHAIN (decl) == chain)
17183 return decl;
17184 tree ret = tree_cons (low_bound, length, chain);
17185 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17186 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17187 return ret;
17188 }
17189 tree ret = tsubst_expr (decl, args, complain, in_decl,
17190 /*integral_constant_expression_p=*/false);
17191 /* Undo convert_from_reference tsubst_expr could have called. */
17192 if (decl
17193 && REFERENCE_REF_P (ret)
17194 && !REFERENCE_REF_P (decl))
17195 ret = TREE_OPERAND (ret, 0);
17196 return ret;
17197 }
17198
17199 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17200
17201 static tree
17202 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17203 tree args, tsubst_flags_t complain, tree in_decl)
17204 {
17205 tree new_clauses = NULL_TREE, nc, oc;
17206 tree linear_no_step = NULL_TREE;
17207 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17208
17209 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17210 {
17211 nc = copy_node (oc);
17212 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17213 new_clauses = nc;
17214
17215 switch (OMP_CLAUSE_CODE (nc))
17216 {
17217 case OMP_CLAUSE_LASTPRIVATE:
17218 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17219 {
17220 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17221 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17222 in_decl, /*integral_constant_expression_p=*/false);
17223 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17224 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17225 }
17226 /* FALLTHRU */
17227 case OMP_CLAUSE_PRIVATE:
17228 case OMP_CLAUSE_SHARED:
17229 case OMP_CLAUSE_FIRSTPRIVATE:
17230 case OMP_CLAUSE_COPYIN:
17231 case OMP_CLAUSE_COPYPRIVATE:
17232 case OMP_CLAUSE_UNIFORM:
17233 case OMP_CLAUSE_DEPEND:
17234 case OMP_CLAUSE_FROM:
17235 case OMP_CLAUSE_TO:
17236 case OMP_CLAUSE_MAP:
17237 case OMP_CLAUSE_NONTEMPORAL:
17238 case OMP_CLAUSE_USE_DEVICE_PTR:
17239 case OMP_CLAUSE_USE_DEVICE_ADDR:
17240 case OMP_CLAUSE_IS_DEVICE_PTR:
17241 case OMP_CLAUSE_INCLUSIVE:
17242 case OMP_CLAUSE_EXCLUSIVE:
17243 OMP_CLAUSE_DECL (nc)
17244 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17245 in_decl, iterator_cache);
17246 break;
17247 case OMP_CLAUSE_TILE:
17248 case OMP_CLAUSE_IF:
17249 case OMP_CLAUSE_NUM_THREADS:
17250 case OMP_CLAUSE_SCHEDULE:
17251 case OMP_CLAUSE_COLLAPSE:
17252 case OMP_CLAUSE_FINAL:
17253 case OMP_CLAUSE_DEVICE:
17254 case OMP_CLAUSE_DIST_SCHEDULE:
17255 case OMP_CLAUSE_NUM_TEAMS:
17256 case OMP_CLAUSE_THREAD_LIMIT:
17257 case OMP_CLAUSE_SAFELEN:
17258 case OMP_CLAUSE_SIMDLEN:
17259 case OMP_CLAUSE_NUM_TASKS:
17260 case OMP_CLAUSE_GRAINSIZE:
17261 case OMP_CLAUSE_PRIORITY:
17262 case OMP_CLAUSE_ORDERED:
17263 case OMP_CLAUSE_HINT:
17264 case OMP_CLAUSE_NUM_GANGS:
17265 case OMP_CLAUSE_NUM_WORKERS:
17266 case OMP_CLAUSE_VECTOR_LENGTH:
17267 case OMP_CLAUSE_WORKER:
17268 case OMP_CLAUSE_VECTOR:
17269 case OMP_CLAUSE_ASYNC:
17270 case OMP_CLAUSE_WAIT:
17271 OMP_CLAUSE_OPERAND (nc, 0)
17272 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17273 in_decl, /*integral_constant_expression_p=*/false);
17274 break;
17275 case OMP_CLAUSE_REDUCTION:
17276 case OMP_CLAUSE_IN_REDUCTION:
17277 case OMP_CLAUSE_TASK_REDUCTION:
17278 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17279 {
17280 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17281 if (TREE_CODE (placeholder) == SCOPE_REF)
17282 {
17283 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17284 complain, in_decl);
17285 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17286 = build_qualified_name (NULL_TREE, scope,
17287 TREE_OPERAND (placeholder, 1),
17288 false);
17289 }
17290 else
17291 gcc_assert (identifier_p (placeholder));
17292 }
17293 OMP_CLAUSE_DECL (nc)
17294 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17295 in_decl, NULL);
17296 break;
17297 case OMP_CLAUSE_GANG:
17298 case OMP_CLAUSE_ALIGNED:
17299 OMP_CLAUSE_DECL (nc)
17300 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17301 in_decl, NULL);
17302 OMP_CLAUSE_OPERAND (nc, 1)
17303 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17304 in_decl, /*integral_constant_expression_p=*/false);
17305 break;
17306 case OMP_CLAUSE_LINEAR:
17307 OMP_CLAUSE_DECL (nc)
17308 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17309 in_decl, NULL);
17310 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17311 {
17312 gcc_assert (!linear_no_step);
17313 linear_no_step = nc;
17314 }
17315 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17316 OMP_CLAUSE_LINEAR_STEP (nc)
17317 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17318 complain, in_decl, NULL);
17319 else
17320 OMP_CLAUSE_LINEAR_STEP (nc)
17321 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17322 in_decl,
17323 /*integral_constant_expression_p=*/false);
17324 break;
17325 case OMP_CLAUSE_NOWAIT:
17326 case OMP_CLAUSE_DEFAULT:
17327 case OMP_CLAUSE_UNTIED:
17328 case OMP_CLAUSE_MERGEABLE:
17329 case OMP_CLAUSE_INBRANCH:
17330 case OMP_CLAUSE_NOTINBRANCH:
17331 case OMP_CLAUSE_PROC_BIND:
17332 case OMP_CLAUSE_FOR:
17333 case OMP_CLAUSE_PARALLEL:
17334 case OMP_CLAUSE_SECTIONS:
17335 case OMP_CLAUSE_TASKGROUP:
17336 case OMP_CLAUSE_NOGROUP:
17337 case OMP_CLAUSE_THREADS:
17338 case OMP_CLAUSE_SIMD:
17339 case OMP_CLAUSE_DEFAULTMAP:
17340 case OMP_CLAUSE_ORDER:
17341 case OMP_CLAUSE_BIND:
17342 case OMP_CLAUSE_INDEPENDENT:
17343 case OMP_CLAUSE_AUTO:
17344 case OMP_CLAUSE_SEQ:
17345 case OMP_CLAUSE_IF_PRESENT:
17346 case OMP_CLAUSE_FINALIZE:
17347 break;
17348 default:
17349 gcc_unreachable ();
17350 }
17351 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17352 switch (OMP_CLAUSE_CODE (nc))
17353 {
17354 case OMP_CLAUSE_SHARED:
17355 case OMP_CLAUSE_PRIVATE:
17356 case OMP_CLAUSE_FIRSTPRIVATE:
17357 case OMP_CLAUSE_LASTPRIVATE:
17358 case OMP_CLAUSE_COPYPRIVATE:
17359 case OMP_CLAUSE_LINEAR:
17360 case OMP_CLAUSE_REDUCTION:
17361 case OMP_CLAUSE_IN_REDUCTION:
17362 case OMP_CLAUSE_TASK_REDUCTION:
17363 case OMP_CLAUSE_USE_DEVICE_PTR:
17364 case OMP_CLAUSE_USE_DEVICE_ADDR:
17365 case OMP_CLAUSE_IS_DEVICE_PTR:
17366 case OMP_CLAUSE_INCLUSIVE:
17367 case OMP_CLAUSE_EXCLUSIVE:
17368 /* tsubst_expr on SCOPE_REF results in returning
17369 finish_non_static_data_member result. Undo that here. */
17370 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17371 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17372 == IDENTIFIER_NODE))
17373 {
17374 tree t = OMP_CLAUSE_DECL (nc);
17375 tree v = t;
17376 while (v)
17377 switch (TREE_CODE (v))
17378 {
17379 case COMPONENT_REF:
17380 case MEM_REF:
17381 case INDIRECT_REF:
17382 CASE_CONVERT:
17383 case POINTER_PLUS_EXPR:
17384 v = TREE_OPERAND (v, 0);
17385 continue;
17386 case PARM_DECL:
17387 if (DECL_CONTEXT (v) == current_function_decl
17388 && DECL_ARTIFICIAL (v)
17389 && DECL_NAME (v) == this_identifier)
17390 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17391 /* FALLTHRU */
17392 default:
17393 v = NULL_TREE;
17394 break;
17395 }
17396 }
17397 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17398 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17399 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17400 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17401 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17402 {
17403 tree decl = OMP_CLAUSE_DECL (nc);
17404 if (VAR_P (decl))
17405 {
17406 retrofit_lang_decl (decl);
17407 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17408 }
17409 }
17410 break;
17411 default:
17412 break;
17413 }
17414 }
17415
17416 new_clauses = nreverse (new_clauses);
17417 if (ort != C_ORT_OMP_DECLARE_SIMD)
17418 {
17419 new_clauses = finish_omp_clauses (new_clauses, ort);
17420 if (linear_no_step)
17421 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17422 if (nc == linear_no_step)
17423 {
17424 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17425 break;
17426 }
17427 }
17428 return new_clauses;
17429 }
17430
17431 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17432
17433 static tree
17434 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17435 tree in_decl)
17436 {
17437 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17438
17439 tree purpose, value, chain;
17440
17441 if (t == NULL)
17442 return t;
17443
17444 if (TREE_CODE (t) != TREE_LIST)
17445 return tsubst_copy_and_build (t, args, complain, in_decl,
17446 /*function_p=*/false,
17447 /*integral_constant_expression_p=*/false);
17448
17449 if (t == void_list_node)
17450 return t;
17451
17452 purpose = TREE_PURPOSE (t);
17453 if (purpose)
17454 purpose = RECUR (purpose);
17455 value = TREE_VALUE (t);
17456 if (value)
17457 {
17458 if (TREE_CODE (value) != LABEL_DECL)
17459 value = RECUR (value);
17460 else
17461 {
17462 value = lookup_label (DECL_NAME (value));
17463 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17464 TREE_USED (value) = 1;
17465 }
17466 }
17467 chain = TREE_CHAIN (t);
17468 if (chain && chain != void_type_node)
17469 chain = RECUR (chain);
17470 return tree_cons (purpose, value, chain);
17471 #undef RECUR
17472 }
17473
17474 /* Used to temporarily communicate the list of #pragma omp parallel
17475 clauses to #pragma omp for instantiation if they are combined
17476 together. */
17477
17478 static tree *omp_parallel_combined_clauses;
17479
17480 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17481 tree *, unsigned int *);
17482
17483 /* Substitute one OMP_FOR iterator. */
17484
17485 static bool
17486 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17487 tree initv, tree condv, tree incrv, tree *clauses,
17488 tree args, tsubst_flags_t complain, tree in_decl,
17489 bool integral_constant_expression_p)
17490 {
17491 #define RECUR(NODE) \
17492 tsubst_expr ((NODE), args, complain, in_decl, \
17493 integral_constant_expression_p)
17494 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17495 bool ret = false;
17496
17497 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17498 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17499
17500 decl = TREE_OPERAND (init, 0);
17501 init = TREE_OPERAND (init, 1);
17502 tree decl_expr = NULL_TREE;
17503 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17504 if (range_for)
17505 {
17506 bool decomp = false;
17507 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17508 {
17509 tree v = DECL_VALUE_EXPR (decl);
17510 if (TREE_CODE (v) == ARRAY_REF
17511 && VAR_P (TREE_OPERAND (v, 0))
17512 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17513 {
17514 tree decomp_first = NULL_TREE;
17515 unsigned decomp_cnt = 0;
17516 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17517 maybe_push_decl (d);
17518 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17519 in_decl, &decomp_first, &decomp_cnt);
17520 decomp = true;
17521 if (d == error_mark_node)
17522 decl = error_mark_node;
17523 else
17524 for (unsigned int i = 0; i < decomp_cnt; i++)
17525 {
17526 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17527 {
17528 tree v = build_nt (ARRAY_REF, d,
17529 size_int (decomp_cnt - i - 1),
17530 NULL_TREE, NULL_TREE);
17531 SET_DECL_VALUE_EXPR (decomp_first, v);
17532 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17533 }
17534 fit_decomposition_lang_decl (decomp_first, d);
17535 decomp_first = DECL_CHAIN (decomp_first);
17536 }
17537 }
17538 }
17539 decl = tsubst_decl (decl, args, complain);
17540 if (!decomp)
17541 maybe_push_decl (decl);
17542 }
17543 else if (init && TREE_CODE (init) == DECL_EXPR)
17544 {
17545 /* We need to jump through some hoops to handle declarations in the
17546 init-statement, since we might need to handle auto deduction,
17547 but we need to keep control of initialization. */
17548 decl_expr = init;
17549 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17550 decl = tsubst_decl (decl, args, complain);
17551 }
17552 else
17553 {
17554 if (TREE_CODE (decl) == SCOPE_REF)
17555 {
17556 decl = RECUR (decl);
17557 if (TREE_CODE (decl) == COMPONENT_REF)
17558 {
17559 tree v = decl;
17560 while (v)
17561 switch (TREE_CODE (v))
17562 {
17563 case COMPONENT_REF:
17564 case MEM_REF:
17565 case INDIRECT_REF:
17566 CASE_CONVERT:
17567 case POINTER_PLUS_EXPR:
17568 v = TREE_OPERAND (v, 0);
17569 continue;
17570 case PARM_DECL:
17571 if (DECL_CONTEXT (v) == current_function_decl
17572 && DECL_ARTIFICIAL (v)
17573 && DECL_NAME (v) == this_identifier)
17574 {
17575 decl = TREE_OPERAND (decl, 1);
17576 decl = omp_privatize_field (decl, false);
17577 }
17578 /* FALLTHRU */
17579 default:
17580 v = NULL_TREE;
17581 break;
17582 }
17583 }
17584 }
17585 else
17586 decl = RECUR (decl);
17587 }
17588 if (init && TREE_CODE (init) == TREE_VEC)
17589 {
17590 init = copy_node (init);
17591 TREE_VEC_ELT (init, 0)
17592 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17593 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17594 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17595 }
17596 else
17597 init = RECUR (init);
17598
17599 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17600 {
17601 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17602 if (TREE_CODE (o) == TREE_LIST)
17603 TREE_VEC_ELT (orig_declv, i)
17604 = tree_cons (RECUR (TREE_PURPOSE (o)),
17605 RECUR (TREE_VALUE (o)),
17606 NULL_TREE);
17607 else
17608 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17609 }
17610
17611 if (range_for)
17612 {
17613 tree this_pre_body = NULL_TREE;
17614 tree orig_init = NULL_TREE;
17615 tree orig_decl = NULL_TREE;
17616 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17617 orig_init, cond, incr);
17618 if (orig_decl)
17619 {
17620 if (orig_declv == NULL_TREE)
17621 orig_declv = copy_node (declv);
17622 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17623 ret = true;
17624 }
17625 else if (orig_declv)
17626 TREE_VEC_ELT (orig_declv, i) = decl;
17627 }
17628
17629 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17630 if (!range_for && auto_node && init)
17631 TREE_TYPE (decl)
17632 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17633
17634 gcc_assert (!type_dependent_expression_p (decl));
17635
17636 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17637 {
17638 if (decl_expr)
17639 {
17640 /* Declare the variable, but don't let that initialize it. */
17641 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17642 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17643 RECUR (decl_expr);
17644 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17645 }
17646
17647 if (!range_for)
17648 {
17649 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17650 if (COMPARISON_CLASS_P (cond)
17651 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17652 {
17653 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17654 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17655 TREE_VEC_ELT (rhs, 0)
17656 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17657 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17658 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17659 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17660 lhs, rhs);
17661 }
17662 else
17663 cond = RECUR (cond);
17664 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17665 if (TREE_CODE (incr) == MODIFY_EXPR)
17666 {
17667 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17668 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17669 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17670 NOP_EXPR, rhs, complain);
17671 }
17672 else
17673 incr = RECUR (incr);
17674 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17675 TREE_VEC_ELT (orig_declv, i) = decl;
17676 }
17677 TREE_VEC_ELT (declv, i) = decl;
17678 TREE_VEC_ELT (initv, i) = init;
17679 TREE_VEC_ELT (condv, i) = cond;
17680 TREE_VEC_ELT (incrv, i) = incr;
17681 return ret;
17682 }
17683
17684 if (decl_expr)
17685 {
17686 /* Declare and initialize the variable. */
17687 RECUR (decl_expr);
17688 init = NULL_TREE;
17689 }
17690 else if (init)
17691 {
17692 tree *pc;
17693 int j;
17694 for (j = ((omp_parallel_combined_clauses == NULL
17695 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17696 {
17697 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17698 {
17699 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17700 && OMP_CLAUSE_DECL (*pc) == decl)
17701 break;
17702 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17703 && OMP_CLAUSE_DECL (*pc) == decl)
17704 {
17705 if (j)
17706 break;
17707 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17708 tree c = *pc;
17709 *pc = OMP_CLAUSE_CHAIN (c);
17710 OMP_CLAUSE_CHAIN (c) = *clauses;
17711 *clauses = c;
17712 }
17713 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17714 && OMP_CLAUSE_DECL (*pc) == decl)
17715 {
17716 error ("iteration variable %qD should not be firstprivate",
17717 decl);
17718 *pc = OMP_CLAUSE_CHAIN (*pc);
17719 }
17720 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17721 && OMP_CLAUSE_DECL (*pc) == decl)
17722 {
17723 error ("iteration variable %qD should not be reduction",
17724 decl);
17725 *pc = OMP_CLAUSE_CHAIN (*pc);
17726 }
17727 else
17728 pc = &OMP_CLAUSE_CHAIN (*pc);
17729 }
17730 if (*pc)
17731 break;
17732 }
17733 if (*pc == NULL_TREE)
17734 {
17735 tree c = build_omp_clause (input_location,
17736 TREE_CODE (t) == OMP_LOOP
17737 ? OMP_CLAUSE_LASTPRIVATE
17738 : OMP_CLAUSE_PRIVATE);
17739 OMP_CLAUSE_DECL (c) = decl;
17740 c = finish_omp_clauses (c, C_ORT_OMP);
17741 if (c)
17742 {
17743 OMP_CLAUSE_CHAIN (c) = *clauses;
17744 *clauses = c;
17745 }
17746 }
17747 }
17748 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17749 if (COMPARISON_CLASS_P (cond))
17750 {
17751 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17752 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17753 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17754 }
17755 else
17756 cond = RECUR (cond);
17757 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17758 switch (TREE_CODE (incr))
17759 {
17760 case PREINCREMENT_EXPR:
17761 case PREDECREMENT_EXPR:
17762 case POSTINCREMENT_EXPR:
17763 case POSTDECREMENT_EXPR:
17764 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17765 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17766 break;
17767 case MODIFY_EXPR:
17768 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17769 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17770 {
17771 tree rhs = TREE_OPERAND (incr, 1);
17772 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17773 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17774 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17775 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17776 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17777 rhs0, rhs1));
17778 }
17779 else
17780 incr = RECUR (incr);
17781 break;
17782 case MODOP_EXPR:
17783 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17784 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17785 {
17786 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17787 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17788 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17789 TREE_TYPE (decl), lhs,
17790 RECUR (TREE_OPERAND (incr, 2))));
17791 }
17792 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17793 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17794 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17795 {
17796 tree rhs = TREE_OPERAND (incr, 2);
17797 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17798 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17799 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17800 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17801 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17802 rhs0, rhs1));
17803 }
17804 else
17805 incr = RECUR (incr);
17806 break;
17807 default:
17808 incr = RECUR (incr);
17809 break;
17810 }
17811
17812 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17813 TREE_VEC_ELT (orig_declv, i) = decl;
17814 TREE_VEC_ELT (declv, i) = decl;
17815 TREE_VEC_ELT (initv, i) = init;
17816 TREE_VEC_ELT (condv, i) = cond;
17817 TREE_VEC_ELT (incrv, i) = incr;
17818 return false;
17819 #undef RECUR
17820 }
17821
17822 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17823 of OMP_TARGET's body. */
17824
17825 static tree
17826 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17827 {
17828 *walk_subtrees = 0;
17829 switch (TREE_CODE (*tp))
17830 {
17831 case OMP_TEAMS:
17832 return *tp;
17833 case BIND_EXPR:
17834 case STATEMENT_LIST:
17835 *walk_subtrees = 1;
17836 break;
17837 default:
17838 break;
17839 }
17840 return NULL_TREE;
17841 }
17842
17843 /* Helper function for tsubst_expr. For decomposition declaration
17844 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17845 also the corresponding decls representing the identifiers
17846 of the decomposition declaration. Return DECL if successful
17847 or error_mark_node otherwise, set *FIRST to the first decl
17848 in the list chained through DECL_CHAIN and *CNT to the number
17849 of such decls. */
17850
17851 static tree
17852 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17853 tsubst_flags_t complain, tree in_decl, tree *first,
17854 unsigned int *cnt)
17855 {
17856 tree decl2, decl3, prev = decl;
17857 *cnt = 0;
17858 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17859 for (decl2 = DECL_CHAIN (pattern_decl);
17860 decl2
17861 && VAR_P (decl2)
17862 && DECL_DECOMPOSITION_P (decl2)
17863 && DECL_NAME (decl2);
17864 decl2 = DECL_CHAIN (decl2))
17865 {
17866 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17867 {
17868 gcc_assert (errorcount);
17869 return error_mark_node;
17870 }
17871 (*cnt)++;
17872 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17873 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17874 tree v = DECL_VALUE_EXPR (decl2);
17875 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17876 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17877 decl3 = tsubst (decl2, args, complain, in_decl);
17878 SET_DECL_VALUE_EXPR (decl2, v);
17879 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17880 if (VAR_P (decl3))
17881 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17882 else
17883 {
17884 gcc_assert (errorcount);
17885 decl = error_mark_node;
17886 continue;
17887 }
17888 maybe_push_decl (decl3);
17889 if (error_operand_p (decl3))
17890 decl = error_mark_node;
17891 else if (decl != error_mark_node
17892 && DECL_CHAIN (decl3) != prev
17893 && decl != prev)
17894 {
17895 gcc_assert (errorcount);
17896 decl = error_mark_node;
17897 }
17898 else
17899 prev = decl3;
17900 }
17901 *first = prev;
17902 return decl;
17903 }
17904
17905 /* Return the proper local_specialization for init-capture pack DECL. */
17906
17907 static tree
17908 lookup_init_capture_pack (tree decl)
17909 {
17910 /* We handle normal pack captures by forwarding to the specialization of the
17911 captured parameter. We can't do that for pack init-captures; we need them
17912 to have their own local_specialization. We created the individual
17913 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17914 when we process the DECL_EXPR for the pack init-capture in the template.
17915 So, how do we find them? We don't know the capture proxy pack when
17916 building the individual resulting proxies, and we don't know the
17917 individual proxies when instantiating the pack. What we have in common is
17918 the FIELD_DECL.
17919
17920 So...when we instantiate the FIELD_DECL, we stick the result in
17921 local_specializations. Then at the DECL_EXPR we look up that result, see
17922 how many elements it has, synthesize the names, and look them up. */
17923
17924 tree cname = DECL_NAME (decl);
17925 tree val = DECL_VALUE_EXPR (decl);
17926 tree field = TREE_OPERAND (val, 1);
17927 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17928 tree fpack = retrieve_local_specialization (field);
17929 if (fpack == error_mark_node)
17930 return error_mark_node;
17931
17932 int len = 1;
17933 tree vec = NULL_TREE;
17934 tree r = NULL_TREE;
17935 if (TREE_CODE (fpack) == TREE_VEC)
17936 {
17937 len = TREE_VEC_LENGTH (fpack);
17938 vec = make_tree_vec (len);
17939 r = make_node (NONTYPE_ARGUMENT_PACK);
17940 SET_ARGUMENT_PACK_ARGS (r, vec);
17941 }
17942 for (int i = 0; i < len; ++i)
17943 {
17944 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17945 tree elt = lookup_name (ename);
17946 if (vec)
17947 TREE_VEC_ELT (vec, i) = elt;
17948 else
17949 r = elt;
17950 }
17951 return r;
17952 }
17953
17954 /* Like tsubst_copy for expressions, etc. but also does semantic
17955 processing. */
17956
17957 tree
17958 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17959 bool integral_constant_expression_p)
17960 {
17961 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17962 #define RECUR(NODE) \
17963 tsubst_expr ((NODE), args, complain, in_decl, \
17964 integral_constant_expression_p)
17965
17966 tree stmt, tmp;
17967 tree r;
17968 location_t loc;
17969
17970 if (t == NULL_TREE || t == error_mark_node)
17971 return t;
17972
17973 loc = input_location;
17974 if (location_t eloc = cp_expr_location (t))
17975 input_location = eloc;
17976 if (STATEMENT_CODE_P (TREE_CODE (t)))
17977 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17978
17979 switch (TREE_CODE (t))
17980 {
17981 case STATEMENT_LIST:
17982 {
17983 tree_stmt_iterator i;
17984 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17985 RECUR (tsi_stmt (i));
17986 break;
17987 }
17988
17989 case CTOR_INITIALIZER:
17990 finish_mem_initializers (tsubst_initializer_list
17991 (TREE_OPERAND (t, 0), args));
17992 break;
17993
17994 case RETURN_EXPR:
17995 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17996 break;
17997
17998 case CO_RETURN_EXPR:
17999 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18000 break;
18001
18002 case CO_YIELD_EXPR:
18003 stmt = finish_co_yield_expr (input_location,
18004 RECUR (TREE_OPERAND (t, 0)));
18005 RETURN (stmt);
18006 break;
18007
18008 case CO_AWAIT_EXPR:
18009 stmt = finish_co_await_expr (input_location,
18010 RECUR (TREE_OPERAND (t, 0)));
18011 RETURN (stmt);
18012 break;
18013
18014 case EXPR_STMT:
18015 tmp = RECUR (EXPR_STMT_EXPR (t));
18016 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18017 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18018 else
18019 finish_expr_stmt (tmp);
18020 break;
18021
18022 case USING_STMT:
18023 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18024 break;
18025
18026 case DECL_EXPR:
18027 {
18028 tree decl, pattern_decl;
18029 tree init;
18030
18031 pattern_decl = decl = DECL_EXPR_DECL (t);
18032 if (TREE_CODE (decl) == LABEL_DECL)
18033 finish_label_decl (DECL_NAME (decl));
18034 else if (TREE_CODE (decl) == USING_DECL)
18035 {
18036 tree scope = USING_DECL_SCOPE (decl);
18037 tree name = DECL_NAME (decl);
18038
18039 scope = tsubst (scope, args, complain, in_decl);
18040 finish_nonmember_using_decl (scope, name);
18041 }
18042 else if (is_capture_proxy (decl)
18043 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18044 {
18045 /* We're in tsubst_lambda_expr, we've already inserted a new
18046 capture proxy, so look it up and register it. */
18047 tree inst;
18048 if (!DECL_PACK_P (decl))
18049 {
18050 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18051 LOOK_want::HIDDEN_LAMBDA);
18052 gcc_assert (inst != decl && is_capture_proxy (inst));
18053 }
18054 else if (is_normal_capture_proxy (decl))
18055 {
18056 inst = (retrieve_local_specialization
18057 (DECL_CAPTURED_VARIABLE (decl)));
18058 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18059 || DECL_PACK_P (inst));
18060 }
18061 else
18062 inst = lookup_init_capture_pack (decl);
18063
18064 register_local_specialization (inst, decl);
18065 break;
18066 }
18067 else if (DECL_PRETTY_FUNCTION_P (decl))
18068 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18069 DECL_NAME (decl),
18070 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18071 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18072 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18073 /* Don't copy the old closure; we'll create a new one in
18074 tsubst_lambda_expr. */
18075 break;
18076 else
18077 {
18078 init = DECL_INITIAL (decl);
18079 decl = tsubst (decl, args, complain, in_decl);
18080 if (decl != error_mark_node)
18081 {
18082 /* By marking the declaration as instantiated, we avoid
18083 trying to instantiate it. Since instantiate_decl can't
18084 handle local variables, and since we've already done
18085 all that needs to be done, that's the right thing to
18086 do. */
18087 if (VAR_P (decl))
18088 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18089 if (VAR_P (decl) && !DECL_NAME (decl)
18090 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18091 /* Anonymous aggregates are a special case. */
18092 finish_anon_union (decl);
18093 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18094 {
18095 DECL_CONTEXT (decl) = current_function_decl;
18096 if (DECL_NAME (decl) == this_identifier)
18097 {
18098 tree lam = DECL_CONTEXT (current_function_decl);
18099 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18100 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18101 }
18102 insert_capture_proxy (decl);
18103 }
18104 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18105 /* We already did a pushtag. */;
18106 else if (TREE_CODE (decl) == FUNCTION_DECL
18107 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18108 && DECL_FUNCTION_SCOPE_P (pattern_decl))
18109 {
18110 /* We pretend this is regular local extern decl of
18111 a namespace-scope fn. Then we make it really
18112 local, it is a nested function. */
18113 gcc_checking_assert (DECL_LOCAL_DECL_P (decl));
18114 DECL_CONTEXT (decl) = global_namespace;
18115 pushdecl (decl);
18116 DECL_CONTEXT (decl) = current_function_decl;
18117 if (cp_check_omp_declare_reduction (decl))
18118 instantiate_body (pattern_decl, args, decl, true);
18119 }
18120 else
18121 {
18122 bool const_init = false;
18123 unsigned int cnt = 0;
18124 tree first = NULL_TREE, ndecl = error_mark_node;
18125 maybe_push_decl (decl);
18126
18127 if (VAR_P (decl)
18128 && DECL_DECOMPOSITION_P (decl)
18129 && TREE_TYPE (pattern_decl) != error_mark_node)
18130 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18131 complain, in_decl, &first,
18132 &cnt);
18133
18134 init = tsubst_init (init, decl, args, complain, in_decl);
18135
18136 if (VAR_P (decl))
18137 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18138 (pattern_decl));
18139
18140 if (ndecl != error_mark_node)
18141 cp_maybe_mangle_decomp (ndecl, first, cnt);
18142
18143 /* In a non-template function, VLA type declarations are
18144 handled in grokdeclarator; for templates, handle them
18145 now. */
18146 predeclare_vla (decl);
18147
18148 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
18149
18150 if (ndecl != error_mark_node)
18151 cp_finish_decomp (ndecl, first, cnt);
18152 }
18153 }
18154 }
18155
18156 break;
18157 }
18158
18159 case FOR_STMT:
18160 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18161 RECUR (FOR_INIT_STMT (t));
18162 finish_init_stmt (stmt);
18163 tmp = RECUR (FOR_COND (t));
18164 finish_for_cond (tmp, stmt, false, 0);
18165 tmp = RECUR (FOR_EXPR (t));
18166 finish_for_expr (tmp, stmt);
18167 {
18168 bool prev = note_iteration_stmt_body_start ();
18169 RECUR (FOR_BODY (t));
18170 note_iteration_stmt_body_end (prev);
18171 }
18172 finish_for_stmt (stmt);
18173 break;
18174
18175 case RANGE_FOR_STMT:
18176 {
18177 /* Construct another range_for, if this is not a final
18178 substitution (for inside a generic lambda of a
18179 template). Otherwise convert to a regular for. */
18180 tree decl, expr;
18181 stmt = (processing_template_decl
18182 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18183 : begin_for_stmt (NULL_TREE, NULL_TREE));
18184 RECUR (RANGE_FOR_INIT_STMT (t));
18185 decl = RANGE_FOR_DECL (t);
18186 decl = tsubst (decl, args, complain, in_decl);
18187 maybe_push_decl (decl);
18188 expr = RECUR (RANGE_FOR_EXPR (t));
18189
18190 tree decomp_first = NULL_TREE;
18191 unsigned decomp_cnt = 0;
18192 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18193 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18194 complain, in_decl,
18195 &decomp_first, &decomp_cnt);
18196
18197 if (processing_template_decl)
18198 {
18199 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18200 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18201 finish_range_for_decl (stmt, decl, expr);
18202 if (decomp_first && decl != error_mark_node)
18203 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18204 }
18205 else
18206 {
18207 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18208 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18209 stmt = cp_convert_range_for (stmt, decl, expr,
18210 decomp_first, decomp_cnt,
18211 RANGE_FOR_IVDEP (t), unroll);
18212 }
18213
18214 bool prev = note_iteration_stmt_body_start ();
18215 RECUR (RANGE_FOR_BODY (t));
18216 note_iteration_stmt_body_end (prev);
18217 finish_for_stmt (stmt);
18218 }
18219 break;
18220
18221 case WHILE_STMT:
18222 stmt = begin_while_stmt ();
18223 tmp = RECUR (WHILE_COND (t));
18224 finish_while_stmt_cond (tmp, stmt, false, 0);
18225 {
18226 bool prev = note_iteration_stmt_body_start ();
18227 RECUR (WHILE_BODY (t));
18228 note_iteration_stmt_body_end (prev);
18229 }
18230 finish_while_stmt (stmt);
18231 break;
18232
18233 case DO_STMT:
18234 stmt = begin_do_stmt ();
18235 {
18236 bool prev = note_iteration_stmt_body_start ();
18237 RECUR (DO_BODY (t));
18238 note_iteration_stmt_body_end (prev);
18239 }
18240 finish_do_body (stmt);
18241 tmp = RECUR (DO_COND (t));
18242 finish_do_stmt (tmp, stmt, false, 0);
18243 break;
18244
18245 case IF_STMT:
18246 stmt = begin_if_stmt ();
18247 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18248 if (IF_STMT_CONSTEXPR_P (t))
18249 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18250 tmp = RECUR (IF_COND (t));
18251 tmp = finish_if_stmt_cond (tmp, stmt);
18252 if (IF_STMT_CONSTEXPR_P (t)
18253 && instantiation_dependent_expression_p (tmp))
18254 {
18255 /* We're partially instantiating a generic lambda, but the condition
18256 of the constexpr if is still dependent. Don't substitute into the
18257 branches now, just remember the template arguments. */
18258 do_poplevel (IF_SCOPE (stmt));
18259 IF_COND (stmt) = IF_COND (t);
18260 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18261 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18262 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18263 add_stmt (stmt);
18264 break;
18265 }
18266 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18267 /* Don't instantiate the THEN_CLAUSE. */;
18268 else
18269 {
18270 tree folded = fold_non_dependent_expr (tmp, complain);
18271 bool inhibit = integer_zerop (folded);
18272 if (inhibit)
18273 ++c_inhibit_evaluation_warnings;
18274 RECUR (THEN_CLAUSE (t));
18275 if (inhibit)
18276 --c_inhibit_evaluation_warnings;
18277 }
18278 finish_then_clause (stmt);
18279
18280 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18281 /* Don't instantiate the ELSE_CLAUSE. */;
18282 else if (ELSE_CLAUSE (t))
18283 {
18284 tree folded = fold_non_dependent_expr (tmp, complain);
18285 bool inhibit = integer_nonzerop (folded);
18286 begin_else_clause (stmt);
18287 if (inhibit)
18288 ++c_inhibit_evaluation_warnings;
18289 RECUR (ELSE_CLAUSE (t));
18290 if (inhibit)
18291 --c_inhibit_evaluation_warnings;
18292 finish_else_clause (stmt);
18293 }
18294
18295 finish_if_stmt (stmt);
18296 break;
18297
18298 case BIND_EXPR:
18299 if (BIND_EXPR_BODY_BLOCK (t))
18300 stmt = begin_function_body ();
18301 else
18302 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18303 ? BCS_TRY_BLOCK : 0);
18304
18305 RECUR (BIND_EXPR_BODY (t));
18306
18307 if (BIND_EXPR_BODY_BLOCK (t))
18308 finish_function_body (stmt);
18309 else
18310 finish_compound_stmt (stmt);
18311 break;
18312
18313 case BREAK_STMT:
18314 finish_break_stmt ();
18315 break;
18316
18317 case CONTINUE_STMT:
18318 finish_continue_stmt ();
18319 break;
18320
18321 case SWITCH_STMT:
18322 stmt = begin_switch_stmt ();
18323 tmp = RECUR (SWITCH_STMT_COND (t));
18324 finish_switch_cond (tmp, stmt);
18325 RECUR (SWITCH_STMT_BODY (t));
18326 finish_switch_stmt (stmt);
18327 break;
18328
18329 case CASE_LABEL_EXPR:
18330 {
18331 tree decl = CASE_LABEL (t);
18332 tree low = RECUR (CASE_LOW (t));
18333 tree high = RECUR (CASE_HIGH (t));
18334 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18335 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18336 {
18337 tree label = CASE_LABEL (l);
18338 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18339 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18340 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18341 }
18342 }
18343 break;
18344
18345 case LABEL_EXPR:
18346 {
18347 tree decl = LABEL_EXPR_LABEL (t);
18348 tree label;
18349
18350 label = finish_label_stmt (DECL_NAME (decl));
18351 if (TREE_CODE (label) == LABEL_DECL)
18352 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18353 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18354 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18355 }
18356 break;
18357
18358 case GOTO_EXPR:
18359 tmp = GOTO_DESTINATION (t);
18360 if (TREE_CODE (tmp) != LABEL_DECL)
18361 /* Computed goto's must be tsubst'd into. On the other hand,
18362 non-computed gotos must not be; the identifier in question
18363 will have no binding. */
18364 tmp = RECUR (tmp);
18365 else
18366 tmp = DECL_NAME (tmp);
18367 finish_goto_stmt (tmp);
18368 break;
18369
18370 case ASM_EXPR:
18371 {
18372 tree string = RECUR (ASM_STRING (t));
18373 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18374 complain, in_decl);
18375 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18376 complain, in_decl);
18377 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18378 complain, in_decl);
18379 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18380 complain, in_decl);
18381 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18382 outputs, inputs, clobbers, labels,
18383 ASM_INLINE_P (t));
18384 tree asm_expr = tmp;
18385 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18386 asm_expr = TREE_OPERAND (asm_expr, 0);
18387 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18388 }
18389 break;
18390
18391 case TRY_BLOCK:
18392 if (CLEANUP_P (t))
18393 {
18394 stmt = begin_try_block ();
18395 RECUR (TRY_STMTS (t));
18396 finish_cleanup_try_block (stmt);
18397 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18398 }
18399 else
18400 {
18401 tree compound_stmt = NULL_TREE;
18402
18403 if (FN_TRY_BLOCK_P (t))
18404 stmt = begin_function_try_block (&compound_stmt);
18405 else
18406 stmt = begin_try_block ();
18407
18408 RECUR (TRY_STMTS (t));
18409
18410 if (FN_TRY_BLOCK_P (t))
18411 finish_function_try_block (stmt);
18412 else
18413 finish_try_block (stmt);
18414
18415 RECUR (TRY_HANDLERS (t));
18416 if (FN_TRY_BLOCK_P (t))
18417 finish_function_handler_sequence (stmt, compound_stmt);
18418 else
18419 finish_handler_sequence (stmt);
18420 }
18421 break;
18422
18423 case HANDLER:
18424 {
18425 tree decl = HANDLER_PARMS (t);
18426
18427 if (decl)
18428 {
18429 decl = tsubst (decl, args, complain, in_decl);
18430 /* Prevent instantiate_decl from trying to instantiate
18431 this variable. We've already done all that needs to be
18432 done. */
18433 if (decl != error_mark_node)
18434 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18435 }
18436 stmt = begin_handler ();
18437 finish_handler_parms (decl, stmt);
18438 RECUR (HANDLER_BODY (t));
18439 finish_handler (stmt);
18440 }
18441 break;
18442
18443 case TAG_DEFN:
18444 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18445 if (CLASS_TYPE_P (tmp))
18446 {
18447 /* Local classes are not independent templates; they are
18448 instantiated along with their containing function. And this
18449 way we don't have to deal with pushing out of one local class
18450 to instantiate a member of another local class. */
18451 /* Closures are handled by the LAMBDA_EXPR. */
18452 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18453 complete_type (tmp);
18454 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18455 if ((VAR_P (fld)
18456 || (TREE_CODE (fld) == FUNCTION_DECL
18457 && !DECL_ARTIFICIAL (fld)))
18458 && DECL_TEMPLATE_INSTANTIATION (fld))
18459 instantiate_decl (fld, /*defer_ok=*/false,
18460 /*expl_inst_class=*/false);
18461 }
18462 break;
18463
18464 case STATIC_ASSERT:
18465 {
18466 tree condition;
18467
18468 ++c_inhibit_evaluation_warnings;
18469 condition =
18470 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18471 args,
18472 complain, in_decl,
18473 /*integral_constant_expression_p=*/true);
18474 --c_inhibit_evaluation_warnings;
18475
18476 finish_static_assert (condition,
18477 STATIC_ASSERT_MESSAGE (t),
18478 STATIC_ASSERT_SOURCE_LOCATION (t),
18479 /*member_p=*/false);
18480 }
18481 break;
18482
18483 case OACC_KERNELS:
18484 case OACC_PARALLEL:
18485 case OACC_SERIAL:
18486 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18487 in_decl);
18488 stmt = begin_omp_parallel ();
18489 RECUR (OMP_BODY (t));
18490 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18491 break;
18492
18493 case OMP_PARALLEL:
18494 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18495 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18496 complain, in_decl);
18497 if (OMP_PARALLEL_COMBINED (t))
18498 omp_parallel_combined_clauses = &tmp;
18499 stmt = begin_omp_parallel ();
18500 RECUR (OMP_PARALLEL_BODY (t));
18501 gcc_assert (omp_parallel_combined_clauses == NULL);
18502 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18503 = OMP_PARALLEL_COMBINED (t);
18504 pop_omp_privatization_clauses (r);
18505 break;
18506
18507 case OMP_TASK:
18508 if (OMP_TASK_BODY (t) == NULL_TREE)
18509 {
18510 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18511 complain, in_decl);
18512 t = copy_node (t);
18513 OMP_TASK_CLAUSES (t) = tmp;
18514 add_stmt (t);
18515 break;
18516 }
18517 r = push_omp_privatization_clauses (false);
18518 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18519 complain, in_decl);
18520 stmt = begin_omp_task ();
18521 RECUR (OMP_TASK_BODY (t));
18522 finish_omp_task (tmp, stmt);
18523 pop_omp_privatization_clauses (r);
18524 break;
18525
18526 case OMP_FOR:
18527 case OMP_LOOP:
18528 case OMP_SIMD:
18529 case OMP_DISTRIBUTE:
18530 case OMP_TASKLOOP:
18531 case OACC_LOOP:
18532 {
18533 tree clauses, body, pre_body;
18534 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18535 tree orig_declv = NULL_TREE;
18536 tree incrv = NULL_TREE;
18537 enum c_omp_region_type ort = C_ORT_OMP;
18538 bool any_range_for = false;
18539 int i;
18540
18541 if (TREE_CODE (t) == OACC_LOOP)
18542 ort = C_ORT_ACC;
18543
18544 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18545 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18546 in_decl);
18547 if (OMP_FOR_INIT (t) != NULL_TREE)
18548 {
18549 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18550 if (OMP_FOR_ORIG_DECLS (t))
18551 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18552 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18553 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18554 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18555 }
18556
18557 keep_next_level (true);
18558 stmt = begin_omp_structured_block ();
18559
18560 pre_body = push_stmt_list ();
18561 RECUR (OMP_FOR_PRE_BODY (t));
18562 pre_body = pop_stmt_list (pre_body);
18563
18564 if (OMP_FOR_INIT (t) != NULL_TREE)
18565 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18566 any_range_for
18567 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18568 condv, incrv, &clauses, args,
18569 complain, in_decl,
18570 integral_constant_expression_p);
18571 omp_parallel_combined_clauses = NULL;
18572
18573 if (any_range_for)
18574 {
18575 gcc_assert (orig_declv);
18576 body = begin_omp_structured_block ();
18577 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18578 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18579 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18580 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18581 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18582 TREE_VEC_ELT (declv, i));
18583 }
18584 else
18585 body = push_stmt_list ();
18586 RECUR (OMP_FOR_BODY (t));
18587 if (any_range_for)
18588 body = finish_omp_structured_block (body);
18589 else
18590 body = pop_stmt_list (body);
18591
18592 if (OMP_FOR_INIT (t) != NULL_TREE)
18593 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18594 orig_declv, initv, condv, incrv, body, pre_body,
18595 NULL, clauses);
18596 else
18597 {
18598 t = make_node (TREE_CODE (t));
18599 TREE_TYPE (t) = void_type_node;
18600 OMP_FOR_BODY (t) = body;
18601 OMP_FOR_PRE_BODY (t) = pre_body;
18602 OMP_FOR_CLAUSES (t) = clauses;
18603 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18604 add_stmt (t);
18605 }
18606
18607 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18608 t));
18609 pop_omp_privatization_clauses (r);
18610 }
18611 break;
18612
18613 case OMP_SECTIONS:
18614 omp_parallel_combined_clauses = NULL;
18615 /* FALLTHRU */
18616 case OMP_SINGLE:
18617 case OMP_TEAMS:
18618 case OMP_CRITICAL:
18619 case OMP_TASKGROUP:
18620 case OMP_SCAN:
18621 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18622 && OMP_TEAMS_COMBINED (t));
18623 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18624 in_decl);
18625 if (TREE_CODE (t) == OMP_TEAMS)
18626 {
18627 keep_next_level (true);
18628 stmt = begin_omp_structured_block ();
18629 RECUR (OMP_BODY (t));
18630 stmt = finish_omp_structured_block (stmt);
18631 }
18632 else
18633 {
18634 stmt = push_stmt_list ();
18635 RECUR (OMP_BODY (t));
18636 stmt = pop_stmt_list (stmt);
18637 }
18638
18639 if (TREE_CODE (t) == OMP_CRITICAL
18640 && tmp != NULL_TREE
18641 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18642 {
18643 error_at (OMP_CLAUSE_LOCATION (tmp),
18644 "%<#pragma omp critical%> with %<hint%> clause requires "
18645 "a name, except when %<omp_sync_hint_none%> is used");
18646 RETURN (error_mark_node);
18647 }
18648 t = copy_node (t);
18649 OMP_BODY (t) = stmt;
18650 OMP_CLAUSES (t) = tmp;
18651 add_stmt (t);
18652 pop_omp_privatization_clauses (r);
18653 break;
18654
18655 case OMP_DEPOBJ:
18656 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18657 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18658 {
18659 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18660 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18661 {
18662 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18663 args, complain, in_decl);
18664 if (tmp == NULL_TREE)
18665 tmp = error_mark_node;
18666 }
18667 else
18668 {
18669 kind = (enum omp_clause_depend_kind)
18670 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18671 tmp = NULL_TREE;
18672 }
18673 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18674 }
18675 else
18676 finish_omp_depobj (EXPR_LOCATION (t), r,
18677 OMP_CLAUSE_DEPEND_SOURCE,
18678 OMP_DEPOBJ_CLAUSES (t));
18679 break;
18680
18681 case OACC_DATA:
18682 case OMP_TARGET_DATA:
18683 case OMP_TARGET:
18684 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18685 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18686 in_decl);
18687 keep_next_level (true);
18688 stmt = begin_omp_structured_block ();
18689
18690 RECUR (OMP_BODY (t));
18691 stmt = finish_omp_structured_block (stmt);
18692
18693 t = copy_node (t);
18694 OMP_BODY (t) = stmt;
18695 OMP_CLAUSES (t) = tmp;
18696 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18697 {
18698 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18699 if (teams)
18700 {
18701 /* For combined target teams, ensure the num_teams and
18702 thread_limit clause expressions are evaluated on the host,
18703 before entering the target construct. */
18704 tree c;
18705 for (c = OMP_TEAMS_CLAUSES (teams);
18706 c; c = OMP_CLAUSE_CHAIN (c))
18707 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18708 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18709 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18710 {
18711 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18712 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18713 if (expr == error_mark_node)
18714 continue;
18715 tmp = TARGET_EXPR_SLOT (expr);
18716 add_stmt (expr);
18717 OMP_CLAUSE_OPERAND (c, 0) = expr;
18718 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18719 OMP_CLAUSE_FIRSTPRIVATE);
18720 OMP_CLAUSE_DECL (tc) = tmp;
18721 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18722 OMP_TARGET_CLAUSES (t) = tc;
18723 }
18724 }
18725 }
18726 add_stmt (t);
18727 break;
18728
18729 case OACC_DECLARE:
18730 t = copy_node (t);
18731 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18732 complain, in_decl);
18733 OACC_DECLARE_CLAUSES (t) = tmp;
18734 add_stmt (t);
18735 break;
18736
18737 case OMP_TARGET_UPDATE:
18738 case OMP_TARGET_ENTER_DATA:
18739 case OMP_TARGET_EXIT_DATA:
18740 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18741 complain, in_decl);
18742 t = copy_node (t);
18743 OMP_STANDALONE_CLAUSES (t) = tmp;
18744 add_stmt (t);
18745 break;
18746
18747 case OACC_ENTER_DATA:
18748 case OACC_EXIT_DATA:
18749 case OACC_UPDATE:
18750 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18751 complain, in_decl);
18752 t = copy_node (t);
18753 OMP_STANDALONE_CLAUSES (t) = tmp;
18754 add_stmt (t);
18755 break;
18756
18757 case OMP_ORDERED:
18758 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18759 complain, in_decl);
18760 stmt = push_stmt_list ();
18761 RECUR (OMP_BODY (t));
18762 stmt = pop_stmt_list (stmt);
18763
18764 t = copy_node (t);
18765 OMP_BODY (t) = stmt;
18766 OMP_ORDERED_CLAUSES (t) = tmp;
18767 add_stmt (t);
18768 break;
18769
18770 case OMP_MASTER:
18771 omp_parallel_combined_clauses = NULL;
18772 /* FALLTHRU */
18773 case OMP_SECTION:
18774 stmt = push_stmt_list ();
18775 RECUR (OMP_BODY (t));
18776 stmt = pop_stmt_list (stmt);
18777
18778 t = copy_node (t);
18779 OMP_BODY (t) = stmt;
18780 add_stmt (t);
18781 break;
18782
18783 case OMP_ATOMIC:
18784 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18785 tmp = NULL_TREE;
18786 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18787 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18788 complain, in_decl);
18789 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18790 {
18791 tree op1 = TREE_OPERAND (t, 1);
18792 tree rhs1 = NULL_TREE;
18793 tree lhs, rhs;
18794 if (TREE_CODE (op1) == COMPOUND_EXPR)
18795 {
18796 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18797 op1 = TREE_OPERAND (op1, 1);
18798 }
18799 lhs = RECUR (TREE_OPERAND (op1, 0));
18800 rhs = RECUR (TREE_OPERAND (op1, 1));
18801 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18802 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18803 OMP_ATOMIC_MEMORY_ORDER (t));
18804 }
18805 else
18806 {
18807 tree op1 = TREE_OPERAND (t, 1);
18808 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18809 tree rhs1 = NULL_TREE;
18810 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18811 enum tree_code opcode = NOP_EXPR;
18812 if (code == OMP_ATOMIC_READ)
18813 {
18814 v = RECUR (TREE_OPERAND (op1, 0));
18815 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18816 }
18817 else if (code == OMP_ATOMIC_CAPTURE_OLD
18818 || code == OMP_ATOMIC_CAPTURE_NEW)
18819 {
18820 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18821 v = RECUR (TREE_OPERAND (op1, 0));
18822 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18823 if (TREE_CODE (op11) == COMPOUND_EXPR)
18824 {
18825 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18826 op11 = TREE_OPERAND (op11, 1);
18827 }
18828 lhs = RECUR (TREE_OPERAND (op11, 0));
18829 rhs = RECUR (TREE_OPERAND (op11, 1));
18830 opcode = TREE_CODE (op11);
18831 if (opcode == MODIFY_EXPR)
18832 opcode = NOP_EXPR;
18833 }
18834 else
18835 {
18836 code = OMP_ATOMIC;
18837 lhs = RECUR (TREE_OPERAND (op1, 0));
18838 rhs = RECUR (TREE_OPERAND (op1, 1));
18839 }
18840 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18841 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18842 }
18843 break;
18844
18845 case TRANSACTION_EXPR:
18846 {
18847 int flags = 0;
18848 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18849 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18850
18851 if (TRANSACTION_EXPR_IS_STMT (t))
18852 {
18853 tree body = TRANSACTION_EXPR_BODY (t);
18854 tree noex = NULL_TREE;
18855 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18856 {
18857 noex = MUST_NOT_THROW_COND (body);
18858 if (noex == NULL_TREE)
18859 noex = boolean_true_node;
18860 body = TREE_OPERAND (body, 0);
18861 }
18862 stmt = begin_transaction_stmt (input_location, NULL, flags);
18863 RECUR (body);
18864 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18865 }
18866 else
18867 {
18868 stmt = build_transaction_expr (EXPR_LOCATION (t),
18869 RECUR (TRANSACTION_EXPR_BODY (t)),
18870 flags, NULL_TREE);
18871 RETURN (stmt);
18872 }
18873 }
18874 break;
18875
18876 case MUST_NOT_THROW_EXPR:
18877 {
18878 tree op0 = RECUR (TREE_OPERAND (t, 0));
18879 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18880 RETURN (build_must_not_throw_expr (op0, cond));
18881 }
18882
18883 case EXPR_PACK_EXPANSION:
18884 error ("invalid use of pack expansion expression");
18885 RETURN (error_mark_node);
18886
18887 case NONTYPE_ARGUMENT_PACK:
18888 error ("use %<...%> to expand argument pack");
18889 RETURN (error_mark_node);
18890
18891 case COMPOUND_EXPR:
18892 tmp = RECUR (TREE_OPERAND (t, 0));
18893 if (tmp == NULL_TREE)
18894 /* If the first operand was a statement, we're done with it. */
18895 RETURN (RECUR (TREE_OPERAND (t, 1)));
18896 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18897 RECUR (TREE_OPERAND (t, 1)),
18898 complain));
18899
18900 case ANNOTATE_EXPR:
18901 tmp = RECUR (TREE_OPERAND (t, 0));
18902 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18903 TREE_TYPE (tmp), tmp,
18904 RECUR (TREE_OPERAND (t, 1)),
18905 RECUR (TREE_OPERAND (t, 2))));
18906
18907 case PREDICT_EXPR:
18908 RETURN (add_stmt (copy_node (t)));
18909
18910 default:
18911 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18912
18913 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18914 /*function_p=*/false,
18915 integral_constant_expression_p));
18916 }
18917
18918 RETURN (NULL_TREE);
18919 out:
18920 input_location = loc;
18921 return r;
18922 #undef RECUR
18923 #undef RETURN
18924 }
18925
18926 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18927 function. For description of the body see comment above
18928 cp_parser_omp_declare_reduction_exprs. */
18929
18930 static void
18931 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18932 {
18933 if (t == NULL_TREE || t == error_mark_node)
18934 return;
18935
18936 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
18937
18938 tree_stmt_iterator tsi;
18939 int i;
18940 tree stmts[7];
18941 memset (stmts, 0, sizeof stmts);
18942 for (i = 0, tsi = tsi_start (t);
18943 i < 7 && !tsi_end_p (tsi);
18944 i++, tsi_next (&tsi))
18945 stmts[i] = tsi_stmt (tsi);
18946 gcc_assert (tsi_end_p (tsi));
18947
18948 if (i >= 3)
18949 {
18950 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18951 && TREE_CODE (stmts[1]) == DECL_EXPR);
18952 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18953 args, complain, in_decl);
18954 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18955 args, complain, in_decl);
18956 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
18957 expect to be pushing it. */
18958 DECL_CONTEXT (omp_out) = current_function_decl;
18959 DECL_CONTEXT (omp_in) = current_function_decl;
18960 keep_next_level (true);
18961 tree block = begin_omp_structured_block ();
18962 tsubst_expr (stmts[2], args, complain, in_decl, false);
18963 block = finish_omp_structured_block (block);
18964 block = maybe_cleanup_point_expr_void (block);
18965 add_decl_expr (omp_out);
18966 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18967 TREE_NO_WARNING (omp_out) = 1;
18968 add_decl_expr (omp_in);
18969 finish_expr_stmt (block);
18970 }
18971 if (i >= 6)
18972 {
18973 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18974 && TREE_CODE (stmts[4]) == DECL_EXPR);
18975 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18976 args, complain, in_decl);
18977 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18978 args, complain, in_decl);
18979 DECL_CONTEXT (omp_priv) = current_function_decl;
18980 DECL_CONTEXT (omp_orig) = current_function_decl;
18981 keep_next_level (true);
18982 tree block = begin_omp_structured_block ();
18983 tsubst_expr (stmts[5], args, complain, in_decl, false);
18984 block = finish_omp_structured_block (block);
18985 block = maybe_cleanup_point_expr_void (block);
18986 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18987 add_decl_expr (omp_priv);
18988 add_decl_expr (omp_orig);
18989 finish_expr_stmt (block);
18990 if (i == 7)
18991 add_decl_expr (omp_orig);
18992 }
18993 }
18994
18995 /* T is a postfix-expression that is not being used in a function
18996 call. Return the substituted version of T. */
18997
18998 static tree
18999 tsubst_non_call_postfix_expression (tree t, tree args,
19000 tsubst_flags_t complain,
19001 tree in_decl)
19002 {
19003 if (TREE_CODE (t) == SCOPE_REF)
19004 t = tsubst_qualified_id (t, args, complain, in_decl,
19005 /*done=*/false, /*address_p=*/false);
19006 else
19007 t = tsubst_copy_and_build (t, args, complain, in_decl,
19008 /*function_p=*/false,
19009 /*integral_constant_expression_p=*/false);
19010
19011 return t;
19012 }
19013
19014 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19015 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19016 dependent init-capture. */
19017
19018 static void
19019 prepend_one_capture (tree field, tree init, tree &list,
19020 tsubst_flags_t complain)
19021 {
19022 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19023 {
19024 tree type = NULL_TREE;
19025 if (!init)
19026 {
19027 if (complain & tf_error)
19028 error ("empty initializer in lambda init-capture");
19029 init = error_mark_node;
19030 }
19031 else if (TREE_CODE (init) == TREE_LIST)
19032 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19033 if (!type)
19034 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19035 TREE_TYPE (field) = type;
19036 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19037 }
19038 list = tree_cons (field, init, list);
19039 }
19040
19041 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19042 instantiation context. Instantiating a pack expansion containing a lambda
19043 might result in multiple lambdas all based on the same lambda in the
19044 template. */
19045
19046 tree
19047 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19048 {
19049 tree oldfn = lambda_function (t);
19050 in_decl = oldfn;
19051
19052 tree r = build_lambda_expr ();
19053
19054 LAMBDA_EXPR_LOCATION (r)
19055 = LAMBDA_EXPR_LOCATION (t);
19056 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19057 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19058 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19059 LAMBDA_EXPR_INSTANTIATED (r) = true;
19060
19061 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19062 /* A lambda in a default argument outside a class gets no
19063 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19064 tsubst_default_argument calls start_lambda_scope, so we need to
19065 specifically ignore it here, and use the global scope. */
19066 record_null_lambda_scope (r);
19067 else
19068 record_lambda_scope (r);
19069
19070 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19071 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19072
19073 vec<tree,va_gc>* field_packs = NULL;
19074
19075 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19076 cap = TREE_CHAIN (cap))
19077 {
19078 tree ofield = TREE_PURPOSE (cap);
19079 tree init = TREE_VALUE (cap);
19080 if (PACK_EXPANSION_P (init))
19081 init = tsubst_pack_expansion (init, args, complain, in_decl);
19082 else
19083 init = tsubst_copy_and_build (init, args, complain, in_decl,
19084 /*fn*/false, /*constexpr*/false);
19085
19086 if (init == error_mark_node)
19087 return error_mark_node;
19088
19089 if (init && TREE_CODE (init) == TREE_LIST)
19090 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19091
19092 if (!processing_template_decl
19093 && init && TREE_CODE (init) != TREE_VEC
19094 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19095 {
19096 /* For a VLA, simply tsubsting the field type won't work, we need to
19097 go through add_capture again. XXX do we want to do this for all
19098 captures? */
19099 tree name = (get_identifier
19100 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19101 tree ftype = TREE_TYPE (ofield);
19102 bool by_ref = (TYPE_REF_P (ftype)
19103 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19104 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19105 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19106 continue;
19107 }
19108
19109 if (PACK_EXPANSION_P (ofield))
19110 ofield = PACK_EXPANSION_PATTERN (ofield);
19111 tree field = tsubst_decl (ofield, args, complain);
19112
19113 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19114 {
19115 /* Remember these for when we've pushed local_specializations. */
19116 vec_safe_push (field_packs, ofield);
19117 vec_safe_push (field_packs, field);
19118 }
19119
19120 if (field == error_mark_node)
19121 return error_mark_node;
19122
19123 if (TREE_CODE (field) == TREE_VEC)
19124 {
19125 int len = TREE_VEC_LENGTH (field);
19126 gcc_assert (TREE_CODE (init) == TREE_VEC
19127 && TREE_VEC_LENGTH (init) == len);
19128 for (int i = 0; i < len; ++i)
19129 prepend_one_capture (TREE_VEC_ELT (field, i),
19130 TREE_VEC_ELT (init, i),
19131 LAMBDA_EXPR_CAPTURE_LIST (r),
19132 complain);
19133 }
19134 else
19135 {
19136 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19137 complain);
19138
19139 if (id_equal (DECL_NAME (field), "__this"))
19140 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19141 }
19142 }
19143
19144 tree type = begin_lambda_type (r);
19145 if (type == error_mark_node)
19146 return error_mark_node;
19147
19148 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19149 determine_visibility (TYPE_NAME (type));
19150
19151 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19152
19153 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19154 ? DECL_TI_TEMPLATE (oldfn)
19155 : NULL_TREE);
19156
19157 tree fntype = static_fn_type (oldfn);
19158 if (oldtmpl)
19159 ++processing_template_decl;
19160 fntype = tsubst (fntype, args, complain, in_decl);
19161 if (oldtmpl)
19162 --processing_template_decl;
19163
19164 if (fntype == error_mark_node)
19165 r = error_mark_node;
19166 else
19167 {
19168 /* The body of a lambda-expression is not a subexpression of the
19169 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19170 which would be skipped if cp_unevaluated_operand. */
19171 cp_evaluated ev;
19172
19173 /* Fix the type of 'this'. */
19174 fntype = build_memfn_type (fntype, type,
19175 type_memfn_quals (fntype),
19176 type_memfn_rqual (fntype));
19177 tree fn, tmpl;
19178 if (oldtmpl)
19179 {
19180 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19181 if (tmpl == error_mark_node)
19182 {
19183 r = error_mark_node;
19184 goto out;
19185 }
19186 fn = DECL_TEMPLATE_RESULT (tmpl);
19187 finish_member_declaration (tmpl);
19188 }
19189 else
19190 {
19191 tmpl = NULL_TREE;
19192 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19193 if (fn == error_mark_node)
19194 {
19195 r = error_mark_node;
19196 goto out;
19197 }
19198 finish_member_declaration (fn);
19199 }
19200
19201 if (tree ci = get_constraints (oldfn))
19202 {
19203 /* Substitute into the lambda's constraints. */
19204 if (oldtmpl)
19205 ++processing_template_decl;
19206 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19207 if (oldtmpl)
19208 --processing_template_decl;
19209 set_constraints (fn, ci);
19210 }
19211
19212 /* Let finish_function set this. */
19213 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19214
19215 bool nested = cfun;
19216 if (nested)
19217 push_function_context ();
19218 else
19219 /* Still increment function_depth so that we don't GC in the
19220 middle of an expression. */
19221 ++function_depth;
19222
19223 local_specialization_stack s (lss_copy);
19224
19225 tree body = start_lambda_function (fn, r);
19226
19227 /* Now record them for lookup_init_capture_pack. */
19228 int fplen = vec_safe_length (field_packs);
19229 for (int i = 0; i < fplen; )
19230 {
19231 tree pack = (*field_packs)[i++];
19232 tree inst = (*field_packs)[i++];
19233 register_local_specialization (inst, pack);
19234 }
19235 release_tree_vector (field_packs);
19236
19237 register_parameter_specializations (oldfn, fn);
19238
19239 if (oldtmpl)
19240 {
19241 /* We might not partially instantiate some parts of the function, so
19242 copy these flags from the original template. */
19243 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19244 current_function_returns_value = ol->returns_value;
19245 current_function_returns_null = ol->returns_null;
19246 current_function_returns_abnormally = ol->returns_abnormally;
19247 current_function_infinite_loop = ol->infinite_loop;
19248 }
19249
19250 /* [temp.deduct] A lambda-expression appearing in a function type or a
19251 template parameter is not considered part of the immediate context for
19252 the purposes of template argument deduction. */
19253 complain = tf_warning_or_error;
19254
19255 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19256 /*constexpr*/false);
19257
19258 finish_lambda_function (body);
19259
19260 if (nested)
19261 pop_function_context ();
19262 else
19263 --function_depth;
19264
19265 /* The capture list was built up in reverse order; fix that now. */
19266 LAMBDA_EXPR_CAPTURE_LIST (r)
19267 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19268
19269 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19270
19271 maybe_add_lambda_conv_op (type);
19272 }
19273
19274 out:
19275 finish_struct (type, /*attr*/NULL_TREE);
19276
19277 insert_pending_capture_proxies ();
19278
19279 return r;
19280 }
19281
19282 /* Like tsubst but deals with expressions and performs semantic
19283 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19284
19285 tree
19286 tsubst_copy_and_build (tree t,
19287 tree args,
19288 tsubst_flags_t complain,
19289 tree in_decl,
19290 bool function_p,
19291 bool integral_constant_expression_p)
19292 {
19293 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19294 #define RECUR(NODE) \
19295 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19296 /*function_p=*/false, \
19297 integral_constant_expression_p)
19298
19299 tree retval, op1;
19300 location_t save_loc;
19301
19302 if (t == NULL_TREE || t == error_mark_node)
19303 return t;
19304
19305 save_loc = input_location;
19306 if (location_t eloc = cp_expr_location (t))
19307 input_location = eloc;
19308
19309 /* N3276 decltype magic only applies to calls at the top level or on the
19310 right side of a comma. */
19311 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19312 complain &= ~tf_decltype;
19313
19314 switch (TREE_CODE (t))
19315 {
19316 case USING_DECL:
19317 t = DECL_NAME (t);
19318 /* Fall through. */
19319 case IDENTIFIER_NODE:
19320 {
19321 tree decl;
19322 cp_id_kind idk;
19323 bool non_integral_constant_expression_p;
19324 const char *error_msg;
19325
19326 if (IDENTIFIER_CONV_OP_P (t))
19327 {
19328 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19329 t = make_conv_op_name (new_type);
19330 }
19331
19332 /* Look up the name. */
19333 decl = lookup_name (t);
19334
19335 /* By convention, expressions use ERROR_MARK_NODE to indicate
19336 failure, not NULL_TREE. */
19337 if (decl == NULL_TREE)
19338 decl = error_mark_node;
19339
19340 decl = finish_id_expression (t, decl, NULL_TREE,
19341 &idk,
19342 integral_constant_expression_p,
19343 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19344 &non_integral_constant_expression_p,
19345 /*template_p=*/false,
19346 /*done=*/true,
19347 /*address_p=*/false,
19348 /*template_arg_p=*/false,
19349 &error_msg,
19350 input_location);
19351 if (error_msg)
19352 error (error_msg);
19353 if (!function_p && identifier_p (decl))
19354 {
19355 if (complain & tf_error)
19356 unqualified_name_lookup_error (decl);
19357 decl = error_mark_node;
19358 }
19359 RETURN (decl);
19360 }
19361
19362 case TEMPLATE_ID_EXPR:
19363 {
19364 tree object;
19365 tree templ = RECUR (TREE_OPERAND (t, 0));
19366 tree targs = TREE_OPERAND (t, 1);
19367
19368 if (targs)
19369 targs = tsubst_template_args (targs, args, complain, in_decl);
19370 if (targs == error_mark_node)
19371 RETURN (error_mark_node);
19372
19373 if (TREE_CODE (templ) == SCOPE_REF)
19374 {
19375 tree name = TREE_OPERAND (templ, 1);
19376 tree tid = lookup_template_function (name, targs);
19377 TREE_OPERAND (templ, 1) = tid;
19378 RETURN (templ);
19379 }
19380
19381 if (concept_definition_p (templ))
19382 {
19383 tree check = build_concept_check (templ, targs, complain);
19384 if (check == error_mark_node)
19385 RETURN (error_mark_node);
19386
19387 tree id = unpack_concept_check (check);
19388
19389 /* If we built a function concept check, return the underlying
19390 template-id. So we can evaluate it as a function call. */
19391 if (function_concept_p (TREE_OPERAND (id, 0)))
19392 RETURN (id);
19393
19394 RETURN (check);
19395 }
19396
19397 if (variable_template_p (templ))
19398 {
19399 tree r = lookup_and_finish_template_variable (templ, targs,
19400 complain);
19401 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19402 RETURN (r);
19403 }
19404
19405 if (TREE_CODE (templ) == COMPONENT_REF)
19406 {
19407 object = TREE_OPERAND (templ, 0);
19408 templ = TREE_OPERAND (templ, 1);
19409 }
19410 else
19411 object = NULL_TREE;
19412 templ = lookup_template_function (templ, targs);
19413
19414 if (object)
19415 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
19416 object, templ, NULL_TREE));
19417 else
19418 RETURN (baselink_for_fns (templ));
19419 }
19420
19421 case INDIRECT_REF:
19422 {
19423 tree r = RECUR (TREE_OPERAND (t, 0));
19424
19425 if (REFERENCE_REF_P (t))
19426 {
19427 /* A type conversion to reference type will be enclosed in
19428 such an indirect ref, but the substitution of the cast
19429 will have also added such an indirect ref. */
19430 r = convert_from_reference (r);
19431 }
19432 else
19433 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19434 complain|decltype_flag);
19435
19436 if (REF_PARENTHESIZED_P (t))
19437 r = force_paren_expr (r);
19438
19439 RETURN (r);
19440 }
19441
19442 case NOP_EXPR:
19443 {
19444 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19445 tree op0 = RECUR (TREE_OPERAND (t, 0));
19446 RETURN (build_nop (type, op0));
19447 }
19448
19449 case IMPLICIT_CONV_EXPR:
19450 {
19451 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19452 tree expr = RECUR (TREE_OPERAND (t, 0));
19453 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19454 {
19455 retval = copy_node (t);
19456 TREE_TYPE (retval) = type;
19457 TREE_OPERAND (retval, 0) = expr;
19458 RETURN (retval);
19459 }
19460 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19461 /* We'll pass this to convert_nontype_argument again, we don't need
19462 to actually perform any conversion here. */
19463 RETURN (expr);
19464 int flags = LOOKUP_IMPLICIT;
19465 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19466 flags = LOOKUP_NORMAL;
19467 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19468 flags |= LOOKUP_NO_NARROWING;
19469 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19470 flags));
19471 }
19472
19473 case CONVERT_EXPR:
19474 {
19475 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19476 tree op0 = RECUR (TREE_OPERAND (t, 0));
19477 if (op0 == error_mark_node)
19478 RETURN (error_mark_node);
19479 RETURN (build1 (CONVERT_EXPR, type, op0));
19480 }
19481
19482 case CAST_EXPR:
19483 case REINTERPRET_CAST_EXPR:
19484 case CONST_CAST_EXPR:
19485 case DYNAMIC_CAST_EXPR:
19486 case STATIC_CAST_EXPR:
19487 {
19488 tree type;
19489 tree op, r = NULL_TREE;
19490
19491 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19492 if (integral_constant_expression_p
19493 && !cast_valid_in_integral_constant_expression_p (type))
19494 {
19495 if (complain & tf_error)
19496 error ("a cast to a type other than an integral or "
19497 "enumeration type cannot appear in a constant-expression");
19498 RETURN (error_mark_node);
19499 }
19500
19501 op = RECUR (TREE_OPERAND (t, 0));
19502
19503 warning_sentinel s(warn_useless_cast);
19504 warning_sentinel s2(warn_ignored_qualifiers);
19505 switch (TREE_CODE (t))
19506 {
19507 case CAST_EXPR:
19508 r = build_functional_cast (input_location, type, op, complain);
19509 break;
19510 case REINTERPRET_CAST_EXPR:
19511 r = build_reinterpret_cast (input_location, type, op, complain);
19512 break;
19513 case CONST_CAST_EXPR:
19514 r = build_const_cast (input_location, type, op, complain);
19515 break;
19516 case DYNAMIC_CAST_EXPR:
19517 r = build_dynamic_cast (input_location, type, op, complain);
19518 break;
19519 case STATIC_CAST_EXPR:
19520 r = build_static_cast (input_location, type, op, complain);
19521 if (IMPLICIT_RVALUE_P (t))
19522 set_implicit_rvalue_p (r);
19523 break;
19524 default:
19525 gcc_unreachable ();
19526 }
19527
19528 RETURN (r);
19529 }
19530
19531 case POSTDECREMENT_EXPR:
19532 case POSTINCREMENT_EXPR:
19533 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19534 args, complain, in_decl);
19535 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19536 complain|decltype_flag));
19537
19538 case PREDECREMENT_EXPR:
19539 case PREINCREMENT_EXPR:
19540 case NEGATE_EXPR:
19541 case BIT_NOT_EXPR:
19542 case ABS_EXPR:
19543 case TRUTH_NOT_EXPR:
19544 case UNARY_PLUS_EXPR: /* Unary + */
19545 case REALPART_EXPR:
19546 case IMAGPART_EXPR:
19547 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19548 RECUR (TREE_OPERAND (t, 0)),
19549 complain|decltype_flag));
19550
19551 case FIX_TRUNC_EXPR:
19552 gcc_unreachable ();
19553
19554 case ADDR_EXPR:
19555 op1 = TREE_OPERAND (t, 0);
19556 if (TREE_CODE (op1) == LABEL_DECL)
19557 RETURN (finish_label_address_expr (DECL_NAME (op1),
19558 EXPR_LOCATION (op1)));
19559 if (TREE_CODE (op1) == SCOPE_REF)
19560 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19561 /*done=*/true, /*address_p=*/true);
19562 else
19563 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19564 in_decl);
19565 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19566 complain|decltype_flag));
19567
19568 case PLUS_EXPR:
19569 case MINUS_EXPR:
19570 case MULT_EXPR:
19571 case TRUNC_DIV_EXPR:
19572 case CEIL_DIV_EXPR:
19573 case FLOOR_DIV_EXPR:
19574 case ROUND_DIV_EXPR:
19575 case EXACT_DIV_EXPR:
19576 case BIT_AND_EXPR:
19577 case BIT_IOR_EXPR:
19578 case BIT_XOR_EXPR:
19579 case TRUNC_MOD_EXPR:
19580 case FLOOR_MOD_EXPR:
19581 case TRUTH_ANDIF_EXPR:
19582 case TRUTH_ORIF_EXPR:
19583 case TRUTH_AND_EXPR:
19584 case TRUTH_OR_EXPR:
19585 case RSHIFT_EXPR:
19586 case LSHIFT_EXPR:
19587 case EQ_EXPR:
19588 case NE_EXPR:
19589 case MAX_EXPR:
19590 case MIN_EXPR:
19591 case LE_EXPR:
19592 case GE_EXPR:
19593 case LT_EXPR:
19594 case GT_EXPR:
19595 case SPACESHIP_EXPR:
19596 case MEMBER_REF:
19597 case DOTSTAR_EXPR:
19598 {
19599 /* If T was type-dependent, suppress warnings that depend on the range
19600 of the types involved. */
19601 bool was_dep = type_dependent_expression_p_push (t);
19602
19603 tree op0 = RECUR (TREE_OPERAND (t, 0));
19604 tree op1 = RECUR (TREE_OPERAND (t, 1));
19605
19606 warning_sentinel s1(warn_type_limits, was_dep);
19607 warning_sentinel s2(warn_div_by_zero, was_dep);
19608 warning_sentinel s3(warn_logical_op, was_dep);
19609 warning_sentinel s4(warn_tautological_compare, was_dep);
19610
19611 tree r = build_x_binary_op
19612 (input_location, TREE_CODE (t),
19613 op0,
19614 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19615 ? ERROR_MARK
19616 : TREE_CODE (TREE_OPERAND (t, 0))),
19617 op1,
19618 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19619 ? ERROR_MARK
19620 : TREE_CODE (TREE_OPERAND (t, 1))),
19621 /*overload=*/NULL,
19622 complain|decltype_flag);
19623 if (EXPR_P (r) && TREE_NO_WARNING (t))
19624 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19625
19626 RETURN (r);
19627 }
19628
19629 case POINTER_PLUS_EXPR:
19630 {
19631 tree op0 = RECUR (TREE_OPERAND (t, 0));
19632 if (op0 == error_mark_node)
19633 RETURN (error_mark_node);
19634 tree op1 = RECUR (TREE_OPERAND (t, 1));
19635 if (op1 == error_mark_node)
19636 RETURN (error_mark_node);
19637 RETURN (fold_build_pointer_plus (op0, op1));
19638 }
19639
19640 case SCOPE_REF:
19641 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19642 /*address_p=*/false));
19643 case ARRAY_REF:
19644 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19645 args, complain, in_decl);
19646 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19647 RECUR (TREE_OPERAND (t, 1)),
19648 complain|decltype_flag));
19649
19650 case SIZEOF_EXPR:
19651 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19652 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19653 RETURN (tsubst_copy (t, args, complain, in_decl));
19654 /* Fall through */
19655
19656 case ALIGNOF_EXPR:
19657 {
19658 tree r;
19659
19660 op1 = TREE_OPERAND (t, 0);
19661 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19662 op1 = TREE_TYPE (op1);
19663 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19664 && ALIGNOF_EXPR_STD_P (t));
19665 if (!args)
19666 {
19667 /* When there are no ARGS, we are trying to evaluate a
19668 non-dependent expression from the parser. Trying to do
19669 the substitutions may not work. */
19670 if (!TYPE_P (op1))
19671 op1 = TREE_TYPE (op1);
19672 }
19673 else
19674 {
19675 ++cp_unevaluated_operand;
19676 ++c_inhibit_evaluation_warnings;
19677 if (TYPE_P (op1))
19678 op1 = tsubst (op1, args, complain, in_decl);
19679 else
19680 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19681 /*function_p=*/false,
19682 /*integral_constant_expression_p=*/
19683 false);
19684 --cp_unevaluated_operand;
19685 --c_inhibit_evaluation_warnings;
19686 }
19687 if (TYPE_P (op1))
19688 r = cxx_sizeof_or_alignof_type (input_location,
19689 op1, TREE_CODE (t), std_alignof,
19690 complain & tf_error);
19691 else
19692 r = cxx_sizeof_or_alignof_expr (input_location,
19693 op1, TREE_CODE (t),
19694 complain & tf_error);
19695 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19696 {
19697 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19698 {
19699 if (!processing_template_decl && TYPE_P (op1))
19700 {
19701 r = build_min (SIZEOF_EXPR, size_type_node,
19702 build1 (NOP_EXPR, op1, error_mark_node));
19703 SIZEOF_EXPR_TYPE_P (r) = 1;
19704 }
19705 else
19706 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19707 TREE_SIDE_EFFECTS (r) = 0;
19708 TREE_READONLY (r) = 1;
19709 }
19710 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19711 }
19712 RETURN (r);
19713 }
19714
19715 case AT_ENCODE_EXPR:
19716 {
19717 op1 = TREE_OPERAND (t, 0);
19718 ++cp_unevaluated_operand;
19719 ++c_inhibit_evaluation_warnings;
19720 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19721 /*function_p=*/false,
19722 /*integral_constant_expression_p=*/false);
19723 --cp_unevaluated_operand;
19724 --c_inhibit_evaluation_warnings;
19725 RETURN (objc_build_encode_expr (op1));
19726 }
19727
19728 case NOEXCEPT_EXPR:
19729 op1 = TREE_OPERAND (t, 0);
19730 ++cp_unevaluated_operand;
19731 ++c_inhibit_evaluation_warnings;
19732 ++cp_noexcept_operand;
19733 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19734 /*function_p=*/false,
19735 /*integral_constant_expression_p=*/false);
19736 --cp_unevaluated_operand;
19737 --c_inhibit_evaluation_warnings;
19738 --cp_noexcept_operand;
19739 RETURN (finish_noexcept_expr (op1, complain));
19740
19741 case MODOP_EXPR:
19742 {
19743 warning_sentinel s(warn_div_by_zero);
19744 tree lhs = RECUR (TREE_OPERAND (t, 0));
19745 tree rhs = RECUR (TREE_OPERAND (t, 2));
19746 tree r = build_x_modify_expr
19747 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19748 complain|decltype_flag);
19749 /* TREE_NO_WARNING must be set if either the expression was
19750 parenthesized or it uses an operator such as >>= rather
19751 than plain assignment. In the former case, it was already
19752 set and must be copied. In the latter case,
19753 build_x_modify_expr sets it and it must not be reset
19754 here. */
19755 if (TREE_NO_WARNING (t))
19756 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19757
19758 RETURN (r);
19759 }
19760
19761 case ARROW_EXPR:
19762 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19763 args, complain, in_decl);
19764 /* Remember that there was a reference to this entity. */
19765 if (DECL_P (op1)
19766 && !mark_used (op1, complain) && !(complain & tf_error))
19767 RETURN (error_mark_node);
19768 RETURN (build_x_arrow (input_location, op1, complain));
19769
19770 case NEW_EXPR:
19771 {
19772 tree placement = RECUR (TREE_OPERAND (t, 0));
19773 tree init = RECUR (TREE_OPERAND (t, 3));
19774 vec<tree, va_gc> *placement_vec;
19775 vec<tree, va_gc> *init_vec;
19776 tree ret;
19777 location_t loc = EXPR_LOCATION (t);
19778
19779 if (placement == NULL_TREE)
19780 placement_vec = NULL;
19781 else if (placement == error_mark_node)
19782 RETURN (error_mark_node);
19783 else
19784 {
19785 placement_vec = make_tree_vector ();
19786 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19787 vec_safe_push (placement_vec, TREE_VALUE (placement));
19788 }
19789
19790 /* If there was an initializer in the original tree, but it
19791 instantiated to an empty list, then we should pass a
19792 non-NULL empty vector to tell build_new that it was an
19793 empty initializer() rather than no initializer. This can
19794 only happen when the initializer is a pack expansion whose
19795 parameter packs are of length zero. */
19796 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19797 init_vec = NULL;
19798 else
19799 {
19800 init_vec = make_tree_vector ();
19801 if (init == void_node)
19802 gcc_assert (init_vec != NULL);
19803 else
19804 {
19805 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19806 vec_safe_push (init_vec, TREE_VALUE (init));
19807 }
19808 }
19809
19810 /* Avoid passing an enclosing decl to valid_array_size_p. */
19811 in_decl = NULL_TREE;
19812
19813 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19814 tree op2 = RECUR (TREE_OPERAND (t, 2));
19815 ret = build_new (loc, &placement_vec, op1, op2,
19816 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19817 complain);
19818
19819 if (placement_vec != NULL)
19820 release_tree_vector (placement_vec);
19821 if (init_vec != NULL)
19822 release_tree_vector (init_vec);
19823
19824 RETURN (ret);
19825 }
19826
19827 case DELETE_EXPR:
19828 {
19829 tree op0 = RECUR (TREE_OPERAND (t, 0));
19830 tree op1 = RECUR (TREE_OPERAND (t, 1));
19831 RETURN (delete_sanity (input_location, op0, op1,
19832 DELETE_EXPR_USE_VEC (t),
19833 DELETE_EXPR_USE_GLOBAL (t),
19834 complain));
19835 }
19836
19837 case COMPOUND_EXPR:
19838 {
19839 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19840 complain & ~tf_decltype, in_decl,
19841 /*function_p=*/false,
19842 integral_constant_expression_p);
19843 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19844 op0,
19845 RECUR (TREE_OPERAND (t, 1)),
19846 complain|decltype_flag));
19847 }
19848
19849 case CALL_EXPR:
19850 {
19851 tree function;
19852 unsigned int nargs, i;
19853 bool qualified_p;
19854 bool koenig_p;
19855 tree ret;
19856
19857 function = CALL_EXPR_FN (t);
19858 /* Internal function with no arguments. */
19859 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19860 RETURN (t);
19861
19862 /* When we parsed the expression, we determined whether or
19863 not Koenig lookup should be performed. */
19864 koenig_p = KOENIG_LOOKUP_P (t);
19865 if (function == NULL_TREE)
19866 {
19867 koenig_p = false;
19868 qualified_p = false;
19869 }
19870 else if (TREE_CODE (function) == SCOPE_REF)
19871 {
19872 qualified_p = true;
19873 function = tsubst_qualified_id (function, args, complain, in_decl,
19874 /*done=*/false,
19875 /*address_p=*/false);
19876 }
19877 else if (koenig_p && identifier_p (function))
19878 {
19879 /* Do nothing; calling tsubst_copy_and_build on an identifier
19880 would incorrectly perform unqualified lookup again.
19881
19882 Note that we can also have an IDENTIFIER_NODE if the earlier
19883 unqualified lookup found a member function; in that case
19884 koenig_p will be false and we do want to do the lookup
19885 again to find the instantiated member function.
19886
19887 FIXME but doing that causes c++/15272, so we need to stop
19888 using IDENTIFIER_NODE in that situation. */
19889 qualified_p = false;
19890 }
19891 else
19892 {
19893 if (TREE_CODE (function) == COMPONENT_REF)
19894 {
19895 tree op = TREE_OPERAND (function, 1);
19896
19897 qualified_p = (TREE_CODE (op) == SCOPE_REF
19898 || (BASELINK_P (op)
19899 && BASELINK_QUALIFIED_P (op)));
19900 }
19901 else
19902 qualified_p = false;
19903
19904 if (TREE_CODE (function) == ADDR_EXPR
19905 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19906 /* Avoid error about taking the address of a constructor. */
19907 function = TREE_OPERAND (function, 0);
19908
19909 function = tsubst_copy_and_build (function, args, complain,
19910 in_decl,
19911 !qualified_p,
19912 integral_constant_expression_p);
19913
19914 if (BASELINK_P (function))
19915 qualified_p = true;
19916 }
19917
19918 nargs = call_expr_nargs (t);
19919 releasing_vec call_args;
19920 for (i = 0; i < nargs; ++i)
19921 {
19922 tree arg = CALL_EXPR_ARG (t, i);
19923
19924 if (!PACK_EXPANSION_P (arg))
19925 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19926 else
19927 {
19928 /* Expand the pack expansion and push each entry onto
19929 CALL_ARGS. */
19930 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19931 if (TREE_CODE (arg) == TREE_VEC)
19932 {
19933 unsigned int len, j;
19934
19935 len = TREE_VEC_LENGTH (arg);
19936 for (j = 0; j < len; ++j)
19937 {
19938 tree value = TREE_VEC_ELT (arg, j);
19939 if (value != NULL_TREE)
19940 value = convert_from_reference (value);
19941 vec_safe_push (call_args, value);
19942 }
19943 }
19944 else
19945 {
19946 /* A partial substitution. Add one entry. */
19947 vec_safe_push (call_args, arg);
19948 }
19949 }
19950 }
19951
19952 /* Stripped-down processing for a call in a thunk. Specifically, in
19953 the thunk template for a generic lambda. */
19954 if (CALL_FROM_THUNK_P (t))
19955 {
19956 /* Now that we've expanded any packs, the number of call args
19957 might be different. */
19958 unsigned int cargs = call_args->length ();
19959 tree thisarg = NULL_TREE;
19960 if (TREE_CODE (function) == COMPONENT_REF)
19961 {
19962 thisarg = TREE_OPERAND (function, 0);
19963 if (TREE_CODE (thisarg) == INDIRECT_REF)
19964 thisarg = TREE_OPERAND (thisarg, 0);
19965 function = TREE_OPERAND (function, 1);
19966 if (TREE_CODE (function) == BASELINK)
19967 function = BASELINK_FUNCTIONS (function);
19968 }
19969 /* We aren't going to do normal overload resolution, so force the
19970 template-id to resolve. */
19971 function = resolve_nondeduced_context (function, complain);
19972 for (unsigned i = 0; i < cargs; ++i)
19973 {
19974 /* In a thunk, pass through args directly, without any
19975 conversions. */
19976 tree arg = (*call_args)[i];
19977 while (TREE_CODE (arg) != PARM_DECL)
19978 arg = TREE_OPERAND (arg, 0);
19979 (*call_args)[i] = arg;
19980 }
19981 if (thisarg)
19982 {
19983 /* If there are no other args, just push 'this'. */
19984 if (cargs == 0)
19985 vec_safe_push (call_args, thisarg);
19986 else
19987 {
19988 /* Otherwise, shift the other args over to make room. */
19989 tree last = (*call_args)[cargs - 1];
19990 vec_safe_push (call_args, last);
19991 for (int i = cargs - 1; i > 0; --i)
19992 (*call_args)[i] = (*call_args)[i - 1];
19993 (*call_args)[0] = thisarg;
19994 }
19995 }
19996 ret = build_call_a (function, call_args->length (),
19997 call_args->address ());
19998 /* The thunk location is not interesting. */
19999 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20000 CALL_FROM_THUNK_P (ret) = true;
20001 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20002 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20003
20004 RETURN (ret);
20005 }
20006
20007 /* We do not perform argument-dependent lookup if normal
20008 lookup finds a non-function, in accordance with the
20009 expected resolution of DR 218. */
20010 if (koenig_p
20011 && ((is_overloaded_fn (function)
20012 /* If lookup found a member function, the Koenig lookup is
20013 not appropriate, even if an unqualified-name was used
20014 to denote the function. */
20015 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20016 || identifier_p (function))
20017 /* Only do this when substitution turns a dependent call
20018 into a non-dependent call. */
20019 && type_dependent_expression_p_push (t)
20020 && !any_type_dependent_arguments_p (call_args))
20021 function = perform_koenig_lookup (function, call_args, tf_none);
20022
20023 if (function != NULL_TREE
20024 && identifier_p (function)
20025 && !any_type_dependent_arguments_p (call_args))
20026 {
20027 if (koenig_p && (complain & tf_warning_or_error))
20028 {
20029 /* For backwards compatibility and good diagnostics, try
20030 the unqualified lookup again if we aren't in SFINAE
20031 context. */
20032 tree unq = (tsubst_copy_and_build
20033 (function, args, complain, in_decl, true,
20034 integral_constant_expression_p));
20035 if (unq == error_mark_node)
20036 RETURN (error_mark_node);
20037
20038 if (unq != function)
20039 {
20040 /* In a lambda fn, we have to be careful to not
20041 introduce new this captures. Legacy code can't
20042 be using lambdas anyway, so it's ok to be
20043 stricter. */
20044 bool in_lambda = (current_class_type
20045 && LAMBDA_TYPE_P (current_class_type));
20046 char const *const msg
20047 = G_("%qD was not declared in this scope, "
20048 "and no declarations were found by "
20049 "argument-dependent lookup at the point "
20050 "of instantiation");
20051
20052 bool diag = true;
20053 if (in_lambda)
20054 error_at (cp_expr_loc_or_input_loc (t),
20055 msg, function);
20056 else
20057 diag = permerror (cp_expr_loc_or_input_loc (t),
20058 msg, function);
20059 if (diag)
20060 {
20061 tree fn = unq;
20062
20063 if (INDIRECT_REF_P (fn))
20064 fn = TREE_OPERAND (fn, 0);
20065 if (is_overloaded_fn (fn))
20066 fn = get_first_fn (fn);
20067
20068 if (!DECL_P (fn))
20069 /* Can't say anything more. */;
20070 else if (DECL_CLASS_SCOPE_P (fn))
20071 {
20072 location_t loc = cp_expr_loc_or_input_loc (t);
20073 inform (loc,
20074 "declarations in dependent base %qT are "
20075 "not found by unqualified lookup",
20076 DECL_CLASS_CONTEXT (fn));
20077 if (current_class_ptr)
20078 inform (loc,
20079 "use %<this->%D%> instead", function);
20080 else
20081 inform (loc,
20082 "use %<%T::%D%> instead",
20083 current_class_name, function);
20084 }
20085 else
20086 inform (DECL_SOURCE_LOCATION (fn),
20087 "%qD declared here, later in the "
20088 "translation unit", fn);
20089 if (in_lambda)
20090 RETURN (error_mark_node);
20091 }
20092
20093 function = unq;
20094 }
20095 }
20096 if (identifier_p (function))
20097 {
20098 if (complain & tf_error)
20099 unqualified_name_lookup_error (function);
20100 RETURN (error_mark_node);
20101 }
20102 }
20103
20104 /* Remember that there was a reference to this entity. */
20105 if (function != NULL_TREE
20106 && DECL_P (function)
20107 && !mark_used (function, complain) && !(complain & tf_error))
20108 RETURN (error_mark_node);
20109
20110 /* Put back tf_decltype for the actual call. */
20111 complain |= decltype_flag;
20112
20113 if (function == NULL_TREE)
20114 switch (CALL_EXPR_IFN (t))
20115 {
20116 case IFN_LAUNDER:
20117 gcc_assert (nargs == 1);
20118 if (vec_safe_length (call_args) != 1)
20119 {
20120 error_at (cp_expr_loc_or_input_loc (t),
20121 "wrong number of arguments to "
20122 "%<__builtin_launder%>");
20123 ret = error_mark_node;
20124 }
20125 else
20126 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20127 (*call_args)[0], complain);
20128 break;
20129
20130 case IFN_VEC_CONVERT:
20131 gcc_assert (nargs == 1);
20132 if (vec_safe_length (call_args) != 1)
20133 {
20134 error_at (cp_expr_loc_or_input_loc (t),
20135 "wrong number of arguments to "
20136 "%<__builtin_convertvector%>");
20137 ret = error_mark_node;
20138 break;
20139 }
20140 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20141 tsubst (TREE_TYPE (t), args,
20142 complain, in_decl),
20143 complain);
20144 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20145 RETURN (ret);
20146 break;
20147
20148 default:
20149 /* Unsupported internal function with arguments. */
20150 gcc_unreachable ();
20151 }
20152 else if (TREE_CODE (function) == OFFSET_REF
20153 || TREE_CODE (function) == DOTSTAR_EXPR
20154 || TREE_CODE (function) == MEMBER_REF)
20155 ret = build_offset_ref_call_from_tree (function, &call_args,
20156 complain);
20157 else if (TREE_CODE (function) == COMPONENT_REF)
20158 {
20159 tree instance = TREE_OPERAND (function, 0);
20160 tree fn = TREE_OPERAND (function, 1);
20161
20162 if (processing_template_decl
20163 && (type_dependent_expression_p (instance)
20164 || (!BASELINK_P (fn)
20165 && TREE_CODE (fn) != FIELD_DECL)
20166 || type_dependent_expression_p (fn)
20167 || any_type_dependent_arguments_p (call_args)))
20168 ret = build_min_nt_call_vec (function, call_args);
20169 else if (!BASELINK_P (fn))
20170 ret = finish_call_expr (function, &call_args,
20171 /*disallow_virtual=*/false,
20172 /*koenig_p=*/false,
20173 complain);
20174 else
20175 ret = (build_new_method_call
20176 (instance, fn,
20177 &call_args, NULL_TREE,
20178 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20179 /*fn_p=*/NULL,
20180 complain));
20181 }
20182 else if (concept_check_p (function))
20183 {
20184 /* FUNCTION is a template-id referring to a concept definition. */
20185 tree id = unpack_concept_check (function);
20186 tree tmpl = TREE_OPERAND (id, 0);
20187 tree args = TREE_OPERAND (id, 1);
20188
20189 /* Calls to standard and variable concepts should have been
20190 previously diagnosed. */
20191 gcc_assert (function_concept_p (tmpl));
20192
20193 /* Ensure the result is wrapped as a call expression. */
20194 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20195 }
20196 else
20197 ret = finish_call_expr (function, &call_args,
20198 /*disallow_virtual=*/qualified_p,
20199 koenig_p,
20200 complain);
20201
20202 if (ret != error_mark_node)
20203 {
20204 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20205 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20206 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20207 if (op || ord || rev)
20208 {
20209 function = extract_call_expr (ret);
20210 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20211 CALL_EXPR_ORDERED_ARGS (function) = ord;
20212 CALL_EXPR_REVERSE_ARGS (function) = rev;
20213 }
20214 }
20215
20216 RETURN (ret);
20217 }
20218
20219 case COND_EXPR:
20220 {
20221 tree cond = RECUR (TREE_OPERAND (t, 0));
20222 cond = mark_rvalue_use (cond);
20223 tree folded_cond = fold_non_dependent_expr (cond, complain);
20224 tree exp1, exp2;
20225
20226 if (TREE_CODE (folded_cond) == INTEGER_CST)
20227 {
20228 if (integer_zerop (folded_cond))
20229 {
20230 ++c_inhibit_evaluation_warnings;
20231 exp1 = RECUR (TREE_OPERAND (t, 1));
20232 --c_inhibit_evaluation_warnings;
20233 exp2 = RECUR (TREE_OPERAND (t, 2));
20234 }
20235 else
20236 {
20237 exp1 = RECUR (TREE_OPERAND (t, 1));
20238 ++c_inhibit_evaluation_warnings;
20239 exp2 = RECUR (TREE_OPERAND (t, 2));
20240 --c_inhibit_evaluation_warnings;
20241 }
20242 cond = folded_cond;
20243 }
20244 else
20245 {
20246 exp1 = RECUR (TREE_OPERAND (t, 1));
20247 exp2 = RECUR (TREE_OPERAND (t, 2));
20248 }
20249
20250 warning_sentinel s(warn_duplicated_branches);
20251 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20252 cond, exp1, exp2, complain));
20253 }
20254
20255 case PSEUDO_DTOR_EXPR:
20256 {
20257 tree op0 = RECUR (TREE_OPERAND (t, 0));
20258 tree op1 = RECUR (TREE_OPERAND (t, 1));
20259 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20260 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20261 input_location));
20262 }
20263
20264 case TREE_LIST:
20265 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20266
20267 case COMPONENT_REF:
20268 {
20269 tree object;
20270 tree object_type;
20271 tree member;
20272 tree r;
20273
20274 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20275 args, complain, in_decl);
20276 /* Remember that there was a reference to this entity. */
20277 if (DECL_P (object)
20278 && !mark_used (object, complain) && !(complain & tf_error))
20279 RETURN (error_mark_node);
20280 object_type = TREE_TYPE (object);
20281
20282 member = TREE_OPERAND (t, 1);
20283 if (BASELINK_P (member))
20284 member = tsubst_baselink (member,
20285 non_reference (TREE_TYPE (object)),
20286 args, complain, in_decl);
20287 else
20288 member = tsubst_copy (member, args, complain, in_decl);
20289 if (member == error_mark_node)
20290 RETURN (error_mark_node);
20291
20292 if (TREE_CODE (member) == FIELD_DECL)
20293 {
20294 r = finish_non_static_data_member (member, object, NULL_TREE);
20295 if (TREE_CODE (r) == COMPONENT_REF)
20296 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20297 RETURN (r);
20298 }
20299 else if (type_dependent_expression_p (object))
20300 /* We can't do much here. */;
20301 else if (!CLASS_TYPE_P (object_type))
20302 {
20303 if (scalarish_type_p (object_type))
20304 {
20305 tree s = NULL_TREE;
20306 tree dtor = member;
20307
20308 if (TREE_CODE (dtor) == SCOPE_REF)
20309 {
20310 s = TREE_OPERAND (dtor, 0);
20311 dtor = TREE_OPERAND (dtor, 1);
20312 }
20313 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20314 {
20315 dtor = TREE_OPERAND (dtor, 0);
20316 if (TYPE_P (dtor))
20317 RETURN (finish_pseudo_destructor_expr
20318 (object, s, dtor, input_location));
20319 }
20320 }
20321 }
20322 else if (TREE_CODE (member) == SCOPE_REF
20323 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20324 {
20325 /* Lookup the template functions now that we know what the
20326 scope is. */
20327 tree scope = TREE_OPERAND (member, 0);
20328 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20329 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20330 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20331 /*complain=*/false);
20332 if (BASELINK_P (member))
20333 {
20334 BASELINK_FUNCTIONS (member)
20335 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20336 args);
20337 member = (adjust_result_of_qualified_name_lookup
20338 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20339 object_type));
20340 }
20341 else
20342 {
20343 qualified_name_lookup_error (scope, tmpl, member,
20344 input_location);
20345 RETURN (error_mark_node);
20346 }
20347 }
20348 else if (TREE_CODE (member) == SCOPE_REF
20349 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20350 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20351 {
20352 if (complain & tf_error)
20353 {
20354 if (TYPE_P (TREE_OPERAND (member, 0)))
20355 error ("%qT is not a class or namespace",
20356 TREE_OPERAND (member, 0));
20357 else
20358 error ("%qD is not a class or namespace",
20359 TREE_OPERAND (member, 0));
20360 }
20361 RETURN (error_mark_node);
20362 }
20363
20364 r = finish_class_member_access_expr (object, member,
20365 /*template_p=*/false,
20366 complain);
20367 if (TREE_CODE (r) == COMPONENT_REF)
20368 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20369 RETURN (r);
20370 }
20371
20372 case THROW_EXPR:
20373 RETURN (build_throw
20374 (input_location, RECUR (TREE_OPERAND (t, 0))));
20375
20376 case CONSTRUCTOR:
20377 {
20378 vec<constructor_elt, va_gc> *n;
20379 constructor_elt *ce;
20380 unsigned HOST_WIDE_INT idx;
20381 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20382 bool process_index_p;
20383 int newlen;
20384 bool need_copy_p = false;
20385 tree r;
20386
20387 if (type == error_mark_node)
20388 RETURN (error_mark_node);
20389
20390 /* We do not want to process the index of aggregate
20391 initializers as they are identifier nodes which will be
20392 looked up by digest_init. */
20393 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20394
20395 if (null_member_pointer_value_p (t))
20396 {
20397 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20398 RETURN (t);
20399 }
20400
20401 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20402 newlen = vec_safe_length (n);
20403 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20404 {
20405 if (ce->index && process_index_p
20406 /* An identifier index is looked up in the type
20407 being initialized, not the current scope. */
20408 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20409 ce->index = RECUR (ce->index);
20410
20411 if (PACK_EXPANSION_P (ce->value))
20412 {
20413 /* Substitute into the pack expansion. */
20414 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20415 in_decl);
20416
20417 if (ce->value == error_mark_node
20418 || PACK_EXPANSION_P (ce->value))
20419 ;
20420 else if (TREE_VEC_LENGTH (ce->value) == 1)
20421 /* Just move the argument into place. */
20422 ce->value = TREE_VEC_ELT (ce->value, 0);
20423 else
20424 {
20425 /* Update the length of the final CONSTRUCTOR
20426 arguments vector, and note that we will need to
20427 copy.*/
20428 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20429 need_copy_p = true;
20430 }
20431 }
20432 else
20433 ce->value = RECUR (ce->value);
20434 }
20435
20436 if (need_copy_p)
20437 {
20438 vec<constructor_elt, va_gc> *old_n = n;
20439
20440 vec_alloc (n, newlen);
20441 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20442 {
20443 if (TREE_CODE (ce->value) == TREE_VEC)
20444 {
20445 int i, len = TREE_VEC_LENGTH (ce->value);
20446 for (i = 0; i < len; ++i)
20447 CONSTRUCTOR_APPEND_ELT (n, 0,
20448 TREE_VEC_ELT (ce->value, i));
20449 }
20450 else
20451 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20452 }
20453 }
20454
20455 r = build_constructor (init_list_type_node, n);
20456 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20457 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20458 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20459
20460 if (TREE_HAS_CONSTRUCTOR (t))
20461 {
20462 fcl_t cl = fcl_functional;
20463 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20464 cl = fcl_c99;
20465 RETURN (finish_compound_literal (type, r, complain, cl));
20466 }
20467
20468 TREE_TYPE (r) = type;
20469 RETURN (r);
20470 }
20471
20472 case TYPEID_EXPR:
20473 {
20474 tree operand_0 = TREE_OPERAND (t, 0);
20475 if (TYPE_P (operand_0))
20476 {
20477 operand_0 = tsubst (operand_0, args, complain, in_decl);
20478 RETURN (get_typeid (operand_0, complain));
20479 }
20480 else
20481 {
20482 operand_0 = RECUR (operand_0);
20483 RETURN (build_typeid (operand_0, complain));
20484 }
20485 }
20486
20487 case VAR_DECL:
20488 if (!args)
20489 RETURN (t);
20490 /* Fall through */
20491
20492 case PARM_DECL:
20493 {
20494 tree r = tsubst_copy (t, args, complain, in_decl);
20495 /* ??? We're doing a subset of finish_id_expression here. */
20496 if (tree wrap = maybe_get_tls_wrapper_call (r))
20497 /* Replace an evaluated use of the thread_local variable with
20498 a call to its wrapper. */
20499 r = wrap;
20500 else if (outer_automatic_var_p (r))
20501 r = process_outer_var_ref (r, complain);
20502
20503 if (!TYPE_REF_P (TREE_TYPE (t)))
20504 /* If the original type was a reference, we'll be wrapped in
20505 the appropriate INDIRECT_REF. */
20506 r = convert_from_reference (r);
20507 RETURN (r);
20508 }
20509
20510 case VA_ARG_EXPR:
20511 {
20512 tree op0 = RECUR (TREE_OPERAND (t, 0));
20513 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20514 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20515 }
20516
20517 case OFFSETOF_EXPR:
20518 {
20519 tree object_ptr
20520 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20521 in_decl, /*function_p=*/false,
20522 /*integral_constant_expression_p=*/false);
20523 RETURN (finish_offsetof (object_ptr,
20524 RECUR (TREE_OPERAND (t, 0)),
20525 EXPR_LOCATION (t)));
20526 }
20527
20528 case ADDRESSOF_EXPR:
20529 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20530 RECUR (TREE_OPERAND (t, 0)), complain));
20531
20532 case TRAIT_EXPR:
20533 {
20534 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20535 complain, in_decl);
20536 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20537 complain, in_decl);
20538 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20539 TRAIT_EXPR_KIND (t), type1, type2));
20540 }
20541
20542 case STMT_EXPR:
20543 {
20544 tree old_stmt_expr = cur_stmt_expr;
20545 tree stmt_expr = begin_stmt_expr ();
20546
20547 cur_stmt_expr = stmt_expr;
20548 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20549 integral_constant_expression_p);
20550 stmt_expr = finish_stmt_expr (stmt_expr, false);
20551 cur_stmt_expr = old_stmt_expr;
20552
20553 /* If the resulting list of expression statement is empty,
20554 fold it further into void_node. */
20555 if (empty_expr_stmt_p (stmt_expr))
20556 stmt_expr = void_node;
20557
20558 RETURN (stmt_expr);
20559 }
20560
20561 case LAMBDA_EXPR:
20562 {
20563 if (complain & tf_partial)
20564 {
20565 /* We don't have a full set of template arguments yet; don't touch
20566 the lambda at all. */
20567 gcc_assert (processing_template_decl);
20568 return t;
20569 }
20570 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20571
20572 RETURN (build_lambda_object (r));
20573 }
20574
20575 case TARGET_EXPR:
20576 /* We can get here for a constant initializer of non-dependent type.
20577 FIXME stop folding in cp_parser_initializer_clause. */
20578 {
20579 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20580 complain);
20581 RETURN (r);
20582 }
20583
20584 case TRANSACTION_EXPR:
20585 RETURN (tsubst_expr(t, args, complain, in_decl,
20586 integral_constant_expression_p));
20587
20588 case PAREN_EXPR:
20589 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20590
20591 case VEC_PERM_EXPR:
20592 {
20593 tree op0 = RECUR (TREE_OPERAND (t, 0));
20594 tree op1 = RECUR (TREE_OPERAND (t, 1));
20595 tree op2 = RECUR (TREE_OPERAND (t, 2));
20596 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20597 complain));
20598 }
20599
20600 case REQUIRES_EXPR:
20601 {
20602 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20603 RETURN (r);
20604 }
20605
20606 case RANGE_EXPR:
20607 /* No need to substitute further, a RANGE_EXPR will always be built
20608 with constant operands. */
20609 RETURN (t);
20610
20611 case NON_LVALUE_EXPR:
20612 case VIEW_CONVERT_EXPR:
20613 if (location_wrapper_p (t))
20614 /* We need to do this here as well as in tsubst_copy so we get the
20615 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20616 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20617 EXPR_LOCATION (t)));
20618 /* fallthrough. */
20619
20620 default:
20621 /* Handle Objective-C++ constructs, if appropriate. */
20622 {
20623 tree subst
20624 = objcp_tsubst_copy_and_build (t, args, complain,
20625 in_decl, /*function_p=*/false);
20626 if (subst)
20627 RETURN (subst);
20628 }
20629 RETURN (tsubst_copy (t, args, complain, in_decl));
20630 }
20631
20632 #undef RECUR
20633 #undef RETURN
20634 out:
20635 input_location = save_loc;
20636 return retval;
20637 }
20638
20639 /* Verify that the instantiated ARGS are valid. For type arguments,
20640 make sure that the type's linkage is ok. For non-type arguments,
20641 make sure they are constants if they are integral or enumerations.
20642 Emit an error under control of COMPLAIN, and return TRUE on error. */
20643
20644 static bool
20645 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20646 {
20647 if (dependent_template_arg_p (t))
20648 return false;
20649 if (ARGUMENT_PACK_P (t))
20650 {
20651 tree vec = ARGUMENT_PACK_ARGS (t);
20652 int len = TREE_VEC_LENGTH (vec);
20653 bool result = false;
20654 int i;
20655
20656 for (i = 0; i < len; ++i)
20657 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20658 result = true;
20659 return result;
20660 }
20661 else if (TYPE_P (t))
20662 {
20663 /* [basic.link]: A name with no linkage (notably, the name
20664 of a class or enumeration declared in a local scope)
20665 shall not be used to declare an entity with linkage.
20666 This implies that names with no linkage cannot be used as
20667 template arguments
20668
20669 DR 757 relaxes this restriction for C++0x. */
20670 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20671 : no_linkage_check (t, /*relaxed_p=*/false));
20672
20673 if (nt)
20674 {
20675 /* DR 488 makes use of a type with no linkage cause
20676 type deduction to fail. */
20677 if (complain & tf_error)
20678 {
20679 if (TYPE_UNNAMED_P (nt))
20680 error ("%qT is/uses unnamed type", t);
20681 else
20682 error ("template argument for %qD uses local type %qT",
20683 tmpl, t);
20684 }
20685 return true;
20686 }
20687 /* In order to avoid all sorts of complications, we do not
20688 allow variably-modified types as template arguments. */
20689 else if (variably_modified_type_p (t, NULL_TREE))
20690 {
20691 if (complain & tf_error)
20692 error ("%qT is a variably modified type", t);
20693 return true;
20694 }
20695 }
20696 /* Class template and alias template arguments should be OK. */
20697 else if (DECL_TYPE_TEMPLATE_P (t))
20698 ;
20699 /* A non-type argument of integral or enumerated type must be a
20700 constant. */
20701 else if (TREE_TYPE (t)
20702 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20703 && !REFERENCE_REF_P (t)
20704 && !TREE_CONSTANT (t))
20705 {
20706 if (complain & tf_error)
20707 error ("integral expression %qE is not constant", t);
20708 return true;
20709 }
20710 return false;
20711 }
20712
20713 static bool
20714 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20715 {
20716 int ix, len = DECL_NTPARMS (tmpl);
20717 bool result = false;
20718
20719 for (ix = 0; ix != len; ix++)
20720 {
20721 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20722 result = true;
20723 }
20724 if (result && (complain & tf_error))
20725 error (" trying to instantiate %qD", tmpl);
20726 return result;
20727 }
20728
20729 /* We're out of SFINAE context now, so generate diagnostics for the access
20730 errors we saw earlier when instantiating D from TMPL and ARGS. */
20731
20732 static void
20733 recheck_decl_substitution (tree d, tree tmpl, tree args)
20734 {
20735 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20736 tree type = TREE_TYPE (pattern);
20737 location_t loc = input_location;
20738
20739 push_access_scope (d);
20740 push_deferring_access_checks (dk_no_deferred);
20741 input_location = DECL_SOURCE_LOCATION (pattern);
20742 tsubst (type, args, tf_warning_or_error, d);
20743 input_location = loc;
20744 pop_deferring_access_checks ();
20745 pop_access_scope (d);
20746 }
20747
20748 /* Instantiate the indicated variable, function, or alias template TMPL with
20749 the template arguments in TARG_PTR. */
20750
20751 static tree
20752 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20753 {
20754 tree targ_ptr = orig_args;
20755 tree fndecl;
20756 tree gen_tmpl;
20757 tree spec;
20758 bool access_ok = true;
20759
20760 if (tmpl == error_mark_node)
20761 return error_mark_node;
20762
20763 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20764
20765 /* If this function is a clone, handle it specially. */
20766 if (DECL_CLONED_FUNCTION_P (tmpl))
20767 {
20768 tree spec;
20769 tree clone;
20770
20771 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20772 DECL_CLONED_FUNCTION. */
20773 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20774 targ_ptr, complain);
20775 if (spec == error_mark_node)
20776 return error_mark_node;
20777
20778 /* Look for the clone. */
20779 FOR_EACH_CLONE (clone, spec)
20780 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20781 return clone;
20782 /* We should always have found the clone by now. */
20783 gcc_unreachable ();
20784 return NULL_TREE;
20785 }
20786
20787 if (targ_ptr == error_mark_node)
20788 return error_mark_node;
20789
20790 /* Check to see if we already have this specialization. */
20791 gen_tmpl = most_general_template (tmpl);
20792 if (TMPL_ARGS_DEPTH (targ_ptr)
20793 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20794 /* targ_ptr only has the innermost template args, so add the outer ones
20795 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20796 the case of a non-dependent call within a template definition). */
20797 targ_ptr = (add_outermost_template_args
20798 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20799 targ_ptr));
20800
20801 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20802 but it doesn't seem to be on the hot path. */
20803 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20804
20805 gcc_checking_assert (tmpl == gen_tmpl
20806 || ((fndecl
20807 = retrieve_specialization (tmpl, orig_args, 0))
20808 == spec)
20809 || fndecl == NULL_TREE);
20810
20811 if (spec != NULL_TREE)
20812 {
20813 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20814 {
20815 if (complain & tf_error)
20816 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20817 return error_mark_node;
20818 }
20819 return spec;
20820 }
20821
20822 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20823 complain))
20824 return error_mark_node;
20825
20826 /* We are building a FUNCTION_DECL, during which the access of its
20827 parameters and return types have to be checked. However this
20828 FUNCTION_DECL which is the desired context for access checking
20829 is not built yet. We solve this chicken-and-egg problem by
20830 deferring all checks until we have the FUNCTION_DECL. */
20831 push_deferring_access_checks (dk_deferred);
20832
20833 /* Instantiation of the function happens in the context of the function
20834 template, not the context of the overload resolution we're doing. */
20835 push_to_top_level ();
20836 /* If there are dependent arguments, e.g. because we're doing partial
20837 ordering, make sure processing_template_decl stays set. */
20838 if (uses_template_parms (targ_ptr))
20839 ++processing_template_decl;
20840 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20841 {
20842 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20843 complain, gen_tmpl, true);
20844 push_nested_class (ctx);
20845 }
20846
20847 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20848
20849 fndecl = NULL_TREE;
20850 if (VAR_P (pattern))
20851 {
20852 /* We need to determine if we're using a partial or explicit
20853 specialization now, because the type of the variable could be
20854 different. */
20855 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20856 tree elt = most_specialized_partial_spec (tid, complain);
20857 if (elt == error_mark_node)
20858 pattern = error_mark_node;
20859 else if (elt)
20860 {
20861 tree partial_tmpl = TREE_VALUE (elt);
20862 tree partial_args = TREE_PURPOSE (elt);
20863 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20864 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20865 }
20866 }
20867
20868 /* Substitute template parameters to obtain the specialization. */
20869 if (fndecl == NULL_TREE)
20870 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20871 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20872 pop_nested_class ();
20873 pop_from_top_level ();
20874
20875 if (fndecl == error_mark_node)
20876 {
20877 pop_deferring_access_checks ();
20878 return error_mark_node;
20879 }
20880
20881 /* The DECL_TI_TEMPLATE should always be the immediate parent
20882 template, not the most general template. */
20883 DECL_TI_TEMPLATE (fndecl) = tmpl;
20884 DECL_TI_ARGS (fndecl) = targ_ptr;
20885
20886 /* Now we know the specialization, compute access previously
20887 deferred. Do no access control for inheriting constructors,
20888 as we already checked access for the inherited constructor. */
20889 if (!(flag_new_inheriting_ctors
20890 && DECL_INHERITED_CTOR (fndecl)))
20891 {
20892 push_access_scope (fndecl);
20893 if (!perform_deferred_access_checks (complain))
20894 access_ok = false;
20895 pop_access_scope (fndecl);
20896 }
20897 pop_deferring_access_checks ();
20898
20899 /* If we've just instantiated the main entry point for a function,
20900 instantiate all the alternate entry points as well. We do this
20901 by cloning the instantiation of the main entry point, not by
20902 instantiating the template clones. */
20903 if (tree chain = DECL_CHAIN (gen_tmpl))
20904 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20905 clone_cdtor (fndecl, /*update_methods=*/false);
20906
20907 if (!access_ok)
20908 {
20909 if (!(complain & tf_error))
20910 {
20911 /* Remember to reinstantiate when we're out of SFINAE so the user
20912 can see the errors. */
20913 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20914 }
20915 return error_mark_node;
20916 }
20917 return fndecl;
20918 }
20919
20920 /* Wrapper for instantiate_template_1. */
20921
20922 tree
20923 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20924 {
20925 tree ret;
20926 timevar_push (TV_TEMPLATE_INST);
20927 ret = instantiate_template_1 (tmpl, orig_args, complain);
20928 timevar_pop (TV_TEMPLATE_INST);
20929 return ret;
20930 }
20931
20932 /* Instantiate the alias template TMPL with ARGS. Also push a template
20933 instantiation level, which instantiate_template doesn't do because
20934 functions and variables have sufficient context established by the
20935 callers. */
20936
20937 static tree
20938 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20939 {
20940 if (tmpl == error_mark_node || args == error_mark_node)
20941 return error_mark_node;
20942
20943 args =
20944 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20945 args, tmpl, complain,
20946 /*require_all_args=*/true,
20947 /*use_default_args=*/true);
20948
20949 /* FIXME check for satisfaction in check_instantiated_args. */
20950 if (flag_concepts
20951 && !any_dependent_template_arguments_p (args)
20952 && !constraints_satisfied_p (tmpl, args))
20953 {
20954 if (complain & tf_error)
20955 {
20956 auto_diagnostic_group d;
20957 error ("template constraint failure for %qD", tmpl);
20958 diagnose_constraints (input_location, tmpl, args);
20959 }
20960 return error_mark_node;
20961 }
20962
20963 if (!push_tinst_level (tmpl, args))
20964 return error_mark_node;
20965 tree r = instantiate_template (tmpl, args, complain);
20966 pop_tinst_level ();
20967
20968 return r;
20969 }
20970
20971 /* PARM is a template parameter pack for FN. Returns true iff
20972 PARM is used in a deducible way in the argument list of FN. */
20973
20974 static bool
20975 pack_deducible_p (tree parm, tree fn)
20976 {
20977 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20978 for (; t; t = TREE_CHAIN (t))
20979 {
20980 tree type = TREE_VALUE (t);
20981 tree packs;
20982 if (!PACK_EXPANSION_P (type))
20983 continue;
20984 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20985 packs; packs = TREE_CHAIN (packs))
20986 if (template_args_equal (TREE_VALUE (packs), parm))
20987 {
20988 /* The template parameter pack is used in a function parameter
20989 pack. If this is the end of the parameter list, the
20990 template parameter pack is deducible. */
20991 if (TREE_CHAIN (t) == void_list_node)
20992 return true;
20993 else
20994 /* Otherwise, not. Well, it could be deduced from
20995 a non-pack parameter, but doing so would end up with
20996 a deduction mismatch, so don't bother. */
20997 return false;
20998 }
20999 }
21000 /* The template parameter pack isn't used in any function parameter
21001 packs, but it might be used deeper, e.g. tuple<Args...>. */
21002 return true;
21003 }
21004
21005 /* Subroutine of fn_type_unification: check non-dependent parms for
21006 convertibility. */
21007
21008 static int
21009 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
21010 tree fn, unification_kind_t strict, int flags,
21011 struct conversion **convs, bool explain_p)
21012 {
21013 /* Non-constructor methods need to leave a conversion for 'this', which
21014 isn't included in nargs here. */
21015 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21016 && !DECL_CONSTRUCTOR_P (fn));
21017
21018 for (unsigned ia = 0;
21019 parms && parms != void_list_node && ia < nargs; )
21020 {
21021 tree parm = TREE_VALUE (parms);
21022
21023 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21024 && (!TREE_CHAIN (parms)
21025 || TREE_CHAIN (parms) == void_list_node))
21026 /* For a function parameter pack that occurs at the end of the
21027 parameter-declaration-list, the type A of each remaining
21028 argument of the call is compared with the type P of the
21029 declarator-id of the function parameter pack. */
21030 break;
21031
21032 parms = TREE_CHAIN (parms);
21033
21034 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21035 /* For a function parameter pack that does not occur at the
21036 end of the parameter-declaration-list, the type of the
21037 parameter pack is a non-deduced context. */
21038 continue;
21039
21040 if (!uses_template_parms (parm))
21041 {
21042 tree arg = args[ia];
21043 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21044 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21045
21046 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21047 conv_p, explain_p))
21048 return 1;
21049 }
21050
21051 ++ia;
21052 }
21053
21054 return 0;
21055 }
21056
21057 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21058 NARGS elements of the arguments that are being used when calling
21059 it. TARGS is a vector into which the deduced template arguments
21060 are placed.
21061
21062 Returns either a FUNCTION_DECL for the matching specialization of FN or
21063 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21064 true, diagnostics will be printed to explain why it failed.
21065
21066 If FN is a conversion operator, or we are trying to produce a specific
21067 specialization, RETURN_TYPE is the return type desired.
21068
21069 The EXPLICIT_TARGS are explicit template arguments provided via a
21070 template-id.
21071
21072 The parameter STRICT is one of:
21073
21074 DEDUCE_CALL:
21075 We are deducing arguments for a function call, as in
21076 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21077 deducing arguments for a call to the result of a conversion
21078 function template, as in [over.call.object].
21079
21080 DEDUCE_CONV:
21081 We are deducing arguments for a conversion function, as in
21082 [temp.deduct.conv].
21083
21084 DEDUCE_EXACT:
21085 We are deducing arguments when doing an explicit instantiation
21086 as in [temp.explicit], when determining an explicit specialization
21087 as in [temp.expl.spec], or when taking the address of a function
21088 template, as in [temp.deduct.funcaddr]. */
21089
21090 tree
21091 fn_type_unification (tree fn,
21092 tree explicit_targs,
21093 tree targs,
21094 const tree *args,
21095 unsigned int nargs,
21096 tree return_type,
21097 unification_kind_t strict,
21098 int flags,
21099 struct conversion **convs,
21100 bool explain_p,
21101 bool decltype_p)
21102 {
21103 tree parms;
21104 tree fntype;
21105 tree decl = NULL_TREE;
21106 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21107 bool ok;
21108 static int deduction_depth;
21109 /* type_unification_real will pass back any access checks from default
21110 template argument substitution. */
21111 vec<deferred_access_check, va_gc> *checks = NULL;
21112 /* We don't have all the template args yet. */
21113 bool incomplete = true;
21114
21115 tree orig_fn = fn;
21116 if (flag_new_inheriting_ctors)
21117 fn = strip_inheriting_ctors (fn);
21118
21119 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21120 tree r = error_mark_node;
21121
21122 tree full_targs = targs;
21123 if (TMPL_ARGS_DEPTH (targs)
21124 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21125 full_targs = (add_outermost_template_args
21126 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21127 targs));
21128
21129 if (decltype_p)
21130 complain |= tf_decltype;
21131
21132 /* In C++0x, it's possible to have a function template whose type depends
21133 on itself recursively. This is most obvious with decltype, but can also
21134 occur with enumeration scope (c++/48969). So we need to catch infinite
21135 recursion and reject the substitution at deduction time; this function
21136 will return error_mark_node for any repeated substitution.
21137
21138 This also catches excessive recursion such as when f<N> depends on
21139 f<N-1> across all integers, and returns error_mark_node for all the
21140 substitutions back up to the initial one.
21141
21142 This is, of course, not reentrant. */
21143 if (excessive_deduction_depth)
21144 return error_mark_node;
21145 ++deduction_depth;
21146
21147 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21148
21149 fntype = TREE_TYPE (fn);
21150 if (explicit_targs)
21151 {
21152 /* [temp.deduct]
21153
21154 The specified template arguments must match the template
21155 parameters in kind (i.e., type, nontype, template), and there
21156 must not be more arguments than there are parameters;
21157 otherwise type deduction fails.
21158
21159 Nontype arguments must match the types of the corresponding
21160 nontype template parameters, or must be convertible to the
21161 types of the corresponding nontype parameters as specified in
21162 _temp.arg.nontype_, otherwise type deduction fails.
21163
21164 All references in the function type of the function template
21165 to the corresponding template parameters are replaced by the
21166 specified template argument values. If a substitution in a
21167 template parameter or in the function type of the function
21168 template results in an invalid type, type deduction fails. */
21169 int i, len = TREE_VEC_LENGTH (tparms);
21170 location_t loc = input_location;
21171 incomplete = false;
21172
21173 if (explicit_targs == error_mark_node)
21174 goto fail;
21175
21176 if (TMPL_ARGS_DEPTH (explicit_targs)
21177 < TMPL_ARGS_DEPTH (full_targs))
21178 explicit_targs = add_outermost_template_args (full_targs,
21179 explicit_targs);
21180
21181 /* Adjust any explicit template arguments before entering the
21182 substitution context. */
21183 explicit_targs
21184 = (coerce_template_parms (tparms, explicit_targs, fn,
21185 complain|tf_partial,
21186 /*require_all_args=*/false,
21187 /*use_default_args=*/false));
21188 if (explicit_targs == error_mark_node)
21189 goto fail;
21190
21191 /* Substitute the explicit args into the function type. This is
21192 necessary so that, for instance, explicitly declared function
21193 arguments can match null pointed constants. If we were given
21194 an incomplete set of explicit args, we must not do semantic
21195 processing during substitution as we could create partial
21196 instantiations. */
21197 for (i = 0; i < len; i++)
21198 {
21199 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21200 bool parameter_pack = false;
21201 tree targ = TREE_VEC_ELT (explicit_targs, i);
21202
21203 /* Dig out the actual parm. */
21204 if (TREE_CODE (parm) == TYPE_DECL
21205 || TREE_CODE (parm) == TEMPLATE_DECL)
21206 {
21207 parm = TREE_TYPE (parm);
21208 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21209 }
21210 else if (TREE_CODE (parm) == PARM_DECL)
21211 {
21212 parm = DECL_INITIAL (parm);
21213 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21214 }
21215
21216 if (targ == NULL_TREE)
21217 /* No explicit argument for this template parameter. */
21218 incomplete = true;
21219 else if (parameter_pack && pack_deducible_p (parm, fn))
21220 {
21221 /* Mark the argument pack as "incomplete". We could
21222 still deduce more arguments during unification.
21223 We remove this mark in type_unification_real. */
21224 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21225 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21226 = ARGUMENT_PACK_ARGS (targ);
21227
21228 /* We have some incomplete argument packs. */
21229 incomplete = true;
21230 }
21231 }
21232
21233 if (incomplete)
21234 {
21235 if (!push_tinst_level (fn, explicit_targs))
21236 {
21237 excessive_deduction_depth = true;
21238 goto fail;
21239 }
21240 ++processing_template_decl;
21241 input_location = DECL_SOURCE_LOCATION (fn);
21242 /* Ignore any access checks; we'll see them again in
21243 instantiate_template and they might have the wrong
21244 access path at this point. */
21245 push_deferring_access_checks (dk_deferred);
21246 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21247 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21248 pop_deferring_access_checks ();
21249 input_location = loc;
21250 --processing_template_decl;
21251 pop_tinst_level ();
21252
21253 if (fntype == error_mark_node)
21254 goto fail;
21255 }
21256
21257 /* Place the explicitly specified arguments in TARGS. */
21258 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21259 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21260 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21261 if (!incomplete && CHECKING_P
21262 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21263 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21264 (targs, NUM_TMPL_ARGS (explicit_targs));
21265 }
21266
21267 if (return_type && strict != DEDUCE_CALL)
21268 {
21269 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21270 new_args[0] = return_type;
21271 memcpy (new_args + 1, args, nargs * sizeof (tree));
21272 args = new_args;
21273 ++nargs;
21274 }
21275
21276 if (!incomplete)
21277 goto deduced;
21278
21279 /* Never do unification on the 'this' parameter. */
21280 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21281
21282 if (return_type && strict == DEDUCE_CALL)
21283 {
21284 /* We're deducing for a call to the result of a template conversion
21285 function. The parms we really want are in return_type. */
21286 if (INDIRECT_TYPE_P (return_type))
21287 return_type = TREE_TYPE (return_type);
21288 parms = TYPE_ARG_TYPES (return_type);
21289 }
21290 else if (return_type)
21291 {
21292 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21293 }
21294
21295 /* We allow incomplete unification without an error message here
21296 because the standard doesn't seem to explicitly prohibit it. Our
21297 callers must be ready to deal with unification failures in any
21298 event. */
21299
21300 /* If we aren't explaining yet, push tinst context so we can see where
21301 any errors (e.g. from class instantiations triggered by instantiation
21302 of default template arguments) come from. If we are explaining, this
21303 context is redundant. */
21304 if (!explain_p && !push_tinst_level (fn, targs))
21305 {
21306 excessive_deduction_depth = true;
21307 goto fail;
21308 }
21309
21310 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21311 full_targs, parms, args, nargs, /*subr=*/0,
21312 strict, &checks, explain_p);
21313 if (!explain_p)
21314 pop_tinst_level ();
21315 if (!ok)
21316 goto fail;
21317
21318 /* Now that we have bindings for all of the template arguments,
21319 ensure that the arguments deduced for the template template
21320 parameters have compatible template parameter lists. We cannot
21321 check this property before we have deduced all template
21322 arguments, because the template parameter types of a template
21323 template parameter might depend on prior template parameters
21324 deduced after the template template parameter. The following
21325 ill-formed example illustrates this issue:
21326
21327 template<typename T, template<T> class C> void f(C<5>, T);
21328
21329 template<int N> struct X {};
21330
21331 void g() {
21332 f(X<5>(), 5l); // error: template argument deduction fails
21333 }
21334
21335 The template parameter list of 'C' depends on the template type
21336 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21337 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21338 time that we deduce 'C'. */
21339 if (!template_template_parm_bindings_ok_p
21340 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21341 {
21342 unify_inconsistent_template_template_parameters (explain_p);
21343 goto fail;
21344 }
21345
21346 deduced:
21347
21348 /* CWG2369: Check satisfaction before non-deducible conversions. */
21349 if (!constraints_satisfied_p (fn, targs))
21350 {
21351 if (explain_p)
21352 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21353 goto fail;
21354 }
21355
21356 /* DR 1391: All parameters have args, now check non-dependent parms for
21357 convertibility. We don't do this if all args were explicitly specified,
21358 as the standard says that we substitute explicit args immediately. */
21359 if (incomplete
21360 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21361 convs, explain_p))
21362 goto fail;
21363
21364 /* All is well so far. Now, check:
21365
21366 [temp.deduct]
21367
21368 When all template arguments have been deduced, all uses of
21369 template parameters in nondeduced contexts are replaced with
21370 the corresponding deduced argument values. If the
21371 substitution results in an invalid type, as described above,
21372 type deduction fails. */
21373 if (!push_tinst_level (fn, targs))
21374 {
21375 excessive_deduction_depth = true;
21376 goto fail;
21377 }
21378
21379 /* Also collect access checks from the instantiation. */
21380 reopen_deferring_access_checks (checks);
21381
21382 decl = instantiate_template (fn, targs, complain);
21383
21384 checks = get_deferred_access_checks ();
21385 pop_deferring_access_checks ();
21386
21387 pop_tinst_level ();
21388
21389 if (decl == error_mark_node)
21390 goto fail;
21391
21392 /* Now perform any access checks encountered during substitution. */
21393 push_access_scope (decl);
21394 ok = perform_access_checks (checks, complain);
21395 pop_access_scope (decl);
21396 if (!ok)
21397 goto fail;
21398
21399 /* If we're looking for an exact match, check that what we got
21400 is indeed an exact match. It might not be if some template
21401 parameters are used in non-deduced contexts. But don't check
21402 for an exact match if we have dependent template arguments;
21403 in that case we're doing partial ordering, and we already know
21404 that we have two candidates that will provide the actual type. */
21405 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21406 {
21407 tree substed = TREE_TYPE (decl);
21408 unsigned int i;
21409
21410 tree sarg
21411 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21412 if (return_type)
21413 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21414 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21415 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21416 {
21417 unify_type_mismatch (explain_p, args[i],
21418 TREE_VALUE (sarg));
21419 goto fail;
21420 }
21421 }
21422
21423 /* After doing deduction with the inherited constructor, actually return an
21424 instantiation of the inheriting constructor. */
21425 if (orig_fn != fn)
21426 decl = instantiate_template (orig_fn, targs, complain);
21427
21428 r = decl;
21429
21430 fail:
21431 --deduction_depth;
21432 if (excessive_deduction_depth)
21433 {
21434 if (deduction_depth == 0)
21435 /* Reset once we're all the way out. */
21436 excessive_deduction_depth = false;
21437 }
21438
21439 return r;
21440 }
21441
21442 /* Adjust types before performing type deduction, as described in
21443 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21444 sections are symmetric. PARM is the type of a function parameter
21445 or the return type of the conversion function. ARG is the type of
21446 the argument passed to the call, or the type of the value
21447 initialized with the result of the conversion function.
21448 ARG_EXPR is the original argument expression, which may be null. */
21449
21450 static int
21451 maybe_adjust_types_for_deduction (unification_kind_t strict,
21452 tree* parm,
21453 tree* arg,
21454 tree arg_expr)
21455 {
21456 int result = 0;
21457
21458 switch (strict)
21459 {
21460 case DEDUCE_CALL:
21461 break;
21462
21463 case DEDUCE_CONV:
21464 /* Swap PARM and ARG throughout the remainder of this
21465 function; the handling is precisely symmetric since PARM
21466 will initialize ARG rather than vice versa. */
21467 std::swap (parm, arg);
21468 break;
21469
21470 case DEDUCE_EXACT:
21471 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21472 too, but here handle it by stripping the reference from PARM
21473 rather than by adding it to ARG. */
21474 if (TYPE_REF_P (*parm)
21475 && TYPE_REF_IS_RVALUE (*parm)
21476 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21477 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21478 && TYPE_REF_P (*arg)
21479 && !TYPE_REF_IS_RVALUE (*arg))
21480 *parm = TREE_TYPE (*parm);
21481 /* Nothing else to do in this case. */
21482 return 0;
21483
21484 default:
21485 gcc_unreachable ();
21486 }
21487
21488 if (!TYPE_REF_P (*parm))
21489 {
21490 /* [temp.deduct.call]
21491
21492 If P is not a reference type:
21493
21494 --If A is an array type, the pointer type produced by the
21495 array-to-pointer standard conversion (_conv.array_) is
21496 used in place of A for type deduction; otherwise,
21497
21498 --If A is a function type, the pointer type produced by
21499 the function-to-pointer standard conversion
21500 (_conv.func_) is used in place of A for type deduction;
21501 otherwise,
21502
21503 --If A is a cv-qualified type, the top level
21504 cv-qualifiers of A's type are ignored for type
21505 deduction. */
21506 if (TREE_CODE (*arg) == ARRAY_TYPE)
21507 *arg = build_pointer_type (TREE_TYPE (*arg));
21508 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21509 *arg = build_pointer_type (*arg);
21510 else
21511 *arg = TYPE_MAIN_VARIANT (*arg);
21512 }
21513
21514 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21515 reference to a cv-unqualified template parameter that does not represent a
21516 template parameter of a class template (during class template argument
21517 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21518 an lvalue, the type "lvalue reference to A" is used in place of A for type
21519 deduction. */
21520 if (TYPE_REF_P (*parm)
21521 && TYPE_REF_IS_RVALUE (*parm)
21522 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21523 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21524 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21525 && (arg_expr ? lvalue_p (arg_expr)
21526 /* try_one_overload doesn't provide an arg_expr, but
21527 functions are always lvalues. */
21528 : TREE_CODE (*arg) == FUNCTION_TYPE))
21529 *arg = build_reference_type (*arg);
21530
21531 /* [temp.deduct.call]
21532
21533 If P is a cv-qualified type, the top level cv-qualifiers
21534 of P's type are ignored for type deduction. If P is a
21535 reference type, the type referred to by P is used for
21536 type deduction. */
21537 *parm = TYPE_MAIN_VARIANT (*parm);
21538 if (TYPE_REF_P (*parm))
21539 {
21540 *parm = TREE_TYPE (*parm);
21541 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21542 }
21543
21544 /* DR 322. For conversion deduction, remove a reference type on parm
21545 too (which has been swapped into ARG). */
21546 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21547 *arg = TREE_TYPE (*arg);
21548
21549 return result;
21550 }
21551
21552 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21553 template which doesn't contain any deducible template parameters; check if
21554 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21555 unify_one_argument. */
21556
21557 static int
21558 check_non_deducible_conversion (tree parm, tree arg, int strict,
21559 int flags, struct conversion **conv_p,
21560 bool explain_p)
21561 {
21562 tree type;
21563
21564 if (!TYPE_P (arg))
21565 type = TREE_TYPE (arg);
21566 else
21567 type = arg;
21568
21569 if (same_type_p (parm, type))
21570 return unify_success (explain_p);
21571
21572 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21573 if (strict == DEDUCE_CONV)
21574 {
21575 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21576 return unify_success (explain_p);
21577 }
21578 else if (strict != DEDUCE_EXACT)
21579 {
21580 bool ok = false;
21581 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21582 if (conv_p)
21583 /* Avoid recalculating this in add_function_candidate. */
21584 ok = (*conv_p
21585 = good_conversion (parm, type, conv_arg, flags, complain));
21586 else
21587 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21588 if (ok)
21589 return unify_success (explain_p);
21590 }
21591
21592 if (strict == DEDUCE_EXACT)
21593 return unify_type_mismatch (explain_p, parm, arg);
21594 else
21595 return unify_arg_conversion (explain_p, parm, type, arg);
21596 }
21597
21598 static bool uses_deducible_template_parms (tree type);
21599
21600 /* Returns true iff the expression EXPR is one from which a template
21601 argument can be deduced. In other words, if it's an undecorated
21602 use of a template non-type parameter. */
21603
21604 static bool
21605 deducible_expression (tree expr)
21606 {
21607 /* Strip implicit conversions. */
21608 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21609 expr = TREE_OPERAND (expr, 0);
21610 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21611 }
21612
21613 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21614 deducible way; that is, if it has a max value of <PARM> - 1. */
21615
21616 static bool
21617 deducible_array_bound (tree domain)
21618 {
21619 if (domain == NULL_TREE)
21620 return false;
21621
21622 tree max = TYPE_MAX_VALUE (domain);
21623 if (TREE_CODE (max) != MINUS_EXPR)
21624 return false;
21625
21626 return deducible_expression (TREE_OPERAND (max, 0));
21627 }
21628
21629 /* Returns true iff the template arguments ARGS use a template parameter
21630 in a deducible way. */
21631
21632 static bool
21633 deducible_template_args (tree args)
21634 {
21635 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21636 {
21637 bool deducible;
21638 tree elt = TREE_VEC_ELT (args, i);
21639 if (ARGUMENT_PACK_P (elt))
21640 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21641 else
21642 {
21643 if (PACK_EXPANSION_P (elt))
21644 elt = PACK_EXPANSION_PATTERN (elt);
21645 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21646 deducible = true;
21647 else if (TYPE_P (elt))
21648 deducible = uses_deducible_template_parms (elt);
21649 else
21650 deducible = deducible_expression (elt);
21651 }
21652 if (deducible)
21653 return true;
21654 }
21655 return false;
21656 }
21657
21658 /* Returns true iff TYPE contains any deducible references to template
21659 parameters, as per 14.8.2.5. */
21660
21661 static bool
21662 uses_deducible_template_parms (tree type)
21663 {
21664 if (PACK_EXPANSION_P (type))
21665 type = PACK_EXPANSION_PATTERN (type);
21666
21667 /* T
21668 cv-list T
21669 TT<T>
21670 TT<i>
21671 TT<> */
21672 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21673 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21674 return true;
21675
21676 /* T*
21677 T&
21678 T&& */
21679 if (INDIRECT_TYPE_P (type))
21680 return uses_deducible_template_parms (TREE_TYPE (type));
21681
21682 /* T[integer-constant ]
21683 type [i] */
21684 if (TREE_CODE (type) == ARRAY_TYPE)
21685 return (uses_deducible_template_parms (TREE_TYPE (type))
21686 || deducible_array_bound (TYPE_DOMAIN (type)));
21687
21688 /* T type ::*
21689 type T::*
21690 T T::*
21691 T (type ::*)()
21692 type (T::*)()
21693 type (type ::*)(T)
21694 type (T::*)(T)
21695 T (type ::*)(T)
21696 T (T::*)()
21697 T (T::*)(T) */
21698 if (TYPE_PTRMEM_P (type))
21699 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21700 || (uses_deducible_template_parms
21701 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21702
21703 /* template-name <T> (where template-name refers to a class template)
21704 template-name <i> (where template-name refers to a class template) */
21705 if (CLASS_TYPE_P (type)
21706 && CLASSTYPE_TEMPLATE_INFO (type)
21707 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21708 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21709 (CLASSTYPE_TI_ARGS (type)));
21710
21711 /* type (T)
21712 T()
21713 T(T) */
21714 if (FUNC_OR_METHOD_TYPE_P (type))
21715 {
21716 if (uses_deducible_template_parms (TREE_TYPE (type)))
21717 return true;
21718 tree parm = TYPE_ARG_TYPES (type);
21719 if (TREE_CODE (type) == METHOD_TYPE)
21720 parm = TREE_CHAIN (parm);
21721 for (; parm; parm = TREE_CHAIN (parm))
21722 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21723 return true;
21724 }
21725
21726 return false;
21727 }
21728
21729 /* Subroutine of type_unification_real and unify_pack_expansion to
21730 handle unification of a single P/A pair. Parameters are as
21731 for those functions. */
21732
21733 static int
21734 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21735 int subr, unification_kind_t strict,
21736 bool explain_p)
21737 {
21738 tree arg_expr = NULL_TREE;
21739 int arg_strict;
21740
21741 if (arg == error_mark_node || parm == error_mark_node)
21742 return unify_invalid (explain_p);
21743 if (arg == unknown_type_node)
21744 /* We can't deduce anything from this, but we might get all the
21745 template args from other function args. */
21746 return unify_success (explain_p);
21747
21748 /* Implicit conversions (Clause 4) will be performed on a function
21749 argument to convert it to the type of the corresponding function
21750 parameter if the parameter type contains no template-parameters that
21751 participate in template argument deduction. */
21752 if (strict != DEDUCE_EXACT
21753 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21754 /* For function parameters with no deducible template parameters,
21755 just return. We'll check non-dependent conversions later. */
21756 return unify_success (explain_p);
21757
21758 switch (strict)
21759 {
21760 case DEDUCE_CALL:
21761 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21762 | UNIFY_ALLOW_MORE_CV_QUAL
21763 | UNIFY_ALLOW_DERIVED);
21764 break;
21765
21766 case DEDUCE_CONV:
21767 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21768 break;
21769
21770 case DEDUCE_EXACT:
21771 arg_strict = UNIFY_ALLOW_NONE;
21772 break;
21773
21774 default:
21775 gcc_unreachable ();
21776 }
21777
21778 /* We only do these transformations if this is the top-level
21779 parameter_type_list in a call or declaration matching; in other
21780 situations (nested function declarators, template argument lists) we
21781 won't be comparing a type to an expression, and we don't do any type
21782 adjustments. */
21783 if (!subr)
21784 {
21785 if (!TYPE_P (arg))
21786 {
21787 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21788 if (type_unknown_p (arg))
21789 {
21790 /* [temp.deduct.type] A template-argument can be
21791 deduced from a pointer to function or pointer
21792 to member function argument if the set of
21793 overloaded functions does not contain function
21794 templates and at most one of a set of
21795 overloaded functions provides a unique
21796 match. */
21797 resolve_overloaded_unification (tparms, targs, parm,
21798 arg, strict,
21799 arg_strict, explain_p);
21800 /* If a unique match was not found, this is a
21801 non-deduced context, so we still succeed. */
21802 return unify_success (explain_p);
21803 }
21804
21805 arg_expr = arg;
21806 arg = unlowered_expr_type (arg);
21807 if (arg == error_mark_node)
21808 return unify_invalid (explain_p);
21809 }
21810
21811 arg_strict |=
21812 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21813 }
21814 else
21815 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21816 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21817 return unify_template_argument_mismatch (explain_p, parm, arg);
21818
21819 /* For deduction from an init-list we need the actual list. */
21820 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21821 arg = arg_expr;
21822 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21823 }
21824
21825 /* for_each_template_parm callback that always returns 0. */
21826
21827 static int
21828 zero_r (tree, void *)
21829 {
21830 return 0;
21831 }
21832
21833 /* for_each_template_parm any_fn callback to handle deduction of a template
21834 type argument from the type of an array bound. */
21835
21836 static int
21837 array_deduction_r (tree t, void *data)
21838 {
21839 tree_pair_p d = (tree_pair_p)data;
21840 tree &tparms = d->purpose;
21841 tree &targs = d->value;
21842
21843 if (TREE_CODE (t) == ARRAY_TYPE)
21844 if (tree dom = TYPE_DOMAIN (t))
21845 if (tree max = TYPE_MAX_VALUE (dom))
21846 {
21847 if (TREE_CODE (max) == MINUS_EXPR)
21848 max = TREE_OPERAND (max, 0);
21849 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21850 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21851 UNIFY_ALLOW_NONE, /*explain*/false);
21852 }
21853
21854 /* Keep walking. */
21855 return 0;
21856 }
21857
21858 /* Try to deduce any not-yet-deduced template type arguments from the type of
21859 an array bound. This is handled separately from unify because 14.8.2.5 says
21860 "The type of a type parameter is only deduced from an array bound if it is
21861 not otherwise deduced." */
21862
21863 static void
21864 try_array_deduction (tree tparms, tree targs, tree parm)
21865 {
21866 tree_pair_s data = { tparms, targs };
21867 hash_set<tree> visited;
21868 for_each_template_parm (parm, zero_r, &data, &visited,
21869 /*nondeduced*/false, array_deduction_r);
21870 }
21871
21872 /* Most parms like fn_type_unification.
21873
21874 If SUBR is 1, we're being called recursively (to unify the
21875 arguments of a function or method parameter of a function
21876 template).
21877
21878 CHECKS is a pointer to a vector of access checks encountered while
21879 substituting default template arguments. */
21880
21881 static int
21882 type_unification_real (tree tparms,
21883 tree full_targs,
21884 tree xparms,
21885 const tree *xargs,
21886 unsigned int xnargs,
21887 int subr,
21888 unification_kind_t strict,
21889 vec<deferred_access_check, va_gc> **checks,
21890 bool explain_p)
21891 {
21892 tree parm, arg;
21893 int i;
21894 int ntparms = TREE_VEC_LENGTH (tparms);
21895 int saw_undeduced = 0;
21896 tree parms;
21897 const tree *args;
21898 unsigned int nargs;
21899 unsigned int ia;
21900
21901 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21902 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21903 gcc_assert (ntparms > 0);
21904
21905 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21906
21907 /* Reset the number of non-defaulted template arguments contained
21908 in TARGS. */
21909 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21910
21911 again:
21912 parms = xparms;
21913 args = xargs;
21914 nargs = xnargs;
21915
21916 ia = 0;
21917 while (parms && parms != void_list_node
21918 && ia < nargs)
21919 {
21920 parm = TREE_VALUE (parms);
21921
21922 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21923 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21924 /* For a function parameter pack that occurs at the end of the
21925 parameter-declaration-list, the type A of each remaining
21926 argument of the call is compared with the type P of the
21927 declarator-id of the function parameter pack. */
21928 break;
21929
21930 parms = TREE_CHAIN (parms);
21931
21932 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21933 /* For a function parameter pack that does not occur at the
21934 end of the parameter-declaration-list, the type of the
21935 parameter pack is a non-deduced context. */
21936 continue;
21937
21938 arg = args[ia];
21939 ++ia;
21940
21941 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21942 explain_p))
21943 return 1;
21944 }
21945
21946 if (parms
21947 && parms != void_list_node
21948 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21949 {
21950 /* Unify the remaining arguments with the pack expansion type. */
21951 tree argvec;
21952 tree parmvec = make_tree_vec (1);
21953
21954 /* Allocate a TREE_VEC and copy in all of the arguments */
21955 argvec = make_tree_vec (nargs - ia);
21956 for (i = 0; ia < nargs; ++ia, ++i)
21957 TREE_VEC_ELT (argvec, i) = args[ia];
21958
21959 /* Copy the parameter into parmvec. */
21960 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21961 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21962 /*subr=*/subr, explain_p))
21963 return 1;
21964
21965 /* Advance to the end of the list of parameters. */
21966 parms = TREE_CHAIN (parms);
21967 }
21968
21969 /* Fail if we've reached the end of the parm list, and more args
21970 are present, and the parm list isn't variadic. */
21971 if (ia < nargs && parms == void_list_node)
21972 return unify_too_many_arguments (explain_p, nargs, ia);
21973 /* Fail if parms are left and they don't have default values and
21974 they aren't all deduced as empty packs (c++/57397). This is
21975 consistent with sufficient_parms_p. */
21976 if (parms && parms != void_list_node
21977 && TREE_PURPOSE (parms) == NULL_TREE)
21978 {
21979 unsigned int count = nargs;
21980 tree p = parms;
21981 bool type_pack_p;
21982 do
21983 {
21984 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21985 if (!type_pack_p)
21986 count++;
21987 p = TREE_CHAIN (p);
21988 }
21989 while (p && p != void_list_node);
21990 if (count != nargs)
21991 return unify_too_few_arguments (explain_p, ia, count,
21992 type_pack_p);
21993 }
21994
21995 if (!subr)
21996 {
21997 tsubst_flags_t complain = (explain_p
21998 ? tf_warning_or_error
21999 : tf_none);
22000 bool tried_array_deduction = (cxx_dialect < cxx17);
22001
22002 for (i = 0; i < ntparms; i++)
22003 {
22004 tree targ = TREE_VEC_ELT (targs, i);
22005 tree tparm = TREE_VEC_ELT (tparms, i);
22006
22007 /* Clear the "incomplete" flags on all argument packs now so that
22008 substituting them into later default arguments works. */
22009 if (targ && ARGUMENT_PACK_P (targ))
22010 {
22011 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
22012 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
22013 }
22014
22015 if (targ || tparm == error_mark_node)
22016 continue;
22017 tparm = TREE_VALUE (tparm);
22018
22019 if (TREE_CODE (tparm) == TYPE_DECL
22020 && !tried_array_deduction)
22021 {
22022 try_array_deduction (tparms, targs, xparms);
22023 tried_array_deduction = true;
22024 if (TREE_VEC_ELT (targs, i))
22025 continue;
22026 }
22027
22028 /* If this is an undeduced nontype parameter that depends on
22029 a type parameter, try another pass; its type may have been
22030 deduced from a later argument than the one from which
22031 this parameter can be deduced. */
22032 if (TREE_CODE (tparm) == PARM_DECL
22033 && uses_template_parms (TREE_TYPE (tparm))
22034 && saw_undeduced < 2)
22035 {
22036 saw_undeduced = 1;
22037 continue;
22038 }
22039
22040 /* Core issue #226 (C++0x) [temp.deduct]:
22041
22042 If a template argument has not been deduced, its
22043 default template argument, if any, is used.
22044
22045 When we are in C++98 mode, TREE_PURPOSE will either
22046 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22047 to explicitly check cxx_dialect here. */
22048 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22049 /* OK, there is a default argument. Wait until after the
22050 conversion check to do substitution. */
22051 continue;
22052
22053 /* If the type parameter is a parameter pack, then it will
22054 be deduced to an empty parameter pack. */
22055 if (template_parameter_pack_p (tparm))
22056 {
22057 tree arg;
22058
22059 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22060 {
22061 arg = make_node (NONTYPE_ARGUMENT_PACK);
22062 TREE_CONSTANT (arg) = 1;
22063 }
22064 else
22065 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22066
22067 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22068
22069 TREE_VEC_ELT (targs, i) = arg;
22070 continue;
22071 }
22072
22073 return unify_parameter_deduction_failure (explain_p, tparm);
22074 }
22075
22076 /* Now substitute into the default template arguments. */
22077 for (i = 0; i < ntparms; i++)
22078 {
22079 tree targ = TREE_VEC_ELT (targs, i);
22080 tree tparm = TREE_VEC_ELT (tparms, i);
22081
22082 if (targ || tparm == error_mark_node)
22083 continue;
22084 tree parm = TREE_VALUE (tparm);
22085 tree arg = TREE_PURPOSE (tparm);
22086 reopen_deferring_access_checks (*checks);
22087 location_t save_loc = input_location;
22088 if (DECL_P (parm))
22089 input_location = DECL_SOURCE_LOCATION (parm);
22090
22091 if (saw_undeduced == 1
22092 && TREE_CODE (parm) == PARM_DECL
22093 && uses_template_parms (TREE_TYPE (parm)))
22094 {
22095 /* The type of this non-type parameter depends on undeduced
22096 parameters. Don't try to use its default argument yet,
22097 since we might deduce an argument for it on the next pass,
22098 but do check whether the arguments we already have cause
22099 substitution failure, so that that happens before we try
22100 later default arguments (78489). */
22101 ++processing_template_decl;
22102 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22103 NULL_TREE);
22104 --processing_template_decl;
22105 if (type == error_mark_node)
22106 arg = error_mark_node;
22107 else
22108 arg = NULL_TREE;
22109 }
22110 else
22111 {
22112 /* Even if the call is happening in template context, getting
22113 here means it's non-dependent, and a default argument is
22114 considered a separate definition under [temp.decls], so we can
22115 do this substitution without processing_template_decl. This
22116 is important if the default argument contains something that
22117 might be instantiation-dependent like access (87480). */
22118 processing_template_decl_sentinel s;
22119 tree substed = NULL_TREE;
22120 if (saw_undeduced == 1)
22121 {
22122 /* First instatiate in template context, in case we still
22123 depend on undeduced template parameters. */
22124 ++processing_template_decl;
22125 substed = tsubst_template_arg (arg, full_targs, complain,
22126 NULL_TREE);
22127 --processing_template_decl;
22128 if (substed != error_mark_node
22129 && !uses_template_parms (substed))
22130 /* We replaced all the tparms, substitute again out of
22131 template context. */
22132 substed = NULL_TREE;
22133 }
22134 if (!substed)
22135 substed = tsubst_template_arg (arg, full_targs, complain,
22136 NULL_TREE);
22137
22138 if (!uses_template_parms (substed))
22139 arg = convert_template_argument (parm, substed, full_targs,
22140 complain, i, NULL_TREE);
22141 else if (saw_undeduced == 1)
22142 arg = NULL_TREE;
22143 else
22144 arg = error_mark_node;
22145 }
22146
22147 input_location = save_loc;
22148 *checks = get_deferred_access_checks ();
22149 pop_deferring_access_checks ();
22150
22151 if (arg == error_mark_node)
22152 return 1;
22153 else if (arg)
22154 {
22155 TREE_VEC_ELT (targs, i) = arg;
22156 /* The position of the first default template argument,
22157 is also the number of non-defaulted arguments in TARGS.
22158 Record that. */
22159 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22160 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22161 }
22162 }
22163
22164 if (saw_undeduced++ == 1)
22165 goto again;
22166 }
22167
22168 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22169 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22170
22171 return unify_success (explain_p);
22172 }
22173
22174 /* Subroutine of type_unification_real. Args are like the variables
22175 at the call site. ARG is an overloaded function (or template-id);
22176 we try deducing template args from each of the overloads, and if
22177 only one succeeds, we go with that. Modifies TARGS and returns
22178 true on success. */
22179
22180 static bool
22181 resolve_overloaded_unification (tree tparms,
22182 tree targs,
22183 tree parm,
22184 tree arg,
22185 unification_kind_t strict,
22186 int sub_strict,
22187 bool explain_p)
22188 {
22189 tree tempargs = copy_node (targs);
22190 int good = 0;
22191 tree goodfn = NULL_TREE;
22192 bool addr_p;
22193
22194 if (TREE_CODE (arg) == ADDR_EXPR)
22195 {
22196 arg = TREE_OPERAND (arg, 0);
22197 addr_p = true;
22198 }
22199 else
22200 addr_p = false;
22201
22202 if (TREE_CODE (arg) == COMPONENT_REF)
22203 /* Handle `&x' where `x' is some static or non-static member
22204 function name. */
22205 arg = TREE_OPERAND (arg, 1);
22206
22207 if (TREE_CODE (arg) == OFFSET_REF)
22208 arg = TREE_OPERAND (arg, 1);
22209
22210 /* Strip baselink information. */
22211 if (BASELINK_P (arg))
22212 arg = BASELINK_FUNCTIONS (arg);
22213
22214 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22215 {
22216 /* If we got some explicit template args, we need to plug them into
22217 the affected templates before we try to unify, in case the
22218 explicit args will completely resolve the templates in question. */
22219
22220 int ok = 0;
22221 tree expl_subargs = TREE_OPERAND (arg, 1);
22222 arg = TREE_OPERAND (arg, 0);
22223
22224 for (lkp_iterator iter (arg); iter; ++iter)
22225 {
22226 tree fn = *iter;
22227 tree subargs, elem;
22228
22229 if (TREE_CODE (fn) != TEMPLATE_DECL)
22230 continue;
22231
22232 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22233 expl_subargs, NULL_TREE, tf_none,
22234 /*require_all_args=*/true,
22235 /*use_default_args=*/true);
22236 if (subargs != error_mark_node
22237 && !any_dependent_template_arguments_p (subargs))
22238 {
22239 fn = instantiate_template (fn, subargs, tf_none);
22240 if (!constraints_satisfied_p (fn))
22241 continue;
22242 if (undeduced_auto_decl (fn))
22243 {
22244 /* Instantiate the function to deduce its return type. */
22245 ++function_depth;
22246 instantiate_decl (fn, /*defer*/false, /*class*/false);
22247 --function_depth;
22248 }
22249
22250 elem = TREE_TYPE (fn);
22251 if (try_one_overload (tparms, targs, tempargs, parm,
22252 elem, strict, sub_strict, addr_p, explain_p)
22253 && (!goodfn || !same_type_p (goodfn, elem)))
22254 {
22255 goodfn = elem;
22256 ++good;
22257 }
22258 }
22259 else if (subargs)
22260 ++ok;
22261 }
22262 /* If no templates (or more than one) are fully resolved by the
22263 explicit arguments, this template-id is a non-deduced context; it
22264 could still be OK if we deduce all template arguments for the
22265 enclosing call through other arguments. */
22266 if (good != 1)
22267 good = ok;
22268 }
22269 else if (!OVL_P (arg))
22270 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22271 -- but the deduction does not succeed because the expression is
22272 not just the function on its own. */
22273 return false;
22274 else
22275 for (lkp_iterator iter (arg); iter; ++iter)
22276 {
22277 tree fn = *iter;
22278 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22279 strict, sub_strict, addr_p, explain_p)
22280 && (!goodfn || !decls_match (goodfn, fn)))
22281 {
22282 goodfn = fn;
22283 ++good;
22284 }
22285 }
22286
22287 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22288 to function or pointer to member function argument if the set of
22289 overloaded functions does not contain function templates and at most
22290 one of a set of overloaded functions provides a unique match.
22291
22292 So if we found multiple possibilities, we return success but don't
22293 deduce anything. */
22294
22295 if (good == 1)
22296 {
22297 int i = TREE_VEC_LENGTH (targs);
22298 for (; i--; )
22299 if (TREE_VEC_ELT (tempargs, i))
22300 {
22301 tree old = TREE_VEC_ELT (targs, i);
22302 tree new_ = TREE_VEC_ELT (tempargs, i);
22303 if (new_ && old && ARGUMENT_PACK_P (old)
22304 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22305 /* Don't forget explicit template arguments in a pack. */
22306 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22307 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22308 TREE_VEC_ELT (targs, i) = new_;
22309 }
22310 }
22311 if (good)
22312 return true;
22313
22314 return false;
22315 }
22316
22317 /* Core DR 115: In contexts where deduction is done and fails, or in
22318 contexts where deduction is not done, if a template argument list is
22319 specified and it, along with any default template arguments, identifies
22320 a single function template specialization, then the template-id is an
22321 lvalue for the function template specialization. */
22322
22323 tree
22324 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22325 {
22326 tree expr, offset, baselink;
22327 bool addr;
22328
22329 if (!type_unknown_p (orig_expr))
22330 return orig_expr;
22331
22332 expr = orig_expr;
22333 addr = false;
22334 offset = NULL_TREE;
22335 baselink = NULL_TREE;
22336
22337 if (TREE_CODE (expr) == ADDR_EXPR)
22338 {
22339 expr = TREE_OPERAND (expr, 0);
22340 addr = true;
22341 }
22342 if (TREE_CODE (expr) == OFFSET_REF)
22343 {
22344 offset = expr;
22345 expr = TREE_OPERAND (expr, 1);
22346 }
22347 if (BASELINK_P (expr))
22348 {
22349 baselink = expr;
22350 expr = BASELINK_FUNCTIONS (expr);
22351 }
22352
22353 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22354 {
22355 int good = 0;
22356 tree goodfn = NULL_TREE;
22357
22358 /* If we got some explicit template args, we need to plug them into
22359 the affected templates before we try to unify, in case the
22360 explicit args will completely resolve the templates in question. */
22361
22362 tree expl_subargs = TREE_OPERAND (expr, 1);
22363 tree arg = TREE_OPERAND (expr, 0);
22364 tree badfn = NULL_TREE;
22365 tree badargs = NULL_TREE;
22366
22367 for (lkp_iterator iter (arg); iter; ++iter)
22368 {
22369 tree fn = *iter;
22370 tree subargs, elem;
22371
22372 if (TREE_CODE (fn) != TEMPLATE_DECL)
22373 continue;
22374
22375 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22376 expl_subargs, NULL_TREE, tf_none,
22377 /*require_all_args=*/true,
22378 /*use_default_args=*/true);
22379 if (subargs != error_mark_node
22380 && !any_dependent_template_arguments_p (subargs))
22381 {
22382 elem = instantiate_template (fn, subargs, tf_none);
22383 if (elem == error_mark_node)
22384 {
22385 badfn = fn;
22386 badargs = subargs;
22387 }
22388 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22389 && constraints_satisfied_p (elem))
22390 {
22391 goodfn = elem;
22392 ++good;
22393 }
22394 }
22395 }
22396 if (good == 1)
22397 {
22398 mark_used (goodfn);
22399 expr = goodfn;
22400 if (baselink)
22401 expr = build_baselink (BASELINK_BINFO (baselink),
22402 BASELINK_ACCESS_BINFO (baselink),
22403 expr, BASELINK_OPTYPE (baselink));
22404 if (offset)
22405 {
22406 tree base
22407 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22408 expr = build_offset_ref (base, expr, addr, complain);
22409 }
22410 if (addr)
22411 expr = cp_build_addr_expr (expr, complain);
22412 return expr;
22413 }
22414 else if (good == 0 && badargs && (complain & tf_error))
22415 /* There were no good options and at least one bad one, so let the
22416 user know what the problem is. */
22417 instantiate_template (badfn, badargs, complain);
22418 }
22419 return orig_expr;
22420 }
22421
22422 /* As above, but error out if the expression remains overloaded. */
22423
22424 tree
22425 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22426 {
22427 exp = resolve_nondeduced_context (exp, complain);
22428 if (type_unknown_p (exp))
22429 {
22430 if (complain & tf_error)
22431 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22432 return error_mark_node;
22433 }
22434 return exp;
22435 }
22436
22437 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22438 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22439 different overloads deduce different arguments for a given parm.
22440 ADDR_P is true if the expression for which deduction is being
22441 performed was of the form "& fn" rather than simply "fn".
22442
22443 Returns 1 on success. */
22444
22445 static int
22446 try_one_overload (tree tparms,
22447 tree orig_targs,
22448 tree targs,
22449 tree parm,
22450 tree arg,
22451 unification_kind_t strict,
22452 int sub_strict,
22453 bool addr_p,
22454 bool explain_p)
22455 {
22456 int nargs;
22457 tree tempargs;
22458 int i;
22459
22460 if (arg == error_mark_node)
22461 return 0;
22462
22463 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22464 to function or pointer to member function argument if the set of
22465 overloaded functions does not contain function templates and at most
22466 one of a set of overloaded functions provides a unique match.
22467
22468 So if this is a template, just return success. */
22469
22470 if (uses_template_parms (arg))
22471 return 1;
22472
22473 if (TREE_CODE (arg) == METHOD_TYPE)
22474 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22475 else if (addr_p)
22476 arg = build_pointer_type (arg);
22477
22478 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22479
22480 /* We don't copy orig_targs for this because if we have already deduced
22481 some template args from previous args, unify would complain when we
22482 try to deduce a template parameter for the same argument, even though
22483 there isn't really a conflict. */
22484 nargs = TREE_VEC_LENGTH (targs);
22485 tempargs = make_tree_vec (nargs);
22486
22487 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22488 return 0;
22489
22490 /* First make sure we didn't deduce anything that conflicts with
22491 explicitly specified args. */
22492 for (i = nargs; i--; )
22493 {
22494 tree elt = TREE_VEC_ELT (tempargs, i);
22495 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22496
22497 if (!elt)
22498 /*NOP*/;
22499 else if (uses_template_parms (elt))
22500 /* Since we're unifying against ourselves, we will fill in
22501 template args used in the function parm list with our own
22502 template parms. Discard them. */
22503 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22504 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22505 {
22506 /* Check that the argument at each index of the deduced argument pack
22507 is equivalent to the corresponding explicitly specified argument.
22508 We may have deduced more arguments than were explicitly specified,
22509 and that's OK. */
22510
22511 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22512 that's wrong if we deduce the same argument pack from multiple
22513 function arguments: it's only incomplete the first time. */
22514
22515 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22516 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22517
22518 if (TREE_VEC_LENGTH (deduced_pack)
22519 < TREE_VEC_LENGTH (explicit_pack))
22520 return 0;
22521
22522 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22523 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22524 TREE_VEC_ELT (deduced_pack, j)))
22525 return 0;
22526 }
22527 else if (oldelt && !template_args_equal (oldelt, elt))
22528 return 0;
22529 }
22530
22531 for (i = nargs; i--; )
22532 {
22533 tree elt = TREE_VEC_ELT (tempargs, i);
22534
22535 if (elt)
22536 TREE_VEC_ELT (targs, i) = elt;
22537 }
22538
22539 return 1;
22540 }
22541
22542 /* PARM is a template class (perhaps with unbound template
22543 parameters). ARG is a fully instantiated type. If ARG can be
22544 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22545 TARGS are as for unify. */
22546
22547 static tree
22548 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22549 bool explain_p)
22550 {
22551 tree copy_of_targs;
22552
22553 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22554 return NULL_TREE;
22555 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22556 /* Matches anything. */;
22557 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22558 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22559 return NULL_TREE;
22560
22561 /* We need to make a new template argument vector for the call to
22562 unify. If we used TARGS, we'd clutter it up with the result of
22563 the attempted unification, even if this class didn't work out.
22564 We also don't want to commit ourselves to all the unifications
22565 we've already done, since unification is supposed to be done on
22566 an argument-by-argument basis. In other words, consider the
22567 following pathological case:
22568
22569 template <int I, int J, int K>
22570 struct S {};
22571
22572 template <int I, int J>
22573 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22574
22575 template <int I, int J, int K>
22576 void f(S<I, J, K>, S<I, I, I>);
22577
22578 void g() {
22579 S<0, 0, 0> s0;
22580 S<0, 1, 2> s2;
22581
22582 f(s0, s2);
22583 }
22584
22585 Now, by the time we consider the unification involving `s2', we
22586 already know that we must have `f<0, 0, 0>'. But, even though
22587 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22588 because there are two ways to unify base classes of S<0, 1, 2>
22589 with S<I, I, I>. If we kept the already deduced knowledge, we
22590 would reject the possibility I=1. */
22591 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22592
22593 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22594 {
22595 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22596 return NULL_TREE;
22597 return arg;
22598 }
22599
22600 /* If unification failed, we're done. */
22601 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22602 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22603 return NULL_TREE;
22604
22605 return arg;
22606 }
22607
22608 /* Given a template type PARM and a class type ARG, find the unique
22609 base type in ARG that is an instance of PARM. We do not examine
22610 ARG itself; only its base-classes. If there is not exactly one
22611 appropriate base class, return NULL_TREE. PARM may be the type of
22612 a partial specialization, as well as a plain template type. Used
22613 by unify. */
22614
22615 static enum template_base_result
22616 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22617 bool explain_p, tree *result)
22618 {
22619 tree rval = NULL_TREE;
22620 tree binfo;
22621
22622 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22623
22624 binfo = TYPE_BINFO (complete_type (arg));
22625 if (!binfo)
22626 {
22627 /* The type could not be completed. */
22628 *result = NULL_TREE;
22629 return tbr_incomplete_type;
22630 }
22631
22632 /* Walk in inheritance graph order. The search order is not
22633 important, and this avoids multiple walks of virtual bases. */
22634 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22635 {
22636 tree r = try_class_unification (tparms, targs, parm,
22637 BINFO_TYPE (binfo), explain_p);
22638
22639 if (r)
22640 {
22641 /* If there is more than one satisfactory baseclass, then:
22642
22643 [temp.deduct.call]
22644
22645 If they yield more than one possible deduced A, the type
22646 deduction fails.
22647
22648 applies. */
22649 if (rval && !same_type_p (r, rval))
22650 {
22651 *result = NULL_TREE;
22652 return tbr_ambiguous_baseclass;
22653 }
22654
22655 rval = r;
22656 }
22657 }
22658
22659 *result = rval;
22660 return tbr_success;
22661 }
22662
22663 /* Returns the level of DECL, which declares a template parameter. */
22664
22665 static int
22666 template_decl_level (tree decl)
22667 {
22668 switch (TREE_CODE (decl))
22669 {
22670 case TYPE_DECL:
22671 case TEMPLATE_DECL:
22672 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22673
22674 case PARM_DECL:
22675 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22676
22677 default:
22678 gcc_unreachable ();
22679 }
22680 return 0;
22681 }
22682
22683 /* Decide whether ARG can be unified with PARM, considering only the
22684 cv-qualifiers of each type, given STRICT as documented for unify.
22685 Returns nonzero iff the unification is OK on that basis. */
22686
22687 static int
22688 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22689 {
22690 int arg_quals = cp_type_quals (arg);
22691 int parm_quals = cp_type_quals (parm);
22692
22693 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22694 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22695 {
22696 /* Although a CVR qualifier is ignored when being applied to a
22697 substituted template parameter ([8.3.2]/1 for example), that
22698 does not allow us to unify "const T" with "int&" because both
22699 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22700 It is ok when we're allowing additional CV qualifiers
22701 at the outer level [14.8.2.1]/3,1st bullet. */
22702 if ((TYPE_REF_P (arg)
22703 || FUNC_OR_METHOD_TYPE_P (arg))
22704 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22705 return 0;
22706
22707 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22708 && (parm_quals & TYPE_QUAL_RESTRICT))
22709 return 0;
22710 }
22711
22712 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22713 && (arg_quals & parm_quals) != parm_quals)
22714 return 0;
22715
22716 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22717 && (parm_quals & arg_quals) != arg_quals)
22718 return 0;
22719
22720 return 1;
22721 }
22722
22723 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22724 void
22725 template_parm_level_and_index (tree parm, int* level, int* index)
22726 {
22727 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22728 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22729 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22730 {
22731 *index = TEMPLATE_TYPE_IDX (parm);
22732 *level = TEMPLATE_TYPE_LEVEL (parm);
22733 }
22734 else
22735 {
22736 *index = TEMPLATE_PARM_IDX (parm);
22737 *level = TEMPLATE_PARM_LEVEL (parm);
22738 }
22739 }
22740
22741 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22742 do { \
22743 if (unify (TP, TA, P, A, S, EP)) \
22744 return 1; \
22745 } while (0)
22746
22747 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22748 expansion at the end of PACKED_PARMS. Returns 0 if the type
22749 deduction succeeds, 1 otherwise. STRICT is the same as in
22750 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22751 function call argument list. We'll need to adjust the arguments to make them
22752 types. SUBR tells us if this is from a recursive call to
22753 type_unification_real, or for comparing two template argument
22754 lists. */
22755
22756 static int
22757 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22758 tree packed_args, unification_kind_t strict,
22759 bool subr, bool explain_p)
22760 {
22761 tree parm
22762 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22763 tree pattern = PACK_EXPANSION_PATTERN (parm);
22764 tree pack, packs = NULL_TREE;
22765 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22766
22767 /* Add in any args remembered from an earlier partial instantiation. */
22768 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22769 int levels = TMPL_ARGS_DEPTH (targs);
22770
22771 packed_args = expand_template_argument_pack (packed_args);
22772
22773 int len = TREE_VEC_LENGTH (packed_args);
22774
22775 /* Determine the parameter packs we will be deducing from the
22776 pattern, and record their current deductions. */
22777 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22778 pack; pack = TREE_CHAIN (pack))
22779 {
22780 tree parm_pack = TREE_VALUE (pack);
22781 int idx, level;
22782
22783 /* Only template parameter packs can be deduced, not e.g. function
22784 parameter packs or __bases or __integer_pack. */
22785 if (!TEMPLATE_PARM_P (parm_pack))
22786 continue;
22787
22788 /* Determine the index and level of this parameter pack. */
22789 template_parm_level_and_index (parm_pack, &level, &idx);
22790 if (level < levels)
22791 continue;
22792
22793 /* Keep track of the parameter packs and their corresponding
22794 argument packs. */
22795 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22796 TREE_TYPE (packs) = make_tree_vec (len - start);
22797 }
22798
22799 /* Loop through all of the arguments that have not yet been
22800 unified and unify each with the pattern. */
22801 for (i = start; i < len; i++)
22802 {
22803 tree parm;
22804 bool any_explicit = false;
22805 tree arg = TREE_VEC_ELT (packed_args, i);
22806
22807 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22808 or the element of its argument pack at the current index if
22809 this argument was explicitly specified. */
22810 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22811 {
22812 int idx, level;
22813 tree arg, pargs;
22814 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22815
22816 arg = NULL_TREE;
22817 if (TREE_VALUE (pack)
22818 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22819 && (i - start < TREE_VEC_LENGTH (pargs)))
22820 {
22821 any_explicit = true;
22822 arg = TREE_VEC_ELT (pargs, i - start);
22823 }
22824 TMPL_ARG (targs, level, idx) = arg;
22825 }
22826
22827 /* If we had explicit template arguments, substitute them into the
22828 pattern before deduction. */
22829 if (any_explicit)
22830 {
22831 /* Some arguments might still be unspecified or dependent. */
22832 bool dependent;
22833 ++processing_template_decl;
22834 dependent = any_dependent_template_arguments_p (targs);
22835 if (!dependent)
22836 --processing_template_decl;
22837 parm = tsubst (pattern, targs,
22838 explain_p ? tf_warning_or_error : tf_none,
22839 NULL_TREE);
22840 if (dependent)
22841 --processing_template_decl;
22842 if (parm == error_mark_node)
22843 return 1;
22844 }
22845 else
22846 parm = pattern;
22847
22848 /* Unify the pattern with the current argument. */
22849 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22850 explain_p))
22851 return 1;
22852
22853 /* For each parameter pack, collect the deduced value. */
22854 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22855 {
22856 int idx, level;
22857 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22858
22859 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22860 TMPL_ARG (targs, level, idx);
22861 }
22862 }
22863
22864 /* Verify that the results of unification with the parameter packs
22865 produce results consistent with what we've seen before, and make
22866 the deduced argument packs available. */
22867 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22868 {
22869 tree old_pack = TREE_VALUE (pack);
22870 tree new_args = TREE_TYPE (pack);
22871 int i, len = TREE_VEC_LENGTH (new_args);
22872 int idx, level;
22873 bool nondeduced_p = false;
22874
22875 /* By default keep the original deduced argument pack.
22876 If necessary, more specific code is going to update the
22877 resulting deduced argument later down in this function. */
22878 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22879 TMPL_ARG (targs, level, idx) = old_pack;
22880
22881 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22882 actually deduce anything. */
22883 for (i = 0; i < len && !nondeduced_p; ++i)
22884 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22885 nondeduced_p = true;
22886 if (nondeduced_p)
22887 continue;
22888
22889 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22890 {
22891 /* If we had fewer function args than explicit template args,
22892 just use the explicits. */
22893 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22894 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22895 if (len < explicit_len)
22896 new_args = explicit_args;
22897 }
22898
22899 if (!old_pack)
22900 {
22901 tree result;
22902 /* Build the deduced *_ARGUMENT_PACK. */
22903 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22904 {
22905 result = make_node (NONTYPE_ARGUMENT_PACK);
22906 TREE_CONSTANT (result) = 1;
22907 }
22908 else
22909 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22910
22911 SET_ARGUMENT_PACK_ARGS (result, new_args);
22912
22913 /* Note the deduced argument packs for this parameter
22914 pack. */
22915 TMPL_ARG (targs, level, idx) = result;
22916 }
22917 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22918 && (ARGUMENT_PACK_ARGS (old_pack)
22919 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22920 {
22921 /* We only had the explicitly-provided arguments before, but
22922 now we have a complete set of arguments. */
22923 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22924
22925 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22926 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22927 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22928 }
22929 else
22930 {
22931 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22932 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22933 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
22934 /* During template argument deduction for the aggregate deduction
22935 candidate, the number of elements in a trailing parameter pack
22936 is only deduced from the number of remaining function
22937 arguments if it is not otherwise deduced. */
22938 if (cxx_dialect >= cxx20
22939 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
22940 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
22941 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
22942 if (!comp_template_args (old_args, new_args,
22943 &bad_old_arg, &bad_new_arg))
22944 /* Inconsistent unification of this parameter pack. */
22945 return unify_parameter_pack_inconsistent (explain_p,
22946 bad_old_arg,
22947 bad_new_arg);
22948 }
22949 }
22950
22951 return unify_success (explain_p);
22952 }
22953
22954 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22955 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22956 parameters and return value are as for unify. */
22957
22958 static int
22959 unify_array_domain (tree tparms, tree targs,
22960 tree parm_dom, tree arg_dom,
22961 bool explain_p)
22962 {
22963 tree parm_max;
22964 tree arg_max;
22965 bool parm_cst;
22966 bool arg_cst;
22967
22968 /* Our representation of array types uses "N - 1" as the
22969 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22970 not an integer constant. We cannot unify arbitrarily
22971 complex expressions, so we eliminate the MINUS_EXPRs
22972 here. */
22973 parm_max = TYPE_MAX_VALUE (parm_dom);
22974 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22975 if (!parm_cst)
22976 {
22977 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22978 parm_max = TREE_OPERAND (parm_max, 0);
22979 }
22980 arg_max = TYPE_MAX_VALUE (arg_dom);
22981 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22982 if (!arg_cst)
22983 {
22984 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22985 trying to unify the type of a variable with the type
22986 of a template parameter. For example:
22987
22988 template <unsigned int N>
22989 void f (char (&) [N]);
22990 int g();
22991 void h(int i) {
22992 char a[g(i)];
22993 f(a);
22994 }
22995
22996 Here, the type of the ARG will be "int [g(i)]", and
22997 may be a SAVE_EXPR, etc. */
22998 if (TREE_CODE (arg_max) != MINUS_EXPR)
22999 return unify_vla_arg (explain_p, arg_dom);
23000 arg_max = TREE_OPERAND (arg_max, 0);
23001 }
23002
23003 /* If only one of the bounds used a MINUS_EXPR, compensate
23004 by adding one to the other bound. */
23005 if (parm_cst && !arg_cst)
23006 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
23007 integer_type_node,
23008 parm_max,
23009 integer_one_node);
23010 else if (arg_cst && !parm_cst)
23011 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
23012 integer_type_node,
23013 arg_max,
23014 integer_one_node);
23015
23016 return unify (tparms, targs, parm_max, arg_max,
23017 UNIFY_ALLOW_INTEGER, explain_p);
23018 }
23019
23020 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23021
23022 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
23023
23024 static pa_kind_t
23025 pa_kind (tree t)
23026 {
23027 if (PACK_EXPANSION_P (t))
23028 t = PACK_EXPANSION_PATTERN (t);
23029 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23030 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23031 || DECL_TYPE_TEMPLATE_P (t))
23032 return pa_tmpl;
23033 else if (TYPE_P (t))
23034 return pa_type;
23035 else
23036 return pa_expr;
23037 }
23038
23039 /* Deduce the value of template parameters. TPARMS is the (innermost)
23040 set of template parameters to a template. TARGS is the bindings
23041 for those template parameters, as determined thus far; TARGS may
23042 include template arguments for outer levels of template parameters
23043 as well. PARM is a parameter to a template function, or a
23044 subcomponent of that parameter; ARG is the corresponding argument.
23045 This function attempts to match PARM with ARG in a manner
23046 consistent with the existing assignments in TARGS. If more values
23047 are deduced, then TARGS is updated.
23048
23049 Returns 0 if the type deduction succeeds, 1 otherwise. The
23050 parameter STRICT is a bitwise or of the following flags:
23051
23052 UNIFY_ALLOW_NONE:
23053 Require an exact match between PARM and ARG.
23054 UNIFY_ALLOW_MORE_CV_QUAL:
23055 Allow the deduced ARG to be more cv-qualified (by qualification
23056 conversion) than ARG.
23057 UNIFY_ALLOW_LESS_CV_QUAL:
23058 Allow the deduced ARG to be less cv-qualified than ARG.
23059 UNIFY_ALLOW_DERIVED:
23060 Allow the deduced ARG to be a template base class of ARG,
23061 or a pointer to a template base class of the type pointed to by
23062 ARG.
23063 UNIFY_ALLOW_INTEGER:
23064 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23065 case for more information.
23066 UNIFY_ALLOW_OUTER_LEVEL:
23067 This is the outermost level of a deduction. Used to determine validity
23068 of qualification conversions. A valid qualification conversion must
23069 have const qualified pointers leading up to the inner type which
23070 requires additional CV quals, except at the outer level, where const
23071 is not required [conv.qual]. It would be normal to set this flag in
23072 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23073 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23074 This is the outermost level of a deduction, and PARM can be more CV
23075 qualified at this point.
23076 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23077 This is the outermost level of a deduction, and PARM can be less CV
23078 qualified at this point. */
23079
23080 static int
23081 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23082 bool explain_p)
23083 {
23084 int idx;
23085 tree targ;
23086 tree tparm;
23087 int strict_in = strict;
23088 tsubst_flags_t complain = (explain_p
23089 ? tf_warning_or_error
23090 : tf_none);
23091
23092 /* I don't think this will do the right thing with respect to types.
23093 But the only case I've seen it in so far has been array bounds, where
23094 signedness is the only information lost, and I think that will be
23095 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23096 finish_id_expression_1, and are also OK. */
23097 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23098 parm = TREE_OPERAND (parm, 0);
23099
23100 if (arg == error_mark_node)
23101 return unify_invalid (explain_p);
23102 if (arg == unknown_type_node
23103 || arg == init_list_type_node)
23104 /* We can't deduce anything from this, but we might get all the
23105 template args from other function args. */
23106 return unify_success (explain_p);
23107
23108 if (parm == any_targ_node || arg == any_targ_node)
23109 return unify_success (explain_p);
23110
23111 /* If PARM uses template parameters, then we can't bail out here,
23112 even if ARG == PARM, since we won't record unifications for the
23113 template parameters. We might need them if we're trying to
23114 figure out which of two things is more specialized. */
23115 if (arg == parm && !uses_template_parms (parm))
23116 return unify_success (explain_p);
23117
23118 /* Handle init lists early, so the rest of the function can assume
23119 we're dealing with a type. */
23120 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23121 {
23122 tree elt, elttype;
23123 unsigned i;
23124 tree orig_parm = parm;
23125
23126 if (!is_std_init_list (parm)
23127 && TREE_CODE (parm) != ARRAY_TYPE)
23128 /* We can only deduce from an initializer list argument if the
23129 parameter is std::initializer_list or an array; otherwise this
23130 is a non-deduced context. */
23131 return unify_success (explain_p);
23132
23133 if (TREE_CODE (parm) == ARRAY_TYPE)
23134 elttype = TREE_TYPE (parm);
23135 else
23136 {
23137 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23138 /* Deduction is defined in terms of a single type, so just punt
23139 on the (bizarre) std::initializer_list<T...>. */
23140 if (PACK_EXPANSION_P (elttype))
23141 return unify_success (explain_p);
23142 }
23143
23144 if (strict != DEDUCE_EXACT
23145 && TYPE_P (elttype)
23146 && !uses_deducible_template_parms (elttype))
23147 /* If ELTTYPE has no deducible template parms, skip deduction from
23148 the list elements. */;
23149 else
23150 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23151 {
23152 int elt_strict = strict;
23153
23154 if (elt == error_mark_node)
23155 return unify_invalid (explain_p);
23156
23157 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23158 {
23159 tree type = TREE_TYPE (elt);
23160 if (type == error_mark_node)
23161 return unify_invalid (explain_p);
23162 /* It should only be possible to get here for a call. */
23163 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23164 elt_strict |= maybe_adjust_types_for_deduction
23165 (DEDUCE_CALL, &elttype, &type, elt);
23166 elt = type;
23167 }
23168
23169 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23170 explain_p);
23171 }
23172
23173 if (TREE_CODE (parm) == ARRAY_TYPE
23174 && deducible_array_bound (TYPE_DOMAIN (parm)))
23175 {
23176 /* Also deduce from the length of the initializer list. */
23177 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23178 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23179 if (idx == error_mark_node)
23180 return unify_invalid (explain_p);
23181 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23182 idx, explain_p);
23183 }
23184
23185 /* If the std::initializer_list<T> deduction worked, replace the
23186 deduced A with std::initializer_list<A>. */
23187 if (orig_parm != parm)
23188 {
23189 idx = TEMPLATE_TYPE_IDX (orig_parm);
23190 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23191 targ = listify (targ);
23192 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23193 }
23194 return unify_success (explain_p);
23195 }
23196
23197 /* If parm and arg aren't the same kind of thing (template, type, or
23198 expression), fail early. */
23199 if (pa_kind (parm) != pa_kind (arg))
23200 return unify_invalid (explain_p);
23201
23202 /* Immediately reject some pairs that won't unify because of
23203 cv-qualification mismatches. */
23204 if (TREE_CODE (arg) == TREE_CODE (parm)
23205 && TYPE_P (arg)
23206 /* It is the elements of the array which hold the cv quals of an array
23207 type, and the elements might be template type parms. We'll check
23208 when we recurse. */
23209 && TREE_CODE (arg) != ARRAY_TYPE
23210 /* We check the cv-qualifiers when unifying with template type
23211 parameters below. We want to allow ARG `const T' to unify with
23212 PARM `T' for example, when computing which of two templates
23213 is more specialized, for example. */
23214 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23215 && !check_cv_quals_for_unify (strict_in, arg, parm))
23216 return unify_cv_qual_mismatch (explain_p, parm, arg);
23217
23218 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23219 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23220 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23221 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23222 strict &= ~UNIFY_ALLOW_DERIVED;
23223 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23224 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23225
23226 switch (TREE_CODE (parm))
23227 {
23228 case TYPENAME_TYPE:
23229 case SCOPE_REF:
23230 case UNBOUND_CLASS_TEMPLATE:
23231 /* In a type which contains a nested-name-specifier, template
23232 argument values cannot be deduced for template parameters used
23233 within the nested-name-specifier. */
23234 return unify_success (explain_p);
23235
23236 case TEMPLATE_TYPE_PARM:
23237 case TEMPLATE_TEMPLATE_PARM:
23238 case BOUND_TEMPLATE_TEMPLATE_PARM:
23239 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23240 if (error_operand_p (tparm))
23241 return unify_invalid (explain_p);
23242
23243 if (TEMPLATE_TYPE_LEVEL (parm)
23244 != template_decl_level (tparm))
23245 /* The PARM is not one we're trying to unify. Just check
23246 to see if it matches ARG. */
23247 {
23248 if (TREE_CODE (arg) == TREE_CODE (parm)
23249 && (is_auto (parm) ? is_auto (arg)
23250 : same_type_p (parm, arg)))
23251 return unify_success (explain_p);
23252 else
23253 return unify_type_mismatch (explain_p, parm, arg);
23254 }
23255 idx = TEMPLATE_TYPE_IDX (parm);
23256 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23257 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23258 if (error_operand_p (tparm))
23259 return unify_invalid (explain_p);
23260
23261 /* Check for mixed types and values. */
23262 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23263 && TREE_CODE (tparm) != TYPE_DECL)
23264 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23265 && TREE_CODE (tparm) != TEMPLATE_DECL))
23266 gcc_unreachable ();
23267
23268 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23269 {
23270 if ((strict_in & UNIFY_ALLOW_DERIVED)
23271 && CLASS_TYPE_P (arg))
23272 {
23273 /* First try to match ARG directly. */
23274 tree t = try_class_unification (tparms, targs, parm, arg,
23275 explain_p);
23276 if (!t)
23277 {
23278 /* Otherwise, look for a suitable base of ARG, as below. */
23279 enum template_base_result r;
23280 r = get_template_base (tparms, targs, parm, arg,
23281 explain_p, &t);
23282 if (!t)
23283 return unify_no_common_base (explain_p, r, parm, arg);
23284 arg = t;
23285 }
23286 }
23287 /* ARG must be constructed from a template class or a template
23288 template parameter. */
23289 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23290 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23291 return unify_template_deduction_failure (explain_p, parm, arg);
23292
23293 /* Deduce arguments T, i from TT<T> or TT<i>. */
23294 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23295 return 1;
23296
23297 arg = TYPE_TI_TEMPLATE (arg);
23298
23299 /* Fall through to deduce template name. */
23300 }
23301
23302 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23303 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23304 {
23305 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23306
23307 /* Simple cases: Value already set, does match or doesn't. */
23308 if (targ != NULL_TREE && template_args_equal (targ, arg))
23309 return unify_success (explain_p);
23310 else if (targ)
23311 return unify_inconsistency (explain_p, parm, targ, arg);
23312 }
23313 else
23314 {
23315 /* If PARM is `const T' and ARG is only `int', we don't have
23316 a match unless we are allowing additional qualification.
23317 If ARG is `const int' and PARM is just `T' that's OK;
23318 that binds `const int' to `T'. */
23319 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23320 arg, parm))
23321 return unify_cv_qual_mismatch (explain_p, parm, arg);
23322
23323 /* Consider the case where ARG is `const volatile int' and
23324 PARM is `const T'. Then, T should be `volatile int'. */
23325 arg = cp_build_qualified_type_real
23326 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23327 if (arg == error_mark_node)
23328 return unify_invalid (explain_p);
23329
23330 /* Simple cases: Value already set, does match or doesn't. */
23331 if (targ != NULL_TREE && same_type_p (targ, arg))
23332 return unify_success (explain_p);
23333 else if (targ)
23334 return unify_inconsistency (explain_p, parm, targ, arg);
23335
23336 /* Make sure that ARG is not a variable-sized array. (Note
23337 that were talking about variable-sized arrays (like
23338 `int[n]'), rather than arrays of unknown size (like
23339 `int[]').) We'll get very confused by such a type since
23340 the bound of the array is not constant, and therefore
23341 not mangleable. Besides, such types are not allowed in
23342 ISO C++, so we can do as we please here. We do allow
23343 them for 'auto' deduction, since that isn't ABI-exposed. */
23344 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23345 return unify_vla_arg (explain_p, arg);
23346
23347 /* Strip typedefs as in convert_template_argument. */
23348 arg = canonicalize_type_argument (arg, tf_none);
23349 }
23350
23351 /* If ARG is a parameter pack or an expansion, we cannot unify
23352 against it unless PARM is also a parameter pack. */
23353 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23354 && !template_parameter_pack_p (parm))
23355 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23356
23357 /* If the argument deduction results is a METHOD_TYPE,
23358 then there is a problem.
23359 METHOD_TYPE doesn't map to any real C++ type the result of
23360 the deduction cannot be of that type. */
23361 if (TREE_CODE (arg) == METHOD_TYPE)
23362 return unify_method_type_error (explain_p, arg);
23363
23364 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23365 return unify_success (explain_p);
23366
23367 case TEMPLATE_PARM_INDEX:
23368 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23369 if (error_operand_p (tparm))
23370 return unify_invalid (explain_p);
23371
23372 if (TEMPLATE_PARM_LEVEL (parm)
23373 != template_decl_level (tparm))
23374 {
23375 /* The PARM is not one we're trying to unify. Just check
23376 to see if it matches ARG. */
23377 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23378 && cp_tree_equal (parm, arg));
23379 if (result)
23380 unify_expression_unequal (explain_p, parm, arg);
23381 return result;
23382 }
23383
23384 idx = TEMPLATE_PARM_IDX (parm);
23385 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23386
23387 if (targ)
23388 {
23389 if ((strict & UNIFY_ALLOW_INTEGER)
23390 && TREE_TYPE (targ) && TREE_TYPE (arg)
23391 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23392 /* We're deducing from an array bound, the type doesn't matter. */
23393 arg = fold_convert (TREE_TYPE (targ), arg);
23394 int x = !cp_tree_equal (targ, arg);
23395 if (x)
23396 unify_inconsistency (explain_p, parm, targ, arg);
23397 return x;
23398 }
23399
23400 /* [temp.deduct.type] If, in the declaration of a function template
23401 with a non-type template-parameter, the non-type
23402 template-parameter is used in an expression in the function
23403 parameter-list and, if the corresponding template-argument is
23404 deduced, the template-argument type shall match the type of the
23405 template-parameter exactly, except that a template-argument
23406 deduced from an array bound may be of any integral type.
23407 The non-type parameter might use already deduced type parameters. */
23408 tparm = TREE_TYPE (parm);
23409 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23410 /* We don't have enough levels of args to do any substitution. This
23411 can happen in the context of -fnew-ttp-matching. */;
23412 else
23413 {
23414 ++processing_template_decl;
23415 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23416 --processing_template_decl;
23417
23418 if (tree a = type_uses_auto (tparm))
23419 {
23420 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23421 if (tparm == error_mark_node)
23422 return 1;
23423 }
23424 }
23425
23426 if (!TREE_TYPE (arg))
23427 /* Template-parameter dependent expression. Just accept it for now.
23428 It will later be processed in convert_template_argument. */
23429 ;
23430 else if (same_type_ignoring_top_level_qualifiers_p
23431 (non_reference (TREE_TYPE (arg)),
23432 non_reference (tparm)))
23433 /* OK. Ignore top-level quals here because a class-type template
23434 parameter object is const. */;
23435 else if ((strict & UNIFY_ALLOW_INTEGER)
23436 && CP_INTEGRAL_TYPE_P (tparm))
23437 /* Convert the ARG to the type of PARM; the deduced non-type
23438 template argument must exactly match the types of the
23439 corresponding parameter. */
23440 arg = fold (build_nop (tparm, arg));
23441 else if (uses_template_parms (tparm))
23442 {
23443 /* We haven't deduced the type of this parameter yet. */
23444 if (cxx_dialect >= cxx17
23445 /* We deduce from array bounds in try_array_deduction. */
23446 && !(strict & UNIFY_ALLOW_INTEGER))
23447 {
23448 /* Deduce it from the non-type argument. */
23449 tree atype = TREE_TYPE (arg);
23450 RECUR_AND_CHECK_FAILURE (tparms, targs,
23451 tparm, atype,
23452 UNIFY_ALLOW_NONE, explain_p);
23453 }
23454 else
23455 /* Try again later. */
23456 return unify_success (explain_p);
23457 }
23458 else
23459 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23460
23461 /* If ARG is a parameter pack or an expansion, we cannot unify
23462 against it unless PARM is also a parameter pack. */
23463 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23464 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23465 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23466
23467 {
23468 bool removed_attr = false;
23469 arg = strip_typedefs_expr (arg, &removed_attr);
23470 }
23471 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23472 return unify_success (explain_p);
23473
23474 case PTRMEM_CST:
23475 {
23476 /* A pointer-to-member constant can be unified only with
23477 another constant. */
23478 if (TREE_CODE (arg) != PTRMEM_CST)
23479 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23480
23481 /* Just unify the class member. It would be useless (and possibly
23482 wrong, depending on the strict flags) to unify also
23483 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23484 arg refer to the same variable, even if through different
23485 classes. For instance:
23486
23487 struct A { int x; };
23488 struct B : A { };
23489
23490 Unification of &A::x and &B::x must succeed. */
23491 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23492 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23493 }
23494
23495 case POINTER_TYPE:
23496 {
23497 if (!TYPE_PTR_P (arg))
23498 return unify_type_mismatch (explain_p, parm, arg);
23499
23500 /* [temp.deduct.call]
23501
23502 A can be another pointer or pointer to member type that can
23503 be converted to the deduced A via a qualification
23504 conversion (_conv.qual_).
23505
23506 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23507 This will allow for additional cv-qualification of the
23508 pointed-to types if appropriate. */
23509
23510 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23511 /* The derived-to-base conversion only persists through one
23512 level of pointers. */
23513 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23514
23515 return unify (tparms, targs, TREE_TYPE (parm),
23516 TREE_TYPE (arg), strict, explain_p);
23517 }
23518
23519 case REFERENCE_TYPE:
23520 if (!TYPE_REF_P (arg))
23521 return unify_type_mismatch (explain_p, parm, arg);
23522 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23523 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23524
23525 case ARRAY_TYPE:
23526 if (TREE_CODE (arg) != ARRAY_TYPE)
23527 return unify_type_mismatch (explain_p, parm, arg);
23528 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23529 != (TYPE_DOMAIN (arg) == NULL_TREE))
23530 return unify_type_mismatch (explain_p, parm, arg);
23531 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23532 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23533 if (TYPE_DOMAIN (parm) != NULL_TREE)
23534 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23535 TYPE_DOMAIN (arg), explain_p);
23536 return unify_success (explain_p);
23537
23538 case REAL_TYPE:
23539 case COMPLEX_TYPE:
23540 case VECTOR_TYPE:
23541 case INTEGER_TYPE:
23542 case BOOLEAN_TYPE:
23543 case ENUMERAL_TYPE:
23544 case VOID_TYPE:
23545 case NULLPTR_TYPE:
23546 if (TREE_CODE (arg) != TREE_CODE (parm))
23547 return unify_type_mismatch (explain_p, parm, arg);
23548
23549 /* We have already checked cv-qualification at the top of the
23550 function. */
23551 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23552 return unify_type_mismatch (explain_p, parm, arg);
23553
23554 /* As far as unification is concerned, this wins. Later checks
23555 will invalidate it if necessary. */
23556 return unify_success (explain_p);
23557
23558 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23559 /* Type INTEGER_CST can come from ordinary constant template args. */
23560 case INTEGER_CST:
23561 while (CONVERT_EXPR_P (arg))
23562 arg = TREE_OPERAND (arg, 0);
23563
23564 if (TREE_CODE (arg) != INTEGER_CST)
23565 return unify_template_argument_mismatch (explain_p, parm, arg);
23566 return (tree_int_cst_equal (parm, arg)
23567 ? unify_success (explain_p)
23568 : unify_template_argument_mismatch (explain_p, parm, arg));
23569
23570 case TREE_VEC:
23571 {
23572 int i, len, argslen;
23573 int parm_variadic_p = 0;
23574
23575 if (TREE_CODE (arg) != TREE_VEC)
23576 return unify_template_argument_mismatch (explain_p, parm, arg);
23577
23578 len = TREE_VEC_LENGTH (parm);
23579 argslen = TREE_VEC_LENGTH (arg);
23580
23581 /* Check for pack expansions in the parameters. */
23582 for (i = 0; i < len; ++i)
23583 {
23584 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23585 {
23586 if (i == len - 1)
23587 /* We can unify against something with a trailing
23588 parameter pack. */
23589 parm_variadic_p = 1;
23590 else
23591 /* [temp.deduct.type]/9: If the template argument list of
23592 P contains a pack expansion that is not the last
23593 template argument, the entire template argument list
23594 is a non-deduced context. */
23595 return unify_success (explain_p);
23596 }
23597 }
23598
23599 /* If we don't have enough arguments to satisfy the parameters
23600 (not counting the pack expression at the end), or we have
23601 too many arguments for a parameter list that doesn't end in
23602 a pack expression, we can't unify. */
23603 if (parm_variadic_p
23604 ? argslen < len - parm_variadic_p
23605 : argslen != len)
23606 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23607
23608 /* Unify all of the parameters that precede the (optional)
23609 pack expression. */
23610 for (i = 0; i < len - parm_variadic_p; ++i)
23611 {
23612 RECUR_AND_CHECK_FAILURE (tparms, targs,
23613 TREE_VEC_ELT (parm, i),
23614 TREE_VEC_ELT (arg, i),
23615 UNIFY_ALLOW_NONE, explain_p);
23616 }
23617 if (parm_variadic_p)
23618 return unify_pack_expansion (tparms, targs, parm, arg,
23619 DEDUCE_EXACT,
23620 /*subr=*/true, explain_p);
23621 return unify_success (explain_p);
23622 }
23623
23624 case RECORD_TYPE:
23625 case UNION_TYPE:
23626 if (TREE_CODE (arg) != TREE_CODE (parm))
23627 return unify_type_mismatch (explain_p, parm, arg);
23628
23629 if (TYPE_PTRMEMFUNC_P (parm))
23630 {
23631 if (!TYPE_PTRMEMFUNC_P (arg))
23632 return unify_type_mismatch (explain_p, parm, arg);
23633
23634 return unify (tparms, targs,
23635 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23636 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23637 strict, explain_p);
23638 }
23639 else if (TYPE_PTRMEMFUNC_P (arg))
23640 return unify_type_mismatch (explain_p, parm, arg);
23641
23642 if (CLASSTYPE_TEMPLATE_INFO (parm))
23643 {
23644 tree t = NULL_TREE;
23645
23646 if (strict_in & UNIFY_ALLOW_DERIVED)
23647 {
23648 /* First, we try to unify the PARM and ARG directly. */
23649 t = try_class_unification (tparms, targs,
23650 parm, arg, explain_p);
23651
23652 if (!t)
23653 {
23654 /* Fallback to the special case allowed in
23655 [temp.deduct.call]:
23656
23657 If P is a class, and P has the form
23658 template-id, then A can be a derived class of
23659 the deduced A. Likewise, if P is a pointer to
23660 a class of the form template-id, A can be a
23661 pointer to a derived class pointed to by the
23662 deduced A. */
23663 enum template_base_result r;
23664 r = get_template_base (tparms, targs, parm, arg,
23665 explain_p, &t);
23666
23667 if (!t)
23668 {
23669 /* Don't give the derived diagnostic if we're
23670 already dealing with the same template. */
23671 bool same_template
23672 = (CLASSTYPE_TEMPLATE_INFO (arg)
23673 && (CLASSTYPE_TI_TEMPLATE (parm)
23674 == CLASSTYPE_TI_TEMPLATE (arg)));
23675 return unify_no_common_base (explain_p && !same_template,
23676 r, parm, arg);
23677 }
23678 }
23679 }
23680 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23681 && (CLASSTYPE_TI_TEMPLATE (parm)
23682 == CLASSTYPE_TI_TEMPLATE (arg)))
23683 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23684 Then, we should unify `int' and `U'. */
23685 t = arg;
23686 else
23687 /* There's no chance of unification succeeding. */
23688 return unify_type_mismatch (explain_p, parm, arg);
23689
23690 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23691 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23692 }
23693 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23694 return unify_type_mismatch (explain_p, parm, arg);
23695 return unify_success (explain_p);
23696
23697 case METHOD_TYPE:
23698 case FUNCTION_TYPE:
23699 {
23700 unsigned int nargs;
23701 tree *args;
23702 tree a;
23703 unsigned int i;
23704
23705 if (TREE_CODE (arg) != TREE_CODE (parm))
23706 return unify_type_mismatch (explain_p, parm, arg);
23707
23708 /* CV qualifications for methods can never be deduced, they must
23709 match exactly. We need to check them explicitly here,
23710 because type_unification_real treats them as any other
23711 cv-qualified parameter. */
23712 if (TREE_CODE (parm) == METHOD_TYPE
23713 && (!check_cv_quals_for_unify
23714 (UNIFY_ALLOW_NONE,
23715 class_of_this_parm (arg),
23716 class_of_this_parm (parm))))
23717 return unify_cv_qual_mismatch (explain_p, parm, arg);
23718 if (TREE_CODE (arg) == FUNCTION_TYPE
23719 && type_memfn_quals (parm) != type_memfn_quals (arg))
23720 return unify_cv_qual_mismatch (explain_p, parm, arg);
23721 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23722 return unify_type_mismatch (explain_p, parm, arg);
23723
23724 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23725 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23726
23727 nargs = list_length (TYPE_ARG_TYPES (arg));
23728 args = XALLOCAVEC (tree, nargs);
23729 for (a = TYPE_ARG_TYPES (arg), i = 0;
23730 a != NULL_TREE && a != void_list_node;
23731 a = TREE_CHAIN (a), ++i)
23732 args[i] = TREE_VALUE (a);
23733 nargs = i;
23734
23735 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23736 args, nargs, 1, DEDUCE_EXACT,
23737 NULL, explain_p))
23738 return 1;
23739
23740 if (flag_noexcept_type)
23741 {
23742 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23743 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23744 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23745 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23746 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23747 && uses_template_parms (TREE_PURPOSE (pspec)))
23748 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23749 TREE_PURPOSE (aspec),
23750 UNIFY_ALLOW_NONE, explain_p);
23751 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23752 return unify_type_mismatch (explain_p, parm, arg);
23753 }
23754
23755 return 0;
23756 }
23757
23758 case OFFSET_TYPE:
23759 /* Unify a pointer to member with a pointer to member function, which
23760 deduces the type of the member as a function type. */
23761 if (TYPE_PTRMEMFUNC_P (arg))
23762 {
23763 /* Check top-level cv qualifiers */
23764 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23765 return unify_cv_qual_mismatch (explain_p, parm, arg);
23766
23767 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23768 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23769 UNIFY_ALLOW_NONE, explain_p);
23770
23771 /* Determine the type of the function we are unifying against. */
23772 tree fntype = static_fn_type (arg);
23773
23774 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23775 }
23776
23777 if (TREE_CODE (arg) != OFFSET_TYPE)
23778 return unify_type_mismatch (explain_p, parm, arg);
23779 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23780 TYPE_OFFSET_BASETYPE (arg),
23781 UNIFY_ALLOW_NONE, explain_p);
23782 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23783 strict, explain_p);
23784
23785 case CONST_DECL:
23786 if (DECL_TEMPLATE_PARM_P (parm))
23787 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23788 if (arg != scalar_constant_value (parm))
23789 return unify_template_argument_mismatch (explain_p, parm, arg);
23790 return unify_success (explain_p);
23791
23792 case FIELD_DECL:
23793 case TEMPLATE_DECL:
23794 /* Matched cases are handled by the ARG == PARM test above. */
23795 return unify_template_argument_mismatch (explain_p, parm, arg);
23796
23797 case VAR_DECL:
23798 /* We might get a variable as a non-type template argument in parm if the
23799 corresponding parameter is type-dependent. Make any necessary
23800 adjustments based on whether arg is a reference. */
23801 if (CONSTANT_CLASS_P (arg))
23802 parm = fold_non_dependent_expr (parm, complain);
23803 else if (REFERENCE_REF_P (arg))
23804 {
23805 tree sub = TREE_OPERAND (arg, 0);
23806 STRIP_NOPS (sub);
23807 if (TREE_CODE (sub) == ADDR_EXPR)
23808 arg = TREE_OPERAND (sub, 0);
23809 }
23810 /* Now use the normal expression code to check whether they match. */
23811 goto expr;
23812
23813 case TYPE_ARGUMENT_PACK:
23814 case NONTYPE_ARGUMENT_PACK:
23815 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23816 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23817
23818 case TYPEOF_TYPE:
23819 case DECLTYPE_TYPE:
23820 case UNDERLYING_TYPE:
23821 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23822 or UNDERLYING_TYPE nodes. */
23823 return unify_success (explain_p);
23824
23825 case ERROR_MARK:
23826 /* Unification fails if we hit an error node. */
23827 return unify_invalid (explain_p);
23828
23829 case INDIRECT_REF:
23830 if (REFERENCE_REF_P (parm))
23831 {
23832 bool pexp = PACK_EXPANSION_P (arg);
23833 if (pexp)
23834 arg = PACK_EXPANSION_PATTERN (arg);
23835 if (REFERENCE_REF_P (arg))
23836 arg = TREE_OPERAND (arg, 0);
23837 if (pexp)
23838 arg = make_pack_expansion (arg, complain);
23839 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23840 strict, explain_p);
23841 }
23842 /* FALLTHRU */
23843
23844 default:
23845 /* An unresolved overload is a nondeduced context. */
23846 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23847 return unify_success (explain_p);
23848 gcc_assert (EXPR_P (parm)
23849 || COMPOUND_LITERAL_P (parm)
23850 || TREE_CODE (parm) == TRAIT_EXPR);
23851 expr:
23852 /* We must be looking at an expression. This can happen with
23853 something like:
23854
23855 template <int I>
23856 void foo(S<I>, S<I + 2>);
23857
23858 or
23859
23860 template<typename T>
23861 void foo(A<T, T{}>);
23862
23863 This is a "non-deduced context":
23864
23865 [deduct.type]
23866
23867 The non-deduced contexts are:
23868
23869 --A non-type template argument or an array bound in which
23870 a subexpression references a template parameter.
23871
23872 In these cases, we assume deduction succeeded, but don't
23873 actually infer any unifications. */
23874
23875 if (!uses_template_parms (parm)
23876 && !template_args_equal (parm, arg))
23877 return unify_expression_unequal (explain_p, parm, arg);
23878 else
23879 return unify_success (explain_p);
23880 }
23881 }
23882 #undef RECUR_AND_CHECK_FAILURE
23883 \f
23884 /* Note that DECL can be defined in this translation unit, if
23885 required. */
23886
23887 static void
23888 mark_definable (tree decl)
23889 {
23890 tree clone;
23891 DECL_NOT_REALLY_EXTERN (decl) = 1;
23892 FOR_EACH_CLONE (clone, decl)
23893 DECL_NOT_REALLY_EXTERN (clone) = 1;
23894 }
23895
23896 /* Called if RESULT is explicitly instantiated, or is a member of an
23897 explicitly instantiated class. */
23898
23899 void
23900 mark_decl_instantiated (tree result, int extern_p)
23901 {
23902 SET_DECL_EXPLICIT_INSTANTIATION (result);
23903
23904 /* If this entity has already been written out, it's too late to
23905 make any modifications. */
23906 if (TREE_ASM_WRITTEN (result))
23907 return;
23908
23909 /* For anonymous namespace we don't need to do anything. */
23910 if (decl_anon_ns_mem_p (result))
23911 {
23912 gcc_assert (!TREE_PUBLIC (result));
23913 return;
23914 }
23915
23916 if (TREE_CODE (result) != FUNCTION_DECL)
23917 /* The TREE_PUBLIC flag for function declarations will have been
23918 set correctly by tsubst. */
23919 TREE_PUBLIC (result) = 1;
23920
23921 /* This might have been set by an earlier implicit instantiation. */
23922 DECL_COMDAT (result) = 0;
23923
23924 if (extern_p)
23925 DECL_NOT_REALLY_EXTERN (result) = 0;
23926 else
23927 {
23928 mark_definable (result);
23929 mark_needed (result);
23930 /* Always make artificials weak. */
23931 if (DECL_ARTIFICIAL (result) && flag_weak)
23932 comdat_linkage (result);
23933 /* For WIN32 we also want to put explicit instantiations in
23934 linkonce sections. */
23935 else if (TREE_PUBLIC (result))
23936 maybe_make_one_only (result);
23937 if (TREE_CODE (result) == FUNCTION_DECL
23938 && DECL_TEMPLATE_INSTANTIATED (result))
23939 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23940 since start_preparsed_function wouldn't have if we had an earlier
23941 extern explicit instantiation. */
23942 DECL_EXTERNAL (result) = 0;
23943 }
23944
23945 /* If EXTERN_P, then this function will not be emitted -- unless
23946 followed by an explicit instantiation, at which point its linkage
23947 will be adjusted. If !EXTERN_P, then this function will be
23948 emitted here. In neither circumstance do we want
23949 import_export_decl to adjust the linkage. */
23950 DECL_INTERFACE_KNOWN (result) = 1;
23951 }
23952
23953 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23954 important template arguments. If any are missing, we check whether
23955 they're important by using error_mark_node for substituting into any
23956 args that were used for partial ordering (the ones between ARGS and END)
23957 and seeing if it bubbles up. */
23958
23959 static bool
23960 check_undeduced_parms (tree targs, tree args, tree end)
23961 {
23962 bool found = false;
23963 int i;
23964 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23965 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23966 {
23967 found = true;
23968 TREE_VEC_ELT (targs, i) = error_mark_node;
23969 }
23970 if (found)
23971 {
23972 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23973 if (substed == error_mark_node)
23974 return true;
23975 }
23976 return false;
23977 }
23978
23979 /* Given two function templates PAT1 and PAT2, return:
23980
23981 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23982 -1 if PAT2 is more specialized than PAT1.
23983 0 if neither is more specialized.
23984
23985 LEN indicates the number of parameters we should consider
23986 (defaulted parameters should not be considered).
23987
23988 The 1998 std underspecified function template partial ordering, and
23989 DR214 addresses the issue. We take pairs of arguments, one from
23990 each of the templates, and deduce them against each other. One of
23991 the templates will be more specialized if all the *other*
23992 template's arguments deduce against its arguments and at least one
23993 of its arguments *does* *not* deduce against the other template's
23994 corresponding argument. Deduction is done as for class templates.
23995 The arguments used in deduction have reference and top level cv
23996 qualifiers removed. Iff both arguments were originally reference
23997 types *and* deduction succeeds in both directions, an lvalue reference
23998 wins against an rvalue reference and otherwise the template
23999 with the more cv-qualified argument wins for that pairing (if
24000 neither is more cv-qualified, they both are equal). Unlike regular
24001 deduction, after all the arguments have been deduced in this way,
24002 we do *not* verify the deduced template argument values can be
24003 substituted into non-deduced contexts.
24004
24005 The logic can be a bit confusing here, because we look at deduce1 and
24006 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24007 can find template arguments for pat1 to make arg1 look like arg2, that
24008 means that arg2 is at least as specialized as arg1. */
24009
24010 int
24011 more_specialized_fn (tree pat1, tree pat2, int len)
24012 {
24013 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
24014 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
24015 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
24016 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
24017 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
24018 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
24019 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
24020 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
24021 tree origs1, origs2;
24022 bool lose1 = false;
24023 bool lose2 = false;
24024
24025 /* Remove the this parameter from non-static member functions. If
24026 one is a non-static member function and the other is not a static
24027 member function, remove the first parameter from that function
24028 also. This situation occurs for operator functions where we
24029 locate both a member function (with this pointer) and non-member
24030 operator (with explicit first operand). */
24031 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24032 {
24033 len--; /* LEN is the number of significant arguments for DECL1 */
24034 args1 = TREE_CHAIN (args1);
24035 if (!DECL_STATIC_FUNCTION_P (decl2))
24036 args2 = TREE_CHAIN (args2);
24037 }
24038 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24039 {
24040 args2 = TREE_CHAIN (args2);
24041 if (!DECL_STATIC_FUNCTION_P (decl1))
24042 {
24043 len--;
24044 args1 = TREE_CHAIN (args1);
24045 }
24046 }
24047
24048 /* If only one is a conversion operator, they are unordered. */
24049 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24050 return 0;
24051
24052 /* Consider the return type for a conversion function */
24053 if (DECL_CONV_FN_P (decl1))
24054 {
24055 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24056 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24057 len++;
24058 }
24059
24060 processing_template_decl++;
24061
24062 origs1 = args1;
24063 origs2 = args2;
24064
24065 while (len--
24066 /* Stop when an ellipsis is seen. */
24067 && args1 != NULL_TREE && args2 != NULL_TREE)
24068 {
24069 tree arg1 = TREE_VALUE (args1);
24070 tree arg2 = TREE_VALUE (args2);
24071 int deduce1, deduce2;
24072 int quals1 = -1;
24073 int quals2 = -1;
24074 int ref1 = 0;
24075 int ref2 = 0;
24076
24077 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24078 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24079 {
24080 /* When both arguments are pack expansions, we need only
24081 unify the patterns themselves. */
24082 arg1 = PACK_EXPANSION_PATTERN (arg1);
24083 arg2 = PACK_EXPANSION_PATTERN (arg2);
24084
24085 /* This is the last comparison we need to do. */
24086 len = 0;
24087 }
24088
24089 if (TYPE_REF_P (arg1))
24090 {
24091 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24092 arg1 = TREE_TYPE (arg1);
24093 quals1 = cp_type_quals (arg1);
24094 }
24095
24096 if (TYPE_REF_P (arg2))
24097 {
24098 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24099 arg2 = TREE_TYPE (arg2);
24100 quals2 = cp_type_quals (arg2);
24101 }
24102
24103 arg1 = TYPE_MAIN_VARIANT (arg1);
24104 arg2 = TYPE_MAIN_VARIANT (arg2);
24105
24106 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24107 {
24108 int i, len2 = remaining_arguments (args2);
24109 tree parmvec = make_tree_vec (1);
24110 tree argvec = make_tree_vec (len2);
24111 tree ta = args2;
24112
24113 /* Setup the parameter vector, which contains only ARG1. */
24114 TREE_VEC_ELT (parmvec, 0) = arg1;
24115
24116 /* Setup the argument vector, which contains the remaining
24117 arguments. */
24118 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24119 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24120
24121 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24122 argvec, DEDUCE_EXACT,
24123 /*subr=*/true, /*explain_p=*/false)
24124 == 0);
24125
24126 /* We cannot deduce in the other direction, because ARG1 is
24127 a pack expansion but ARG2 is not. */
24128 deduce2 = 0;
24129 }
24130 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24131 {
24132 int i, len1 = remaining_arguments (args1);
24133 tree parmvec = make_tree_vec (1);
24134 tree argvec = make_tree_vec (len1);
24135 tree ta = args1;
24136
24137 /* Setup the parameter vector, which contains only ARG1. */
24138 TREE_VEC_ELT (parmvec, 0) = arg2;
24139
24140 /* Setup the argument vector, which contains the remaining
24141 arguments. */
24142 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24143 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24144
24145 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24146 argvec, DEDUCE_EXACT,
24147 /*subr=*/true, /*explain_p=*/false)
24148 == 0);
24149
24150 /* We cannot deduce in the other direction, because ARG2 is
24151 a pack expansion but ARG1 is not.*/
24152 deduce1 = 0;
24153 }
24154
24155 else
24156 {
24157 /* The normal case, where neither argument is a pack
24158 expansion. */
24159 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24160 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24161 == 0);
24162 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24163 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24164 == 0);
24165 }
24166
24167 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24168 arg2, then arg2 is not as specialized as arg1. */
24169 if (!deduce1)
24170 lose2 = true;
24171 if (!deduce2)
24172 lose1 = true;
24173
24174 /* "If, for a given type, deduction succeeds in both directions
24175 (i.e., the types are identical after the transformations above)
24176 and both P and A were reference types (before being replaced with
24177 the type referred to above):
24178 - if the type from the argument template was an lvalue reference and
24179 the type from the parameter template was not, the argument type is
24180 considered to be more specialized than the other; otherwise,
24181 - if the type from the argument template is more cv-qualified
24182 than the type from the parameter template (as described above),
24183 the argument type is considered to be more specialized than the other;
24184 otherwise,
24185 - neither type is more specialized than the other." */
24186
24187 if (deduce1 && deduce2)
24188 {
24189 if (ref1 && ref2 && ref1 != ref2)
24190 {
24191 if (ref1 > ref2)
24192 lose1 = true;
24193 else
24194 lose2 = true;
24195 }
24196 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24197 {
24198 if ((quals1 & quals2) == quals2)
24199 lose2 = true;
24200 if ((quals1 & quals2) == quals1)
24201 lose1 = true;
24202 }
24203 }
24204
24205 if (lose1 && lose2)
24206 /* We've failed to deduce something in either direction.
24207 These must be unordered. */
24208 break;
24209
24210 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24211 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24212 /* We have already processed all of the arguments in our
24213 handing of the pack expansion type. */
24214 len = 0;
24215
24216 args1 = TREE_CHAIN (args1);
24217 args2 = TREE_CHAIN (args2);
24218 }
24219
24220 /* "In most cases, all template parameters must have values in order for
24221 deduction to succeed, but for partial ordering purposes a template
24222 parameter may remain without a value provided it is not used in the
24223 types being used for partial ordering."
24224
24225 Thus, if we are missing any of the targs1 we need to substitute into
24226 origs1, then pat2 is not as specialized as pat1. This can happen when
24227 there is a nondeduced context. */
24228 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24229 lose2 = true;
24230 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24231 lose1 = true;
24232
24233 processing_template_decl--;
24234
24235 /* If both deductions succeed, the partial ordering selects the more
24236 constrained template. */
24237 /* P2113: If the corresponding template-parameters of the
24238 template-parameter-lists are not equivalent ([temp.over.link]) or if
24239 the function parameters that positionally correspond between the two
24240 templates are not of the same type, neither template is more
24241 specialized than the other. */
24242 if (!lose1 && !lose2
24243 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24244 DECL_TEMPLATE_PARMS (pat2))
24245 && compparms (origs1, origs2))
24246 {
24247 int winner = more_constrained (decl1, decl2);
24248 if (winner > 0)
24249 lose2 = true;
24250 else if (winner < 0)
24251 lose1 = true;
24252 }
24253
24254 /* All things being equal, if the next argument is a pack expansion
24255 for one function but not for the other, prefer the
24256 non-variadic function. FIXME this is bogus; see c++/41958. */
24257 if (lose1 == lose2
24258 && args1 && TREE_VALUE (args1)
24259 && args2 && TREE_VALUE (args2))
24260 {
24261 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24262 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24263 }
24264
24265 if (lose1 == lose2)
24266 return 0;
24267 else if (!lose1)
24268 return 1;
24269 else
24270 return -1;
24271 }
24272
24273 /* Determine which of two partial specializations of TMPL is more
24274 specialized.
24275
24276 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24277 to the first partial specialization. The TREE_PURPOSE is the
24278 innermost set of template parameters for the partial
24279 specialization. PAT2 is similar, but for the second template.
24280
24281 Return 1 if the first partial specialization is more specialized;
24282 -1 if the second is more specialized; 0 if neither is more
24283 specialized.
24284
24285 See [temp.class.order] for information about determining which of
24286 two templates is more specialized. */
24287
24288 static int
24289 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24290 {
24291 tree targs;
24292 int winner = 0;
24293 bool any_deductions = false;
24294
24295 tree tmpl1 = TREE_VALUE (pat1);
24296 tree tmpl2 = TREE_VALUE (pat2);
24297 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24298 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24299
24300 /* Just like what happens for functions, if we are ordering between
24301 different template specializations, we may encounter dependent
24302 types in the arguments, and we need our dependency check functions
24303 to behave correctly. */
24304 ++processing_template_decl;
24305 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24306 if (targs)
24307 {
24308 --winner;
24309 any_deductions = true;
24310 }
24311
24312 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24313 if (targs)
24314 {
24315 ++winner;
24316 any_deductions = true;
24317 }
24318 --processing_template_decl;
24319
24320 /* If both deductions succeed, the partial ordering selects the more
24321 constrained template. */
24322 if (!winner && any_deductions)
24323 winner = more_constrained (tmpl1, tmpl2);
24324
24325 /* In the case of a tie where at least one of the templates
24326 has a parameter pack at the end, the template with the most
24327 non-packed parameters wins. */
24328 if (winner == 0
24329 && any_deductions
24330 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24331 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24332 {
24333 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24334 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24335 int len1 = TREE_VEC_LENGTH (args1);
24336 int len2 = TREE_VEC_LENGTH (args2);
24337
24338 /* We don't count the pack expansion at the end. */
24339 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24340 --len1;
24341 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24342 --len2;
24343
24344 if (len1 > len2)
24345 return 1;
24346 else if (len1 < len2)
24347 return -1;
24348 }
24349
24350 return winner;
24351 }
24352
24353 /* Return the template arguments that will produce the function signature
24354 DECL from the function template FN, with the explicit template
24355 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24356 also match. Return NULL_TREE if no satisfactory arguments could be
24357 found. */
24358
24359 static tree
24360 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24361 {
24362 int ntparms = DECL_NTPARMS (fn);
24363 tree targs = make_tree_vec (ntparms);
24364 tree decl_type = TREE_TYPE (decl);
24365 tree decl_arg_types;
24366 tree *args;
24367 unsigned int nargs, ix;
24368 tree arg;
24369
24370 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24371
24372 /* Never do unification on the 'this' parameter. */
24373 decl_arg_types = skip_artificial_parms_for (decl,
24374 TYPE_ARG_TYPES (decl_type));
24375
24376 nargs = list_length (decl_arg_types);
24377 args = XALLOCAVEC (tree, nargs);
24378 for (arg = decl_arg_types, ix = 0;
24379 arg != NULL_TREE && arg != void_list_node;
24380 arg = TREE_CHAIN (arg), ++ix)
24381 args[ix] = TREE_VALUE (arg);
24382
24383 if (fn_type_unification (fn, explicit_args, targs,
24384 args, ix,
24385 (check_rettype || DECL_CONV_FN_P (fn)
24386 ? TREE_TYPE (decl_type) : NULL_TREE),
24387 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24388 /*explain_p=*/false,
24389 /*decltype*/false)
24390 == error_mark_node)
24391 return NULL_TREE;
24392
24393 return targs;
24394 }
24395
24396 /* Return the innermost template arguments that, when applied to a partial
24397 specialization SPEC_TMPL of TMPL, yield the ARGS.
24398
24399 For example, suppose we have:
24400
24401 template <class T, class U> struct S {};
24402 template <class T> struct S<T*, int> {};
24403
24404 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24405 partial specialization and the ARGS will be {double*, int}. The resulting
24406 vector will be {double}, indicating that `T' is bound to `double'. */
24407
24408 static tree
24409 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24410 {
24411 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24412 tree spec_args
24413 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24414 int i, ntparms = TREE_VEC_LENGTH (tparms);
24415 tree deduced_args;
24416 tree innermost_deduced_args;
24417
24418 innermost_deduced_args = make_tree_vec (ntparms);
24419 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24420 {
24421 deduced_args = copy_node (args);
24422 SET_TMPL_ARGS_LEVEL (deduced_args,
24423 TMPL_ARGS_DEPTH (deduced_args),
24424 innermost_deduced_args);
24425 }
24426 else
24427 deduced_args = innermost_deduced_args;
24428
24429 bool tried_array_deduction = (cxx_dialect < cxx17);
24430 again:
24431 if (unify (tparms, deduced_args,
24432 INNERMOST_TEMPLATE_ARGS (spec_args),
24433 INNERMOST_TEMPLATE_ARGS (args),
24434 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24435 return NULL_TREE;
24436
24437 for (i = 0; i < ntparms; ++i)
24438 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24439 {
24440 if (!tried_array_deduction)
24441 {
24442 try_array_deduction (tparms, innermost_deduced_args,
24443 INNERMOST_TEMPLATE_ARGS (spec_args));
24444 tried_array_deduction = true;
24445 if (TREE_VEC_ELT (innermost_deduced_args, i))
24446 goto again;
24447 }
24448 return NULL_TREE;
24449 }
24450
24451 if (!push_tinst_level (spec_tmpl, deduced_args))
24452 {
24453 excessive_deduction_depth = true;
24454 return NULL_TREE;
24455 }
24456
24457 /* Verify that nondeduced template arguments agree with the type
24458 obtained from argument deduction.
24459
24460 For example:
24461
24462 struct A { typedef int X; };
24463 template <class T, class U> struct C {};
24464 template <class T> struct C<T, typename T::X> {};
24465
24466 Then with the instantiation `C<A, int>', we can deduce that
24467 `T' is `A' but unify () does not check whether `typename T::X'
24468 is `int'. */
24469 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24470
24471 if (spec_args != error_mark_node)
24472 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24473 INNERMOST_TEMPLATE_ARGS (spec_args),
24474 tmpl, tf_none, false, false);
24475
24476 pop_tinst_level ();
24477
24478 if (spec_args == error_mark_node
24479 /* We only need to check the innermost arguments; the other
24480 arguments will always agree. */
24481 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24482 INNERMOST_TEMPLATE_ARGS (args)))
24483 return NULL_TREE;
24484
24485 /* Now that we have bindings for all of the template arguments,
24486 ensure that the arguments deduced for the template template
24487 parameters have compatible template parameter lists. See the use
24488 of template_template_parm_bindings_ok_p in fn_type_unification
24489 for more information. */
24490 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24491 return NULL_TREE;
24492
24493 return deduced_args;
24494 }
24495
24496 // Compare two function templates T1 and T2 by deducing bindings
24497 // from one against the other. If both deductions succeed, compare
24498 // constraints to see which is more constrained.
24499 static int
24500 more_specialized_inst (tree t1, tree t2)
24501 {
24502 int fate = 0;
24503 int count = 0;
24504
24505 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24506 {
24507 --fate;
24508 ++count;
24509 }
24510
24511 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24512 {
24513 ++fate;
24514 ++count;
24515 }
24516
24517 // If both deductions succeed, then one may be more constrained.
24518 if (count == 2 && fate == 0)
24519 fate = more_constrained (t1, t2);
24520
24521 return fate;
24522 }
24523
24524 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24525 Return the TREE_LIST node with the most specialized template, if
24526 any. If there is no most specialized template, the error_mark_node
24527 is returned.
24528
24529 Note that this function does not look at, or modify, the
24530 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24531 returned is one of the elements of INSTANTIATIONS, callers may
24532 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24533 and retrieve it from the value returned. */
24534
24535 tree
24536 most_specialized_instantiation (tree templates)
24537 {
24538 tree fn, champ;
24539
24540 ++processing_template_decl;
24541
24542 champ = templates;
24543 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24544 {
24545 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24546 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24547 if (fate == -1)
24548 champ = fn;
24549 else if (!fate)
24550 {
24551 /* Equally specialized, move to next function. If there
24552 is no next function, nothing's most specialized. */
24553 fn = TREE_CHAIN (fn);
24554 champ = fn;
24555 if (!fn)
24556 break;
24557 }
24558 }
24559
24560 if (champ)
24561 /* Now verify that champ is better than everything earlier in the
24562 instantiation list. */
24563 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24564 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24565 {
24566 champ = NULL_TREE;
24567 break;
24568 }
24569 }
24570
24571 processing_template_decl--;
24572
24573 if (!champ)
24574 return error_mark_node;
24575
24576 return champ;
24577 }
24578
24579 /* If DECL is a specialization of some template, return the most
24580 general such template. Otherwise, returns NULL_TREE.
24581
24582 For example, given:
24583
24584 template <class T> struct S { template <class U> void f(U); };
24585
24586 if TMPL is `template <class U> void S<int>::f(U)' this will return
24587 the full template. This function will not trace past partial
24588 specializations, however. For example, given in addition:
24589
24590 template <class T> struct S<T*> { template <class U> void f(U); };
24591
24592 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24593 `template <class T> template <class U> S<T*>::f(U)'. */
24594
24595 tree
24596 most_general_template (tree decl)
24597 {
24598 if (TREE_CODE (decl) != TEMPLATE_DECL)
24599 {
24600 if (tree tinfo = get_template_info (decl))
24601 decl = TI_TEMPLATE (tinfo);
24602 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24603 template friend, or a FIELD_DECL for a capture pack. */
24604 if (TREE_CODE (decl) != TEMPLATE_DECL)
24605 return NULL_TREE;
24606 }
24607
24608 /* Look for more and more general templates. */
24609 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24610 {
24611 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24612 (See cp-tree.h for details.) */
24613 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24614 break;
24615
24616 if (CLASS_TYPE_P (TREE_TYPE (decl))
24617 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24618 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24619 break;
24620
24621 /* Stop if we run into an explicitly specialized class template. */
24622 if (!DECL_NAMESPACE_SCOPE_P (decl)
24623 && DECL_CONTEXT (decl)
24624 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24625 break;
24626
24627 decl = DECL_TI_TEMPLATE (decl);
24628 }
24629
24630 return decl;
24631 }
24632
24633 /* Return the most specialized of the template partial specializations
24634 which can produce TARGET, a specialization of some class or variable
24635 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24636 a TEMPLATE_DECL node corresponding to the partial specialization, while
24637 the TREE_PURPOSE is the set of template arguments that must be
24638 substituted into the template pattern in order to generate TARGET.
24639
24640 If the choice of partial specialization is ambiguous, a diagnostic
24641 is issued, and the error_mark_node is returned. If there are no
24642 partial specializations matching TARGET, then NULL_TREE is
24643 returned, indicating that the primary template should be used. */
24644
24645 tree
24646 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24647 {
24648 tree list = NULL_TREE;
24649 tree t;
24650 tree champ;
24651 int fate;
24652 bool ambiguous_p;
24653 tree outer_args = NULL_TREE;
24654 tree tmpl, args;
24655
24656 if (TYPE_P (target))
24657 {
24658 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24659 tmpl = TI_TEMPLATE (tinfo);
24660 args = TI_ARGS (tinfo);
24661 }
24662 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24663 {
24664 tmpl = TREE_OPERAND (target, 0);
24665 args = TREE_OPERAND (target, 1);
24666 }
24667 else if (VAR_P (target))
24668 {
24669 tree tinfo = DECL_TEMPLATE_INFO (target);
24670 tmpl = TI_TEMPLATE (tinfo);
24671 args = TI_ARGS (tinfo);
24672 }
24673 else
24674 gcc_unreachable ();
24675
24676 tree main_tmpl = most_general_template (tmpl);
24677
24678 /* For determining which partial specialization to use, only the
24679 innermost args are interesting. */
24680 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24681 {
24682 outer_args = strip_innermost_template_args (args, 1);
24683 args = INNERMOST_TEMPLATE_ARGS (args);
24684 }
24685
24686 /* The caller hasn't called push_to_top_level yet, but we need
24687 get_partial_spec_bindings to be done in non-template context so that we'll
24688 fully resolve everything. */
24689 processing_template_decl_sentinel ptds;
24690
24691 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24692 {
24693 const tree ospec_tmpl = TREE_VALUE (t);
24694
24695 tree spec_tmpl;
24696 if (outer_args)
24697 {
24698 /* Substitute in the template args from the enclosing class. */
24699 ++processing_template_decl;
24700 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
24701 --processing_template_decl;
24702 if (spec_tmpl == error_mark_node)
24703 return error_mark_node;
24704 }
24705 else
24706 spec_tmpl = ospec_tmpl;
24707
24708 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24709 if (spec_args)
24710 {
24711 if (outer_args)
24712 spec_args = add_to_template_args (outer_args, spec_args);
24713
24714 /* Keep the candidate only if the constraints are satisfied,
24715 or if we're not compiling with concepts. */
24716 if (!flag_concepts
24717 || constraints_satisfied_p (ospec_tmpl, spec_args))
24718 {
24719 list = tree_cons (spec_args, ospec_tmpl, list);
24720 TREE_TYPE (list) = TREE_TYPE (t);
24721 }
24722 }
24723 }
24724
24725 if (! list)
24726 return NULL_TREE;
24727
24728 ambiguous_p = false;
24729 t = list;
24730 champ = t;
24731 t = TREE_CHAIN (t);
24732 for (; t; t = TREE_CHAIN (t))
24733 {
24734 fate = more_specialized_partial_spec (tmpl, champ, t);
24735 if (fate == 1)
24736 ;
24737 else
24738 {
24739 if (fate == 0)
24740 {
24741 t = TREE_CHAIN (t);
24742 if (! t)
24743 {
24744 ambiguous_p = true;
24745 break;
24746 }
24747 }
24748 champ = t;
24749 }
24750 }
24751
24752 if (!ambiguous_p)
24753 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24754 {
24755 fate = more_specialized_partial_spec (tmpl, champ, t);
24756 if (fate != 1)
24757 {
24758 ambiguous_p = true;
24759 break;
24760 }
24761 }
24762
24763 if (ambiguous_p)
24764 {
24765 const char *str;
24766 char *spaces = NULL;
24767 if (!(complain & tf_error))
24768 return error_mark_node;
24769 if (TYPE_P (target))
24770 error ("ambiguous template instantiation for %q#T", target);
24771 else
24772 error ("ambiguous template instantiation for %q#D", target);
24773 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24774 for (t = list; t; t = TREE_CHAIN (t))
24775 {
24776 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24777 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24778 "%s %#qS", spaces ? spaces : str, subst);
24779 spaces = spaces ? spaces : get_spaces (str);
24780 }
24781 free (spaces);
24782 return error_mark_node;
24783 }
24784
24785 return champ;
24786 }
24787
24788 /* Explicitly instantiate DECL. */
24789
24790 void
24791 do_decl_instantiation (tree decl, tree storage)
24792 {
24793 tree result = NULL_TREE;
24794 int extern_p = 0;
24795
24796 if (!decl || decl == error_mark_node)
24797 /* An error occurred, for which grokdeclarator has already issued
24798 an appropriate message. */
24799 return;
24800 else if (! DECL_LANG_SPECIFIC (decl))
24801 {
24802 error ("explicit instantiation of non-template %q#D", decl);
24803 return;
24804 }
24805 else if (DECL_DECLARED_CONCEPT_P (decl))
24806 {
24807 if (VAR_P (decl))
24808 error ("explicit instantiation of variable concept %q#D", decl);
24809 else
24810 error ("explicit instantiation of function concept %q#D", decl);
24811 return;
24812 }
24813
24814 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24815 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24816
24817 if (VAR_P (decl) && !var_templ)
24818 {
24819 /* There is an asymmetry here in the way VAR_DECLs and
24820 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24821 the latter, the DECL we get back will be marked as a
24822 template instantiation, and the appropriate
24823 DECL_TEMPLATE_INFO will be set up. This does not happen for
24824 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24825 should handle VAR_DECLs as it currently handles
24826 FUNCTION_DECLs. */
24827 if (!DECL_CLASS_SCOPE_P (decl))
24828 {
24829 error ("%qD is not a static data member of a class template", decl);
24830 return;
24831 }
24832 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24833 if (!result || !VAR_P (result))
24834 {
24835 error ("no matching template for %qD found", decl);
24836 return;
24837 }
24838 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24839 {
24840 error ("type %qT for explicit instantiation %qD does not match "
24841 "declared type %qT", TREE_TYPE (result), decl,
24842 TREE_TYPE (decl));
24843 return;
24844 }
24845 }
24846 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24847 {
24848 error ("explicit instantiation of %q#D", decl);
24849 return;
24850 }
24851 else
24852 result = decl;
24853
24854 /* Check for various error cases. Note that if the explicit
24855 instantiation is valid the RESULT will currently be marked as an
24856 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24857 until we get here. */
24858
24859 if (DECL_TEMPLATE_SPECIALIZATION (result))
24860 {
24861 /* DR 259 [temp.spec].
24862
24863 Both an explicit instantiation and a declaration of an explicit
24864 specialization shall not appear in a program unless the explicit
24865 instantiation follows a declaration of the explicit specialization.
24866
24867 For a given set of template parameters, if an explicit
24868 instantiation of a template appears after a declaration of an
24869 explicit specialization for that template, the explicit
24870 instantiation has no effect. */
24871 return;
24872 }
24873 else if (DECL_EXPLICIT_INSTANTIATION (result))
24874 {
24875 /* [temp.spec]
24876
24877 No program shall explicitly instantiate any template more
24878 than once.
24879
24880 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24881 the first instantiation was `extern' and the second is not,
24882 and EXTERN_P for the opposite case. */
24883 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24884 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24885 /* If an "extern" explicit instantiation follows an ordinary
24886 explicit instantiation, the template is instantiated. */
24887 if (extern_p)
24888 return;
24889 }
24890 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24891 {
24892 error ("no matching template for %qD found", result);
24893 return;
24894 }
24895 else if (!DECL_TEMPLATE_INFO (result))
24896 {
24897 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24898 return;
24899 }
24900
24901 if (storage == NULL_TREE)
24902 ;
24903 else if (storage == ridpointers[(int) RID_EXTERN])
24904 {
24905 if (cxx_dialect == cxx98)
24906 pedwarn (input_location, OPT_Wpedantic,
24907 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24908 "instantiations");
24909 extern_p = 1;
24910 }
24911 else
24912 error ("storage class %qD applied to template instantiation", storage);
24913
24914 check_explicit_instantiation_namespace (result);
24915 mark_decl_instantiated (result, extern_p);
24916 if (! extern_p)
24917 instantiate_decl (result, /*defer_ok=*/true,
24918 /*expl_inst_class_mem_p=*/false);
24919 }
24920
24921 static void
24922 mark_class_instantiated (tree t, int extern_p)
24923 {
24924 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24925 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24926 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24927 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24928 if (! extern_p)
24929 {
24930 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24931 rest_of_type_compilation (t, 1);
24932 }
24933 }
24934
24935 /* Called from do_type_instantiation through binding_table_foreach to
24936 do recursive instantiation for the type bound in ENTRY. */
24937 static void
24938 bt_instantiate_type_proc (binding_entry entry, void *data)
24939 {
24940 tree storage = *(tree *) data;
24941
24942 if (MAYBE_CLASS_TYPE_P (entry->type)
24943 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24944 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24945 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24946 }
24947
24948 /* Perform an explicit instantiation of template class T. STORAGE, if
24949 non-null, is the RID for extern, inline or static. COMPLAIN is
24950 nonzero if this is called from the parser, zero if called recursively,
24951 since the standard is unclear (as detailed below). */
24952
24953 void
24954 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24955 {
24956 int extern_p = 0;
24957 int nomem_p = 0;
24958 int static_p = 0;
24959 int previous_instantiation_extern_p = 0;
24960
24961 if (TREE_CODE (t) == TYPE_DECL)
24962 t = TREE_TYPE (t);
24963
24964 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24965 {
24966 tree tmpl =
24967 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24968 if (tmpl)
24969 error ("explicit instantiation of non-class template %qD", tmpl);
24970 else
24971 error ("explicit instantiation of non-template type %qT", t);
24972 return;
24973 }
24974
24975 complete_type (t);
24976
24977 if (!COMPLETE_TYPE_P (t))
24978 {
24979 if (complain & tf_error)
24980 error ("explicit instantiation of %q#T before definition of template",
24981 t);
24982 return;
24983 }
24984
24985 if (storage != NULL_TREE)
24986 {
24987 if (storage == ridpointers[(int) RID_EXTERN])
24988 {
24989 if (cxx_dialect == cxx98)
24990 pedwarn (input_location, OPT_Wpedantic,
24991 "ISO C++ 1998 forbids the use of %<extern%> on "
24992 "explicit instantiations");
24993 }
24994 else
24995 pedwarn (input_location, OPT_Wpedantic,
24996 "ISO C++ forbids the use of %qE"
24997 " on explicit instantiations", storage);
24998
24999 if (storage == ridpointers[(int) RID_INLINE])
25000 nomem_p = 1;
25001 else if (storage == ridpointers[(int) RID_EXTERN])
25002 extern_p = 1;
25003 else if (storage == ridpointers[(int) RID_STATIC])
25004 static_p = 1;
25005 else
25006 {
25007 error ("storage class %qD applied to template instantiation",
25008 storage);
25009 extern_p = 0;
25010 }
25011 }
25012
25013 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
25014 {
25015 /* DR 259 [temp.spec].
25016
25017 Both an explicit instantiation and a declaration of an explicit
25018 specialization shall not appear in a program unless the explicit
25019 instantiation follows a declaration of the explicit specialization.
25020
25021 For a given set of template parameters, if an explicit
25022 instantiation of a template appears after a declaration of an
25023 explicit specialization for that template, the explicit
25024 instantiation has no effect. */
25025 return;
25026 }
25027 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
25028 {
25029 /* [temp.spec]
25030
25031 No program shall explicitly instantiate any template more
25032 than once.
25033
25034 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
25035 instantiation was `extern'. If EXTERN_P then the second is.
25036 These cases are OK. */
25037 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
25038
25039 if (!previous_instantiation_extern_p && !extern_p
25040 && (complain & tf_error))
25041 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
25042
25043 /* If we've already instantiated the template, just return now. */
25044 if (!CLASSTYPE_INTERFACE_ONLY (t))
25045 return;
25046 }
25047
25048 check_explicit_instantiation_namespace (TYPE_NAME (t));
25049 mark_class_instantiated (t, extern_p);
25050
25051 if (nomem_p)
25052 return;
25053
25054 /* In contrast to implicit instantiation, where only the
25055 declarations, and not the definitions, of members are
25056 instantiated, we have here:
25057
25058 [temp.explicit]
25059
25060 An explicit instantiation that names a class template
25061 specialization is also an explicit instantiation of the same
25062 kind (declaration or definition) of each of its members (not
25063 including members inherited from base classes and members
25064 that are templates) that has not been previously explicitly
25065 specialized in the translation unit containing the explicit
25066 instantiation, provided that the associated constraints, if
25067 any, of that member are satisfied by the template arguments
25068 of the explicit instantiation. */
25069 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25070 if ((VAR_P (fld)
25071 || (TREE_CODE (fld) == FUNCTION_DECL
25072 && !static_p
25073 && user_provided_p (fld)))
25074 && DECL_TEMPLATE_INSTANTIATION (fld)
25075 && constraints_satisfied_p (fld))
25076 {
25077 mark_decl_instantiated (fld, extern_p);
25078 if (! extern_p)
25079 instantiate_decl (fld, /*defer_ok=*/true,
25080 /*expl_inst_class_mem_p=*/true);
25081 }
25082
25083 if (CLASSTYPE_NESTED_UTDS (t))
25084 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
25085 bt_instantiate_type_proc, &storage);
25086 }
25087
25088 /* Given a function DECL, which is a specialization of TMPL, modify
25089 DECL to be a re-instantiation of TMPL with the same template
25090 arguments. TMPL should be the template into which tsubst'ing
25091 should occur for DECL, not the most general template.
25092
25093 One reason for doing this is a scenario like this:
25094
25095 template <class T>
25096 void f(const T&, int i);
25097
25098 void g() { f(3, 7); }
25099
25100 template <class T>
25101 void f(const T& t, const int i) { }
25102
25103 Note that when the template is first instantiated, with
25104 instantiate_template, the resulting DECL will have no name for the
25105 first parameter, and the wrong type for the second. So, when we go
25106 to instantiate the DECL, we regenerate it. */
25107
25108 static void
25109 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25110 {
25111 /* The arguments used to instantiate DECL, from the most general
25112 template. */
25113 tree code_pattern;
25114
25115 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25116
25117 /* Make sure that we can see identifiers, and compute access
25118 correctly. */
25119 push_access_scope (decl);
25120
25121 if (TREE_CODE (decl) == FUNCTION_DECL)
25122 {
25123 tree decl_parm;
25124 tree pattern_parm;
25125 tree specs;
25126 int args_depth;
25127 int parms_depth;
25128
25129 args_depth = TMPL_ARGS_DEPTH (args);
25130 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25131 if (args_depth > parms_depth)
25132 args = get_innermost_template_args (args, parms_depth);
25133
25134 /* Instantiate a dynamic exception-specification. noexcept will be
25135 handled below. */
25136 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25137 if (TREE_VALUE (raises))
25138 {
25139 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25140 args, tf_error, NULL_TREE,
25141 /*defer_ok*/false);
25142 if (specs && specs != error_mark_node)
25143 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25144 specs);
25145 }
25146
25147 /* Merge parameter declarations. */
25148 decl_parm = skip_artificial_parms_for (decl,
25149 DECL_ARGUMENTS (decl));
25150 pattern_parm
25151 = skip_artificial_parms_for (code_pattern,
25152 DECL_ARGUMENTS (code_pattern));
25153 while (decl_parm && !DECL_PACK_P (pattern_parm))
25154 {
25155 tree parm_type;
25156 tree attributes;
25157
25158 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25159 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25160 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25161 NULL_TREE);
25162 parm_type = type_decays_to (parm_type);
25163 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25164 TREE_TYPE (decl_parm) = parm_type;
25165 attributes = DECL_ATTRIBUTES (pattern_parm);
25166 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25167 {
25168 DECL_ATTRIBUTES (decl_parm) = attributes;
25169 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25170 }
25171 decl_parm = DECL_CHAIN (decl_parm);
25172 pattern_parm = DECL_CHAIN (pattern_parm);
25173 }
25174 /* Merge any parameters that match with the function parameter
25175 pack. */
25176 if (pattern_parm && DECL_PACK_P (pattern_parm))
25177 {
25178 int i, len;
25179 tree expanded_types;
25180 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25181 the parameters in this function parameter pack. */
25182 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25183 args, tf_error, NULL_TREE);
25184 len = TREE_VEC_LENGTH (expanded_types);
25185 for (i = 0; i < len; i++)
25186 {
25187 tree parm_type;
25188 tree attributes;
25189
25190 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25191 /* Rename the parameter to include the index. */
25192 DECL_NAME (decl_parm) =
25193 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25194 parm_type = TREE_VEC_ELT (expanded_types, i);
25195 parm_type = type_decays_to (parm_type);
25196 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25197 TREE_TYPE (decl_parm) = parm_type;
25198 attributes = DECL_ATTRIBUTES (pattern_parm);
25199 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25200 {
25201 DECL_ATTRIBUTES (decl_parm) = attributes;
25202 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25203 }
25204 decl_parm = DECL_CHAIN (decl_parm);
25205 }
25206 }
25207 /* Merge additional specifiers from the CODE_PATTERN. */
25208 if (DECL_DECLARED_INLINE_P (code_pattern)
25209 && !DECL_DECLARED_INLINE_P (decl))
25210 DECL_DECLARED_INLINE_P (decl) = 1;
25211
25212 maybe_instantiate_noexcept (decl, tf_error);
25213 }
25214 else if (VAR_P (decl))
25215 {
25216 start_lambda_scope (decl);
25217 DECL_INITIAL (decl) =
25218 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25219 tf_error, DECL_TI_TEMPLATE (decl));
25220 finish_lambda_scope ();
25221 if (VAR_HAD_UNKNOWN_BOUND (decl))
25222 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25223 tf_error, DECL_TI_TEMPLATE (decl));
25224 }
25225 else
25226 gcc_unreachable ();
25227
25228 pop_access_scope (decl);
25229 }
25230
25231 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25232 substituted to get DECL. */
25233
25234 tree
25235 template_for_substitution (tree decl)
25236 {
25237 tree tmpl = DECL_TI_TEMPLATE (decl);
25238
25239 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25240 for the instantiation. This is not always the most general
25241 template. Consider, for example:
25242
25243 template <class T>
25244 struct S { template <class U> void f();
25245 template <> void f<int>(); };
25246
25247 and an instantiation of S<double>::f<int>. We want TD to be the
25248 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25249 while (/* An instantiation cannot have a definition, so we need a
25250 more general template. */
25251 DECL_TEMPLATE_INSTANTIATION (tmpl)
25252 /* We must also deal with friend templates. Given:
25253
25254 template <class T> struct S {
25255 template <class U> friend void f() {};
25256 };
25257
25258 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25259 so far as the language is concerned, but that's still
25260 where we get the pattern for the instantiation from. On
25261 other hand, if the definition comes outside the class, say:
25262
25263 template <class T> struct S {
25264 template <class U> friend void f();
25265 };
25266 template <class U> friend void f() {}
25267
25268 we don't need to look any further. That's what the check for
25269 DECL_INITIAL is for. */
25270 || (TREE_CODE (decl) == FUNCTION_DECL
25271 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25272 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25273 {
25274 /* The present template, TD, should not be a definition. If it
25275 were a definition, we should be using it! Note that we
25276 cannot restructure the loop to just keep going until we find
25277 a template with a definition, since that might go too far if
25278 a specialization was declared, but not defined. */
25279
25280 /* Fetch the more general template. */
25281 tmpl = DECL_TI_TEMPLATE (tmpl);
25282 }
25283
25284 return tmpl;
25285 }
25286
25287 /* Returns true if we need to instantiate this template instance even if we
25288 know we aren't going to emit it. */
25289
25290 bool
25291 always_instantiate_p (tree decl)
25292 {
25293 /* We always instantiate inline functions so that we can inline them. An
25294 explicit instantiation declaration prohibits implicit instantiation of
25295 non-inline functions. With high levels of optimization, we would
25296 normally inline non-inline functions -- but we're not allowed to do
25297 that for "extern template" functions. Therefore, we check
25298 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25299 return ((TREE_CODE (decl) == FUNCTION_DECL
25300 && (DECL_DECLARED_INLINE_P (decl)
25301 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25302 /* And we need to instantiate static data members so that
25303 their initializers are available in integral constant
25304 expressions. */
25305 || (VAR_P (decl)
25306 && decl_maybe_constant_var_p (decl)));
25307 }
25308
25309 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25310 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25311 error, true otherwise. */
25312
25313 bool
25314 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25315 {
25316 tree fntype, spec, noex;
25317
25318 /* Don't instantiate a noexcept-specification from template context. */
25319 if (processing_template_decl
25320 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25321 return true;
25322
25323 if (DECL_MAYBE_DELETED (fn))
25324 {
25325 if (fn == current_function_decl)
25326 /* We're in start_preparsed_function, keep going. */
25327 return true;
25328
25329 ++function_depth;
25330 synthesize_method (fn);
25331 --function_depth;
25332 return !DECL_MAYBE_DELETED (fn);
25333 }
25334
25335 fntype = TREE_TYPE (fn);
25336 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25337
25338 if (!spec || !TREE_PURPOSE (spec))
25339 return true;
25340
25341 noex = TREE_PURPOSE (spec);
25342 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25343 && TREE_CODE (noex) != DEFERRED_PARSE)
25344 return true;
25345
25346 tree orig_fn = NULL_TREE;
25347 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25348 its FUNCTION_DECL for the rest of this function -- push_access_scope
25349 doesn't accept TEMPLATE_DECLs. */
25350 if (DECL_FUNCTION_TEMPLATE_P (fn))
25351 {
25352 orig_fn = fn;
25353 fn = DECL_TEMPLATE_RESULT (fn);
25354 }
25355
25356 if (DECL_CLONED_FUNCTION_P (fn))
25357 {
25358 tree prime = DECL_CLONED_FUNCTION (fn);
25359 if (!maybe_instantiate_noexcept (prime, complain))
25360 return false;
25361 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25362 }
25363 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25364 {
25365 static hash_set<tree>* fns = new hash_set<tree>;
25366 bool added = false;
25367 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25368 {
25369 spec = get_defaulted_eh_spec (fn, complain);
25370 if (spec == error_mark_node)
25371 /* This might have failed because of an unparsed DMI, so
25372 let's try again later. */
25373 return false;
25374 }
25375 else if (!(added = !fns->add (fn)))
25376 {
25377 /* If hash_set::add returns true, the element was already there. */
25378 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25379 DECL_SOURCE_LOCATION (fn));
25380 error_at (loc,
25381 "exception specification of %qD depends on itself",
25382 fn);
25383 spec = noexcept_false_spec;
25384 }
25385 else if (push_tinst_level (fn))
25386 {
25387 push_to_top_level ();
25388 push_access_scope (fn);
25389 push_deferring_access_checks (dk_no_deferred);
25390 input_location = DECL_SOURCE_LOCATION (fn);
25391
25392 /* If needed, set current_class_ptr for the benefit of
25393 tsubst_copy/PARM_DECL. */
25394 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25395 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25396 {
25397 tree this_parm = DECL_ARGUMENTS (tdecl);
25398 current_class_ptr = NULL_TREE;
25399 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25400 current_class_ptr = this_parm;
25401 }
25402
25403 /* If this function is represented by a TEMPLATE_DECL, then
25404 the deferred noexcept-specification might still contain
25405 dependent types, even after substitution. And we need the
25406 dependency check functions to work in build_noexcept_spec. */
25407 if (orig_fn)
25408 ++processing_template_decl;
25409
25410 /* Do deferred instantiation of the noexcept-specifier. */
25411 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25412 DEFERRED_NOEXCEPT_ARGS (noex),
25413 tf_warning_or_error, fn,
25414 /*function_p=*/false,
25415 /*i_c_e_p=*/true);
25416
25417 /* Build up the noexcept-specification. */
25418 spec = build_noexcept_spec (noex, tf_warning_or_error);
25419
25420 if (orig_fn)
25421 --processing_template_decl;
25422
25423 pop_deferring_access_checks ();
25424 pop_access_scope (fn);
25425 pop_tinst_level ();
25426 pop_from_top_level ();
25427 }
25428 else
25429 spec = noexcept_false_spec;
25430
25431 if (added)
25432 fns->remove (fn);
25433 }
25434
25435 if (spec == error_mark_node)
25436 {
25437 /* This failed with a hard error, so let's go with false. */
25438 gcc_assert (seen_error ());
25439 spec = noexcept_false_spec;
25440 }
25441
25442 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25443 if (orig_fn)
25444 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25445
25446 return true;
25447 }
25448
25449 /* We're starting to process the function INST, an instantiation of PATTERN;
25450 add their parameters to local_specializations. */
25451
25452 static void
25453 register_parameter_specializations (tree pattern, tree inst)
25454 {
25455 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25456 tree spec_parm = DECL_ARGUMENTS (inst);
25457 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25458 {
25459 register_local_specialization (spec_parm, tmpl_parm);
25460 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25461 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25462 }
25463 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25464 {
25465 if (!DECL_PACK_P (tmpl_parm)
25466 || (spec_parm && DECL_PACK_P (spec_parm)))
25467 {
25468 register_local_specialization (spec_parm, tmpl_parm);
25469 spec_parm = DECL_CHAIN (spec_parm);
25470 }
25471 else
25472 {
25473 /* Register the (value) argument pack as a specialization of
25474 TMPL_PARM, then move on. */
25475 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25476 register_local_specialization (argpack, tmpl_parm);
25477 }
25478 }
25479 gcc_assert (!spec_parm);
25480 }
25481
25482 /* Instantiate the body of D using PATTERN with ARGS. We have
25483 already determined PATTERN is the correct template to use.
25484 NESTED_P is true if this is a nested function, in which case
25485 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25486
25487 static void
25488 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
25489 {
25490 tree td = NULL_TREE;
25491 tree code_pattern = pattern;
25492
25493 if (!nested_p)
25494 {
25495 td = pattern;
25496 code_pattern = DECL_TEMPLATE_RESULT (td);
25497 }
25498 else
25499 /* Only OMP reductions are nested. */
25500 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
25501
25502 vec<tree> omp_privatization_save;
25503 if (current_function_decl)
25504 save_omp_privatization_clauses (omp_privatization_save);
25505
25506 bool push_to_top
25507 = !(current_function_decl
25508 && !LAMBDA_FUNCTION_P (d)
25509 && decl_function_context (d) == current_function_decl);
25510
25511 if (push_to_top)
25512 push_to_top_level ();
25513 else
25514 {
25515 gcc_assert (!processing_template_decl);
25516 push_function_context ();
25517 cp_unevaluated_operand = 0;
25518 c_inhibit_evaluation_warnings = 0;
25519 }
25520
25521 if (VAR_P (d))
25522 {
25523 /* The variable might be a lambda's extra scope, and that
25524 lambda's visibility depends on D's. */
25525 maybe_commonize_var (d);
25526 determine_visibility (d);
25527 }
25528
25529 /* Mark D as instantiated so that recursive calls to
25530 instantiate_decl do not try to instantiate it again. */
25531 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25532
25533 if (td)
25534 /* Regenerate the declaration in case the template has been modified
25535 by a subsequent redeclaration. */
25536 regenerate_decl_from_template (d, td, args);
25537
25538 /* We already set the file and line above. Reset them now in case
25539 they changed as a result of calling regenerate_decl_from_template. */
25540 input_location = DECL_SOURCE_LOCATION (d);
25541
25542 if (VAR_P (d))
25543 {
25544 tree init;
25545 bool const_init = false;
25546
25547 /* Clear out DECL_RTL; whatever was there before may not be right
25548 since we've reset the type of the declaration. */
25549 SET_DECL_RTL (d, NULL);
25550 DECL_IN_AGGR_P (d) = 0;
25551
25552 /* The initializer is placed in DECL_INITIAL by
25553 regenerate_decl_from_template so we don't need to
25554 push/pop_access_scope again here. Pull it out so that
25555 cp_finish_decl can process it. */
25556 init = DECL_INITIAL (d);
25557 DECL_INITIAL (d) = NULL_TREE;
25558 DECL_INITIALIZED_P (d) = 0;
25559
25560 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25561 initializer. That function will defer actual emission until
25562 we have a chance to determine linkage. */
25563 DECL_EXTERNAL (d) = 0;
25564
25565 /* Enter the scope of D so that access-checking works correctly. */
25566 bool enter_context = DECL_CLASS_SCOPE_P (d);
25567 if (enter_context)
25568 push_nested_class (DECL_CONTEXT (d));
25569
25570 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25571 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25572
25573 if (enter_context)
25574 pop_nested_class ();
25575 }
25576 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25577 synthesize_method (d);
25578 else if (TREE_CODE (d) == FUNCTION_DECL)
25579 {
25580 /* Set up the list of local specializations. */
25581 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25582 tree block = NULL_TREE;
25583
25584 /* Set up context. */
25585 if (nested_p)
25586 block = push_stmt_list ();
25587 else
25588 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25589
25590 perform_instantiation_time_access_checks (code_pattern, args);
25591
25592 /* Create substitution entries for the parameters. */
25593 register_parameter_specializations (code_pattern, d);
25594
25595 /* Substitute into the body of the function. */
25596 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25597 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25598 tf_warning_or_error, d);
25599 else
25600 {
25601 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25602 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25603 /*integral_constant_expression_p=*/false);
25604
25605 /* Set the current input_location to the end of the function
25606 so that finish_function knows where we are. */
25607 input_location
25608 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25609
25610 /* Remember if we saw an infinite loop in the template. */
25611 current_function_infinite_loop
25612 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25613 }
25614
25615 /* Finish the function. */
25616 if (nested_p)
25617 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25618 else
25619 {
25620 d = finish_function (/*inline_p=*/false);
25621 expand_or_defer_fn (d);
25622 }
25623
25624 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25625 cp_check_omp_declare_reduction (d);
25626 }
25627
25628 /* We're not deferring instantiation any more. */
25629 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25630
25631 if (push_to_top)
25632 pop_from_top_level ();
25633 else
25634 pop_function_context ();
25635
25636 if (current_function_decl)
25637 restore_omp_privatization_clauses (omp_privatization_save);
25638 }
25639
25640 /* Produce the definition of D, a _DECL generated from a template. If
25641 DEFER_OK is true, then we don't have to actually do the
25642 instantiation now; we just have to do it sometime. Normally it is
25643 an error if this is an explicit instantiation but D is undefined.
25644 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25645 instantiated class template. */
25646
25647 tree
25648 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25649 {
25650 tree tmpl = DECL_TI_TEMPLATE (d);
25651 tree gen_args;
25652 tree args;
25653 tree td;
25654 tree code_pattern;
25655 tree spec;
25656 tree gen_tmpl;
25657 bool pattern_defined;
25658 location_t saved_loc = input_location;
25659 int saved_unevaluated_operand = cp_unevaluated_operand;
25660 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25661 bool external_p;
25662 bool deleted_p;
25663
25664 /* This function should only be used to instantiate templates for
25665 functions and static member variables. */
25666 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25667
25668 /* A concept is never instantiated. */
25669 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25670
25671 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
25672
25673 /* Variables are never deferred; if instantiation is required, they
25674 are instantiated right away. That allows for better code in the
25675 case that an expression refers to the value of the variable --
25676 if the variable has a constant value the referring expression can
25677 take advantage of that fact. */
25678 if (VAR_P (d))
25679 defer_ok = false;
25680
25681 /* Don't instantiate cloned functions. Instead, instantiate the
25682 functions they cloned. */
25683 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25684 d = DECL_CLONED_FUNCTION (d);
25685
25686 if (DECL_TEMPLATE_INSTANTIATED (d)
25687 || TREE_TYPE (d) == error_mark_node
25688 || (TREE_CODE (d) == FUNCTION_DECL
25689 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25690 || DECL_TEMPLATE_SPECIALIZATION (d))
25691 /* D has already been instantiated or explicitly specialized, so
25692 there's nothing for us to do here.
25693
25694 It might seem reasonable to check whether or not D is an explicit
25695 instantiation, and, if so, stop here. But when an explicit
25696 instantiation is deferred until the end of the compilation,
25697 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25698 the instantiation. */
25699 return d;
25700
25701 /* Check to see whether we know that this template will be
25702 instantiated in some other file, as with "extern template"
25703 extension. */
25704 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25705
25706 /* In general, we do not instantiate such templates. */
25707 if (external_p && !always_instantiate_p (d))
25708 return d;
25709
25710 gen_tmpl = most_general_template (tmpl);
25711 gen_args = DECL_TI_ARGS (d);
25712
25713 /* We should already have the extra args. */
25714 gcc_checking_assert (tmpl == gen_tmpl
25715 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25716 == TMPL_ARGS_DEPTH (gen_args)));
25717 /* And what's in the hash table should match D. */
25718 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25719 == d
25720 || spec == NULL_TREE);
25721
25722 /* This needs to happen before any tsubsting. */
25723 if (! push_tinst_level (d))
25724 return d;
25725
25726 timevar_push (TV_TEMPLATE_INST);
25727
25728 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25729 for the instantiation. */
25730 td = template_for_substitution (d);
25731 args = gen_args;
25732
25733 if (VAR_P (d))
25734 {
25735 /* Look up an explicit specialization, if any. */
25736 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25737 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25738 if (elt && elt != error_mark_node)
25739 {
25740 td = TREE_VALUE (elt);
25741 args = TREE_PURPOSE (elt);
25742 }
25743 }
25744
25745 code_pattern = DECL_TEMPLATE_RESULT (td);
25746
25747 /* We should never be trying to instantiate a member of a class
25748 template or partial specialization. */
25749 gcc_assert (d != code_pattern);
25750
25751 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25752 || DECL_TEMPLATE_SPECIALIZATION (td))
25753 /* In the case of a friend template whose definition is provided
25754 outside the class, we may have too many arguments. Drop the
25755 ones we don't need. The same is true for specializations. */
25756 args = get_innermost_template_args
25757 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25758
25759 if (TREE_CODE (d) == FUNCTION_DECL)
25760 {
25761 deleted_p = DECL_DELETED_FN (code_pattern);
25762 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25763 && DECL_INITIAL (code_pattern) != error_mark_node)
25764 || DECL_DEFAULTED_FN (code_pattern)
25765 || deleted_p);
25766 }
25767 else
25768 {
25769 deleted_p = false;
25770 if (DECL_CLASS_SCOPE_P (code_pattern))
25771 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25772 else
25773 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25774 }
25775
25776 /* We may be in the middle of deferred access check. Disable it now. */
25777 push_deferring_access_checks (dk_no_deferred);
25778
25779 /* Unless an explicit instantiation directive has already determined
25780 the linkage of D, remember that a definition is available for
25781 this entity. */
25782 if (pattern_defined
25783 && !DECL_INTERFACE_KNOWN (d)
25784 && !DECL_NOT_REALLY_EXTERN (d))
25785 mark_definable (d);
25786
25787 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25788 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25789 input_location = DECL_SOURCE_LOCATION (d);
25790
25791 /* If D is a member of an explicitly instantiated class template,
25792 and no definition is available, treat it like an implicit
25793 instantiation. */
25794 if (!pattern_defined && expl_inst_class_mem_p
25795 && DECL_EXPLICIT_INSTANTIATION (d))
25796 {
25797 /* Leave linkage flags alone on instantiations with anonymous
25798 visibility. */
25799 if (TREE_PUBLIC (d))
25800 {
25801 DECL_NOT_REALLY_EXTERN (d) = 0;
25802 DECL_INTERFACE_KNOWN (d) = 0;
25803 }
25804 SET_DECL_IMPLICIT_INSTANTIATION (d);
25805 }
25806
25807 /* Defer all other templates, unless we have been explicitly
25808 forbidden from doing so. */
25809 if (/* If there is no definition, we cannot instantiate the
25810 template. */
25811 ! pattern_defined
25812 /* If it's OK to postpone instantiation, do so. */
25813 || defer_ok
25814 /* If this is a static data member that will be defined
25815 elsewhere, we don't want to instantiate the entire data
25816 member, but we do want to instantiate the initializer so that
25817 we can substitute that elsewhere. */
25818 || (external_p && VAR_P (d))
25819 /* Handle here a deleted function too, avoid generating
25820 its body (c++/61080). */
25821 || deleted_p)
25822 {
25823 /* The definition of the static data member is now required so
25824 we must substitute the initializer. */
25825 if (VAR_P (d)
25826 && !DECL_INITIAL (d)
25827 && DECL_INITIAL (code_pattern))
25828 {
25829 tree ns;
25830 tree init;
25831 bool const_init = false;
25832 bool enter_context = DECL_CLASS_SCOPE_P (d);
25833
25834 ns = decl_namespace_context (d);
25835 push_nested_namespace (ns);
25836 if (enter_context)
25837 push_nested_class (DECL_CONTEXT (d));
25838 init = tsubst_expr (DECL_INITIAL (code_pattern),
25839 args,
25840 tf_warning_or_error, NULL_TREE,
25841 /*integral_constant_expression_p=*/false);
25842 /* If instantiating the initializer involved instantiating this
25843 again, don't call cp_finish_decl twice. */
25844 if (!DECL_INITIAL (d))
25845 {
25846 /* Make sure the initializer is still constant, in case of
25847 circular dependency (template/instantiate6.C). */
25848 const_init
25849 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25850 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25851 /*asmspec_tree=*/NULL_TREE,
25852 LOOKUP_ONLYCONVERTING);
25853 }
25854 if (enter_context)
25855 pop_nested_class ();
25856 pop_nested_namespace (ns);
25857 }
25858
25859 /* We restore the source position here because it's used by
25860 add_pending_template. */
25861 input_location = saved_loc;
25862
25863 if (at_eof && !pattern_defined
25864 && DECL_EXPLICIT_INSTANTIATION (d)
25865 && DECL_NOT_REALLY_EXTERN (d))
25866 /* [temp.explicit]
25867
25868 The definition of a non-exported function template, a
25869 non-exported member function template, or a non-exported
25870 member function or static data member of a class template
25871 shall be present in every translation unit in which it is
25872 explicitly instantiated. */
25873 permerror (input_location, "explicit instantiation of %qD "
25874 "but no definition available", d);
25875
25876 /* If we're in unevaluated context, we just wanted to get the
25877 constant value; this isn't an odr use, so don't queue
25878 a full instantiation. */
25879 if (!cp_unevaluated_operand
25880 /* ??? Historically, we have instantiated inline functions, even
25881 when marked as "extern template". */
25882 && !(external_p && VAR_P (d)))
25883 add_pending_template (d);
25884 }
25885 else
25886 {
25887 if (variable_template_p (gen_tmpl))
25888 note_variable_template_instantiation (d);
25889 instantiate_body (td, args, d, false);
25890 }
25891
25892 pop_deferring_access_checks ();
25893 timevar_pop (TV_TEMPLATE_INST);
25894 pop_tinst_level ();
25895 input_location = saved_loc;
25896 cp_unevaluated_operand = saved_unevaluated_operand;
25897 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25898
25899 return d;
25900 }
25901
25902 /* Run through the list of templates that we wish we could
25903 instantiate, and instantiate any we can. RETRIES is the
25904 number of times we retry pending template instantiation. */
25905
25906 void
25907 instantiate_pending_templates (int retries)
25908 {
25909 int reconsider;
25910 location_t saved_loc = input_location;
25911
25912 /* Instantiating templates may trigger vtable generation. This in turn
25913 may require further template instantiations. We place a limit here
25914 to avoid infinite loop. */
25915 if (pending_templates && retries >= max_tinst_depth)
25916 {
25917 tree decl = pending_templates->tinst->maybe_get_node ();
25918
25919 fatal_error (input_location,
25920 "template instantiation depth exceeds maximum of %d"
25921 " instantiating %q+D, possibly from virtual table generation"
25922 " (use %<-ftemplate-depth=%> to increase the maximum)",
25923 max_tinst_depth, decl);
25924 if (TREE_CODE (decl) == FUNCTION_DECL)
25925 /* Pretend that we defined it. */
25926 DECL_INITIAL (decl) = error_mark_node;
25927 return;
25928 }
25929
25930 do
25931 {
25932 struct pending_template **t = &pending_templates;
25933 struct pending_template *last = NULL;
25934 reconsider = 0;
25935 while (*t)
25936 {
25937 tree instantiation = reopen_tinst_level ((*t)->tinst);
25938 bool complete = false;
25939
25940 if (TYPE_P (instantiation))
25941 {
25942 if (!COMPLETE_TYPE_P (instantiation))
25943 {
25944 instantiate_class_template (instantiation);
25945 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25946 for (tree fld = TYPE_FIELDS (instantiation);
25947 fld; fld = TREE_CHAIN (fld))
25948 if ((VAR_P (fld)
25949 || (TREE_CODE (fld) == FUNCTION_DECL
25950 && !DECL_ARTIFICIAL (fld)))
25951 && DECL_TEMPLATE_INSTANTIATION (fld))
25952 instantiate_decl (fld,
25953 /*defer_ok=*/false,
25954 /*expl_inst_class_mem_p=*/false);
25955
25956 if (COMPLETE_TYPE_P (instantiation))
25957 reconsider = 1;
25958 }
25959
25960 complete = COMPLETE_TYPE_P (instantiation);
25961 }
25962 else
25963 {
25964 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25965 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25966 {
25967 instantiation
25968 = instantiate_decl (instantiation,
25969 /*defer_ok=*/false,
25970 /*expl_inst_class_mem_p=*/false);
25971 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25972 reconsider = 1;
25973 }
25974
25975 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25976 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25977 }
25978
25979 if (complete)
25980 {
25981 /* If INSTANTIATION has been instantiated, then we don't
25982 need to consider it again in the future. */
25983 struct pending_template *drop = *t;
25984 *t = (*t)->next;
25985 set_refcount_ptr (drop->tinst);
25986 pending_template_freelist ().free (drop);
25987 }
25988 else
25989 {
25990 last = *t;
25991 t = &(*t)->next;
25992 }
25993 tinst_depth = 0;
25994 set_refcount_ptr (current_tinst_level);
25995 }
25996 last_pending_template = last;
25997 }
25998 while (reconsider);
25999
26000 input_location = saved_loc;
26001 }
26002
26003 /* Substitute ARGVEC into T, which is a list of initializers for
26004 either base class or a non-static data member. The TREE_PURPOSEs
26005 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26006 instantiate_decl. */
26007
26008 static tree
26009 tsubst_initializer_list (tree t, tree argvec)
26010 {
26011 tree inits = NULL_TREE;
26012 tree target_ctor = error_mark_node;
26013
26014 for (; t; t = TREE_CHAIN (t))
26015 {
26016 tree decl;
26017 tree init;
26018 tree expanded_bases = NULL_TREE;
26019 tree expanded_arguments = NULL_TREE;
26020 int i, len = 1;
26021
26022 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
26023 {
26024 tree expr;
26025 tree arg;
26026
26027 /* Expand the base class expansion type into separate base
26028 classes. */
26029 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26030 tf_warning_or_error,
26031 NULL_TREE);
26032 if (expanded_bases == error_mark_node)
26033 continue;
26034
26035 /* We'll be building separate TREE_LISTs of arguments for
26036 each base. */
26037 len = TREE_VEC_LENGTH (expanded_bases);
26038 expanded_arguments = make_tree_vec (len);
26039 for (i = 0; i < len; i++)
26040 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26041
26042 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26043 expand each argument in the TREE_VALUE of t. */
26044 expr = make_node (EXPR_PACK_EXPANSION);
26045 PACK_EXPANSION_LOCAL_P (expr) = true;
26046 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26047 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26048
26049 if (TREE_VALUE (t) == void_type_node)
26050 /* VOID_TYPE_NODE is used to indicate
26051 value-initialization. */
26052 {
26053 for (i = 0; i < len; i++)
26054 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26055 }
26056 else
26057 {
26058 /* Substitute parameter packs into each argument in the
26059 TREE_LIST. */
26060 in_base_initializer = 1;
26061 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26062 {
26063 tree expanded_exprs;
26064
26065 /* Expand the argument. */
26066 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
26067 expanded_exprs
26068 = tsubst_pack_expansion (expr, argvec,
26069 tf_warning_or_error,
26070 NULL_TREE);
26071 if (expanded_exprs == error_mark_node)
26072 continue;
26073
26074 /* Prepend each of the expanded expressions to the
26075 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26076 for (i = 0; i < len; i++)
26077 {
26078 TREE_VEC_ELT (expanded_arguments, i) =
26079 tree_cons (NULL_TREE,
26080 TREE_VEC_ELT (expanded_exprs, i),
26081 TREE_VEC_ELT (expanded_arguments, i));
26082 }
26083 }
26084 in_base_initializer = 0;
26085
26086 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26087 since we built them backwards. */
26088 for (i = 0; i < len; i++)
26089 {
26090 TREE_VEC_ELT (expanded_arguments, i) =
26091 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26092 }
26093 }
26094 }
26095
26096 for (i = 0; i < len; ++i)
26097 {
26098 if (expanded_bases)
26099 {
26100 decl = TREE_VEC_ELT (expanded_bases, i);
26101 decl = expand_member_init (decl);
26102 init = TREE_VEC_ELT (expanded_arguments, i);
26103 }
26104 else
26105 {
26106 tree tmp;
26107 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26108 tf_warning_or_error, NULL_TREE);
26109
26110 decl = expand_member_init (decl);
26111 if (decl && !DECL_P (decl))
26112 in_base_initializer = 1;
26113
26114 init = TREE_VALUE (t);
26115 tmp = init;
26116 if (init != void_type_node)
26117 init = tsubst_expr (init, argvec,
26118 tf_warning_or_error, NULL_TREE,
26119 /*integral_constant_expression_p=*/false);
26120 if (init == NULL_TREE && tmp != NULL_TREE)
26121 /* If we had an initializer but it instantiated to nothing,
26122 value-initialize the object. This will only occur when
26123 the initializer was a pack expansion where the parameter
26124 packs used in that expansion were of length zero. */
26125 init = void_type_node;
26126 in_base_initializer = 0;
26127 }
26128
26129 if (target_ctor != error_mark_node
26130 && init != error_mark_node)
26131 {
26132 error ("mem-initializer for %qD follows constructor delegation",
26133 decl);
26134 return inits;
26135 }
26136 /* Look for a target constructor. */
26137 if (init != error_mark_node
26138 && decl && CLASS_TYPE_P (decl)
26139 && same_type_p (decl, current_class_type))
26140 {
26141 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26142 if (inits)
26143 {
26144 error ("constructor delegation follows mem-initializer for %qD",
26145 TREE_PURPOSE (inits));
26146 continue;
26147 }
26148 target_ctor = init;
26149 }
26150
26151 if (decl)
26152 {
26153 init = build_tree_list (decl, init);
26154 /* Carry over the dummy TREE_TYPE node containing the source
26155 location. */
26156 TREE_TYPE (init) = TREE_TYPE (t);
26157 TREE_CHAIN (init) = inits;
26158 inits = init;
26159 }
26160 }
26161 }
26162 return inits;
26163 }
26164
26165 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26166
26167 static void
26168 set_current_access_from_decl (tree decl)
26169 {
26170 if (TREE_PRIVATE (decl))
26171 current_access_specifier = access_private_node;
26172 else if (TREE_PROTECTED (decl))
26173 current_access_specifier = access_protected_node;
26174 else
26175 current_access_specifier = access_public_node;
26176 }
26177
26178 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26179 is the instantiation (which should have been created with
26180 start_enum) and ARGS are the template arguments to use. */
26181
26182 static void
26183 tsubst_enum (tree tag, tree newtag, tree args)
26184 {
26185 tree e;
26186
26187 if (SCOPED_ENUM_P (newtag))
26188 begin_scope (sk_scoped_enum, newtag);
26189
26190 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26191 {
26192 tree value;
26193 tree decl;
26194
26195 decl = TREE_VALUE (e);
26196 /* Note that in a template enum, the TREE_VALUE is the
26197 CONST_DECL, not the corresponding INTEGER_CST. */
26198 value = tsubst_expr (DECL_INITIAL (decl),
26199 args, tf_warning_or_error, NULL_TREE,
26200 /*integral_constant_expression_p=*/true);
26201
26202 /* Give this enumeration constant the correct access. */
26203 set_current_access_from_decl (decl);
26204
26205 /* Actually build the enumerator itself. Here we're assuming that
26206 enumerators can't have dependent attributes. */
26207 build_enumerator (DECL_NAME (decl), value, newtag,
26208 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26209 }
26210
26211 if (SCOPED_ENUM_P (newtag))
26212 finish_scope ();
26213
26214 finish_enum_value_list (newtag);
26215 finish_enum (newtag);
26216
26217 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26218 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26219 }
26220
26221 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26222 its type -- but without substituting the innermost set of template
26223 arguments. So, innermost set of template parameters will appear in
26224 the type. */
26225
26226 tree
26227 get_mostly_instantiated_function_type (tree decl)
26228 {
26229 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26230 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26231 }
26232
26233 /* Return truthvalue if we're processing a template different from
26234 the last one involved in diagnostics. */
26235 bool
26236 problematic_instantiation_changed (void)
26237 {
26238 return current_tinst_level != last_error_tinst_level;
26239 }
26240
26241 /* Remember current template involved in diagnostics. */
26242 void
26243 record_last_problematic_instantiation (void)
26244 {
26245 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26246 }
26247
26248 struct tinst_level *
26249 current_instantiation (void)
26250 {
26251 return current_tinst_level;
26252 }
26253
26254 /* Return TRUE if current_function_decl is being instantiated, false
26255 otherwise. */
26256
26257 bool
26258 instantiating_current_function_p (void)
26259 {
26260 return (current_instantiation ()
26261 && (current_instantiation ()->maybe_get_node ()
26262 == current_function_decl));
26263 }
26264
26265 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26266 type. Return false for ok, true for disallowed. Issue error and
26267 inform messages under control of COMPLAIN. */
26268
26269 static bool
26270 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26271 {
26272 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26273 return false;
26274 else if (TYPE_PTR_P (type))
26275 return false;
26276 else if (TYPE_REF_P (type)
26277 && !TYPE_REF_IS_RVALUE (type))
26278 return false;
26279 else if (TYPE_PTRMEM_P (type))
26280 return false;
26281 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26282 {
26283 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26284 {
26285 if (complain & tf_error)
26286 error ("non-type template parameters of deduced class type only "
26287 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26288 return true;
26289 }
26290 return false;
26291 }
26292 else if (TREE_CODE (type) == TYPENAME_TYPE)
26293 return false;
26294 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26295 return false;
26296 else if (TREE_CODE (type) == NULLPTR_TYPE)
26297 return false;
26298 /* A bound template template parm could later be instantiated to have a valid
26299 nontype parm type via an alias template. */
26300 else if (cxx_dialect >= cxx11
26301 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26302 return false;
26303 else if (VOID_TYPE_P (type))
26304 /* Fall through. */;
26305 else if (cxx_dialect >= cxx20)
26306 {
26307 if (dependent_type_p (type))
26308 return false;
26309 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26310 return true;
26311 if (structural_type_p (type))
26312 return false;
26313 if (complain & tf_error)
26314 {
26315 auto_diagnostic_group d;
26316 error ("%qT is not a valid type for a template non-type "
26317 "parameter because it is not structural", type);
26318 structural_type_p (type, true);
26319 }
26320 return true;
26321 }
26322 else if (CLASS_TYPE_P (type))
26323 {
26324 if (complain & tf_error)
26325 error ("non-type template parameters of class type only available "
26326 "with %<-std=c++20%> or %<-std=gnu++20%>");
26327 return true;
26328 }
26329
26330 if (complain & tf_error)
26331 {
26332 if (type == error_mark_node)
26333 inform (input_location, "invalid template non-type parameter");
26334 else
26335 error ("%q#T is not a valid type for a template non-type parameter",
26336 type);
26337 }
26338 return true;
26339 }
26340
26341 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26342 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26343
26344 static bool
26345 dependent_type_p_r (tree type)
26346 {
26347 tree scope;
26348
26349 /* [temp.dep.type]
26350
26351 A type is dependent if it is:
26352
26353 -- a template parameter. Template template parameters are types
26354 for us (since TYPE_P holds true for them) so we handle
26355 them here. */
26356 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26357 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26358 return true;
26359 /* -- a qualified-id with a nested-name-specifier which contains a
26360 class-name that names a dependent type or whose unqualified-id
26361 names a dependent type. */
26362 if (TREE_CODE (type) == TYPENAME_TYPE)
26363 return true;
26364
26365 /* An alias template specialization can be dependent even if the
26366 resulting type is not. */
26367 if (dependent_alias_template_spec_p (type, nt_transparent))
26368 return true;
26369
26370 /* -- a cv-qualified type where the cv-unqualified type is
26371 dependent.
26372 No code is necessary for this bullet; the code below handles
26373 cv-qualified types, and we don't want to strip aliases with
26374 TYPE_MAIN_VARIANT because of DR 1558. */
26375 /* -- a compound type constructed from any dependent type. */
26376 if (TYPE_PTRMEM_P (type))
26377 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26378 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26379 (type)));
26380 else if (INDIRECT_TYPE_P (type))
26381 return dependent_type_p (TREE_TYPE (type));
26382 else if (FUNC_OR_METHOD_TYPE_P (type))
26383 {
26384 tree arg_type;
26385
26386 if (dependent_type_p (TREE_TYPE (type)))
26387 return true;
26388 for (arg_type = TYPE_ARG_TYPES (type);
26389 arg_type;
26390 arg_type = TREE_CHAIN (arg_type))
26391 if (dependent_type_p (TREE_VALUE (arg_type)))
26392 return true;
26393 if (cxx_dialect >= cxx17)
26394 /* A value-dependent noexcept-specifier makes the type dependent. */
26395 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26396 if (tree noex = TREE_PURPOSE (spec))
26397 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26398 affect overload resolution and treating it as dependent breaks
26399 things. Same for an unparsed noexcept expression. */
26400 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26401 && TREE_CODE (noex) != DEFERRED_PARSE
26402 && value_dependent_expression_p (noex))
26403 return true;
26404 return false;
26405 }
26406 /* -- an array type constructed from any dependent type or whose
26407 size is specified by a constant expression that is
26408 value-dependent.
26409
26410 We checked for type- and value-dependence of the bounds in
26411 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26412 if (TREE_CODE (type) == ARRAY_TYPE)
26413 {
26414 if (TYPE_DOMAIN (type)
26415 && dependent_type_p (TYPE_DOMAIN (type)))
26416 return true;
26417 return dependent_type_p (TREE_TYPE (type));
26418 }
26419
26420 /* -- a template-id in which either the template name is a template
26421 parameter ... */
26422 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26423 return true;
26424 /* ... or any of the template arguments is a dependent type or
26425 an expression that is type-dependent or value-dependent. */
26426 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26427 && (any_dependent_template_arguments_p
26428 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26429 return true;
26430
26431 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26432 dependent; if the argument of the `typeof' expression is not
26433 type-dependent, then it should already been have resolved. */
26434 if (TREE_CODE (type) == TYPEOF_TYPE
26435 || TREE_CODE (type) == DECLTYPE_TYPE
26436 || TREE_CODE (type) == UNDERLYING_TYPE)
26437 return true;
26438
26439 /* A template argument pack is dependent if any of its packed
26440 arguments are. */
26441 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26442 {
26443 tree args = ARGUMENT_PACK_ARGS (type);
26444 int i, len = TREE_VEC_LENGTH (args);
26445 for (i = 0; i < len; ++i)
26446 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26447 return true;
26448 }
26449
26450 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26451 be template parameters. */
26452 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26453 return true;
26454
26455 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26456 return true;
26457
26458 /* The standard does not specifically mention types that are local
26459 to template functions or local classes, but they should be
26460 considered dependent too. For example:
26461
26462 template <int I> void f() {
26463 enum E { a = I };
26464 S<sizeof (E)> s;
26465 }
26466
26467 The size of `E' cannot be known until the value of `I' has been
26468 determined. Therefore, `E' must be considered dependent. */
26469 scope = TYPE_CONTEXT (type);
26470 if (scope && TYPE_P (scope))
26471 return dependent_type_p (scope);
26472 /* Don't use type_dependent_expression_p here, as it can lead
26473 to infinite recursion trying to determine whether a lambda
26474 nested in a lambda is dependent (c++/47687). */
26475 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26476 && DECL_LANG_SPECIFIC (scope)
26477 && DECL_TEMPLATE_INFO (scope)
26478 && (any_dependent_template_arguments_p
26479 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26480 return true;
26481
26482 /* Other types are non-dependent. */
26483 return false;
26484 }
26485
26486 /* Returns TRUE if TYPE is dependent, in the sense of
26487 [temp.dep.type]. Note that a NULL type is considered dependent. */
26488
26489 bool
26490 dependent_type_p (tree type)
26491 {
26492 /* If there are no template parameters in scope, then there can't be
26493 any dependent types. */
26494 if (!processing_template_decl)
26495 {
26496 /* If we are not processing a template, then nobody should be
26497 providing us with a dependent type. */
26498 gcc_assert (type);
26499 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26500 return false;
26501 }
26502
26503 /* If the type is NULL, we have not computed a type for the entity
26504 in question; in that case, the type is dependent. */
26505 if (!type)
26506 return true;
26507
26508 /* Erroneous types can be considered non-dependent. */
26509 if (type == error_mark_node)
26510 return false;
26511
26512 /* Getting here with global_type_node means we improperly called this
26513 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26514 gcc_checking_assert (type != global_type_node);
26515
26516 /* If we have not already computed the appropriate value for TYPE,
26517 do so now. */
26518 if (!TYPE_DEPENDENT_P_VALID (type))
26519 {
26520 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26521 TYPE_DEPENDENT_P_VALID (type) = 1;
26522 }
26523
26524 return TYPE_DEPENDENT_P (type);
26525 }
26526
26527 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26528 lookup. In other words, a dependent type that is not the current
26529 instantiation. */
26530
26531 bool
26532 dependent_scope_p (tree scope)
26533 {
26534 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26535 && !currently_open_class (scope));
26536 }
26537
26538 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26539 an unknown base of 'this' (and is therefore instantiation-dependent). */
26540
26541 static bool
26542 unknown_base_ref_p (tree t)
26543 {
26544 if (!current_class_ptr)
26545 return false;
26546
26547 tree mem = TREE_OPERAND (t, 1);
26548 if (shared_member_p (mem))
26549 return false;
26550
26551 tree cur = current_nonlambda_class_type ();
26552 if (!any_dependent_bases_p (cur))
26553 return false;
26554
26555 tree ctx = TREE_OPERAND (t, 0);
26556 if (DERIVED_FROM_P (ctx, cur))
26557 return false;
26558
26559 return true;
26560 }
26561
26562 /* T is a SCOPE_REF; return whether we need to consider it
26563 instantiation-dependent so that we can check access at instantiation
26564 time even though we know which member it resolves to. */
26565
26566 static bool
26567 instantiation_dependent_scope_ref_p (tree t)
26568 {
26569 if (DECL_P (TREE_OPERAND (t, 1))
26570 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26571 && !unknown_base_ref_p (t)
26572 && accessible_in_template_p (TREE_OPERAND (t, 0),
26573 TREE_OPERAND (t, 1)))
26574 return false;
26575 else
26576 return true;
26577 }
26578
26579 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26580 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26581 expression. */
26582
26583 /* Note that this predicate is not appropriate for general expressions;
26584 only constant expressions (that satisfy potential_constant_expression)
26585 can be tested for value dependence. */
26586
26587 bool
26588 value_dependent_expression_p (tree expression)
26589 {
26590 if (!processing_template_decl || expression == NULL_TREE)
26591 return false;
26592
26593 /* A type-dependent expression is also value-dependent. */
26594 if (type_dependent_expression_p (expression))
26595 return true;
26596
26597 switch (TREE_CODE (expression))
26598 {
26599 case BASELINK:
26600 /* A dependent member function of the current instantiation. */
26601 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26602
26603 case FUNCTION_DECL:
26604 /* A dependent member function of the current instantiation. */
26605 if (DECL_CLASS_SCOPE_P (expression)
26606 && dependent_type_p (DECL_CONTEXT (expression)))
26607 return true;
26608 break;
26609
26610 case IDENTIFIER_NODE:
26611 /* A name that has not been looked up -- must be dependent. */
26612 return true;
26613
26614 case TEMPLATE_PARM_INDEX:
26615 /* A non-type template parm. */
26616 return true;
26617
26618 case CONST_DECL:
26619 /* A non-type template parm. */
26620 if (DECL_TEMPLATE_PARM_P (expression))
26621 return true;
26622 return value_dependent_expression_p (DECL_INITIAL (expression));
26623
26624 case VAR_DECL:
26625 /* A constant with literal type and is initialized
26626 with an expression that is value-dependent. */
26627 if (DECL_DEPENDENT_INIT_P (expression)
26628 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26629 || TYPE_REF_P (TREE_TYPE (expression)))
26630 return true;
26631 if (DECL_HAS_VALUE_EXPR_P (expression))
26632 {
26633 tree value_expr = DECL_VALUE_EXPR (expression);
26634 if (value_dependent_expression_p (value_expr)
26635 /* __PRETTY_FUNCTION__ inside a template function is dependent
26636 on the name of the function. */
26637 || (DECL_PRETTY_FUNCTION_P (expression)
26638 /* It might be used in a template, but not a template
26639 function, in which case its DECL_VALUE_EXPR will be
26640 "top level". */
26641 && value_expr == error_mark_node))
26642 return true;
26643 }
26644 return false;
26645
26646 case DYNAMIC_CAST_EXPR:
26647 case STATIC_CAST_EXPR:
26648 case CONST_CAST_EXPR:
26649 case REINTERPRET_CAST_EXPR:
26650 case CAST_EXPR:
26651 case IMPLICIT_CONV_EXPR:
26652 /* These expressions are value-dependent if the type to which
26653 the cast occurs is dependent or the expression being casted
26654 is value-dependent. */
26655 {
26656 tree type = TREE_TYPE (expression);
26657
26658 if (dependent_type_p (type))
26659 return true;
26660
26661 /* A functional cast has a list of operands. */
26662 expression = TREE_OPERAND (expression, 0);
26663 if (!expression)
26664 {
26665 /* If there are no operands, it must be an expression such
26666 as "int()". This should not happen for aggregate types
26667 because it would form non-constant expressions. */
26668 gcc_assert (cxx_dialect >= cxx11
26669 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26670
26671 return false;
26672 }
26673
26674 if (TREE_CODE (expression) == TREE_LIST)
26675 return any_value_dependent_elements_p (expression);
26676
26677 return value_dependent_expression_p (expression);
26678 }
26679
26680 case SIZEOF_EXPR:
26681 if (SIZEOF_EXPR_TYPE_P (expression))
26682 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26683 /* FALLTHRU */
26684 case ALIGNOF_EXPR:
26685 case TYPEID_EXPR:
26686 /* A `sizeof' expression is value-dependent if the operand is
26687 type-dependent or is a pack expansion. */
26688 expression = TREE_OPERAND (expression, 0);
26689 if (PACK_EXPANSION_P (expression))
26690 return true;
26691 else if (TYPE_P (expression))
26692 return dependent_type_p (expression);
26693 return instantiation_dependent_uneval_expression_p (expression);
26694
26695 case AT_ENCODE_EXPR:
26696 /* An 'encode' expression is value-dependent if the operand is
26697 type-dependent. */
26698 expression = TREE_OPERAND (expression, 0);
26699 return dependent_type_p (expression);
26700
26701 case NOEXCEPT_EXPR:
26702 expression = TREE_OPERAND (expression, 0);
26703 return instantiation_dependent_uneval_expression_p (expression);
26704
26705 case SCOPE_REF:
26706 /* All instantiation-dependent expressions should also be considered
26707 value-dependent. */
26708 return instantiation_dependent_scope_ref_p (expression);
26709
26710 case COMPONENT_REF:
26711 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26712 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26713
26714 case NONTYPE_ARGUMENT_PACK:
26715 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26716 is value-dependent. */
26717 {
26718 tree values = ARGUMENT_PACK_ARGS (expression);
26719 int i, len = TREE_VEC_LENGTH (values);
26720
26721 for (i = 0; i < len; ++i)
26722 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26723 return true;
26724
26725 return false;
26726 }
26727
26728 case TRAIT_EXPR:
26729 {
26730 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26731
26732 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26733 return true;
26734
26735 if (!type2)
26736 return false;
26737
26738 if (TREE_CODE (type2) != TREE_LIST)
26739 return dependent_type_p (type2);
26740
26741 for (; type2; type2 = TREE_CHAIN (type2))
26742 if (dependent_type_p (TREE_VALUE (type2)))
26743 return true;
26744
26745 return false;
26746 }
26747
26748 case MODOP_EXPR:
26749 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26750 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26751
26752 case ARRAY_REF:
26753 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26754 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26755
26756 case ADDR_EXPR:
26757 {
26758 tree op = TREE_OPERAND (expression, 0);
26759 return (value_dependent_expression_p (op)
26760 || has_value_dependent_address (op));
26761 }
26762
26763 case REQUIRES_EXPR:
26764 /* Treat all requires-expressions as value-dependent so
26765 we don't try to fold them. */
26766 return true;
26767
26768 case TYPE_REQ:
26769 return dependent_type_p (TREE_OPERAND (expression, 0));
26770
26771 case CALL_EXPR:
26772 {
26773 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26774 return true;
26775 tree fn = get_callee_fndecl (expression);
26776 int i, nargs;
26777 nargs = call_expr_nargs (expression);
26778 for (i = 0; i < nargs; ++i)
26779 {
26780 tree op = CALL_EXPR_ARG (expression, i);
26781 /* In a call to a constexpr member function, look through the
26782 implicit ADDR_EXPR on the object argument so that it doesn't
26783 cause the call to be considered value-dependent. We also
26784 look through it in potential_constant_expression. */
26785 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26786 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26787 && TREE_CODE (op) == ADDR_EXPR)
26788 op = TREE_OPERAND (op, 0);
26789 if (value_dependent_expression_p (op))
26790 return true;
26791 }
26792 return false;
26793 }
26794
26795 case TEMPLATE_ID_EXPR:
26796 return concept_definition_p (TREE_OPERAND (expression, 0));
26797
26798 case CONSTRUCTOR:
26799 {
26800 unsigned ix;
26801 tree val;
26802 if (dependent_type_p (TREE_TYPE (expression)))
26803 return true;
26804 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26805 if (value_dependent_expression_p (val))
26806 return true;
26807 return false;
26808 }
26809
26810 case STMT_EXPR:
26811 /* Treat a GNU statement expression as dependent to avoid crashing
26812 under instantiate_non_dependent_expr; it can't be constant. */
26813 return true;
26814
26815 default:
26816 /* A constant expression is value-dependent if any subexpression is
26817 value-dependent. */
26818 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26819 {
26820 case tcc_reference:
26821 case tcc_unary:
26822 case tcc_comparison:
26823 case tcc_binary:
26824 case tcc_expression:
26825 case tcc_vl_exp:
26826 {
26827 int i, len = cp_tree_operand_length (expression);
26828
26829 for (i = 0; i < len; i++)
26830 {
26831 tree t = TREE_OPERAND (expression, i);
26832
26833 /* In some cases, some of the operands may be missing.
26834 (For example, in the case of PREDECREMENT_EXPR, the
26835 amount to increment by may be missing.) That doesn't
26836 make the expression dependent. */
26837 if (t && value_dependent_expression_p (t))
26838 return true;
26839 }
26840 }
26841 break;
26842 default:
26843 break;
26844 }
26845 break;
26846 }
26847
26848 /* The expression is not value-dependent. */
26849 return false;
26850 }
26851
26852 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26853 [temp.dep.expr]. Note that an expression with no type is
26854 considered dependent. Other parts of the compiler arrange for an
26855 expression with type-dependent subexpressions to have no type, so
26856 this function doesn't have to be fully recursive. */
26857
26858 bool
26859 type_dependent_expression_p (tree expression)
26860 {
26861 if (!processing_template_decl)
26862 return false;
26863
26864 if (expression == NULL_TREE || expression == error_mark_node)
26865 return false;
26866
26867 STRIP_ANY_LOCATION_WRAPPER (expression);
26868
26869 /* An unresolved name is always dependent. */
26870 if (identifier_p (expression)
26871 || TREE_CODE (expression) == USING_DECL
26872 || TREE_CODE (expression) == WILDCARD_DECL)
26873 return true;
26874
26875 /* A lambda-expression in template context is dependent. dependent_type_p is
26876 true for a lambda in the scope of a class or function template, but that
26877 doesn't cover all template contexts, like a default template argument. */
26878 if (TREE_CODE (expression) == LAMBDA_EXPR)
26879 return true;
26880
26881 /* A fold expression is type-dependent. */
26882 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26883 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26884 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26885 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26886 return true;
26887
26888 /* Some expression forms are never type-dependent. */
26889 if (TREE_CODE (expression) == SIZEOF_EXPR
26890 || TREE_CODE (expression) == ALIGNOF_EXPR
26891 || TREE_CODE (expression) == AT_ENCODE_EXPR
26892 || TREE_CODE (expression) == NOEXCEPT_EXPR
26893 || TREE_CODE (expression) == TRAIT_EXPR
26894 || TREE_CODE (expression) == TYPEID_EXPR
26895 || TREE_CODE (expression) == DELETE_EXPR
26896 || TREE_CODE (expression) == VEC_DELETE_EXPR
26897 || TREE_CODE (expression) == THROW_EXPR
26898 || TREE_CODE (expression) == REQUIRES_EXPR)
26899 return false;
26900
26901 /* The types of these expressions depends only on the type to which
26902 the cast occurs. */
26903 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26904 || TREE_CODE (expression) == STATIC_CAST_EXPR
26905 || TREE_CODE (expression) == CONST_CAST_EXPR
26906 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26907 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26908 || TREE_CODE (expression) == CAST_EXPR)
26909 return dependent_type_p (TREE_TYPE (expression));
26910
26911 /* The types of these expressions depends only on the type created
26912 by the expression. */
26913 if (TREE_CODE (expression) == NEW_EXPR
26914 || TREE_CODE (expression) == VEC_NEW_EXPR)
26915 {
26916 /* For NEW_EXPR tree nodes created inside a template, either
26917 the object type itself or a TREE_LIST may appear as the
26918 operand 1. */
26919 tree type = TREE_OPERAND (expression, 1);
26920 if (TREE_CODE (type) == TREE_LIST)
26921 /* This is an array type. We need to check array dimensions
26922 as well. */
26923 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26924 || value_dependent_expression_p
26925 (TREE_OPERAND (TREE_VALUE (type), 1));
26926 /* Array type whose dimension has to be deduced. */
26927 else if (TREE_CODE (type) == ARRAY_TYPE
26928 && TREE_OPERAND (expression, 2) == NULL_TREE)
26929 return true;
26930 else
26931 return dependent_type_p (type);
26932 }
26933
26934 if (TREE_CODE (expression) == SCOPE_REF)
26935 {
26936 tree scope = TREE_OPERAND (expression, 0);
26937 tree name = TREE_OPERAND (expression, 1);
26938
26939 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26940 contains an identifier associated by name lookup with one or more
26941 declarations declared with a dependent type, or...a
26942 nested-name-specifier or qualified-id that names a member of an
26943 unknown specialization. */
26944 return (type_dependent_expression_p (name)
26945 || dependent_scope_p (scope));
26946 }
26947
26948 if (TREE_CODE (expression) == TEMPLATE_DECL
26949 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26950 return uses_outer_template_parms (expression);
26951
26952 if (TREE_CODE (expression) == STMT_EXPR)
26953 expression = stmt_expr_value_expr (expression);
26954
26955 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26956 {
26957 tree elt;
26958 unsigned i;
26959
26960 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26961 {
26962 if (type_dependent_expression_p (elt))
26963 return true;
26964 }
26965 return false;
26966 }
26967
26968 /* A static data member of the current instantiation with incomplete
26969 array type is type-dependent, as the definition and specializations
26970 can have different bounds. */
26971 if (VAR_P (expression)
26972 && DECL_CLASS_SCOPE_P (expression)
26973 && dependent_type_p (DECL_CONTEXT (expression))
26974 && VAR_HAD_UNKNOWN_BOUND (expression))
26975 return true;
26976
26977 /* An array of unknown bound depending on a variadic parameter, eg:
26978
26979 template<typename... Args>
26980 void foo (Args... args)
26981 {
26982 int arr[] = { args... };
26983 }
26984
26985 template<int... vals>
26986 void bar ()
26987 {
26988 int arr[] = { vals... };
26989 }
26990
26991 If the array has no length and has an initializer, it must be that
26992 we couldn't determine its length in cp_complete_array_type because
26993 it is dependent. */
26994 if (VAR_P (expression)
26995 && TREE_TYPE (expression) != NULL_TREE
26996 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26997 && !TYPE_DOMAIN (TREE_TYPE (expression))
26998 && DECL_INITIAL (expression))
26999 return true;
27000
27001 /* A function or variable template-id is type-dependent if it has any
27002 dependent template arguments. */
27003 if (VAR_OR_FUNCTION_DECL_P (expression)
27004 && DECL_LANG_SPECIFIC (expression)
27005 && DECL_TEMPLATE_INFO (expression))
27006 {
27007 /* Consider the innermost template arguments, since those are the ones
27008 that come from the template-id; the template arguments for the
27009 enclosing class do not make it type-dependent unless they are used in
27010 the type of the decl. */
27011 if (instantiates_primary_template_p (expression)
27012 && (any_dependent_template_arguments_p
27013 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
27014 return true;
27015 }
27016
27017 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27018 type-dependent. Checking this is important for functions with auto return
27019 type, which looks like a dependent type. */
27020 if (TREE_CODE (expression) == FUNCTION_DECL
27021 && !(DECL_CLASS_SCOPE_P (expression)
27022 && dependent_type_p (DECL_CONTEXT (expression)))
27023 && !(DECL_LANG_SPECIFIC (expression)
27024 && DECL_FRIEND_P (expression)
27025 && (!DECL_FRIEND_CONTEXT (expression)
27026 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27027 && !DECL_LOCAL_DECL_P (expression))
27028 {
27029 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27030 || undeduced_auto_decl (expression));
27031 return false;
27032 }
27033
27034 /* Always dependent, on the number of arguments if nothing else. */
27035 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27036 return true;
27037
27038 if (TREE_TYPE (expression) == unknown_type_node)
27039 {
27040 if (TREE_CODE (expression) == ADDR_EXPR)
27041 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27042 if (TREE_CODE (expression) == COMPONENT_REF
27043 || TREE_CODE (expression) == OFFSET_REF)
27044 {
27045 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27046 return true;
27047 expression = TREE_OPERAND (expression, 1);
27048 if (identifier_p (expression))
27049 return false;
27050 }
27051 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27052 if (TREE_CODE (expression) == SCOPE_REF)
27053 return false;
27054
27055 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27056 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27057 || TREE_CODE (expression) == CO_YIELD_EXPR)
27058 return true;
27059
27060 if (BASELINK_P (expression))
27061 {
27062 if (BASELINK_OPTYPE (expression)
27063 && dependent_type_p (BASELINK_OPTYPE (expression)))
27064 return true;
27065 expression = BASELINK_FUNCTIONS (expression);
27066 }
27067
27068 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27069 {
27070 if (any_dependent_template_arguments_p
27071 (TREE_OPERAND (expression, 1)))
27072 return true;
27073 expression = TREE_OPERAND (expression, 0);
27074 if (identifier_p (expression))
27075 return true;
27076 }
27077
27078 gcc_assert (OVL_P (expression));
27079
27080 for (lkp_iterator iter (expression); iter; ++iter)
27081 if (type_dependent_expression_p (*iter))
27082 return true;
27083
27084 return false;
27085 }
27086
27087 /* The type of a non-type template parm declared with a placeholder type
27088 depends on the corresponding template argument, even though
27089 placeholders are not normally considered dependent. */
27090 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27091 && is_auto (TREE_TYPE (expression)))
27092 return true;
27093
27094 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27095
27096 /* Dependent type attributes might not have made it from the decl to
27097 the type yet. */
27098 if (DECL_P (expression)
27099 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27100 return true;
27101
27102 return (dependent_type_p (TREE_TYPE (expression)));
27103 }
27104
27105 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27106 type-dependent if the expression refers to a member of the current
27107 instantiation and the type of the referenced member is dependent, or the
27108 class member access expression refers to a member of an unknown
27109 specialization.
27110
27111 This function returns true if the OBJECT in such a class member access
27112 expression is of an unknown specialization. */
27113
27114 bool
27115 type_dependent_object_expression_p (tree object)
27116 {
27117 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27118 dependent. */
27119 if (TREE_CODE (object) == IDENTIFIER_NODE)
27120 return true;
27121 tree scope = TREE_TYPE (object);
27122 return (!scope || dependent_scope_p (scope));
27123 }
27124
27125 /* walk_tree callback function for instantiation_dependent_expression_p,
27126 below. Returns non-zero if a dependent subexpression is found. */
27127
27128 static tree
27129 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27130 void * /*data*/)
27131 {
27132 if (TYPE_P (*tp))
27133 {
27134 /* We don't have to worry about decltype currently because decltype
27135 of an instantiation-dependent expr is a dependent type. This
27136 might change depending on the resolution of DR 1172. */
27137 *walk_subtrees = false;
27138 return NULL_TREE;
27139 }
27140 enum tree_code code = TREE_CODE (*tp);
27141 switch (code)
27142 {
27143 /* Don't treat an argument list as dependent just because it has no
27144 TREE_TYPE. */
27145 case TREE_LIST:
27146 case TREE_VEC:
27147 case NONTYPE_ARGUMENT_PACK:
27148 return NULL_TREE;
27149
27150 case TEMPLATE_PARM_INDEX:
27151 if (dependent_type_p (TREE_TYPE (*tp)))
27152 return *tp;
27153 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27154 return *tp;
27155 /* We'll check value-dependence separately. */
27156 return NULL_TREE;
27157
27158 /* Handle expressions with type operands. */
27159 case SIZEOF_EXPR:
27160 case ALIGNOF_EXPR:
27161 case TYPEID_EXPR:
27162 case AT_ENCODE_EXPR:
27163 {
27164 tree op = TREE_OPERAND (*tp, 0);
27165 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27166 op = TREE_TYPE (op);
27167 if (TYPE_P (op))
27168 {
27169 if (dependent_type_p (op))
27170 return *tp;
27171 else
27172 {
27173 *walk_subtrees = false;
27174 return NULL_TREE;
27175 }
27176 }
27177 break;
27178 }
27179
27180 case COMPONENT_REF:
27181 if (identifier_p (TREE_OPERAND (*tp, 1)))
27182 /* In a template, finish_class_member_access_expr creates a
27183 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27184 type-dependent, so that we can check access control at
27185 instantiation time (PR 42277). See also Core issue 1273. */
27186 return *tp;
27187 break;
27188
27189 case SCOPE_REF:
27190 if (instantiation_dependent_scope_ref_p (*tp))
27191 return *tp;
27192 else
27193 break;
27194
27195 /* Treat statement-expressions as dependent. */
27196 case BIND_EXPR:
27197 return *tp;
27198
27199 /* Treat requires-expressions as dependent. */
27200 case REQUIRES_EXPR:
27201 return *tp;
27202
27203 case CALL_EXPR:
27204 /* Treat concept checks as dependent. */
27205 if (concept_check_p (*tp))
27206 return *tp;
27207 break;
27208
27209 case TEMPLATE_ID_EXPR:
27210 /* Treat concept checks as dependent. */
27211 if (concept_check_p (*tp))
27212 return *tp;
27213 break;
27214
27215 case CONSTRUCTOR:
27216 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27217 return *tp;
27218 break;
27219
27220 default:
27221 break;
27222 }
27223
27224 if (type_dependent_expression_p (*tp))
27225 return *tp;
27226 else
27227 return NULL_TREE;
27228 }
27229
27230 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27231 sense defined by the ABI:
27232
27233 "An expression is instantiation-dependent if it is type-dependent
27234 or value-dependent, or it has a subexpression that is type-dependent
27235 or value-dependent."
27236
27237 Except don't actually check value-dependence for unevaluated expressions,
27238 because in sizeof(i) we don't care about the value of i. Checking
27239 type-dependence will in turn check value-dependence of array bounds/template
27240 arguments as needed. */
27241
27242 bool
27243 instantiation_dependent_uneval_expression_p (tree expression)
27244 {
27245 tree result;
27246
27247 if (!processing_template_decl)
27248 return false;
27249
27250 if (expression == error_mark_node)
27251 return false;
27252
27253 result = cp_walk_tree_without_duplicates (&expression,
27254 instantiation_dependent_r, NULL);
27255 return result != NULL_TREE;
27256 }
27257
27258 /* As above, but also check value-dependence of the expression as a whole. */
27259
27260 bool
27261 instantiation_dependent_expression_p (tree expression)
27262 {
27263 return (instantiation_dependent_uneval_expression_p (expression)
27264 || value_dependent_expression_p (expression));
27265 }
27266
27267 /* Like type_dependent_expression_p, but it also works while not processing
27268 a template definition, i.e. during substitution or mangling. */
27269
27270 bool
27271 type_dependent_expression_p_push (tree expr)
27272 {
27273 bool b;
27274 ++processing_template_decl;
27275 b = type_dependent_expression_p (expr);
27276 --processing_template_decl;
27277 return b;
27278 }
27279
27280 /* Returns TRUE if ARGS contains a type-dependent expression. */
27281
27282 bool
27283 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27284 {
27285 unsigned int i;
27286 tree arg;
27287
27288 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27289 {
27290 if (type_dependent_expression_p (arg))
27291 return true;
27292 }
27293 return false;
27294 }
27295
27296 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27297 expressions) contains any type-dependent expressions. */
27298
27299 bool
27300 any_type_dependent_elements_p (const_tree list)
27301 {
27302 for (; list; list = TREE_CHAIN (list))
27303 if (type_dependent_expression_p (TREE_VALUE (list)))
27304 return true;
27305
27306 return false;
27307 }
27308
27309 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27310 expressions) contains any value-dependent expressions. */
27311
27312 bool
27313 any_value_dependent_elements_p (const_tree list)
27314 {
27315 for (; list; list = TREE_CHAIN (list))
27316 if (value_dependent_expression_p (TREE_VALUE (list)))
27317 return true;
27318
27319 return false;
27320 }
27321
27322 /* Returns TRUE if the ARG (a template argument) is dependent. */
27323
27324 bool
27325 dependent_template_arg_p (tree arg)
27326 {
27327 if (!processing_template_decl)
27328 return false;
27329
27330 /* Assume a template argument that was wrongly written by the user
27331 is dependent. This is consistent with what
27332 any_dependent_template_arguments_p [that calls this function]
27333 does. */
27334 if (!arg || arg == error_mark_node)
27335 return true;
27336
27337 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27338 arg = argument_pack_select_arg (arg);
27339
27340 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27341 return true;
27342 if (TREE_CODE (arg) == TEMPLATE_DECL)
27343 {
27344 if (DECL_TEMPLATE_PARM_P (arg))
27345 return true;
27346 /* A member template of a dependent class is not necessarily
27347 type-dependent, but it is a dependent template argument because it
27348 will be a member of an unknown specialization to that template. */
27349 tree scope = CP_DECL_CONTEXT (arg);
27350 return TYPE_P (scope) && dependent_type_p (scope);
27351 }
27352 else if (ARGUMENT_PACK_P (arg))
27353 {
27354 tree args = ARGUMENT_PACK_ARGS (arg);
27355 int i, len = TREE_VEC_LENGTH (args);
27356 for (i = 0; i < len; ++i)
27357 {
27358 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27359 return true;
27360 }
27361
27362 return false;
27363 }
27364 else if (TYPE_P (arg))
27365 return dependent_type_p (arg);
27366 else
27367 return value_dependent_expression_p (arg);
27368 }
27369
27370 /* Returns true if ARGS (a collection of template arguments) contains
27371 any types that require structural equality testing. */
27372
27373 bool
27374 any_template_arguments_need_structural_equality_p (tree args)
27375 {
27376 int i;
27377 int j;
27378
27379 if (!args)
27380 return false;
27381 if (args == error_mark_node)
27382 return true;
27383
27384 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27385 {
27386 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27387 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27388 {
27389 tree arg = TREE_VEC_ELT (level, j);
27390 tree packed_args = NULL_TREE;
27391 int k, len = 1;
27392
27393 if (ARGUMENT_PACK_P (arg))
27394 {
27395 /* Look inside the argument pack. */
27396 packed_args = ARGUMENT_PACK_ARGS (arg);
27397 len = TREE_VEC_LENGTH (packed_args);
27398 }
27399
27400 for (k = 0; k < len; ++k)
27401 {
27402 if (packed_args)
27403 arg = TREE_VEC_ELT (packed_args, k);
27404
27405 if (error_operand_p (arg))
27406 return true;
27407 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27408 continue;
27409 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27410 return true;
27411 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27412 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27413 return true;
27414 }
27415 }
27416 }
27417
27418 return false;
27419 }
27420
27421 /* Returns true if ARGS (a collection of template arguments) contains
27422 any dependent arguments. */
27423
27424 bool
27425 any_dependent_template_arguments_p (const_tree args)
27426 {
27427 int i;
27428 int j;
27429
27430 if (!args)
27431 return false;
27432 if (args == error_mark_node)
27433 return true;
27434
27435 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27436 {
27437 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27438 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27439 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27440 return true;
27441 }
27442
27443 return false;
27444 }
27445
27446 /* Returns true if ARGS contains any errors. */
27447
27448 bool
27449 any_erroneous_template_args_p (const_tree args)
27450 {
27451 int i;
27452 int j;
27453
27454 if (args == error_mark_node)
27455 return true;
27456
27457 if (args && TREE_CODE (args) != TREE_VEC)
27458 {
27459 if (tree ti = get_template_info (args))
27460 args = TI_ARGS (ti);
27461 else
27462 args = NULL_TREE;
27463 }
27464
27465 if (!args)
27466 return false;
27467
27468 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27469 {
27470 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27471 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27472 if (error_operand_p (TREE_VEC_ELT (level, j)))
27473 return true;
27474 }
27475
27476 return false;
27477 }
27478
27479 /* Returns TRUE if the template TMPL is type-dependent. */
27480
27481 bool
27482 dependent_template_p (tree tmpl)
27483 {
27484 if (TREE_CODE (tmpl) == OVERLOAD)
27485 {
27486 for (lkp_iterator iter (tmpl); iter; ++iter)
27487 if (dependent_template_p (*iter))
27488 return true;
27489 return false;
27490 }
27491
27492 /* Template template parameters are dependent. */
27493 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27494 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27495 return true;
27496 /* So are names that have not been looked up. */
27497 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27498 return true;
27499 return false;
27500 }
27501
27502 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27503
27504 bool
27505 dependent_template_id_p (tree tmpl, tree args)
27506 {
27507 return (dependent_template_p (tmpl)
27508 || any_dependent_template_arguments_p (args));
27509 }
27510
27511 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27512 are dependent. */
27513
27514 bool
27515 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27516 {
27517 int i;
27518
27519 if (!processing_template_decl)
27520 return false;
27521
27522 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27523 {
27524 tree decl = TREE_VEC_ELT (declv, i);
27525 tree init = TREE_VEC_ELT (initv, i);
27526 tree cond = TREE_VEC_ELT (condv, i);
27527 tree incr = TREE_VEC_ELT (incrv, i);
27528
27529 if (type_dependent_expression_p (decl)
27530 || TREE_CODE (decl) == SCOPE_REF)
27531 return true;
27532
27533 if (init && type_dependent_expression_p (init))
27534 return true;
27535
27536 if (cond == global_namespace)
27537 return true;
27538
27539 if (type_dependent_expression_p (cond))
27540 return true;
27541
27542 if (COMPARISON_CLASS_P (cond)
27543 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27544 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27545 return true;
27546
27547 if (TREE_CODE (incr) == MODOP_EXPR)
27548 {
27549 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27550 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27551 return true;
27552 }
27553 else if (type_dependent_expression_p (incr))
27554 return true;
27555 else if (TREE_CODE (incr) == MODIFY_EXPR)
27556 {
27557 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27558 return true;
27559 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27560 {
27561 tree t = TREE_OPERAND (incr, 1);
27562 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27563 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27564 return true;
27565
27566 /* If this loop has a class iterator with != comparison
27567 with increment other than i++/++i/i--/--i, make sure the
27568 increment is constant. */
27569 if (CLASS_TYPE_P (TREE_TYPE (decl))
27570 && TREE_CODE (cond) == NE_EXPR)
27571 {
27572 if (TREE_OPERAND (t, 0) == decl)
27573 t = TREE_OPERAND (t, 1);
27574 else
27575 t = TREE_OPERAND (t, 0);
27576 if (TREE_CODE (t) != INTEGER_CST)
27577 return true;
27578 }
27579 }
27580 }
27581 }
27582
27583 return false;
27584 }
27585
27586 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27587 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27588 no such TYPE can be found. Note that this function peers inside
27589 uninstantiated templates and therefore should be used only in
27590 extremely limited situations. ONLY_CURRENT_P restricts this
27591 peering to the currently open classes hierarchy (which is required
27592 when comparing types). */
27593
27594 tree
27595 resolve_typename_type (tree type, bool only_current_p)
27596 {
27597 tree scope;
27598 tree name;
27599 tree decl;
27600 int quals;
27601 tree pushed_scope;
27602 tree result;
27603
27604 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27605
27606 scope = TYPE_CONTEXT (type);
27607 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27608 gcc_checking_assert (uses_template_parms (scope));
27609
27610 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27611 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27612 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27613 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27614 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27615 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27616 the TYPENAME_TYPE instead, we avoid messing up with a possible
27617 typedef variant case. */
27618 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27619
27620 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27621 it first before we can figure out what NAME refers to. */
27622 if (TREE_CODE (scope) == TYPENAME_TYPE)
27623 {
27624 if (TYPENAME_IS_RESOLVING_P (scope))
27625 /* Given a class template A with a dependent base with nested type C,
27626 typedef typename A::C::C C will land us here, as trying to resolve
27627 the initial A::C leads to the local C typedef, which leads back to
27628 A::C::C. So we break the recursion now. */
27629 return type;
27630 else
27631 scope = resolve_typename_type (scope, only_current_p);
27632 }
27633 /* If we don't know what SCOPE refers to, then we cannot resolve the
27634 TYPENAME_TYPE. */
27635 if (!CLASS_TYPE_P (scope))
27636 return type;
27637 /* If this is a typedef, we don't want to look inside (c++/11987). */
27638 if (typedef_variant_p (type))
27639 return type;
27640 /* If SCOPE isn't the template itself, it will not have a valid
27641 TYPE_FIELDS list. */
27642 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27643 /* scope is either the template itself or a compatible instantiation
27644 like X<T>, so look up the name in the original template. */
27645 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27646 /* If scope has no fields, it can't be a current instantiation. Check this
27647 before currently_open_class to avoid infinite recursion (71515). */
27648 if (!TYPE_FIELDS (scope))
27649 return type;
27650 /* If the SCOPE is not the current instantiation, there's no reason
27651 to look inside it. */
27652 if (only_current_p && !currently_open_class (scope))
27653 return type;
27654 /* Enter the SCOPE so that name lookup will be resolved as if we
27655 were in the class definition. In particular, SCOPE will no
27656 longer be considered a dependent type. */
27657 pushed_scope = push_scope (scope);
27658 /* Look up the declaration. */
27659 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27660 tf_warning_or_error);
27661
27662 result = NULL_TREE;
27663
27664 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27665 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27666 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27667 if (!decl)
27668 /*nop*/;
27669 else if (identifier_p (fullname)
27670 && TREE_CODE (decl) == TYPE_DECL)
27671 {
27672 result = TREE_TYPE (decl);
27673 if (result == error_mark_node)
27674 result = NULL_TREE;
27675 }
27676 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27677 && DECL_CLASS_TEMPLATE_P (decl))
27678 {
27679 /* Obtain the template and the arguments. */
27680 tree tmpl = TREE_OPERAND (fullname, 0);
27681 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27682 {
27683 /* We get here with a plain identifier because a previous tentative
27684 parse of the nested-name-specifier as part of a ptr-operator saw
27685 ::template X<A>. The use of ::template is necessary in a
27686 ptr-operator, but wrong in a declarator-id.
27687
27688 [temp.names]: In a qualified-id of a declarator-id, the keyword
27689 template shall not appear at the top level. */
27690 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27691 "keyword %<template%> not allowed in declarator-id");
27692 tmpl = decl;
27693 }
27694 tree args = TREE_OPERAND (fullname, 1);
27695 /* Instantiate the template. */
27696 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27697 /*entering_scope=*/true,
27698 tf_error | tf_user);
27699 if (result == error_mark_node)
27700 result = NULL_TREE;
27701 }
27702
27703 /* Leave the SCOPE. */
27704 if (pushed_scope)
27705 pop_scope (pushed_scope);
27706
27707 /* If we failed to resolve it, return the original typename. */
27708 if (!result)
27709 return type;
27710
27711 /* If lookup found a typename type, resolve that too. */
27712 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27713 {
27714 /* Ill-formed programs can cause infinite recursion here, so we
27715 must catch that. */
27716 TYPENAME_IS_RESOLVING_P (result) = 1;
27717 result = resolve_typename_type (result, only_current_p);
27718 TYPENAME_IS_RESOLVING_P (result) = 0;
27719 }
27720
27721 /* Qualify the resulting type. */
27722 quals = cp_type_quals (type);
27723 if (quals)
27724 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27725
27726 return result;
27727 }
27728
27729 /* EXPR is an expression which is not type-dependent. Return a proxy
27730 for EXPR that can be used to compute the types of larger
27731 expressions containing EXPR. */
27732
27733 tree
27734 build_non_dependent_expr (tree expr)
27735 {
27736 tree orig_expr = expr;
27737 tree inner_expr;
27738
27739 /* When checking, try to get a constant value for all non-dependent
27740 expressions in order to expose bugs in *_dependent_expression_p
27741 and constexpr. This can affect code generation, see PR70704, so
27742 only do this for -fchecking=2. */
27743 if (flag_checking > 1
27744 && cxx_dialect >= cxx11
27745 /* Don't do this during nsdmi parsing as it can lead to
27746 unexpected recursive instantiations. */
27747 && !parsing_nsdmi ()
27748 /* Don't do this during concept processing either and for
27749 the same reason. */
27750 && !processing_constraint_expression_p ())
27751 fold_non_dependent_expr (expr, tf_none);
27752
27753 STRIP_ANY_LOCATION_WRAPPER (expr);
27754
27755 /* Preserve OVERLOADs; the functions must be available to resolve
27756 types. */
27757 inner_expr = expr;
27758 if (TREE_CODE (inner_expr) == STMT_EXPR)
27759 inner_expr = stmt_expr_value_expr (inner_expr);
27760 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27761 inner_expr = TREE_OPERAND (inner_expr, 0);
27762 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27763 inner_expr = TREE_OPERAND (inner_expr, 1);
27764 if (is_overloaded_fn (inner_expr)
27765 || TREE_CODE (inner_expr) == OFFSET_REF)
27766 return orig_expr;
27767 /* There is no need to return a proxy for a variable or enumerator. */
27768 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27769 return orig_expr;
27770 /* Preserve string constants; conversions from string constants to
27771 "char *" are allowed, even though normally a "const char *"
27772 cannot be used to initialize a "char *". */
27773 if (TREE_CODE (expr) == STRING_CST)
27774 return orig_expr;
27775 /* Preserve void and arithmetic constants, as an optimization -- there is no
27776 reason to create a new node. */
27777 if (TREE_CODE (expr) == VOID_CST
27778 || TREE_CODE (expr) == INTEGER_CST
27779 || TREE_CODE (expr) == REAL_CST)
27780 return orig_expr;
27781 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27782 There is at least one place where we want to know that a
27783 particular expression is a throw-expression: when checking a ?:
27784 expression, there are special rules if the second or third
27785 argument is a throw-expression. */
27786 if (TREE_CODE (expr) == THROW_EXPR)
27787 return orig_expr;
27788
27789 /* Don't wrap an initializer list, we need to be able to look inside. */
27790 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27791 return orig_expr;
27792
27793 /* Don't wrap a dummy object, we need to be able to test for it. */
27794 if (is_dummy_object (expr))
27795 return orig_expr;
27796
27797 if (TREE_CODE (expr) == COND_EXPR)
27798 return build3 (COND_EXPR,
27799 TREE_TYPE (expr),
27800 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27801 (TREE_OPERAND (expr, 1)
27802 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27803 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27804 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27805 if (TREE_CODE (expr) == COMPOUND_EXPR
27806 && !COMPOUND_EXPR_OVERLOADED (expr))
27807 return build2 (COMPOUND_EXPR,
27808 TREE_TYPE (expr),
27809 TREE_OPERAND (expr, 0),
27810 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27811
27812 /* If the type is unknown, it can't really be non-dependent */
27813 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27814
27815 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27816 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27817 TREE_TYPE (expr), expr);
27818 }
27819
27820 /* ARGS is a vector of expressions as arguments to a function call.
27821 Replace the arguments with equivalent non-dependent expressions.
27822 This modifies ARGS in place. */
27823
27824 void
27825 make_args_non_dependent (vec<tree, va_gc> *args)
27826 {
27827 unsigned int ix;
27828 tree arg;
27829
27830 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27831 {
27832 tree newarg = build_non_dependent_expr (arg);
27833 if (newarg != arg)
27834 (*args)[ix] = newarg;
27835 }
27836 }
27837
27838 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27839 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27840 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27841
27842 static tree
27843 make_auto_1 (tree name, bool set_canonical)
27844 {
27845 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27846 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27847 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27848 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27849 (0, processing_template_decl + 1, processing_template_decl + 1,
27850 TYPE_NAME (au), NULL_TREE);
27851 if (set_canonical)
27852 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27853 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27854 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27855 if (name == decltype_auto_identifier)
27856 AUTO_IS_DECLTYPE (au) = true;
27857
27858 return au;
27859 }
27860
27861 tree
27862 make_decltype_auto (void)
27863 {
27864 return make_auto_1 (decltype_auto_identifier, true);
27865 }
27866
27867 tree
27868 make_auto (void)
27869 {
27870 return make_auto_1 (auto_identifier, true);
27871 }
27872
27873 /* Return a C++17 deduction placeholder for class template TMPL. */
27874
27875 tree
27876 make_template_placeholder (tree tmpl)
27877 {
27878 tree t = make_auto_1 (auto_identifier, false);
27879 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27880 /* Our canonical type depends on the placeholder. */
27881 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27882 return t;
27883 }
27884
27885 /* True iff T is a C++17 class template deduction placeholder. */
27886
27887 bool
27888 template_placeholder_p (tree t)
27889 {
27890 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27891 }
27892
27893 /* Make a "constrained auto" type-specifier. This is an auto or
27894 decltype(auto) type with constraints that must be associated after
27895 deduction. The constraint is formed from the given concept CON
27896 and its optional sequence of template arguments ARGS.
27897
27898 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27899
27900 static tree
27901 make_constrained_placeholder_type (tree type, tree con, tree args)
27902 {
27903 /* Build the constraint. */
27904 tree tmpl = DECL_TI_TEMPLATE (con);
27905 tree expr = tmpl;
27906 if (TREE_CODE (con) == FUNCTION_DECL)
27907 expr = ovl_make (tmpl);
27908 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27909
27910 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27911
27912 /* Our canonical type depends on the constraint. */
27913 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27914
27915 /* Attach the constraint to the type declaration. */
27916 return TYPE_NAME (type);
27917 }
27918
27919 /* Make a "constrained auto" type-specifier. */
27920
27921 tree
27922 make_constrained_auto (tree con, tree args)
27923 {
27924 tree type = make_auto_1 (auto_identifier, false);
27925 return make_constrained_placeholder_type (type, con, args);
27926 }
27927
27928 /* Make a "constrained decltype(auto)" type-specifier. */
27929
27930 tree
27931 make_constrained_decltype_auto (tree con, tree args)
27932 {
27933 tree type = make_auto_1 (decltype_auto_identifier, false);
27934 return make_constrained_placeholder_type (type, con, args);
27935 }
27936
27937 /* Build and return a concept definition. Like other templates, the
27938 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27939 the TEMPLATE_DECL. */
27940
27941 tree
27942 finish_concept_definition (cp_expr id, tree init)
27943 {
27944 gcc_assert (identifier_p (id));
27945 gcc_assert (processing_template_decl);
27946
27947 location_t loc = id.get_location();
27948
27949 /* A concept-definition shall not have associated constraints. */
27950 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27951 {
27952 error_at (loc, "a concept cannot be constrained");
27953 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27954 }
27955
27956 /* A concept-definition shall appear in namespace scope. Templates
27957 aren't allowed in block scope, so we only need to check for class
27958 scope. */
27959 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27960 {
27961 error_at (loc, "concept %qE not in namespace scope", *id);
27962 return error_mark_node;
27963 }
27964
27965 /* Initially build the concept declaration; its type is bool. */
27966 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27967 DECL_CONTEXT (decl) = current_scope ();
27968 DECL_INITIAL (decl) = init;
27969
27970 /* Push the enclosing template. */
27971 return push_template_decl (decl);
27972 }
27973
27974 /* Given type ARG, return std::initializer_list<ARG>. */
27975
27976 static tree
27977 listify (tree arg)
27978 {
27979 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27980
27981 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27982 {
27983 gcc_rich_location richloc (input_location);
27984 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27985 error_at (&richloc,
27986 "deducing from brace-enclosed initializer list"
27987 " requires %<#include <initializer_list>%>");
27988
27989 return error_mark_node;
27990 }
27991 tree argvec = make_tree_vec (1);
27992 TREE_VEC_ELT (argvec, 0) = arg;
27993
27994 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27995 NULL_TREE, 0, tf_warning_or_error);
27996 }
27997
27998 /* Replace auto in TYPE with std::initializer_list<auto>. */
27999
28000 static tree
28001 listify_autos (tree type, tree auto_node)
28002 {
28003 tree init_auto = listify (strip_top_quals (auto_node));
28004 tree argvec = make_tree_vec (1);
28005 TREE_VEC_ELT (argvec, 0) = init_auto;
28006 if (processing_template_decl)
28007 argvec = add_to_template_args (current_template_args (), argvec);
28008 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
28009 }
28010
28011 /* Hash traits for hashing possibly constrained 'auto'
28012 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28013
28014 struct auto_hash : default_hash_traits<tree>
28015 {
28016 static inline hashval_t hash (tree);
28017 static inline bool equal (tree, tree);
28018 };
28019
28020 /* Hash the 'auto' T. */
28021
28022 inline hashval_t
28023 auto_hash::hash (tree t)
28024 {
28025 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28026 /* Matching constrained-type-specifiers denote the same template
28027 parameter, so hash the constraint. */
28028 return hash_placeholder_constraint (c);
28029 else
28030 /* But unconstrained autos are all separate, so just hash the pointer. */
28031 return iterative_hash_object (t, 0);
28032 }
28033
28034 /* Compare two 'auto's. */
28035
28036 inline bool
28037 auto_hash::equal (tree t1, tree t2)
28038 {
28039 if (t1 == t2)
28040 return true;
28041
28042 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28043 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28044
28045 /* Two unconstrained autos are distinct. */
28046 if (!c1 || !c2)
28047 return false;
28048
28049 return equivalent_placeholder_constraints (c1, c2);
28050 }
28051
28052 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28053 constrained) auto, add it to the vector. */
28054
28055 static int
28056 extract_autos_r (tree t, void *data)
28057 {
28058 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28059 if (is_auto (t))
28060 {
28061 /* All the autos were built with index 0; fix that up now. */
28062 tree *p = hash.find_slot (t, INSERT);
28063 unsigned idx;
28064 if (*p)
28065 /* If this is a repeated constrained-type-specifier, use the index we
28066 chose before. */
28067 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28068 else
28069 {
28070 /* Otherwise this is new, so use the current count. */
28071 *p = t;
28072 idx = hash.elements () - 1;
28073 }
28074 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28075 }
28076
28077 /* Always keep walking. */
28078 return 0;
28079 }
28080
28081 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28082 says they can appear anywhere in the type. */
28083
28084 static tree
28085 extract_autos (tree type)
28086 {
28087 hash_set<tree> visited;
28088 hash_table<auto_hash> hash (2);
28089
28090 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28091
28092 tree tree_vec = make_tree_vec (hash.elements());
28093 for (hash_table<auto_hash>::iterator iter = hash.begin();
28094 iter != hash.end(); ++iter)
28095 {
28096 tree elt = *iter;
28097 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28098 TREE_VEC_ELT (tree_vec, i)
28099 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28100 }
28101
28102 return tree_vec;
28103 }
28104
28105 /* The stem for deduction guide names. */
28106 const char *const dguide_base = "__dguide_";
28107
28108 /* Return the name for a deduction guide for class template TMPL. */
28109
28110 tree
28111 dguide_name (tree tmpl)
28112 {
28113 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28114 tree tname = TYPE_IDENTIFIER (type);
28115 char *buf = (char *) alloca (1 + strlen (dguide_base)
28116 + IDENTIFIER_LENGTH (tname));
28117 memcpy (buf, dguide_base, strlen (dguide_base));
28118 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28119 IDENTIFIER_LENGTH (tname) + 1);
28120 tree dname = get_identifier (buf);
28121 TREE_TYPE (dname) = type;
28122 return dname;
28123 }
28124
28125 /* True if NAME is the name of a deduction guide. */
28126
28127 bool
28128 dguide_name_p (tree name)
28129 {
28130 return (TREE_CODE (name) == IDENTIFIER_NODE
28131 && TREE_TYPE (name)
28132 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
28133 strlen (dguide_base)));
28134 }
28135
28136 /* True if FN is a deduction guide. */
28137
28138 bool
28139 deduction_guide_p (const_tree fn)
28140 {
28141 if (DECL_P (fn))
28142 if (tree name = DECL_NAME (fn))
28143 return dguide_name_p (name);
28144 return false;
28145 }
28146
28147 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28148
28149 bool
28150 copy_guide_p (const_tree fn)
28151 {
28152 gcc_assert (deduction_guide_p (fn));
28153 if (!DECL_ARTIFICIAL (fn))
28154 return false;
28155 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28156 return (TREE_CHAIN (parms) == void_list_node
28157 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28158 }
28159
28160 /* True if FN is a guide generated from a constructor template. */
28161
28162 bool
28163 template_guide_p (const_tree fn)
28164 {
28165 gcc_assert (deduction_guide_p (fn));
28166 if (!DECL_ARTIFICIAL (fn))
28167 return false;
28168 tree tmpl = DECL_TI_TEMPLATE (fn);
28169 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28170 return PRIMARY_TEMPLATE_P (org);
28171 return false;
28172 }
28173
28174 /* True if FN is an aggregate initialization guide or the copy deduction
28175 guide. */
28176
28177 bool
28178 builtin_guide_p (const_tree fn)
28179 {
28180 if (!deduction_guide_p (fn))
28181 return false;
28182 if (!DECL_ARTIFICIAL (fn))
28183 /* Explicitly declared. */
28184 return false;
28185 if (DECL_ABSTRACT_ORIGIN (fn))
28186 /* Derived from a constructor. */
28187 return false;
28188 return true;
28189 }
28190
28191 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28192 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28193 template parameter types. Note that the handling of template template
28194 parameters relies on current_template_parms being set appropriately for the
28195 new template. */
28196
28197 static tree
28198 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28199 tree tsubst_args, tsubst_flags_t complain)
28200 {
28201 if (olddecl == error_mark_node)
28202 return error_mark_node;
28203
28204 tree oldidx = get_template_parm_index (olddecl);
28205
28206 tree newtype;
28207 if (TREE_CODE (olddecl) == TYPE_DECL
28208 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28209 {
28210 tree oldtype = TREE_TYPE (olddecl);
28211 newtype = cxx_make_type (TREE_CODE (oldtype));
28212 TYPE_MAIN_VARIANT (newtype) = newtype;
28213 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28214 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28215 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28216 }
28217 else
28218 {
28219 newtype = TREE_TYPE (olddecl);
28220 if (type_uses_auto (newtype))
28221 {
28222 // Substitute once to fix references to other template parameters.
28223 newtype = tsubst (newtype, tsubst_args,
28224 complain|tf_partial, NULL_TREE);
28225 // Now substitute again to reduce the level of the auto.
28226 newtype = tsubst (newtype, current_template_args (),
28227 complain, NULL_TREE);
28228 }
28229 else
28230 newtype = tsubst (newtype, tsubst_args,
28231 complain, NULL_TREE);
28232 }
28233
28234 tree newdecl
28235 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28236 DECL_NAME (olddecl), newtype);
28237 SET_DECL_TEMPLATE_PARM_P (newdecl);
28238
28239 tree newidx;
28240 if (TREE_CODE (olddecl) == TYPE_DECL
28241 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28242 {
28243 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28244 = build_template_parm_index (index, level, level,
28245 newdecl, newtype);
28246 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28247 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28248 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28249 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28250 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28251 else
28252 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28253
28254 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28255 {
28256 DECL_TEMPLATE_RESULT (newdecl)
28257 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28258 DECL_NAME (olddecl), newtype);
28259 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28260 // First create a copy (ttargs) of tsubst_args with an
28261 // additional level for the template template parameter's own
28262 // template parameters (ttparms).
28263 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28264 (DECL_TEMPLATE_PARMS (olddecl)));
28265 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28266 tree ttargs = make_tree_vec (depth + 1);
28267 for (int i = 0; i < depth; ++i)
28268 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28269 TREE_VEC_ELT (ttargs, depth)
28270 = template_parms_level_to_args (ttparms);
28271 // Substitute ttargs into ttparms to fix references to
28272 // other template parameters.
28273 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28274 complain|tf_partial);
28275 // Now substitute again with args based on tparms, to reduce
28276 // the level of the ttparms.
28277 ttargs = current_template_args ();
28278 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28279 complain);
28280 // Finally, tack the adjusted parms onto tparms.
28281 ttparms = tree_cons (size_int (depth), ttparms,
28282 current_template_parms);
28283 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28284 }
28285 }
28286 else
28287 {
28288 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28289 tree newconst
28290 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28291 TREE_CODE (oldconst),
28292 DECL_NAME (oldconst), newtype);
28293 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28294 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28295 SET_DECL_TEMPLATE_PARM_P (newconst);
28296 newidx = build_template_parm_index (index, level, level,
28297 newconst, newtype);
28298 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28299 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28300 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28301 }
28302
28303 return newdecl;
28304 }
28305
28306 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28307 template parameter. */
28308
28309 static tree
28310 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28311 tree targs, unsigned targs_index, tsubst_flags_t complain)
28312 {
28313 tree olddecl = TREE_VALUE (oldelt);
28314 tree newdecl = rewrite_template_parm (olddecl, index, level,
28315 targs, complain);
28316 if (newdecl == error_mark_node)
28317 return error_mark_node;
28318 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28319 targs, complain, NULL_TREE);
28320 tree list = build_tree_list (newdef, newdecl);
28321 TEMPLATE_PARM_CONSTRAINTS (list)
28322 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28323 targs, complain, NULL_TREE);
28324 int depth = TMPL_ARGS_DEPTH (targs);
28325 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28326 return list;
28327 }
28328
28329 /* Returns a C++17 class deduction guide template based on the constructor
28330 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28331 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28332 aggregate initialization guide. */
28333
28334 static tree
28335 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28336 {
28337 tree tparms, targs, fparms, fargs, ci;
28338 bool memtmpl = false;
28339 bool explicit_p;
28340 location_t loc;
28341 tree fn_tmpl = NULL_TREE;
28342
28343 if (outer_args)
28344 {
28345 ++processing_template_decl;
28346 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28347 --processing_template_decl;
28348 }
28349
28350 if (!DECL_DECLARES_FUNCTION_P (ctor))
28351 {
28352 if (TYPE_P (ctor))
28353 {
28354 bool copy_p = TYPE_REF_P (ctor);
28355 if (copy_p)
28356 fparms = tree_cons (NULL_TREE, type, void_list_node);
28357 else
28358 fparms = void_list_node;
28359 }
28360 else if (TREE_CODE (ctor) == TREE_LIST)
28361 fparms = ctor;
28362 else
28363 gcc_unreachable ();
28364
28365 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28366 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28367 targs = CLASSTYPE_TI_ARGS (type);
28368 ci = NULL_TREE;
28369 fargs = NULL_TREE;
28370 loc = DECL_SOURCE_LOCATION (ctmpl);
28371 explicit_p = false;
28372 }
28373 else
28374 {
28375 ++processing_template_decl;
28376 bool ok = true;
28377
28378 fn_tmpl
28379 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28380 : DECL_TI_TEMPLATE (ctor));
28381 if (outer_args)
28382 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28383 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28384
28385 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28386 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28387 fully specialized args for the enclosing class. Strip those off, as
28388 the deduction guide won't have those template parameters. */
28389 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28390 TMPL_PARMS_DEPTH (tparms));
28391 /* Discard the 'this' parameter. */
28392 fparms = FUNCTION_ARG_CHAIN (ctor);
28393 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28394 ci = get_constraints (ctor);
28395 loc = DECL_SOURCE_LOCATION (ctor);
28396 explicit_p = DECL_NONCONVERTING_P (ctor);
28397
28398 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28399 {
28400 memtmpl = true;
28401
28402 /* For a member template constructor, we need to flatten the two
28403 template parameter lists into one, and then adjust the function
28404 signature accordingly. This gets...complicated. */
28405 tree save_parms = current_template_parms;
28406
28407 /* For a member template we should have two levels of parms/args, one
28408 for the class and one for the constructor. We stripped
28409 specialized args for further enclosing classes above. */
28410 const int depth = 2;
28411 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28412
28413 /* Template args for translating references to the two-level template
28414 parameters into references to the one-level template parameters we
28415 are creating. */
28416 tree tsubst_args = copy_node (targs);
28417 TMPL_ARGS_LEVEL (tsubst_args, depth)
28418 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28419
28420 /* Template parms for the constructor template. */
28421 tree ftparms = TREE_VALUE (tparms);
28422 unsigned flen = TREE_VEC_LENGTH (ftparms);
28423 /* Template parms for the class template. */
28424 tparms = TREE_CHAIN (tparms);
28425 tree ctparms = TREE_VALUE (tparms);
28426 unsigned clen = TREE_VEC_LENGTH (ctparms);
28427 /* Template parms for the deduction guide start as a copy of the
28428 template parms for the class. We set current_template_parms for
28429 lookup_template_class_1. */
28430 current_template_parms = tparms = copy_node (tparms);
28431 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28432 for (unsigned i = 0; i < clen; ++i)
28433 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28434
28435 /* Now we need to rewrite the constructor parms to append them to the
28436 class parms. */
28437 for (unsigned i = 0; i < flen; ++i)
28438 {
28439 unsigned index = i + clen;
28440 unsigned level = 1;
28441 tree oldelt = TREE_VEC_ELT (ftparms, i);
28442 tree newelt
28443 = rewrite_tparm_list (oldelt, index, level,
28444 tsubst_args, i, complain);
28445 if (newelt == error_mark_node)
28446 ok = false;
28447 TREE_VEC_ELT (new_vec, index) = newelt;
28448 }
28449
28450 /* Now we have a final set of template parms to substitute into the
28451 function signature. */
28452 targs = template_parms_to_args (tparms);
28453 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28454 complain, ctor);
28455 if (fparms == error_mark_node)
28456 ok = false;
28457 if (ci)
28458 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28459
28460 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28461 cp_unevaluated_operand. */
28462 cp_evaluated ev;
28463 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28464 current_template_parms = save_parms;
28465 }
28466 else
28467 {
28468 /* Substitute in the same arguments to rewrite class members into
28469 references to members of an unknown specialization. */
28470 cp_evaluated ev;
28471 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28472 fargs = tsubst (fargs, targs, complain, ctor);
28473 if (ci)
28474 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28475 }
28476
28477 --processing_template_decl;
28478 if (!ok)
28479 return error_mark_node;
28480 }
28481
28482 if (!memtmpl)
28483 {
28484 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28485 tparms = copy_node (tparms);
28486 INNERMOST_TEMPLATE_PARMS (tparms)
28487 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28488 }
28489
28490 tree fntype = build_function_type (type, fparms);
28491 tree ded_fn = build_lang_decl_loc (loc,
28492 FUNCTION_DECL,
28493 dguide_name (type), fntype);
28494 DECL_ARGUMENTS (ded_fn) = fargs;
28495 DECL_ARTIFICIAL (ded_fn) = true;
28496 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28497 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28498 DECL_ARTIFICIAL (ded_tmpl) = true;
28499 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28500 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28501 if (DECL_P (ctor))
28502 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28503 if (ci)
28504 set_constraints (ded_tmpl, ci);
28505
28506 return ded_tmpl;
28507 }
28508
28509 /* Add to LIST the member types for the reshaped initializer CTOR. */
28510
28511 static tree
28512 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28513 {
28514 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28515 tree idx, val; unsigned i;
28516 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28517 {
28518 tree ftype = elt ? elt : TREE_TYPE (idx);
28519 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28520 && CONSTRUCTOR_NELTS (val)
28521 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28522 type gets a single initializer. */
28523 && CP_AGGREGATE_TYPE_P (ftype)
28524 && !(TREE_CODE (ftype) == ARRAY_TYPE
28525 && uses_template_parms (TYPE_DOMAIN (ftype))))
28526 {
28527 tree subelt = NULL_TREE;
28528 if (TREE_CODE (ftype) == ARRAY_TYPE)
28529 subelt = TREE_TYPE (ftype);
28530 list = collect_ctor_idx_types (val, list, subelt);
28531 continue;
28532 }
28533 tree arg = NULL_TREE;
28534 if (i == v->length() - 1
28535 && PACK_EXPANSION_P (ftype))
28536 /* Give the trailing pack expansion parameter a default argument to
28537 match aggregate initialization behavior, even if we deduce the
28538 length of the pack separately to more than we have initializers. */
28539 arg = build_constructor (init_list_type_node, NULL);
28540 /* if ei is of array type and xi is a braced-init-list or string literal,
28541 Ti is an rvalue reference to the declared type of ei */
28542 STRIP_ANY_LOCATION_WRAPPER (val);
28543 if (TREE_CODE (ftype) == ARRAY_TYPE
28544 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28545 || TREE_CODE (val) == STRING_CST))
28546 {
28547 if (TREE_CODE (val) == STRING_CST)
28548 ftype = cp_build_qualified_type
28549 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28550 ftype = (cp_build_reference_type
28551 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28552 }
28553 list = tree_cons (arg, ftype, list);
28554 }
28555
28556 return list;
28557 }
28558
28559 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28560
28561 static bool
28562 is_spec_or_derived (tree etype, tree tmpl)
28563 {
28564 if (!etype || !CLASS_TYPE_P (etype))
28565 return false;
28566
28567 tree type = TREE_TYPE (tmpl);
28568 tree tparms = (INNERMOST_TEMPLATE_PARMS
28569 (DECL_TEMPLATE_PARMS (tmpl)));
28570 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28571 int err = unify (tparms, targs, type, etype,
28572 UNIFY_ALLOW_DERIVED, /*explain*/false);
28573 ggc_free (targs);
28574 return !err;
28575 }
28576
28577 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28578 INIT. */
28579
28580 static tree
28581 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28582 {
28583 if (cxx_dialect < cxx20)
28584 return NULL_TREE;
28585
28586 if (init == NULL_TREE)
28587 return NULL_TREE;
28588
28589 tree type = TREE_TYPE (tmpl);
28590 if (!CP_AGGREGATE_TYPE_P (type))
28591 return NULL_TREE;
28592
28593 /* No aggregate candidate for copy-initialization. */
28594 if (args->length() == 1)
28595 {
28596 tree val = (*args)[0];
28597 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28598 return NULL_TREE;
28599 }
28600
28601 /* If we encounter a problem, we just won't add the candidate. */
28602 tsubst_flags_t complain = tf_none;
28603
28604 tree parms = NULL_TREE;
28605 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28606 {
28607 init = reshape_init (type, init, complain);
28608 if (init == error_mark_node)
28609 return NULL_TREE;
28610 parms = collect_ctor_idx_types (init, parms);
28611 }
28612 else if (TREE_CODE (init) == TREE_LIST)
28613 {
28614 int len = list_length (init);
28615 for (tree field = TYPE_FIELDS (type);
28616 len;
28617 --len, field = DECL_CHAIN (field))
28618 {
28619 field = next_initializable_field (field);
28620 if (!field)
28621 return NULL_TREE;
28622 tree ftype = finish_decltype_type (field, true, complain);
28623 parms = tree_cons (NULL_TREE, ftype, parms);
28624 }
28625 }
28626 else
28627 /* Aggregate initialization doesn't apply to an initializer expression. */
28628 return NULL_TREE;
28629
28630 if (parms)
28631 {
28632 tree last = parms;
28633 parms = nreverse (parms);
28634 TREE_CHAIN (last) = void_list_node;
28635 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28636 return guide;
28637 }
28638
28639 return NULL_TREE;
28640 }
28641
28642 /* UGUIDES are the deduction guides for the underlying template of alias
28643 template TMPL; adjust them to be deduction guides for TMPL. */
28644
28645 static tree
28646 alias_ctad_tweaks (tree tmpl, tree uguides)
28647 {
28648 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28649 class type (9.2.8.2) where the template-name names an alias template A,
28650 the defining-type-id of A must be of the form
28651
28652 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28653
28654 as specified in 9.2.8.2. The guides of A are the set of functions or
28655 function templates formed as follows. For each function or function
28656 template f in the guides of the template named by the simple-template-id
28657 of the defining-type-id, the template arguments of the return type of f
28658 are deduced from the defining-type-id of A according to the process in
28659 13.10.2.5 with the exception that deduction does not fail if not all
28660 template arguments are deduced. Let g denote the result of substituting
28661 these deductions into f. If substitution succeeds, form a function or
28662 function template f' with the following properties and add it to the set
28663 of guides of A:
28664
28665 * The function type of f' is the function type of g.
28666
28667 * If f is a function template, f' is a function template whose template
28668 parameter list consists of all the template parameters of A (including
28669 their default template arguments) that appear in the above deductions or
28670 (recursively) in their default template arguments, followed by the
28671 template parameters of f that were not deduced (including their default
28672 template arguments), otherwise f' is not a function template.
28673
28674 * The associated constraints (13.5.2) are the conjunction of the
28675 associated constraints of g and a constraint that is satisfied if and only
28676 if the arguments of A are deducible (see below) from the return type.
28677
28678 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28679 be so as well.
28680
28681 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28682 considered to be so as well.
28683
28684 * The explicit-specifier of f' is the explicit-specifier of g (if
28685 any). */
28686
28687 /* This implementation differs from the above in two significant ways:
28688
28689 1) We include all template parameters of A, not just some.
28690 2) The added constraint is same_type instead of deducible.
28691
28692 I believe that while it's probably possible to construct a testcase that
28693 behaves differently with this simplification, it should have the same
28694 effect for real uses. Including all template parameters means that we
28695 deduce all parameters of A when resolving the call, so when we're in the
28696 constraint we don't need to deduce them again, we can just check whether
28697 the deduction produced the desired result. */
28698
28699 tsubst_flags_t complain = tf_warning_or_error;
28700 tree atype = TREE_TYPE (tmpl);
28701 tree aguides = NULL_TREE;
28702 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28703 unsigned natparms = TREE_VEC_LENGTH (atparms);
28704 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28705 for (ovl_iterator iter (uguides); iter; ++iter)
28706 {
28707 tree f = *iter;
28708 tree in_decl = f;
28709 location_t loc = DECL_SOURCE_LOCATION (f);
28710 tree ret = TREE_TYPE (TREE_TYPE (f));
28711 tree fprime = f;
28712 if (TREE_CODE (f) == TEMPLATE_DECL)
28713 {
28714 processing_template_decl_sentinel ptds (/*reset*/false);
28715 ++processing_template_decl;
28716
28717 /* Deduce template arguments for f from the type-id of A. */
28718 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28719 unsigned len = TREE_VEC_LENGTH (ftparms);
28720 tree targs = make_tree_vec (len);
28721 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28722 gcc_assert (!err);
28723
28724 /* The number of parms for f' is the number of parms for A plus
28725 non-deduced parms of f. */
28726 unsigned ndlen = 0;
28727 unsigned j;
28728 for (unsigned i = 0; i < len; ++i)
28729 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28730 ++ndlen;
28731 tree gtparms = make_tree_vec (natparms + ndlen);
28732
28733 /* First copy over the parms of A. */
28734 for (j = 0; j < natparms; ++j)
28735 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28736 /* Now rewrite the non-deduced parms of f. */
28737 for (unsigned i = 0; ndlen && i < len; ++i)
28738 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28739 {
28740 --ndlen;
28741 unsigned index = j++;
28742 unsigned level = 1;
28743 tree oldlist = TREE_VEC_ELT (ftparms, i);
28744 tree list = rewrite_tparm_list (oldlist, index, level,
28745 targs, i, complain);
28746 TREE_VEC_ELT (gtparms, index) = list;
28747 }
28748 gtparms = build_tree_list (size_one_node, gtparms);
28749
28750 /* Substitute the deduced arguments plus the rewritten template
28751 parameters into f to get g. This covers the type, copyness,
28752 guideness, and explicit-specifier. */
28753 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28754 if (g == error_mark_node)
28755 return error_mark_node;
28756 DECL_USE_TEMPLATE (g) = 0;
28757 fprime = build_template_decl (g, gtparms, false);
28758 DECL_TEMPLATE_RESULT (fprime) = g;
28759 TREE_TYPE (fprime) = TREE_TYPE (g);
28760 tree gtargs = template_parms_to_args (gtparms);
28761 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28762 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28763
28764 /* Substitute the associated constraints. */
28765 tree ci = get_constraints (f);
28766 if (ci)
28767 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28768 if (ci == error_mark_node)
28769 return error_mark_node;
28770
28771 /* Add a constraint that the return type matches the instantiation of
28772 A with the same template arguments. */
28773 ret = TREE_TYPE (TREE_TYPE (fprime));
28774 if (!same_type_p (atype, ret)
28775 /* FIXME this should mean they don't compare as equivalent. */
28776 || dependent_alias_template_spec_p (atype, nt_opaque))
28777 {
28778 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28779 ci = append_constraint (ci, same);
28780 }
28781
28782 if (ci)
28783 {
28784 remove_constraints (fprime);
28785 set_constraints (fprime, ci);
28786 }
28787 }
28788 else
28789 {
28790 /* For a non-template deduction guide, if the arguments of A aren't
28791 deducible from the return type, don't add the candidate. */
28792 tree targs = make_tree_vec (natparms);
28793 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28794 for (unsigned i = 0; !err && i < natparms; ++i)
28795 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28796 err = true;
28797 if (err)
28798 continue;
28799 }
28800
28801 aguides = lookup_add (fprime, aguides);
28802 }
28803
28804 return aguides;
28805 }
28806
28807 /* Return artificial deduction guides built from the constructors of class
28808 template TMPL. */
28809
28810 static tree
28811 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28812 {
28813 tree type = TREE_TYPE (tmpl);
28814 tree outer_args = NULL_TREE;
28815 if (DECL_CLASS_SCOPE_P (tmpl)
28816 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28817 {
28818 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28819 type = TREE_TYPE (most_general_template (tmpl));
28820 }
28821
28822 tree cands = NULL_TREE;
28823
28824 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28825 {
28826 /* Skip inherited constructors. */
28827 if (iter.using_p ())
28828 continue;
28829
28830 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28831 cands = lookup_add (guide, cands);
28832 }
28833
28834 /* Add implicit default constructor deduction guide. */
28835 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28836 {
28837 tree guide = build_deduction_guide (type, type, outer_args,
28838 complain);
28839 cands = lookup_add (guide, cands);
28840 }
28841
28842 /* Add copy guide. */
28843 {
28844 tree gtype = build_reference_type (type);
28845 tree guide = build_deduction_guide (type, gtype, outer_args,
28846 complain);
28847 cands = lookup_add (guide, cands);
28848 }
28849
28850 return cands;
28851 }
28852
28853 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28854
28855 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28856 aggregate candidate is added separately because it depends on the
28857 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
28858 guide. */
28859
28860 static tree
28861 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
28862 {
28863 tree guides = NULL_TREE;
28864 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28865 {
28866 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28867 tree tinfo = get_template_info (under);
28868 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
28869 complain);
28870 }
28871 else
28872 {
28873 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28874 dguide_name (tmpl),
28875 LOOK_want::NORMAL, /*complain*/false);
28876 if (guides == error_mark_node)
28877 guides = NULL_TREE;
28878 else
28879 any_dguides_p = true;
28880 }
28881
28882 /* Cache the deduction guides for a template. We also remember the result of
28883 lookup, and rebuild everything if it changes; should be very rare. */
28884 tree_pair_p cache = NULL;
28885 if (tree_pair_p &r
28886 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28887 {
28888 cache = r;
28889 if (cache->purpose == guides)
28890 return cache->value;
28891 }
28892 else
28893 {
28894 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28895 cache->purpose = guides;
28896 }
28897
28898 tree cands = NULL_TREE;
28899 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28900 cands = alias_ctad_tweaks (tmpl, guides);
28901 else
28902 {
28903 cands = ctor_deduction_guides_for (tmpl, complain);
28904 for (ovl_iterator it (guides); it; ++it)
28905 cands = lookup_add (*it, cands);
28906 }
28907
28908 cache->value = cands;
28909 return cands;
28910 }
28911
28912 /* Return whether TMPL is a (class template argument-) deducible template. */
28913
28914 bool
28915 ctad_template_p (tree tmpl)
28916 {
28917 /* A deducible template is either a class template or is an alias template
28918 whose defining-type-id is of the form
28919
28920 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28921
28922 where the nested-name-specifier (if any) is non-dependent and the
28923 template-name of the simple-template-id names a deducible template. */
28924
28925 if (DECL_CLASS_TEMPLATE_P (tmpl)
28926 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28927 return true;
28928 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28929 return false;
28930 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28931 if (tree tinfo = get_template_info (orig))
28932 return ctad_template_p (TI_TEMPLATE (tinfo));
28933 return false;
28934 }
28935
28936 /* Deduce template arguments for the class template placeholder PTYPE for
28937 template TMPL based on the initializer INIT, and return the resulting
28938 type. */
28939
28940 static tree
28941 do_class_deduction (tree ptype, tree tmpl, tree init,
28942 int flags, tsubst_flags_t complain)
28943 {
28944 /* We should have handled this in the caller. */
28945 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28946 return ptype;
28947
28948 /* Look through alias templates that just rename another template. */
28949 tmpl = get_underlying_template (tmpl);
28950 if (!ctad_template_p (tmpl))
28951 {
28952 if (complain & tf_error)
28953 error ("non-deducible template %qT used without template arguments", tmpl);
28954 return error_mark_node;
28955 }
28956 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
28957 {
28958 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28959 if (complain & tf_warning_or_error)
28960 pedwarn (input_location, 0, "alias template deduction only available "
28961 "with %<-std=c++20%> or %<-std=gnu++20%>");
28962 }
28963
28964 if (init && TREE_TYPE (init) == ptype)
28965 /* Using the template parm as its own argument. */
28966 return ptype;
28967
28968 tree type = TREE_TYPE (tmpl);
28969
28970 bool try_list_ctor = false;
28971
28972 releasing_vec rv_args = NULL;
28973 vec<tree,va_gc> *&args = *&rv_args;
28974 if (init == NULL_TREE)
28975 args = make_tree_vector ();
28976 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28977 {
28978 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28979 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28980 {
28981 /* As an exception, the first phase in 16.3.1.7 (considering the
28982 initializer list as a single argument) is omitted if the
28983 initializer list consists of a single expression of type cv U,
28984 where U is a specialization of C or a class derived from a
28985 specialization of C. */
28986 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28987 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28988 try_list_ctor = false;
28989 }
28990 if (try_list_ctor || is_std_init_list (type))
28991 args = make_tree_vector_single (init);
28992 else
28993 args = make_tree_vector_from_ctor (init);
28994 }
28995 else if (TREE_CODE (init) == TREE_LIST)
28996 args = make_tree_vector_from_list (init);
28997 else
28998 args = make_tree_vector_single (init);
28999
29000 /* Do this now to avoid problems with erroneous args later on. */
29001 args = resolve_args (args, complain);
29002 if (args == NULL)
29003 return error_mark_node;
29004
29005 bool any_dguides_p = false;
29006 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
29007 if (cands == error_mark_node)
29008 return error_mark_node;
29009
29010 /* Prune explicit deduction guides in copy-initialization context. */
29011 bool elided = false;
29012 if (flags & LOOKUP_ONLYCONVERTING)
29013 {
29014 for (lkp_iterator iter (cands); !elided && iter; ++iter)
29015 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29016 elided = true;
29017
29018 if (elided)
29019 {
29020 /* Found a nonconverting guide, prune the candidates. */
29021 tree pruned = NULL_TREE;
29022 for (lkp_iterator iter (cands); iter; ++iter)
29023 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29024 pruned = lookup_add (*iter, pruned);
29025
29026 cands = pruned;
29027 }
29028 }
29029
29030 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29031 cands = lookup_add (guide, cands);
29032
29033 tree call = error_mark_node;
29034
29035 /* If this is list-initialization and the class has a list constructor, first
29036 try deducing from the list as a single argument, as [over.match.list]. */
29037 tree list_cands = NULL_TREE;
29038 if (try_list_ctor && cands)
29039 for (lkp_iterator iter (cands); iter; ++iter)
29040 {
29041 tree dg = *iter;
29042 if (is_list_ctor (dg))
29043 list_cands = lookup_add (dg, list_cands);
29044 }
29045 if (list_cands)
29046 {
29047 ++cp_unevaluated_operand;
29048 call = build_new_function_call (list_cands, &args, tf_decltype);
29049 --cp_unevaluated_operand;
29050
29051 if (call == error_mark_node)
29052 {
29053 /* That didn't work, now try treating the list as a sequence of
29054 arguments. */
29055 release_tree_vector (args);
29056 args = make_tree_vector_from_ctor (init);
29057 }
29058 }
29059
29060 if (elided && !cands)
29061 {
29062 error ("cannot deduce template arguments for copy-initialization"
29063 " of %qT, as it has no non-explicit deduction guides or "
29064 "user-declared constructors", type);
29065 return error_mark_node;
29066 }
29067 else if (!cands && call == error_mark_node)
29068 {
29069 error ("cannot deduce template arguments of %qT, as it has no viable "
29070 "deduction guides", type);
29071 return error_mark_node;
29072 }
29073
29074 if (call == error_mark_node)
29075 {
29076 ++cp_unevaluated_operand;
29077 call = build_new_function_call (cands, &args, tf_decltype);
29078 --cp_unevaluated_operand;
29079 }
29080
29081 if (call == error_mark_node
29082 && (complain & tf_warning_or_error))
29083 {
29084 error ("class template argument deduction failed:");
29085
29086 ++cp_unevaluated_operand;
29087 call = build_new_function_call (cands, &args, complain | tf_decltype);
29088 --cp_unevaluated_operand;
29089
29090 if (elided)
29091 inform (input_location, "explicit deduction guides not considered "
29092 "for copy-initialization");
29093 }
29094
29095 /* If CTAD succeeded but the type doesn't have any explicit deduction
29096 guides, this deduction might not be what the user intended. */
29097 if (call != error_mark_node && !any_dguides_p)
29098 {
29099 tree fndecl = cp_get_callee_fndecl_nofold (call);
29100 if (fndecl != NULL_TREE
29101 && (!DECL_IN_SYSTEM_HEADER (fndecl)
29102 || global_dc->dc_warn_system_headers)
29103 && warning (OPT_Wctad_maybe_unsupported,
29104 "%qT may not intend to support class template argument "
29105 "deduction", type))
29106 inform (input_location, "add a deduction guide to suppress this "
29107 "warning");
29108 }
29109
29110 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
29111 }
29112
29113 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29114 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29115 The CONTEXT determines the context in which auto deduction is performed
29116 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29117 OUTER_TARGS are used during template argument deduction
29118 (context == adc_unify) to properly substitute the result, and is ignored
29119 in other contexts.
29120
29121 For partial-concept-ids, extra args may be appended to the list of deduced
29122 template arguments prior to determining constraint satisfaction. */
29123
29124 tree
29125 do_auto_deduction (tree type, tree init, tree auto_node,
29126 tsubst_flags_t complain, auto_deduction_context context,
29127 tree outer_targs, int flags)
29128 {
29129 tree targs;
29130
29131 if (init == error_mark_node)
29132 return error_mark_node;
29133
29134 if (init && type_dependent_expression_p (init)
29135 && context != adc_unify)
29136 /* Defining a subset of type-dependent expressions that we can deduce
29137 from ahead of time isn't worth the trouble. */
29138 return type;
29139
29140 /* Similarly, we can't deduce from another undeduced decl. */
29141 if (init && undeduced_auto_decl (init))
29142 return type;
29143
29144 /* We may be doing a partial substitution, but we still want to replace
29145 auto_node. */
29146 complain &= ~tf_partial;
29147
29148 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29149 /* C++17 class template argument deduction. */
29150 return do_class_deduction (type, tmpl, init, flags, complain);
29151
29152 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29153 /* Nothing we can do with this, even in deduction context. */
29154 return type;
29155
29156 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29157 with either a new invented type template parameter U or, if the
29158 initializer is a braced-init-list (8.5.4), with
29159 std::initializer_list<U>. */
29160 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29161 {
29162 if (!DIRECT_LIST_INIT_P (init))
29163 type = listify_autos (type, auto_node);
29164 else if (CONSTRUCTOR_NELTS (init) == 1)
29165 init = CONSTRUCTOR_ELT (init, 0)->value;
29166 else
29167 {
29168 if (complain & tf_warning_or_error)
29169 {
29170 if (permerror (input_location, "direct-list-initialization of "
29171 "%<auto%> requires exactly one element"))
29172 inform (input_location,
29173 "for deduction to %<std::initializer_list%>, use copy-"
29174 "list-initialization (i.e. add %<=%> before the %<{%>)");
29175 }
29176 type = listify_autos (type, auto_node);
29177 }
29178 }
29179
29180 if (type == error_mark_node)
29181 return error_mark_node;
29182
29183 init = resolve_nondeduced_context (init, complain);
29184
29185 if (context == adc_decomp_type
29186 && auto_node == type
29187 && init != error_mark_node
29188 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29189 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29190 and initializer has array type, deduce cv-qualified array type. */
29191 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
29192 complain);
29193 else if (AUTO_IS_DECLTYPE (auto_node))
29194 {
29195 tree stripped_init = tree_strip_any_location_wrapper (init);
29196 bool id = (DECL_P (stripped_init)
29197 || ((TREE_CODE (init) == COMPONENT_REF
29198 || TREE_CODE (init) == SCOPE_REF)
29199 && !REF_PARENTHESIZED_P (init)));
29200 targs = make_tree_vec (1);
29201 TREE_VEC_ELT (targs, 0)
29202 = finish_decltype_type (init, id, tf_warning_or_error);
29203 if (type != auto_node)
29204 {
29205 if (complain & tf_error)
29206 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29207 return error_mark_node;
29208 }
29209 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29210 {
29211 if (complain & tf_error)
29212 error ("%<decltype(auto)%> cannot be cv-qualified");
29213 return error_mark_node;
29214 }
29215 }
29216 else
29217 {
29218 if (error_operand_p (init))
29219 return error_mark_node;
29220
29221 tree parms = build_tree_list (NULL_TREE, type);
29222 tree tparms;
29223
29224 if (flag_concepts)
29225 tparms = extract_autos (type);
29226 else
29227 {
29228 tparms = make_tree_vec (1);
29229 TREE_VEC_ELT (tparms, 0)
29230 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29231 }
29232
29233 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29234 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29235 DEDUCE_CALL,
29236 NULL, /*explain_p=*/false);
29237 if (val > 0)
29238 {
29239 if (processing_template_decl)
29240 /* Try again at instantiation time. */
29241 return type;
29242 if (type && type != error_mark_node
29243 && (complain & tf_error))
29244 /* If type is error_mark_node a diagnostic must have been
29245 emitted by now. Also, having a mention to '<type error>'
29246 in the diagnostic is not really useful to the user. */
29247 {
29248 if (cfun
29249 && FNDECL_USED_AUTO (current_function_decl)
29250 && (auto_node
29251 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29252 && LAMBDA_FUNCTION_P (current_function_decl))
29253 error ("unable to deduce lambda return type from %qE", init);
29254 else
29255 error ("unable to deduce %qT from %qE", type, init);
29256 type_unification_real (tparms, targs, parms, &init, 1, 0,
29257 DEDUCE_CALL,
29258 NULL, /*explain_p=*/true);
29259 }
29260 return error_mark_node;
29261 }
29262 }
29263
29264 /* Check any placeholder constraints against the deduced type. */
29265 if (flag_concepts && !processing_template_decl)
29266 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29267 {
29268 /* Use the deduced type to check the associated constraints. If we
29269 have a partial-concept-id, rebuild the argument list so that
29270 we check using the extra arguments. */
29271 check = unpack_concept_check (check);
29272 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29273 tree cdecl = TREE_OPERAND (check, 0);
29274 if (OVL_P (cdecl))
29275 cdecl = OVL_FIRST (cdecl);
29276 tree cargs = TREE_OPERAND (check, 1);
29277 if (TREE_VEC_LENGTH (cargs) > 1)
29278 {
29279 cargs = copy_node (cargs);
29280 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29281 }
29282 else
29283 cargs = targs;
29284
29285 /* Rebuild the check using the deduced arguments. */
29286 check = build_concept_check (cdecl, cargs, tf_none);
29287
29288 if (!constraints_satisfied_p (check))
29289 {
29290 if (complain & tf_warning_or_error)
29291 {
29292 auto_diagnostic_group d;
29293 switch (context)
29294 {
29295 case adc_unspecified:
29296 case adc_unify:
29297 error("placeholder constraints not satisfied");
29298 break;
29299 case adc_variable_type:
29300 case adc_decomp_type:
29301 error ("deduced initializer does not satisfy "
29302 "placeholder constraints");
29303 break;
29304 case adc_return_type:
29305 error ("deduced return type does not satisfy "
29306 "placeholder constraints");
29307 break;
29308 case adc_requirement:
29309 error ("deduced expression type does not satisfy "
29310 "placeholder constraints");
29311 break;
29312 }
29313 diagnose_constraints (input_location, check, targs);
29314 }
29315 return error_mark_node;
29316 }
29317 }
29318
29319 if (processing_template_decl && context != adc_unify)
29320 outer_targs = current_template_args ();
29321 targs = add_to_template_args (outer_targs, targs);
29322 return tsubst (type, targs, complain, NULL_TREE);
29323 }
29324
29325 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29326 result. */
29327
29328 tree
29329 splice_late_return_type (tree type, tree late_return_type)
29330 {
29331 if (late_return_type)
29332 {
29333 gcc_assert (is_auto (type) || seen_error ());
29334 return late_return_type;
29335 }
29336
29337 if (tree *auto_node = find_type_usage (&type, is_auto))
29338 {
29339 tree idx = get_template_parm_index (*auto_node);
29340 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29341 {
29342 /* In an abbreviated function template we didn't know we were dealing
29343 with a function template when we saw the auto return type, so update
29344 it to have the correct level. */
29345 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29346 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29347 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29348 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29349 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29350 *auto_node = new_auto;
29351 }
29352 }
29353 return type;
29354 }
29355
29356 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29357 'decltype(auto)' or a deduced class template. */
29358
29359 bool
29360 is_auto (const_tree type)
29361 {
29362 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29363 && (TYPE_IDENTIFIER (type) == auto_identifier
29364 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29365 return true;
29366 else
29367 return false;
29368 }
29369
29370 /* for_each_template_parm callback for type_uses_auto. */
29371
29372 int
29373 is_auto_r (tree tp, void */*data*/)
29374 {
29375 return is_auto (tp);
29376 }
29377
29378 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29379 a use of `auto'. Returns NULL_TREE otherwise. */
29380
29381 tree
29382 type_uses_auto (tree type)
29383 {
29384 if (type == NULL_TREE)
29385 return NULL_TREE;
29386 else if (flag_concepts)
29387 {
29388 /* The Concepts TS allows multiple autos in one type-specifier; just
29389 return the first one we find, do_auto_deduction will collect all of
29390 them. */
29391 if (uses_template_parms (type))
29392 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29393 /*visited*/NULL, /*nondeduced*/false);
29394 else
29395 return NULL_TREE;
29396 }
29397 else if (tree *tp = find_type_usage (&type, is_auto))
29398 return *tp;
29399 else
29400 return NULL_TREE;
29401 }
29402
29403 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29404 concepts are enabled, auto is acceptable in template arguments, but
29405 only when TEMPL identifies a template class. Return TRUE if any
29406 such errors were reported. */
29407
29408 bool
29409 check_auto_in_tmpl_args (tree tmpl, tree args)
29410 {
29411 /* If there were previous errors, nevermind. */
29412 if (!args || TREE_CODE (args) != TREE_VEC)
29413 return false;
29414
29415 /* If TMPL is an identifier, we're parsing and we can't tell yet
29416 whether TMPL is supposed to be a type, a function or a variable.
29417 We'll only be able to tell during template substitution, so we
29418 expect to be called again then. If concepts are enabled and we
29419 know we have a type, we're ok. */
29420 if (flag_concepts
29421 && (identifier_p (tmpl)
29422 || (DECL_P (tmpl)
29423 && (DECL_TYPE_TEMPLATE_P (tmpl)
29424 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29425 return false;
29426
29427 /* Quickly search for any occurrences of auto; usually there won't
29428 be any, and then we'll avoid allocating the vector. */
29429 if (!type_uses_auto (args))
29430 return false;
29431
29432 bool errors = false;
29433
29434 tree vec = extract_autos (args);
29435 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29436 {
29437 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29438 error_at (DECL_SOURCE_LOCATION (xauto),
29439 "invalid use of %qT in template argument", xauto);
29440 errors = true;
29441 }
29442
29443 return errors;
29444 }
29445
29446 /* Recursively walk over && expressions searching for EXPR. Return a reference
29447 to that expression. */
29448
29449 static tree *find_template_requirement (tree *t, tree key)
29450 {
29451 if (*t == key)
29452 return t;
29453 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29454 {
29455 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29456 return p;
29457 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29458 return p;
29459 }
29460 return 0;
29461 }
29462
29463 /* Convert the generic type parameters in PARM that match the types given in the
29464 range [START_IDX, END_IDX) from the current_template_parms into generic type
29465 packs. */
29466
29467 tree
29468 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29469 {
29470 tree current = current_template_parms;
29471 int depth = TMPL_PARMS_DEPTH (current);
29472 current = INNERMOST_TEMPLATE_PARMS (current);
29473 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29474
29475 for (int i = 0; i < start_idx; ++i)
29476 TREE_VEC_ELT (replacement, i)
29477 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29478
29479 for (int i = start_idx; i < end_idx; ++i)
29480 {
29481 /* Create a distinct parameter pack type from the current parm and add it
29482 to the replacement args to tsubst below into the generic function
29483 parameter. */
29484 tree node = TREE_VEC_ELT (current, i);
29485 tree o = TREE_TYPE (TREE_VALUE (node));
29486 tree t = copy_type (o);
29487 TEMPLATE_TYPE_PARM_INDEX (t)
29488 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29489 t, 0, 0, tf_none);
29490 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29491 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29492 TYPE_MAIN_VARIANT (t) = t;
29493 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29494 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29495 TREE_VEC_ELT (replacement, i) = t;
29496
29497 /* Replace the current template parameter with new pack. */
29498 TREE_VALUE (node) = TREE_CHAIN (t);
29499
29500 /* Surgically adjust the associated constraint of adjusted parameter
29501 and it's corresponding contribution to the current template
29502 requirements. */
29503 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29504 {
29505 tree id = unpack_concept_check (constr);
29506 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
29507 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29508 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29509
29510 /* If there was a constraint, we also need to replace that in
29511 the template requirements, which we've already built. */
29512 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29513 reqs = find_template_requirement (reqs, constr);
29514 *reqs = fold;
29515 }
29516 }
29517
29518 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29519 TREE_VEC_ELT (replacement, i)
29520 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29521
29522 /* If there are more levels then build up the replacement with the outer
29523 template parms. */
29524 if (depth > 1)
29525 replacement = add_to_template_args (template_parms_to_args
29526 (TREE_CHAIN (current_template_parms)),
29527 replacement);
29528
29529 return tsubst (parm, replacement, tf_none, NULL_TREE);
29530 }
29531
29532 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29533 0..N-1. */
29534
29535 void
29536 declare_integer_pack (void)
29537 {
29538 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29539 build_function_type_list (integer_type_node,
29540 integer_type_node,
29541 NULL_TREE),
29542 NULL_TREE, ECF_CONST);
29543 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29544 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29545 CP_BUILT_IN_INTEGER_PACK);
29546 }
29547
29548 /* Set up the hash tables for template instantiations. */
29549
29550 void
29551 init_template_processing (void)
29552 {
29553 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29554 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29555
29556 if (cxx_dialect >= cxx11)
29557 declare_integer_pack ();
29558 }
29559
29560 /* Print stats about the template hash tables for -fstats. */
29561
29562 void
29563 print_template_statistics (void)
29564 {
29565 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29566 "%f collisions\n", (long) decl_specializations->size (),
29567 (long) decl_specializations->elements (),
29568 decl_specializations->collisions ());
29569 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29570 "%f collisions\n", (long) type_specializations->size (),
29571 (long) type_specializations->elements (),
29572 type_specializations->collisions ());
29573 }
29574
29575 #if CHECKING_P
29576
29577 namespace selftest {
29578
29579 /* Verify that build_non_dependent_expr () works, for various expressions,
29580 and that location wrappers don't affect the results. */
29581
29582 static void
29583 test_build_non_dependent_expr ()
29584 {
29585 location_t loc = BUILTINS_LOCATION;
29586
29587 /* Verify constants, without and with location wrappers. */
29588 tree int_cst = build_int_cst (integer_type_node, 42);
29589 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29590
29591 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29592 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29593 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29594
29595 tree string_lit = build_string (4, "foo");
29596 TREE_TYPE (string_lit) = char_array_type_node;
29597 string_lit = fix_string_type (string_lit);
29598 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29599
29600 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29601 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29602 ASSERT_EQ (wrapped_string_lit,
29603 build_non_dependent_expr (wrapped_string_lit));
29604 }
29605
29606 /* Verify that type_dependent_expression_p () works correctly, even
29607 in the presence of location wrapper nodes. */
29608
29609 static void
29610 test_type_dependent_expression_p ()
29611 {
29612 location_t loc = BUILTINS_LOCATION;
29613
29614 tree name = get_identifier ("foo");
29615
29616 /* If no templates are involved, nothing is type-dependent. */
29617 gcc_assert (!processing_template_decl);
29618 ASSERT_FALSE (type_dependent_expression_p (name));
29619
29620 ++processing_template_decl;
29621
29622 /* Within a template, an unresolved name is always type-dependent. */
29623 ASSERT_TRUE (type_dependent_expression_p (name));
29624
29625 /* Ensure it copes with NULL_TREE and errors. */
29626 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29627 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29628
29629 /* A USING_DECL in a template should be type-dependent, even if wrapped
29630 with a location wrapper (PR c++/83799). */
29631 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29632 TREE_TYPE (using_decl) = integer_type_node;
29633 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29634 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29635 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29636 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29637
29638 --processing_template_decl;
29639 }
29640
29641 /* Run all of the selftests within this file. */
29642
29643 void
29644 cp_pt_c_tests ()
29645 {
29646 test_build_non_dependent_expr ();
29647 test_type_dependent_expression_p ();
29648 }
29649
29650 } // namespace selftest
29651
29652 #endif /* #if CHECKING_P */
29653
29654 #include "gt-cp-pt.h"