]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
c++: Correct the handling of alignof(expr) [PR88115]
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2020 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45 #include "target.h"
46
47 /* The type of functions taking a tree, and some additional data, and
48 returning an int. */
49 typedef int (*tree_fn_t) (tree, void*);
50
51 /* The PENDING_TEMPLATES is a list of templates whose instantiations
52 have been deferred, either because their definitions were not yet
53 available, or because we were putting off doing the work. */
54 struct GTY ((chain_next ("%h.next"))) pending_template
55 {
56 struct pending_template *next;
57 struct tinst_level *tinst;
58 };
59
60 static GTY(()) struct pending_template *pending_templates;
61 static GTY(()) struct pending_template *last_pending_template;
62
63 int processing_template_parmlist;
64 static int template_header_count;
65
66 static GTY(()) tree saved_trees;
67 static vec<int> inline_parm_levels;
68
69 static GTY(()) struct tinst_level *current_tinst_level;
70
71 static GTY(()) vec<tree, va_gc> *saved_access_scope;
72
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
77
78 // -------------------------------------------------------------------------- //
79 // Local Specialization Stack
80 //
81 // Implementation of the RAII helper for creating new local
82 // specializations.
83 local_specialization_stack::local_specialization_stack (lss_policy policy)
84 : saved (local_specializations)
85 {
86 if (policy == lss_nop)
87 ;
88 else if (policy == lss_blank || !saved)
89 local_specializations = new hash_map<tree, tree>;
90 else
91 local_specializations = new hash_map<tree, tree>(*saved);
92 }
93
94 local_specialization_stack::~local_specialization_stack ()
95 {
96 if (local_specializations != saved)
97 {
98 delete local_specializations;
99 local_specializations = saved;
100 }
101 }
102
103 /* True if we've recursed into fn_type_unification too many times. */
104 static bool excessive_deduction_depth;
105
106 struct GTY((for_user)) spec_entry
107 {
108 tree tmpl;
109 tree args;
110 tree spec;
111 };
112
113 struct spec_hasher : ggc_ptr_hash<spec_entry>
114 {
115 static hashval_t hash (spec_entry *);
116 static bool equal (spec_entry *, spec_entry *);
117 };
118
119 /* The general template is not in these tables. */
120 typedef hash_table<spec_hasher> spec_hash_table;
121 static GTY (()) spec_hash_table *decl_specializations;
122 static GTY (()) spec_hash_table *type_specializations;
123
124 /* Contains canonical template parameter types. The vector is indexed by
125 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
126 TREE_LIST, whose TREE_VALUEs contain the canonical template
127 parameters of various types and levels. */
128 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
129
130 #define UNIFY_ALLOW_NONE 0
131 #define UNIFY_ALLOW_MORE_CV_QUAL 1
132 #define UNIFY_ALLOW_LESS_CV_QUAL 2
133 #define UNIFY_ALLOW_DERIVED 4
134 #define UNIFY_ALLOW_INTEGER 8
135 #define UNIFY_ALLOW_OUTER_LEVEL 16
136 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
137 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
138
139 enum template_base_result {
140 tbr_incomplete_type,
141 tbr_ambiguous_baseclass,
142 tbr_success
143 };
144
145 static bool resolve_overloaded_unification (tree, tree, tree, tree,
146 unification_kind_t, int,
147 bool);
148 static int try_one_overload (tree, tree, tree, tree, tree,
149 unification_kind_t, int, bool, bool);
150 static int unify (tree, tree, tree, tree, int, bool);
151 static void add_pending_template (tree);
152 static tree reopen_tinst_level (struct tinst_level *);
153 static tree tsubst_initializer_list (tree, tree);
154 static tree get_partial_spec_bindings (tree, tree, tree);
155 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
156 bool, bool);
157 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
158 bool, bool);
159 static void tsubst_enum (tree, tree, tree);
160 static tree add_to_template_args (tree, tree);
161 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
162 static int check_non_deducible_conversion (tree, tree, int, int,
163 struct conversion **, bool);
164 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
165 tree);
166 static int type_unification_real (tree, tree, tree, const tree *,
167 unsigned int, int, unification_kind_t,
168 vec<deferred_access_check, va_gc> **,
169 bool);
170 static void note_template_header (int);
171 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
172 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
173 static tree convert_template_argument (tree, tree, tree,
174 tsubst_flags_t, int, tree);
175 static tree for_each_template_parm (tree, tree_fn_t, void*,
176 hash_set<tree> *, bool, tree_fn_t = NULL);
177 static tree expand_template_argument_pack (tree);
178 static tree build_template_parm_index (int, int, int, tree, tree);
179 static bool inline_needs_template_parms (tree, bool);
180 static void push_inline_template_parms_recursive (tree, int);
181 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
182 static int mark_template_parm (tree, void *);
183 static int template_parm_this_level_p (tree, void *);
184 static tree tsubst_friend_function (tree, tree);
185 static tree tsubst_friend_class (tree, tree);
186 static int can_complete_type_without_circularity (tree);
187 static tree get_bindings (tree, tree, tree, bool);
188 static int template_decl_level (tree);
189 static int check_cv_quals_for_unify (int, tree, tree);
190 static int unify_pack_expansion (tree, tree, tree,
191 tree, unification_kind_t, bool, bool);
192 static tree copy_template_args (tree);
193 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
194 tree most_specialized_partial_spec (tree, tsubst_flags_t);
195 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
196 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
197 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
198 static bool check_specialization_scope (void);
199 static tree process_partial_specialization (tree);
200 static void set_current_access_from_decl (tree);
201 static enum template_base_result get_template_base (tree, tree, tree, tree,
202 bool , tree *);
203 static tree try_class_unification (tree, tree, tree, tree, bool);
204 static bool class_nttp_const_wrapper_p (tree t);
205 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
206 tree, tree);
207 static bool template_template_parm_bindings_ok_p (tree, tree);
208 static void tsubst_default_arguments (tree, tsubst_flags_t);
209 static tree for_each_template_parm_r (tree *, int *, void *);
210 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
211 static void copy_default_args_to_explicit_spec (tree);
212 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
213 static bool dependent_template_arg_p (tree);
214 static bool any_template_arguments_need_structural_equality_p (tree);
215 static bool dependent_type_p_r (tree);
216 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
217 static tree tsubst_decl (tree, tree, tsubst_flags_t);
218 static void perform_instantiation_time_access_checks (tree, tree);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
223 static bool complex_alias_template_p (const_tree tmpl);
224 static tree get_underlying_template (tree);
225 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
226 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
227 static tree make_argument_pack (tree);
228 static void register_parameter_specializations (tree, tree);
229 static tree enclosing_instantiation_of (tree tctx);
230 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_UNIQUE_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, /*hiding=*/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, /*hiding=*/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, /*hiding=*/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 bool found_hidden = false;
2992
2993 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2994 if (!was_template_id)
2995 {
2996 tree fns;
2997
2998 gcc_assert (identifier_p (declarator));
2999 if (ctype)
3000 fns = dname;
3001 else
3002 {
3003 /* If there is no class context, the explicit instantiation
3004 must be at namespace scope. */
3005 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
3006
3007 /* Find the namespace binding, using the declaration
3008 context. */
3009 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3010 LOOK_want::NORMAL, true);
3011 if (fns == error_mark_node)
3012 {
3013 /* If lookup fails, look for a friend declaration so we can
3014 give a better diagnostic. */
3015 fns = (lookup_qualified_name
3016 (CP_DECL_CONTEXT (decl), dname,
3017 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3018 /*complain*/true));
3019 found_hidden = true;
3020 }
3021
3022 if (fns == error_mark_node || !is_overloaded_fn (fns))
3023 {
3024 error ("%qD is not a template function", dname);
3025 fns = error_mark_node;
3026 }
3027 }
3028
3029 declarator = lookup_template_function (fns, NULL_TREE);
3030 }
3031
3032 if (declarator == error_mark_node)
3033 return error_mark_node;
3034
3035 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3036 {
3037 if (!explicit_instantiation)
3038 /* A specialization in class scope. This is invalid,
3039 but the error will already have been flagged by
3040 check_specialization_scope. */
3041 return error_mark_node;
3042 else
3043 {
3044 /* It's not valid to write an explicit instantiation in
3045 class scope, e.g.:
3046
3047 class C { template void f(); }
3048
3049 This case is caught by the parser. However, on
3050 something like:
3051
3052 template class C { void f(); };
3053
3054 (which is invalid) we can get here. The error will be
3055 issued later. */
3056 ;
3057 }
3058
3059 return decl;
3060 }
3061 else if (ctype != NULL_TREE
3062 && (identifier_p (TREE_OPERAND (declarator, 0))))
3063 {
3064 // We'll match variable templates in start_decl.
3065 if (VAR_P (decl))
3066 return decl;
3067
3068 /* Find the list of functions in ctype that have the same
3069 name as the declared function. */
3070 tree name = TREE_OPERAND (declarator, 0);
3071
3072 if (constructor_name_p (name, ctype))
3073 {
3074 if (DECL_CONSTRUCTOR_P (decl)
3075 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3076 : !CLASSTYPE_DESTRUCTOR (ctype))
3077 {
3078 /* From [temp.expl.spec]:
3079
3080 If such an explicit specialization for the member
3081 of a class template names an implicitly-declared
3082 special member function (clause _special_), the
3083 program is ill-formed.
3084
3085 Similar language is found in [temp.explicit]. */
3086 error ("specialization of implicitly-declared special member function");
3087 return error_mark_node;
3088 }
3089
3090 name = DECL_NAME (decl);
3091 }
3092
3093 /* For a type-conversion operator, We might be looking for
3094 `operator int' which will be a specialization of
3095 `operator T'. Grab all the conversion operators, and
3096 then select from them. */
3097 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3098 ? conv_op_identifier : name);
3099
3100 if (fns == NULL_TREE)
3101 {
3102 error ("no member function %qD declared in %qT", name, ctype);
3103 return error_mark_node;
3104 }
3105 else
3106 TREE_OPERAND (declarator, 0) = fns;
3107 }
3108
3109 /* Figure out what exactly is being specialized at this point.
3110 Note that for an explicit instantiation, even one for a
3111 member function, we cannot tell a priori whether the
3112 instantiation is for a member template, or just a member
3113 function of a template class. Even if a member template is
3114 being instantiated, the member template arguments may be
3115 elided if they can be deduced from the rest of the
3116 declaration. */
3117 tmpl = determine_specialization (declarator, decl,
3118 &targs,
3119 member_specialization,
3120 template_count,
3121 tsk);
3122
3123 if (!tmpl || tmpl == error_mark_node)
3124 /* We couldn't figure out what this declaration was
3125 specializing. */
3126 return error_mark_node;
3127 else
3128 {
3129 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3130 {
3131 auto_diagnostic_group d;
3132 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3133 "friend declaration %qD is not visible to "
3134 "explicit specialization", tmpl))
3135 inform (DECL_SOURCE_LOCATION (tmpl),
3136 "friend declaration here");
3137 }
3138
3139 if (!ctype && !is_friend
3140 && CP_DECL_CONTEXT (decl) == current_namespace)
3141 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3142
3143 tree gen_tmpl = most_general_template (tmpl);
3144
3145 if (explicit_instantiation)
3146 {
3147 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3148 is done by do_decl_instantiation later. */
3149
3150 int arg_depth = TMPL_ARGS_DEPTH (targs);
3151 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3152
3153 if (arg_depth > parm_depth)
3154 {
3155 /* If TMPL is not the most general template (for
3156 example, if TMPL is a friend template that is
3157 injected into namespace scope), then there will
3158 be too many levels of TARGS. Remove some of them
3159 here. */
3160 int i;
3161 tree new_targs;
3162
3163 new_targs = make_tree_vec (parm_depth);
3164 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3165 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3166 = TREE_VEC_ELT (targs, i);
3167 targs = new_targs;
3168 }
3169
3170 return instantiate_template (tmpl, targs, tf_error);
3171 }
3172
3173 /* If we thought that the DECL was a member function, but it
3174 turns out to be specializing a static member function,
3175 make DECL a static member function as well. */
3176 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3177 && DECL_STATIC_FUNCTION_P (tmpl)
3178 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3179 revert_static_member_fn (decl);
3180
3181 /* If this is a specialization of a member template of a
3182 template class, we want to return the TEMPLATE_DECL, not
3183 the specialization of it. */
3184 if (tsk == tsk_template && !was_template_id)
3185 {
3186 tree result = DECL_TEMPLATE_RESULT (tmpl);
3187 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3188 DECL_INITIAL (result) = NULL_TREE;
3189 if (have_def)
3190 {
3191 tree parm;
3192 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3193 DECL_SOURCE_LOCATION (result)
3194 = DECL_SOURCE_LOCATION (decl);
3195 /* We want to use the argument list specified in the
3196 definition, not in the original declaration. */
3197 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3198 for (parm = DECL_ARGUMENTS (result); parm;
3199 parm = DECL_CHAIN (parm))
3200 DECL_CONTEXT (parm) = result;
3201 }
3202 return register_specialization (tmpl, gen_tmpl, targs,
3203 is_friend, 0);
3204 }
3205
3206 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3207 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3208
3209 if (was_template_id)
3210 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3211
3212 /* Inherit default function arguments from the template
3213 DECL is specializing. */
3214 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3215 copy_default_args_to_explicit_spec (decl);
3216
3217 /* This specialization has the same protection as the
3218 template it specializes. */
3219 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3220 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3221
3222 /* 7.1.1-1 [dcl.stc]
3223
3224 A storage-class-specifier shall not be specified in an
3225 explicit specialization...
3226
3227 The parser rejects these, so unless action is taken here,
3228 explicit function specializations will always appear with
3229 global linkage.
3230
3231 The action recommended by the C++ CWG in response to C++
3232 defect report 605 is to make the storage class and linkage
3233 of the explicit specialization match the templated function:
3234
3235 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3236 */
3237 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3238 {
3239 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3240 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3241
3242 /* A concept cannot be specialized. */
3243 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3244 {
3245 error ("explicit specialization of function concept %qD",
3246 gen_tmpl);
3247 return error_mark_node;
3248 }
3249
3250 /* This specialization has the same linkage and visibility as
3251 the function template it specializes. */
3252 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3253 if (! TREE_PUBLIC (decl))
3254 {
3255 DECL_INTERFACE_KNOWN (decl) = 1;
3256 DECL_NOT_REALLY_EXTERN (decl) = 1;
3257 }
3258 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3259 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3260 {
3261 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3262 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3263 }
3264 }
3265
3266 /* If DECL is a friend declaration, declared using an
3267 unqualified name, the namespace associated with DECL may
3268 have been set incorrectly. For example, in:
3269
3270 template <typename T> void f(T);
3271 namespace N {
3272 struct S { friend void f<int>(int); }
3273 }
3274
3275 we will have set the DECL_CONTEXT for the friend
3276 declaration to N, rather than to the global namespace. */
3277 if (DECL_NAMESPACE_SCOPE_P (decl))
3278 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3279
3280 if (is_friend && !have_def)
3281 /* This is not really a declaration of a specialization.
3282 It's just the name of an instantiation. But, it's not
3283 a request for an instantiation, either. */
3284 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3285 else if (TREE_CODE (decl) == FUNCTION_DECL)
3286 /* A specialization is not necessarily COMDAT. */
3287 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3288 && DECL_DECLARED_INLINE_P (decl));
3289 else if (VAR_P (decl))
3290 DECL_COMDAT (decl) = false;
3291
3292 /* If this is a full specialization, register it so that we can find
3293 it again. Partial specializations will be registered in
3294 process_partial_specialization. */
3295 if (!processing_template_decl)
3296 {
3297 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3298
3299 decl = register_specialization (decl, gen_tmpl, targs,
3300 is_friend, 0);
3301 }
3302
3303
3304 /* A 'structor should already have clones. */
3305 gcc_assert (decl == error_mark_node
3306 || variable_template_p (tmpl)
3307 || !(DECL_CONSTRUCTOR_P (decl)
3308 || DECL_DESTRUCTOR_P (decl))
3309 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3310 }
3311 }
3312
3313 return decl;
3314 }
3315
3316 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3317 parameters. These are represented in the same format used for
3318 DECL_TEMPLATE_PARMS. */
3319
3320 int
3321 comp_template_parms (const_tree parms1, const_tree parms2)
3322 {
3323 const_tree p1;
3324 const_tree p2;
3325
3326 if (parms1 == parms2)
3327 return 1;
3328
3329 for (p1 = parms1, p2 = parms2;
3330 p1 != NULL_TREE && p2 != NULL_TREE;
3331 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3332 {
3333 tree t1 = TREE_VALUE (p1);
3334 tree t2 = TREE_VALUE (p2);
3335 int i;
3336
3337 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3338 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3339
3340 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3341 return 0;
3342
3343 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3344 {
3345 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3346 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3347
3348 /* If either of the template parameters are invalid, assume
3349 they match for the sake of error recovery. */
3350 if (error_operand_p (parm1) || error_operand_p (parm2))
3351 return 1;
3352
3353 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3354 return 0;
3355
3356 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3357 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3358 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3359 continue;
3360 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3361 return 0;
3362 }
3363 }
3364
3365 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3366 /* One set of parameters has more parameters lists than the
3367 other. */
3368 return 0;
3369
3370 return 1;
3371 }
3372
3373 /* Returns true if two template parameters are declared with
3374 equivalent constraints. */
3375
3376 static bool
3377 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3378 {
3379 tree req1 = TREE_TYPE (parm1);
3380 tree req2 = TREE_TYPE (parm2);
3381 if (!req1 != !req2)
3382 return false;
3383 if (req1)
3384 return cp_tree_equal (req1, req2);
3385 return true;
3386 }
3387
3388 /* Returns true when two template parameters are equivalent. */
3389
3390 static bool
3391 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3392 {
3393 tree decl1 = TREE_VALUE (parm1);
3394 tree decl2 = TREE_VALUE (parm2);
3395
3396 /* If either of the template parameters are invalid, assume
3397 they match for the sake of error recovery. */
3398 if (error_operand_p (decl1) || error_operand_p (decl2))
3399 return true;
3400
3401 /* ... they declare parameters of the same kind. */
3402 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3403 return false;
3404
3405 /* ... one parameter was introduced by a parameter declaration, then
3406 both are. This case arises as a result of eagerly rewriting declarations
3407 during parsing. */
3408 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3409 return false;
3410
3411 /* ... if either declares a pack, they both do. */
3412 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3413 return false;
3414
3415 if (TREE_CODE (decl1) == PARM_DECL)
3416 {
3417 /* ... if they declare non-type parameters, the types are equivalent. */
3418 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3419 return false;
3420 }
3421 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3422 {
3423 /* ... if they declare template template parameters, their template
3424 parameter lists are equivalent. */
3425 if (!template_heads_equivalent_p (decl1, decl2))
3426 return false;
3427 }
3428
3429 /* ... if they are declared with a qualified-concept name, they both
3430 are, and those names are equivalent. */
3431 return template_parameter_constraints_equivalent_p (parm1, parm2);
3432 }
3433
3434 /* Returns true if two template parameters lists are equivalent.
3435 Two template parameter lists are equivalent if they have the
3436 same length and their corresponding parameters are equivalent.
3437
3438 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3439 data structure returned by DECL_TEMPLATE_PARMS.
3440
3441 This is generally the same implementation as comp_template_parms
3442 except that it also the concept names and arguments used to
3443 introduce parameters. */
3444
3445 static bool
3446 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3447 {
3448 if (parms1 == parms2)
3449 return true;
3450
3451 const_tree p1 = parms1;
3452 const_tree p2 = parms2;
3453 while (p1 != NULL_TREE && p2 != NULL_TREE)
3454 {
3455 tree list1 = TREE_VALUE (p1);
3456 tree list2 = TREE_VALUE (p2);
3457
3458 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3459 return 0;
3460
3461 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3462 {
3463 tree parm1 = TREE_VEC_ELT (list1, i);
3464 tree parm2 = TREE_VEC_ELT (list2, i);
3465 if (!template_parameters_equivalent_p (parm1, parm2))
3466 return false;
3467 }
3468
3469 p1 = TREE_CHAIN (p1);
3470 p2 = TREE_CHAIN (p2);
3471 }
3472
3473 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3474 return false;
3475
3476 return true;
3477 }
3478
3479 /* Return true if the requires-clause of the template parameter lists are
3480 equivalent and false otherwise. */
3481 static bool
3482 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3483 {
3484 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3485 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3486 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3487 return false;
3488 if (!cp_tree_equal (req1, req2))
3489 return false;
3490 return true;
3491 }
3492
3493 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3494 Two template heads are equivalent if their template parameter
3495 lists are equivalent and their requires clauses are equivalent.
3496
3497 In pre-C++20, this is equivalent to calling comp_template_parms
3498 for the template parameters of TMPL1 and TMPL2. */
3499
3500 bool
3501 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3502 {
3503 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3504 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3505
3506 /* Don't change the matching rules for pre-C++20. */
3507 if (cxx_dialect < cxx20)
3508 return comp_template_parms (parms1, parms2);
3509
3510 /* ... have the same number of template parameters, and their
3511 corresponding parameters are equivalent. */
3512 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3513 return false;
3514
3515 /* ... if either has a requires-clause, they both do and their
3516 corresponding constraint-expressions are equivalent. */
3517 return template_requirements_equivalent_p (parms1, parms2);
3518 }
3519
3520 /* Determine whether PARM is a parameter pack. */
3521
3522 bool
3523 template_parameter_pack_p (const_tree parm)
3524 {
3525 /* Determine if we have a non-type template parameter pack. */
3526 if (TREE_CODE (parm) == PARM_DECL)
3527 return (DECL_TEMPLATE_PARM_P (parm)
3528 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3529 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3530 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3531
3532 /* If this is a list of template parameters, we could get a
3533 TYPE_DECL or a TEMPLATE_DECL. */
3534 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3535 parm = TREE_TYPE (parm);
3536
3537 /* Otherwise it must be a type template parameter. */
3538 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3539 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3540 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3541 }
3542
3543 /* Determine if T is a function parameter pack. */
3544
3545 bool
3546 function_parameter_pack_p (const_tree t)
3547 {
3548 if (t && TREE_CODE (t) == PARM_DECL)
3549 return DECL_PACK_P (t);
3550 return false;
3551 }
3552
3553 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3554 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3555
3556 tree
3557 get_function_template_decl (const_tree primary_func_tmpl_inst)
3558 {
3559 if (! primary_func_tmpl_inst
3560 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3561 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3562 return NULL;
3563
3564 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3565 }
3566
3567 /* Return true iff the function parameter PARAM_DECL was expanded
3568 from the function parameter pack PACK. */
3569
3570 bool
3571 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3572 {
3573 if (DECL_ARTIFICIAL (param_decl)
3574 || !function_parameter_pack_p (pack))
3575 return false;
3576
3577 /* The parameter pack and its pack arguments have the same
3578 DECL_PARM_INDEX. */
3579 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3580 }
3581
3582 /* Determine whether ARGS describes a variadic template args list,
3583 i.e., one that is terminated by a template argument pack. */
3584
3585 static bool
3586 template_args_variadic_p (tree args)
3587 {
3588 int nargs;
3589 tree last_parm;
3590
3591 if (args == NULL_TREE)
3592 return false;
3593
3594 args = INNERMOST_TEMPLATE_ARGS (args);
3595 nargs = TREE_VEC_LENGTH (args);
3596
3597 if (nargs == 0)
3598 return false;
3599
3600 last_parm = TREE_VEC_ELT (args, nargs - 1);
3601
3602 return ARGUMENT_PACK_P (last_parm);
3603 }
3604
3605 /* Generate a new name for the parameter pack name NAME (an
3606 IDENTIFIER_NODE) that incorporates its */
3607
3608 static tree
3609 make_ith_pack_parameter_name (tree name, int i)
3610 {
3611 /* Munge the name to include the parameter index. */
3612 #define NUMBUF_LEN 128
3613 char numbuf[NUMBUF_LEN];
3614 char* newname;
3615 int newname_len;
3616
3617 if (name == NULL_TREE)
3618 return name;
3619 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3620 newname_len = IDENTIFIER_LENGTH (name)
3621 + strlen (numbuf) + 2;
3622 newname = (char*)alloca (newname_len);
3623 snprintf (newname, newname_len,
3624 "%s#%i", IDENTIFIER_POINTER (name), i);
3625 return get_identifier (newname);
3626 }
3627
3628 /* Return true if T is a primary function, class or alias template
3629 specialization, not including the template pattern. */
3630
3631 bool
3632 primary_template_specialization_p (const_tree t)
3633 {
3634 if (!t)
3635 return false;
3636
3637 if (VAR_OR_FUNCTION_DECL_P (t))
3638 return (DECL_LANG_SPECIFIC (t)
3639 && DECL_USE_TEMPLATE (t)
3640 && DECL_TEMPLATE_INFO (t)
3641 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3642 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3643 return (CLASSTYPE_TEMPLATE_INFO (t)
3644 && CLASSTYPE_USE_TEMPLATE (t)
3645 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3646 else if (alias_template_specialization_p (t, nt_transparent))
3647 return true;
3648 return false;
3649 }
3650
3651 /* Return true if PARM is a template template parameter. */
3652
3653 bool
3654 template_template_parameter_p (const_tree parm)
3655 {
3656 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3657 }
3658
3659 /* Return true iff PARM is a DECL representing a type template
3660 parameter. */
3661
3662 bool
3663 template_type_parameter_p (const_tree parm)
3664 {
3665 return (parm
3666 && (TREE_CODE (parm) == TYPE_DECL
3667 || TREE_CODE (parm) == TEMPLATE_DECL)
3668 && DECL_TEMPLATE_PARM_P (parm));
3669 }
3670
3671 /* Return the template parameters of T if T is a
3672 primary template instantiation, NULL otherwise. */
3673
3674 tree
3675 get_primary_template_innermost_parameters (const_tree t)
3676 {
3677 tree parms = NULL, template_info = NULL;
3678
3679 if ((template_info = get_template_info (t))
3680 && primary_template_specialization_p (t))
3681 parms = INNERMOST_TEMPLATE_PARMS
3682 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3683
3684 return parms;
3685 }
3686
3687 /* Return the template parameters of the LEVELth level from the full list
3688 of template parameters PARMS. */
3689
3690 tree
3691 get_template_parms_at_level (tree parms, int level)
3692 {
3693 tree p;
3694 if (!parms
3695 || TREE_CODE (parms) != TREE_LIST
3696 || level > TMPL_PARMS_DEPTH (parms))
3697 return NULL_TREE;
3698
3699 for (p = parms; p; p = TREE_CHAIN (p))
3700 if (TMPL_PARMS_DEPTH (p) == level)
3701 return p;
3702
3703 return NULL_TREE;
3704 }
3705
3706 /* Returns the template arguments of T if T is a template instantiation,
3707 NULL otherwise. */
3708
3709 tree
3710 get_template_innermost_arguments (const_tree t)
3711 {
3712 tree args = NULL, template_info = NULL;
3713
3714 if ((template_info = get_template_info (t))
3715 && TI_ARGS (template_info))
3716 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3717
3718 return args;
3719 }
3720
3721 /* Return the argument pack elements of T if T is a template argument pack,
3722 NULL otherwise. */
3723
3724 tree
3725 get_template_argument_pack_elems (const_tree t)
3726 {
3727 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3728 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3729 return NULL;
3730
3731 return ARGUMENT_PACK_ARGS (t);
3732 }
3733
3734 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3735 ARGUMENT_PACK_SELECT represents. */
3736
3737 static tree
3738 argument_pack_select_arg (tree t)
3739 {
3740 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3741 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3742
3743 /* If the selected argument is an expansion E, that most likely means we were
3744 called from gen_elem_of_pack_expansion_instantiation during the
3745 substituting of an argument pack (of which the Ith element is a pack
3746 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3747 In this case, the Ith element resulting from this substituting is going to
3748 be a pack expansion, which pattern is the pattern of E. Let's return the
3749 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3750 resulting pack expansion from it. */
3751 if (PACK_EXPANSION_P (arg))
3752 {
3753 /* Make sure we aren't throwing away arg info. */
3754 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3755 arg = PACK_EXPANSION_PATTERN (arg);
3756 }
3757
3758 return arg;
3759 }
3760
3761
3762 /* True iff FN is a function representing a built-in variadic parameter
3763 pack. */
3764
3765 bool
3766 builtin_pack_fn_p (tree fn)
3767 {
3768 if (!fn
3769 || TREE_CODE (fn) != FUNCTION_DECL
3770 || !DECL_IS_UNDECLARED_BUILTIN (fn))
3771 return false;
3772
3773 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3774 return true;
3775
3776 return false;
3777 }
3778
3779 /* True iff CALL is a call to a function representing a built-in variadic
3780 parameter pack. */
3781
3782 static bool
3783 builtin_pack_call_p (tree call)
3784 {
3785 if (TREE_CODE (call) != CALL_EXPR)
3786 return false;
3787 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3788 }
3789
3790 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3791
3792 static tree
3793 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3794 tree in_decl)
3795 {
3796 tree ohi = CALL_EXPR_ARG (call, 0);
3797 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3798 false/*fn*/, true/*int_cst*/);
3799
3800 if (value_dependent_expression_p (hi))
3801 {
3802 if (hi != ohi)
3803 {
3804 call = copy_node (call);
3805 CALL_EXPR_ARG (call, 0) = hi;
3806 }
3807 tree ex = make_pack_expansion (call, complain);
3808 tree vec = make_tree_vec (1);
3809 TREE_VEC_ELT (vec, 0) = ex;
3810 return vec;
3811 }
3812 else
3813 {
3814 hi = cxx_constant_value (hi);
3815 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3816
3817 /* Calculate the largest value of len that won't make the size of the vec
3818 overflow an int. The compiler will exceed resource limits long before
3819 this, but it seems a decent place to diagnose. */
3820 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3821
3822 if (len < 0 || len > max)
3823 {
3824 if ((complain & tf_error)
3825 && hi != error_mark_node)
3826 error ("argument to %<__integer_pack%> must be between 0 and %d",
3827 max);
3828 return error_mark_node;
3829 }
3830
3831 tree vec = make_tree_vec (len);
3832
3833 for (int i = 0; i < len; ++i)
3834 TREE_VEC_ELT (vec, i) = size_int (i);
3835
3836 return vec;
3837 }
3838 }
3839
3840 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3841 CALL. */
3842
3843 static tree
3844 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3845 tree in_decl)
3846 {
3847 if (!builtin_pack_call_p (call))
3848 return NULL_TREE;
3849
3850 tree fn = CALL_EXPR_FN (call);
3851
3852 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3853 return expand_integer_pack (call, args, complain, in_decl);
3854
3855 return NULL_TREE;
3856 }
3857
3858 /* Structure used to track the progress of find_parameter_packs_r. */
3859 struct find_parameter_pack_data
3860 {
3861 /* TREE_LIST that will contain all of the parameter packs found by
3862 the traversal. */
3863 tree* parameter_packs;
3864
3865 /* Set of AST nodes that have been visited by the traversal. */
3866 hash_set<tree> *visited;
3867
3868 /* True iff we're making a type pack expansion. */
3869 bool type_pack_expansion_p;
3870 };
3871
3872 /* Identifies all of the argument packs that occur in a template
3873 argument and appends them to the TREE_LIST inside DATA, which is a
3874 find_parameter_pack_data structure. This is a subroutine of
3875 make_pack_expansion and uses_parameter_packs. */
3876 static tree
3877 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3878 {
3879 tree t = *tp;
3880 struct find_parameter_pack_data* ppd =
3881 (struct find_parameter_pack_data*)data;
3882 bool parameter_pack_p = false;
3883
3884 /* Don't look through typedefs; we are interested in whether a
3885 parameter pack is actually written in the expression/type we're
3886 looking at, not the target type. */
3887 if (TYPE_P (t) && typedef_variant_p (t))
3888 {
3889 /* But do look at arguments for an alias template. */
3890 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3891 cp_walk_tree (&TI_ARGS (tinfo),
3892 &find_parameter_packs_r,
3893 ppd, ppd->visited);
3894 *walk_subtrees = 0;
3895 return NULL_TREE;
3896 }
3897
3898 /* Identify whether this is a parameter pack or not. */
3899 switch (TREE_CODE (t))
3900 {
3901 case TEMPLATE_PARM_INDEX:
3902 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3903 parameter_pack_p = true;
3904 break;
3905
3906 case TEMPLATE_TYPE_PARM:
3907 t = TYPE_MAIN_VARIANT (t);
3908 /* FALLTHRU */
3909 case TEMPLATE_TEMPLATE_PARM:
3910 /* If the placeholder appears in the decl-specifier-seq of a function
3911 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3912 is a pack expansion, the invented template parameter is a template
3913 parameter pack. */
3914 if (ppd->type_pack_expansion_p && is_auto (t))
3915 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3916 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3917 parameter_pack_p = true;
3918 break;
3919
3920 case FIELD_DECL:
3921 case PARM_DECL:
3922 if (DECL_PACK_P (t))
3923 {
3924 /* We don't want to walk into the type of a PARM_DECL,
3925 because we don't want to see the type parameter pack. */
3926 *walk_subtrees = 0;
3927 parameter_pack_p = true;
3928 }
3929 break;
3930
3931 case VAR_DECL:
3932 if (DECL_PACK_P (t))
3933 {
3934 /* We don't want to walk into the type of a variadic capture proxy,
3935 because we don't want to see the type parameter pack. */
3936 *walk_subtrees = 0;
3937 parameter_pack_p = true;
3938 }
3939 else if (variable_template_specialization_p (t))
3940 {
3941 cp_walk_tree (&DECL_TI_ARGS (t),
3942 find_parameter_packs_r,
3943 ppd, ppd->visited);
3944 *walk_subtrees = 0;
3945 }
3946 break;
3947
3948 case CALL_EXPR:
3949 if (builtin_pack_call_p (t))
3950 parameter_pack_p = true;
3951 break;
3952
3953 case BASES:
3954 parameter_pack_p = true;
3955 break;
3956 default:
3957 /* Not a parameter pack. */
3958 break;
3959 }
3960
3961 if (parameter_pack_p)
3962 {
3963 /* Add this parameter pack to the list. */
3964 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3965 }
3966
3967 if (TYPE_P (t))
3968 cp_walk_tree (&TYPE_CONTEXT (t),
3969 &find_parameter_packs_r, ppd, ppd->visited);
3970
3971 /* This switch statement will return immediately if we don't find a
3972 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3973 switch (TREE_CODE (t))
3974 {
3975 case BOUND_TEMPLATE_TEMPLATE_PARM:
3976 /* Check the template itself. */
3977 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3978 &find_parameter_packs_r, ppd, ppd->visited);
3979 return NULL_TREE;
3980
3981 case DECL_EXPR:
3982 {
3983 tree decl = DECL_EXPR_DECL (t);
3984 /* Ignore the declaration of a capture proxy for a parameter pack. */
3985 if (is_capture_proxy (decl))
3986 *walk_subtrees = 0;
3987 if (is_typedef_decl (decl))
3988 /* Since we stop at typedefs above, we need to look through them at
3989 the point of the DECL_EXPR. */
3990 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3991 &find_parameter_packs_r, ppd, ppd->visited);
3992 return NULL_TREE;
3993 }
3994
3995 case TEMPLATE_DECL:
3996 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3997 return NULL_TREE;
3998 cp_walk_tree (&TREE_TYPE (t),
3999 &find_parameter_packs_r, ppd, ppd->visited);
4000 return NULL_TREE;
4001
4002 case TYPE_PACK_EXPANSION:
4003 case EXPR_PACK_EXPANSION:
4004 *walk_subtrees = 0;
4005 return NULL_TREE;
4006
4007 case INTEGER_TYPE:
4008 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4009 ppd, ppd->visited);
4010 *walk_subtrees = 0;
4011 return NULL_TREE;
4012
4013 case IDENTIFIER_NODE:
4014 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4015 ppd->visited);
4016 *walk_subtrees = 0;
4017 return NULL_TREE;
4018
4019 case LAMBDA_EXPR:
4020 {
4021 /* Since we defer implicit capture, look in the parms and body. */
4022 tree fn = lambda_function (t);
4023 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4024 ppd->visited);
4025 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4026 ppd->visited);
4027 return NULL_TREE;
4028 }
4029
4030 case DECLTYPE_TYPE:
4031 {
4032 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4033 type_pack_expansion_p to false so that any placeholders
4034 within the expression don't get marked as parameter packs. */
4035 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4036 ppd->type_pack_expansion_p = false;
4037 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4038 ppd, ppd->visited);
4039 ppd->type_pack_expansion_p = type_pack_expansion_p;
4040 *walk_subtrees = 0;
4041 return NULL_TREE;
4042 }
4043
4044 case IF_STMT:
4045 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4046 ppd, ppd->visited);
4047 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4048 ppd, ppd->visited);
4049 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4050 ppd, ppd->visited);
4051 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4052 *walk_subtrees = 0;
4053 return NULL_TREE;
4054
4055 default:
4056 return NULL_TREE;
4057 }
4058
4059 return NULL_TREE;
4060 }
4061
4062 /* Determines if the expression or type T uses any parameter packs. */
4063 tree
4064 uses_parameter_packs (tree t)
4065 {
4066 tree parameter_packs = NULL_TREE;
4067 struct find_parameter_pack_data ppd;
4068 ppd.parameter_packs = &parameter_packs;
4069 ppd.visited = new hash_set<tree>;
4070 ppd.type_pack_expansion_p = false;
4071 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4072 delete ppd.visited;
4073 return parameter_packs;
4074 }
4075
4076 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4077 representation a base-class initializer into a parameter pack
4078 expansion. If all goes well, the resulting node will be an
4079 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4080 respectively. */
4081 tree
4082 make_pack_expansion (tree arg, tsubst_flags_t complain)
4083 {
4084 tree result;
4085 tree parameter_packs = NULL_TREE;
4086 bool for_types = false;
4087 struct find_parameter_pack_data ppd;
4088
4089 if (!arg || arg == error_mark_node)
4090 return arg;
4091
4092 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4093 {
4094 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4095 class initializer. In this case, the TREE_PURPOSE will be a
4096 _TYPE node (representing the base class expansion we're
4097 initializing) and the TREE_VALUE will be a TREE_LIST
4098 containing the initialization arguments.
4099
4100 The resulting expansion looks somewhat different from most
4101 expansions. Rather than returning just one _EXPANSION, we
4102 return a TREE_LIST whose TREE_PURPOSE is a
4103 TYPE_PACK_EXPANSION containing the bases that will be
4104 initialized. The TREE_VALUE will be identical to the
4105 original TREE_VALUE, which is a list of arguments that will
4106 be passed to each base. We do not introduce any new pack
4107 expansion nodes into the TREE_VALUE (although it is possible
4108 that some already exist), because the TREE_PURPOSE and
4109 TREE_VALUE all need to be expanded together with the same
4110 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4111 resulting TREE_PURPOSE will mention the parameter packs in
4112 both the bases and the arguments to the bases. */
4113 tree purpose;
4114 tree value;
4115 tree parameter_packs = NULL_TREE;
4116
4117 /* Determine which parameter packs will be used by the base
4118 class expansion. */
4119 ppd.visited = new hash_set<tree>;
4120 ppd.parameter_packs = &parameter_packs;
4121 ppd.type_pack_expansion_p = false;
4122 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4123 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4124 &ppd, ppd.visited);
4125
4126 if (parameter_packs == NULL_TREE)
4127 {
4128 if (complain & tf_error)
4129 error ("base initializer expansion %qT contains no parameter packs",
4130 arg);
4131 delete ppd.visited;
4132 return error_mark_node;
4133 }
4134
4135 if (TREE_VALUE (arg) != void_type_node)
4136 {
4137 /* Collect the sets of parameter packs used in each of the
4138 initialization arguments. */
4139 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4140 {
4141 /* Determine which parameter packs will be expanded in this
4142 argument. */
4143 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4144 &ppd, ppd.visited);
4145 }
4146 }
4147
4148 delete ppd.visited;
4149
4150 /* Create the pack expansion type for the base type. */
4151 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4152 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4153 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4154 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4155
4156 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4157 they will rarely be compared to anything. */
4158 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4159
4160 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4161 }
4162
4163 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4164 for_types = true;
4165
4166 /* Build the PACK_EXPANSION_* node. */
4167 result = for_types
4168 ? cxx_make_type (TYPE_PACK_EXPANSION)
4169 : make_node (EXPR_PACK_EXPANSION);
4170 SET_PACK_EXPANSION_PATTERN (result, arg);
4171 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4172 {
4173 /* Propagate type and const-expression information. */
4174 TREE_TYPE (result) = TREE_TYPE (arg);
4175 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4176 /* Mark this read now, since the expansion might be length 0. */
4177 mark_exp_read (arg);
4178 }
4179 else
4180 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4181 they will rarely be compared to anything. */
4182 SET_TYPE_STRUCTURAL_EQUALITY (result);
4183
4184 /* Determine which parameter packs will be expanded. */
4185 ppd.parameter_packs = &parameter_packs;
4186 ppd.visited = new hash_set<tree>;
4187 ppd.type_pack_expansion_p = TYPE_P (arg);
4188 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4189 delete ppd.visited;
4190
4191 /* Make sure we found some parameter packs. */
4192 if (parameter_packs == NULL_TREE)
4193 {
4194 if (complain & tf_error)
4195 {
4196 if (TYPE_P (arg))
4197 error ("expansion pattern %qT contains no parameter packs", arg);
4198 else
4199 error ("expansion pattern %qE contains no parameter packs", arg);
4200 }
4201 return error_mark_node;
4202 }
4203 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4204
4205 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4206
4207 return result;
4208 }
4209
4210 /* Checks T for any "bare" parameter packs, which have not yet been
4211 expanded, and issues an error if any are found. This operation can
4212 only be done on full expressions or types (e.g., an expression
4213 statement, "if" condition, etc.), because we could have expressions like:
4214
4215 foo(f(g(h(args)))...)
4216
4217 where "args" is a parameter pack. check_for_bare_parameter_packs
4218 should not be called for the subexpressions args, h(args),
4219 g(h(args)), or f(g(h(args))), because we would produce erroneous
4220 error messages.
4221
4222 Returns TRUE and emits an error if there were bare parameter packs,
4223 returns FALSE otherwise. */
4224 bool
4225 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4226 {
4227 tree parameter_packs = NULL_TREE;
4228 struct find_parameter_pack_data ppd;
4229
4230 if (!processing_template_decl || !t || t == error_mark_node)
4231 return false;
4232
4233 if (TREE_CODE (t) == TYPE_DECL)
4234 t = TREE_TYPE (t);
4235
4236 ppd.parameter_packs = &parameter_packs;
4237 ppd.visited = new hash_set<tree>;
4238 ppd.type_pack_expansion_p = false;
4239 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4240 delete ppd.visited;
4241
4242 /* It's OK for a lambda to have an unexpanded parameter pack from the
4243 containing context, but do complain about unexpanded capture packs. */
4244 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4245 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4246 for (; parameter_packs;
4247 parameter_packs = TREE_CHAIN (parameter_packs))
4248 {
4249 tree pack = TREE_VALUE (parameter_packs);
4250 if (is_capture_proxy (pack))
4251 break;
4252 }
4253
4254 if (parameter_packs)
4255 {
4256 if (loc == UNKNOWN_LOCATION)
4257 loc = cp_expr_loc_or_input_loc (t);
4258 error_at (loc, "parameter packs not expanded with %<...%>:");
4259 while (parameter_packs)
4260 {
4261 tree pack = TREE_VALUE (parameter_packs);
4262 tree name = NULL_TREE;
4263
4264 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4265 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4266 name = TYPE_NAME (pack);
4267 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4268 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4269 else if (TREE_CODE (pack) == CALL_EXPR)
4270 name = DECL_NAME (CALL_EXPR_FN (pack));
4271 else
4272 name = DECL_NAME (pack);
4273
4274 if (name)
4275 inform (loc, " %qD", name);
4276 else
4277 inform (loc, " %s", "<anonymous>");
4278
4279 parameter_packs = TREE_CHAIN (parameter_packs);
4280 }
4281
4282 return true;
4283 }
4284
4285 return false;
4286 }
4287
4288 /* Expand any parameter packs that occur in the template arguments in
4289 ARGS. */
4290 tree
4291 expand_template_argument_pack (tree args)
4292 {
4293 if (args == error_mark_node)
4294 return error_mark_node;
4295
4296 tree result_args = NULL_TREE;
4297 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4298 int num_result_args = -1;
4299 int non_default_args_count = -1;
4300
4301 /* First, determine if we need to expand anything, and the number of
4302 slots we'll need. */
4303 for (in_arg = 0; in_arg < nargs; ++in_arg)
4304 {
4305 tree arg = TREE_VEC_ELT (args, in_arg);
4306 if (arg == NULL_TREE)
4307 return args;
4308 if (ARGUMENT_PACK_P (arg))
4309 {
4310 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4311 if (num_result_args < 0)
4312 num_result_args = in_arg + num_packed;
4313 else
4314 num_result_args += num_packed;
4315 }
4316 else
4317 {
4318 if (num_result_args >= 0)
4319 num_result_args++;
4320 }
4321 }
4322
4323 /* If no expansion is necessary, we're done. */
4324 if (num_result_args < 0)
4325 return args;
4326
4327 /* Expand arguments. */
4328 result_args = make_tree_vec (num_result_args);
4329 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4330 non_default_args_count =
4331 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4332 for (in_arg = 0; in_arg < nargs; ++in_arg)
4333 {
4334 tree arg = TREE_VEC_ELT (args, in_arg);
4335 if (ARGUMENT_PACK_P (arg))
4336 {
4337 tree packed = ARGUMENT_PACK_ARGS (arg);
4338 int i, num_packed = TREE_VEC_LENGTH (packed);
4339 for (i = 0; i < num_packed; ++i, ++out_arg)
4340 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4341 if (non_default_args_count > 0)
4342 non_default_args_count += num_packed - 1;
4343 }
4344 else
4345 {
4346 TREE_VEC_ELT (result_args, out_arg) = arg;
4347 ++out_arg;
4348 }
4349 }
4350 if (non_default_args_count >= 0)
4351 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4352 return result_args;
4353 }
4354
4355 /* Checks if DECL shadows a template parameter.
4356
4357 [temp.local]: A template-parameter shall not be redeclared within its
4358 scope (including nested scopes).
4359
4360 Emits an error and returns TRUE if the DECL shadows a parameter,
4361 returns FALSE otherwise. */
4362
4363 bool
4364 check_template_shadow (tree decl)
4365 {
4366 tree olddecl;
4367
4368 /* If we're not in a template, we can't possibly shadow a template
4369 parameter. */
4370 if (!current_template_parms)
4371 return true;
4372
4373 /* Figure out what we're shadowing. */
4374 decl = OVL_FIRST (decl);
4375 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4376
4377 /* If there's no previous binding for this name, we're not shadowing
4378 anything, let alone a template parameter. */
4379 if (!olddecl)
4380 return true;
4381
4382 /* If we're not shadowing a template parameter, we're done. Note
4383 that OLDDECL might be an OVERLOAD (or perhaps even an
4384 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4385 node. */
4386 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4387 return true;
4388
4389 /* We check for decl != olddecl to avoid bogus errors for using a
4390 name inside a class. We check TPFI to avoid duplicate errors for
4391 inline member templates. */
4392 if (decl == olddecl
4393 || (DECL_TEMPLATE_PARM_P (decl)
4394 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4395 return true;
4396
4397 /* Don't complain about the injected class name, as we've already
4398 complained about the class itself. */
4399 if (DECL_SELF_REFERENCE_P (decl))
4400 return false;
4401
4402 if (DECL_TEMPLATE_PARM_P (decl))
4403 error ("declaration of template parameter %q+D shadows "
4404 "template parameter", decl);
4405 else
4406 error ("declaration of %q+#D shadows template parameter", decl);
4407 inform (DECL_SOURCE_LOCATION (olddecl),
4408 "template parameter %qD declared here", olddecl);
4409 return false;
4410 }
4411
4412 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4413 ORIG_LEVEL, DECL, and TYPE. */
4414
4415 static tree
4416 build_template_parm_index (int index,
4417 int level,
4418 int orig_level,
4419 tree decl,
4420 tree type)
4421 {
4422 tree t = make_node (TEMPLATE_PARM_INDEX);
4423 TEMPLATE_PARM_IDX (t) = index;
4424 TEMPLATE_PARM_LEVEL (t) = level;
4425 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4426 TEMPLATE_PARM_DECL (t) = decl;
4427 TREE_TYPE (t) = type;
4428 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4429 TREE_READONLY (t) = TREE_READONLY (decl);
4430
4431 return t;
4432 }
4433
4434 /* Find the canonical type parameter for the given template type
4435 parameter. Returns the canonical type parameter, which may be TYPE
4436 if no such parameter existed. */
4437
4438 static tree
4439 canonical_type_parameter (tree type)
4440 {
4441 int idx = TEMPLATE_TYPE_IDX (type);
4442
4443 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4444
4445 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4446 vec_safe_grow_cleared (canonical_template_parms, idx + 1, true);
4447
4448 for (tree list = (*canonical_template_parms)[idx];
4449 list; list = TREE_CHAIN (list))
4450 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4451 return TREE_VALUE (list);
4452
4453 (*canonical_template_parms)[idx]
4454 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4455 return type;
4456 }
4457
4458 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4459 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4460 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4461 new one is created. */
4462
4463 static tree
4464 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4465 tsubst_flags_t complain)
4466 {
4467 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4468 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4469 != TEMPLATE_PARM_LEVEL (index) - levels)
4470 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4471 {
4472 tree orig_decl = TEMPLATE_PARM_DECL (index);
4473
4474 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4475 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4476 type);
4477 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4478 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4479 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4480 DECL_ARTIFICIAL (decl) = 1;
4481 SET_DECL_TEMPLATE_PARM_P (decl);
4482
4483 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4484 TEMPLATE_PARM_LEVEL (index) - levels,
4485 TEMPLATE_PARM_ORIG_LEVEL (index),
4486 decl, type);
4487 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4488 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4489 = TEMPLATE_PARM_PARAMETER_PACK (index);
4490
4491 /* Template template parameters need this. */
4492 tree inner = decl;
4493 if (TREE_CODE (decl) == TEMPLATE_DECL)
4494 {
4495 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4496 TYPE_DECL, DECL_NAME (decl), type);
4497 DECL_TEMPLATE_RESULT (decl) = inner;
4498 DECL_ARTIFICIAL (inner) = true;
4499 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4500 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4501 }
4502
4503 /* Attach the TPI to the decl. */
4504 if (TREE_CODE (inner) == TYPE_DECL)
4505 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4506 else
4507 DECL_INITIAL (decl) = tpi;
4508 }
4509
4510 return TEMPLATE_PARM_DESCENDANTS (index);
4511 }
4512
4513 /* Process information from new template parameter PARM and append it
4514 to the LIST being built. This new parameter is a non-type
4515 parameter iff IS_NON_TYPE is true. This new parameter is a
4516 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4517 is in PARM_LOC. */
4518
4519 tree
4520 process_template_parm (tree list, location_t parm_loc, tree parm,
4521 bool is_non_type, bool is_parameter_pack)
4522 {
4523 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4524 tree prev = NULL_TREE;
4525 int idx = 0;
4526
4527 if (list)
4528 {
4529 prev = tree_last (list);
4530
4531 tree p = TREE_VALUE (prev);
4532 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4533 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4534 else if (TREE_CODE (p) == PARM_DECL)
4535 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4536
4537 ++idx;
4538 }
4539
4540 tree decl = NULL_TREE;
4541 tree defval = TREE_PURPOSE (parm);
4542 tree constr = TREE_TYPE (parm);
4543
4544 if (is_non_type)
4545 {
4546 parm = TREE_VALUE (parm);
4547
4548 SET_DECL_TEMPLATE_PARM_P (parm);
4549
4550 if (TREE_TYPE (parm) != error_mark_node)
4551 {
4552 /* [temp.param]
4553
4554 The top-level cv-qualifiers on the template-parameter are
4555 ignored when determining its type. */
4556 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4557 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4558 TREE_TYPE (parm) = error_mark_node;
4559 else if (uses_parameter_packs (TREE_TYPE (parm))
4560 && !is_parameter_pack
4561 /* If we're in a nested template parameter list, the template
4562 template parameter could be a parameter pack. */
4563 && processing_template_parmlist == 1)
4564 {
4565 /* This template parameter is not a parameter pack, but it
4566 should be. Complain about "bare" parameter packs. */
4567 check_for_bare_parameter_packs (TREE_TYPE (parm));
4568
4569 /* Recover by calling this a parameter pack. */
4570 is_parameter_pack = true;
4571 }
4572 }
4573
4574 /* A template parameter is not modifiable. */
4575 TREE_CONSTANT (parm) = 1;
4576 TREE_READONLY (parm) = 1;
4577 decl = build_decl (parm_loc,
4578 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4579 TREE_CONSTANT (decl) = 1;
4580 TREE_READONLY (decl) = 1;
4581 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4582 = build_template_parm_index (idx, processing_template_decl,
4583 processing_template_decl,
4584 decl, TREE_TYPE (parm));
4585
4586 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4587 = is_parameter_pack;
4588 }
4589 else
4590 {
4591 tree t;
4592 parm = TREE_VALUE (TREE_VALUE (parm));
4593
4594 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4595 {
4596 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4597 /* This is for distinguishing between real templates and template
4598 template parameters */
4599 TREE_TYPE (parm) = t;
4600
4601 /* any_template_parm_r expects to be able to get the targs of a
4602 DECL_TEMPLATE_RESULT. */
4603 tree result = DECL_TEMPLATE_RESULT (parm);
4604 TREE_TYPE (result) = t;
4605 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4606 tree tinfo = build_template_info (parm, args);
4607 retrofit_lang_decl (result);
4608 DECL_TEMPLATE_INFO (result) = tinfo;
4609
4610 decl = parm;
4611 }
4612 else
4613 {
4614 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4615 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4616 decl = build_decl (parm_loc,
4617 TYPE_DECL, parm, t);
4618 }
4619
4620 TYPE_NAME (t) = decl;
4621 TYPE_STUB_DECL (t) = decl;
4622 parm = decl;
4623 TEMPLATE_TYPE_PARM_INDEX (t)
4624 = build_template_parm_index (idx, processing_template_decl,
4625 processing_template_decl,
4626 decl, TREE_TYPE (parm));
4627 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4628 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4629 SET_TYPE_STRUCTURAL_EQUALITY (t);
4630 else
4631 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4632 }
4633 DECL_ARTIFICIAL (decl) = 1;
4634 SET_DECL_TEMPLATE_PARM_P (decl);
4635
4636 /* Build requirements for the type/template parameter.
4637 This must be done after SET_DECL_TEMPLATE_PARM_P or
4638 process_template_parm could fail. */
4639 tree reqs = finish_shorthand_constraint (parm, constr);
4640
4641 decl = pushdecl (decl);
4642 if (!is_non_type)
4643 parm = decl;
4644
4645 /* Build the parameter node linking the parameter declaration,
4646 its default argument (if any), and its constraints (if any). */
4647 parm = build_tree_list (defval, parm);
4648 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4649
4650 if (prev)
4651 TREE_CHAIN (prev) = parm;
4652 else
4653 list = parm;
4654
4655 return list;
4656 }
4657
4658 /* The end of a template parameter list has been reached. Process the
4659 tree list into a parameter vector, converting each parameter into a more
4660 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4661 as PARM_DECLs. */
4662
4663 tree
4664 end_template_parm_list (tree parms)
4665 {
4666 tree saved_parmlist = make_tree_vec (list_length (parms));
4667
4668 /* Pop the dummy parameter level and add the real one. We do not
4669 morph the dummy parameter in place, as it might have been
4670 captured by a (nested) template-template-parm. */
4671 current_template_parms = TREE_CHAIN (current_template_parms);
4672
4673 current_template_parms
4674 = tree_cons (size_int (processing_template_decl),
4675 saved_parmlist, current_template_parms);
4676
4677 for (unsigned ix = 0; parms; ix++)
4678 {
4679 tree parm = parms;
4680 parms = TREE_CHAIN (parms);
4681 TREE_CHAIN (parm) = NULL_TREE;
4682
4683 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4684 }
4685
4686 --processing_template_parmlist;
4687
4688 return saved_parmlist;
4689 }
4690
4691 // Explicitly indicate the end of the template parameter list. We assume
4692 // that the current template parameters have been constructed and/or
4693 // managed explicitly, as when creating new template template parameters
4694 // from a shorthand constraint.
4695 void
4696 end_template_parm_list ()
4697 {
4698 --processing_template_parmlist;
4699 }
4700
4701 /* end_template_decl is called after a template declaration is seen. */
4702
4703 void
4704 end_template_decl (void)
4705 {
4706 reset_specialization ();
4707
4708 if (! processing_template_decl)
4709 return;
4710
4711 /* This matches the pushlevel in begin_template_parm_list. */
4712 finish_scope ();
4713
4714 --processing_template_decl;
4715 current_template_parms = TREE_CHAIN (current_template_parms);
4716 }
4717
4718 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4719 thereof, and converts it into an argument suitable to be passed to
4720 the type substitution functions. Note that if the TREE_LIST contains
4721 an error_mark node, the returned argument is error_mark_node. */
4722
4723 tree
4724 template_parm_to_arg (tree t)
4725 {
4726 if (!t)
4727 return NULL_TREE;
4728
4729 if (TREE_CODE (t) == TREE_LIST)
4730 t = TREE_VALUE (t);
4731
4732 if (error_operand_p (t))
4733 return error_mark_node;
4734
4735 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4736 {
4737 if (TREE_CODE (t) == TYPE_DECL
4738 || TREE_CODE (t) == TEMPLATE_DECL)
4739 t = TREE_TYPE (t);
4740 else
4741 t = DECL_INITIAL (t);
4742 }
4743
4744 gcc_assert (TEMPLATE_PARM_P (t));
4745
4746 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4747 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4748 {
4749 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4750 {
4751 /* Turn this argument into a TYPE_ARGUMENT_PACK
4752 with a single element, which expands T. */
4753 tree vec = make_tree_vec (1);
4754 if (CHECKING_P)
4755 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4756
4757 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4758
4759 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4760 SET_ARGUMENT_PACK_ARGS (t, vec);
4761 }
4762 }
4763 else
4764 {
4765 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4766 {
4767 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4768 with a single element, which expands T. */
4769 tree vec = make_tree_vec (1);
4770 if (CHECKING_P)
4771 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4772
4773 t = convert_from_reference (t);
4774 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4775
4776 t = make_node (NONTYPE_ARGUMENT_PACK);
4777 SET_ARGUMENT_PACK_ARGS (t, vec);
4778 }
4779 else
4780 t = convert_from_reference (t);
4781 }
4782 return t;
4783 }
4784
4785 /* Given a single level of template parameters (a TREE_VEC), return it
4786 as a set of template arguments. */
4787
4788 tree
4789 template_parms_level_to_args (tree parms)
4790 {
4791 tree a = copy_node (parms);
4792 TREE_TYPE (a) = NULL_TREE;
4793 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4794 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4795
4796 if (CHECKING_P)
4797 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4798
4799 return a;
4800 }
4801
4802 /* Given a set of template parameters, return them as a set of template
4803 arguments. The template parameters are represented as a TREE_VEC, in
4804 the form documented in cp-tree.h for template arguments. */
4805
4806 tree
4807 template_parms_to_args (tree parms)
4808 {
4809 tree header;
4810 tree args = NULL_TREE;
4811 int length = TMPL_PARMS_DEPTH (parms);
4812 int l = length;
4813
4814 /* If there is only one level of template parameters, we do not
4815 create a TREE_VEC of TREE_VECs. Instead, we return a single
4816 TREE_VEC containing the arguments. */
4817 if (length > 1)
4818 args = make_tree_vec (length);
4819
4820 for (header = parms; header; header = TREE_CHAIN (header))
4821 {
4822 tree a = template_parms_level_to_args (TREE_VALUE (header));
4823
4824 if (length > 1)
4825 TREE_VEC_ELT (args, --l) = a;
4826 else
4827 args = a;
4828 }
4829
4830 return args;
4831 }
4832
4833 /* Within the declaration of a template, return the currently active
4834 template parameters as an argument TREE_VEC. */
4835
4836 static tree
4837 current_template_args (void)
4838 {
4839 return template_parms_to_args (current_template_parms);
4840 }
4841
4842 /* Return the fully generic arguments for of TMPL, i.e. what
4843 current_template_args would be while parsing it. */
4844
4845 tree
4846 generic_targs_for (tree tmpl)
4847 {
4848 if (tmpl == NULL_TREE)
4849 return NULL_TREE;
4850 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4851 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4852 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4853 template parameter, it has no TEMPLATE_INFO; for a partial
4854 specialization, it has the arguments for the primary template, and we
4855 want the arguments for the partial specialization. */;
4856 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4857 if (tree ti = get_template_info (result))
4858 return TI_ARGS (ti);
4859 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4860 }
4861
4862 /* Update the declared TYPE by doing any lookups which were thought to be
4863 dependent, but are not now that we know the SCOPE of the declarator. */
4864
4865 tree
4866 maybe_update_decl_type (tree orig_type, tree scope)
4867 {
4868 tree type = orig_type;
4869
4870 if (type == NULL_TREE)
4871 return type;
4872
4873 if (TREE_CODE (orig_type) == TYPE_DECL)
4874 type = TREE_TYPE (type);
4875
4876 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4877 && dependent_type_p (type)
4878 /* Don't bother building up the args in this case. */
4879 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4880 {
4881 /* tsubst in the args corresponding to the template parameters,
4882 including auto if present. Most things will be unchanged, but
4883 make_typename_type and tsubst_qualified_id will resolve
4884 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4885 tree args = current_template_args ();
4886 tree auto_node = type_uses_auto (type);
4887 tree pushed;
4888 if (auto_node)
4889 {
4890 tree auto_vec = make_tree_vec (1);
4891 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4892 args = add_to_template_args (args, auto_vec);
4893 }
4894 pushed = push_scope (scope);
4895 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4896 if (pushed)
4897 pop_scope (scope);
4898 }
4899
4900 if (type == error_mark_node)
4901 return orig_type;
4902
4903 if (TREE_CODE (orig_type) == TYPE_DECL)
4904 {
4905 if (same_type_p (type, TREE_TYPE (orig_type)))
4906 type = orig_type;
4907 else
4908 type = TYPE_NAME (type);
4909 }
4910 return type;
4911 }
4912
4913 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4914 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4915 the new template is a member template. */
4916
4917 static tree
4918 build_template_decl (tree decl, tree parms, bool member_template_p)
4919 {
4920 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4921 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4922 DECL_TEMPLATE_PARMS (tmpl) = parms;
4923 DECL_TEMPLATE_RESULT (tmpl) = decl;
4924 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4925 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4926 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4927 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4928
4929 return tmpl;
4930 }
4931
4932 struct template_parm_data
4933 {
4934 /* The level of the template parameters we are currently
4935 processing. */
4936 int level;
4937
4938 /* The index of the specialization argument we are currently
4939 processing. */
4940 int current_arg;
4941
4942 /* An array whose size is the number of template parameters. The
4943 elements are nonzero if the parameter has been used in any one
4944 of the arguments processed so far. */
4945 int* parms;
4946
4947 /* An array whose size is the number of template arguments. The
4948 elements are nonzero if the argument makes use of template
4949 parameters of this level. */
4950 int* arg_uses_template_parms;
4951 };
4952
4953 /* Subroutine of push_template_decl used to see if each template
4954 parameter in a partial specialization is used in the explicit
4955 argument list. If T is of the LEVEL given in DATA (which is
4956 treated as a template_parm_data*), then DATA->PARMS is marked
4957 appropriately. */
4958
4959 static int
4960 mark_template_parm (tree t, void* data)
4961 {
4962 int level;
4963 int idx;
4964 struct template_parm_data* tpd = (struct template_parm_data*) data;
4965
4966 template_parm_level_and_index (t, &level, &idx);
4967
4968 if (level == tpd->level)
4969 {
4970 tpd->parms[idx] = 1;
4971 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4972 }
4973
4974 /* In C++17 the type of a non-type argument is a deduced context. */
4975 if (cxx_dialect >= cxx17
4976 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4977 for_each_template_parm (TREE_TYPE (t),
4978 &mark_template_parm,
4979 data,
4980 NULL,
4981 /*include_nondeduced_p=*/false);
4982
4983 /* Return zero so that for_each_template_parm will continue the
4984 traversal of the tree; we want to mark *every* template parm. */
4985 return 0;
4986 }
4987
4988 /* Process the partial specialization DECL. */
4989
4990 static tree
4991 process_partial_specialization (tree decl)
4992 {
4993 tree type = TREE_TYPE (decl);
4994 tree tinfo = get_template_info (decl);
4995 tree maintmpl = TI_TEMPLATE (tinfo);
4996 tree specargs = TI_ARGS (tinfo);
4997 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4998 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4999 tree inner_parms;
5000 tree inst;
5001 int nargs = TREE_VEC_LENGTH (inner_args);
5002 int ntparms;
5003 int i;
5004 bool did_error_intro = false;
5005 struct template_parm_data tpd;
5006 struct template_parm_data tpd2;
5007
5008 gcc_assert (current_template_parms);
5009
5010 /* A concept cannot be specialized. */
5011 if (flag_concepts && variable_concept_p (maintmpl))
5012 {
5013 error ("specialization of variable concept %q#D", maintmpl);
5014 return error_mark_node;
5015 }
5016
5017 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5018 ntparms = TREE_VEC_LENGTH (inner_parms);
5019
5020 /* We check that each of the template parameters given in the
5021 partial specialization is used in the argument list to the
5022 specialization. For example:
5023
5024 template <class T> struct S;
5025 template <class T> struct S<T*>;
5026
5027 The second declaration is OK because `T*' uses the template
5028 parameter T, whereas
5029
5030 template <class T> struct S<int>;
5031
5032 is no good. Even trickier is:
5033
5034 template <class T>
5035 struct S1
5036 {
5037 template <class U>
5038 struct S2;
5039 template <class U>
5040 struct S2<T>;
5041 };
5042
5043 The S2<T> declaration is actually invalid; it is a
5044 full-specialization. Of course,
5045
5046 template <class U>
5047 struct S2<T (*)(U)>;
5048
5049 or some such would have been OK. */
5050 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5051 tpd.parms = XALLOCAVEC (int, ntparms);
5052 memset (tpd.parms, 0, sizeof (int) * ntparms);
5053
5054 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5055 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5056 for (i = 0; i < nargs; ++i)
5057 {
5058 tpd.current_arg = i;
5059 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5060 &mark_template_parm,
5061 &tpd,
5062 NULL,
5063 /*include_nondeduced_p=*/false);
5064 }
5065 for (i = 0; i < ntparms; ++i)
5066 if (tpd.parms[i] == 0)
5067 {
5068 /* One of the template parms was not used in a deduced context in the
5069 specialization. */
5070 if (!did_error_intro)
5071 {
5072 error ("template parameters not deducible in "
5073 "partial specialization:");
5074 did_error_intro = true;
5075 }
5076
5077 inform (input_location, " %qD",
5078 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5079 }
5080
5081 if (did_error_intro)
5082 return error_mark_node;
5083
5084 /* [temp.class.spec]
5085
5086 The argument list of the specialization shall not be identical to
5087 the implicit argument list of the primary template. */
5088 tree main_args
5089 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5090 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5091 && (!flag_concepts
5092 || !strictly_subsumes (current_template_constraints (), maintmpl)))
5093 {
5094 if (!flag_concepts)
5095 error ("partial specialization %q+D does not specialize "
5096 "any template arguments; to define the primary template, "
5097 "remove the template argument list", decl);
5098 else
5099 error ("partial specialization %q+D does not specialize any "
5100 "template arguments and is not more constrained than "
5101 "the primary template; to define the primary template, "
5102 "remove the template argument list", decl);
5103 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5104 }
5105
5106 /* A partial specialization that replaces multiple parameters of the
5107 primary template with a pack expansion is less specialized for those
5108 parameters. */
5109 if (nargs < DECL_NTPARMS (maintmpl))
5110 {
5111 error ("partial specialization is not more specialized than the "
5112 "primary template because it replaces multiple parameters "
5113 "with a pack expansion");
5114 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5115 /* Avoid crash in process_partial_specialization. */
5116 return decl;
5117 }
5118
5119 else if (nargs > DECL_NTPARMS (maintmpl))
5120 {
5121 error ("too many arguments for partial specialization %qT", type);
5122 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5123 /* Avoid crash below. */
5124 return decl;
5125 }
5126
5127 /* If we aren't in a dependent class, we can actually try deduction. */
5128 else if (tpd.level == 1
5129 /* FIXME we should be able to handle a partial specialization of a
5130 partial instantiation, but currently we can't (c++/41727). */
5131 && TMPL_ARGS_DEPTH (specargs) == 1
5132 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5133 {
5134 auto_diagnostic_group d;
5135 if (permerror (input_location, "partial specialization %qD is not "
5136 "more specialized than", decl))
5137 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5138 maintmpl);
5139 }
5140
5141 /* [temp.class.spec]
5142
5143 A partially specialized non-type argument expression shall not
5144 involve template parameters of the partial specialization except
5145 when the argument expression is a simple identifier.
5146
5147 The type of a template parameter corresponding to a specialized
5148 non-type argument shall not be dependent on a parameter of the
5149 specialization.
5150
5151 Also, we verify that pack expansions only occur at the
5152 end of the argument list. */
5153 tpd2.parms = 0;
5154 for (i = 0; i < nargs; ++i)
5155 {
5156 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5157 tree arg = TREE_VEC_ELT (inner_args, i);
5158 tree packed_args = NULL_TREE;
5159 int j, len = 1;
5160
5161 if (ARGUMENT_PACK_P (arg))
5162 {
5163 /* Extract the arguments from the argument pack. We'll be
5164 iterating over these in the following loop. */
5165 packed_args = ARGUMENT_PACK_ARGS (arg);
5166 len = TREE_VEC_LENGTH (packed_args);
5167 }
5168
5169 for (j = 0; j < len; j++)
5170 {
5171 if (packed_args)
5172 /* Get the Jth argument in the parameter pack. */
5173 arg = TREE_VEC_ELT (packed_args, j);
5174
5175 if (PACK_EXPANSION_P (arg))
5176 {
5177 /* Pack expansions must come at the end of the
5178 argument list. */
5179 if ((packed_args && j < len - 1)
5180 || (!packed_args && i < nargs - 1))
5181 {
5182 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5183 error ("parameter pack argument %qE must be at the "
5184 "end of the template argument list", arg);
5185 else
5186 error ("parameter pack argument %qT must be at the "
5187 "end of the template argument list", arg);
5188 }
5189 }
5190
5191 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5192 /* We only care about the pattern. */
5193 arg = PACK_EXPANSION_PATTERN (arg);
5194
5195 if (/* These first two lines are the `non-type' bit. */
5196 !TYPE_P (arg)
5197 && TREE_CODE (arg) != TEMPLATE_DECL
5198 /* This next two lines are the `argument expression is not just a
5199 simple identifier' condition and also the `specialized
5200 non-type argument' bit. */
5201 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5202 && !((REFERENCE_REF_P (arg)
5203 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5204 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5205 {
5206 if ((!packed_args && tpd.arg_uses_template_parms[i])
5207 || (packed_args && uses_template_parms (arg)))
5208 error_at (cp_expr_loc_or_input_loc (arg),
5209 "template argument %qE involves template "
5210 "parameter(s)", arg);
5211 else
5212 {
5213 /* Look at the corresponding template parameter,
5214 marking which template parameters its type depends
5215 upon. */
5216 tree type = TREE_TYPE (parm);
5217
5218 if (!tpd2.parms)
5219 {
5220 /* We haven't yet initialized TPD2. Do so now. */
5221 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5222 /* The number of parameters here is the number in the
5223 main template, which, as checked in the assertion
5224 above, is NARGS. */
5225 tpd2.parms = XALLOCAVEC (int, nargs);
5226 tpd2.level =
5227 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5228 }
5229
5230 /* Mark the template parameters. But this time, we're
5231 looking for the template parameters of the main
5232 template, not in the specialization. */
5233 tpd2.current_arg = i;
5234 tpd2.arg_uses_template_parms[i] = 0;
5235 memset (tpd2.parms, 0, sizeof (int) * nargs);
5236 for_each_template_parm (type,
5237 &mark_template_parm,
5238 &tpd2,
5239 NULL,
5240 /*include_nondeduced_p=*/false);
5241
5242 if (tpd2.arg_uses_template_parms [i])
5243 {
5244 /* The type depended on some template parameters.
5245 If they are fully specialized in the
5246 specialization, that's OK. */
5247 int j;
5248 int count = 0;
5249 for (j = 0; j < nargs; ++j)
5250 if (tpd2.parms[j] != 0
5251 && tpd.arg_uses_template_parms [j])
5252 ++count;
5253 if (count != 0)
5254 error_n (input_location, count,
5255 "type %qT of template argument %qE depends "
5256 "on a template parameter",
5257 "type %qT of template argument %qE depends "
5258 "on template parameters",
5259 type,
5260 arg);
5261 }
5262 }
5263 }
5264 }
5265 }
5266
5267 /* We should only get here once. */
5268 if (TREE_CODE (decl) == TYPE_DECL)
5269 gcc_assert (!COMPLETE_TYPE_P (type));
5270
5271 // Build the template decl.
5272 tree tmpl = build_template_decl (decl, current_template_parms,
5273 DECL_MEMBER_TEMPLATE_P (maintmpl));
5274 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5275 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5276 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5277
5278 /* Give template template parms a DECL_CONTEXT of the template
5279 for which they are a parameter. */
5280 for (i = 0; i < ntparms; ++i)
5281 {
5282 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5283 if (TREE_CODE (parm) == TEMPLATE_DECL)
5284 DECL_CONTEXT (parm) = tmpl;
5285 }
5286
5287 if (VAR_P (decl))
5288 /* We didn't register this in check_explicit_specialization so we could
5289 wait until the constraints were set. */
5290 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5291 else
5292 associate_classtype_constraints (type);
5293
5294 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5295 = tree_cons (specargs, tmpl,
5296 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5297 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5298
5299 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5300 inst = TREE_CHAIN (inst))
5301 {
5302 tree instance = TREE_VALUE (inst);
5303 if (TYPE_P (instance)
5304 ? (COMPLETE_TYPE_P (instance)
5305 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5306 : DECL_TEMPLATE_INSTANTIATION (instance))
5307 {
5308 tree spec = most_specialized_partial_spec (instance, tf_none);
5309 tree inst_decl = (DECL_P (instance)
5310 ? instance : TYPE_NAME (instance));
5311 if (!spec)
5312 /* OK */;
5313 else if (spec == error_mark_node)
5314 permerror (input_location,
5315 "declaration of %qD ambiguates earlier template "
5316 "instantiation for %qD", decl, inst_decl);
5317 else if (TREE_VALUE (spec) == tmpl)
5318 permerror (input_location,
5319 "partial specialization of %qD after instantiation "
5320 "of %qD", decl, inst_decl);
5321 }
5322 }
5323
5324 return decl;
5325 }
5326
5327 /* PARM is a template parameter of some form; return the corresponding
5328 TEMPLATE_PARM_INDEX. */
5329
5330 static tree
5331 get_template_parm_index (tree parm)
5332 {
5333 if (TREE_CODE (parm) == PARM_DECL
5334 || TREE_CODE (parm) == CONST_DECL)
5335 parm = DECL_INITIAL (parm);
5336 else if (TREE_CODE (parm) == TYPE_DECL
5337 || TREE_CODE (parm) == TEMPLATE_DECL)
5338 parm = TREE_TYPE (parm);
5339 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5340 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5341 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5342 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5343 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5344 return parm;
5345 }
5346
5347 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5348 parameter packs used by the template parameter PARM. */
5349
5350 static void
5351 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5352 {
5353 /* A type parm can't refer to another parm. */
5354 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5355 return;
5356 else if (TREE_CODE (parm) == PARM_DECL)
5357 {
5358 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5359 ppd, ppd->visited);
5360 return;
5361 }
5362
5363 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5364
5365 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5366 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5367 {
5368 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5369 if (template_parameter_pack_p (p))
5370 /* Any packs in the type are expanded by this parameter. */;
5371 else
5372 fixed_parameter_pack_p_1 (p, ppd);
5373 }
5374 }
5375
5376 /* PARM is a template parameter pack. Return any parameter packs used in
5377 its type or the type of any of its template parameters. If there are
5378 any such packs, it will be instantiated into a fixed template parameter
5379 list by partial instantiation rather than be fully deduced. */
5380
5381 tree
5382 fixed_parameter_pack_p (tree parm)
5383 {
5384 /* This can only be true in a member template. */
5385 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5386 return NULL_TREE;
5387 /* This can only be true for a parameter pack. */
5388 if (!template_parameter_pack_p (parm))
5389 return NULL_TREE;
5390 /* A type parm can't refer to another parm. */
5391 if (TREE_CODE (parm) == TYPE_DECL)
5392 return NULL_TREE;
5393
5394 tree parameter_packs = NULL_TREE;
5395 struct find_parameter_pack_data ppd;
5396 ppd.parameter_packs = &parameter_packs;
5397 ppd.visited = new hash_set<tree>;
5398 ppd.type_pack_expansion_p = false;
5399
5400 fixed_parameter_pack_p_1 (parm, &ppd);
5401
5402 delete ppd.visited;
5403 return parameter_packs;
5404 }
5405
5406 /* Check that a template declaration's use of default arguments and
5407 parameter packs is not invalid. Here, PARMS are the template
5408 parameters. IS_PRIMARY is true if DECL is the thing declared by
5409 a primary template. IS_PARTIAL is true if DECL is a partial
5410 specialization.
5411
5412 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5413 function template declaration or a friend class template
5414 declaration. In the function case, 1 indicates a declaration, 2
5415 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5416 emitted for extraneous default arguments.
5417
5418 Returns TRUE if there were no errors found, FALSE otherwise. */
5419
5420 bool
5421 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5422 bool is_partial, int is_friend_decl)
5423 {
5424 const char *msg;
5425 int last_level_to_check;
5426 tree parm_level;
5427 bool no_errors = true;
5428
5429 /* [temp.param]
5430
5431 A default template-argument shall not be specified in a
5432 function template declaration or a function template definition, nor
5433 in the template-parameter-list of the definition of a member of a
5434 class template. */
5435
5436 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5437 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5438 /* You can't have a function template declaration in a local
5439 scope, nor you can you define a member of a class template in a
5440 local scope. */
5441 return true;
5442
5443 if ((TREE_CODE (decl) == TYPE_DECL
5444 && TREE_TYPE (decl)
5445 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5446 || (TREE_CODE (decl) == FUNCTION_DECL
5447 && LAMBDA_FUNCTION_P (decl)))
5448 /* A lambda doesn't have an explicit declaration; don't complain
5449 about the parms of the enclosing class. */
5450 return true;
5451
5452 if (current_class_type
5453 && !TYPE_BEING_DEFINED (current_class_type)
5454 && DECL_LANG_SPECIFIC (decl)
5455 && DECL_DECLARES_FUNCTION_P (decl)
5456 /* If this is either a friend defined in the scope of the class
5457 or a member function. */
5458 && (DECL_FUNCTION_MEMBER_P (decl)
5459 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5460 : DECL_FRIEND_CONTEXT (decl)
5461 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5462 : false)
5463 /* And, if it was a member function, it really was defined in
5464 the scope of the class. */
5465 && (!DECL_FUNCTION_MEMBER_P (decl)
5466 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5467 /* We already checked these parameters when the template was
5468 declared, so there's no need to do it again now. This function
5469 was defined in class scope, but we're processing its body now
5470 that the class is complete. */
5471 return true;
5472
5473 /* Core issue 226 (C++0x only): the following only applies to class
5474 templates. */
5475 if (is_primary
5476 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5477 {
5478 /* [temp.param]
5479
5480 If a template-parameter has a default template-argument, all
5481 subsequent template-parameters shall have a default
5482 template-argument supplied. */
5483 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5484 {
5485 tree inner_parms = TREE_VALUE (parm_level);
5486 int ntparms = TREE_VEC_LENGTH (inner_parms);
5487 int seen_def_arg_p = 0;
5488 int i;
5489
5490 for (i = 0; i < ntparms; ++i)
5491 {
5492 tree parm = TREE_VEC_ELT (inner_parms, i);
5493
5494 if (parm == error_mark_node)
5495 continue;
5496
5497 if (TREE_PURPOSE (parm))
5498 seen_def_arg_p = 1;
5499 else if (seen_def_arg_p
5500 && !template_parameter_pack_p (TREE_VALUE (parm)))
5501 {
5502 error ("no default argument for %qD", TREE_VALUE (parm));
5503 /* For better subsequent error-recovery, we indicate that
5504 there should have been a default argument. */
5505 TREE_PURPOSE (parm) = error_mark_node;
5506 no_errors = false;
5507 }
5508 else if (!is_partial
5509 && !is_friend_decl
5510 /* Don't complain about an enclosing partial
5511 specialization. */
5512 && parm_level == parms
5513 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5514 && i < ntparms - 1
5515 && template_parameter_pack_p (TREE_VALUE (parm))
5516 /* A fixed parameter pack will be partially
5517 instantiated into a fixed length list. */
5518 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5519 {
5520 /* A primary class template, primary variable template
5521 (DR 2032), or alias template can only have one
5522 parameter pack, at the end of the template
5523 parameter list. */
5524
5525 error ("parameter pack %q+D must be at the end of the"
5526 " template parameter list", TREE_VALUE (parm));
5527
5528 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5529 = error_mark_node;
5530 no_errors = false;
5531 }
5532 }
5533 }
5534 }
5535
5536 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5537 || is_partial
5538 || !is_primary
5539 || is_friend_decl)
5540 /* For an ordinary class template, default template arguments are
5541 allowed at the innermost level, e.g.:
5542 template <class T = int>
5543 struct S {};
5544 but, in a partial specialization, they're not allowed even
5545 there, as we have in [temp.class.spec]:
5546
5547 The template parameter list of a specialization shall not
5548 contain default template argument values.
5549
5550 So, for a partial specialization, or for a function template
5551 (in C++98/C++03), we look at all of them. */
5552 ;
5553 else
5554 /* But, for a primary class template that is not a partial
5555 specialization we look at all template parameters except the
5556 innermost ones. */
5557 parms = TREE_CHAIN (parms);
5558
5559 /* Figure out what error message to issue. */
5560 if (is_friend_decl == 2)
5561 msg = G_("default template arguments may not be used in function template "
5562 "friend re-declaration");
5563 else if (is_friend_decl)
5564 msg = G_("default template arguments may not be used in template "
5565 "friend declarations");
5566 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5567 msg = G_("default template arguments may not be used in function templates "
5568 "without %<-std=c++11%> or %<-std=gnu++11%>");
5569 else if (is_partial)
5570 msg = G_("default template arguments may not be used in "
5571 "partial specializations");
5572 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5573 msg = G_("default argument for template parameter for class enclosing %qD");
5574 else
5575 /* Per [temp.param]/9, "A default template-argument shall not be
5576 specified in the template-parameter-lists of the definition of
5577 a member of a class template that appears outside of the member's
5578 class.", thus if we aren't handling a member of a class template
5579 there is no need to examine the parameters. */
5580 return true;
5581
5582 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5583 /* If we're inside a class definition, there's no need to
5584 examine the parameters to the class itself. On the one
5585 hand, they will be checked when the class is defined, and,
5586 on the other, default arguments are valid in things like:
5587 template <class T = double>
5588 struct S { template <class U> void f(U); };
5589 Here the default argument for `S' has no bearing on the
5590 declaration of `f'. */
5591 last_level_to_check = template_class_depth (current_class_type) + 1;
5592 else
5593 /* Check everything. */
5594 last_level_to_check = 0;
5595
5596 for (parm_level = parms;
5597 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5598 parm_level = TREE_CHAIN (parm_level))
5599 {
5600 tree inner_parms = TREE_VALUE (parm_level);
5601 int i;
5602 int ntparms;
5603
5604 ntparms = TREE_VEC_LENGTH (inner_parms);
5605 for (i = 0; i < ntparms; ++i)
5606 {
5607 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5608 continue;
5609
5610 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5611 {
5612 if (msg)
5613 {
5614 no_errors = false;
5615 if (is_friend_decl == 2)
5616 return no_errors;
5617
5618 error (msg, decl);
5619 msg = 0;
5620 }
5621
5622 /* Clear out the default argument so that we are not
5623 confused later. */
5624 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5625 }
5626 }
5627
5628 /* At this point, if we're still interested in issuing messages,
5629 they must apply to classes surrounding the object declared. */
5630 if (msg)
5631 msg = G_("default argument for template parameter for class "
5632 "enclosing %qD");
5633 }
5634
5635 return no_errors;
5636 }
5637
5638 /* Worker for push_template_decl_real, called via
5639 for_each_template_parm. DATA is really an int, indicating the
5640 level of the parameters we are interested in. If T is a template
5641 parameter of that level, return nonzero. */
5642
5643 static int
5644 template_parm_this_level_p (tree t, void* data)
5645 {
5646 int this_level = *(int *)data;
5647 int level;
5648
5649 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5650 level = TEMPLATE_PARM_LEVEL (t);
5651 else
5652 level = TEMPLATE_TYPE_LEVEL (t);
5653 return level == this_level;
5654 }
5655
5656 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5657 DATA is really an int, indicating the innermost outer level of parameters.
5658 If T is a template parameter of that level or further out, return
5659 nonzero. */
5660
5661 static int
5662 template_parm_outer_level (tree t, void *data)
5663 {
5664 int this_level = *(int *)data;
5665 int level;
5666
5667 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5668 level = TEMPLATE_PARM_LEVEL (t);
5669 else
5670 level = TEMPLATE_TYPE_LEVEL (t);
5671 return level <= this_level;
5672 }
5673
5674 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5675 parameters given by current_template_args, or reuses a
5676 previously existing one, if appropriate. Returns the DECL, or an
5677 equivalent one, if it is replaced via a call to duplicate_decls.
5678
5679 If IS_FRIEND is true, DECL is a friend declaration. */
5680
5681 tree
5682 push_template_decl (tree decl, bool is_friend)
5683 {
5684 if (decl == error_mark_node || !current_template_parms)
5685 return error_mark_node;
5686
5687 /* See if this is a partial specialization. */
5688 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5689 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5690 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5691 || (VAR_P (decl)
5692 && DECL_LANG_SPECIFIC (decl)
5693 && DECL_TEMPLATE_SPECIALIZATION (decl)
5694 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5695
5696 /* No surprising friend functions. */
5697 gcc_checking_assert (is_friend
5698 || !(TREE_CODE (decl) == FUNCTION_DECL
5699 && DECL_UNIQUE_FRIEND_P (decl)));
5700
5701 tree ctx;
5702 if (is_friend)
5703 /* For a friend, we want the context of the friend, not
5704 the type of which it is a friend. */
5705 ctx = CP_DECL_CONTEXT (decl);
5706 else if (CP_DECL_CONTEXT (decl)
5707 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5708 /* In the case of a virtual function, we want the class in which
5709 it is defined. */
5710 ctx = CP_DECL_CONTEXT (decl);
5711 else
5712 /* Otherwise, if we're currently defining some class, the DECL
5713 is assumed to be a member of the class. */
5714 ctx = current_scope ();
5715
5716 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5717 ctx = NULL_TREE;
5718
5719 if (!DECL_CONTEXT (decl))
5720 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5721
5722 /* See if this is a primary template. */
5723 bool is_primary = false;
5724 if (is_friend && ctx
5725 && uses_template_parms_level (ctx, processing_template_decl))
5726 /* A friend template that specifies a class context, i.e.
5727 template <typename T> friend void A<T>::f();
5728 is not primary. */
5729 ;
5730 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5731 /* Lambdas are not primary. */
5732 ;
5733 else
5734 is_primary = template_parm_scope_p ();
5735
5736 /* True if the template is a member template, in the sense of
5737 [temp.mem]. */
5738 bool member_template_p = false;
5739
5740 if (is_primary)
5741 {
5742 warning (OPT_Wtemplates, "template %qD declared", decl);
5743
5744 if (DECL_CLASS_SCOPE_P (decl))
5745 member_template_p = true;
5746
5747 if (TREE_CODE (decl) == TYPE_DECL
5748 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5749 {
5750 error ("template class without a name");
5751 return error_mark_node;
5752 }
5753 else if (TREE_CODE (decl) == FUNCTION_DECL)
5754 {
5755 if (member_template_p)
5756 {
5757 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5758 error ("member template %qD may not have virt-specifiers", decl);
5759 }
5760 if (DECL_DESTRUCTOR_P (decl))
5761 {
5762 /* [temp.mem]
5763
5764 A destructor shall not be a member template. */
5765 error_at (DECL_SOURCE_LOCATION (decl),
5766 "destructor %qD declared as member template", decl);
5767 return error_mark_node;
5768 }
5769 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5770 && (!prototype_p (TREE_TYPE (decl))
5771 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5772 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5773 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5774 == void_list_node)))
5775 {
5776 /* [basic.stc.dynamic.allocation]
5777
5778 An allocation function can be a function
5779 template. ... Template allocation functions shall
5780 have two or more parameters. */
5781 error ("invalid template declaration of %qD", decl);
5782 return error_mark_node;
5783 }
5784 }
5785 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5786 && CLASS_TYPE_P (TREE_TYPE (decl)))
5787 {
5788 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5789 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5790 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5791 {
5792 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5793 if (TREE_CODE (t) == TYPE_DECL)
5794 t = TREE_TYPE (t);
5795 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5796 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5797 }
5798 }
5799 else if (TREE_CODE (decl) == TYPE_DECL
5800 && TYPE_DECL_ALIAS_P (decl))
5801 /* alias-declaration */
5802 gcc_assert (!DECL_ARTIFICIAL (decl));
5803 else if (VAR_P (decl))
5804 /* C++14 variable template. */;
5805 else if (TREE_CODE (decl) == CONCEPT_DECL)
5806 /* C++20 concept definitions. */;
5807 else
5808 {
5809 error ("template declaration of %q#D", decl);
5810 return error_mark_node;
5811 }
5812 }
5813
5814 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
5815 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
5816 || (VAR_OR_FUNCTION_DECL_P (decl)
5817 && DECL_LOCAL_DECL_P (decl))));
5818
5819 /* Check to see that the rules regarding the use of default
5820 arguments are not being violated. We check args for a friend
5821 functions when we know whether it's a definition, introducing
5822 declaration or re-declaration. */
5823 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
5824 check_default_tmpl_args (decl, current_template_parms,
5825 is_primary, is_partial, is_friend);
5826
5827 /* Ensure that there are no parameter packs in the type of this
5828 declaration that have not been expanded. */
5829 if (TREE_CODE (decl) == FUNCTION_DECL)
5830 {
5831 /* Check each of the arguments individually to see if there are
5832 any bare parameter packs. */
5833 tree type = TREE_TYPE (decl);
5834 tree arg = DECL_ARGUMENTS (decl);
5835 tree argtype = TYPE_ARG_TYPES (type);
5836
5837 while (arg && argtype)
5838 {
5839 if (!DECL_PACK_P (arg)
5840 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5841 {
5842 /* This is a PARM_DECL that contains unexpanded parameter
5843 packs. We have already complained about this in the
5844 check_for_bare_parameter_packs call, so just replace
5845 these types with ERROR_MARK_NODE. */
5846 TREE_TYPE (arg) = error_mark_node;
5847 TREE_VALUE (argtype) = error_mark_node;
5848 }
5849
5850 arg = DECL_CHAIN (arg);
5851 argtype = TREE_CHAIN (argtype);
5852 }
5853
5854 /* Check for bare parameter packs in the return type and the
5855 exception specifiers. */
5856 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5857 /* Errors were already issued, set return type to int
5858 as the frontend doesn't expect error_mark_node as
5859 the return type. */
5860 TREE_TYPE (type) = integer_type_node;
5861 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5862 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5863 }
5864 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5865 ? DECL_ORIGINAL_TYPE (decl)
5866 : TREE_TYPE (decl)))
5867 {
5868 TREE_TYPE (decl) = error_mark_node;
5869 return error_mark_node;
5870 }
5871
5872 if (is_partial)
5873 return process_partial_specialization (decl);
5874
5875 tree args = current_template_args ();
5876 tree tmpl = NULL_TREE;
5877 bool new_template_p = false;
5878 if (local_p)
5879 {
5880 /* Does not get a template head. */
5881 tmpl = NULL_TREE;
5882 gcc_checking_assert (!is_primary);
5883 }
5884 else if (!ctx
5885 || TREE_CODE (ctx) == FUNCTION_DECL
5886 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5887 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5888 || (is_friend && !(DECL_LANG_SPECIFIC (decl)
5889 && DECL_TEMPLATE_INFO (decl))))
5890 {
5891 if (DECL_LANG_SPECIFIC (decl)
5892 && DECL_TEMPLATE_INFO (decl)
5893 && DECL_TI_TEMPLATE (decl))
5894 tmpl = DECL_TI_TEMPLATE (decl);
5895 /* If DECL is a TYPE_DECL for a class-template, then there won't
5896 be DECL_LANG_SPECIFIC. The information equivalent to
5897 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5898 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5899 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5900 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5901 {
5902 /* Since a template declaration already existed for this
5903 class-type, we must be redeclaring it here. Make sure
5904 that the redeclaration is valid. */
5905 redeclare_class_template (TREE_TYPE (decl),
5906 current_template_parms,
5907 current_template_constraints ());
5908 /* We don't need to create a new TEMPLATE_DECL; just use the
5909 one we already had. */
5910 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5911 }
5912 else
5913 {
5914 tmpl = build_template_decl (decl, current_template_parms,
5915 member_template_p);
5916 new_template_p = true;
5917
5918 if (DECL_LANG_SPECIFIC (decl)
5919 && DECL_TEMPLATE_SPECIALIZATION (decl))
5920 {
5921 /* A specialization of a member template of a template
5922 class. */
5923 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5924 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5925 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5926 }
5927 }
5928 }
5929 else
5930 {
5931 tree a, t, current, parms;
5932 int i;
5933 tree tinfo = get_template_info (decl);
5934
5935 if (!tinfo)
5936 {
5937 error ("template definition of non-template %q#D", decl);
5938 return error_mark_node;
5939 }
5940
5941 tmpl = TI_TEMPLATE (tinfo);
5942
5943 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5944 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5945 && DECL_TEMPLATE_SPECIALIZATION (decl)
5946 && DECL_MEMBER_TEMPLATE_P (tmpl))
5947 {
5948 /* The declaration is a specialization of a member
5949 template, declared outside the class. Therefore, the
5950 innermost template arguments will be NULL, so we
5951 replace them with the arguments determined by the
5952 earlier call to check_explicit_specialization. */
5953 args = DECL_TI_ARGS (decl);
5954
5955 tree new_tmpl
5956 = build_template_decl (decl, current_template_parms,
5957 member_template_p);
5958 DECL_TI_TEMPLATE (decl) = new_tmpl;
5959 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5960 DECL_TEMPLATE_INFO (new_tmpl)
5961 = build_template_info (tmpl, args);
5962
5963 register_specialization (new_tmpl,
5964 most_general_template (tmpl),
5965 args,
5966 is_friend, 0);
5967 return decl;
5968 }
5969
5970 /* Make sure the template headers we got make sense. */
5971
5972 parms = DECL_TEMPLATE_PARMS (tmpl);
5973 i = TMPL_PARMS_DEPTH (parms);
5974 if (TMPL_ARGS_DEPTH (args) != i)
5975 {
5976 error ("expected %d levels of template parms for %q#D, got %d",
5977 i, decl, TMPL_ARGS_DEPTH (args));
5978 DECL_INTERFACE_KNOWN (decl) = 1;
5979 return error_mark_node;
5980 }
5981 else
5982 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5983 {
5984 a = TMPL_ARGS_LEVEL (args, i);
5985 t = INNERMOST_TEMPLATE_PARMS (parms);
5986
5987 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5988 {
5989 if (current == decl)
5990 error ("got %d template parameters for %q#D",
5991 TREE_VEC_LENGTH (a), decl);
5992 else
5993 error ("got %d template parameters for %q#T",
5994 TREE_VEC_LENGTH (a), current);
5995 error (" but %d required", TREE_VEC_LENGTH (t));
5996 /* Avoid crash in import_export_decl. */
5997 DECL_INTERFACE_KNOWN (decl) = 1;
5998 return error_mark_node;
5999 }
6000
6001 if (current == decl)
6002 current = ctx;
6003 else if (current == NULL_TREE)
6004 /* Can happen in erroneous input. */
6005 break;
6006 else
6007 current = get_containing_scope (current);
6008 }
6009
6010 /* Check that the parms are used in the appropriate qualifying scopes
6011 in the declarator. */
6012 if (!comp_template_args
6013 (TI_ARGS (tinfo),
6014 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6015 {
6016 error ("template arguments to %qD do not match original "
6017 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6018 if (!uses_template_parms (TI_ARGS (tinfo)))
6019 inform (input_location, "use %<template<>%> for"
6020 " an explicit specialization");
6021 /* Avoid crash in import_export_decl. */
6022 DECL_INTERFACE_KNOWN (decl) = 1;
6023 return error_mark_node;
6024 }
6025 }
6026
6027 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6028
6029 if (new_template_p)
6030 {
6031 /* Push template declarations for global functions and types.
6032 Note that we do not try to push a global template friend
6033 declared in a template class; such a thing may well depend on
6034 the template parameters of the class and we'll push it when
6035 instantiating the befriending class. */
6036 if (!ctx
6037 && !(is_friend && template_class_depth (current_class_type) > 0))
6038 {
6039 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6040 if (pushed == error_mark_node)
6041 return error_mark_node;
6042
6043 /* pushdecl may have found an existing template. */
6044 if (pushed != tmpl)
6045 {
6046 decl = DECL_TEMPLATE_RESULT (pushed);
6047 tmpl = NULL_TREE;
6048 }
6049 }
6050 }
6051 else if (tmpl)
6052 /* The type may have been completed, or (erroneously) changed. */
6053 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6054
6055 if (tmpl)
6056 {
6057 if (is_primary)
6058 {
6059 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6060
6061 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6062
6063 /* Give template template parms a DECL_CONTEXT of the template
6064 for which they are a parameter. */
6065 parms = INNERMOST_TEMPLATE_PARMS (parms);
6066 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6067 {
6068 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6069 if (TREE_CODE (parm) == TEMPLATE_DECL)
6070 DECL_CONTEXT (parm) = tmpl;
6071 }
6072
6073 if (TREE_CODE (decl) == TYPE_DECL
6074 && TYPE_DECL_ALIAS_P (decl))
6075 {
6076 if (tree constr
6077 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6078 {
6079 /* ??? Why don't we do this here for all templates? */
6080 constr = build_constraints (constr, NULL_TREE);
6081 set_constraints (decl, constr);
6082 }
6083 if (complex_alias_template_p (tmpl))
6084 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6085 }
6086 }
6087
6088 /* The DECL_TI_ARGS of DECL contains full set of arguments
6089 referring wback to its most general template. If TMPL is a
6090 specialization, ARGS may only have the innermost set of
6091 arguments. Add the missing argument levels if necessary. */
6092 if (DECL_TEMPLATE_INFO (tmpl))
6093 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6094
6095 tree info = build_template_info (tmpl, args);
6096
6097 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6098 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6099 else
6100 {
6101 retrofit_lang_decl (decl);
6102 DECL_TEMPLATE_INFO (decl) = info;
6103 }
6104 }
6105
6106 if (flag_implicit_templates
6107 && !is_friend
6108 && TREE_PUBLIC (decl)
6109 && VAR_OR_FUNCTION_DECL_P (decl))
6110 /* Set DECL_COMDAT on template instantiations; if we force
6111 them to be emitted by explicit instantiation,
6112 mark_needed will tell cgraph to do the right thing. */
6113 DECL_COMDAT (decl) = true;
6114
6115 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6116
6117 return decl;
6118 }
6119
6120 /* FN is an inheriting constructor that inherits from the constructor
6121 template INHERITED; turn FN into a constructor template with a matching
6122 template header. */
6123
6124 tree
6125 add_inherited_template_parms (tree fn, tree inherited)
6126 {
6127 tree inner_parms
6128 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6129 inner_parms = copy_node (inner_parms);
6130 tree parms
6131 = tree_cons (size_int (processing_template_decl + 1),
6132 inner_parms, current_template_parms);
6133 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6134 tree args = template_parms_to_args (parms);
6135 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6136 DECL_ARTIFICIAL (tmpl) = true;
6137 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6138 return tmpl;
6139 }
6140
6141 /* Called when a class template TYPE is redeclared with the indicated
6142 template PARMS, e.g.:
6143
6144 template <class T> struct S;
6145 template <class T> struct S {}; */
6146
6147 bool
6148 redeclare_class_template (tree type, tree parms, tree cons)
6149 {
6150 tree tmpl;
6151 tree tmpl_parms;
6152 int i;
6153
6154 if (!TYPE_TEMPLATE_INFO (type))
6155 {
6156 error ("%qT is not a template type", type);
6157 return false;
6158 }
6159
6160 tmpl = TYPE_TI_TEMPLATE (type);
6161 if (!PRIMARY_TEMPLATE_P (tmpl))
6162 /* The type is nested in some template class. Nothing to worry
6163 about here; there are no new template parameters for the nested
6164 type. */
6165 return true;
6166
6167 if (!parms)
6168 {
6169 error ("template specifiers not specified in declaration of %qD",
6170 tmpl);
6171 return false;
6172 }
6173
6174 parms = INNERMOST_TEMPLATE_PARMS (parms);
6175 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6176
6177 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6178 {
6179 error_n (input_location, TREE_VEC_LENGTH (parms),
6180 "redeclared with %d template parameter",
6181 "redeclared with %d template parameters",
6182 TREE_VEC_LENGTH (parms));
6183 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6184 "previous declaration %qD used %d template parameter",
6185 "previous declaration %qD used %d template parameters",
6186 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6187 return false;
6188 }
6189
6190 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6191 {
6192 tree tmpl_parm;
6193 tree parm;
6194 tree tmpl_default;
6195 tree parm_default;
6196
6197 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6198 || TREE_VEC_ELT (parms, i) == error_mark_node)
6199 continue;
6200
6201 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6202 if (error_operand_p (tmpl_parm))
6203 return false;
6204
6205 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6206 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6207 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6208
6209 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6210 TEMPLATE_DECL. */
6211 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6212 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6213 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6214 || (TREE_CODE (tmpl_parm) != PARM_DECL
6215 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6216 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6217 || (TREE_CODE (tmpl_parm) == PARM_DECL
6218 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6219 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6220 {
6221 auto_diagnostic_group d;
6222 error ("template parameter %q+#D", tmpl_parm);
6223 inform (input_location, "redeclared here as %q#D", parm);
6224 return false;
6225 }
6226
6227 /* The parameters can be declared to introduce different
6228 constraints. */
6229 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6230 tree p2 = TREE_VEC_ELT (parms, i);
6231 if (!template_parameter_constraints_equivalent_p (p1, p2))
6232 {
6233 auto_diagnostic_group d;
6234 error ("declaration of template parameter %q+#D with different "
6235 "constraints", parm);
6236 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6237 "original declaration appeared here");
6238 return false;
6239 }
6240
6241 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6242 {
6243 /* We have in [temp.param]:
6244
6245 A template-parameter may not be given default arguments
6246 by two different declarations in the same scope. */
6247 auto_diagnostic_group d;
6248 error_at (input_location, "redefinition of default argument for %q#D", parm);
6249 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6250 "original definition appeared here");
6251 return false;
6252 }
6253
6254 if (parm_default != NULL_TREE)
6255 /* Update the previous template parameters (which are the ones
6256 that will really count) with the new default value. */
6257 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6258 else if (tmpl_default != NULL_TREE)
6259 /* Update the new parameters, too; they'll be used as the
6260 parameters for any members. */
6261 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6262
6263 /* Give each template template parm in this redeclaration a
6264 DECL_CONTEXT of the template for which they are a parameter. */
6265 if (TREE_CODE (parm) == TEMPLATE_DECL)
6266 {
6267 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6268 DECL_CONTEXT (parm) = tmpl;
6269 }
6270
6271 if (TREE_CODE (parm) == TYPE_DECL)
6272 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6273 }
6274
6275 tree ci = get_constraints (tmpl);
6276 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6277 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6278
6279 /* Two classes with different constraints declare different entities. */
6280 if (!cp_tree_equal (req1, req2))
6281 {
6282 auto_diagnostic_group d;
6283 error_at (input_location, "redeclaration %q#D with different "
6284 "constraints", tmpl);
6285 inform (DECL_SOURCE_LOCATION (tmpl),
6286 "original declaration appeared here");
6287 return false;
6288 }
6289
6290 return true;
6291 }
6292
6293 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6294 to be used when the caller has already checked
6295 (processing_template_decl
6296 && !instantiation_dependent_expression_p (expr)
6297 && potential_constant_expression (expr))
6298 and cleared processing_template_decl. */
6299
6300 tree
6301 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6302 {
6303 return tsubst_copy_and_build (expr,
6304 /*args=*/NULL_TREE,
6305 complain,
6306 /*in_decl=*/NULL_TREE,
6307 /*function_p=*/false,
6308 /*integral_constant_expression_p=*/true);
6309 }
6310
6311 /* Simplify EXPR if it is a non-dependent expression. Returns the
6312 (possibly simplified) expression. */
6313
6314 tree
6315 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6316 {
6317 if (expr == NULL_TREE)
6318 return NULL_TREE;
6319
6320 /* If we're in a template, but EXPR isn't value dependent, simplify
6321 it. We're supposed to treat:
6322
6323 template <typename T> void f(T[1 + 1]);
6324 template <typename T> void f(T[2]);
6325
6326 as two declarations of the same function, for example. */
6327 if (processing_template_decl
6328 && is_nondependent_constant_expression (expr))
6329 {
6330 processing_template_decl_sentinel s;
6331 expr = instantiate_non_dependent_expr_internal (expr, complain);
6332 }
6333 return expr;
6334 }
6335
6336 tree
6337 instantiate_non_dependent_expr (tree expr)
6338 {
6339 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6340 }
6341
6342 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6343 an uninstantiated expression. */
6344
6345 tree
6346 instantiate_non_dependent_or_null (tree expr)
6347 {
6348 if (expr == NULL_TREE)
6349 return NULL_TREE;
6350 if (processing_template_decl)
6351 {
6352 if (!is_nondependent_constant_expression (expr))
6353 expr = NULL_TREE;
6354 else
6355 {
6356 processing_template_decl_sentinel s;
6357 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6358 }
6359 }
6360 return expr;
6361 }
6362
6363 /* True iff T is a specialization of a variable template. */
6364
6365 bool
6366 variable_template_specialization_p (tree t)
6367 {
6368 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6369 return false;
6370 tree tmpl = DECL_TI_TEMPLATE (t);
6371 return variable_template_p (tmpl);
6372 }
6373
6374 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6375 template declaration, or a TYPE_DECL for an alias declaration. */
6376
6377 bool
6378 alias_type_or_template_p (tree t)
6379 {
6380 if (t == NULL_TREE)
6381 return false;
6382 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6383 || (TYPE_P (t)
6384 && TYPE_NAME (t)
6385 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6386 || DECL_ALIAS_TEMPLATE_P (t));
6387 }
6388
6389 /* If T is a specialization of an alias template, return it; otherwise return
6390 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6391
6392 tree
6393 alias_template_specialization_p (const_tree t,
6394 bool transparent_typedefs)
6395 {
6396 if (!TYPE_P (t))
6397 return NULL_TREE;
6398
6399 /* It's an alias template specialization if it's an alias and its
6400 TYPE_NAME is a specialization of a primary template. */
6401 if (typedef_variant_p (t))
6402 {
6403 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6404 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6405 return CONST_CAST_TREE (t);
6406 if (transparent_typedefs)
6407 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6408 (TYPE_NAME (t)),
6409 transparent_typedefs);
6410 }
6411
6412 return NULL_TREE;
6413 }
6414
6415 /* An alias template is complex from a SFINAE perspective if a template-id
6416 using that alias can be ill-formed when the expansion is not, as with
6417 the void_t template. We determine this by checking whether the
6418 expansion for the alias template uses all its template parameters. */
6419
6420 struct uses_all_template_parms_data
6421 {
6422 int level;
6423 bool *seen;
6424 };
6425
6426 static int
6427 uses_all_template_parms_r (tree t, void *data_)
6428 {
6429 struct uses_all_template_parms_data &data
6430 = *(struct uses_all_template_parms_data*)data_;
6431 tree idx = get_template_parm_index (t);
6432
6433 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6434 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6435 return 0;
6436 }
6437
6438 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6439
6440 static int
6441 complex_pack_expansion_r (tree t, void *data_)
6442 {
6443 /* An alias template with a pack expansion that expands a pack from the
6444 enclosing class needs to be considered complex, to avoid confusion with
6445 the same pack being used as an argument to the alias's own template
6446 parameter (91966). */
6447 if (!PACK_EXPANSION_P (t))
6448 return 0;
6449 struct uses_all_template_parms_data &data
6450 = *(struct uses_all_template_parms_data*)data_;
6451 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6452 pack = TREE_CHAIN (pack))
6453 {
6454 tree parm_pack = TREE_VALUE (pack);
6455 if (!TEMPLATE_PARM_P (parm_pack))
6456 continue;
6457 int idx, level;
6458 template_parm_level_and_index (parm_pack, &level, &idx);
6459 if (level < data.level)
6460 return 1;
6461 }
6462 return 0;
6463 }
6464
6465 static bool
6466 complex_alias_template_p (const_tree tmpl)
6467 {
6468 /* A renaming alias isn't complex. */
6469 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6470 return false;
6471
6472 /* Any other constrained alias is complex. */
6473 if (get_constraints (tmpl))
6474 return true;
6475
6476 struct uses_all_template_parms_data data;
6477 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6478 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6479 data.level = TMPL_PARMS_DEPTH (parms);
6480 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6481 data.seen = XALLOCAVEC (bool, len);
6482 for (int i = 0; i < len; ++i)
6483 data.seen[i] = false;
6484
6485 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6486 NULL, true, complex_pack_expansion_r))
6487 return true;
6488 for (int i = 0; i < len; ++i)
6489 if (!data.seen[i])
6490 return true;
6491 return false;
6492 }
6493
6494 /* If T is a specialization of a complex alias template with dependent
6495 template-arguments, return it; otherwise return NULL_TREE. If T is a
6496 typedef to such a specialization, return the specialization. */
6497
6498 tree
6499 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6500 {
6501 if (!TYPE_P (t) || !typedef_variant_p (t))
6502 return NULL_TREE;
6503
6504 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6505 if (tinfo
6506 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6507 && (any_dependent_template_arguments_p
6508 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6509 return CONST_CAST_TREE (t);
6510
6511 if (transparent_typedefs)
6512 {
6513 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6514 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6515 }
6516
6517 return NULL_TREE;
6518 }
6519
6520 /* Return the number of innermost template parameters in TMPL. */
6521
6522 static int
6523 num_innermost_template_parms (const_tree tmpl)
6524 {
6525 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6526 return TREE_VEC_LENGTH (parms);
6527 }
6528
6529 /* Return either TMPL or another template that it is equivalent to under DR
6530 1286: An alias that just changes the name of a template is equivalent to
6531 the other template. */
6532
6533 static tree
6534 get_underlying_template (tree tmpl)
6535 {
6536 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6537 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6538 {
6539 /* Determine if the alias is equivalent to an underlying template. */
6540 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6541 /* The underlying type may have been ill-formed. Don't proceed. */
6542 if (!orig_type)
6543 break;
6544 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6545 if (!tinfo)
6546 break;
6547
6548 tree underlying = TI_TEMPLATE (tinfo);
6549 if (!PRIMARY_TEMPLATE_P (underlying)
6550 || (num_innermost_template_parms (tmpl)
6551 != num_innermost_template_parms (underlying)))
6552 break;
6553
6554 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6555 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6556 break;
6557
6558 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6559 it's appropriate to treat a less-constrained alias as equivalent. */
6560 if (!at_least_as_constrained (underlying, tmpl))
6561 break;
6562
6563 /* Alias is equivalent. Strip it and repeat. */
6564 tmpl = underlying;
6565 }
6566
6567 return tmpl;
6568 }
6569
6570 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6571 must be a reference-to-function or a pointer-to-function type, as specified
6572 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6573 and check that the resulting function has external linkage. */
6574
6575 static tree
6576 convert_nontype_argument_function (tree type, tree expr,
6577 tsubst_flags_t complain)
6578 {
6579 tree fns = expr;
6580 tree fn, fn_no_ptr;
6581 linkage_kind linkage;
6582
6583 fn = instantiate_type (type, fns, tf_none);
6584 if (fn == error_mark_node)
6585 return error_mark_node;
6586
6587 if (value_dependent_expression_p (fn))
6588 goto accept;
6589
6590 fn_no_ptr = strip_fnptr_conv (fn);
6591 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6592 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6593 if (BASELINK_P (fn_no_ptr))
6594 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6595
6596 /* [temp.arg.nontype]/1
6597
6598 A template-argument for a non-type, non-template template-parameter
6599 shall be one of:
6600 [...]
6601 -- the address of an object or function with external [C++11: or
6602 internal] linkage. */
6603
6604 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6605 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6606 {
6607 if (complain & tf_error)
6608 {
6609 location_t loc = cp_expr_loc_or_input_loc (expr);
6610 error_at (loc, "%qE is not a valid template argument for type %qT",
6611 expr, type);
6612 if (TYPE_PTR_P (type))
6613 inform (loc, "it must be the address of a function "
6614 "with external linkage");
6615 else
6616 inform (loc, "it must be the name of a function with "
6617 "external linkage");
6618 }
6619 return NULL_TREE;
6620 }
6621
6622 linkage = decl_linkage (fn_no_ptr);
6623 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6624 {
6625 if (complain & tf_error)
6626 {
6627 location_t loc = cp_expr_loc_or_input_loc (expr);
6628 if (cxx_dialect >= cxx11)
6629 error_at (loc, "%qE is not a valid template argument for type "
6630 "%qT because %qD has no linkage",
6631 expr, type, fn_no_ptr);
6632 else
6633 error_at (loc, "%qE is not a valid template argument for type "
6634 "%qT because %qD does not have external linkage",
6635 expr, type, fn_no_ptr);
6636 }
6637 return NULL_TREE;
6638 }
6639
6640 accept:
6641 if (TYPE_REF_P (type))
6642 {
6643 if (REFERENCE_REF_P (fn))
6644 fn = TREE_OPERAND (fn, 0);
6645 else
6646 fn = build_address (fn);
6647 }
6648 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6649 fn = build_nop (type, fn);
6650
6651 return fn;
6652 }
6653
6654 /* Subroutine of convert_nontype_argument.
6655 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6656 Emit an error otherwise. */
6657
6658 static bool
6659 check_valid_ptrmem_cst_expr (tree type, tree expr,
6660 tsubst_flags_t complain)
6661 {
6662 tree orig_expr = expr;
6663 STRIP_NOPS (expr);
6664 if (null_ptr_cst_p (expr))
6665 return true;
6666 if (TREE_CODE (expr) == PTRMEM_CST
6667 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6668 PTRMEM_CST_CLASS (expr)))
6669 return true;
6670 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6671 return true;
6672 if (processing_template_decl
6673 && TREE_CODE (expr) == ADDR_EXPR
6674 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6675 return true;
6676 if (complain & tf_error)
6677 {
6678 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6679 error_at (loc, "%qE is not a valid template argument for type %qT",
6680 orig_expr, type);
6681 if (TREE_CODE (expr) != PTRMEM_CST)
6682 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6683 else
6684 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6685 }
6686 return false;
6687 }
6688
6689 /* Returns TRUE iff the address of OP is value-dependent.
6690
6691 14.6.2.4 [temp.dep.temp]:
6692 A non-integral non-type template-argument is dependent if its type is
6693 dependent or it has either of the following forms
6694 qualified-id
6695 & qualified-id
6696 and contains a nested-name-specifier which specifies a class-name that
6697 names a dependent type.
6698
6699 We generalize this to just say that the address of a member of a
6700 dependent class is value-dependent; the above doesn't cover the
6701 address of a static data member named with an unqualified-id. */
6702
6703 static bool
6704 has_value_dependent_address (tree op)
6705 {
6706 STRIP_ANY_LOCATION_WRAPPER (op);
6707
6708 /* We could use get_inner_reference here, but there's no need;
6709 this is only relevant for template non-type arguments, which
6710 can only be expressed as &id-expression. */
6711 if (DECL_P (op))
6712 {
6713 tree ctx = CP_DECL_CONTEXT (op);
6714 if (TYPE_P (ctx) && dependent_type_p (ctx))
6715 return true;
6716 }
6717
6718 return false;
6719 }
6720
6721 /* The next set of functions are used for providing helpful explanatory
6722 diagnostics for failed overload resolution. Their messages should be
6723 indented by two spaces for consistency with the messages in
6724 call.c */
6725
6726 static int
6727 unify_success (bool /*explain_p*/)
6728 {
6729 return 0;
6730 }
6731
6732 /* Other failure functions should call this one, to provide a single function
6733 for setting a breakpoint on. */
6734
6735 static int
6736 unify_invalid (bool /*explain_p*/)
6737 {
6738 return 1;
6739 }
6740
6741 static int
6742 unify_parameter_deduction_failure (bool explain_p, tree parm)
6743 {
6744 if (explain_p)
6745 inform (input_location,
6746 " couldn%'t deduce template parameter %qD", parm);
6747 return unify_invalid (explain_p);
6748 }
6749
6750 static int
6751 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6752 {
6753 if (explain_p)
6754 inform (input_location,
6755 " types %qT and %qT have incompatible cv-qualifiers",
6756 parm, arg);
6757 return unify_invalid (explain_p);
6758 }
6759
6760 static int
6761 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6762 {
6763 if (explain_p)
6764 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6765 return unify_invalid (explain_p);
6766 }
6767
6768 static int
6769 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6770 {
6771 if (explain_p)
6772 inform (input_location,
6773 " template parameter %qD is not a parameter pack, but "
6774 "argument %qD is",
6775 parm, arg);
6776 return unify_invalid (explain_p);
6777 }
6778
6779 static int
6780 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6781 {
6782 if (explain_p)
6783 inform (input_location,
6784 " template argument %qE does not match "
6785 "pointer-to-member constant %qE",
6786 arg, parm);
6787 return unify_invalid (explain_p);
6788 }
6789
6790 static int
6791 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6792 {
6793 if (explain_p)
6794 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6795 return unify_invalid (explain_p);
6796 }
6797
6798 static int
6799 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6800 {
6801 if (explain_p)
6802 inform (input_location,
6803 " inconsistent parameter pack deduction with %qT and %qT",
6804 old_arg, new_arg);
6805 return unify_invalid (explain_p);
6806 }
6807
6808 static int
6809 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6810 {
6811 if (explain_p)
6812 {
6813 if (TYPE_P (parm))
6814 inform (input_location,
6815 " deduced conflicting types for parameter %qT (%qT and %qT)",
6816 parm, first, second);
6817 else
6818 inform (input_location,
6819 " deduced conflicting values for non-type parameter "
6820 "%qE (%qE and %qE)", parm, first, second);
6821 }
6822 return unify_invalid (explain_p);
6823 }
6824
6825 static int
6826 unify_vla_arg (bool explain_p, tree arg)
6827 {
6828 if (explain_p)
6829 inform (input_location,
6830 " variable-sized array type %qT is not "
6831 "a valid template argument",
6832 arg);
6833 return unify_invalid (explain_p);
6834 }
6835
6836 static int
6837 unify_method_type_error (bool explain_p, tree arg)
6838 {
6839 if (explain_p)
6840 inform (input_location,
6841 " member function type %qT is not a valid template argument",
6842 arg);
6843 return unify_invalid (explain_p);
6844 }
6845
6846 static int
6847 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6848 {
6849 if (explain_p)
6850 {
6851 if (least_p)
6852 inform_n (input_location, wanted,
6853 " candidate expects at least %d argument, %d provided",
6854 " candidate expects at least %d arguments, %d provided",
6855 wanted, have);
6856 else
6857 inform_n (input_location, wanted,
6858 " candidate expects %d argument, %d provided",
6859 " candidate expects %d arguments, %d provided",
6860 wanted, have);
6861 }
6862 return unify_invalid (explain_p);
6863 }
6864
6865 static int
6866 unify_too_many_arguments (bool explain_p, int have, int wanted)
6867 {
6868 return unify_arity (explain_p, have, wanted);
6869 }
6870
6871 static int
6872 unify_too_few_arguments (bool explain_p, int have, int wanted,
6873 bool least_p = false)
6874 {
6875 return unify_arity (explain_p, have, wanted, least_p);
6876 }
6877
6878 static int
6879 unify_arg_conversion (bool explain_p, tree to_type,
6880 tree from_type, tree arg)
6881 {
6882 if (explain_p)
6883 inform (cp_expr_loc_or_input_loc (arg),
6884 " cannot convert %qE (type %qT) to type %qT",
6885 arg, from_type, to_type);
6886 return unify_invalid (explain_p);
6887 }
6888
6889 static int
6890 unify_no_common_base (bool explain_p, enum template_base_result r,
6891 tree parm, tree arg)
6892 {
6893 if (explain_p)
6894 switch (r)
6895 {
6896 case tbr_ambiguous_baseclass:
6897 inform (input_location, " %qT is an ambiguous base class of %qT",
6898 parm, arg);
6899 break;
6900 default:
6901 inform (input_location, " %qT is not derived from %qT", arg, parm);
6902 break;
6903 }
6904 return unify_invalid (explain_p);
6905 }
6906
6907 static int
6908 unify_inconsistent_template_template_parameters (bool explain_p)
6909 {
6910 if (explain_p)
6911 inform (input_location,
6912 " template parameters of a template template argument are "
6913 "inconsistent with other deduced template arguments");
6914 return unify_invalid (explain_p);
6915 }
6916
6917 static int
6918 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6919 {
6920 if (explain_p)
6921 inform (input_location,
6922 " cannot deduce a template for %qT from non-template type %qT",
6923 parm, arg);
6924 return unify_invalid (explain_p);
6925 }
6926
6927 static int
6928 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6929 {
6930 if (explain_p)
6931 inform (input_location,
6932 " template argument %qE does not match %qE", arg, parm);
6933 return unify_invalid (explain_p);
6934 }
6935
6936 /* True if T is a C++20 template parameter object to store the argument for a
6937 template parameter of class type. */
6938
6939 bool
6940 template_parm_object_p (const_tree t)
6941 {
6942 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6943 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6944 }
6945
6946 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6947 argument for TYPE, points to an unsuitable object.
6948
6949 Also adjust the type of the index in C++20 array subobject references. */
6950
6951 static bool
6952 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6953 {
6954 switch (TREE_CODE (expr))
6955 {
6956 CASE_CONVERT:
6957 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6958 complain);
6959
6960 case TARGET_EXPR:
6961 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6962 complain);
6963
6964 case CONSTRUCTOR:
6965 {
6966 unsigned i; tree elt;
6967 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6968 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6969 return true;
6970 }
6971 break;
6972
6973 case ADDR_EXPR:
6974 {
6975 tree decl = TREE_OPERAND (expr, 0);
6976
6977 if (cxx_dialect >= cxx20)
6978 while (TREE_CODE (decl) == COMPONENT_REF
6979 || TREE_CODE (decl) == ARRAY_REF)
6980 {
6981 tree &op = TREE_OPERAND (decl, 1);
6982 if (TREE_CODE (decl) == ARRAY_REF
6983 && TREE_CODE (op) == INTEGER_CST)
6984 /* Canonicalize array offsets to ptrdiff_t; how they were
6985 written doesn't matter for subobject identity. */
6986 op = fold_convert (ptrdiff_type_node, op);
6987 decl = TREE_OPERAND (decl, 0);
6988 }
6989
6990 if (!VAR_P (decl))
6991 {
6992 if (complain & tf_error)
6993 error_at (cp_expr_loc_or_input_loc (expr),
6994 "%qE is not a valid template argument of type %qT "
6995 "because %qE is not a variable", expr, type, decl);
6996 return true;
6997 }
6998 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6999 {
7000 if (complain & tf_error)
7001 error_at (cp_expr_loc_or_input_loc (expr),
7002 "%qE is not a valid template argument of type %qT "
7003 "in C++98 because %qD does not have external linkage",
7004 expr, type, decl);
7005 return true;
7006 }
7007 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
7008 && decl_linkage (decl) == lk_none)
7009 {
7010 if (complain & tf_error)
7011 error_at (cp_expr_loc_or_input_loc (expr),
7012 "%qE is not a valid template argument of type %qT "
7013 "because %qD has no linkage", expr, type, decl);
7014 return true;
7015 }
7016 /* C++17: For a non-type template-parameter of reference or pointer
7017 type, the value of the constant expression shall not refer to (or
7018 for a pointer type, shall not be the address of):
7019 * a subobject (4.5),
7020 * a temporary object (15.2),
7021 * a string literal (5.13.5),
7022 * the result of a typeid expression (8.2.8), or
7023 * a predefined __func__ variable (11.4.1). */
7024 else if (DECL_ARTIFICIAL (decl))
7025 {
7026 if (complain & tf_error)
7027 error ("the address of %qD is not a valid template argument",
7028 decl);
7029 return true;
7030 }
7031 else if (cxx_dialect < cxx20
7032 && !(same_type_ignoring_top_level_qualifiers_p
7033 (strip_array_types (TREE_TYPE (type)),
7034 strip_array_types (TREE_TYPE (decl)))))
7035 {
7036 if (complain & tf_error)
7037 error ("the address of the %qT subobject of %qD is not a "
7038 "valid template argument", TREE_TYPE (type), decl);
7039 return true;
7040 }
7041 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7042 {
7043 if (complain & tf_error)
7044 error ("the address of %qD is not a valid template argument "
7045 "because it does not have static storage duration",
7046 decl);
7047 return true;
7048 }
7049 }
7050 break;
7051
7052 default:
7053 if (!INDIRECT_TYPE_P (type))
7054 /* We're only concerned about pointers and references here. */;
7055 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7056 /* Null pointer values are OK in C++11. */;
7057 else
7058 {
7059 if (VAR_P (expr))
7060 {
7061 if (complain & tf_error)
7062 error ("%qD is not a valid template argument "
7063 "because %qD is a variable, not the address of "
7064 "a variable", expr, expr);
7065 return true;
7066 }
7067 else
7068 {
7069 if (complain & tf_error)
7070 error ("%qE is not a valid template argument for %qT "
7071 "because it is not the address of a variable",
7072 expr, type);
7073 return true;
7074 }
7075 }
7076 }
7077 return false;
7078
7079 }
7080
7081 /* The template arguments corresponding to template parameter objects of types
7082 that contain pointers to members. */
7083
7084 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7085
7086 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7087 template argument EXPR. */
7088
7089 static tree
7090 get_template_parm_object (tree expr, tsubst_flags_t complain)
7091 {
7092 if (TREE_CODE (expr) == TARGET_EXPR)
7093 expr = TARGET_EXPR_INITIAL (expr);
7094
7095 if (!TREE_CONSTANT (expr))
7096 {
7097 if ((complain & tf_error)
7098 && require_rvalue_constant_expression (expr))
7099 cxx_constant_value (expr);
7100 return error_mark_node;
7101 }
7102 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7103 return error_mark_node;
7104
7105 tree name = mangle_template_parm_object (expr);
7106 tree decl = get_global_binding (name);
7107 if (decl)
7108 return decl;
7109
7110 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7111 decl = create_temporary_var (type);
7112 DECL_CONTEXT (decl) = NULL_TREE;
7113 TREE_STATIC (decl) = true;
7114 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7115 TREE_READONLY (decl) = true;
7116 DECL_NAME (decl) = name;
7117 SET_DECL_ASSEMBLER_NAME (decl, name);
7118 comdat_linkage (decl);
7119
7120 if (!zero_init_p (type))
7121 {
7122 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7123 lower_var_init before we're done mangling. So store the original
7124 value elsewhere. */
7125 tree copy = unshare_constructor (expr);
7126 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7127 }
7128
7129 pushdecl_top_level_and_finish (decl, expr);
7130
7131 return decl;
7132 }
7133
7134 /* Return the actual template argument corresponding to template parameter
7135 object VAR. */
7136
7137 tree
7138 tparm_object_argument (tree var)
7139 {
7140 if (zero_init_p (TREE_TYPE (var)))
7141 return DECL_INITIAL (var);
7142 return *(tparm_obj_values->get (var));
7143 }
7144
7145 /* Attempt to convert the non-type template parameter EXPR to the
7146 indicated TYPE. If the conversion is successful, return the
7147 converted value. If the conversion is unsuccessful, return
7148 NULL_TREE if we issued an error message, or error_mark_node if we
7149 did not. We issue error messages for out-and-out bad template
7150 parameters, but not simply because the conversion failed, since we
7151 might be just trying to do argument deduction. Both TYPE and EXPR
7152 must be non-dependent.
7153
7154 The conversion follows the special rules described in
7155 [temp.arg.nontype], and it is much more strict than an implicit
7156 conversion.
7157
7158 This function is called twice for each template argument (see
7159 lookup_template_class for a more accurate description of this
7160 problem). This means that we need to handle expressions which
7161 are not valid in a C++ source, but can be created from the
7162 first call (for instance, casts to perform conversions). These
7163 hacks can go away after we fix the double coercion problem. */
7164
7165 static tree
7166 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7167 {
7168 tree expr_type;
7169 location_t loc = cp_expr_loc_or_input_loc (expr);
7170
7171 /* Detect immediately string literals as invalid non-type argument.
7172 This special-case is not needed for correctness (we would easily
7173 catch this later), but only to provide better diagnostic for this
7174 common user mistake. As suggested by DR 100, we do not mention
7175 linkage issues in the diagnostic as this is not the point. */
7176 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7177 {
7178 if (complain & tf_error)
7179 error ("%qE is not a valid template argument for type %qT "
7180 "because string literals can never be used in this context",
7181 expr, type);
7182 return NULL_TREE;
7183 }
7184
7185 /* Add the ADDR_EXPR now for the benefit of
7186 value_dependent_expression_p. */
7187 if (TYPE_PTROBV_P (type)
7188 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7189 {
7190 expr = decay_conversion (expr, complain);
7191 if (expr == error_mark_node)
7192 return error_mark_node;
7193 }
7194
7195 /* If we are in a template, EXPR may be non-dependent, but still
7196 have a syntactic, rather than semantic, form. For example, EXPR
7197 might be a SCOPE_REF, rather than the VAR_DECL to which the
7198 SCOPE_REF refers. Preserving the qualifying scope is necessary
7199 so that access checking can be performed when the template is
7200 instantiated -- but here we need the resolved form so that we can
7201 convert the argument. */
7202 bool non_dep = false;
7203 if (TYPE_REF_OBJ_P (type)
7204 && has_value_dependent_address (expr))
7205 /* If we want the address and it's value-dependent, don't fold. */;
7206 else if (processing_template_decl
7207 && is_nondependent_constant_expression (expr))
7208 non_dep = true;
7209 if (error_operand_p (expr))
7210 return error_mark_node;
7211 expr_type = TREE_TYPE (expr);
7212
7213 /* If the argument is non-dependent, perform any conversions in
7214 non-dependent context as well. */
7215 processing_template_decl_sentinel s (non_dep);
7216 if (non_dep)
7217 expr = instantiate_non_dependent_expr_internal (expr, complain);
7218
7219 const bool val_dep_p = value_dependent_expression_p (expr);
7220 if (val_dep_p)
7221 expr = canonicalize_expr_argument (expr, complain);
7222
7223 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7224 to a non-type argument of "nullptr". */
7225 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7226 expr = fold_simple (convert (type, expr));
7227
7228 /* In C++11, integral or enumeration non-type template arguments can be
7229 arbitrary constant expressions. Pointer and pointer to
7230 member arguments can be general constant expressions that evaluate
7231 to a null value, but otherwise still need to be of a specific form. */
7232 if (cxx_dialect >= cxx11)
7233 {
7234 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7235 /* A PTRMEM_CST is already constant, and a valid template
7236 argument for a parameter of pointer to member type, we just want
7237 to leave it in that form rather than lower it to a
7238 CONSTRUCTOR. */;
7239 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7240 || cxx_dialect >= cxx17)
7241 {
7242 /* C++17: A template-argument for a non-type template-parameter shall
7243 be a converted constant expression (8.20) of the type of the
7244 template-parameter. */
7245 expr = build_converted_constant_expr (type, expr, complain);
7246 if (expr == error_mark_node)
7247 /* Make sure we return NULL_TREE only if we have really issued
7248 an error, as described above. */
7249 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7250 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7251 {
7252 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7253 return expr;
7254 }
7255 expr = maybe_constant_value (expr, NULL_TREE,
7256 /*manifestly_const_eval=*/true);
7257 expr = convert_from_reference (expr);
7258 }
7259 else if (TYPE_PTR_OR_PTRMEM_P (type))
7260 {
7261 tree folded = maybe_constant_value (expr, NULL_TREE,
7262 /*manifestly_const_eval=*/true);
7263 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7264 : null_member_pointer_value_p (folded))
7265 expr = folded;
7266 }
7267 }
7268
7269 if (TYPE_REF_P (type))
7270 expr = mark_lvalue_use (expr);
7271 else
7272 expr = mark_rvalue_use (expr);
7273
7274 /* HACK: Due to double coercion, we can get a
7275 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7276 which is the tree that we built on the first call (see
7277 below when coercing to reference to object or to reference to
7278 function). We just strip everything and get to the arg.
7279 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7280 for examples. */
7281 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7282 {
7283 tree probe_type, probe = expr;
7284 if (REFERENCE_REF_P (probe))
7285 probe = TREE_OPERAND (probe, 0);
7286 probe_type = TREE_TYPE (probe);
7287 if (TREE_CODE (probe) == NOP_EXPR)
7288 {
7289 /* ??? Maybe we could use convert_from_reference here, but we
7290 would need to relax its constraints because the NOP_EXPR
7291 could actually change the type to something more cv-qualified,
7292 and this is not folded by convert_from_reference. */
7293 tree addr = TREE_OPERAND (probe, 0);
7294 if (TYPE_REF_P (probe_type)
7295 && TREE_CODE (addr) == ADDR_EXPR
7296 && TYPE_PTR_P (TREE_TYPE (addr))
7297 && (same_type_ignoring_top_level_qualifiers_p
7298 (TREE_TYPE (probe_type),
7299 TREE_TYPE (TREE_TYPE (addr)))))
7300 {
7301 expr = TREE_OPERAND (addr, 0);
7302 expr_type = TREE_TYPE (probe_type);
7303 }
7304 }
7305 }
7306
7307 /* [temp.arg.nontype]/5, bullet 1
7308
7309 For a non-type template-parameter of integral or enumeration type,
7310 integral promotions (_conv.prom_) and integral conversions
7311 (_conv.integral_) are applied. */
7312 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7313 || TREE_CODE (type) == REAL_TYPE)
7314 {
7315 if (cxx_dialect < cxx11)
7316 {
7317 tree t = build_converted_constant_expr (type, expr, complain);
7318 t = maybe_constant_value (t);
7319 if (t != error_mark_node)
7320 expr = t;
7321 }
7322
7323 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7324 return error_mark_node;
7325
7326 /* Notice that there are constant expressions like '4 % 0' which
7327 do not fold into integer constants. */
7328 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7329 {
7330 if (complain & tf_error)
7331 {
7332 int errs = errorcount, warns = warningcount + werrorcount;
7333 if (!require_potential_constant_expression (expr))
7334 expr = error_mark_node;
7335 else
7336 expr = cxx_constant_value (expr);
7337 if (errorcount > errs || warningcount + werrorcount > warns)
7338 inform (loc, "in template argument for type %qT", type);
7339 if (expr == error_mark_node)
7340 return NULL_TREE;
7341 /* else cxx_constant_value complained but gave us
7342 a real constant, so go ahead. */
7343 if (!CONSTANT_CLASS_P (expr))
7344 {
7345 /* Some assemble time constant expressions like
7346 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7347 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7348 as we can emit them into .rodata initializers of
7349 variables, yet they can't fold into an INTEGER_CST at
7350 compile time. Refuse them here. */
7351 gcc_checking_assert (reduced_constant_expression_p (expr));
7352 error_at (loc, "template argument %qE for type %qT not "
7353 "a compile-time constant", expr, type);
7354 return NULL_TREE;
7355 }
7356 }
7357 else
7358 return NULL_TREE;
7359 }
7360
7361 /* Avoid typedef problems. */
7362 if (TREE_TYPE (expr) != type)
7363 expr = fold_convert (type, expr);
7364 }
7365 /* [temp.arg.nontype]/5, bullet 2
7366
7367 For a non-type template-parameter of type pointer to object,
7368 qualification conversions (_conv.qual_) and the array-to-pointer
7369 conversion (_conv.array_) are applied. */
7370 else if (TYPE_PTROBV_P (type))
7371 {
7372 tree decayed = expr;
7373
7374 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7375 decay_conversion or an explicit cast. If it's a problematic cast,
7376 we'll complain about it below. */
7377 if (TREE_CODE (expr) == NOP_EXPR)
7378 {
7379 tree probe = expr;
7380 STRIP_NOPS (probe);
7381 if (TREE_CODE (probe) == ADDR_EXPR
7382 && TYPE_PTR_P (TREE_TYPE (probe)))
7383 {
7384 expr = probe;
7385 expr_type = TREE_TYPE (expr);
7386 }
7387 }
7388
7389 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7390
7391 A template-argument for a non-type, non-template template-parameter
7392 shall be one of: [...]
7393
7394 -- the name of a non-type template-parameter;
7395 -- the address of an object or function with external linkage, [...]
7396 expressed as "& id-expression" where the & is optional if the name
7397 refers to a function or array, or if the corresponding
7398 template-parameter is a reference.
7399
7400 Here, we do not care about functions, as they are invalid anyway
7401 for a parameter of type pointer-to-object. */
7402
7403 if (val_dep_p)
7404 /* Non-type template parameters are OK. */
7405 ;
7406 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7407 /* Null pointer values are OK in C++11. */;
7408 else if (TREE_CODE (expr) != ADDR_EXPR
7409 && !INDIRECT_TYPE_P (expr_type))
7410 /* Other values, like integer constants, might be valid
7411 non-type arguments of some other type. */
7412 return error_mark_node;
7413 else if (invalid_tparm_referent_p (type, expr, complain))
7414 return NULL_TREE;
7415
7416 expr = decayed;
7417
7418 expr = perform_qualification_conversions (type, expr);
7419 if (expr == error_mark_node)
7420 return error_mark_node;
7421 }
7422 /* [temp.arg.nontype]/5, bullet 3
7423
7424 For a non-type template-parameter of type reference to object, no
7425 conversions apply. The type referred to by the reference may be more
7426 cv-qualified than the (otherwise identical) type of the
7427 template-argument. The template-parameter is bound directly to the
7428 template-argument, which must be an lvalue. */
7429 else if (TYPE_REF_OBJ_P (type))
7430 {
7431 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7432 expr_type))
7433 return error_mark_node;
7434
7435 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7436 {
7437 if (complain & tf_error)
7438 error ("%qE is not a valid template argument for type %qT "
7439 "because of conflicts in cv-qualification", expr, type);
7440 return NULL_TREE;
7441 }
7442
7443 if (!lvalue_p (expr))
7444 {
7445 if (complain & tf_error)
7446 error ("%qE is not a valid template argument for type %qT "
7447 "because it is not an lvalue", expr, type);
7448 return NULL_TREE;
7449 }
7450
7451 /* [temp.arg.nontype]/1
7452
7453 A template-argument for a non-type, non-template template-parameter
7454 shall be one of: [...]
7455
7456 -- the address of an object or function with external linkage. */
7457 if (INDIRECT_REF_P (expr)
7458 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7459 {
7460 expr = TREE_OPERAND (expr, 0);
7461 if (DECL_P (expr))
7462 {
7463 if (complain & tf_error)
7464 error ("%q#D is not a valid template argument for type %qT "
7465 "because a reference variable does not have a constant "
7466 "address", expr, type);
7467 return NULL_TREE;
7468 }
7469 }
7470
7471 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7472 /* OK, dependent reference. We don't want to ask whether a DECL is
7473 itself value-dependent, since what we want here is its address. */;
7474 else
7475 {
7476 expr = build_address (expr);
7477
7478 if (invalid_tparm_referent_p (type, expr, complain))
7479 return NULL_TREE;
7480 }
7481
7482 if (!same_type_p (type, TREE_TYPE (expr)))
7483 expr = build_nop (type, expr);
7484 }
7485 /* [temp.arg.nontype]/5, bullet 4
7486
7487 For a non-type template-parameter of type pointer to function, only
7488 the function-to-pointer conversion (_conv.func_) is applied. If the
7489 template-argument represents a set of overloaded functions (or a
7490 pointer to such), the matching function is selected from the set
7491 (_over.over_). */
7492 else if (TYPE_PTRFN_P (type))
7493 {
7494 /* If the argument is a template-id, we might not have enough
7495 context information to decay the pointer. */
7496 if (!type_unknown_p (expr_type))
7497 {
7498 expr = decay_conversion (expr, complain);
7499 if (expr == error_mark_node)
7500 return error_mark_node;
7501 }
7502
7503 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7504 /* Null pointer values are OK in C++11. */
7505 return perform_qualification_conversions (type, expr);
7506
7507 expr = convert_nontype_argument_function (type, expr, complain);
7508 if (!expr || expr == error_mark_node)
7509 return expr;
7510 }
7511 /* [temp.arg.nontype]/5, bullet 5
7512
7513 For a non-type template-parameter of type reference to function, no
7514 conversions apply. If the template-argument represents a set of
7515 overloaded functions, the matching function is selected from the set
7516 (_over.over_). */
7517 else if (TYPE_REFFN_P (type))
7518 {
7519 if (TREE_CODE (expr) == ADDR_EXPR)
7520 {
7521 if (complain & tf_error)
7522 {
7523 error ("%qE is not a valid template argument for type %qT "
7524 "because it is a pointer", expr, type);
7525 inform (input_location, "try using %qE instead",
7526 TREE_OPERAND (expr, 0));
7527 }
7528 return NULL_TREE;
7529 }
7530
7531 expr = convert_nontype_argument_function (type, expr, complain);
7532 if (!expr || expr == error_mark_node)
7533 return expr;
7534 }
7535 /* [temp.arg.nontype]/5, bullet 6
7536
7537 For a non-type template-parameter of type pointer to member function,
7538 no conversions apply. If the template-argument represents a set of
7539 overloaded member functions, the matching member function is selected
7540 from the set (_over.over_). */
7541 else if (TYPE_PTRMEMFUNC_P (type))
7542 {
7543 expr = instantiate_type (type, expr, tf_none);
7544 if (expr == error_mark_node)
7545 return error_mark_node;
7546
7547 /* [temp.arg.nontype] bullet 1 says the pointer to member
7548 expression must be a pointer-to-member constant. */
7549 if (!val_dep_p
7550 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7551 return NULL_TREE;
7552
7553 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7554 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7555 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7556 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7557 }
7558 /* [temp.arg.nontype]/5, bullet 7
7559
7560 For a non-type template-parameter of type pointer to data member,
7561 qualification conversions (_conv.qual_) are applied. */
7562 else if (TYPE_PTRDATAMEM_P (type))
7563 {
7564 /* [temp.arg.nontype] bullet 1 says the pointer to member
7565 expression must be a pointer-to-member constant. */
7566 if (!val_dep_p
7567 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7568 return NULL_TREE;
7569
7570 expr = perform_qualification_conversions (type, expr);
7571 if (expr == error_mark_node)
7572 return expr;
7573 }
7574 else if (NULLPTR_TYPE_P (type))
7575 {
7576 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7577 {
7578 if (complain & tf_error)
7579 error ("%qE is not a valid template argument for type %qT "
7580 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7581 return NULL_TREE;
7582 }
7583 return expr;
7584 }
7585 else if (CLASS_TYPE_P (type))
7586 {
7587 /* Replace the argument with a reference to the corresponding template
7588 parameter object. */
7589 if (!val_dep_p)
7590 expr = get_template_parm_object (expr, complain);
7591 if (expr == error_mark_node)
7592 return NULL_TREE;
7593 }
7594 /* A template non-type parameter must be one of the above. */
7595 else
7596 gcc_unreachable ();
7597
7598 /* Sanity check: did we actually convert the argument to the
7599 right type? */
7600 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7601 (type, TREE_TYPE (expr)));
7602 return convert_from_reference (expr);
7603 }
7604
7605 /* Subroutine of coerce_template_template_parms, which returns 1 if
7606 PARM_PARM and ARG_PARM match using the rule for the template
7607 parameters of template template parameters. Both PARM and ARG are
7608 template parameters; the rest of the arguments are the same as for
7609 coerce_template_template_parms.
7610 */
7611 static int
7612 coerce_template_template_parm (tree parm,
7613 tree arg,
7614 tsubst_flags_t complain,
7615 tree in_decl,
7616 tree outer_args)
7617 {
7618 if (arg == NULL_TREE || error_operand_p (arg)
7619 || parm == NULL_TREE || error_operand_p (parm))
7620 return 0;
7621
7622 if (TREE_CODE (arg) != TREE_CODE (parm))
7623 return 0;
7624
7625 switch (TREE_CODE (parm))
7626 {
7627 case TEMPLATE_DECL:
7628 /* We encounter instantiations of templates like
7629 template <template <template <class> class> class TT>
7630 class C; */
7631 {
7632 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7633 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7634
7635 if (!coerce_template_template_parms
7636 (parmparm, argparm, complain, in_decl, outer_args))
7637 return 0;
7638 }
7639 /* Fall through. */
7640
7641 case TYPE_DECL:
7642 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7643 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7644 /* Argument is a parameter pack but parameter is not. */
7645 return 0;
7646 break;
7647
7648 case PARM_DECL:
7649 /* The tsubst call is used to handle cases such as
7650
7651 template <int> class C {};
7652 template <class T, template <T> class TT> class D {};
7653 D<int, C> d;
7654
7655 i.e. the parameter list of TT depends on earlier parameters. */
7656 if (!uses_template_parms (TREE_TYPE (arg)))
7657 {
7658 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7659 if (!uses_template_parms (t)
7660 && !same_type_p (t, TREE_TYPE (arg)))
7661 return 0;
7662 }
7663
7664 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7665 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7666 /* Argument is a parameter pack but parameter is not. */
7667 return 0;
7668
7669 break;
7670
7671 default:
7672 gcc_unreachable ();
7673 }
7674
7675 return 1;
7676 }
7677
7678 /* Coerce template argument list ARGLIST for use with template
7679 template-parameter TEMPL. */
7680
7681 static tree
7682 coerce_template_args_for_ttp (tree templ, tree arglist,
7683 tsubst_flags_t complain)
7684 {
7685 /* Consider an example where a template template parameter declared as
7686
7687 template <class T, class U = std::allocator<T> > class TT
7688
7689 The template parameter level of T and U are one level larger than
7690 of TT. To proper process the default argument of U, say when an
7691 instantiation `TT<int>' is seen, we need to build the full
7692 arguments containing {int} as the innermost level. Outer levels,
7693 available when not appearing as default template argument, can be
7694 obtained from the arguments of the enclosing template.
7695
7696 Suppose that TT is later substituted with std::vector. The above
7697 instantiation is `TT<int, std::allocator<T> >' with TT at
7698 level 1, and T at level 2, while the template arguments at level 1
7699 becomes {std::vector} and the inner level 2 is {int}. */
7700
7701 tree outer = DECL_CONTEXT (templ);
7702 if (outer)
7703 outer = generic_targs_for (outer);
7704 else if (current_template_parms)
7705 {
7706 /* This is an argument of the current template, so we haven't set
7707 DECL_CONTEXT yet. */
7708 tree relevant_template_parms;
7709
7710 /* Parameter levels that are greater than the level of the given
7711 template template parm are irrelevant. */
7712 relevant_template_parms = current_template_parms;
7713 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7714 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7715 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7716
7717 outer = template_parms_to_args (relevant_template_parms);
7718 }
7719
7720 if (outer)
7721 arglist = add_to_template_args (outer, arglist);
7722
7723 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7724 return coerce_template_parms (parmlist, arglist, templ,
7725 complain,
7726 /*require_all_args=*/true,
7727 /*use_default_args=*/true);
7728 }
7729
7730 /* A cache of template template parameters with match-all default
7731 arguments. */
7732 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7733
7734 /* T is a bound template template-parameter. Copy its arguments into default
7735 arguments of the template template-parameter's template parameters. */
7736
7737 static tree
7738 add_defaults_to_ttp (tree otmpl)
7739 {
7740 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7741 return *c;
7742
7743 tree ntmpl = copy_node (otmpl);
7744
7745 tree ntype = copy_node (TREE_TYPE (otmpl));
7746 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7747 TYPE_MAIN_VARIANT (ntype) = ntype;
7748 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7749 TYPE_NAME (ntype) = ntmpl;
7750 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7751
7752 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7753 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7754 TEMPLATE_PARM_DECL (idx) = ntmpl;
7755 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7756
7757 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7758 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7759 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7760 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7761 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7762 {
7763 tree o = TREE_VEC_ELT (vec, i);
7764 if (!template_parameter_pack_p (TREE_VALUE (o)))
7765 {
7766 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7767 TREE_PURPOSE (n) = any_targ_node;
7768 }
7769 }
7770
7771 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7772 return ntmpl;
7773 }
7774
7775 /* ARG is a bound potential template template-argument, and PARGS is a list
7776 of arguments for the corresponding template template-parameter. Adjust
7777 PARGS as appropriate for application to ARG's template, and if ARG is a
7778 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7779 arguments to the template template parameter. */
7780
7781 static tree
7782 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7783 {
7784 ++processing_template_decl;
7785 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7786 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7787 {
7788 /* When comparing two template template-parameters in partial ordering,
7789 rewrite the one currently being used as an argument to have default
7790 arguments for all parameters. */
7791 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7792 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7793 if (pargs != error_mark_node)
7794 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7795 TYPE_TI_ARGS (arg));
7796 }
7797 else
7798 {
7799 tree aparms
7800 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7801 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7802 /*require_all*/true,
7803 /*use_default*/true);
7804 }
7805 --processing_template_decl;
7806 return pargs;
7807 }
7808
7809 /* Subroutine of unify for the case when PARM is a
7810 BOUND_TEMPLATE_TEMPLATE_PARM. */
7811
7812 static int
7813 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7814 bool explain_p)
7815 {
7816 tree parmvec = TYPE_TI_ARGS (parm);
7817 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7818
7819 /* The template template parm might be variadic and the argument
7820 not, so flatten both argument lists. */
7821 parmvec = expand_template_argument_pack (parmvec);
7822 argvec = expand_template_argument_pack (argvec);
7823
7824 if (flag_new_ttp)
7825 {
7826 /* In keeping with P0522R0, adjust P's template arguments
7827 to apply to A's template; then flatten it again. */
7828 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7829 nparmvec = expand_template_argument_pack (nparmvec);
7830
7831 if (unify (tparms, targs, nparmvec, argvec,
7832 UNIFY_ALLOW_NONE, explain_p))
7833 return 1;
7834
7835 /* If the P0522 adjustment eliminated a pack expansion, deduce
7836 empty packs. */
7837 if (flag_new_ttp
7838 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7839 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7840 DEDUCE_EXACT, /*sub*/true, explain_p))
7841 return 1;
7842 }
7843 else
7844 {
7845 /* Deduce arguments T, i from TT<T> or TT<i>.
7846 We check each element of PARMVEC and ARGVEC individually
7847 rather than the whole TREE_VEC since they can have
7848 different number of elements, which is allowed under N2555. */
7849
7850 int len = TREE_VEC_LENGTH (parmvec);
7851
7852 /* Check if the parameters end in a pack, making them
7853 variadic. */
7854 int parm_variadic_p = 0;
7855 if (len > 0
7856 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7857 parm_variadic_p = 1;
7858
7859 for (int i = 0; i < len - parm_variadic_p; ++i)
7860 /* If the template argument list of P contains a pack
7861 expansion that is not the last template argument, the
7862 entire template argument list is a non-deduced
7863 context. */
7864 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7865 return unify_success (explain_p);
7866
7867 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7868 return unify_too_few_arguments (explain_p,
7869 TREE_VEC_LENGTH (argvec), len);
7870
7871 for (int i = 0; i < len - parm_variadic_p; ++i)
7872 if (unify (tparms, targs,
7873 TREE_VEC_ELT (parmvec, i),
7874 TREE_VEC_ELT (argvec, i),
7875 UNIFY_ALLOW_NONE, explain_p))
7876 return 1;
7877
7878 if (parm_variadic_p
7879 && unify_pack_expansion (tparms, targs,
7880 parmvec, argvec,
7881 DEDUCE_EXACT,
7882 /*subr=*/true, explain_p))
7883 return 1;
7884 }
7885
7886 return 0;
7887 }
7888
7889 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7890 template template parameters. Both PARM_PARMS and ARG_PARMS are
7891 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7892 or PARM_DECL.
7893
7894 Consider the example:
7895 template <class T> class A;
7896 template<template <class U> class TT> class B;
7897
7898 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7899 the parameters to A, and OUTER_ARGS contains A. */
7900
7901 static int
7902 coerce_template_template_parms (tree parm_parms,
7903 tree arg_parms,
7904 tsubst_flags_t complain,
7905 tree in_decl,
7906 tree outer_args)
7907 {
7908 int nparms, nargs, i;
7909 tree parm, arg;
7910 int variadic_p = 0;
7911
7912 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7913 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7914
7915 nparms = TREE_VEC_LENGTH (parm_parms);
7916 nargs = TREE_VEC_LENGTH (arg_parms);
7917
7918 if (flag_new_ttp)
7919 {
7920 /* P0522R0: A template template-parameter P is at least as specialized as
7921 a template template-argument A if, given the following rewrite to two
7922 function templates, the function template corresponding to P is at
7923 least as specialized as the function template corresponding to A
7924 according to the partial ordering rules for function templates
7925 ([temp.func.order]). Given an invented class template X with the
7926 template parameter list of A (including default arguments):
7927
7928 * Each of the two function templates has the same template parameters,
7929 respectively, as P or A.
7930
7931 * Each function template has a single function parameter whose type is
7932 a specialization of X with template arguments corresponding to the
7933 template parameters from the respective function template where, for
7934 each template parameter PP in the template parameter list of the
7935 function template, a corresponding template argument AA is formed. If
7936 PP declares a parameter pack, then AA is the pack expansion
7937 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7938
7939 If the rewrite produces an invalid type, then P is not at least as
7940 specialized as A. */
7941
7942 /* So coerce P's args to apply to A's parms, and then deduce between A's
7943 args and the converted args. If that succeeds, A is at least as
7944 specialized as P, so they match.*/
7945 tree pargs = template_parms_level_to_args (parm_parms);
7946 pargs = add_outermost_template_args (outer_args, pargs);
7947 ++processing_template_decl;
7948 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7949 /*require_all*/true, /*use_default*/true);
7950 --processing_template_decl;
7951 if (pargs != error_mark_node)
7952 {
7953 tree targs = make_tree_vec (nargs);
7954 tree aargs = template_parms_level_to_args (arg_parms);
7955 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7956 /*explain*/false))
7957 return 1;
7958 }
7959 }
7960
7961 /* Determine whether we have a parameter pack at the end of the
7962 template template parameter's template parameter list. */
7963 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7964 {
7965 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7966
7967 if (error_operand_p (parm))
7968 return 0;
7969
7970 switch (TREE_CODE (parm))
7971 {
7972 case TEMPLATE_DECL:
7973 case TYPE_DECL:
7974 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7975 variadic_p = 1;
7976 break;
7977
7978 case PARM_DECL:
7979 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7980 variadic_p = 1;
7981 break;
7982
7983 default:
7984 gcc_unreachable ();
7985 }
7986 }
7987
7988 if (nargs != nparms
7989 && !(variadic_p && nargs >= nparms - 1))
7990 return 0;
7991
7992 /* Check all of the template parameters except the parameter pack at
7993 the end (if any). */
7994 for (i = 0; i < nparms - variadic_p; ++i)
7995 {
7996 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7997 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7998 continue;
7999
8000 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8001 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8002
8003 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8004 outer_args))
8005 return 0;
8006
8007 }
8008
8009 if (variadic_p)
8010 {
8011 /* Check each of the template parameters in the template
8012 argument against the template parameter pack at the end of
8013 the template template parameter. */
8014 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8015 return 0;
8016
8017 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8018
8019 for (; i < nargs; ++i)
8020 {
8021 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8022 continue;
8023
8024 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8025
8026 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8027 outer_args))
8028 return 0;
8029 }
8030 }
8031
8032 return 1;
8033 }
8034
8035 /* Verifies that the deduced template arguments (in TARGS) for the
8036 template template parameters (in TPARMS) represent valid bindings,
8037 by comparing the template parameter list of each template argument
8038 to the template parameter list of its corresponding template
8039 template parameter, in accordance with DR150. This
8040 routine can only be called after all template arguments have been
8041 deduced. It will return TRUE if all of the template template
8042 parameter bindings are okay, FALSE otherwise. */
8043 bool
8044 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8045 {
8046 int i, ntparms = TREE_VEC_LENGTH (tparms);
8047 bool ret = true;
8048
8049 /* We're dealing with template parms in this process. */
8050 ++processing_template_decl;
8051
8052 targs = INNERMOST_TEMPLATE_ARGS (targs);
8053
8054 for (i = 0; i < ntparms; ++i)
8055 {
8056 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8057 tree targ = TREE_VEC_ELT (targs, i);
8058
8059 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8060 {
8061 tree packed_args = NULL_TREE;
8062 int idx, len = 1;
8063
8064 if (ARGUMENT_PACK_P (targ))
8065 {
8066 /* Look inside the argument pack. */
8067 packed_args = ARGUMENT_PACK_ARGS (targ);
8068 len = TREE_VEC_LENGTH (packed_args);
8069 }
8070
8071 for (idx = 0; idx < len; ++idx)
8072 {
8073 tree targ_parms = NULL_TREE;
8074
8075 if (packed_args)
8076 /* Extract the next argument from the argument
8077 pack. */
8078 targ = TREE_VEC_ELT (packed_args, idx);
8079
8080 if (PACK_EXPANSION_P (targ))
8081 /* Look at the pattern of the pack expansion. */
8082 targ = PACK_EXPANSION_PATTERN (targ);
8083
8084 /* Extract the template parameters from the template
8085 argument. */
8086 if (TREE_CODE (targ) == TEMPLATE_DECL)
8087 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8088 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8089 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8090
8091 /* Verify that we can coerce the template template
8092 parameters from the template argument to the template
8093 parameter. This requires an exact match. */
8094 if (targ_parms
8095 && !coerce_template_template_parms
8096 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8097 targ_parms,
8098 tf_none,
8099 tparm,
8100 targs))
8101 {
8102 ret = false;
8103 goto out;
8104 }
8105 }
8106 }
8107 }
8108
8109 out:
8110
8111 --processing_template_decl;
8112 return ret;
8113 }
8114
8115 /* Since type attributes aren't mangled, we need to strip them from
8116 template type arguments. */
8117
8118 tree
8119 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8120 {
8121 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8122 return arg;
8123 bool removed_attributes = false;
8124 tree canon = strip_typedefs (arg, &removed_attributes);
8125 if (removed_attributes
8126 && (complain & tf_warning))
8127 warning (OPT_Wignored_attributes,
8128 "ignoring attributes on template argument %qT", arg);
8129 return canon;
8130 }
8131
8132 /* And from inside dependent non-type arguments like sizeof(Type). */
8133
8134 static tree
8135 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8136 {
8137 if (!arg || arg == error_mark_node)
8138 return arg;
8139 bool removed_attributes = false;
8140 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8141 if (removed_attributes
8142 && (complain & tf_warning))
8143 warning (OPT_Wignored_attributes,
8144 "ignoring attributes in template argument %qE", arg);
8145 return canon;
8146 }
8147
8148 /* A template declaration can be substituted for a constrained
8149 template template parameter only when the argument is no more
8150 constrained than the parameter. */
8151
8152 static bool
8153 is_compatible_template_arg (tree parm, tree arg)
8154 {
8155 tree parm_cons = get_constraints (parm);
8156
8157 /* For now, allow constrained template template arguments
8158 and unconstrained template template parameters. */
8159 if (parm_cons == NULL_TREE)
8160 return true;
8161
8162 /* If the template parameter is constrained, we need to rewrite its
8163 constraints in terms of the ARG's template parameters. This ensures
8164 that all of the template parameter types will have the same depth.
8165
8166 Note that this is only valid when coerce_template_template_parm is
8167 true for the innermost template parameters of PARM and ARG. In other
8168 words, because coercion is successful, this conversion will be valid. */
8169 tree new_args = NULL_TREE;
8170 if (parm_cons)
8171 {
8172 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8173 new_args = template_parms_level_to_args (aparms);
8174 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8175 tf_none, NULL_TREE);
8176 if (parm_cons == error_mark_node)
8177 return false;
8178 }
8179
8180 return weakly_subsumes (parm_cons, arg);
8181 }
8182
8183 // Convert a placeholder argument into a binding to the original
8184 // parameter. The original parameter is saved as the TREE_TYPE of
8185 // ARG.
8186 static inline tree
8187 convert_wildcard_argument (tree parm, tree arg)
8188 {
8189 TREE_TYPE (arg) = parm;
8190 return arg;
8191 }
8192
8193 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8194 because one of them is dependent. But we need to represent the
8195 conversion for the benefit of cp_tree_equal. */
8196
8197 static tree
8198 maybe_convert_nontype_argument (tree type, tree arg)
8199 {
8200 /* Auto parms get no conversion. */
8201 if (type_uses_auto (type))
8202 return arg;
8203 /* We don't need or want to add this conversion now if we're going to use the
8204 argument for deduction. */
8205 if (value_dependent_expression_p (arg))
8206 return arg;
8207
8208 type = cv_unqualified (type);
8209 tree argtype = TREE_TYPE (arg);
8210 if (same_type_p (type, argtype))
8211 return arg;
8212
8213 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8214 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8215 return arg;
8216 }
8217
8218 /* Convert the indicated template ARG as necessary to match the
8219 indicated template PARM. Returns the converted ARG, or
8220 error_mark_node if the conversion was unsuccessful. Error and
8221 warning messages are issued under control of COMPLAIN. This
8222 conversion is for the Ith parameter in the parameter list. ARGS is
8223 the full set of template arguments deduced so far. */
8224
8225 static tree
8226 convert_template_argument (tree parm,
8227 tree arg,
8228 tree args,
8229 tsubst_flags_t complain,
8230 int i,
8231 tree in_decl)
8232 {
8233 tree orig_arg;
8234 tree val;
8235 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8236
8237 if (parm == error_mark_node || error_operand_p (arg))
8238 return error_mark_node;
8239
8240 /* Trivially convert placeholders. */
8241 if (TREE_CODE (arg) == WILDCARD_DECL)
8242 return convert_wildcard_argument (parm, arg);
8243
8244 if (arg == any_targ_node)
8245 return arg;
8246
8247 if (TREE_CODE (arg) == TREE_LIST
8248 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8249 {
8250 /* The template argument was the name of some
8251 member function. That's usually
8252 invalid, but static members are OK. In any
8253 case, grab the underlying fields/functions
8254 and issue an error later if required. */
8255 TREE_TYPE (arg) = unknown_type_node;
8256 }
8257
8258 orig_arg = arg;
8259
8260 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8261 requires_type = (TREE_CODE (parm) == TYPE_DECL
8262 || requires_tmpl_type);
8263
8264 /* When determining whether an argument pack expansion is a template,
8265 look at the pattern. */
8266 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8267 arg = PACK_EXPANSION_PATTERN (arg);
8268
8269 /* Deal with an injected-class-name used as a template template arg. */
8270 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8271 {
8272 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8273 if (TREE_CODE (t) == TEMPLATE_DECL)
8274 {
8275 if (cxx_dialect >= cxx11)
8276 /* OK under DR 1004. */;
8277 else if (complain & tf_warning_or_error)
8278 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8279 " used as template template argument", TYPE_NAME (arg));
8280 else if (flag_pedantic_errors)
8281 t = arg;
8282
8283 arg = t;
8284 }
8285 }
8286
8287 is_tmpl_type =
8288 ((TREE_CODE (arg) == TEMPLATE_DECL
8289 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8290 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8291 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8292 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8293
8294 if (is_tmpl_type
8295 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8296 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8297 arg = TYPE_STUB_DECL (arg);
8298
8299 is_type = TYPE_P (arg) || is_tmpl_type;
8300
8301 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8302 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8303 {
8304 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8305 {
8306 if (complain & tf_error)
8307 error ("invalid use of destructor %qE as a type", orig_arg);
8308 return error_mark_node;
8309 }
8310
8311 permerror (input_location,
8312 "to refer to a type member of a template parameter, "
8313 "use %<typename %E%>", orig_arg);
8314
8315 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8316 TREE_OPERAND (arg, 1),
8317 typename_type,
8318 complain);
8319 arg = orig_arg;
8320 is_type = 1;
8321 }
8322 if (is_type != requires_type)
8323 {
8324 if (in_decl)
8325 {
8326 if (complain & tf_error)
8327 {
8328 error ("type/value mismatch at argument %d in template "
8329 "parameter list for %qD",
8330 i + 1, in_decl);
8331 if (is_type)
8332 {
8333 /* The template argument is a type, but we're expecting
8334 an expression. */
8335 inform (input_location,
8336 " expected a constant of type %qT, got %qT",
8337 TREE_TYPE (parm),
8338 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8339 /* [temp.arg]/2: "In a template-argument, an ambiguity
8340 between a type-id and an expression is resolved to a
8341 type-id, regardless of the form of the corresponding
8342 template-parameter." So give the user a clue. */
8343 if (TREE_CODE (arg) == FUNCTION_TYPE)
8344 inform (input_location, " ambiguous template argument "
8345 "for non-type template parameter is treated as "
8346 "function type");
8347 }
8348 else if (requires_tmpl_type)
8349 inform (input_location,
8350 " expected a class template, got %qE", orig_arg);
8351 else
8352 inform (input_location,
8353 " expected a type, got %qE", orig_arg);
8354 }
8355 }
8356 return error_mark_node;
8357 }
8358 if (is_tmpl_type ^ requires_tmpl_type)
8359 {
8360 if (in_decl && (complain & tf_error))
8361 {
8362 error ("type/value mismatch at argument %d in template "
8363 "parameter list for %qD",
8364 i + 1, in_decl);
8365 if (is_tmpl_type)
8366 inform (input_location,
8367 " expected a type, got %qT", DECL_NAME (arg));
8368 else
8369 inform (input_location,
8370 " expected a class template, got %qT", orig_arg);
8371 }
8372 return error_mark_node;
8373 }
8374
8375 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8376 /* We already did the appropriate conversion when packing args. */
8377 val = orig_arg;
8378 else if (is_type)
8379 {
8380 if (requires_tmpl_type)
8381 {
8382 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8383 /* The number of argument required is not known yet.
8384 Just accept it for now. */
8385 val = orig_arg;
8386 else
8387 {
8388 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8389 tree argparm;
8390
8391 /* Strip alias templates that are equivalent to another
8392 template. */
8393 arg = get_underlying_template (arg);
8394 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8395
8396 if (coerce_template_template_parms (parmparm, argparm,
8397 complain, in_decl,
8398 args))
8399 {
8400 val = arg;
8401
8402 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8403 TEMPLATE_DECL. */
8404 if (val != error_mark_node)
8405 {
8406 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8407 val = TREE_TYPE (val);
8408 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8409 val = make_pack_expansion (val, complain);
8410 }
8411 }
8412 else
8413 {
8414 if (in_decl && (complain & tf_error))
8415 {
8416 error ("type/value mismatch at argument %d in "
8417 "template parameter list for %qD",
8418 i + 1, in_decl);
8419 inform (input_location,
8420 " expected a template of type %qD, got %qT",
8421 parm, orig_arg);
8422 }
8423
8424 val = error_mark_node;
8425 }
8426
8427 // Check that the constraints are compatible before allowing the
8428 // substitution.
8429 if (val != error_mark_node)
8430 if (!is_compatible_template_arg (parm, arg))
8431 {
8432 if (in_decl && (complain & tf_error))
8433 {
8434 error ("constraint mismatch at argument %d in "
8435 "template parameter list for %qD",
8436 i + 1, in_decl);
8437 inform (input_location, " expected %qD but got %qD",
8438 parm, arg);
8439 }
8440 val = error_mark_node;
8441 }
8442 }
8443 }
8444 else
8445 val = orig_arg;
8446 /* We only form one instance of each template specialization.
8447 Therefore, if we use a non-canonical variant (i.e., a
8448 typedef), any future messages referring to the type will use
8449 the typedef, which is confusing if those future uses do not
8450 themselves also use the typedef. */
8451 if (TYPE_P (val))
8452 val = canonicalize_type_argument (val, complain);
8453 }
8454 else
8455 {
8456 tree t = TREE_TYPE (parm);
8457
8458 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8459 > TMPL_ARGS_DEPTH (args))
8460 /* We don't have enough levels of args to do any substitution. This
8461 can happen in the context of -fnew-ttp-matching. */;
8462 else if (tree a = type_uses_auto (t))
8463 {
8464 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8465 if (t == error_mark_node)
8466 return error_mark_node;
8467 }
8468 else
8469 t = tsubst (t, args, complain, in_decl);
8470
8471 if (invalid_nontype_parm_type_p (t, complain))
8472 return error_mark_node;
8473
8474 if (t != TREE_TYPE (parm))
8475 t = canonicalize_type_argument (t, complain);
8476
8477 if (!type_dependent_expression_p (orig_arg)
8478 && !uses_template_parms (t))
8479 /* We used to call digest_init here. However, digest_init
8480 will report errors, which we don't want when complain
8481 is zero. More importantly, digest_init will try too
8482 hard to convert things: for example, `0' should not be
8483 converted to pointer type at this point according to
8484 the standard. Accepting this is not merely an
8485 extension, since deciding whether or not these
8486 conversions can occur is part of determining which
8487 function template to call, or whether a given explicit
8488 argument specification is valid. */
8489 val = convert_nontype_argument (t, orig_arg, complain);
8490 else
8491 {
8492 val = canonicalize_expr_argument (orig_arg, complain);
8493 val = maybe_convert_nontype_argument (t, val);
8494 }
8495
8496
8497 if (val == NULL_TREE)
8498 val = error_mark_node;
8499 else if (val == error_mark_node && (complain & tf_error))
8500 error_at (cp_expr_loc_or_input_loc (orig_arg),
8501 "could not convert template argument %qE from %qT to %qT",
8502 orig_arg, TREE_TYPE (orig_arg), t);
8503
8504 if (INDIRECT_REF_P (val))
8505 {
8506 /* Reject template arguments that are references to built-in
8507 functions with no library fallbacks. */
8508 const_tree inner = TREE_OPERAND (val, 0);
8509 const_tree innertype = TREE_TYPE (inner);
8510 if (innertype
8511 && TYPE_REF_P (innertype)
8512 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8513 && TREE_OPERAND_LENGTH (inner) > 0
8514 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8515 return error_mark_node;
8516 }
8517
8518 if (TREE_CODE (val) == SCOPE_REF)
8519 {
8520 /* Strip typedefs from the SCOPE_REF. */
8521 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8522 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8523 complain);
8524 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8525 QUALIFIED_NAME_IS_TEMPLATE (val));
8526 }
8527 }
8528
8529 return val;
8530 }
8531
8532 /* Coerces the remaining template arguments in INNER_ARGS (from
8533 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8534 Returns the coerced argument pack. PARM_IDX is the position of this
8535 parameter in the template parameter list. ARGS is the original
8536 template argument list. */
8537 static tree
8538 coerce_template_parameter_pack (tree parms,
8539 int parm_idx,
8540 tree args,
8541 tree inner_args,
8542 int arg_idx,
8543 tree new_args,
8544 int* lost,
8545 tree in_decl,
8546 tsubst_flags_t complain)
8547 {
8548 tree parm = TREE_VEC_ELT (parms, parm_idx);
8549 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8550 tree packed_args;
8551 tree argument_pack;
8552 tree packed_parms = NULL_TREE;
8553
8554 if (arg_idx > nargs)
8555 arg_idx = nargs;
8556
8557 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8558 {
8559 /* When the template parameter is a non-type template parameter pack
8560 or template template parameter pack whose type or template
8561 parameters use parameter packs, we know exactly how many arguments
8562 we are looking for. Build a vector of the instantiated decls for
8563 these template parameters in PACKED_PARMS. */
8564 /* We can't use make_pack_expansion here because it would interpret a
8565 _DECL as a use rather than a declaration. */
8566 tree decl = TREE_VALUE (parm);
8567 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8568 SET_PACK_EXPANSION_PATTERN (exp, decl);
8569 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8570 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8571
8572 TREE_VEC_LENGTH (args)--;
8573 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8574 TREE_VEC_LENGTH (args)++;
8575
8576 if (packed_parms == error_mark_node)
8577 return error_mark_node;
8578
8579 /* If we're doing a partial instantiation of a member template,
8580 verify that all of the types used for the non-type
8581 template parameter pack are, in fact, valid for non-type
8582 template parameters. */
8583 if (arg_idx < nargs
8584 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8585 {
8586 int j, len = TREE_VEC_LENGTH (packed_parms);
8587 for (j = 0; j < len; ++j)
8588 {
8589 tree t = TREE_VEC_ELT (packed_parms, j);
8590 if (TREE_CODE (t) == PARM_DECL
8591 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8592 return error_mark_node;
8593 }
8594 /* We don't know how many args we have yet, just
8595 use the unconverted ones for now. */
8596 return NULL_TREE;
8597 }
8598
8599 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8600 }
8601 /* Check if we have a placeholder pack, which indicates we're
8602 in the context of a introduction list. In that case we want
8603 to match this pack to the single placeholder. */
8604 else if (arg_idx < nargs
8605 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8606 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8607 {
8608 nargs = arg_idx + 1;
8609 packed_args = make_tree_vec (1);
8610 }
8611 else
8612 packed_args = make_tree_vec (nargs - arg_idx);
8613
8614 /* Convert the remaining arguments, which will be a part of the
8615 parameter pack "parm". */
8616 int first_pack_arg = arg_idx;
8617 for (; arg_idx < nargs; ++arg_idx)
8618 {
8619 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8620 tree actual_parm = TREE_VALUE (parm);
8621 int pack_idx = arg_idx - first_pack_arg;
8622
8623 if (packed_parms)
8624 {
8625 /* Once we've packed as many args as we have types, stop. */
8626 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8627 break;
8628 else if (PACK_EXPANSION_P (arg))
8629 /* We don't know how many args we have yet, just
8630 use the unconverted ones for now. */
8631 return NULL_TREE;
8632 else
8633 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8634 }
8635
8636 if (arg == error_mark_node)
8637 {
8638 if (complain & tf_error)
8639 error ("template argument %d is invalid", arg_idx + 1);
8640 }
8641 else
8642 arg = convert_template_argument (actual_parm,
8643 arg, new_args, complain, parm_idx,
8644 in_decl);
8645 if (arg == error_mark_node)
8646 (*lost)++;
8647 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8648 }
8649
8650 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8651 && TREE_VEC_LENGTH (packed_args) > 0)
8652 {
8653 if (complain & tf_error)
8654 error ("wrong number of template arguments (%d, should be %d)",
8655 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8656 return error_mark_node;
8657 }
8658
8659 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8660 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8661 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8662 else
8663 {
8664 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8665 TREE_CONSTANT (argument_pack) = 1;
8666 }
8667
8668 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8669 if (CHECKING_P)
8670 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8671 TREE_VEC_LENGTH (packed_args));
8672 return argument_pack;
8673 }
8674
8675 /* Returns the number of pack expansions in the template argument vector
8676 ARGS. */
8677
8678 static int
8679 pack_expansion_args_count (tree args)
8680 {
8681 int i;
8682 int count = 0;
8683 if (args)
8684 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8685 {
8686 tree elt = TREE_VEC_ELT (args, i);
8687 if (elt && PACK_EXPANSION_P (elt))
8688 ++count;
8689 }
8690 return count;
8691 }
8692
8693 /* Convert all template arguments to their appropriate types, and
8694 return a vector containing the innermost resulting template
8695 arguments. If any error occurs, return error_mark_node. Error and
8696 warning messages are issued under control of COMPLAIN.
8697
8698 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8699 for arguments not specified in ARGS. Otherwise, if
8700 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8701 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8702 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8703 ARGS. */
8704
8705 static tree
8706 coerce_template_parms (tree parms,
8707 tree args,
8708 tree in_decl,
8709 tsubst_flags_t complain,
8710 bool require_all_args,
8711 bool use_default_args)
8712 {
8713 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8714 tree orig_inner_args;
8715 tree inner_args;
8716 tree new_args;
8717 tree new_inner_args;
8718
8719 /* When used as a boolean value, indicates whether this is a
8720 variadic template parameter list. Since it's an int, we can also
8721 subtract it from nparms to get the number of non-variadic
8722 parameters. */
8723 int variadic_p = 0;
8724 int variadic_args_p = 0;
8725 int post_variadic_parms = 0;
8726
8727 /* Adjustment to nparms for fixed parameter packs. */
8728 int fixed_pack_adjust = 0;
8729 int fixed_packs = 0;
8730 int missing = 0;
8731
8732 /* Likewise for parameters with default arguments. */
8733 int default_p = 0;
8734
8735 if (args == error_mark_node)
8736 return error_mark_node;
8737
8738 nparms = TREE_VEC_LENGTH (parms);
8739
8740 /* Determine if there are any parameter packs or default arguments. */
8741 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8742 {
8743 tree parm = TREE_VEC_ELT (parms, parm_idx);
8744 if (variadic_p)
8745 ++post_variadic_parms;
8746 if (template_parameter_pack_p (TREE_VALUE (parm)))
8747 ++variadic_p;
8748 if (TREE_PURPOSE (parm))
8749 ++default_p;
8750 }
8751
8752 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8753 /* If there are no parameters that follow a parameter pack, we need to
8754 expand any argument packs so that we can deduce a parameter pack from
8755 some non-packed args followed by an argument pack, as in variadic85.C.
8756 If there are such parameters, we need to leave argument packs intact
8757 so the arguments are assigned properly. This can happen when dealing
8758 with a nested class inside a partial specialization of a class
8759 template, as in variadic92.C, or when deducing a template parameter pack
8760 from a sub-declarator, as in variadic114.C. */
8761 if (!post_variadic_parms)
8762 inner_args = expand_template_argument_pack (inner_args);
8763
8764 /* Count any pack expansion args. */
8765 variadic_args_p = pack_expansion_args_count (inner_args);
8766
8767 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8768 if ((nargs - variadic_args_p > nparms && !variadic_p)
8769 || (nargs < nparms - variadic_p
8770 && require_all_args
8771 && !variadic_args_p
8772 && (!use_default_args
8773 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8774 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8775 {
8776 bad_nargs:
8777 if (complain & tf_error)
8778 {
8779 if (variadic_p || default_p)
8780 {
8781 nparms -= variadic_p + default_p;
8782 error ("wrong number of template arguments "
8783 "(%d, should be at least %d)", nargs, nparms);
8784 }
8785 else
8786 error ("wrong number of template arguments "
8787 "(%d, should be %d)", nargs, nparms);
8788
8789 if (in_decl)
8790 inform (DECL_SOURCE_LOCATION (in_decl),
8791 "provided for %qD", in_decl);
8792 }
8793
8794 return error_mark_node;
8795 }
8796 /* We can't pass a pack expansion to a non-pack parameter of an alias
8797 template (DR 1430). */
8798 else if (in_decl
8799 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8800 || concept_definition_p (in_decl))
8801 && variadic_args_p
8802 && nargs - variadic_args_p < nparms - variadic_p)
8803 {
8804 if (complain & tf_error)
8805 {
8806 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8807 {
8808 tree arg = TREE_VEC_ELT (inner_args, i);
8809 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8810
8811 if (PACK_EXPANSION_P (arg)
8812 && !template_parameter_pack_p (parm))
8813 {
8814 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8815 error_at (location_of (arg),
8816 "pack expansion argument for non-pack parameter "
8817 "%qD of alias template %qD", parm, in_decl);
8818 else
8819 error_at (location_of (arg),
8820 "pack expansion argument for non-pack parameter "
8821 "%qD of concept %qD", parm, in_decl);
8822 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8823 goto found;
8824 }
8825 }
8826 gcc_unreachable ();
8827 found:;
8828 }
8829 return error_mark_node;
8830 }
8831
8832 /* We need to evaluate the template arguments, even though this
8833 template-id may be nested within a "sizeof". */
8834 cp_evaluated ev;
8835
8836 new_inner_args = make_tree_vec (nparms);
8837 new_args = add_outermost_template_args (args, new_inner_args);
8838 int pack_adjust = 0;
8839 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8840 {
8841 tree arg;
8842 tree parm;
8843
8844 /* Get the Ith template parameter. */
8845 parm = TREE_VEC_ELT (parms, parm_idx);
8846
8847 if (parm == error_mark_node)
8848 {
8849 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8850 continue;
8851 }
8852
8853 /* Calculate the next argument. */
8854 if (arg_idx < nargs)
8855 arg = TREE_VEC_ELT (inner_args, arg_idx);
8856 else
8857 arg = NULL_TREE;
8858
8859 if (template_parameter_pack_p (TREE_VALUE (parm))
8860 && (arg || require_all_args || !(complain & tf_partial))
8861 && !(arg && ARGUMENT_PACK_P (arg)))
8862 {
8863 /* Some arguments will be placed in the
8864 template parameter pack PARM. */
8865 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8866 inner_args, arg_idx,
8867 new_args, &lost,
8868 in_decl, complain);
8869
8870 if (arg == NULL_TREE)
8871 {
8872 /* We don't know how many args we have yet, just use the
8873 unconverted (and still packed) ones for now. */
8874 new_inner_args = orig_inner_args;
8875 arg_idx = nargs;
8876 break;
8877 }
8878
8879 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8880
8881 /* Store this argument. */
8882 if (arg == error_mark_node)
8883 {
8884 lost++;
8885 /* We are done with all of the arguments. */
8886 arg_idx = nargs;
8887 break;
8888 }
8889 else
8890 {
8891 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8892 arg_idx += pack_adjust;
8893 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8894 {
8895 ++fixed_packs;
8896 fixed_pack_adjust += pack_adjust;
8897 }
8898 }
8899
8900 continue;
8901 }
8902 else if (arg)
8903 {
8904 if (PACK_EXPANSION_P (arg))
8905 {
8906 /* "If every valid specialization of a variadic template
8907 requires an empty template parameter pack, the template is
8908 ill-formed, no diagnostic required." So check that the
8909 pattern works with this parameter. */
8910 tree pattern = PACK_EXPANSION_PATTERN (arg);
8911 tree conv = convert_template_argument (TREE_VALUE (parm),
8912 pattern, new_args,
8913 complain, parm_idx,
8914 in_decl);
8915 if (conv == error_mark_node)
8916 {
8917 if (complain & tf_error)
8918 inform (input_location, "so any instantiation with a "
8919 "non-empty parameter pack would be ill-formed");
8920 ++lost;
8921 }
8922 else if (TYPE_P (conv) && !TYPE_P (pattern))
8923 /* Recover from missing typename. */
8924 TREE_VEC_ELT (inner_args, arg_idx)
8925 = make_pack_expansion (conv, complain);
8926
8927 /* We don't know how many args we have yet, just
8928 use the unconverted ones for now. */
8929 new_inner_args = inner_args;
8930 arg_idx = nargs;
8931 break;
8932 }
8933 }
8934 else if (require_all_args)
8935 {
8936 /* There must be a default arg in this case. */
8937 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8938 complain, in_decl);
8939 /* The position of the first default template argument,
8940 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8941 Record that. */
8942 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8943 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8944 arg_idx - pack_adjust);
8945 }
8946 else
8947 break;
8948
8949 if (arg == error_mark_node)
8950 {
8951 if (complain & tf_error)
8952 error ("template argument %d is invalid", arg_idx + 1);
8953 }
8954 else if (!arg)
8955 {
8956 /* This can occur if there was an error in the template
8957 parameter list itself (which we would already have
8958 reported) that we are trying to recover from, e.g., a class
8959 template with a parameter list such as
8960 template<typename..., typename> (cpp0x/variadic150.C). */
8961 ++lost;
8962
8963 /* This can also happen with a fixed parameter pack (71834). */
8964 if (arg_idx >= nargs)
8965 ++missing;
8966 }
8967 else
8968 arg = convert_template_argument (TREE_VALUE (parm),
8969 arg, new_args, complain,
8970 parm_idx, in_decl);
8971
8972 if (arg == error_mark_node)
8973 lost++;
8974
8975 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8976 }
8977
8978 if (missing || arg_idx < nargs - variadic_args_p)
8979 {
8980 /* If we had fixed parameter packs, we didn't know how many arguments we
8981 actually needed earlier; now we do. */
8982 nparms += fixed_pack_adjust;
8983 variadic_p -= fixed_packs;
8984 goto bad_nargs;
8985 }
8986
8987 if (arg_idx < nargs)
8988 {
8989 /* We had some pack expansion arguments that will only work if the packs
8990 are empty, but wait until instantiation time to complain.
8991 See variadic-ttp3.C. */
8992
8993 /* Except that we can't provide empty packs to alias templates or
8994 concepts when there are no corresponding parameters. Basically,
8995 we can get here with this:
8996
8997 template<typename T> concept C = true;
8998
8999 template<typename... Args>
9000 requires C<Args...>
9001 void f();
9002
9003 When parsing C<Args...>, we try to form a concept check of
9004 C<?, Args...>. Without the extra check for substituting an empty
9005 pack past the last parameter, we can accept the check as valid.
9006
9007 FIXME: This may be valid for alias templates (but I doubt it).
9008
9009 FIXME: The error could be better also. */
9010 if (in_decl && concept_definition_p (in_decl))
9011 {
9012 if (complain & tf_error)
9013 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
9014 "too many arguments");
9015 return error_mark_node;
9016 }
9017
9018 int len = nparms + (nargs - arg_idx);
9019 tree args = make_tree_vec (len);
9020 int i = 0;
9021 for (; i < nparms; ++i)
9022 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9023 for (; i < len; ++i, ++arg_idx)
9024 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9025 arg_idx - pack_adjust);
9026 new_inner_args = args;
9027 }
9028
9029 if (lost)
9030 {
9031 gcc_assert (!(complain & tf_error) || seen_error ());
9032 return error_mark_node;
9033 }
9034
9035 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9036 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9037 TREE_VEC_LENGTH (new_inner_args));
9038
9039 return new_inner_args;
9040 }
9041
9042 /* Convert all template arguments to their appropriate types, and
9043 return a vector containing the innermost resulting template
9044 arguments. If any error occurs, return error_mark_node. Error and
9045 warning messages are not issued.
9046
9047 Note that no function argument deduction is performed, and default
9048 arguments are used to fill in unspecified arguments. */
9049 tree
9050 coerce_template_parms (tree parms, tree args, tree in_decl)
9051 {
9052 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9053 }
9054
9055 /* Convert all template arguments to their appropriate type, and
9056 instantiate default arguments as needed. This returns a vector
9057 containing the innermost resulting template arguments, or
9058 error_mark_node if unsuccessful. */
9059 tree
9060 coerce_template_parms (tree parms, tree args, tree in_decl,
9061 tsubst_flags_t complain)
9062 {
9063 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9064 }
9065
9066 /* Like coerce_template_parms. If PARMS represents all template
9067 parameters levels, this function returns a vector of vectors
9068 representing all the resulting argument levels. Note that in this
9069 case, only the innermost arguments are coerced because the
9070 outermost ones are supposed to have been coerced already.
9071
9072 Otherwise, if PARMS represents only (the innermost) vector of
9073 parameters, this function returns a vector containing just the
9074 innermost resulting arguments. */
9075
9076 static tree
9077 coerce_innermost_template_parms (tree parms,
9078 tree args,
9079 tree in_decl,
9080 tsubst_flags_t complain,
9081 bool require_all_args,
9082 bool use_default_args)
9083 {
9084 int parms_depth = TMPL_PARMS_DEPTH (parms);
9085 int args_depth = TMPL_ARGS_DEPTH (args);
9086 tree coerced_args;
9087
9088 if (parms_depth > 1)
9089 {
9090 coerced_args = make_tree_vec (parms_depth);
9091 tree level;
9092 int cur_depth;
9093
9094 for (level = parms, cur_depth = parms_depth;
9095 parms_depth > 0 && level != NULL_TREE;
9096 level = TREE_CHAIN (level), --cur_depth)
9097 {
9098 tree l;
9099 if (cur_depth == args_depth)
9100 l = coerce_template_parms (TREE_VALUE (level),
9101 args, in_decl, complain,
9102 require_all_args,
9103 use_default_args);
9104 else
9105 l = TMPL_ARGS_LEVEL (args, cur_depth);
9106
9107 if (l == error_mark_node)
9108 return error_mark_node;
9109
9110 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9111 }
9112 }
9113 else
9114 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9115 args, in_decl, complain,
9116 require_all_args,
9117 use_default_args);
9118 return coerced_args;
9119 }
9120
9121 /* Returns true if T is a wrapper to make a C++20 template parameter
9122 object const. */
9123
9124 static bool
9125 class_nttp_const_wrapper_p (tree t)
9126 {
9127 if (cxx_dialect < cxx20)
9128 return false;
9129 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9130 && CP_TYPE_CONST_P (TREE_TYPE (t))
9131 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9132 }
9133
9134 /* Returns 1 if template args OT and NT are equivalent. */
9135
9136 int
9137 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9138 {
9139 if (nt == ot)
9140 return 1;
9141 if (nt == NULL_TREE || ot == NULL_TREE)
9142 return false;
9143 if (nt == any_targ_node || ot == any_targ_node)
9144 return true;
9145
9146 if (class_nttp_const_wrapper_p (nt))
9147 nt = TREE_OPERAND (nt, 0);
9148 if (class_nttp_const_wrapper_p (ot))
9149 ot = TREE_OPERAND (ot, 0);
9150
9151 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9152 /* For member templates */
9153 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9154 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9155 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9156 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9157 PACK_EXPANSION_PATTERN (nt))
9158 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9159 PACK_EXPANSION_EXTRA_ARGS (nt)));
9160 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9161 return cp_tree_equal (ot, nt);
9162 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9163 gcc_unreachable ();
9164 else if (TYPE_P (nt) || TYPE_P (ot))
9165 {
9166 if (!(TYPE_P (nt) && TYPE_P (ot)))
9167 return false;
9168 /* Don't treat an alias template specialization with dependent
9169 arguments as equivalent to its underlying type when used as a
9170 template argument; we need them to be distinct so that we
9171 substitute into the specialization arguments at instantiation
9172 time. And aliases can't be equivalent without being ==, so
9173 we don't need to look any deeper.
9174
9175 During partial ordering, however, we need to treat them normally so
9176 that we can order uses of the same alias with different
9177 cv-qualification (79960). */
9178 if (!partial_order
9179 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9180 return false;
9181 else
9182 return same_type_p (ot, nt);
9183 }
9184 else
9185 {
9186 /* Try to treat a template non-type argument that has been converted
9187 to the parameter type as equivalent to one that hasn't yet. */
9188 for (enum tree_code code1 = TREE_CODE (ot);
9189 CONVERT_EXPR_CODE_P (code1)
9190 || code1 == NON_LVALUE_EXPR;
9191 code1 = TREE_CODE (ot))
9192 ot = TREE_OPERAND (ot, 0);
9193
9194 for (enum tree_code code2 = TREE_CODE (nt);
9195 CONVERT_EXPR_CODE_P (code2)
9196 || code2 == NON_LVALUE_EXPR;
9197 code2 = TREE_CODE (nt))
9198 nt = TREE_OPERAND (nt, 0);
9199
9200 return cp_tree_equal (ot, nt);
9201 }
9202 }
9203
9204 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9205 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9206 NEWARG_PTR with the offending arguments if they are non-NULL. */
9207
9208 int
9209 comp_template_args (tree oldargs, tree newargs,
9210 tree *oldarg_ptr, tree *newarg_ptr,
9211 bool partial_order)
9212 {
9213 int i;
9214
9215 if (oldargs == newargs)
9216 return 1;
9217
9218 if (!oldargs || !newargs)
9219 return 0;
9220
9221 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9222 return 0;
9223
9224 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9225 {
9226 tree nt = TREE_VEC_ELT (newargs, i);
9227 tree ot = TREE_VEC_ELT (oldargs, i);
9228
9229 if (! template_args_equal (ot, nt, partial_order))
9230 {
9231 if (oldarg_ptr != NULL)
9232 *oldarg_ptr = ot;
9233 if (newarg_ptr != NULL)
9234 *newarg_ptr = nt;
9235 return 0;
9236 }
9237 }
9238 return 1;
9239 }
9240
9241 inline bool
9242 comp_template_args_porder (tree oargs, tree nargs)
9243 {
9244 return comp_template_args (oargs, nargs, NULL, NULL, true);
9245 }
9246
9247 /* Implement a freelist interface for objects of type T.
9248
9249 Head is a separate object, rather than a regular member, so that we
9250 can define it as a GTY deletable pointer, which is highly
9251 desirable. A data member could be declared that way, but then the
9252 containing object would implicitly get GTY((user)), which would
9253 prevent us from instantiating freelists as global objects.
9254 Although this way we can create freelist global objects, they're
9255 such thin wrappers that instantiating temporaries at every use
9256 loses nothing and saves permanent storage for the freelist object.
9257
9258 Member functions next, anew, poison and reinit have default
9259 implementations that work for most of the types we're interested
9260 in, but if they don't work for some type, they should be explicitly
9261 specialized. See the comments before them for requirements, and
9262 the example specializations for the tree_list_freelist. */
9263 template <typename T>
9264 class freelist
9265 {
9266 /* Return the next object in a chain. We could just do type
9267 punning, but if we access the object with its underlying type, we
9268 avoid strict-aliasing trouble. This needs only work between
9269 poison and reinit. */
9270 static T *&next (T *obj) { return obj->next; }
9271
9272 /* Return a newly allocated, uninitialized or minimally-initialized
9273 object of type T. Any initialization performed by anew should
9274 either remain across the life of the object and the execution of
9275 poison, or be redone by reinit. */
9276 static T *anew () { return ggc_alloc<T> (); }
9277
9278 /* Optionally scribble all over the bits holding the object, so that
9279 they become (mostly?) uninitialized memory. This is called while
9280 preparing to make the object part of the free list. */
9281 static void poison (T *obj) {
9282 T *p ATTRIBUTE_UNUSED = obj;
9283 T **q ATTRIBUTE_UNUSED = &next (obj);
9284
9285 #ifdef ENABLE_GC_CHECKING
9286 /* Poison the data, to indicate the data is garbage. */
9287 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9288 memset (p, 0xa5, sizeof (*p));
9289 #endif
9290 /* Let valgrind know the object is free. */
9291 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9292
9293 /* Let valgrind know the next portion of the object is available,
9294 but uninitialized. */
9295 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9296 }
9297
9298 /* Bring an object that underwent at least one lifecycle after anew
9299 and before the most recent free and poison, back to a usable
9300 state, reinitializing whatever is needed for it to be
9301 functionally equivalent to an object just allocated and returned
9302 by anew. This may poison or clear the next field, used by
9303 freelist housekeeping after poison was called. */
9304 static void reinit (T *obj) {
9305 T **q ATTRIBUTE_UNUSED = &next (obj);
9306
9307 #ifdef ENABLE_GC_CHECKING
9308 memset (q, 0xa5, sizeof (*q));
9309 #endif
9310 /* Let valgrind know the entire object is available, but
9311 uninitialized. */
9312 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9313 }
9314
9315 /* Reference a GTY-deletable pointer that points to the first object
9316 in the free list proper. */
9317 T *&head;
9318 public:
9319 /* Construct a freelist object chaining objects off of HEAD. */
9320 freelist (T *&head) : head(head) {}
9321
9322 /* Add OBJ to the free object list. The former head becomes OBJ's
9323 successor. */
9324 void free (T *obj)
9325 {
9326 poison (obj);
9327 next (obj) = head;
9328 head = obj;
9329 }
9330
9331 /* Take an object from the free list, if one is available, or
9332 allocate a new one. Objects taken from the free list should be
9333 regarded as filled with garbage, except for bits that are
9334 configured to be preserved across free and alloc. */
9335 T *alloc ()
9336 {
9337 if (head)
9338 {
9339 T *obj = head;
9340 head = next (head);
9341 reinit (obj);
9342 return obj;
9343 }
9344 else
9345 return anew ();
9346 }
9347 };
9348
9349 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9350 want to allocate a TREE_LIST using the usual interface, and ensure
9351 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9352 build_tree_list logic in reinit, so this could go out of sync. */
9353 template <>
9354 inline tree &
9355 freelist<tree_node>::next (tree obj)
9356 {
9357 return TREE_CHAIN (obj);
9358 }
9359 template <>
9360 inline tree
9361 freelist<tree_node>::anew ()
9362 {
9363 return build_tree_list (NULL, NULL);
9364 }
9365 template <>
9366 inline void
9367 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9368 {
9369 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9370 tree p ATTRIBUTE_UNUSED = obj;
9371 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9372 tree *q ATTRIBUTE_UNUSED = &next (obj);
9373
9374 #ifdef ENABLE_GC_CHECKING
9375 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9376
9377 /* Poison the data, to indicate the data is garbage. */
9378 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9379 memset (p, 0xa5, size);
9380 #endif
9381 /* Let valgrind know the object is free. */
9382 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9383 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9384 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9385 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9386
9387 #ifdef ENABLE_GC_CHECKING
9388 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9389 /* Keep TREE_CHAIN functional. */
9390 TREE_SET_CODE (obj, TREE_LIST);
9391 #else
9392 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9393 #endif
9394 }
9395 template <>
9396 inline void
9397 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9398 {
9399 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9400
9401 #ifdef ENABLE_GC_CHECKING
9402 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9403 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9404 memset (obj, 0, sizeof (tree_list));
9405 #endif
9406
9407 /* Let valgrind know the entire object is available, but
9408 uninitialized. */
9409 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9410
9411 #ifdef ENABLE_GC_CHECKING
9412 TREE_SET_CODE (obj, TREE_LIST);
9413 #else
9414 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9415 #endif
9416 }
9417
9418 /* Point to the first object in the TREE_LIST freelist. */
9419 static GTY((deletable)) tree tree_list_freelist_head;
9420 /* Return the/an actual TREE_LIST freelist. */
9421 static inline freelist<tree_node>
9422 tree_list_freelist ()
9423 {
9424 return tree_list_freelist_head;
9425 }
9426
9427 /* Point to the first object in the tinst_level freelist. */
9428 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9429 /* Return the/an actual tinst_level freelist. */
9430 static inline freelist<tinst_level>
9431 tinst_level_freelist ()
9432 {
9433 return tinst_level_freelist_head;
9434 }
9435
9436 /* Point to the first object in the pending_template freelist. */
9437 static GTY((deletable)) pending_template *pending_template_freelist_head;
9438 /* Return the/an actual pending_template freelist. */
9439 static inline freelist<pending_template>
9440 pending_template_freelist ()
9441 {
9442 return pending_template_freelist_head;
9443 }
9444
9445 /* Build the TREE_LIST object out of a split list, store it
9446 permanently, and return it. */
9447 tree
9448 tinst_level::to_list ()
9449 {
9450 gcc_assert (split_list_p ());
9451 tree ret = tree_list_freelist ().alloc ();
9452 TREE_PURPOSE (ret) = tldcl;
9453 TREE_VALUE (ret) = targs;
9454 tldcl = ret;
9455 targs = NULL;
9456 gcc_assert (tree_list_p ());
9457 return ret;
9458 }
9459
9460 const unsigned short tinst_level::refcount_infinity;
9461
9462 /* Increment OBJ's refcount unless it is already infinite. */
9463 static tinst_level *
9464 inc_refcount_use (tinst_level *obj)
9465 {
9466 if (obj && obj->refcount != tinst_level::refcount_infinity)
9467 ++obj->refcount;
9468 return obj;
9469 }
9470
9471 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9472 void
9473 tinst_level::free (tinst_level *obj)
9474 {
9475 if (obj->tree_list_p ())
9476 tree_list_freelist ().free (obj->get_node ());
9477 tinst_level_freelist ().free (obj);
9478 }
9479
9480 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9481 OBJ's DECL and OBJ, and start over with the tinst_level object that
9482 used to be referenced by OBJ's NEXT. */
9483 static void
9484 dec_refcount_use (tinst_level *obj)
9485 {
9486 while (obj
9487 && obj->refcount != tinst_level::refcount_infinity
9488 && !--obj->refcount)
9489 {
9490 tinst_level *next = obj->next;
9491 tinst_level::free (obj);
9492 obj = next;
9493 }
9494 }
9495
9496 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9497 and of the former PTR. Omitting the second argument is equivalent
9498 to passing (T*)NULL; this is allowed because passing the
9499 zero-valued integral constant NULL confuses type deduction and/or
9500 overload resolution. */
9501 template <typename T>
9502 static void
9503 set_refcount_ptr (T *& ptr, T *obj = NULL)
9504 {
9505 T *save = ptr;
9506 ptr = inc_refcount_use (obj);
9507 dec_refcount_use (save);
9508 }
9509
9510 static void
9511 add_pending_template (tree d)
9512 {
9513 tree ti = (TYPE_P (d)
9514 ? CLASSTYPE_TEMPLATE_INFO (d)
9515 : DECL_TEMPLATE_INFO (d));
9516 struct pending_template *pt;
9517 int level;
9518
9519 if (TI_PENDING_TEMPLATE_FLAG (ti))
9520 return;
9521
9522 /* We are called both from instantiate_decl, where we've already had a
9523 tinst_level pushed, and instantiate_template, where we haven't.
9524 Compensate. */
9525 gcc_assert (TREE_CODE (d) != TREE_LIST);
9526 level = !current_tinst_level
9527 || current_tinst_level->maybe_get_node () != d;
9528
9529 if (level)
9530 push_tinst_level (d);
9531
9532 pt = pending_template_freelist ().alloc ();
9533 pt->next = NULL;
9534 pt->tinst = NULL;
9535 set_refcount_ptr (pt->tinst, current_tinst_level);
9536 if (last_pending_template)
9537 last_pending_template->next = pt;
9538 else
9539 pending_templates = pt;
9540
9541 last_pending_template = pt;
9542
9543 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9544
9545 if (level)
9546 pop_tinst_level ();
9547 }
9548
9549
9550 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9551 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9552 documentation for TEMPLATE_ID_EXPR. */
9553
9554 tree
9555 lookup_template_function (tree fns, tree arglist)
9556 {
9557 if (fns == error_mark_node || arglist == error_mark_node)
9558 return error_mark_node;
9559
9560 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9561
9562 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9563 {
9564 error ("%q#D is not a function template", fns);
9565 return error_mark_node;
9566 }
9567
9568 if (BASELINK_P (fns))
9569 {
9570 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9571 unknown_type_node,
9572 BASELINK_FUNCTIONS (fns),
9573 arglist);
9574 return fns;
9575 }
9576
9577 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9578 }
9579
9580 /* Within the scope of a template class S<T>, the name S gets bound
9581 (in build_self_reference) to a TYPE_DECL for the class, not a
9582 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9583 or one of its enclosing classes, and that type is a template,
9584 return the associated TEMPLATE_DECL. Otherwise, the original
9585 DECL is returned.
9586
9587 Also handle the case when DECL is a TREE_LIST of ambiguous
9588 injected-class-names from different bases. */
9589
9590 tree
9591 maybe_get_template_decl_from_type_decl (tree decl)
9592 {
9593 if (decl == NULL_TREE)
9594 return decl;
9595
9596 /* DR 176: A lookup that finds an injected-class-name (10.2
9597 [class.member.lookup]) can result in an ambiguity in certain cases
9598 (for example, if it is found in more than one base class). If all of
9599 the injected-class-names that are found refer to specializations of
9600 the same class template, and if the name is followed by a
9601 template-argument-list, the reference refers to the class template
9602 itself and not a specialization thereof, and is not ambiguous. */
9603 if (TREE_CODE (decl) == TREE_LIST)
9604 {
9605 tree t, tmpl = NULL_TREE;
9606 for (t = decl; t; t = TREE_CHAIN (t))
9607 {
9608 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9609 if (!tmpl)
9610 tmpl = elt;
9611 else if (tmpl != elt)
9612 break;
9613 }
9614 if (tmpl && t == NULL_TREE)
9615 return tmpl;
9616 else
9617 return decl;
9618 }
9619
9620 return (decl != NULL_TREE
9621 && DECL_SELF_REFERENCE_P (decl)
9622 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9623 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9624 }
9625
9626 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9627 parameters, find the desired type.
9628
9629 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9630
9631 IN_DECL, if non-NULL, is the template declaration we are trying to
9632 instantiate.
9633
9634 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9635 the class we are looking up.
9636
9637 Issue error and warning messages under control of COMPLAIN.
9638
9639 If the template class is really a local class in a template
9640 function, then the FUNCTION_CONTEXT is the function in which it is
9641 being instantiated.
9642
9643 ??? Note that this function is currently called *twice* for each
9644 template-id: the first time from the parser, while creating the
9645 incomplete type (finish_template_type), and the second type during the
9646 real instantiation (instantiate_template_class). This is surely something
9647 that we want to avoid. It also causes some problems with argument
9648 coercion (see convert_nontype_argument for more information on this). */
9649
9650 static tree
9651 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9652 int entering_scope, tsubst_flags_t complain)
9653 {
9654 tree templ = NULL_TREE, parmlist;
9655 tree t;
9656 spec_entry **slot;
9657 spec_entry *entry;
9658 spec_entry elt;
9659 hashval_t hash;
9660
9661 if (identifier_p (d1))
9662 {
9663 tree value = innermost_non_namespace_value (d1);
9664 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9665 templ = value;
9666 else
9667 {
9668 if (context)
9669 push_decl_namespace (context);
9670 templ = lookup_name (d1);
9671 templ = maybe_get_template_decl_from_type_decl (templ);
9672 if (context)
9673 pop_decl_namespace ();
9674 }
9675 if (templ)
9676 context = DECL_CONTEXT (templ);
9677 }
9678 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9679 {
9680 tree type = TREE_TYPE (d1);
9681
9682 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9683 an implicit typename for the second A. Deal with it. */
9684 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9685 type = TREE_TYPE (type);
9686
9687 if (CLASSTYPE_TEMPLATE_INFO (type))
9688 {
9689 templ = CLASSTYPE_TI_TEMPLATE (type);
9690 d1 = DECL_NAME (templ);
9691 }
9692 }
9693 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9694 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9695 {
9696 templ = TYPE_TI_TEMPLATE (d1);
9697 d1 = DECL_NAME (templ);
9698 }
9699 else if (DECL_TYPE_TEMPLATE_P (d1))
9700 {
9701 templ = d1;
9702 d1 = DECL_NAME (templ);
9703 context = DECL_CONTEXT (templ);
9704 }
9705 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9706 {
9707 templ = d1;
9708 d1 = DECL_NAME (templ);
9709 }
9710
9711 /* Issue an error message if we didn't find a template. */
9712 if (! templ)
9713 {
9714 if (complain & tf_error)
9715 error ("%qT is not a template", d1);
9716 return error_mark_node;
9717 }
9718
9719 if (TREE_CODE (templ) != TEMPLATE_DECL
9720 /* Make sure it's a user visible template, if it was named by
9721 the user. */
9722 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9723 && !PRIMARY_TEMPLATE_P (templ)))
9724 {
9725 if (complain & tf_error)
9726 {
9727 error ("non-template type %qT used as a template", d1);
9728 if (in_decl)
9729 error ("for template declaration %q+D", in_decl);
9730 }
9731 return error_mark_node;
9732 }
9733
9734 complain &= ~tf_user;
9735
9736 /* An alias that just changes the name of a template is equivalent to the
9737 other template, so if any of the arguments are pack expansions, strip
9738 the alias to avoid problems with a pack expansion passed to a non-pack
9739 alias template parameter (DR 1430). */
9740 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9741 templ = get_underlying_template (templ);
9742
9743 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9744 {
9745 tree parm;
9746 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9747 if (arglist2 == error_mark_node
9748 || (!uses_template_parms (arglist2)
9749 && check_instantiated_args (templ, arglist2, complain)))
9750 return error_mark_node;
9751
9752 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9753 return parm;
9754 }
9755 else
9756 {
9757 tree template_type = TREE_TYPE (templ);
9758 tree gen_tmpl;
9759 tree type_decl;
9760 tree found = NULL_TREE;
9761 int arg_depth;
9762 int parm_depth;
9763 int is_dependent_type;
9764 int use_partial_inst_tmpl = false;
9765
9766 if (template_type == error_mark_node)
9767 /* An error occurred while building the template TEMPL, and a
9768 diagnostic has most certainly been emitted for that
9769 already. Let's propagate that error. */
9770 return error_mark_node;
9771
9772 gen_tmpl = most_general_template (templ);
9773 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9774 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9775 arg_depth = TMPL_ARGS_DEPTH (arglist);
9776
9777 if (arg_depth == 1 && parm_depth > 1)
9778 {
9779 /* We've been given an incomplete set of template arguments.
9780 For example, given:
9781
9782 template <class T> struct S1 {
9783 template <class U> struct S2 {};
9784 template <class U> struct S2<U*> {};
9785 };
9786
9787 we will be called with an ARGLIST of `U*', but the
9788 TEMPLATE will be `template <class T> template
9789 <class U> struct S1<T>::S2'. We must fill in the missing
9790 arguments. */
9791 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9792 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9793 arg_depth = TMPL_ARGS_DEPTH (arglist);
9794 }
9795
9796 /* Now we should have enough arguments. */
9797 gcc_assert (parm_depth == arg_depth);
9798
9799 /* From here on, we're only interested in the most general
9800 template. */
9801
9802 /* Calculate the BOUND_ARGS. These will be the args that are
9803 actually tsubst'd into the definition to create the
9804 instantiation. */
9805 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9806 complain,
9807 /*require_all_args=*/true,
9808 /*use_default_args=*/true);
9809
9810 if (arglist == error_mark_node)
9811 /* We were unable to bind the arguments. */
9812 return error_mark_node;
9813
9814 /* In the scope of a template class, explicit references to the
9815 template class refer to the type of the template, not any
9816 instantiation of it. For example, in:
9817
9818 template <class T> class C { void f(C<T>); }
9819
9820 the `C<T>' is just the same as `C'. Outside of the
9821 class, however, such a reference is an instantiation. */
9822 if (entering_scope
9823 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9824 || currently_open_class (template_type))
9825 {
9826 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9827
9828 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9829 return template_type;
9830 }
9831
9832 /* If we already have this specialization, return it. */
9833 elt.tmpl = gen_tmpl;
9834 elt.args = arglist;
9835 elt.spec = NULL_TREE;
9836 hash = spec_hasher::hash (&elt);
9837 entry = type_specializations->find_with_hash (&elt, hash);
9838
9839 if (entry)
9840 return entry->spec;
9841
9842 /* If the template's constraints are not satisfied,
9843 then we cannot form a valid type.
9844
9845 Note that the check is deferred until after the hash
9846 lookup. This prevents redundant checks on previously
9847 instantiated specializations. */
9848 if (flag_concepts
9849 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9850 && !constraints_satisfied_p (gen_tmpl, arglist))
9851 {
9852 if (complain & tf_error)
9853 {
9854 auto_diagnostic_group d;
9855 error ("template constraint failure for %qD", gen_tmpl);
9856 diagnose_constraints (input_location, gen_tmpl, arglist);
9857 }
9858 return error_mark_node;
9859 }
9860
9861 is_dependent_type = uses_template_parms (arglist);
9862
9863 /* If the deduced arguments are invalid, then the binding
9864 failed. */
9865 if (!is_dependent_type
9866 && check_instantiated_args (gen_tmpl,
9867 INNERMOST_TEMPLATE_ARGS (arglist),
9868 complain))
9869 return error_mark_node;
9870
9871 if (!is_dependent_type
9872 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9873 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9874 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9875 /* This occurs when the user has tried to define a tagged type
9876 in a scope that forbids it. We emitted an error during the
9877 parse. We didn't complete the bail out then, so here we
9878 are. */
9879 return error_mark_node;
9880
9881 context = DECL_CONTEXT (gen_tmpl);
9882 if (context && TYPE_P (context))
9883 {
9884 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9885 context = complete_type (context);
9886 }
9887 else
9888 context = tsubst (context, arglist, complain, in_decl);
9889
9890 if (context == error_mark_node)
9891 return error_mark_node;
9892
9893 if (!context)
9894 context = global_namespace;
9895
9896 /* Create the type. */
9897 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9898 {
9899 /* The user referred to a specialization of an alias
9900 template represented by GEN_TMPL.
9901
9902 [temp.alias]/2 says:
9903
9904 When a template-id refers to the specialization of an
9905 alias template, it is equivalent to the associated
9906 type obtained by substitution of its
9907 template-arguments for the template-parameters in the
9908 type-id of the alias template. */
9909
9910 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9911 /* Note that the call above (by indirectly calling
9912 register_specialization in tsubst_decl) registers the
9913 TYPE_DECL representing the specialization of the alias
9914 template. So next time someone substitutes ARGLIST for
9915 the template parms into the alias template (GEN_TMPL),
9916 she'll get that TYPE_DECL back. */
9917
9918 if (t == error_mark_node)
9919 return t;
9920 }
9921 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9922 {
9923 if (!is_dependent_type)
9924 {
9925 set_current_access_from_decl (TYPE_NAME (template_type));
9926 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9927 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9928 arglist, complain, in_decl),
9929 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9930 arglist, complain, in_decl),
9931 SCOPED_ENUM_P (template_type), NULL);
9932
9933 if (t == error_mark_node)
9934 return t;
9935 }
9936 else
9937 {
9938 /* We don't want to call start_enum for this type, since
9939 the values for the enumeration constants may involve
9940 template parameters. And, no one should be interested
9941 in the enumeration constants for such a type. */
9942 t = cxx_make_type (ENUMERAL_TYPE);
9943 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9944 }
9945 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9946 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9947 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9948 }
9949 else if (CLASS_TYPE_P (template_type))
9950 {
9951 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9952 instantiated here. */
9953 gcc_assert (!LAMBDA_TYPE_P (template_type));
9954
9955 t = make_class_type (TREE_CODE (template_type));
9956 CLASSTYPE_DECLARED_CLASS (t)
9957 = CLASSTYPE_DECLARED_CLASS (template_type);
9958 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9959
9960 /* A local class. Make sure the decl gets registered properly. */
9961 if (context == current_function_decl)
9962 if (pushtag (DECL_NAME (gen_tmpl), t)
9963 == error_mark_node)
9964 return error_mark_node;
9965
9966 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9967 /* This instantiation is another name for the primary
9968 template type. Set the TYPE_CANONICAL field
9969 appropriately. */
9970 TYPE_CANONICAL (t) = template_type;
9971 else if (any_template_arguments_need_structural_equality_p (arglist))
9972 /* Some of the template arguments require structural
9973 equality testing, so this template class requires
9974 structural equality testing. */
9975 SET_TYPE_STRUCTURAL_EQUALITY (t);
9976 }
9977 else
9978 gcc_unreachable ();
9979
9980 /* If we called start_enum or pushtag above, this information
9981 will already be set up. */
9982 type_decl = TYPE_NAME (t);
9983 if (!type_decl)
9984 {
9985 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9986
9987 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9988 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9989 DECL_SOURCE_LOCATION (type_decl)
9990 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9991 }
9992
9993 if (CLASS_TYPE_P (template_type))
9994 {
9995 TREE_PRIVATE (type_decl)
9996 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9997 TREE_PROTECTED (type_decl)
9998 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9999 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
10000 {
10001 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
10002 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
10003 }
10004 }
10005
10006 if (OVERLOAD_TYPE_P (t)
10007 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10008 {
10009 static const char *tags[] = {"abi_tag", "may_alias"};
10010
10011 for (unsigned ix = 0; ix != 2; ix++)
10012 {
10013 tree attributes
10014 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
10015
10016 if (attributes)
10017 TYPE_ATTRIBUTES (t)
10018 = tree_cons (TREE_PURPOSE (attributes),
10019 TREE_VALUE (attributes),
10020 TYPE_ATTRIBUTES (t));
10021 }
10022 }
10023
10024 /* Let's consider the explicit specialization of a member
10025 of a class template specialization that is implicitly instantiated,
10026 e.g.:
10027 template<class T>
10028 struct S
10029 {
10030 template<class U> struct M {}; //#0
10031 };
10032
10033 template<>
10034 template<>
10035 struct S<int>::M<char> //#1
10036 {
10037 int i;
10038 };
10039 [temp.expl.spec]/4 says this is valid.
10040
10041 In this case, when we write:
10042 S<int>::M<char> m;
10043
10044 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10045 the one of #0.
10046
10047 When we encounter #1, we want to store the partial instantiation
10048 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10049
10050 For all cases other than this "explicit specialization of member of a
10051 class template", we just want to store the most general template into
10052 the CLASSTYPE_TI_TEMPLATE of M.
10053
10054 This case of "explicit specialization of member of a class template"
10055 only happens when:
10056 1/ the enclosing class is an instantiation of, and therefore not
10057 the same as, the context of the most general template, and
10058 2/ we aren't looking at the partial instantiation itself, i.e.
10059 the innermost arguments are not the same as the innermost parms of
10060 the most general template.
10061
10062 So it's only when 1/ and 2/ happens that we want to use the partial
10063 instantiation of the member template in lieu of its most general
10064 template. */
10065
10066 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10067 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10068 /* the enclosing class must be an instantiation... */
10069 && CLASS_TYPE_P (context)
10070 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10071 {
10072 TREE_VEC_LENGTH (arglist)--;
10073 ++processing_template_decl;
10074 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10075 tree partial_inst_args =
10076 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10077 arglist, complain, NULL_TREE);
10078 --processing_template_decl;
10079 TREE_VEC_LENGTH (arglist)++;
10080 if (partial_inst_args == error_mark_node)
10081 return error_mark_node;
10082 use_partial_inst_tmpl =
10083 /*...and we must not be looking at the partial instantiation
10084 itself. */
10085 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10086 partial_inst_args);
10087 }
10088
10089 if (!use_partial_inst_tmpl)
10090 /* This case is easy; there are no member templates involved. */
10091 found = gen_tmpl;
10092 else
10093 {
10094 /* This is a full instantiation of a member template. Find
10095 the partial instantiation of which this is an instance. */
10096
10097 /* Temporarily reduce by one the number of levels in the ARGLIST
10098 so as to avoid comparing the last set of arguments. */
10099 TREE_VEC_LENGTH (arglist)--;
10100 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
10101 TREE_VEC_LENGTH (arglist)++;
10102 /* FOUND is either a proper class type, or an alias
10103 template specialization. In the later case, it's a
10104 TYPE_DECL, resulting from the substituting of arguments
10105 for parameters in the TYPE_DECL of the alias template
10106 done earlier. So be careful while getting the template
10107 of FOUND. */
10108 found = (TREE_CODE (found) == TEMPLATE_DECL
10109 ? found
10110 : (TREE_CODE (found) == TYPE_DECL
10111 ? DECL_TI_TEMPLATE (found)
10112 : CLASSTYPE_TI_TEMPLATE (found)));
10113
10114 if (DECL_CLASS_TEMPLATE_P (found)
10115 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10116 {
10117 /* If this partial instantiation is specialized, we want to
10118 use it for hash table lookup. */
10119 elt.tmpl = found;
10120 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10121 hash = spec_hasher::hash (&elt);
10122 }
10123 }
10124
10125 /* Build template info for the new specialization. */
10126 if (TYPE_ALIAS_P (t))
10127 {
10128 /* This is constructed during instantiation of the alias
10129 decl. But for member templates of template classes, that
10130 is not correct as we need to refer to the partially
10131 instantiated template, not the most general template.
10132 The incorrect knowledge will not have escaped this
10133 instantiation process, so we're good just updating the
10134 template_info we made then. */
10135 tree ti = DECL_TEMPLATE_INFO (TYPE_NAME (t));
10136 gcc_checking_assert (template_args_equal (TI_ARGS (ti), arglist));
10137 if (TI_TEMPLATE (ti) != found)
10138 {
10139 gcc_checking_assert (DECL_TI_TEMPLATE (found) == TI_TEMPLATE (ti));
10140 TI_TEMPLATE (ti) = found;
10141 }
10142 }
10143 else
10144 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10145
10146 elt.spec = t;
10147 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10148 gcc_checking_assert (*slot == NULL);
10149 entry = ggc_alloc<spec_entry> ();
10150 *entry = elt;
10151 *slot = entry;
10152
10153 /* Note this use of the partial instantiation so we can check it
10154 later in maybe_process_partial_specialization. */
10155 DECL_TEMPLATE_INSTANTIATIONS (found)
10156 = tree_cons (arglist, t,
10157 DECL_TEMPLATE_INSTANTIATIONS (found));
10158
10159 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10160 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10161 /* Now that the type has been registered on the instantiations
10162 list, we set up the enumerators. Because the enumeration
10163 constants may involve the enumeration type itself, we make
10164 sure to register the type first, and then create the
10165 constants. That way, doing tsubst_expr for the enumeration
10166 constants won't result in recursive calls here; we'll find
10167 the instantiation and exit above. */
10168 tsubst_enum (template_type, t, arglist);
10169
10170 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10171 /* If the type makes use of template parameters, the
10172 code that generates debugging information will crash. */
10173 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10174
10175 /* Possibly limit visibility based on template args. */
10176 TREE_PUBLIC (type_decl) = 1;
10177 determine_visibility (type_decl);
10178
10179 inherit_targ_abi_tags (t);
10180
10181 return t;
10182 }
10183 }
10184
10185 /* Wrapper for lookup_template_class_1. */
10186
10187 tree
10188 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10189 int entering_scope, tsubst_flags_t complain)
10190 {
10191 tree ret;
10192 timevar_push (TV_TEMPLATE_INST);
10193 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10194 entering_scope, complain);
10195 timevar_pop (TV_TEMPLATE_INST);
10196 return ret;
10197 }
10198
10199 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10200
10201 tree
10202 lookup_template_variable (tree templ, tree arglist)
10203 {
10204 if (flag_concepts && variable_concept_p (templ))
10205 return build_concept_check (templ, arglist, tf_none);
10206
10207 /* The type of the expression is NULL_TREE since the template-id could refer
10208 to an explicit or partial specialization. */
10209 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10210 }
10211
10212 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10213
10214 tree
10215 finish_template_variable (tree var, tsubst_flags_t complain)
10216 {
10217 tree templ = TREE_OPERAND (var, 0);
10218 tree arglist = TREE_OPERAND (var, 1);
10219
10220 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10221 arglist = add_outermost_template_args (tmpl_args, arglist);
10222
10223 templ = most_general_template (templ);
10224 tree parms = DECL_TEMPLATE_PARMS (templ);
10225 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10226 /*req_all*/true,
10227 /*use_default*/true);
10228 if (arglist == error_mark_node)
10229 return error_mark_node;
10230
10231 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10232 {
10233 if (complain & tf_error)
10234 {
10235 auto_diagnostic_group d;
10236 error ("use of invalid variable template %qE", var);
10237 diagnose_constraints (location_of (var), templ, arglist);
10238 }
10239 return error_mark_node;
10240 }
10241
10242 return instantiate_template (templ, arglist, complain);
10243 }
10244
10245 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10246 TARGS template args, and instantiate it if it's not dependent. */
10247
10248 tree
10249 lookup_and_finish_template_variable (tree templ, tree targs,
10250 tsubst_flags_t complain)
10251 {
10252 templ = lookup_template_variable (templ, targs);
10253 if (!any_dependent_template_arguments_p (targs))
10254 {
10255 templ = finish_template_variable (templ, complain);
10256 mark_used (templ);
10257 }
10258
10259 return convert_from_reference (templ);
10260 }
10261
10262 /* If the set of template parameters PARMS contains a template parameter
10263 at the given LEVEL and INDEX, then return this parameter. Otherwise
10264 return NULL_TREE. */
10265
10266 static tree
10267 corresponding_template_parameter (tree parms, int level, int index)
10268 {
10269 while (TMPL_PARMS_DEPTH (parms) > level)
10270 parms = TREE_CHAIN (parms);
10271
10272 if (TMPL_PARMS_DEPTH (parms) != level
10273 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10274 return NULL_TREE;
10275
10276 tree t = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), index));
10277 /* As in template_parm_to_arg. */
10278 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10279 t = TREE_TYPE (t);
10280 else
10281 t = DECL_INITIAL (t);
10282
10283 gcc_assert (TEMPLATE_PARM_P (t));
10284 return t;
10285 }
10286
10287 /* Return the template parameter from PARMS that positionally corresponds
10288 to the template parameter PARM, or else return NULL_TREE. */
10289
10290 static tree
10291 corresponding_template_parameter (tree parms, tree parm)
10292 {
10293 int level, index;
10294 template_parm_level_and_index (parm, &level, &index);
10295 return corresponding_template_parameter (parms, level, index);
10296 }
10297
10298 \f
10299 struct pair_fn_data
10300 {
10301 tree_fn_t fn;
10302 tree_fn_t any_fn;
10303 void *data;
10304 /* True when we should also visit template parameters that occur in
10305 non-deduced contexts. */
10306 bool include_nondeduced_p;
10307 hash_set<tree> *visited;
10308 };
10309
10310 /* Called from for_each_template_parm via walk_tree. */
10311
10312 static tree
10313 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10314 {
10315 tree t = *tp;
10316 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10317 tree_fn_t fn = pfd->fn;
10318 void *data = pfd->data;
10319 tree result = NULL_TREE;
10320
10321 #define WALK_SUBTREE(NODE) \
10322 do \
10323 { \
10324 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10325 pfd->include_nondeduced_p, \
10326 pfd->any_fn); \
10327 if (result) goto out; \
10328 } \
10329 while (0)
10330
10331 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10332 return t;
10333
10334 if (TYPE_P (t)
10335 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10336 WALK_SUBTREE (TYPE_CONTEXT (t));
10337
10338 switch (TREE_CODE (t))
10339 {
10340 case RECORD_TYPE:
10341 if (TYPE_PTRMEMFUNC_P (t))
10342 break;
10343 /* Fall through. */
10344
10345 case UNION_TYPE:
10346 case ENUMERAL_TYPE:
10347 if (!TYPE_TEMPLATE_INFO (t))
10348 *walk_subtrees = 0;
10349 else
10350 WALK_SUBTREE (TYPE_TI_ARGS (t));
10351 break;
10352
10353 case INTEGER_TYPE:
10354 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10355 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10356 break;
10357
10358 case METHOD_TYPE:
10359 /* Since we're not going to walk subtrees, we have to do this
10360 explicitly here. */
10361 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10362 /* Fall through. */
10363
10364 case FUNCTION_TYPE:
10365 /* Check the return type. */
10366 WALK_SUBTREE (TREE_TYPE (t));
10367
10368 /* Check the parameter types. Since default arguments are not
10369 instantiated until they are needed, the TYPE_ARG_TYPES may
10370 contain expressions that involve template parameters. But,
10371 no-one should be looking at them yet. And, once they're
10372 instantiated, they don't contain template parameters, so
10373 there's no point in looking at them then, either. */
10374 {
10375 tree parm;
10376
10377 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10378 WALK_SUBTREE (TREE_VALUE (parm));
10379
10380 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10381 want walk_tree walking into them itself. */
10382 *walk_subtrees = 0;
10383 }
10384
10385 if (flag_noexcept_type)
10386 {
10387 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10388 if (spec)
10389 WALK_SUBTREE (TREE_PURPOSE (spec));
10390 }
10391 break;
10392
10393 case TYPEOF_TYPE:
10394 case DECLTYPE_TYPE:
10395 case UNDERLYING_TYPE:
10396 if (pfd->include_nondeduced_p
10397 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10398 pfd->visited,
10399 pfd->include_nondeduced_p,
10400 pfd->any_fn))
10401 return error_mark_node;
10402 *walk_subtrees = false;
10403 break;
10404
10405 case FUNCTION_DECL:
10406 case VAR_DECL:
10407 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10408 WALK_SUBTREE (DECL_TI_ARGS (t));
10409 /* Fall through. */
10410
10411 case PARM_DECL:
10412 case CONST_DECL:
10413 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10414 WALK_SUBTREE (DECL_INITIAL (t));
10415 if (DECL_CONTEXT (t)
10416 && pfd->include_nondeduced_p)
10417 WALK_SUBTREE (DECL_CONTEXT (t));
10418 break;
10419
10420 case BOUND_TEMPLATE_TEMPLATE_PARM:
10421 /* Record template parameters such as `T' inside `TT<T>'. */
10422 WALK_SUBTREE (TYPE_TI_ARGS (t));
10423 /* Fall through. */
10424
10425 case TEMPLATE_TEMPLATE_PARM:
10426 case TEMPLATE_TYPE_PARM:
10427 case TEMPLATE_PARM_INDEX:
10428 if (fn && (*fn)(t, data))
10429 return t;
10430 else if (!fn)
10431 return t;
10432 break;
10433
10434 case TEMPLATE_DECL:
10435 /* A template template parameter is encountered. */
10436 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10437 WALK_SUBTREE (TREE_TYPE (t));
10438
10439 /* Already substituted template template parameter */
10440 *walk_subtrees = 0;
10441 break;
10442
10443 case TYPENAME_TYPE:
10444 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10445 partial instantiation. */
10446 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10447 *walk_subtrees = 0;
10448 break;
10449
10450 case CONSTRUCTOR:
10451 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10452 && pfd->include_nondeduced_p)
10453 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10454 break;
10455
10456 case INDIRECT_REF:
10457 case COMPONENT_REF:
10458 /* If there's no type, then this thing must be some expression
10459 involving template parameters. */
10460 if (!fn && !TREE_TYPE (t))
10461 return error_mark_node;
10462 break;
10463
10464 case MODOP_EXPR:
10465 case CAST_EXPR:
10466 case IMPLICIT_CONV_EXPR:
10467 case REINTERPRET_CAST_EXPR:
10468 case CONST_CAST_EXPR:
10469 case STATIC_CAST_EXPR:
10470 case DYNAMIC_CAST_EXPR:
10471 case ARROW_EXPR:
10472 case DOTSTAR_EXPR:
10473 case TYPEID_EXPR:
10474 case PSEUDO_DTOR_EXPR:
10475 if (!fn)
10476 return error_mark_node;
10477 break;
10478
10479 case SCOPE_REF:
10480 if (pfd->include_nondeduced_p)
10481 WALK_SUBTREE (TREE_OPERAND (t, 0));
10482 break;
10483
10484 case REQUIRES_EXPR:
10485 {
10486 if (!fn)
10487 return error_mark_node;
10488
10489 /* Recursively walk the type of each constraint variable. */
10490 tree p = TREE_OPERAND (t, 0);
10491 while (p)
10492 {
10493 WALK_SUBTREE (TREE_TYPE (p));
10494 p = TREE_CHAIN (p);
10495 }
10496 }
10497 break;
10498
10499 default:
10500 break;
10501 }
10502
10503 #undef WALK_SUBTREE
10504
10505 /* We didn't find any template parameters we liked. */
10506 out:
10507 return result;
10508 }
10509
10510 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10511 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10512 call FN with the parameter and the DATA.
10513 If FN returns nonzero, the iteration is terminated, and
10514 for_each_template_parm returns 1. Otherwise, the iteration
10515 continues. If FN never returns a nonzero value, the value
10516 returned by for_each_template_parm is 0. If FN is NULL, it is
10517 considered to be the function which always returns 1.
10518
10519 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10520 parameters that occur in non-deduced contexts. When false, only
10521 visits those template parameters that can be deduced. */
10522
10523 static tree
10524 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10525 hash_set<tree> *visited,
10526 bool include_nondeduced_p,
10527 tree_fn_t any_fn)
10528 {
10529 struct pair_fn_data pfd;
10530 tree result;
10531
10532 /* Set up. */
10533 pfd.fn = fn;
10534 pfd.any_fn = any_fn;
10535 pfd.data = data;
10536 pfd.include_nondeduced_p = include_nondeduced_p;
10537
10538 /* Walk the tree. (Conceptually, we would like to walk without
10539 duplicates, but for_each_template_parm_r recursively calls
10540 for_each_template_parm, so we would need to reorganize a fair
10541 bit to use walk_tree_without_duplicates, so we keep our own
10542 visited list.) */
10543 if (visited)
10544 pfd.visited = visited;
10545 else
10546 pfd.visited = new hash_set<tree>;
10547 result = cp_walk_tree (&t,
10548 for_each_template_parm_r,
10549 &pfd,
10550 pfd.visited);
10551
10552 /* Clean up. */
10553 if (!visited)
10554 {
10555 delete pfd.visited;
10556 pfd.visited = 0;
10557 }
10558
10559 return result;
10560 }
10561
10562 struct find_template_parameter_info
10563 {
10564 explicit find_template_parameter_info (tree ctx_parms)
10565 : parm_list (NULL_TREE),
10566 ctx_parms (ctx_parms),
10567 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10568 {}
10569
10570 hash_set<tree> visited;
10571 hash_set<tree> parms;
10572 tree parm_list;
10573 tree ctx_parms;
10574 int max_depth;
10575 };
10576
10577 /* Appends the declaration of T to the list in DATA. */
10578
10579 static int
10580 keep_template_parm (tree t, void* data)
10581 {
10582 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10583
10584 /* Template parameters declared within the expression are not part of
10585 the parameter mapping. For example, in this concept:
10586
10587 template<typename T>
10588 concept C = requires { <expr> } -> same_as<int>;
10589
10590 the return specifier same_as<int> declares a new decltype parameter
10591 that must not be part of the parameter mapping. The same is true
10592 for generic lambda parameters, lambda template parameters, etc. */
10593 int level;
10594 int index;
10595 template_parm_level_and_index (t, &level, &index);
10596 if (level > ftpi->max_depth)
10597 return 0;
10598
10599 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10600 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10601 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10602 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10603
10604 /* This template parameter might be an argument to a cached dependent
10605 specalization that was formed earlier inside some other template, in
10606 which case the parameter is not among the ones that are in-scope.
10607 Look in CTX_PARMS to find the corresponding in-scope template
10608 parameter, and use it instead. */
10609 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
10610 t = in_scope;
10611
10612 /* Arguments like const T yield parameters like const T. This means that
10613 a template-id like X<T, const T> would yield two distinct parameters:
10614 T and const T. Adjust types to their unqualified versions. */
10615 if (TYPE_P (t))
10616 t = TYPE_MAIN_VARIANT (t);
10617 if (!ftpi->parms.add (t))
10618 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10619
10620 /* Verify the parameter we found has a valid index. */
10621 if (flag_checking)
10622 {
10623 tree parms = ftpi->ctx_parms;
10624 while (TMPL_PARMS_DEPTH (parms) > level)
10625 parms = TREE_CHAIN (parms);
10626 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
10627 gcc_assert (index < len);
10628 }
10629
10630 return 0;
10631 }
10632
10633 /* Ensure that we recursively examine certain terms that are not normally
10634 visited in for_each_template_parm_r. */
10635
10636 static int
10637 any_template_parm_r (tree t, void *data)
10638 {
10639 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10640
10641 #define WALK_SUBTREE(NODE) \
10642 do \
10643 { \
10644 for_each_template_parm (NODE, keep_template_parm, data, \
10645 &ftpi->visited, true, \
10646 any_template_parm_r); \
10647 } \
10648 while (0)
10649
10650 /* A mention of a member alias/typedef is a use of all of its template
10651 arguments, including those from the enclosing class, so we don't use
10652 alias_template_specialization_p here. */
10653 if (TYPE_P (t) && typedef_variant_p (t))
10654 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10655 WALK_SUBTREE (TI_ARGS (tinfo));
10656
10657 switch (TREE_CODE (t))
10658 {
10659 case TEMPLATE_TYPE_PARM:
10660 /* Type constraints of a placeholder type may contain parameters. */
10661 if (is_auto (t))
10662 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10663 WALK_SUBTREE (constr);
10664 break;
10665
10666 case TEMPLATE_ID_EXPR:
10667 /* Search through references to variable templates. */
10668 WALK_SUBTREE (TREE_OPERAND (t, 0));
10669 WALK_SUBTREE (TREE_OPERAND (t, 1));
10670 break;
10671
10672 case TEMPLATE_PARM_INDEX:
10673 case PARM_DECL:
10674 /* A parameter or constraint variable may also depend on a template
10675 parameter without explicitly naming it. */
10676 WALK_SUBTREE (TREE_TYPE (t));
10677 break;
10678
10679 case TEMPLATE_DECL:
10680 {
10681 /* If T is a member template that shares template parameters with
10682 ctx_parms, we need to mark all those parameters for mapping. */
10683 tree dparms = DECL_TEMPLATE_PARMS (t);
10684 tree cparms = ftpi->ctx_parms;
10685 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10686 dparms = TREE_CHAIN (dparms);
10687 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10688 cparms = TREE_CHAIN (cparms);
10689 while (dparms
10690 && (TREE_TYPE (TREE_VALUE (dparms))
10691 != TREE_TYPE (TREE_VALUE (cparms))))
10692 dparms = TREE_CHAIN (dparms),
10693 cparms = TREE_CHAIN (cparms);
10694 if (dparms)
10695 {
10696 int ddepth = TMPL_PARMS_DEPTH (dparms);
10697 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10698 for (int i = 0; i < ddepth; ++i)
10699 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10700 }
10701 }
10702 break;
10703
10704 case LAMBDA_EXPR:
10705 {
10706 /* Look in the parms and body. */
10707 tree fn = lambda_function (t);
10708 WALK_SUBTREE (TREE_TYPE (fn));
10709 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10710 }
10711 break;
10712
10713 case IDENTIFIER_NODE:
10714 if (IDENTIFIER_CONV_OP_P (t))
10715 /* The conversion-type-id of a conversion operator may be dependent. */
10716 WALK_SUBTREE (TREE_TYPE (t));
10717 break;
10718
10719 default:
10720 break;
10721 }
10722
10723 /* Keep walking. */
10724 return 0;
10725 }
10726
10727 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10728 are the template parameters in scope. */
10729
10730 tree
10731 find_template_parameters (tree t, tree ctx_parms)
10732 {
10733 if (!ctx_parms)
10734 return NULL_TREE;
10735
10736 find_template_parameter_info ftpi (ctx_parms);
10737 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10738 /*include_nondeduced*/true, any_template_parm_r);
10739 return ftpi.parm_list;
10740 }
10741
10742 /* Returns true if T depends on any template parameter. */
10743
10744 int
10745 uses_template_parms (tree t)
10746 {
10747 if (t == NULL_TREE)
10748 return false;
10749
10750 bool dependent_p;
10751 int saved_processing_template_decl;
10752
10753 saved_processing_template_decl = processing_template_decl;
10754 if (!saved_processing_template_decl)
10755 processing_template_decl = 1;
10756 if (TYPE_P (t))
10757 dependent_p = dependent_type_p (t);
10758 else if (TREE_CODE (t) == TREE_VEC)
10759 dependent_p = any_dependent_template_arguments_p (t);
10760 else if (TREE_CODE (t) == TREE_LIST)
10761 dependent_p = (uses_template_parms (TREE_VALUE (t))
10762 || uses_template_parms (TREE_CHAIN (t)));
10763 else if (TREE_CODE (t) == TYPE_DECL)
10764 dependent_p = dependent_type_p (TREE_TYPE (t));
10765 else if (t == error_mark_node)
10766 dependent_p = false;
10767 else
10768 dependent_p = instantiation_dependent_expression_p (t);
10769
10770 processing_template_decl = saved_processing_template_decl;
10771
10772 return dependent_p;
10773 }
10774
10775 /* Returns true iff current_function_decl is an incompletely instantiated
10776 template. Useful instead of processing_template_decl because the latter
10777 is set to 0 during instantiate_non_dependent_expr. */
10778
10779 bool
10780 in_template_function (void)
10781 {
10782 tree fn = current_function_decl;
10783 bool ret;
10784 ++processing_template_decl;
10785 ret = (fn && DECL_LANG_SPECIFIC (fn)
10786 && DECL_TEMPLATE_INFO (fn)
10787 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10788 --processing_template_decl;
10789 return ret;
10790 }
10791
10792 /* Returns true if T depends on any template parameter with level LEVEL. */
10793
10794 bool
10795 uses_template_parms_level (tree t, int level)
10796 {
10797 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10798 /*include_nondeduced_p=*/true);
10799 }
10800
10801 /* Returns true if the signature of DECL depends on any template parameter from
10802 its enclosing class. */
10803
10804 bool
10805 uses_outer_template_parms (tree decl)
10806 {
10807 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10808 if (depth == 0)
10809 return false;
10810 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10811 &depth, NULL, /*include_nondeduced_p=*/true))
10812 return true;
10813 if (PRIMARY_TEMPLATE_P (decl)
10814 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10815 (DECL_TEMPLATE_PARMS (decl)),
10816 template_parm_outer_level,
10817 &depth, NULL, /*include_nondeduced_p=*/true))
10818 return true;
10819 tree ci = get_constraints (decl);
10820 if (ci)
10821 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10822 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10823 &depth, NULL, /*nondeduced*/true))
10824 return true;
10825 return false;
10826 }
10827
10828 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10829 ill-formed translation unit, i.e. a variable or function that isn't
10830 usable in a constant expression. */
10831
10832 static inline bool
10833 neglectable_inst_p (tree d)
10834 {
10835 return (d && DECL_P (d)
10836 && !undeduced_auto_decl (d)
10837 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10838 : decl_maybe_constant_var_p (d)));
10839 }
10840
10841 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10842 neglectable and instantiated from within an erroneous instantiation. */
10843
10844 static bool
10845 limit_bad_template_recursion (tree decl)
10846 {
10847 struct tinst_level *lev = current_tinst_level;
10848 int errs = errorcount + sorrycount;
10849 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10850 return false;
10851
10852 for (; lev; lev = lev->next)
10853 if (neglectable_inst_p (lev->maybe_get_node ()))
10854 break;
10855
10856 return (lev && errs > lev->errors);
10857 }
10858
10859 static int tinst_depth;
10860 extern int max_tinst_depth;
10861 int depth_reached;
10862
10863 static GTY(()) struct tinst_level *last_error_tinst_level;
10864
10865 /* We're starting to instantiate D; record the template instantiation context
10866 at LOC for diagnostics and to restore it later. */
10867
10868 bool
10869 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10870 {
10871 struct tinst_level *new_level;
10872
10873 if (tinst_depth >= max_tinst_depth)
10874 {
10875 /* Tell error.c not to try to instantiate any templates. */
10876 at_eof = 2;
10877 fatal_error (input_location,
10878 "template instantiation depth exceeds maximum of %d"
10879 " (use %<-ftemplate-depth=%> to increase the maximum)",
10880 max_tinst_depth);
10881 return false;
10882 }
10883
10884 /* If the current instantiation caused problems, don't let it instantiate
10885 anything else. Do allow deduction substitution and decls usable in
10886 constant expressions. */
10887 if (!targs && limit_bad_template_recursion (tldcl))
10888 {
10889 /* Avoid no_linkage_errors and unused function warnings for this
10890 decl. */
10891 TREE_NO_WARNING (tldcl) = 1;
10892 return false;
10893 }
10894
10895 /* When not -quiet, dump template instantiations other than functions, since
10896 announce_function will take care of those. */
10897 if (!quiet_flag && !targs
10898 && TREE_CODE (tldcl) != TREE_LIST
10899 && TREE_CODE (tldcl) != FUNCTION_DECL)
10900 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10901
10902 new_level = tinst_level_freelist ().alloc ();
10903 new_level->tldcl = tldcl;
10904 new_level->targs = targs;
10905 new_level->locus = loc;
10906 new_level->errors = errorcount + sorrycount;
10907 new_level->next = NULL;
10908 new_level->refcount = 0;
10909 set_refcount_ptr (new_level->next, current_tinst_level);
10910 set_refcount_ptr (current_tinst_level, new_level);
10911
10912 ++tinst_depth;
10913 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10914 depth_reached = tinst_depth;
10915
10916 return true;
10917 }
10918
10919 /* We're starting substitution of TMPL<ARGS>; record the template
10920 substitution context for diagnostics and to restore it later. */
10921
10922 bool
10923 push_tinst_level (tree tmpl, tree args)
10924 {
10925 return push_tinst_level_loc (tmpl, args, input_location);
10926 }
10927
10928 /* We're starting to instantiate D; record INPUT_LOCATION and the
10929 template instantiation context for diagnostics and to restore it
10930 later. */
10931
10932 bool
10933 push_tinst_level (tree d)
10934 {
10935 return push_tinst_level_loc (d, input_location);
10936 }
10937
10938 /* Likewise, but record LOC as the program location. */
10939
10940 bool
10941 push_tinst_level_loc (tree d, location_t loc)
10942 {
10943 gcc_assert (TREE_CODE (d) != TREE_LIST);
10944 return push_tinst_level_loc (d, NULL, loc);
10945 }
10946
10947 /* We're done instantiating this template; return to the instantiation
10948 context. */
10949
10950 void
10951 pop_tinst_level (void)
10952 {
10953 /* Restore the filename and line number stashed away when we started
10954 this instantiation. */
10955 input_location = current_tinst_level->locus;
10956 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10957 --tinst_depth;
10958 }
10959
10960 /* We're instantiating a deferred template; restore the template
10961 instantiation context in which the instantiation was requested, which
10962 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10963
10964 static tree
10965 reopen_tinst_level (struct tinst_level *level)
10966 {
10967 struct tinst_level *t;
10968
10969 tinst_depth = 0;
10970 for (t = level; t; t = t->next)
10971 ++tinst_depth;
10972
10973 set_refcount_ptr (current_tinst_level, level);
10974 pop_tinst_level ();
10975 if (current_tinst_level)
10976 current_tinst_level->errors = errorcount+sorrycount;
10977 return level->maybe_get_node ();
10978 }
10979
10980 /* Returns the TINST_LEVEL which gives the original instantiation
10981 context. */
10982
10983 struct tinst_level *
10984 outermost_tinst_level (void)
10985 {
10986 struct tinst_level *level = current_tinst_level;
10987 if (level)
10988 while (level->next)
10989 level = level->next;
10990 return level;
10991 }
10992
10993 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10994 vector of template arguments, as for tsubst.
10995
10996 Returns an appropriate tsubst'd friend declaration. */
10997
10998 static tree
10999 tsubst_friend_function (tree decl, tree args)
11000 {
11001 tree new_friend;
11002
11003 if (TREE_CODE (decl) == FUNCTION_DECL
11004 && DECL_TEMPLATE_INSTANTIATION (decl)
11005 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11006 /* This was a friend declared with an explicit template
11007 argument list, e.g.:
11008
11009 friend void f<>(T);
11010
11011 to indicate that f was a template instantiation, not a new
11012 function declaration. Now, we have to figure out what
11013 instantiation of what template. */
11014 {
11015 tree template_id, arglist, fns;
11016 tree new_args;
11017 tree tmpl;
11018 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
11019
11020 /* Friend functions are looked up in the containing namespace scope.
11021 We must enter that scope, to avoid finding member functions of the
11022 current class with same name. */
11023 push_nested_namespace (ns);
11024 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
11025 tf_warning_or_error, NULL_TREE,
11026 /*integral_constant_expression_p=*/false);
11027 pop_nested_namespace (ns);
11028 arglist = tsubst (DECL_TI_ARGS (decl), args,
11029 tf_warning_or_error, NULL_TREE);
11030 template_id = lookup_template_function (fns, arglist);
11031
11032 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11033 tmpl = determine_specialization (template_id, new_friend,
11034 &new_args,
11035 /*need_member_template=*/0,
11036 TREE_VEC_LENGTH (args),
11037 tsk_none);
11038 return instantiate_template (tmpl, new_args, tf_error);
11039 }
11040
11041 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11042 if (new_friend == error_mark_node)
11043 return error_mark_node;
11044
11045 /* The NEW_FRIEND will look like an instantiation, to the
11046 compiler, but is not an instantiation from the point of view of
11047 the language. For example, we might have had:
11048
11049 template <class T> struct S {
11050 template <class U> friend void f(T, U);
11051 };
11052
11053 Then, in S<int>, template <class U> void f(int, U) is not an
11054 instantiation of anything. */
11055
11056 DECL_USE_TEMPLATE (new_friend) = 0;
11057 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11058 {
11059 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11060 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11061 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11062
11063 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11064 match in decls_match. */
11065 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11066 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11067 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11068 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11069 }
11070
11071 /* The mangled name for the NEW_FRIEND is incorrect. The function
11072 is not a template instantiation and should not be mangled like
11073 one. Therefore, we forget the mangling here; we'll recompute it
11074 later if we need it. */
11075 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11076 {
11077 SET_DECL_RTL (new_friend, NULL);
11078 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11079 }
11080
11081 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11082 {
11083 tree old_decl;
11084 tree ns;
11085
11086 /* We must save some information from NEW_FRIEND before calling
11087 duplicate decls since that function will free NEW_FRIEND if
11088 possible. */
11089 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11090 tree new_friend_result_template_info = NULL_TREE;
11091 bool new_friend_is_defn =
11092 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11093 (template_for_substitution (new_friend)))
11094 != NULL_TREE);
11095 tree not_tmpl = new_friend;
11096
11097 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11098 {
11099 /* This declaration is a `primary' template. */
11100 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11101
11102 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11103 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11104 }
11105
11106 /* Inside pushdecl_namespace_level, we will push into the
11107 current namespace. However, the friend function should go
11108 into the namespace of the template. */
11109 ns = decl_namespace_context (new_friend);
11110 push_nested_namespace (ns);
11111 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11112 pop_nested_namespace (ns);
11113
11114 if (old_decl == error_mark_node)
11115 return error_mark_node;
11116
11117 if (old_decl != new_friend)
11118 {
11119 /* This new friend declaration matched an existing
11120 declaration. For example, given:
11121
11122 template <class T> void f(T);
11123 template <class U> class C {
11124 template <class T> friend void f(T) {}
11125 };
11126
11127 the friend declaration actually provides the definition
11128 of `f', once C has been instantiated for some type. So,
11129 old_decl will be the out-of-class template declaration,
11130 while new_friend is the in-class definition.
11131
11132 But, if `f' was called before this point, the
11133 instantiation of `f' will have DECL_TI_ARGS corresponding
11134 to `T' but not to `U', references to which might appear
11135 in the definition of `f'. Previously, the most general
11136 template for an instantiation of `f' was the out-of-class
11137 version; now it is the in-class version. Therefore, we
11138 run through all specialization of `f', adding to their
11139 DECL_TI_ARGS appropriately. In particular, they need a
11140 new set of outer arguments, corresponding to the
11141 arguments for this class instantiation.
11142
11143 The same situation can arise with something like this:
11144
11145 friend void f(int);
11146 template <class T> class C {
11147 friend void f(T) {}
11148 };
11149
11150 when `C<int>' is instantiated. Now, `f(int)' is defined
11151 in the class. */
11152
11153 if (!new_friend_is_defn)
11154 /* On the other hand, if the in-class declaration does
11155 *not* provide a definition, then we don't want to alter
11156 existing definitions. We can just leave everything
11157 alone. */
11158 ;
11159 else
11160 {
11161 tree new_template = TI_TEMPLATE (new_friend_template_info);
11162 tree new_args = TI_ARGS (new_friend_template_info);
11163
11164 /* Overwrite whatever template info was there before, if
11165 any, with the new template information pertaining to
11166 the declaration. */
11167 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11168
11169 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11170 {
11171 /* We should have called reregister_specialization in
11172 duplicate_decls. */
11173 gcc_assert (retrieve_specialization (new_template,
11174 new_args, 0)
11175 == old_decl);
11176
11177 /* Instantiate it if the global has already been used. */
11178 if (DECL_ODR_USED (old_decl))
11179 instantiate_decl (old_decl, /*defer_ok=*/true,
11180 /*expl_inst_class_mem_p=*/false);
11181 }
11182 else
11183 {
11184 tree t;
11185
11186 /* Indicate that the old function template is a partial
11187 instantiation. */
11188 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11189 = new_friend_result_template_info;
11190
11191 gcc_assert (new_template
11192 == most_general_template (new_template));
11193 gcc_assert (new_template != old_decl);
11194
11195 /* Reassign any specializations already in the hash table
11196 to the new more general template, and add the
11197 additional template args. */
11198 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11199 t != NULL_TREE;
11200 t = TREE_CHAIN (t))
11201 {
11202 tree spec = TREE_VALUE (t);
11203 spec_entry elt;
11204
11205 elt.tmpl = old_decl;
11206 elt.args = DECL_TI_ARGS (spec);
11207 elt.spec = NULL_TREE;
11208
11209 decl_specializations->remove_elt (&elt);
11210
11211 DECL_TI_ARGS (spec)
11212 = add_outermost_template_args (new_args,
11213 DECL_TI_ARGS (spec));
11214
11215 register_specialization
11216 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11217
11218 }
11219 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11220 }
11221 }
11222
11223 /* The information from NEW_FRIEND has been merged into OLD_DECL
11224 by duplicate_decls. */
11225 new_friend = old_decl;
11226 }
11227 }
11228 else
11229 {
11230 tree context = DECL_CONTEXT (new_friend);
11231 bool dependent_p;
11232
11233 /* In the code
11234 template <class T> class C {
11235 template <class U> friend void C1<U>::f (); // case 1
11236 friend void C2<T>::f (); // case 2
11237 };
11238 we only need to make sure CONTEXT is a complete type for
11239 case 2. To distinguish between the two cases, we note that
11240 CONTEXT of case 1 remains dependent type after tsubst while
11241 this isn't true for case 2. */
11242 ++processing_template_decl;
11243 dependent_p = dependent_type_p (context);
11244 --processing_template_decl;
11245
11246 if (!dependent_p
11247 && !complete_type_or_else (context, NULL_TREE))
11248 return error_mark_node;
11249
11250 if (COMPLETE_TYPE_P (context))
11251 {
11252 tree fn = new_friend;
11253 /* do_friend adds the TEMPLATE_DECL for any member friend
11254 template even if it isn't a member template, i.e.
11255 template <class T> friend A<T>::f();
11256 Look through it in that case. */
11257 if (TREE_CODE (fn) == TEMPLATE_DECL
11258 && !PRIMARY_TEMPLATE_P (fn))
11259 fn = DECL_TEMPLATE_RESULT (fn);
11260 /* Check to see that the declaration is really present, and,
11261 possibly obtain an improved declaration. */
11262 fn = check_classfn (context, fn, NULL_TREE);
11263
11264 if (fn)
11265 new_friend = fn;
11266 }
11267 }
11268
11269 return new_friend;
11270 }
11271
11272 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11273 template arguments, as for tsubst.
11274
11275 Returns an appropriate tsubst'd friend type or error_mark_node on
11276 failure. */
11277
11278 static tree
11279 tsubst_friend_class (tree friend_tmpl, tree args)
11280 {
11281 tree tmpl;
11282
11283 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11284 {
11285 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11286 return TREE_TYPE (tmpl);
11287 }
11288
11289 tree context = CP_DECL_CONTEXT (friend_tmpl);
11290 if (TREE_CODE (context) == NAMESPACE_DECL)
11291 push_nested_namespace (context);
11292 else
11293 {
11294 context = tsubst (context, args, tf_error, NULL_TREE);
11295 push_nested_class (context);
11296 }
11297
11298 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11299 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11300
11301 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11302 {
11303 /* The friend template has already been declared. Just
11304 check to see that the declarations match, and install any new
11305 default parameters. We must tsubst the default parameters,
11306 of course. We only need the innermost template parameters
11307 because that is all that redeclare_class_template will look
11308 at. */
11309 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11310 > TMPL_ARGS_DEPTH (args))
11311 {
11312 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11313 args, tf_warning_or_error);
11314 location_t saved_input_location = input_location;
11315 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11316 tree cons = get_constraints (tmpl);
11317 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11318 input_location = saved_input_location;
11319 }
11320 }
11321 else
11322 {
11323 /* The friend template has not already been declared. In this
11324 case, the instantiation of the template class will cause the
11325 injection of this template into the namespace scope. */
11326 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11327
11328 if (tmpl != error_mark_node)
11329 {
11330 /* The new TMPL is not an instantiation of anything, so we
11331 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11332 for the new type because that is supposed to be the
11333 corresponding template decl, i.e., TMPL. */
11334 DECL_USE_TEMPLATE (tmpl) = 0;
11335 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11336 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11337 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11338 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11339
11340 /* Substitute into and set the constraints on the new declaration. */
11341 if (tree ci = get_constraints (friend_tmpl))
11342 {
11343 ++processing_template_decl;
11344 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11345 DECL_FRIEND_CONTEXT (friend_tmpl));
11346 --processing_template_decl;
11347 set_constraints (tmpl, ci);
11348 }
11349
11350 /* Inject this template into the enclosing namspace scope. */
11351 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
11352 }
11353 }
11354
11355 if (TREE_CODE (context) == NAMESPACE_DECL)
11356 pop_nested_namespace (context);
11357 else
11358 pop_nested_class ();
11359
11360 return TREE_TYPE (tmpl);
11361 }
11362
11363 /* Returns zero if TYPE cannot be completed later due to circularity.
11364 Otherwise returns one. */
11365
11366 static int
11367 can_complete_type_without_circularity (tree type)
11368 {
11369 if (type == NULL_TREE || type == error_mark_node)
11370 return 0;
11371 else if (COMPLETE_TYPE_P (type))
11372 return 1;
11373 else if (TREE_CODE (type) == ARRAY_TYPE)
11374 return can_complete_type_without_circularity (TREE_TYPE (type));
11375 else if (CLASS_TYPE_P (type)
11376 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11377 return 0;
11378 else
11379 return 1;
11380 }
11381
11382 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11383 tsubst_flags_t, tree);
11384
11385 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11386 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11387
11388 static tree
11389 tsubst_attribute (tree t, tree *decl_p, tree args,
11390 tsubst_flags_t complain, tree in_decl)
11391 {
11392 gcc_assert (ATTR_IS_DEPENDENT (t));
11393
11394 tree val = TREE_VALUE (t);
11395 if (val == NULL_TREE)
11396 /* Nothing to do. */;
11397 else if ((flag_openmp || flag_openmp_simd)
11398 && is_attribute_p ("omp declare simd",
11399 get_attribute_name (t)))
11400 {
11401 tree clauses = TREE_VALUE (val);
11402 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11403 complain, in_decl);
11404 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11405 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11406 tree parms = DECL_ARGUMENTS (*decl_p);
11407 clauses
11408 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11409 if (clauses)
11410 val = build_tree_list (NULL_TREE, clauses);
11411 else
11412 val = NULL_TREE;
11413 }
11414 else if (flag_openmp
11415 && is_attribute_p ("omp declare variant base",
11416 get_attribute_name (t)))
11417 {
11418 ++cp_unevaluated_operand;
11419 tree varid
11420 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11421 in_decl, /*integral_constant_expression_p=*/false);
11422 --cp_unevaluated_operand;
11423 tree chain = TREE_CHAIN (val);
11424 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11425 tree ctx = copy_list (TREE_VALUE (val));
11426 tree simd = get_identifier ("simd");
11427 tree score = get_identifier (" score");
11428 tree condition = get_identifier ("condition");
11429 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11430 {
11431 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11432 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11433 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11434 {
11435 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11436 {
11437 tree clauses = TREE_VALUE (t2);
11438 clauses = tsubst_omp_clauses (clauses,
11439 C_ORT_OMP_DECLARE_SIMD, args,
11440 complain, in_decl);
11441 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11442 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11443 TREE_VALUE (t2) = clauses;
11444 }
11445 else
11446 {
11447 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11448 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11449 if (TREE_VALUE (t3))
11450 {
11451 bool allow_string
11452 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11453 && TREE_PURPOSE (t3) != score);
11454 tree v = TREE_VALUE (t3);
11455 if (TREE_CODE (v) == STRING_CST && allow_string)
11456 continue;
11457 v = tsubst_expr (v, args, complain, in_decl, true);
11458 v = fold_non_dependent_expr (v);
11459 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11460 || (TREE_PURPOSE (t3) == score
11461 ? TREE_CODE (v) != INTEGER_CST
11462 : !tree_fits_shwi_p (v)))
11463 {
11464 location_t loc
11465 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11466 match_loc);
11467 if (TREE_PURPOSE (t3) == score)
11468 error_at (loc, "score argument must be "
11469 "constant integer expression");
11470 else if (allow_string)
11471 error_at (loc, "property must be constant "
11472 "integer expression or string "
11473 "literal");
11474 else
11475 error_at (loc, "property must be constant "
11476 "integer expression");
11477 return NULL_TREE;
11478 }
11479 else if (TREE_PURPOSE (t3) == score
11480 && tree_int_cst_sgn (v) < 0)
11481 {
11482 location_t loc
11483 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11484 match_loc);
11485 error_at (loc, "score argument must be "
11486 "non-negative");
11487 return NULL_TREE;
11488 }
11489 TREE_VALUE (t3) = v;
11490 }
11491 }
11492 }
11493 }
11494 val = tree_cons (varid, ctx, chain);
11495 }
11496 /* If the first attribute argument is an identifier, don't
11497 pass it through tsubst. Attributes like mode, format,
11498 cleanup and several target specific attributes expect it
11499 unmodified. */
11500 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11501 {
11502 tree chain
11503 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11504 /*integral_constant_expression_p=*/false);
11505 if (chain != TREE_CHAIN (val))
11506 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11507 }
11508 else if (PACK_EXPANSION_P (val))
11509 {
11510 /* An attribute pack expansion. */
11511 tree purp = TREE_PURPOSE (t);
11512 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11513 if (pack == error_mark_node)
11514 return error_mark_node;
11515 int len = TREE_VEC_LENGTH (pack);
11516 tree list = NULL_TREE;
11517 tree *q = &list;
11518 for (int i = 0; i < len; ++i)
11519 {
11520 tree elt = TREE_VEC_ELT (pack, i);
11521 *q = build_tree_list (purp, elt);
11522 q = &TREE_CHAIN (*q);
11523 }
11524 return list;
11525 }
11526 else
11527 val = tsubst_expr (val, args, complain, in_decl,
11528 /*integral_constant_expression_p=*/false);
11529
11530 if (val != TREE_VALUE (t))
11531 return build_tree_list (TREE_PURPOSE (t), val);
11532 return t;
11533 }
11534
11535 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11536 unchanged or a new TREE_LIST chain. */
11537
11538 static tree
11539 tsubst_attributes (tree attributes, tree args,
11540 tsubst_flags_t complain, tree in_decl)
11541 {
11542 tree last_dep = NULL_TREE;
11543
11544 for (tree t = attributes; t; t = TREE_CHAIN (t))
11545 if (ATTR_IS_DEPENDENT (t))
11546 {
11547 last_dep = t;
11548 attributes = copy_list (attributes);
11549 break;
11550 }
11551
11552 if (last_dep)
11553 for (tree *p = &attributes; *p; )
11554 {
11555 tree t = *p;
11556 if (ATTR_IS_DEPENDENT (t))
11557 {
11558 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11559 if (subst != t)
11560 {
11561 *p = subst;
11562 while (*p)
11563 p = &TREE_CHAIN (*p);
11564 *p = TREE_CHAIN (t);
11565 continue;
11566 }
11567 }
11568 p = &TREE_CHAIN (*p);
11569 }
11570
11571 return attributes;
11572 }
11573
11574 /* Apply any attributes which had to be deferred until instantiation
11575 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11576 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11577
11578 static void
11579 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11580 tree args, tsubst_flags_t complain, tree in_decl)
11581 {
11582 tree last_dep = NULL_TREE;
11583 tree t;
11584 tree *p;
11585
11586 if (attributes == NULL_TREE)
11587 return;
11588
11589 if (DECL_P (*decl_p))
11590 {
11591 if (TREE_TYPE (*decl_p) == error_mark_node)
11592 return;
11593 p = &DECL_ATTRIBUTES (*decl_p);
11594 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11595 to our attributes parameter. */
11596 gcc_assert (*p == attributes);
11597 }
11598 else
11599 {
11600 p = &TYPE_ATTRIBUTES (*decl_p);
11601 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11602 lookup_template_class_1, and should be preserved. */
11603 gcc_assert (*p != attributes);
11604 while (*p)
11605 p = &TREE_CHAIN (*p);
11606 }
11607
11608 for (t = attributes; t; t = TREE_CHAIN (t))
11609 if (ATTR_IS_DEPENDENT (t))
11610 {
11611 last_dep = t;
11612 attributes = copy_list (attributes);
11613 break;
11614 }
11615
11616 *p = attributes;
11617 if (last_dep)
11618 {
11619 tree late_attrs = NULL_TREE;
11620 tree *q = &late_attrs;
11621
11622 for (; *p; )
11623 {
11624 t = *p;
11625 if (ATTR_IS_DEPENDENT (t))
11626 {
11627 *p = TREE_CHAIN (t);
11628 TREE_CHAIN (t) = NULL_TREE;
11629 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11630 while (*q)
11631 q = &TREE_CHAIN (*q);
11632 }
11633 else
11634 p = &TREE_CHAIN (t);
11635 }
11636
11637 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11638 }
11639 }
11640
11641 /* The template TMPL is being instantiated with the template arguments TARGS.
11642 Perform the access checks that we deferred when parsing the template. */
11643
11644 static void
11645 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11646 {
11647 unsigned i;
11648 deferred_access_check *chk;
11649
11650 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11651 return;
11652
11653 if (vec<deferred_access_check, va_gc> *access_checks
11654 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11655 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11656 {
11657 tree decl = chk->decl;
11658 tree diag_decl = chk->diag_decl;
11659 tree type_scope = TREE_TYPE (chk->binfo);
11660
11661 if (uses_template_parms (type_scope))
11662 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11663
11664 /* Make access check error messages point to the location
11665 of the use of the typedef. */
11666 iloc_sentinel ils (chk->loc);
11667 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11668 decl, diag_decl, tf_warning_or_error);
11669 }
11670 }
11671
11672 static tree
11673 instantiate_class_template_1 (tree type)
11674 {
11675 tree templ, args, pattern, t, member;
11676 tree typedecl;
11677 tree pbinfo;
11678 tree base_list;
11679 unsigned int saved_maximum_field_alignment;
11680 tree fn_context;
11681
11682 if (type == error_mark_node)
11683 return error_mark_node;
11684
11685 if (COMPLETE_OR_OPEN_TYPE_P (type)
11686 || uses_template_parms (type))
11687 return type;
11688
11689 /* Figure out which template is being instantiated. */
11690 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11691 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11692
11693 /* Mark the type as in the process of being defined. */
11694 TYPE_BEING_DEFINED (type) = 1;
11695
11696 /* We may be in the middle of deferred access check. Disable
11697 it now. */
11698 deferring_access_check_sentinel acs (dk_no_deferred);
11699
11700 /* Determine what specialization of the original template to
11701 instantiate. */
11702 t = most_specialized_partial_spec (type, tf_warning_or_error);
11703 if (t == error_mark_node)
11704 return error_mark_node;
11705 else if (t)
11706 {
11707 /* This TYPE is actually an instantiation of a partial
11708 specialization. We replace the innermost set of ARGS with
11709 the arguments appropriate for substitution. For example,
11710 given:
11711
11712 template <class T> struct S {};
11713 template <class T> struct S<T*> {};
11714
11715 and supposing that we are instantiating S<int*>, ARGS will
11716 presently be {int*} -- but we need {int}. */
11717 pattern = TREE_TYPE (t);
11718 args = TREE_PURPOSE (t);
11719 }
11720 else
11721 {
11722 pattern = TREE_TYPE (templ);
11723 args = CLASSTYPE_TI_ARGS (type);
11724 }
11725
11726 /* If the template we're instantiating is incomplete, then clearly
11727 there's nothing we can do. */
11728 if (!COMPLETE_TYPE_P (pattern))
11729 {
11730 /* We can try again later. */
11731 TYPE_BEING_DEFINED (type) = 0;
11732 return type;
11733 }
11734
11735 /* If we've recursively instantiated too many templates, stop. */
11736 if (! push_tinst_level (type))
11737 return type;
11738
11739 int saved_unevaluated_operand = cp_unevaluated_operand;
11740 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11741
11742 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11743 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11744 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11745 fn_context = error_mark_node;
11746 if (!fn_context)
11747 push_to_top_level ();
11748 else
11749 {
11750 cp_unevaluated_operand = 0;
11751 c_inhibit_evaluation_warnings = 0;
11752 }
11753 /* Use #pragma pack from the template context. */
11754 saved_maximum_field_alignment = maximum_field_alignment;
11755 maximum_field_alignment = TYPE_PRECISION (pattern);
11756
11757 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11758
11759 /* Set the input location to the most specialized template definition.
11760 This is needed if tsubsting causes an error. */
11761 typedecl = TYPE_MAIN_DECL (pattern);
11762 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11763 DECL_SOURCE_LOCATION (typedecl);
11764
11765 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11766 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11767 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11768 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11769 if (ANON_AGGR_TYPE_P (pattern))
11770 SET_ANON_AGGR_TYPE_P (type);
11771 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11772 {
11773 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11774 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11775 /* Adjust visibility for template arguments. */
11776 determine_visibility (TYPE_MAIN_DECL (type));
11777 }
11778 if (CLASS_TYPE_P (type))
11779 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11780
11781 pbinfo = TYPE_BINFO (pattern);
11782
11783 /* We should never instantiate a nested class before its enclosing
11784 class; we need to look up the nested class by name before we can
11785 instantiate it, and that lookup should instantiate the enclosing
11786 class. */
11787 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11788 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11789
11790 base_list = NULL_TREE;
11791 if (BINFO_N_BASE_BINFOS (pbinfo))
11792 {
11793 tree pbase_binfo;
11794 tree pushed_scope;
11795 int i;
11796
11797 /* We must enter the scope containing the type, as that is where
11798 the accessibility of types named in dependent bases are
11799 looked up from. */
11800 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11801
11802 /* Substitute into each of the bases to determine the actual
11803 basetypes. */
11804 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11805 {
11806 tree base;
11807 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11808 tree expanded_bases = NULL_TREE;
11809 int idx, len = 1;
11810
11811 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11812 {
11813 expanded_bases =
11814 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11815 args, tf_error, NULL_TREE);
11816 if (expanded_bases == error_mark_node)
11817 continue;
11818
11819 len = TREE_VEC_LENGTH (expanded_bases);
11820 }
11821
11822 for (idx = 0; idx < len; idx++)
11823 {
11824 if (expanded_bases)
11825 /* Extract the already-expanded base class. */
11826 base = TREE_VEC_ELT (expanded_bases, idx);
11827 else
11828 /* Substitute to figure out the base class. */
11829 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11830 NULL_TREE);
11831
11832 if (base == error_mark_node)
11833 continue;
11834
11835 base_list = tree_cons (access, base, base_list);
11836 if (BINFO_VIRTUAL_P (pbase_binfo))
11837 TREE_TYPE (base_list) = integer_type_node;
11838 }
11839 }
11840
11841 /* The list is now in reverse order; correct that. */
11842 base_list = nreverse (base_list);
11843
11844 if (pushed_scope)
11845 pop_scope (pushed_scope);
11846 }
11847 /* Now call xref_basetypes to set up all the base-class
11848 information. */
11849 xref_basetypes (type, base_list);
11850
11851 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11852 (int) ATTR_FLAG_TYPE_IN_PLACE,
11853 args, tf_error, NULL_TREE);
11854 fixup_attribute_variants (type);
11855
11856 /* Now that our base classes are set up, enter the scope of the
11857 class, so that name lookups into base classes, etc. will work
11858 correctly. This is precisely analogous to what we do in
11859 begin_class_definition when defining an ordinary non-template
11860 class, except we also need to push the enclosing classes. */
11861 push_nested_class (type);
11862
11863 /* Now members are processed in the order of declaration. */
11864 for (member = CLASSTYPE_DECL_LIST (pattern);
11865 member; member = TREE_CHAIN (member))
11866 {
11867 tree t = TREE_VALUE (member);
11868
11869 if (TREE_PURPOSE (member))
11870 {
11871 if (TYPE_P (t))
11872 {
11873 if (LAMBDA_TYPE_P (t))
11874 /* A closure type for a lambda in an NSDMI or default argument.
11875 Ignore it; it will be regenerated when needed. */
11876 continue;
11877
11878 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11879 && TYPE_LANG_SPECIFIC (t)
11880 && CLASSTYPE_IS_TEMPLATE (t));
11881
11882 /* If the member is a class template, then -- even after
11883 substitution -- there may be dependent types in the
11884 template argument list for the class. We increment
11885 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11886 that function will assume that no types are dependent
11887 when outside of a template. */
11888 if (class_template_p)
11889 ++processing_template_decl;
11890 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11891 if (class_template_p)
11892 --processing_template_decl;
11893 if (newtag == error_mark_node)
11894 continue;
11895
11896 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11897 {
11898 tree name = TYPE_IDENTIFIER (t);
11899
11900 if (class_template_p)
11901 /* Unfortunately, lookup_template_class sets
11902 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11903 instantiation (i.e., for the type of a member
11904 template class nested within a template class.)
11905 This behavior is required for
11906 maybe_process_partial_specialization to work
11907 correctly, but is not accurate in this case;
11908 the TAG is not an instantiation of anything.
11909 (The corresponding TEMPLATE_DECL is an
11910 instantiation, but the TYPE is not.) */
11911 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11912
11913 /* Now, we call pushtag to put this NEWTAG into the scope of
11914 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11915 pushtag calling push_template_decl. We don't have to do
11916 this for enums because it will already have been done in
11917 tsubst_enum. */
11918 if (name)
11919 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11920 pushtag (name, newtag);
11921 }
11922 }
11923 else if (DECL_DECLARES_FUNCTION_P (t))
11924 {
11925 tree r;
11926
11927 if (TREE_CODE (t) == TEMPLATE_DECL)
11928 ++processing_template_decl;
11929 r = tsubst (t, args, tf_error, NULL_TREE);
11930 if (TREE_CODE (t) == TEMPLATE_DECL)
11931 --processing_template_decl;
11932 set_current_access_from_decl (r);
11933 finish_member_declaration (r);
11934 /* Instantiate members marked with attribute used. */
11935 if (r != error_mark_node && DECL_PRESERVE_P (r))
11936 mark_used (r);
11937 if (TREE_CODE (r) == FUNCTION_DECL
11938 && DECL_OMP_DECLARE_REDUCTION_P (r))
11939 cp_check_omp_declare_reduction (r);
11940 }
11941 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11942 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11943 /* A closure type for a lambda in an NSDMI or default argument.
11944 Ignore it; it will be regenerated when needed. */;
11945 else
11946 {
11947 /* Build new TYPE_FIELDS. */
11948 if (TREE_CODE (t) == STATIC_ASSERT)
11949 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11950 /*integral_constant_expression_p=*/true);
11951 else if (TREE_CODE (t) != CONST_DECL)
11952 {
11953 tree r;
11954 tree vec = NULL_TREE;
11955 int len = 1;
11956
11957 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
11958 /* The file and line for this declaration, to
11959 assist in error message reporting. Since we
11960 called push_tinst_level above, we don't need to
11961 restore these. */
11962 input_location = DECL_SOURCE_LOCATION (t);
11963
11964 if (TREE_CODE (t) == TEMPLATE_DECL)
11965 ++processing_template_decl;
11966 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11967 if (TREE_CODE (t) == TEMPLATE_DECL)
11968 --processing_template_decl;
11969
11970 if (TREE_CODE (r) == TREE_VEC)
11971 {
11972 /* A capture pack became multiple fields. */
11973 vec = r;
11974 len = TREE_VEC_LENGTH (vec);
11975 }
11976
11977 for (int i = 0; i < len; ++i)
11978 {
11979 if (vec)
11980 r = TREE_VEC_ELT (vec, i);
11981 if (VAR_P (r))
11982 {
11983 /* In [temp.inst]:
11984
11985 [t]he initialization (and any associated
11986 side-effects) of a static data member does
11987 not occur unless the static data member is
11988 itself used in a way that requires the
11989 definition of the static data member to
11990 exist.
11991
11992 Therefore, we do not substitute into the
11993 initialized for the static data member here. */
11994 finish_static_data_member_decl
11995 (r,
11996 /*init=*/NULL_TREE,
11997 /*init_const_expr_p=*/false,
11998 /*asmspec_tree=*/NULL_TREE,
11999 /*flags=*/0);
12000 /* Instantiate members marked with attribute used. */
12001 if (r != error_mark_node && DECL_PRESERVE_P (r))
12002 mark_used (r);
12003 }
12004 else if (TREE_CODE (r) == FIELD_DECL)
12005 {
12006 /* Determine whether R has a valid type and can be
12007 completed later. If R is invalid, then its type
12008 is replaced by error_mark_node. */
12009 tree rtype = TREE_TYPE (r);
12010 if (can_complete_type_without_circularity (rtype))
12011 complete_type (rtype);
12012
12013 if (!complete_or_array_type_p (rtype))
12014 {
12015 /* If R's type couldn't be completed and
12016 it isn't a flexible array member (whose
12017 type is incomplete by definition) give
12018 an error. */
12019 cxx_incomplete_type_error (r, rtype);
12020 TREE_TYPE (r) = error_mark_node;
12021 }
12022 else if (TREE_CODE (rtype) == ARRAY_TYPE
12023 && TYPE_DOMAIN (rtype) == NULL_TREE
12024 && (TREE_CODE (type) == UNION_TYPE
12025 || TREE_CODE (type) == QUAL_UNION_TYPE))
12026 {
12027 error ("flexible array member %qD in union", r);
12028 TREE_TYPE (r) = error_mark_node;
12029 }
12030 else if (!verify_type_context (input_location,
12031 TCTX_FIELD, rtype))
12032 TREE_TYPE (r) = error_mark_node;
12033 }
12034
12035 /* If it is a TYPE_DECL for a class-scoped
12036 ENUMERAL_TYPE, such a thing will already have
12037 been added to the field list by tsubst_enum
12038 in finish_member_declaration case above. */
12039 if (!(TREE_CODE (r) == TYPE_DECL
12040 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12041 && DECL_ARTIFICIAL (r)))
12042 {
12043 set_current_access_from_decl (r);
12044 finish_member_declaration (r);
12045 }
12046 }
12047 }
12048 }
12049 }
12050 else
12051 {
12052 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12053 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12054 {
12055 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12056
12057 tree friend_type = t;
12058 bool adjust_processing_template_decl = false;
12059
12060 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12061 {
12062 /* template <class T> friend class C; */
12063 friend_type = tsubst_friend_class (friend_type, args);
12064 adjust_processing_template_decl = true;
12065 }
12066 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12067 {
12068 /* template <class T> friend class C::D; */
12069 friend_type = tsubst (friend_type, args,
12070 tf_warning_or_error, NULL_TREE);
12071 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12072 friend_type = TREE_TYPE (friend_type);
12073 adjust_processing_template_decl = true;
12074 }
12075 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12076 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12077 {
12078 /* This could be either
12079
12080 friend class T::C;
12081
12082 when dependent_type_p is false or
12083
12084 template <class U> friend class T::C;
12085
12086 otherwise. */
12087 /* Bump processing_template_decl in case this is something like
12088 template <class T> friend struct A<T>::B. */
12089 ++processing_template_decl;
12090 friend_type = tsubst (friend_type, args,
12091 tf_warning_or_error, NULL_TREE);
12092 if (dependent_type_p (friend_type))
12093 adjust_processing_template_decl = true;
12094 --processing_template_decl;
12095 }
12096 else if (uses_template_parms (friend_type))
12097 /* friend class C<T>; */
12098 friend_type = tsubst (friend_type, args,
12099 tf_warning_or_error, NULL_TREE);
12100
12101 /* Otherwise it's
12102
12103 friend class C;
12104
12105 where C is already declared or
12106
12107 friend class C<int>;
12108
12109 We don't have to do anything in these cases. */
12110
12111 if (adjust_processing_template_decl)
12112 /* Trick make_friend_class into realizing that the friend
12113 we're adding is a template, not an ordinary class. It's
12114 important that we use make_friend_class since it will
12115 perform some error-checking and output cross-reference
12116 information. */
12117 ++processing_template_decl;
12118
12119 if (friend_type != error_mark_node)
12120 make_friend_class (type, friend_type, /*complain=*/false);
12121
12122 if (adjust_processing_template_decl)
12123 --processing_template_decl;
12124 }
12125 else
12126 {
12127 /* Build new DECL_FRIENDLIST. */
12128 tree r;
12129
12130 /* The file and line for this declaration, to
12131 assist in error message reporting. Since we
12132 called push_tinst_level above, we don't need to
12133 restore these. */
12134 input_location = DECL_SOURCE_LOCATION (t);
12135
12136 if (TREE_CODE (t) == TEMPLATE_DECL)
12137 {
12138 ++processing_template_decl;
12139 push_deferring_access_checks (dk_no_check);
12140 }
12141
12142 r = tsubst_friend_function (t, args);
12143 add_friend (type, r, /*complain=*/false);
12144 if (TREE_CODE (t) == TEMPLATE_DECL)
12145 {
12146 pop_deferring_access_checks ();
12147 --processing_template_decl;
12148 }
12149 }
12150 }
12151 }
12152
12153 if (fn_context)
12154 {
12155 /* Restore these before substituting into the lambda capture
12156 initializers. */
12157 cp_unevaluated_operand = saved_unevaluated_operand;
12158 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12159 }
12160
12161 /* Set the file and line number information to whatever is given for
12162 the class itself. This puts error messages involving generated
12163 implicit functions at a predictable point, and the same point
12164 that would be used for non-template classes. */
12165 input_location = DECL_SOURCE_LOCATION (typedecl);
12166
12167 unreverse_member_declarations (type);
12168 finish_struct_1 (type);
12169 TYPE_BEING_DEFINED (type) = 0;
12170
12171 /* We don't instantiate default arguments for member functions. 14.7.1:
12172
12173 The implicit instantiation of a class template specialization causes
12174 the implicit instantiation of the declarations, but not of the
12175 definitions or default arguments, of the class member functions,
12176 member classes, static data members and member templates.... */
12177
12178 perform_instantiation_time_access_checks (pattern, args);
12179 perform_deferred_access_checks (tf_warning_or_error);
12180 pop_nested_class ();
12181 maximum_field_alignment = saved_maximum_field_alignment;
12182 if (!fn_context)
12183 pop_from_top_level ();
12184 pop_tinst_level ();
12185
12186 /* The vtable for a template class can be emitted in any translation
12187 unit in which the class is instantiated. When there is no key
12188 method, however, finish_struct_1 will already have added TYPE to
12189 the keyed_classes. */
12190 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12191 vec_safe_push (keyed_classes, type);
12192
12193 return type;
12194 }
12195
12196 /* Wrapper for instantiate_class_template_1. */
12197
12198 tree
12199 instantiate_class_template (tree type)
12200 {
12201 tree ret;
12202 timevar_push (TV_TEMPLATE_INST);
12203 ret = instantiate_class_template_1 (type);
12204 timevar_pop (TV_TEMPLATE_INST);
12205 return ret;
12206 }
12207
12208 tree
12209 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12210 {
12211 tree r;
12212
12213 if (!t)
12214 r = t;
12215 else if (TYPE_P (t))
12216 r = tsubst (t, args, complain, in_decl);
12217 else
12218 {
12219 if (!(complain & tf_warning))
12220 ++c_inhibit_evaluation_warnings;
12221 r = tsubst_expr (t, args, complain, in_decl,
12222 /*integral_constant_expression_p=*/true);
12223 if (!(complain & tf_warning))
12224 --c_inhibit_evaluation_warnings;
12225 }
12226
12227 return r;
12228 }
12229
12230 /* Given a function parameter pack TMPL_PARM and some function parameters
12231 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12232 and set *SPEC_P to point at the next point in the list. */
12233
12234 tree
12235 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12236 {
12237 /* Collect all of the extra "packed" parameters into an
12238 argument pack. */
12239 tree parmvec;
12240 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12241 tree spec_parm = *spec_p;
12242 int i, len;
12243
12244 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12245 if (tmpl_parm
12246 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12247 break;
12248
12249 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12250 parmvec = make_tree_vec (len);
12251 spec_parm = *spec_p;
12252 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12253 {
12254 tree elt = spec_parm;
12255 if (DECL_PACK_P (elt))
12256 elt = make_pack_expansion (elt);
12257 TREE_VEC_ELT (parmvec, i) = elt;
12258 }
12259
12260 /* Build the argument packs. */
12261 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12262 *spec_p = spec_parm;
12263
12264 return argpack;
12265 }
12266
12267 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12268 NONTYPE_ARGUMENT_PACK. */
12269
12270 static tree
12271 make_fnparm_pack (tree spec_parm)
12272 {
12273 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12274 }
12275
12276 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12277 pack expansion with no extra args, 2 if it has extra args, or 0
12278 if it is not a pack expansion. */
12279
12280 static int
12281 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12282 {
12283 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12284 /* We're being called before this happens in tsubst_pack_expansion. */
12285 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12286 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12287 if (i >= TREE_VEC_LENGTH (vec))
12288 return 0;
12289 tree elt = TREE_VEC_ELT (vec, i);
12290 if (DECL_P (elt))
12291 /* A decl pack is itself an expansion. */
12292 elt = TREE_TYPE (elt);
12293 if (!PACK_EXPANSION_P (elt))
12294 return 0;
12295 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12296 return 2;
12297 return 1;
12298 }
12299
12300
12301 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12302
12303 static tree
12304 make_argument_pack_select (tree arg_pack, unsigned index)
12305 {
12306 tree aps = make_node (ARGUMENT_PACK_SELECT);
12307
12308 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12309 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12310
12311 return aps;
12312 }
12313
12314 /* This is a subroutine of tsubst_pack_expansion.
12315
12316 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12317 mechanism to store the (non complete list of) arguments of the
12318 substitution and return a non substituted pack expansion, in order
12319 to wait for when we have enough arguments to really perform the
12320 substitution. */
12321
12322 static bool
12323 use_pack_expansion_extra_args_p (tree parm_packs,
12324 int arg_pack_len,
12325 bool has_empty_arg)
12326 {
12327 /* If one pack has an expansion and another pack has a normal
12328 argument or if one pack has an empty argument and an another
12329 one hasn't then tsubst_pack_expansion cannot perform the
12330 substitution and need to fall back on the
12331 PACK_EXPANSION_EXTRA mechanism. */
12332 if (parm_packs == NULL_TREE)
12333 return false;
12334 else if (has_empty_arg)
12335 {
12336 /* If all the actual packs are pack expansions, we can still
12337 subsitute directly. */
12338 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12339 {
12340 tree a = TREE_VALUE (p);
12341 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12342 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12343 a = ARGUMENT_PACK_ARGS (a);
12344 if (TREE_VEC_LENGTH (a) == 1)
12345 a = TREE_VEC_ELT (a, 0);
12346 if (PACK_EXPANSION_P (a))
12347 continue;
12348 return true;
12349 }
12350 return false;
12351 }
12352
12353 bool has_expansion_arg = false;
12354 for (int i = 0 ; i < arg_pack_len; ++i)
12355 {
12356 bool has_non_expansion_arg = false;
12357 for (tree parm_pack = parm_packs;
12358 parm_pack;
12359 parm_pack = TREE_CHAIN (parm_pack))
12360 {
12361 tree arg = TREE_VALUE (parm_pack);
12362
12363 int exp = argument_pack_element_is_expansion_p (arg, i);
12364 if (exp == 2)
12365 /* We can't substitute a pack expansion with extra args into
12366 our pattern. */
12367 return true;
12368 else if (exp)
12369 has_expansion_arg = true;
12370 else
12371 has_non_expansion_arg = true;
12372 }
12373
12374 if (has_expansion_arg && has_non_expansion_arg)
12375 return true;
12376 }
12377 return false;
12378 }
12379
12380 /* [temp.variadic]/6 says that:
12381
12382 The instantiation of a pack expansion [...]
12383 produces a list E1,E2, ..., En, where N is the number of elements
12384 in the pack expansion parameters.
12385
12386 This subroutine of tsubst_pack_expansion produces one of these Ei.
12387
12388 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12389 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12390 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12391 INDEX is the index 'i' of the element Ei to produce. ARGS,
12392 COMPLAIN, and IN_DECL are the same parameters as for the
12393 tsubst_pack_expansion function.
12394
12395 The function returns the resulting Ei upon successful completion,
12396 or error_mark_node.
12397
12398 Note that this function possibly modifies the ARGS parameter, so
12399 it's the responsibility of the caller to restore it. */
12400
12401 static tree
12402 gen_elem_of_pack_expansion_instantiation (tree pattern,
12403 tree parm_packs,
12404 unsigned index,
12405 tree args /* This parm gets
12406 modified. */,
12407 tsubst_flags_t complain,
12408 tree in_decl)
12409 {
12410 tree t;
12411 bool ith_elem_is_expansion = false;
12412
12413 /* For each parameter pack, change the substitution of the parameter
12414 pack to the ith argument in its argument pack, then expand the
12415 pattern. */
12416 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12417 {
12418 tree parm = TREE_PURPOSE (pack);
12419 tree arg_pack = TREE_VALUE (pack);
12420 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12421
12422 ith_elem_is_expansion |=
12423 argument_pack_element_is_expansion_p (arg_pack, index);
12424
12425 /* Select the Ith argument from the pack. */
12426 if (TREE_CODE (parm) == PARM_DECL
12427 || VAR_P (parm)
12428 || TREE_CODE (parm) == FIELD_DECL)
12429 {
12430 if (index == 0)
12431 {
12432 aps = make_argument_pack_select (arg_pack, index);
12433 if (!mark_used (parm, complain) && !(complain & tf_error))
12434 return error_mark_node;
12435 register_local_specialization (aps, parm);
12436 }
12437 else
12438 aps = retrieve_local_specialization (parm);
12439 }
12440 else
12441 {
12442 int idx, level;
12443 template_parm_level_and_index (parm, &level, &idx);
12444
12445 if (index == 0)
12446 {
12447 aps = make_argument_pack_select (arg_pack, index);
12448 /* Update the corresponding argument. */
12449 TMPL_ARG (args, level, idx) = aps;
12450 }
12451 else
12452 /* Re-use the ARGUMENT_PACK_SELECT. */
12453 aps = TMPL_ARG (args, level, idx);
12454 }
12455 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12456 }
12457
12458 /* Substitute into the PATTERN with the (possibly altered)
12459 arguments. */
12460 if (pattern == in_decl)
12461 /* Expanding a fixed parameter pack from
12462 coerce_template_parameter_pack. */
12463 t = tsubst_decl (pattern, args, complain);
12464 else if (pattern == error_mark_node)
12465 t = error_mark_node;
12466 else if (!TYPE_P (pattern))
12467 t = tsubst_expr (pattern, args, complain, in_decl,
12468 /*integral_constant_expression_p=*/false);
12469 else
12470 t = tsubst (pattern, args, complain, in_decl);
12471
12472 /* If the Ith argument pack element is a pack expansion, then
12473 the Ith element resulting from the substituting is going to
12474 be a pack expansion as well. */
12475 if (ith_elem_is_expansion)
12476 t = make_pack_expansion (t, complain);
12477
12478 return t;
12479 }
12480
12481 /* When the unexpanded parameter pack in a fold expression expands to an empty
12482 sequence, the value of the expression is as follows; the program is
12483 ill-formed if the operator is not listed in this table.
12484
12485 && true
12486 || false
12487 , void() */
12488
12489 tree
12490 expand_empty_fold (tree t, tsubst_flags_t complain)
12491 {
12492 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12493 if (!FOLD_EXPR_MODIFY_P (t))
12494 switch (code)
12495 {
12496 case TRUTH_ANDIF_EXPR:
12497 return boolean_true_node;
12498 case TRUTH_ORIF_EXPR:
12499 return boolean_false_node;
12500 case COMPOUND_EXPR:
12501 return void_node;
12502 default:
12503 break;
12504 }
12505
12506 if (complain & tf_error)
12507 error_at (location_of (t),
12508 "fold of empty expansion over %O", code);
12509 return error_mark_node;
12510 }
12511
12512 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12513 form an expression that combines the two terms using the
12514 operator of T. */
12515
12516 static tree
12517 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12518 {
12519 tree op = FOLD_EXPR_OP (t);
12520 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12521
12522 // Handle compound assignment operators.
12523 if (FOLD_EXPR_MODIFY_P (t))
12524 return build_x_modify_expr (input_location, left, code, right, complain);
12525
12526 warning_sentinel s(warn_parentheses);
12527 switch (code)
12528 {
12529 case COMPOUND_EXPR:
12530 return build_x_compound_expr (input_location, left, right, complain);
12531 default:
12532 return build_x_binary_op (input_location, code,
12533 left, TREE_CODE (left),
12534 right, TREE_CODE (right),
12535 /*overload=*/NULL,
12536 complain);
12537 }
12538 }
12539
12540 /* Substitute ARGS into the pack of a fold expression T. */
12541
12542 static inline tree
12543 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12544 {
12545 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12546 }
12547
12548 /* Substitute ARGS into the pack of a fold expression T. */
12549
12550 static inline tree
12551 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12552 {
12553 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12554 }
12555
12556 /* Expand a PACK of arguments into a grouped as left fold.
12557 Given a pack containing elements A0, A1, ..., An and an
12558 operator @, this builds the expression:
12559
12560 ((A0 @ A1) @ A2) ... @ An
12561
12562 Note that PACK must not be empty.
12563
12564 The operator is defined by the original fold expression T. */
12565
12566 static tree
12567 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12568 {
12569 tree left = TREE_VEC_ELT (pack, 0);
12570 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12571 {
12572 tree right = TREE_VEC_ELT (pack, i);
12573 left = fold_expression (t, left, right, complain);
12574 }
12575 return left;
12576 }
12577
12578 /* Substitute into a unary left fold expression. */
12579
12580 static tree
12581 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12582 tree in_decl)
12583 {
12584 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12585 if (pack == error_mark_node)
12586 return error_mark_node;
12587 if (PACK_EXPANSION_P (pack))
12588 {
12589 tree r = copy_node (t);
12590 FOLD_EXPR_PACK (r) = pack;
12591 return r;
12592 }
12593 if (TREE_VEC_LENGTH (pack) == 0)
12594 return expand_empty_fold (t, complain);
12595 else
12596 return expand_left_fold (t, pack, complain);
12597 }
12598
12599 /* Substitute into a binary left fold expression.
12600
12601 Do ths by building a single (non-empty) vector of argumnts and
12602 building the expression from those elements. */
12603
12604 static tree
12605 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12606 tree in_decl)
12607 {
12608 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12609 if (pack == error_mark_node)
12610 return error_mark_node;
12611 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12612 if (init == error_mark_node)
12613 return error_mark_node;
12614
12615 if (PACK_EXPANSION_P (pack))
12616 {
12617 tree r = copy_node (t);
12618 FOLD_EXPR_PACK (r) = pack;
12619 FOLD_EXPR_INIT (r) = init;
12620 return r;
12621 }
12622
12623 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12624 TREE_VEC_ELT (vec, 0) = init;
12625 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12626 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12627
12628 return expand_left_fold (t, vec, complain);
12629 }
12630
12631 /* Expand a PACK of arguments into a grouped as right fold.
12632 Given a pack containing elementns A0, A1, ..., and an
12633 operator @, this builds the expression:
12634
12635 A0@ ... (An-2 @ (An-1 @ An))
12636
12637 Note that PACK must not be empty.
12638
12639 The operator is defined by the original fold expression T. */
12640
12641 tree
12642 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12643 {
12644 // Build the expression.
12645 int n = TREE_VEC_LENGTH (pack);
12646 tree right = TREE_VEC_ELT (pack, n - 1);
12647 for (--n; n != 0; --n)
12648 {
12649 tree left = TREE_VEC_ELT (pack, n - 1);
12650 right = fold_expression (t, left, right, complain);
12651 }
12652 return right;
12653 }
12654
12655 /* Substitute into a unary right fold expression. */
12656
12657 static tree
12658 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12659 tree in_decl)
12660 {
12661 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12662 if (pack == error_mark_node)
12663 return error_mark_node;
12664 if (PACK_EXPANSION_P (pack))
12665 {
12666 tree r = copy_node (t);
12667 FOLD_EXPR_PACK (r) = pack;
12668 return r;
12669 }
12670 if (TREE_VEC_LENGTH (pack) == 0)
12671 return expand_empty_fold (t, complain);
12672 else
12673 return expand_right_fold (t, pack, complain);
12674 }
12675
12676 /* Substitute into a binary right fold expression.
12677
12678 Do ths by building a single (non-empty) vector of arguments and
12679 building the expression from those elements. */
12680
12681 static tree
12682 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12683 tree in_decl)
12684 {
12685 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12686 if (pack == error_mark_node)
12687 return error_mark_node;
12688 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12689 if (init == error_mark_node)
12690 return error_mark_node;
12691
12692 if (PACK_EXPANSION_P (pack))
12693 {
12694 tree r = copy_node (t);
12695 FOLD_EXPR_PACK (r) = pack;
12696 FOLD_EXPR_INIT (r) = init;
12697 return r;
12698 }
12699
12700 int n = TREE_VEC_LENGTH (pack);
12701 tree vec = make_tree_vec (n + 1);
12702 for (int i = 0; i < n; ++i)
12703 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12704 TREE_VEC_ELT (vec, n) = init;
12705
12706 return expand_right_fold (t, vec, complain);
12707 }
12708
12709 /* Walk through the pattern of a pack expansion, adding everything in
12710 local_specializations to a list. */
12711
12712 class el_data
12713 {
12714 public:
12715 hash_set<tree> internal;
12716 tree extra;
12717 tsubst_flags_t complain;
12718
12719 el_data (tsubst_flags_t c)
12720 : extra (NULL_TREE), complain (c) {}
12721 };
12722 static tree
12723 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12724 {
12725 el_data &data = *reinterpret_cast<el_data*>(data_);
12726 tree *extra = &data.extra;
12727 tsubst_flags_t complain = data.complain;
12728
12729 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12730 /* Remember local typedefs (85214). */
12731 tp = &TYPE_NAME (*tp);
12732
12733 if (TREE_CODE (*tp) == DECL_EXPR)
12734 data.internal.add (DECL_EXPR_DECL (*tp));
12735 else if (tree spec = retrieve_local_specialization (*tp))
12736 {
12737 if (data.internal.contains (*tp))
12738 /* Don't mess with variables declared within the pattern. */
12739 return NULL_TREE;
12740 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12741 {
12742 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12743 tree args = ARGUMENT_PACK_ARGS (spec);
12744 if (TREE_VEC_LENGTH (args) == 1)
12745 {
12746 tree elt = TREE_VEC_ELT (args, 0);
12747 if (PACK_EXPANSION_P (elt))
12748 elt = PACK_EXPANSION_PATTERN (elt);
12749 if (DECL_PACK_P (elt))
12750 spec = elt;
12751 }
12752 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12753 {
12754 /* Handle lambda capture here, since we aren't doing any
12755 substitution now, and so tsubst_copy won't call
12756 process_outer_var_ref. */
12757 tree args = ARGUMENT_PACK_ARGS (spec);
12758 int len = TREE_VEC_LENGTH (args);
12759 for (int i = 0; i < len; ++i)
12760 {
12761 tree arg = TREE_VEC_ELT (args, i);
12762 tree carg = arg;
12763 if (outer_automatic_var_p (arg))
12764 carg = process_outer_var_ref (arg, complain);
12765 if (carg != arg)
12766 {
12767 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12768 proxies. */
12769 if (i == 0)
12770 {
12771 spec = copy_node (spec);
12772 args = copy_node (args);
12773 SET_ARGUMENT_PACK_ARGS (spec, args);
12774 register_local_specialization (spec, *tp);
12775 }
12776 TREE_VEC_ELT (args, i) = carg;
12777 }
12778 }
12779 }
12780 }
12781 if (outer_automatic_var_p (spec))
12782 spec = process_outer_var_ref (spec, complain);
12783 *extra = tree_cons (*tp, spec, *extra);
12784 }
12785 return NULL_TREE;
12786 }
12787 static tree
12788 extract_local_specs (tree pattern, tsubst_flags_t complain)
12789 {
12790 el_data data (complain);
12791 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12792 return data.extra;
12793 }
12794
12795 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12796 for use in PACK_EXPANSION_EXTRA_ARGS. */
12797
12798 tree
12799 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12800 {
12801 tree extra = args;
12802 if (local_specializations)
12803 if (tree locals = extract_local_specs (pattern, complain))
12804 extra = tree_cons (NULL_TREE, extra, locals);
12805 return extra;
12806 }
12807
12808 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12809 normal template args to ARGS. */
12810
12811 tree
12812 add_extra_args (tree extra, tree args)
12813 {
12814 if (extra && TREE_CODE (extra) == TREE_LIST)
12815 {
12816 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12817 {
12818 /* The partial instantiation involved local declarations collected in
12819 extract_local_specs; map from the general template to our local
12820 context. */
12821 tree gen = TREE_PURPOSE (elt);
12822 tree inst = TREE_VALUE (elt);
12823 if (DECL_P (inst))
12824 if (tree local = retrieve_local_specialization (inst))
12825 inst = local;
12826 /* else inst is already a full instantiation of the pack. */
12827 register_local_specialization (inst, gen);
12828 }
12829 gcc_assert (!TREE_PURPOSE (extra));
12830 extra = TREE_VALUE (extra);
12831 }
12832 #if 1
12833 /* I think we should always be able to substitute dependent args into the
12834 pattern. If that turns out to be incorrect in some cases, enable the
12835 alternate code (and add complain/in_decl parms to this function). */
12836 gcc_checking_assert (!uses_template_parms (extra));
12837 #else
12838 if (!uses_template_parms (extra))
12839 {
12840 gcc_unreachable ();
12841 extra = tsubst_template_args (extra, args, complain, in_decl);
12842 args = add_outermost_template_args (args, extra);
12843 }
12844 else
12845 #endif
12846 args = add_to_template_args (extra, args);
12847 return args;
12848 }
12849
12850 /* Substitute ARGS into T, which is an pack expansion
12851 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12852 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12853 (if only a partial substitution could be performed) or
12854 ERROR_MARK_NODE if there was an error. */
12855 tree
12856 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12857 tree in_decl)
12858 {
12859 tree pattern;
12860 tree pack, packs = NULL_TREE;
12861 bool unsubstituted_packs = false;
12862 int i, len = -1;
12863 tree result;
12864 bool need_local_specializations = false;
12865 int levels;
12866
12867 gcc_assert (PACK_EXPANSION_P (t));
12868 pattern = PACK_EXPANSION_PATTERN (t);
12869
12870 /* Add in any args remembered from an earlier partial instantiation. */
12871 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12872
12873 levels = TMPL_ARGS_DEPTH (args);
12874
12875 /* Determine the argument packs that will instantiate the parameter
12876 packs used in the expansion expression. While we're at it,
12877 compute the number of arguments to be expanded and make sure it
12878 is consistent. */
12879 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12880 pack = TREE_CHAIN (pack))
12881 {
12882 tree parm_pack = TREE_VALUE (pack);
12883 tree arg_pack = NULL_TREE;
12884 tree orig_arg = NULL_TREE;
12885 int level = 0;
12886
12887 if (TREE_CODE (parm_pack) == BASES)
12888 {
12889 gcc_assert (parm_pack == pattern);
12890 if (BASES_DIRECT (parm_pack))
12891 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12892 args, complain,
12893 in_decl, false),
12894 complain);
12895 else
12896 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12897 args, complain, in_decl,
12898 false), complain);
12899 }
12900 else if (builtin_pack_call_p (parm_pack))
12901 {
12902 if (parm_pack != pattern)
12903 {
12904 if (complain & tf_error)
12905 sorry ("%qE is not the entire pattern of the pack expansion",
12906 parm_pack);
12907 return error_mark_node;
12908 }
12909 return expand_builtin_pack_call (parm_pack, args,
12910 complain, in_decl);
12911 }
12912 else if (TREE_CODE (parm_pack) == PARM_DECL)
12913 {
12914 /* We know we have correct local_specializations if this
12915 expansion is at function scope, or if we're dealing with a
12916 local parameter in a requires expression; for the latter,
12917 tsubst_requires_expr set it up appropriately. */
12918 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12919 arg_pack = retrieve_local_specialization (parm_pack);
12920 else
12921 /* We can't rely on local_specializations for a parameter
12922 name used later in a function declaration (such as in a
12923 late-specified return type). Even if it exists, it might
12924 have the wrong value for a recursive call. */
12925 need_local_specializations = true;
12926
12927 if (!arg_pack)
12928 {
12929 /* This parameter pack was used in an unevaluated context. Just
12930 make a dummy decl, since it's only used for its type. */
12931 ++cp_unevaluated_operand;
12932 arg_pack = tsubst_decl (parm_pack, args, complain);
12933 --cp_unevaluated_operand;
12934 if (arg_pack && DECL_PACK_P (arg_pack))
12935 /* Partial instantiation of the parm_pack, we can't build
12936 up an argument pack yet. */
12937 arg_pack = NULL_TREE;
12938 else
12939 arg_pack = make_fnparm_pack (arg_pack);
12940 }
12941 else if (DECL_PACK_P (arg_pack))
12942 /* This argument pack isn't fully instantiated yet. */
12943 arg_pack = NULL_TREE;
12944 }
12945 else if (is_capture_proxy (parm_pack))
12946 {
12947 arg_pack = retrieve_local_specialization (parm_pack);
12948 if (DECL_PACK_P (arg_pack))
12949 arg_pack = NULL_TREE;
12950 }
12951 else
12952 {
12953 int idx;
12954 template_parm_level_and_index (parm_pack, &level, &idx);
12955 if (level <= levels)
12956 arg_pack = TMPL_ARG (args, level, idx);
12957
12958 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
12959 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
12960 arg_pack = NULL_TREE;
12961 }
12962
12963 orig_arg = arg_pack;
12964 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12965 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12966
12967 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12968 /* This can only happen if we forget to expand an argument
12969 pack somewhere else. Just return an error, silently. */
12970 {
12971 result = make_tree_vec (1);
12972 TREE_VEC_ELT (result, 0) = error_mark_node;
12973 return result;
12974 }
12975
12976 if (arg_pack)
12977 {
12978 int my_len =
12979 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12980
12981 /* Don't bother trying to do a partial substitution with
12982 incomplete packs; we'll try again after deduction. */
12983 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12984 return t;
12985
12986 if (len < 0)
12987 len = my_len;
12988 else if (len != my_len)
12989 {
12990 if (!(complain & tf_error))
12991 /* Fail quietly. */;
12992 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12993 error ("mismatched argument pack lengths while expanding %qT",
12994 pattern);
12995 else
12996 error ("mismatched argument pack lengths while expanding %qE",
12997 pattern);
12998 return error_mark_node;
12999 }
13000
13001 /* Keep track of the parameter packs and their corresponding
13002 argument packs. */
13003 packs = tree_cons (parm_pack, arg_pack, packs);
13004 TREE_TYPE (packs) = orig_arg;
13005 }
13006 else
13007 {
13008 /* We can't substitute for this parameter pack. We use a flag as
13009 well as the missing_level counter because function parameter
13010 packs don't have a level. */
13011 gcc_assert (processing_template_decl || is_auto (parm_pack));
13012 unsubstituted_packs = true;
13013 }
13014 }
13015
13016 /* If the expansion is just T..., return the matching argument pack, unless
13017 we need to call convert_from_reference on all the elements. This is an
13018 important optimization; see c++/68422. */
13019 if (!unsubstituted_packs
13020 && TREE_PURPOSE (packs) == pattern)
13021 {
13022 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13023
13024 /* If the argument pack is a single pack expansion, pull it out. */
13025 if (TREE_VEC_LENGTH (args) == 1
13026 && pack_expansion_args_count (args))
13027 return TREE_VEC_ELT (args, 0);
13028
13029 /* Types need no adjustment, nor does sizeof..., and if we still have
13030 some pack expansion args we won't do anything yet. */
13031 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13032 || PACK_EXPANSION_SIZEOF_P (t)
13033 || pack_expansion_args_count (args))
13034 return args;
13035 /* Also optimize expression pack expansions if we can tell that the
13036 elements won't have reference type. */
13037 tree type = TREE_TYPE (pattern);
13038 if (type && !TYPE_REF_P (type)
13039 && !PACK_EXPANSION_P (type)
13040 && !WILDCARD_TYPE_P (type))
13041 return args;
13042 /* Otherwise use the normal path so we get convert_from_reference. */
13043 }
13044
13045 /* We cannot expand this expansion expression, because we don't have
13046 all of the argument packs we need. */
13047 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
13048 {
13049 /* We got some full packs, but we can't substitute them in until we
13050 have values for all the packs. So remember these until then. */
13051
13052 t = make_pack_expansion (pattern, complain);
13053 PACK_EXPANSION_EXTRA_ARGS (t)
13054 = build_extra_args (pattern, args, complain);
13055 return t;
13056 }
13057
13058 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13059 type, so create our own local specializations map; the current map is
13060 either NULL or (in the case of recursive unification) might have
13061 bindings that we don't want to use or alter. */
13062 local_specialization_stack lss (need_local_specializations
13063 ? lss_blank : lss_nop);
13064
13065 if (unsubstituted_packs)
13066 {
13067 /* There were no real arguments, we're just replacing a parameter
13068 pack with another version of itself. Substitute into the
13069 pattern and return a PACK_EXPANSION_*. The caller will need to
13070 deal with that. */
13071 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13072 t = tsubst_expr (pattern, args, complain, in_decl,
13073 /*integral_constant_expression_p=*/false);
13074 else
13075 t = tsubst (pattern, args, complain, in_decl);
13076 t = make_pack_expansion (t, complain);
13077 return t;
13078 }
13079
13080 gcc_assert (len >= 0);
13081
13082 /* For each argument in each argument pack, substitute into the
13083 pattern. */
13084 result = make_tree_vec (len);
13085 tree elem_args = copy_template_args (args);
13086 for (i = 0; i < len; ++i)
13087 {
13088 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13089 i,
13090 elem_args, complain,
13091 in_decl);
13092 TREE_VEC_ELT (result, i) = t;
13093 if (t == error_mark_node)
13094 {
13095 result = error_mark_node;
13096 break;
13097 }
13098 }
13099
13100 /* Update ARGS to restore the substitution from parameter packs to
13101 their argument packs. */
13102 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13103 {
13104 tree parm = TREE_PURPOSE (pack);
13105
13106 if (TREE_CODE (parm) == PARM_DECL
13107 || VAR_P (parm)
13108 || TREE_CODE (parm) == FIELD_DECL)
13109 register_local_specialization (TREE_TYPE (pack), parm);
13110 else
13111 {
13112 int idx, level;
13113
13114 if (TREE_VALUE (pack) == NULL_TREE)
13115 continue;
13116
13117 template_parm_level_and_index (parm, &level, &idx);
13118
13119 /* Update the corresponding argument. */
13120 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13121 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13122 TREE_TYPE (pack);
13123 else
13124 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13125 }
13126 }
13127
13128 /* If the dependent pack arguments were such that we end up with only a
13129 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13130 if (len == 1 && TREE_CODE (result) == TREE_VEC
13131 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13132 return TREE_VEC_ELT (result, 0);
13133
13134 return result;
13135 }
13136
13137 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13138 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13139 parameter packs; all parms generated from a function parameter pack will
13140 have the same DECL_PARM_INDEX. */
13141
13142 tree
13143 get_pattern_parm (tree parm, tree tmpl)
13144 {
13145 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13146 tree patparm;
13147
13148 if (DECL_ARTIFICIAL (parm))
13149 {
13150 for (patparm = DECL_ARGUMENTS (pattern);
13151 patparm; patparm = DECL_CHAIN (patparm))
13152 if (DECL_ARTIFICIAL (patparm)
13153 && DECL_NAME (parm) == DECL_NAME (patparm))
13154 break;
13155 }
13156 else
13157 {
13158 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13159 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13160 gcc_assert (DECL_PARM_INDEX (patparm)
13161 == DECL_PARM_INDEX (parm));
13162 }
13163
13164 return patparm;
13165 }
13166
13167 /* Make an argument pack out of the TREE_VEC VEC. */
13168
13169 static tree
13170 make_argument_pack (tree vec)
13171 {
13172 tree pack;
13173
13174 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13175 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13176 else
13177 {
13178 pack = make_node (NONTYPE_ARGUMENT_PACK);
13179 TREE_CONSTANT (pack) = 1;
13180 }
13181 SET_ARGUMENT_PACK_ARGS (pack, vec);
13182 return pack;
13183 }
13184
13185 /* Return an exact copy of template args T that can be modified
13186 independently. */
13187
13188 static tree
13189 copy_template_args (tree t)
13190 {
13191 if (t == error_mark_node)
13192 return t;
13193
13194 int len = TREE_VEC_LENGTH (t);
13195 tree new_vec = make_tree_vec (len);
13196
13197 for (int i = 0; i < len; ++i)
13198 {
13199 tree elt = TREE_VEC_ELT (t, i);
13200 if (elt && TREE_CODE (elt) == TREE_VEC)
13201 elt = copy_template_args (elt);
13202 TREE_VEC_ELT (new_vec, i) = elt;
13203 }
13204
13205 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13206 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13207
13208 return new_vec;
13209 }
13210
13211 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13212
13213 tree
13214 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13215 tree in_decl)
13216 {
13217 /* Substitute into each of the arguments. */
13218 tree new_arg = TYPE_P (orig_arg)
13219 ? cxx_make_type (TREE_CODE (orig_arg))
13220 : make_node (TREE_CODE (orig_arg));
13221
13222 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13223 args, complain, in_decl);
13224 if (pack_args == error_mark_node)
13225 new_arg = error_mark_node;
13226 else
13227 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13228
13229 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13230 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13231
13232 return new_arg;
13233 }
13234
13235 /* Substitute ARGS into the vector or list of template arguments T. */
13236
13237 tree
13238 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13239 {
13240 tree orig_t = t;
13241 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13242 tree *elts;
13243
13244 if (t == error_mark_node)
13245 return error_mark_node;
13246
13247 len = TREE_VEC_LENGTH (t);
13248 elts = XALLOCAVEC (tree, len);
13249
13250 for (i = 0; i < len; i++)
13251 {
13252 tree orig_arg = TREE_VEC_ELT (t, i);
13253 tree new_arg;
13254
13255 if (TREE_CODE (orig_arg) == TREE_VEC)
13256 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13257 else if (PACK_EXPANSION_P (orig_arg))
13258 {
13259 /* Substitute into an expansion expression. */
13260 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13261
13262 if (TREE_CODE (new_arg) == TREE_VEC)
13263 /* Add to the expanded length adjustment the number of
13264 expanded arguments. We subtract one from this
13265 measurement, because the argument pack expression
13266 itself is already counted as 1 in
13267 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13268 the argument pack is empty. */
13269 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13270 }
13271 else if (ARGUMENT_PACK_P (orig_arg))
13272 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13273 else
13274 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13275
13276 if (new_arg == error_mark_node)
13277 return error_mark_node;
13278
13279 elts[i] = new_arg;
13280 if (new_arg != orig_arg)
13281 need_new = 1;
13282 }
13283
13284 if (!need_new)
13285 return t;
13286
13287 /* Make space for the expanded arguments coming from template
13288 argument packs. */
13289 t = make_tree_vec (len + expanded_len_adjust);
13290 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13291 arguments for a member template.
13292 In that case each TREE_VEC in ORIG_T represents a level of template
13293 arguments, and ORIG_T won't carry any non defaulted argument count.
13294 It will rather be the nested TREE_VECs that will carry one.
13295 In other words, ORIG_T carries a non defaulted argument count only
13296 if it doesn't contain any nested TREE_VEC. */
13297 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13298 {
13299 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13300 count += expanded_len_adjust;
13301 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13302 }
13303 for (i = 0, out = 0; i < len; i++)
13304 {
13305 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13306 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13307 && TREE_CODE (elts[i]) == TREE_VEC)
13308 {
13309 int idx;
13310
13311 /* Now expand the template argument pack "in place". */
13312 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13313 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13314 }
13315 else
13316 {
13317 TREE_VEC_ELT (t, out) = elts[i];
13318 out++;
13319 }
13320 }
13321
13322 return t;
13323 }
13324
13325 /* Substitute ARGS into one level PARMS of template parameters. */
13326
13327 static tree
13328 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13329 {
13330 if (parms == error_mark_node)
13331 return error_mark_node;
13332
13333 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13334
13335 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13336 {
13337 tree tuple = TREE_VEC_ELT (parms, i);
13338
13339 if (tuple == error_mark_node)
13340 continue;
13341
13342 TREE_VEC_ELT (new_vec, i) =
13343 tsubst_template_parm (tuple, args, complain);
13344 }
13345
13346 return new_vec;
13347 }
13348
13349 /* Return the result of substituting ARGS into the template parameters
13350 given by PARMS. If there are m levels of ARGS and m + n levels of
13351 PARMS, then the result will contain n levels of PARMS. For
13352 example, if PARMS is `template <class T> template <class U>
13353 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13354 result will be `template <int*, double, class V>'. */
13355
13356 static tree
13357 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13358 {
13359 tree r = NULL_TREE;
13360 tree* new_parms;
13361
13362 /* When substituting into a template, we must set
13363 PROCESSING_TEMPLATE_DECL as the template parameters may be
13364 dependent if they are based on one-another, and the dependency
13365 predicates are short-circuit outside of templates. */
13366 ++processing_template_decl;
13367
13368 for (new_parms = &r;
13369 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13370 new_parms = &(TREE_CHAIN (*new_parms)),
13371 parms = TREE_CHAIN (parms))
13372 {
13373 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13374 args, complain);
13375 *new_parms =
13376 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13377 - TMPL_ARGS_DEPTH (args)),
13378 new_vec, NULL_TREE);
13379 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13380 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13381 }
13382
13383 --processing_template_decl;
13384
13385 return r;
13386 }
13387
13388 /* Return the result of substituting ARGS into one template parameter
13389 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13390 parameter and which TREE_PURPOSE is the default argument of the
13391 template parameter. */
13392
13393 static tree
13394 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13395 {
13396 tree default_value, parm_decl;
13397
13398 if (args == NULL_TREE
13399 || t == NULL_TREE
13400 || t == error_mark_node)
13401 return t;
13402
13403 gcc_assert (TREE_CODE (t) == TREE_LIST);
13404
13405 default_value = TREE_PURPOSE (t);
13406 parm_decl = TREE_VALUE (t);
13407 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13408
13409 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13410 if (TREE_CODE (parm_decl) == PARM_DECL
13411 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13412 parm_decl = error_mark_node;
13413 default_value = tsubst_template_arg (default_value, args,
13414 complain, NULL_TREE);
13415 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13416
13417 tree r = build_tree_list (default_value, parm_decl);
13418 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13419 return r;
13420 }
13421
13422 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13423 type T. If T is not an aggregate or enumeration type, it is
13424 handled as if by tsubst. IN_DECL is as for tsubst. If
13425 ENTERING_SCOPE is nonzero, T is the context for a template which
13426 we are presently tsubst'ing. Return the substituted value. */
13427
13428 static tree
13429 tsubst_aggr_type (tree t,
13430 tree args,
13431 tsubst_flags_t complain,
13432 tree in_decl,
13433 int entering_scope)
13434 {
13435 if (t == NULL_TREE)
13436 return NULL_TREE;
13437
13438 switch (TREE_CODE (t))
13439 {
13440 case RECORD_TYPE:
13441 if (TYPE_PTRMEMFUNC_P (t))
13442 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13443
13444 /* Fall through. */
13445 case ENUMERAL_TYPE:
13446 case UNION_TYPE:
13447 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13448 {
13449 tree argvec;
13450 tree context;
13451 tree r;
13452
13453 /* In "sizeof(X<I>)" we need to evaluate "I". */
13454 cp_evaluated ev;
13455
13456 /* First, determine the context for the type we are looking
13457 up. */
13458 context = TYPE_CONTEXT (t);
13459 if (context && TYPE_P (context))
13460 {
13461 context = tsubst_aggr_type (context, args, complain,
13462 in_decl, /*entering_scope=*/1);
13463 /* If context is a nested class inside a class template,
13464 it may still need to be instantiated (c++/33959). */
13465 context = complete_type (context);
13466 }
13467
13468 /* Then, figure out what arguments are appropriate for the
13469 type we are trying to find. For example, given:
13470
13471 template <class T> struct S;
13472 template <class T, class U> void f(T, U) { S<U> su; }
13473
13474 and supposing that we are instantiating f<int, double>,
13475 then our ARGS will be {int, double}, but, when looking up
13476 S we only want {double}. */
13477 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13478 complain, in_decl);
13479 if (argvec == error_mark_node)
13480 r = error_mark_node;
13481 else if (!entering_scope
13482 && cxx_dialect >= cxx17 && dependent_scope_p (context))
13483 {
13484 /* See maybe_dependent_member_ref. */
13485 tree name = TYPE_IDENTIFIER (t);
13486 tree fullname = name;
13487 if (instantiates_primary_template_p (t))
13488 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13489 INNERMOST_TEMPLATE_ARGS (argvec));
13490 return build_typename_type (context, name, fullname,
13491 typename_type);
13492 }
13493 else
13494 {
13495 r = lookup_template_class (t, argvec, in_decl, context,
13496 entering_scope, complain);
13497 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13498 }
13499
13500 return r;
13501 }
13502 else
13503 /* This is not a template type, so there's nothing to do. */
13504 return t;
13505
13506 default:
13507 return tsubst (t, args, complain, in_decl);
13508 }
13509 }
13510
13511 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13512
13513 /* Substitute into the default argument ARG (a default argument for
13514 FN), which has the indicated TYPE. */
13515
13516 tree
13517 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13518 tsubst_flags_t complain)
13519 {
13520 int errs = errorcount + sorrycount;
13521
13522 /* This can happen in invalid code. */
13523 if (TREE_CODE (arg) == DEFERRED_PARSE)
13524 return arg;
13525
13526 /* Shortcut {}. */
13527 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13528 && CONSTRUCTOR_NELTS (arg) == 0)
13529 return arg;
13530
13531 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13532 parm = chain_index (parmnum, parm);
13533 tree parmtype = TREE_TYPE (parm);
13534 if (DECL_BY_REFERENCE (parm))
13535 parmtype = TREE_TYPE (parmtype);
13536 if (parmtype == error_mark_node)
13537 return error_mark_node;
13538
13539 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13540
13541 tree *slot;
13542 if (defarg_inst && (slot = defarg_inst->get (parm)))
13543 return *slot;
13544
13545 /* This default argument came from a template. Instantiate the
13546 default argument here, not in tsubst. In the case of
13547 something like:
13548
13549 template <class T>
13550 struct S {
13551 static T t();
13552 void f(T = t());
13553 };
13554
13555 we must be careful to do name lookup in the scope of S<T>,
13556 rather than in the current class. */
13557 push_to_top_level ();
13558 push_access_scope (fn);
13559 push_deferring_access_checks (dk_no_deferred);
13560 start_lambda_scope (parm);
13561
13562 /* The default argument expression may cause implicitly defined
13563 member functions to be synthesized, which will result in garbage
13564 collection. We must treat this situation as if we were within
13565 the body of function so as to avoid collecting live data on the
13566 stack. */
13567 ++function_depth;
13568 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13569 complain, NULL_TREE,
13570 /*integral_constant_expression_p=*/false);
13571 --function_depth;
13572
13573 finish_lambda_scope ();
13574
13575 /* Make sure the default argument is reasonable. */
13576 arg = check_default_argument (type, arg, complain);
13577
13578 if (errorcount+sorrycount > errs
13579 && (complain & tf_warning_or_error))
13580 inform (input_location,
13581 " when instantiating default argument for call to %qD", fn);
13582
13583 pop_deferring_access_checks ();
13584 pop_access_scope (fn);
13585 pop_from_top_level ();
13586
13587 if (arg != error_mark_node && !cp_unevaluated_operand)
13588 {
13589 if (!defarg_inst)
13590 defarg_inst = decl_tree_cache_map::create_ggc (37);
13591 defarg_inst->put (parm, arg);
13592 }
13593
13594 return arg;
13595 }
13596
13597 /* Substitute into all the default arguments for FN. */
13598
13599 static void
13600 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13601 {
13602 tree arg;
13603 tree tmpl_args;
13604
13605 tmpl_args = DECL_TI_ARGS (fn);
13606
13607 /* If this function is not yet instantiated, we certainly don't need
13608 its default arguments. */
13609 if (uses_template_parms (tmpl_args))
13610 return;
13611 /* Don't do this again for clones. */
13612 if (DECL_CLONED_FUNCTION_P (fn))
13613 return;
13614
13615 int i = 0;
13616 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13617 arg;
13618 arg = TREE_CHAIN (arg), ++i)
13619 if (TREE_PURPOSE (arg))
13620 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13621 TREE_VALUE (arg),
13622 TREE_PURPOSE (arg),
13623 complain);
13624 }
13625
13626 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13627 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13628
13629 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13630
13631 void
13632 store_explicit_specifier (tree v, tree t)
13633 {
13634 if (!explicit_specifier_map)
13635 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13636 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13637 explicit_specifier_map->put (v, t);
13638 }
13639
13640 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13641
13642 static tree
13643 lookup_explicit_specifier (tree v)
13644 {
13645 return *explicit_specifier_map->get (v);
13646 }
13647
13648 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13649 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13650 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13651 identical to T. */
13652
13653 static tree
13654 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13655 tree raises, tsubst_flags_t complain)
13656 {
13657 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13658
13659 tree new_type;
13660 if (TREE_CODE (t) == FUNCTION_TYPE)
13661 {
13662 new_type = build_function_type (return_type, arg_types);
13663 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13664 }
13665 else
13666 {
13667 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13668 /* Don't pick up extra function qualifiers from the basetype. */
13669 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13670 if (! MAYBE_CLASS_TYPE_P (r))
13671 {
13672 /* [temp.deduct]
13673
13674 Type deduction may fail for any of the following
13675 reasons:
13676
13677 -- Attempting to create "pointer to member of T" when T
13678 is not a class type. */
13679 if (complain & tf_error)
13680 error ("creating pointer to member function of non-class type %qT",
13681 r);
13682 return error_mark_node;
13683 }
13684
13685 new_type = build_method_type_directly (r, return_type,
13686 TREE_CHAIN (arg_types));
13687 }
13688 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13689
13690 cp_ref_qualifier rqual = type_memfn_rqual (t);
13691 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13692 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13693 }
13694
13695 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13696 each of its formal parameters. If there is a disagreement then rebuild
13697 DECL's function type according to its formal parameter types, as part of a
13698 resolution for Core issues 1001/1322. */
13699
13700 static void
13701 maybe_rebuild_function_decl_type (tree decl)
13702 {
13703 bool function_type_needs_rebuilding = false;
13704 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13705 {
13706 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13707 while (parm_type_list && parm_type_list != void_list_node)
13708 {
13709 tree parm_type = TREE_VALUE (parm_type_list);
13710 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13711 if (!same_type_p (parm_type, formal_parm_type_unqual))
13712 {
13713 function_type_needs_rebuilding = true;
13714 break;
13715 }
13716
13717 parm_list = DECL_CHAIN (parm_list);
13718 parm_type_list = TREE_CHAIN (parm_type_list);
13719 }
13720 }
13721
13722 if (!function_type_needs_rebuilding)
13723 return;
13724
13725 const tree fntype = TREE_TYPE (decl);
13726 tree parm_list = DECL_ARGUMENTS (decl);
13727 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13728 tree new_parm_type_list = NULL_TREE;
13729 tree *q = &new_parm_type_list;
13730 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13731 {
13732 *q = copy_node (old_parm_type_list);
13733 parm_list = DECL_CHAIN (parm_list);
13734 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13735 q = &TREE_CHAIN (*q);
13736 }
13737 while (old_parm_type_list && old_parm_type_list != void_list_node)
13738 {
13739 *q = copy_node (old_parm_type_list);
13740 tree *new_parm_type = &TREE_VALUE (*q);
13741 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13742 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13743 *new_parm_type = formal_parm_type_unqual;
13744
13745 parm_list = DECL_CHAIN (parm_list);
13746 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13747 q = &TREE_CHAIN (*q);
13748 }
13749 if (old_parm_type_list == void_list_node)
13750 *q = void_list_node;
13751
13752 TREE_TYPE (decl)
13753 = rebuild_function_or_method_type (fntype,
13754 TREE_TYPE (fntype), new_parm_type_list,
13755 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13756 }
13757
13758 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13759
13760 static tree
13761 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13762 tree lambda_fntype)
13763 {
13764 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13765 hashval_t hash = 0;
13766 tree in_decl = t;
13767
13768 /* Nobody should be tsubst'ing into non-template functions. */
13769 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13770 || DECL_LOCAL_DECL_P (t));
13771
13772 if (DECL_LOCAL_DECL_P (t))
13773 {
13774 if (tree spec = retrieve_local_specialization (t))
13775 return spec;
13776 }
13777 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13778 {
13779 /* If T is not dependent, just return it. */
13780 if (!uses_template_parms (DECL_TI_ARGS (t))
13781 && !LAMBDA_FUNCTION_P (t))
13782 return t;
13783
13784 /* Calculate the most general template of which R is a
13785 specialization. */
13786 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13787
13788 /* We're substituting a lambda function under tsubst_lambda_expr but not
13789 directly from it; find the matching function we're already inside.
13790 But don't do this if T is a generic lambda with a single level of
13791 template parms, as in that case we're doing a normal instantiation. */
13792 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13793 && (!generic_lambda_fn_p (t)
13794 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13795 return enclosing_instantiation_of (t);
13796
13797 /* Calculate the complete set of arguments used to
13798 specialize R. */
13799 argvec = tsubst_template_args (DECL_TI_ARGS
13800 (DECL_TEMPLATE_RESULT
13801 (DECL_TI_TEMPLATE (t))),
13802 args, complain, in_decl);
13803 if (argvec == error_mark_node)
13804 return error_mark_node;
13805
13806 /* Check to see if we already have this specialization. */
13807 if (!lambda_fntype)
13808 {
13809 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13810 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13811 return spec;
13812 }
13813
13814 /* We can see more levels of arguments than parameters if
13815 there was a specialization of a member template, like
13816 this:
13817
13818 template <class T> struct S { template <class U> void f(); }
13819 template <> template <class U> void S<int>::f(U);
13820
13821 Here, we'll be substituting into the specialization,
13822 because that's where we can find the code we actually
13823 want to generate, but we'll have enough arguments for
13824 the most general template.
13825
13826 We also deal with the peculiar case:
13827
13828 template <class T> struct S {
13829 template <class U> friend void f();
13830 };
13831 template <class U> void f() {}
13832 template S<int>;
13833 template void f<double>();
13834
13835 Here, the ARGS for the instantiation of will be {int,
13836 double}. But, we only need as many ARGS as there are
13837 levels of template parameters in CODE_PATTERN. We are
13838 careful not to get fooled into reducing the ARGS in
13839 situations like:
13840
13841 template <class T> struct S { template <class U> void f(U); }
13842 template <class T> template <> void S<T>::f(int) {}
13843
13844 which we can spot because the pattern will be a
13845 specialization in this case. */
13846 int args_depth = TMPL_ARGS_DEPTH (args);
13847 int parms_depth =
13848 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13849
13850 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13851 args = get_innermost_template_args (args, parms_depth);
13852 }
13853 else
13854 {
13855 /* This special case arises when we have something like this:
13856
13857 template <class T> struct S {
13858 friend void f<int>(int, double);
13859 };
13860
13861 Here, the DECL_TI_TEMPLATE for the friend declaration
13862 will be an IDENTIFIER_NODE. We are being called from
13863 tsubst_friend_function, and we want only to create a
13864 new decl (R) with appropriate types so that we can call
13865 determine_specialization. */
13866 gen_tmpl = NULL_TREE;
13867 argvec = NULL_TREE;
13868 }
13869
13870 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13871 : NULL_TREE);
13872 tree ctx = closure ? closure : DECL_CONTEXT (t);
13873 bool member = ctx && TYPE_P (ctx);
13874
13875 if (member && !closure)
13876 ctx = tsubst_aggr_type (ctx, args,
13877 complain, t, /*entering_scope=*/1);
13878
13879 tree type = (lambda_fntype ? lambda_fntype
13880 : tsubst (TREE_TYPE (t), args,
13881 complain | tf_fndecl_type, in_decl));
13882 if (type == error_mark_node)
13883 return error_mark_node;
13884
13885 /* If we hit excessive deduction depth, the type is bogus even if
13886 it isn't error_mark_node, so don't build a decl. */
13887 if (excessive_deduction_depth)
13888 return error_mark_node;
13889
13890 /* We do NOT check for matching decls pushed separately at this
13891 point, as they may not represent instantiations of this
13892 template, and in any case are considered separate under the
13893 discrete model. */
13894 tree r = copy_decl (t);
13895 DECL_USE_TEMPLATE (r) = 0;
13896 TREE_TYPE (r) = type;
13897 /* Clear out the mangled name and RTL for the instantiation. */
13898 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13899 SET_DECL_RTL (r, NULL);
13900 /* Leave DECL_INITIAL set on deleted instantiations. */
13901 if (!DECL_DELETED_FN (r))
13902 DECL_INITIAL (r) = NULL_TREE;
13903 DECL_CONTEXT (r) = ctx;
13904
13905 /* Handle explicit(dependent-expr). */
13906 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13907 {
13908 tree spec = lookup_explicit_specifier (t);
13909 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13910 /*function_p=*/false,
13911 /*i_c_e_p=*/true);
13912 spec = build_explicit_specifier (spec, complain);
13913 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13914 }
13915
13916 /* OpenMP UDRs have the only argument a reference to the declared
13917 type. We want to diagnose if the declared type is a reference,
13918 which is invalid, but as references to references are usually
13919 quietly merged, diagnose it here. */
13920 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13921 {
13922 tree argtype
13923 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13924 argtype = tsubst (argtype, args, complain, in_decl);
13925 if (TYPE_REF_P (argtype))
13926 error_at (DECL_SOURCE_LOCATION (t),
13927 "reference type %qT in "
13928 "%<#pragma omp declare reduction%>", argtype);
13929 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13930 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13931 argtype);
13932 }
13933
13934 if (member && DECL_CONV_FN_P (r))
13935 /* Type-conversion operator. Reconstruct the name, in
13936 case it's the name of one of the template's parameters. */
13937 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13938
13939 tree parms = DECL_ARGUMENTS (t);
13940 if (closure)
13941 parms = DECL_CHAIN (parms);
13942 parms = tsubst (parms, args, complain, t);
13943 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13944 DECL_CONTEXT (parm) = r;
13945 if (closure)
13946 {
13947 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13948 DECL_NAME (tparm) = closure_identifier;
13949 DECL_CHAIN (tparm) = parms;
13950 parms = tparm;
13951 }
13952 DECL_ARGUMENTS (r) = parms;
13953 DECL_RESULT (r) = NULL_TREE;
13954
13955 maybe_rebuild_function_decl_type (r);
13956
13957 TREE_STATIC (r) = 0;
13958 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13959 DECL_EXTERNAL (r) = 1;
13960 /* If this is an instantiation of a function with internal
13961 linkage, we already know what object file linkage will be
13962 assigned to the instantiation. */
13963 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13964 DECL_DEFER_OUTPUT (r) = 0;
13965 DECL_CHAIN (r) = NULL_TREE;
13966 DECL_PENDING_INLINE_INFO (r) = 0;
13967 DECL_PENDING_INLINE_P (r) = 0;
13968 DECL_SAVED_TREE (r) = NULL_TREE;
13969 DECL_STRUCT_FUNCTION (r) = NULL;
13970 TREE_USED (r) = 0;
13971 /* We'll re-clone as appropriate in instantiate_template. */
13972 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13973
13974 /* If we aren't complaining now, return on error before we register
13975 the specialization so that we'll complain eventually. */
13976 if ((complain & tf_error) == 0
13977 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13978 && !grok_op_properties (r, /*complain=*/false))
13979 return error_mark_node;
13980
13981 /* Associate the constraints directly with the instantiation. We
13982 don't substitute through the constraints; that's only done when
13983 they are checked. */
13984 if (tree ci = get_constraints (t))
13985 /* Unless we're regenerating a lambda, in which case we'll set the
13986 lambda's constraints in tsubst_lambda_expr. */
13987 if (!lambda_fntype)
13988 set_constraints (r, ci);
13989
13990 if (DECL_FRIEND_CONTEXT (t))
13991 SET_DECL_FRIEND_CONTEXT (r,
13992 tsubst (DECL_FRIEND_CONTEXT (t),
13993 args, complain, in_decl));
13994
13995 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13996 this in the special friend case mentioned above where
13997 GEN_TMPL is NULL. */
13998 if (gen_tmpl && !closure)
13999 {
14000 DECL_TEMPLATE_INFO (r)
14001 = build_template_info (gen_tmpl, argvec);
14002 SET_DECL_IMPLICIT_INSTANTIATION (r);
14003
14004 tree new_r
14005 = register_specialization (r, gen_tmpl, argvec, false, hash);
14006 if (new_r != r)
14007 /* We instantiated this while substituting into
14008 the type earlier (template/friend54.C). */
14009 return new_r;
14010
14011 /* We're not supposed to instantiate default arguments
14012 until they are called, for a template. But, for a
14013 declaration like:
14014
14015 template <class T> void f ()
14016 { extern void g(int i = T()); }
14017
14018 we should do the substitution when the template is
14019 instantiated. We handle the member function case in
14020 instantiate_class_template since the default arguments
14021 might refer to other members of the class. */
14022 if (!member
14023 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14024 && !uses_template_parms (argvec))
14025 tsubst_default_arguments (r, complain);
14026 }
14027 else if (DECL_LOCAL_DECL_P (r))
14028 {
14029 if (!cp_unevaluated_operand)
14030 register_local_specialization (r, t);
14031 }
14032 else
14033 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14034
14035 /* Copy the list of befriending classes. */
14036 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14037 *friends;
14038 friends = &TREE_CHAIN (*friends))
14039 {
14040 *friends = copy_node (*friends);
14041 TREE_VALUE (*friends)
14042 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14043 }
14044
14045 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14046 {
14047 maybe_retrofit_in_chrg (r);
14048 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14049 return error_mark_node;
14050 /* If this is an instantiation of a member template, clone it.
14051 If it isn't, that'll be handled by
14052 clone_constructors_and_destructors. */
14053 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14054 clone_cdtor (r, /*update_methods=*/false);
14055 }
14056 else if ((complain & tf_error) != 0
14057 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14058 && !grok_op_properties (r, /*complain=*/true))
14059 return error_mark_node;
14060
14061 /* Possibly limit visibility based on template args. */
14062 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14063 if (DECL_VISIBILITY_SPECIFIED (t))
14064 {
14065 DECL_VISIBILITY_SPECIFIED (r) = 0;
14066 DECL_ATTRIBUTES (r)
14067 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14068 }
14069 determine_visibility (r);
14070 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14071 && !processing_template_decl)
14072 defaulted_late_check (r);
14073
14074 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14075 args, complain, in_decl);
14076 if (flag_openmp)
14077 if (tree attr = lookup_attribute ("omp declare variant base",
14078 DECL_ATTRIBUTES (r)))
14079 omp_declare_variant_finalize (r, attr);
14080
14081 return r;
14082 }
14083
14084 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14085
14086 static tree
14087 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14088 tree lambda_fntype)
14089 {
14090 /* We can get here when processing a member function template,
14091 member class template, or template template parameter. */
14092 tree decl = DECL_TEMPLATE_RESULT (t);
14093 tree in_decl = t;
14094 tree spec;
14095 tree tmpl_args;
14096 tree full_args;
14097 tree r;
14098 hashval_t hash = 0;
14099
14100 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14101 {
14102 /* Template template parameter is treated here. */
14103 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14104 if (new_type == error_mark_node)
14105 r = error_mark_node;
14106 /* If we get a real template back, return it. This can happen in
14107 the context of most_specialized_partial_spec. */
14108 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14109 r = new_type;
14110 else
14111 /* The new TEMPLATE_DECL was built in
14112 reduce_template_parm_level. */
14113 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14114 return r;
14115 }
14116
14117 if (!lambda_fntype)
14118 {
14119 /* We might already have an instance of this template.
14120 The ARGS are for the surrounding class type, so the
14121 full args contain the tsubst'd args for the context,
14122 plus the innermost args from the template decl. */
14123 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14124 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14125 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14126 /* Because this is a template, the arguments will still be
14127 dependent, even after substitution. If
14128 PROCESSING_TEMPLATE_DECL is not set, the dependency
14129 predicates will short-circuit. */
14130 ++processing_template_decl;
14131 full_args = tsubst_template_args (tmpl_args, args,
14132 complain, in_decl);
14133 --processing_template_decl;
14134 if (full_args == error_mark_node)
14135 return error_mark_node;
14136
14137 /* If this is a default template template argument,
14138 tsubst might not have changed anything. */
14139 if (full_args == tmpl_args)
14140 return t;
14141
14142 hash = hash_tmpl_and_args (t, full_args);
14143 spec = retrieve_specialization (t, full_args, hash);
14144 if (spec != NULL_TREE)
14145 {
14146 if (TYPE_P (spec))
14147 /* Type partial instantiations are stored as the type by
14148 lookup_template_class_1, not here as the template. */
14149 spec = CLASSTYPE_TI_TEMPLATE (spec);
14150 return spec;
14151 }
14152 }
14153
14154 /* Make a new template decl. It will be similar to the
14155 original, but will record the current template arguments.
14156 We also create a new function declaration, which is just
14157 like the old one, but points to this new template, rather
14158 than the old one. */
14159 r = copy_decl (t);
14160 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14161 DECL_CHAIN (r) = NULL_TREE;
14162
14163 // Build new template info linking to the original template decl.
14164 if (!lambda_fntype)
14165 {
14166 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14167 SET_DECL_IMPLICIT_INSTANTIATION (r);
14168 }
14169 else
14170 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14171
14172 /* The template parameters for this new template are all the
14173 template parameters for the old template, except the
14174 outermost level of parameters. */
14175 DECL_TEMPLATE_PARMS (r)
14176 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14177 complain);
14178
14179 bool class_p = false;
14180 tree inner = decl;
14181 ++processing_template_decl;
14182 if (TREE_CODE (inner) == FUNCTION_DECL)
14183 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14184 else
14185 {
14186 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14187 {
14188 class_p = true;
14189 inner = TREE_TYPE (inner);
14190 }
14191 if (class_p)
14192 inner = tsubst_aggr_type (inner, args, complain,
14193 in_decl, /*entering*/1);
14194 else
14195 inner = tsubst (inner, args, complain, in_decl);
14196 }
14197 --processing_template_decl;
14198 if (inner == error_mark_node)
14199 return error_mark_node;
14200
14201 if (class_p)
14202 {
14203 /* For a partial specialization, we need to keep pointing to
14204 the primary template. */
14205 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14206 CLASSTYPE_TI_TEMPLATE (inner) = r;
14207
14208 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14209 inner = TYPE_MAIN_DECL (inner);
14210 }
14211 else if (lambda_fntype)
14212 {
14213 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14214 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14215 }
14216 else
14217 {
14218 if (TREE_CODE (decl) != TYPE_DECL || !TYPE_DECL_ALIAS_P (decl))
14219 DECL_TI_TEMPLATE (inner) = r;
14220 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14221 }
14222
14223 DECL_TEMPLATE_RESULT (r) = inner;
14224 TREE_TYPE (r) = TREE_TYPE (inner);
14225 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14226
14227 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14228 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14229
14230 if (PRIMARY_TEMPLATE_P (t))
14231 DECL_PRIMARY_TEMPLATE (r) = r;
14232
14233 if (TREE_CODE (decl) == FUNCTION_DECL && !lambda_fntype)
14234 /* Record this non-type partial instantiation. */
14235 register_specialization (r, t,
14236 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14237 false, hash);
14238
14239 return r;
14240 }
14241
14242 /* True if FN is the op() for a lambda in an uninstantiated template. */
14243
14244 bool
14245 lambda_fn_in_template_p (tree fn)
14246 {
14247 if (!fn || !LAMBDA_FUNCTION_P (fn))
14248 return false;
14249 tree closure = DECL_CONTEXT (fn);
14250 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14251 }
14252
14253 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14254 which the above is true. */
14255
14256 bool
14257 instantiated_lambda_fn_p (tree fn)
14258 {
14259 if (!fn || !LAMBDA_FUNCTION_P (fn))
14260 return false;
14261 tree closure = DECL_CONTEXT (fn);
14262 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14263 return LAMBDA_EXPR_INSTANTIATED (lam);
14264 }
14265
14266 /* We're instantiating a variable from template function TCTX. Return the
14267 corresponding current enclosing scope. This gets complicated because lambda
14268 functions in templates are regenerated rather than instantiated, but generic
14269 lambda functions are subsequently instantiated. */
14270
14271 static tree
14272 enclosing_instantiation_of (tree otctx)
14273 {
14274 tree tctx = otctx;
14275 tree fn = current_function_decl;
14276 int lambda_count = 0;
14277
14278 for (; tctx && (lambda_fn_in_template_p (tctx)
14279 || instantiated_lambda_fn_p (tctx));
14280 tctx = decl_function_context (tctx))
14281 ++lambda_count;
14282 for (; fn; fn = decl_function_context (fn))
14283 {
14284 tree ofn = fn;
14285 int flambda_count = 0;
14286 for (; fn && instantiated_lambda_fn_p (fn);
14287 fn = decl_function_context (fn))
14288 ++flambda_count;
14289 if ((fn && DECL_TEMPLATE_INFO (fn))
14290 ? most_general_template (fn) != most_general_template (tctx)
14291 : fn != tctx)
14292 continue;
14293 if (flambda_count != lambda_count)
14294 {
14295 gcc_assert (flambda_count > lambda_count);
14296 for (; flambda_count > lambda_count; --flambda_count)
14297 ofn = decl_function_context (ofn);
14298 }
14299 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14300 || DECL_CONV_FN_P (ofn));
14301 return ofn;
14302 }
14303 gcc_unreachable ();
14304 }
14305
14306 /* Substitute the ARGS into the T, which is a _DECL. Return the
14307 result of the substitution. Issue error and warning messages under
14308 control of COMPLAIN. */
14309
14310 static tree
14311 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14312 {
14313 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14314 location_t saved_loc;
14315 tree r = NULL_TREE;
14316 tree in_decl = t;
14317 hashval_t hash = 0;
14318
14319 /* Set the filename and linenumber to improve error-reporting. */
14320 saved_loc = input_location;
14321 input_location = DECL_SOURCE_LOCATION (t);
14322
14323 switch (TREE_CODE (t))
14324 {
14325 case TEMPLATE_DECL:
14326 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14327 break;
14328
14329 case FUNCTION_DECL:
14330 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14331 break;
14332
14333 case PARM_DECL:
14334 {
14335 tree type = NULL_TREE;
14336 int i, len = 1;
14337 tree expanded_types = NULL_TREE;
14338 tree prev_r = NULL_TREE;
14339 tree first_r = NULL_TREE;
14340
14341 if (DECL_PACK_P (t))
14342 {
14343 /* If there is a local specialization that isn't a
14344 parameter pack, it means that we're doing a "simple"
14345 substitution from inside tsubst_pack_expansion. Just
14346 return the local specialization (which will be a single
14347 parm). */
14348 tree spec = retrieve_local_specialization (t);
14349 if (spec
14350 && TREE_CODE (spec) == PARM_DECL
14351 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14352 RETURN (spec);
14353
14354 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14355 the parameters in this function parameter pack. */
14356 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14357 complain, in_decl);
14358 if (TREE_CODE (expanded_types) == TREE_VEC)
14359 {
14360 len = TREE_VEC_LENGTH (expanded_types);
14361
14362 /* Zero-length parameter packs are boring. Just substitute
14363 into the chain. */
14364 if (len == 0 && !cp_unevaluated_operand)
14365 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14366 TREE_CHAIN (t)));
14367 }
14368 else
14369 {
14370 /* All we did was update the type. Make a note of that. */
14371 type = expanded_types;
14372 expanded_types = NULL_TREE;
14373 }
14374 }
14375
14376 /* Loop through all of the parameters we'll build. When T is
14377 a function parameter pack, LEN is the number of expanded
14378 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14379 r = NULL_TREE;
14380 for (i = 0; i < len; ++i)
14381 {
14382 prev_r = r;
14383 r = copy_node (t);
14384 if (DECL_TEMPLATE_PARM_P (t))
14385 SET_DECL_TEMPLATE_PARM_P (r);
14386
14387 if (expanded_types)
14388 /* We're on the Ith parameter of the function parameter
14389 pack. */
14390 {
14391 /* Get the Ith type. */
14392 type = TREE_VEC_ELT (expanded_types, i);
14393
14394 /* Rename the parameter to include the index. */
14395 DECL_NAME (r)
14396 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14397 }
14398 else if (!type)
14399 /* We're dealing with a normal parameter. */
14400 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14401
14402 type = type_decays_to (type);
14403 TREE_TYPE (r) = type;
14404 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14405
14406 if (DECL_INITIAL (r))
14407 {
14408 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14409 DECL_INITIAL (r) = TREE_TYPE (r);
14410 else
14411 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14412 complain, in_decl);
14413 }
14414
14415 DECL_CONTEXT (r) = NULL_TREE;
14416
14417 if (!DECL_TEMPLATE_PARM_P (r))
14418 DECL_ARG_TYPE (r) = type_passed_as (type);
14419
14420 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14421 args, complain, in_decl);
14422
14423 /* Keep track of the first new parameter we
14424 generate. That's what will be returned to the
14425 caller. */
14426 if (!first_r)
14427 first_r = r;
14428
14429 /* Build a proper chain of parameters when substituting
14430 into a function parameter pack. */
14431 if (prev_r)
14432 DECL_CHAIN (prev_r) = r;
14433 }
14434
14435 /* If cp_unevaluated_operand is set, we're just looking for a
14436 single dummy parameter, so don't keep going. */
14437 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14438 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14439 complain, DECL_CHAIN (t));
14440
14441 /* FIRST_R contains the start of the chain we've built. */
14442 r = first_r;
14443 }
14444 break;
14445
14446 case FIELD_DECL:
14447 {
14448 tree type = NULL_TREE;
14449 tree vec = NULL_TREE;
14450 tree expanded_types = NULL_TREE;
14451 int len = 1;
14452
14453 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14454 {
14455 /* This field is a lambda capture pack. Return a TREE_VEC of
14456 the expanded fields to instantiate_class_template_1. */
14457 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14458 complain, in_decl);
14459 if (TREE_CODE (expanded_types) == TREE_VEC)
14460 {
14461 len = TREE_VEC_LENGTH (expanded_types);
14462 vec = make_tree_vec (len);
14463 }
14464 else
14465 {
14466 /* All we did was update the type. Make a note of that. */
14467 type = expanded_types;
14468 expanded_types = NULL_TREE;
14469 }
14470 }
14471
14472 for (int i = 0; i < len; ++i)
14473 {
14474 r = copy_decl (t);
14475 if (expanded_types)
14476 {
14477 type = TREE_VEC_ELT (expanded_types, i);
14478 DECL_NAME (r)
14479 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14480 }
14481 else if (!type)
14482 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14483
14484 if (type == error_mark_node)
14485 RETURN (error_mark_node);
14486 TREE_TYPE (r) = type;
14487 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14488
14489 if (DECL_C_BIT_FIELD (r))
14490 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14491 number of bits. */
14492 DECL_BIT_FIELD_REPRESENTATIVE (r)
14493 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14494 complain, in_decl,
14495 /*integral_constant_expression_p=*/true);
14496 if (DECL_INITIAL (t))
14497 {
14498 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14499 NSDMI in perform_member_init. Still set DECL_INITIAL
14500 so that we know there is one. */
14501 DECL_INITIAL (r) = void_node;
14502 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14503 retrofit_lang_decl (r);
14504 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14505 }
14506 /* We don't have to set DECL_CONTEXT here; it is set by
14507 finish_member_declaration. */
14508 DECL_CHAIN (r) = NULL_TREE;
14509
14510 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14511 args, complain, in_decl);
14512
14513 if (vec)
14514 TREE_VEC_ELT (vec, i) = r;
14515 }
14516
14517 if (vec)
14518 r = vec;
14519 }
14520 break;
14521
14522 case USING_DECL:
14523 /* We reach here only for member using decls. We also need to check
14524 uses_template_parms because DECL_DEPENDENT_P is not set for a
14525 using-declaration that designates a member of the current
14526 instantiation (c++/53549). */
14527 if (DECL_DEPENDENT_P (t)
14528 || uses_template_parms (USING_DECL_SCOPE (t)))
14529 {
14530 tree scope = USING_DECL_SCOPE (t);
14531 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14532 if (PACK_EXPANSION_P (scope))
14533 {
14534 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14535 int len = TREE_VEC_LENGTH (vec);
14536 r = make_tree_vec (len);
14537 for (int i = 0; i < len; ++i)
14538 {
14539 tree escope = TREE_VEC_ELT (vec, i);
14540 tree elt = do_class_using_decl (escope, name);
14541 if (!elt)
14542 {
14543 r = error_mark_node;
14544 break;
14545 }
14546 else
14547 {
14548 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14549 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14550 }
14551 TREE_VEC_ELT (r, i) = elt;
14552 }
14553 }
14554 else
14555 {
14556 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14557 complain, in_decl);
14558 r = do_class_using_decl (inst_scope, name);
14559 if (!r)
14560 r = error_mark_node;
14561 else
14562 {
14563 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14564 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14565 }
14566 }
14567 }
14568 else
14569 {
14570 r = copy_node (t);
14571 DECL_CHAIN (r) = NULL_TREE;
14572 }
14573 break;
14574
14575 case TYPE_DECL:
14576 case VAR_DECL:
14577 {
14578 tree argvec = NULL_TREE;
14579 tree gen_tmpl = NULL_TREE;
14580 tree tmpl = NULL_TREE;
14581 tree type = NULL_TREE;
14582
14583 if (TREE_TYPE (t) == error_mark_node)
14584 RETURN (error_mark_node);
14585
14586 if (TREE_CODE (t) == TYPE_DECL
14587 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14588 {
14589 /* If this is the canonical decl, we don't have to
14590 mess with instantiations, and often we can't (for
14591 typename, template type parms and such). Note that
14592 TYPE_NAME is not correct for the above test if
14593 we've copied the type for a typedef. */
14594 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14595 if (type == error_mark_node)
14596 RETURN (error_mark_node);
14597 r = TYPE_NAME (type);
14598 break;
14599 }
14600
14601 /* Check to see if we already have the specialization we
14602 need. */
14603 tree spec = NULL_TREE;
14604 bool local_p = false;
14605 tree ctx = DECL_CONTEXT (t);
14606 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14607 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14608 {
14609 local_p = false;
14610 if (DECL_CLASS_SCOPE_P (t))
14611 {
14612 ctx = tsubst_aggr_type (ctx, args,
14613 complain,
14614 in_decl, /*entering_scope=*/1);
14615 /* If CTX is unchanged, then T is in fact the
14616 specialization we want. That situation occurs when
14617 referencing a static data member within in its own
14618 class. We can use pointer equality, rather than
14619 same_type_p, because DECL_CONTEXT is always
14620 canonical... */
14621 if (ctx == DECL_CONTEXT (t)
14622 /* ... unless T is a member template; in which
14623 case our caller can be willing to create a
14624 specialization of that template represented
14625 by T. */
14626 && !(DECL_TI_TEMPLATE (t)
14627 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14628 spec = t;
14629 }
14630
14631 if (!spec)
14632 {
14633 tmpl = DECL_TI_TEMPLATE (t);
14634 gen_tmpl = most_general_template (tmpl);
14635 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14636 if (argvec != error_mark_node)
14637 argvec = (coerce_innermost_template_parms
14638 (DECL_TEMPLATE_PARMS (gen_tmpl),
14639 argvec, t, complain,
14640 /*all*/true, /*defarg*/true));
14641 if (argvec == error_mark_node)
14642 RETURN (error_mark_node);
14643 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14644 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14645 }
14646 }
14647 else
14648 {
14649 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14650 /* Subsequent calls to pushdecl will fill this in. */
14651 ctx = NULL_TREE;
14652 /* A local variable. */
14653 local_p = true;
14654 /* Unless this is a reference to a static variable from an
14655 enclosing function, in which case we need to fill it in now. */
14656 if (TREE_STATIC (t))
14657 {
14658 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14659 if (fn != current_function_decl)
14660 ctx = fn;
14661 }
14662 spec = retrieve_local_specialization (t);
14663 }
14664 /* If we already have the specialization we need, there is
14665 nothing more to do. */
14666 if (spec)
14667 {
14668 r = spec;
14669 break;
14670 }
14671
14672 /* Create a new node for the specialization we need. */
14673 if (type == NULL_TREE)
14674 {
14675 if (is_typedef_decl (t))
14676 type = DECL_ORIGINAL_TYPE (t);
14677 else
14678 type = TREE_TYPE (t);
14679 if (VAR_P (t)
14680 && VAR_HAD_UNKNOWN_BOUND (t)
14681 && type != error_mark_node)
14682 type = strip_array_domain (type);
14683 tree sub_args = args;
14684 if (tree auto_node = type_uses_auto (type))
14685 {
14686 /* Mask off any template args past the variable's context so we
14687 don't replace the auto with an unrelated argument. */
14688 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14689 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14690 if (extra > 0)
14691 /* This should never happen with the new lambda instantiation
14692 model, but keep the handling just in case. */
14693 gcc_assert (!CHECKING_P),
14694 sub_args = strip_innermost_template_args (args, extra);
14695 }
14696 type = tsubst (type, sub_args, complain, in_decl);
14697 /* Substituting the type might have recursively instantiated this
14698 same alias (c++/86171). */
14699 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14700 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14701 {
14702 r = spec;
14703 break;
14704 }
14705 }
14706 r = copy_decl (t);
14707 if (VAR_P (r))
14708 {
14709 DECL_INITIALIZED_P (r) = 0;
14710 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14711 if (type == error_mark_node)
14712 RETURN (error_mark_node);
14713 if (TREE_CODE (type) == FUNCTION_TYPE)
14714 {
14715 /* It may seem that this case cannot occur, since:
14716
14717 typedef void f();
14718 void g() { f x; }
14719
14720 declares a function, not a variable. However:
14721
14722 typedef void f();
14723 template <typename T> void g() { T t; }
14724 template void g<f>();
14725
14726 is an attempt to declare a variable with function
14727 type. */
14728 error ("variable %qD has function type",
14729 /* R is not yet sufficiently initialized, so we
14730 just use its name. */
14731 DECL_NAME (r));
14732 RETURN (error_mark_node);
14733 }
14734 type = complete_type (type);
14735 /* Wait until cp_finish_decl to set this again, to handle
14736 circular dependency (template/instantiate6.C). */
14737 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14738 type = check_var_type (DECL_NAME (r), type,
14739 DECL_SOURCE_LOCATION (r));
14740 if (DECL_HAS_VALUE_EXPR_P (t))
14741 {
14742 tree ve = DECL_VALUE_EXPR (t);
14743 /* If the DECL_VALUE_EXPR is converted to the declared type,
14744 preserve the identity so that gimplify_type_sizes works. */
14745 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14746 if (nop)
14747 ve = TREE_OPERAND (ve, 0);
14748 ve = tsubst_expr (ve, args, complain, in_decl,
14749 /*constant_expression_p=*/false);
14750 if (REFERENCE_REF_P (ve))
14751 {
14752 gcc_assert (TYPE_REF_P (type));
14753 ve = TREE_OPERAND (ve, 0);
14754 }
14755 if (nop)
14756 ve = build_nop (type, ve);
14757 else if (DECL_LANG_SPECIFIC (t)
14758 && DECL_OMP_PRIVATIZED_MEMBER (t)
14759 && TREE_CODE (ve) == COMPONENT_REF
14760 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14761 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14762 type = TREE_TYPE (ve);
14763 else
14764 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14765 == TYPE_MAIN_VARIANT (type));
14766 SET_DECL_VALUE_EXPR (r, ve);
14767 }
14768 if (CP_DECL_THREAD_LOCAL_P (r)
14769 && !processing_template_decl)
14770 set_decl_tls_model (r, decl_default_tls_model (r));
14771 }
14772 else if (DECL_SELF_REFERENCE_P (t))
14773 SET_DECL_SELF_REFERENCE_P (r);
14774 TREE_TYPE (r) = type;
14775 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14776 DECL_CONTEXT (r) = ctx;
14777 /* Clear out the mangled name and RTL for the instantiation. */
14778 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14779 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14780 SET_DECL_RTL (r, NULL);
14781 /* The initializer must not be expanded until it is required;
14782 see [temp.inst]. */
14783 DECL_INITIAL (r) = NULL_TREE;
14784 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14785 if (VAR_P (r))
14786 {
14787 if (DECL_LANG_SPECIFIC (r))
14788 SET_DECL_DEPENDENT_INIT_P (r, false);
14789
14790 SET_DECL_MODE (r, VOIDmode);
14791
14792 /* Possibly limit visibility based on template args. */
14793 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14794 if (DECL_VISIBILITY_SPECIFIED (t))
14795 {
14796 DECL_VISIBILITY_SPECIFIED (r) = 0;
14797 DECL_ATTRIBUTES (r)
14798 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14799 }
14800 determine_visibility (r);
14801 }
14802
14803 if (!local_p)
14804 {
14805 /* A static data member declaration is always marked
14806 external when it is declared in-class, even if an
14807 initializer is present. We mimic the non-template
14808 processing here. */
14809 DECL_EXTERNAL (r) = 1;
14810 if (DECL_NAMESPACE_SCOPE_P (t))
14811 DECL_NOT_REALLY_EXTERN (r) = 1;
14812
14813 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14814 SET_DECL_IMPLICIT_INSTANTIATION (r);
14815 if (!error_operand_p (r) || (complain & tf_error))
14816 register_specialization (r, gen_tmpl, argvec, false, hash);
14817 }
14818 else
14819 {
14820 if (DECL_LANG_SPECIFIC (r))
14821 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14822 if (!cp_unevaluated_operand)
14823 register_local_specialization (r, t);
14824 }
14825
14826 DECL_CHAIN (r) = NULL_TREE;
14827
14828 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14829 /*flags=*/0,
14830 args, complain, in_decl);
14831
14832 /* Preserve a typedef that names a type. */
14833 if (is_typedef_decl (r) && type != error_mark_node)
14834 {
14835 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14836 set_underlying_type (r);
14837 if (TYPE_DECL_ALIAS_P (r))
14838 /* An alias template specialization can be dependent
14839 even if its underlying type is not. */
14840 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14841 }
14842
14843 layout_decl (r, 0);
14844 }
14845 break;
14846
14847 default:
14848 gcc_unreachable ();
14849 }
14850 #undef RETURN
14851
14852 out:
14853 /* Restore the file and line information. */
14854 input_location = saved_loc;
14855
14856 return r;
14857 }
14858
14859 /* Substitute into the complete parameter type list PARMS. */
14860
14861 tree
14862 tsubst_function_parms (tree parms,
14863 tree args,
14864 tsubst_flags_t complain,
14865 tree in_decl)
14866 {
14867 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14868 }
14869
14870 /* Substitute into the ARG_TYPES of a function type.
14871 If END is a TREE_CHAIN, leave it and any following types
14872 un-substituted. */
14873
14874 static tree
14875 tsubst_arg_types (tree arg_types,
14876 tree args,
14877 tree end,
14878 tsubst_flags_t complain,
14879 tree in_decl)
14880 {
14881 tree remaining_arg_types;
14882 tree type = NULL_TREE;
14883 int i = 1;
14884 tree expanded_args = NULL_TREE;
14885 tree default_arg;
14886
14887 if (!arg_types || arg_types == void_list_node || arg_types == end)
14888 return arg_types;
14889
14890 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14891 args, end, complain, in_decl);
14892 if (remaining_arg_types == error_mark_node)
14893 return error_mark_node;
14894
14895 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14896 {
14897 /* For a pack expansion, perform substitution on the
14898 entire expression. Later on, we'll handle the arguments
14899 one-by-one. */
14900 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14901 args, complain, in_decl);
14902
14903 if (TREE_CODE (expanded_args) == TREE_VEC)
14904 /* So that we'll spin through the parameters, one by one. */
14905 i = TREE_VEC_LENGTH (expanded_args);
14906 else
14907 {
14908 /* We only partially substituted into the parameter
14909 pack. Our type is TYPE_PACK_EXPANSION. */
14910 type = expanded_args;
14911 expanded_args = NULL_TREE;
14912 }
14913 }
14914
14915 while (i > 0) {
14916 --i;
14917
14918 if (expanded_args)
14919 type = TREE_VEC_ELT (expanded_args, i);
14920 else if (!type)
14921 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14922
14923 if (type == error_mark_node)
14924 return error_mark_node;
14925 if (VOID_TYPE_P (type))
14926 {
14927 if (complain & tf_error)
14928 {
14929 error ("invalid parameter type %qT", type);
14930 if (in_decl)
14931 error ("in declaration %q+D", in_decl);
14932 }
14933 return error_mark_node;
14934 }
14935 /* DR 657. */
14936 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14937 return error_mark_node;
14938
14939 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14940 top-level qualifiers as required. */
14941 type = cv_unqualified (type_decays_to (type));
14942
14943 /* We do not substitute into default arguments here. The standard
14944 mandates that they be instantiated only when needed, which is
14945 done in build_over_call. */
14946 default_arg = TREE_PURPOSE (arg_types);
14947
14948 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14949 since the new op() won't have any associated template arguments for us
14950 to refer to later. */
14951 if (lambda_fn_in_template_p (in_decl))
14952 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14953 false/*fn*/, false/*constexpr*/);
14954
14955 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14956 {
14957 /* We've instantiated a template before its default arguments
14958 have been parsed. This can happen for a nested template
14959 class, and is not an error unless we require the default
14960 argument in a call of this function. */
14961 remaining_arg_types =
14962 tree_cons (default_arg, type, remaining_arg_types);
14963 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14964 remaining_arg_types);
14965 }
14966 else
14967 remaining_arg_types =
14968 hash_tree_cons (default_arg, type, remaining_arg_types);
14969 }
14970
14971 return remaining_arg_types;
14972 }
14973
14974 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14975 *not* handle the exception-specification for FNTYPE, because the
14976 initial substitution of explicitly provided template parameters
14977 during argument deduction forbids substitution into the
14978 exception-specification:
14979
14980 [temp.deduct]
14981
14982 All references in the function type of the function template to the
14983 corresponding template parameters are replaced by the specified tem-
14984 plate argument values. If a substitution in a template parameter or
14985 in the function type of the function template results in an invalid
14986 type, type deduction fails. [Note: The equivalent substitution in
14987 exception specifications is done only when the function is instanti-
14988 ated, at which point a program is ill-formed if the substitution
14989 results in an invalid type.] */
14990
14991 static tree
14992 tsubst_function_type (tree t,
14993 tree args,
14994 tsubst_flags_t complain,
14995 tree in_decl)
14996 {
14997 tree return_type;
14998 tree arg_types = NULL_TREE;
14999
15000 /* The TYPE_CONTEXT is not used for function/method types. */
15001 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
15002
15003 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15004 failure. */
15005 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
15006
15007 if (late_return_type_p)
15008 {
15009 /* Substitute the argument types. */
15010 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15011 complain, in_decl);
15012 if (arg_types == error_mark_node)
15013 return error_mark_node;
15014
15015 tree save_ccp = current_class_ptr;
15016 tree save_ccr = current_class_ref;
15017 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15018 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15019 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15020 if (do_inject)
15021 {
15022 /* DR 1207: 'this' is in scope in the trailing return type. */
15023 inject_this_parameter (this_type, cp_type_quals (this_type));
15024 }
15025
15026 /* Substitute the return type. */
15027 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15028
15029 if (do_inject)
15030 {
15031 current_class_ptr = save_ccp;
15032 current_class_ref = save_ccr;
15033 }
15034 }
15035 else
15036 /* Substitute the return type. */
15037 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15038
15039 if (return_type == error_mark_node)
15040 return error_mark_node;
15041 /* DR 486 clarifies that creation of a function type with an
15042 invalid return type is a deduction failure. */
15043 if (TREE_CODE (return_type) == ARRAY_TYPE
15044 || TREE_CODE (return_type) == FUNCTION_TYPE)
15045 {
15046 if (complain & tf_error)
15047 {
15048 if (TREE_CODE (return_type) == ARRAY_TYPE)
15049 error ("function returning an array");
15050 else
15051 error ("function returning a function");
15052 }
15053 return error_mark_node;
15054 }
15055 /* And DR 657. */
15056 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
15057 return error_mark_node;
15058
15059 if (!late_return_type_p)
15060 {
15061 /* Substitute the argument types. */
15062 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15063 complain, in_decl);
15064 if (arg_types == error_mark_node)
15065 return error_mark_node;
15066 }
15067
15068 /* Construct a new type node and return it. */
15069 return rebuild_function_or_method_type (t, return_type, arg_types,
15070 /*raises=*/NULL_TREE, complain);
15071 }
15072
15073 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15074 ARGS into that specification, and return the substituted
15075 specification. If there is no specification, return NULL_TREE. */
15076
15077 static tree
15078 tsubst_exception_specification (tree fntype,
15079 tree args,
15080 tsubst_flags_t complain,
15081 tree in_decl,
15082 bool defer_ok)
15083 {
15084 tree specs;
15085 tree new_specs;
15086
15087 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15088 new_specs = NULL_TREE;
15089 if (specs && TREE_PURPOSE (specs))
15090 {
15091 /* A noexcept-specifier. */
15092 tree expr = TREE_PURPOSE (specs);
15093 if (TREE_CODE (expr) == INTEGER_CST)
15094 new_specs = expr;
15095 else if (defer_ok)
15096 {
15097 /* Defer instantiation of noexcept-specifiers to avoid
15098 excessive instantiations (c++/49107). */
15099 new_specs = make_node (DEFERRED_NOEXCEPT);
15100 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15101 {
15102 /* We already partially instantiated this member template,
15103 so combine the new args with the old. */
15104 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15105 = DEFERRED_NOEXCEPT_PATTERN (expr);
15106 DEFERRED_NOEXCEPT_ARGS (new_specs)
15107 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15108 }
15109 else
15110 {
15111 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15112 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15113 }
15114 }
15115 else
15116 {
15117 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15118 {
15119 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15120 args);
15121 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15122 }
15123 new_specs = tsubst_copy_and_build
15124 (expr, args, complain, in_decl, /*function_p=*/false,
15125 /*integral_constant_expression_p=*/true);
15126 }
15127 new_specs = build_noexcept_spec (new_specs, complain);
15128 }
15129 else if (specs)
15130 {
15131 if (! TREE_VALUE (specs))
15132 new_specs = specs;
15133 else
15134 while (specs)
15135 {
15136 tree spec;
15137 int i, len = 1;
15138 tree expanded_specs = NULL_TREE;
15139
15140 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15141 {
15142 /* Expand the pack expansion type. */
15143 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15144 args, complain,
15145 in_decl);
15146
15147 if (expanded_specs == error_mark_node)
15148 return error_mark_node;
15149 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15150 len = TREE_VEC_LENGTH (expanded_specs);
15151 else
15152 {
15153 /* We're substituting into a member template, so
15154 we got a TYPE_PACK_EXPANSION back. Add that
15155 expansion and move on. */
15156 gcc_assert (TREE_CODE (expanded_specs)
15157 == TYPE_PACK_EXPANSION);
15158 new_specs = add_exception_specifier (new_specs,
15159 expanded_specs,
15160 complain);
15161 specs = TREE_CHAIN (specs);
15162 continue;
15163 }
15164 }
15165
15166 for (i = 0; i < len; ++i)
15167 {
15168 if (expanded_specs)
15169 spec = TREE_VEC_ELT (expanded_specs, i);
15170 else
15171 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15172 if (spec == error_mark_node)
15173 return spec;
15174 new_specs = add_exception_specifier (new_specs, spec,
15175 complain);
15176 }
15177
15178 specs = TREE_CHAIN (specs);
15179 }
15180 }
15181 return new_specs;
15182 }
15183
15184 /* Substitute through a TREE_LIST of types or expressions, handling pack
15185 expansions. */
15186
15187 tree
15188 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15189 {
15190 if (t == void_list_node)
15191 return t;
15192
15193 tree purpose = TREE_PURPOSE (t);
15194 tree purposevec = NULL_TREE;
15195 if (!purpose)
15196 ;
15197 else if (PACK_EXPANSION_P (purpose))
15198 {
15199 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15200 if (TREE_CODE (purpose) == TREE_VEC)
15201 purposevec = purpose;
15202 }
15203 else if (TYPE_P (purpose))
15204 purpose = tsubst (purpose, args, complain, in_decl);
15205 else
15206 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15207 if (purpose == error_mark_node || purposevec == error_mark_node)
15208 return error_mark_node;
15209
15210 tree value = TREE_VALUE (t);
15211 tree valuevec = NULL_TREE;
15212 if (!value)
15213 ;
15214 else if (PACK_EXPANSION_P (value))
15215 {
15216 value = tsubst_pack_expansion (value, args, complain, in_decl);
15217 if (TREE_CODE (value) == TREE_VEC)
15218 valuevec = value;
15219 }
15220 else if (TYPE_P (value))
15221 value = tsubst (value, args, complain, in_decl);
15222 else
15223 value = tsubst_copy_and_build (value, args, complain, in_decl);
15224 if (value == error_mark_node || valuevec == error_mark_node)
15225 return error_mark_node;
15226
15227 tree chain = TREE_CHAIN (t);
15228 if (!chain)
15229 ;
15230 else if (TREE_CODE (chain) == TREE_LIST)
15231 chain = tsubst_tree_list (chain, args, complain, in_decl);
15232 else if (TYPE_P (chain))
15233 chain = tsubst (chain, args, complain, in_decl);
15234 else
15235 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15236 if (chain == error_mark_node)
15237 return error_mark_node;
15238
15239 if (purpose == TREE_PURPOSE (t)
15240 && value == TREE_VALUE (t)
15241 && chain == TREE_CHAIN (t))
15242 return t;
15243
15244 int len;
15245 /* Determine the number of arguments. */
15246 if (purposevec)
15247 {
15248 len = TREE_VEC_LENGTH (purposevec);
15249 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15250 }
15251 else if (valuevec)
15252 len = TREE_VEC_LENGTH (valuevec);
15253 else
15254 len = 1;
15255
15256 for (int i = len; i-- > 0; )
15257 {
15258 if (purposevec)
15259 purpose = TREE_VEC_ELT (purposevec, i);
15260 if (valuevec)
15261 value = TREE_VEC_ELT (valuevec, i);
15262
15263 if (value && TYPE_P (value))
15264 chain = hash_tree_cons (purpose, value, chain);
15265 else
15266 chain = tree_cons (purpose, value, chain);
15267 }
15268
15269 return chain;
15270 }
15271
15272 /* Take the tree structure T and replace template parameters used
15273 therein with the argument vector ARGS. IN_DECL is an associated
15274 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15275 Issue error and warning messages under control of COMPLAIN. Note
15276 that we must be relatively non-tolerant of extensions here, in
15277 order to preserve conformance; if we allow substitutions that
15278 should not be allowed, we may allow argument deductions that should
15279 not succeed, and therefore report ambiguous overload situations
15280 where there are none. In theory, we could allow the substitution,
15281 but indicate that it should have failed, and allow our caller to
15282 make sure that the right thing happens, but we don't try to do this
15283 yet.
15284
15285 This function is used for dealing with types, decls and the like;
15286 for expressions, use tsubst_expr or tsubst_copy. */
15287
15288 tree
15289 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15290 {
15291 enum tree_code code;
15292 tree type, r = NULL_TREE;
15293
15294 if (t == NULL_TREE || t == error_mark_node
15295 || t == integer_type_node
15296 || t == void_type_node
15297 || t == char_type_node
15298 || t == unknown_type_node
15299 || TREE_CODE (t) == NAMESPACE_DECL
15300 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15301 return t;
15302
15303 if (DECL_P (t))
15304 return tsubst_decl (t, args, complain);
15305
15306 if (args == NULL_TREE)
15307 return t;
15308
15309 code = TREE_CODE (t);
15310
15311 if (code == IDENTIFIER_NODE)
15312 type = IDENTIFIER_TYPE_VALUE (t);
15313 else
15314 type = TREE_TYPE (t);
15315
15316 gcc_assert (type != unknown_type_node);
15317
15318 /* Reuse typedefs. We need to do this to handle dependent attributes,
15319 such as attribute aligned. */
15320 if (TYPE_P (t)
15321 && typedef_variant_p (t))
15322 {
15323 tree decl = TYPE_NAME (t);
15324
15325 if (alias_template_specialization_p (t, nt_opaque))
15326 {
15327 /* DECL represents an alias template and we want to
15328 instantiate it. */
15329 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15330 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15331 r = instantiate_alias_template (tmpl, gen_args, complain);
15332 }
15333 else if (DECL_CLASS_SCOPE_P (decl)
15334 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15335 && uses_template_parms (DECL_CONTEXT (decl)))
15336 {
15337 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15338 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15339 r = retrieve_specialization (tmpl, gen_args, 0);
15340 }
15341 else if (DECL_FUNCTION_SCOPE_P (decl)
15342 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15343 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15344 r = retrieve_local_specialization (decl);
15345 else
15346 /* The typedef is from a non-template context. */
15347 return t;
15348
15349 if (r)
15350 {
15351 r = TREE_TYPE (r);
15352 r = cp_build_qualified_type_real
15353 (r, cp_type_quals (t) | cp_type_quals (r),
15354 complain | tf_ignore_bad_quals);
15355 return r;
15356 }
15357 else
15358 {
15359 /* We don't have an instantiation yet, so drop the typedef. */
15360 int quals = cp_type_quals (t);
15361 t = DECL_ORIGINAL_TYPE (decl);
15362 t = cp_build_qualified_type_real (t, quals,
15363 complain | tf_ignore_bad_quals);
15364 }
15365 }
15366
15367 bool fndecl_type = (complain & tf_fndecl_type);
15368 complain &= ~tf_fndecl_type;
15369
15370 if (type
15371 && code != TYPENAME_TYPE
15372 && code != TEMPLATE_TYPE_PARM
15373 && code != TEMPLATE_PARM_INDEX
15374 && code != IDENTIFIER_NODE
15375 && code != FUNCTION_TYPE
15376 && code != METHOD_TYPE)
15377 type = tsubst (type, args, complain, in_decl);
15378 if (type == error_mark_node)
15379 return error_mark_node;
15380
15381 switch (code)
15382 {
15383 case RECORD_TYPE:
15384 case UNION_TYPE:
15385 case ENUMERAL_TYPE:
15386 return tsubst_aggr_type (t, args, complain, in_decl,
15387 /*entering_scope=*/0);
15388
15389 case ERROR_MARK:
15390 case IDENTIFIER_NODE:
15391 case VOID_TYPE:
15392 case REAL_TYPE:
15393 case COMPLEX_TYPE:
15394 case VECTOR_TYPE:
15395 case BOOLEAN_TYPE:
15396 case NULLPTR_TYPE:
15397 case LANG_TYPE:
15398 return t;
15399
15400 case INTEGER_TYPE:
15401 if (t == integer_type_node)
15402 return t;
15403
15404 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15405 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15406 return t;
15407
15408 {
15409 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15410
15411 max = tsubst_expr (omax, args, complain, in_decl,
15412 /*integral_constant_expression_p=*/false);
15413
15414 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15415 needed. */
15416 if (TREE_CODE (max) == NOP_EXPR
15417 && TREE_SIDE_EFFECTS (omax)
15418 && !TREE_TYPE (max))
15419 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15420
15421 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15422 with TREE_SIDE_EFFECTS that indicates this is not an integral
15423 constant expression. */
15424 if (processing_template_decl
15425 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15426 {
15427 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15428 TREE_SIDE_EFFECTS (max) = 1;
15429 }
15430
15431 return compute_array_index_type (NULL_TREE, max, complain);
15432 }
15433
15434 case TEMPLATE_TYPE_PARM:
15435 case TEMPLATE_TEMPLATE_PARM:
15436 case BOUND_TEMPLATE_TEMPLATE_PARM:
15437 case TEMPLATE_PARM_INDEX:
15438 {
15439 int idx;
15440 int level;
15441 int levels;
15442 tree arg = NULL_TREE;
15443
15444 r = NULL_TREE;
15445
15446 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15447 template_parm_level_and_index (t, &level, &idx);
15448
15449 levels = TMPL_ARGS_DEPTH (args);
15450 if (level <= levels
15451 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15452 {
15453 arg = TMPL_ARG (args, level, idx);
15454
15455 /* See through ARGUMENT_PACK_SELECT arguments. */
15456 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15457 arg = argument_pack_select_arg (arg);
15458 }
15459
15460 if (arg == error_mark_node)
15461 return error_mark_node;
15462 else if (arg != NULL_TREE)
15463 {
15464 if (ARGUMENT_PACK_P (arg))
15465 /* If ARG is an argument pack, we don't actually want to
15466 perform a substitution here, because substitutions
15467 for argument packs are only done
15468 element-by-element. We can get to this point when
15469 substituting the type of a non-type template
15470 parameter pack, when that type actually contains
15471 template parameter packs from an outer template, e.g.,
15472
15473 template<typename... Types> struct A {
15474 template<Types... Values> struct B { };
15475 }; */
15476 return t;
15477
15478 if (code == TEMPLATE_TYPE_PARM)
15479 {
15480 int quals;
15481
15482 /* When building concept checks for the purpose of
15483 deducing placeholders, we can end up with wildcards
15484 where types are expected. Adjust this to the deduced
15485 value. */
15486 if (TREE_CODE (arg) == WILDCARD_DECL)
15487 arg = TREE_TYPE (TREE_TYPE (arg));
15488
15489 gcc_assert (TYPE_P (arg));
15490
15491 quals = cp_type_quals (arg) | cp_type_quals (t);
15492
15493 return cp_build_qualified_type_real
15494 (arg, quals, complain | tf_ignore_bad_quals);
15495 }
15496 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15497 {
15498 /* We are processing a type constructed from a
15499 template template parameter. */
15500 tree argvec = tsubst (TYPE_TI_ARGS (t),
15501 args, complain, in_decl);
15502 if (argvec == error_mark_node)
15503 return error_mark_node;
15504
15505 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15506 || TREE_CODE (arg) == TEMPLATE_DECL
15507 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15508
15509 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15510 /* Consider this code:
15511
15512 template <template <class> class Template>
15513 struct Internal {
15514 template <class Arg> using Bind = Template<Arg>;
15515 };
15516
15517 template <template <class> class Template, class Arg>
15518 using Instantiate = Template<Arg>; //#0
15519
15520 template <template <class> class Template,
15521 class Argument>
15522 using Bind =
15523 Instantiate<Internal<Template>::template Bind,
15524 Argument>; //#1
15525
15526 When #1 is parsed, the
15527 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15528 parameter `Template' in #0 matches the
15529 UNBOUND_CLASS_TEMPLATE representing the argument
15530 `Internal<Template>::template Bind'; We then want
15531 to assemble the type `Bind<Argument>' that can't
15532 be fully created right now, because
15533 `Internal<Template>' not being complete, the Bind
15534 template cannot be looked up in that context. So
15535 we need to "store" `Bind<Argument>' for later
15536 when the context of Bind becomes complete. Let's
15537 store that in a TYPENAME_TYPE. */
15538 return make_typename_type (TYPE_CONTEXT (arg),
15539 build_nt (TEMPLATE_ID_EXPR,
15540 TYPE_IDENTIFIER (arg),
15541 argvec),
15542 typename_type,
15543 complain);
15544
15545 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15546 are resolving nested-types in the signature of a
15547 member function templates. Otherwise ARG is a
15548 TEMPLATE_DECL and is the real template to be
15549 instantiated. */
15550 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15551 arg = TYPE_NAME (arg);
15552
15553 r = lookup_template_class (arg,
15554 argvec, in_decl,
15555 DECL_CONTEXT (arg),
15556 /*entering_scope=*/0,
15557 complain);
15558 return cp_build_qualified_type_real
15559 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15560 }
15561 else if (code == TEMPLATE_TEMPLATE_PARM)
15562 return arg;
15563 else
15564 /* TEMPLATE_PARM_INDEX. */
15565 return convert_from_reference (unshare_expr (arg));
15566 }
15567
15568 if (level == 1)
15569 /* This can happen during the attempted tsubst'ing in
15570 unify. This means that we don't yet have any information
15571 about the template parameter in question. */
15572 return t;
15573
15574 /* Early in template argument deduction substitution, we don't
15575 want to reduce the level of 'auto', or it will be confused
15576 with a normal template parm in subsequent deduction.
15577 Similarly, don't reduce the level of template parameters to
15578 avoid mismatches when deducing their types. */
15579 if (complain & tf_partial)
15580 return t;
15581
15582 /* If we get here, we must have been looking at a parm for a
15583 more deeply nested template. Make a new version of this
15584 template parameter, but with a lower level. */
15585 switch (code)
15586 {
15587 case TEMPLATE_TYPE_PARM:
15588 case TEMPLATE_TEMPLATE_PARM:
15589 case BOUND_TEMPLATE_TEMPLATE_PARM:
15590 if (cp_type_quals (t))
15591 {
15592 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15593 r = cp_build_qualified_type_real
15594 (r, cp_type_quals (t),
15595 complain | (code == TEMPLATE_TYPE_PARM
15596 ? tf_ignore_bad_quals : 0));
15597 }
15598 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15599 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15600 && (r = (TEMPLATE_PARM_DESCENDANTS
15601 (TEMPLATE_TYPE_PARM_INDEX (t))))
15602 && (r = TREE_TYPE (r))
15603 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15604 /* Break infinite recursion when substituting the constraints
15605 of a constrained placeholder. */;
15606 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15607 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15608 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15609 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15610 r = TEMPLATE_PARM_DESCENDANTS (arg))
15611 && (TEMPLATE_PARM_LEVEL (r)
15612 == TEMPLATE_PARM_LEVEL (arg) - levels))
15613 /* Cache the simple case of lowering a type parameter. */
15614 r = TREE_TYPE (r);
15615 else
15616 {
15617 r = copy_type (t);
15618 TEMPLATE_TYPE_PARM_INDEX (r)
15619 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15620 r, levels, args, complain);
15621 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15622 TYPE_MAIN_VARIANT (r) = r;
15623 TYPE_POINTER_TO (r) = NULL_TREE;
15624 TYPE_REFERENCE_TO (r) = NULL_TREE;
15625
15626 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15627 {
15628 /* Propagate constraints on placeholders since they are
15629 only instantiated during satisfaction. */
15630 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15631 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15632 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15633 {
15634 pl = tsubst_copy (pl, args, complain, in_decl);
15635 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15636 }
15637 }
15638
15639 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15640 /* We have reduced the level of the template
15641 template parameter, but not the levels of its
15642 template parameters, so canonical_type_parameter
15643 will not be able to find the canonical template
15644 template parameter for this level. Thus, we
15645 require structural equality checking to compare
15646 TEMPLATE_TEMPLATE_PARMs. */
15647 SET_TYPE_STRUCTURAL_EQUALITY (r);
15648 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15649 SET_TYPE_STRUCTURAL_EQUALITY (r);
15650 else
15651 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15652
15653 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15654 {
15655 tree tinfo = TYPE_TEMPLATE_INFO (t);
15656 /* We might need to substitute into the types of non-type
15657 template parameters. */
15658 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15659 complain, in_decl);
15660 if (tmpl == error_mark_node)
15661 return error_mark_node;
15662 tree argvec = tsubst (TI_ARGS (tinfo), args,
15663 complain, in_decl);
15664 if (argvec == error_mark_node)
15665 return error_mark_node;
15666
15667 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15668 = build_template_info (tmpl, argvec);
15669 }
15670 }
15671 break;
15672
15673 case TEMPLATE_PARM_INDEX:
15674 /* OK, now substitute the type of the non-type parameter. We
15675 couldn't do it earlier because it might be an auto parameter,
15676 and we wouldn't need to if we had an argument. */
15677 type = tsubst (type, args, complain, in_decl);
15678 if (type == error_mark_node)
15679 return error_mark_node;
15680 r = reduce_template_parm_level (t, type, levels, args, complain);
15681 break;
15682
15683 default:
15684 gcc_unreachable ();
15685 }
15686
15687 return r;
15688 }
15689
15690 case TREE_LIST:
15691 return tsubst_tree_list (t, args, complain, in_decl);
15692
15693 case TREE_BINFO:
15694 /* We should never be tsubsting a binfo. */
15695 gcc_unreachable ();
15696
15697 case TREE_VEC:
15698 /* A vector of template arguments. */
15699 gcc_assert (!type);
15700 return tsubst_template_args (t, args, complain, in_decl);
15701
15702 case POINTER_TYPE:
15703 case REFERENCE_TYPE:
15704 {
15705 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15706 return t;
15707
15708 /* [temp.deduct]
15709
15710 Type deduction may fail for any of the following
15711 reasons:
15712
15713 -- Attempting to create a pointer to reference type.
15714 -- Attempting to create a reference to a reference type or
15715 a reference to void.
15716
15717 Core issue 106 says that creating a reference to a reference
15718 during instantiation is no longer a cause for failure. We
15719 only enforce this check in strict C++98 mode. */
15720 if ((TYPE_REF_P (type)
15721 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15722 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15723 {
15724 static location_t last_loc;
15725
15726 /* We keep track of the last time we issued this error
15727 message to avoid spewing a ton of messages during a
15728 single bad template instantiation. */
15729 if (complain & tf_error
15730 && last_loc != input_location)
15731 {
15732 if (VOID_TYPE_P (type))
15733 error ("forming reference to void");
15734 else if (code == POINTER_TYPE)
15735 error ("forming pointer to reference type %qT", type);
15736 else
15737 error ("forming reference to reference type %qT", type);
15738 last_loc = input_location;
15739 }
15740
15741 return error_mark_node;
15742 }
15743 else if (TREE_CODE (type) == FUNCTION_TYPE
15744 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15745 || type_memfn_rqual (type) != REF_QUAL_NONE))
15746 {
15747 if (complain & tf_error)
15748 {
15749 if (code == POINTER_TYPE)
15750 error ("forming pointer to qualified function type %qT",
15751 type);
15752 else
15753 error ("forming reference to qualified function type %qT",
15754 type);
15755 }
15756 return error_mark_node;
15757 }
15758 else if (code == POINTER_TYPE)
15759 {
15760 r = build_pointer_type (type);
15761 if (TREE_CODE (type) == METHOD_TYPE)
15762 r = build_ptrmemfunc_type (r);
15763 }
15764 else if (TYPE_REF_P (type))
15765 /* In C++0x, during template argument substitution, when there is an
15766 attempt to create a reference to a reference type, reference
15767 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15768
15769 "If a template-argument for a template-parameter T names a type
15770 that is a reference to a type A, an attempt to create the type
15771 'lvalue reference to cv T' creates the type 'lvalue reference to
15772 A,' while an attempt to create the type type rvalue reference to
15773 cv T' creates the type T"
15774 */
15775 r = cp_build_reference_type
15776 (TREE_TYPE (type),
15777 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15778 else
15779 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15780 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15781
15782 if (r != error_mark_node)
15783 /* Will this ever be needed for TYPE_..._TO values? */
15784 layout_type (r);
15785
15786 return r;
15787 }
15788 case OFFSET_TYPE:
15789 {
15790 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15791 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15792 {
15793 /* [temp.deduct]
15794
15795 Type deduction may fail for any of the following
15796 reasons:
15797
15798 -- Attempting to create "pointer to member of T" when T
15799 is not a class type. */
15800 if (complain & tf_error)
15801 error ("creating pointer to member of non-class type %qT", r);
15802 return error_mark_node;
15803 }
15804 if (TYPE_REF_P (type))
15805 {
15806 if (complain & tf_error)
15807 error ("creating pointer to member reference type %qT", type);
15808 return error_mark_node;
15809 }
15810 if (VOID_TYPE_P (type))
15811 {
15812 if (complain & tf_error)
15813 error ("creating pointer to member of type void");
15814 return error_mark_node;
15815 }
15816 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15817 if (TREE_CODE (type) == FUNCTION_TYPE)
15818 {
15819 /* The type of the implicit object parameter gets its
15820 cv-qualifiers from the FUNCTION_TYPE. */
15821 tree memptr;
15822 tree method_type
15823 = build_memfn_type (type, r, type_memfn_quals (type),
15824 type_memfn_rqual (type));
15825 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15826 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15827 complain);
15828 }
15829 else
15830 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15831 cp_type_quals (t),
15832 complain);
15833 }
15834 case FUNCTION_TYPE:
15835 case METHOD_TYPE:
15836 {
15837 tree fntype;
15838 tree specs;
15839 fntype = tsubst_function_type (t, args, complain, in_decl);
15840 if (fntype == error_mark_node)
15841 return error_mark_node;
15842
15843 /* Substitute the exception specification. */
15844 specs = tsubst_exception_specification (t, args, complain, in_decl,
15845 /*defer_ok*/fndecl_type);
15846 if (specs == error_mark_node)
15847 return error_mark_node;
15848 if (specs)
15849 fntype = build_exception_variant (fntype, specs);
15850 return fntype;
15851 }
15852 case ARRAY_TYPE:
15853 {
15854 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15855 if (domain == error_mark_node)
15856 return error_mark_node;
15857
15858 /* As an optimization, we avoid regenerating the array type if
15859 it will obviously be the same as T. */
15860 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15861 return t;
15862
15863 /* These checks should match the ones in create_array_type_for_decl.
15864
15865 [temp.deduct]
15866
15867 The deduction may fail for any of the following reasons:
15868
15869 -- Attempting to create an array with an element type that
15870 is void, a function type, or a reference type, or [DR337]
15871 an abstract class type. */
15872 if (VOID_TYPE_P (type)
15873 || TREE_CODE (type) == FUNCTION_TYPE
15874 || (TREE_CODE (type) == ARRAY_TYPE
15875 && TYPE_DOMAIN (type) == NULL_TREE)
15876 || TYPE_REF_P (type))
15877 {
15878 if (complain & tf_error)
15879 error ("creating array of %qT", type);
15880 return error_mark_node;
15881 }
15882
15883 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15884 return error_mark_node;
15885
15886 r = build_cplus_array_type (type, domain);
15887
15888 if (!valid_array_size_p (input_location, r, in_decl,
15889 (complain & tf_error)))
15890 return error_mark_node;
15891
15892 if (TYPE_USER_ALIGN (t))
15893 {
15894 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15895 TYPE_USER_ALIGN (r) = 1;
15896 }
15897
15898 return r;
15899 }
15900
15901 case TYPENAME_TYPE:
15902 {
15903 tree ctx = TYPE_CONTEXT (t);
15904 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15905 {
15906 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15907 if (ctx == error_mark_node
15908 || TREE_VEC_LENGTH (ctx) > 1)
15909 return error_mark_node;
15910 if (TREE_VEC_LENGTH (ctx) == 0)
15911 {
15912 if (complain & tf_error)
15913 error ("%qD is instantiated for an empty pack",
15914 TYPENAME_TYPE_FULLNAME (t));
15915 return error_mark_node;
15916 }
15917 ctx = TREE_VEC_ELT (ctx, 0);
15918 }
15919 else
15920 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15921 /*entering_scope=*/1);
15922 if (ctx == error_mark_node)
15923 return error_mark_node;
15924
15925 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15926 complain, in_decl);
15927 if (f == error_mark_node)
15928 return error_mark_node;
15929
15930 if (!MAYBE_CLASS_TYPE_P (ctx))
15931 {
15932 if (complain & tf_error)
15933 error ("%qT is not a class, struct, or union type", ctx);
15934 return error_mark_node;
15935 }
15936 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15937 {
15938 /* Normally, make_typename_type does not require that the CTX
15939 have complete type in order to allow things like:
15940
15941 template <class T> struct S { typename S<T>::X Y; };
15942
15943 But, such constructs have already been resolved by this
15944 point, so here CTX really should have complete type, unless
15945 it's a partial instantiation. */
15946 ctx = complete_type (ctx);
15947 if (!COMPLETE_TYPE_P (ctx))
15948 {
15949 if (complain & tf_error)
15950 cxx_incomplete_type_error (NULL_TREE, ctx);
15951 return error_mark_node;
15952 }
15953 }
15954
15955 f = make_typename_type (ctx, f, typename_type,
15956 complain | tf_keep_type_decl);
15957 if (f == error_mark_node)
15958 return f;
15959 if (TREE_CODE (f) == TYPE_DECL)
15960 {
15961 complain |= tf_ignore_bad_quals;
15962 f = TREE_TYPE (f);
15963 }
15964
15965 if (TREE_CODE (f) != TYPENAME_TYPE)
15966 {
15967 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15968 {
15969 if (complain & tf_error)
15970 error ("%qT resolves to %qT, which is not an enumeration type",
15971 t, f);
15972 else
15973 return error_mark_node;
15974 }
15975 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15976 {
15977 if (complain & tf_error)
15978 error ("%qT resolves to %qT, which is not a class type",
15979 t, f);
15980 else
15981 return error_mark_node;
15982 }
15983 }
15984
15985 return cp_build_qualified_type_real
15986 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15987 }
15988
15989 case UNBOUND_CLASS_TEMPLATE:
15990 {
15991 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15992 in_decl, /*entering_scope=*/1);
15993 tree name = TYPE_IDENTIFIER (t);
15994 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15995
15996 if (ctx == error_mark_node || name == error_mark_node)
15997 return error_mark_node;
15998
15999 if (parm_list)
16000 parm_list = tsubst_template_parms (parm_list, args, complain);
16001 return make_unbound_class_template (ctx, name, parm_list, complain);
16002 }
16003
16004 case TYPEOF_TYPE:
16005 {
16006 tree type;
16007
16008 ++cp_unevaluated_operand;
16009 ++c_inhibit_evaluation_warnings;
16010
16011 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
16012 complain, in_decl,
16013 /*integral_constant_expression_p=*/false);
16014
16015 --cp_unevaluated_operand;
16016 --c_inhibit_evaluation_warnings;
16017
16018 type = finish_typeof (type);
16019 return cp_build_qualified_type_real (type,
16020 cp_type_quals (t)
16021 | cp_type_quals (type),
16022 complain);
16023 }
16024
16025 case DECLTYPE_TYPE:
16026 {
16027 tree type;
16028
16029 ++cp_unevaluated_operand;
16030 ++c_inhibit_evaluation_warnings;
16031
16032 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16033 complain|tf_decltype, in_decl,
16034 /*function_p*/false,
16035 /*integral_constant_expression*/false);
16036
16037 --cp_unevaluated_operand;
16038 --c_inhibit_evaluation_warnings;
16039
16040 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16041 type = lambda_capture_field_type (type,
16042 false /*explicit_init*/,
16043 DECLTYPE_FOR_REF_CAPTURE (t));
16044 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16045 type = lambda_proxy_type (type);
16046 else
16047 {
16048 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16049 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16050 && EXPR_P (type))
16051 /* In a template ~id could be either a complement expression
16052 or an unqualified-id naming a destructor; if instantiating
16053 it produces an expression, it's not an id-expression or
16054 member access. */
16055 id = false;
16056 type = finish_decltype_type (type, id, complain);
16057 }
16058 return cp_build_qualified_type_real (type,
16059 cp_type_quals (t)
16060 | cp_type_quals (type),
16061 complain | tf_ignore_bad_quals);
16062 }
16063
16064 case UNDERLYING_TYPE:
16065 {
16066 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16067 complain, in_decl);
16068 return finish_underlying_type (type);
16069 }
16070
16071 case TYPE_ARGUMENT_PACK:
16072 case NONTYPE_ARGUMENT_PACK:
16073 return tsubst_argument_pack (t, args, complain, in_decl);
16074
16075 case VOID_CST:
16076 case INTEGER_CST:
16077 case REAL_CST:
16078 case STRING_CST:
16079 case PLUS_EXPR:
16080 case MINUS_EXPR:
16081 case NEGATE_EXPR:
16082 case NOP_EXPR:
16083 case INDIRECT_REF:
16084 case ADDR_EXPR:
16085 case CALL_EXPR:
16086 case ARRAY_REF:
16087 case SCOPE_REF:
16088 /* We should use one of the expression tsubsts for these codes. */
16089 gcc_unreachable ();
16090
16091 default:
16092 sorry ("use of %qs in template", get_tree_code_name (code));
16093 return error_mark_node;
16094 }
16095 }
16096
16097 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16098 expression on the left-hand side of the "." or "->" operator. We
16099 only do the lookup if we had a dependent BASELINK. Otherwise we
16100 adjust it onto the instantiated heirarchy. */
16101
16102 static tree
16103 tsubst_baselink (tree baselink, tree object_type,
16104 tree args, tsubst_flags_t complain, tree in_decl)
16105 {
16106 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16107 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16108 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16109
16110 tree optype = BASELINK_OPTYPE (baselink);
16111 optype = tsubst (optype, args, complain, in_decl);
16112
16113 tree template_args = NULL_TREE;
16114 bool template_id_p = false;
16115 tree fns = BASELINK_FUNCTIONS (baselink);
16116 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16117 {
16118 template_id_p = true;
16119 template_args = TREE_OPERAND (fns, 1);
16120 fns = TREE_OPERAND (fns, 0);
16121 if (template_args)
16122 template_args = tsubst_template_args (template_args, args,
16123 complain, in_decl);
16124 }
16125
16126 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16127 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16128 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16129
16130 if (dependent_p)
16131 {
16132 tree name = OVL_NAME (fns);
16133 if (IDENTIFIER_CONV_OP_P (name))
16134 name = make_conv_op_name (optype);
16135
16136 if (name == complete_dtor_identifier)
16137 /* Treat as-if non-dependent below. */
16138 dependent_p = false;
16139
16140 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16141 complain);
16142 if (!baselink)
16143 {
16144 if ((complain & tf_error)
16145 && constructor_name_p (name, qualifying_scope))
16146 error ("cannot call constructor %<%T::%D%> directly",
16147 qualifying_scope, name);
16148 return error_mark_node;
16149 }
16150
16151 if (BASELINK_P (baselink))
16152 fns = BASELINK_FUNCTIONS (baselink);
16153 }
16154 else
16155 /* We're going to overwrite pieces below, make a duplicate. */
16156 baselink = copy_node (baselink);
16157
16158 /* If lookup found a single function, mark it as used at this point.
16159 (If lookup found multiple functions the one selected later by
16160 overload resolution will be marked as used at that point.) */
16161 if (!template_id_p && !really_overloaded_fn (fns))
16162 {
16163 tree fn = OVL_FIRST (fns);
16164 bool ok = mark_used (fn, complain);
16165 if (!ok && !(complain & tf_error))
16166 return error_mark_node;
16167 if (ok && BASELINK_P (baselink))
16168 /* We might have instantiated an auto function. */
16169 TREE_TYPE (baselink) = TREE_TYPE (fn);
16170 }
16171
16172 if (BASELINK_P (baselink))
16173 {
16174 /* Add back the template arguments, if present. */
16175 if (template_id_p)
16176 BASELINK_FUNCTIONS (baselink)
16177 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16178
16179 /* Update the conversion operator type. */
16180 BASELINK_OPTYPE (baselink) = optype;
16181 }
16182
16183 if (!object_type)
16184 object_type = current_class_type;
16185
16186 if (qualified_p || !dependent_p)
16187 {
16188 baselink = adjust_result_of_qualified_name_lookup (baselink,
16189 qualifying_scope,
16190 object_type);
16191 if (!qualified_p)
16192 /* We need to call adjust_result_of_qualified_name_lookup in case the
16193 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16194 so that we still get virtual function binding. */
16195 BASELINK_QUALIFIED_P (baselink) = false;
16196 }
16197
16198 return baselink;
16199 }
16200
16201 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16202 true if the qualified-id will be a postfix-expression in-and-of
16203 itself; false if more of the postfix-expression follows the
16204 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16205 of "&". */
16206
16207 static tree
16208 tsubst_qualified_id (tree qualified_id, tree args,
16209 tsubst_flags_t complain, tree in_decl,
16210 bool done, bool address_p)
16211 {
16212 tree expr;
16213 tree scope;
16214 tree name;
16215 bool is_template;
16216 tree template_args;
16217 location_t loc = EXPR_LOCATION (qualified_id);
16218
16219 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16220
16221 /* Figure out what name to look up. */
16222 name = TREE_OPERAND (qualified_id, 1);
16223 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16224 {
16225 is_template = true;
16226 template_args = TREE_OPERAND (name, 1);
16227 if (template_args)
16228 template_args = tsubst_template_args (template_args, args,
16229 complain, in_decl);
16230 if (template_args == error_mark_node)
16231 return error_mark_node;
16232 name = TREE_OPERAND (name, 0);
16233 }
16234 else
16235 {
16236 is_template = false;
16237 template_args = NULL_TREE;
16238 }
16239
16240 /* Substitute into the qualifying scope. When there are no ARGS, we
16241 are just trying to simplify a non-dependent expression. In that
16242 case the qualifying scope may be dependent, and, in any case,
16243 substituting will not help. */
16244 scope = TREE_OPERAND (qualified_id, 0);
16245 if (args)
16246 {
16247 scope = tsubst (scope, args, complain, in_decl);
16248 expr = tsubst_copy (name, args, complain, in_decl);
16249 }
16250 else
16251 expr = name;
16252
16253 if (dependent_scope_p (scope))
16254 {
16255 if (is_template)
16256 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16257 tree r = build_qualified_name (NULL_TREE, scope, expr,
16258 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16259 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16260 return r;
16261 }
16262
16263 if (!BASELINK_P (name) && !DECL_P (expr))
16264 {
16265 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16266 {
16267 /* A BIT_NOT_EXPR is used to represent a destructor. */
16268 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16269 {
16270 error ("qualifying type %qT does not match destructor name ~%qT",
16271 scope, TREE_OPERAND (expr, 0));
16272 expr = error_mark_node;
16273 }
16274 else
16275 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16276 LOOK_want::NORMAL, false);
16277 }
16278 else
16279 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16280 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16281 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16282 {
16283 if (complain & tf_error)
16284 {
16285 error ("dependent-name %qE is parsed as a non-type, but "
16286 "instantiation yields a type", qualified_id);
16287 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16288 }
16289 return error_mark_node;
16290 }
16291 }
16292
16293 if (DECL_P (expr))
16294 {
16295 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16296 scope, complain))
16297 return error_mark_node;
16298 /* Remember that there was a reference to this entity. */
16299 if (!mark_used (expr, complain) && !(complain & tf_error))
16300 return error_mark_node;
16301 }
16302
16303 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16304 {
16305 if (complain & tf_error)
16306 qualified_name_lookup_error (scope,
16307 TREE_OPERAND (qualified_id, 1),
16308 expr, input_location);
16309 return error_mark_node;
16310 }
16311
16312 if (is_template)
16313 {
16314 /* We may be repeating a check already done during parsing, but
16315 if it was well-formed and passed then, it will pass again
16316 now, and if it didn't, we wouldn't have got here. The case
16317 we want to catch is when we couldn't tell then, and can now,
16318 namely when templ prior to substitution was an
16319 identifier. */
16320 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16321 return error_mark_node;
16322
16323 if (variable_template_p (expr))
16324 expr = lookup_and_finish_template_variable (expr, template_args,
16325 complain);
16326 else
16327 expr = lookup_template_function (expr, template_args);
16328 }
16329
16330 if (expr == error_mark_node && complain & tf_error)
16331 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16332 expr, input_location);
16333 else if (TYPE_P (scope))
16334 {
16335 expr = (adjust_result_of_qualified_name_lookup
16336 (expr, scope, current_nonlambda_class_type ()));
16337 expr = (finish_qualified_id_expr
16338 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16339 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16340 /*template_arg_p=*/false, complain));
16341 }
16342
16343 /* Expressions do not generally have reference type. */
16344 if (TREE_CODE (expr) != SCOPE_REF
16345 /* However, if we're about to form a pointer-to-member, we just
16346 want the referenced member referenced. */
16347 && TREE_CODE (expr) != OFFSET_REF)
16348 expr = convert_from_reference (expr);
16349
16350 if (REF_PARENTHESIZED_P (qualified_id))
16351 expr = force_paren_expr (expr);
16352
16353 expr = maybe_wrap_with_location (expr, loc);
16354
16355 return expr;
16356 }
16357
16358 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16359 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16360 for tsubst. */
16361
16362 static tree
16363 tsubst_init (tree init, tree decl, tree args,
16364 tsubst_flags_t complain, tree in_decl)
16365 {
16366 if (!init)
16367 return NULL_TREE;
16368
16369 init = tsubst_expr (init, args, complain, in_decl, false);
16370
16371 tree type = TREE_TYPE (decl);
16372
16373 if (!init && type != error_mark_node)
16374 {
16375 if (tree auto_node = type_uses_auto (type))
16376 {
16377 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16378 {
16379 if (complain & tf_error)
16380 error ("initializer for %q#D expands to an empty list "
16381 "of expressions", decl);
16382 return error_mark_node;
16383 }
16384 }
16385 else if (!dependent_type_p (type))
16386 {
16387 /* If we had an initializer but it
16388 instantiated to nothing,
16389 value-initialize the object. This will
16390 only occur when the initializer was a
16391 pack expansion where the parameter packs
16392 used in that expansion were of length
16393 zero. */
16394 init = build_value_init (type, complain);
16395 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16396 init = get_target_expr_sfinae (init, complain);
16397 if (TREE_CODE (init) == TARGET_EXPR)
16398 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16399 }
16400 }
16401
16402 return init;
16403 }
16404
16405 /* If T is a reference to a dependent member of the current instantiation C and
16406 we are trying to refer to that member in a partial instantiation of C,
16407 return a SCOPE_REF; otherwise, return NULL_TREE.
16408
16409 This can happen when forming a C++17 deduction guide, as in PR96199. */
16410
16411 static tree
16412 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16413 tree in_decl)
16414 {
16415 if (cxx_dialect < cxx17)
16416 return NULL_TREE;
16417
16418 tree ctx = context_for_name_lookup (t);
16419 if (!CLASS_TYPE_P (ctx))
16420 return NULL_TREE;
16421
16422 ctx = tsubst (ctx, args, complain, in_decl);
16423 if (dependent_scope_p (ctx))
16424 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16425 /*template_p=*/false);
16426
16427 return NULL_TREE;
16428 }
16429
16430 /* Like tsubst, but deals with expressions. This function just replaces
16431 template parms; to finish processing the resultant expression, use
16432 tsubst_copy_and_build or tsubst_expr. */
16433
16434 static tree
16435 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16436 {
16437 enum tree_code code;
16438 tree r;
16439
16440 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16441 return t;
16442
16443 code = TREE_CODE (t);
16444
16445 switch (code)
16446 {
16447 case PARM_DECL:
16448 r = retrieve_local_specialization (t);
16449
16450 if (r == NULL_TREE)
16451 {
16452 /* We get here for a use of 'this' in an NSDMI. */
16453 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16454 return current_class_ptr;
16455
16456 /* This can happen for a parameter name used later in a function
16457 declaration (such as in a late-specified return type). Just
16458 make a dummy decl, since it's only used for its type. */
16459 gcc_assert (cp_unevaluated_operand != 0);
16460 r = tsubst_decl (t, args, complain);
16461 /* Give it the template pattern as its context; its true context
16462 hasn't been instantiated yet and this is good enough for
16463 mangling. */
16464 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16465 }
16466
16467 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16468 r = argument_pack_select_arg (r);
16469 if (!mark_used (r, complain) && !(complain & tf_error))
16470 return error_mark_node;
16471 return r;
16472
16473 case CONST_DECL:
16474 {
16475 tree enum_type;
16476 tree v;
16477
16478 if (DECL_TEMPLATE_PARM_P (t))
16479 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16480 /* There is no need to substitute into namespace-scope
16481 enumerators. */
16482 if (DECL_NAMESPACE_SCOPE_P (t))
16483 return t;
16484 /* If ARGS is NULL, then T is known to be non-dependent. */
16485 if (args == NULL_TREE)
16486 return scalar_constant_value (t);
16487
16488 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16489 return ref;
16490
16491 /* Unfortunately, we cannot just call lookup_name here.
16492 Consider:
16493
16494 template <int I> int f() {
16495 enum E { a = I };
16496 struct S { void g() { E e = a; } };
16497 };
16498
16499 When we instantiate f<7>::S::g(), say, lookup_name is not
16500 clever enough to find f<7>::a. */
16501 enum_type
16502 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16503 /*entering_scope=*/0);
16504
16505 for (v = TYPE_VALUES (enum_type);
16506 v != NULL_TREE;
16507 v = TREE_CHAIN (v))
16508 if (TREE_PURPOSE (v) == DECL_NAME (t))
16509 return TREE_VALUE (v);
16510
16511 /* We didn't find the name. That should never happen; if
16512 name-lookup found it during preliminary parsing, we
16513 should find it again here during instantiation. */
16514 gcc_unreachable ();
16515 }
16516 return t;
16517
16518 case FIELD_DECL:
16519 if (DECL_CONTEXT (t))
16520 {
16521 tree ctx;
16522
16523 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16524 /*entering_scope=*/1);
16525 if (ctx != DECL_CONTEXT (t))
16526 {
16527 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16528 if (!r)
16529 {
16530 if (complain & tf_error)
16531 error ("using invalid field %qD", t);
16532 return error_mark_node;
16533 }
16534 return r;
16535 }
16536 }
16537
16538 return t;
16539
16540 case VAR_DECL:
16541 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16542 return ref;
16543 gcc_fallthrough();
16544 case FUNCTION_DECL:
16545 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16546 r = tsubst (t, args, complain, in_decl);
16547 else if (DECL_LOCAL_DECL_P (t))
16548 {
16549 /* Local specialization will have been created when we
16550 instantiated the DECL_EXPR_DECL. */
16551 r = retrieve_local_specialization (t);
16552 if (!r)
16553 r = error_mark_node;
16554 }
16555 else if (local_variable_p (t)
16556 && uses_template_parms (DECL_CONTEXT (t)))
16557 {
16558 r = retrieve_local_specialization (t);
16559 if (r == NULL_TREE)
16560 {
16561 /* First try name lookup to find the instantiation. */
16562 r = lookup_name (DECL_NAME (t));
16563 if (r)
16564 {
16565 if (!VAR_P (r))
16566 {
16567 /* During error-recovery we may find a non-variable,
16568 even an OVERLOAD: just bail out and avoid ICEs and
16569 duplicate diagnostics (c++/62207). */
16570 gcc_assert (seen_error ());
16571 return error_mark_node;
16572 }
16573 if (!is_capture_proxy (r))
16574 {
16575 /* Make sure the one we found is the one we want. */
16576 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16577 if (ctx != DECL_CONTEXT (r))
16578 r = NULL_TREE;
16579 }
16580 }
16581
16582 if (r)
16583 /* OK */;
16584 else
16585 {
16586 /* This can happen for a variable used in a
16587 late-specified return type of a local lambda, or for a
16588 local static or constant. Building a new VAR_DECL
16589 should be OK in all those cases. */
16590 r = tsubst_decl (t, args, complain);
16591 if (local_specializations)
16592 /* Avoid infinite recursion (79640). */
16593 register_local_specialization (r, t);
16594 if (decl_maybe_constant_var_p (r))
16595 {
16596 /* We can't call cp_finish_decl, so handle the
16597 initializer by hand. */
16598 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16599 complain, in_decl);
16600 if (!processing_template_decl)
16601 init = maybe_constant_init (init);
16602 if (processing_template_decl
16603 ? potential_constant_expression (init)
16604 : reduced_constant_expression_p (init))
16605 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16606 = TREE_CONSTANT (r) = true;
16607 DECL_INITIAL (r) = init;
16608 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16609 TREE_TYPE (r)
16610 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16611 complain, adc_variable_type);
16612 }
16613 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16614 || decl_constant_var_p (r)
16615 || seen_error ());
16616 if (!processing_template_decl
16617 && !TREE_STATIC (r))
16618 r = process_outer_var_ref (r, complain);
16619 }
16620 /* Remember this for subsequent uses. */
16621 if (local_specializations)
16622 register_local_specialization (r, t);
16623 }
16624 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16625 r = argument_pack_select_arg (r);
16626 }
16627 else
16628 r = t;
16629 if (!mark_used (r, complain))
16630 return error_mark_node;
16631 return r;
16632
16633 case NAMESPACE_DECL:
16634 return t;
16635
16636 case OVERLOAD:
16637 return t;
16638
16639 case BASELINK:
16640 return tsubst_baselink (t, current_nonlambda_class_type (),
16641 args, complain, in_decl);
16642
16643 case TEMPLATE_DECL:
16644 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16645 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16646 args, complain, in_decl);
16647 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16648 return tsubst (t, args, complain, in_decl);
16649 else if (DECL_CLASS_SCOPE_P (t)
16650 && uses_template_parms (DECL_CONTEXT (t)))
16651 {
16652 /* Template template argument like the following example need
16653 special treatment:
16654
16655 template <template <class> class TT> struct C {};
16656 template <class T> struct D {
16657 template <class U> struct E {};
16658 C<E> c; // #1
16659 };
16660 D<int> d; // #2
16661
16662 We are processing the template argument `E' in #1 for
16663 the template instantiation #2. Originally, `E' is a
16664 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16665 have to substitute this with one having context `D<int>'. */
16666
16667 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16668 if (dependent_scope_p (context))
16669 {
16670 /* When rewriting a constructor into a deduction guide, a
16671 non-dependent name can become dependent, so memtmpl<args>
16672 becomes context::template memtmpl<args>. */
16673 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16674 return build_qualified_name (type, context, DECL_NAME (t),
16675 /*template*/true);
16676 }
16677 return lookup_field (context, DECL_NAME(t), 0, false);
16678 }
16679 else
16680 /* Ordinary template template argument. */
16681 return t;
16682
16683 case NON_LVALUE_EXPR:
16684 case VIEW_CONVERT_EXPR:
16685 {
16686 /* Handle location wrappers by substituting the wrapped node
16687 first, *then* reusing the resulting type. Doing the type
16688 first ensures that we handle template parameters and
16689 parameter pack expansions. */
16690 if (location_wrapper_p (t))
16691 {
16692 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16693 complain, in_decl);
16694 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16695 }
16696 tree op = TREE_OPERAND (t, 0);
16697 if (code == VIEW_CONVERT_EXPR
16698 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16699 {
16700 /* Wrapper to make a C++20 template parameter object const. */
16701 op = tsubst_copy (op, args, complain, in_decl);
16702 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16703 {
16704 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16705 return build1 (code, type, op);
16706 }
16707 else
16708 {
16709 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16710 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16711 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16712 return op;
16713 }
16714 }
16715 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16716 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16717 {
16718 op = tsubst_copy (op, args, complain, in_decl);
16719 op = build1 (code, TREE_TYPE (op), op);
16720 REF_PARENTHESIZED_P (op) = true;
16721 return op;
16722 }
16723 /* We shouldn't see any other uses of these in templates. */
16724 gcc_unreachable ();
16725 }
16726
16727 case CAST_EXPR:
16728 case REINTERPRET_CAST_EXPR:
16729 case CONST_CAST_EXPR:
16730 case STATIC_CAST_EXPR:
16731 case DYNAMIC_CAST_EXPR:
16732 case IMPLICIT_CONV_EXPR:
16733 case CONVERT_EXPR:
16734 case NOP_EXPR:
16735 {
16736 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16737 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16738 return build1 (code, type, op0);
16739 }
16740
16741 case SIZEOF_EXPR:
16742 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16743 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16744 {
16745 tree expanded, op = TREE_OPERAND (t, 0);
16746 int len = 0;
16747
16748 if (SIZEOF_EXPR_TYPE_P (t))
16749 op = TREE_TYPE (op);
16750
16751 ++cp_unevaluated_operand;
16752 ++c_inhibit_evaluation_warnings;
16753 /* We only want to compute the number of arguments. */
16754 if (PACK_EXPANSION_P (op))
16755 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16756 else
16757 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16758 args, complain, in_decl);
16759 --cp_unevaluated_operand;
16760 --c_inhibit_evaluation_warnings;
16761
16762 if (TREE_CODE (expanded) == TREE_VEC)
16763 {
16764 len = TREE_VEC_LENGTH (expanded);
16765 /* Set TREE_USED for the benefit of -Wunused. */
16766 for (int i = 0; i < len; i++)
16767 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16768 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16769 }
16770
16771 if (expanded == error_mark_node)
16772 return error_mark_node;
16773 else if (PACK_EXPANSION_P (expanded)
16774 || (TREE_CODE (expanded) == TREE_VEC
16775 && pack_expansion_args_count (expanded)))
16776
16777 {
16778 if (PACK_EXPANSION_P (expanded))
16779 /* OK. */;
16780 else if (TREE_VEC_LENGTH (expanded) == 1)
16781 expanded = TREE_VEC_ELT (expanded, 0);
16782 else
16783 expanded = make_argument_pack (expanded);
16784
16785 if (TYPE_P (expanded))
16786 return cxx_sizeof_or_alignof_type (input_location,
16787 expanded, SIZEOF_EXPR,
16788 false,
16789 complain & tf_error);
16790 else
16791 return cxx_sizeof_or_alignof_expr (input_location,
16792 expanded, SIZEOF_EXPR,
16793 false,
16794 complain & tf_error);
16795 }
16796 else
16797 return build_int_cst (size_type_node, len);
16798 }
16799 if (SIZEOF_EXPR_TYPE_P (t))
16800 {
16801 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16802 args, complain, in_decl);
16803 r = build1 (NOP_EXPR, r, error_mark_node);
16804 r = build1 (SIZEOF_EXPR,
16805 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16806 SIZEOF_EXPR_TYPE_P (r) = 1;
16807 return r;
16808 }
16809 /* Fall through */
16810
16811 case INDIRECT_REF:
16812 case NEGATE_EXPR:
16813 case TRUTH_NOT_EXPR:
16814 case BIT_NOT_EXPR:
16815 case ADDR_EXPR:
16816 case UNARY_PLUS_EXPR: /* Unary + */
16817 case ALIGNOF_EXPR:
16818 case AT_ENCODE_EXPR:
16819 case ARROW_EXPR:
16820 case THROW_EXPR:
16821 case TYPEID_EXPR:
16822 case REALPART_EXPR:
16823 case IMAGPART_EXPR:
16824 case PAREN_EXPR:
16825 {
16826 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16827 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16828 r = build1 (code, type, op0);
16829 if (code == ALIGNOF_EXPR)
16830 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16831 return r;
16832 }
16833
16834 case COMPONENT_REF:
16835 {
16836 tree object;
16837 tree name;
16838
16839 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16840 name = TREE_OPERAND (t, 1);
16841 if (TREE_CODE (name) == BIT_NOT_EXPR)
16842 {
16843 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16844 complain, in_decl);
16845 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16846 }
16847 else if (TREE_CODE (name) == SCOPE_REF
16848 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16849 {
16850 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16851 complain, in_decl);
16852 name = TREE_OPERAND (name, 1);
16853 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16854 complain, in_decl);
16855 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16856 name = build_qualified_name (/*type=*/NULL_TREE,
16857 base, name,
16858 /*template_p=*/false);
16859 }
16860 else if (BASELINK_P (name))
16861 name = tsubst_baselink (name,
16862 non_reference (TREE_TYPE (object)),
16863 args, complain,
16864 in_decl);
16865 else
16866 name = tsubst_copy (name, args, complain, in_decl);
16867 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16868 }
16869
16870 case PLUS_EXPR:
16871 case MINUS_EXPR:
16872 case MULT_EXPR:
16873 case TRUNC_DIV_EXPR:
16874 case CEIL_DIV_EXPR:
16875 case FLOOR_DIV_EXPR:
16876 case ROUND_DIV_EXPR:
16877 case EXACT_DIV_EXPR:
16878 case BIT_AND_EXPR:
16879 case BIT_IOR_EXPR:
16880 case BIT_XOR_EXPR:
16881 case TRUNC_MOD_EXPR:
16882 case FLOOR_MOD_EXPR:
16883 case TRUTH_ANDIF_EXPR:
16884 case TRUTH_ORIF_EXPR:
16885 case TRUTH_AND_EXPR:
16886 case TRUTH_OR_EXPR:
16887 case RSHIFT_EXPR:
16888 case LSHIFT_EXPR:
16889 case EQ_EXPR:
16890 case NE_EXPR:
16891 case MAX_EXPR:
16892 case MIN_EXPR:
16893 case LE_EXPR:
16894 case GE_EXPR:
16895 case LT_EXPR:
16896 case GT_EXPR:
16897 case COMPOUND_EXPR:
16898 case DOTSTAR_EXPR:
16899 case MEMBER_REF:
16900 case PREDECREMENT_EXPR:
16901 case PREINCREMENT_EXPR:
16902 case POSTDECREMENT_EXPR:
16903 case POSTINCREMENT_EXPR:
16904 {
16905 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16906 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16907 return build_nt (code, op0, op1);
16908 }
16909
16910 case SCOPE_REF:
16911 {
16912 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16913 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16914 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16915 QUALIFIED_NAME_IS_TEMPLATE (t));
16916 }
16917
16918 case ARRAY_REF:
16919 {
16920 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16921 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16922 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16923 }
16924
16925 case CALL_EXPR:
16926 {
16927 int n = VL_EXP_OPERAND_LENGTH (t);
16928 tree result = build_vl_exp (CALL_EXPR, n);
16929 int i;
16930 for (i = 0; i < n; i++)
16931 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16932 complain, in_decl);
16933 return result;
16934 }
16935
16936 case COND_EXPR:
16937 case MODOP_EXPR:
16938 case PSEUDO_DTOR_EXPR:
16939 case VEC_PERM_EXPR:
16940 {
16941 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16942 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16943 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16944 r = build_nt (code, op0, op1, op2);
16945 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16946 return r;
16947 }
16948
16949 case NEW_EXPR:
16950 {
16951 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16952 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16953 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16954 r = build_nt (code, op0, op1, op2);
16955 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16956 return r;
16957 }
16958
16959 case DELETE_EXPR:
16960 {
16961 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16962 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16963 r = build_nt (code, op0, op1);
16964 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16965 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16966 return r;
16967 }
16968
16969 case TEMPLATE_ID_EXPR:
16970 {
16971 /* Substituted template arguments */
16972 tree fn = TREE_OPERAND (t, 0);
16973 tree targs = TREE_OPERAND (t, 1);
16974
16975 fn = tsubst_copy (fn, args, complain, in_decl);
16976 if (targs)
16977 targs = tsubst_template_args (targs, args, complain, in_decl);
16978
16979 return lookup_template_function (fn, targs);
16980 }
16981
16982 case TREE_LIST:
16983 {
16984 tree purpose, value, chain;
16985
16986 if (t == void_list_node)
16987 return t;
16988
16989 purpose = TREE_PURPOSE (t);
16990 if (purpose)
16991 purpose = tsubst_copy (purpose, args, complain, in_decl);
16992 value = TREE_VALUE (t);
16993 if (value)
16994 value = tsubst_copy (value, args, complain, in_decl);
16995 chain = TREE_CHAIN (t);
16996 if (chain && chain != void_type_node)
16997 chain = tsubst_copy (chain, args, complain, in_decl);
16998 if (purpose == TREE_PURPOSE (t)
16999 && value == TREE_VALUE (t)
17000 && chain == TREE_CHAIN (t))
17001 return t;
17002 return tree_cons (purpose, value, chain);
17003 }
17004
17005 case RECORD_TYPE:
17006 case UNION_TYPE:
17007 case ENUMERAL_TYPE:
17008 case INTEGER_TYPE:
17009 case TEMPLATE_TYPE_PARM:
17010 case TEMPLATE_TEMPLATE_PARM:
17011 case BOUND_TEMPLATE_TEMPLATE_PARM:
17012 case TEMPLATE_PARM_INDEX:
17013 case POINTER_TYPE:
17014 case REFERENCE_TYPE:
17015 case OFFSET_TYPE:
17016 case FUNCTION_TYPE:
17017 case METHOD_TYPE:
17018 case ARRAY_TYPE:
17019 case TYPENAME_TYPE:
17020 case UNBOUND_CLASS_TEMPLATE:
17021 case TYPEOF_TYPE:
17022 case DECLTYPE_TYPE:
17023 case TYPE_DECL:
17024 return tsubst (t, args, complain, in_decl);
17025
17026 case USING_DECL:
17027 t = DECL_NAME (t);
17028 /* Fall through. */
17029 case IDENTIFIER_NODE:
17030 if (IDENTIFIER_CONV_OP_P (t))
17031 {
17032 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17033 return make_conv_op_name (new_type);
17034 }
17035 else
17036 return t;
17037
17038 case CONSTRUCTOR:
17039 /* This is handled by tsubst_copy_and_build. */
17040 gcc_unreachable ();
17041
17042 case VA_ARG_EXPR:
17043 {
17044 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17045 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17046 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17047 }
17048
17049 case CLEANUP_POINT_EXPR:
17050 /* We shouldn't have built any of these during initial template
17051 generation. Instead, they should be built during instantiation
17052 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17053 gcc_unreachable ();
17054
17055 case OFFSET_REF:
17056 {
17057 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17058 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17059 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17060 r = build2 (code, type, op0, op1);
17061 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17062 if (!mark_used (TREE_OPERAND (r, 1), complain)
17063 && !(complain & tf_error))
17064 return error_mark_node;
17065 return r;
17066 }
17067
17068 case EXPR_PACK_EXPANSION:
17069 error ("invalid use of pack expansion expression");
17070 return error_mark_node;
17071
17072 case NONTYPE_ARGUMENT_PACK:
17073 error ("use %<...%> to expand argument pack");
17074 return error_mark_node;
17075
17076 case VOID_CST:
17077 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17078 return t;
17079
17080 case INTEGER_CST:
17081 case REAL_CST:
17082 case COMPLEX_CST:
17083 {
17084 /* Instantiate any typedefs in the type. */
17085 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17086 r = fold_convert (type, t);
17087 gcc_assert (TREE_CODE (r) == code);
17088 return r;
17089 }
17090
17091 case STRING_CST:
17092 {
17093 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17094 r = t;
17095 if (type != TREE_TYPE (t))
17096 {
17097 r = copy_node (t);
17098 TREE_TYPE (r) = type;
17099 }
17100 return r;
17101 }
17102
17103 case PTRMEM_CST:
17104 /* These can sometimes show up in a partial instantiation, but never
17105 involve template parms. */
17106 gcc_assert (!uses_template_parms (t));
17107 return t;
17108
17109 case UNARY_LEFT_FOLD_EXPR:
17110 return tsubst_unary_left_fold (t, args, complain, in_decl);
17111 case UNARY_RIGHT_FOLD_EXPR:
17112 return tsubst_unary_right_fold (t, args, complain, in_decl);
17113 case BINARY_LEFT_FOLD_EXPR:
17114 return tsubst_binary_left_fold (t, args, complain, in_decl);
17115 case BINARY_RIGHT_FOLD_EXPR:
17116 return tsubst_binary_right_fold (t, args, complain, in_decl);
17117 case PREDICT_EXPR:
17118 return t;
17119
17120 case DEBUG_BEGIN_STMT:
17121 /* ??? There's no point in copying it for now, but maybe some
17122 day it will contain more information, such as a pointer back
17123 to the containing function, inlined copy or so. */
17124 return t;
17125
17126 case CO_AWAIT_EXPR:
17127 return tsubst_expr (t, args, complain, in_decl,
17128 /*integral_constant_expression_p=*/false);
17129 break;
17130
17131 default:
17132 /* We shouldn't get here, but keep going if !flag_checking. */
17133 if (flag_checking)
17134 gcc_unreachable ();
17135 return t;
17136 }
17137 }
17138
17139 /* Helper function for tsubst_omp_clauses, used for instantiation of
17140 OMP_CLAUSE_DECL of clauses. */
17141
17142 static tree
17143 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17144 tree in_decl, tree *iterator_cache)
17145 {
17146 if (decl == NULL_TREE)
17147 return NULL_TREE;
17148
17149 /* Handle OpenMP iterators. */
17150 if (TREE_CODE (decl) == TREE_LIST
17151 && TREE_PURPOSE (decl)
17152 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17153 {
17154 tree ret;
17155 if (iterator_cache[0] == TREE_PURPOSE (decl))
17156 ret = iterator_cache[1];
17157 else
17158 {
17159 tree *tp = &ret;
17160 begin_scope (sk_omp, NULL);
17161 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17162 {
17163 *tp = copy_node (it);
17164 TREE_VEC_ELT (*tp, 0)
17165 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17166 TREE_VEC_ELT (*tp, 1)
17167 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17168 /*integral_constant_expression_p=*/false);
17169 TREE_VEC_ELT (*tp, 2)
17170 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17171 /*integral_constant_expression_p=*/false);
17172 TREE_VEC_ELT (*tp, 3)
17173 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17174 /*integral_constant_expression_p=*/false);
17175 TREE_CHAIN (*tp) = NULL_TREE;
17176 tp = &TREE_CHAIN (*tp);
17177 }
17178 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17179 iterator_cache[0] = TREE_PURPOSE (decl);
17180 iterator_cache[1] = ret;
17181 }
17182 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17183 args, complain,
17184 in_decl, NULL));
17185 }
17186
17187 /* Handle an OpenMP array section represented as a TREE_LIST (or
17188 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17189 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17190 TREE_LIST. We can handle it exactly the same as an array section
17191 (purpose, value, and a chain), even though the nomenclature
17192 (low_bound, length, etc) is different. */
17193 if (TREE_CODE (decl) == TREE_LIST)
17194 {
17195 tree low_bound
17196 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17197 /*integral_constant_expression_p=*/false);
17198 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17199 /*integral_constant_expression_p=*/false);
17200 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17201 in_decl, NULL);
17202 if (TREE_PURPOSE (decl) == low_bound
17203 && TREE_VALUE (decl) == length
17204 && TREE_CHAIN (decl) == chain)
17205 return decl;
17206 tree ret = tree_cons (low_bound, length, chain);
17207 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17208 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17209 return ret;
17210 }
17211 tree ret = tsubst_expr (decl, args, complain, in_decl,
17212 /*integral_constant_expression_p=*/false);
17213 /* Undo convert_from_reference tsubst_expr could have called. */
17214 if (decl
17215 && REFERENCE_REF_P (ret)
17216 && !REFERENCE_REF_P (decl))
17217 ret = TREE_OPERAND (ret, 0);
17218 return ret;
17219 }
17220
17221 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17222
17223 static tree
17224 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17225 tree args, tsubst_flags_t complain, tree in_decl)
17226 {
17227 tree new_clauses = NULL_TREE, nc, oc;
17228 tree linear_no_step = NULL_TREE;
17229 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17230
17231 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17232 {
17233 nc = copy_node (oc);
17234 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17235 new_clauses = nc;
17236
17237 switch (OMP_CLAUSE_CODE (nc))
17238 {
17239 case OMP_CLAUSE_LASTPRIVATE:
17240 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17241 {
17242 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17243 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17244 in_decl, /*integral_constant_expression_p=*/false);
17245 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17246 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17247 }
17248 /* FALLTHRU */
17249 case OMP_CLAUSE_PRIVATE:
17250 case OMP_CLAUSE_SHARED:
17251 case OMP_CLAUSE_FIRSTPRIVATE:
17252 case OMP_CLAUSE_COPYIN:
17253 case OMP_CLAUSE_COPYPRIVATE:
17254 case OMP_CLAUSE_UNIFORM:
17255 case OMP_CLAUSE_DEPEND:
17256 case OMP_CLAUSE_FROM:
17257 case OMP_CLAUSE_TO:
17258 case OMP_CLAUSE_MAP:
17259 case OMP_CLAUSE_NONTEMPORAL:
17260 case OMP_CLAUSE_USE_DEVICE_PTR:
17261 case OMP_CLAUSE_USE_DEVICE_ADDR:
17262 case OMP_CLAUSE_IS_DEVICE_PTR:
17263 case OMP_CLAUSE_INCLUSIVE:
17264 case OMP_CLAUSE_EXCLUSIVE:
17265 OMP_CLAUSE_DECL (nc)
17266 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17267 in_decl, iterator_cache);
17268 break;
17269 case OMP_CLAUSE_TILE:
17270 case OMP_CLAUSE_IF:
17271 case OMP_CLAUSE_NUM_THREADS:
17272 case OMP_CLAUSE_SCHEDULE:
17273 case OMP_CLAUSE_COLLAPSE:
17274 case OMP_CLAUSE_FINAL:
17275 case OMP_CLAUSE_DEVICE:
17276 case OMP_CLAUSE_DIST_SCHEDULE:
17277 case OMP_CLAUSE_NUM_TEAMS:
17278 case OMP_CLAUSE_THREAD_LIMIT:
17279 case OMP_CLAUSE_SAFELEN:
17280 case OMP_CLAUSE_SIMDLEN:
17281 case OMP_CLAUSE_NUM_TASKS:
17282 case OMP_CLAUSE_GRAINSIZE:
17283 case OMP_CLAUSE_PRIORITY:
17284 case OMP_CLAUSE_ORDERED:
17285 case OMP_CLAUSE_HINT:
17286 case OMP_CLAUSE_NUM_GANGS:
17287 case OMP_CLAUSE_NUM_WORKERS:
17288 case OMP_CLAUSE_VECTOR_LENGTH:
17289 case OMP_CLAUSE_WORKER:
17290 case OMP_CLAUSE_VECTOR:
17291 case OMP_CLAUSE_ASYNC:
17292 case OMP_CLAUSE_WAIT:
17293 OMP_CLAUSE_OPERAND (nc, 0)
17294 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17295 in_decl, /*integral_constant_expression_p=*/false);
17296 break;
17297 case OMP_CLAUSE_REDUCTION:
17298 case OMP_CLAUSE_IN_REDUCTION:
17299 case OMP_CLAUSE_TASK_REDUCTION:
17300 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17301 {
17302 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17303 if (TREE_CODE (placeholder) == SCOPE_REF)
17304 {
17305 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17306 complain, in_decl);
17307 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17308 = build_qualified_name (NULL_TREE, scope,
17309 TREE_OPERAND (placeholder, 1),
17310 false);
17311 }
17312 else
17313 gcc_assert (identifier_p (placeholder));
17314 }
17315 OMP_CLAUSE_DECL (nc)
17316 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17317 in_decl, NULL);
17318 break;
17319 case OMP_CLAUSE_GANG:
17320 case OMP_CLAUSE_ALIGNED:
17321 case OMP_CLAUSE_ALLOCATE:
17322 OMP_CLAUSE_DECL (nc)
17323 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17324 in_decl, NULL);
17325 OMP_CLAUSE_OPERAND (nc, 1)
17326 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17327 in_decl, /*integral_constant_expression_p=*/false);
17328 break;
17329 case OMP_CLAUSE_LINEAR:
17330 OMP_CLAUSE_DECL (nc)
17331 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17332 in_decl, NULL);
17333 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17334 {
17335 gcc_assert (!linear_no_step);
17336 linear_no_step = nc;
17337 }
17338 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17339 OMP_CLAUSE_LINEAR_STEP (nc)
17340 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17341 complain, in_decl, NULL);
17342 else
17343 OMP_CLAUSE_LINEAR_STEP (nc)
17344 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17345 in_decl,
17346 /*integral_constant_expression_p=*/false);
17347 break;
17348 case OMP_CLAUSE_NOWAIT:
17349 case OMP_CLAUSE_DEFAULT:
17350 case OMP_CLAUSE_UNTIED:
17351 case OMP_CLAUSE_MERGEABLE:
17352 case OMP_CLAUSE_INBRANCH:
17353 case OMP_CLAUSE_NOTINBRANCH:
17354 case OMP_CLAUSE_PROC_BIND:
17355 case OMP_CLAUSE_FOR:
17356 case OMP_CLAUSE_PARALLEL:
17357 case OMP_CLAUSE_SECTIONS:
17358 case OMP_CLAUSE_TASKGROUP:
17359 case OMP_CLAUSE_NOGROUP:
17360 case OMP_CLAUSE_THREADS:
17361 case OMP_CLAUSE_SIMD:
17362 case OMP_CLAUSE_DEFAULTMAP:
17363 case OMP_CLAUSE_ORDER:
17364 case OMP_CLAUSE_BIND:
17365 case OMP_CLAUSE_INDEPENDENT:
17366 case OMP_CLAUSE_AUTO:
17367 case OMP_CLAUSE_SEQ:
17368 case OMP_CLAUSE_IF_PRESENT:
17369 case OMP_CLAUSE_FINALIZE:
17370 break;
17371 default:
17372 gcc_unreachable ();
17373 }
17374 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17375 switch (OMP_CLAUSE_CODE (nc))
17376 {
17377 case OMP_CLAUSE_SHARED:
17378 case OMP_CLAUSE_PRIVATE:
17379 case OMP_CLAUSE_FIRSTPRIVATE:
17380 case OMP_CLAUSE_LASTPRIVATE:
17381 case OMP_CLAUSE_COPYPRIVATE:
17382 case OMP_CLAUSE_LINEAR:
17383 case OMP_CLAUSE_REDUCTION:
17384 case OMP_CLAUSE_IN_REDUCTION:
17385 case OMP_CLAUSE_TASK_REDUCTION:
17386 case OMP_CLAUSE_USE_DEVICE_PTR:
17387 case OMP_CLAUSE_USE_DEVICE_ADDR:
17388 case OMP_CLAUSE_IS_DEVICE_PTR:
17389 case OMP_CLAUSE_INCLUSIVE:
17390 case OMP_CLAUSE_EXCLUSIVE:
17391 case OMP_CLAUSE_ALLOCATE:
17392 /* tsubst_expr on SCOPE_REF results in returning
17393 finish_non_static_data_member result. Undo that here. */
17394 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17395 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17396 == IDENTIFIER_NODE))
17397 {
17398 tree t = OMP_CLAUSE_DECL (nc);
17399 tree v = t;
17400 while (v)
17401 switch (TREE_CODE (v))
17402 {
17403 case COMPONENT_REF:
17404 case MEM_REF:
17405 case INDIRECT_REF:
17406 CASE_CONVERT:
17407 case POINTER_PLUS_EXPR:
17408 v = TREE_OPERAND (v, 0);
17409 continue;
17410 case PARM_DECL:
17411 if (DECL_CONTEXT (v) == current_function_decl
17412 && DECL_ARTIFICIAL (v)
17413 && DECL_NAME (v) == this_identifier)
17414 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17415 /* FALLTHRU */
17416 default:
17417 v = NULL_TREE;
17418 break;
17419 }
17420 }
17421 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17422 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17423 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17424 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17425 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17426 {
17427 tree decl = OMP_CLAUSE_DECL (nc);
17428 if (VAR_P (decl))
17429 {
17430 retrofit_lang_decl (decl);
17431 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17432 }
17433 }
17434 break;
17435 default:
17436 break;
17437 }
17438 }
17439
17440 new_clauses = nreverse (new_clauses);
17441 if (ort != C_ORT_OMP_DECLARE_SIMD)
17442 {
17443 new_clauses = finish_omp_clauses (new_clauses, ort);
17444 if (linear_no_step)
17445 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17446 if (nc == linear_no_step)
17447 {
17448 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17449 break;
17450 }
17451 }
17452 return new_clauses;
17453 }
17454
17455 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17456
17457 static tree
17458 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17459 tree in_decl)
17460 {
17461 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17462
17463 tree purpose, value, chain;
17464
17465 if (t == NULL)
17466 return t;
17467
17468 if (TREE_CODE (t) != TREE_LIST)
17469 return tsubst_copy_and_build (t, args, complain, in_decl,
17470 /*function_p=*/false,
17471 /*integral_constant_expression_p=*/false);
17472
17473 if (t == void_list_node)
17474 return t;
17475
17476 purpose = TREE_PURPOSE (t);
17477 if (purpose)
17478 purpose = RECUR (purpose);
17479 value = TREE_VALUE (t);
17480 if (value)
17481 {
17482 if (TREE_CODE (value) != LABEL_DECL)
17483 value = RECUR (value);
17484 else
17485 {
17486 value = lookup_label (DECL_NAME (value));
17487 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17488 TREE_USED (value) = 1;
17489 }
17490 }
17491 chain = TREE_CHAIN (t);
17492 if (chain && chain != void_type_node)
17493 chain = RECUR (chain);
17494 return tree_cons (purpose, value, chain);
17495 #undef RECUR
17496 }
17497
17498 /* Used to temporarily communicate the list of #pragma omp parallel
17499 clauses to #pragma omp for instantiation if they are combined
17500 together. */
17501
17502 static tree *omp_parallel_combined_clauses;
17503
17504 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17505 tree *, unsigned int *);
17506
17507 /* Substitute one OMP_FOR iterator. */
17508
17509 static bool
17510 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17511 tree initv, tree condv, tree incrv, tree *clauses,
17512 tree args, tsubst_flags_t complain, tree in_decl,
17513 bool integral_constant_expression_p)
17514 {
17515 #define RECUR(NODE) \
17516 tsubst_expr ((NODE), args, complain, in_decl, \
17517 integral_constant_expression_p)
17518 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17519 bool ret = false;
17520
17521 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17522 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17523
17524 decl = TREE_OPERAND (init, 0);
17525 init = TREE_OPERAND (init, 1);
17526 tree decl_expr = NULL_TREE;
17527 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17528 if (range_for)
17529 {
17530 bool decomp = false;
17531 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17532 {
17533 tree v = DECL_VALUE_EXPR (decl);
17534 if (TREE_CODE (v) == ARRAY_REF
17535 && VAR_P (TREE_OPERAND (v, 0))
17536 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17537 {
17538 tree decomp_first = NULL_TREE;
17539 unsigned decomp_cnt = 0;
17540 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17541 maybe_push_decl (d);
17542 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17543 in_decl, &decomp_first, &decomp_cnt);
17544 decomp = true;
17545 if (d == error_mark_node)
17546 decl = error_mark_node;
17547 else
17548 for (unsigned int i = 0; i < decomp_cnt; i++)
17549 {
17550 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17551 {
17552 tree v = build_nt (ARRAY_REF, d,
17553 size_int (decomp_cnt - i - 1),
17554 NULL_TREE, NULL_TREE);
17555 SET_DECL_VALUE_EXPR (decomp_first, v);
17556 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17557 }
17558 fit_decomposition_lang_decl (decomp_first, d);
17559 decomp_first = DECL_CHAIN (decomp_first);
17560 }
17561 }
17562 }
17563 decl = tsubst_decl (decl, args, complain);
17564 if (!decomp)
17565 maybe_push_decl (decl);
17566 }
17567 else if (init && TREE_CODE (init) == DECL_EXPR)
17568 {
17569 /* We need to jump through some hoops to handle declarations in the
17570 init-statement, since we might need to handle auto deduction,
17571 but we need to keep control of initialization. */
17572 decl_expr = init;
17573 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17574 decl = tsubst_decl (decl, args, complain);
17575 }
17576 else
17577 {
17578 if (TREE_CODE (decl) == SCOPE_REF)
17579 {
17580 decl = RECUR (decl);
17581 if (TREE_CODE (decl) == COMPONENT_REF)
17582 {
17583 tree v = decl;
17584 while (v)
17585 switch (TREE_CODE (v))
17586 {
17587 case COMPONENT_REF:
17588 case MEM_REF:
17589 case INDIRECT_REF:
17590 CASE_CONVERT:
17591 case POINTER_PLUS_EXPR:
17592 v = TREE_OPERAND (v, 0);
17593 continue;
17594 case PARM_DECL:
17595 if (DECL_CONTEXT (v) == current_function_decl
17596 && DECL_ARTIFICIAL (v)
17597 && DECL_NAME (v) == this_identifier)
17598 {
17599 decl = TREE_OPERAND (decl, 1);
17600 decl = omp_privatize_field (decl, false);
17601 }
17602 /* FALLTHRU */
17603 default:
17604 v = NULL_TREE;
17605 break;
17606 }
17607 }
17608 }
17609 else
17610 decl = RECUR (decl);
17611 }
17612 if (init && TREE_CODE (init) == TREE_VEC)
17613 {
17614 init = copy_node (init);
17615 TREE_VEC_ELT (init, 0)
17616 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17617 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17618 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17619 }
17620 else
17621 init = RECUR (init);
17622
17623 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17624 {
17625 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17626 if (TREE_CODE (o) == TREE_LIST)
17627 TREE_VEC_ELT (orig_declv, i)
17628 = tree_cons (RECUR (TREE_PURPOSE (o)),
17629 RECUR (TREE_VALUE (o)),
17630 NULL_TREE);
17631 else
17632 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17633 }
17634
17635 if (range_for)
17636 {
17637 tree this_pre_body = NULL_TREE;
17638 tree orig_init = NULL_TREE;
17639 tree orig_decl = NULL_TREE;
17640 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17641 orig_init, cond, incr);
17642 if (orig_decl)
17643 {
17644 if (orig_declv == NULL_TREE)
17645 orig_declv = copy_node (declv);
17646 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17647 ret = true;
17648 }
17649 else if (orig_declv)
17650 TREE_VEC_ELT (orig_declv, i) = decl;
17651 }
17652
17653 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17654 if (!range_for && auto_node && init)
17655 TREE_TYPE (decl)
17656 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17657
17658 gcc_assert (!type_dependent_expression_p (decl));
17659
17660 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17661 {
17662 if (decl_expr)
17663 {
17664 /* Declare the variable, but don't let that initialize it. */
17665 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17666 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17667 RECUR (decl_expr);
17668 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17669 }
17670
17671 if (!range_for)
17672 {
17673 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17674 if (COMPARISON_CLASS_P (cond)
17675 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17676 {
17677 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17678 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17679 TREE_VEC_ELT (rhs, 0)
17680 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17681 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17682 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17683 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17684 lhs, rhs);
17685 }
17686 else
17687 cond = RECUR (cond);
17688 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17689 if (TREE_CODE (incr) == MODIFY_EXPR)
17690 {
17691 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17692 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17693 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17694 NOP_EXPR, rhs, complain);
17695 }
17696 else
17697 incr = RECUR (incr);
17698 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17699 TREE_VEC_ELT (orig_declv, i) = decl;
17700 }
17701 TREE_VEC_ELT (declv, i) = decl;
17702 TREE_VEC_ELT (initv, i) = init;
17703 TREE_VEC_ELT (condv, i) = cond;
17704 TREE_VEC_ELT (incrv, i) = incr;
17705 return ret;
17706 }
17707
17708 if (decl_expr)
17709 {
17710 /* Declare and initialize the variable. */
17711 RECUR (decl_expr);
17712 init = NULL_TREE;
17713 }
17714 else if (init)
17715 {
17716 tree *pc;
17717 int j;
17718 for (j = ((omp_parallel_combined_clauses == NULL
17719 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17720 {
17721 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17722 {
17723 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17724 && OMP_CLAUSE_DECL (*pc) == decl)
17725 break;
17726 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17727 && OMP_CLAUSE_DECL (*pc) == decl)
17728 {
17729 if (j)
17730 break;
17731 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17732 tree c = *pc;
17733 *pc = OMP_CLAUSE_CHAIN (c);
17734 OMP_CLAUSE_CHAIN (c) = *clauses;
17735 *clauses = c;
17736 }
17737 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17738 && OMP_CLAUSE_DECL (*pc) == decl)
17739 {
17740 error ("iteration variable %qD should not be firstprivate",
17741 decl);
17742 *pc = OMP_CLAUSE_CHAIN (*pc);
17743 }
17744 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17745 && OMP_CLAUSE_DECL (*pc) == decl)
17746 {
17747 error ("iteration variable %qD should not be reduction",
17748 decl);
17749 *pc = OMP_CLAUSE_CHAIN (*pc);
17750 }
17751 else
17752 pc = &OMP_CLAUSE_CHAIN (*pc);
17753 }
17754 if (*pc)
17755 break;
17756 }
17757 if (*pc == NULL_TREE)
17758 {
17759 tree c = build_omp_clause (input_location,
17760 TREE_CODE (t) == OMP_LOOP
17761 ? OMP_CLAUSE_LASTPRIVATE
17762 : OMP_CLAUSE_PRIVATE);
17763 OMP_CLAUSE_DECL (c) = decl;
17764 c = finish_omp_clauses (c, C_ORT_OMP);
17765 if (c)
17766 {
17767 OMP_CLAUSE_CHAIN (c) = *clauses;
17768 *clauses = c;
17769 }
17770 }
17771 }
17772 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17773 if (COMPARISON_CLASS_P (cond))
17774 {
17775 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17776 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17777 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17778 }
17779 else
17780 cond = RECUR (cond);
17781 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17782 switch (TREE_CODE (incr))
17783 {
17784 case PREINCREMENT_EXPR:
17785 case PREDECREMENT_EXPR:
17786 case POSTINCREMENT_EXPR:
17787 case POSTDECREMENT_EXPR:
17788 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17789 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17790 break;
17791 case MODIFY_EXPR:
17792 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17793 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17794 {
17795 tree rhs = TREE_OPERAND (incr, 1);
17796 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17797 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17798 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17799 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17800 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17801 rhs0, rhs1));
17802 }
17803 else
17804 incr = RECUR (incr);
17805 break;
17806 case MODOP_EXPR:
17807 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17808 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17809 {
17810 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17811 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17812 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17813 TREE_TYPE (decl), lhs,
17814 RECUR (TREE_OPERAND (incr, 2))));
17815 }
17816 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17817 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17818 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17819 {
17820 tree rhs = TREE_OPERAND (incr, 2);
17821 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17822 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17823 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17824 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17825 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17826 rhs0, rhs1));
17827 }
17828 else
17829 incr = RECUR (incr);
17830 break;
17831 default:
17832 incr = RECUR (incr);
17833 break;
17834 }
17835
17836 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17837 TREE_VEC_ELT (orig_declv, i) = decl;
17838 TREE_VEC_ELT (declv, i) = decl;
17839 TREE_VEC_ELT (initv, i) = init;
17840 TREE_VEC_ELT (condv, i) = cond;
17841 TREE_VEC_ELT (incrv, i) = incr;
17842 return false;
17843 #undef RECUR
17844 }
17845
17846 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17847 of OMP_TARGET's body. */
17848
17849 static tree
17850 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17851 {
17852 *walk_subtrees = 0;
17853 switch (TREE_CODE (*tp))
17854 {
17855 case OMP_TEAMS:
17856 return *tp;
17857 case BIND_EXPR:
17858 case STATEMENT_LIST:
17859 *walk_subtrees = 1;
17860 break;
17861 default:
17862 break;
17863 }
17864 return NULL_TREE;
17865 }
17866
17867 /* Helper function for tsubst_expr. For decomposition declaration
17868 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17869 also the corresponding decls representing the identifiers
17870 of the decomposition declaration. Return DECL if successful
17871 or error_mark_node otherwise, set *FIRST to the first decl
17872 in the list chained through DECL_CHAIN and *CNT to the number
17873 of such decls. */
17874
17875 static tree
17876 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17877 tsubst_flags_t complain, tree in_decl, tree *first,
17878 unsigned int *cnt)
17879 {
17880 tree decl2, decl3, prev = decl;
17881 *cnt = 0;
17882 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17883 for (decl2 = DECL_CHAIN (pattern_decl);
17884 decl2
17885 && VAR_P (decl2)
17886 && DECL_DECOMPOSITION_P (decl2)
17887 && DECL_NAME (decl2);
17888 decl2 = DECL_CHAIN (decl2))
17889 {
17890 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17891 {
17892 gcc_assert (errorcount);
17893 return error_mark_node;
17894 }
17895 (*cnt)++;
17896 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17897 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17898 tree v = DECL_VALUE_EXPR (decl2);
17899 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17900 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17901 decl3 = tsubst (decl2, args, complain, in_decl);
17902 SET_DECL_VALUE_EXPR (decl2, v);
17903 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17904 if (VAR_P (decl3))
17905 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17906 else
17907 {
17908 gcc_assert (errorcount);
17909 decl = error_mark_node;
17910 continue;
17911 }
17912 maybe_push_decl (decl3);
17913 if (error_operand_p (decl3))
17914 decl = error_mark_node;
17915 else if (decl != error_mark_node
17916 && DECL_CHAIN (decl3) != prev
17917 && decl != prev)
17918 {
17919 gcc_assert (errorcount);
17920 decl = error_mark_node;
17921 }
17922 else
17923 prev = decl3;
17924 }
17925 *first = prev;
17926 return decl;
17927 }
17928
17929 /* Return the proper local_specialization for init-capture pack DECL. */
17930
17931 static tree
17932 lookup_init_capture_pack (tree decl)
17933 {
17934 /* We handle normal pack captures by forwarding to the specialization of the
17935 captured parameter. We can't do that for pack init-captures; we need them
17936 to have their own local_specialization. We created the individual
17937 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17938 when we process the DECL_EXPR for the pack init-capture in the template.
17939 So, how do we find them? We don't know the capture proxy pack when
17940 building the individual resulting proxies, and we don't know the
17941 individual proxies when instantiating the pack. What we have in common is
17942 the FIELD_DECL.
17943
17944 So...when we instantiate the FIELD_DECL, we stick the result in
17945 local_specializations. Then at the DECL_EXPR we look up that result, see
17946 how many elements it has, synthesize the names, and look them up. */
17947
17948 tree cname = DECL_NAME (decl);
17949 tree val = DECL_VALUE_EXPR (decl);
17950 tree field = TREE_OPERAND (val, 1);
17951 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17952 tree fpack = retrieve_local_specialization (field);
17953 if (fpack == error_mark_node)
17954 return error_mark_node;
17955
17956 int len = 1;
17957 tree vec = NULL_TREE;
17958 tree r = NULL_TREE;
17959 if (TREE_CODE (fpack) == TREE_VEC)
17960 {
17961 len = TREE_VEC_LENGTH (fpack);
17962 vec = make_tree_vec (len);
17963 r = make_node (NONTYPE_ARGUMENT_PACK);
17964 SET_ARGUMENT_PACK_ARGS (r, vec);
17965 }
17966 for (int i = 0; i < len; ++i)
17967 {
17968 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17969 tree elt = lookup_name (ename);
17970 if (vec)
17971 TREE_VEC_ELT (vec, i) = elt;
17972 else
17973 r = elt;
17974 }
17975 return r;
17976 }
17977
17978 /* Like tsubst_copy for expressions, etc. but also does semantic
17979 processing. */
17980
17981 tree
17982 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17983 bool integral_constant_expression_p)
17984 {
17985 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17986 #define RECUR(NODE) \
17987 tsubst_expr ((NODE), args, complain, in_decl, \
17988 integral_constant_expression_p)
17989
17990 tree stmt, tmp;
17991 tree r;
17992 location_t loc;
17993
17994 if (t == NULL_TREE || t == error_mark_node)
17995 return t;
17996
17997 loc = input_location;
17998 if (location_t eloc = cp_expr_location (t))
17999 input_location = eloc;
18000 if (STATEMENT_CODE_P (TREE_CODE (t)))
18001 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18002
18003 switch (TREE_CODE (t))
18004 {
18005 case STATEMENT_LIST:
18006 {
18007 tree_stmt_iterator i;
18008 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
18009 RECUR (tsi_stmt (i));
18010 break;
18011 }
18012
18013 case CTOR_INITIALIZER:
18014 finish_mem_initializers (tsubst_initializer_list
18015 (TREE_OPERAND (t, 0), args));
18016 break;
18017
18018 case RETURN_EXPR:
18019 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18020 break;
18021
18022 case CO_RETURN_EXPR:
18023 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18024 break;
18025
18026 case CO_YIELD_EXPR:
18027 stmt = finish_co_yield_expr (input_location,
18028 RECUR (TREE_OPERAND (t, 0)));
18029 RETURN (stmt);
18030 break;
18031
18032 case CO_AWAIT_EXPR:
18033 stmt = finish_co_await_expr (input_location,
18034 RECUR (TREE_OPERAND (t, 0)));
18035 RETURN (stmt);
18036 break;
18037
18038 case EXPR_STMT:
18039 tmp = RECUR (EXPR_STMT_EXPR (t));
18040 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18041 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18042 else
18043 finish_expr_stmt (tmp);
18044 break;
18045
18046 case USING_STMT:
18047 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18048 break;
18049
18050 case DECL_EXPR:
18051 {
18052 tree decl, pattern_decl;
18053 tree init;
18054
18055 pattern_decl = decl = DECL_EXPR_DECL (t);
18056 if (TREE_CODE (decl) == LABEL_DECL)
18057 finish_label_decl (DECL_NAME (decl));
18058 else if (TREE_CODE (decl) == USING_DECL)
18059 {
18060 /* We cannot have a member-using decl here (until 'using
18061 enum T' is a thing). */
18062 gcc_checking_assert (!DECL_DEPENDENT_P (decl));
18063
18064 /* This must be a non-dependent using-decl, and we'll have
18065 used the names it found during template parsing. We do
18066 not want to do the lookup again, because we might not
18067 find the things we found then. (Again, using enum T
18068 might mean we have to do things here.) */
18069 tree scope = USING_DECL_SCOPE (decl);
18070 gcc_checking_assert (scope
18071 == tsubst (scope, args, complain, in_decl));
18072 }
18073 else if (is_capture_proxy (decl)
18074 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18075 {
18076 /* We're in tsubst_lambda_expr, we've already inserted a new
18077 capture proxy, so look it up and register it. */
18078 tree inst;
18079 if (!DECL_PACK_P (decl))
18080 {
18081 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18082 LOOK_want::HIDDEN_LAMBDA);
18083 gcc_assert (inst != decl && is_capture_proxy (inst));
18084 }
18085 else if (is_normal_capture_proxy (decl))
18086 {
18087 inst = (retrieve_local_specialization
18088 (DECL_CAPTURED_VARIABLE (decl)));
18089 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18090 || DECL_PACK_P (inst));
18091 }
18092 else
18093 inst = lookup_init_capture_pack (decl);
18094
18095 register_local_specialization (inst, decl);
18096 break;
18097 }
18098 else if (DECL_PRETTY_FUNCTION_P (decl))
18099 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18100 DECL_NAME (decl),
18101 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18102 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18103 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18104 /* Don't copy the old closure; we'll create a new one in
18105 tsubst_lambda_expr. */
18106 break;
18107 else
18108 {
18109 init = DECL_INITIAL (decl);
18110 decl = tsubst (decl, args, complain, in_decl);
18111 if (decl != error_mark_node)
18112 {
18113 /* By marking the declaration as instantiated, we avoid
18114 trying to instantiate it. Since instantiate_decl can't
18115 handle local variables, and since we've already done
18116 all that needs to be done, that's the right thing to
18117 do. */
18118 if (VAR_P (decl))
18119 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18120 if (VAR_P (decl) && !DECL_NAME (decl)
18121 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18122 /* Anonymous aggregates are a special case. */
18123 finish_anon_union (decl);
18124 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18125 {
18126 DECL_CONTEXT (decl) = current_function_decl;
18127 if (DECL_NAME (decl) == this_identifier)
18128 {
18129 tree lam = DECL_CONTEXT (current_function_decl);
18130 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18131 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18132 }
18133 insert_capture_proxy (decl);
18134 }
18135 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18136 /* We already did a pushtag. */;
18137 else if (VAR_OR_FUNCTION_DECL_P (decl)
18138 && DECL_LOCAL_DECL_P (decl))
18139 {
18140 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
18141 DECL_CONTEXT (decl) = NULL_TREE;
18142 decl = pushdecl (decl);
18143 if (TREE_CODE (decl) == FUNCTION_DECL
18144 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18145 && cp_check_omp_declare_reduction (decl))
18146 instantiate_body (pattern_decl, args, decl, true);
18147 }
18148 else
18149 {
18150 bool const_init = false;
18151 unsigned int cnt = 0;
18152 tree first = NULL_TREE, ndecl = error_mark_node;
18153 maybe_push_decl (decl);
18154
18155 if (VAR_P (decl)
18156 && DECL_DECOMPOSITION_P (decl)
18157 && TREE_TYPE (pattern_decl) != error_mark_node)
18158 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18159 complain, in_decl, &first,
18160 &cnt);
18161
18162 init = tsubst_init (init, decl, args, complain, in_decl);
18163
18164 if (VAR_P (decl))
18165 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18166 (pattern_decl));
18167
18168 if (ndecl != error_mark_node)
18169 cp_maybe_mangle_decomp (ndecl, first, cnt);
18170
18171 /* In a non-template function, VLA type declarations are
18172 handled in grokdeclarator; for templates, handle them
18173 now. */
18174 predeclare_vla (decl);
18175
18176 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
18177
18178 if (ndecl != error_mark_node)
18179 cp_finish_decomp (ndecl, first, cnt);
18180 }
18181 }
18182 }
18183
18184 break;
18185 }
18186
18187 case FOR_STMT:
18188 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18189 RECUR (FOR_INIT_STMT (t));
18190 finish_init_stmt (stmt);
18191 tmp = RECUR (FOR_COND (t));
18192 finish_for_cond (tmp, stmt, false, 0);
18193 tmp = RECUR (FOR_EXPR (t));
18194 finish_for_expr (tmp, stmt);
18195 {
18196 bool prev = note_iteration_stmt_body_start ();
18197 RECUR (FOR_BODY (t));
18198 note_iteration_stmt_body_end (prev);
18199 }
18200 finish_for_stmt (stmt);
18201 break;
18202
18203 case RANGE_FOR_STMT:
18204 {
18205 /* Construct another range_for, if this is not a final
18206 substitution (for inside a generic lambda of a
18207 template). Otherwise convert to a regular for. */
18208 tree decl, expr;
18209 stmt = (processing_template_decl
18210 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18211 : begin_for_stmt (NULL_TREE, NULL_TREE));
18212 RECUR (RANGE_FOR_INIT_STMT (t));
18213 decl = RANGE_FOR_DECL (t);
18214 decl = tsubst (decl, args, complain, in_decl);
18215 maybe_push_decl (decl);
18216 expr = RECUR (RANGE_FOR_EXPR (t));
18217
18218 tree decomp_first = NULL_TREE;
18219 unsigned decomp_cnt = 0;
18220 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18221 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18222 complain, in_decl,
18223 &decomp_first, &decomp_cnt);
18224
18225 if (processing_template_decl)
18226 {
18227 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18228 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18229 finish_range_for_decl (stmt, decl, expr);
18230 if (decomp_first && decl != error_mark_node)
18231 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18232 }
18233 else
18234 {
18235 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18236 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18237 stmt = cp_convert_range_for (stmt, decl, expr,
18238 decomp_first, decomp_cnt,
18239 RANGE_FOR_IVDEP (t), unroll);
18240 }
18241
18242 bool prev = note_iteration_stmt_body_start ();
18243 RECUR (RANGE_FOR_BODY (t));
18244 note_iteration_stmt_body_end (prev);
18245 finish_for_stmt (stmt);
18246 }
18247 break;
18248
18249 case WHILE_STMT:
18250 stmt = begin_while_stmt ();
18251 tmp = RECUR (WHILE_COND (t));
18252 finish_while_stmt_cond (tmp, stmt, false, 0);
18253 {
18254 bool prev = note_iteration_stmt_body_start ();
18255 RECUR (WHILE_BODY (t));
18256 note_iteration_stmt_body_end (prev);
18257 }
18258 finish_while_stmt (stmt);
18259 break;
18260
18261 case DO_STMT:
18262 stmt = begin_do_stmt ();
18263 {
18264 bool prev = note_iteration_stmt_body_start ();
18265 RECUR (DO_BODY (t));
18266 note_iteration_stmt_body_end (prev);
18267 }
18268 finish_do_body (stmt);
18269 tmp = RECUR (DO_COND (t));
18270 finish_do_stmt (tmp, stmt, false, 0);
18271 break;
18272
18273 case IF_STMT:
18274 stmt = begin_if_stmt ();
18275 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18276 if (IF_STMT_CONSTEXPR_P (t))
18277 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18278 tmp = RECUR (IF_COND (t));
18279 tmp = finish_if_stmt_cond (tmp, stmt);
18280 if (IF_STMT_CONSTEXPR_P (t)
18281 && instantiation_dependent_expression_p (tmp))
18282 {
18283 /* We're partially instantiating a generic lambda, but the condition
18284 of the constexpr if is still dependent. Don't substitute into the
18285 branches now, just remember the template arguments. */
18286 do_poplevel (IF_SCOPE (stmt));
18287 IF_COND (stmt) = IF_COND (t);
18288 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18289 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18290 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18291 add_stmt (stmt);
18292 break;
18293 }
18294 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18295 /* Don't instantiate the THEN_CLAUSE. */;
18296 else
18297 {
18298 tree folded = fold_non_dependent_expr (tmp, complain);
18299 bool inhibit = integer_zerop (folded);
18300 if (inhibit)
18301 ++c_inhibit_evaluation_warnings;
18302 RECUR (THEN_CLAUSE (t));
18303 if (inhibit)
18304 --c_inhibit_evaluation_warnings;
18305 }
18306 finish_then_clause (stmt);
18307
18308 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18309 /* Don't instantiate the ELSE_CLAUSE. */;
18310 else if (ELSE_CLAUSE (t))
18311 {
18312 tree folded = fold_non_dependent_expr (tmp, complain);
18313 bool inhibit = integer_nonzerop (folded);
18314 begin_else_clause (stmt);
18315 if (inhibit)
18316 ++c_inhibit_evaluation_warnings;
18317 RECUR (ELSE_CLAUSE (t));
18318 if (inhibit)
18319 --c_inhibit_evaluation_warnings;
18320 finish_else_clause (stmt);
18321 }
18322
18323 finish_if_stmt (stmt);
18324 break;
18325
18326 case BIND_EXPR:
18327 if (BIND_EXPR_BODY_BLOCK (t))
18328 stmt = begin_function_body ();
18329 else
18330 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18331 ? BCS_TRY_BLOCK : 0);
18332
18333 RECUR (BIND_EXPR_BODY (t));
18334
18335 if (BIND_EXPR_BODY_BLOCK (t))
18336 finish_function_body (stmt);
18337 else
18338 finish_compound_stmt (stmt);
18339 break;
18340
18341 case BREAK_STMT:
18342 finish_break_stmt ();
18343 break;
18344
18345 case CONTINUE_STMT:
18346 finish_continue_stmt ();
18347 break;
18348
18349 case SWITCH_STMT:
18350 stmt = begin_switch_stmt ();
18351 tmp = RECUR (SWITCH_STMT_COND (t));
18352 finish_switch_cond (tmp, stmt);
18353 RECUR (SWITCH_STMT_BODY (t));
18354 finish_switch_stmt (stmt);
18355 break;
18356
18357 case CASE_LABEL_EXPR:
18358 {
18359 tree decl = CASE_LABEL (t);
18360 tree low = RECUR (CASE_LOW (t));
18361 tree high = RECUR (CASE_HIGH (t));
18362 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18363 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18364 {
18365 tree label = CASE_LABEL (l);
18366 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18367 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18368 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18369 }
18370 }
18371 break;
18372
18373 case LABEL_EXPR:
18374 {
18375 tree decl = LABEL_EXPR_LABEL (t);
18376 tree label;
18377
18378 label = finish_label_stmt (DECL_NAME (decl));
18379 if (TREE_CODE (label) == LABEL_DECL)
18380 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18381 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18382 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18383 }
18384 break;
18385
18386 case GOTO_EXPR:
18387 tmp = GOTO_DESTINATION (t);
18388 if (TREE_CODE (tmp) != LABEL_DECL)
18389 /* Computed goto's must be tsubst'd into. On the other hand,
18390 non-computed gotos must not be; the identifier in question
18391 will have no binding. */
18392 tmp = RECUR (tmp);
18393 else
18394 tmp = DECL_NAME (tmp);
18395 finish_goto_stmt (tmp);
18396 break;
18397
18398 case ASM_EXPR:
18399 {
18400 tree string = RECUR (ASM_STRING (t));
18401 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18402 complain, in_decl);
18403 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18404 complain, in_decl);
18405 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18406 complain, in_decl);
18407 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18408 complain, in_decl);
18409 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18410 outputs, inputs, clobbers, labels,
18411 ASM_INLINE_P (t));
18412 tree asm_expr = tmp;
18413 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18414 asm_expr = TREE_OPERAND (asm_expr, 0);
18415 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18416 }
18417 break;
18418
18419 case TRY_BLOCK:
18420 if (CLEANUP_P (t))
18421 {
18422 stmt = begin_try_block ();
18423 RECUR (TRY_STMTS (t));
18424 finish_cleanup_try_block (stmt);
18425 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18426 }
18427 else
18428 {
18429 tree compound_stmt = NULL_TREE;
18430
18431 if (FN_TRY_BLOCK_P (t))
18432 stmt = begin_function_try_block (&compound_stmt);
18433 else
18434 stmt = begin_try_block ();
18435
18436 RECUR (TRY_STMTS (t));
18437
18438 if (FN_TRY_BLOCK_P (t))
18439 finish_function_try_block (stmt);
18440 else
18441 finish_try_block (stmt);
18442
18443 RECUR (TRY_HANDLERS (t));
18444 if (FN_TRY_BLOCK_P (t))
18445 finish_function_handler_sequence (stmt, compound_stmt);
18446 else
18447 finish_handler_sequence (stmt);
18448 }
18449 break;
18450
18451 case HANDLER:
18452 {
18453 tree decl = HANDLER_PARMS (t);
18454
18455 if (decl)
18456 {
18457 decl = tsubst (decl, args, complain, in_decl);
18458 /* Prevent instantiate_decl from trying to instantiate
18459 this variable. We've already done all that needs to be
18460 done. */
18461 if (decl != error_mark_node)
18462 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18463 }
18464 stmt = begin_handler ();
18465 finish_handler_parms (decl, stmt);
18466 RECUR (HANDLER_BODY (t));
18467 finish_handler (stmt);
18468 }
18469 break;
18470
18471 case TAG_DEFN:
18472 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18473 if (CLASS_TYPE_P (tmp))
18474 {
18475 /* Local classes are not independent templates; they are
18476 instantiated along with their containing function. And this
18477 way we don't have to deal with pushing out of one local class
18478 to instantiate a member of another local class. */
18479 /* Closures are handled by the LAMBDA_EXPR. */
18480 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18481 complete_type (tmp);
18482 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18483 if ((VAR_P (fld)
18484 || (TREE_CODE (fld) == FUNCTION_DECL
18485 && !DECL_ARTIFICIAL (fld)))
18486 && DECL_TEMPLATE_INSTANTIATION (fld))
18487 instantiate_decl (fld, /*defer_ok=*/false,
18488 /*expl_inst_class=*/false);
18489 }
18490 break;
18491
18492 case STATIC_ASSERT:
18493 {
18494 tree condition;
18495
18496 ++c_inhibit_evaluation_warnings;
18497 condition =
18498 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18499 args,
18500 complain, in_decl,
18501 /*integral_constant_expression_p=*/true);
18502 --c_inhibit_evaluation_warnings;
18503
18504 finish_static_assert (condition,
18505 STATIC_ASSERT_MESSAGE (t),
18506 STATIC_ASSERT_SOURCE_LOCATION (t),
18507 /*member_p=*/false, /*show_expr_p=*/true);
18508 }
18509 break;
18510
18511 case OACC_KERNELS:
18512 case OACC_PARALLEL:
18513 case OACC_SERIAL:
18514 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18515 in_decl);
18516 stmt = begin_omp_parallel ();
18517 RECUR (OMP_BODY (t));
18518 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18519 break;
18520
18521 case OMP_PARALLEL:
18522 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18523 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18524 complain, in_decl);
18525 if (OMP_PARALLEL_COMBINED (t))
18526 omp_parallel_combined_clauses = &tmp;
18527 stmt = begin_omp_parallel ();
18528 RECUR (OMP_PARALLEL_BODY (t));
18529 gcc_assert (omp_parallel_combined_clauses == NULL);
18530 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18531 = OMP_PARALLEL_COMBINED (t);
18532 pop_omp_privatization_clauses (r);
18533 break;
18534
18535 case OMP_TASK:
18536 if (OMP_TASK_BODY (t) == NULL_TREE)
18537 {
18538 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18539 complain, in_decl);
18540 t = copy_node (t);
18541 OMP_TASK_CLAUSES (t) = tmp;
18542 add_stmt (t);
18543 break;
18544 }
18545 r = push_omp_privatization_clauses (false);
18546 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18547 complain, in_decl);
18548 stmt = begin_omp_task ();
18549 RECUR (OMP_TASK_BODY (t));
18550 finish_omp_task (tmp, stmt);
18551 pop_omp_privatization_clauses (r);
18552 break;
18553
18554 case OMP_FOR:
18555 case OMP_LOOP:
18556 case OMP_SIMD:
18557 case OMP_DISTRIBUTE:
18558 case OMP_TASKLOOP:
18559 case OACC_LOOP:
18560 {
18561 tree clauses, body, pre_body;
18562 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18563 tree orig_declv = NULL_TREE;
18564 tree incrv = NULL_TREE;
18565 enum c_omp_region_type ort = C_ORT_OMP;
18566 bool any_range_for = false;
18567 int i;
18568
18569 if (TREE_CODE (t) == OACC_LOOP)
18570 ort = C_ORT_ACC;
18571
18572 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18573 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18574 in_decl);
18575 if (OMP_FOR_INIT (t) != NULL_TREE)
18576 {
18577 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18578 if (OMP_FOR_ORIG_DECLS (t))
18579 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18580 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18581 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18582 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18583 }
18584
18585 keep_next_level (true);
18586 stmt = begin_omp_structured_block ();
18587
18588 pre_body = push_stmt_list ();
18589 RECUR (OMP_FOR_PRE_BODY (t));
18590 pre_body = pop_stmt_list (pre_body);
18591
18592 if (OMP_FOR_INIT (t) != NULL_TREE)
18593 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18594 any_range_for
18595 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18596 condv, incrv, &clauses, args,
18597 complain, in_decl,
18598 integral_constant_expression_p);
18599 omp_parallel_combined_clauses = NULL;
18600
18601 if (any_range_for)
18602 {
18603 gcc_assert (orig_declv);
18604 body = begin_omp_structured_block ();
18605 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18606 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18607 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18608 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18609 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18610 TREE_VEC_ELT (declv, i));
18611 }
18612 else
18613 body = push_stmt_list ();
18614 RECUR (OMP_FOR_BODY (t));
18615 if (any_range_for)
18616 body = finish_omp_structured_block (body);
18617 else
18618 body = pop_stmt_list (body);
18619
18620 if (OMP_FOR_INIT (t) != NULL_TREE)
18621 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18622 orig_declv, initv, condv, incrv, body, pre_body,
18623 NULL, clauses);
18624 else
18625 {
18626 t = make_node (TREE_CODE (t));
18627 TREE_TYPE (t) = void_type_node;
18628 OMP_FOR_BODY (t) = body;
18629 OMP_FOR_PRE_BODY (t) = pre_body;
18630 OMP_FOR_CLAUSES (t) = clauses;
18631 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18632 add_stmt (t);
18633 }
18634
18635 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18636 t));
18637 pop_omp_privatization_clauses (r);
18638 }
18639 break;
18640
18641 case OMP_SECTIONS:
18642 omp_parallel_combined_clauses = NULL;
18643 /* FALLTHRU */
18644 case OMP_SINGLE:
18645 case OMP_TEAMS:
18646 case OMP_CRITICAL:
18647 case OMP_TASKGROUP:
18648 case OMP_SCAN:
18649 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18650 && OMP_TEAMS_COMBINED (t));
18651 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18652 in_decl);
18653 if (TREE_CODE (t) == OMP_TEAMS)
18654 {
18655 keep_next_level (true);
18656 stmt = begin_omp_structured_block ();
18657 RECUR (OMP_BODY (t));
18658 stmt = finish_omp_structured_block (stmt);
18659 }
18660 else
18661 {
18662 stmt = push_stmt_list ();
18663 RECUR (OMP_BODY (t));
18664 stmt = pop_stmt_list (stmt);
18665 }
18666
18667 if (TREE_CODE (t) == OMP_CRITICAL
18668 && tmp != NULL_TREE
18669 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18670 {
18671 error_at (OMP_CLAUSE_LOCATION (tmp),
18672 "%<#pragma omp critical%> with %<hint%> clause requires "
18673 "a name, except when %<omp_sync_hint_none%> is used");
18674 RETURN (error_mark_node);
18675 }
18676 t = copy_node (t);
18677 OMP_BODY (t) = stmt;
18678 OMP_CLAUSES (t) = tmp;
18679 add_stmt (t);
18680 pop_omp_privatization_clauses (r);
18681 break;
18682
18683 case OMP_DEPOBJ:
18684 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18685 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18686 {
18687 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18688 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18689 {
18690 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18691 args, complain, in_decl);
18692 if (tmp == NULL_TREE)
18693 tmp = error_mark_node;
18694 }
18695 else
18696 {
18697 kind = (enum omp_clause_depend_kind)
18698 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18699 tmp = NULL_TREE;
18700 }
18701 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18702 }
18703 else
18704 finish_omp_depobj (EXPR_LOCATION (t), r,
18705 OMP_CLAUSE_DEPEND_SOURCE,
18706 OMP_DEPOBJ_CLAUSES (t));
18707 break;
18708
18709 case OACC_DATA:
18710 case OMP_TARGET_DATA:
18711 case OMP_TARGET:
18712 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18713 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18714 in_decl);
18715 keep_next_level (true);
18716 stmt = begin_omp_structured_block ();
18717
18718 RECUR (OMP_BODY (t));
18719 stmt = finish_omp_structured_block (stmt);
18720
18721 t = copy_node (t);
18722 OMP_BODY (t) = stmt;
18723 OMP_CLAUSES (t) = tmp;
18724 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18725 {
18726 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18727 if (teams)
18728 {
18729 /* For combined target teams, ensure the num_teams and
18730 thread_limit clause expressions are evaluated on the host,
18731 before entering the target construct. */
18732 tree c;
18733 for (c = OMP_TEAMS_CLAUSES (teams);
18734 c; c = OMP_CLAUSE_CHAIN (c))
18735 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18736 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18737 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18738 {
18739 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18740 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18741 if (expr == error_mark_node)
18742 continue;
18743 tmp = TARGET_EXPR_SLOT (expr);
18744 add_stmt (expr);
18745 OMP_CLAUSE_OPERAND (c, 0) = expr;
18746 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18747 OMP_CLAUSE_FIRSTPRIVATE);
18748 OMP_CLAUSE_DECL (tc) = tmp;
18749 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18750 OMP_TARGET_CLAUSES (t) = tc;
18751 }
18752 }
18753 }
18754 add_stmt (t);
18755 break;
18756
18757 case OACC_DECLARE:
18758 t = copy_node (t);
18759 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18760 complain, in_decl);
18761 OACC_DECLARE_CLAUSES (t) = tmp;
18762 add_stmt (t);
18763 break;
18764
18765 case OMP_TARGET_UPDATE:
18766 case OMP_TARGET_ENTER_DATA:
18767 case OMP_TARGET_EXIT_DATA:
18768 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18769 complain, in_decl);
18770 t = copy_node (t);
18771 OMP_STANDALONE_CLAUSES (t) = tmp;
18772 add_stmt (t);
18773 break;
18774
18775 case OACC_ENTER_DATA:
18776 case OACC_EXIT_DATA:
18777 case OACC_UPDATE:
18778 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18779 complain, in_decl);
18780 t = copy_node (t);
18781 OMP_STANDALONE_CLAUSES (t) = tmp;
18782 add_stmt (t);
18783 break;
18784
18785 case OMP_ORDERED:
18786 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18787 complain, in_decl);
18788 stmt = push_stmt_list ();
18789 RECUR (OMP_BODY (t));
18790 stmt = pop_stmt_list (stmt);
18791
18792 t = copy_node (t);
18793 OMP_BODY (t) = stmt;
18794 OMP_ORDERED_CLAUSES (t) = tmp;
18795 add_stmt (t);
18796 break;
18797
18798 case OMP_MASTER:
18799 omp_parallel_combined_clauses = NULL;
18800 /* FALLTHRU */
18801 case OMP_SECTION:
18802 stmt = push_stmt_list ();
18803 RECUR (OMP_BODY (t));
18804 stmt = pop_stmt_list (stmt);
18805
18806 t = copy_node (t);
18807 OMP_BODY (t) = stmt;
18808 add_stmt (t);
18809 break;
18810
18811 case OMP_ATOMIC:
18812 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18813 tmp = NULL_TREE;
18814 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18815 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18816 complain, in_decl);
18817 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18818 {
18819 tree op1 = TREE_OPERAND (t, 1);
18820 tree rhs1 = NULL_TREE;
18821 tree lhs, rhs;
18822 if (TREE_CODE (op1) == COMPOUND_EXPR)
18823 {
18824 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18825 op1 = TREE_OPERAND (op1, 1);
18826 }
18827 lhs = RECUR (TREE_OPERAND (op1, 0));
18828 rhs = RECUR (TREE_OPERAND (op1, 1));
18829 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18830 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18831 OMP_ATOMIC_MEMORY_ORDER (t));
18832 }
18833 else
18834 {
18835 tree op1 = TREE_OPERAND (t, 1);
18836 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18837 tree rhs1 = NULL_TREE;
18838 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18839 enum tree_code opcode = NOP_EXPR;
18840 if (code == OMP_ATOMIC_READ)
18841 {
18842 v = RECUR (TREE_OPERAND (op1, 0));
18843 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18844 }
18845 else if (code == OMP_ATOMIC_CAPTURE_OLD
18846 || code == OMP_ATOMIC_CAPTURE_NEW)
18847 {
18848 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18849 v = RECUR (TREE_OPERAND (op1, 0));
18850 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18851 if (TREE_CODE (op11) == COMPOUND_EXPR)
18852 {
18853 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18854 op11 = TREE_OPERAND (op11, 1);
18855 }
18856 lhs = RECUR (TREE_OPERAND (op11, 0));
18857 rhs = RECUR (TREE_OPERAND (op11, 1));
18858 opcode = TREE_CODE (op11);
18859 if (opcode == MODIFY_EXPR)
18860 opcode = NOP_EXPR;
18861 }
18862 else
18863 {
18864 code = OMP_ATOMIC;
18865 lhs = RECUR (TREE_OPERAND (op1, 0));
18866 rhs = RECUR (TREE_OPERAND (op1, 1));
18867 }
18868 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18869 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18870 }
18871 break;
18872
18873 case TRANSACTION_EXPR:
18874 {
18875 int flags = 0;
18876 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18877 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18878
18879 if (TRANSACTION_EXPR_IS_STMT (t))
18880 {
18881 tree body = TRANSACTION_EXPR_BODY (t);
18882 tree noex = NULL_TREE;
18883 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18884 {
18885 noex = MUST_NOT_THROW_COND (body);
18886 if (noex == NULL_TREE)
18887 noex = boolean_true_node;
18888 body = TREE_OPERAND (body, 0);
18889 }
18890 stmt = begin_transaction_stmt (input_location, NULL, flags);
18891 RECUR (body);
18892 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18893 }
18894 else
18895 {
18896 stmt = build_transaction_expr (EXPR_LOCATION (t),
18897 RECUR (TRANSACTION_EXPR_BODY (t)),
18898 flags, NULL_TREE);
18899 RETURN (stmt);
18900 }
18901 }
18902 break;
18903
18904 case MUST_NOT_THROW_EXPR:
18905 {
18906 tree op0 = RECUR (TREE_OPERAND (t, 0));
18907 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18908 RETURN (build_must_not_throw_expr (op0, cond));
18909 }
18910
18911 case EXPR_PACK_EXPANSION:
18912 error ("invalid use of pack expansion expression");
18913 RETURN (error_mark_node);
18914
18915 case NONTYPE_ARGUMENT_PACK:
18916 error ("use %<...%> to expand argument pack");
18917 RETURN (error_mark_node);
18918
18919 case COMPOUND_EXPR:
18920 tmp = RECUR (TREE_OPERAND (t, 0));
18921 if (tmp == NULL_TREE)
18922 /* If the first operand was a statement, we're done with it. */
18923 RETURN (RECUR (TREE_OPERAND (t, 1)));
18924 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18925 RECUR (TREE_OPERAND (t, 1)),
18926 complain));
18927
18928 case ANNOTATE_EXPR:
18929 tmp = RECUR (TREE_OPERAND (t, 0));
18930 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18931 TREE_TYPE (tmp), tmp,
18932 RECUR (TREE_OPERAND (t, 1)),
18933 RECUR (TREE_OPERAND (t, 2))));
18934
18935 case PREDICT_EXPR:
18936 RETURN (add_stmt (copy_node (t)));
18937
18938 default:
18939 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18940
18941 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18942 /*function_p=*/false,
18943 integral_constant_expression_p));
18944 }
18945
18946 RETURN (NULL_TREE);
18947 out:
18948 input_location = loc;
18949 return r;
18950 #undef RECUR
18951 #undef RETURN
18952 }
18953
18954 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18955 function. For description of the body see comment above
18956 cp_parser_omp_declare_reduction_exprs. */
18957
18958 static void
18959 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18960 {
18961 if (t == NULL_TREE || t == error_mark_node)
18962 return;
18963
18964 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
18965
18966 tree_stmt_iterator tsi;
18967 int i;
18968 tree stmts[7];
18969 memset (stmts, 0, sizeof stmts);
18970 for (i = 0, tsi = tsi_start (t);
18971 i < 7 && !tsi_end_p (tsi);
18972 i++, tsi_next (&tsi))
18973 stmts[i] = tsi_stmt (tsi);
18974 gcc_assert (tsi_end_p (tsi));
18975
18976 if (i >= 3)
18977 {
18978 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18979 && TREE_CODE (stmts[1]) == DECL_EXPR);
18980 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18981 args, complain, in_decl);
18982 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18983 args, complain, in_decl);
18984 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
18985 expect to be pushing it. */
18986 DECL_CONTEXT (omp_out) = current_function_decl;
18987 DECL_CONTEXT (omp_in) = current_function_decl;
18988 keep_next_level (true);
18989 tree block = begin_omp_structured_block ();
18990 tsubst_expr (stmts[2], args, complain, in_decl, false);
18991 block = finish_omp_structured_block (block);
18992 block = maybe_cleanup_point_expr_void (block);
18993 add_decl_expr (omp_out);
18994 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18995 TREE_NO_WARNING (omp_out) = 1;
18996 add_decl_expr (omp_in);
18997 finish_expr_stmt (block);
18998 }
18999 if (i >= 6)
19000 {
19001 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19002 && TREE_CODE (stmts[4]) == DECL_EXPR);
19003 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19004 args, complain, in_decl);
19005 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19006 args, complain, in_decl);
19007 DECL_CONTEXT (omp_priv) = current_function_decl;
19008 DECL_CONTEXT (omp_orig) = current_function_decl;
19009 keep_next_level (true);
19010 tree block = begin_omp_structured_block ();
19011 tsubst_expr (stmts[5], args, complain, in_decl, false);
19012 block = finish_omp_structured_block (block);
19013 block = maybe_cleanup_point_expr_void (block);
19014 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19015 add_decl_expr (omp_priv);
19016 add_decl_expr (omp_orig);
19017 finish_expr_stmt (block);
19018 if (i == 7)
19019 add_decl_expr (omp_orig);
19020 }
19021 }
19022
19023 /* T is a postfix-expression that is not being used in a function
19024 call. Return the substituted version of T. */
19025
19026 static tree
19027 tsubst_non_call_postfix_expression (tree t, tree args,
19028 tsubst_flags_t complain,
19029 tree in_decl)
19030 {
19031 if (TREE_CODE (t) == SCOPE_REF)
19032 t = tsubst_qualified_id (t, args, complain, in_decl,
19033 /*done=*/false, /*address_p=*/false);
19034 else
19035 t = tsubst_copy_and_build (t, args, complain, in_decl,
19036 /*function_p=*/false,
19037 /*integral_constant_expression_p=*/false);
19038
19039 return t;
19040 }
19041
19042 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19043 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19044 dependent init-capture. */
19045
19046 static void
19047 prepend_one_capture (tree field, tree init, tree &list,
19048 tsubst_flags_t complain)
19049 {
19050 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19051 {
19052 tree type = NULL_TREE;
19053 if (!init)
19054 {
19055 if (complain & tf_error)
19056 error ("empty initializer in lambda init-capture");
19057 init = error_mark_node;
19058 }
19059 else if (TREE_CODE (init) == TREE_LIST)
19060 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19061 if (!type)
19062 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19063 TREE_TYPE (field) = type;
19064 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19065 }
19066 list = tree_cons (field, init, list);
19067 }
19068
19069 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19070 instantiation context. Instantiating a pack expansion containing a lambda
19071 might result in multiple lambdas all based on the same lambda in the
19072 template. */
19073
19074 tree
19075 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19076 {
19077 tree oldfn = lambda_function (t);
19078 in_decl = oldfn;
19079
19080 tree r = build_lambda_expr ();
19081
19082 LAMBDA_EXPR_LOCATION (r)
19083 = LAMBDA_EXPR_LOCATION (t);
19084 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19085 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19086 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19087 LAMBDA_EXPR_INSTANTIATED (r) = true;
19088
19089 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19090 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19091
19092 vec<tree,va_gc>* field_packs = NULL;
19093
19094 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19095 cap = TREE_CHAIN (cap))
19096 {
19097 tree ofield = TREE_PURPOSE (cap);
19098 tree init = TREE_VALUE (cap);
19099 if (PACK_EXPANSION_P (init))
19100 init = tsubst_pack_expansion (init, args, complain, in_decl);
19101 else
19102 init = tsubst_copy_and_build (init, args, complain, in_decl,
19103 /*fn*/false, /*constexpr*/false);
19104
19105 if (init == error_mark_node)
19106 return error_mark_node;
19107
19108 if (init && TREE_CODE (init) == TREE_LIST)
19109 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19110
19111 if (!processing_template_decl
19112 && init && TREE_CODE (init) != TREE_VEC
19113 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19114 {
19115 /* For a VLA, simply tsubsting the field type won't work, we need to
19116 go through add_capture again. XXX do we want to do this for all
19117 captures? */
19118 tree name = (get_identifier
19119 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19120 tree ftype = TREE_TYPE (ofield);
19121 bool by_ref = (TYPE_REF_P (ftype)
19122 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19123 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19124 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19125 continue;
19126 }
19127
19128 if (PACK_EXPANSION_P (ofield))
19129 ofield = PACK_EXPANSION_PATTERN (ofield);
19130 tree field = tsubst_decl (ofield, args, complain);
19131
19132 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19133 {
19134 /* Remember these for when we've pushed local_specializations. */
19135 vec_safe_push (field_packs, ofield);
19136 vec_safe_push (field_packs, field);
19137 }
19138
19139 if (field == error_mark_node)
19140 return error_mark_node;
19141
19142 if (TREE_CODE (field) == TREE_VEC)
19143 {
19144 int len = TREE_VEC_LENGTH (field);
19145 gcc_assert (TREE_CODE (init) == TREE_VEC
19146 && TREE_VEC_LENGTH (init) == len);
19147 for (int i = 0; i < len; ++i)
19148 prepend_one_capture (TREE_VEC_ELT (field, i),
19149 TREE_VEC_ELT (init, i),
19150 LAMBDA_EXPR_CAPTURE_LIST (r),
19151 complain);
19152 }
19153 else
19154 {
19155 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19156 complain);
19157
19158 if (id_equal (DECL_NAME (field), "__this"))
19159 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19160 }
19161 }
19162
19163 tree type = begin_lambda_type (r);
19164 if (type == error_mark_node)
19165 return error_mark_node;
19166
19167 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19168 /* A lambda in a default argument outside a class gets no
19169 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19170 tsubst_default_argument calls start_lambda_scope, so we need to
19171 specifically ignore it here, and use the global scope. */
19172 record_null_lambda_scope (r);
19173 else
19174 record_lambda_scope (r);
19175
19176 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19177 determine_visibility (TYPE_NAME (type));
19178
19179 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19180
19181 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19182 ? DECL_TI_TEMPLATE (oldfn)
19183 : NULL_TREE);
19184
19185 tree fntype = static_fn_type (oldfn);
19186 if (oldtmpl)
19187 ++processing_template_decl;
19188 fntype = tsubst (fntype, args, complain, in_decl);
19189 if (oldtmpl)
19190 --processing_template_decl;
19191
19192 if (fntype == error_mark_node)
19193 r = error_mark_node;
19194 else
19195 {
19196 /* The body of a lambda-expression is not a subexpression of the
19197 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19198 which would be skipped if cp_unevaluated_operand. */
19199 cp_evaluated ev;
19200
19201 /* Fix the type of 'this'. */
19202 fntype = build_memfn_type (fntype, type,
19203 type_memfn_quals (fntype),
19204 type_memfn_rqual (fntype));
19205 tree fn, tmpl;
19206 if (oldtmpl)
19207 {
19208 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19209 if (tmpl == error_mark_node)
19210 {
19211 r = error_mark_node;
19212 goto out;
19213 }
19214 fn = DECL_TEMPLATE_RESULT (tmpl);
19215 finish_member_declaration (tmpl);
19216 }
19217 else
19218 {
19219 tmpl = NULL_TREE;
19220 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19221 if (fn == error_mark_node)
19222 {
19223 r = error_mark_node;
19224 goto out;
19225 }
19226 finish_member_declaration (fn);
19227 }
19228
19229 if (tree ci = get_constraints (oldfn))
19230 {
19231 /* Substitute into the lambda's constraints. */
19232 if (oldtmpl)
19233 ++processing_template_decl;
19234 ci = tsubst_constraint_info (ci, args, complain, in_decl);
19235 if (oldtmpl)
19236 --processing_template_decl;
19237 set_constraints (fn, ci);
19238 }
19239
19240 /* Let finish_function set this. */
19241 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19242
19243 bool nested = cfun;
19244 if (nested)
19245 push_function_context ();
19246 else
19247 /* Still increment function_depth so that we don't GC in the
19248 middle of an expression. */
19249 ++function_depth;
19250
19251 local_specialization_stack s (lss_copy);
19252
19253 tree body = start_lambda_function (fn, r);
19254
19255 /* Now record them for lookup_init_capture_pack. */
19256 int fplen = vec_safe_length (field_packs);
19257 for (int i = 0; i < fplen; )
19258 {
19259 tree pack = (*field_packs)[i++];
19260 tree inst = (*field_packs)[i++];
19261 register_local_specialization (inst, pack);
19262 }
19263 release_tree_vector (field_packs);
19264
19265 register_parameter_specializations (oldfn, fn);
19266
19267 if (oldtmpl)
19268 {
19269 /* We might not partially instantiate some parts of the function, so
19270 copy these flags from the original template. */
19271 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19272 current_function_returns_value = ol->returns_value;
19273 current_function_returns_null = ol->returns_null;
19274 current_function_returns_abnormally = ol->returns_abnormally;
19275 current_function_infinite_loop = ol->infinite_loop;
19276 }
19277
19278 /* [temp.deduct] A lambda-expression appearing in a function type or a
19279 template parameter is not considered part of the immediate context for
19280 the purposes of template argument deduction. */
19281 complain = tf_warning_or_error;
19282
19283 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19284 /*constexpr*/false);
19285
19286 finish_lambda_function (body);
19287
19288 if (nested)
19289 pop_function_context ();
19290 else
19291 --function_depth;
19292
19293 /* The capture list was built up in reverse order; fix that now. */
19294 LAMBDA_EXPR_CAPTURE_LIST (r)
19295 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19296
19297 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19298
19299 maybe_add_lambda_conv_op (type);
19300 }
19301
19302 out:
19303 finish_struct (type, /*attr*/NULL_TREE);
19304
19305 insert_pending_capture_proxies ();
19306
19307 return r;
19308 }
19309
19310 /* Like tsubst but deals with expressions and performs semantic
19311 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19312 "F<TARGS> (ARGS)". */
19313
19314 tree
19315 tsubst_copy_and_build (tree t,
19316 tree args,
19317 tsubst_flags_t complain,
19318 tree in_decl,
19319 bool function_p,
19320 bool integral_constant_expression_p)
19321 {
19322 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19323 #define RECUR(NODE) \
19324 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19325 /*function_p=*/false, \
19326 integral_constant_expression_p)
19327
19328 tree retval, op1;
19329 location_t save_loc;
19330
19331 if (t == NULL_TREE || t == error_mark_node)
19332 return t;
19333
19334 save_loc = input_location;
19335 if (location_t eloc = cp_expr_location (t))
19336 input_location = eloc;
19337
19338 /* N3276 decltype magic only applies to calls at the top level or on the
19339 right side of a comma. */
19340 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19341 complain &= ~tf_decltype;
19342
19343 switch (TREE_CODE (t))
19344 {
19345 case USING_DECL:
19346 t = DECL_NAME (t);
19347 /* Fall through. */
19348 case IDENTIFIER_NODE:
19349 {
19350 tree decl;
19351 cp_id_kind idk;
19352 bool non_integral_constant_expression_p;
19353 const char *error_msg;
19354
19355 if (IDENTIFIER_CONV_OP_P (t))
19356 {
19357 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19358 t = make_conv_op_name (new_type);
19359 }
19360
19361 /* Look up the name. */
19362 decl = lookup_name (t);
19363
19364 /* By convention, expressions use ERROR_MARK_NODE to indicate
19365 failure, not NULL_TREE. */
19366 if (decl == NULL_TREE)
19367 decl = error_mark_node;
19368
19369 decl = finish_id_expression (t, decl, NULL_TREE,
19370 &idk,
19371 integral_constant_expression_p,
19372 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19373 &non_integral_constant_expression_p,
19374 /*template_p=*/false,
19375 /*done=*/true,
19376 /*address_p=*/false,
19377 /*template_arg_p=*/false,
19378 &error_msg,
19379 input_location);
19380 if (error_msg)
19381 error (error_msg);
19382 if (!function_p && identifier_p (decl))
19383 {
19384 if (complain & tf_error)
19385 unqualified_name_lookup_error (decl);
19386 decl = error_mark_node;
19387 }
19388 RETURN (decl);
19389 }
19390
19391 case TEMPLATE_ID_EXPR:
19392 {
19393 tree object;
19394 tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19395 complain, in_decl,
19396 function_p,
19397 integral_constant_expression_p);
19398 tree targs = TREE_OPERAND (t, 1);
19399
19400 if (targs)
19401 targs = tsubst_template_args (targs, args, complain, in_decl);
19402 if (targs == error_mark_node)
19403 RETURN (error_mark_node);
19404
19405 if (TREE_CODE (templ) == SCOPE_REF)
19406 {
19407 tree name = TREE_OPERAND (templ, 1);
19408 tree tid = lookup_template_function (name, targs);
19409 TREE_OPERAND (templ, 1) = tid;
19410 RETURN (templ);
19411 }
19412
19413 if (concept_definition_p (templ))
19414 {
19415 tree check = build_concept_check (templ, targs, complain);
19416 if (check == error_mark_node)
19417 RETURN (error_mark_node);
19418
19419 tree id = unpack_concept_check (check);
19420
19421 /* If we built a function concept check, return the underlying
19422 template-id. So we can evaluate it as a function call. */
19423 if (function_concept_p (TREE_OPERAND (id, 0)))
19424 RETURN (id);
19425
19426 RETURN (check);
19427 }
19428
19429 if (variable_template_p (templ))
19430 {
19431 tree r = lookup_and_finish_template_variable (templ, targs,
19432 complain);
19433 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19434 RETURN (r);
19435 }
19436
19437 if (TREE_CODE (templ) == COMPONENT_REF)
19438 {
19439 object = TREE_OPERAND (templ, 0);
19440 templ = TREE_OPERAND (templ, 1);
19441 }
19442 else
19443 object = NULL_TREE;
19444
19445 tree tid = lookup_template_function (templ, targs);
19446
19447 if (object)
19448 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
19449 object, tid, NULL_TREE));
19450 else if (identifier_p (templ))
19451 {
19452 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19453 name lookup found nothing when parsing the template name. */
19454 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
19455 RETURN (tid);
19456 }
19457 else
19458 RETURN (baselink_for_fns (tid));
19459 }
19460
19461 case INDIRECT_REF:
19462 {
19463 tree r = RECUR (TREE_OPERAND (t, 0));
19464
19465 if (REFERENCE_REF_P (t))
19466 {
19467 /* A type conversion to reference type will be enclosed in
19468 such an indirect ref, but the substitution of the cast
19469 will have also added such an indirect ref. */
19470 r = convert_from_reference (r);
19471 }
19472 else
19473 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19474 complain|decltype_flag);
19475
19476 if (REF_PARENTHESIZED_P (t))
19477 r = force_paren_expr (r);
19478
19479 RETURN (r);
19480 }
19481
19482 case NOP_EXPR:
19483 {
19484 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19485 tree op0 = RECUR (TREE_OPERAND (t, 0));
19486 RETURN (build_nop (type, op0));
19487 }
19488
19489 case IMPLICIT_CONV_EXPR:
19490 {
19491 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19492 tree expr = RECUR (TREE_OPERAND (t, 0));
19493 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19494 {
19495 retval = copy_node (t);
19496 TREE_TYPE (retval) = type;
19497 TREE_OPERAND (retval, 0) = expr;
19498 RETURN (retval);
19499 }
19500 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19501 /* We'll pass this to convert_nontype_argument again, we don't need
19502 to actually perform any conversion here. */
19503 RETURN (expr);
19504 int flags = LOOKUP_IMPLICIT;
19505 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19506 flags = LOOKUP_NORMAL;
19507 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19508 flags |= LOOKUP_NO_NARROWING;
19509 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19510 flags));
19511 }
19512
19513 case CONVERT_EXPR:
19514 {
19515 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19516 tree op0 = RECUR (TREE_OPERAND (t, 0));
19517 if (op0 == error_mark_node)
19518 RETURN (error_mark_node);
19519 RETURN (build1 (CONVERT_EXPR, type, op0));
19520 }
19521
19522 case CAST_EXPR:
19523 case REINTERPRET_CAST_EXPR:
19524 case CONST_CAST_EXPR:
19525 case DYNAMIC_CAST_EXPR:
19526 case STATIC_CAST_EXPR:
19527 {
19528 tree type;
19529 tree op, r = NULL_TREE;
19530
19531 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19532 if (integral_constant_expression_p
19533 && !cast_valid_in_integral_constant_expression_p (type))
19534 {
19535 if (complain & tf_error)
19536 error ("a cast to a type other than an integral or "
19537 "enumeration type cannot appear in a constant-expression");
19538 RETURN (error_mark_node);
19539 }
19540
19541 op = RECUR (TREE_OPERAND (t, 0));
19542
19543 warning_sentinel s(warn_useless_cast);
19544 warning_sentinel s2(warn_ignored_qualifiers);
19545 switch (TREE_CODE (t))
19546 {
19547 case CAST_EXPR:
19548 r = build_functional_cast (input_location, type, op, complain);
19549 break;
19550 case REINTERPRET_CAST_EXPR:
19551 r = build_reinterpret_cast (input_location, type, op, complain);
19552 break;
19553 case CONST_CAST_EXPR:
19554 r = build_const_cast (input_location, type, op, complain);
19555 break;
19556 case DYNAMIC_CAST_EXPR:
19557 r = build_dynamic_cast (input_location, type, op, complain);
19558 break;
19559 case STATIC_CAST_EXPR:
19560 r = build_static_cast (input_location, type, op, complain);
19561 if (IMPLICIT_RVALUE_P (t))
19562 set_implicit_rvalue_p (r);
19563 break;
19564 default:
19565 gcc_unreachable ();
19566 }
19567
19568 RETURN (r);
19569 }
19570
19571 case POSTDECREMENT_EXPR:
19572 case POSTINCREMENT_EXPR:
19573 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19574 args, complain, in_decl);
19575 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19576 complain|decltype_flag));
19577
19578 case PREDECREMENT_EXPR:
19579 case PREINCREMENT_EXPR:
19580 case NEGATE_EXPR:
19581 case BIT_NOT_EXPR:
19582 case ABS_EXPR:
19583 case TRUTH_NOT_EXPR:
19584 case UNARY_PLUS_EXPR: /* Unary + */
19585 case REALPART_EXPR:
19586 case IMAGPART_EXPR:
19587 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19588 RECUR (TREE_OPERAND (t, 0)),
19589 complain|decltype_flag));
19590
19591 case FIX_TRUNC_EXPR:
19592 gcc_unreachable ();
19593
19594 case ADDR_EXPR:
19595 op1 = TREE_OPERAND (t, 0);
19596 if (TREE_CODE (op1) == LABEL_DECL)
19597 RETURN (finish_label_address_expr (DECL_NAME (op1),
19598 EXPR_LOCATION (op1)));
19599 if (TREE_CODE (op1) == SCOPE_REF)
19600 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19601 /*done=*/true, /*address_p=*/true);
19602 else
19603 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19604 in_decl);
19605 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19606 complain|decltype_flag));
19607
19608 case PLUS_EXPR:
19609 case MINUS_EXPR:
19610 case MULT_EXPR:
19611 case TRUNC_DIV_EXPR:
19612 case CEIL_DIV_EXPR:
19613 case FLOOR_DIV_EXPR:
19614 case ROUND_DIV_EXPR:
19615 case EXACT_DIV_EXPR:
19616 case BIT_AND_EXPR:
19617 case BIT_IOR_EXPR:
19618 case BIT_XOR_EXPR:
19619 case TRUNC_MOD_EXPR:
19620 case FLOOR_MOD_EXPR:
19621 case TRUTH_ANDIF_EXPR:
19622 case TRUTH_ORIF_EXPR:
19623 case TRUTH_AND_EXPR:
19624 case TRUTH_OR_EXPR:
19625 case RSHIFT_EXPR:
19626 case LSHIFT_EXPR:
19627 case EQ_EXPR:
19628 case NE_EXPR:
19629 case MAX_EXPR:
19630 case MIN_EXPR:
19631 case LE_EXPR:
19632 case GE_EXPR:
19633 case LT_EXPR:
19634 case GT_EXPR:
19635 case SPACESHIP_EXPR:
19636 case MEMBER_REF:
19637 case DOTSTAR_EXPR:
19638 {
19639 /* If T was type-dependent, suppress warnings that depend on the range
19640 of the types involved. */
19641 ++processing_template_decl;
19642 const bool was_dep = (potential_constant_expression (t)
19643 ? value_dependent_expression_p (t)
19644 : type_dependent_expression_p (t));
19645 --processing_template_decl;
19646 tree op0 = RECUR (TREE_OPERAND (t, 0));
19647 tree op1 = RECUR (TREE_OPERAND (t, 1));
19648
19649 warning_sentinel s1(warn_type_limits, was_dep);
19650 warning_sentinel s2(warn_div_by_zero, was_dep);
19651 warning_sentinel s3(warn_logical_op, was_dep);
19652 warning_sentinel s4(warn_tautological_compare, was_dep);
19653
19654 tree r = build_x_binary_op
19655 (input_location, TREE_CODE (t),
19656 op0,
19657 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19658 ? ERROR_MARK
19659 : TREE_CODE (TREE_OPERAND (t, 0))),
19660 op1,
19661 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19662 ? ERROR_MARK
19663 : TREE_CODE (TREE_OPERAND (t, 1))),
19664 /*overload=*/NULL,
19665 complain|decltype_flag);
19666 if (EXPR_P (r) && TREE_NO_WARNING (t))
19667 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19668
19669 RETURN (r);
19670 }
19671
19672 case POINTER_PLUS_EXPR:
19673 {
19674 tree op0 = RECUR (TREE_OPERAND (t, 0));
19675 if (op0 == error_mark_node)
19676 RETURN (error_mark_node);
19677 tree op1 = RECUR (TREE_OPERAND (t, 1));
19678 if (op1 == error_mark_node)
19679 RETURN (error_mark_node);
19680 RETURN (fold_build_pointer_plus (op0, op1));
19681 }
19682
19683 case SCOPE_REF:
19684 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19685 /*address_p=*/false));
19686 case ARRAY_REF:
19687 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19688 args, complain, in_decl);
19689 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19690 RECUR (TREE_OPERAND (t, 1)),
19691 complain|decltype_flag));
19692
19693 case SIZEOF_EXPR:
19694 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19695 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19696 RETURN (tsubst_copy (t, args, complain, in_decl));
19697 /* Fall through */
19698
19699 case ALIGNOF_EXPR:
19700 {
19701 tree r;
19702
19703 op1 = TREE_OPERAND (t, 0);
19704 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19705 op1 = TREE_TYPE (op1);
19706 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19707 && ALIGNOF_EXPR_STD_P (t));
19708 if (!args)
19709 {
19710 /* When there are no ARGS, we are trying to evaluate a
19711 non-dependent expression from the parser. Trying to do
19712 the substitutions may not work. */
19713 if (!TYPE_P (op1))
19714 op1 = TREE_TYPE (op1);
19715 }
19716 else
19717 {
19718 ++cp_unevaluated_operand;
19719 ++c_inhibit_evaluation_warnings;
19720 if (TYPE_P (op1))
19721 op1 = tsubst (op1, args, complain, in_decl);
19722 else
19723 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19724 /*function_p=*/false,
19725 /*integral_constant_expression_p=*/
19726 false);
19727 --cp_unevaluated_operand;
19728 --c_inhibit_evaluation_warnings;
19729 }
19730 if (TYPE_P (op1))
19731 r = cxx_sizeof_or_alignof_type (input_location,
19732 op1, TREE_CODE (t), std_alignof,
19733 complain & tf_error);
19734 else
19735 r = cxx_sizeof_or_alignof_expr (input_location,
19736 op1, TREE_CODE (t), std_alignof,
19737 complain & tf_error);
19738 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19739 {
19740 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19741 {
19742 if (!processing_template_decl && TYPE_P (op1))
19743 {
19744 r = build_min (SIZEOF_EXPR, size_type_node,
19745 build1 (NOP_EXPR, op1, error_mark_node));
19746 SIZEOF_EXPR_TYPE_P (r) = 1;
19747 }
19748 else
19749 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19750 TREE_SIDE_EFFECTS (r) = 0;
19751 TREE_READONLY (r) = 1;
19752 }
19753 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19754 }
19755 RETURN (r);
19756 }
19757
19758 case AT_ENCODE_EXPR:
19759 {
19760 op1 = TREE_OPERAND (t, 0);
19761 ++cp_unevaluated_operand;
19762 ++c_inhibit_evaluation_warnings;
19763 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19764 /*function_p=*/false,
19765 /*integral_constant_expression_p=*/false);
19766 --cp_unevaluated_operand;
19767 --c_inhibit_evaluation_warnings;
19768 RETURN (objc_build_encode_expr (op1));
19769 }
19770
19771 case NOEXCEPT_EXPR:
19772 op1 = TREE_OPERAND (t, 0);
19773 ++cp_unevaluated_operand;
19774 ++c_inhibit_evaluation_warnings;
19775 ++cp_noexcept_operand;
19776 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19777 /*function_p=*/false,
19778 /*integral_constant_expression_p=*/false);
19779 --cp_unevaluated_operand;
19780 --c_inhibit_evaluation_warnings;
19781 --cp_noexcept_operand;
19782 RETURN (finish_noexcept_expr (op1, complain));
19783
19784 case MODOP_EXPR:
19785 {
19786 warning_sentinel s(warn_div_by_zero);
19787 tree lhs = RECUR (TREE_OPERAND (t, 0));
19788 tree rhs = RECUR (TREE_OPERAND (t, 2));
19789 tree r = build_x_modify_expr
19790 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19791 complain|decltype_flag);
19792 /* TREE_NO_WARNING must be set if either the expression was
19793 parenthesized or it uses an operator such as >>= rather
19794 than plain assignment. In the former case, it was already
19795 set and must be copied. In the latter case,
19796 build_x_modify_expr sets it and it must not be reset
19797 here. */
19798 if (TREE_NO_WARNING (t))
19799 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19800
19801 RETURN (r);
19802 }
19803
19804 case ARROW_EXPR:
19805 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19806 args, complain, in_decl);
19807 /* Remember that there was a reference to this entity. */
19808 if (DECL_P (op1)
19809 && !mark_used (op1, complain) && !(complain & tf_error))
19810 RETURN (error_mark_node);
19811 RETURN (build_x_arrow (input_location, op1, complain));
19812
19813 case NEW_EXPR:
19814 {
19815 tree placement = RECUR (TREE_OPERAND (t, 0));
19816 tree init = RECUR (TREE_OPERAND (t, 3));
19817 vec<tree, va_gc> *placement_vec;
19818 vec<tree, va_gc> *init_vec;
19819 tree ret;
19820 location_t loc = EXPR_LOCATION (t);
19821
19822 if (placement == NULL_TREE)
19823 placement_vec = NULL;
19824 else if (placement == error_mark_node)
19825 RETURN (error_mark_node);
19826 else
19827 {
19828 placement_vec = make_tree_vector ();
19829 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19830 vec_safe_push (placement_vec, TREE_VALUE (placement));
19831 }
19832
19833 /* If there was an initializer in the original tree, but it
19834 instantiated to an empty list, then we should pass a
19835 non-NULL empty vector to tell build_new that it was an
19836 empty initializer() rather than no initializer. This can
19837 only happen when the initializer is a pack expansion whose
19838 parameter packs are of length zero. */
19839 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19840 init_vec = NULL;
19841 else
19842 {
19843 init_vec = make_tree_vector ();
19844 if (init == void_node)
19845 gcc_assert (init_vec != NULL);
19846 else
19847 {
19848 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19849 vec_safe_push (init_vec, TREE_VALUE (init));
19850 }
19851 }
19852
19853 /* Avoid passing an enclosing decl to valid_array_size_p. */
19854 in_decl = NULL_TREE;
19855
19856 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19857 tree op2 = RECUR (TREE_OPERAND (t, 2));
19858 ret = build_new (loc, &placement_vec, op1, op2,
19859 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19860 complain);
19861
19862 if (placement_vec != NULL)
19863 release_tree_vector (placement_vec);
19864 if (init_vec != NULL)
19865 release_tree_vector (init_vec);
19866
19867 RETURN (ret);
19868 }
19869
19870 case DELETE_EXPR:
19871 {
19872 tree op0 = RECUR (TREE_OPERAND (t, 0));
19873 tree op1 = RECUR (TREE_OPERAND (t, 1));
19874 RETURN (delete_sanity (input_location, op0, op1,
19875 DELETE_EXPR_USE_VEC (t),
19876 DELETE_EXPR_USE_GLOBAL (t),
19877 complain));
19878 }
19879
19880 case COMPOUND_EXPR:
19881 {
19882 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19883 complain & ~tf_decltype, in_decl,
19884 /*function_p=*/false,
19885 integral_constant_expression_p);
19886 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19887 op0,
19888 RECUR (TREE_OPERAND (t, 1)),
19889 complain|decltype_flag));
19890 }
19891
19892 case CALL_EXPR:
19893 {
19894 tree function;
19895 unsigned int nargs, i;
19896 bool qualified_p;
19897 bool koenig_p;
19898 tree ret;
19899
19900 function = CALL_EXPR_FN (t);
19901 /* Internal function with no arguments. */
19902 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19903 RETURN (t);
19904
19905 /* When we parsed the expression, we determined whether or
19906 not Koenig lookup should be performed. */
19907 koenig_p = KOENIG_LOOKUP_P (t);
19908 if (function == NULL_TREE)
19909 {
19910 koenig_p = false;
19911 qualified_p = false;
19912 }
19913 else if (TREE_CODE (function) == SCOPE_REF)
19914 {
19915 qualified_p = true;
19916 function = tsubst_qualified_id (function, args, complain, in_decl,
19917 /*done=*/false,
19918 /*address_p=*/false);
19919 }
19920 else if (koenig_p && identifier_p (function))
19921 {
19922 /* Do nothing; calling tsubst_copy_and_build on an identifier
19923 would incorrectly perform unqualified lookup again.
19924
19925 Note that we can also have an IDENTIFIER_NODE if the earlier
19926 unqualified lookup found a member function; in that case
19927 koenig_p will be false and we do want to do the lookup
19928 again to find the instantiated member function.
19929
19930 FIXME but doing that causes c++/15272, so we need to stop
19931 using IDENTIFIER_NODE in that situation. */
19932 qualified_p = false;
19933 }
19934 else
19935 {
19936 if (TREE_CODE (function) == COMPONENT_REF)
19937 {
19938 tree op = TREE_OPERAND (function, 1);
19939
19940 qualified_p = (TREE_CODE (op) == SCOPE_REF
19941 || (BASELINK_P (op)
19942 && BASELINK_QUALIFIED_P (op)));
19943 }
19944 else
19945 qualified_p = false;
19946
19947 if (TREE_CODE (function) == ADDR_EXPR
19948 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19949 /* Avoid error about taking the address of a constructor. */
19950 function = TREE_OPERAND (function, 0);
19951
19952 function = tsubst_copy_and_build (function, args, complain,
19953 in_decl,
19954 !qualified_p,
19955 integral_constant_expression_p);
19956
19957 if (BASELINK_P (function))
19958 qualified_p = true;
19959 }
19960
19961 nargs = call_expr_nargs (t);
19962 releasing_vec call_args;
19963 for (i = 0; i < nargs; ++i)
19964 {
19965 tree arg = CALL_EXPR_ARG (t, i);
19966
19967 if (!PACK_EXPANSION_P (arg))
19968 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19969 else
19970 {
19971 /* Expand the pack expansion and push each entry onto
19972 CALL_ARGS. */
19973 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19974 if (TREE_CODE (arg) == TREE_VEC)
19975 {
19976 unsigned int len, j;
19977
19978 len = TREE_VEC_LENGTH (arg);
19979 for (j = 0; j < len; ++j)
19980 {
19981 tree value = TREE_VEC_ELT (arg, j);
19982 if (value != NULL_TREE)
19983 value = convert_from_reference (value);
19984 vec_safe_push (call_args, value);
19985 }
19986 }
19987 else
19988 {
19989 /* A partial substitution. Add one entry. */
19990 vec_safe_push (call_args, arg);
19991 }
19992 }
19993 }
19994
19995 /* Stripped-down processing for a call in a thunk. Specifically, in
19996 the thunk template for a generic lambda. */
19997 if (call_from_lambda_thunk_p (t))
19998 {
19999 /* Now that we've expanded any packs, the number of call args
20000 might be different. */
20001 unsigned int cargs = call_args->length ();
20002 tree thisarg = NULL_TREE;
20003 if (TREE_CODE (function) == COMPONENT_REF)
20004 {
20005 thisarg = TREE_OPERAND (function, 0);
20006 if (TREE_CODE (thisarg) == INDIRECT_REF)
20007 thisarg = TREE_OPERAND (thisarg, 0);
20008 function = TREE_OPERAND (function, 1);
20009 if (TREE_CODE (function) == BASELINK)
20010 function = BASELINK_FUNCTIONS (function);
20011 }
20012 /* We aren't going to do normal overload resolution, so force the
20013 template-id to resolve. */
20014 function = resolve_nondeduced_context (function, complain);
20015 for (unsigned i = 0; i < cargs; ++i)
20016 {
20017 /* In a thunk, pass through args directly, without any
20018 conversions. */
20019 tree arg = (*call_args)[i];
20020 while (TREE_CODE (arg) != PARM_DECL)
20021 arg = TREE_OPERAND (arg, 0);
20022 (*call_args)[i] = arg;
20023 }
20024 if (thisarg)
20025 {
20026 /* If there are no other args, just push 'this'. */
20027 if (cargs == 0)
20028 vec_safe_push (call_args, thisarg);
20029 else
20030 {
20031 /* Otherwise, shift the other args over to make room. */
20032 tree last = (*call_args)[cargs - 1];
20033 vec_safe_push (call_args, last);
20034 for (int i = cargs - 1; i > 0; --i)
20035 (*call_args)[i] = (*call_args)[i - 1];
20036 (*call_args)[0] = thisarg;
20037 }
20038 }
20039 ret = build_call_a (function, call_args->length (),
20040 call_args->address ());
20041 /* The thunk location is not interesting. */
20042 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20043 CALL_FROM_THUNK_P (ret) = true;
20044 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20045 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20046
20047 RETURN (ret);
20048 }
20049
20050 /* We do not perform argument-dependent lookup if normal
20051 lookup finds a non-function, in accordance with the
20052 resolution of DR 218. */
20053 if (koenig_p
20054 && ((is_overloaded_fn (function)
20055 /* If lookup found a member function, the Koenig lookup is
20056 not appropriate, even if an unqualified-name was used
20057 to denote the function. */
20058 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20059 || identifier_p (function)
20060 /* C++20 P0846: Lookup found nothing. */
20061 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20062 && identifier_p (TREE_OPERAND (function, 0))))
20063 /* Only do this when substitution turns a dependent call
20064 into a non-dependent call. */
20065 && type_dependent_expression_p_push (t)
20066 && !any_type_dependent_arguments_p (call_args))
20067 function = perform_koenig_lookup (function, call_args, tf_none);
20068
20069 if (function != NULL_TREE
20070 && (identifier_p (function)
20071 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20072 && identifier_p (TREE_OPERAND (function, 0))))
20073 && !any_type_dependent_arguments_p (call_args))
20074 {
20075 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20076 function = TREE_OPERAND (function, 0);
20077 if (koenig_p && (complain & tf_warning_or_error))
20078 {
20079 /* For backwards compatibility and good diagnostics, try
20080 the unqualified lookup again if we aren't in SFINAE
20081 context. */
20082 tree unq = (tsubst_copy_and_build
20083 (function, args, complain, in_decl, true,
20084 integral_constant_expression_p));
20085 if (unq == error_mark_node)
20086 RETURN (error_mark_node);
20087
20088 if (unq != function)
20089 {
20090 /* In a lambda fn, we have to be careful to not
20091 introduce new this captures. Legacy code can't
20092 be using lambdas anyway, so it's ok to be
20093 stricter. */
20094 bool in_lambda = (current_class_type
20095 && LAMBDA_TYPE_P (current_class_type));
20096 char const *const msg
20097 = G_("%qD was not declared in this scope, "
20098 "and no declarations were found by "
20099 "argument-dependent lookup at the point "
20100 "of instantiation");
20101
20102 bool diag = true;
20103 if (in_lambda)
20104 error_at (cp_expr_loc_or_input_loc (t),
20105 msg, function);
20106 else
20107 diag = permerror (cp_expr_loc_or_input_loc (t),
20108 msg, function);
20109 if (diag)
20110 {
20111 tree fn = unq;
20112
20113 if (INDIRECT_REF_P (fn))
20114 fn = TREE_OPERAND (fn, 0);
20115 if (is_overloaded_fn (fn))
20116 fn = get_first_fn (fn);
20117
20118 if (!DECL_P (fn))
20119 /* Can't say anything more. */;
20120 else if (DECL_CLASS_SCOPE_P (fn))
20121 {
20122 location_t loc = cp_expr_loc_or_input_loc (t);
20123 inform (loc,
20124 "declarations in dependent base %qT are "
20125 "not found by unqualified lookup",
20126 DECL_CLASS_CONTEXT (fn));
20127 if (current_class_ptr)
20128 inform (loc,
20129 "use %<this->%D%> instead", function);
20130 else
20131 inform (loc,
20132 "use %<%T::%D%> instead",
20133 current_class_name, function);
20134 }
20135 else
20136 inform (DECL_SOURCE_LOCATION (fn),
20137 "%qD declared here, later in the "
20138 "translation unit", fn);
20139 if (in_lambda)
20140 RETURN (error_mark_node);
20141 }
20142
20143 function = unq;
20144 }
20145 }
20146 if (identifier_p (function))
20147 {
20148 if (complain & tf_error)
20149 unqualified_name_lookup_error (function);
20150 RETURN (error_mark_node);
20151 }
20152 }
20153
20154 /* Remember that there was a reference to this entity. */
20155 if (function != NULL_TREE
20156 && DECL_P (function)
20157 && !mark_used (function, complain) && !(complain & tf_error))
20158 RETURN (error_mark_node);
20159
20160 /* Put back tf_decltype for the actual call. */
20161 complain |= decltype_flag;
20162
20163 if (function == NULL_TREE)
20164 switch (CALL_EXPR_IFN (t))
20165 {
20166 case IFN_LAUNDER:
20167 gcc_assert (nargs == 1);
20168 if (vec_safe_length (call_args) != 1)
20169 {
20170 error_at (cp_expr_loc_or_input_loc (t),
20171 "wrong number of arguments to "
20172 "%<__builtin_launder%>");
20173 ret = error_mark_node;
20174 }
20175 else
20176 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20177 (*call_args)[0], complain);
20178 break;
20179
20180 case IFN_VEC_CONVERT:
20181 gcc_assert (nargs == 1);
20182 if (vec_safe_length (call_args) != 1)
20183 {
20184 error_at (cp_expr_loc_or_input_loc (t),
20185 "wrong number of arguments to "
20186 "%<__builtin_convertvector%>");
20187 ret = error_mark_node;
20188 break;
20189 }
20190 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20191 tsubst (TREE_TYPE (t), args,
20192 complain, in_decl),
20193 complain);
20194 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20195 RETURN (ret);
20196 break;
20197
20198 default:
20199 /* Unsupported internal function with arguments. */
20200 gcc_unreachable ();
20201 }
20202 else if (TREE_CODE (function) == OFFSET_REF
20203 || TREE_CODE (function) == DOTSTAR_EXPR
20204 || TREE_CODE (function) == MEMBER_REF)
20205 ret = build_offset_ref_call_from_tree (function, &call_args,
20206 complain);
20207 else if (TREE_CODE (function) == COMPONENT_REF)
20208 {
20209 tree instance = TREE_OPERAND (function, 0);
20210 tree fn = TREE_OPERAND (function, 1);
20211
20212 if (processing_template_decl
20213 && (type_dependent_expression_p (instance)
20214 || (!BASELINK_P (fn)
20215 && TREE_CODE (fn) != FIELD_DECL)
20216 || type_dependent_expression_p (fn)
20217 || any_type_dependent_arguments_p (call_args)))
20218 ret = build_min_nt_call_vec (function, call_args);
20219 else if (!BASELINK_P (fn))
20220 ret = finish_call_expr (function, &call_args,
20221 /*disallow_virtual=*/false,
20222 /*koenig_p=*/false,
20223 complain);
20224 else
20225 ret = (build_new_method_call
20226 (instance, fn,
20227 &call_args, NULL_TREE,
20228 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20229 /*fn_p=*/NULL,
20230 complain));
20231 }
20232 else if (concept_check_p (function))
20233 {
20234 /* FUNCTION is a template-id referring to a concept definition. */
20235 tree id = unpack_concept_check (function);
20236 tree tmpl = TREE_OPERAND (id, 0);
20237 tree args = TREE_OPERAND (id, 1);
20238
20239 /* Calls to standard and variable concepts should have been
20240 previously diagnosed. */
20241 gcc_assert (function_concept_p (tmpl));
20242
20243 /* Ensure the result is wrapped as a call expression. */
20244 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20245 }
20246 else
20247 ret = finish_call_expr (function, &call_args,
20248 /*disallow_virtual=*/qualified_p,
20249 koenig_p,
20250 complain);
20251
20252 if (ret != error_mark_node)
20253 {
20254 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20255 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20256 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20257 if (op || ord || rev)
20258 {
20259 function = extract_call_expr (ret);
20260 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20261 CALL_EXPR_ORDERED_ARGS (function) = ord;
20262 CALL_EXPR_REVERSE_ARGS (function) = rev;
20263 }
20264 }
20265
20266 RETURN (ret);
20267 }
20268
20269 case COND_EXPR:
20270 {
20271 tree cond = RECUR (TREE_OPERAND (t, 0));
20272 cond = mark_rvalue_use (cond);
20273 tree folded_cond = fold_non_dependent_expr (cond, complain);
20274 tree exp1, exp2;
20275
20276 if (TREE_CODE (folded_cond) == INTEGER_CST)
20277 {
20278 if (integer_zerop (folded_cond))
20279 {
20280 ++c_inhibit_evaluation_warnings;
20281 exp1 = RECUR (TREE_OPERAND (t, 1));
20282 --c_inhibit_evaluation_warnings;
20283 exp2 = RECUR (TREE_OPERAND (t, 2));
20284 }
20285 else
20286 {
20287 exp1 = RECUR (TREE_OPERAND (t, 1));
20288 ++c_inhibit_evaluation_warnings;
20289 exp2 = RECUR (TREE_OPERAND (t, 2));
20290 --c_inhibit_evaluation_warnings;
20291 }
20292 cond = folded_cond;
20293 }
20294 else
20295 {
20296 exp1 = RECUR (TREE_OPERAND (t, 1));
20297 exp2 = RECUR (TREE_OPERAND (t, 2));
20298 }
20299
20300 warning_sentinel s(warn_duplicated_branches);
20301 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20302 cond, exp1, exp2, complain));
20303 }
20304
20305 case PSEUDO_DTOR_EXPR:
20306 {
20307 tree op0 = RECUR (TREE_OPERAND (t, 0));
20308 tree op1 = RECUR (TREE_OPERAND (t, 1));
20309 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20310 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20311 input_location));
20312 }
20313
20314 case TREE_LIST:
20315 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20316
20317 case COMPONENT_REF:
20318 {
20319 tree object;
20320 tree object_type;
20321 tree member;
20322 tree r;
20323
20324 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20325 args, complain, in_decl);
20326 /* Remember that there was a reference to this entity. */
20327 if (DECL_P (object)
20328 && !mark_used (object, complain) && !(complain & tf_error))
20329 RETURN (error_mark_node);
20330 object_type = TREE_TYPE (object);
20331
20332 member = TREE_OPERAND (t, 1);
20333 if (BASELINK_P (member))
20334 member = tsubst_baselink (member,
20335 non_reference (TREE_TYPE (object)),
20336 args, complain, in_decl);
20337 else
20338 member = tsubst_copy (member, args, complain, in_decl);
20339 if (member == error_mark_node)
20340 RETURN (error_mark_node);
20341
20342 if (TREE_CODE (member) == FIELD_DECL)
20343 {
20344 r = finish_non_static_data_member (member, object, NULL_TREE);
20345 if (TREE_CODE (r) == COMPONENT_REF)
20346 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20347 RETURN (r);
20348 }
20349 else if (type_dependent_expression_p (object))
20350 /* We can't do much here. */;
20351 else if (!CLASS_TYPE_P (object_type))
20352 {
20353 if (scalarish_type_p (object_type))
20354 {
20355 tree s = NULL_TREE;
20356 tree dtor = member;
20357
20358 if (TREE_CODE (dtor) == SCOPE_REF)
20359 {
20360 s = TREE_OPERAND (dtor, 0);
20361 dtor = TREE_OPERAND (dtor, 1);
20362 }
20363 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20364 {
20365 dtor = TREE_OPERAND (dtor, 0);
20366 if (TYPE_P (dtor))
20367 RETURN (finish_pseudo_destructor_expr
20368 (object, s, dtor, input_location));
20369 }
20370 }
20371 }
20372 else if (TREE_CODE (member) == SCOPE_REF
20373 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20374 {
20375 /* Lookup the template functions now that we know what the
20376 scope is. */
20377 tree scope = TREE_OPERAND (member, 0);
20378 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20379 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20380 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20381 /*complain=*/false);
20382 if (BASELINK_P (member))
20383 {
20384 BASELINK_FUNCTIONS (member)
20385 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20386 args);
20387 member = (adjust_result_of_qualified_name_lookup
20388 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20389 object_type));
20390 }
20391 else
20392 {
20393 qualified_name_lookup_error (scope, tmpl, member,
20394 input_location);
20395 RETURN (error_mark_node);
20396 }
20397 }
20398 else if (TREE_CODE (member) == SCOPE_REF
20399 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20400 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20401 {
20402 if (complain & tf_error)
20403 {
20404 if (TYPE_P (TREE_OPERAND (member, 0)))
20405 error ("%qT is not a class or namespace",
20406 TREE_OPERAND (member, 0));
20407 else
20408 error ("%qD is not a class or namespace",
20409 TREE_OPERAND (member, 0));
20410 }
20411 RETURN (error_mark_node);
20412 }
20413
20414 r = finish_class_member_access_expr (object, member,
20415 /*template_p=*/false,
20416 complain);
20417 if (TREE_CODE (r) == COMPONENT_REF)
20418 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20419 RETURN (r);
20420 }
20421
20422 case THROW_EXPR:
20423 RETURN (build_throw
20424 (input_location, RECUR (TREE_OPERAND (t, 0))));
20425
20426 case CONSTRUCTOR:
20427 {
20428 vec<constructor_elt, va_gc> *n;
20429 constructor_elt *ce;
20430 unsigned HOST_WIDE_INT idx;
20431 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20432 bool process_index_p;
20433 int newlen;
20434 bool need_copy_p = false;
20435 tree r;
20436
20437 if (type == error_mark_node)
20438 RETURN (error_mark_node);
20439
20440 /* We do not want to process the index of aggregate
20441 initializers as they are identifier nodes which will be
20442 looked up by digest_init. */
20443 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20444
20445 if (null_member_pointer_value_p (t))
20446 {
20447 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20448 RETURN (t);
20449 }
20450
20451 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20452 newlen = vec_safe_length (n);
20453 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20454 {
20455 if (ce->index && process_index_p
20456 /* An identifier index is looked up in the type
20457 being initialized, not the current scope. */
20458 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20459 ce->index = RECUR (ce->index);
20460
20461 if (PACK_EXPANSION_P (ce->value))
20462 {
20463 /* Substitute into the pack expansion. */
20464 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20465 in_decl);
20466
20467 if (ce->value == error_mark_node
20468 || PACK_EXPANSION_P (ce->value))
20469 ;
20470 else if (TREE_VEC_LENGTH (ce->value) == 1)
20471 /* Just move the argument into place. */
20472 ce->value = TREE_VEC_ELT (ce->value, 0);
20473 else
20474 {
20475 /* Update the length of the final CONSTRUCTOR
20476 arguments vector, and note that we will need to
20477 copy.*/
20478 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20479 need_copy_p = true;
20480 }
20481 }
20482 else
20483 ce->value = RECUR (ce->value);
20484 }
20485
20486 if (need_copy_p)
20487 {
20488 vec<constructor_elt, va_gc> *old_n = n;
20489
20490 vec_alloc (n, newlen);
20491 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20492 {
20493 if (TREE_CODE (ce->value) == TREE_VEC)
20494 {
20495 int i, len = TREE_VEC_LENGTH (ce->value);
20496 for (i = 0; i < len; ++i)
20497 CONSTRUCTOR_APPEND_ELT (n, 0,
20498 TREE_VEC_ELT (ce->value, i));
20499 }
20500 else
20501 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20502 }
20503 }
20504
20505 r = build_constructor (init_list_type_node, n);
20506 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20507 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20508 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20509
20510 if (TREE_HAS_CONSTRUCTOR (t))
20511 {
20512 fcl_t cl = fcl_functional;
20513 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20514 cl = fcl_c99;
20515 RETURN (finish_compound_literal (type, r, complain, cl));
20516 }
20517
20518 TREE_TYPE (r) = type;
20519 RETURN (r);
20520 }
20521
20522 case TYPEID_EXPR:
20523 {
20524 tree operand_0 = TREE_OPERAND (t, 0);
20525 if (TYPE_P (operand_0))
20526 {
20527 operand_0 = tsubst (operand_0, args, complain, in_decl);
20528 RETURN (get_typeid (operand_0, complain));
20529 }
20530 else
20531 {
20532 operand_0 = RECUR (operand_0);
20533 RETURN (build_typeid (operand_0, complain));
20534 }
20535 }
20536
20537 case VAR_DECL:
20538 if (!args)
20539 RETURN (t);
20540 /* Fall through */
20541
20542 case PARM_DECL:
20543 {
20544 tree r = tsubst_copy (t, args, complain, in_decl);
20545 /* ??? We're doing a subset of finish_id_expression here. */
20546 if (tree wrap = maybe_get_tls_wrapper_call (r))
20547 /* Replace an evaluated use of the thread_local variable with
20548 a call to its wrapper. */
20549 r = wrap;
20550 else if (outer_automatic_var_p (r))
20551 r = process_outer_var_ref (r, complain);
20552
20553 if (!TYPE_REF_P (TREE_TYPE (t)))
20554 /* If the original type was a reference, we'll be wrapped in
20555 the appropriate INDIRECT_REF. */
20556 r = convert_from_reference (r);
20557 RETURN (r);
20558 }
20559
20560 case VA_ARG_EXPR:
20561 {
20562 tree op0 = RECUR (TREE_OPERAND (t, 0));
20563 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20564 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20565 }
20566
20567 case OFFSETOF_EXPR:
20568 {
20569 tree object_ptr
20570 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20571 in_decl, /*function_p=*/false,
20572 /*integral_constant_expression_p=*/false);
20573 RETURN (finish_offsetof (object_ptr,
20574 RECUR (TREE_OPERAND (t, 0)),
20575 EXPR_LOCATION (t)));
20576 }
20577
20578 case ADDRESSOF_EXPR:
20579 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20580 RECUR (TREE_OPERAND (t, 0)), complain));
20581
20582 case TRAIT_EXPR:
20583 {
20584 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20585 complain, in_decl);
20586 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20587 complain, in_decl);
20588 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20589 TRAIT_EXPR_KIND (t), type1, type2));
20590 }
20591
20592 case STMT_EXPR:
20593 {
20594 tree old_stmt_expr = cur_stmt_expr;
20595 tree stmt_expr = begin_stmt_expr ();
20596
20597 cur_stmt_expr = stmt_expr;
20598 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20599 integral_constant_expression_p);
20600 stmt_expr = finish_stmt_expr (stmt_expr, false);
20601 cur_stmt_expr = old_stmt_expr;
20602
20603 /* If the resulting list of expression statement is empty,
20604 fold it further into void_node. */
20605 if (empty_expr_stmt_p (stmt_expr))
20606 stmt_expr = void_node;
20607
20608 RETURN (stmt_expr);
20609 }
20610
20611 case LAMBDA_EXPR:
20612 {
20613 if (complain & tf_partial)
20614 {
20615 /* We don't have a full set of template arguments yet; don't touch
20616 the lambda at all. */
20617 gcc_assert (processing_template_decl);
20618 return t;
20619 }
20620 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20621
20622 RETURN (build_lambda_object (r));
20623 }
20624
20625 case TARGET_EXPR:
20626 /* We can get here for a constant initializer of non-dependent type.
20627 FIXME stop folding in cp_parser_initializer_clause. */
20628 {
20629 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20630 complain);
20631 RETURN (r);
20632 }
20633
20634 case TRANSACTION_EXPR:
20635 RETURN (tsubst_expr(t, args, complain, in_decl,
20636 integral_constant_expression_p));
20637
20638 case PAREN_EXPR:
20639 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20640
20641 case VEC_PERM_EXPR:
20642 {
20643 tree op0 = RECUR (TREE_OPERAND (t, 0));
20644 tree op1 = RECUR (TREE_OPERAND (t, 1));
20645 tree op2 = RECUR (TREE_OPERAND (t, 2));
20646 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20647 complain));
20648 }
20649
20650 case REQUIRES_EXPR:
20651 {
20652 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20653 RETURN (r);
20654 }
20655
20656 case RANGE_EXPR:
20657 /* No need to substitute further, a RANGE_EXPR will always be built
20658 with constant operands. */
20659 RETURN (t);
20660
20661 case NON_LVALUE_EXPR:
20662 case VIEW_CONVERT_EXPR:
20663 if (location_wrapper_p (t))
20664 /* We need to do this here as well as in tsubst_copy so we get the
20665 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20666 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20667 EXPR_LOCATION (t)));
20668 /* fallthrough. */
20669
20670 default:
20671 /* Handle Objective-C++ constructs, if appropriate. */
20672 {
20673 tree subst
20674 = objcp_tsubst_copy_and_build (t, args, complain,
20675 in_decl, /*function_p=*/false);
20676 if (subst)
20677 RETURN (subst);
20678 }
20679 RETURN (tsubst_copy (t, args, complain, in_decl));
20680 }
20681
20682 #undef RECUR
20683 #undef RETURN
20684 out:
20685 input_location = save_loc;
20686 return retval;
20687 }
20688
20689 /* Verify that the instantiated ARGS are valid. For type arguments,
20690 make sure that the type's linkage is ok. For non-type arguments,
20691 make sure they are constants if they are integral or enumerations.
20692 Emit an error under control of COMPLAIN, and return TRUE on error. */
20693
20694 static bool
20695 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20696 {
20697 if (dependent_template_arg_p (t))
20698 return false;
20699 if (ARGUMENT_PACK_P (t))
20700 {
20701 tree vec = ARGUMENT_PACK_ARGS (t);
20702 int len = TREE_VEC_LENGTH (vec);
20703 bool result = false;
20704 int i;
20705
20706 for (i = 0; i < len; ++i)
20707 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20708 result = true;
20709 return result;
20710 }
20711 else if (TYPE_P (t))
20712 {
20713 /* [basic.link]: A name with no linkage (notably, the name
20714 of a class or enumeration declared in a local scope)
20715 shall not be used to declare an entity with linkage.
20716 This implies that names with no linkage cannot be used as
20717 template arguments
20718
20719 DR 757 relaxes this restriction for C++0x. */
20720 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20721 : no_linkage_check (t, /*relaxed_p=*/false));
20722
20723 if (nt)
20724 {
20725 /* DR 488 makes use of a type with no linkage cause
20726 type deduction to fail. */
20727 if (complain & tf_error)
20728 {
20729 if (TYPE_UNNAMED_P (nt))
20730 error ("%qT is/uses unnamed type", t);
20731 else
20732 error ("template argument for %qD uses local type %qT",
20733 tmpl, t);
20734 }
20735 return true;
20736 }
20737 /* In order to avoid all sorts of complications, we do not
20738 allow variably-modified types as template arguments. */
20739 else if (variably_modified_type_p (t, NULL_TREE))
20740 {
20741 if (complain & tf_error)
20742 error ("%qT is a variably modified type", t);
20743 return true;
20744 }
20745 }
20746 /* Class template and alias template arguments should be OK. */
20747 else if (DECL_TYPE_TEMPLATE_P (t))
20748 ;
20749 /* A non-type argument of integral or enumerated type must be a
20750 constant. */
20751 else if (TREE_TYPE (t)
20752 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20753 && !REFERENCE_REF_P (t)
20754 && !TREE_CONSTANT (t))
20755 {
20756 if (complain & tf_error)
20757 error ("integral expression %qE is not constant", t);
20758 return true;
20759 }
20760 return false;
20761 }
20762
20763 static bool
20764 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20765 {
20766 int ix, len = DECL_NTPARMS (tmpl);
20767 bool result = false;
20768
20769 for (ix = 0; ix != len; ix++)
20770 {
20771 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20772 result = true;
20773 }
20774 if (result && (complain & tf_error))
20775 error (" trying to instantiate %qD", tmpl);
20776 return result;
20777 }
20778
20779 /* We're out of SFINAE context now, so generate diagnostics for the access
20780 errors we saw earlier when instantiating D from TMPL and ARGS. */
20781
20782 static void
20783 recheck_decl_substitution (tree d, tree tmpl, tree args)
20784 {
20785 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20786 tree type = TREE_TYPE (pattern);
20787 location_t loc = input_location;
20788
20789 push_access_scope (d);
20790 push_deferring_access_checks (dk_no_deferred);
20791 input_location = DECL_SOURCE_LOCATION (pattern);
20792 tsubst (type, args, tf_warning_or_error, d);
20793 input_location = loc;
20794 pop_deferring_access_checks ();
20795 pop_access_scope (d);
20796 }
20797
20798 /* Instantiate the indicated variable, function, or alias template TMPL with
20799 the template arguments in TARG_PTR. */
20800
20801 static tree
20802 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20803 {
20804 tree targ_ptr = orig_args;
20805 tree fndecl;
20806 tree gen_tmpl;
20807 tree spec;
20808 bool access_ok = true;
20809
20810 if (tmpl == error_mark_node)
20811 return error_mark_node;
20812
20813 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20814
20815 /* If this function is a clone, handle it specially. */
20816 if (DECL_CLONED_FUNCTION_P (tmpl))
20817 {
20818 tree spec;
20819 tree clone;
20820
20821 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20822 DECL_CLONED_FUNCTION. */
20823 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20824 targ_ptr, complain);
20825 if (spec == error_mark_node)
20826 return error_mark_node;
20827
20828 /* Look for the clone. */
20829 FOR_EACH_CLONE (clone, spec)
20830 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20831 return clone;
20832 /* We should always have found the clone by now. */
20833 gcc_unreachable ();
20834 return NULL_TREE;
20835 }
20836
20837 if (targ_ptr == error_mark_node)
20838 return error_mark_node;
20839
20840 /* Check to see if we already have this specialization. */
20841 gen_tmpl = most_general_template (tmpl);
20842 if (TMPL_ARGS_DEPTH (targ_ptr)
20843 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20844 /* targ_ptr only has the innermost template args, so add the outer ones
20845 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20846 the case of a non-dependent call within a template definition). */
20847 targ_ptr = (add_outermost_template_args
20848 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20849 targ_ptr));
20850
20851 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20852 but it doesn't seem to be on the hot path. */
20853 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20854
20855 gcc_checking_assert (tmpl == gen_tmpl
20856 || ((fndecl
20857 = retrieve_specialization (tmpl, orig_args, 0))
20858 == spec)
20859 || fndecl == NULL_TREE);
20860
20861 if (spec != NULL_TREE)
20862 {
20863 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20864 {
20865 if (complain & tf_error)
20866 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20867 return error_mark_node;
20868 }
20869 return spec;
20870 }
20871
20872 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20873 complain))
20874 return error_mark_node;
20875
20876 /* We are building a FUNCTION_DECL, during which the access of its
20877 parameters and return types have to be checked. However this
20878 FUNCTION_DECL which is the desired context for access checking
20879 is not built yet. We solve this chicken-and-egg problem by
20880 deferring all checks until we have the FUNCTION_DECL. */
20881 push_deferring_access_checks (dk_deferred);
20882
20883 /* Instantiation of the function happens in the context of the function
20884 template, not the context of the overload resolution we're doing. */
20885 push_to_top_level ();
20886 /* If there are dependent arguments, e.g. because we're doing partial
20887 ordering, make sure processing_template_decl stays set. */
20888 if (uses_template_parms (targ_ptr))
20889 ++processing_template_decl;
20890 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20891 {
20892 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20893 complain, gen_tmpl, true);
20894 push_nested_class (ctx);
20895 }
20896
20897 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20898
20899 fndecl = NULL_TREE;
20900 if (VAR_P (pattern))
20901 {
20902 /* We need to determine if we're using a partial or explicit
20903 specialization now, because the type of the variable could be
20904 different. */
20905 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20906 tree elt = most_specialized_partial_spec (tid, complain);
20907 if (elt == error_mark_node)
20908 pattern = error_mark_node;
20909 else if (elt)
20910 {
20911 tree partial_tmpl = TREE_VALUE (elt);
20912 tree partial_args = TREE_PURPOSE (elt);
20913 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20914 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20915 }
20916 }
20917
20918 /* Substitute template parameters to obtain the specialization. */
20919 if (fndecl == NULL_TREE)
20920 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20921 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20922 pop_nested_class ();
20923 pop_from_top_level ();
20924
20925 if (fndecl == error_mark_node)
20926 {
20927 pop_deferring_access_checks ();
20928 return error_mark_node;
20929 }
20930
20931 /* The DECL_TI_TEMPLATE should always be the immediate parent
20932 template, not the most general template. */
20933 DECL_TI_TEMPLATE (fndecl) = tmpl;
20934 DECL_TI_ARGS (fndecl) = targ_ptr;
20935
20936 /* Now we know the specialization, compute access previously
20937 deferred. Do no access control for inheriting constructors,
20938 as we already checked access for the inherited constructor. */
20939 if (!(flag_new_inheriting_ctors
20940 && DECL_INHERITED_CTOR (fndecl)))
20941 {
20942 push_access_scope (fndecl);
20943 if (!perform_deferred_access_checks (complain))
20944 access_ok = false;
20945 pop_access_scope (fndecl);
20946 }
20947 pop_deferring_access_checks ();
20948
20949 /* If we've just instantiated the main entry point for a function,
20950 instantiate all the alternate entry points as well. We do this
20951 by cloning the instantiation of the main entry point, not by
20952 instantiating the template clones. */
20953 if (tree chain = DECL_CHAIN (gen_tmpl))
20954 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20955 clone_cdtor (fndecl, /*update_methods=*/false);
20956
20957 if (!access_ok)
20958 {
20959 if (!(complain & tf_error))
20960 {
20961 /* Remember to reinstantiate when we're out of SFINAE so the user
20962 can see the errors. */
20963 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20964 }
20965 return error_mark_node;
20966 }
20967 return fndecl;
20968 }
20969
20970 /* Wrapper for instantiate_template_1. */
20971
20972 tree
20973 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20974 {
20975 tree ret;
20976 timevar_push (TV_TEMPLATE_INST);
20977 ret = instantiate_template_1 (tmpl, orig_args, complain);
20978 timevar_pop (TV_TEMPLATE_INST);
20979 return ret;
20980 }
20981
20982 /* Instantiate the alias template TMPL with ARGS. Also push a template
20983 instantiation level, which instantiate_template doesn't do because
20984 functions and variables have sufficient context established by the
20985 callers. */
20986
20987 static tree
20988 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20989 {
20990 if (tmpl == error_mark_node || args == error_mark_node)
20991 return error_mark_node;
20992
20993 args =
20994 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20995 args, tmpl, complain,
20996 /*require_all_args=*/true,
20997 /*use_default_args=*/true);
20998
20999 /* FIXME check for satisfaction in check_instantiated_args. */
21000 if (flag_concepts
21001 && !any_dependent_template_arguments_p (args)
21002 && !constraints_satisfied_p (tmpl, args))
21003 {
21004 if (complain & tf_error)
21005 {
21006 auto_diagnostic_group d;
21007 error ("template constraint failure for %qD", tmpl);
21008 diagnose_constraints (input_location, tmpl, args);
21009 }
21010 return error_mark_node;
21011 }
21012
21013 if (!push_tinst_level (tmpl, args))
21014 return error_mark_node;
21015 tree r = instantiate_template (tmpl, args, complain);
21016 pop_tinst_level ();
21017
21018 return r;
21019 }
21020
21021 /* PARM is a template parameter pack for FN. Returns true iff
21022 PARM is used in a deducible way in the argument list of FN. */
21023
21024 static bool
21025 pack_deducible_p (tree parm, tree fn)
21026 {
21027 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
21028 for (; t; t = TREE_CHAIN (t))
21029 {
21030 tree type = TREE_VALUE (t);
21031 tree packs;
21032 if (!PACK_EXPANSION_P (type))
21033 continue;
21034 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
21035 packs; packs = TREE_CHAIN (packs))
21036 if (template_args_equal (TREE_VALUE (packs), parm))
21037 {
21038 /* The template parameter pack is used in a function parameter
21039 pack. If this is the end of the parameter list, the
21040 template parameter pack is deducible. */
21041 if (TREE_CHAIN (t) == void_list_node)
21042 return true;
21043 else
21044 /* Otherwise, not. Well, it could be deduced from
21045 a non-pack parameter, but doing so would end up with
21046 a deduction mismatch, so don't bother. */
21047 return false;
21048 }
21049 }
21050 /* The template parameter pack isn't used in any function parameter
21051 packs, but it might be used deeper, e.g. tuple<Args...>. */
21052 return true;
21053 }
21054
21055 /* Subroutine of fn_type_unification: check non-dependent parms for
21056 convertibility. */
21057
21058 static int
21059 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
21060 tree fn, unification_kind_t strict, int flags,
21061 struct conversion **convs, bool explain_p)
21062 {
21063 /* Non-constructor methods need to leave a conversion for 'this', which
21064 isn't included in nargs here. */
21065 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21066 && !DECL_CONSTRUCTOR_P (fn));
21067
21068 for (unsigned ia = 0;
21069 parms && parms != void_list_node && ia < nargs; )
21070 {
21071 tree parm = TREE_VALUE (parms);
21072
21073 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21074 && (!TREE_CHAIN (parms)
21075 || TREE_CHAIN (parms) == void_list_node))
21076 /* For a function parameter pack that occurs at the end of the
21077 parameter-declaration-list, the type A of each remaining
21078 argument of the call is compared with the type P of the
21079 declarator-id of the function parameter pack. */
21080 break;
21081
21082 parms = TREE_CHAIN (parms);
21083
21084 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21085 /* For a function parameter pack that does not occur at the
21086 end of the parameter-declaration-list, the type of the
21087 parameter pack is a non-deduced context. */
21088 continue;
21089
21090 if (!uses_template_parms (parm))
21091 {
21092 tree arg = args[ia];
21093 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21094 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21095
21096 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21097 conv_p, explain_p))
21098 return 1;
21099 }
21100
21101 ++ia;
21102 }
21103
21104 return 0;
21105 }
21106
21107 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21108 NARGS elements of the arguments that are being used when calling
21109 it. TARGS is a vector into which the deduced template arguments
21110 are placed.
21111
21112 Returns either a FUNCTION_DECL for the matching specialization of FN or
21113 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21114 true, diagnostics will be printed to explain why it failed.
21115
21116 If FN is a conversion operator, or we are trying to produce a specific
21117 specialization, RETURN_TYPE is the return type desired.
21118
21119 The EXPLICIT_TARGS are explicit template arguments provided via a
21120 template-id.
21121
21122 The parameter STRICT is one of:
21123
21124 DEDUCE_CALL:
21125 We are deducing arguments for a function call, as in
21126 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21127 deducing arguments for a call to the result of a conversion
21128 function template, as in [over.call.object].
21129
21130 DEDUCE_CONV:
21131 We are deducing arguments for a conversion function, as in
21132 [temp.deduct.conv].
21133
21134 DEDUCE_EXACT:
21135 We are deducing arguments when doing an explicit instantiation
21136 as in [temp.explicit], when determining an explicit specialization
21137 as in [temp.expl.spec], or when taking the address of a function
21138 template, as in [temp.deduct.funcaddr]. */
21139
21140 tree
21141 fn_type_unification (tree fn,
21142 tree explicit_targs,
21143 tree targs,
21144 const tree *args,
21145 unsigned int nargs,
21146 tree return_type,
21147 unification_kind_t strict,
21148 int flags,
21149 struct conversion **convs,
21150 bool explain_p,
21151 bool decltype_p)
21152 {
21153 tree parms;
21154 tree fntype;
21155 tree decl = NULL_TREE;
21156 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21157 bool ok;
21158 static int deduction_depth;
21159 /* type_unification_real will pass back any access checks from default
21160 template argument substitution. */
21161 vec<deferred_access_check, va_gc> *checks = NULL;
21162 /* We don't have all the template args yet. */
21163 bool incomplete = true;
21164
21165 tree orig_fn = fn;
21166 if (flag_new_inheriting_ctors)
21167 fn = strip_inheriting_ctors (fn);
21168
21169 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21170 tree r = error_mark_node;
21171
21172 tree full_targs = targs;
21173 if (TMPL_ARGS_DEPTH (targs)
21174 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21175 full_targs = (add_outermost_template_args
21176 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21177 targs));
21178
21179 if (decltype_p)
21180 complain |= tf_decltype;
21181
21182 /* In C++0x, it's possible to have a function template whose type depends
21183 on itself recursively. This is most obvious with decltype, but can also
21184 occur with enumeration scope (c++/48969). So we need to catch infinite
21185 recursion and reject the substitution at deduction time; this function
21186 will return error_mark_node for any repeated substitution.
21187
21188 This also catches excessive recursion such as when f<N> depends on
21189 f<N-1> across all integers, and returns error_mark_node for all the
21190 substitutions back up to the initial one.
21191
21192 This is, of course, not reentrant. */
21193 if (excessive_deduction_depth)
21194 return error_mark_node;
21195 ++deduction_depth;
21196
21197 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21198
21199 fntype = TREE_TYPE (fn);
21200 if (explicit_targs)
21201 {
21202 /* [temp.deduct]
21203
21204 The specified template arguments must match the template
21205 parameters in kind (i.e., type, nontype, template), and there
21206 must not be more arguments than there are parameters;
21207 otherwise type deduction fails.
21208
21209 Nontype arguments must match the types of the corresponding
21210 nontype template parameters, or must be convertible to the
21211 types of the corresponding nontype parameters as specified in
21212 _temp.arg.nontype_, otherwise type deduction fails.
21213
21214 All references in the function type of the function template
21215 to the corresponding template parameters are replaced by the
21216 specified template argument values. If a substitution in a
21217 template parameter or in the function type of the function
21218 template results in an invalid type, type deduction fails. */
21219 int i, len = TREE_VEC_LENGTH (tparms);
21220 location_t loc = input_location;
21221 incomplete = false;
21222
21223 if (explicit_targs == error_mark_node)
21224 goto fail;
21225
21226 if (TMPL_ARGS_DEPTH (explicit_targs)
21227 < TMPL_ARGS_DEPTH (full_targs))
21228 explicit_targs = add_outermost_template_args (full_targs,
21229 explicit_targs);
21230
21231 /* Adjust any explicit template arguments before entering the
21232 substitution context. */
21233 explicit_targs
21234 = (coerce_template_parms (tparms, explicit_targs, fn,
21235 complain|tf_partial,
21236 /*require_all_args=*/false,
21237 /*use_default_args=*/false));
21238 if (explicit_targs == error_mark_node)
21239 goto fail;
21240
21241 /* Substitute the explicit args into the function type. This is
21242 necessary so that, for instance, explicitly declared function
21243 arguments can match null pointed constants. If we were given
21244 an incomplete set of explicit args, we must not do semantic
21245 processing during substitution as we could create partial
21246 instantiations. */
21247 for (i = 0; i < len; i++)
21248 {
21249 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21250 bool parameter_pack = false;
21251 tree targ = TREE_VEC_ELT (explicit_targs, i);
21252
21253 /* Dig out the actual parm. */
21254 if (TREE_CODE (parm) == TYPE_DECL
21255 || TREE_CODE (parm) == TEMPLATE_DECL)
21256 {
21257 parm = TREE_TYPE (parm);
21258 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21259 }
21260 else if (TREE_CODE (parm) == PARM_DECL)
21261 {
21262 parm = DECL_INITIAL (parm);
21263 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21264 }
21265
21266 if (targ == NULL_TREE)
21267 /* No explicit argument for this template parameter. */
21268 incomplete = true;
21269 else if (parameter_pack && pack_deducible_p (parm, fn))
21270 {
21271 /* Mark the argument pack as "incomplete". We could
21272 still deduce more arguments during unification.
21273 We remove this mark in type_unification_real. */
21274 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21275 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21276 = ARGUMENT_PACK_ARGS (targ);
21277
21278 /* We have some incomplete argument packs. */
21279 incomplete = true;
21280 }
21281 }
21282
21283 if (incomplete)
21284 {
21285 if (!push_tinst_level (fn, explicit_targs))
21286 {
21287 excessive_deduction_depth = true;
21288 goto fail;
21289 }
21290 ++processing_template_decl;
21291 input_location = DECL_SOURCE_LOCATION (fn);
21292 /* Ignore any access checks; we'll see them again in
21293 instantiate_template and they might have the wrong
21294 access path at this point. */
21295 push_deferring_access_checks (dk_deferred);
21296 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21297 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21298 pop_deferring_access_checks ();
21299 input_location = loc;
21300 --processing_template_decl;
21301 pop_tinst_level ();
21302
21303 if (fntype == error_mark_node)
21304 goto fail;
21305 }
21306
21307 /* Place the explicitly specified arguments in TARGS. */
21308 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21309 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21310 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21311 if (!incomplete && CHECKING_P
21312 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21313 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21314 (targs, NUM_TMPL_ARGS (explicit_targs));
21315 }
21316
21317 if (return_type && strict != DEDUCE_CALL)
21318 {
21319 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21320 new_args[0] = return_type;
21321 memcpy (new_args + 1, args, nargs * sizeof (tree));
21322 args = new_args;
21323 ++nargs;
21324 }
21325
21326 if (!incomplete)
21327 goto deduced;
21328
21329 /* Never do unification on the 'this' parameter. */
21330 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21331
21332 if (return_type && strict == DEDUCE_CALL)
21333 {
21334 /* We're deducing for a call to the result of a template conversion
21335 function. The parms we really want are in return_type. */
21336 if (INDIRECT_TYPE_P (return_type))
21337 return_type = TREE_TYPE (return_type);
21338 parms = TYPE_ARG_TYPES (return_type);
21339 }
21340 else if (return_type)
21341 {
21342 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21343 }
21344
21345 /* We allow incomplete unification without an error message here
21346 because the standard doesn't seem to explicitly prohibit it. Our
21347 callers must be ready to deal with unification failures in any
21348 event. */
21349
21350 /* If we aren't explaining yet, push tinst context so we can see where
21351 any errors (e.g. from class instantiations triggered by instantiation
21352 of default template arguments) come from. If we are explaining, this
21353 context is redundant. */
21354 if (!explain_p && !push_tinst_level (fn, targs))
21355 {
21356 excessive_deduction_depth = true;
21357 goto fail;
21358 }
21359
21360 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21361 full_targs, parms, args, nargs, /*subr=*/0,
21362 strict, &checks, explain_p);
21363 if (!explain_p)
21364 pop_tinst_level ();
21365 if (!ok)
21366 goto fail;
21367
21368 /* Now that we have bindings for all of the template arguments,
21369 ensure that the arguments deduced for the template template
21370 parameters have compatible template parameter lists. We cannot
21371 check this property before we have deduced all template
21372 arguments, because the template parameter types of a template
21373 template parameter might depend on prior template parameters
21374 deduced after the template template parameter. The following
21375 ill-formed example illustrates this issue:
21376
21377 template<typename T, template<T> class C> void f(C<5>, T);
21378
21379 template<int N> struct X {};
21380
21381 void g() {
21382 f(X<5>(), 5l); // error: template argument deduction fails
21383 }
21384
21385 The template parameter list of 'C' depends on the template type
21386 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21387 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21388 time that we deduce 'C'. */
21389 if (!template_template_parm_bindings_ok_p
21390 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21391 {
21392 unify_inconsistent_template_template_parameters (explain_p);
21393 goto fail;
21394 }
21395
21396 deduced:
21397
21398 /* CWG2369: Check satisfaction before non-deducible conversions. */
21399 if (!constraints_satisfied_p (fn, targs))
21400 {
21401 if (explain_p)
21402 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21403 goto fail;
21404 }
21405
21406 /* DR 1391: All parameters have args, now check non-dependent parms for
21407 convertibility. We don't do this if all args were explicitly specified,
21408 as the standard says that we substitute explicit args immediately. */
21409 if (incomplete
21410 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21411 convs, explain_p))
21412 goto fail;
21413
21414 /* All is well so far. Now, check:
21415
21416 [temp.deduct]
21417
21418 When all template arguments have been deduced, all uses of
21419 template parameters in nondeduced contexts are replaced with
21420 the corresponding deduced argument values. If the
21421 substitution results in an invalid type, as described above,
21422 type deduction fails. */
21423 if (!push_tinst_level (fn, targs))
21424 {
21425 excessive_deduction_depth = true;
21426 goto fail;
21427 }
21428
21429 /* Also collect access checks from the instantiation. */
21430 reopen_deferring_access_checks (checks);
21431
21432 decl = instantiate_template (fn, targs, complain);
21433
21434 checks = get_deferred_access_checks ();
21435 pop_deferring_access_checks ();
21436
21437 pop_tinst_level ();
21438
21439 if (decl == error_mark_node)
21440 goto fail;
21441
21442 /* Now perform any access checks encountered during substitution. */
21443 push_access_scope (decl);
21444 ok = perform_access_checks (checks, complain);
21445 pop_access_scope (decl);
21446 if (!ok)
21447 goto fail;
21448
21449 /* If we're looking for an exact match, check that what we got
21450 is indeed an exact match. It might not be if some template
21451 parameters are used in non-deduced contexts. But don't check
21452 for an exact match if we have dependent template arguments;
21453 in that case we're doing partial ordering, and we already know
21454 that we have two candidates that will provide the actual type. */
21455 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21456 {
21457 tree substed = TREE_TYPE (decl);
21458 unsigned int i;
21459
21460 tree sarg
21461 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21462 if (return_type)
21463 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21464 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21465 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21466 {
21467 unify_type_mismatch (explain_p, args[i],
21468 TREE_VALUE (sarg));
21469 goto fail;
21470 }
21471 }
21472
21473 /* After doing deduction with the inherited constructor, actually return an
21474 instantiation of the inheriting constructor. */
21475 if (orig_fn != fn)
21476 decl = instantiate_template (orig_fn, targs, complain);
21477
21478 r = decl;
21479
21480 fail:
21481 --deduction_depth;
21482 if (excessive_deduction_depth)
21483 {
21484 if (deduction_depth == 0)
21485 /* Reset once we're all the way out. */
21486 excessive_deduction_depth = false;
21487 }
21488
21489 return r;
21490 }
21491
21492 /* Adjust types before performing type deduction, as described in
21493 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21494 sections are symmetric. PARM is the type of a function parameter
21495 or the return type of the conversion function. ARG is the type of
21496 the argument passed to the call, or the type of the value
21497 initialized with the result of the conversion function.
21498 ARG_EXPR is the original argument expression, which may be null. */
21499
21500 static int
21501 maybe_adjust_types_for_deduction (unification_kind_t strict,
21502 tree* parm,
21503 tree* arg,
21504 tree arg_expr)
21505 {
21506 int result = 0;
21507
21508 switch (strict)
21509 {
21510 case DEDUCE_CALL:
21511 break;
21512
21513 case DEDUCE_CONV:
21514 /* Swap PARM and ARG throughout the remainder of this
21515 function; the handling is precisely symmetric since PARM
21516 will initialize ARG rather than vice versa. */
21517 std::swap (parm, arg);
21518 break;
21519
21520 case DEDUCE_EXACT:
21521 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21522 too, but here handle it by stripping the reference from PARM
21523 rather than by adding it to ARG. */
21524 if (TYPE_REF_P (*parm)
21525 && TYPE_REF_IS_RVALUE (*parm)
21526 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21527 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21528 && TYPE_REF_P (*arg)
21529 && !TYPE_REF_IS_RVALUE (*arg))
21530 *parm = TREE_TYPE (*parm);
21531 /* Nothing else to do in this case. */
21532 return 0;
21533
21534 default:
21535 gcc_unreachable ();
21536 }
21537
21538 if (!TYPE_REF_P (*parm))
21539 {
21540 /* [temp.deduct.call]
21541
21542 If P is not a reference type:
21543
21544 --If A is an array type, the pointer type produced by the
21545 array-to-pointer standard conversion (_conv.array_) is
21546 used in place of A for type deduction; otherwise,
21547
21548 --If A is a function type, the pointer type produced by
21549 the function-to-pointer standard conversion
21550 (_conv.func_) is used in place of A for type deduction;
21551 otherwise,
21552
21553 --If A is a cv-qualified type, the top level
21554 cv-qualifiers of A's type are ignored for type
21555 deduction. */
21556 if (TREE_CODE (*arg) == ARRAY_TYPE)
21557 *arg = build_pointer_type (TREE_TYPE (*arg));
21558 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21559 *arg = build_pointer_type (*arg);
21560 else
21561 *arg = TYPE_MAIN_VARIANT (*arg);
21562 }
21563
21564 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21565 reference to a cv-unqualified template parameter that does not represent a
21566 template parameter of a class template (during class template argument
21567 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21568 an lvalue, the type "lvalue reference to A" is used in place of A for type
21569 deduction. */
21570 if (TYPE_REF_P (*parm)
21571 && TYPE_REF_IS_RVALUE (*parm)
21572 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21573 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21574 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21575 && (arg_expr ? lvalue_p (arg_expr)
21576 /* try_one_overload doesn't provide an arg_expr, but
21577 functions are always lvalues. */
21578 : TREE_CODE (*arg) == FUNCTION_TYPE))
21579 *arg = build_reference_type (*arg);
21580
21581 /* [temp.deduct.call]
21582
21583 If P is a cv-qualified type, the top level cv-qualifiers
21584 of P's type are ignored for type deduction. If P is a
21585 reference type, the type referred to by P is used for
21586 type deduction. */
21587 *parm = TYPE_MAIN_VARIANT (*parm);
21588 if (TYPE_REF_P (*parm))
21589 {
21590 *parm = TREE_TYPE (*parm);
21591 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21592 }
21593
21594 /* DR 322. For conversion deduction, remove a reference type on parm
21595 too (which has been swapped into ARG). */
21596 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21597 *arg = TREE_TYPE (*arg);
21598
21599 return result;
21600 }
21601
21602 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21603 template which doesn't contain any deducible template parameters; check if
21604 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21605 unify_one_argument. */
21606
21607 static int
21608 check_non_deducible_conversion (tree parm, tree arg, int strict,
21609 int flags, struct conversion **conv_p,
21610 bool explain_p)
21611 {
21612 tree type;
21613
21614 if (!TYPE_P (arg))
21615 type = TREE_TYPE (arg);
21616 else
21617 type = arg;
21618
21619 if (same_type_p (parm, type))
21620 return unify_success (explain_p);
21621
21622 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21623 if (strict == DEDUCE_CONV)
21624 {
21625 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21626 return unify_success (explain_p);
21627 }
21628 else if (strict != DEDUCE_EXACT)
21629 {
21630 bool ok = false;
21631 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21632 if (conv_p)
21633 /* Avoid recalculating this in add_function_candidate. */
21634 ok = (*conv_p
21635 = good_conversion (parm, type, conv_arg, flags, complain));
21636 else
21637 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21638 if (ok)
21639 return unify_success (explain_p);
21640 }
21641
21642 if (strict == DEDUCE_EXACT)
21643 return unify_type_mismatch (explain_p, parm, arg);
21644 else
21645 return unify_arg_conversion (explain_p, parm, type, arg);
21646 }
21647
21648 static bool uses_deducible_template_parms (tree type);
21649
21650 /* Returns true iff the expression EXPR is one from which a template
21651 argument can be deduced. In other words, if it's an undecorated
21652 use of a template non-type parameter. */
21653
21654 static bool
21655 deducible_expression (tree expr)
21656 {
21657 /* Strip implicit conversions. */
21658 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21659 expr = TREE_OPERAND (expr, 0);
21660 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21661 }
21662
21663 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21664 deducible way; that is, if it has a max value of <PARM> - 1. */
21665
21666 static bool
21667 deducible_array_bound (tree domain)
21668 {
21669 if (domain == NULL_TREE)
21670 return false;
21671
21672 tree max = TYPE_MAX_VALUE (domain);
21673 if (TREE_CODE (max) != MINUS_EXPR)
21674 return false;
21675
21676 return deducible_expression (TREE_OPERAND (max, 0));
21677 }
21678
21679 /* Returns true iff the template arguments ARGS use a template parameter
21680 in a deducible way. */
21681
21682 static bool
21683 deducible_template_args (tree args)
21684 {
21685 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21686 {
21687 bool deducible;
21688 tree elt = TREE_VEC_ELT (args, i);
21689 if (ARGUMENT_PACK_P (elt))
21690 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21691 else
21692 {
21693 if (PACK_EXPANSION_P (elt))
21694 elt = PACK_EXPANSION_PATTERN (elt);
21695 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21696 deducible = true;
21697 else if (TYPE_P (elt))
21698 deducible = uses_deducible_template_parms (elt);
21699 else
21700 deducible = deducible_expression (elt);
21701 }
21702 if (deducible)
21703 return true;
21704 }
21705 return false;
21706 }
21707
21708 /* Returns true iff TYPE contains any deducible references to template
21709 parameters, as per 14.8.2.5. */
21710
21711 static bool
21712 uses_deducible_template_parms (tree type)
21713 {
21714 if (PACK_EXPANSION_P (type))
21715 type = PACK_EXPANSION_PATTERN (type);
21716
21717 /* T
21718 cv-list T
21719 TT<T>
21720 TT<i>
21721 TT<> */
21722 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21723 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21724 return true;
21725
21726 /* T*
21727 T&
21728 T&& */
21729 if (INDIRECT_TYPE_P (type))
21730 return uses_deducible_template_parms (TREE_TYPE (type));
21731
21732 /* T[integer-constant ]
21733 type [i] */
21734 if (TREE_CODE (type) == ARRAY_TYPE)
21735 return (uses_deducible_template_parms (TREE_TYPE (type))
21736 || deducible_array_bound (TYPE_DOMAIN (type)));
21737
21738 /* T type ::*
21739 type T::*
21740 T T::*
21741 T (type ::*)()
21742 type (T::*)()
21743 type (type ::*)(T)
21744 type (T::*)(T)
21745 T (type ::*)(T)
21746 T (T::*)()
21747 T (T::*)(T) */
21748 if (TYPE_PTRMEM_P (type))
21749 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21750 || (uses_deducible_template_parms
21751 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21752
21753 /* template-name <T> (where template-name refers to a class template)
21754 template-name <i> (where template-name refers to a class template) */
21755 if (CLASS_TYPE_P (type)
21756 && CLASSTYPE_TEMPLATE_INFO (type)
21757 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21758 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21759 (CLASSTYPE_TI_ARGS (type)));
21760
21761 /* type (T)
21762 T()
21763 T(T) */
21764 if (FUNC_OR_METHOD_TYPE_P (type))
21765 {
21766 if (uses_deducible_template_parms (TREE_TYPE (type)))
21767 return true;
21768 tree parm = TYPE_ARG_TYPES (type);
21769 if (TREE_CODE (type) == METHOD_TYPE)
21770 parm = TREE_CHAIN (parm);
21771 for (; parm; parm = TREE_CHAIN (parm))
21772 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21773 return true;
21774 }
21775
21776 return false;
21777 }
21778
21779 /* Subroutine of type_unification_real and unify_pack_expansion to
21780 handle unification of a single P/A pair. Parameters are as
21781 for those functions. */
21782
21783 static int
21784 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21785 int subr, unification_kind_t strict,
21786 bool explain_p)
21787 {
21788 tree arg_expr = NULL_TREE;
21789 int arg_strict;
21790
21791 if (arg == error_mark_node || parm == error_mark_node)
21792 return unify_invalid (explain_p);
21793 if (arg == unknown_type_node)
21794 /* We can't deduce anything from this, but we might get all the
21795 template args from other function args. */
21796 return unify_success (explain_p);
21797
21798 /* Implicit conversions (Clause 4) will be performed on a function
21799 argument to convert it to the type of the corresponding function
21800 parameter if the parameter type contains no template-parameters that
21801 participate in template argument deduction. */
21802 if (strict != DEDUCE_EXACT
21803 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21804 /* For function parameters with no deducible template parameters,
21805 just return. We'll check non-dependent conversions later. */
21806 return unify_success (explain_p);
21807
21808 switch (strict)
21809 {
21810 case DEDUCE_CALL:
21811 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21812 | UNIFY_ALLOW_MORE_CV_QUAL
21813 | UNIFY_ALLOW_DERIVED);
21814 break;
21815
21816 case DEDUCE_CONV:
21817 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21818 break;
21819
21820 case DEDUCE_EXACT:
21821 arg_strict = UNIFY_ALLOW_NONE;
21822 break;
21823
21824 default:
21825 gcc_unreachable ();
21826 }
21827
21828 /* We only do these transformations if this is the top-level
21829 parameter_type_list in a call or declaration matching; in other
21830 situations (nested function declarators, template argument lists) we
21831 won't be comparing a type to an expression, and we don't do any type
21832 adjustments. */
21833 if (!subr)
21834 {
21835 if (!TYPE_P (arg))
21836 {
21837 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21838 if (type_unknown_p (arg))
21839 {
21840 /* [temp.deduct.type] A template-argument can be
21841 deduced from a pointer to function or pointer
21842 to member function argument if the set of
21843 overloaded functions does not contain function
21844 templates and at most one of a set of
21845 overloaded functions provides a unique
21846 match. */
21847 resolve_overloaded_unification (tparms, targs, parm,
21848 arg, strict,
21849 arg_strict, explain_p);
21850 /* If a unique match was not found, this is a
21851 non-deduced context, so we still succeed. */
21852 return unify_success (explain_p);
21853 }
21854
21855 arg_expr = arg;
21856 arg = unlowered_expr_type (arg);
21857 if (arg == error_mark_node)
21858 return unify_invalid (explain_p);
21859 }
21860
21861 arg_strict |=
21862 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21863 }
21864 else
21865 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21866 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21867 return unify_template_argument_mismatch (explain_p, parm, arg);
21868
21869 /* For deduction from an init-list we need the actual list. */
21870 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21871 arg = arg_expr;
21872 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21873 }
21874
21875 /* for_each_template_parm callback that always returns 0. */
21876
21877 static int
21878 zero_r (tree, void *)
21879 {
21880 return 0;
21881 }
21882
21883 /* for_each_template_parm any_fn callback to handle deduction of a template
21884 type argument from the type of an array bound. */
21885
21886 static int
21887 array_deduction_r (tree t, void *data)
21888 {
21889 tree_pair_p d = (tree_pair_p)data;
21890 tree &tparms = d->purpose;
21891 tree &targs = d->value;
21892
21893 if (TREE_CODE (t) == ARRAY_TYPE)
21894 if (tree dom = TYPE_DOMAIN (t))
21895 if (tree max = TYPE_MAX_VALUE (dom))
21896 {
21897 if (TREE_CODE (max) == MINUS_EXPR)
21898 max = TREE_OPERAND (max, 0);
21899 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21900 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21901 UNIFY_ALLOW_NONE, /*explain*/false);
21902 }
21903
21904 /* Keep walking. */
21905 return 0;
21906 }
21907
21908 /* Try to deduce any not-yet-deduced template type arguments from the type of
21909 an array bound. This is handled separately from unify because 14.8.2.5 says
21910 "The type of a type parameter is only deduced from an array bound if it is
21911 not otherwise deduced." */
21912
21913 static void
21914 try_array_deduction (tree tparms, tree targs, tree parm)
21915 {
21916 tree_pair_s data = { tparms, targs };
21917 hash_set<tree> visited;
21918 for_each_template_parm (parm, zero_r, &data, &visited,
21919 /*nondeduced*/false, array_deduction_r);
21920 }
21921
21922 /* Most parms like fn_type_unification.
21923
21924 If SUBR is 1, we're being called recursively (to unify the
21925 arguments of a function or method parameter of a function
21926 template).
21927
21928 CHECKS is a pointer to a vector of access checks encountered while
21929 substituting default template arguments. */
21930
21931 static int
21932 type_unification_real (tree tparms,
21933 tree full_targs,
21934 tree xparms,
21935 const tree *xargs,
21936 unsigned int xnargs,
21937 int subr,
21938 unification_kind_t strict,
21939 vec<deferred_access_check, va_gc> **checks,
21940 bool explain_p)
21941 {
21942 tree parm, arg;
21943 int i;
21944 int ntparms = TREE_VEC_LENGTH (tparms);
21945 int saw_undeduced = 0;
21946 tree parms;
21947 const tree *args;
21948 unsigned int nargs;
21949 unsigned int ia;
21950
21951 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21952 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21953 gcc_assert (ntparms > 0);
21954
21955 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21956
21957 /* Reset the number of non-defaulted template arguments contained
21958 in TARGS. */
21959 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21960
21961 again:
21962 parms = xparms;
21963 args = xargs;
21964 nargs = xnargs;
21965
21966 ia = 0;
21967 while (parms && parms != void_list_node
21968 && ia < nargs)
21969 {
21970 parm = TREE_VALUE (parms);
21971
21972 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21973 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21974 /* For a function parameter pack that occurs at the end of the
21975 parameter-declaration-list, the type A of each remaining
21976 argument of the call is compared with the type P of the
21977 declarator-id of the function parameter pack. */
21978 break;
21979
21980 parms = TREE_CHAIN (parms);
21981
21982 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21983 /* For a function parameter pack that does not occur at the
21984 end of the parameter-declaration-list, the type of the
21985 parameter pack is a non-deduced context. */
21986 continue;
21987
21988 arg = args[ia];
21989 ++ia;
21990
21991 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21992 explain_p))
21993 return 1;
21994 }
21995
21996 if (parms
21997 && parms != void_list_node
21998 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21999 {
22000 /* Unify the remaining arguments with the pack expansion type. */
22001 tree argvec;
22002 tree parmvec = make_tree_vec (1);
22003
22004 /* Allocate a TREE_VEC and copy in all of the arguments */
22005 argvec = make_tree_vec (nargs - ia);
22006 for (i = 0; ia < nargs; ++ia, ++i)
22007 TREE_VEC_ELT (argvec, i) = args[ia];
22008
22009 /* Copy the parameter into parmvec. */
22010 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
22011 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
22012 /*subr=*/subr, explain_p))
22013 return 1;
22014
22015 /* Advance to the end of the list of parameters. */
22016 parms = TREE_CHAIN (parms);
22017 }
22018
22019 /* Fail if we've reached the end of the parm list, and more args
22020 are present, and the parm list isn't variadic. */
22021 if (ia < nargs && parms == void_list_node)
22022 return unify_too_many_arguments (explain_p, nargs, ia);
22023 /* Fail if parms are left and they don't have default values and
22024 they aren't all deduced as empty packs (c++/57397). This is
22025 consistent with sufficient_parms_p. */
22026 if (parms && parms != void_list_node
22027 && TREE_PURPOSE (parms) == NULL_TREE)
22028 {
22029 unsigned int count = nargs;
22030 tree p = parms;
22031 bool type_pack_p;
22032 do
22033 {
22034 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
22035 if (!type_pack_p)
22036 count++;
22037 p = TREE_CHAIN (p);
22038 }
22039 while (p && p != void_list_node);
22040 if (count != nargs)
22041 return unify_too_few_arguments (explain_p, ia, count,
22042 type_pack_p);
22043 }
22044
22045 if (!subr)
22046 {
22047 tsubst_flags_t complain = (explain_p
22048 ? tf_warning_or_error
22049 : tf_none);
22050 bool tried_array_deduction = (cxx_dialect < cxx17);
22051
22052 for (i = 0; i < ntparms; i++)
22053 {
22054 tree targ = TREE_VEC_ELT (targs, i);
22055 tree tparm = TREE_VEC_ELT (tparms, i);
22056
22057 /* Clear the "incomplete" flags on all argument packs now so that
22058 substituting them into later default arguments works. */
22059 if (targ && ARGUMENT_PACK_P (targ))
22060 {
22061 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
22062 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
22063 }
22064
22065 if (targ || tparm == error_mark_node)
22066 continue;
22067 tparm = TREE_VALUE (tparm);
22068
22069 if (TREE_CODE (tparm) == TYPE_DECL
22070 && !tried_array_deduction)
22071 {
22072 try_array_deduction (tparms, targs, xparms);
22073 tried_array_deduction = true;
22074 if (TREE_VEC_ELT (targs, i))
22075 continue;
22076 }
22077
22078 /* If this is an undeduced nontype parameter that depends on
22079 a type parameter, try another pass; its type may have been
22080 deduced from a later argument than the one from which
22081 this parameter can be deduced. */
22082 if (TREE_CODE (tparm) == PARM_DECL
22083 && uses_template_parms (TREE_TYPE (tparm))
22084 && saw_undeduced < 2)
22085 {
22086 saw_undeduced = 1;
22087 continue;
22088 }
22089
22090 /* Core issue #226 (C++0x) [temp.deduct]:
22091
22092 If a template argument has not been deduced, its
22093 default template argument, if any, is used.
22094
22095 When we are in C++98 mode, TREE_PURPOSE will either
22096 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22097 to explicitly check cxx_dialect here. */
22098 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22099 /* OK, there is a default argument. Wait until after the
22100 conversion check to do substitution. */
22101 continue;
22102
22103 /* If the type parameter is a parameter pack, then it will
22104 be deduced to an empty parameter pack. */
22105 if (template_parameter_pack_p (tparm))
22106 {
22107 tree arg;
22108
22109 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22110 {
22111 arg = make_node (NONTYPE_ARGUMENT_PACK);
22112 TREE_CONSTANT (arg) = 1;
22113 }
22114 else
22115 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22116
22117 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22118
22119 TREE_VEC_ELT (targs, i) = arg;
22120 continue;
22121 }
22122
22123 return unify_parameter_deduction_failure (explain_p, tparm);
22124 }
22125
22126 /* Now substitute into the default template arguments. */
22127 for (i = 0; i < ntparms; i++)
22128 {
22129 tree targ = TREE_VEC_ELT (targs, i);
22130 tree tparm = TREE_VEC_ELT (tparms, i);
22131
22132 if (targ || tparm == error_mark_node)
22133 continue;
22134 tree parm = TREE_VALUE (tparm);
22135 tree arg = TREE_PURPOSE (tparm);
22136 reopen_deferring_access_checks (*checks);
22137 location_t save_loc = input_location;
22138 if (DECL_P (parm))
22139 input_location = DECL_SOURCE_LOCATION (parm);
22140
22141 if (saw_undeduced == 1
22142 && TREE_CODE (parm) == PARM_DECL
22143 && uses_template_parms (TREE_TYPE (parm)))
22144 {
22145 /* The type of this non-type parameter depends on undeduced
22146 parameters. Don't try to use its default argument yet,
22147 since we might deduce an argument for it on the next pass,
22148 but do check whether the arguments we already have cause
22149 substitution failure, so that that happens before we try
22150 later default arguments (78489). */
22151 ++processing_template_decl;
22152 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22153 NULL_TREE);
22154 --processing_template_decl;
22155 if (type == error_mark_node)
22156 arg = error_mark_node;
22157 else
22158 arg = NULL_TREE;
22159 }
22160 else
22161 {
22162 /* Even if the call is happening in template context, getting
22163 here means it's non-dependent, and a default argument is
22164 considered a separate definition under [temp.decls], so we can
22165 do this substitution without processing_template_decl. This
22166 is important if the default argument contains something that
22167 might be instantiation-dependent like access (87480). */
22168 processing_template_decl_sentinel s;
22169 tree substed = NULL_TREE;
22170 if (saw_undeduced == 1)
22171 {
22172 /* First instatiate in template context, in case we still
22173 depend on undeduced template parameters. */
22174 ++processing_template_decl;
22175 substed = tsubst_template_arg (arg, full_targs, complain,
22176 NULL_TREE);
22177 --processing_template_decl;
22178 if (substed != error_mark_node
22179 && !uses_template_parms (substed))
22180 /* We replaced all the tparms, substitute again out of
22181 template context. */
22182 substed = NULL_TREE;
22183 }
22184 if (!substed)
22185 substed = tsubst_template_arg (arg, full_targs, complain,
22186 NULL_TREE);
22187
22188 if (!uses_template_parms (substed))
22189 arg = convert_template_argument (parm, substed, full_targs,
22190 complain, i, NULL_TREE);
22191 else if (saw_undeduced == 1)
22192 arg = NULL_TREE;
22193 else
22194 arg = error_mark_node;
22195 }
22196
22197 input_location = save_loc;
22198 *checks = get_deferred_access_checks ();
22199 pop_deferring_access_checks ();
22200
22201 if (arg == error_mark_node)
22202 return 1;
22203 else if (arg)
22204 {
22205 TREE_VEC_ELT (targs, i) = arg;
22206 /* The position of the first default template argument,
22207 is also the number of non-defaulted arguments in TARGS.
22208 Record that. */
22209 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22210 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22211 }
22212 }
22213
22214 if (saw_undeduced++ == 1)
22215 goto again;
22216 }
22217
22218 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22219 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22220
22221 return unify_success (explain_p);
22222 }
22223
22224 /* Subroutine of type_unification_real. Args are like the variables
22225 at the call site. ARG is an overloaded function (or template-id);
22226 we try deducing template args from each of the overloads, and if
22227 only one succeeds, we go with that. Modifies TARGS and returns
22228 true on success. */
22229
22230 static bool
22231 resolve_overloaded_unification (tree tparms,
22232 tree targs,
22233 tree parm,
22234 tree arg,
22235 unification_kind_t strict,
22236 int sub_strict,
22237 bool explain_p)
22238 {
22239 tree tempargs = copy_node (targs);
22240 int good = 0;
22241 tree goodfn = NULL_TREE;
22242 bool addr_p;
22243
22244 if (TREE_CODE (arg) == ADDR_EXPR)
22245 {
22246 arg = TREE_OPERAND (arg, 0);
22247 addr_p = true;
22248 }
22249 else
22250 addr_p = false;
22251
22252 if (TREE_CODE (arg) == COMPONENT_REF)
22253 /* Handle `&x' where `x' is some static or non-static member
22254 function name. */
22255 arg = TREE_OPERAND (arg, 1);
22256
22257 if (TREE_CODE (arg) == OFFSET_REF)
22258 arg = TREE_OPERAND (arg, 1);
22259
22260 /* Strip baselink information. */
22261 if (BASELINK_P (arg))
22262 arg = BASELINK_FUNCTIONS (arg);
22263
22264 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22265 {
22266 /* If we got some explicit template args, we need to plug them into
22267 the affected templates before we try to unify, in case the
22268 explicit args will completely resolve the templates in question. */
22269
22270 int ok = 0;
22271 tree expl_subargs = TREE_OPERAND (arg, 1);
22272 arg = TREE_OPERAND (arg, 0);
22273
22274 for (lkp_iterator iter (arg); iter; ++iter)
22275 {
22276 tree fn = *iter;
22277 tree subargs, elem;
22278
22279 if (TREE_CODE (fn) != TEMPLATE_DECL)
22280 continue;
22281
22282 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22283 expl_subargs, NULL_TREE, tf_none,
22284 /*require_all_args=*/true,
22285 /*use_default_args=*/true);
22286 if (subargs != error_mark_node
22287 && !any_dependent_template_arguments_p (subargs))
22288 {
22289 fn = instantiate_template (fn, subargs, tf_none);
22290 if (!constraints_satisfied_p (fn))
22291 continue;
22292 if (undeduced_auto_decl (fn))
22293 {
22294 /* Instantiate the function to deduce its return type. */
22295 ++function_depth;
22296 instantiate_decl (fn, /*defer*/false, /*class*/false);
22297 --function_depth;
22298 }
22299
22300 elem = TREE_TYPE (fn);
22301 if (try_one_overload (tparms, targs, tempargs, parm,
22302 elem, strict, sub_strict, addr_p, explain_p)
22303 && (!goodfn || !same_type_p (goodfn, elem)))
22304 {
22305 goodfn = elem;
22306 ++good;
22307 }
22308 }
22309 else if (subargs)
22310 ++ok;
22311 }
22312 /* If no templates (or more than one) are fully resolved by the
22313 explicit arguments, this template-id is a non-deduced context; it
22314 could still be OK if we deduce all template arguments for the
22315 enclosing call through other arguments. */
22316 if (good != 1)
22317 good = ok;
22318 }
22319 else if (!OVL_P (arg))
22320 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22321 -- but the deduction does not succeed because the expression is
22322 not just the function on its own. */
22323 return false;
22324 else
22325 for (lkp_iterator iter (arg); iter; ++iter)
22326 {
22327 tree fn = *iter;
22328 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22329 strict, sub_strict, addr_p, explain_p)
22330 && (!goodfn || !decls_match (goodfn, fn)))
22331 {
22332 goodfn = fn;
22333 ++good;
22334 }
22335 }
22336
22337 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22338 to function or pointer to member function argument if the set of
22339 overloaded functions does not contain function templates and at most
22340 one of a set of overloaded functions provides a unique match.
22341
22342 So if we found multiple possibilities, we return success but don't
22343 deduce anything. */
22344
22345 if (good == 1)
22346 {
22347 int i = TREE_VEC_LENGTH (targs);
22348 for (; i--; )
22349 if (TREE_VEC_ELT (tempargs, i))
22350 {
22351 tree old = TREE_VEC_ELT (targs, i);
22352 tree new_ = TREE_VEC_ELT (tempargs, i);
22353 if (new_ && old && ARGUMENT_PACK_P (old)
22354 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22355 /* Don't forget explicit template arguments in a pack. */
22356 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22357 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22358 TREE_VEC_ELT (targs, i) = new_;
22359 }
22360 }
22361 if (good)
22362 return true;
22363
22364 return false;
22365 }
22366
22367 /* Core DR 115: In contexts where deduction is done and fails, or in
22368 contexts where deduction is not done, if a template argument list is
22369 specified and it, along with any default template arguments, identifies
22370 a single function template specialization, then the template-id is an
22371 lvalue for the function template specialization. */
22372
22373 tree
22374 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22375 {
22376 tree expr, offset, baselink;
22377 bool addr;
22378
22379 if (!type_unknown_p (orig_expr))
22380 return orig_expr;
22381
22382 expr = orig_expr;
22383 addr = false;
22384 offset = NULL_TREE;
22385 baselink = NULL_TREE;
22386
22387 if (TREE_CODE (expr) == ADDR_EXPR)
22388 {
22389 expr = TREE_OPERAND (expr, 0);
22390 addr = true;
22391 }
22392 if (TREE_CODE (expr) == OFFSET_REF)
22393 {
22394 offset = expr;
22395 expr = TREE_OPERAND (expr, 1);
22396 }
22397 if (BASELINK_P (expr))
22398 {
22399 baselink = expr;
22400 expr = BASELINK_FUNCTIONS (expr);
22401 }
22402
22403 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22404 {
22405 int good = 0;
22406 tree goodfn = NULL_TREE;
22407
22408 /* If we got some explicit template args, we need to plug them into
22409 the affected templates before we try to unify, in case the
22410 explicit args will completely resolve the templates in question. */
22411
22412 tree expl_subargs = TREE_OPERAND (expr, 1);
22413 tree arg = TREE_OPERAND (expr, 0);
22414 tree badfn = NULL_TREE;
22415 tree badargs = NULL_TREE;
22416
22417 for (lkp_iterator iter (arg); iter; ++iter)
22418 {
22419 tree fn = *iter;
22420 tree subargs, elem;
22421
22422 if (TREE_CODE (fn) != TEMPLATE_DECL)
22423 continue;
22424
22425 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22426 expl_subargs, NULL_TREE, tf_none,
22427 /*require_all_args=*/true,
22428 /*use_default_args=*/true);
22429 if (subargs != error_mark_node
22430 && !any_dependent_template_arguments_p (subargs))
22431 {
22432 elem = instantiate_template (fn, subargs, tf_none);
22433 if (elem == error_mark_node)
22434 {
22435 badfn = fn;
22436 badargs = subargs;
22437 }
22438 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22439 && constraints_satisfied_p (elem))
22440 {
22441 goodfn = elem;
22442 ++good;
22443 }
22444 }
22445 }
22446 if (good == 1)
22447 {
22448 mark_used (goodfn);
22449 expr = goodfn;
22450 if (baselink)
22451 expr = build_baselink (BASELINK_BINFO (baselink),
22452 BASELINK_ACCESS_BINFO (baselink),
22453 expr, BASELINK_OPTYPE (baselink));
22454 if (offset)
22455 {
22456 tree base
22457 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22458 expr = build_offset_ref (base, expr, addr, complain);
22459 }
22460 if (addr)
22461 expr = cp_build_addr_expr (expr, complain);
22462 return expr;
22463 }
22464 else if (good == 0 && badargs && (complain & tf_error))
22465 /* There were no good options and at least one bad one, so let the
22466 user know what the problem is. */
22467 instantiate_template (badfn, badargs, complain);
22468 }
22469 return orig_expr;
22470 }
22471
22472 /* As above, but error out if the expression remains overloaded. */
22473
22474 tree
22475 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22476 {
22477 exp = resolve_nondeduced_context (exp, complain);
22478 if (type_unknown_p (exp))
22479 {
22480 if (complain & tf_error)
22481 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22482 return error_mark_node;
22483 }
22484 return exp;
22485 }
22486
22487 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22488 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22489 different overloads deduce different arguments for a given parm.
22490 ADDR_P is true if the expression for which deduction is being
22491 performed was of the form "& fn" rather than simply "fn".
22492
22493 Returns 1 on success. */
22494
22495 static int
22496 try_one_overload (tree tparms,
22497 tree orig_targs,
22498 tree targs,
22499 tree parm,
22500 tree arg,
22501 unification_kind_t strict,
22502 int sub_strict,
22503 bool addr_p,
22504 bool explain_p)
22505 {
22506 int nargs;
22507 tree tempargs;
22508 int i;
22509
22510 if (arg == error_mark_node)
22511 return 0;
22512
22513 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22514 to function or pointer to member function argument if the set of
22515 overloaded functions does not contain function templates and at most
22516 one of a set of overloaded functions provides a unique match.
22517
22518 So if this is a template, just return success. */
22519
22520 if (uses_template_parms (arg))
22521 return 1;
22522
22523 if (TREE_CODE (arg) == METHOD_TYPE)
22524 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22525 else if (addr_p)
22526 arg = build_pointer_type (arg);
22527
22528 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22529
22530 /* We don't copy orig_targs for this because if we have already deduced
22531 some template args from previous args, unify would complain when we
22532 try to deduce a template parameter for the same argument, even though
22533 there isn't really a conflict. */
22534 nargs = TREE_VEC_LENGTH (targs);
22535 tempargs = make_tree_vec (nargs);
22536
22537 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22538 return 0;
22539
22540 /* First make sure we didn't deduce anything that conflicts with
22541 explicitly specified args. */
22542 for (i = nargs; i--; )
22543 {
22544 tree elt = TREE_VEC_ELT (tempargs, i);
22545 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22546
22547 if (!elt)
22548 /*NOP*/;
22549 else if (uses_template_parms (elt))
22550 /* Since we're unifying against ourselves, we will fill in
22551 template args used in the function parm list with our own
22552 template parms. Discard them. */
22553 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22554 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22555 {
22556 /* Check that the argument at each index of the deduced argument pack
22557 is equivalent to the corresponding explicitly specified argument.
22558 We may have deduced more arguments than were explicitly specified,
22559 and that's OK. */
22560
22561 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22562 that's wrong if we deduce the same argument pack from multiple
22563 function arguments: it's only incomplete the first time. */
22564
22565 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22566 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22567
22568 if (TREE_VEC_LENGTH (deduced_pack)
22569 < TREE_VEC_LENGTH (explicit_pack))
22570 return 0;
22571
22572 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22573 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22574 TREE_VEC_ELT (deduced_pack, j)))
22575 return 0;
22576 }
22577 else if (oldelt && !template_args_equal (oldelt, elt))
22578 return 0;
22579 }
22580
22581 for (i = nargs; i--; )
22582 {
22583 tree elt = TREE_VEC_ELT (tempargs, i);
22584
22585 if (elt)
22586 TREE_VEC_ELT (targs, i) = elt;
22587 }
22588
22589 return 1;
22590 }
22591
22592 /* PARM is a template class (perhaps with unbound template
22593 parameters). ARG is a fully instantiated type. If ARG can be
22594 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22595 TARGS are as for unify. */
22596
22597 static tree
22598 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22599 bool explain_p)
22600 {
22601 tree copy_of_targs;
22602
22603 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22604 return NULL_TREE;
22605 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22606 /* Matches anything. */;
22607 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22608 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22609 return NULL_TREE;
22610
22611 /* We need to make a new template argument vector for the call to
22612 unify. If we used TARGS, we'd clutter it up with the result of
22613 the attempted unification, even if this class didn't work out.
22614 We also don't want to commit ourselves to all the unifications
22615 we've already done, since unification is supposed to be done on
22616 an argument-by-argument basis. In other words, consider the
22617 following pathological case:
22618
22619 template <int I, int J, int K>
22620 struct S {};
22621
22622 template <int I, int J>
22623 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22624
22625 template <int I, int J, int K>
22626 void f(S<I, J, K>, S<I, I, I>);
22627
22628 void g() {
22629 S<0, 0, 0> s0;
22630 S<0, 1, 2> s2;
22631
22632 f(s0, s2);
22633 }
22634
22635 Now, by the time we consider the unification involving `s2', we
22636 already know that we must have `f<0, 0, 0>'. But, even though
22637 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22638 because there are two ways to unify base classes of S<0, 1, 2>
22639 with S<I, I, I>. If we kept the already deduced knowledge, we
22640 would reject the possibility I=1. */
22641 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22642
22643 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22644 {
22645 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22646 return NULL_TREE;
22647 return arg;
22648 }
22649
22650 /* If unification failed, we're done. */
22651 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22652 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22653 return NULL_TREE;
22654
22655 return arg;
22656 }
22657
22658 /* Given a template type PARM and a class type ARG, find the unique
22659 base type in ARG that is an instance of PARM. We do not examine
22660 ARG itself; only its base-classes. If there is not exactly one
22661 appropriate base class, return NULL_TREE. PARM may be the type of
22662 a partial specialization, as well as a plain template type. Used
22663 by unify. */
22664
22665 static enum template_base_result
22666 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22667 bool explain_p, tree *result)
22668 {
22669 tree rval = NULL_TREE;
22670 tree binfo;
22671
22672 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22673
22674 binfo = TYPE_BINFO (complete_type (arg));
22675 if (!binfo)
22676 {
22677 /* The type could not be completed. */
22678 *result = NULL_TREE;
22679 return tbr_incomplete_type;
22680 }
22681
22682 /* Walk in inheritance graph order. The search order is not
22683 important, and this avoids multiple walks of virtual bases. */
22684 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22685 {
22686 tree r = try_class_unification (tparms, targs, parm,
22687 BINFO_TYPE (binfo), explain_p);
22688
22689 if (r)
22690 {
22691 /* If there is more than one satisfactory baseclass, then:
22692
22693 [temp.deduct.call]
22694
22695 If they yield more than one possible deduced A, the type
22696 deduction fails.
22697
22698 applies. */
22699 if (rval && !same_type_p (r, rval))
22700 {
22701 /* [temp.deduct.call]/4.3: If there is a class C that is a
22702 (direct or indirect) base class of D and derived (directly or
22703 indirectly) from a class B and that would be a valid deduced
22704 A, the deduced A cannot be B or pointer to B, respectively. */
22705 if (DERIVED_FROM_P (r, rval))
22706 /* Ignore r. */
22707 continue;
22708 else if (DERIVED_FROM_P (rval, r))
22709 /* Ignore rval. */;
22710 else
22711 {
22712 *result = NULL_TREE;
22713 return tbr_ambiguous_baseclass;
22714 }
22715 }
22716
22717 rval = r;
22718 }
22719 }
22720
22721 *result = rval;
22722 return tbr_success;
22723 }
22724
22725 /* Returns the level of DECL, which declares a template parameter. */
22726
22727 static int
22728 template_decl_level (tree decl)
22729 {
22730 switch (TREE_CODE (decl))
22731 {
22732 case TYPE_DECL:
22733 case TEMPLATE_DECL:
22734 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22735
22736 case PARM_DECL:
22737 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22738
22739 default:
22740 gcc_unreachable ();
22741 }
22742 return 0;
22743 }
22744
22745 /* Decide whether ARG can be unified with PARM, considering only the
22746 cv-qualifiers of each type, given STRICT as documented for unify.
22747 Returns nonzero iff the unification is OK on that basis. */
22748
22749 static int
22750 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22751 {
22752 int arg_quals = cp_type_quals (arg);
22753 int parm_quals = cp_type_quals (parm);
22754
22755 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22756 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22757 {
22758 /* Although a CVR qualifier is ignored when being applied to a
22759 substituted template parameter ([8.3.2]/1 for example), that
22760 does not allow us to unify "const T" with "int&" because both
22761 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22762 It is ok when we're allowing additional CV qualifiers
22763 at the outer level [14.8.2.1]/3,1st bullet. */
22764 if ((TYPE_REF_P (arg)
22765 || FUNC_OR_METHOD_TYPE_P (arg))
22766 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22767 return 0;
22768
22769 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22770 && (parm_quals & TYPE_QUAL_RESTRICT))
22771 return 0;
22772 }
22773
22774 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22775 && (arg_quals & parm_quals) != parm_quals)
22776 return 0;
22777
22778 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22779 && (parm_quals & arg_quals) != arg_quals)
22780 return 0;
22781
22782 return 1;
22783 }
22784
22785 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22786 void
22787 template_parm_level_and_index (tree parm, int* level, int* index)
22788 {
22789 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22790 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22791 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22792 {
22793 *index = TEMPLATE_TYPE_IDX (parm);
22794 *level = TEMPLATE_TYPE_LEVEL (parm);
22795 }
22796 else
22797 {
22798 *index = TEMPLATE_PARM_IDX (parm);
22799 *level = TEMPLATE_PARM_LEVEL (parm);
22800 }
22801 }
22802
22803 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22804 do { \
22805 if (unify (TP, TA, P, A, S, EP)) \
22806 return 1; \
22807 } while (0)
22808
22809 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22810 expansion at the end of PACKED_PARMS. Returns 0 if the type
22811 deduction succeeds, 1 otherwise. STRICT is the same as in
22812 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22813 function call argument list. We'll need to adjust the arguments to make them
22814 types. SUBR tells us if this is from a recursive call to
22815 type_unification_real, or for comparing two template argument
22816 lists. */
22817
22818 static int
22819 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22820 tree packed_args, unification_kind_t strict,
22821 bool subr, bool explain_p)
22822 {
22823 tree parm
22824 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22825 tree pattern = PACK_EXPANSION_PATTERN (parm);
22826 tree pack, packs = NULL_TREE;
22827 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22828
22829 /* Add in any args remembered from an earlier partial instantiation. */
22830 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22831 int levels = TMPL_ARGS_DEPTH (targs);
22832
22833 packed_args = expand_template_argument_pack (packed_args);
22834
22835 int len = TREE_VEC_LENGTH (packed_args);
22836
22837 /* Determine the parameter packs we will be deducing from the
22838 pattern, and record their current deductions. */
22839 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22840 pack; pack = TREE_CHAIN (pack))
22841 {
22842 tree parm_pack = TREE_VALUE (pack);
22843 int idx, level;
22844
22845 /* Only template parameter packs can be deduced, not e.g. function
22846 parameter packs or __bases or __integer_pack. */
22847 if (!TEMPLATE_PARM_P (parm_pack))
22848 continue;
22849
22850 /* Determine the index and level of this parameter pack. */
22851 template_parm_level_and_index (parm_pack, &level, &idx);
22852 if (level < levels)
22853 continue;
22854
22855 /* Keep track of the parameter packs and their corresponding
22856 argument packs. */
22857 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22858 TREE_TYPE (packs) = make_tree_vec (len - start);
22859 }
22860
22861 /* Loop through all of the arguments that have not yet been
22862 unified and unify each with the pattern. */
22863 for (i = start; i < len; i++)
22864 {
22865 tree parm;
22866 bool any_explicit = false;
22867 tree arg = TREE_VEC_ELT (packed_args, i);
22868
22869 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22870 or the element of its argument pack at the current index if
22871 this argument was explicitly specified. */
22872 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22873 {
22874 int idx, level;
22875 tree arg, pargs;
22876 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22877
22878 arg = NULL_TREE;
22879 if (TREE_VALUE (pack)
22880 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22881 && (i - start < TREE_VEC_LENGTH (pargs)))
22882 {
22883 any_explicit = true;
22884 arg = TREE_VEC_ELT (pargs, i - start);
22885 }
22886 TMPL_ARG (targs, level, idx) = arg;
22887 }
22888
22889 /* If we had explicit template arguments, substitute them into the
22890 pattern before deduction. */
22891 if (any_explicit)
22892 {
22893 /* Some arguments might still be unspecified or dependent. */
22894 bool dependent;
22895 ++processing_template_decl;
22896 dependent = any_dependent_template_arguments_p (targs);
22897 if (!dependent)
22898 --processing_template_decl;
22899 parm = tsubst (pattern, targs,
22900 explain_p ? tf_warning_or_error : tf_none,
22901 NULL_TREE);
22902 if (dependent)
22903 --processing_template_decl;
22904 if (parm == error_mark_node)
22905 return 1;
22906 }
22907 else
22908 parm = pattern;
22909
22910 /* Unify the pattern with the current argument. */
22911 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22912 explain_p))
22913 return 1;
22914
22915 /* For each parameter pack, collect the deduced value. */
22916 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22917 {
22918 int idx, level;
22919 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22920
22921 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22922 TMPL_ARG (targs, level, idx);
22923 }
22924 }
22925
22926 /* Verify that the results of unification with the parameter packs
22927 produce results consistent with what we've seen before, and make
22928 the deduced argument packs available. */
22929 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22930 {
22931 tree old_pack = TREE_VALUE (pack);
22932 tree new_args = TREE_TYPE (pack);
22933 int i, len = TREE_VEC_LENGTH (new_args);
22934 int idx, level;
22935 bool nondeduced_p = false;
22936
22937 /* By default keep the original deduced argument pack.
22938 If necessary, more specific code is going to update the
22939 resulting deduced argument later down in this function. */
22940 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22941 TMPL_ARG (targs, level, idx) = old_pack;
22942
22943 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22944 actually deduce anything. */
22945 for (i = 0; i < len && !nondeduced_p; ++i)
22946 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22947 nondeduced_p = true;
22948 if (nondeduced_p)
22949 continue;
22950
22951 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22952 {
22953 /* If we had fewer function args than explicit template args,
22954 just use the explicits. */
22955 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22956 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22957 if (len < explicit_len)
22958 new_args = explicit_args;
22959 }
22960
22961 if (!old_pack)
22962 {
22963 tree result;
22964 /* Build the deduced *_ARGUMENT_PACK. */
22965 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22966 {
22967 result = make_node (NONTYPE_ARGUMENT_PACK);
22968 TREE_CONSTANT (result) = 1;
22969 }
22970 else
22971 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22972
22973 SET_ARGUMENT_PACK_ARGS (result, new_args);
22974
22975 /* Note the deduced argument packs for this parameter
22976 pack. */
22977 TMPL_ARG (targs, level, idx) = result;
22978 }
22979 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22980 && (ARGUMENT_PACK_ARGS (old_pack)
22981 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22982 {
22983 /* We only had the explicitly-provided arguments before, but
22984 now we have a complete set of arguments. */
22985 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22986
22987 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22988 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22989 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22990 }
22991 else
22992 {
22993 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22994 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22995 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
22996 /* During template argument deduction for the aggregate deduction
22997 candidate, the number of elements in a trailing parameter pack
22998 is only deduced from the number of remaining function
22999 arguments if it is not otherwise deduced. */
23000 if (cxx_dialect >= cxx20
23001 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
23002 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
23003 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
23004 if (!comp_template_args (old_args, new_args,
23005 &bad_old_arg, &bad_new_arg))
23006 /* Inconsistent unification of this parameter pack. */
23007 return unify_parameter_pack_inconsistent (explain_p,
23008 bad_old_arg,
23009 bad_new_arg);
23010 }
23011 }
23012
23013 return unify_success (explain_p);
23014 }
23015
23016 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23017 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23018 parameters and return value are as for unify. */
23019
23020 static int
23021 unify_array_domain (tree tparms, tree targs,
23022 tree parm_dom, tree arg_dom,
23023 bool explain_p)
23024 {
23025 tree parm_max;
23026 tree arg_max;
23027 bool parm_cst;
23028 bool arg_cst;
23029
23030 /* Our representation of array types uses "N - 1" as the
23031 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23032 not an integer constant. We cannot unify arbitrarily
23033 complex expressions, so we eliminate the MINUS_EXPRs
23034 here. */
23035 parm_max = TYPE_MAX_VALUE (parm_dom);
23036 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
23037 if (!parm_cst)
23038 {
23039 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
23040 parm_max = TREE_OPERAND (parm_max, 0);
23041 }
23042 arg_max = TYPE_MAX_VALUE (arg_dom);
23043 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
23044 if (!arg_cst)
23045 {
23046 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23047 trying to unify the type of a variable with the type
23048 of a template parameter. For example:
23049
23050 template <unsigned int N>
23051 void f (char (&) [N]);
23052 int g();
23053 void h(int i) {
23054 char a[g(i)];
23055 f(a);
23056 }
23057
23058 Here, the type of the ARG will be "int [g(i)]", and
23059 may be a SAVE_EXPR, etc. */
23060 if (TREE_CODE (arg_max) != MINUS_EXPR)
23061 return unify_vla_arg (explain_p, arg_dom);
23062 arg_max = TREE_OPERAND (arg_max, 0);
23063 }
23064
23065 /* If only one of the bounds used a MINUS_EXPR, compensate
23066 by adding one to the other bound. */
23067 if (parm_cst && !arg_cst)
23068 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
23069 integer_type_node,
23070 parm_max,
23071 integer_one_node);
23072 else if (arg_cst && !parm_cst)
23073 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
23074 integer_type_node,
23075 arg_max,
23076 integer_one_node);
23077
23078 return unify (tparms, targs, parm_max, arg_max,
23079 UNIFY_ALLOW_INTEGER, explain_p);
23080 }
23081
23082 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23083
23084 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
23085
23086 static pa_kind_t
23087 pa_kind (tree t)
23088 {
23089 if (PACK_EXPANSION_P (t))
23090 t = PACK_EXPANSION_PATTERN (t);
23091 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23092 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23093 || DECL_TYPE_TEMPLATE_P (t))
23094 return pa_tmpl;
23095 else if (TYPE_P (t))
23096 return pa_type;
23097 else
23098 return pa_expr;
23099 }
23100
23101 /* Deduce the value of template parameters. TPARMS is the (innermost)
23102 set of template parameters to a template. TARGS is the bindings
23103 for those template parameters, as determined thus far; TARGS may
23104 include template arguments for outer levels of template parameters
23105 as well. PARM is a parameter to a template function, or a
23106 subcomponent of that parameter; ARG is the corresponding argument.
23107 This function attempts to match PARM with ARG in a manner
23108 consistent with the existing assignments in TARGS. If more values
23109 are deduced, then TARGS is updated.
23110
23111 Returns 0 if the type deduction succeeds, 1 otherwise. The
23112 parameter STRICT is a bitwise or of the following flags:
23113
23114 UNIFY_ALLOW_NONE:
23115 Require an exact match between PARM and ARG.
23116 UNIFY_ALLOW_MORE_CV_QUAL:
23117 Allow the deduced ARG to be more cv-qualified (by qualification
23118 conversion) than ARG.
23119 UNIFY_ALLOW_LESS_CV_QUAL:
23120 Allow the deduced ARG to be less cv-qualified than ARG.
23121 UNIFY_ALLOW_DERIVED:
23122 Allow the deduced ARG to be a template base class of ARG,
23123 or a pointer to a template base class of the type pointed to by
23124 ARG.
23125 UNIFY_ALLOW_INTEGER:
23126 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23127 case for more information.
23128 UNIFY_ALLOW_OUTER_LEVEL:
23129 This is the outermost level of a deduction. Used to determine validity
23130 of qualification conversions. A valid qualification conversion must
23131 have const qualified pointers leading up to the inner type which
23132 requires additional CV quals, except at the outer level, where const
23133 is not required [conv.qual]. It would be normal to set this flag in
23134 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23135 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23136 This is the outermost level of a deduction, and PARM can be more CV
23137 qualified at this point.
23138 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23139 This is the outermost level of a deduction, and PARM can be less CV
23140 qualified at this point. */
23141
23142 static int
23143 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23144 bool explain_p)
23145 {
23146 int idx;
23147 tree targ;
23148 tree tparm;
23149 int strict_in = strict;
23150 tsubst_flags_t complain = (explain_p
23151 ? tf_warning_or_error
23152 : tf_none);
23153
23154 /* I don't think this will do the right thing with respect to types.
23155 But the only case I've seen it in so far has been array bounds, where
23156 signedness is the only information lost, and I think that will be
23157 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23158 finish_id_expression_1, and are also OK. */
23159 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23160 parm = TREE_OPERAND (parm, 0);
23161
23162 if (arg == error_mark_node)
23163 return unify_invalid (explain_p);
23164 if (arg == unknown_type_node
23165 || arg == init_list_type_node)
23166 /* We can't deduce anything from this, but we might get all the
23167 template args from other function args. */
23168 return unify_success (explain_p);
23169
23170 if (parm == any_targ_node || arg == any_targ_node)
23171 return unify_success (explain_p);
23172
23173 /* If PARM uses template parameters, then we can't bail out here,
23174 even if ARG == PARM, since we won't record unifications for the
23175 template parameters. We might need them if we're trying to
23176 figure out which of two things is more specialized. */
23177 if (arg == parm && !uses_template_parms (parm))
23178 return unify_success (explain_p);
23179
23180 /* Handle init lists early, so the rest of the function can assume
23181 we're dealing with a type. */
23182 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23183 {
23184 tree elt, elttype;
23185 unsigned i;
23186 tree orig_parm = parm;
23187
23188 if (!is_std_init_list (parm)
23189 && TREE_CODE (parm) != ARRAY_TYPE)
23190 /* We can only deduce from an initializer list argument if the
23191 parameter is std::initializer_list or an array; otherwise this
23192 is a non-deduced context. */
23193 return unify_success (explain_p);
23194
23195 if (TREE_CODE (parm) == ARRAY_TYPE)
23196 elttype = TREE_TYPE (parm);
23197 else
23198 {
23199 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23200 /* Deduction is defined in terms of a single type, so just punt
23201 on the (bizarre) std::initializer_list<T...>. */
23202 if (PACK_EXPANSION_P (elttype))
23203 return unify_success (explain_p);
23204 }
23205
23206 if (strict != DEDUCE_EXACT
23207 && TYPE_P (elttype)
23208 && !uses_deducible_template_parms (elttype))
23209 /* If ELTTYPE has no deducible template parms, skip deduction from
23210 the list elements. */;
23211 else
23212 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23213 {
23214 int elt_strict = strict;
23215
23216 if (elt == error_mark_node)
23217 return unify_invalid (explain_p);
23218
23219 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23220 {
23221 tree type = TREE_TYPE (elt);
23222 if (type == error_mark_node)
23223 return unify_invalid (explain_p);
23224 /* It should only be possible to get here for a call. */
23225 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23226 elt_strict |= maybe_adjust_types_for_deduction
23227 (DEDUCE_CALL, &elttype, &type, elt);
23228 elt = type;
23229 }
23230
23231 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23232 explain_p);
23233 }
23234
23235 if (TREE_CODE (parm) == ARRAY_TYPE
23236 && deducible_array_bound (TYPE_DOMAIN (parm)))
23237 {
23238 /* Also deduce from the length of the initializer list. */
23239 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23240 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23241 if (idx == error_mark_node)
23242 return unify_invalid (explain_p);
23243 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23244 idx, explain_p);
23245 }
23246
23247 /* If the std::initializer_list<T> deduction worked, replace the
23248 deduced A with std::initializer_list<A>. */
23249 if (orig_parm != parm)
23250 {
23251 idx = TEMPLATE_TYPE_IDX (orig_parm);
23252 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23253 targ = listify (targ);
23254 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23255 }
23256 return unify_success (explain_p);
23257 }
23258
23259 /* If parm and arg aren't the same kind of thing (template, type, or
23260 expression), fail early. */
23261 if (pa_kind (parm) != pa_kind (arg))
23262 return unify_invalid (explain_p);
23263
23264 /* Immediately reject some pairs that won't unify because of
23265 cv-qualification mismatches. */
23266 if (TREE_CODE (arg) == TREE_CODE (parm)
23267 && TYPE_P (arg)
23268 /* It is the elements of the array which hold the cv quals of an array
23269 type, and the elements might be template type parms. We'll check
23270 when we recurse. */
23271 && TREE_CODE (arg) != ARRAY_TYPE
23272 /* We check the cv-qualifiers when unifying with template type
23273 parameters below. We want to allow ARG `const T' to unify with
23274 PARM `T' for example, when computing which of two templates
23275 is more specialized, for example. */
23276 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23277 && !check_cv_quals_for_unify (strict_in, arg, parm))
23278 return unify_cv_qual_mismatch (explain_p, parm, arg);
23279
23280 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23281 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23282 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23283 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23284 strict &= ~UNIFY_ALLOW_DERIVED;
23285 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23286 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23287
23288 switch (TREE_CODE (parm))
23289 {
23290 case TYPENAME_TYPE:
23291 case SCOPE_REF:
23292 case UNBOUND_CLASS_TEMPLATE:
23293 /* In a type which contains a nested-name-specifier, template
23294 argument values cannot be deduced for template parameters used
23295 within the nested-name-specifier. */
23296 return unify_success (explain_p);
23297
23298 case TEMPLATE_TYPE_PARM:
23299 case TEMPLATE_TEMPLATE_PARM:
23300 case BOUND_TEMPLATE_TEMPLATE_PARM:
23301 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23302 if (error_operand_p (tparm))
23303 return unify_invalid (explain_p);
23304
23305 if (TEMPLATE_TYPE_LEVEL (parm)
23306 != template_decl_level (tparm))
23307 /* The PARM is not one we're trying to unify. Just check
23308 to see if it matches ARG. */
23309 {
23310 if (TREE_CODE (arg) == TREE_CODE (parm)
23311 && (is_auto (parm) ? is_auto (arg)
23312 : same_type_p (parm, arg)))
23313 return unify_success (explain_p);
23314 else
23315 return unify_type_mismatch (explain_p, parm, arg);
23316 }
23317 idx = TEMPLATE_TYPE_IDX (parm);
23318 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23319 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23320 if (error_operand_p (tparm))
23321 return unify_invalid (explain_p);
23322
23323 /* Check for mixed types and values. */
23324 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23325 && TREE_CODE (tparm) != TYPE_DECL)
23326 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23327 && TREE_CODE (tparm) != TEMPLATE_DECL))
23328 gcc_unreachable ();
23329
23330 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23331 {
23332 if ((strict_in & UNIFY_ALLOW_DERIVED)
23333 && CLASS_TYPE_P (arg))
23334 {
23335 /* First try to match ARG directly. */
23336 tree t = try_class_unification (tparms, targs, parm, arg,
23337 explain_p);
23338 if (!t)
23339 {
23340 /* Otherwise, look for a suitable base of ARG, as below. */
23341 enum template_base_result r;
23342 r = get_template_base (tparms, targs, parm, arg,
23343 explain_p, &t);
23344 if (!t)
23345 return unify_no_common_base (explain_p, r, parm, arg);
23346 arg = t;
23347 }
23348 }
23349 /* ARG must be constructed from a template class or a template
23350 template parameter. */
23351 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23352 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23353 return unify_template_deduction_failure (explain_p, parm, arg);
23354
23355 /* Deduce arguments T, i from TT<T> or TT<i>. */
23356 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23357 return 1;
23358
23359 arg = TYPE_TI_TEMPLATE (arg);
23360
23361 /* Fall through to deduce template name. */
23362 }
23363
23364 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23365 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23366 {
23367 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23368
23369 /* Simple cases: Value already set, does match or doesn't. */
23370 if (targ != NULL_TREE && template_args_equal (targ, arg))
23371 return unify_success (explain_p);
23372 else if (targ)
23373 return unify_inconsistency (explain_p, parm, targ, arg);
23374 }
23375 else
23376 {
23377 /* If PARM is `const T' and ARG is only `int', we don't have
23378 a match unless we are allowing additional qualification.
23379 If ARG is `const int' and PARM is just `T' that's OK;
23380 that binds `const int' to `T'. */
23381 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23382 arg, parm))
23383 return unify_cv_qual_mismatch (explain_p, parm, arg);
23384
23385 /* Consider the case where ARG is `const volatile int' and
23386 PARM is `const T'. Then, T should be `volatile int'. */
23387 arg = cp_build_qualified_type_real
23388 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23389 if (arg == error_mark_node)
23390 return unify_invalid (explain_p);
23391
23392 /* Simple cases: Value already set, does match or doesn't. */
23393 if (targ != NULL_TREE && same_type_p (targ, arg))
23394 return unify_success (explain_p);
23395 else if (targ)
23396 return unify_inconsistency (explain_p, parm, targ, arg);
23397
23398 /* Make sure that ARG is not a variable-sized array. (Note
23399 that were talking about variable-sized arrays (like
23400 `int[n]'), rather than arrays of unknown size (like
23401 `int[]').) We'll get very confused by such a type since
23402 the bound of the array is not constant, and therefore
23403 not mangleable. Besides, such types are not allowed in
23404 ISO C++, so we can do as we please here. We do allow
23405 them for 'auto' deduction, since that isn't ABI-exposed. */
23406 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23407 return unify_vla_arg (explain_p, arg);
23408
23409 /* Strip typedefs as in convert_template_argument. */
23410 arg = canonicalize_type_argument (arg, tf_none);
23411 }
23412
23413 /* If ARG is a parameter pack or an expansion, we cannot unify
23414 against it unless PARM is also a parameter pack. */
23415 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23416 && !template_parameter_pack_p (parm))
23417 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23418
23419 /* If the argument deduction results is a METHOD_TYPE,
23420 then there is a problem.
23421 METHOD_TYPE doesn't map to any real C++ type the result of
23422 the deduction cannot be of that type. */
23423 if (TREE_CODE (arg) == METHOD_TYPE)
23424 return unify_method_type_error (explain_p, arg);
23425
23426 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23427 return unify_success (explain_p);
23428
23429 case TEMPLATE_PARM_INDEX:
23430 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23431 if (error_operand_p (tparm))
23432 return unify_invalid (explain_p);
23433
23434 if (TEMPLATE_PARM_LEVEL (parm)
23435 != template_decl_level (tparm))
23436 {
23437 /* The PARM is not one we're trying to unify. Just check
23438 to see if it matches ARG. */
23439 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23440 && cp_tree_equal (parm, arg));
23441 if (result)
23442 unify_expression_unequal (explain_p, parm, arg);
23443 return result;
23444 }
23445
23446 idx = TEMPLATE_PARM_IDX (parm);
23447 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23448
23449 if (targ)
23450 {
23451 if ((strict & UNIFY_ALLOW_INTEGER)
23452 && TREE_TYPE (targ) && TREE_TYPE (arg)
23453 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23454 /* We're deducing from an array bound, the type doesn't matter. */
23455 arg = fold_convert (TREE_TYPE (targ), arg);
23456 int x = !cp_tree_equal (targ, arg);
23457 if (x)
23458 unify_inconsistency (explain_p, parm, targ, arg);
23459 return x;
23460 }
23461
23462 /* [temp.deduct.type] If, in the declaration of a function template
23463 with a non-type template-parameter, the non-type
23464 template-parameter is used in an expression in the function
23465 parameter-list and, if the corresponding template-argument is
23466 deduced, the template-argument type shall match the type of the
23467 template-parameter exactly, except that a template-argument
23468 deduced from an array bound may be of any integral type.
23469 The non-type parameter might use already deduced type parameters. */
23470 tparm = TREE_TYPE (parm);
23471 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23472 /* We don't have enough levels of args to do any substitution. This
23473 can happen in the context of -fnew-ttp-matching. */;
23474 else
23475 {
23476 ++processing_template_decl;
23477 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23478 --processing_template_decl;
23479
23480 if (tree a = type_uses_auto (tparm))
23481 {
23482 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23483 if (tparm == error_mark_node)
23484 return 1;
23485 }
23486 }
23487
23488 if (!TREE_TYPE (arg))
23489 /* Template-parameter dependent expression. Just accept it for now.
23490 It will later be processed in convert_template_argument. */
23491 ;
23492 else if (same_type_ignoring_top_level_qualifiers_p
23493 (non_reference (TREE_TYPE (arg)),
23494 non_reference (tparm)))
23495 /* OK. Ignore top-level quals here because a class-type template
23496 parameter object is const. */;
23497 else if ((strict & UNIFY_ALLOW_INTEGER)
23498 && CP_INTEGRAL_TYPE_P (tparm))
23499 /* Convert the ARG to the type of PARM; the deduced non-type
23500 template argument must exactly match the types of the
23501 corresponding parameter. */
23502 arg = fold (build_nop (tparm, arg));
23503 else if (uses_template_parms (tparm))
23504 {
23505 /* We haven't deduced the type of this parameter yet. */
23506 if (cxx_dialect >= cxx17
23507 /* We deduce from array bounds in try_array_deduction. */
23508 && !(strict & UNIFY_ALLOW_INTEGER))
23509 {
23510 /* Deduce it from the non-type argument. */
23511 tree atype = TREE_TYPE (arg);
23512 RECUR_AND_CHECK_FAILURE (tparms, targs,
23513 tparm, atype,
23514 UNIFY_ALLOW_NONE, explain_p);
23515 }
23516 else
23517 /* Try again later. */
23518 return unify_success (explain_p);
23519 }
23520 else
23521 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23522
23523 /* If ARG is a parameter pack or an expansion, we cannot unify
23524 against it unless PARM is also a parameter pack. */
23525 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23526 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23527 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23528
23529 {
23530 bool removed_attr = false;
23531 arg = strip_typedefs_expr (arg, &removed_attr);
23532 }
23533 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23534 return unify_success (explain_p);
23535
23536 case PTRMEM_CST:
23537 {
23538 /* A pointer-to-member constant can be unified only with
23539 another constant. */
23540 if (TREE_CODE (arg) != PTRMEM_CST)
23541 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23542
23543 /* Just unify the class member. It would be useless (and possibly
23544 wrong, depending on the strict flags) to unify also
23545 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23546 arg refer to the same variable, even if through different
23547 classes. For instance:
23548
23549 struct A { int x; };
23550 struct B : A { };
23551
23552 Unification of &A::x and &B::x must succeed. */
23553 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23554 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23555 }
23556
23557 case POINTER_TYPE:
23558 {
23559 if (!TYPE_PTR_P (arg))
23560 return unify_type_mismatch (explain_p, parm, arg);
23561
23562 /* [temp.deduct.call]
23563
23564 A can be another pointer or pointer to member type that can
23565 be converted to the deduced A via a qualification
23566 conversion (_conv.qual_).
23567
23568 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23569 This will allow for additional cv-qualification of the
23570 pointed-to types if appropriate. */
23571
23572 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23573 /* The derived-to-base conversion only persists through one
23574 level of pointers. */
23575 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23576
23577 return unify (tparms, targs, TREE_TYPE (parm),
23578 TREE_TYPE (arg), strict, explain_p);
23579 }
23580
23581 case REFERENCE_TYPE:
23582 if (!TYPE_REF_P (arg))
23583 return unify_type_mismatch (explain_p, parm, arg);
23584 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23585 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23586
23587 case ARRAY_TYPE:
23588 if (TREE_CODE (arg) != ARRAY_TYPE)
23589 return unify_type_mismatch (explain_p, parm, arg);
23590 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23591 != (TYPE_DOMAIN (arg) == NULL_TREE))
23592 return unify_type_mismatch (explain_p, parm, arg);
23593 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23594 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23595 if (TYPE_DOMAIN (parm) != NULL_TREE)
23596 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23597 TYPE_DOMAIN (arg), explain_p);
23598 return unify_success (explain_p);
23599
23600 case REAL_TYPE:
23601 case COMPLEX_TYPE:
23602 case VECTOR_TYPE:
23603 case INTEGER_TYPE:
23604 case BOOLEAN_TYPE:
23605 case ENUMERAL_TYPE:
23606 case VOID_TYPE:
23607 case NULLPTR_TYPE:
23608 if (TREE_CODE (arg) != TREE_CODE (parm))
23609 return unify_type_mismatch (explain_p, parm, arg);
23610
23611 /* We have already checked cv-qualification at the top of the
23612 function. */
23613 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23614 return unify_type_mismatch (explain_p, parm, arg);
23615
23616 /* As far as unification is concerned, this wins. Later checks
23617 will invalidate it if necessary. */
23618 return unify_success (explain_p);
23619
23620 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23621 /* Type INTEGER_CST can come from ordinary constant template args. */
23622 case INTEGER_CST:
23623 while (CONVERT_EXPR_P (arg))
23624 arg = TREE_OPERAND (arg, 0);
23625
23626 if (TREE_CODE (arg) != INTEGER_CST)
23627 return unify_template_argument_mismatch (explain_p, parm, arg);
23628 return (tree_int_cst_equal (parm, arg)
23629 ? unify_success (explain_p)
23630 : unify_template_argument_mismatch (explain_p, parm, arg));
23631
23632 case TREE_VEC:
23633 {
23634 int i, len, argslen;
23635 int parm_variadic_p = 0;
23636
23637 if (TREE_CODE (arg) != TREE_VEC)
23638 return unify_template_argument_mismatch (explain_p, parm, arg);
23639
23640 len = TREE_VEC_LENGTH (parm);
23641 argslen = TREE_VEC_LENGTH (arg);
23642
23643 /* Check for pack expansions in the parameters. */
23644 for (i = 0; i < len; ++i)
23645 {
23646 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23647 {
23648 if (i == len - 1)
23649 /* We can unify against something with a trailing
23650 parameter pack. */
23651 parm_variadic_p = 1;
23652 else
23653 /* [temp.deduct.type]/9: If the template argument list of
23654 P contains a pack expansion that is not the last
23655 template argument, the entire template argument list
23656 is a non-deduced context. */
23657 return unify_success (explain_p);
23658 }
23659 }
23660
23661 /* If we don't have enough arguments to satisfy the parameters
23662 (not counting the pack expression at the end), or we have
23663 too many arguments for a parameter list that doesn't end in
23664 a pack expression, we can't unify. */
23665 if (parm_variadic_p
23666 ? argslen < len - parm_variadic_p
23667 : argslen != len)
23668 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23669
23670 /* Unify all of the parameters that precede the (optional)
23671 pack expression. */
23672 for (i = 0; i < len - parm_variadic_p; ++i)
23673 {
23674 RECUR_AND_CHECK_FAILURE (tparms, targs,
23675 TREE_VEC_ELT (parm, i),
23676 TREE_VEC_ELT (arg, i),
23677 UNIFY_ALLOW_NONE, explain_p);
23678 }
23679 if (parm_variadic_p)
23680 return unify_pack_expansion (tparms, targs, parm, arg,
23681 DEDUCE_EXACT,
23682 /*subr=*/true, explain_p);
23683 return unify_success (explain_p);
23684 }
23685
23686 case RECORD_TYPE:
23687 case UNION_TYPE:
23688 if (TREE_CODE (arg) != TREE_CODE (parm))
23689 return unify_type_mismatch (explain_p, parm, arg);
23690
23691 if (TYPE_PTRMEMFUNC_P (parm))
23692 {
23693 if (!TYPE_PTRMEMFUNC_P (arg))
23694 return unify_type_mismatch (explain_p, parm, arg);
23695
23696 return unify (tparms, targs,
23697 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23698 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23699 strict, explain_p);
23700 }
23701 else if (TYPE_PTRMEMFUNC_P (arg))
23702 return unify_type_mismatch (explain_p, parm, arg);
23703
23704 if (CLASSTYPE_TEMPLATE_INFO (parm))
23705 {
23706 tree t = NULL_TREE;
23707
23708 if (strict_in & UNIFY_ALLOW_DERIVED)
23709 {
23710 /* First, we try to unify the PARM and ARG directly. */
23711 t = try_class_unification (tparms, targs,
23712 parm, arg, explain_p);
23713
23714 if (!t)
23715 {
23716 /* Fallback to the special case allowed in
23717 [temp.deduct.call]:
23718
23719 If P is a class, and P has the form
23720 template-id, then A can be a derived class of
23721 the deduced A. Likewise, if P is a pointer to
23722 a class of the form template-id, A can be a
23723 pointer to a derived class pointed to by the
23724 deduced A. */
23725 enum template_base_result r;
23726 r = get_template_base (tparms, targs, parm, arg,
23727 explain_p, &t);
23728
23729 if (!t)
23730 {
23731 /* Don't give the derived diagnostic if we're
23732 already dealing with the same template. */
23733 bool same_template
23734 = (CLASSTYPE_TEMPLATE_INFO (arg)
23735 && (CLASSTYPE_TI_TEMPLATE (parm)
23736 == CLASSTYPE_TI_TEMPLATE (arg)));
23737 return unify_no_common_base (explain_p && !same_template,
23738 r, parm, arg);
23739 }
23740 }
23741 }
23742 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23743 && (CLASSTYPE_TI_TEMPLATE (parm)
23744 == CLASSTYPE_TI_TEMPLATE (arg)))
23745 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23746 Then, we should unify `int' and `U'. */
23747 t = arg;
23748 else
23749 /* There's no chance of unification succeeding. */
23750 return unify_type_mismatch (explain_p, parm, arg);
23751
23752 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23753 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23754 }
23755 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23756 return unify_type_mismatch (explain_p, parm, arg);
23757 return unify_success (explain_p);
23758
23759 case METHOD_TYPE:
23760 case FUNCTION_TYPE:
23761 {
23762 unsigned int nargs;
23763 tree *args;
23764 tree a;
23765 unsigned int i;
23766
23767 if (TREE_CODE (arg) != TREE_CODE (parm))
23768 return unify_type_mismatch (explain_p, parm, arg);
23769
23770 /* CV qualifications for methods can never be deduced, they must
23771 match exactly. We need to check them explicitly here,
23772 because type_unification_real treats them as any other
23773 cv-qualified parameter. */
23774 if (TREE_CODE (parm) == METHOD_TYPE
23775 && (!check_cv_quals_for_unify
23776 (UNIFY_ALLOW_NONE,
23777 class_of_this_parm (arg),
23778 class_of_this_parm (parm))))
23779 return unify_cv_qual_mismatch (explain_p, parm, arg);
23780 if (TREE_CODE (arg) == FUNCTION_TYPE
23781 && type_memfn_quals (parm) != type_memfn_quals (arg))
23782 return unify_cv_qual_mismatch (explain_p, parm, arg);
23783 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23784 return unify_type_mismatch (explain_p, parm, arg);
23785
23786 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23787 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23788
23789 nargs = list_length (TYPE_ARG_TYPES (arg));
23790 args = XALLOCAVEC (tree, nargs);
23791 for (a = TYPE_ARG_TYPES (arg), i = 0;
23792 a != NULL_TREE && a != void_list_node;
23793 a = TREE_CHAIN (a), ++i)
23794 args[i] = TREE_VALUE (a);
23795 nargs = i;
23796
23797 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23798 args, nargs, 1, DEDUCE_EXACT,
23799 NULL, explain_p))
23800 return 1;
23801
23802 if (flag_noexcept_type)
23803 {
23804 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23805 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23806 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23807 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23808 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23809 && uses_template_parms (TREE_PURPOSE (pspec)))
23810 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23811 TREE_PURPOSE (aspec),
23812 UNIFY_ALLOW_NONE, explain_p);
23813 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23814 return unify_type_mismatch (explain_p, parm, arg);
23815 }
23816
23817 return 0;
23818 }
23819
23820 case OFFSET_TYPE:
23821 /* Unify a pointer to member with a pointer to member function, which
23822 deduces the type of the member as a function type. */
23823 if (TYPE_PTRMEMFUNC_P (arg))
23824 {
23825 /* Check top-level cv qualifiers */
23826 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23827 return unify_cv_qual_mismatch (explain_p, parm, arg);
23828
23829 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23830 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23831 UNIFY_ALLOW_NONE, explain_p);
23832
23833 /* Determine the type of the function we are unifying against. */
23834 tree fntype = static_fn_type (arg);
23835
23836 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23837 }
23838
23839 if (TREE_CODE (arg) != OFFSET_TYPE)
23840 return unify_type_mismatch (explain_p, parm, arg);
23841 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23842 TYPE_OFFSET_BASETYPE (arg),
23843 UNIFY_ALLOW_NONE, explain_p);
23844 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23845 strict, explain_p);
23846
23847 case CONST_DECL:
23848 if (DECL_TEMPLATE_PARM_P (parm))
23849 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23850 if (arg != scalar_constant_value (parm))
23851 return unify_template_argument_mismatch (explain_p, parm, arg);
23852 return unify_success (explain_p);
23853
23854 case FIELD_DECL:
23855 case TEMPLATE_DECL:
23856 /* Matched cases are handled by the ARG == PARM test above. */
23857 return unify_template_argument_mismatch (explain_p, parm, arg);
23858
23859 case VAR_DECL:
23860 /* We might get a variable as a non-type template argument in parm if the
23861 corresponding parameter is type-dependent. Make any necessary
23862 adjustments based on whether arg is a reference. */
23863 if (CONSTANT_CLASS_P (arg))
23864 parm = fold_non_dependent_expr (parm, complain);
23865 else if (REFERENCE_REF_P (arg))
23866 {
23867 tree sub = TREE_OPERAND (arg, 0);
23868 STRIP_NOPS (sub);
23869 if (TREE_CODE (sub) == ADDR_EXPR)
23870 arg = TREE_OPERAND (sub, 0);
23871 }
23872 /* Now use the normal expression code to check whether they match. */
23873 goto expr;
23874
23875 case TYPE_ARGUMENT_PACK:
23876 case NONTYPE_ARGUMENT_PACK:
23877 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23878 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23879
23880 case TYPEOF_TYPE:
23881 case DECLTYPE_TYPE:
23882 case UNDERLYING_TYPE:
23883 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23884 or UNDERLYING_TYPE nodes. */
23885 return unify_success (explain_p);
23886
23887 case ERROR_MARK:
23888 /* Unification fails if we hit an error node. */
23889 return unify_invalid (explain_p);
23890
23891 case INDIRECT_REF:
23892 if (REFERENCE_REF_P (parm))
23893 {
23894 bool pexp = PACK_EXPANSION_P (arg);
23895 if (pexp)
23896 arg = PACK_EXPANSION_PATTERN (arg);
23897 if (REFERENCE_REF_P (arg))
23898 arg = TREE_OPERAND (arg, 0);
23899 if (pexp)
23900 arg = make_pack_expansion (arg, complain);
23901 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23902 strict, explain_p);
23903 }
23904 /* FALLTHRU */
23905
23906 default:
23907 /* An unresolved overload is a nondeduced context. */
23908 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23909 return unify_success (explain_p);
23910 gcc_assert (EXPR_P (parm)
23911 || COMPOUND_LITERAL_P (parm)
23912 || TREE_CODE (parm) == TRAIT_EXPR);
23913 expr:
23914 /* We must be looking at an expression. This can happen with
23915 something like:
23916
23917 template <int I>
23918 void foo(S<I>, S<I + 2>);
23919
23920 or
23921
23922 template<typename T>
23923 void foo(A<T, T{}>);
23924
23925 This is a "non-deduced context":
23926
23927 [deduct.type]
23928
23929 The non-deduced contexts are:
23930
23931 --A non-type template argument or an array bound in which
23932 a subexpression references a template parameter.
23933
23934 In these cases, we assume deduction succeeded, but don't
23935 actually infer any unifications. */
23936
23937 if (!uses_template_parms (parm)
23938 && !template_args_equal (parm, arg))
23939 return unify_expression_unequal (explain_p, parm, arg);
23940 else
23941 return unify_success (explain_p);
23942 }
23943 }
23944 #undef RECUR_AND_CHECK_FAILURE
23945 \f
23946 /* Note that DECL can be defined in this translation unit, if
23947 required. */
23948
23949 static void
23950 mark_definable (tree decl)
23951 {
23952 tree clone;
23953 DECL_NOT_REALLY_EXTERN (decl) = 1;
23954 FOR_EACH_CLONE (clone, decl)
23955 DECL_NOT_REALLY_EXTERN (clone) = 1;
23956 }
23957
23958 /* Called if RESULT is explicitly instantiated, or is a member of an
23959 explicitly instantiated class. */
23960
23961 void
23962 mark_decl_instantiated (tree result, int extern_p)
23963 {
23964 SET_DECL_EXPLICIT_INSTANTIATION (result);
23965
23966 /* If this entity has already been written out, it's too late to
23967 make any modifications. */
23968 if (TREE_ASM_WRITTEN (result))
23969 return;
23970
23971 /* For anonymous namespace we don't need to do anything. */
23972 if (decl_anon_ns_mem_p (result))
23973 {
23974 gcc_assert (!TREE_PUBLIC (result));
23975 return;
23976 }
23977
23978 if (TREE_CODE (result) != FUNCTION_DECL)
23979 /* The TREE_PUBLIC flag for function declarations will have been
23980 set correctly by tsubst. */
23981 TREE_PUBLIC (result) = 1;
23982
23983 /* This might have been set by an earlier implicit instantiation. */
23984 DECL_COMDAT (result) = 0;
23985
23986 if (extern_p)
23987 DECL_NOT_REALLY_EXTERN (result) = 0;
23988 else
23989 {
23990 mark_definable (result);
23991 mark_needed (result);
23992 /* Always make artificials weak. */
23993 if (DECL_ARTIFICIAL (result) && flag_weak)
23994 comdat_linkage (result);
23995 /* For WIN32 we also want to put explicit instantiations in
23996 linkonce sections. */
23997 else if (TREE_PUBLIC (result))
23998 maybe_make_one_only (result);
23999 if (TREE_CODE (result) == FUNCTION_DECL
24000 && DECL_TEMPLATE_INSTANTIATED (result))
24001 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24002 since start_preparsed_function wouldn't have if we had an earlier
24003 extern explicit instantiation. */
24004 DECL_EXTERNAL (result) = 0;
24005 }
24006
24007 /* If EXTERN_P, then this function will not be emitted -- unless
24008 followed by an explicit instantiation, at which point its linkage
24009 will be adjusted. If !EXTERN_P, then this function will be
24010 emitted here. In neither circumstance do we want
24011 import_export_decl to adjust the linkage. */
24012 DECL_INTERFACE_KNOWN (result) = 1;
24013 }
24014
24015 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24016 important template arguments. If any are missing, we check whether
24017 they're important by using error_mark_node for substituting into any
24018 args that were used for partial ordering (the ones between ARGS and END)
24019 and seeing if it bubbles up. */
24020
24021 static bool
24022 check_undeduced_parms (tree targs, tree args, tree end)
24023 {
24024 bool found = false;
24025 int i;
24026 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
24027 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
24028 {
24029 found = true;
24030 TREE_VEC_ELT (targs, i) = error_mark_node;
24031 }
24032 if (found)
24033 {
24034 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
24035 if (substed == error_mark_node)
24036 return true;
24037 }
24038 return false;
24039 }
24040
24041 /* Given two function templates PAT1 and PAT2, return:
24042
24043 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24044 -1 if PAT2 is more specialized than PAT1.
24045 0 if neither is more specialized.
24046
24047 LEN indicates the number of parameters we should consider
24048 (defaulted parameters should not be considered).
24049
24050 The 1998 std underspecified function template partial ordering, and
24051 DR214 addresses the issue. We take pairs of arguments, one from
24052 each of the templates, and deduce them against each other. One of
24053 the templates will be more specialized if all the *other*
24054 template's arguments deduce against its arguments and at least one
24055 of its arguments *does* *not* deduce against the other template's
24056 corresponding argument. Deduction is done as for class templates.
24057 The arguments used in deduction have reference and top level cv
24058 qualifiers removed. Iff both arguments were originally reference
24059 types *and* deduction succeeds in both directions, an lvalue reference
24060 wins against an rvalue reference and otherwise the template
24061 with the more cv-qualified argument wins for that pairing (if
24062 neither is more cv-qualified, they both are equal). Unlike regular
24063 deduction, after all the arguments have been deduced in this way,
24064 we do *not* verify the deduced template argument values can be
24065 substituted into non-deduced contexts.
24066
24067 The logic can be a bit confusing here, because we look at deduce1 and
24068 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24069 can find template arguments for pat1 to make arg1 look like arg2, that
24070 means that arg2 is at least as specialized as arg1. */
24071
24072 int
24073 more_specialized_fn (tree pat1, tree pat2, int len)
24074 {
24075 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
24076 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
24077 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
24078 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
24079 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
24080 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
24081 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
24082 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
24083 tree origs1, origs2;
24084 bool lose1 = false;
24085 bool lose2 = false;
24086
24087 /* Remove the this parameter from non-static member functions. If
24088 one is a non-static member function and the other is not a static
24089 member function, remove the first parameter from that function
24090 also. This situation occurs for operator functions where we
24091 locate both a member function (with this pointer) and non-member
24092 operator (with explicit first operand). */
24093 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24094 {
24095 len--; /* LEN is the number of significant arguments for DECL1 */
24096 args1 = TREE_CHAIN (args1);
24097 if (!DECL_STATIC_FUNCTION_P (decl2))
24098 args2 = TREE_CHAIN (args2);
24099 }
24100 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24101 {
24102 args2 = TREE_CHAIN (args2);
24103 if (!DECL_STATIC_FUNCTION_P (decl1))
24104 {
24105 len--;
24106 args1 = TREE_CHAIN (args1);
24107 }
24108 }
24109
24110 /* If only one is a conversion operator, they are unordered. */
24111 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24112 return 0;
24113
24114 /* Consider the return type for a conversion function */
24115 if (DECL_CONV_FN_P (decl1))
24116 {
24117 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24118 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24119 len++;
24120 }
24121
24122 processing_template_decl++;
24123
24124 origs1 = args1;
24125 origs2 = args2;
24126
24127 while (len--
24128 /* Stop when an ellipsis is seen. */
24129 && args1 != NULL_TREE && args2 != NULL_TREE)
24130 {
24131 tree arg1 = TREE_VALUE (args1);
24132 tree arg2 = TREE_VALUE (args2);
24133 int deduce1, deduce2;
24134 int quals1 = -1;
24135 int quals2 = -1;
24136 int ref1 = 0;
24137 int ref2 = 0;
24138
24139 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24140 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24141 {
24142 /* When both arguments are pack expansions, we need only
24143 unify the patterns themselves. */
24144 arg1 = PACK_EXPANSION_PATTERN (arg1);
24145 arg2 = PACK_EXPANSION_PATTERN (arg2);
24146
24147 /* This is the last comparison we need to do. */
24148 len = 0;
24149 }
24150
24151 if (TYPE_REF_P (arg1))
24152 {
24153 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24154 arg1 = TREE_TYPE (arg1);
24155 quals1 = cp_type_quals (arg1);
24156 }
24157
24158 if (TYPE_REF_P (arg2))
24159 {
24160 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24161 arg2 = TREE_TYPE (arg2);
24162 quals2 = cp_type_quals (arg2);
24163 }
24164
24165 arg1 = TYPE_MAIN_VARIANT (arg1);
24166 arg2 = TYPE_MAIN_VARIANT (arg2);
24167
24168 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24169 {
24170 int i, len2 = remaining_arguments (args2);
24171 tree parmvec = make_tree_vec (1);
24172 tree argvec = make_tree_vec (len2);
24173 tree ta = args2;
24174
24175 /* Setup the parameter vector, which contains only ARG1. */
24176 TREE_VEC_ELT (parmvec, 0) = arg1;
24177
24178 /* Setup the argument vector, which contains the remaining
24179 arguments. */
24180 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24181 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24182
24183 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24184 argvec, DEDUCE_EXACT,
24185 /*subr=*/true, /*explain_p=*/false)
24186 == 0);
24187
24188 /* We cannot deduce in the other direction, because ARG1 is
24189 a pack expansion but ARG2 is not. */
24190 deduce2 = 0;
24191 }
24192 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24193 {
24194 int i, len1 = remaining_arguments (args1);
24195 tree parmvec = make_tree_vec (1);
24196 tree argvec = make_tree_vec (len1);
24197 tree ta = args1;
24198
24199 /* Setup the parameter vector, which contains only ARG1. */
24200 TREE_VEC_ELT (parmvec, 0) = arg2;
24201
24202 /* Setup the argument vector, which contains the remaining
24203 arguments. */
24204 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24205 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24206
24207 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24208 argvec, DEDUCE_EXACT,
24209 /*subr=*/true, /*explain_p=*/false)
24210 == 0);
24211
24212 /* We cannot deduce in the other direction, because ARG2 is
24213 a pack expansion but ARG1 is not.*/
24214 deduce1 = 0;
24215 }
24216
24217 else
24218 {
24219 /* The normal case, where neither argument is a pack
24220 expansion. */
24221 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24222 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24223 == 0);
24224 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24225 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24226 == 0);
24227 }
24228
24229 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24230 arg2, then arg2 is not as specialized as arg1. */
24231 if (!deduce1)
24232 lose2 = true;
24233 if (!deduce2)
24234 lose1 = true;
24235
24236 /* "If, for a given type, deduction succeeds in both directions
24237 (i.e., the types are identical after the transformations above)
24238 and both P and A were reference types (before being replaced with
24239 the type referred to above):
24240 - if the type from the argument template was an lvalue reference and
24241 the type from the parameter template was not, the argument type is
24242 considered to be more specialized than the other; otherwise,
24243 - if the type from the argument template is more cv-qualified
24244 than the type from the parameter template (as described above),
24245 the argument type is considered to be more specialized than the other;
24246 otherwise,
24247 - neither type is more specialized than the other." */
24248
24249 if (deduce1 && deduce2)
24250 {
24251 if (ref1 && ref2 && ref1 != ref2)
24252 {
24253 if (ref1 > ref2)
24254 lose1 = true;
24255 else
24256 lose2 = true;
24257 }
24258 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24259 {
24260 if ((quals1 & quals2) == quals2)
24261 lose2 = true;
24262 if ((quals1 & quals2) == quals1)
24263 lose1 = true;
24264 }
24265 }
24266
24267 if (lose1 && lose2)
24268 /* We've failed to deduce something in either direction.
24269 These must be unordered. */
24270 break;
24271
24272 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24273 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24274 /* We have already processed all of the arguments in our
24275 handing of the pack expansion type. */
24276 len = 0;
24277
24278 args1 = TREE_CHAIN (args1);
24279 args2 = TREE_CHAIN (args2);
24280 }
24281
24282 /* "In most cases, all template parameters must have values in order for
24283 deduction to succeed, but for partial ordering purposes a template
24284 parameter may remain without a value provided it is not used in the
24285 types being used for partial ordering."
24286
24287 Thus, if we are missing any of the targs1 we need to substitute into
24288 origs1, then pat2 is not as specialized as pat1. This can happen when
24289 there is a nondeduced context. */
24290 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24291 lose2 = true;
24292 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24293 lose1 = true;
24294
24295 processing_template_decl--;
24296
24297 /* If both deductions succeed, the partial ordering selects the more
24298 constrained template. */
24299 /* P2113: If the corresponding template-parameters of the
24300 template-parameter-lists are not equivalent ([temp.over.link]) or if
24301 the function parameters that positionally correspond between the two
24302 templates are not of the same type, neither template is more
24303 specialized than the other. */
24304 if (!lose1 && !lose2
24305 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24306 DECL_TEMPLATE_PARMS (pat2))
24307 && compparms (origs1, origs2))
24308 {
24309 int winner = more_constrained (decl1, decl2);
24310 if (winner > 0)
24311 lose2 = true;
24312 else if (winner < 0)
24313 lose1 = true;
24314 }
24315
24316 /* All things being equal, if the next argument is a pack expansion
24317 for one function but not for the other, prefer the
24318 non-variadic function. FIXME this is bogus; see c++/41958. */
24319 if (lose1 == lose2
24320 && args1 && TREE_VALUE (args1)
24321 && args2 && TREE_VALUE (args2))
24322 {
24323 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24324 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24325 }
24326
24327 if (lose1 == lose2)
24328 return 0;
24329 else if (!lose1)
24330 return 1;
24331 else
24332 return -1;
24333 }
24334
24335 /* Determine which of two partial specializations of TMPL is more
24336 specialized.
24337
24338 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24339 to the first partial specialization. The TREE_PURPOSE is the
24340 innermost set of template parameters for the partial
24341 specialization. PAT2 is similar, but for the second template.
24342
24343 Return 1 if the first partial specialization is more specialized;
24344 -1 if the second is more specialized; 0 if neither is more
24345 specialized.
24346
24347 See [temp.class.order] for information about determining which of
24348 two templates is more specialized. */
24349
24350 static int
24351 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24352 {
24353 tree targs;
24354 int winner = 0;
24355 bool any_deductions = false;
24356
24357 tree tmpl1 = TREE_VALUE (pat1);
24358 tree tmpl2 = TREE_VALUE (pat2);
24359 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24360 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24361
24362 /* Just like what happens for functions, if we are ordering between
24363 different template specializations, we may encounter dependent
24364 types in the arguments, and we need our dependency check functions
24365 to behave correctly. */
24366 ++processing_template_decl;
24367 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24368 if (targs)
24369 {
24370 --winner;
24371 any_deductions = true;
24372 }
24373
24374 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24375 if (targs)
24376 {
24377 ++winner;
24378 any_deductions = true;
24379 }
24380 --processing_template_decl;
24381
24382 /* If both deductions succeed, the partial ordering selects the more
24383 constrained template. */
24384 if (!winner && any_deductions)
24385 winner = more_constrained (tmpl1, tmpl2);
24386
24387 /* In the case of a tie where at least one of the templates
24388 has a parameter pack at the end, the template with the most
24389 non-packed parameters wins. */
24390 if (winner == 0
24391 && any_deductions
24392 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24393 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24394 {
24395 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24396 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24397 int len1 = TREE_VEC_LENGTH (args1);
24398 int len2 = TREE_VEC_LENGTH (args2);
24399
24400 /* We don't count the pack expansion at the end. */
24401 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24402 --len1;
24403 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24404 --len2;
24405
24406 if (len1 > len2)
24407 return 1;
24408 else if (len1 < len2)
24409 return -1;
24410 }
24411
24412 return winner;
24413 }
24414
24415 /* Return the template arguments that will produce the function signature
24416 DECL from the function template FN, with the explicit template
24417 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24418 also match. Return NULL_TREE if no satisfactory arguments could be
24419 found. */
24420
24421 static tree
24422 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24423 {
24424 int ntparms = DECL_NTPARMS (fn);
24425 tree targs = make_tree_vec (ntparms);
24426 tree decl_type = TREE_TYPE (decl);
24427 tree decl_arg_types;
24428 tree *args;
24429 unsigned int nargs, ix;
24430 tree arg;
24431
24432 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24433
24434 /* Never do unification on the 'this' parameter. */
24435 decl_arg_types = skip_artificial_parms_for (decl,
24436 TYPE_ARG_TYPES (decl_type));
24437
24438 nargs = list_length (decl_arg_types);
24439 args = XALLOCAVEC (tree, nargs);
24440 for (arg = decl_arg_types, ix = 0;
24441 arg != NULL_TREE && arg != void_list_node;
24442 arg = TREE_CHAIN (arg), ++ix)
24443 args[ix] = TREE_VALUE (arg);
24444
24445 if (fn_type_unification (fn, explicit_args, targs,
24446 args, ix,
24447 (check_rettype || DECL_CONV_FN_P (fn)
24448 ? TREE_TYPE (decl_type) : NULL_TREE),
24449 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24450 /*explain_p=*/false,
24451 /*decltype*/false)
24452 == error_mark_node)
24453 return NULL_TREE;
24454
24455 return targs;
24456 }
24457
24458 /* Return the innermost template arguments that, when applied to a partial
24459 specialization SPEC_TMPL of TMPL, yield the ARGS.
24460
24461 For example, suppose we have:
24462
24463 template <class T, class U> struct S {};
24464 template <class T> struct S<T*, int> {};
24465
24466 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24467 partial specialization and the ARGS will be {double*, int}. The resulting
24468 vector will be {double}, indicating that `T' is bound to `double'. */
24469
24470 static tree
24471 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24472 {
24473 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24474 tree spec_args
24475 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24476 int i, ntparms = TREE_VEC_LENGTH (tparms);
24477 tree deduced_args;
24478 tree innermost_deduced_args;
24479
24480 innermost_deduced_args = make_tree_vec (ntparms);
24481 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24482 {
24483 deduced_args = copy_node (args);
24484 SET_TMPL_ARGS_LEVEL (deduced_args,
24485 TMPL_ARGS_DEPTH (deduced_args),
24486 innermost_deduced_args);
24487 }
24488 else
24489 deduced_args = innermost_deduced_args;
24490
24491 bool tried_array_deduction = (cxx_dialect < cxx17);
24492 again:
24493 if (unify (tparms, deduced_args,
24494 INNERMOST_TEMPLATE_ARGS (spec_args),
24495 INNERMOST_TEMPLATE_ARGS (args),
24496 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24497 return NULL_TREE;
24498
24499 for (i = 0; i < ntparms; ++i)
24500 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24501 {
24502 if (!tried_array_deduction)
24503 {
24504 try_array_deduction (tparms, innermost_deduced_args,
24505 INNERMOST_TEMPLATE_ARGS (spec_args));
24506 tried_array_deduction = true;
24507 if (TREE_VEC_ELT (innermost_deduced_args, i))
24508 goto again;
24509 }
24510 return NULL_TREE;
24511 }
24512
24513 if (!push_tinst_level (spec_tmpl, deduced_args))
24514 {
24515 excessive_deduction_depth = true;
24516 return NULL_TREE;
24517 }
24518
24519 /* Verify that nondeduced template arguments agree with the type
24520 obtained from argument deduction.
24521
24522 For example:
24523
24524 struct A { typedef int X; };
24525 template <class T, class U> struct C {};
24526 template <class T> struct C<T, typename T::X> {};
24527
24528 Then with the instantiation `C<A, int>', we can deduce that
24529 `T' is `A' but unify () does not check whether `typename T::X'
24530 is `int'. */
24531 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24532
24533 if (spec_args != error_mark_node)
24534 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24535 INNERMOST_TEMPLATE_ARGS (spec_args),
24536 tmpl, tf_none, false, false);
24537
24538 pop_tinst_level ();
24539
24540 if (spec_args == error_mark_node
24541 /* We only need to check the innermost arguments; the other
24542 arguments will always agree. */
24543 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24544 INNERMOST_TEMPLATE_ARGS (args)))
24545 return NULL_TREE;
24546
24547 /* Now that we have bindings for all of the template arguments,
24548 ensure that the arguments deduced for the template template
24549 parameters have compatible template parameter lists. See the use
24550 of template_template_parm_bindings_ok_p in fn_type_unification
24551 for more information. */
24552 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24553 return NULL_TREE;
24554
24555 return deduced_args;
24556 }
24557
24558 // Compare two function templates T1 and T2 by deducing bindings
24559 // from one against the other. If both deductions succeed, compare
24560 // constraints to see which is more constrained.
24561 static int
24562 more_specialized_inst (tree t1, tree t2)
24563 {
24564 int fate = 0;
24565 int count = 0;
24566
24567 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24568 {
24569 --fate;
24570 ++count;
24571 }
24572
24573 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24574 {
24575 ++fate;
24576 ++count;
24577 }
24578
24579 // If both deductions succeed, then one may be more constrained.
24580 if (count == 2 && fate == 0)
24581 fate = more_constrained (t1, t2);
24582
24583 return fate;
24584 }
24585
24586 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24587 Return the TREE_LIST node with the most specialized template, if
24588 any. If there is no most specialized template, the error_mark_node
24589 is returned.
24590
24591 Note that this function does not look at, or modify, the
24592 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24593 returned is one of the elements of INSTANTIATIONS, callers may
24594 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24595 and retrieve it from the value returned. */
24596
24597 tree
24598 most_specialized_instantiation (tree templates)
24599 {
24600 tree fn, champ;
24601
24602 ++processing_template_decl;
24603
24604 champ = templates;
24605 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24606 {
24607 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24608 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24609 if (fate == -1)
24610 champ = fn;
24611 else if (!fate)
24612 {
24613 /* Equally specialized, move to next function. If there
24614 is no next function, nothing's most specialized. */
24615 fn = TREE_CHAIN (fn);
24616 champ = fn;
24617 if (!fn)
24618 break;
24619 }
24620 }
24621
24622 if (champ)
24623 /* Now verify that champ is better than everything earlier in the
24624 instantiation list. */
24625 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24626 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24627 {
24628 champ = NULL_TREE;
24629 break;
24630 }
24631 }
24632
24633 processing_template_decl--;
24634
24635 if (!champ)
24636 return error_mark_node;
24637
24638 return champ;
24639 }
24640
24641 /* If DECL is a specialization of some template, return the most
24642 general such template. Otherwise, returns NULL_TREE.
24643
24644 For example, given:
24645
24646 template <class T> struct S { template <class U> void f(U); };
24647
24648 if TMPL is `template <class U> void S<int>::f(U)' this will return
24649 the full template. This function will not trace past partial
24650 specializations, however. For example, given in addition:
24651
24652 template <class T> struct S<T*> { template <class U> void f(U); };
24653
24654 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24655 `template <class T> template <class U> S<T*>::f(U)'. */
24656
24657 tree
24658 most_general_template (tree decl)
24659 {
24660 if (TREE_CODE (decl) != TEMPLATE_DECL)
24661 {
24662 if (tree tinfo = get_template_info (decl))
24663 decl = TI_TEMPLATE (tinfo);
24664 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24665 template friend, or a FIELD_DECL for a capture pack. */
24666 if (TREE_CODE (decl) != TEMPLATE_DECL)
24667 return NULL_TREE;
24668 }
24669
24670 /* Look for more and more general templates. */
24671 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24672 {
24673 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24674 (See cp-tree.h for details.) */
24675 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24676 break;
24677
24678 if (CLASS_TYPE_P (TREE_TYPE (decl))
24679 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24680 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24681 break;
24682
24683 /* Stop if we run into an explicitly specialized class template. */
24684 if (!DECL_NAMESPACE_SCOPE_P (decl)
24685 && DECL_CONTEXT (decl)
24686 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24687 break;
24688
24689 decl = DECL_TI_TEMPLATE (decl);
24690 }
24691
24692 return decl;
24693 }
24694
24695 /* Return the most specialized of the template partial specializations
24696 which can produce TARGET, a specialization of some class or variable
24697 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24698 a TEMPLATE_DECL node corresponding to the partial specialization, while
24699 the TREE_PURPOSE is the set of template arguments that must be
24700 substituted into the template pattern in order to generate TARGET.
24701
24702 If the choice of partial specialization is ambiguous, a diagnostic
24703 is issued, and the error_mark_node is returned. If there are no
24704 partial specializations matching TARGET, then NULL_TREE is
24705 returned, indicating that the primary template should be used. */
24706
24707 tree
24708 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24709 {
24710 tree list = NULL_TREE;
24711 tree t;
24712 tree champ;
24713 int fate;
24714 bool ambiguous_p;
24715 tree outer_args = NULL_TREE;
24716 tree tmpl, args;
24717
24718 if (TYPE_P (target))
24719 {
24720 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24721 tmpl = TI_TEMPLATE (tinfo);
24722 args = TI_ARGS (tinfo);
24723 }
24724 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24725 {
24726 tmpl = TREE_OPERAND (target, 0);
24727 args = TREE_OPERAND (target, 1);
24728 }
24729 else if (VAR_P (target))
24730 {
24731 tree tinfo = DECL_TEMPLATE_INFO (target);
24732 tmpl = TI_TEMPLATE (tinfo);
24733 args = TI_ARGS (tinfo);
24734 }
24735 else
24736 gcc_unreachable ();
24737
24738 tree main_tmpl = most_general_template (tmpl);
24739
24740 /* For determining which partial specialization to use, only the
24741 innermost args are interesting. */
24742 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24743 {
24744 outer_args = strip_innermost_template_args (args, 1);
24745 args = INNERMOST_TEMPLATE_ARGS (args);
24746 }
24747
24748 /* The caller hasn't called push_to_top_level yet, but we need
24749 get_partial_spec_bindings to be done in non-template context so that we'll
24750 fully resolve everything. */
24751 processing_template_decl_sentinel ptds;
24752
24753 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24754 {
24755 const tree ospec_tmpl = TREE_VALUE (t);
24756
24757 tree spec_tmpl;
24758 if (outer_args)
24759 {
24760 /* Substitute in the template args from the enclosing class. */
24761 ++processing_template_decl;
24762 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
24763 --processing_template_decl;
24764 if (spec_tmpl == error_mark_node)
24765 return error_mark_node;
24766 }
24767 else
24768 spec_tmpl = ospec_tmpl;
24769
24770 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24771 if (spec_args)
24772 {
24773 if (outer_args)
24774 spec_args = add_to_template_args (outer_args, spec_args);
24775
24776 /* Keep the candidate only if the constraints are satisfied,
24777 or if we're not compiling with concepts. */
24778 if (!flag_concepts
24779 || constraints_satisfied_p (ospec_tmpl, spec_args))
24780 {
24781 list = tree_cons (spec_args, ospec_tmpl, list);
24782 TREE_TYPE (list) = TREE_TYPE (t);
24783 }
24784 }
24785 }
24786
24787 if (! list)
24788 return NULL_TREE;
24789
24790 ambiguous_p = false;
24791 t = list;
24792 champ = t;
24793 t = TREE_CHAIN (t);
24794 for (; t; t = TREE_CHAIN (t))
24795 {
24796 fate = more_specialized_partial_spec (tmpl, champ, t);
24797 if (fate == 1)
24798 ;
24799 else
24800 {
24801 if (fate == 0)
24802 {
24803 t = TREE_CHAIN (t);
24804 if (! t)
24805 {
24806 ambiguous_p = true;
24807 break;
24808 }
24809 }
24810 champ = t;
24811 }
24812 }
24813
24814 if (!ambiguous_p)
24815 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24816 {
24817 fate = more_specialized_partial_spec (tmpl, champ, t);
24818 if (fate != 1)
24819 {
24820 ambiguous_p = true;
24821 break;
24822 }
24823 }
24824
24825 if (ambiguous_p)
24826 {
24827 const char *str;
24828 char *spaces = NULL;
24829 if (!(complain & tf_error))
24830 return error_mark_node;
24831 if (TYPE_P (target))
24832 error ("ambiguous template instantiation for %q#T", target);
24833 else
24834 error ("ambiguous template instantiation for %q#D", target);
24835 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24836 for (t = list; t; t = TREE_CHAIN (t))
24837 {
24838 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24839 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24840 "%s %#qS", spaces ? spaces : str, subst);
24841 spaces = spaces ? spaces : get_spaces (str);
24842 }
24843 free (spaces);
24844 return error_mark_node;
24845 }
24846
24847 return champ;
24848 }
24849
24850 /* Explicitly instantiate DECL. */
24851
24852 void
24853 do_decl_instantiation (tree decl, tree storage)
24854 {
24855 tree result = NULL_TREE;
24856 int extern_p = 0;
24857
24858 if (!decl || decl == error_mark_node)
24859 /* An error occurred, for which grokdeclarator has already issued
24860 an appropriate message. */
24861 return;
24862 else if (! DECL_LANG_SPECIFIC (decl))
24863 {
24864 error ("explicit instantiation of non-template %q#D", decl);
24865 return;
24866 }
24867 else if (DECL_DECLARED_CONCEPT_P (decl))
24868 {
24869 if (VAR_P (decl))
24870 error ("explicit instantiation of variable concept %q#D", decl);
24871 else
24872 error ("explicit instantiation of function concept %q#D", decl);
24873 return;
24874 }
24875
24876 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24877 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24878
24879 if (VAR_P (decl) && !var_templ)
24880 {
24881 /* There is an asymmetry here in the way VAR_DECLs and
24882 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24883 the latter, the DECL we get back will be marked as a
24884 template instantiation, and the appropriate
24885 DECL_TEMPLATE_INFO will be set up. This does not happen for
24886 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24887 should handle VAR_DECLs as it currently handles
24888 FUNCTION_DECLs. */
24889 if (!DECL_CLASS_SCOPE_P (decl))
24890 {
24891 error ("%qD is not a static data member of a class template", decl);
24892 return;
24893 }
24894 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24895 if (!result || !VAR_P (result))
24896 {
24897 error ("no matching template for %qD found", decl);
24898 return;
24899 }
24900 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24901 {
24902 error ("type %qT for explicit instantiation %qD does not match "
24903 "declared type %qT", TREE_TYPE (result), decl,
24904 TREE_TYPE (decl));
24905 return;
24906 }
24907 }
24908 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24909 {
24910 error ("explicit instantiation of %q#D", decl);
24911 return;
24912 }
24913 else
24914 result = decl;
24915
24916 /* Check for various error cases. Note that if the explicit
24917 instantiation is valid the RESULT will currently be marked as an
24918 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24919 until we get here. */
24920
24921 if (DECL_TEMPLATE_SPECIALIZATION (result))
24922 {
24923 /* DR 259 [temp.spec].
24924
24925 Both an explicit instantiation and a declaration of an explicit
24926 specialization shall not appear in a program unless the explicit
24927 instantiation follows a declaration of the explicit specialization.
24928
24929 For a given set of template parameters, if an explicit
24930 instantiation of a template appears after a declaration of an
24931 explicit specialization for that template, the explicit
24932 instantiation has no effect. */
24933 return;
24934 }
24935 else if (DECL_EXPLICIT_INSTANTIATION (result))
24936 {
24937 /* [temp.spec]
24938
24939 No program shall explicitly instantiate any template more
24940 than once.
24941
24942 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24943 the first instantiation was `extern' and the second is not,
24944 and EXTERN_P for the opposite case. */
24945 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24946 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24947 /* If an "extern" explicit instantiation follows an ordinary
24948 explicit instantiation, the template is instantiated. */
24949 if (extern_p)
24950 return;
24951 }
24952 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24953 {
24954 error ("no matching template for %qD found", result);
24955 return;
24956 }
24957 else if (!DECL_TEMPLATE_INFO (result))
24958 {
24959 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24960 return;
24961 }
24962
24963 if (storage == NULL_TREE)
24964 ;
24965 else if (storage == ridpointers[(int) RID_EXTERN])
24966 {
24967 if (cxx_dialect == cxx98)
24968 pedwarn (input_location, OPT_Wpedantic,
24969 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24970 "instantiations");
24971 extern_p = 1;
24972 }
24973 else
24974 error ("storage class %qD applied to template instantiation", storage);
24975
24976 check_explicit_instantiation_namespace (result);
24977 mark_decl_instantiated (result, extern_p);
24978 if (! extern_p)
24979 instantiate_decl (result, /*defer_ok=*/true,
24980 /*expl_inst_class_mem_p=*/false);
24981 }
24982
24983 static void
24984 mark_class_instantiated (tree t, int extern_p)
24985 {
24986 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24987 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24988 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24989 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24990 if (! extern_p)
24991 {
24992 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24993 rest_of_type_compilation (t, 1);
24994 }
24995 }
24996
24997 /* Perform an explicit instantiation of template class T. STORAGE, if
24998 non-null, is the RID for extern, inline or static. COMPLAIN is
24999 nonzero if this is called from the parser, zero if called recursively,
25000 since the standard is unclear (as detailed below). */
25001
25002 void
25003 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
25004 {
25005 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
25006 {
25007 if (tree ti = TYPE_TEMPLATE_INFO (t))
25008 error ("explicit instantiation of non-class template %qD",
25009 TI_TEMPLATE (ti));
25010 else
25011 error ("explicit instantiation of non-template type %qT", t);
25012 return;
25013 }
25014
25015 complete_type (t);
25016
25017 if (!COMPLETE_TYPE_P (t))
25018 {
25019 if (complain & tf_error)
25020 error ("explicit instantiation of %q#T before definition of template",
25021 t);
25022 return;
25023 }
25024
25025 /* At most one of these will be true. */
25026 bool extern_p = false;
25027 bool nomem_p = false;
25028 bool static_p = false;
25029
25030 if (storage != NULL_TREE)
25031 {
25032 if (storage == ridpointers[(int) RID_EXTERN])
25033 {
25034 if (cxx_dialect == cxx98)
25035 pedwarn (input_location, OPT_Wpedantic,
25036 "ISO C++ 1998 forbids the use of %<extern%> on "
25037 "explicit instantiations");
25038 }
25039 else
25040 pedwarn (input_location, OPT_Wpedantic,
25041 "ISO C++ forbids the use of %qE"
25042 " on explicit instantiations", storage);
25043
25044 if (storage == ridpointers[(int) RID_INLINE])
25045 nomem_p = true;
25046 else if (storage == ridpointers[(int) RID_EXTERN])
25047 extern_p = true;
25048 else if (storage == ridpointers[(int) RID_STATIC])
25049 static_p = true;
25050 else
25051 error ("storage class %qD applied to template instantiation",
25052 storage);
25053 }
25054
25055 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
25056 /* DR 259 [temp.spec].
25057
25058 Both an explicit instantiation and a declaration of an explicit
25059 specialization shall not appear in a program unless the
25060 explicit instantiation follows a declaration of the explicit
25061 specialization.
25062
25063 For a given set of template parameters, if an explicit
25064 instantiation of a template appears after a declaration of an
25065 explicit specialization for that template, the explicit
25066 instantiation has no effect. */
25067 return;
25068
25069 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
25070 {
25071 /* We've already instantiated the template. */
25072
25073 /* [temp.spec]
25074
25075 No program shall explicitly instantiate any template more
25076 than once.
25077
25078 If EXTERN_P then this is ok. */
25079 if (!extern_p && (complain & tf_error))
25080 permerror (input_location,
25081 "duplicate explicit instantiation of %q#T", t);
25082
25083 return;
25084 }
25085
25086 check_explicit_instantiation_namespace (TYPE_NAME (t));
25087 mark_class_instantiated (t, extern_p);
25088
25089 if (nomem_p)
25090 return;
25091
25092 /* In contrast to implicit instantiation, where only the
25093 declarations, and not the definitions, of members are
25094 instantiated, we have here:
25095
25096 [temp.explicit]
25097
25098 An explicit instantiation that names a class template
25099 specialization is also an explicit instantiation of the same
25100 kind (declaration or definition) of each of its members (not
25101 including members inherited from base classes and members
25102 that are templates) that has not been previously explicitly
25103 specialized in the translation unit containing the explicit
25104 instantiation, provided that the associated constraints, if
25105 any, of that member are satisfied by the template arguments
25106 of the explicit instantiation. */
25107 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25108 if ((VAR_P (fld)
25109 || (TREE_CODE (fld) == FUNCTION_DECL
25110 && !static_p
25111 && user_provided_p (fld)))
25112 && DECL_TEMPLATE_INSTANTIATION (fld)
25113 && constraints_satisfied_p (fld))
25114 {
25115 mark_decl_instantiated (fld, extern_p);
25116 if (! extern_p)
25117 instantiate_decl (fld, /*defer_ok=*/true,
25118 /*expl_inst_class_mem_p=*/true);
25119 }
25120 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
25121 {
25122 tree type = TREE_TYPE (fld);
25123
25124 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25125 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
25126 do_type_instantiation (type, storage, 0);
25127 }
25128 }
25129
25130 /* Given a function DECL, which is a specialization of TMPL, modify
25131 DECL to be a re-instantiation of TMPL with the same template
25132 arguments. TMPL should be the template into which tsubst'ing
25133 should occur for DECL, not the most general template.
25134
25135 One reason for doing this is a scenario like this:
25136
25137 template <class T>
25138 void f(const T&, int i);
25139
25140 void g() { f(3, 7); }
25141
25142 template <class T>
25143 void f(const T& t, const int i) { }
25144
25145 Note that when the template is first instantiated, with
25146 instantiate_template, the resulting DECL will have no name for the
25147 first parameter, and the wrong type for the second. So, when we go
25148 to instantiate the DECL, we regenerate it. */
25149
25150 static void
25151 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25152 {
25153 /* The arguments used to instantiate DECL, from the most general
25154 template. */
25155 tree code_pattern;
25156
25157 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25158
25159 /* Make sure that we can see identifiers, and compute access
25160 correctly. */
25161 push_access_scope (decl);
25162
25163 if (TREE_CODE (decl) == FUNCTION_DECL)
25164 {
25165 tree decl_parm;
25166 tree pattern_parm;
25167 tree specs;
25168 int args_depth;
25169 int parms_depth;
25170
25171 args_depth = TMPL_ARGS_DEPTH (args);
25172 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25173 if (args_depth > parms_depth)
25174 args = get_innermost_template_args (args, parms_depth);
25175
25176 /* Instantiate a dynamic exception-specification. noexcept will be
25177 handled below. */
25178 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25179 if (TREE_VALUE (raises))
25180 {
25181 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25182 args, tf_error, NULL_TREE,
25183 /*defer_ok*/false);
25184 if (specs && specs != error_mark_node)
25185 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25186 specs);
25187 }
25188
25189 /* Merge parameter declarations. */
25190 decl_parm = skip_artificial_parms_for (decl,
25191 DECL_ARGUMENTS (decl));
25192 pattern_parm
25193 = skip_artificial_parms_for (code_pattern,
25194 DECL_ARGUMENTS (code_pattern));
25195 while (decl_parm && !DECL_PACK_P (pattern_parm))
25196 {
25197 tree parm_type;
25198 tree attributes;
25199
25200 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25201 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25202 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25203 NULL_TREE);
25204 parm_type = type_decays_to (parm_type);
25205 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25206 TREE_TYPE (decl_parm) = parm_type;
25207 attributes = DECL_ATTRIBUTES (pattern_parm);
25208 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25209 {
25210 DECL_ATTRIBUTES (decl_parm) = attributes;
25211 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25212 }
25213 decl_parm = DECL_CHAIN (decl_parm);
25214 pattern_parm = DECL_CHAIN (pattern_parm);
25215 }
25216 /* Merge any parameters that match with the function parameter
25217 pack. */
25218 if (pattern_parm && DECL_PACK_P (pattern_parm))
25219 {
25220 int i, len;
25221 tree expanded_types;
25222 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25223 the parameters in this function parameter pack. */
25224 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25225 args, tf_error, NULL_TREE);
25226 len = TREE_VEC_LENGTH (expanded_types);
25227 for (i = 0; i < len; i++)
25228 {
25229 tree parm_type;
25230 tree attributes;
25231
25232 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25233 /* Rename the parameter to include the index. */
25234 DECL_NAME (decl_parm) =
25235 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25236 parm_type = TREE_VEC_ELT (expanded_types, i);
25237 parm_type = type_decays_to (parm_type);
25238 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25239 TREE_TYPE (decl_parm) = parm_type;
25240 attributes = DECL_ATTRIBUTES (pattern_parm);
25241 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25242 {
25243 DECL_ATTRIBUTES (decl_parm) = attributes;
25244 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25245 }
25246 decl_parm = DECL_CHAIN (decl_parm);
25247 }
25248 }
25249 /* Merge additional specifiers from the CODE_PATTERN. */
25250 if (DECL_DECLARED_INLINE_P (code_pattern)
25251 && !DECL_DECLARED_INLINE_P (decl))
25252 DECL_DECLARED_INLINE_P (decl) = 1;
25253
25254 maybe_instantiate_noexcept (decl, tf_error);
25255 }
25256 else if (VAR_P (decl))
25257 {
25258 start_lambda_scope (decl);
25259 DECL_INITIAL (decl) =
25260 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25261 tf_error, DECL_TI_TEMPLATE (decl));
25262 finish_lambda_scope ();
25263 if (VAR_HAD_UNKNOWN_BOUND (decl))
25264 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25265 tf_error, DECL_TI_TEMPLATE (decl));
25266 }
25267 else
25268 gcc_unreachable ();
25269
25270 pop_access_scope (decl);
25271 }
25272
25273 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25274 substituted to get DECL. */
25275
25276 tree
25277 template_for_substitution (tree decl)
25278 {
25279 tree tmpl = DECL_TI_TEMPLATE (decl);
25280
25281 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25282 for the instantiation. This is not always the most general
25283 template. Consider, for example:
25284
25285 template <class T>
25286 struct S { template <class U> void f();
25287 template <> void f<int>(); };
25288
25289 and an instantiation of S<double>::f<int>. We want TD to be the
25290 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25291 while (/* An instantiation cannot have a definition, so we need a
25292 more general template. */
25293 DECL_TEMPLATE_INSTANTIATION (tmpl)
25294 /* We must also deal with friend templates. Given:
25295
25296 template <class T> struct S {
25297 template <class U> friend void f() {};
25298 };
25299
25300 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25301 so far as the language is concerned, but that's still
25302 where we get the pattern for the instantiation from. On
25303 other hand, if the definition comes outside the class, say:
25304
25305 template <class T> struct S {
25306 template <class U> friend void f();
25307 };
25308 template <class U> friend void f() {}
25309
25310 we don't need to look any further. That's what the check for
25311 DECL_INITIAL is for. */
25312 || (TREE_CODE (decl) == FUNCTION_DECL
25313 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25314 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25315 {
25316 /* The present template, TD, should not be a definition. If it
25317 were a definition, we should be using it! Note that we
25318 cannot restructure the loop to just keep going until we find
25319 a template with a definition, since that might go too far if
25320 a specialization was declared, but not defined. */
25321
25322 /* Fetch the more general template. */
25323 tmpl = DECL_TI_TEMPLATE (tmpl);
25324 }
25325
25326 return tmpl;
25327 }
25328
25329 /* Returns true if we need to instantiate this template instance even if we
25330 know we aren't going to emit it. */
25331
25332 bool
25333 always_instantiate_p (tree decl)
25334 {
25335 /* We always instantiate inline functions so that we can inline them. An
25336 explicit instantiation declaration prohibits implicit instantiation of
25337 non-inline functions. With high levels of optimization, we would
25338 normally inline non-inline functions -- but we're not allowed to do
25339 that for "extern template" functions. Therefore, we check
25340 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25341 return ((TREE_CODE (decl) == FUNCTION_DECL
25342 && (DECL_DECLARED_INLINE_P (decl)
25343 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25344 /* And we need to instantiate static data members so that
25345 their initializers are available in integral constant
25346 expressions. */
25347 || (VAR_P (decl)
25348 && decl_maybe_constant_var_p (decl)));
25349 }
25350
25351 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25352 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25353 error, true otherwise. */
25354
25355 bool
25356 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25357 {
25358 tree fntype, spec, noex;
25359
25360 /* Don't instantiate a noexcept-specification from template context. */
25361 if (processing_template_decl
25362 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25363 return true;
25364
25365 if (DECL_MAYBE_DELETED (fn))
25366 {
25367 if (fn == current_function_decl)
25368 /* We're in start_preparsed_function, keep going. */
25369 return true;
25370
25371 ++function_depth;
25372 synthesize_method (fn);
25373 --function_depth;
25374 return !DECL_MAYBE_DELETED (fn);
25375 }
25376
25377 fntype = TREE_TYPE (fn);
25378 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25379
25380 if (!spec || !TREE_PURPOSE (spec))
25381 return true;
25382
25383 noex = TREE_PURPOSE (spec);
25384 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25385 && TREE_CODE (noex) != DEFERRED_PARSE)
25386 return true;
25387
25388 tree orig_fn = NULL_TREE;
25389 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25390 its FUNCTION_DECL for the rest of this function -- push_access_scope
25391 doesn't accept TEMPLATE_DECLs. */
25392 if (DECL_FUNCTION_TEMPLATE_P (fn))
25393 {
25394 orig_fn = fn;
25395 fn = DECL_TEMPLATE_RESULT (fn);
25396 }
25397
25398 if (DECL_CLONED_FUNCTION_P (fn))
25399 {
25400 tree prime = DECL_CLONED_FUNCTION (fn);
25401 if (!maybe_instantiate_noexcept (prime, complain))
25402 return false;
25403 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25404 }
25405 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25406 {
25407 static hash_set<tree>* fns = new hash_set<tree>;
25408 bool added = false;
25409 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25410 {
25411 spec = get_defaulted_eh_spec (fn, complain);
25412 if (spec == error_mark_node)
25413 /* This might have failed because of an unparsed DMI, so
25414 let's try again later. */
25415 return false;
25416 }
25417 else if (!(added = !fns->add (fn)))
25418 {
25419 /* If hash_set::add returns true, the element was already there. */
25420 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25421 DECL_SOURCE_LOCATION (fn));
25422 error_at (loc,
25423 "exception specification of %qD depends on itself",
25424 fn);
25425 spec = noexcept_false_spec;
25426 }
25427 else if (push_tinst_level (fn))
25428 {
25429 push_to_top_level ();
25430 push_access_scope (fn);
25431 push_deferring_access_checks (dk_no_deferred);
25432 input_location = DECL_SOURCE_LOCATION (fn);
25433
25434 if (!DECL_LOCAL_DECL_P (fn))
25435 {
25436 /* If needed, set current_class_ptr for the benefit of
25437 tsubst_copy/PARM_DECL. The exception pattern will
25438 refer to the parm of the template, not the
25439 instantiation. */
25440 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25441 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25442 {
25443 tree this_parm = DECL_ARGUMENTS (tdecl);
25444 current_class_ptr = NULL_TREE;
25445 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25446 current_class_ptr = this_parm;
25447 }
25448 }
25449
25450 /* If this function is represented by a TEMPLATE_DECL, then
25451 the deferred noexcept-specification might still contain
25452 dependent types, even after substitution. And we need the
25453 dependency check functions to work in build_noexcept_spec. */
25454 if (orig_fn)
25455 ++processing_template_decl;
25456
25457 /* Do deferred instantiation of the noexcept-specifier. */
25458 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25459 DEFERRED_NOEXCEPT_ARGS (noex),
25460 tf_warning_or_error, fn,
25461 /*function_p=*/false,
25462 /*i_c_e_p=*/true);
25463
25464 /* Build up the noexcept-specification. */
25465 spec = build_noexcept_spec (noex, tf_warning_or_error);
25466
25467 if (orig_fn)
25468 --processing_template_decl;
25469
25470 pop_deferring_access_checks ();
25471 pop_access_scope (fn);
25472 pop_tinst_level ();
25473 pop_from_top_level ();
25474 }
25475 else
25476 spec = noexcept_false_spec;
25477
25478 if (added)
25479 fns->remove (fn);
25480 }
25481
25482 if (spec == error_mark_node)
25483 {
25484 /* This failed with a hard error, so let's go with false. */
25485 gcc_assert (seen_error ());
25486 spec = noexcept_false_spec;
25487 }
25488
25489 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25490 if (orig_fn)
25491 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25492
25493 return true;
25494 }
25495
25496 /* We're starting to process the function INST, an instantiation of PATTERN;
25497 add their parameters to local_specializations. */
25498
25499 static void
25500 register_parameter_specializations (tree pattern, tree inst)
25501 {
25502 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25503 tree spec_parm = DECL_ARGUMENTS (inst);
25504 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25505 {
25506 register_local_specialization (spec_parm, tmpl_parm);
25507 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25508 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25509 }
25510 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25511 {
25512 if (!DECL_PACK_P (tmpl_parm)
25513 || (spec_parm && DECL_PACK_P (spec_parm)))
25514 {
25515 register_local_specialization (spec_parm, tmpl_parm);
25516 spec_parm = DECL_CHAIN (spec_parm);
25517 }
25518 else
25519 {
25520 /* Register the (value) argument pack as a specialization of
25521 TMPL_PARM, then move on. */
25522 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25523 register_local_specialization (argpack, tmpl_parm);
25524 }
25525 }
25526 gcc_assert (!spec_parm);
25527 }
25528
25529 /* Instantiate the body of D using PATTERN with ARGS. We have
25530 already determined PATTERN is the correct template to use.
25531 NESTED_P is true if this is a nested function, in which case
25532 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25533
25534 static void
25535 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
25536 {
25537 tree td = NULL_TREE;
25538 tree code_pattern = pattern;
25539
25540 if (!nested_p)
25541 {
25542 td = pattern;
25543 code_pattern = DECL_TEMPLATE_RESULT (td);
25544 }
25545 else
25546 /* Only OMP reductions are nested. */
25547 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
25548
25549 vec<tree> omp_privatization_save;
25550 if (current_function_decl)
25551 save_omp_privatization_clauses (omp_privatization_save);
25552
25553 bool push_to_top
25554 = !(current_function_decl
25555 && !LAMBDA_FUNCTION_P (d)
25556 && decl_function_context (d) == current_function_decl);
25557
25558 if (push_to_top)
25559 push_to_top_level ();
25560 else
25561 {
25562 gcc_assert (!processing_template_decl);
25563 push_function_context ();
25564 cp_unevaluated_operand = 0;
25565 c_inhibit_evaluation_warnings = 0;
25566 }
25567
25568 if (VAR_P (d))
25569 {
25570 /* The variable might be a lambda's extra scope, and that
25571 lambda's visibility depends on D's. */
25572 maybe_commonize_var (d);
25573 determine_visibility (d);
25574 }
25575
25576 /* Mark D as instantiated so that recursive calls to
25577 instantiate_decl do not try to instantiate it again. */
25578 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25579
25580 if (td)
25581 /* Regenerate the declaration in case the template has been modified
25582 by a subsequent redeclaration. */
25583 regenerate_decl_from_template (d, td, args);
25584
25585 /* We already set the file and line above. Reset them now in case
25586 they changed as a result of calling regenerate_decl_from_template. */
25587 input_location = DECL_SOURCE_LOCATION (d);
25588
25589 if (VAR_P (d))
25590 {
25591 /* Clear out DECL_RTL; whatever was there before may not be right
25592 since we've reset the type of the declaration. */
25593 SET_DECL_RTL (d, NULL);
25594 DECL_IN_AGGR_P (d) = 0;
25595
25596 /* The initializer is placed in DECL_INITIAL by
25597 regenerate_decl_from_template so we don't need to
25598 push/pop_access_scope again here. Pull it out so that
25599 cp_finish_decl can process it. */
25600 bool const_init = false;
25601 tree init = DECL_INITIAL (d);
25602 DECL_INITIAL (d) = NULL_TREE;
25603 DECL_INITIALIZED_P (d) = 0;
25604
25605 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25606 initializer. That function will defer actual emission until
25607 we have a chance to determine linkage. */
25608 DECL_EXTERNAL (d) = 0;
25609
25610 /* Enter the scope of D so that access-checking works correctly. */
25611 bool enter_context = DECL_CLASS_SCOPE_P (d);
25612 if (enter_context)
25613 push_nested_class (DECL_CONTEXT (d));
25614
25615 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25616 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25617
25618 if (enter_context)
25619 pop_nested_class ();
25620 }
25621 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25622 synthesize_method (d);
25623 else if (TREE_CODE (d) == FUNCTION_DECL)
25624 {
25625 /* Set up the list of local specializations. */
25626 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25627 tree block = NULL_TREE;
25628
25629 /* Set up context. */
25630 if (nested_p)
25631 block = push_stmt_list ();
25632 else
25633 {
25634 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25635
25636 perform_instantiation_time_access_checks (code_pattern, args);
25637 }
25638
25639 /* Create substitution entries for the parameters. */
25640 register_parameter_specializations (code_pattern, d);
25641
25642 /* Substitute into the body of the function. */
25643 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25644 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25645 tf_warning_or_error, d);
25646 else
25647 {
25648 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25649 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25650 /*integral_constant_expression_p=*/false);
25651
25652 /* Set the current input_location to the end of the function
25653 so that finish_function knows where we are. */
25654 input_location
25655 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25656
25657 /* Remember if we saw an infinite loop in the template. */
25658 current_function_infinite_loop
25659 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25660 }
25661
25662 /* Finish the function. */
25663 if (nested_p)
25664 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25665 else
25666 {
25667 d = finish_function (/*inline_p=*/false);
25668 expand_or_defer_fn (d);
25669 }
25670
25671 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25672 cp_check_omp_declare_reduction (d);
25673 }
25674
25675 /* We're not deferring instantiation any more. */
25676 if (!nested_p)
25677 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25678
25679 if (push_to_top)
25680 pop_from_top_level ();
25681 else
25682 pop_function_context ();
25683
25684 if (current_function_decl)
25685 restore_omp_privatization_clauses (omp_privatization_save);
25686 }
25687
25688 /* Produce the definition of D, a _DECL generated from a template. If
25689 DEFER_OK is true, then we don't have to actually do the
25690 instantiation now; we just have to do it sometime. Normally it is
25691 an error if this is an explicit instantiation but D is undefined.
25692 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25693 instantiated class template. */
25694
25695 tree
25696 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25697 {
25698 tree tmpl = DECL_TI_TEMPLATE (d);
25699 tree gen_args;
25700 tree args;
25701 tree td;
25702 tree code_pattern;
25703 tree spec;
25704 tree gen_tmpl;
25705 bool pattern_defined;
25706 location_t saved_loc = input_location;
25707 int saved_unevaluated_operand = cp_unevaluated_operand;
25708 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25709 bool external_p;
25710 bool deleted_p;
25711
25712 /* This function should only be used to instantiate templates for
25713 functions and static member variables. */
25714 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25715
25716 /* A concept is never instantiated. */
25717 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25718
25719 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
25720
25721 /* Variables are never deferred; if instantiation is required, they
25722 are instantiated right away. That allows for better code in the
25723 case that an expression refers to the value of the variable --
25724 if the variable has a constant value the referring expression can
25725 take advantage of that fact. */
25726 if (VAR_P (d))
25727 defer_ok = false;
25728
25729 /* Don't instantiate cloned functions. Instead, instantiate the
25730 functions they cloned. */
25731 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25732 d = DECL_CLONED_FUNCTION (d);
25733
25734 if (DECL_TEMPLATE_INSTANTIATED (d)
25735 || TREE_TYPE (d) == error_mark_node
25736 || (TREE_CODE (d) == FUNCTION_DECL
25737 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25738 || DECL_TEMPLATE_SPECIALIZATION (d))
25739 /* D has already been instantiated or explicitly specialized, so
25740 there's nothing for us to do here.
25741
25742 It might seem reasonable to check whether or not D is an explicit
25743 instantiation, and, if so, stop here. But when an explicit
25744 instantiation is deferred until the end of the compilation,
25745 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25746 the instantiation. */
25747 return d;
25748
25749 /* Check to see whether we know that this template will be
25750 instantiated in some other file, as with "extern template"
25751 extension. */
25752 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25753
25754 /* In general, we do not instantiate such templates. */
25755 if (external_p && !always_instantiate_p (d))
25756 return d;
25757
25758 gen_tmpl = most_general_template (tmpl);
25759 gen_args = DECL_TI_ARGS (d);
25760
25761 /* We should already have the extra args. */
25762 gcc_checking_assert (tmpl == gen_tmpl
25763 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25764 == TMPL_ARGS_DEPTH (gen_args)));
25765 /* And what's in the hash table should match D. */
25766 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25767 == d
25768 || spec == NULL_TREE);
25769
25770 /* This needs to happen before any tsubsting. */
25771 if (! push_tinst_level (d))
25772 return d;
25773
25774 timevar_push (TV_TEMPLATE_INST);
25775
25776 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25777 for the instantiation. */
25778 td = template_for_substitution (d);
25779 args = gen_args;
25780
25781 if (VAR_P (d))
25782 {
25783 /* Look up an explicit specialization, if any. */
25784 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25785 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25786 if (elt && elt != error_mark_node)
25787 {
25788 td = TREE_VALUE (elt);
25789 args = TREE_PURPOSE (elt);
25790 }
25791 }
25792
25793 code_pattern = DECL_TEMPLATE_RESULT (td);
25794
25795 /* We should never be trying to instantiate a member of a class
25796 template or partial specialization. */
25797 gcc_assert (d != code_pattern);
25798
25799 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25800 || DECL_TEMPLATE_SPECIALIZATION (td))
25801 /* In the case of a friend template whose definition is provided
25802 outside the class, we may have too many arguments. Drop the
25803 ones we don't need. The same is true for specializations. */
25804 args = get_innermost_template_args
25805 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25806
25807 if (TREE_CODE (d) == FUNCTION_DECL)
25808 {
25809 deleted_p = DECL_DELETED_FN (code_pattern);
25810 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25811 && DECL_INITIAL (code_pattern) != error_mark_node)
25812 || DECL_DEFAULTED_FN (code_pattern)
25813 || deleted_p);
25814 }
25815 else
25816 {
25817 deleted_p = false;
25818 if (DECL_CLASS_SCOPE_P (code_pattern))
25819 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25820 else
25821 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25822 }
25823
25824 /* We may be in the middle of deferred access check. Disable it now. */
25825 push_deferring_access_checks (dk_no_deferred);
25826
25827 /* Unless an explicit instantiation directive has already determined
25828 the linkage of D, remember that a definition is available for
25829 this entity. */
25830 if (pattern_defined
25831 && !DECL_INTERFACE_KNOWN (d)
25832 && !DECL_NOT_REALLY_EXTERN (d))
25833 mark_definable (d);
25834
25835 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25836 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25837 input_location = DECL_SOURCE_LOCATION (d);
25838
25839 /* If D is a member of an explicitly instantiated class template,
25840 and no definition is available, treat it like an implicit
25841 instantiation. */
25842 if (!pattern_defined && expl_inst_class_mem_p
25843 && DECL_EXPLICIT_INSTANTIATION (d))
25844 {
25845 /* Leave linkage flags alone on instantiations with anonymous
25846 visibility. */
25847 if (TREE_PUBLIC (d))
25848 {
25849 DECL_NOT_REALLY_EXTERN (d) = 0;
25850 DECL_INTERFACE_KNOWN (d) = 0;
25851 }
25852 SET_DECL_IMPLICIT_INSTANTIATION (d);
25853 }
25854
25855 /* Defer all other templates, unless we have been explicitly
25856 forbidden from doing so. */
25857 if (/* If there is no definition, we cannot instantiate the
25858 template. */
25859 ! pattern_defined
25860 /* If it's OK to postpone instantiation, do so. */
25861 || defer_ok
25862 /* If this is a static data member that will be defined
25863 elsewhere, we don't want to instantiate the entire data
25864 member, but we do want to instantiate the initializer so that
25865 we can substitute that elsewhere. */
25866 || (external_p && VAR_P (d))
25867 /* Handle here a deleted function too, avoid generating
25868 its body (c++/61080). */
25869 || deleted_p)
25870 {
25871 /* The definition of the static data member is now required so
25872 we must substitute the initializer. */
25873 if (VAR_P (d)
25874 && !DECL_INITIAL (d)
25875 && DECL_INITIAL (code_pattern))
25876 {
25877 tree ns;
25878 tree init;
25879 bool const_init = false;
25880 bool enter_context = DECL_CLASS_SCOPE_P (d);
25881
25882 ns = decl_namespace_context (d);
25883 push_nested_namespace (ns);
25884 if (enter_context)
25885 push_nested_class (DECL_CONTEXT (d));
25886 init = tsubst_expr (DECL_INITIAL (code_pattern),
25887 args,
25888 tf_warning_or_error, NULL_TREE,
25889 /*integral_constant_expression_p=*/false);
25890 /* If instantiating the initializer involved instantiating this
25891 again, don't call cp_finish_decl twice. */
25892 if (!DECL_INITIAL (d))
25893 {
25894 /* Make sure the initializer is still constant, in case of
25895 circular dependency (template/instantiate6.C). */
25896 const_init
25897 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25898 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25899 /*asmspec_tree=*/NULL_TREE,
25900 LOOKUP_ONLYCONVERTING);
25901 }
25902 if (enter_context)
25903 pop_nested_class ();
25904 pop_nested_namespace (ns);
25905 }
25906
25907 /* We restore the source position here because it's used by
25908 add_pending_template. */
25909 input_location = saved_loc;
25910
25911 if (at_eof && !pattern_defined
25912 && DECL_EXPLICIT_INSTANTIATION (d)
25913 && DECL_NOT_REALLY_EXTERN (d))
25914 /* [temp.explicit]
25915
25916 The definition of a non-exported function template, a
25917 non-exported member function template, or a non-exported
25918 member function or static data member of a class template
25919 shall be present in every translation unit in which it is
25920 explicitly instantiated. */
25921 permerror (input_location, "explicit instantiation of %qD "
25922 "but no definition available", d);
25923
25924 /* If we're in unevaluated context, we just wanted to get the
25925 constant value; this isn't an odr use, so don't queue
25926 a full instantiation. */
25927 if (!cp_unevaluated_operand
25928 /* ??? Historically, we have instantiated inline functions, even
25929 when marked as "extern template". */
25930 && !(external_p && VAR_P (d)))
25931 add_pending_template (d);
25932 }
25933 else
25934 {
25935 if (variable_template_p (gen_tmpl))
25936 note_variable_template_instantiation (d);
25937 instantiate_body (td, args, d, false);
25938 }
25939
25940 pop_deferring_access_checks ();
25941 timevar_pop (TV_TEMPLATE_INST);
25942 pop_tinst_level ();
25943 input_location = saved_loc;
25944 cp_unevaluated_operand = saved_unevaluated_operand;
25945 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25946
25947 return d;
25948 }
25949
25950 /* Run through the list of templates that we wish we could
25951 instantiate, and instantiate any we can. RETRIES is the
25952 number of times we retry pending template instantiation. */
25953
25954 void
25955 instantiate_pending_templates (int retries)
25956 {
25957 int reconsider;
25958 location_t saved_loc = input_location;
25959
25960 /* Instantiating templates may trigger vtable generation. This in turn
25961 may require further template instantiations. We place a limit here
25962 to avoid infinite loop. */
25963 if (pending_templates && retries >= max_tinst_depth)
25964 {
25965 tree decl = pending_templates->tinst->maybe_get_node ();
25966
25967 fatal_error (input_location,
25968 "template instantiation depth exceeds maximum of %d"
25969 " instantiating %q+D, possibly from virtual table generation"
25970 " (use %<-ftemplate-depth=%> to increase the maximum)",
25971 max_tinst_depth, decl);
25972 if (TREE_CODE (decl) == FUNCTION_DECL)
25973 /* Pretend that we defined it. */
25974 DECL_INITIAL (decl) = error_mark_node;
25975 return;
25976 }
25977
25978 do
25979 {
25980 struct pending_template **t = &pending_templates;
25981 struct pending_template *last = NULL;
25982 reconsider = 0;
25983 while (*t)
25984 {
25985 tree instantiation = reopen_tinst_level ((*t)->tinst);
25986 bool complete = false;
25987
25988 if (TYPE_P (instantiation))
25989 {
25990 if (!COMPLETE_TYPE_P (instantiation))
25991 {
25992 instantiate_class_template (instantiation);
25993 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25994 for (tree fld = TYPE_FIELDS (instantiation);
25995 fld; fld = TREE_CHAIN (fld))
25996 if ((VAR_P (fld)
25997 || (TREE_CODE (fld) == FUNCTION_DECL
25998 && !DECL_ARTIFICIAL (fld)))
25999 && DECL_TEMPLATE_INSTANTIATION (fld))
26000 instantiate_decl (fld,
26001 /*defer_ok=*/false,
26002 /*expl_inst_class_mem_p=*/false);
26003
26004 if (COMPLETE_TYPE_P (instantiation))
26005 reconsider = 1;
26006 }
26007
26008 complete = COMPLETE_TYPE_P (instantiation);
26009 }
26010 else
26011 {
26012 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
26013 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
26014 {
26015 instantiation
26016 = instantiate_decl (instantiation,
26017 /*defer_ok=*/false,
26018 /*expl_inst_class_mem_p=*/false);
26019 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
26020 reconsider = 1;
26021 }
26022
26023 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
26024 || DECL_TEMPLATE_INSTANTIATED (instantiation));
26025 }
26026
26027 if (complete)
26028 {
26029 /* If INSTANTIATION has been instantiated, then we don't
26030 need to consider it again in the future. */
26031 struct pending_template *drop = *t;
26032 *t = (*t)->next;
26033 set_refcount_ptr (drop->tinst);
26034 pending_template_freelist ().free (drop);
26035 }
26036 else
26037 {
26038 last = *t;
26039 t = &(*t)->next;
26040 }
26041 tinst_depth = 0;
26042 set_refcount_ptr (current_tinst_level);
26043 }
26044 last_pending_template = last;
26045 }
26046 while (reconsider);
26047
26048 input_location = saved_loc;
26049 }
26050
26051 /* Substitute ARGVEC into T, which is a list of initializers for
26052 either base class or a non-static data member. The TREE_PURPOSEs
26053 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26054 instantiate_decl. */
26055
26056 static tree
26057 tsubst_initializer_list (tree t, tree argvec)
26058 {
26059 tree inits = NULL_TREE;
26060 tree target_ctor = error_mark_node;
26061
26062 for (; t; t = TREE_CHAIN (t))
26063 {
26064 tree decl;
26065 tree init;
26066 tree expanded_bases = NULL_TREE;
26067 tree expanded_arguments = NULL_TREE;
26068 int i, len = 1;
26069
26070 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
26071 {
26072 tree expr;
26073 tree arg;
26074
26075 /* Expand the base class expansion type into separate base
26076 classes. */
26077 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26078 tf_warning_or_error,
26079 NULL_TREE);
26080 if (expanded_bases == error_mark_node)
26081 continue;
26082
26083 /* We'll be building separate TREE_LISTs of arguments for
26084 each base. */
26085 len = TREE_VEC_LENGTH (expanded_bases);
26086 expanded_arguments = make_tree_vec (len);
26087 for (i = 0; i < len; i++)
26088 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26089
26090 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26091 expand each argument in the TREE_VALUE of t. */
26092 expr = make_node (EXPR_PACK_EXPANSION);
26093 PACK_EXPANSION_LOCAL_P (expr) = true;
26094 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26095 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26096
26097 if (TREE_VALUE (t) == void_type_node)
26098 /* VOID_TYPE_NODE is used to indicate
26099 value-initialization. */
26100 {
26101 for (i = 0; i < len; i++)
26102 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26103 }
26104 else
26105 {
26106 /* Substitute parameter packs into each argument in the
26107 TREE_LIST. */
26108 in_base_initializer = 1;
26109 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26110 {
26111 tree expanded_exprs;
26112
26113 /* Expand the argument. */
26114 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
26115 expanded_exprs
26116 = tsubst_pack_expansion (expr, argvec,
26117 tf_warning_or_error,
26118 NULL_TREE);
26119 if (expanded_exprs == error_mark_node)
26120 continue;
26121
26122 /* Prepend each of the expanded expressions to the
26123 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26124 for (i = 0; i < len; i++)
26125 {
26126 TREE_VEC_ELT (expanded_arguments, i) =
26127 tree_cons (NULL_TREE,
26128 TREE_VEC_ELT (expanded_exprs, i),
26129 TREE_VEC_ELT (expanded_arguments, i));
26130 }
26131 }
26132 in_base_initializer = 0;
26133
26134 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26135 since we built them backwards. */
26136 for (i = 0; i < len; i++)
26137 {
26138 TREE_VEC_ELT (expanded_arguments, i) =
26139 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26140 }
26141 }
26142 }
26143
26144 for (i = 0; i < len; ++i)
26145 {
26146 if (expanded_bases)
26147 {
26148 decl = TREE_VEC_ELT (expanded_bases, i);
26149 decl = expand_member_init (decl);
26150 init = TREE_VEC_ELT (expanded_arguments, i);
26151 }
26152 else
26153 {
26154 tree tmp;
26155 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26156 tf_warning_or_error, NULL_TREE);
26157
26158 decl = expand_member_init (decl);
26159 if (decl && !DECL_P (decl))
26160 in_base_initializer = 1;
26161
26162 init = TREE_VALUE (t);
26163 tmp = init;
26164 if (init != void_type_node)
26165 init = tsubst_expr (init, argvec,
26166 tf_warning_or_error, NULL_TREE,
26167 /*integral_constant_expression_p=*/false);
26168 if (init == NULL_TREE && tmp != NULL_TREE)
26169 /* If we had an initializer but it instantiated to nothing,
26170 value-initialize the object. This will only occur when
26171 the initializer was a pack expansion where the parameter
26172 packs used in that expansion were of length zero. */
26173 init = void_type_node;
26174 in_base_initializer = 0;
26175 }
26176
26177 if (target_ctor != error_mark_node
26178 && init != error_mark_node)
26179 {
26180 error ("mem-initializer for %qD follows constructor delegation",
26181 decl);
26182 return inits;
26183 }
26184 /* Look for a target constructor. */
26185 if (init != error_mark_node
26186 && decl && CLASS_TYPE_P (decl)
26187 && same_type_p (decl, current_class_type))
26188 {
26189 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26190 if (inits)
26191 {
26192 error ("constructor delegation follows mem-initializer for %qD",
26193 TREE_PURPOSE (inits));
26194 continue;
26195 }
26196 target_ctor = init;
26197 }
26198
26199 if (decl)
26200 {
26201 init = build_tree_list (decl, init);
26202 /* Carry over the dummy TREE_TYPE node containing the source
26203 location. */
26204 TREE_TYPE (init) = TREE_TYPE (t);
26205 TREE_CHAIN (init) = inits;
26206 inits = init;
26207 }
26208 }
26209 }
26210 return inits;
26211 }
26212
26213 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26214
26215 static void
26216 set_current_access_from_decl (tree decl)
26217 {
26218 if (TREE_PRIVATE (decl))
26219 current_access_specifier = access_private_node;
26220 else if (TREE_PROTECTED (decl))
26221 current_access_specifier = access_protected_node;
26222 else
26223 current_access_specifier = access_public_node;
26224 }
26225
26226 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26227 is the instantiation (which should have been created with
26228 start_enum) and ARGS are the template arguments to use. */
26229
26230 static void
26231 tsubst_enum (tree tag, tree newtag, tree args)
26232 {
26233 tree e;
26234
26235 if (SCOPED_ENUM_P (newtag))
26236 begin_scope (sk_scoped_enum, newtag);
26237
26238 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26239 {
26240 tree value;
26241 tree decl;
26242
26243 decl = TREE_VALUE (e);
26244 /* Note that in a template enum, the TREE_VALUE is the
26245 CONST_DECL, not the corresponding INTEGER_CST. */
26246 value = tsubst_expr (DECL_INITIAL (decl),
26247 args, tf_warning_or_error, NULL_TREE,
26248 /*integral_constant_expression_p=*/true);
26249
26250 /* Give this enumeration constant the correct access. */
26251 set_current_access_from_decl (decl);
26252
26253 /* Actually build the enumerator itself. Here we're assuming that
26254 enumerators can't have dependent attributes. */
26255 build_enumerator (DECL_NAME (decl), value, newtag,
26256 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26257 }
26258
26259 if (SCOPED_ENUM_P (newtag))
26260 finish_scope ();
26261
26262 finish_enum_value_list (newtag);
26263 finish_enum (newtag);
26264
26265 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26266 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26267 }
26268
26269 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26270 its type -- but without substituting the innermost set of template
26271 arguments. So, innermost set of template parameters will appear in
26272 the type. */
26273
26274 tree
26275 get_mostly_instantiated_function_type (tree decl)
26276 {
26277 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26278 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26279 }
26280
26281 /* Return truthvalue if we're processing a template different from
26282 the last one involved in diagnostics. */
26283 bool
26284 problematic_instantiation_changed (void)
26285 {
26286 return current_tinst_level != last_error_tinst_level;
26287 }
26288
26289 /* Remember current template involved in diagnostics. */
26290 void
26291 record_last_problematic_instantiation (void)
26292 {
26293 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26294 }
26295
26296 struct tinst_level *
26297 current_instantiation (void)
26298 {
26299 return current_tinst_level;
26300 }
26301
26302 /* Return TRUE if current_function_decl is being instantiated, false
26303 otherwise. */
26304
26305 bool
26306 instantiating_current_function_p (void)
26307 {
26308 return (current_instantiation ()
26309 && (current_instantiation ()->maybe_get_node ()
26310 == current_function_decl));
26311 }
26312
26313 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26314 type. Return false for ok, true for disallowed. Issue error and
26315 inform messages under control of COMPLAIN. */
26316
26317 static bool
26318 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26319 {
26320 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26321 return false;
26322 else if (TYPE_PTR_P (type))
26323 return false;
26324 else if (TYPE_REF_P (type)
26325 && !TYPE_REF_IS_RVALUE (type))
26326 return false;
26327 else if (TYPE_PTRMEM_P (type))
26328 return false;
26329 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26330 {
26331 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26332 {
26333 if (complain & tf_error)
26334 error ("non-type template parameters of deduced class type only "
26335 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26336 return true;
26337 }
26338 return false;
26339 }
26340 else if (TREE_CODE (type) == TYPENAME_TYPE)
26341 return false;
26342 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26343 return false;
26344 else if (TREE_CODE (type) == NULLPTR_TYPE)
26345 return false;
26346 /* A bound template template parm could later be instantiated to have a valid
26347 nontype parm type via an alias template. */
26348 else if (cxx_dialect >= cxx11
26349 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26350 return false;
26351 else if (VOID_TYPE_P (type))
26352 /* Fall through. */;
26353 else if (cxx_dialect >= cxx20)
26354 {
26355 if (dependent_type_p (type))
26356 return false;
26357 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26358 return true;
26359 if (structural_type_p (type))
26360 return false;
26361 if (complain & tf_error)
26362 {
26363 auto_diagnostic_group d;
26364 error ("%qT is not a valid type for a template non-type "
26365 "parameter because it is not structural", type);
26366 structural_type_p (type, true);
26367 }
26368 return true;
26369 }
26370 else if (CLASS_TYPE_P (type))
26371 {
26372 if (complain & tf_error)
26373 error ("non-type template parameters of class type only available "
26374 "with %<-std=c++20%> or %<-std=gnu++20%>");
26375 return true;
26376 }
26377
26378 if (complain & tf_error)
26379 {
26380 if (type == error_mark_node)
26381 inform (input_location, "invalid template non-type parameter");
26382 else
26383 error ("%q#T is not a valid type for a template non-type parameter",
26384 type);
26385 }
26386 return true;
26387 }
26388
26389 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26390 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26391
26392 static bool
26393 dependent_type_p_r (tree type)
26394 {
26395 tree scope;
26396
26397 /* [temp.dep.type]
26398
26399 A type is dependent if it is:
26400
26401 -- a template parameter. Template template parameters are types
26402 for us (since TYPE_P holds true for them) so we handle
26403 them here. */
26404 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26405 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26406 return true;
26407 /* -- a qualified-id with a nested-name-specifier which contains a
26408 class-name that names a dependent type or whose unqualified-id
26409 names a dependent type. */
26410 if (TREE_CODE (type) == TYPENAME_TYPE)
26411 return true;
26412
26413 /* An alias template specialization can be dependent even if the
26414 resulting type is not. */
26415 if (dependent_alias_template_spec_p (type, nt_transparent))
26416 return true;
26417
26418 /* -- a cv-qualified type where the cv-unqualified type is
26419 dependent.
26420 No code is necessary for this bullet; the code below handles
26421 cv-qualified types, and we don't want to strip aliases with
26422 TYPE_MAIN_VARIANT because of DR 1558. */
26423 /* -- a compound type constructed from any dependent type. */
26424 if (TYPE_PTRMEM_P (type))
26425 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26426 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26427 (type)));
26428 else if (INDIRECT_TYPE_P (type))
26429 return dependent_type_p (TREE_TYPE (type));
26430 else if (FUNC_OR_METHOD_TYPE_P (type))
26431 {
26432 tree arg_type;
26433
26434 if (dependent_type_p (TREE_TYPE (type)))
26435 return true;
26436 for (arg_type = TYPE_ARG_TYPES (type);
26437 arg_type;
26438 arg_type = TREE_CHAIN (arg_type))
26439 if (dependent_type_p (TREE_VALUE (arg_type)))
26440 return true;
26441 if (cxx_dialect >= cxx17)
26442 /* A value-dependent noexcept-specifier makes the type dependent. */
26443 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26444 if (tree noex = TREE_PURPOSE (spec))
26445 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26446 affect overload resolution and treating it as dependent breaks
26447 things. Same for an unparsed noexcept expression. */
26448 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26449 && TREE_CODE (noex) != DEFERRED_PARSE
26450 && value_dependent_expression_p (noex))
26451 return true;
26452 return false;
26453 }
26454 /* -- an array type constructed from any dependent type or whose
26455 size is specified by a constant expression that is
26456 value-dependent.
26457
26458 We checked for type- and value-dependence of the bounds in
26459 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26460 if (TREE_CODE (type) == ARRAY_TYPE)
26461 {
26462 if (TYPE_DOMAIN (type)
26463 && dependent_type_p (TYPE_DOMAIN (type)))
26464 return true;
26465 return dependent_type_p (TREE_TYPE (type));
26466 }
26467
26468 /* -- a template-id in which either the template name is a template
26469 parameter ... */
26470 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26471 return true;
26472 /* ... or any of the template arguments is a dependent type or
26473 an expression that is type-dependent or value-dependent. */
26474 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26475 && (any_dependent_template_arguments_p
26476 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26477 return true;
26478
26479 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26480 dependent; if the argument of the `typeof' expression is not
26481 type-dependent, then it should already been have resolved. */
26482 if (TREE_CODE (type) == TYPEOF_TYPE
26483 || TREE_CODE (type) == DECLTYPE_TYPE
26484 || TREE_CODE (type) == UNDERLYING_TYPE)
26485 return true;
26486
26487 /* A template argument pack is dependent if any of its packed
26488 arguments are. */
26489 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26490 {
26491 tree args = ARGUMENT_PACK_ARGS (type);
26492 int i, len = TREE_VEC_LENGTH (args);
26493 for (i = 0; i < len; ++i)
26494 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26495 return true;
26496 }
26497
26498 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26499 be template parameters. */
26500 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26501 return true;
26502
26503 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26504 return true;
26505
26506 /* The standard does not specifically mention types that are local
26507 to template functions or local classes, but they should be
26508 considered dependent too. For example:
26509
26510 template <int I> void f() {
26511 enum E { a = I };
26512 S<sizeof (E)> s;
26513 }
26514
26515 The size of `E' cannot be known until the value of `I' has been
26516 determined. Therefore, `E' must be considered dependent. */
26517 scope = TYPE_CONTEXT (type);
26518 if (scope && TYPE_P (scope))
26519 return dependent_type_p (scope);
26520 /* Don't use type_dependent_expression_p here, as it can lead
26521 to infinite recursion trying to determine whether a lambda
26522 nested in a lambda is dependent (c++/47687). */
26523 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26524 && DECL_LANG_SPECIFIC (scope)
26525 && DECL_TEMPLATE_INFO (scope)
26526 && (any_dependent_template_arguments_p
26527 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26528 return true;
26529
26530 /* Other types are non-dependent. */
26531 return false;
26532 }
26533
26534 /* Returns TRUE if TYPE is dependent, in the sense of
26535 [temp.dep.type]. Note that a NULL type is considered dependent. */
26536
26537 bool
26538 dependent_type_p (tree type)
26539 {
26540 /* If there are no template parameters in scope, then there can't be
26541 any dependent types. */
26542 if (!processing_template_decl)
26543 {
26544 /* If we are not processing a template, then nobody should be
26545 providing us with a dependent type. */
26546 gcc_assert (type);
26547 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26548 return false;
26549 }
26550
26551 /* If the type is NULL, we have not computed a type for the entity
26552 in question; in that case, the type is dependent. */
26553 if (!type)
26554 return true;
26555
26556 /* Erroneous types can be considered non-dependent. */
26557 if (type == error_mark_node)
26558 return false;
26559
26560 /* Getting here with global_type_node means we improperly called this
26561 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26562 gcc_checking_assert (type != global_type_node);
26563
26564 /* If we have not already computed the appropriate value for TYPE,
26565 do so now. */
26566 if (!TYPE_DEPENDENT_P_VALID (type))
26567 {
26568 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26569 TYPE_DEPENDENT_P_VALID (type) = 1;
26570 }
26571
26572 return TYPE_DEPENDENT_P (type);
26573 }
26574
26575 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26576 lookup. In other words, a dependent type that is not the current
26577 instantiation. */
26578
26579 bool
26580 dependent_scope_p (tree scope)
26581 {
26582 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26583 && !currently_open_class (scope));
26584 }
26585
26586 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26587 an unknown base of 'this' (and is therefore instantiation-dependent). */
26588
26589 static bool
26590 unknown_base_ref_p (tree t)
26591 {
26592 if (!current_class_ptr)
26593 return false;
26594
26595 tree mem = TREE_OPERAND (t, 1);
26596 if (shared_member_p (mem))
26597 return false;
26598
26599 tree cur = current_nonlambda_class_type ();
26600 if (!any_dependent_bases_p (cur))
26601 return false;
26602
26603 tree ctx = TREE_OPERAND (t, 0);
26604 if (DERIVED_FROM_P (ctx, cur))
26605 return false;
26606
26607 return true;
26608 }
26609
26610 /* T is a SCOPE_REF; return whether we need to consider it
26611 instantiation-dependent so that we can check access at instantiation
26612 time even though we know which member it resolves to. */
26613
26614 static bool
26615 instantiation_dependent_scope_ref_p (tree t)
26616 {
26617 if (DECL_P (TREE_OPERAND (t, 1))
26618 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26619 && !unknown_base_ref_p (t)
26620 && accessible_in_template_p (TREE_OPERAND (t, 0),
26621 TREE_OPERAND (t, 1)))
26622 return false;
26623 else
26624 return true;
26625 }
26626
26627 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26628 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26629 expression. */
26630
26631 /* Note that this predicate is not appropriate for general expressions;
26632 only constant expressions (that satisfy potential_constant_expression)
26633 can be tested for value dependence. */
26634
26635 bool
26636 value_dependent_expression_p (tree expression)
26637 {
26638 if (!processing_template_decl || expression == NULL_TREE)
26639 return false;
26640
26641 /* A type-dependent expression is also value-dependent. */
26642 if (type_dependent_expression_p (expression))
26643 return true;
26644
26645 switch (TREE_CODE (expression))
26646 {
26647 case BASELINK:
26648 /* A dependent member function of the current instantiation. */
26649 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26650
26651 case FUNCTION_DECL:
26652 /* A dependent member function of the current instantiation. */
26653 if (DECL_CLASS_SCOPE_P (expression)
26654 && dependent_type_p (DECL_CONTEXT (expression)))
26655 return true;
26656 break;
26657
26658 case IDENTIFIER_NODE:
26659 /* A name that has not been looked up -- must be dependent. */
26660 return true;
26661
26662 case TEMPLATE_PARM_INDEX:
26663 /* A non-type template parm. */
26664 return true;
26665
26666 case CONST_DECL:
26667 /* A non-type template parm. */
26668 if (DECL_TEMPLATE_PARM_P (expression))
26669 return true;
26670 return value_dependent_expression_p (DECL_INITIAL (expression));
26671
26672 case VAR_DECL:
26673 /* A constant with literal type and is initialized
26674 with an expression that is value-dependent. */
26675 if (DECL_DEPENDENT_INIT_P (expression)
26676 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26677 || TYPE_REF_P (TREE_TYPE (expression)))
26678 return true;
26679 if (DECL_HAS_VALUE_EXPR_P (expression))
26680 {
26681 tree value_expr = DECL_VALUE_EXPR (expression);
26682 if (value_dependent_expression_p (value_expr)
26683 /* __PRETTY_FUNCTION__ inside a template function is dependent
26684 on the name of the function. */
26685 || (DECL_PRETTY_FUNCTION_P (expression)
26686 /* It might be used in a template, but not a template
26687 function, in which case its DECL_VALUE_EXPR will be
26688 "top level". */
26689 && value_expr == error_mark_node))
26690 return true;
26691 }
26692 return false;
26693
26694 case DYNAMIC_CAST_EXPR:
26695 case STATIC_CAST_EXPR:
26696 case CONST_CAST_EXPR:
26697 case REINTERPRET_CAST_EXPR:
26698 case CAST_EXPR:
26699 case IMPLICIT_CONV_EXPR:
26700 /* These expressions are value-dependent if the type to which
26701 the cast occurs is dependent or the expression being casted
26702 is value-dependent. */
26703 {
26704 tree type = TREE_TYPE (expression);
26705
26706 if (dependent_type_p (type))
26707 return true;
26708
26709 /* A functional cast has a list of operands. */
26710 expression = TREE_OPERAND (expression, 0);
26711 if (!expression)
26712 {
26713 /* If there are no operands, it must be an expression such
26714 as "int()". This should not happen for aggregate types
26715 because it would form non-constant expressions. */
26716 gcc_assert (cxx_dialect >= cxx11
26717 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26718
26719 return false;
26720 }
26721
26722 if (TREE_CODE (expression) == TREE_LIST)
26723 return any_value_dependent_elements_p (expression);
26724
26725 return value_dependent_expression_p (expression);
26726 }
26727
26728 case SIZEOF_EXPR:
26729 if (SIZEOF_EXPR_TYPE_P (expression))
26730 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26731 /* FALLTHRU */
26732 case ALIGNOF_EXPR:
26733 case TYPEID_EXPR:
26734 /* A `sizeof' expression is value-dependent if the operand is
26735 type-dependent or is a pack expansion. */
26736 expression = TREE_OPERAND (expression, 0);
26737 if (PACK_EXPANSION_P (expression))
26738 return true;
26739 else if (TYPE_P (expression))
26740 return dependent_type_p (expression);
26741 return instantiation_dependent_uneval_expression_p (expression);
26742
26743 case AT_ENCODE_EXPR:
26744 /* An 'encode' expression is value-dependent if the operand is
26745 type-dependent. */
26746 expression = TREE_OPERAND (expression, 0);
26747 return dependent_type_p (expression);
26748
26749 case NOEXCEPT_EXPR:
26750 expression = TREE_OPERAND (expression, 0);
26751 return instantiation_dependent_uneval_expression_p (expression);
26752
26753 case SCOPE_REF:
26754 /* All instantiation-dependent expressions should also be considered
26755 value-dependent. */
26756 return instantiation_dependent_scope_ref_p (expression);
26757
26758 case COMPONENT_REF:
26759 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26760 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26761
26762 case NONTYPE_ARGUMENT_PACK:
26763 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26764 is value-dependent. */
26765 {
26766 tree values = ARGUMENT_PACK_ARGS (expression);
26767 int i, len = TREE_VEC_LENGTH (values);
26768
26769 for (i = 0; i < len; ++i)
26770 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26771 return true;
26772
26773 return false;
26774 }
26775
26776 case TRAIT_EXPR:
26777 {
26778 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26779
26780 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26781 return true;
26782
26783 if (!type2)
26784 return false;
26785
26786 if (TREE_CODE (type2) != TREE_LIST)
26787 return dependent_type_p (type2);
26788
26789 for (; type2; type2 = TREE_CHAIN (type2))
26790 if (dependent_type_p (TREE_VALUE (type2)))
26791 return true;
26792
26793 return false;
26794 }
26795
26796 case MODOP_EXPR:
26797 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26798 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26799
26800 case ARRAY_REF:
26801 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26802 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26803
26804 case ADDR_EXPR:
26805 {
26806 tree op = TREE_OPERAND (expression, 0);
26807 return (value_dependent_expression_p (op)
26808 || has_value_dependent_address (op));
26809 }
26810
26811 case REQUIRES_EXPR:
26812 /* Treat all requires-expressions as value-dependent so
26813 we don't try to fold them. */
26814 return true;
26815
26816 case TYPE_REQ:
26817 return dependent_type_p (TREE_OPERAND (expression, 0));
26818
26819 case CALL_EXPR:
26820 {
26821 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26822 return true;
26823 tree fn = get_callee_fndecl (expression);
26824 int i, nargs;
26825 nargs = call_expr_nargs (expression);
26826 for (i = 0; i < nargs; ++i)
26827 {
26828 tree op = CALL_EXPR_ARG (expression, i);
26829 /* In a call to a constexpr member function, look through the
26830 implicit ADDR_EXPR on the object argument so that it doesn't
26831 cause the call to be considered value-dependent. We also
26832 look through it in potential_constant_expression. */
26833 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26834 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26835 && TREE_CODE (op) == ADDR_EXPR)
26836 op = TREE_OPERAND (op, 0);
26837 if (value_dependent_expression_p (op))
26838 return true;
26839 }
26840 return false;
26841 }
26842
26843 case TEMPLATE_ID_EXPR:
26844 return concept_definition_p (TREE_OPERAND (expression, 0));
26845
26846 case CONSTRUCTOR:
26847 {
26848 unsigned ix;
26849 tree val;
26850 if (dependent_type_p (TREE_TYPE (expression)))
26851 return true;
26852 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26853 if (value_dependent_expression_p (val))
26854 return true;
26855 return false;
26856 }
26857
26858 case STMT_EXPR:
26859 /* Treat a GNU statement expression as dependent to avoid crashing
26860 under instantiate_non_dependent_expr; it can't be constant. */
26861 return true;
26862
26863 default:
26864 /* A constant expression is value-dependent if any subexpression is
26865 value-dependent. */
26866 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26867 {
26868 case tcc_reference:
26869 case tcc_unary:
26870 case tcc_comparison:
26871 case tcc_binary:
26872 case tcc_expression:
26873 case tcc_vl_exp:
26874 {
26875 int i, len = cp_tree_operand_length (expression);
26876
26877 for (i = 0; i < len; i++)
26878 {
26879 tree t = TREE_OPERAND (expression, i);
26880
26881 /* In some cases, some of the operands may be missing.
26882 (For example, in the case of PREDECREMENT_EXPR, the
26883 amount to increment by may be missing.) That doesn't
26884 make the expression dependent. */
26885 if (t && value_dependent_expression_p (t))
26886 return true;
26887 }
26888 }
26889 break;
26890 default:
26891 break;
26892 }
26893 break;
26894 }
26895
26896 /* The expression is not value-dependent. */
26897 return false;
26898 }
26899
26900 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26901 [temp.dep.expr]. Note that an expression with no type is
26902 considered dependent. Other parts of the compiler arrange for an
26903 expression with type-dependent subexpressions to have no type, so
26904 this function doesn't have to be fully recursive. */
26905
26906 bool
26907 type_dependent_expression_p (tree expression)
26908 {
26909 if (!processing_template_decl)
26910 return false;
26911
26912 if (expression == NULL_TREE || expression == error_mark_node)
26913 return false;
26914
26915 STRIP_ANY_LOCATION_WRAPPER (expression);
26916
26917 /* An unresolved name is always dependent. */
26918 if (identifier_p (expression)
26919 || TREE_CODE (expression) == USING_DECL
26920 || TREE_CODE (expression) == WILDCARD_DECL)
26921 return true;
26922
26923 /* A lambda-expression in template context is dependent. dependent_type_p is
26924 true for a lambda in the scope of a class or function template, but that
26925 doesn't cover all template contexts, like a default template argument. */
26926 if (TREE_CODE (expression) == LAMBDA_EXPR)
26927 return true;
26928
26929 /* A fold expression is type-dependent. */
26930 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26931 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26932 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26933 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26934 return true;
26935
26936 /* Some expression forms are never type-dependent. */
26937 if (TREE_CODE (expression) == SIZEOF_EXPR
26938 || TREE_CODE (expression) == ALIGNOF_EXPR
26939 || TREE_CODE (expression) == AT_ENCODE_EXPR
26940 || TREE_CODE (expression) == NOEXCEPT_EXPR
26941 || TREE_CODE (expression) == TRAIT_EXPR
26942 || TREE_CODE (expression) == TYPEID_EXPR
26943 || TREE_CODE (expression) == DELETE_EXPR
26944 || TREE_CODE (expression) == VEC_DELETE_EXPR
26945 || TREE_CODE (expression) == THROW_EXPR
26946 || TREE_CODE (expression) == REQUIRES_EXPR)
26947 return false;
26948
26949 /* The types of these expressions depends only on the type to which
26950 the cast occurs. */
26951 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26952 || TREE_CODE (expression) == STATIC_CAST_EXPR
26953 || TREE_CODE (expression) == CONST_CAST_EXPR
26954 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26955 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26956 || TREE_CODE (expression) == CAST_EXPR)
26957 return dependent_type_p (TREE_TYPE (expression));
26958
26959 /* The types of these expressions depends only on the type created
26960 by the expression. */
26961 if (TREE_CODE (expression) == NEW_EXPR
26962 || TREE_CODE (expression) == VEC_NEW_EXPR)
26963 {
26964 /* For NEW_EXPR tree nodes created inside a template, either
26965 the object type itself or a TREE_LIST may appear as the
26966 operand 1. */
26967 tree type = TREE_OPERAND (expression, 1);
26968 if (TREE_CODE (type) == TREE_LIST)
26969 /* This is an array type. We need to check array dimensions
26970 as well. */
26971 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26972 || value_dependent_expression_p
26973 (TREE_OPERAND (TREE_VALUE (type), 1));
26974 /* Array type whose dimension has to be deduced. */
26975 else if (TREE_CODE (type) == ARRAY_TYPE
26976 && TREE_OPERAND (expression, 2) == NULL_TREE)
26977 return true;
26978 else
26979 return dependent_type_p (type);
26980 }
26981
26982 if (TREE_CODE (expression) == SCOPE_REF)
26983 {
26984 tree scope = TREE_OPERAND (expression, 0);
26985 tree name = TREE_OPERAND (expression, 1);
26986
26987 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26988 contains an identifier associated by name lookup with one or more
26989 declarations declared with a dependent type, or...a
26990 nested-name-specifier or qualified-id that names a member of an
26991 unknown specialization. */
26992 return (type_dependent_expression_p (name)
26993 || dependent_scope_p (scope));
26994 }
26995
26996 if (TREE_CODE (expression) == TEMPLATE_DECL
26997 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26998 return uses_outer_template_parms (expression);
26999
27000 if (TREE_CODE (expression) == STMT_EXPR)
27001 expression = stmt_expr_value_expr (expression);
27002
27003 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
27004 {
27005 tree elt;
27006 unsigned i;
27007
27008 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
27009 {
27010 if (type_dependent_expression_p (elt))
27011 return true;
27012 }
27013 return false;
27014 }
27015
27016 /* A static data member of the current instantiation with incomplete
27017 array type is type-dependent, as the definition and specializations
27018 can have different bounds. */
27019 if (VAR_P (expression)
27020 && DECL_CLASS_SCOPE_P (expression)
27021 && dependent_type_p (DECL_CONTEXT (expression))
27022 && VAR_HAD_UNKNOWN_BOUND (expression))
27023 return true;
27024
27025 /* An array of unknown bound depending on a variadic parameter, eg:
27026
27027 template<typename... Args>
27028 void foo (Args... args)
27029 {
27030 int arr[] = { args... };
27031 }
27032
27033 template<int... vals>
27034 void bar ()
27035 {
27036 int arr[] = { vals... };
27037 }
27038
27039 If the array has no length and has an initializer, it must be that
27040 we couldn't determine its length in cp_complete_array_type because
27041 it is dependent. */
27042 if (VAR_P (expression)
27043 && TREE_TYPE (expression) != NULL_TREE
27044 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
27045 && !TYPE_DOMAIN (TREE_TYPE (expression))
27046 && DECL_INITIAL (expression))
27047 return true;
27048
27049 /* A function or variable template-id is type-dependent if it has any
27050 dependent template arguments. */
27051 if (VAR_OR_FUNCTION_DECL_P (expression)
27052 && DECL_LANG_SPECIFIC (expression)
27053 && DECL_TEMPLATE_INFO (expression))
27054 {
27055 /* Consider the innermost template arguments, since those are the ones
27056 that come from the template-id; the template arguments for the
27057 enclosing class do not make it type-dependent unless they are used in
27058 the type of the decl. */
27059 if (instantiates_primary_template_p (expression)
27060 && (any_dependent_template_arguments_p
27061 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
27062 return true;
27063 }
27064
27065 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27066 type-dependent. Checking this is important for functions with auto return
27067 type, which looks like a dependent type. */
27068 if (TREE_CODE (expression) == FUNCTION_DECL
27069 && !(DECL_CLASS_SCOPE_P (expression)
27070 && dependent_type_p (DECL_CONTEXT (expression)))
27071 && !(DECL_LANG_SPECIFIC (expression)
27072 && DECL_UNIQUE_FRIEND_P (expression)
27073 && (!DECL_FRIEND_CONTEXT (expression)
27074 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27075 && !DECL_LOCAL_DECL_P (expression))
27076 {
27077 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27078 || undeduced_auto_decl (expression));
27079 return false;
27080 }
27081
27082 /* Always dependent, on the number of arguments if nothing else. */
27083 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27084 return true;
27085
27086 if (TREE_TYPE (expression) == unknown_type_node)
27087 {
27088 if (TREE_CODE (expression) == ADDR_EXPR)
27089 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27090 if (TREE_CODE (expression) == COMPONENT_REF
27091 || TREE_CODE (expression) == OFFSET_REF)
27092 {
27093 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27094 return true;
27095 expression = TREE_OPERAND (expression, 1);
27096 if (identifier_p (expression))
27097 return false;
27098 }
27099 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27100 if (TREE_CODE (expression) == SCOPE_REF)
27101 return false;
27102
27103 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27104 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27105 || TREE_CODE (expression) == CO_YIELD_EXPR)
27106 return true;
27107
27108 if (BASELINK_P (expression))
27109 {
27110 if (BASELINK_OPTYPE (expression)
27111 && dependent_type_p (BASELINK_OPTYPE (expression)))
27112 return true;
27113 expression = BASELINK_FUNCTIONS (expression);
27114 }
27115
27116 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27117 {
27118 if (any_dependent_template_arguments_p
27119 (TREE_OPERAND (expression, 1)))
27120 return true;
27121 expression = TREE_OPERAND (expression, 0);
27122 if (identifier_p (expression))
27123 return true;
27124 }
27125
27126 gcc_assert (OVL_P (expression));
27127
27128 for (lkp_iterator iter (expression); iter; ++iter)
27129 if (type_dependent_expression_p (*iter))
27130 return true;
27131
27132 return false;
27133 }
27134
27135 /* The type of a non-type template parm declared with a placeholder type
27136 depends on the corresponding template argument, even though
27137 placeholders are not normally considered dependent. */
27138 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27139 && is_auto (TREE_TYPE (expression)))
27140 return true;
27141
27142 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27143
27144 /* Dependent type attributes might not have made it from the decl to
27145 the type yet. */
27146 if (DECL_P (expression)
27147 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27148 return true;
27149
27150 return (dependent_type_p (TREE_TYPE (expression)));
27151 }
27152
27153 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27154 type-dependent if the expression refers to a member of the current
27155 instantiation and the type of the referenced member is dependent, or the
27156 class member access expression refers to a member of an unknown
27157 specialization.
27158
27159 This function returns true if the OBJECT in such a class member access
27160 expression is of an unknown specialization. */
27161
27162 bool
27163 type_dependent_object_expression_p (tree object)
27164 {
27165 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27166 dependent. */
27167 if (TREE_CODE (object) == IDENTIFIER_NODE)
27168 return true;
27169 tree scope = TREE_TYPE (object);
27170 return (!scope || dependent_scope_p (scope));
27171 }
27172
27173 /* walk_tree callback function for instantiation_dependent_expression_p,
27174 below. Returns non-zero if a dependent subexpression is found. */
27175
27176 static tree
27177 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27178 void * /*data*/)
27179 {
27180 if (TYPE_P (*tp))
27181 {
27182 /* We don't have to worry about decltype currently because decltype
27183 of an instantiation-dependent expr is a dependent type. This
27184 might change depending on the resolution of DR 1172. */
27185 *walk_subtrees = false;
27186 return NULL_TREE;
27187 }
27188 enum tree_code code = TREE_CODE (*tp);
27189 switch (code)
27190 {
27191 /* Don't treat an argument list as dependent just because it has no
27192 TREE_TYPE. */
27193 case TREE_LIST:
27194 case TREE_VEC:
27195 case NONTYPE_ARGUMENT_PACK:
27196 return NULL_TREE;
27197
27198 case TEMPLATE_PARM_INDEX:
27199 if (dependent_type_p (TREE_TYPE (*tp)))
27200 return *tp;
27201 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27202 return *tp;
27203 /* We'll check value-dependence separately. */
27204 return NULL_TREE;
27205
27206 /* Handle expressions with type operands. */
27207 case SIZEOF_EXPR:
27208 case ALIGNOF_EXPR:
27209 case TYPEID_EXPR:
27210 case AT_ENCODE_EXPR:
27211 {
27212 tree op = TREE_OPERAND (*tp, 0);
27213 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27214 op = TREE_TYPE (op);
27215 if (TYPE_P (op))
27216 {
27217 if (dependent_type_p (op))
27218 return *tp;
27219 else
27220 {
27221 *walk_subtrees = false;
27222 return NULL_TREE;
27223 }
27224 }
27225 break;
27226 }
27227
27228 case COMPONENT_REF:
27229 if (identifier_p (TREE_OPERAND (*tp, 1)))
27230 /* In a template, finish_class_member_access_expr creates a
27231 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27232 type-dependent, so that we can check access control at
27233 instantiation time (PR 42277). See also Core issue 1273. */
27234 return *tp;
27235 break;
27236
27237 case SCOPE_REF:
27238 if (instantiation_dependent_scope_ref_p (*tp))
27239 return *tp;
27240 else
27241 break;
27242
27243 /* Treat statement-expressions as dependent. */
27244 case BIND_EXPR:
27245 return *tp;
27246
27247 /* Treat requires-expressions as dependent. */
27248 case REQUIRES_EXPR:
27249 return *tp;
27250
27251 case CALL_EXPR:
27252 /* Treat concept checks as dependent. */
27253 if (concept_check_p (*tp))
27254 return *tp;
27255 break;
27256
27257 case TEMPLATE_ID_EXPR:
27258 /* Treat concept checks as dependent. */
27259 if (concept_check_p (*tp))
27260 return *tp;
27261 break;
27262
27263 case CONSTRUCTOR:
27264 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27265 return *tp;
27266 break;
27267
27268 default:
27269 break;
27270 }
27271
27272 if (type_dependent_expression_p (*tp))
27273 return *tp;
27274 else
27275 return NULL_TREE;
27276 }
27277
27278 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27279 sense defined by the ABI:
27280
27281 "An expression is instantiation-dependent if it is type-dependent
27282 or value-dependent, or it has a subexpression that is type-dependent
27283 or value-dependent."
27284
27285 Except don't actually check value-dependence for unevaluated expressions,
27286 because in sizeof(i) we don't care about the value of i. Checking
27287 type-dependence will in turn check value-dependence of array bounds/template
27288 arguments as needed. */
27289
27290 bool
27291 instantiation_dependent_uneval_expression_p (tree expression)
27292 {
27293 tree result;
27294
27295 if (!processing_template_decl)
27296 return false;
27297
27298 if (expression == error_mark_node)
27299 return false;
27300
27301 result = cp_walk_tree_without_duplicates (&expression,
27302 instantiation_dependent_r, NULL);
27303 return result != NULL_TREE;
27304 }
27305
27306 /* As above, but also check value-dependence of the expression as a whole. */
27307
27308 bool
27309 instantiation_dependent_expression_p (tree expression)
27310 {
27311 return (instantiation_dependent_uneval_expression_p (expression)
27312 || (potential_constant_expression (expression)
27313 && value_dependent_expression_p (expression)));
27314 }
27315
27316 /* Like type_dependent_expression_p, but it also works while not processing
27317 a template definition, i.e. during substitution or mangling. */
27318
27319 bool
27320 type_dependent_expression_p_push (tree expr)
27321 {
27322 bool b;
27323 ++processing_template_decl;
27324 b = type_dependent_expression_p (expr);
27325 --processing_template_decl;
27326 return b;
27327 }
27328
27329 /* Returns TRUE if ARGS contains a type-dependent expression. */
27330
27331 bool
27332 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27333 {
27334 unsigned int i;
27335 tree arg;
27336
27337 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27338 {
27339 if (type_dependent_expression_p (arg))
27340 return true;
27341 }
27342 return false;
27343 }
27344
27345 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27346 expressions) contains any type-dependent expressions. */
27347
27348 bool
27349 any_type_dependent_elements_p (const_tree list)
27350 {
27351 for (; list; list = TREE_CHAIN (list))
27352 if (type_dependent_expression_p (TREE_VALUE (list)))
27353 return true;
27354
27355 return false;
27356 }
27357
27358 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27359 expressions) contains any value-dependent expressions. */
27360
27361 bool
27362 any_value_dependent_elements_p (const_tree list)
27363 {
27364 for (; list; list = TREE_CHAIN (list))
27365 if (value_dependent_expression_p (TREE_VALUE (list)))
27366 return true;
27367
27368 return false;
27369 }
27370
27371 /* Returns TRUE if the ARG (a template argument) is dependent. */
27372
27373 bool
27374 dependent_template_arg_p (tree arg)
27375 {
27376 if (!processing_template_decl)
27377 return false;
27378
27379 /* Assume a template argument that was wrongly written by the user
27380 is dependent. This is consistent with what
27381 any_dependent_template_arguments_p [that calls this function]
27382 does. */
27383 if (!arg || arg == error_mark_node)
27384 return true;
27385
27386 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27387 arg = argument_pack_select_arg (arg);
27388
27389 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27390 return true;
27391 if (TREE_CODE (arg) == TEMPLATE_DECL)
27392 {
27393 if (DECL_TEMPLATE_PARM_P (arg))
27394 return true;
27395 /* A member template of a dependent class is not necessarily
27396 type-dependent, but it is a dependent template argument because it
27397 will be a member of an unknown specialization to that template. */
27398 tree scope = CP_DECL_CONTEXT (arg);
27399 return TYPE_P (scope) && dependent_type_p (scope);
27400 }
27401 else if (ARGUMENT_PACK_P (arg))
27402 {
27403 tree args = ARGUMENT_PACK_ARGS (arg);
27404 int i, len = TREE_VEC_LENGTH (args);
27405 for (i = 0; i < len; ++i)
27406 {
27407 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27408 return true;
27409 }
27410
27411 return false;
27412 }
27413 else if (TYPE_P (arg))
27414 return dependent_type_p (arg);
27415 else
27416 return value_dependent_expression_p (arg);
27417 }
27418
27419 /* Returns true if ARGS (a collection of template arguments) contains
27420 any types that require structural equality testing. */
27421
27422 bool
27423 any_template_arguments_need_structural_equality_p (tree args)
27424 {
27425 int i;
27426 int j;
27427
27428 if (!args)
27429 return false;
27430 if (args == error_mark_node)
27431 return true;
27432
27433 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27434 {
27435 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27436 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27437 {
27438 tree arg = TREE_VEC_ELT (level, j);
27439 tree packed_args = NULL_TREE;
27440 int k, len = 1;
27441
27442 if (ARGUMENT_PACK_P (arg))
27443 {
27444 /* Look inside the argument pack. */
27445 packed_args = ARGUMENT_PACK_ARGS (arg);
27446 len = TREE_VEC_LENGTH (packed_args);
27447 }
27448
27449 for (k = 0; k < len; ++k)
27450 {
27451 if (packed_args)
27452 arg = TREE_VEC_ELT (packed_args, k);
27453
27454 if (error_operand_p (arg))
27455 return true;
27456 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27457 continue;
27458 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27459 return true;
27460 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27461 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27462 return true;
27463 }
27464 }
27465 }
27466
27467 return false;
27468 }
27469
27470 /* Returns true if ARGS (a collection of template arguments) contains
27471 any dependent arguments. */
27472
27473 bool
27474 any_dependent_template_arguments_p (const_tree args)
27475 {
27476 int i;
27477 int j;
27478
27479 if (!args)
27480 return false;
27481 if (args == error_mark_node)
27482 return true;
27483
27484 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27485 {
27486 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27487 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27488 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27489 return true;
27490 }
27491
27492 return false;
27493 }
27494
27495 /* Returns true if ARGS contains any errors. */
27496
27497 bool
27498 any_erroneous_template_args_p (const_tree args)
27499 {
27500 int i;
27501 int j;
27502
27503 if (args == error_mark_node)
27504 return true;
27505
27506 if (args && TREE_CODE (args) != TREE_VEC)
27507 {
27508 if (tree ti = get_template_info (args))
27509 args = TI_ARGS (ti);
27510 else
27511 args = NULL_TREE;
27512 }
27513
27514 if (!args)
27515 return false;
27516
27517 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27518 {
27519 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27520 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27521 if (error_operand_p (TREE_VEC_ELT (level, j)))
27522 return true;
27523 }
27524
27525 return false;
27526 }
27527
27528 /* Returns TRUE if the template TMPL is type-dependent. */
27529
27530 bool
27531 dependent_template_p (tree tmpl)
27532 {
27533 if (TREE_CODE (tmpl) == OVERLOAD)
27534 {
27535 for (lkp_iterator iter (tmpl); iter; ++iter)
27536 if (dependent_template_p (*iter))
27537 return true;
27538 return false;
27539 }
27540
27541 /* Template template parameters are dependent. */
27542 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27543 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27544 return true;
27545 /* So are names that have not been looked up. */
27546 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27547 return true;
27548 return false;
27549 }
27550
27551 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27552
27553 bool
27554 dependent_template_id_p (tree tmpl, tree args)
27555 {
27556 return (dependent_template_p (tmpl)
27557 || any_dependent_template_arguments_p (args));
27558 }
27559
27560 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27561 are dependent. */
27562
27563 bool
27564 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27565 {
27566 int i;
27567
27568 if (!processing_template_decl)
27569 return false;
27570
27571 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27572 {
27573 tree decl = TREE_VEC_ELT (declv, i);
27574 tree init = TREE_VEC_ELT (initv, i);
27575 tree cond = TREE_VEC_ELT (condv, i);
27576 tree incr = TREE_VEC_ELT (incrv, i);
27577
27578 if (type_dependent_expression_p (decl)
27579 || TREE_CODE (decl) == SCOPE_REF)
27580 return true;
27581
27582 if (init && type_dependent_expression_p (init))
27583 return true;
27584
27585 if (cond == global_namespace)
27586 return true;
27587
27588 if (type_dependent_expression_p (cond))
27589 return true;
27590
27591 if (COMPARISON_CLASS_P (cond)
27592 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27593 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27594 return true;
27595
27596 if (TREE_CODE (incr) == MODOP_EXPR)
27597 {
27598 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27599 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27600 return true;
27601 }
27602 else if (type_dependent_expression_p (incr))
27603 return true;
27604 else if (TREE_CODE (incr) == MODIFY_EXPR)
27605 {
27606 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27607 return true;
27608 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27609 {
27610 tree t = TREE_OPERAND (incr, 1);
27611 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27612 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27613 return true;
27614
27615 /* If this loop has a class iterator with != comparison
27616 with increment other than i++/++i/i--/--i, make sure the
27617 increment is constant. */
27618 if (CLASS_TYPE_P (TREE_TYPE (decl))
27619 && TREE_CODE (cond) == NE_EXPR)
27620 {
27621 if (TREE_OPERAND (t, 0) == decl)
27622 t = TREE_OPERAND (t, 1);
27623 else
27624 t = TREE_OPERAND (t, 0);
27625 if (TREE_CODE (t) != INTEGER_CST)
27626 return true;
27627 }
27628 }
27629 }
27630 }
27631
27632 return false;
27633 }
27634
27635 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27636 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27637 no such TYPE can be found. Note that this function peers inside
27638 uninstantiated templates and therefore should be used only in
27639 extremely limited situations. ONLY_CURRENT_P restricts this
27640 peering to the currently open classes hierarchy (which is required
27641 when comparing types). */
27642
27643 tree
27644 resolve_typename_type (tree type, bool only_current_p)
27645 {
27646 tree scope;
27647 tree name;
27648 tree decl;
27649 int quals;
27650 tree pushed_scope;
27651 tree result;
27652
27653 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27654
27655 scope = TYPE_CONTEXT (type);
27656 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27657 gcc_checking_assert (uses_template_parms (scope));
27658
27659 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27660 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27661 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27662 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27663 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27664 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27665 the TYPENAME_TYPE instead, we avoid messing up with a possible
27666 typedef variant case. */
27667 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27668
27669 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27670 it first before we can figure out what NAME refers to. */
27671 if (TREE_CODE (scope) == TYPENAME_TYPE)
27672 {
27673 if (TYPENAME_IS_RESOLVING_P (scope))
27674 /* Given a class template A with a dependent base with nested type C,
27675 typedef typename A::C::C C will land us here, as trying to resolve
27676 the initial A::C leads to the local C typedef, which leads back to
27677 A::C::C. So we break the recursion now. */
27678 return type;
27679 else
27680 scope = resolve_typename_type (scope, only_current_p);
27681 }
27682 /* If we don't know what SCOPE refers to, then we cannot resolve the
27683 TYPENAME_TYPE. */
27684 if (!CLASS_TYPE_P (scope))
27685 return type;
27686 /* If this is a typedef, we don't want to look inside (c++/11987). */
27687 if (typedef_variant_p (type))
27688 return type;
27689 /* If SCOPE isn't the template itself, it will not have a valid
27690 TYPE_FIELDS list. */
27691 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27692 /* scope is either the template itself or a compatible instantiation
27693 like X<T>, so look up the name in the original template. */
27694 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27695 /* If scope has no fields, it can't be a current instantiation. Check this
27696 before currently_open_class to avoid infinite recursion (71515). */
27697 if (!TYPE_FIELDS (scope))
27698 return type;
27699 /* If the SCOPE is not the current instantiation, there's no reason
27700 to look inside it. */
27701 if (only_current_p && !currently_open_class (scope))
27702 return type;
27703 /* Enter the SCOPE so that name lookup will be resolved as if we
27704 were in the class definition. In particular, SCOPE will no
27705 longer be considered a dependent type. */
27706 pushed_scope = push_scope (scope);
27707 /* Look up the declaration. */
27708 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27709 tf_warning_or_error);
27710
27711 result = NULL_TREE;
27712
27713 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27714 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27715 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27716 if (!decl)
27717 /*nop*/;
27718 else if (identifier_p (fullname)
27719 && TREE_CODE (decl) == TYPE_DECL)
27720 {
27721 result = TREE_TYPE (decl);
27722 if (result == error_mark_node)
27723 result = NULL_TREE;
27724 }
27725 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27726 && DECL_CLASS_TEMPLATE_P (decl))
27727 {
27728 /* Obtain the template and the arguments. */
27729 tree tmpl = TREE_OPERAND (fullname, 0);
27730 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27731 {
27732 /* We get here with a plain identifier because a previous tentative
27733 parse of the nested-name-specifier as part of a ptr-operator saw
27734 ::template X<A>. The use of ::template is necessary in a
27735 ptr-operator, but wrong in a declarator-id.
27736
27737 [temp.names]: In a qualified-id of a declarator-id, the keyword
27738 template shall not appear at the top level. */
27739 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27740 "keyword %<template%> not allowed in declarator-id");
27741 tmpl = decl;
27742 }
27743 tree args = TREE_OPERAND (fullname, 1);
27744 /* Instantiate the template. */
27745 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27746 /*entering_scope=*/true,
27747 tf_error | tf_user);
27748 if (result == error_mark_node)
27749 result = NULL_TREE;
27750 }
27751
27752 /* Leave the SCOPE. */
27753 if (pushed_scope)
27754 pop_scope (pushed_scope);
27755
27756 /* If we failed to resolve it, return the original typename. */
27757 if (!result)
27758 return type;
27759
27760 /* If lookup found a typename type, resolve that too. */
27761 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27762 {
27763 /* Ill-formed programs can cause infinite recursion here, so we
27764 must catch that. */
27765 TYPENAME_IS_RESOLVING_P (result) = 1;
27766 result = resolve_typename_type (result, only_current_p);
27767 TYPENAME_IS_RESOLVING_P (result) = 0;
27768 }
27769
27770 /* Qualify the resulting type. */
27771 quals = cp_type_quals (type);
27772 if (quals)
27773 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27774
27775 return result;
27776 }
27777
27778 /* EXPR is an expression which is not type-dependent. Return a proxy
27779 for EXPR that can be used to compute the types of larger
27780 expressions containing EXPR. */
27781
27782 tree
27783 build_non_dependent_expr (tree expr)
27784 {
27785 tree orig_expr = expr;
27786 tree inner_expr;
27787
27788 /* When checking, try to get a constant value for all non-dependent
27789 expressions in order to expose bugs in *_dependent_expression_p
27790 and constexpr. This can affect code generation, see PR70704, so
27791 only do this for -fchecking=2. */
27792 if (flag_checking > 1
27793 && cxx_dialect >= cxx11
27794 /* Don't do this during nsdmi parsing as it can lead to
27795 unexpected recursive instantiations. */
27796 && !parsing_nsdmi ()
27797 /* Don't do this during concept processing either and for
27798 the same reason. */
27799 && !processing_constraint_expression_p ())
27800 fold_non_dependent_expr (expr, tf_none);
27801
27802 STRIP_ANY_LOCATION_WRAPPER (expr);
27803
27804 /* Preserve OVERLOADs; the functions must be available to resolve
27805 types. */
27806 inner_expr = expr;
27807 if (TREE_CODE (inner_expr) == STMT_EXPR)
27808 inner_expr = stmt_expr_value_expr (inner_expr);
27809 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27810 inner_expr = TREE_OPERAND (inner_expr, 0);
27811 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27812 inner_expr = TREE_OPERAND (inner_expr, 1);
27813 if (is_overloaded_fn (inner_expr)
27814 || TREE_CODE (inner_expr) == OFFSET_REF)
27815 return orig_expr;
27816 /* There is no need to return a proxy for a variable or enumerator. */
27817 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27818 return orig_expr;
27819 /* Preserve string constants; conversions from string constants to
27820 "char *" are allowed, even though normally a "const char *"
27821 cannot be used to initialize a "char *". */
27822 if (TREE_CODE (expr) == STRING_CST)
27823 return orig_expr;
27824 /* Preserve void and arithmetic constants, as an optimization -- there is no
27825 reason to create a new node. */
27826 if (TREE_CODE (expr) == VOID_CST
27827 || TREE_CODE (expr) == INTEGER_CST
27828 || TREE_CODE (expr) == REAL_CST)
27829 return orig_expr;
27830 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27831 There is at least one place where we want to know that a
27832 particular expression is a throw-expression: when checking a ?:
27833 expression, there are special rules if the second or third
27834 argument is a throw-expression. */
27835 if (TREE_CODE (expr) == THROW_EXPR)
27836 return orig_expr;
27837
27838 /* Don't wrap an initializer list, we need to be able to look inside. */
27839 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27840 return orig_expr;
27841
27842 /* Don't wrap a dummy object, we need to be able to test for it. */
27843 if (is_dummy_object (expr))
27844 return orig_expr;
27845
27846 if (TREE_CODE (expr) == COND_EXPR)
27847 return build3 (COND_EXPR,
27848 TREE_TYPE (expr),
27849 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27850 (TREE_OPERAND (expr, 1)
27851 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27852 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27853 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27854 if (TREE_CODE (expr) == COMPOUND_EXPR
27855 && !COMPOUND_EXPR_OVERLOADED (expr))
27856 return build2 (COMPOUND_EXPR,
27857 TREE_TYPE (expr),
27858 TREE_OPERAND (expr, 0),
27859 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27860
27861 /* If the type is unknown, it can't really be non-dependent */
27862 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27863
27864 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27865 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27866 TREE_TYPE (expr), expr);
27867 }
27868
27869 /* ARGS is a vector of expressions as arguments to a function call.
27870 Replace the arguments with equivalent non-dependent expressions.
27871 This modifies ARGS in place. */
27872
27873 void
27874 make_args_non_dependent (vec<tree, va_gc> *args)
27875 {
27876 unsigned int ix;
27877 tree arg;
27878
27879 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27880 {
27881 tree newarg = build_non_dependent_expr (arg);
27882 if (newarg != arg)
27883 (*args)[ix] = newarg;
27884 }
27885 }
27886
27887 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27888 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27889 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27890
27891 static tree
27892 make_auto_1 (tree name, bool set_canonical)
27893 {
27894 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27895 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27896 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27897 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27898 (0, processing_template_decl + 1, processing_template_decl + 1,
27899 TYPE_NAME (au), NULL_TREE);
27900 if (set_canonical)
27901 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27902 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27903 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27904 if (name == decltype_auto_identifier)
27905 AUTO_IS_DECLTYPE (au) = true;
27906
27907 return au;
27908 }
27909
27910 tree
27911 make_decltype_auto (void)
27912 {
27913 return make_auto_1 (decltype_auto_identifier, true);
27914 }
27915
27916 tree
27917 make_auto (void)
27918 {
27919 return make_auto_1 (auto_identifier, true);
27920 }
27921
27922 /* Return a C++17 deduction placeholder for class template TMPL. */
27923
27924 tree
27925 make_template_placeholder (tree tmpl)
27926 {
27927 tree t = make_auto_1 (auto_identifier, false);
27928 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27929 /* Our canonical type depends on the placeholder. */
27930 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27931 return t;
27932 }
27933
27934 /* True iff T is a C++17 class template deduction placeholder. */
27935
27936 bool
27937 template_placeholder_p (tree t)
27938 {
27939 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27940 }
27941
27942 /* Make a "constrained auto" type-specifier. This is an auto or
27943 decltype(auto) type with constraints that must be associated after
27944 deduction. The constraint is formed from the given concept CON
27945 and its optional sequence of template arguments ARGS.
27946
27947 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27948
27949 static tree
27950 make_constrained_placeholder_type (tree type, tree con, tree args)
27951 {
27952 /* Build the constraint. */
27953 tree tmpl = DECL_TI_TEMPLATE (con);
27954 tree expr = tmpl;
27955 if (TREE_CODE (con) == FUNCTION_DECL)
27956 expr = ovl_make (tmpl);
27957 ++processing_template_decl;
27958 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27959 --processing_template_decl;
27960
27961 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27962
27963 /* Our canonical type depends on the constraint. */
27964 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27965
27966 /* Attach the constraint to the type declaration. */
27967 return TYPE_NAME (type);
27968 }
27969
27970 /* Make a "constrained auto" type-specifier. */
27971
27972 tree
27973 make_constrained_auto (tree con, tree args)
27974 {
27975 tree type = make_auto_1 (auto_identifier, false);
27976 return make_constrained_placeholder_type (type, con, args);
27977 }
27978
27979 /* Make a "constrained decltype(auto)" type-specifier. */
27980
27981 tree
27982 make_constrained_decltype_auto (tree con, tree args)
27983 {
27984 tree type = make_auto_1 (decltype_auto_identifier, false);
27985 return make_constrained_placeholder_type (type, con, args);
27986 }
27987
27988 /* Build and return a concept definition. Like other templates, the
27989 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27990 the TEMPLATE_DECL. */
27991
27992 tree
27993 finish_concept_definition (cp_expr id, tree init)
27994 {
27995 gcc_assert (identifier_p (id));
27996 gcc_assert (processing_template_decl);
27997
27998 location_t loc = id.get_location();
27999
28000 /* A concept-definition shall not have associated constraints. */
28001 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
28002 {
28003 error_at (loc, "a concept cannot be constrained");
28004 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
28005 }
28006
28007 /* A concept-definition shall appear in namespace scope. Templates
28008 aren't allowed in block scope, so we only need to check for class
28009 scope. */
28010 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28011 {
28012 error_at (loc, "concept %qE not in namespace scope", *id);
28013 return error_mark_node;
28014 }
28015
28016 /* Initially build the concept declaration; its type is bool. */
28017 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
28018 DECL_CONTEXT (decl) = current_scope ();
28019 DECL_INITIAL (decl) = init;
28020
28021 /* Push the enclosing template. */
28022 return push_template_decl (decl);
28023 }
28024
28025 /* Given type ARG, return std::initializer_list<ARG>. */
28026
28027 static tree
28028 listify (tree arg)
28029 {
28030 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
28031
28032 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
28033 {
28034 gcc_rich_location richloc (input_location);
28035 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
28036 error_at (&richloc,
28037 "deducing from brace-enclosed initializer list"
28038 " requires %<#include <initializer_list>%>");
28039
28040 return error_mark_node;
28041 }
28042 tree argvec = make_tree_vec (1);
28043 TREE_VEC_ELT (argvec, 0) = arg;
28044
28045 return lookup_template_class (std_init_list, argvec, NULL_TREE,
28046 NULL_TREE, 0, tf_warning_or_error);
28047 }
28048
28049 /* Replace auto in TYPE with std::initializer_list<auto>. */
28050
28051 static tree
28052 listify_autos (tree type, tree auto_node)
28053 {
28054 tree init_auto = listify (strip_top_quals (auto_node));
28055 tree argvec = make_tree_vec (1);
28056 TREE_VEC_ELT (argvec, 0) = init_auto;
28057 if (processing_template_decl)
28058 argvec = add_to_template_args (current_template_args (), argvec);
28059 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
28060 }
28061
28062 /* Hash traits for hashing possibly constrained 'auto'
28063 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28064
28065 struct auto_hash : default_hash_traits<tree>
28066 {
28067 static inline hashval_t hash (tree);
28068 static inline bool equal (tree, tree);
28069 };
28070
28071 /* Hash the 'auto' T. */
28072
28073 inline hashval_t
28074 auto_hash::hash (tree t)
28075 {
28076 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28077 /* Matching constrained-type-specifiers denote the same template
28078 parameter, so hash the constraint. */
28079 return hash_placeholder_constraint (c);
28080 else
28081 /* But unconstrained autos are all separate, so just hash the pointer. */
28082 return iterative_hash_object (t, 0);
28083 }
28084
28085 /* Compare two 'auto's. */
28086
28087 inline bool
28088 auto_hash::equal (tree t1, tree t2)
28089 {
28090 if (t1 == t2)
28091 return true;
28092
28093 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28094 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28095
28096 /* Two unconstrained autos are distinct. */
28097 if (!c1 || !c2)
28098 return false;
28099
28100 return equivalent_placeholder_constraints (c1, c2);
28101 }
28102
28103 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28104 constrained) auto, add it to the vector. */
28105
28106 static int
28107 extract_autos_r (tree t, void *data)
28108 {
28109 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28110 if (is_auto (t))
28111 {
28112 /* All the autos were built with index 0; fix that up now. */
28113 tree *p = hash.find_slot (t, INSERT);
28114 unsigned idx;
28115 if (*p)
28116 /* If this is a repeated constrained-type-specifier, use the index we
28117 chose before. */
28118 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28119 else
28120 {
28121 /* Otherwise this is new, so use the current count. */
28122 *p = t;
28123 idx = hash.elements () - 1;
28124 }
28125 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28126 }
28127
28128 /* Always keep walking. */
28129 return 0;
28130 }
28131
28132 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28133 says they can appear anywhere in the type. */
28134
28135 static tree
28136 extract_autos (tree type)
28137 {
28138 hash_set<tree> visited;
28139 hash_table<auto_hash> hash (2);
28140
28141 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28142
28143 tree tree_vec = make_tree_vec (hash.elements());
28144 for (hash_table<auto_hash>::iterator iter = hash.begin();
28145 iter != hash.end(); ++iter)
28146 {
28147 tree elt = *iter;
28148 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28149 TREE_VEC_ELT (tree_vec, i)
28150 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28151 }
28152
28153 return tree_vec;
28154 }
28155
28156 /* The stem for deduction guide names. */
28157 const char *const dguide_base = "__dguide_";
28158
28159 /* Return the name for a deduction guide for class template TMPL. */
28160
28161 tree
28162 dguide_name (tree tmpl)
28163 {
28164 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28165 tree tname = TYPE_IDENTIFIER (type);
28166 char *buf = (char *) alloca (1 + strlen (dguide_base)
28167 + IDENTIFIER_LENGTH (tname));
28168 memcpy (buf, dguide_base, strlen (dguide_base));
28169 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28170 IDENTIFIER_LENGTH (tname) + 1);
28171 tree dname = get_identifier (buf);
28172 TREE_TYPE (dname) = type;
28173 return dname;
28174 }
28175
28176 /* True if NAME is the name of a deduction guide. */
28177
28178 bool
28179 dguide_name_p (tree name)
28180 {
28181 return (TREE_CODE (name) == IDENTIFIER_NODE
28182 && TREE_TYPE (name)
28183 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
28184 strlen (dguide_base)));
28185 }
28186
28187 /* True if FN is a deduction guide. */
28188
28189 bool
28190 deduction_guide_p (const_tree fn)
28191 {
28192 if (DECL_P (fn))
28193 if (tree name = DECL_NAME (fn))
28194 return dguide_name_p (name);
28195 return false;
28196 }
28197
28198 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28199
28200 bool
28201 copy_guide_p (const_tree fn)
28202 {
28203 gcc_assert (deduction_guide_p (fn));
28204 if (!DECL_ARTIFICIAL (fn))
28205 return false;
28206 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28207 return (TREE_CHAIN (parms) == void_list_node
28208 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28209 }
28210
28211 /* True if FN is a guide generated from a constructor template. */
28212
28213 bool
28214 template_guide_p (const_tree fn)
28215 {
28216 gcc_assert (deduction_guide_p (fn));
28217 if (!DECL_ARTIFICIAL (fn))
28218 return false;
28219 tree tmpl = DECL_TI_TEMPLATE (fn);
28220 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28221 return PRIMARY_TEMPLATE_P (org);
28222 return false;
28223 }
28224
28225 /* True if FN is an aggregate initialization guide or the copy deduction
28226 guide. */
28227
28228 bool
28229 builtin_guide_p (const_tree fn)
28230 {
28231 if (!deduction_guide_p (fn))
28232 return false;
28233 if (!DECL_ARTIFICIAL (fn))
28234 /* Explicitly declared. */
28235 return false;
28236 if (DECL_ABSTRACT_ORIGIN (fn))
28237 /* Derived from a constructor. */
28238 return false;
28239 return true;
28240 }
28241
28242 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28243 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28244 template parameter types. Note that the handling of template template
28245 parameters relies on current_template_parms being set appropriately for the
28246 new template. */
28247
28248 static tree
28249 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28250 tree tsubst_args, tsubst_flags_t complain)
28251 {
28252 if (olddecl == error_mark_node)
28253 return error_mark_node;
28254
28255 tree oldidx = get_template_parm_index (olddecl);
28256
28257 tree newtype;
28258 if (TREE_CODE (olddecl) == TYPE_DECL
28259 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28260 {
28261 tree oldtype = TREE_TYPE (olddecl);
28262 newtype = cxx_make_type (TREE_CODE (oldtype));
28263 TYPE_MAIN_VARIANT (newtype) = newtype;
28264 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28265 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28266 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28267 }
28268 else
28269 {
28270 newtype = TREE_TYPE (olddecl);
28271 if (type_uses_auto (newtype))
28272 {
28273 // Substitute once to fix references to other template parameters.
28274 newtype = tsubst (newtype, tsubst_args,
28275 complain|tf_partial, NULL_TREE);
28276 // Now substitute again to reduce the level of the auto.
28277 newtype = tsubst (newtype, current_template_args (),
28278 complain, NULL_TREE);
28279 }
28280 else
28281 newtype = tsubst (newtype, tsubst_args,
28282 complain, NULL_TREE);
28283 }
28284
28285 tree newdecl
28286 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28287 DECL_NAME (olddecl), newtype);
28288 SET_DECL_TEMPLATE_PARM_P (newdecl);
28289
28290 tree newidx;
28291 if (TREE_CODE (olddecl) == TYPE_DECL
28292 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28293 {
28294 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28295 = build_template_parm_index (index, level, level,
28296 newdecl, newtype);
28297 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28298 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28299 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28300 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28301 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28302 else
28303 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28304
28305 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28306 {
28307 DECL_TEMPLATE_RESULT (newdecl)
28308 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28309 DECL_NAME (olddecl), newtype);
28310 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28311 // First create a copy (ttargs) of tsubst_args with an
28312 // additional level for the template template parameter's own
28313 // template parameters (ttparms).
28314 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28315 (DECL_TEMPLATE_PARMS (olddecl)));
28316 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28317 tree ttargs = make_tree_vec (depth + 1);
28318 for (int i = 0; i < depth; ++i)
28319 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28320 TREE_VEC_ELT (ttargs, depth)
28321 = template_parms_level_to_args (ttparms);
28322 // Substitute ttargs into ttparms to fix references to
28323 // other template parameters.
28324 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28325 complain|tf_partial);
28326 // Now substitute again with args based on tparms, to reduce
28327 // the level of the ttparms.
28328 ttargs = current_template_args ();
28329 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28330 complain);
28331 // Finally, tack the adjusted parms onto tparms.
28332 ttparms = tree_cons (size_int (depth), ttparms,
28333 current_template_parms);
28334 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28335 }
28336 }
28337 else
28338 {
28339 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28340 tree newconst
28341 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28342 TREE_CODE (oldconst),
28343 DECL_NAME (oldconst), newtype);
28344 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28345 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28346 SET_DECL_TEMPLATE_PARM_P (newconst);
28347 newidx = build_template_parm_index (index, level, level,
28348 newconst, newtype);
28349 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28350 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28351 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28352 }
28353
28354 return newdecl;
28355 }
28356
28357 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28358 template parameter. */
28359
28360 static tree
28361 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28362 tree targs, unsigned targs_index, tsubst_flags_t complain)
28363 {
28364 tree olddecl = TREE_VALUE (oldelt);
28365 tree newdecl = rewrite_template_parm (olddecl, index, level,
28366 targs, complain);
28367 if (newdecl == error_mark_node)
28368 return error_mark_node;
28369 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28370 targs, complain, NULL_TREE);
28371 tree list = build_tree_list (newdef, newdecl);
28372 TEMPLATE_PARM_CONSTRAINTS (list)
28373 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28374 targs, complain, NULL_TREE);
28375 int depth = TMPL_ARGS_DEPTH (targs);
28376 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28377 return list;
28378 }
28379
28380 /* Returns a C++17 class deduction guide template based on the constructor
28381 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28382 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28383 aggregate initialization guide. */
28384
28385 static tree
28386 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28387 {
28388 tree tparms, targs, fparms, fargs, ci;
28389 bool memtmpl = false;
28390 bool explicit_p;
28391 location_t loc;
28392 tree fn_tmpl = NULL_TREE;
28393
28394 if (outer_args)
28395 {
28396 ++processing_template_decl;
28397 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28398 --processing_template_decl;
28399 }
28400
28401 if (!DECL_DECLARES_FUNCTION_P (ctor))
28402 {
28403 if (TYPE_P (ctor))
28404 {
28405 bool copy_p = TYPE_REF_P (ctor);
28406 if (copy_p)
28407 fparms = tree_cons (NULL_TREE, type, void_list_node);
28408 else
28409 fparms = void_list_node;
28410 }
28411 else if (TREE_CODE (ctor) == TREE_LIST)
28412 fparms = ctor;
28413 else
28414 gcc_unreachable ();
28415
28416 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28417 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28418 targs = CLASSTYPE_TI_ARGS (type);
28419 ci = NULL_TREE;
28420 fargs = NULL_TREE;
28421 loc = DECL_SOURCE_LOCATION (ctmpl);
28422 explicit_p = false;
28423 }
28424 else
28425 {
28426 ++processing_template_decl;
28427 bool ok = true;
28428
28429 fn_tmpl
28430 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28431 : DECL_TI_TEMPLATE (ctor));
28432 if (outer_args)
28433 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28434 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28435
28436 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28437 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28438 fully specialized args for the enclosing class. Strip those off, as
28439 the deduction guide won't have those template parameters. */
28440 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28441 TMPL_PARMS_DEPTH (tparms));
28442 /* Discard the 'this' parameter. */
28443 fparms = FUNCTION_ARG_CHAIN (ctor);
28444 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28445 ci = get_constraints (ctor);
28446 loc = DECL_SOURCE_LOCATION (ctor);
28447 explicit_p = DECL_NONCONVERTING_P (ctor);
28448
28449 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28450 {
28451 memtmpl = true;
28452
28453 /* For a member template constructor, we need to flatten the two
28454 template parameter lists into one, and then adjust the function
28455 signature accordingly. This gets...complicated. */
28456 tree save_parms = current_template_parms;
28457
28458 /* For a member template we should have two levels of parms/args, one
28459 for the class and one for the constructor. We stripped
28460 specialized args for further enclosing classes above. */
28461 const int depth = 2;
28462 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28463
28464 /* Template args for translating references to the two-level template
28465 parameters into references to the one-level template parameters we
28466 are creating. */
28467 tree tsubst_args = copy_node (targs);
28468 TMPL_ARGS_LEVEL (tsubst_args, depth)
28469 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28470
28471 /* Template parms for the constructor template. */
28472 tree ftparms = TREE_VALUE (tparms);
28473 unsigned flen = TREE_VEC_LENGTH (ftparms);
28474 /* Template parms for the class template. */
28475 tparms = TREE_CHAIN (tparms);
28476 tree ctparms = TREE_VALUE (tparms);
28477 unsigned clen = TREE_VEC_LENGTH (ctparms);
28478 /* Template parms for the deduction guide start as a copy of the
28479 template parms for the class. We set current_template_parms for
28480 lookup_template_class_1. */
28481 current_template_parms = tparms = copy_node (tparms);
28482 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28483 for (unsigned i = 0; i < clen; ++i)
28484 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28485
28486 /* Now we need to rewrite the constructor parms to append them to the
28487 class parms. */
28488 for (unsigned i = 0; i < flen; ++i)
28489 {
28490 unsigned index = i + clen;
28491 unsigned level = 1;
28492 tree oldelt = TREE_VEC_ELT (ftparms, i);
28493 tree newelt
28494 = rewrite_tparm_list (oldelt, index, level,
28495 tsubst_args, i, complain);
28496 if (newelt == error_mark_node)
28497 ok = false;
28498 TREE_VEC_ELT (new_vec, index) = newelt;
28499 }
28500
28501 /* Now we have a final set of template parms to substitute into the
28502 function signature. */
28503 targs = template_parms_to_args (tparms);
28504 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28505 complain, ctor);
28506 if (fparms == error_mark_node)
28507 ok = false;
28508 if (ci)
28509 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28510
28511 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28512 cp_unevaluated_operand. */
28513 cp_evaluated ev;
28514 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28515 current_template_parms = save_parms;
28516 }
28517 else
28518 {
28519 /* Substitute in the same arguments to rewrite class members into
28520 references to members of an unknown specialization. */
28521 cp_evaluated ev;
28522 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28523 fargs = tsubst (fargs, targs, complain, ctor);
28524 if (ci)
28525 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28526 }
28527
28528 --processing_template_decl;
28529 if (!ok)
28530 return error_mark_node;
28531 }
28532
28533 if (!memtmpl)
28534 {
28535 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28536 tparms = copy_node (tparms);
28537 INNERMOST_TEMPLATE_PARMS (tparms)
28538 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28539 }
28540
28541 tree fntype = build_function_type (type, fparms);
28542 tree ded_fn = build_lang_decl_loc (loc,
28543 FUNCTION_DECL,
28544 dguide_name (type), fntype);
28545 DECL_ARGUMENTS (ded_fn) = fargs;
28546 DECL_ARTIFICIAL (ded_fn) = true;
28547 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28548 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28549 DECL_ARTIFICIAL (ded_tmpl) = true;
28550 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28551 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28552 if (DECL_P (ctor))
28553 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28554 if (ci)
28555 set_constraints (ded_tmpl, ci);
28556
28557 return ded_tmpl;
28558 }
28559
28560 /* Add to LIST the member types for the reshaped initializer CTOR. */
28561
28562 static tree
28563 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28564 {
28565 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28566 tree idx, val; unsigned i;
28567 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28568 {
28569 tree ftype = elt ? elt : TREE_TYPE (idx);
28570 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28571 && CONSTRUCTOR_NELTS (val)
28572 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28573 type gets a single initializer. */
28574 && CP_AGGREGATE_TYPE_P (ftype)
28575 && !(TREE_CODE (ftype) == ARRAY_TYPE
28576 && uses_template_parms (TYPE_DOMAIN (ftype))))
28577 {
28578 tree subelt = NULL_TREE;
28579 if (TREE_CODE (ftype) == ARRAY_TYPE)
28580 subelt = TREE_TYPE (ftype);
28581 list = collect_ctor_idx_types (val, list, subelt);
28582 continue;
28583 }
28584 tree arg = NULL_TREE;
28585 if (i == v->length() - 1
28586 && PACK_EXPANSION_P (ftype))
28587 /* Give the trailing pack expansion parameter a default argument to
28588 match aggregate initialization behavior, even if we deduce the
28589 length of the pack separately to more than we have initializers. */
28590 arg = build_constructor (init_list_type_node, NULL);
28591 /* if ei is of array type and xi is a braced-init-list or string literal,
28592 Ti is an rvalue reference to the declared type of ei */
28593 STRIP_ANY_LOCATION_WRAPPER (val);
28594 if (TREE_CODE (ftype) == ARRAY_TYPE
28595 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28596 || TREE_CODE (val) == STRING_CST))
28597 {
28598 if (TREE_CODE (val) == STRING_CST)
28599 ftype = cp_build_qualified_type
28600 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28601 ftype = (cp_build_reference_type
28602 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28603 }
28604 list = tree_cons (arg, ftype, list);
28605 }
28606
28607 return list;
28608 }
28609
28610 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28611
28612 static bool
28613 is_spec_or_derived (tree etype, tree tmpl)
28614 {
28615 if (!etype || !CLASS_TYPE_P (etype))
28616 return false;
28617
28618 tree type = TREE_TYPE (tmpl);
28619 tree tparms = (INNERMOST_TEMPLATE_PARMS
28620 (DECL_TEMPLATE_PARMS (tmpl)));
28621 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28622 int err = unify (tparms, targs, type, etype,
28623 UNIFY_ALLOW_DERIVED, /*explain*/false);
28624 ggc_free (targs);
28625 return !err;
28626 }
28627
28628 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28629 INIT. */
28630
28631 static tree
28632 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28633 {
28634 if (cxx_dialect < cxx20)
28635 return NULL_TREE;
28636
28637 if (init == NULL_TREE)
28638 return NULL_TREE;
28639
28640 tree type = TREE_TYPE (tmpl);
28641 if (!CP_AGGREGATE_TYPE_P (type))
28642 return NULL_TREE;
28643
28644 /* No aggregate candidate for copy-initialization. */
28645 if (args->length() == 1)
28646 {
28647 tree val = (*args)[0];
28648 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28649 return NULL_TREE;
28650 }
28651
28652 /* If we encounter a problem, we just won't add the candidate. */
28653 tsubst_flags_t complain = tf_none;
28654
28655 tree parms = NULL_TREE;
28656 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28657 {
28658 init = reshape_init (type, init, complain);
28659 if (init == error_mark_node)
28660 return NULL_TREE;
28661 parms = collect_ctor_idx_types (init, parms);
28662 }
28663 else if (TREE_CODE (init) == TREE_LIST)
28664 {
28665 int len = list_length (init);
28666 for (tree field = TYPE_FIELDS (type);
28667 len;
28668 --len, field = DECL_CHAIN (field))
28669 {
28670 field = next_initializable_field (field);
28671 if (!field)
28672 return NULL_TREE;
28673 tree ftype = finish_decltype_type (field, true, complain);
28674 parms = tree_cons (NULL_TREE, ftype, parms);
28675 }
28676 }
28677 else
28678 /* Aggregate initialization doesn't apply to an initializer expression. */
28679 return NULL_TREE;
28680
28681 if (parms)
28682 {
28683 tree last = parms;
28684 parms = nreverse (parms);
28685 TREE_CHAIN (last) = void_list_node;
28686 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28687 return guide;
28688 }
28689
28690 return NULL_TREE;
28691 }
28692
28693 /* UGUIDES are the deduction guides for the underlying template of alias
28694 template TMPL; adjust them to be deduction guides for TMPL. */
28695
28696 static tree
28697 alias_ctad_tweaks (tree tmpl, tree uguides)
28698 {
28699 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28700 class type (9.2.8.2) where the template-name names an alias template A,
28701 the defining-type-id of A must be of the form
28702
28703 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28704
28705 as specified in 9.2.8.2. The guides of A are the set of functions or
28706 function templates formed as follows. For each function or function
28707 template f in the guides of the template named by the simple-template-id
28708 of the defining-type-id, the template arguments of the return type of f
28709 are deduced from the defining-type-id of A according to the process in
28710 13.10.2.5 with the exception that deduction does not fail if not all
28711 template arguments are deduced. Let g denote the result of substituting
28712 these deductions into f. If substitution succeeds, form a function or
28713 function template f' with the following properties and add it to the set
28714 of guides of A:
28715
28716 * The function type of f' is the function type of g.
28717
28718 * If f is a function template, f' is a function template whose template
28719 parameter list consists of all the template parameters of A (including
28720 their default template arguments) that appear in the above deductions or
28721 (recursively) in their default template arguments, followed by the
28722 template parameters of f that were not deduced (including their default
28723 template arguments), otherwise f' is not a function template.
28724
28725 * The associated constraints (13.5.2) are the conjunction of the
28726 associated constraints of g and a constraint that is satisfied if and only
28727 if the arguments of A are deducible (see below) from the return type.
28728
28729 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28730 be so as well.
28731
28732 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28733 considered to be so as well.
28734
28735 * The explicit-specifier of f' is the explicit-specifier of g (if
28736 any). */
28737
28738 /* This implementation differs from the above in two significant ways:
28739
28740 1) We include all template parameters of A, not just some.
28741 2) The added constraint is same_type instead of deducible.
28742
28743 I believe that while it's probably possible to construct a testcase that
28744 behaves differently with this simplification, it should have the same
28745 effect for real uses. Including all template parameters means that we
28746 deduce all parameters of A when resolving the call, so when we're in the
28747 constraint we don't need to deduce them again, we can just check whether
28748 the deduction produced the desired result. */
28749
28750 tsubst_flags_t complain = tf_warning_or_error;
28751 tree atype = TREE_TYPE (tmpl);
28752 tree aguides = NULL_TREE;
28753 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28754 unsigned natparms = TREE_VEC_LENGTH (atparms);
28755 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28756 for (ovl_iterator iter (uguides); iter; ++iter)
28757 {
28758 tree f = *iter;
28759 tree in_decl = f;
28760 location_t loc = DECL_SOURCE_LOCATION (f);
28761 tree ret = TREE_TYPE (TREE_TYPE (f));
28762 tree fprime = f;
28763 if (TREE_CODE (f) == TEMPLATE_DECL)
28764 {
28765 processing_template_decl_sentinel ptds (/*reset*/false);
28766 ++processing_template_decl;
28767
28768 /* Deduce template arguments for f from the type-id of A. */
28769 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28770 unsigned len = TREE_VEC_LENGTH (ftparms);
28771 tree targs = make_tree_vec (len);
28772 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28773 gcc_assert (!err);
28774
28775 /* The number of parms for f' is the number of parms for A plus
28776 non-deduced parms of f. */
28777 unsigned ndlen = 0;
28778 unsigned j;
28779 for (unsigned i = 0; i < len; ++i)
28780 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28781 ++ndlen;
28782 tree gtparms = make_tree_vec (natparms + ndlen);
28783
28784 /* First copy over the parms of A. */
28785 for (j = 0; j < natparms; ++j)
28786 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28787 /* Now rewrite the non-deduced parms of f. */
28788 for (unsigned i = 0; ndlen && i < len; ++i)
28789 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28790 {
28791 --ndlen;
28792 unsigned index = j++;
28793 unsigned level = 1;
28794 tree oldlist = TREE_VEC_ELT (ftparms, i);
28795 tree list = rewrite_tparm_list (oldlist, index, level,
28796 targs, i, complain);
28797 TREE_VEC_ELT (gtparms, index) = list;
28798 }
28799 gtparms = build_tree_list (size_one_node, gtparms);
28800
28801 /* Substitute the deduced arguments plus the rewritten template
28802 parameters into f to get g. This covers the type, copyness,
28803 guideness, and explicit-specifier. */
28804 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28805 if (g == error_mark_node)
28806 return error_mark_node;
28807 DECL_USE_TEMPLATE (g) = 0;
28808 fprime = build_template_decl (g, gtparms, false);
28809 DECL_TEMPLATE_RESULT (fprime) = g;
28810 TREE_TYPE (fprime) = TREE_TYPE (g);
28811 tree gtargs = template_parms_to_args (gtparms);
28812 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28813 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28814
28815 /* Substitute the associated constraints. */
28816 tree ci = get_constraints (f);
28817 if (ci)
28818 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28819 if (ci == error_mark_node)
28820 return error_mark_node;
28821
28822 /* Add a constraint that the return type matches the instantiation of
28823 A with the same template arguments. */
28824 ret = TREE_TYPE (TREE_TYPE (fprime));
28825 if (!same_type_p (atype, ret)
28826 /* FIXME this should mean they don't compare as equivalent. */
28827 || dependent_alias_template_spec_p (atype, nt_opaque))
28828 {
28829 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28830 ci = append_constraint (ci, same);
28831 }
28832
28833 if (ci)
28834 {
28835 remove_constraints (fprime);
28836 set_constraints (fprime, ci);
28837 }
28838 }
28839 else
28840 {
28841 /* For a non-template deduction guide, if the arguments of A aren't
28842 deducible from the return type, don't add the candidate. */
28843 tree targs = make_tree_vec (natparms);
28844 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28845 for (unsigned i = 0; !err && i < natparms; ++i)
28846 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28847 err = true;
28848 if (err)
28849 continue;
28850 }
28851
28852 aguides = lookup_add (fprime, aguides);
28853 }
28854
28855 return aguides;
28856 }
28857
28858 /* Return artificial deduction guides built from the constructors of class
28859 template TMPL. */
28860
28861 static tree
28862 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28863 {
28864 tree type = TREE_TYPE (tmpl);
28865 tree outer_args = NULL_TREE;
28866 if (DECL_CLASS_SCOPE_P (tmpl)
28867 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28868 {
28869 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28870 type = TREE_TYPE (most_general_template (tmpl));
28871 }
28872
28873 tree cands = NULL_TREE;
28874
28875 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28876 {
28877 /* Skip inherited constructors. */
28878 if (iter.using_p ())
28879 continue;
28880
28881 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28882 cands = lookup_add (guide, cands);
28883 }
28884
28885 /* Add implicit default constructor deduction guide. */
28886 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28887 {
28888 tree guide = build_deduction_guide (type, type, outer_args,
28889 complain);
28890 cands = lookup_add (guide, cands);
28891 }
28892
28893 /* Add copy guide. */
28894 {
28895 tree gtype = build_reference_type (type);
28896 tree guide = build_deduction_guide (type, gtype, outer_args,
28897 complain);
28898 cands = lookup_add (guide, cands);
28899 }
28900
28901 return cands;
28902 }
28903
28904 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28905
28906 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28907 aggregate candidate is added separately because it depends on the
28908 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
28909 guide. */
28910
28911 static tree
28912 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
28913 {
28914 tree guides = NULL_TREE;
28915 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28916 {
28917 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28918 tree tinfo = get_template_info (under);
28919 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
28920 complain);
28921 }
28922 else
28923 {
28924 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28925 dguide_name (tmpl),
28926 LOOK_want::NORMAL, /*complain*/false);
28927 if (guides == error_mark_node)
28928 guides = NULL_TREE;
28929 else
28930 any_dguides_p = true;
28931 }
28932
28933 /* Cache the deduction guides for a template. We also remember the result of
28934 lookup, and rebuild everything if it changes; should be very rare. */
28935 tree_pair_p cache = NULL;
28936 if (tree_pair_p &r
28937 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28938 {
28939 cache = r;
28940 if (cache->purpose == guides)
28941 return cache->value;
28942 }
28943 else
28944 {
28945 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28946 cache->purpose = guides;
28947 }
28948
28949 tree cands = NULL_TREE;
28950 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28951 cands = alias_ctad_tweaks (tmpl, guides);
28952 else
28953 {
28954 cands = ctor_deduction_guides_for (tmpl, complain);
28955 for (ovl_iterator it (guides); it; ++it)
28956 cands = lookup_add (*it, cands);
28957 }
28958
28959 cache->value = cands;
28960 return cands;
28961 }
28962
28963 /* Return whether TMPL is a (class template argument-) deducible template. */
28964
28965 bool
28966 ctad_template_p (tree tmpl)
28967 {
28968 /* A deducible template is either a class template or is an alias template
28969 whose defining-type-id is of the form
28970
28971 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28972
28973 where the nested-name-specifier (if any) is non-dependent and the
28974 template-name of the simple-template-id names a deducible template. */
28975
28976 if (DECL_CLASS_TEMPLATE_P (tmpl)
28977 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28978 return true;
28979 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28980 return false;
28981 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28982 if (tree tinfo = get_template_info (orig))
28983 return ctad_template_p (TI_TEMPLATE (tinfo));
28984 return false;
28985 }
28986
28987 /* Deduce template arguments for the class template placeholder PTYPE for
28988 template TMPL based on the initializer INIT, and return the resulting
28989 type. */
28990
28991 static tree
28992 do_class_deduction (tree ptype, tree tmpl, tree init,
28993 int flags, tsubst_flags_t complain)
28994 {
28995 /* We should have handled this in the caller. */
28996 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28997 return ptype;
28998
28999 /* Look through alias templates that just rename another template. */
29000 tmpl = get_underlying_template (tmpl);
29001 if (!ctad_template_p (tmpl))
29002 {
29003 if (complain & tf_error)
29004 error ("non-deducible template %qT used without template arguments", tmpl);
29005 return error_mark_node;
29006 }
29007 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
29008 {
29009 /* This doesn't affect conforming C++17 code, so just pedwarn. */
29010 if (complain & tf_warning_or_error)
29011 pedwarn (input_location, 0, "alias template deduction only available "
29012 "with %<-std=c++20%> or %<-std=gnu++20%>");
29013 }
29014
29015 if (init && TREE_TYPE (init) == ptype)
29016 /* Using the template parm as its own argument. */
29017 return ptype;
29018
29019 tree type = TREE_TYPE (tmpl);
29020
29021 bool try_list_ctor = false;
29022 bool list_init_p = false;
29023
29024 releasing_vec rv_args = NULL;
29025 vec<tree,va_gc> *&args = *&rv_args;
29026 if (init == NULL_TREE)
29027 args = make_tree_vector ();
29028 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
29029 {
29030 list_init_p = true;
29031 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
29032 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
29033 {
29034 /* As an exception, the first phase in 16.3.1.7 (considering the
29035 initializer list as a single argument) is omitted if the
29036 initializer list consists of a single expression of type cv U,
29037 where U is a specialization of C or a class derived from a
29038 specialization of C. */
29039 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
29040 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
29041 try_list_ctor = false;
29042 }
29043 if (try_list_ctor || is_std_init_list (type))
29044 args = make_tree_vector_single (init);
29045 else
29046 args = make_tree_vector_from_ctor (init);
29047 }
29048 else if (TREE_CODE (init) == TREE_LIST)
29049 args = make_tree_vector_from_list (init);
29050 else
29051 args = make_tree_vector_single (init);
29052
29053 /* Do this now to avoid problems with erroneous args later on. */
29054 args = resolve_args (args, complain);
29055 if (args == NULL)
29056 return error_mark_node;
29057
29058 bool any_dguides_p = false;
29059 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
29060 if (cands == error_mark_node)
29061 return error_mark_node;
29062
29063 /* Prune explicit deduction guides in copy-initialization context (but
29064 not copy-list-initialization). */
29065 bool elided = false;
29066 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
29067 {
29068 for (lkp_iterator iter (cands); !elided && iter; ++iter)
29069 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29070 elided = true;
29071
29072 if (elided)
29073 {
29074 /* Found a nonconverting guide, prune the candidates. */
29075 tree pruned = NULL_TREE;
29076 for (lkp_iterator iter (cands); iter; ++iter)
29077 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29078 pruned = lookup_add (*iter, pruned);
29079
29080 cands = pruned;
29081 }
29082 }
29083
29084 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29085 cands = lookup_add (guide, cands);
29086
29087 tree call = error_mark_node;
29088
29089 /* If this is list-initialization and the class has a list constructor, first
29090 try deducing from the list as a single argument, as [over.match.list]. */
29091 tree list_cands = NULL_TREE;
29092 if (try_list_ctor && cands)
29093 for (lkp_iterator iter (cands); iter; ++iter)
29094 {
29095 tree dg = *iter;
29096 if (is_list_ctor (dg))
29097 list_cands = lookup_add (dg, list_cands);
29098 }
29099 if (list_cands)
29100 {
29101 ++cp_unevaluated_operand;
29102 call = build_new_function_call (list_cands, &args, tf_decltype);
29103 --cp_unevaluated_operand;
29104
29105 if (call == error_mark_node)
29106 {
29107 /* That didn't work, now try treating the list as a sequence of
29108 arguments. */
29109 release_tree_vector (args);
29110 args = make_tree_vector_from_ctor (init);
29111 }
29112 }
29113
29114 if (elided && !cands)
29115 {
29116 error ("cannot deduce template arguments for copy-initialization"
29117 " of %qT, as it has no non-explicit deduction guides or "
29118 "user-declared constructors", type);
29119 return error_mark_node;
29120 }
29121 else if (!cands && call == error_mark_node)
29122 {
29123 error ("cannot deduce template arguments of %qT, as it has no viable "
29124 "deduction guides", type);
29125 return error_mark_node;
29126 }
29127
29128 if (call == error_mark_node)
29129 {
29130 ++cp_unevaluated_operand;
29131 call = build_new_function_call (cands, &args, tf_decltype);
29132 --cp_unevaluated_operand;
29133 }
29134
29135 if (call == error_mark_node)
29136 {
29137 if (complain & tf_warning_or_error)
29138 {
29139 error ("class template argument deduction failed:");
29140
29141 ++cp_unevaluated_operand;
29142 call = build_new_function_call (cands, &args,
29143 complain | tf_decltype);
29144 --cp_unevaluated_operand;
29145
29146 if (elided)
29147 inform (input_location, "explicit deduction guides not considered "
29148 "for copy-initialization");
29149 }
29150 return error_mark_node;
29151 }
29152 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29153 constructor is chosen, the initialization is ill-formed. */
29154 else if (flags & LOOKUP_ONLYCONVERTING)
29155 {
29156 tree fndecl = cp_get_callee_fndecl_nofold (call);
29157 if (fndecl && DECL_NONCONVERTING_P (fndecl))
29158 {
29159 if (complain & tf_warning_or_error)
29160 {
29161 // TODO: Pass down location from cp_finish_decl.
29162 error ("class template argument deduction for %qT failed: "
29163 "explicit deduction guide selected in "
29164 "copy-list-initialization", type);
29165 inform (DECL_SOURCE_LOCATION (fndecl),
29166 "explicit deduction guide declared here");
29167
29168 }
29169 return error_mark_node;
29170 }
29171 }
29172
29173 /* If CTAD succeeded but the type doesn't have any explicit deduction
29174 guides, this deduction might not be what the user intended. */
29175 if (call != error_mark_node && !any_dguides_p)
29176 {
29177 tree fndecl = cp_get_callee_fndecl_nofold (call);
29178 if (fndecl != NULL_TREE
29179 && (!DECL_IN_SYSTEM_HEADER (fndecl)
29180 || global_dc->dc_warn_system_headers)
29181 && warning (OPT_Wctad_maybe_unsupported,
29182 "%qT may not intend to support class template argument "
29183 "deduction", type))
29184 inform (input_location, "add a deduction guide to suppress this "
29185 "warning");
29186 }
29187
29188 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
29189 }
29190
29191 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29192 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29193 The CONTEXT determines the context in which auto deduction is performed
29194 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29195 OUTER_TARGS are used during template argument deduction
29196 (context == adc_unify) to properly substitute the result, and is ignored
29197 in other contexts.
29198
29199 For partial-concept-ids, extra args may be appended to the list of deduced
29200 template arguments prior to determining constraint satisfaction. */
29201
29202 tree
29203 do_auto_deduction (tree type, tree init, tree auto_node,
29204 tsubst_flags_t complain, auto_deduction_context context,
29205 tree outer_targs, int flags)
29206 {
29207 tree targs;
29208
29209 if (init == error_mark_node)
29210 return error_mark_node;
29211
29212 if (init && type_dependent_expression_p (init)
29213 && context != adc_unify)
29214 /* Defining a subset of type-dependent expressions that we can deduce
29215 from ahead of time isn't worth the trouble. */
29216 return type;
29217
29218 /* Similarly, we can't deduce from another undeduced decl. */
29219 if (init && undeduced_auto_decl (init))
29220 return type;
29221
29222 /* We may be doing a partial substitution, but we still want to replace
29223 auto_node. */
29224 complain &= ~tf_partial;
29225
29226 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29227 /* C++17 class template argument deduction. */
29228 return do_class_deduction (type, tmpl, init, flags, complain);
29229
29230 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29231 /* Nothing we can do with this, even in deduction context. */
29232 return type;
29233
29234 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29235 with either a new invented type template parameter U or, if the
29236 initializer is a braced-init-list (8.5.4), with
29237 std::initializer_list<U>. */
29238 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29239 {
29240 if (!DIRECT_LIST_INIT_P (init))
29241 type = listify_autos (type, auto_node);
29242 else if (CONSTRUCTOR_NELTS (init) == 1)
29243 init = CONSTRUCTOR_ELT (init, 0)->value;
29244 else
29245 {
29246 if (complain & tf_warning_or_error)
29247 {
29248 if (permerror (input_location, "direct-list-initialization of "
29249 "%<auto%> requires exactly one element"))
29250 inform (input_location,
29251 "for deduction to %<std::initializer_list%>, use copy-"
29252 "list-initialization (i.e. add %<=%> before the %<{%>)");
29253 }
29254 type = listify_autos (type, auto_node);
29255 }
29256 }
29257
29258 if (type == error_mark_node)
29259 return error_mark_node;
29260
29261 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29262 /* We don't recurse here because we can't deduce from a nested
29263 initializer_list. */
29264 for (constructor_elt &elt : *CONSTRUCTOR_ELTS (init))
29265 elt.value = resolve_nondeduced_context (elt.value, complain);
29266 else
29267 init = resolve_nondeduced_context (init, complain);
29268
29269 if (context == adc_decomp_type
29270 && auto_node == type
29271 && init != error_mark_node
29272 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29273 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
29274 and initializer has array type, deduce cv-qualified array type. */
29275 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
29276 complain);
29277 else if (AUTO_IS_DECLTYPE (auto_node))
29278 {
29279 tree stripped_init = tree_strip_any_location_wrapper (init);
29280 if (REFERENCE_REF_P (stripped_init))
29281 stripped_init = TREE_OPERAND (stripped_init, 0);
29282 bool id = (DECL_P (stripped_init)
29283 || ((TREE_CODE (init) == COMPONENT_REF
29284 || TREE_CODE (init) == SCOPE_REF)
29285 && !REF_PARENTHESIZED_P (init)));
29286 targs = make_tree_vec (1);
29287 TREE_VEC_ELT (targs, 0)
29288 = finish_decltype_type (init, id, tf_warning_or_error);
29289 if (type != auto_node)
29290 {
29291 if (complain & tf_error)
29292 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29293 return error_mark_node;
29294 }
29295 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29296 {
29297 if (complain & tf_error)
29298 error ("%<decltype(auto)%> cannot be cv-qualified");
29299 return error_mark_node;
29300 }
29301 }
29302 else
29303 {
29304 if (error_operand_p (init))
29305 return error_mark_node;
29306
29307 tree parms = build_tree_list (NULL_TREE, type);
29308 tree tparms;
29309
29310 if (flag_concepts)
29311 tparms = extract_autos (type);
29312 else
29313 {
29314 tparms = make_tree_vec (1);
29315 TREE_VEC_ELT (tparms, 0)
29316 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29317 }
29318
29319 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29320 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29321 DEDUCE_CALL,
29322 NULL, /*explain_p=*/false);
29323 if (val > 0)
29324 {
29325 if (processing_template_decl)
29326 /* Try again at instantiation time. */
29327 return type;
29328 if (type && type != error_mark_node
29329 && (complain & tf_error))
29330 /* If type is error_mark_node a diagnostic must have been
29331 emitted by now. Also, having a mention to '<type error>'
29332 in the diagnostic is not really useful to the user. */
29333 {
29334 if (cfun
29335 && FNDECL_USED_AUTO (current_function_decl)
29336 && (auto_node
29337 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29338 && LAMBDA_FUNCTION_P (current_function_decl))
29339 error ("unable to deduce lambda return type from %qE", init);
29340 else
29341 error ("unable to deduce %qT from %qE", type, init);
29342 type_unification_real (tparms, targs, parms, &init, 1, 0,
29343 DEDUCE_CALL,
29344 NULL, /*explain_p=*/true);
29345 }
29346 return error_mark_node;
29347 }
29348 }
29349
29350 /* Check any placeholder constraints against the deduced type. */
29351 if (flag_concepts && !processing_template_decl)
29352 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29353 {
29354 /* Use the deduced type to check the associated constraints. If we
29355 have a partial-concept-id, rebuild the argument list so that
29356 we check using the extra arguments. */
29357 check = unpack_concept_check (check);
29358 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29359 tree cdecl = TREE_OPERAND (check, 0);
29360 if (OVL_P (cdecl))
29361 cdecl = OVL_FIRST (cdecl);
29362 tree cargs = TREE_OPERAND (check, 1);
29363 if (TREE_VEC_LENGTH (cargs) > 1)
29364 {
29365 cargs = copy_node (cargs);
29366 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29367 }
29368 else
29369 cargs = targs;
29370
29371 /* Rebuild the check using the deduced arguments. */
29372 check = build_concept_check (cdecl, cargs, tf_none);
29373
29374 if (!constraints_satisfied_p (check))
29375 {
29376 if (complain & tf_warning_or_error)
29377 {
29378 auto_diagnostic_group d;
29379 switch (context)
29380 {
29381 case adc_unspecified:
29382 case adc_unify:
29383 error("placeholder constraints not satisfied");
29384 break;
29385 case adc_variable_type:
29386 case adc_decomp_type:
29387 error ("deduced initializer does not satisfy "
29388 "placeholder constraints");
29389 break;
29390 case adc_return_type:
29391 error ("deduced return type does not satisfy "
29392 "placeholder constraints");
29393 break;
29394 case adc_requirement:
29395 error ("deduced expression type does not satisfy "
29396 "placeholder constraints");
29397 break;
29398 }
29399 diagnose_constraints (input_location, check, targs);
29400 }
29401 return error_mark_node;
29402 }
29403 }
29404
29405 if (processing_template_decl && context != adc_unify)
29406 outer_targs = current_template_args ();
29407 targs = add_to_template_args (outer_targs, targs);
29408 return tsubst (type, targs, complain, NULL_TREE);
29409 }
29410
29411 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29412 result. */
29413
29414 tree
29415 splice_late_return_type (tree type, tree late_return_type)
29416 {
29417 if (late_return_type)
29418 {
29419 gcc_assert (is_auto (type) || seen_error ());
29420 return late_return_type;
29421 }
29422
29423 if (tree *auto_node = find_type_usage (&type, is_auto))
29424 {
29425 tree idx = get_template_parm_index (*auto_node);
29426 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29427 {
29428 /* In an abbreviated function template we didn't know we were dealing
29429 with a function template when we saw the auto return type, so update
29430 it to have the correct level. */
29431 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29432 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29433 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29434 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29435 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29436 *auto_node = new_auto;
29437 }
29438 }
29439 return type;
29440 }
29441
29442 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29443 'decltype(auto)' or a deduced class template. */
29444
29445 bool
29446 is_auto (const_tree type)
29447 {
29448 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29449 && (TYPE_IDENTIFIER (type) == auto_identifier
29450 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29451 return true;
29452 else
29453 return false;
29454 }
29455
29456 /* for_each_template_parm callback for type_uses_auto. */
29457
29458 int
29459 is_auto_r (tree tp, void */*data*/)
29460 {
29461 return is_auto (tp);
29462 }
29463
29464 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29465 a use of `auto'. Returns NULL_TREE otherwise. */
29466
29467 tree
29468 type_uses_auto (tree type)
29469 {
29470 if (type == NULL_TREE)
29471 return NULL_TREE;
29472 else if (flag_concepts)
29473 {
29474 /* The Concepts TS allows multiple autos in one type-specifier; just
29475 return the first one we find, do_auto_deduction will collect all of
29476 them. */
29477 if (uses_template_parms (type))
29478 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29479 /*visited*/NULL, /*nondeduced*/false);
29480 else
29481 return NULL_TREE;
29482 }
29483 else if (tree *tp = find_type_usage (&type, is_auto))
29484 return *tp;
29485 else
29486 return NULL_TREE;
29487 }
29488
29489 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29490 concepts are enabled, auto is acceptable in template arguments, but
29491 only when TEMPL identifies a template class. Return TRUE if any
29492 such errors were reported. */
29493
29494 bool
29495 check_auto_in_tmpl_args (tree tmpl, tree args)
29496 {
29497 /* If there were previous errors, nevermind. */
29498 if (!args || TREE_CODE (args) != TREE_VEC)
29499 return false;
29500
29501 /* If TMPL is an identifier, we're parsing and we can't tell yet
29502 whether TMPL is supposed to be a type, a function or a variable.
29503 We'll only be able to tell during template substitution, so we
29504 expect to be called again then. If concepts are enabled and we
29505 know we have a type, we're ok. */
29506 if (flag_concepts
29507 && (identifier_p (tmpl)
29508 || (DECL_P (tmpl)
29509 && (DECL_TYPE_TEMPLATE_P (tmpl)
29510 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29511 return false;
29512
29513 /* Quickly search for any occurrences of auto; usually there won't
29514 be any, and then we'll avoid allocating the vector. */
29515 if (!type_uses_auto (args))
29516 return false;
29517
29518 bool errors = false;
29519
29520 tree vec = extract_autos (args);
29521 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29522 {
29523 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29524 error_at (DECL_SOURCE_LOCATION (xauto),
29525 "invalid use of %qT in template argument", xauto);
29526 errors = true;
29527 }
29528
29529 return errors;
29530 }
29531
29532 /* Recursively walk over && expressions searching for EXPR. Return a reference
29533 to that expression. */
29534
29535 static tree *find_template_requirement (tree *t, tree key)
29536 {
29537 if (*t == key)
29538 return t;
29539 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29540 {
29541 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29542 return p;
29543 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29544 return p;
29545 }
29546 return 0;
29547 }
29548
29549 /* Convert the generic type parameters in PARM that match the types given in the
29550 range [START_IDX, END_IDX) from the current_template_parms into generic type
29551 packs. */
29552
29553 tree
29554 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29555 {
29556 tree current = current_template_parms;
29557 int depth = TMPL_PARMS_DEPTH (current);
29558 current = INNERMOST_TEMPLATE_PARMS (current);
29559 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29560
29561 for (int i = 0; i < start_idx; ++i)
29562 TREE_VEC_ELT (replacement, i)
29563 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29564
29565 for (int i = start_idx; i < end_idx; ++i)
29566 {
29567 /* Create a distinct parameter pack type from the current parm and add it
29568 to the replacement args to tsubst below into the generic function
29569 parameter. */
29570 tree node = TREE_VEC_ELT (current, i);
29571 tree o = TREE_TYPE (TREE_VALUE (node));
29572 tree t = copy_type (o);
29573 TEMPLATE_TYPE_PARM_INDEX (t)
29574 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29575 t, 0, 0, tf_none);
29576 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29577 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29578 TYPE_MAIN_VARIANT (t) = t;
29579 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29580 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29581 TREE_VEC_ELT (replacement, i) = t;
29582
29583 /* Replace the current template parameter with new pack. */
29584 TREE_VALUE (node) = TREE_CHAIN (t);
29585
29586 /* Surgically adjust the associated constraint of adjusted parameter
29587 and it's corresponding contribution to the current template
29588 requirements. */
29589 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29590 {
29591 tree id = unpack_concept_check (constr);
29592 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
29593 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29594 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29595
29596 /* If there was a constraint, we also need to replace that in
29597 the template requirements, which we've already built. */
29598 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29599 reqs = find_template_requirement (reqs, constr);
29600 *reqs = fold;
29601 }
29602 }
29603
29604 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29605 TREE_VEC_ELT (replacement, i)
29606 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29607
29608 /* If there are more levels then build up the replacement with the outer
29609 template parms. */
29610 if (depth > 1)
29611 replacement = add_to_template_args (template_parms_to_args
29612 (TREE_CHAIN (current_template_parms)),
29613 replacement);
29614
29615 return tsubst (parm, replacement, tf_none, NULL_TREE);
29616 }
29617
29618 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29619 0..N-1. */
29620
29621 void
29622 declare_integer_pack (void)
29623 {
29624 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29625 build_function_type_list (integer_type_node,
29626 integer_type_node,
29627 NULL_TREE),
29628 NULL_TREE, ECF_CONST);
29629 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29630 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29631 CP_BUILT_IN_INTEGER_PACK);
29632 }
29633
29634 /* Set up the hash tables for template instantiations. */
29635
29636 void
29637 init_template_processing (void)
29638 {
29639 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
29640 type_specializations = hash_table<spec_hasher>::create_ggc (37);
29641
29642 if (cxx_dialect >= cxx11)
29643 declare_integer_pack ();
29644 }
29645
29646 /* Print stats about the template hash tables for -fstats. */
29647
29648 void
29649 print_template_statistics (void)
29650 {
29651 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29652 "%f collisions\n", (long) decl_specializations->size (),
29653 (long) decl_specializations->elements (),
29654 decl_specializations->collisions ());
29655 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29656 "%f collisions\n", (long) type_specializations->size (),
29657 (long) type_specializations->elements (),
29658 type_specializations->collisions ());
29659 }
29660
29661 #if CHECKING_P
29662
29663 namespace selftest {
29664
29665 /* Verify that build_non_dependent_expr () works, for various expressions,
29666 and that location wrappers don't affect the results. */
29667
29668 static void
29669 test_build_non_dependent_expr ()
29670 {
29671 location_t loc = BUILTINS_LOCATION;
29672
29673 /* Verify constants, without and with location wrappers. */
29674 tree int_cst = build_int_cst (integer_type_node, 42);
29675 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29676
29677 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29678 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29679 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29680
29681 tree string_lit = build_string (4, "foo");
29682 TREE_TYPE (string_lit) = char_array_type_node;
29683 string_lit = fix_string_type (string_lit);
29684 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29685
29686 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29687 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29688 ASSERT_EQ (wrapped_string_lit,
29689 build_non_dependent_expr (wrapped_string_lit));
29690 }
29691
29692 /* Verify that type_dependent_expression_p () works correctly, even
29693 in the presence of location wrapper nodes. */
29694
29695 static void
29696 test_type_dependent_expression_p ()
29697 {
29698 location_t loc = BUILTINS_LOCATION;
29699
29700 tree name = get_identifier ("foo");
29701
29702 /* If no templates are involved, nothing is type-dependent. */
29703 gcc_assert (!processing_template_decl);
29704 ASSERT_FALSE (type_dependent_expression_p (name));
29705
29706 ++processing_template_decl;
29707
29708 /* Within a template, an unresolved name is always type-dependent. */
29709 ASSERT_TRUE (type_dependent_expression_p (name));
29710
29711 /* Ensure it copes with NULL_TREE and errors. */
29712 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29713 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29714
29715 /* A USING_DECL in a template should be type-dependent, even if wrapped
29716 with a location wrapper (PR c++/83799). */
29717 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29718 TREE_TYPE (using_decl) = integer_type_node;
29719 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29720 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29721 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29722 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29723
29724 --processing_template_decl;
29725 }
29726
29727 /* Run all of the selftests within this file. */
29728
29729 void
29730 cp_pt_c_tests ()
29731 {
29732 test_build_non_dependent_expr ();
29733 test_type_dependent_expression_p ();
29734 }
29735
29736 } // namespace selftest
29737
29738 #endif /* #if CHECKING_P */
29739
29740 #include "gt-cp-pt.h"