]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
init.c (build_new): Add location_t parameter and use it throughout.
[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 TREE_LIST of templates whose
52 instantiations have been deferred, either because their definitions
53 were not yet 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_blank || !saved)
87 local_specializations = new hash_map<tree, tree>;
88 else
89 local_specializations = new hash_map<tree, tree>(*saved);
90 }
91
92 local_specialization_stack::~local_specialization_stack ()
93 {
94 delete local_specializations;
95 local_specializations = saved;
96 }
97
98 /* True if we've recursed into fn_type_unification too many times. */
99 static bool excessive_deduction_depth;
100
101 struct GTY((for_user)) spec_entry
102 {
103 tree tmpl;
104 tree args;
105 tree spec;
106 };
107
108 struct spec_hasher : ggc_ptr_hash<spec_entry>
109 {
110 static hashval_t hash (spec_entry *);
111 static bool equal (spec_entry *, spec_entry *);
112 };
113
114 static GTY (()) hash_table<spec_hasher> *decl_specializations;
115
116 static GTY (()) hash_table<spec_hasher> *type_specializations;
117
118 /* Contains canonical template parameter types. The vector is indexed by
119 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
120 TREE_LIST, whose TREE_VALUEs contain the canonical template
121 parameters of various types and levels. */
122 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
123
124 #define UNIFY_ALLOW_NONE 0
125 #define UNIFY_ALLOW_MORE_CV_QUAL 1
126 #define UNIFY_ALLOW_LESS_CV_QUAL 2
127 #define UNIFY_ALLOW_DERIVED 4
128 #define UNIFY_ALLOW_INTEGER 8
129 #define UNIFY_ALLOW_OUTER_LEVEL 16
130 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
131 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
132
133 enum template_base_result {
134 tbr_incomplete_type,
135 tbr_ambiguous_baseclass,
136 tbr_success
137 };
138
139 static bool resolve_overloaded_unification (tree, tree, tree, tree,
140 unification_kind_t, int,
141 bool);
142 static int try_one_overload (tree, tree, tree, tree, tree,
143 unification_kind_t, int, bool, bool);
144 static int unify (tree, tree, tree, tree, int, bool);
145 static void add_pending_template (tree);
146 static tree reopen_tinst_level (struct tinst_level *);
147 static tree tsubst_initializer_list (tree, tree);
148 static tree get_partial_spec_bindings (tree, tree, tree);
149 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static void tsubst_enum (tree, tree, tree);
154 static tree add_to_template_args (tree, tree);
155 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
156 static int check_non_deducible_conversion (tree, tree, int, int,
157 struct conversion **, bool);
158 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
159 tree);
160 static int type_unification_real (tree, tree, tree, const tree *,
161 unsigned int, int, unification_kind_t,
162 vec<deferred_access_check, va_gc> **,
163 bool);
164 static void note_template_header (int);
165 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
166 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
167 static tree convert_template_argument (tree, tree, tree,
168 tsubst_flags_t, int, tree);
169 static tree for_each_template_parm (tree, tree_fn_t, void*,
170 hash_set<tree> *, bool, tree_fn_t = NULL);
171 static tree expand_template_argument_pack (tree);
172 static tree build_template_parm_index (int, int, int, tree, tree);
173 static bool inline_needs_template_parms (tree, bool);
174 static void push_inline_template_parms_recursive (tree, int);
175 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
176 static int mark_template_parm (tree, void *);
177 static int template_parm_this_level_p (tree, void *);
178 static tree tsubst_friend_function (tree, tree);
179 static tree tsubst_friend_class (tree, tree);
180 static int can_complete_type_without_circularity (tree);
181 static tree get_bindings (tree, tree, tree, bool);
182 static int template_decl_level (tree);
183 static int check_cv_quals_for_unify (int, tree, tree);
184 static int unify_pack_expansion (tree, tree, tree,
185 tree, unification_kind_t, bool, bool);
186 static tree copy_template_args (tree);
187 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
188 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
189 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
190 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
192 static bool check_specialization_scope (void);
193 static tree process_partial_specialization (tree);
194 static void set_current_access_from_decl (tree);
195 static enum template_base_result get_template_base (tree, tree, tree, tree,
196 bool , tree *);
197 static tree try_class_unification (tree, tree, tree, tree, bool);
198 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
199 tree, tree);
200 static bool template_template_parm_bindings_ok_p (tree, tree);
201 static void tsubst_default_arguments (tree, tsubst_flags_t);
202 static tree for_each_template_parm_r (tree *, int *, void *);
203 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
204 static void copy_default_args_to_explicit_spec (tree);
205 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
206 static bool dependent_template_arg_p (tree);
207 static bool any_template_arguments_need_structural_equality_p (tree);
208 static bool dependent_type_p_r (tree);
209 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
210 static tree tsubst_decl (tree, tree, tsubst_flags_t);
211 static void perform_typedefs_access_check (tree tmpl, tree targs);
212 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
213 location_t);
214 static tree listify (tree);
215 static tree listify_autos (tree, tree);
216 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
217 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
218 static bool complex_alias_template_p (const_tree tmpl);
219 static tree get_underlying_template (tree);
220 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
221 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
222 static tree make_argument_pack (tree);
223 static void register_parameter_specializations (tree, tree);
224 static tree enclosing_instantiation_of (tree tctx);
225
226 /* Make the current scope suitable for access checking when we are
227 processing T. T can be FUNCTION_DECL for instantiated function
228 template, VAR_DECL for static member variable, or TYPE_DECL for
229 alias template (needed by instantiate_decl). */
230
231 void
232 push_access_scope (tree t)
233 {
234 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
235 || TREE_CODE (t) == TYPE_DECL);
236
237 if (DECL_FRIEND_CONTEXT (t))
238 push_nested_class (DECL_FRIEND_CONTEXT (t));
239 else if (DECL_CLASS_SCOPE_P (t))
240 push_nested_class (DECL_CONTEXT (t));
241 else
242 push_to_top_level ();
243
244 if (TREE_CODE (t) == FUNCTION_DECL)
245 {
246 vec_safe_push (saved_access_scope, current_function_decl);
247 current_function_decl = t;
248 }
249 }
250
251 /* Restore the scope set up by push_access_scope. T is the node we
252 are processing. */
253
254 void
255 pop_access_scope (tree t)
256 {
257 if (TREE_CODE (t) == FUNCTION_DECL)
258 current_function_decl = saved_access_scope->pop();
259
260 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
261 pop_nested_class ();
262 else
263 pop_from_top_level ();
264 }
265
266 /* Do any processing required when DECL (a member template
267 declaration) is finished. Returns the TEMPLATE_DECL corresponding
268 to DECL, unless it is a specialization, in which case the DECL
269 itself is returned. */
270
271 tree
272 finish_member_template_decl (tree decl)
273 {
274 if (decl == error_mark_node)
275 return error_mark_node;
276
277 gcc_assert (DECL_P (decl));
278
279 if (TREE_CODE (decl) == TYPE_DECL)
280 {
281 tree type;
282
283 type = TREE_TYPE (decl);
284 if (type == error_mark_node)
285 return error_mark_node;
286 if (MAYBE_CLASS_TYPE_P (type)
287 && CLASSTYPE_TEMPLATE_INFO (type)
288 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
289 {
290 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
291 check_member_template (tmpl);
292 return tmpl;
293 }
294 return NULL_TREE;
295 }
296 else if (TREE_CODE (decl) == FIELD_DECL)
297 error_at (DECL_SOURCE_LOCATION (decl),
298 "data member %qD cannot be a member template", decl);
299 else if (DECL_TEMPLATE_INFO (decl))
300 {
301 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
302 {
303 check_member_template (DECL_TI_TEMPLATE (decl));
304 return DECL_TI_TEMPLATE (decl);
305 }
306 else
307 return decl;
308 }
309 else
310 error_at (DECL_SOURCE_LOCATION (decl),
311 "invalid member template declaration %qD", decl);
312
313 return error_mark_node;
314 }
315
316 /* Create a template info node. */
317
318 tree
319 build_template_info (tree template_decl, tree template_args)
320 {
321 tree result = make_node (TEMPLATE_INFO);
322 TI_TEMPLATE (result) = template_decl;
323 TI_ARGS (result) = template_args;
324 return result;
325 }
326
327 /* Return the template info node corresponding to T, whatever T is. */
328
329 tree
330 get_template_info (const_tree t)
331 {
332 tree tinfo = NULL_TREE;
333
334 if (!t || t == error_mark_node)
335 return NULL;
336
337 if (TREE_CODE (t) == NAMESPACE_DECL
338 || TREE_CODE (t) == PARM_DECL)
339 return NULL;
340
341 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
342 tinfo = DECL_TEMPLATE_INFO (t);
343
344 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
345 t = TREE_TYPE (t);
346
347 if (OVERLOAD_TYPE_P (t))
348 tinfo = TYPE_TEMPLATE_INFO (t);
349 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
350 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
351
352 return tinfo;
353 }
354
355 /* Returns the template nesting level of the indicated class TYPE.
356
357 For example, in:
358 template <class T>
359 struct A
360 {
361 template <class U>
362 struct B {};
363 };
364
365 A<T>::B<U> has depth two, while A<T> has depth one.
366 Both A<T>::B<int> and A<int>::B<U> have depth one, if
367 they are instantiations, not specializations.
368
369 This function is guaranteed to return 0 if passed NULL_TREE so
370 that, for example, `template_class_depth (current_class_type)' is
371 always safe. */
372
373 int
374 template_class_depth (tree type)
375 {
376 int depth;
377
378 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
379 {
380 tree tinfo = get_template_info (type);
381
382 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
383 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
384 ++depth;
385
386 if (DECL_P (type))
387 type = CP_DECL_CONTEXT (type);
388 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
389 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
390 else
391 type = CP_TYPE_CONTEXT (type);
392 }
393
394 return depth;
395 }
396
397 /* Return TRUE if NODE instantiates a template that has arguments of
398 its own, be it directly a primary template or indirectly through a
399 partial specializations. */
400 static bool
401 instantiates_primary_template_p (tree node)
402 {
403 tree tinfo = get_template_info (node);
404 if (!tinfo)
405 return false;
406
407 tree tmpl = TI_TEMPLATE (tinfo);
408 if (PRIMARY_TEMPLATE_P (tmpl))
409 return true;
410
411 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
412 return false;
413
414 /* So now we know we have a specialization, but it could be a full
415 or a partial specialization. To tell which, compare the depth of
416 its template arguments with those of its context. */
417
418 tree ctxt = DECL_CONTEXT (tmpl);
419 tree ctinfo = get_template_info (ctxt);
420 if (!ctinfo)
421 return true;
422
423 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
424 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
425 }
426
427 /* Subroutine of maybe_begin_member_template_processing.
428 Returns true if processing DECL needs us to push template parms. */
429
430 static bool
431 inline_needs_template_parms (tree decl, bool nsdmi)
432 {
433 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
434 return false;
435
436 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
437 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
438 }
439
440 /* Subroutine of maybe_begin_member_template_processing.
441 Push the template parms in PARMS, starting from LEVELS steps into the
442 chain, and ending at the beginning, since template parms are listed
443 innermost first. */
444
445 static void
446 push_inline_template_parms_recursive (tree parmlist, int levels)
447 {
448 tree parms = TREE_VALUE (parmlist);
449 int i;
450
451 if (levels > 1)
452 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
453
454 ++processing_template_decl;
455 current_template_parms
456 = tree_cons (size_int (processing_template_decl),
457 parms, current_template_parms);
458 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
459
460 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
461 NULL);
462 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
463 {
464 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
465
466 if (error_operand_p (parm))
467 continue;
468
469 gcc_assert (DECL_P (parm));
470
471 switch (TREE_CODE (parm))
472 {
473 case TYPE_DECL:
474 case TEMPLATE_DECL:
475 pushdecl (parm);
476 break;
477
478 case PARM_DECL:
479 /* Push the CONST_DECL. */
480 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
481 break;
482
483 default:
484 gcc_unreachable ();
485 }
486 }
487 }
488
489 /* Restore the template parameter context for a member template, a
490 friend template defined in a class definition, or a non-template
491 member of template class. */
492
493 void
494 maybe_begin_member_template_processing (tree decl)
495 {
496 tree parms;
497 int levels = 0;
498 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
499
500 if (nsdmi)
501 {
502 tree ctx = DECL_CONTEXT (decl);
503 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
504 /* Disregard full specializations (c++/60999). */
505 && uses_template_parms (ctx)
506 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
507 }
508
509 if (inline_needs_template_parms (decl, nsdmi))
510 {
511 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
512 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
513
514 if (DECL_TEMPLATE_SPECIALIZATION (decl))
515 {
516 --levels;
517 parms = TREE_CHAIN (parms);
518 }
519
520 push_inline_template_parms_recursive (parms, levels);
521 }
522
523 /* Remember how many levels of template parameters we pushed so that
524 we can pop them later. */
525 inline_parm_levels.safe_push (levels);
526 }
527
528 /* Undo the effects of maybe_begin_member_template_processing. */
529
530 void
531 maybe_end_member_template_processing (void)
532 {
533 int i;
534 int last;
535
536 if (inline_parm_levels.length () == 0)
537 return;
538
539 last = inline_parm_levels.pop ();
540 for (i = 0; i < last; ++i)
541 {
542 --processing_template_decl;
543 current_template_parms = TREE_CHAIN (current_template_parms);
544 poplevel (0, 0, 0);
545 }
546 }
547
548 /* Return a new template argument vector which contains all of ARGS,
549 but has as its innermost set of arguments the EXTRA_ARGS. */
550
551 static tree
552 add_to_template_args (tree args, tree extra_args)
553 {
554 tree new_args;
555 int extra_depth;
556 int i;
557 int j;
558
559 if (args == NULL_TREE || extra_args == error_mark_node)
560 return extra_args;
561
562 extra_depth = TMPL_ARGS_DEPTH (extra_args);
563 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
564
565 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
566 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
567
568 for (j = 1; j <= extra_depth; ++j, ++i)
569 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
570
571 return new_args;
572 }
573
574 /* Like add_to_template_args, but only the outermost ARGS are added to
575 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
576 (EXTRA_ARGS) levels are added. This function is used to combine
577 the template arguments from a partial instantiation with the
578 template arguments used to attain the full instantiation from the
579 partial instantiation. */
580
581 tree
582 add_outermost_template_args (tree args, tree extra_args)
583 {
584 tree new_args;
585
586 /* If there are more levels of EXTRA_ARGS than there are ARGS,
587 something very fishy is going on. */
588 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
589
590 /* If *all* the new arguments will be the EXTRA_ARGS, just return
591 them. */
592 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
593 return extra_args;
594
595 /* For the moment, we make ARGS look like it contains fewer levels. */
596 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
597
598 new_args = add_to_template_args (args, extra_args);
599
600 /* Now, we restore ARGS to its full dimensions. */
601 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
602
603 return new_args;
604 }
605
606 /* Return the N levels of innermost template arguments from the ARGS. */
607
608 tree
609 get_innermost_template_args (tree args, int n)
610 {
611 tree new_args;
612 int extra_levels;
613 int i;
614
615 gcc_assert (n >= 0);
616
617 /* If N is 1, just return the innermost set of template arguments. */
618 if (n == 1)
619 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
620
621 /* If we're not removing anything, just return the arguments we were
622 given. */
623 extra_levels = TMPL_ARGS_DEPTH (args) - n;
624 gcc_assert (extra_levels >= 0);
625 if (extra_levels == 0)
626 return args;
627
628 /* Make a new set of arguments, not containing the outer arguments. */
629 new_args = make_tree_vec (n);
630 for (i = 1; i <= n; ++i)
631 SET_TMPL_ARGS_LEVEL (new_args, i,
632 TMPL_ARGS_LEVEL (args, i + extra_levels));
633
634 return new_args;
635 }
636
637 /* The inverse of get_innermost_template_args: Return all but the innermost
638 EXTRA_LEVELS levels of template arguments from the ARGS. */
639
640 static tree
641 strip_innermost_template_args (tree args, int extra_levels)
642 {
643 tree new_args;
644 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
645 int i;
646
647 gcc_assert (n >= 0);
648
649 /* If N is 1, just return the outermost set of template arguments. */
650 if (n == 1)
651 return TMPL_ARGS_LEVEL (args, 1);
652
653 /* If we're not removing anything, just return the arguments we were
654 given. */
655 gcc_assert (extra_levels >= 0);
656 if (extra_levels == 0)
657 return args;
658
659 /* Make a new set of arguments, not containing the inner arguments. */
660 new_args = make_tree_vec (n);
661 for (i = 1; i <= n; ++i)
662 SET_TMPL_ARGS_LEVEL (new_args, i,
663 TMPL_ARGS_LEVEL (args, i));
664
665 return new_args;
666 }
667
668 /* We've got a template header coming up; push to a new level for storing
669 the parms. */
670
671 void
672 begin_template_parm_list (void)
673 {
674 /* We use a non-tag-transparent scope here, which causes pushtag to
675 put tags in this scope, rather than in the enclosing class or
676 namespace scope. This is the right thing, since we want
677 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
678 global template class, push_template_decl handles putting the
679 TEMPLATE_DECL into top-level scope. For a nested template class,
680 e.g.:
681
682 template <class T> struct S1 {
683 template <class T> struct S2 {};
684 };
685
686 pushtag contains special code to insert the TEMPLATE_DECL for S2
687 at the right scope. */
688 begin_scope (sk_template_parms, NULL);
689 ++processing_template_decl;
690 ++processing_template_parmlist;
691 note_template_header (0);
692
693 /* Add a dummy parameter level while we process the parameter list. */
694 current_template_parms
695 = tree_cons (size_int (processing_template_decl),
696 make_tree_vec (0),
697 current_template_parms);
698 }
699
700 /* This routine is called when a specialization is declared. If it is
701 invalid to declare a specialization here, an error is reported and
702 false is returned, otherwise this routine will return true. */
703
704 static bool
705 check_specialization_scope (void)
706 {
707 tree scope = current_scope ();
708
709 /* [temp.expl.spec]
710
711 An explicit specialization shall be declared in the namespace of
712 which the template is a member, or, for member templates, in the
713 namespace of which the enclosing class or enclosing class
714 template is a member. An explicit specialization of a member
715 function, member class or static data member of a class template
716 shall be declared in the namespace of which the class template
717 is a member. */
718 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
719 {
720 error ("explicit specialization in non-namespace scope %qD", scope);
721 return false;
722 }
723
724 /* [temp.expl.spec]
725
726 In an explicit specialization declaration for a member of a class
727 template or a member template that appears in namespace scope,
728 the member template and some of its enclosing class templates may
729 remain unspecialized, except that the declaration shall not
730 explicitly specialize a class member template if its enclosing
731 class templates are not explicitly specialized as well. */
732 if (current_template_parms)
733 {
734 error ("enclosing class templates are not explicitly specialized");
735 return false;
736 }
737
738 return true;
739 }
740
741 /* We've just seen template <>. */
742
743 bool
744 begin_specialization (void)
745 {
746 begin_scope (sk_template_spec, NULL);
747 note_template_header (1);
748 return check_specialization_scope ();
749 }
750
751 /* Called at then end of processing a declaration preceded by
752 template<>. */
753
754 void
755 end_specialization (void)
756 {
757 finish_scope ();
758 reset_specialization ();
759 }
760
761 /* Any template <>'s that we have seen thus far are not referring to a
762 function specialization. */
763
764 void
765 reset_specialization (void)
766 {
767 processing_specialization = 0;
768 template_header_count = 0;
769 }
770
771 /* We've just seen a template header. If SPECIALIZATION is nonzero,
772 it was of the form template <>. */
773
774 static void
775 note_template_header (int specialization)
776 {
777 processing_specialization = specialization;
778 template_header_count++;
779 }
780
781 /* We're beginning an explicit instantiation. */
782
783 void
784 begin_explicit_instantiation (void)
785 {
786 gcc_assert (!processing_explicit_instantiation);
787 processing_explicit_instantiation = true;
788 }
789
790
791 void
792 end_explicit_instantiation (void)
793 {
794 gcc_assert (processing_explicit_instantiation);
795 processing_explicit_instantiation = false;
796 }
797
798 /* An explicit specialization or partial specialization of TMPL is being
799 declared. Check that the namespace in which the specialization is
800 occurring is permissible. Returns false iff it is invalid to
801 specialize TMPL in the current namespace. */
802
803 static bool
804 check_specialization_namespace (tree tmpl)
805 {
806 tree tpl_ns = decl_namespace_context (tmpl);
807
808 /* [tmpl.expl.spec]
809
810 An explicit specialization shall be declared in a namespace enclosing the
811 specialized template. An explicit specialization whose declarator-id is
812 not qualified shall be declared in the nearest enclosing namespace of the
813 template, or, if the namespace is inline (7.3.1), any namespace from its
814 enclosing namespace set. */
815 if (current_scope() != DECL_CONTEXT (tmpl)
816 && !at_namespace_scope_p ())
817 {
818 error ("specialization of %qD must appear at namespace scope", tmpl);
819 return false;
820 }
821
822 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
823 /* Same or enclosing namespace. */
824 return true;
825 else
826 {
827 auto_diagnostic_group d;
828 if (permerror (input_location,
829 "specialization of %qD in different namespace", tmpl))
830 inform (DECL_SOURCE_LOCATION (tmpl),
831 " from definition of %q#D", tmpl);
832 return false;
833 }
834 }
835
836 /* SPEC is an explicit instantiation. Check that it is valid to
837 perform this explicit instantiation in the current namespace. */
838
839 static void
840 check_explicit_instantiation_namespace (tree spec)
841 {
842 tree ns;
843
844 /* DR 275: An explicit instantiation shall appear in an enclosing
845 namespace of its template. */
846 ns = decl_namespace_context (spec);
847 if (!is_nested_namespace (current_namespace, ns))
848 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
849 "(which does not enclose namespace %qD)",
850 spec, current_namespace, ns);
851 }
852
853 /* Returns the type of a template specialization only if that
854 specialization needs to be defined. Otherwise (e.g., if the type has
855 already been defined), the function returns NULL_TREE. */
856
857 static tree
858 maybe_new_partial_specialization (tree type)
859 {
860 /* An implicit instantiation of an incomplete type implies
861 the definition of a new class template.
862
863 template<typename T>
864 struct S;
865
866 template<typename T>
867 struct S<T*>;
868
869 Here, S<T*> is an implicit instantiation of S whose type
870 is incomplete. */
871 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
872 return type;
873
874 /* It can also be the case that TYPE is a completed specialization.
875 Continuing the previous example, suppose we also declare:
876
877 template<typename T>
878 requires Integral<T>
879 struct S<T*>;
880
881 Here, S<T*> refers to the specialization S<T*> defined
882 above. However, we need to differentiate definitions because
883 we intend to define a new partial specialization. In this case,
884 we rely on the fact that the constraints are different for
885 this declaration than that above.
886
887 Note that we also get here for injected class names and
888 late-parsed template definitions. We must ensure that we
889 do not create new type declarations for those cases. */
890 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
891 {
892 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
893 tree args = CLASSTYPE_TI_ARGS (type);
894
895 /* If there are no template parameters, this cannot be a new
896 partial template specialization? */
897 if (!current_template_parms)
898 return NULL_TREE;
899
900 /* The injected-class-name is not a new partial specialization. */
901 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
902 return NULL_TREE;
903
904 /* If the constraints are not the same as those of the primary
905 then, we can probably create a new specialization. */
906 tree type_constr = current_template_constraints ();
907
908 if (type == TREE_TYPE (tmpl))
909 {
910 tree main_constr = get_constraints (tmpl);
911 if (equivalent_constraints (type_constr, main_constr))
912 return NULL_TREE;
913 }
914
915 /* Also, if there's a pre-existing specialization with matching
916 constraints, then this also isn't new. */
917 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
918 while (specs)
919 {
920 tree spec_tmpl = TREE_VALUE (specs);
921 tree spec_args = TREE_PURPOSE (specs);
922 tree spec_constr = get_constraints (spec_tmpl);
923 if (comp_template_args (args, spec_args)
924 && equivalent_constraints (type_constr, spec_constr))
925 return NULL_TREE;
926 specs = TREE_CHAIN (specs);
927 }
928
929 /* Create a new type node (and corresponding type decl)
930 for the newly declared specialization. */
931 tree t = make_class_type (TREE_CODE (type));
932 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
933 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
934
935 /* We only need a separate type node for storing the definition of this
936 partial specialization; uses of S<T*> are unconstrained, so all are
937 equivalent. So keep TYPE_CANONICAL the same. */
938 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
939
940 /* Build the corresponding type decl. */
941 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
942 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
943 DECL_SOURCE_LOCATION (d) = input_location;
944 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
945 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
946
947 return t;
948 }
949
950 return NULL_TREE;
951 }
952
953 /* The TYPE is being declared. If it is a template type, that means it
954 is a partial specialization. Do appropriate error-checking. */
955
956 tree
957 maybe_process_partial_specialization (tree type)
958 {
959 tree context;
960
961 if (type == error_mark_node)
962 return error_mark_node;
963
964 /* A lambda that appears in specialization context is not itself a
965 specialization. */
966 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
967 return type;
968
969 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
970 {
971 error ("name of class shadows template template parameter %qD",
972 TYPE_NAME (type));
973 return error_mark_node;
974 }
975
976 context = TYPE_CONTEXT (type);
977
978 if (TYPE_ALIAS_P (type))
979 {
980 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
981
982 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
983 error ("specialization of alias template %qD",
984 TI_TEMPLATE (tinfo));
985 else
986 error ("explicit specialization of non-template %qT", type);
987 return error_mark_node;
988 }
989 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
990 {
991 /* This is for ordinary explicit specialization and partial
992 specialization of a template class such as:
993
994 template <> class C<int>;
995
996 or:
997
998 template <class T> class C<T*>;
999
1000 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1001
1002 if (tree t = maybe_new_partial_specialization (type))
1003 {
1004 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1005 && !at_namespace_scope_p ())
1006 return error_mark_node;
1007 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1008 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1009 if (processing_template_decl)
1010 {
1011 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1012 if (decl == error_mark_node)
1013 return error_mark_node;
1014 return TREE_TYPE (decl);
1015 }
1016 }
1017 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1018 error ("specialization of %qT after instantiation", type);
1019 else if (errorcount && !processing_specialization
1020 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1021 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1022 /* Trying to define a specialization either without a template<> header
1023 or in an inappropriate place. We've already given an error, so just
1024 bail now so we don't actually define the specialization. */
1025 return error_mark_node;
1026 }
1027 else if (CLASS_TYPE_P (type)
1028 && !CLASSTYPE_USE_TEMPLATE (type)
1029 && CLASSTYPE_TEMPLATE_INFO (type)
1030 && context && CLASS_TYPE_P (context)
1031 && CLASSTYPE_TEMPLATE_INFO (context))
1032 {
1033 /* This is for an explicit specialization of member class
1034 template according to [temp.expl.spec/18]:
1035
1036 template <> template <class U> class C<int>::D;
1037
1038 The context `C<int>' must be an implicit instantiation.
1039 Otherwise this is just a member class template declared
1040 earlier like:
1041
1042 template <> class C<int> { template <class U> class D; };
1043 template <> template <class U> class C<int>::D;
1044
1045 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1046 while in the second case, `C<int>::D' is a primary template
1047 and `C<T>::D' may not exist. */
1048
1049 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1050 && !COMPLETE_TYPE_P (type))
1051 {
1052 tree t;
1053 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1054
1055 if (current_namespace
1056 != decl_namespace_context (tmpl))
1057 {
1058 if (permerror (input_location,
1059 "specialization of %qD in different namespace",
1060 type))
1061 inform (DECL_SOURCE_LOCATION (tmpl),
1062 "from definition of %q#D", tmpl);
1063 }
1064
1065 /* Check for invalid specialization after instantiation:
1066
1067 template <> template <> class C<int>::D<int>;
1068 template <> template <class U> class C<int>::D; */
1069
1070 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1071 t; t = TREE_CHAIN (t))
1072 {
1073 tree inst = TREE_VALUE (t);
1074 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1075 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1076 {
1077 /* We already have a full specialization of this partial
1078 instantiation, or a full specialization has been
1079 looked up but not instantiated. Reassign it to the
1080 new member specialization template. */
1081 spec_entry elt;
1082 spec_entry *entry;
1083
1084 elt.tmpl = most_general_template (tmpl);
1085 elt.args = CLASSTYPE_TI_ARGS (inst);
1086 elt.spec = inst;
1087
1088 type_specializations->remove_elt (&elt);
1089
1090 elt.tmpl = tmpl;
1091 CLASSTYPE_TI_ARGS (inst)
1092 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1093
1094 spec_entry **slot
1095 = type_specializations->find_slot (&elt, INSERT);
1096 entry = ggc_alloc<spec_entry> ();
1097 *entry = elt;
1098 *slot = entry;
1099 }
1100 else
1101 /* But if we've had an implicit instantiation, that's a
1102 problem ([temp.expl.spec]/6). */
1103 error ("specialization %qT after instantiation %qT",
1104 type, inst);
1105 }
1106
1107 /* Mark TYPE as a specialization. And as a result, we only
1108 have one level of template argument for the innermost
1109 class template. */
1110 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1111 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1112 CLASSTYPE_TI_ARGS (type)
1113 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1114 }
1115 }
1116 else if (processing_specialization)
1117 {
1118 /* Someday C++0x may allow for enum template specialization. */
1119 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1120 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1121 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1122 "of %qD not allowed by ISO C++", type);
1123 else
1124 {
1125 error ("explicit specialization of non-template %qT", type);
1126 return error_mark_node;
1127 }
1128 }
1129
1130 return type;
1131 }
1132
1133 /* Returns nonzero if we can optimize the retrieval of specializations
1134 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1135 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1136
1137 static inline bool
1138 optimize_specialization_lookup_p (tree tmpl)
1139 {
1140 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1141 && DECL_CLASS_SCOPE_P (tmpl)
1142 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1143 parameter. */
1144 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1145 /* The optimized lookup depends on the fact that the
1146 template arguments for the member function template apply
1147 purely to the containing class, which is not true if the
1148 containing class is an explicit or partial
1149 specialization. */
1150 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1151 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1152 && !DECL_CONV_FN_P (tmpl)
1153 /* It is possible to have a template that is not a member
1154 template and is not a member of a template class:
1155
1156 template <typename T>
1157 struct S { friend A::f(); };
1158
1159 Here, the friend function is a template, but the context does
1160 not have template information. The optimized lookup relies
1161 on having ARGS be the template arguments for both the class
1162 and the function template. */
1163 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1164 }
1165
1166 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1167 gone through coerce_template_parms by now. */
1168
1169 static void
1170 verify_unstripped_args_1 (tree inner)
1171 {
1172 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1173 {
1174 tree arg = TREE_VEC_ELT (inner, i);
1175 if (TREE_CODE (arg) == TEMPLATE_DECL)
1176 /* OK */;
1177 else if (TYPE_P (arg))
1178 gcc_assert (strip_typedefs (arg, NULL) == arg);
1179 else if (ARGUMENT_PACK_P (arg))
1180 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1181 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1182 /* Allow typedefs on the type of a non-type argument, since a
1183 parameter can have them. */;
1184 else
1185 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1186 }
1187 }
1188
1189 static void
1190 verify_unstripped_args (tree args)
1191 {
1192 ++processing_template_decl;
1193 if (!any_dependent_template_arguments_p (args))
1194 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1195 --processing_template_decl;
1196 }
1197
1198 /* Retrieve the specialization (in the sense of [temp.spec] - a
1199 specialization is either an instantiation or an explicit
1200 specialization) of TMPL for the given template ARGS. If there is
1201 no such specialization, return NULL_TREE. The ARGS are a vector of
1202 arguments, or a vector of vectors of arguments, in the case of
1203 templates with more than one level of parameters.
1204
1205 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1206 then we search for a partial specialization matching ARGS. This
1207 parameter is ignored if TMPL is not a class template.
1208
1209 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1210 result is a NONTYPE_ARGUMENT_PACK. */
1211
1212 static tree
1213 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1214 {
1215 if (tmpl == NULL_TREE)
1216 return NULL_TREE;
1217
1218 if (args == error_mark_node)
1219 return NULL_TREE;
1220
1221 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1222 || TREE_CODE (tmpl) == FIELD_DECL);
1223
1224 /* There should be as many levels of arguments as there are
1225 levels of parameters. */
1226 gcc_assert (TMPL_ARGS_DEPTH (args)
1227 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1228 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1229 : template_class_depth (DECL_CONTEXT (tmpl))));
1230
1231 if (flag_checking)
1232 verify_unstripped_args (args);
1233
1234 /* Lambda functions in templates aren't instantiated normally, but through
1235 tsubst_lambda_expr. */
1236 if (lambda_fn_in_template_p (tmpl))
1237 return NULL_TREE;
1238
1239 if (optimize_specialization_lookup_p (tmpl))
1240 {
1241 /* The template arguments actually apply to the containing
1242 class. Find the class specialization with those
1243 arguments. */
1244 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1245 tree class_specialization
1246 = retrieve_specialization (class_template, args, 0);
1247 if (!class_specialization)
1248 return NULL_TREE;
1249
1250 /* Find the instance of TMPL. */
1251 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1252 for (ovl_iterator iter (fns); iter; ++iter)
1253 {
1254 tree fn = *iter;
1255 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1256 /* using-declarations can add base methods to the method vec,
1257 and we don't want those here. */
1258 && DECL_CONTEXT (fn) == class_specialization)
1259 return fn;
1260 }
1261 return NULL_TREE;
1262 }
1263 else
1264 {
1265 spec_entry *found;
1266 spec_entry elt;
1267 hash_table<spec_hasher> *specializations;
1268
1269 elt.tmpl = tmpl;
1270 elt.args = args;
1271 elt.spec = NULL_TREE;
1272
1273 if (DECL_CLASS_TEMPLATE_P (tmpl))
1274 specializations = type_specializations;
1275 else
1276 specializations = decl_specializations;
1277
1278 if (hash == 0)
1279 hash = spec_hasher::hash (&elt);
1280 found = specializations->find_with_hash (&elt, hash);
1281 if (found)
1282 return found->spec;
1283 }
1284
1285 return NULL_TREE;
1286 }
1287
1288 /* Like retrieve_specialization, but for local declarations. */
1289
1290 tree
1291 retrieve_local_specialization (tree tmpl)
1292 {
1293 if (local_specializations == NULL)
1294 return NULL_TREE;
1295
1296 tree *slot = local_specializations->get (tmpl);
1297 return slot ? *slot : NULL_TREE;
1298 }
1299
1300 /* Returns nonzero iff DECL is a specialization of TMPL. */
1301
1302 int
1303 is_specialization_of (tree decl, tree tmpl)
1304 {
1305 tree t;
1306
1307 if (TREE_CODE (decl) == FUNCTION_DECL)
1308 {
1309 for (t = decl;
1310 t != NULL_TREE;
1311 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1312 if (t == tmpl)
1313 return 1;
1314 }
1315 else
1316 {
1317 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1318
1319 for (t = TREE_TYPE (decl);
1320 t != NULL_TREE;
1321 t = CLASSTYPE_USE_TEMPLATE (t)
1322 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1323 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1324 return 1;
1325 }
1326
1327 return 0;
1328 }
1329
1330 /* Returns nonzero iff DECL is a specialization of friend declaration
1331 FRIEND_DECL according to [temp.friend]. */
1332
1333 bool
1334 is_specialization_of_friend (tree decl, tree friend_decl)
1335 {
1336 bool need_template = true;
1337 int template_depth;
1338
1339 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1340 || TREE_CODE (decl) == TYPE_DECL);
1341
1342 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1343 of a template class, we want to check if DECL is a specialization
1344 if this. */
1345 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1346 && DECL_TEMPLATE_INFO (friend_decl)
1347 && !DECL_USE_TEMPLATE (friend_decl))
1348 {
1349 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1350 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1351 need_template = false;
1352 }
1353 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1354 && !PRIMARY_TEMPLATE_P (friend_decl))
1355 need_template = false;
1356
1357 /* There is nothing to do if this is not a template friend. */
1358 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1359 return false;
1360
1361 if (is_specialization_of (decl, friend_decl))
1362 return true;
1363
1364 /* [temp.friend/6]
1365 A member of a class template may be declared to be a friend of a
1366 non-template class. In this case, the corresponding member of
1367 every specialization of the class template is a friend of the
1368 class granting friendship.
1369
1370 For example, given a template friend declaration
1371
1372 template <class T> friend void A<T>::f();
1373
1374 the member function below is considered a friend
1375
1376 template <> struct A<int> {
1377 void f();
1378 };
1379
1380 For this type of template friend, TEMPLATE_DEPTH below will be
1381 nonzero. To determine if DECL is a friend of FRIEND, we first
1382 check if the enclosing class is a specialization of another. */
1383
1384 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1385 if (template_depth
1386 && DECL_CLASS_SCOPE_P (decl)
1387 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1388 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1389 {
1390 /* Next, we check the members themselves. In order to handle
1391 a few tricky cases, such as when FRIEND_DECL's are
1392
1393 template <class T> friend void A<T>::g(T t);
1394 template <class T> template <T t> friend void A<T>::h();
1395
1396 and DECL's are
1397
1398 void A<int>::g(int);
1399 template <int> void A<int>::h();
1400
1401 we need to figure out ARGS, the template arguments from
1402 the context of DECL. This is required for template substitution
1403 of `T' in the function parameter of `g' and template parameter
1404 of `h' in the above examples. Here ARGS corresponds to `int'. */
1405
1406 tree context = DECL_CONTEXT (decl);
1407 tree args = NULL_TREE;
1408 int current_depth = 0;
1409
1410 while (current_depth < template_depth)
1411 {
1412 if (CLASSTYPE_TEMPLATE_INFO (context))
1413 {
1414 if (current_depth == 0)
1415 args = TYPE_TI_ARGS (context);
1416 else
1417 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1418 current_depth++;
1419 }
1420 context = TYPE_CONTEXT (context);
1421 }
1422
1423 if (TREE_CODE (decl) == FUNCTION_DECL)
1424 {
1425 bool is_template;
1426 tree friend_type;
1427 tree decl_type;
1428 tree friend_args_type;
1429 tree decl_args_type;
1430
1431 /* Make sure that both DECL and FRIEND_DECL are templates or
1432 non-templates. */
1433 is_template = DECL_TEMPLATE_INFO (decl)
1434 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1435 if (need_template ^ is_template)
1436 return false;
1437 else if (is_template)
1438 {
1439 /* If both are templates, check template parameter list. */
1440 tree friend_parms
1441 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1442 args, tf_none);
1443 if (!comp_template_parms
1444 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1445 friend_parms))
1446 return false;
1447
1448 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1449 }
1450 else
1451 decl_type = TREE_TYPE (decl);
1452
1453 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1454 tf_none, NULL_TREE);
1455 if (friend_type == error_mark_node)
1456 return false;
1457
1458 /* Check if return types match. */
1459 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1460 return false;
1461
1462 /* Check if function parameter types match, ignoring the
1463 `this' parameter. */
1464 friend_args_type = TYPE_ARG_TYPES (friend_type);
1465 decl_args_type = TYPE_ARG_TYPES (decl_type);
1466 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1467 friend_args_type = TREE_CHAIN (friend_args_type);
1468 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1469 decl_args_type = TREE_CHAIN (decl_args_type);
1470
1471 return compparms (decl_args_type, friend_args_type);
1472 }
1473 else
1474 {
1475 /* DECL is a TYPE_DECL */
1476 bool is_template;
1477 tree decl_type = TREE_TYPE (decl);
1478
1479 /* Make sure that both DECL and FRIEND_DECL are templates or
1480 non-templates. */
1481 is_template
1482 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1483 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1484
1485 if (need_template ^ is_template)
1486 return false;
1487 else if (is_template)
1488 {
1489 tree friend_parms;
1490 /* If both are templates, check the name of the two
1491 TEMPLATE_DECL's first because is_friend didn't. */
1492 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1493 != DECL_NAME (friend_decl))
1494 return false;
1495
1496 /* Now check template parameter list. */
1497 friend_parms
1498 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1499 args, tf_none);
1500 return comp_template_parms
1501 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1502 friend_parms);
1503 }
1504 else
1505 return (DECL_NAME (decl)
1506 == DECL_NAME (friend_decl));
1507 }
1508 }
1509 return false;
1510 }
1511
1512 /* Register the specialization SPEC as a specialization of TMPL with
1513 the indicated ARGS. IS_FRIEND indicates whether the specialization
1514 is actually just a friend declaration. ATTRLIST is the list of
1515 attributes that the specialization is declared with or NULL when
1516 it isn't. Returns SPEC, or an equivalent prior declaration, if
1517 available.
1518
1519 We also store instantiations of field packs in the hash table, even
1520 though they are not themselves templates, to make lookup easier. */
1521
1522 static tree
1523 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1524 hashval_t hash)
1525 {
1526 tree fn;
1527 spec_entry **slot = NULL;
1528 spec_entry elt;
1529
1530 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1531 || (TREE_CODE (tmpl) == FIELD_DECL
1532 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1533
1534 if (TREE_CODE (spec) == FUNCTION_DECL
1535 && uses_template_parms (DECL_TI_ARGS (spec)))
1536 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1537 register it; we want the corresponding TEMPLATE_DECL instead.
1538 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1539 the more obvious `uses_template_parms (spec)' to avoid problems
1540 with default function arguments. In particular, given
1541 something like this:
1542
1543 template <class T> void f(T t1, T t = T())
1544
1545 the default argument expression is not substituted for in an
1546 instantiation unless and until it is actually needed. */
1547 return spec;
1548
1549 if (optimize_specialization_lookup_p (tmpl))
1550 /* We don't put these specializations in the hash table, but we might
1551 want to give an error about a mismatch. */
1552 fn = retrieve_specialization (tmpl, args, 0);
1553 else
1554 {
1555 elt.tmpl = tmpl;
1556 elt.args = args;
1557 elt.spec = spec;
1558
1559 if (hash == 0)
1560 hash = spec_hasher::hash (&elt);
1561
1562 slot =
1563 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1564 if (*slot)
1565 fn = ((spec_entry *) *slot)->spec;
1566 else
1567 fn = NULL_TREE;
1568 }
1569
1570 /* We can sometimes try to re-register a specialization that we've
1571 already got. In particular, regenerate_decl_from_template calls
1572 duplicate_decls which will update the specialization list. But,
1573 we'll still get called again here anyhow. It's more convenient
1574 to simply allow this than to try to prevent it. */
1575 if (fn == spec)
1576 return spec;
1577 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1578 {
1579 if (DECL_TEMPLATE_INSTANTIATION (fn))
1580 {
1581 if (DECL_ODR_USED (fn)
1582 || DECL_EXPLICIT_INSTANTIATION (fn))
1583 {
1584 error ("specialization of %qD after instantiation",
1585 fn);
1586 return error_mark_node;
1587 }
1588 else
1589 {
1590 tree clone;
1591 /* This situation should occur only if the first
1592 specialization is an implicit instantiation, the
1593 second is an explicit specialization, and the
1594 implicit instantiation has not yet been used. That
1595 situation can occur if we have implicitly
1596 instantiated a member function and then specialized
1597 it later.
1598
1599 We can also wind up here if a friend declaration that
1600 looked like an instantiation turns out to be a
1601 specialization:
1602
1603 template <class T> void foo(T);
1604 class S { friend void foo<>(int) };
1605 template <> void foo(int);
1606
1607 We transform the existing DECL in place so that any
1608 pointers to it become pointers to the updated
1609 declaration.
1610
1611 If there was a definition for the template, but not
1612 for the specialization, we want this to look as if
1613 there were no definition, and vice versa. */
1614 DECL_INITIAL (fn) = NULL_TREE;
1615 duplicate_decls (spec, fn, is_friend);
1616 /* The call to duplicate_decls will have applied
1617 [temp.expl.spec]:
1618
1619 An explicit specialization of a function template
1620 is inline only if it is explicitly declared to be,
1621 and independently of whether its function template
1622 is.
1623
1624 to the primary function; now copy the inline bits to
1625 the various clones. */
1626 FOR_EACH_CLONE (clone, fn)
1627 {
1628 DECL_DECLARED_INLINE_P (clone)
1629 = DECL_DECLARED_INLINE_P (fn);
1630 DECL_SOURCE_LOCATION (clone)
1631 = DECL_SOURCE_LOCATION (fn);
1632 DECL_DELETED_FN (clone)
1633 = DECL_DELETED_FN (fn);
1634 }
1635 check_specialization_namespace (tmpl);
1636
1637 return fn;
1638 }
1639 }
1640 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1641 {
1642 tree dd = duplicate_decls (spec, fn, is_friend);
1643 if (dd == error_mark_node)
1644 /* We've already complained in duplicate_decls. */
1645 return error_mark_node;
1646
1647 if (dd == NULL_TREE && DECL_INITIAL (spec))
1648 /* Dup decl failed, but this is a new definition. Set the
1649 line number so any errors match this new
1650 definition. */
1651 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1652
1653 return fn;
1654 }
1655 }
1656 else if (fn)
1657 return duplicate_decls (spec, fn, is_friend);
1658
1659 /* A specialization must be declared in the same namespace as the
1660 template it is specializing. */
1661 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1662 && !check_specialization_namespace (tmpl))
1663 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1664
1665 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1666 {
1667 spec_entry *entry = ggc_alloc<spec_entry> ();
1668 gcc_assert (tmpl && args && spec);
1669 *entry = elt;
1670 *slot = entry;
1671 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1672 && PRIMARY_TEMPLATE_P (tmpl)
1673 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1674 || variable_template_p (tmpl))
1675 /* If TMPL is a forward declaration of a template function, keep a list
1676 of all specializations in case we need to reassign them to a friend
1677 template later in tsubst_friend_function.
1678
1679 Also keep a list of all variable template instantiations so that
1680 process_partial_specialization can check whether a later partial
1681 specialization would have used it. */
1682 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1683 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1684 }
1685
1686 return spec;
1687 }
1688
1689 /* Returns true iff two spec_entry nodes are equivalent. */
1690
1691 int comparing_specializations;
1692
1693 bool
1694 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1695 {
1696 int equal;
1697
1698 ++comparing_specializations;
1699 equal = (e1->tmpl == e2->tmpl
1700 && comp_template_args (e1->args, e2->args));
1701 if (equal && flag_concepts
1702 /* tmpl could be a FIELD_DECL for a capture pack. */
1703 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1704 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1705 && uses_template_parms (e1->args))
1706 {
1707 /* Partial specializations of a variable template can be distinguished by
1708 constraints. */
1709 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1710 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1711 equal = equivalent_constraints (c1, c2);
1712 }
1713 --comparing_specializations;
1714
1715 return equal;
1716 }
1717
1718 /* Returns a hash for a template TMPL and template arguments ARGS. */
1719
1720 static hashval_t
1721 hash_tmpl_and_args (tree tmpl, tree args)
1722 {
1723 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1724 return iterative_hash_template_arg (args, val);
1725 }
1726
1727 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1728 ignoring SPEC. */
1729
1730 hashval_t
1731 spec_hasher::hash (spec_entry *e)
1732 {
1733 return hash_tmpl_and_args (e->tmpl, e->args);
1734 }
1735
1736 /* Recursively calculate a hash value for a template argument ARG, for use
1737 in the hash tables of template specializations. */
1738
1739 hashval_t
1740 iterative_hash_template_arg (tree arg, hashval_t val)
1741 {
1742 unsigned HOST_WIDE_INT i;
1743 enum tree_code code;
1744 char tclass;
1745
1746 if (arg == NULL_TREE)
1747 return iterative_hash_object (arg, val);
1748
1749 if (!TYPE_P (arg))
1750 STRIP_NOPS (arg);
1751
1752 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1753 gcc_unreachable ();
1754
1755 code = TREE_CODE (arg);
1756 tclass = TREE_CODE_CLASS (code);
1757
1758 val = iterative_hash_object (code, val);
1759
1760 switch (code)
1761 {
1762 case ERROR_MARK:
1763 return val;
1764
1765 case IDENTIFIER_NODE:
1766 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1767
1768 case TREE_VEC:
1769 {
1770 int i, len = TREE_VEC_LENGTH (arg);
1771 for (i = 0; i < len; ++i)
1772 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1773 return val;
1774 }
1775
1776 case TYPE_PACK_EXPANSION:
1777 case EXPR_PACK_EXPANSION:
1778 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1779 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1780
1781 case TYPE_ARGUMENT_PACK:
1782 case NONTYPE_ARGUMENT_PACK:
1783 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1784
1785 case TREE_LIST:
1786 for (; arg; arg = TREE_CHAIN (arg))
1787 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1788 return val;
1789
1790 case OVERLOAD:
1791 for (lkp_iterator iter (arg); iter; ++iter)
1792 val = iterative_hash_template_arg (*iter, val);
1793 return val;
1794
1795 case CONSTRUCTOR:
1796 {
1797 tree field, value;
1798 iterative_hash_template_arg (TREE_TYPE (arg), val);
1799 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1800 {
1801 val = iterative_hash_template_arg (field, val);
1802 val = iterative_hash_template_arg (value, val);
1803 }
1804 return val;
1805 }
1806
1807 case PARM_DECL:
1808 if (!DECL_ARTIFICIAL (arg))
1809 {
1810 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1811 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1812 }
1813 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1814
1815 case TARGET_EXPR:
1816 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1817
1818 case PTRMEM_CST:
1819 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1820 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1821
1822 case TEMPLATE_PARM_INDEX:
1823 val = iterative_hash_template_arg
1824 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1825 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1826 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1827
1828 case TRAIT_EXPR:
1829 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1830 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1831 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1832
1833 case BASELINK:
1834 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1835 val);
1836 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1837 val);
1838
1839 case MODOP_EXPR:
1840 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1841 code = TREE_CODE (TREE_OPERAND (arg, 1));
1842 val = iterative_hash_object (code, val);
1843 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1844
1845 case LAMBDA_EXPR:
1846 /* [temp.over.link] Two lambda-expressions are never considered
1847 equivalent.
1848
1849 So just hash the closure type. */
1850 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1851
1852 case CAST_EXPR:
1853 case IMPLICIT_CONV_EXPR:
1854 case STATIC_CAST_EXPR:
1855 case REINTERPRET_CAST_EXPR:
1856 case CONST_CAST_EXPR:
1857 case DYNAMIC_CAST_EXPR:
1858 case NEW_EXPR:
1859 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1860 /* Now hash operands as usual. */
1861 break;
1862
1863 case CALL_EXPR:
1864 {
1865 tree fn = CALL_EXPR_FN (arg);
1866 if (tree name = dependent_name (fn))
1867 {
1868 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1869 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1870 fn = name;
1871 }
1872 val = iterative_hash_template_arg (fn, val);
1873 call_expr_arg_iterator ai;
1874 for (tree x = first_call_expr_arg (arg, &ai); x;
1875 x = next_call_expr_arg (&ai))
1876 val = iterative_hash_template_arg (x, val);
1877 return val;
1878 }
1879
1880 default:
1881 break;
1882 }
1883
1884 switch (tclass)
1885 {
1886 case tcc_type:
1887 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1888 {
1889 // We want an alias specialization that survived strip_typedefs
1890 // to hash differently from its TYPE_CANONICAL, to avoid hash
1891 // collisions that compare as different in template_args_equal.
1892 // These could be dependent specializations that strip_typedefs
1893 // left alone, or untouched specializations because
1894 // coerce_template_parms returns the unconverted template
1895 // arguments if it sees incomplete argument packs.
1896 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1897 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1898 }
1899 if (TYPE_CANONICAL (arg))
1900 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1901 val);
1902 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1903 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1904 /* Otherwise just compare the types during lookup. */
1905 return val;
1906
1907 case tcc_declaration:
1908 case tcc_constant:
1909 return iterative_hash_expr (arg, val);
1910
1911 default:
1912 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1913 {
1914 unsigned n = cp_tree_operand_length (arg);
1915 for (i = 0; i < n; ++i)
1916 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1917 return val;
1918 }
1919 }
1920 gcc_unreachable ();
1921 return 0;
1922 }
1923
1924 /* Unregister the specialization SPEC as a specialization of TMPL.
1925 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1926 if the SPEC was listed as a specialization of TMPL.
1927
1928 Note that SPEC has been ggc_freed, so we can't look inside it. */
1929
1930 bool
1931 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1932 {
1933 spec_entry *entry;
1934 spec_entry elt;
1935
1936 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1937 elt.args = TI_ARGS (tinfo);
1938 elt.spec = NULL_TREE;
1939
1940 entry = decl_specializations->find (&elt);
1941 if (entry != NULL)
1942 {
1943 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1944 gcc_assert (new_spec != NULL_TREE);
1945 entry->spec = new_spec;
1946 return 1;
1947 }
1948
1949 return 0;
1950 }
1951
1952 /* Like register_specialization, but for local declarations. We are
1953 registering SPEC, an instantiation of TMPL. */
1954
1955 void
1956 register_local_specialization (tree spec, tree tmpl)
1957 {
1958 gcc_assert (tmpl != spec);
1959 local_specializations->put (tmpl, spec);
1960 }
1961
1962 /* TYPE is a class type. Returns true if TYPE is an explicitly
1963 specialized class. */
1964
1965 bool
1966 explicit_class_specialization_p (tree type)
1967 {
1968 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1969 return false;
1970 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1971 }
1972
1973 /* Print the list of functions at FNS, going through all the overloads
1974 for each element of the list. Alternatively, FNS cannot be a
1975 TREE_LIST, in which case it will be printed together with all the
1976 overloads.
1977
1978 MORE and *STR should respectively be FALSE and NULL when the function
1979 is called from the outside. They are used internally on recursive
1980 calls. print_candidates manages the two parameters and leaves NULL
1981 in *STR when it ends. */
1982
1983 static void
1984 print_candidates_1 (tree fns, char **str, bool more = false)
1985 {
1986 if (TREE_CODE (fns) == TREE_LIST)
1987 for (; fns; fns = TREE_CHAIN (fns))
1988 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1989 else
1990 for (lkp_iterator iter (fns); iter;)
1991 {
1992 tree cand = *iter;
1993 ++iter;
1994
1995 const char *pfx = *str;
1996 if (!pfx)
1997 {
1998 if (more || iter)
1999 pfx = _("candidates are:");
2000 else
2001 pfx = _("candidate is:");
2002 *str = get_spaces (pfx);
2003 }
2004 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2005 }
2006 }
2007
2008 /* Print the list of candidate FNS in an error message. FNS can also
2009 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2010
2011 void
2012 print_candidates (tree fns)
2013 {
2014 char *str = NULL;
2015 print_candidates_1 (fns, &str);
2016 free (str);
2017 }
2018
2019 /* Get a (possibly) constrained template declaration for the
2020 purpose of ordering candidates. */
2021 static tree
2022 get_template_for_ordering (tree list)
2023 {
2024 gcc_assert (TREE_CODE (list) == TREE_LIST);
2025 tree f = TREE_VALUE (list);
2026 if (tree ti = DECL_TEMPLATE_INFO (f))
2027 return TI_TEMPLATE (ti);
2028 return f;
2029 }
2030
2031 /* Among candidates having the same signature, return the
2032 most constrained or NULL_TREE if there is no best candidate.
2033 If the signatures of candidates vary (e.g., template
2034 specialization vs. member function), then there can be no
2035 most constrained.
2036
2037 Note that we don't compare constraints on the functions
2038 themselves, but rather those of their templates. */
2039 static tree
2040 most_constrained_function (tree candidates)
2041 {
2042 // Try to find the best candidate in a first pass.
2043 tree champ = candidates;
2044 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2045 {
2046 int winner = more_constrained (get_template_for_ordering (champ),
2047 get_template_for_ordering (c));
2048 if (winner == -1)
2049 champ = c; // The candidate is more constrained
2050 else if (winner == 0)
2051 return NULL_TREE; // Neither is more constrained
2052 }
2053
2054 // Verify that the champ is better than previous candidates.
2055 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2056 if (!more_constrained (get_template_for_ordering (champ),
2057 get_template_for_ordering (c)))
2058 return NULL_TREE;
2059 }
2060
2061 return champ;
2062 }
2063
2064
2065 /* Returns the template (one of the functions given by TEMPLATE_ID)
2066 which can be specialized to match the indicated DECL with the
2067 explicit template args given in TEMPLATE_ID. The DECL may be
2068 NULL_TREE if none is available. In that case, the functions in
2069 TEMPLATE_ID are non-members.
2070
2071 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2072 specialization of a member template.
2073
2074 The TEMPLATE_COUNT is the number of references to qualifying
2075 template classes that appeared in the name of the function. See
2076 check_explicit_specialization for a more accurate description.
2077
2078 TSK indicates what kind of template declaration (if any) is being
2079 declared. TSK_TEMPLATE indicates that the declaration given by
2080 DECL, though a FUNCTION_DECL, has template parameters, and is
2081 therefore a template function.
2082
2083 The template args (those explicitly specified and those deduced)
2084 are output in a newly created vector *TARGS_OUT.
2085
2086 If it is impossible to determine the result, an error message is
2087 issued. The error_mark_node is returned to indicate failure. */
2088
2089 static tree
2090 determine_specialization (tree template_id,
2091 tree decl,
2092 tree* targs_out,
2093 int need_member_template,
2094 int template_count,
2095 tmpl_spec_kind tsk)
2096 {
2097 tree fns;
2098 tree targs;
2099 tree explicit_targs;
2100 tree candidates = NULL_TREE;
2101
2102 /* A TREE_LIST of templates of which DECL may be a specialization.
2103 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2104 corresponding TREE_PURPOSE is the set of template arguments that,
2105 when used to instantiate the template, would produce a function
2106 with the signature of DECL. */
2107 tree templates = NULL_TREE;
2108 int header_count;
2109 cp_binding_level *b;
2110
2111 *targs_out = NULL_TREE;
2112
2113 if (template_id == error_mark_node || decl == error_mark_node)
2114 return error_mark_node;
2115
2116 /* We shouldn't be specializing a member template of an
2117 unspecialized class template; we already gave an error in
2118 check_specialization_scope, now avoid crashing. */
2119 if (!VAR_P (decl)
2120 && template_count && DECL_CLASS_SCOPE_P (decl)
2121 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2122 {
2123 gcc_assert (errorcount);
2124 return error_mark_node;
2125 }
2126
2127 fns = TREE_OPERAND (template_id, 0);
2128 explicit_targs = TREE_OPERAND (template_id, 1);
2129
2130 if (fns == error_mark_node)
2131 return error_mark_node;
2132
2133 /* Check for baselinks. */
2134 if (BASELINK_P (fns))
2135 fns = BASELINK_FUNCTIONS (fns);
2136
2137 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2138 {
2139 error_at (DECL_SOURCE_LOCATION (decl),
2140 "%qD is not a function template", fns);
2141 return error_mark_node;
2142 }
2143 else if (VAR_P (decl) && !variable_template_p (fns))
2144 {
2145 error ("%qD is not a variable template", fns);
2146 return error_mark_node;
2147 }
2148
2149 /* Count the number of template headers specified for this
2150 specialization. */
2151 header_count = 0;
2152 for (b = current_binding_level;
2153 b->kind == sk_template_parms;
2154 b = b->level_chain)
2155 ++header_count;
2156
2157 tree orig_fns = fns;
2158
2159 if (variable_template_p (fns))
2160 {
2161 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2162 targs = coerce_template_parms (parms, explicit_targs, fns,
2163 tf_warning_or_error,
2164 /*req_all*/true, /*use_defarg*/true);
2165 if (targs != error_mark_node)
2166 templates = tree_cons (targs, fns, templates);
2167 }
2168 else for (lkp_iterator iter (fns); iter; ++iter)
2169 {
2170 tree fn = *iter;
2171
2172 if (TREE_CODE (fn) == TEMPLATE_DECL)
2173 {
2174 tree decl_arg_types;
2175 tree fn_arg_types;
2176 tree insttype;
2177
2178 /* In case of explicit specialization, we need to check if
2179 the number of template headers appearing in the specialization
2180 is correct. This is usually done in check_explicit_specialization,
2181 but the check done there cannot be exhaustive when specializing
2182 member functions. Consider the following code:
2183
2184 template <> void A<int>::f(int);
2185 template <> template <> void A<int>::f(int);
2186
2187 Assuming that A<int> is not itself an explicit specialization
2188 already, the first line specializes "f" which is a non-template
2189 member function, whilst the second line specializes "f" which
2190 is a template member function. So both lines are syntactically
2191 correct, and check_explicit_specialization does not reject
2192 them.
2193
2194 Here, we can do better, as we are matching the specialization
2195 against the declarations. We count the number of template
2196 headers, and we check if they match TEMPLATE_COUNT + 1
2197 (TEMPLATE_COUNT is the number of qualifying template classes,
2198 plus there must be another header for the member template
2199 itself).
2200
2201 Notice that if header_count is zero, this is not a
2202 specialization but rather a template instantiation, so there
2203 is no check we can perform here. */
2204 if (header_count && header_count != template_count + 1)
2205 continue;
2206
2207 /* Check that the number of template arguments at the
2208 innermost level for DECL is the same as for FN. */
2209 if (current_binding_level->kind == sk_template_parms
2210 && !current_binding_level->explicit_spec_p
2211 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2212 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2213 (current_template_parms))))
2214 continue;
2215
2216 /* DECL might be a specialization of FN. */
2217 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2218 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2219
2220 /* For a non-static member function, we need to make sure
2221 that the const qualification is the same. Since
2222 get_bindings does not try to merge the "this" parameter,
2223 we must do the comparison explicitly. */
2224 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2225 {
2226 if (!same_type_p (TREE_VALUE (fn_arg_types),
2227 TREE_VALUE (decl_arg_types)))
2228 continue;
2229
2230 /* And the ref-qualification. */
2231 if (type_memfn_rqual (TREE_TYPE (decl))
2232 != type_memfn_rqual (TREE_TYPE (fn)))
2233 continue;
2234 }
2235
2236 /* Skip the "this" parameter and, for constructors of
2237 classes with virtual bases, the VTT parameter. A
2238 full specialization of a constructor will have a VTT
2239 parameter, but a template never will. */
2240 decl_arg_types
2241 = skip_artificial_parms_for (decl, decl_arg_types);
2242 fn_arg_types
2243 = skip_artificial_parms_for (fn, fn_arg_types);
2244
2245 /* Function templates cannot be specializations; there are
2246 no partial specializations of functions. Therefore, if
2247 the type of DECL does not match FN, there is no
2248 match.
2249
2250 Note that it should never be the case that we have both
2251 candidates added here, and for regular member functions
2252 below. */
2253 if (tsk == tsk_template)
2254 {
2255 if (compparms (fn_arg_types, decl_arg_types))
2256 candidates = tree_cons (NULL_TREE, fn, candidates);
2257 continue;
2258 }
2259
2260 /* See whether this function might be a specialization of this
2261 template. Suppress access control because we might be trying
2262 to make this specialization a friend, and we have already done
2263 access control for the declaration of the specialization. */
2264 push_deferring_access_checks (dk_no_check);
2265 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2266 pop_deferring_access_checks ();
2267
2268 if (!targs)
2269 /* We cannot deduce template arguments that when used to
2270 specialize TMPL will produce DECL. */
2271 continue;
2272
2273 if (uses_template_parms (targs))
2274 /* We deduced something involving 'auto', which isn't a valid
2275 template argument. */
2276 continue;
2277
2278 /* Remove, from the set of candidates, all those functions
2279 whose constraints are not satisfied. */
2280 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2281 continue;
2282
2283 // Then, try to form the new function type.
2284 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2285 if (insttype == error_mark_node)
2286 continue;
2287 fn_arg_types
2288 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2289 if (!compparms (fn_arg_types, decl_arg_types))
2290 continue;
2291
2292 /* Save this template, and the arguments deduced. */
2293 templates = tree_cons (targs, fn, templates);
2294 }
2295 else if (need_member_template)
2296 /* FN is an ordinary member function, and we need a
2297 specialization of a member template. */
2298 ;
2299 else if (TREE_CODE (fn) != FUNCTION_DECL)
2300 /* We can get IDENTIFIER_NODEs here in certain erroneous
2301 cases. */
2302 ;
2303 else if (!DECL_FUNCTION_MEMBER_P (fn))
2304 /* This is just an ordinary non-member function. Nothing can
2305 be a specialization of that. */
2306 ;
2307 else if (DECL_ARTIFICIAL (fn))
2308 /* Cannot specialize functions that are created implicitly. */
2309 ;
2310 else
2311 {
2312 tree decl_arg_types;
2313
2314 /* This is an ordinary member function. However, since
2315 we're here, we can assume its enclosing class is a
2316 template class. For example,
2317
2318 template <typename T> struct S { void f(); };
2319 template <> void S<int>::f() {}
2320
2321 Here, S<int>::f is a non-template, but S<int> is a
2322 template class. If FN has the same type as DECL, we
2323 might be in business. */
2324
2325 if (!DECL_TEMPLATE_INFO (fn))
2326 /* Its enclosing class is an explicit specialization
2327 of a template class. This is not a candidate. */
2328 continue;
2329
2330 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2331 TREE_TYPE (TREE_TYPE (fn))))
2332 /* The return types differ. */
2333 continue;
2334
2335 /* Adjust the type of DECL in case FN is a static member. */
2336 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2337 if (DECL_STATIC_FUNCTION_P (fn)
2338 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2339 decl_arg_types = TREE_CHAIN (decl_arg_types);
2340
2341 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2342 decl_arg_types))
2343 continue;
2344
2345 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2346 && (type_memfn_rqual (TREE_TYPE (decl))
2347 != type_memfn_rqual (TREE_TYPE (fn))))
2348 continue;
2349
2350 // If the deduced arguments do not satisfy the constraints,
2351 // this is not a candidate.
2352 if (flag_concepts && !constraints_satisfied_p (fn))
2353 continue;
2354
2355 // Add the candidate.
2356 candidates = tree_cons (NULL_TREE, fn, candidates);
2357 }
2358 }
2359
2360 if (templates && TREE_CHAIN (templates))
2361 {
2362 /* We have:
2363
2364 [temp.expl.spec]
2365
2366 It is possible for a specialization with a given function
2367 signature to be instantiated from more than one function
2368 template. In such cases, explicit specification of the
2369 template arguments must be used to uniquely identify the
2370 function template specialization being specialized.
2371
2372 Note that here, there's no suggestion that we're supposed to
2373 determine which of the candidate templates is most
2374 specialized. However, we, also have:
2375
2376 [temp.func.order]
2377
2378 Partial ordering of overloaded function template
2379 declarations is used in the following contexts to select
2380 the function template to which a function template
2381 specialization refers:
2382
2383 -- when an explicit specialization refers to a function
2384 template.
2385
2386 So, we do use the partial ordering rules, at least for now.
2387 This extension can only serve to make invalid programs valid,
2388 so it's safe. And, there is strong anecdotal evidence that
2389 the committee intended the partial ordering rules to apply;
2390 the EDG front end has that behavior, and John Spicer claims
2391 that the committee simply forgot to delete the wording in
2392 [temp.expl.spec]. */
2393 tree tmpl = most_specialized_instantiation (templates);
2394 if (tmpl != error_mark_node)
2395 {
2396 templates = tmpl;
2397 TREE_CHAIN (templates) = NULL_TREE;
2398 }
2399 }
2400
2401 // Concepts allows multiple declarations of member functions
2402 // with the same signature. Like above, we need to rely on
2403 // on the partial ordering of those candidates to determine which
2404 // is the best.
2405 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2406 {
2407 if (tree cand = most_constrained_function (candidates))
2408 {
2409 candidates = cand;
2410 TREE_CHAIN (cand) = NULL_TREE;
2411 }
2412 }
2413
2414 if (templates == NULL_TREE && candidates == NULL_TREE)
2415 {
2416 error ("template-id %qD for %q+D does not match any template "
2417 "declaration", template_id, decl);
2418 if (header_count && header_count != template_count + 1)
2419 inform (DECL_SOURCE_LOCATION (decl),
2420 "saw %d %<template<>%>, need %d for "
2421 "specializing a member function template",
2422 header_count, template_count + 1);
2423 else
2424 print_candidates (orig_fns);
2425 return error_mark_node;
2426 }
2427 else if ((templates && TREE_CHAIN (templates))
2428 || (candidates && TREE_CHAIN (candidates))
2429 || (templates && candidates))
2430 {
2431 error ("ambiguous template specialization %qD for %q+D",
2432 template_id, decl);
2433 candidates = chainon (candidates, templates);
2434 print_candidates (candidates);
2435 return error_mark_node;
2436 }
2437
2438 /* We have one, and exactly one, match. */
2439 if (candidates)
2440 {
2441 tree fn = TREE_VALUE (candidates);
2442 *targs_out = copy_node (DECL_TI_ARGS (fn));
2443
2444 /* Propagate the candidate's constraints to the declaration. */
2445 set_constraints (decl, get_constraints (fn));
2446
2447 /* DECL is a re-declaration or partial instantiation of a template
2448 function. */
2449 if (TREE_CODE (fn) == TEMPLATE_DECL)
2450 return fn;
2451 /* It was a specialization of an ordinary member function in a
2452 template class. */
2453 return DECL_TI_TEMPLATE (fn);
2454 }
2455
2456 /* It was a specialization of a template. */
2457 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2458 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2459 {
2460 *targs_out = copy_node (targs);
2461 SET_TMPL_ARGS_LEVEL (*targs_out,
2462 TMPL_ARGS_DEPTH (*targs_out),
2463 TREE_PURPOSE (templates));
2464 }
2465 else
2466 *targs_out = TREE_PURPOSE (templates);
2467 return TREE_VALUE (templates);
2468 }
2469
2470 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2471 but with the default argument values filled in from those in the
2472 TMPL_TYPES. */
2473
2474 static tree
2475 copy_default_args_to_explicit_spec_1 (tree spec_types,
2476 tree tmpl_types)
2477 {
2478 tree new_spec_types;
2479
2480 if (!spec_types)
2481 return NULL_TREE;
2482
2483 if (spec_types == void_list_node)
2484 return void_list_node;
2485
2486 /* Substitute into the rest of the list. */
2487 new_spec_types =
2488 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2489 TREE_CHAIN (tmpl_types));
2490
2491 /* Add the default argument for this parameter. */
2492 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2493 TREE_VALUE (spec_types),
2494 new_spec_types);
2495 }
2496
2497 /* DECL is an explicit specialization. Replicate default arguments
2498 from the template it specializes. (That way, code like:
2499
2500 template <class T> void f(T = 3);
2501 template <> void f(double);
2502 void g () { f (); }
2503
2504 works, as required.) An alternative approach would be to look up
2505 the correct default arguments at the call-site, but this approach
2506 is consistent with how implicit instantiations are handled. */
2507
2508 static void
2509 copy_default_args_to_explicit_spec (tree decl)
2510 {
2511 tree tmpl;
2512 tree spec_types;
2513 tree tmpl_types;
2514 tree new_spec_types;
2515 tree old_type;
2516 tree new_type;
2517 tree t;
2518 tree object_type = NULL_TREE;
2519 tree in_charge = NULL_TREE;
2520 tree vtt = NULL_TREE;
2521
2522 /* See if there's anything we need to do. */
2523 tmpl = DECL_TI_TEMPLATE (decl);
2524 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2525 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2526 if (TREE_PURPOSE (t))
2527 break;
2528 if (!t)
2529 return;
2530
2531 old_type = TREE_TYPE (decl);
2532 spec_types = TYPE_ARG_TYPES (old_type);
2533
2534 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2535 {
2536 /* Remove the this pointer, but remember the object's type for
2537 CV quals. */
2538 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2539 spec_types = TREE_CHAIN (spec_types);
2540 tmpl_types = TREE_CHAIN (tmpl_types);
2541
2542 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2543 {
2544 /* DECL may contain more parameters than TMPL due to the extra
2545 in-charge parameter in constructors and destructors. */
2546 in_charge = spec_types;
2547 spec_types = TREE_CHAIN (spec_types);
2548 }
2549 if (DECL_HAS_VTT_PARM_P (decl))
2550 {
2551 vtt = spec_types;
2552 spec_types = TREE_CHAIN (spec_types);
2553 }
2554 }
2555
2556 /* Compute the merged default arguments. */
2557 new_spec_types =
2558 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2559
2560 /* Compute the new FUNCTION_TYPE. */
2561 if (object_type)
2562 {
2563 if (vtt)
2564 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2565 TREE_VALUE (vtt),
2566 new_spec_types);
2567
2568 if (in_charge)
2569 /* Put the in-charge parameter back. */
2570 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2571 TREE_VALUE (in_charge),
2572 new_spec_types);
2573
2574 new_type = build_method_type_directly (object_type,
2575 TREE_TYPE (old_type),
2576 new_spec_types);
2577 }
2578 else
2579 new_type = build_function_type (TREE_TYPE (old_type),
2580 new_spec_types);
2581 new_type = cp_build_type_attribute_variant (new_type,
2582 TYPE_ATTRIBUTES (old_type));
2583 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2584
2585 TREE_TYPE (decl) = new_type;
2586 }
2587
2588 /* Return the number of template headers we expect to see for a definition
2589 or specialization of CTYPE or one of its non-template members. */
2590
2591 int
2592 num_template_headers_for_class (tree ctype)
2593 {
2594 int num_templates = 0;
2595
2596 while (ctype && CLASS_TYPE_P (ctype))
2597 {
2598 /* You're supposed to have one `template <...>' for every
2599 template class, but you don't need one for a full
2600 specialization. For example:
2601
2602 template <class T> struct S{};
2603 template <> struct S<int> { void f(); };
2604 void S<int>::f () {}
2605
2606 is correct; there shouldn't be a `template <>' for the
2607 definition of `S<int>::f'. */
2608 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2609 /* If CTYPE does not have template information of any
2610 kind, then it is not a template, nor is it nested
2611 within a template. */
2612 break;
2613 if (explicit_class_specialization_p (ctype))
2614 break;
2615 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2616 ++num_templates;
2617
2618 ctype = TYPE_CONTEXT (ctype);
2619 }
2620
2621 return num_templates;
2622 }
2623
2624 /* Do a simple sanity check on the template headers that precede the
2625 variable declaration DECL. */
2626
2627 void
2628 check_template_variable (tree decl)
2629 {
2630 tree ctx = CP_DECL_CONTEXT (decl);
2631 int wanted = num_template_headers_for_class (ctx);
2632 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2633 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2634 {
2635 if (cxx_dialect < cxx14)
2636 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2637 "variable templates only available with "
2638 "%<-std=c++14%> or %<-std=gnu++14%>");
2639
2640 // Namespace-scope variable templates should have a template header.
2641 ++wanted;
2642 }
2643 if (template_header_count > wanted)
2644 {
2645 auto_diagnostic_group d;
2646 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2647 "too many template headers for %qD "
2648 "(should be %d)",
2649 decl, wanted);
2650 if (warned && CLASS_TYPE_P (ctx)
2651 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2652 inform (DECL_SOURCE_LOCATION (decl),
2653 "members of an explicitly specialized class are defined "
2654 "without a template header");
2655 }
2656 }
2657
2658 /* An explicit specialization whose declarator-id or class-head-name is not
2659 qualified shall be declared in the nearest enclosing namespace of the
2660 template, or, if the namespace is inline (7.3.1), any namespace from its
2661 enclosing namespace set.
2662
2663 If the name declared in the explicit instantiation is an unqualified name,
2664 the explicit instantiation shall appear in the namespace where its template
2665 is declared or, if that namespace is inline (7.3.1), any namespace from its
2666 enclosing namespace set. */
2667
2668 void
2669 check_unqualified_spec_or_inst (tree t, location_t loc)
2670 {
2671 tree tmpl = most_general_template (t);
2672 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2673 && !is_nested_namespace (current_namespace,
2674 CP_DECL_CONTEXT (tmpl), true))
2675 {
2676 if (processing_specialization)
2677 permerror (loc, "explicit specialization of %qD outside its "
2678 "namespace must use a nested-name-specifier", tmpl);
2679 else if (processing_explicit_instantiation
2680 && cxx_dialect >= cxx11)
2681 /* This was allowed in C++98, so only pedwarn. */
2682 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2683 "outside its namespace must use a nested-name-"
2684 "specifier", tmpl);
2685 }
2686 }
2687
2688 /* Warn for a template specialization SPEC that is missing some of a set
2689 of function or type attributes that the template TEMPL is declared with.
2690 ATTRLIST is a list of additional attributes that SPEC should be taken
2691 to ultimately be declared with. */
2692
2693 static void
2694 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2695 {
2696 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2697 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2698
2699 /* Avoid warning if the difference between the primary and
2700 the specialization is not in one of the attributes below. */
2701 const char* const blacklist[] = {
2702 "alloc_align", "alloc_size", "assume_aligned", "format",
2703 "format_arg", "malloc", "nonnull", NULL
2704 };
2705
2706 /* Put together a list of the black listed attributes that the primary
2707 template is declared with that the specialization is not, in case
2708 it's not apparent from the most recent declaration of the primary. */
2709 pretty_printer str;
2710 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2711 blacklist, &str);
2712
2713 if (!nattrs)
2714 return;
2715
2716 auto_diagnostic_group d;
2717 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2718 "explicit specialization %q#D may be missing attributes",
2719 spec))
2720 inform (DECL_SOURCE_LOCATION (tmpl),
2721 nattrs > 1
2722 ? G_("missing primary template attributes %s")
2723 : G_("missing primary template attribute %s"),
2724 pp_formatted_text (&str));
2725 }
2726
2727 /* Check to see if the function just declared, as indicated in
2728 DECLARATOR, and in DECL, is a specialization of a function
2729 template. We may also discover that the declaration is an explicit
2730 instantiation at this point.
2731
2732 Returns DECL, or an equivalent declaration that should be used
2733 instead if all goes well. Issues an error message if something is
2734 amiss. Returns error_mark_node if the error is not easily
2735 recoverable.
2736
2737 FLAGS is a bitmask consisting of the following flags:
2738
2739 2: The function has a definition.
2740 4: The function is a friend.
2741
2742 The TEMPLATE_COUNT is the number of references to qualifying
2743 template classes that appeared in the name of the function. For
2744 example, in
2745
2746 template <class T> struct S { void f(); };
2747 void S<int>::f();
2748
2749 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2750 classes are not counted in the TEMPLATE_COUNT, so that in
2751
2752 template <class T> struct S {};
2753 template <> struct S<int> { void f(); }
2754 template <> void S<int>::f();
2755
2756 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2757 invalid; there should be no template <>.)
2758
2759 If the function is a specialization, it is marked as such via
2760 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2761 is set up correctly, and it is added to the list of specializations
2762 for that template. */
2763
2764 tree
2765 check_explicit_specialization (tree declarator,
2766 tree decl,
2767 int template_count,
2768 int flags,
2769 tree attrlist)
2770 {
2771 int have_def = flags & 2;
2772 int is_friend = flags & 4;
2773 bool is_concept = flags & 8;
2774 int specialization = 0;
2775 int explicit_instantiation = 0;
2776 int member_specialization = 0;
2777 tree ctype = DECL_CLASS_CONTEXT (decl);
2778 tree dname = DECL_NAME (decl);
2779 tmpl_spec_kind tsk;
2780
2781 if (is_friend)
2782 {
2783 if (!processing_specialization)
2784 tsk = tsk_none;
2785 else
2786 tsk = tsk_excessive_parms;
2787 }
2788 else
2789 tsk = current_tmpl_spec_kind (template_count);
2790
2791 switch (tsk)
2792 {
2793 case tsk_none:
2794 if (processing_specialization && !VAR_P (decl))
2795 {
2796 specialization = 1;
2797 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2798 }
2799 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2800 {
2801 if (is_friend)
2802 /* This could be something like:
2803
2804 template <class T> void f(T);
2805 class S { friend void f<>(int); } */
2806 specialization = 1;
2807 else
2808 {
2809 /* This case handles bogus declarations like template <>
2810 template <class T> void f<int>(); */
2811
2812 error_at (cp_expr_loc_or_input_loc (declarator),
2813 "template-id %qE in declaration of primary template",
2814 declarator);
2815 return decl;
2816 }
2817 }
2818 break;
2819
2820 case tsk_invalid_member_spec:
2821 /* The error has already been reported in
2822 check_specialization_scope. */
2823 return error_mark_node;
2824
2825 case tsk_invalid_expl_inst:
2826 error ("template parameter list used in explicit instantiation");
2827
2828 /* Fall through. */
2829
2830 case tsk_expl_inst:
2831 if (have_def)
2832 error ("definition provided for explicit instantiation");
2833
2834 explicit_instantiation = 1;
2835 break;
2836
2837 case tsk_excessive_parms:
2838 case tsk_insufficient_parms:
2839 if (tsk == tsk_excessive_parms)
2840 error ("too many template parameter lists in declaration of %qD",
2841 decl);
2842 else if (template_header_count)
2843 error("too few template parameter lists in declaration of %qD", decl);
2844 else
2845 error("explicit specialization of %qD must be introduced by "
2846 "%<template <>%>", decl);
2847
2848 /* Fall through. */
2849 case tsk_expl_spec:
2850 if (is_concept)
2851 error ("explicit specialization declared %<concept%>");
2852
2853 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2854 /* In cases like template<> constexpr bool v = true;
2855 We'll give an error in check_template_variable. */
2856 break;
2857
2858 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2859 if (ctype)
2860 member_specialization = 1;
2861 else
2862 specialization = 1;
2863 break;
2864
2865 case tsk_template:
2866 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2867 {
2868 /* This case handles bogus declarations like template <>
2869 template <class T> void f<int>(); */
2870
2871 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2872 error_at (cp_expr_loc_or_input_loc (declarator),
2873 "template-id %qE in declaration of primary template",
2874 declarator);
2875 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2876 {
2877 /* Partial specialization of variable template. */
2878 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2879 specialization = 1;
2880 goto ok;
2881 }
2882 else if (cxx_dialect < cxx14)
2883 error_at (cp_expr_loc_or_input_loc (declarator),
2884 "non-type partial specialization %qE "
2885 "is not allowed", declarator);
2886 else
2887 error_at (cp_expr_loc_or_input_loc (declarator),
2888 "non-class, non-variable partial specialization %qE "
2889 "is not allowed", declarator);
2890 return decl;
2891 ok:;
2892 }
2893
2894 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2895 /* This is a specialization of a member template, without
2896 specialization the containing class. Something like:
2897
2898 template <class T> struct S {
2899 template <class U> void f (U);
2900 };
2901 template <> template <class U> void S<int>::f(U) {}
2902
2903 That's a specialization -- but of the entire template. */
2904 specialization = 1;
2905 break;
2906
2907 default:
2908 gcc_unreachable ();
2909 }
2910
2911 if ((specialization || member_specialization)
2912 /* This doesn't apply to variable templates. */
2913 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2914 {
2915 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2916 for (; t; t = TREE_CHAIN (t))
2917 if (TREE_PURPOSE (t))
2918 {
2919 permerror (input_location,
2920 "default argument specified in explicit specialization");
2921 break;
2922 }
2923 }
2924
2925 if (specialization || member_specialization || explicit_instantiation)
2926 {
2927 tree tmpl = NULL_TREE;
2928 tree targs = NULL_TREE;
2929 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2930
2931 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2932 if (!was_template_id)
2933 {
2934 tree fns;
2935
2936 gcc_assert (identifier_p (declarator));
2937 if (ctype)
2938 fns = dname;
2939 else
2940 {
2941 /* If there is no class context, the explicit instantiation
2942 must be at namespace scope. */
2943 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2944
2945 /* Find the namespace binding, using the declaration
2946 context. */
2947 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2948 false, true);
2949 if (fns == error_mark_node)
2950 /* If lookup fails, look for a friend declaration so we can
2951 give a better diagnostic. */
2952 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2953 /*type*/false, /*complain*/true,
2954 /*hidden*/true);
2955
2956 if (fns == error_mark_node || !is_overloaded_fn (fns))
2957 {
2958 error ("%qD is not a template function", dname);
2959 fns = error_mark_node;
2960 }
2961 }
2962
2963 declarator = lookup_template_function (fns, NULL_TREE);
2964 }
2965
2966 if (declarator == error_mark_node)
2967 return error_mark_node;
2968
2969 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2970 {
2971 if (!explicit_instantiation)
2972 /* A specialization in class scope. This is invalid,
2973 but the error will already have been flagged by
2974 check_specialization_scope. */
2975 return error_mark_node;
2976 else
2977 {
2978 /* It's not valid to write an explicit instantiation in
2979 class scope, e.g.:
2980
2981 class C { template void f(); }
2982
2983 This case is caught by the parser. However, on
2984 something like:
2985
2986 template class C { void f(); };
2987
2988 (which is invalid) we can get here. The error will be
2989 issued later. */
2990 ;
2991 }
2992
2993 return decl;
2994 }
2995 else if (ctype != NULL_TREE
2996 && (identifier_p (TREE_OPERAND (declarator, 0))))
2997 {
2998 // We'll match variable templates in start_decl.
2999 if (VAR_P (decl))
3000 return decl;
3001
3002 /* Find the list of functions in ctype that have the same
3003 name as the declared function. */
3004 tree name = TREE_OPERAND (declarator, 0);
3005
3006 if (constructor_name_p (name, ctype))
3007 {
3008 if (DECL_CONSTRUCTOR_P (decl)
3009 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3010 : !CLASSTYPE_DESTRUCTOR (ctype))
3011 {
3012 /* From [temp.expl.spec]:
3013
3014 If such an explicit specialization for the member
3015 of a class template names an implicitly-declared
3016 special member function (clause _special_), the
3017 program is ill-formed.
3018
3019 Similar language is found in [temp.explicit]. */
3020 error ("specialization of implicitly-declared special member function");
3021 return error_mark_node;
3022 }
3023
3024 name = DECL_NAME (decl);
3025 }
3026
3027 /* For a type-conversion operator, We might be looking for
3028 `operator int' which will be a specialization of
3029 `operator T'. Grab all the conversion operators, and
3030 then select from them. */
3031 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3032 ? conv_op_identifier : name);
3033
3034 if (fns == NULL_TREE)
3035 {
3036 error ("no member function %qD declared in %qT", name, ctype);
3037 return error_mark_node;
3038 }
3039 else
3040 TREE_OPERAND (declarator, 0) = fns;
3041 }
3042
3043 /* Figure out what exactly is being specialized at this point.
3044 Note that for an explicit instantiation, even one for a
3045 member function, we cannot tell a priori whether the
3046 instantiation is for a member template, or just a member
3047 function of a template class. Even if a member template is
3048 being instantiated, the member template arguments may be
3049 elided if they can be deduced from the rest of the
3050 declaration. */
3051 tmpl = determine_specialization (declarator, decl,
3052 &targs,
3053 member_specialization,
3054 template_count,
3055 tsk);
3056
3057 if (!tmpl || tmpl == error_mark_node)
3058 /* We couldn't figure out what this declaration was
3059 specializing. */
3060 return error_mark_node;
3061 else
3062 {
3063 if (TREE_CODE (decl) == FUNCTION_DECL
3064 && DECL_HIDDEN_FRIEND_P (tmpl))
3065 {
3066 auto_diagnostic_group d;
3067 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3068 "friend declaration %qD is not visible to "
3069 "explicit specialization", tmpl))
3070 inform (DECL_SOURCE_LOCATION (tmpl),
3071 "friend declaration here");
3072 }
3073 else if (!ctype && !is_friend
3074 && CP_DECL_CONTEXT (decl) == current_namespace)
3075 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3076
3077 tree gen_tmpl = most_general_template (tmpl);
3078
3079 if (explicit_instantiation)
3080 {
3081 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3082 is done by do_decl_instantiation later. */
3083
3084 int arg_depth = TMPL_ARGS_DEPTH (targs);
3085 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3086
3087 if (arg_depth > parm_depth)
3088 {
3089 /* If TMPL is not the most general template (for
3090 example, if TMPL is a friend template that is
3091 injected into namespace scope), then there will
3092 be too many levels of TARGS. Remove some of them
3093 here. */
3094 int i;
3095 tree new_targs;
3096
3097 new_targs = make_tree_vec (parm_depth);
3098 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3099 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3100 = TREE_VEC_ELT (targs, i);
3101 targs = new_targs;
3102 }
3103
3104 return instantiate_template (tmpl, targs, tf_error);
3105 }
3106
3107 /* If we thought that the DECL was a member function, but it
3108 turns out to be specializing a static member function,
3109 make DECL a static member function as well. */
3110 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3111 && DECL_STATIC_FUNCTION_P (tmpl)
3112 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3113 revert_static_member_fn (decl);
3114
3115 /* If this is a specialization of a member template of a
3116 template class, we want to return the TEMPLATE_DECL, not
3117 the specialization of it. */
3118 if (tsk == tsk_template && !was_template_id)
3119 {
3120 tree result = DECL_TEMPLATE_RESULT (tmpl);
3121 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3122 DECL_INITIAL (result) = NULL_TREE;
3123 if (have_def)
3124 {
3125 tree parm;
3126 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3127 DECL_SOURCE_LOCATION (result)
3128 = DECL_SOURCE_LOCATION (decl);
3129 /* We want to use the argument list specified in the
3130 definition, not in the original declaration. */
3131 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3132 for (parm = DECL_ARGUMENTS (result); parm;
3133 parm = DECL_CHAIN (parm))
3134 DECL_CONTEXT (parm) = result;
3135 }
3136 return register_specialization (tmpl, gen_tmpl, targs,
3137 is_friend, 0);
3138 }
3139
3140 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3141 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3142
3143 if (was_template_id)
3144 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3145
3146 /* Inherit default function arguments from the template
3147 DECL is specializing. */
3148 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3149 copy_default_args_to_explicit_spec (decl);
3150
3151 /* This specialization has the same protection as the
3152 template it specializes. */
3153 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3154 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3155
3156 /* 7.1.1-1 [dcl.stc]
3157
3158 A storage-class-specifier shall not be specified in an
3159 explicit specialization...
3160
3161 The parser rejects these, so unless action is taken here,
3162 explicit function specializations will always appear with
3163 global linkage.
3164
3165 The action recommended by the C++ CWG in response to C++
3166 defect report 605 is to make the storage class and linkage
3167 of the explicit specialization match the templated function:
3168
3169 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3170 */
3171 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3172 {
3173 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3174 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3175
3176 /* A concept cannot be specialized. */
3177 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3178 {
3179 error ("explicit specialization of function concept %qD",
3180 gen_tmpl);
3181 return error_mark_node;
3182 }
3183
3184 /* This specialization has the same linkage and visibility as
3185 the function template it specializes. */
3186 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3187 if (! TREE_PUBLIC (decl))
3188 {
3189 DECL_INTERFACE_KNOWN (decl) = 1;
3190 DECL_NOT_REALLY_EXTERN (decl) = 1;
3191 }
3192 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3193 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3194 {
3195 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3196 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3197 }
3198 }
3199
3200 /* If DECL is a friend declaration, declared using an
3201 unqualified name, the namespace associated with DECL may
3202 have been set incorrectly. For example, in:
3203
3204 template <typename T> void f(T);
3205 namespace N {
3206 struct S { friend void f<int>(int); }
3207 }
3208
3209 we will have set the DECL_CONTEXT for the friend
3210 declaration to N, rather than to the global namespace. */
3211 if (DECL_NAMESPACE_SCOPE_P (decl))
3212 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3213
3214 if (is_friend && !have_def)
3215 /* This is not really a declaration of a specialization.
3216 It's just the name of an instantiation. But, it's not
3217 a request for an instantiation, either. */
3218 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3219 else if (TREE_CODE (decl) == FUNCTION_DECL)
3220 /* A specialization is not necessarily COMDAT. */
3221 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3222 && DECL_DECLARED_INLINE_P (decl));
3223 else if (VAR_P (decl))
3224 DECL_COMDAT (decl) = false;
3225
3226 /* If this is a full specialization, register it so that we can find
3227 it again. Partial specializations will be registered in
3228 process_partial_specialization. */
3229 if (!processing_template_decl)
3230 {
3231 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3232
3233 decl = register_specialization (decl, gen_tmpl, targs,
3234 is_friend, 0);
3235 }
3236
3237
3238 /* A 'structor should already have clones. */
3239 gcc_assert (decl == error_mark_node
3240 || variable_template_p (tmpl)
3241 || !(DECL_CONSTRUCTOR_P (decl)
3242 || DECL_DESTRUCTOR_P (decl))
3243 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3244 }
3245 }
3246
3247 return decl;
3248 }
3249
3250 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3251 parameters. These are represented in the same format used for
3252 DECL_TEMPLATE_PARMS. */
3253
3254 int
3255 comp_template_parms (const_tree parms1, const_tree parms2)
3256 {
3257 const_tree p1;
3258 const_tree p2;
3259
3260 if (parms1 == parms2)
3261 return 1;
3262
3263 for (p1 = parms1, p2 = parms2;
3264 p1 != NULL_TREE && p2 != NULL_TREE;
3265 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3266 {
3267 tree t1 = TREE_VALUE (p1);
3268 tree t2 = TREE_VALUE (p2);
3269 int i;
3270
3271 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3272 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3273
3274 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3275 return 0;
3276
3277 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3278 {
3279 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3280 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3281
3282 /* If either of the template parameters are invalid, assume
3283 they match for the sake of error recovery. */
3284 if (error_operand_p (parm1) || error_operand_p (parm2))
3285 return 1;
3286
3287 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3288 return 0;
3289
3290 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3291 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3292 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3293 continue;
3294 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3295 return 0;
3296 }
3297 }
3298
3299 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3300 /* One set of parameters has more parameters lists than the
3301 other. */
3302 return 0;
3303
3304 return 1;
3305 }
3306
3307 /* Returns true if two template parameters are declared with
3308 equivalent constraints. */
3309
3310 static bool
3311 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3312 {
3313 tree req1 = TREE_TYPE (parm1);
3314 tree req2 = TREE_TYPE (parm2);
3315 if (!req1 != !req2)
3316 return false;
3317 if (req1)
3318 return cp_tree_equal (req1, req2);
3319 return true;
3320 }
3321
3322 /* Returns true when two template parameters are equivalent. */
3323
3324 static bool
3325 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3326 {
3327 tree decl1 = TREE_VALUE (parm1);
3328 tree decl2 = TREE_VALUE (parm2);
3329
3330 /* If either of the template parameters are invalid, assume
3331 they match for the sake of error recovery. */
3332 if (error_operand_p (decl1) || error_operand_p (decl2))
3333 return true;
3334
3335 /* ... they declare parameters of the same kind. */
3336 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3337 return false;
3338
3339 /* ... one parameter was introduced by a parameter declaration, then
3340 both are. This case arises as a result of eagerly rewriting declarations
3341 during parsing. */
3342 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3343 return false;
3344
3345 /* ... if either declares a pack, they both do. */
3346 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3347 return false;
3348
3349 if (TREE_CODE (decl1) == PARM_DECL)
3350 {
3351 /* ... if they declare non-type parameters, the types are equivalent. */
3352 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3353 return false;
3354 }
3355 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3356 {
3357 /* ... if they declare template template parameters, their template
3358 parameter lists are equivalent. */
3359 if (!template_heads_equivalent_p (decl1, decl2))
3360 return false;
3361 }
3362
3363 /* ... if they are declared with a qualified-concept name, they both
3364 are, and those names are equivalent. */
3365 return template_parameter_constraints_equivalent_p (parm1, parm2);
3366 }
3367
3368 /* Returns true if two template parameters lists are equivalent.
3369 Two template parameter lists are equivalent if they have the
3370 same length and their corresponding parameters are equivalent.
3371
3372 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3373 data structure returned by DECL_TEMPLATE_PARMS.
3374
3375 This is generally the same implementation as comp_template_parms
3376 except that it also the concept names and arguments used to
3377 introduce parameters. */
3378
3379 static bool
3380 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3381 {
3382 if (parms1 == parms2)
3383 return true;
3384
3385 const_tree p1 = parms1;
3386 const_tree p2 = parms2;
3387 while (p1 != NULL_TREE && p2 != NULL_TREE)
3388 {
3389 tree list1 = TREE_VALUE (p1);
3390 tree list2 = TREE_VALUE (p2);
3391
3392 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3393 return 0;
3394
3395 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3396 {
3397 tree parm1 = TREE_VEC_ELT (list1, i);
3398 tree parm2 = TREE_VEC_ELT (list2, i);
3399 if (!template_parameters_equivalent_p (parm1, parm2))
3400 return false;
3401 }
3402
3403 p1 = TREE_CHAIN (p1);
3404 p2 = TREE_CHAIN (p2);
3405 }
3406
3407 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3408 return false;
3409
3410 return true;
3411 }
3412
3413 /* Return true if the requires-clause of the template parameter lists are
3414 equivalent and false otherwise. */
3415 static bool
3416 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3417 {
3418 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3419 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3420 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3421 return false;
3422 if (!cp_tree_equal (req1, req2))
3423 return false;
3424 return true;
3425 }
3426
3427 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3428 Two template heads are equivalent if their template parameter
3429 lists are equivalent and their requires clauses are equivalent.
3430
3431 In pre-C++20, this is equivalent to calling comp_template_parms
3432 for the template parameters of TMPL1 and TMPL2. */
3433
3434 bool
3435 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3436 {
3437 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3438 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3439
3440 /* Don't change the matching rules for pre-C++20. */
3441 if (cxx_dialect < cxx2a)
3442 return comp_template_parms (parms1, parms2);
3443
3444 /* ... have the same number of template parameters, and their
3445 corresponding parameters are equivalent. */
3446 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3447 return false;
3448
3449 /* ... if either has a requires-clause, they both do and their
3450 corresponding constraint-expressions are equivalent. */
3451 return template_requirements_equivalent_p (parms1, parms2);
3452 }
3453
3454 /* Determine whether PARM is a parameter pack. */
3455
3456 bool
3457 template_parameter_pack_p (const_tree parm)
3458 {
3459 /* Determine if we have a non-type template parameter pack. */
3460 if (TREE_CODE (parm) == PARM_DECL)
3461 return (DECL_TEMPLATE_PARM_P (parm)
3462 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3463 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3464 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3465
3466 /* If this is a list of template parameters, we could get a
3467 TYPE_DECL or a TEMPLATE_DECL. */
3468 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3469 parm = TREE_TYPE (parm);
3470
3471 /* Otherwise it must be a type template parameter. */
3472 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3473 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3474 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3475 }
3476
3477 /* Determine if T is a function parameter pack. */
3478
3479 bool
3480 function_parameter_pack_p (const_tree t)
3481 {
3482 if (t && TREE_CODE (t) == PARM_DECL)
3483 return DECL_PACK_P (t);
3484 return false;
3485 }
3486
3487 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3488 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3489
3490 tree
3491 get_function_template_decl (const_tree primary_func_tmpl_inst)
3492 {
3493 if (! primary_func_tmpl_inst
3494 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3495 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3496 return NULL;
3497
3498 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3499 }
3500
3501 /* Return true iff the function parameter PARAM_DECL was expanded
3502 from the function parameter pack PACK. */
3503
3504 bool
3505 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3506 {
3507 if (DECL_ARTIFICIAL (param_decl)
3508 || !function_parameter_pack_p (pack))
3509 return false;
3510
3511 /* The parameter pack and its pack arguments have the same
3512 DECL_PARM_INDEX. */
3513 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3514 }
3515
3516 /* Determine whether ARGS describes a variadic template args list,
3517 i.e., one that is terminated by a template argument pack. */
3518
3519 static bool
3520 template_args_variadic_p (tree args)
3521 {
3522 int nargs;
3523 tree last_parm;
3524
3525 if (args == NULL_TREE)
3526 return false;
3527
3528 args = INNERMOST_TEMPLATE_ARGS (args);
3529 nargs = TREE_VEC_LENGTH (args);
3530
3531 if (nargs == 0)
3532 return false;
3533
3534 last_parm = TREE_VEC_ELT (args, nargs - 1);
3535
3536 return ARGUMENT_PACK_P (last_parm);
3537 }
3538
3539 /* Generate a new name for the parameter pack name NAME (an
3540 IDENTIFIER_NODE) that incorporates its */
3541
3542 static tree
3543 make_ith_pack_parameter_name (tree name, int i)
3544 {
3545 /* Munge the name to include the parameter index. */
3546 #define NUMBUF_LEN 128
3547 char numbuf[NUMBUF_LEN];
3548 char* newname;
3549 int newname_len;
3550
3551 if (name == NULL_TREE)
3552 return name;
3553 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3554 newname_len = IDENTIFIER_LENGTH (name)
3555 + strlen (numbuf) + 2;
3556 newname = (char*)alloca (newname_len);
3557 snprintf (newname, newname_len,
3558 "%s#%i", IDENTIFIER_POINTER (name), i);
3559 return get_identifier (newname);
3560 }
3561
3562 /* Return true if T is a primary function, class or alias template
3563 specialization, not including the template pattern. */
3564
3565 bool
3566 primary_template_specialization_p (const_tree t)
3567 {
3568 if (!t)
3569 return false;
3570
3571 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3572 return (DECL_LANG_SPECIFIC (t)
3573 && DECL_USE_TEMPLATE (t)
3574 && DECL_TEMPLATE_INFO (t)
3575 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3576 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3577 return (CLASSTYPE_TEMPLATE_INFO (t)
3578 && CLASSTYPE_USE_TEMPLATE (t)
3579 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3580 else if (alias_template_specialization_p (t, nt_transparent))
3581 return true;
3582 return false;
3583 }
3584
3585 /* Return true if PARM is a template template parameter. */
3586
3587 bool
3588 template_template_parameter_p (const_tree parm)
3589 {
3590 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3591 }
3592
3593 /* Return true iff PARM is a DECL representing a type template
3594 parameter. */
3595
3596 bool
3597 template_type_parameter_p (const_tree parm)
3598 {
3599 return (parm
3600 && (TREE_CODE (parm) == TYPE_DECL
3601 || TREE_CODE (parm) == TEMPLATE_DECL)
3602 && DECL_TEMPLATE_PARM_P (parm));
3603 }
3604
3605 /* Return the template parameters of T if T is a
3606 primary template instantiation, NULL otherwise. */
3607
3608 tree
3609 get_primary_template_innermost_parameters (const_tree t)
3610 {
3611 tree parms = NULL, template_info = NULL;
3612
3613 if ((template_info = get_template_info (t))
3614 && primary_template_specialization_p (t))
3615 parms = INNERMOST_TEMPLATE_PARMS
3616 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3617
3618 return parms;
3619 }
3620
3621 /* Return the template parameters of the LEVELth level from the full list
3622 of template parameters PARMS. */
3623
3624 tree
3625 get_template_parms_at_level (tree parms, int level)
3626 {
3627 tree p;
3628 if (!parms
3629 || TREE_CODE (parms) != TREE_LIST
3630 || level > TMPL_PARMS_DEPTH (parms))
3631 return NULL_TREE;
3632
3633 for (p = parms; p; p = TREE_CHAIN (p))
3634 if (TMPL_PARMS_DEPTH (p) == level)
3635 return p;
3636
3637 return NULL_TREE;
3638 }
3639
3640 /* Returns the template arguments of T if T is a template instantiation,
3641 NULL otherwise. */
3642
3643 tree
3644 get_template_innermost_arguments (const_tree t)
3645 {
3646 tree args = NULL, template_info = NULL;
3647
3648 if ((template_info = get_template_info (t))
3649 && TI_ARGS (template_info))
3650 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3651
3652 return args;
3653 }
3654
3655 /* Return the argument pack elements of T if T is a template argument pack,
3656 NULL otherwise. */
3657
3658 tree
3659 get_template_argument_pack_elems (const_tree t)
3660 {
3661 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3662 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3663 return NULL;
3664
3665 return ARGUMENT_PACK_ARGS (t);
3666 }
3667
3668 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3669 ARGUMENT_PACK_SELECT represents. */
3670
3671 static tree
3672 argument_pack_select_arg (tree t)
3673 {
3674 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3675 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3676
3677 /* If the selected argument is an expansion E, that most likely means we were
3678 called from gen_elem_of_pack_expansion_instantiation during the
3679 substituting of an argument pack (of which the Ith element is a pack
3680 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3681 In this case, the Ith element resulting from this substituting is going to
3682 be a pack expansion, which pattern is the pattern of E. Let's return the
3683 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3684 resulting pack expansion from it. */
3685 if (PACK_EXPANSION_P (arg))
3686 {
3687 /* Make sure we aren't throwing away arg info. */
3688 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3689 arg = PACK_EXPANSION_PATTERN (arg);
3690 }
3691
3692 return arg;
3693 }
3694
3695
3696 /* True iff FN is a function representing a built-in variadic parameter
3697 pack. */
3698
3699 bool
3700 builtin_pack_fn_p (tree fn)
3701 {
3702 if (!fn
3703 || TREE_CODE (fn) != FUNCTION_DECL
3704 || !DECL_IS_BUILTIN (fn))
3705 return false;
3706
3707 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3708 return true;
3709
3710 return false;
3711 }
3712
3713 /* True iff CALL is a call to a function representing a built-in variadic
3714 parameter pack. */
3715
3716 static bool
3717 builtin_pack_call_p (tree call)
3718 {
3719 if (TREE_CODE (call) != CALL_EXPR)
3720 return false;
3721 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3722 }
3723
3724 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3725
3726 static tree
3727 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3728 tree in_decl)
3729 {
3730 tree ohi = CALL_EXPR_ARG (call, 0);
3731 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3732 false/*fn*/, true/*int_cst*/);
3733
3734 if (value_dependent_expression_p (hi))
3735 {
3736 if (hi != ohi)
3737 {
3738 call = copy_node (call);
3739 CALL_EXPR_ARG (call, 0) = hi;
3740 }
3741 tree ex = make_pack_expansion (call, complain);
3742 tree vec = make_tree_vec (1);
3743 TREE_VEC_ELT (vec, 0) = ex;
3744 return vec;
3745 }
3746 else
3747 {
3748 hi = cxx_constant_value (hi);
3749 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3750
3751 /* Calculate the largest value of len that won't make the size of the vec
3752 overflow an int. The compiler will exceed resource limits long before
3753 this, but it seems a decent place to diagnose. */
3754 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3755
3756 if (len < 0 || len > max)
3757 {
3758 if ((complain & tf_error)
3759 && hi != error_mark_node)
3760 error ("argument to %<__integer_pack%> must be between 0 and %d",
3761 max);
3762 return error_mark_node;
3763 }
3764
3765 tree vec = make_tree_vec (len);
3766
3767 for (int i = 0; i < len; ++i)
3768 TREE_VEC_ELT (vec, i) = size_int (i);
3769
3770 return vec;
3771 }
3772 }
3773
3774 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3775 CALL. */
3776
3777 static tree
3778 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3779 tree in_decl)
3780 {
3781 if (!builtin_pack_call_p (call))
3782 return NULL_TREE;
3783
3784 tree fn = CALL_EXPR_FN (call);
3785
3786 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3787 return expand_integer_pack (call, args, complain, in_decl);
3788
3789 return NULL_TREE;
3790 }
3791
3792 /* Structure used to track the progress of find_parameter_packs_r. */
3793 struct find_parameter_pack_data
3794 {
3795 /* TREE_LIST that will contain all of the parameter packs found by
3796 the traversal. */
3797 tree* parameter_packs;
3798
3799 /* Set of AST nodes that have been visited by the traversal. */
3800 hash_set<tree> *visited;
3801
3802 /* True iff we're making a type pack expansion. */
3803 bool type_pack_expansion_p;
3804 };
3805
3806 /* Identifies all of the argument packs that occur in a template
3807 argument and appends them to the TREE_LIST inside DATA, which is a
3808 find_parameter_pack_data structure. This is a subroutine of
3809 make_pack_expansion and uses_parameter_packs. */
3810 static tree
3811 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3812 {
3813 tree t = *tp;
3814 struct find_parameter_pack_data* ppd =
3815 (struct find_parameter_pack_data*)data;
3816 bool parameter_pack_p = false;
3817
3818 /* Handle type aliases/typedefs. */
3819 if (TYPE_ALIAS_P (t))
3820 {
3821 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3822 cp_walk_tree (&TI_ARGS (tinfo),
3823 &find_parameter_packs_r,
3824 ppd, ppd->visited);
3825 *walk_subtrees = 0;
3826 return NULL_TREE;
3827 }
3828
3829 /* Identify whether this is a parameter pack or not. */
3830 switch (TREE_CODE (t))
3831 {
3832 case TEMPLATE_PARM_INDEX:
3833 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3834 parameter_pack_p = true;
3835 break;
3836
3837 case TEMPLATE_TYPE_PARM:
3838 t = TYPE_MAIN_VARIANT (t);
3839 /* FALLTHRU */
3840 case TEMPLATE_TEMPLATE_PARM:
3841 /* If the placeholder appears in the decl-specifier-seq of a function
3842 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3843 is a pack expansion, the invented template parameter is a template
3844 parameter pack. */
3845 if (ppd->type_pack_expansion_p && is_auto (t))
3846 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3847 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3848 parameter_pack_p = true;
3849 break;
3850
3851 case FIELD_DECL:
3852 case PARM_DECL:
3853 if (DECL_PACK_P (t))
3854 {
3855 /* We don't want to walk into the type of a PARM_DECL,
3856 because we don't want to see the type parameter pack. */
3857 *walk_subtrees = 0;
3858 parameter_pack_p = true;
3859 }
3860 break;
3861
3862 case VAR_DECL:
3863 if (DECL_PACK_P (t))
3864 {
3865 /* We don't want to walk into the type of a variadic capture proxy,
3866 because we don't want to see the type parameter pack. */
3867 *walk_subtrees = 0;
3868 parameter_pack_p = true;
3869 }
3870 else if (variable_template_specialization_p (t))
3871 {
3872 cp_walk_tree (&DECL_TI_ARGS (t),
3873 find_parameter_packs_r,
3874 ppd, ppd->visited);
3875 *walk_subtrees = 0;
3876 }
3877 break;
3878
3879 case CALL_EXPR:
3880 if (builtin_pack_call_p (t))
3881 parameter_pack_p = true;
3882 break;
3883
3884 case BASES:
3885 parameter_pack_p = true;
3886 break;
3887 default:
3888 /* Not a parameter pack. */
3889 break;
3890 }
3891
3892 if (parameter_pack_p)
3893 {
3894 /* Add this parameter pack to the list. */
3895 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3896 }
3897
3898 if (TYPE_P (t))
3899 cp_walk_tree (&TYPE_CONTEXT (t),
3900 &find_parameter_packs_r, ppd, ppd->visited);
3901
3902 /* This switch statement will return immediately if we don't find a
3903 parameter pack. */
3904 switch (TREE_CODE (t))
3905 {
3906 case TEMPLATE_PARM_INDEX:
3907 return NULL_TREE;
3908
3909 case BOUND_TEMPLATE_TEMPLATE_PARM:
3910 /* Check the template itself. */
3911 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3912 &find_parameter_packs_r, ppd, ppd->visited);
3913 /* Check the template arguments. */
3914 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3915 ppd->visited);
3916 *walk_subtrees = 0;
3917 return NULL_TREE;
3918
3919 case TEMPLATE_TYPE_PARM:
3920 case TEMPLATE_TEMPLATE_PARM:
3921 return NULL_TREE;
3922
3923 case PARM_DECL:
3924 return NULL_TREE;
3925
3926 case DECL_EXPR:
3927 /* Ignore the declaration of a capture proxy for a parameter pack. */
3928 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3929 *walk_subtrees = 0;
3930 return NULL_TREE;
3931
3932 case RECORD_TYPE:
3933 if (TYPE_PTRMEMFUNC_P (t))
3934 return NULL_TREE;
3935 /* Fall through. */
3936
3937 case UNION_TYPE:
3938 case ENUMERAL_TYPE:
3939 if (TYPE_TEMPLATE_INFO (t))
3940 cp_walk_tree (&TYPE_TI_ARGS (t),
3941 &find_parameter_packs_r, ppd, ppd->visited);
3942
3943 *walk_subtrees = 0;
3944 return NULL_TREE;
3945
3946 case TEMPLATE_DECL:
3947 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3948 return NULL_TREE;
3949 gcc_fallthrough();
3950
3951 case CONSTRUCTOR:
3952 cp_walk_tree (&TREE_TYPE (t),
3953 &find_parameter_packs_r, ppd, ppd->visited);
3954 return NULL_TREE;
3955
3956 case TYPENAME_TYPE:
3957 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3958 ppd, ppd->visited);
3959 *walk_subtrees = 0;
3960 return NULL_TREE;
3961
3962 case TYPE_PACK_EXPANSION:
3963 case EXPR_PACK_EXPANSION:
3964 *walk_subtrees = 0;
3965 return NULL_TREE;
3966
3967 case INTEGER_TYPE:
3968 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3969 ppd, ppd->visited);
3970 *walk_subtrees = 0;
3971 return NULL_TREE;
3972
3973 case IDENTIFIER_NODE:
3974 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3975 ppd->visited);
3976 *walk_subtrees = 0;
3977 return NULL_TREE;
3978
3979 case LAMBDA_EXPR:
3980 {
3981 /* Look at explicit captures. */
3982 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3983 cap; cap = TREE_CHAIN (cap))
3984 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3985 ppd->visited);
3986 /* Since we defer implicit capture, look in the parms and body. */
3987 tree fn = lambda_function (t);
3988 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3989 ppd->visited);
3990 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3991 ppd->visited);
3992 *walk_subtrees = 0;
3993 return NULL_TREE;
3994 }
3995
3996 case DECLTYPE_TYPE:
3997 {
3998 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3999 type_pack_expansion_p to false so that any placeholders
4000 within the expression don't get marked as parameter packs. */
4001 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4002 ppd->type_pack_expansion_p = false;
4003 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4004 ppd, ppd->visited);
4005 ppd->type_pack_expansion_p = type_pack_expansion_p;
4006 *walk_subtrees = 0;
4007 return NULL_TREE;
4008 }
4009
4010 case IF_STMT:
4011 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4012 ppd, ppd->visited);
4013 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4014 ppd, ppd->visited);
4015 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4016 ppd, ppd->visited);
4017 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4018 *walk_subtrees = 0;
4019 return NULL_TREE;
4020
4021 default:
4022 return NULL_TREE;
4023 }
4024
4025 return NULL_TREE;
4026 }
4027
4028 /* Determines if the expression or type T uses any parameter packs. */
4029 tree
4030 uses_parameter_packs (tree t)
4031 {
4032 tree parameter_packs = NULL_TREE;
4033 struct find_parameter_pack_data ppd;
4034 ppd.parameter_packs = &parameter_packs;
4035 ppd.visited = new hash_set<tree>;
4036 ppd.type_pack_expansion_p = false;
4037 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4038 delete ppd.visited;
4039 return parameter_packs;
4040 }
4041
4042 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4043 representation a base-class initializer into a parameter pack
4044 expansion. If all goes well, the resulting node will be an
4045 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4046 respectively. */
4047 tree
4048 make_pack_expansion (tree arg, tsubst_flags_t complain)
4049 {
4050 tree result;
4051 tree parameter_packs = NULL_TREE;
4052 bool for_types = false;
4053 struct find_parameter_pack_data ppd;
4054
4055 if (!arg || arg == error_mark_node)
4056 return arg;
4057
4058 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4059 {
4060 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4061 class initializer. In this case, the TREE_PURPOSE will be a
4062 _TYPE node (representing the base class expansion we're
4063 initializing) and the TREE_VALUE will be a TREE_LIST
4064 containing the initialization arguments.
4065
4066 The resulting expansion looks somewhat different from most
4067 expansions. Rather than returning just one _EXPANSION, we
4068 return a TREE_LIST whose TREE_PURPOSE is a
4069 TYPE_PACK_EXPANSION containing the bases that will be
4070 initialized. The TREE_VALUE will be identical to the
4071 original TREE_VALUE, which is a list of arguments that will
4072 be passed to each base. We do not introduce any new pack
4073 expansion nodes into the TREE_VALUE (although it is possible
4074 that some already exist), because the TREE_PURPOSE and
4075 TREE_VALUE all need to be expanded together with the same
4076 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4077 resulting TREE_PURPOSE will mention the parameter packs in
4078 both the bases and the arguments to the bases. */
4079 tree purpose;
4080 tree value;
4081 tree parameter_packs = NULL_TREE;
4082
4083 /* Determine which parameter packs will be used by the base
4084 class expansion. */
4085 ppd.visited = new hash_set<tree>;
4086 ppd.parameter_packs = &parameter_packs;
4087 ppd.type_pack_expansion_p = false;
4088 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4089 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4090 &ppd, ppd.visited);
4091
4092 if (parameter_packs == NULL_TREE)
4093 {
4094 if (complain & tf_error)
4095 error ("base initializer expansion %qT contains no parameter packs",
4096 arg);
4097 delete ppd.visited;
4098 return error_mark_node;
4099 }
4100
4101 if (TREE_VALUE (arg) != void_type_node)
4102 {
4103 /* Collect the sets of parameter packs used in each of the
4104 initialization arguments. */
4105 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4106 {
4107 /* Determine which parameter packs will be expanded in this
4108 argument. */
4109 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4110 &ppd, ppd.visited);
4111 }
4112 }
4113
4114 delete ppd.visited;
4115
4116 /* Create the pack expansion type for the base type. */
4117 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4118 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4119 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4120 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4121
4122 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4123 they will rarely be compared to anything. */
4124 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4125
4126 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4127 }
4128
4129 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4130 for_types = true;
4131
4132 /* Build the PACK_EXPANSION_* node. */
4133 result = for_types
4134 ? cxx_make_type (TYPE_PACK_EXPANSION)
4135 : make_node (EXPR_PACK_EXPANSION);
4136 SET_PACK_EXPANSION_PATTERN (result, arg);
4137 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4138 {
4139 /* Propagate type and const-expression information. */
4140 TREE_TYPE (result) = TREE_TYPE (arg);
4141 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4142 /* Mark this read now, since the expansion might be length 0. */
4143 mark_exp_read (arg);
4144 }
4145 else
4146 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4147 they will rarely be compared to anything. */
4148 SET_TYPE_STRUCTURAL_EQUALITY (result);
4149
4150 /* Determine which parameter packs will be expanded. */
4151 ppd.parameter_packs = &parameter_packs;
4152 ppd.visited = new hash_set<tree>;
4153 ppd.type_pack_expansion_p = TYPE_P (arg);
4154 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4155 delete ppd.visited;
4156
4157 /* Make sure we found some parameter packs. */
4158 if (parameter_packs == NULL_TREE)
4159 {
4160 if (complain & tf_error)
4161 {
4162 if (TYPE_P (arg))
4163 error ("expansion pattern %qT contains no parameter packs", arg);
4164 else
4165 error ("expansion pattern %qE contains no parameter packs", arg);
4166 }
4167 return error_mark_node;
4168 }
4169 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4170
4171 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4172
4173 return result;
4174 }
4175
4176 /* Checks T for any "bare" parameter packs, which have not yet been
4177 expanded, and issues an error if any are found. This operation can
4178 only be done on full expressions or types (e.g., an expression
4179 statement, "if" condition, etc.), because we could have expressions like:
4180
4181 foo(f(g(h(args)))...)
4182
4183 where "args" is a parameter pack. check_for_bare_parameter_packs
4184 should not be called for the subexpressions args, h(args),
4185 g(h(args)), or f(g(h(args))), because we would produce erroneous
4186 error messages.
4187
4188 Returns TRUE and emits an error if there were bare parameter packs,
4189 returns FALSE otherwise. */
4190 bool
4191 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4192 {
4193 tree parameter_packs = NULL_TREE;
4194 struct find_parameter_pack_data ppd;
4195
4196 if (!processing_template_decl || !t || t == error_mark_node)
4197 return false;
4198
4199 /* A lambda might use a parameter pack from the containing context. */
4200 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4201 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4202 return false;
4203
4204 if (TREE_CODE (t) == TYPE_DECL)
4205 t = TREE_TYPE (t);
4206
4207 ppd.parameter_packs = &parameter_packs;
4208 ppd.visited = new hash_set<tree>;
4209 ppd.type_pack_expansion_p = false;
4210 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4211 delete ppd.visited;
4212
4213 if (parameter_packs)
4214 {
4215 if (loc == UNKNOWN_LOCATION)
4216 loc = cp_expr_loc_or_input_loc (t);
4217 error_at (loc, "parameter packs not expanded with %<...%>:");
4218 while (parameter_packs)
4219 {
4220 tree pack = TREE_VALUE (parameter_packs);
4221 tree name = NULL_TREE;
4222
4223 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4224 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4225 name = TYPE_NAME (pack);
4226 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4227 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4228 else if (TREE_CODE (pack) == CALL_EXPR)
4229 name = DECL_NAME (CALL_EXPR_FN (pack));
4230 else
4231 name = DECL_NAME (pack);
4232
4233 if (name)
4234 inform (loc, " %qD", name);
4235 else
4236 inform (loc, " %s", "<anonymous>");
4237
4238 parameter_packs = TREE_CHAIN (parameter_packs);
4239 }
4240
4241 return true;
4242 }
4243
4244 return false;
4245 }
4246
4247 /* Expand any parameter packs that occur in the template arguments in
4248 ARGS. */
4249 tree
4250 expand_template_argument_pack (tree args)
4251 {
4252 if (args == error_mark_node)
4253 return error_mark_node;
4254
4255 tree result_args = NULL_TREE;
4256 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4257 int num_result_args = -1;
4258 int non_default_args_count = -1;
4259
4260 /* First, determine if we need to expand anything, and the number of
4261 slots we'll need. */
4262 for (in_arg = 0; in_arg < nargs; ++in_arg)
4263 {
4264 tree arg = TREE_VEC_ELT (args, in_arg);
4265 if (arg == NULL_TREE)
4266 return args;
4267 if (ARGUMENT_PACK_P (arg))
4268 {
4269 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4270 if (num_result_args < 0)
4271 num_result_args = in_arg + num_packed;
4272 else
4273 num_result_args += num_packed;
4274 }
4275 else
4276 {
4277 if (num_result_args >= 0)
4278 num_result_args++;
4279 }
4280 }
4281
4282 /* If no expansion is necessary, we're done. */
4283 if (num_result_args < 0)
4284 return args;
4285
4286 /* Expand arguments. */
4287 result_args = make_tree_vec (num_result_args);
4288 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4289 non_default_args_count =
4290 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4291 for (in_arg = 0; in_arg < nargs; ++in_arg)
4292 {
4293 tree arg = TREE_VEC_ELT (args, in_arg);
4294 if (ARGUMENT_PACK_P (arg))
4295 {
4296 tree packed = ARGUMENT_PACK_ARGS (arg);
4297 int i, num_packed = TREE_VEC_LENGTH (packed);
4298 for (i = 0; i < num_packed; ++i, ++out_arg)
4299 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4300 if (non_default_args_count > 0)
4301 non_default_args_count += num_packed - 1;
4302 }
4303 else
4304 {
4305 TREE_VEC_ELT (result_args, out_arg) = arg;
4306 ++out_arg;
4307 }
4308 }
4309 if (non_default_args_count >= 0)
4310 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4311 return result_args;
4312 }
4313
4314 /* Checks if DECL shadows a template parameter.
4315
4316 [temp.local]: A template-parameter shall not be redeclared within its
4317 scope (including nested scopes).
4318
4319 Emits an error and returns TRUE if the DECL shadows a parameter,
4320 returns FALSE otherwise. */
4321
4322 bool
4323 check_template_shadow (tree decl)
4324 {
4325 tree olddecl;
4326
4327 /* If we're not in a template, we can't possibly shadow a template
4328 parameter. */
4329 if (!current_template_parms)
4330 return true;
4331
4332 /* Figure out what we're shadowing. */
4333 decl = OVL_FIRST (decl);
4334 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4335
4336 /* If there's no previous binding for this name, we're not shadowing
4337 anything, let alone a template parameter. */
4338 if (!olddecl)
4339 return true;
4340
4341 /* If we're not shadowing a template parameter, we're done. Note
4342 that OLDDECL might be an OVERLOAD (or perhaps even an
4343 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4344 node. */
4345 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4346 return true;
4347
4348 /* We check for decl != olddecl to avoid bogus errors for using a
4349 name inside a class. We check TPFI to avoid duplicate errors for
4350 inline member templates. */
4351 if (decl == olddecl
4352 || (DECL_TEMPLATE_PARM_P (decl)
4353 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4354 return true;
4355
4356 /* Don't complain about the injected class name, as we've already
4357 complained about the class itself. */
4358 if (DECL_SELF_REFERENCE_P (decl))
4359 return false;
4360
4361 if (DECL_TEMPLATE_PARM_P (decl))
4362 error ("declaration of template parameter %q+D shadows "
4363 "template parameter", decl);
4364 else
4365 error ("declaration of %q+#D shadows template parameter", decl);
4366 inform (DECL_SOURCE_LOCATION (olddecl),
4367 "template parameter %qD declared here", olddecl);
4368 return false;
4369 }
4370
4371 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4372 ORIG_LEVEL, DECL, and TYPE. */
4373
4374 static tree
4375 build_template_parm_index (int index,
4376 int level,
4377 int orig_level,
4378 tree decl,
4379 tree type)
4380 {
4381 tree t = make_node (TEMPLATE_PARM_INDEX);
4382 TEMPLATE_PARM_IDX (t) = index;
4383 TEMPLATE_PARM_LEVEL (t) = level;
4384 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4385 TEMPLATE_PARM_DECL (t) = decl;
4386 TREE_TYPE (t) = type;
4387 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4388 TREE_READONLY (t) = TREE_READONLY (decl);
4389
4390 return t;
4391 }
4392
4393 /* Find the canonical type parameter for the given template type
4394 parameter. Returns the canonical type parameter, which may be TYPE
4395 if no such parameter existed. */
4396
4397 static tree
4398 canonical_type_parameter (tree type)
4399 {
4400 tree list;
4401 int idx = TEMPLATE_TYPE_IDX (type);
4402 if (!canonical_template_parms)
4403 vec_alloc (canonical_template_parms, idx + 1);
4404
4405 if (canonical_template_parms->length () <= (unsigned) idx)
4406 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4407
4408 list = (*canonical_template_parms)[idx];
4409 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4410 list = TREE_CHAIN (list);
4411
4412 if (list)
4413 return TREE_VALUE (list);
4414 else
4415 {
4416 (*canonical_template_parms)[idx]
4417 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4418 return type;
4419 }
4420 }
4421
4422 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4423 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4424 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4425 new one is created. */
4426
4427 static tree
4428 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4429 tsubst_flags_t complain)
4430 {
4431 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4432 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4433 != TEMPLATE_PARM_LEVEL (index) - levels)
4434 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4435 {
4436 tree orig_decl = TEMPLATE_PARM_DECL (index);
4437
4438 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4439 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4440 type);
4441 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4442 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4443 DECL_ARTIFICIAL (decl) = 1;
4444 SET_DECL_TEMPLATE_PARM_P (decl);
4445
4446 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4447 TEMPLATE_PARM_LEVEL (index) - levels,
4448 TEMPLATE_PARM_ORIG_LEVEL (index),
4449 decl, type);
4450 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4451 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4452 = TEMPLATE_PARM_PARAMETER_PACK (index);
4453
4454 /* Template template parameters need this. */
4455 tree inner = decl;
4456 if (TREE_CODE (decl) == TEMPLATE_DECL)
4457 {
4458 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4459 TYPE_DECL, DECL_NAME (decl), type);
4460 DECL_TEMPLATE_RESULT (decl) = inner;
4461 DECL_ARTIFICIAL (inner) = true;
4462 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4463 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4464 }
4465
4466 /* Attach the TPI to the decl. */
4467 if (TREE_CODE (inner) == TYPE_DECL)
4468 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4469 else
4470 DECL_INITIAL (decl) = tpi;
4471 }
4472
4473 return TEMPLATE_PARM_DESCENDANTS (index);
4474 }
4475
4476 /* Process information from new template parameter PARM and append it
4477 to the LIST being built. This new parameter is a non-type
4478 parameter iff IS_NON_TYPE is true. This new parameter is a
4479 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4480 is in PARM_LOC. */
4481
4482 tree
4483 process_template_parm (tree list, location_t parm_loc, tree parm,
4484 bool is_non_type, bool is_parameter_pack)
4485 {
4486 tree decl = 0;
4487 int idx = 0;
4488
4489 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4490 tree defval = TREE_PURPOSE (parm);
4491 tree constr = TREE_TYPE (parm);
4492
4493 if (list)
4494 {
4495 tree p = tree_last (list);
4496
4497 if (p && TREE_VALUE (p) != error_mark_node)
4498 {
4499 p = TREE_VALUE (p);
4500 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4501 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4502 else
4503 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4504 }
4505
4506 ++idx;
4507 }
4508
4509 if (is_non_type)
4510 {
4511 parm = TREE_VALUE (parm);
4512
4513 SET_DECL_TEMPLATE_PARM_P (parm);
4514
4515 if (TREE_TYPE (parm) != error_mark_node)
4516 {
4517 /* [temp.param]
4518
4519 The top-level cv-qualifiers on the template-parameter are
4520 ignored when determining its type. */
4521 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4522 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4523 TREE_TYPE (parm) = error_mark_node;
4524 else if (uses_parameter_packs (TREE_TYPE (parm))
4525 && !is_parameter_pack
4526 /* If we're in a nested template parameter list, the template
4527 template parameter could be a parameter pack. */
4528 && processing_template_parmlist == 1)
4529 {
4530 /* This template parameter is not a parameter pack, but it
4531 should be. Complain about "bare" parameter packs. */
4532 check_for_bare_parameter_packs (TREE_TYPE (parm));
4533
4534 /* Recover by calling this a parameter pack. */
4535 is_parameter_pack = true;
4536 }
4537 }
4538
4539 /* A template parameter is not modifiable. */
4540 TREE_CONSTANT (parm) = 1;
4541 TREE_READONLY (parm) = 1;
4542 decl = build_decl (parm_loc,
4543 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4544 TREE_CONSTANT (decl) = 1;
4545 TREE_READONLY (decl) = 1;
4546 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4547 = build_template_parm_index (idx, processing_template_decl,
4548 processing_template_decl,
4549 decl, TREE_TYPE (parm));
4550
4551 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4552 = is_parameter_pack;
4553 }
4554 else
4555 {
4556 tree t;
4557 parm = TREE_VALUE (TREE_VALUE (parm));
4558
4559 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4560 {
4561 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4562 /* This is for distinguishing between real templates and template
4563 template parameters */
4564 TREE_TYPE (parm) = t;
4565 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4566 decl = parm;
4567 }
4568 else
4569 {
4570 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4571 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4572 decl = build_decl (parm_loc,
4573 TYPE_DECL, parm, t);
4574 }
4575
4576 TYPE_NAME (t) = decl;
4577 TYPE_STUB_DECL (t) = decl;
4578 parm = decl;
4579 TEMPLATE_TYPE_PARM_INDEX (t)
4580 = build_template_parm_index (idx, processing_template_decl,
4581 processing_template_decl,
4582 decl, TREE_TYPE (parm));
4583 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4584 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4585 }
4586 DECL_ARTIFICIAL (decl) = 1;
4587 SET_DECL_TEMPLATE_PARM_P (decl);
4588
4589 /* Build requirements for the type/template parameter.
4590 This must be done after SET_DECL_TEMPLATE_PARM_P or
4591 process_template_parm could fail. */
4592 tree reqs = finish_shorthand_constraint (parm, constr);
4593
4594 decl = pushdecl (decl);
4595 if (!is_non_type)
4596 parm = decl;
4597
4598 /* Build the parameter node linking the parameter declaration,
4599 its default argument (if any), and its constraints (if any). */
4600 parm = build_tree_list (defval, parm);
4601 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4602
4603 return chainon (list, parm);
4604 }
4605
4606 /* The end of a template parameter list has been reached. Process the
4607 tree list into a parameter vector, converting each parameter into a more
4608 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4609 as PARM_DECLs. */
4610
4611 tree
4612 end_template_parm_list (tree parms)
4613 {
4614 int nparms;
4615 tree parm, next;
4616 tree saved_parmlist = make_tree_vec (list_length (parms));
4617
4618 /* Pop the dummy parameter level and add the real one. */
4619 current_template_parms = TREE_CHAIN (current_template_parms);
4620
4621 current_template_parms
4622 = tree_cons (size_int (processing_template_decl),
4623 saved_parmlist, current_template_parms);
4624
4625 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4626 {
4627 next = TREE_CHAIN (parm);
4628 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4629 TREE_CHAIN (parm) = NULL_TREE;
4630 }
4631
4632 --processing_template_parmlist;
4633
4634 return saved_parmlist;
4635 }
4636
4637 // Explicitly indicate the end of the template parameter list. We assume
4638 // that the current template parameters have been constructed and/or
4639 // managed explicitly, as when creating new template template parameters
4640 // from a shorthand constraint.
4641 void
4642 end_template_parm_list ()
4643 {
4644 --processing_template_parmlist;
4645 }
4646
4647 /* end_template_decl is called after a template declaration is seen. */
4648
4649 void
4650 end_template_decl (void)
4651 {
4652 reset_specialization ();
4653
4654 if (! processing_template_decl)
4655 return;
4656
4657 /* This matches the pushlevel in begin_template_parm_list. */
4658 finish_scope ();
4659
4660 --processing_template_decl;
4661 current_template_parms = TREE_CHAIN (current_template_parms);
4662 }
4663
4664 /* Takes a TREE_LIST representing a template parameter and convert it
4665 into an argument suitable to be passed to the type substitution
4666 functions. Note that If the TREE_LIST contains an error_mark
4667 node, the returned argument is error_mark_node. */
4668
4669 tree
4670 template_parm_to_arg (tree t)
4671 {
4672
4673 if (t == NULL_TREE
4674 || TREE_CODE (t) != TREE_LIST)
4675 return t;
4676
4677 if (error_operand_p (TREE_VALUE (t)))
4678 return error_mark_node;
4679
4680 t = TREE_VALUE (t);
4681
4682 if (TREE_CODE (t) == TYPE_DECL
4683 || TREE_CODE (t) == TEMPLATE_DECL)
4684 {
4685 t = TREE_TYPE (t);
4686
4687 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4688 {
4689 /* Turn this argument into a TYPE_ARGUMENT_PACK
4690 with a single element, which expands T. */
4691 tree vec = make_tree_vec (1);
4692 if (CHECKING_P)
4693 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4694
4695 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4696
4697 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4698 SET_ARGUMENT_PACK_ARGS (t, vec);
4699 }
4700 }
4701 else
4702 {
4703 t = DECL_INITIAL (t);
4704
4705 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4706 {
4707 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4708 with a single element, which expands T. */
4709 tree vec = make_tree_vec (1);
4710 if (CHECKING_P)
4711 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4712
4713 t = convert_from_reference (t);
4714 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4715
4716 t = make_node (NONTYPE_ARGUMENT_PACK);
4717 SET_ARGUMENT_PACK_ARGS (t, vec);
4718 }
4719 else
4720 t = convert_from_reference (t);
4721 }
4722 return t;
4723 }
4724
4725 /* Given a single level of template parameters (a TREE_VEC), return it
4726 as a set of template arguments. */
4727
4728 tree
4729 template_parms_level_to_args (tree parms)
4730 {
4731 tree a = copy_node (parms);
4732 TREE_TYPE (a) = NULL_TREE;
4733 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4734 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4735
4736 if (CHECKING_P)
4737 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4738
4739 return a;
4740 }
4741
4742 /* Given a set of template parameters, return them as a set of template
4743 arguments. The template parameters are represented as a TREE_VEC, in
4744 the form documented in cp-tree.h for template arguments. */
4745
4746 tree
4747 template_parms_to_args (tree parms)
4748 {
4749 tree header;
4750 tree args = NULL_TREE;
4751 int length = TMPL_PARMS_DEPTH (parms);
4752 int l = length;
4753
4754 /* If there is only one level of template parameters, we do not
4755 create a TREE_VEC of TREE_VECs. Instead, we return a single
4756 TREE_VEC containing the arguments. */
4757 if (length > 1)
4758 args = make_tree_vec (length);
4759
4760 for (header = parms; header; header = TREE_CHAIN (header))
4761 {
4762 tree a = template_parms_level_to_args (TREE_VALUE (header));
4763
4764 if (length > 1)
4765 TREE_VEC_ELT (args, --l) = a;
4766 else
4767 args = a;
4768 }
4769
4770 return args;
4771 }
4772
4773 /* Within the declaration of a template, return the currently active
4774 template parameters as an argument TREE_VEC. */
4775
4776 static tree
4777 current_template_args (void)
4778 {
4779 return template_parms_to_args (current_template_parms);
4780 }
4781
4782 /* Return the fully generic arguments for of TMPL, i.e. what
4783 current_template_args would be while parsing it. */
4784
4785 tree
4786 generic_targs_for (tree tmpl)
4787 {
4788 if (tmpl == NULL_TREE)
4789 return NULL_TREE;
4790 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4791 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4792 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4793 template parameter, it has no TEMPLATE_INFO; for a partial
4794 specialization, it has the arguments for the primary template, and we
4795 want the arguments for the partial specialization. */;
4796 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4797 if (tree ti = get_template_info (result))
4798 return TI_ARGS (ti);
4799 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4800 }
4801
4802 /* Update the declared TYPE by doing any lookups which were thought to be
4803 dependent, but are not now that we know the SCOPE of the declarator. */
4804
4805 tree
4806 maybe_update_decl_type (tree orig_type, tree scope)
4807 {
4808 tree type = orig_type;
4809
4810 if (type == NULL_TREE)
4811 return type;
4812
4813 if (TREE_CODE (orig_type) == TYPE_DECL)
4814 type = TREE_TYPE (type);
4815
4816 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4817 && dependent_type_p (type)
4818 /* Don't bother building up the args in this case. */
4819 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4820 {
4821 /* tsubst in the args corresponding to the template parameters,
4822 including auto if present. Most things will be unchanged, but
4823 make_typename_type and tsubst_qualified_id will resolve
4824 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4825 tree args = current_template_args ();
4826 tree auto_node = type_uses_auto (type);
4827 tree pushed;
4828 if (auto_node)
4829 {
4830 tree auto_vec = make_tree_vec (1);
4831 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4832 args = add_to_template_args (args, auto_vec);
4833 }
4834 pushed = push_scope (scope);
4835 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4836 if (pushed)
4837 pop_scope (scope);
4838 }
4839
4840 if (type == error_mark_node)
4841 return orig_type;
4842
4843 if (TREE_CODE (orig_type) == TYPE_DECL)
4844 {
4845 if (same_type_p (type, TREE_TYPE (orig_type)))
4846 type = orig_type;
4847 else
4848 type = TYPE_NAME (type);
4849 }
4850 return type;
4851 }
4852
4853 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4854 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4855 the new template is a member template. */
4856
4857 static tree
4858 build_template_decl (tree decl, tree parms, bool member_template_p)
4859 {
4860 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4861 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4862 DECL_TEMPLATE_PARMS (tmpl) = parms;
4863 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4864 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4865 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4866
4867 return tmpl;
4868 }
4869
4870 struct template_parm_data
4871 {
4872 /* The level of the template parameters we are currently
4873 processing. */
4874 int level;
4875
4876 /* The index of the specialization argument we are currently
4877 processing. */
4878 int current_arg;
4879
4880 /* An array whose size is the number of template parameters. The
4881 elements are nonzero if the parameter has been used in any one
4882 of the arguments processed so far. */
4883 int* parms;
4884
4885 /* An array whose size is the number of template arguments. The
4886 elements are nonzero if the argument makes use of template
4887 parameters of this level. */
4888 int* arg_uses_template_parms;
4889 };
4890
4891 /* Subroutine of push_template_decl used to see if each template
4892 parameter in a partial specialization is used in the explicit
4893 argument list. If T is of the LEVEL given in DATA (which is
4894 treated as a template_parm_data*), then DATA->PARMS is marked
4895 appropriately. */
4896
4897 static int
4898 mark_template_parm (tree t, void* data)
4899 {
4900 int level;
4901 int idx;
4902 struct template_parm_data* tpd = (struct template_parm_data*) data;
4903
4904 template_parm_level_and_index (t, &level, &idx);
4905
4906 if (level == tpd->level)
4907 {
4908 tpd->parms[idx] = 1;
4909 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4910 }
4911
4912 /* In C++17 the type of a non-type argument is a deduced context. */
4913 if (cxx_dialect >= cxx17
4914 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4915 for_each_template_parm (TREE_TYPE (t),
4916 &mark_template_parm,
4917 data,
4918 NULL,
4919 /*include_nondeduced_p=*/false);
4920
4921 /* Return zero so that for_each_template_parm will continue the
4922 traversal of the tree; we want to mark *every* template parm. */
4923 return 0;
4924 }
4925
4926 /* Process the partial specialization DECL. */
4927
4928 static tree
4929 process_partial_specialization (tree decl)
4930 {
4931 tree type = TREE_TYPE (decl);
4932 tree tinfo = get_template_info (decl);
4933 tree maintmpl = TI_TEMPLATE (tinfo);
4934 tree specargs = TI_ARGS (tinfo);
4935 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4936 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4937 tree inner_parms;
4938 tree inst;
4939 int nargs = TREE_VEC_LENGTH (inner_args);
4940 int ntparms;
4941 int i;
4942 bool did_error_intro = false;
4943 struct template_parm_data tpd;
4944 struct template_parm_data tpd2;
4945
4946 gcc_assert (current_template_parms);
4947
4948 /* A concept cannot be specialized. */
4949 if (flag_concepts && variable_concept_p (maintmpl))
4950 {
4951 error ("specialization of variable concept %q#D", maintmpl);
4952 return error_mark_node;
4953 }
4954
4955 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4956 ntparms = TREE_VEC_LENGTH (inner_parms);
4957
4958 /* We check that each of the template parameters given in the
4959 partial specialization is used in the argument list to the
4960 specialization. For example:
4961
4962 template <class T> struct S;
4963 template <class T> struct S<T*>;
4964
4965 The second declaration is OK because `T*' uses the template
4966 parameter T, whereas
4967
4968 template <class T> struct S<int>;
4969
4970 is no good. Even trickier is:
4971
4972 template <class T>
4973 struct S1
4974 {
4975 template <class U>
4976 struct S2;
4977 template <class U>
4978 struct S2<T>;
4979 };
4980
4981 The S2<T> declaration is actually invalid; it is a
4982 full-specialization. Of course,
4983
4984 template <class U>
4985 struct S2<T (*)(U)>;
4986
4987 or some such would have been OK. */
4988 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4989 tpd.parms = XALLOCAVEC (int, ntparms);
4990 memset (tpd.parms, 0, sizeof (int) * ntparms);
4991
4992 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4993 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4994 for (i = 0; i < nargs; ++i)
4995 {
4996 tpd.current_arg = i;
4997 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4998 &mark_template_parm,
4999 &tpd,
5000 NULL,
5001 /*include_nondeduced_p=*/false);
5002 }
5003 for (i = 0; i < ntparms; ++i)
5004 if (tpd.parms[i] == 0)
5005 {
5006 /* One of the template parms was not used in a deduced context in the
5007 specialization. */
5008 if (!did_error_intro)
5009 {
5010 error ("template parameters not deducible in "
5011 "partial specialization:");
5012 did_error_intro = true;
5013 }
5014
5015 inform (input_location, " %qD",
5016 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5017 }
5018
5019 if (did_error_intro)
5020 return error_mark_node;
5021
5022 /* [temp.class.spec]
5023
5024 The argument list of the specialization shall not be identical to
5025 the implicit argument list of the primary template. */
5026 tree main_args
5027 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5028 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5029 && (!flag_concepts
5030 || !strictly_subsumes (current_template_constraints (),
5031 inner_args, maintmpl)))
5032 {
5033 if (!flag_concepts)
5034 error ("partial specialization %q+D does not specialize "
5035 "any template arguments; to define the primary template, "
5036 "remove the template argument list", decl);
5037 else
5038 error ("partial specialization %q+D does not specialize any "
5039 "template arguments and is not more constrained than "
5040 "the primary template; to define the primary template, "
5041 "remove the template argument list", decl);
5042 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5043 }
5044
5045 /* A partial specialization that replaces multiple parameters of the
5046 primary template with a pack expansion is less specialized for those
5047 parameters. */
5048 if (nargs < DECL_NTPARMS (maintmpl))
5049 {
5050 error ("partial specialization is not more specialized than the "
5051 "primary template because it replaces multiple parameters "
5052 "with a pack expansion");
5053 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5054 /* Avoid crash in process_partial_specialization. */
5055 return decl;
5056 }
5057
5058 /* If we aren't in a dependent class, we can actually try deduction. */
5059 else if (tpd.level == 1
5060 /* FIXME we should be able to handle a partial specialization of a
5061 partial instantiation, but currently we can't (c++/41727). */
5062 && TMPL_ARGS_DEPTH (specargs) == 1
5063 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5064 {
5065 auto_diagnostic_group d;
5066 if (permerror (input_location, "partial specialization %qD is not "
5067 "more specialized than", decl))
5068 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5069 maintmpl);
5070 }
5071
5072 /* [temp.class.spec]
5073
5074 A partially specialized non-type argument expression shall not
5075 involve template parameters of the partial specialization except
5076 when the argument expression is a simple identifier.
5077
5078 The type of a template parameter corresponding to a specialized
5079 non-type argument shall not be dependent on a parameter of the
5080 specialization.
5081
5082 Also, we verify that pack expansions only occur at the
5083 end of the argument list. */
5084 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
5085 tpd2.parms = 0;
5086 for (i = 0; i < nargs; ++i)
5087 {
5088 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5089 tree arg = TREE_VEC_ELT (inner_args, i);
5090 tree packed_args = NULL_TREE;
5091 int j, len = 1;
5092
5093 if (ARGUMENT_PACK_P (arg))
5094 {
5095 /* Extract the arguments from the argument pack. We'll be
5096 iterating over these in the following loop. */
5097 packed_args = ARGUMENT_PACK_ARGS (arg);
5098 len = TREE_VEC_LENGTH (packed_args);
5099 }
5100
5101 for (j = 0; j < len; j++)
5102 {
5103 if (packed_args)
5104 /* Get the Jth argument in the parameter pack. */
5105 arg = TREE_VEC_ELT (packed_args, j);
5106
5107 if (PACK_EXPANSION_P (arg))
5108 {
5109 /* Pack expansions must come at the end of the
5110 argument list. */
5111 if ((packed_args && j < len - 1)
5112 || (!packed_args && i < nargs - 1))
5113 {
5114 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5115 error ("parameter pack argument %qE must be at the "
5116 "end of the template argument list", arg);
5117 else
5118 error ("parameter pack argument %qT must be at the "
5119 "end of the template argument list", arg);
5120 }
5121 }
5122
5123 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5124 /* We only care about the pattern. */
5125 arg = PACK_EXPANSION_PATTERN (arg);
5126
5127 if (/* These first two lines are the `non-type' bit. */
5128 !TYPE_P (arg)
5129 && TREE_CODE (arg) != TEMPLATE_DECL
5130 /* This next two lines are the `argument expression is not just a
5131 simple identifier' condition and also the `specialized
5132 non-type argument' bit. */
5133 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5134 && !((REFERENCE_REF_P (arg)
5135 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5136 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5137 {
5138 if ((!packed_args && tpd.arg_uses_template_parms[i])
5139 || (packed_args && uses_template_parms (arg)))
5140 error_at (cp_expr_loc_or_input_loc (arg),
5141 "template argument %qE involves template "
5142 "parameter(s)", arg);
5143 else
5144 {
5145 /* Look at the corresponding template parameter,
5146 marking which template parameters its type depends
5147 upon. */
5148 tree type = TREE_TYPE (parm);
5149
5150 if (!tpd2.parms)
5151 {
5152 /* We haven't yet initialized TPD2. Do so now. */
5153 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5154 /* The number of parameters here is the number in the
5155 main template, which, as checked in the assertion
5156 above, is NARGS. */
5157 tpd2.parms = XALLOCAVEC (int, nargs);
5158 tpd2.level =
5159 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5160 }
5161
5162 /* Mark the template parameters. But this time, we're
5163 looking for the template parameters of the main
5164 template, not in the specialization. */
5165 tpd2.current_arg = i;
5166 tpd2.arg_uses_template_parms[i] = 0;
5167 memset (tpd2.parms, 0, sizeof (int) * nargs);
5168 for_each_template_parm (type,
5169 &mark_template_parm,
5170 &tpd2,
5171 NULL,
5172 /*include_nondeduced_p=*/false);
5173
5174 if (tpd2.arg_uses_template_parms [i])
5175 {
5176 /* The type depended on some template parameters.
5177 If they are fully specialized in the
5178 specialization, that's OK. */
5179 int j;
5180 int count = 0;
5181 for (j = 0; j < nargs; ++j)
5182 if (tpd2.parms[j] != 0
5183 && tpd.arg_uses_template_parms [j])
5184 ++count;
5185 if (count != 0)
5186 error_n (input_location, count,
5187 "type %qT of template argument %qE depends "
5188 "on a template parameter",
5189 "type %qT of template argument %qE depends "
5190 "on template parameters",
5191 type,
5192 arg);
5193 }
5194 }
5195 }
5196 }
5197 }
5198
5199 /* We should only get here once. */
5200 if (TREE_CODE (decl) == TYPE_DECL)
5201 gcc_assert (!COMPLETE_TYPE_P (type));
5202
5203 // Build the template decl.
5204 tree tmpl = build_template_decl (decl, current_template_parms,
5205 DECL_MEMBER_TEMPLATE_P (maintmpl));
5206 TREE_TYPE (tmpl) = type;
5207 DECL_TEMPLATE_RESULT (tmpl) = decl;
5208 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5209 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5210 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5211
5212 /* Give template template parms a DECL_CONTEXT of the template
5213 for which they are a parameter. */
5214 for (i = 0; i < ntparms; ++i)
5215 {
5216 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5217 if (TREE_CODE (parm) == TEMPLATE_DECL)
5218 DECL_CONTEXT (parm) = tmpl;
5219 }
5220
5221 if (VAR_P (decl))
5222 /* We didn't register this in check_explicit_specialization so we could
5223 wait until the constraints were set. */
5224 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5225 else
5226 associate_classtype_constraints (type);
5227
5228 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5229 = tree_cons (specargs, tmpl,
5230 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5231 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5232
5233 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5234 inst = TREE_CHAIN (inst))
5235 {
5236 tree instance = TREE_VALUE (inst);
5237 if (TYPE_P (instance)
5238 ? (COMPLETE_TYPE_P (instance)
5239 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5240 : DECL_TEMPLATE_INSTANTIATION (instance))
5241 {
5242 tree spec = most_specialized_partial_spec (instance, tf_none);
5243 tree inst_decl = (DECL_P (instance)
5244 ? instance : TYPE_NAME (instance));
5245 if (!spec)
5246 /* OK */;
5247 else if (spec == error_mark_node)
5248 permerror (input_location,
5249 "declaration of %qD ambiguates earlier template "
5250 "instantiation for %qD", decl, inst_decl);
5251 else if (TREE_VALUE (spec) == tmpl)
5252 permerror (input_location,
5253 "partial specialization of %qD after instantiation "
5254 "of %qD", decl, inst_decl);
5255 }
5256 }
5257
5258 return decl;
5259 }
5260
5261 /* PARM is a template parameter of some form; return the corresponding
5262 TEMPLATE_PARM_INDEX. */
5263
5264 static tree
5265 get_template_parm_index (tree parm)
5266 {
5267 if (TREE_CODE (parm) == PARM_DECL
5268 || TREE_CODE (parm) == CONST_DECL)
5269 parm = DECL_INITIAL (parm);
5270 else if (TREE_CODE (parm) == TYPE_DECL
5271 || TREE_CODE (parm) == TEMPLATE_DECL)
5272 parm = TREE_TYPE (parm);
5273 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5274 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5275 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5276 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5277 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5278 return parm;
5279 }
5280
5281 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5282 parameter packs used by the template parameter PARM. */
5283
5284 static void
5285 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5286 {
5287 /* A type parm can't refer to another parm. */
5288 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5289 return;
5290 else if (TREE_CODE (parm) == PARM_DECL)
5291 {
5292 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5293 ppd, ppd->visited);
5294 return;
5295 }
5296
5297 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5298
5299 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5300 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5301 {
5302 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5303 if (template_parameter_pack_p (p))
5304 /* Any packs in the type are expanded by this parameter. */;
5305 else
5306 fixed_parameter_pack_p_1 (p, ppd);
5307 }
5308 }
5309
5310 /* PARM is a template parameter pack. Return any parameter packs used in
5311 its type or the type of any of its template parameters. If there are
5312 any such packs, it will be instantiated into a fixed template parameter
5313 list by partial instantiation rather than be fully deduced. */
5314
5315 tree
5316 fixed_parameter_pack_p (tree parm)
5317 {
5318 /* This can only be true in a member template. */
5319 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5320 return NULL_TREE;
5321 /* This can only be true for a parameter pack. */
5322 if (!template_parameter_pack_p (parm))
5323 return NULL_TREE;
5324 /* A type parm can't refer to another parm. */
5325 if (TREE_CODE (parm) == TYPE_DECL)
5326 return NULL_TREE;
5327
5328 tree parameter_packs = NULL_TREE;
5329 struct find_parameter_pack_data ppd;
5330 ppd.parameter_packs = &parameter_packs;
5331 ppd.visited = new hash_set<tree>;
5332 ppd.type_pack_expansion_p = false;
5333
5334 fixed_parameter_pack_p_1 (parm, &ppd);
5335
5336 delete ppd.visited;
5337 return parameter_packs;
5338 }
5339
5340 /* Check that a template declaration's use of default arguments and
5341 parameter packs is not invalid. Here, PARMS are the template
5342 parameters. IS_PRIMARY is true if DECL is the thing declared by
5343 a primary template. IS_PARTIAL is true if DECL is a partial
5344 specialization.
5345
5346 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5347 function template declaration or a friend class template
5348 declaration. In the function case, 1 indicates a declaration, 2
5349 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5350 emitted for extraneous default arguments.
5351
5352 Returns TRUE if there were no errors found, FALSE otherwise. */
5353
5354 bool
5355 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5356 bool is_partial, int is_friend_decl)
5357 {
5358 const char *msg;
5359 int last_level_to_check;
5360 tree parm_level;
5361 bool no_errors = true;
5362
5363 /* [temp.param]
5364
5365 A default template-argument shall not be specified in a
5366 function template declaration or a function template definition, nor
5367 in the template-parameter-list of the definition of a member of a
5368 class template. */
5369
5370 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5371 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5372 /* You can't have a function template declaration in a local
5373 scope, nor you can you define a member of a class template in a
5374 local scope. */
5375 return true;
5376
5377 if ((TREE_CODE (decl) == TYPE_DECL
5378 && TREE_TYPE (decl)
5379 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5380 || (TREE_CODE (decl) == FUNCTION_DECL
5381 && LAMBDA_FUNCTION_P (decl)))
5382 /* A lambda doesn't have an explicit declaration; don't complain
5383 about the parms of the enclosing class. */
5384 return true;
5385
5386 if (current_class_type
5387 && !TYPE_BEING_DEFINED (current_class_type)
5388 && DECL_LANG_SPECIFIC (decl)
5389 && DECL_DECLARES_FUNCTION_P (decl)
5390 /* If this is either a friend defined in the scope of the class
5391 or a member function. */
5392 && (DECL_FUNCTION_MEMBER_P (decl)
5393 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5394 : DECL_FRIEND_CONTEXT (decl)
5395 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5396 : false)
5397 /* And, if it was a member function, it really was defined in
5398 the scope of the class. */
5399 && (!DECL_FUNCTION_MEMBER_P (decl)
5400 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5401 /* We already checked these parameters when the template was
5402 declared, so there's no need to do it again now. This function
5403 was defined in class scope, but we're processing its body now
5404 that the class is complete. */
5405 return true;
5406
5407 /* Core issue 226 (C++0x only): the following only applies to class
5408 templates. */
5409 if (is_primary
5410 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5411 {
5412 /* [temp.param]
5413
5414 If a template-parameter has a default template-argument, all
5415 subsequent template-parameters shall have a default
5416 template-argument supplied. */
5417 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5418 {
5419 tree inner_parms = TREE_VALUE (parm_level);
5420 int ntparms = TREE_VEC_LENGTH (inner_parms);
5421 int seen_def_arg_p = 0;
5422 int i;
5423
5424 for (i = 0; i < ntparms; ++i)
5425 {
5426 tree parm = TREE_VEC_ELT (inner_parms, i);
5427
5428 if (parm == error_mark_node)
5429 continue;
5430
5431 if (TREE_PURPOSE (parm))
5432 seen_def_arg_p = 1;
5433 else if (seen_def_arg_p
5434 && !template_parameter_pack_p (TREE_VALUE (parm)))
5435 {
5436 error ("no default argument for %qD", TREE_VALUE (parm));
5437 /* For better subsequent error-recovery, we indicate that
5438 there should have been a default argument. */
5439 TREE_PURPOSE (parm) = error_mark_node;
5440 no_errors = false;
5441 }
5442 else if (!is_partial
5443 && !is_friend_decl
5444 /* Don't complain about an enclosing partial
5445 specialization. */
5446 && parm_level == parms
5447 && TREE_CODE (decl) == TYPE_DECL
5448 && i < ntparms - 1
5449 && template_parameter_pack_p (TREE_VALUE (parm))
5450 /* A fixed parameter pack will be partially
5451 instantiated into a fixed length list. */
5452 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5453 {
5454 /* A primary class template can only have one
5455 parameter pack, at the end of the template
5456 parameter list. */
5457
5458 error ("parameter pack %q+D must be at the end of the"
5459 " template parameter list", TREE_VALUE (parm));
5460
5461 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5462 = error_mark_node;
5463 no_errors = false;
5464 }
5465 }
5466 }
5467 }
5468
5469 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5470 || is_partial
5471 || !is_primary
5472 || is_friend_decl)
5473 /* For an ordinary class template, default template arguments are
5474 allowed at the innermost level, e.g.:
5475 template <class T = int>
5476 struct S {};
5477 but, in a partial specialization, they're not allowed even
5478 there, as we have in [temp.class.spec]:
5479
5480 The template parameter list of a specialization shall not
5481 contain default template argument values.
5482
5483 So, for a partial specialization, or for a function template
5484 (in C++98/C++03), we look at all of them. */
5485 ;
5486 else
5487 /* But, for a primary class template that is not a partial
5488 specialization we look at all template parameters except the
5489 innermost ones. */
5490 parms = TREE_CHAIN (parms);
5491
5492 /* Figure out what error message to issue. */
5493 if (is_friend_decl == 2)
5494 msg = G_("default template arguments may not be used in function template "
5495 "friend re-declaration");
5496 else if (is_friend_decl)
5497 msg = G_("default template arguments may not be used in template "
5498 "friend declarations");
5499 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5500 msg = G_("default template arguments may not be used in function templates "
5501 "without %<-std=c++11%> or %<-std=gnu++11%>");
5502 else if (is_partial)
5503 msg = G_("default template arguments may not be used in "
5504 "partial specializations");
5505 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5506 msg = G_("default argument for template parameter for class enclosing %qD");
5507 else
5508 /* Per [temp.param]/9, "A default template-argument shall not be
5509 specified in the template-parameter-lists of the definition of
5510 a member of a class template that appears outside of the member's
5511 class.", thus if we aren't handling a member of a class template
5512 there is no need to examine the parameters. */
5513 return true;
5514
5515 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5516 /* If we're inside a class definition, there's no need to
5517 examine the parameters to the class itself. On the one
5518 hand, they will be checked when the class is defined, and,
5519 on the other, default arguments are valid in things like:
5520 template <class T = double>
5521 struct S { template <class U> void f(U); };
5522 Here the default argument for `S' has no bearing on the
5523 declaration of `f'. */
5524 last_level_to_check = template_class_depth (current_class_type) + 1;
5525 else
5526 /* Check everything. */
5527 last_level_to_check = 0;
5528
5529 for (parm_level = parms;
5530 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5531 parm_level = TREE_CHAIN (parm_level))
5532 {
5533 tree inner_parms = TREE_VALUE (parm_level);
5534 int i;
5535 int ntparms;
5536
5537 ntparms = TREE_VEC_LENGTH (inner_parms);
5538 for (i = 0; i < ntparms; ++i)
5539 {
5540 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5541 continue;
5542
5543 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5544 {
5545 if (msg)
5546 {
5547 no_errors = false;
5548 if (is_friend_decl == 2)
5549 return no_errors;
5550
5551 error (msg, decl);
5552 msg = 0;
5553 }
5554
5555 /* Clear out the default argument so that we are not
5556 confused later. */
5557 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5558 }
5559 }
5560
5561 /* At this point, if we're still interested in issuing messages,
5562 they must apply to classes surrounding the object declared. */
5563 if (msg)
5564 msg = G_("default argument for template parameter for class "
5565 "enclosing %qD");
5566 }
5567
5568 return no_errors;
5569 }
5570
5571 /* Worker for push_template_decl_real, called via
5572 for_each_template_parm. DATA is really an int, indicating the
5573 level of the parameters we are interested in. If T is a template
5574 parameter of that level, return nonzero. */
5575
5576 static int
5577 template_parm_this_level_p (tree t, void* data)
5578 {
5579 int this_level = *(int *)data;
5580 int level;
5581
5582 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5583 level = TEMPLATE_PARM_LEVEL (t);
5584 else
5585 level = TEMPLATE_TYPE_LEVEL (t);
5586 return level == this_level;
5587 }
5588
5589 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5590 DATA is really an int, indicating the innermost outer level of parameters.
5591 If T is a template parameter of that level or further out, return
5592 nonzero. */
5593
5594 static int
5595 template_parm_outer_level (tree t, void *data)
5596 {
5597 int this_level = *(int *)data;
5598 int level;
5599
5600 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5601 level = TEMPLATE_PARM_LEVEL (t);
5602 else
5603 level = TEMPLATE_TYPE_LEVEL (t);
5604 return level <= this_level;
5605 }
5606
5607 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5608 parameters given by current_template_args, or reuses a
5609 previously existing one, if appropriate. Returns the DECL, or an
5610 equivalent one, if it is replaced via a call to duplicate_decls.
5611
5612 If IS_FRIEND is true, DECL is a friend declaration. */
5613
5614 tree
5615 push_template_decl_real (tree decl, bool is_friend)
5616 {
5617 tree tmpl;
5618 tree args;
5619 tree info;
5620 tree ctx;
5621 bool is_primary;
5622 bool is_partial;
5623 int new_template_p = 0;
5624 /* True if the template is a member template, in the sense of
5625 [temp.mem]. */
5626 bool member_template_p = false;
5627
5628 if (decl == error_mark_node || !current_template_parms)
5629 return error_mark_node;
5630
5631 /* See if this is a partial specialization. */
5632 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5633 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5634 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5635 || (VAR_P (decl)
5636 && DECL_LANG_SPECIFIC (decl)
5637 && DECL_TEMPLATE_SPECIALIZATION (decl)
5638 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5639
5640 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5641 is_friend = true;
5642
5643 if (is_friend)
5644 /* For a friend, we want the context of the friend, not
5645 the type of which it is a friend. */
5646 ctx = CP_DECL_CONTEXT (decl);
5647 else if (CP_DECL_CONTEXT (decl)
5648 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5649 /* In the case of a virtual function, we want the class in which
5650 it is defined. */
5651 ctx = CP_DECL_CONTEXT (decl);
5652 else
5653 /* Otherwise, if we're currently defining some class, the DECL
5654 is assumed to be a member of the class. */
5655 ctx = current_scope ();
5656
5657 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5658 ctx = NULL_TREE;
5659
5660 if (!DECL_CONTEXT (decl))
5661 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5662
5663 /* See if this is a primary template. */
5664 if (is_friend && ctx
5665 && uses_template_parms_level (ctx, processing_template_decl))
5666 /* A friend template that specifies a class context, i.e.
5667 template <typename T> friend void A<T>::f();
5668 is not primary. */
5669 is_primary = false;
5670 else if (TREE_CODE (decl) == TYPE_DECL
5671 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5672 is_primary = false;
5673 else
5674 is_primary = template_parm_scope_p ();
5675
5676 if (is_primary)
5677 {
5678 warning (OPT_Wtemplates, "template %qD declared", decl);
5679
5680 if (DECL_CLASS_SCOPE_P (decl))
5681 member_template_p = true;
5682 if (TREE_CODE (decl) == TYPE_DECL
5683 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5684 {
5685 error ("template class without a name");
5686 return error_mark_node;
5687 }
5688 else if (TREE_CODE (decl) == FUNCTION_DECL)
5689 {
5690 if (member_template_p)
5691 {
5692 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5693 error ("member template %qD may not have virt-specifiers", decl);
5694 }
5695 if (DECL_DESTRUCTOR_P (decl))
5696 {
5697 /* [temp.mem]
5698
5699 A destructor shall not be a member template. */
5700 error_at (DECL_SOURCE_LOCATION (decl),
5701 "destructor %qD declared as member template", decl);
5702 return error_mark_node;
5703 }
5704 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5705 && (!prototype_p (TREE_TYPE (decl))
5706 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5707 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5708 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5709 == void_list_node)))
5710 {
5711 /* [basic.stc.dynamic.allocation]
5712
5713 An allocation function can be a function
5714 template. ... Template allocation functions shall
5715 have two or more parameters. */
5716 error ("invalid template declaration of %qD", decl);
5717 return error_mark_node;
5718 }
5719 }
5720 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5721 && CLASS_TYPE_P (TREE_TYPE (decl)))
5722 {
5723 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5724 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5725 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5726 {
5727 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5728 if (TREE_CODE (t) == TYPE_DECL)
5729 t = TREE_TYPE (t);
5730 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5731 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5732 }
5733 }
5734 else if (TREE_CODE (decl) == TYPE_DECL
5735 && TYPE_DECL_ALIAS_P (decl))
5736 /* alias-declaration */
5737 gcc_assert (!DECL_ARTIFICIAL (decl));
5738 else if (VAR_P (decl))
5739 /* C++14 variable template. */;
5740 else if (TREE_CODE (decl) == CONCEPT_DECL)
5741 /* C++2a concept definitions. */;
5742 else
5743 {
5744 error ("template declaration of %q#D", decl);
5745 return error_mark_node;
5746 }
5747 }
5748
5749 /* Check to see that the rules regarding the use of default
5750 arguments are not being violated. We check args for a friend
5751 functions when we know whether it's a definition, introducing
5752 declaration or re-declaration. */
5753 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5754 check_default_tmpl_args (decl, current_template_parms,
5755 is_primary, is_partial, is_friend);
5756
5757 /* Ensure that there are no parameter packs in the type of this
5758 declaration that have not been expanded. */
5759 if (TREE_CODE (decl) == FUNCTION_DECL)
5760 {
5761 /* Check each of the arguments individually to see if there are
5762 any bare parameter packs. */
5763 tree type = TREE_TYPE (decl);
5764 tree arg = DECL_ARGUMENTS (decl);
5765 tree argtype = TYPE_ARG_TYPES (type);
5766
5767 while (arg && argtype)
5768 {
5769 if (!DECL_PACK_P (arg)
5770 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5771 {
5772 /* This is a PARM_DECL that contains unexpanded parameter
5773 packs. We have already complained about this in the
5774 check_for_bare_parameter_packs call, so just replace
5775 these types with ERROR_MARK_NODE. */
5776 TREE_TYPE (arg) = error_mark_node;
5777 TREE_VALUE (argtype) = error_mark_node;
5778 }
5779
5780 arg = DECL_CHAIN (arg);
5781 argtype = TREE_CHAIN (argtype);
5782 }
5783
5784 /* Check for bare parameter packs in the return type and the
5785 exception specifiers. */
5786 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5787 /* Errors were already issued, set return type to int
5788 as the frontend doesn't expect error_mark_node as
5789 the return type. */
5790 TREE_TYPE (type) = integer_type_node;
5791 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5792 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5793 }
5794 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5795 && TYPE_DECL_ALIAS_P (decl))
5796 ? DECL_ORIGINAL_TYPE (decl)
5797 : TREE_TYPE (decl)))
5798 {
5799 TREE_TYPE (decl) = error_mark_node;
5800 return error_mark_node;
5801 }
5802
5803 if (is_partial)
5804 return process_partial_specialization (decl);
5805
5806 args = current_template_args ();
5807
5808 if (!ctx
5809 || TREE_CODE (ctx) == FUNCTION_DECL
5810 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5811 || (TREE_CODE (decl) == TYPE_DECL
5812 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5813 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5814 {
5815 if (DECL_LANG_SPECIFIC (decl)
5816 && DECL_TEMPLATE_INFO (decl)
5817 && DECL_TI_TEMPLATE (decl))
5818 tmpl = DECL_TI_TEMPLATE (decl);
5819 /* If DECL is a TYPE_DECL for a class-template, then there won't
5820 be DECL_LANG_SPECIFIC. The information equivalent to
5821 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5822 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5823 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5824 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5825 {
5826 /* Since a template declaration already existed for this
5827 class-type, we must be redeclaring it here. Make sure
5828 that the redeclaration is valid. */
5829 redeclare_class_template (TREE_TYPE (decl),
5830 current_template_parms,
5831 current_template_constraints ());
5832 /* We don't need to create a new TEMPLATE_DECL; just use the
5833 one we already had. */
5834 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5835 }
5836 else
5837 {
5838 tmpl = build_template_decl (decl, current_template_parms,
5839 member_template_p);
5840 new_template_p = 1;
5841
5842 if (DECL_LANG_SPECIFIC (decl)
5843 && DECL_TEMPLATE_SPECIALIZATION (decl))
5844 {
5845 /* A specialization of a member template of a template
5846 class. */
5847 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5848 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5849 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5850 }
5851 }
5852 }
5853 else
5854 {
5855 tree a, t, current, parms;
5856 int i;
5857 tree tinfo = get_template_info (decl);
5858
5859 if (!tinfo)
5860 {
5861 error ("template definition of non-template %q#D", decl);
5862 return error_mark_node;
5863 }
5864
5865 tmpl = TI_TEMPLATE (tinfo);
5866
5867 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5868 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5869 && DECL_TEMPLATE_SPECIALIZATION (decl)
5870 && DECL_MEMBER_TEMPLATE_P (tmpl))
5871 {
5872 tree new_tmpl;
5873
5874 /* The declaration is a specialization of a member
5875 template, declared outside the class. Therefore, the
5876 innermost template arguments will be NULL, so we
5877 replace them with the arguments determined by the
5878 earlier call to check_explicit_specialization. */
5879 args = DECL_TI_ARGS (decl);
5880
5881 new_tmpl
5882 = build_template_decl (decl, current_template_parms,
5883 member_template_p);
5884 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5885 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5886 DECL_TI_TEMPLATE (decl) = new_tmpl;
5887 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5888 DECL_TEMPLATE_INFO (new_tmpl)
5889 = build_template_info (tmpl, args);
5890
5891 register_specialization (new_tmpl,
5892 most_general_template (tmpl),
5893 args,
5894 is_friend, 0);
5895 return decl;
5896 }
5897
5898 /* Make sure the template headers we got make sense. */
5899
5900 parms = DECL_TEMPLATE_PARMS (tmpl);
5901 i = TMPL_PARMS_DEPTH (parms);
5902 if (TMPL_ARGS_DEPTH (args) != i)
5903 {
5904 error ("expected %d levels of template parms for %q#D, got %d",
5905 i, decl, TMPL_ARGS_DEPTH (args));
5906 DECL_INTERFACE_KNOWN (decl) = 1;
5907 return error_mark_node;
5908 }
5909 else
5910 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5911 {
5912 a = TMPL_ARGS_LEVEL (args, i);
5913 t = INNERMOST_TEMPLATE_PARMS (parms);
5914
5915 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5916 {
5917 if (current == decl)
5918 error ("got %d template parameters for %q#D",
5919 TREE_VEC_LENGTH (a), decl);
5920 else
5921 error ("got %d template parameters for %q#T",
5922 TREE_VEC_LENGTH (a), current);
5923 error (" but %d required", TREE_VEC_LENGTH (t));
5924 /* Avoid crash in import_export_decl. */
5925 DECL_INTERFACE_KNOWN (decl) = 1;
5926 return error_mark_node;
5927 }
5928
5929 if (current == decl)
5930 current = ctx;
5931 else if (current == NULL_TREE)
5932 /* Can happen in erroneous input. */
5933 break;
5934 else
5935 current = get_containing_scope (current);
5936 }
5937
5938 /* Check that the parms are used in the appropriate qualifying scopes
5939 in the declarator. */
5940 if (!comp_template_args
5941 (TI_ARGS (tinfo),
5942 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5943 {
5944 error ("template arguments to %qD do not match original "
5945 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5946 if (!uses_template_parms (TI_ARGS (tinfo)))
5947 inform (input_location, "use %<template<>%> for"
5948 " an explicit specialization");
5949 /* Avoid crash in import_export_decl. */
5950 DECL_INTERFACE_KNOWN (decl) = 1;
5951 return error_mark_node;
5952 }
5953 }
5954
5955 DECL_TEMPLATE_RESULT (tmpl) = decl;
5956 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5957
5958 /* Push template declarations for global functions and types. Note
5959 that we do not try to push a global template friend declared in a
5960 template class; such a thing may well depend on the template
5961 parameters of the class. */
5962 if (new_template_p && !ctx
5963 && !(is_friend && template_class_depth (current_class_type) > 0))
5964 {
5965 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5966 if (tmpl == error_mark_node)
5967 return error_mark_node;
5968
5969 /* Hide template friend classes that haven't been declared yet. */
5970 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5971 {
5972 DECL_ANTICIPATED (tmpl) = 1;
5973 DECL_FRIEND_P (tmpl) = 1;
5974 }
5975 }
5976
5977 if (is_primary)
5978 {
5979 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5980
5981 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5982
5983 /* Give template template parms a DECL_CONTEXT of the template
5984 for which they are a parameter. */
5985 parms = INNERMOST_TEMPLATE_PARMS (parms);
5986 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5987 {
5988 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5989 if (TREE_CODE (parm) == TEMPLATE_DECL)
5990 DECL_CONTEXT (parm) = tmpl;
5991 }
5992
5993 if (TREE_CODE (decl) == TYPE_DECL
5994 && TYPE_DECL_ALIAS_P (decl))
5995 {
5996 if (tree constr
5997 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
5998 {
5999 /* ??? Why don't we do this here for all templates? */
6000 constr = build_constraints (constr, NULL_TREE);
6001 set_constraints (decl, constr);
6002 }
6003 if (complex_alias_template_p (tmpl))
6004 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6005 }
6006 }
6007
6008 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6009 back to its most general template. If TMPL is a specialization,
6010 ARGS may only have the innermost set of arguments. Add the missing
6011 argument levels if necessary. */
6012 if (DECL_TEMPLATE_INFO (tmpl))
6013 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6014
6015 info = build_template_info (tmpl, args);
6016
6017 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6018 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6019 else
6020 {
6021 if (is_primary)
6022 retrofit_lang_decl (decl);
6023 if (DECL_LANG_SPECIFIC (decl))
6024 DECL_TEMPLATE_INFO (decl) = info;
6025 }
6026
6027 if (flag_implicit_templates
6028 && !is_friend
6029 && TREE_PUBLIC (decl)
6030 && VAR_OR_FUNCTION_DECL_P (decl))
6031 /* Set DECL_COMDAT on template instantiations; if we force
6032 them to be emitted by explicit instantiation,
6033 mark_needed will tell cgraph to do the right thing. */
6034 DECL_COMDAT (decl) = true;
6035
6036 return DECL_TEMPLATE_RESULT (tmpl);
6037 }
6038
6039 tree
6040 push_template_decl (tree decl)
6041 {
6042 return push_template_decl_real (decl, false);
6043 }
6044
6045 /* FN is an inheriting constructor that inherits from the constructor
6046 template INHERITED; turn FN into a constructor template with a matching
6047 template header. */
6048
6049 tree
6050 add_inherited_template_parms (tree fn, tree inherited)
6051 {
6052 tree inner_parms
6053 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6054 inner_parms = copy_node (inner_parms);
6055 tree parms
6056 = tree_cons (size_int (processing_template_decl + 1),
6057 inner_parms, current_template_parms);
6058 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6059 tree args = template_parms_to_args (parms);
6060 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6061 TREE_TYPE (tmpl) = TREE_TYPE (fn);
6062 DECL_TEMPLATE_RESULT (tmpl) = fn;
6063 DECL_ARTIFICIAL (tmpl) = true;
6064 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6065 return tmpl;
6066 }
6067
6068 /* Called when a class template TYPE is redeclared with the indicated
6069 template PARMS, e.g.:
6070
6071 template <class T> struct S;
6072 template <class T> struct S {}; */
6073
6074 bool
6075 redeclare_class_template (tree type, tree parms, tree cons)
6076 {
6077 tree tmpl;
6078 tree tmpl_parms;
6079 int i;
6080
6081 if (!TYPE_TEMPLATE_INFO (type))
6082 {
6083 error ("%qT is not a template type", type);
6084 return false;
6085 }
6086
6087 tmpl = TYPE_TI_TEMPLATE (type);
6088 if (!PRIMARY_TEMPLATE_P (tmpl))
6089 /* The type is nested in some template class. Nothing to worry
6090 about here; there are no new template parameters for the nested
6091 type. */
6092 return true;
6093
6094 if (!parms)
6095 {
6096 error ("template specifiers not specified in declaration of %qD",
6097 tmpl);
6098 return false;
6099 }
6100
6101 parms = INNERMOST_TEMPLATE_PARMS (parms);
6102 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6103
6104 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6105 {
6106 error_n (input_location, TREE_VEC_LENGTH (parms),
6107 "redeclared with %d template parameter",
6108 "redeclared with %d template parameters",
6109 TREE_VEC_LENGTH (parms));
6110 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6111 "previous declaration %qD used %d template parameter",
6112 "previous declaration %qD used %d template parameters",
6113 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6114 return false;
6115 }
6116
6117 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6118 {
6119 tree tmpl_parm;
6120 tree parm;
6121 tree tmpl_default;
6122 tree parm_default;
6123
6124 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6125 || TREE_VEC_ELT (parms, i) == error_mark_node)
6126 continue;
6127
6128 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6129 if (error_operand_p (tmpl_parm))
6130 return false;
6131
6132 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6133 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6134 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6135
6136 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6137 TEMPLATE_DECL. */
6138 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6139 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6140 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6141 || (TREE_CODE (tmpl_parm) != PARM_DECL
6142 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6143 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6144 || (TREE_CODE (tmpl_parm) == PARM_DECL
6145 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6146 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6147 {
6148 error ("template parameter %q+#D", tmpl_parm);
6149 error ("redeclared here as %q#D", parm);
6150 return false;
6151 }
6152
6153 /* The parameters can be declared to introduce different
6154 constraints. */
6155 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6156 tree p2 = TREE_VEC_ELT (parms, i);
6157 if (!template_parameter_constraints_equivalent_p (p1, p2))
6158 {
6159 error ("declaration of template parameter %q+#D with different "
6160 "constraints", parm);
6161 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6162 "original declaration appeared here");
6163 return false;
6164 }
6165
6166 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6167 {
6168 /* We have in [temp.param]:
6169
6170 A template-parameter may not be given default arguments
6171 by two different declarations in the same scope. */
6172 error_at (input_location, "redefinition of default argument for %q#D", parm);
6173 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6174 "original definition appeared here");
6175 return false;
6176 }
6177
6178 if (parm_default != NULL_TREE)
6179 /* Update the previous template parameters (which are the ones
6180 that will really count) with the new default value. */
6181 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6182 else if (tmpl_default != NULL_TREE)
6183 /* Update the new parameters, too; they'll be used as the
6184 parameters for any members. */
6185 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6186
6187 /* Give each template template parm in this redeclaration a
6188 DECL_CONTEXT of the template for which they are a parameter. */
6189 if (TREE_CODE (parm) == TEMPLATE_DECL)
6190 {
6191 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6192 DECL_CONTEXT (parm) = tmpl;
6193 }
6194
6195 if (TREE_CODE (parm) == TYPE_DECL)
6196 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6197 }
6198
6199 tree ci = get_constraints (tmpl);
6200 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6201 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6202
6203 /* Two classes with different constraints declare different entities. */
6204 if (!cp_tree_equal (req1, req2))
6205 {
6206 error_at (input_location, "redeclaration %q#D with different "
6207 "constraints", tmpl);
6208 inform (DECL_SOURCE_LOCATION (tmpl),
6209 "original declaration appeared here");
6210 return false;
6211 }
6212
6213 return true;
6214 }
6215
6216 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6217 to be used when the caller has already checked
6218 (processing_template_decl
6219 && !instantiation_dependent_expression_p (expr)
6220 && potential_constant_expression (expr))
6221 and cleared processing_template_decl. */
6222
6223 tree
6224 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6225 {
6226 return tsubst_copy_and_build (expr,
6227 /*args=*/NULL_TREE,
6228 complain,
6229 /*in_decl=*/NULL_TREE,
6230 /*function_p=*/false,
6231 /*integral_constant_expression_p=*/true);
6232 }
6233
6234 /* Simplify EXPR if it is a non-dependent expression. Returns the
6235 (possibly simplified) expression. */
6236
6237 tree
6238 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6239 {
6240 if (expr == NULL_TREE)
6241 return NULL_TREE;
6242
6243 /* If we're in a template, but EXPR isn't value dependent, simplify
6244 it. We're supposed to treat:
6245
6246 template <typename T> void f(T[1 + 1]);
6247 template <typename T> void f(T[2]);
6248
6249 as two declarations of the same function, for example. */
6250 if (processing_template_decl
6251 && is_nondependent_constant_expression (expr))
6252 {
6253 processing_template_decl_sentinel s;
6254 expr = instantiate_non_dependent_expr_internal (expr, complain);
6255 }
6256 return expr;
6257 }
6258
6259 tree
6260 instantiate_non_dependent_expr (tree expr)
6261 {
6262 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6263 }
6264
6265 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6266 an uninstantiated expression. */
6267
6268 tree
6269 instantiate_non_dependent_or_null (tree expr)
6270 {
6271 if (expr == NULL_TREE)
6272 return NULL_TREE;
6273 if (processing_template_decl)
6274 {
6275 if (!is_nondependent_constant_expression (expr))
6276 expr = NULL_TREE;
6277 else
6278 {
6279 processing_template_decl_sentinel s;
6280 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6281 }
6282 }
6283 return expr;
6284 }
6285
6286 /* True iff T is a specialization of a variable template. */
6287
6288 bool
6289 variable_template_specialization_p (tree t)
6290 {
6291 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6292 return false;
6293 tree tmpl = DECL_TI_TEMPLATE (t);
6294 return variable_template_p (tmpl);
6295 }
6296
6297 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6298 template declaration, or a TYPE_DECL for an alias declaration. */
6299
6300 bool
6301 alias_type_or_template_p (tree t)
6302 {
6303 if (t == NULL_TREE)
6304 return false;
6305 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6306 || (TYPE_P (t)
6307 && TYPE_NAME (t)
6308 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6309 || DECL_ALIAS_TEMPLATE_P (t));
6310 }
6311
6312 /* If T is a specialization of an alias template, return it; otherwise return
6313 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6314
6315 tree
6316 alias_template_specialization_p (const_tree t,
6317 bool transparent_typedefs)
6318 {
6319 if (!TYPE_P (t))
6320 return NULL_TREE;
6321
6322 /* It's an alias template specialization if it's an alias and its
6323 TYPE_NAME is a specialization of a primary template. */
6324 if (typedef_variant_p (t))
6325 {
6326 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6327 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6328 return CONST_CAST_TREE (t);
6329 if (transparent_typedefs)
6330 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6331 (TYPE_NAME (t)),
6332 transparent_typedefs);
6333 }
6334
6335 return NULL_TREE;
6336 }
6337
6338 /* An alias template is complex from a SFINAE perspective if a template-id
6339 using that alias can be ill-formed when the expansion is not, as with
6340 the void_t template. We determine this by checking whether the
6341 expansion for the alias template uses all its template parameters. */
6342
6343 struct uses_all_template_parms_data
6344 {
6345 int level;
6346 bool *seen;
6347 };
6348
6349 static int
6350 uses_all_template_parms_r (tree t, void *data_)
6351 {
6352 struct uses_all_template_parms_data &data
6353 = *(struct uses_all_template_parms_data*)data_;
6354 tree idx = get_template_parm_index (t);
6355
6356 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6357 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6358 return 0;
6359 }
6360
6361 static bool
6362 complex_alias_template_p (const_tree tmpl)
6363 {
6364 /* A renaming alias isn't complex. */
6365 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6366 return false;
6367
6368 /* Any other constrained alias is complex. */
6369 if (get_constraints (tmpl))
6370 return true;
6371
6372 struct uses_all_template_parms_data data;
6373 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6374 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6375 data.level = TMPL_PARMS_DEPTH (parms);
6376 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6377 data.seen = XALLOCAVEC (bool, len);
6378 for (int i = 0; i < len; ++i)
6379 data.seen[i] = false;
6380
6381 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6382 for (int i = 0; i < len; ++i)
6383 if (!data.seen[i])
6384 return true;
6385 return false;
6386 }
6387
6388 /* If T is a specialization of a complex alias template with dependent
6389 template-arguments, return it; otherwise return NULL_TREE. If T is a
6390 typedef to such a specialization, return the specialization. */
6391
6392 tree
6393 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6394 {
6395 if (!TYPE_P (t) || !typedef_variant_p (t))
6396 return NULL_TREE;
6397
6398 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6399 if (tinfo
6400 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6401 && (any_dependent_template_arguments_p
6402 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6403 return CONST_CAST_TREE (t);
6404
6405 if (transparent_typedefs)
6406 {
6407 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6408 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6409 }
6410
6411 return NULL_TREE;
6412 }
6413
6414 /* Return the number of innermost template parameters in TMPL. */
6415
6416 static int
6417 num_innermost_template_parms (const_tree tmpl)
6418 {
6419 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6420 return TREE_VEC_LENGTH (parms);
6421 }
6422
6423 /* Return either TMPL or another template that it is equivalent to under DR
6424 1286: An alias that just changes the name of a template is equivalent to
6425 the other template. */
6426
6427 static tree
6428 get_underlying_template (tree tmpl)
6429 {
6430 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6431 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6432 {
6433 /* Determine if the alias is equivalent to an underlying template. */
6434 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6435 /* The underlying type may have been ill-formed. Don't proceed. */
6436 if (!orig_type)
6437 break;
6438 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6439 if (!tinfo)
6440 break;
6441
6442 tree underlying = TI_TEMPLATE (tinfo);
6443 if (!PRIMARY_TEMPLATE_P (underlying)
6444 || (num_innermost_template_parms (tmpl)
6445 != num_innermost_template_parms (underlying)))
6446 break;
6447
6448 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6449 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6450 break;
6451
6452 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6453 it's appropriate to treat a less-constrained alias as equivalent. */
6454 if (!at_least_as_constrained (underlying, tmpl))
6455 break;
6456
6457 /* Alias is equivalent. Strip it and repeat. */
6458 tmpl = underlying;
6459 }
6460
6461 return tmpl;
6462 }
6463
6464 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6465 must be a reference-to-function or a pointer-to-function type, as specified
6466 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6467 and check that the resulting function has external linkage. */
6468
6469 static tree
6470 convert_nontype_argument_function (tree type, tree expr,
6471 tsubst_flags_t complain)
6472 {
6473 tree fns = expr;
6474 tree fn, fn_no_ptr;
6475 linkage_kind linkage;
6476
6477 fn = instantiate_type (type, fns, tf_none);
6478 if (fn == error_mark_node)
6479 return error_mark_node;
6480
6481 if (value_dependent_expression_p (fn))
6482 goto accept;
6483
6484 fn_no_ptr = strip_fnptr_conv (fn);
6485 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6486 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6487 if (BASELINK_P (fn_no_ptr))
6488 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6489
6490 /* [temp.arg.nontype]/1
6491
6492 A template-argument for a non-type, non-template template-parameter
6493 shall be one of:
6494 [...]
6495 -- the address of an object or function with external [C++11: or
6496 internal] linkage. */
6497
6498 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6499 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6500 {
6501 if (complain & tf_error)
6502 {
6503 location_t loc = cp_expr_loc_or_input_loc (expr);
6504 error_at (loc, "%qE is not a valid template argument for type %qT",
6505 expr, type);
6506 if (TYPE_PTR_P (type))
6507 inform (loc, "it must be the address of a function "
6508 "with external linkage");
6509 else
6510 inform (loc, "it must be the name of a function with "
6511 "external linkage");
6512 }
6513 return NULL_TREE;
6514 }
6515
6516 linkage = decl_linkage (fn_no_ptr);
6517 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6518 {
6519 if (complain & tf_error)
6520 {
6521 location_t loc = cp_expr_loc_or_input_loc (expr);
6522 if (cxx_dialect >= cxx11)
6523 error_at (loc, "%qE is not a valid template argument for type "
6524 "%qT because %qD has no linkage",
6525 expr, type, fn_no_ptr);
6526 else
6527 error_at (loc, "%qE is not a valid template argument for type "
6528 "%qT because %qD does not have external linkage",
6529 expr, type, fn_no_ptr);
6530 }
6531 return NULL_TREE;
6532 }
6533
6534 accept:
6535 if (TYPE_REF_P (type))
6536 {
6537 if (REFERENCE_REF_P (fn))
6538 fn = TREE_OPERAND (fn, 0);
6539 else
6540 fn = build_address (fn);
6541 }
6542 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6543 fn = build_nop (type, fn);
6544
6545 return fn;
6546 }
6547
6548 /* Subroutine of convert_nontype_argument.
6549 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6550 Emit an error otherwise. */
6551
6552 static bool
6553 check_valid_ptrmem_cst_expr (tree type, tree expr,
6554 tsubst_flags_t complain)
6555 {
6556 tree orig_expr = expr;
6557 STRIP_NOPS (expr);
6558 if (null_ptr_cst_p (expr))
6559 return true;
6560 if (TREE_CODE (expr) == PTRMEM_CST
6561 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6562 PTRMEM_CST_CLASS (expr)))
6563 return true;
6564 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6565 return true;
6566 if (processing_template_decl
6567 && TREE_CODE (expr) == ADDR_EXPR
6568 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6569 return true;
6570 if (complain & tf_error)
6571 {
6572 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6573 error_at (loc, "%qE is not a valid template argument for type %qT",
6574 orig_expr, type);
6575 if (TREE_CODE (expr) != PTRMEM_CST)
6576 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6577 else
6578 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6579 }
6580 return false;
6581 }
6582
6583 /* Returns TRUE iff the address of OP is value-dependent.
6584
6585 14.6.2.4 [temp.dep.temp]:
6586 A non-integral non-type template-argument is dependent if its type is
6587 dependent or it has either of the following forms
6588 qualified-id
6589 & qualified-id
6590 and contains a nested-name-specifier which specifies a class-name that
6591 names a dependent type.
6592
6593 We generalize this to just say that the address of a member of a
6594 dependent class is value-dependent; the above doesn't cover the
6595 address of a static data member named with an unqualified-id. */
6596
6597 static bool
6598 has_value_dependent_address (tree op)
6599 {
6600 STRIP_ANY_LOCATION_WRAPPER (op);
6601
6602 /* We could use get_inner_reference here, but there's no need;
6603 this is only relevant for template non-type arguments, which
6604 can only be expressed as &id-expression. */
6605 if (DECL_P (op))
6606 {
6607 tree ctx = CP_DECL_CONTEXT (op);
6608 if (TYPE_P (ctx) && dependent_type_p (ctx))
6609 return true;
6610 }
6611
6612 return false;
6613 }
6614
6615 /* The next set of functions are used for providing helpful explanatory
6616 diagnostics for failed overload resolution. Their messages should be
6617 indented by two spaces for consistency with the messages in
6618 call.c */
6619
6620 static int
6621 unify_success (bool /*explain_p*/)
6622 {
6623 return 0;
6624 }
6625
6626 /* Other failure functions should call this one, to provide a single function
6627 for setting a breakpoint on. */
6628
6629 static int
6630 unify_invalid (bool /*explain_p*/)
6631 {
6632 return 1;
6633 }
6634
6635 static int
6636 unify_parameter_deduction_failure (bool explain_p, tree parm)
6637 {
6638 if (explain_p)
6639 inform (input_location,
6640 " couldn%'t deduce template parameter %qD", parm);
6641 return unify_invalid (explain_p);
6642 }
6643
6644 static int
6645 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6646 {
6647 if (explain_p)
6648 inform (input_location,
6649 " types %qT and %qT have incompatible cv-qualifiers",
6650 parm, arg);
6651 return unify_invalid (explain_p);
6652 }
6653
6654 static int
6655 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6656 {
6657 if (explain_p)
6658 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6659 return unify_invalid (explain_p);
6660 }
6661
6662 static int
6663 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6664 {
6665 if (explain_p)
6666 inform (input_location,
6667 " template parameter %qD is not a parameter pack, but "
6668 "argument %qD is",
6669 parm, arg);
6670 return unify_invalid (explain_p);
6671 }
6672
6673 static int
6674 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6675 {
6676 if (explain_p)
6677 inform (input_location,
6678 " template argument %qE does not match "
6679 "pointer-to-member constant %qE",
6680 arg, parm);
6681 return unify_invalid (explain_p);
6682 }
6683
6684 static int
6685 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6686 {
6687 if (explain_p)
6688 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6689 return unify_invalid (explain_p);
6690 }
6691
6692 static int
6693 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6694 {
6695 if (explain_p)
6696 inform (input_location,
6697 " inconsistent parameter pack deduction with %qT and %qT",
6698 old_arg, new_arg);
6699 return unify_invalid (explain_p);
6700 }
6701
6702 static int
6703 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6704 {
6705 if (explain_p)
6706 {
6707 if (TYPE_P (parm))
6708 inform (input_location,
6709 " deduced conflicting types for parameter %qT (%qT and %qT)",
6710 parm, first, second);
6711 else
6712 inform (input_location,
6713 " deduced conflicting values for non-type parameter "
6714 "%qE (%qE and %qE)", parm, first, second);
6715 }
6716 return unify_invalid (explain_p);
6717 }
6718
6719 static int
6720 unify_vla_arg (bool explain_p, tree arg)
6721 {
6722 if (explain_p)
6723 inform (input_location,
6724 " variable-sized array type %qT is not "
6725 "a valid template argument",
6726 arg);
6727 return unify_invalid (explain_p);
6728 }
6729
6730 static int
6731 unify_method_type_error (bool explain_p, tree arg)
6732 {
6733 if (explain_p)
6734 inform (input_location,
6735 " member function type %qT is not a valid template argument",
6736 arg);
6737 return unify_invalid (explain_p);
6738 }
6739
6740 static int
6741 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6742 {
6743 if (explain_p)
6744 {
6745 if (least_p)
6746 inform_n (input_location, wanted,
6747 " candidate expects at least %d argument, %d provided",
6748 " candidate expects at least %d arguments, %d provided",
6749 wanted, have);
6750 else
6751 inform_n (input_location, wanted,
6752 " candidate expects %d argument, %d provided",
6753 " candidate expects %d arguments, %d provided",
6754 wanted, have);
6755 }
6756 return unify_invalid (explain_p);
6757 }
6758
6759 static int
6760 unify_too_many_arguments (bool explain_p, int have, int wanted)
6761 {
6762 return unify_arity (explain_p, have, wanted);
6763 }
6764
6765 static int
6766 unify_too_few_arguments (bool explain_p, int have, int wanted,
6767 bool least_p = false)
6768 {
6769 return unify_arity (explain_p, have, wanted, least_p);
6770 }
6771
6772 static int
6773 unify_arg_conversion (bool explain_p, tree to_type,
6774 tree from_type, tree arg)
6775 {
6776 if (explain_p)
6777 inform (cp_expr_loc_or_input_loc (arg),
6778 " cannot convert %qE (type %qT) to type %qT",
6779 arg, from_type, to_type);
6780 return unify_invalid (explain_p);
6781 }
6782
6783 static int
6784 unify_no_common_base (bool explain_p, enum template_base_result r,
6785 tree parm, tree arg)
6786 {
6787 if (explain_p)
6788 switch (r)
6789 {
6790 case tbr_ambiguous_baseclass:
6791 inform (input_location, " %qT is an ambiguous base class of %qT",
6792 parm, arg);
6793 break;
6794 default:
6795 inform (input_location, " %qT is not derived from %qT", arg, parm);
6796 break;
6797 }
6798 return unify_invalid (explain_p);
6799 }
6800
6801 static int
6802 unify_inconsistent_template_template_parameters (bool explain_p)
6803 {
6804 if (explain_p)
6805 inform (input_location,
6806 " template parameters of a template template argument are "
6807 "inconsistent with other deduced template arguments");
6808 return unify_invalid (explain_p);
6809 }
6810
6811 static int
6812 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6813 {
6814 if (explain_p)
6815 inform (input_location,
6816 " cannot deduce a template for %qT from non-template type %qT",
6817 parm, arg);
6818 return unify_invalid (explain_p);
6819 }
6820
6821 static int
6822 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6823 {
6824 if (explain_p)
6825 inform (input_location,
6826 " template argument %qE does not match %qE", arg, parm);
6827 return unify_invalid (explain_p);
6828 }
6829
6830 /* True if T is a C++20 template parameter object to store the argument for a
6831 template parameter of class type. */
6832
6833 bool
6834 template_parm_object_p (const_tree t)
6835 {
6836 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6837 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6838 }
6839
6840 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6841 argument for TYPE, points to an unsuitable object. */
6842
6843 static bool
6844 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6845 {
6846 switch (TREE_CODE (expr))
6847 {
6848 CASE_CONVERT:
6849 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6850 complain);
6851
6852 case TARGET_EXPR:
6853 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6854 complain);
6855
6856 case CONSTRUCTOR:
6857 {
6858 unsigned i; tree elt;
6859 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6860 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6861 return true;
6862 }
6863 break;
6864
6865 case ADDR_EXPR:
6866 {
6867 tree decl = TREE_OPERAND (expr, 0);
6868
6869 if (!VAR_P (decl))
6870 {
6871 if (complain & tf_error)
6872 error_at (cp_expr_loc_or_input_loc (expr),
6873 "%qE is not a valid template argument of type %qT "
6874 "because %qE is not a variable", expr, type, decl);
6875 return true;
6876 }
6877 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6878 {
6879 if (complain & tf_error)
6880 error_at (cp_expr_loc_or_input_loc (expr),
6881 "%qE is not a valid template argument of type %qT "
6882 "in C++98 because %qD does not have external linkage",
6883 expr, type, decl);
6884 return true;
6885 }
6886 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6887 && decl_linkage (decl) == lk_none)
6888 {
6889 if (complain & tf_error)
6890 error_at (cp_expr_loc_or_input_loc (expr),
6891 "%qE is not a valid template argument of type %qT "
6892 "because %qD has no linkage", expr, type, decl);
6893 return true;
6894 }
6895 /* C++17: For a non-type template-parameter of reference or pointer
6896 type, the value of the constant expression shall not refer to (or
6897 for a pointer type, shall not be the address of):
6898 * a subobject (4.5),
6899 * a temporary object (15.2),
6900 * a string literal (5.13.5),
6901 * the result of a typeid expression (8.2.8), or
6902 * a predefined __func__ variable (11.4.1). */
6903 else if (DECL_ARTIFICIAL (decl))
6904 {
6905 if (complain & tf_error)
6906 error ("the address of %qD is not a valid template argument",
6907 decl);
6908 return true;
6909 }
6910 else if (!same_type_ignoring_top_level_qualifiers_p
6911 (strip_array_types (TREE_TYPE (type)),
6912 strip_array_types (TREE_TYPE (decl))))
6913 {
6914 if (complain & tf_error)
6915 error ("the address of the %qT subobject of %qD is not a "
6916 "valid template argument", TREE_TYPE (type), decl);
6917 return true;
6918 }
6919 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6920 {
6921 if (complain & tf_error)
6922 error ("the address of %qD is not a valid template argument "
6923 "because it does not have static storage duration",
6924 decl);
6925 return true;
6926 }
6927 }
6928 break;
6929
6930 default:
6931 if (!INDIRECT_TYPE_P (type))
6932 /* We're only concerned about pointers and references here. */;
6933 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6934 /* Null pointer values are OK in C++11. */;
6935 else
6936 {
6937 if (VAR_P (expr))
6938 {
6939 if (complain & tf_error)
6940 error ("%qD is not a valid template argument "
6941 "because %qD is a variable, not the address of "
6942 "a variable", expr, expr);
6943 return true;
6944 }
6945 else
6946 {
6947 if (complain & tf_error)
6948 error ("%qE is not a valid template argument for %qT "
6949 "because it is not the address of a variable",
6950 expr, type);
6951 return true;
6952 }
6953 }
6954 }
6955 return false;
6956
6957 }
6958
6959 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6960 template argument EXPR. */
6961
6962 static tree
6963 get_template_parm_object (tree expr, tsubst_flags_t complain)
6964 {
6965 if (TREE_CODE (expr) == TARGET_EXPR)
6966 expr = TARGET_EXPR_INITIAL (expr);
6967
6968 if (!TREE_CONSTANT (expr))
6969 {
6970 if ((complain & tf_error)
6971 && require_rvalue_constant_expression (expr))
6972 cxx_constant_value (expr);
6973 return error_mark_node;
6974 }
6975 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6976 return error_mark_node;
6977
6978 tree name = mangle_template_parm_object (expr);
6979 tree decl = get_global_binding (name);
6980 if (decl)
6981 return decl;
6982
6983 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6984 decl = create_temporary_var (type);
6985 TREE_STATIC (decl) = true;
6986 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6987 TREE_READONLY (decl) = true;
6988 DECL_NAME (decl) = name;
6989 SET_DECL_ASSEMBLER_NAME (decl, name);
6990 DECL_CONTEXT (decl) = global_namespace;
6991 comdat_linkage (decl);
6992 pushdecl_top_level_and_finish (decl, expr);
6993 return decl;
6994 }
6995
6996 /* Attempt to convert the non-type template parameter EXPR to the
6997 indicated TYPE. If the conversion is successful, return the
6998 converted value. If the conversion is unsuccessful, return
6999 NULL_TREE if we issued an error message, or error_mark_node if we
7000 did not. We issue error messages for out-and-out bad template
7001 parameters, but not simply because the conversion failed, since we
7002 might be just trying to do argument deduction. Both TYPE and EXPR
7003 must be non-dependent.
7004
7005 The conversion follows the special rules described in
7006 [temp.arg.nontype], and it is much more strict than an implicit
7007 conversion.
7008
7009 This function is called twice for each template argument (see
7010 lookup_template_class for a more accurate description of this
7011 problem). This means that we need to handle expressions which
7012 are not valid in a C++ source, but can be created from the
7013 first call (for instance, casts to perform conversions). These
7014 hacks can go away after we fix the double coercion problem. */
7015
7016 static tree
7017 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7018 {
7019 tree expr_type;
7020 location_t loc = cp_expr_loc_or_input_loc (expr);
7021
7022 /* Detect immediately string literals as invalid non-type argument.
7023 This special-case is not needed for correctness (we would easily
7024 catch this later), but only to provide better diagnostic for this
7025 common user mistake. As suggested by DR 100, we do not mention
7026 linkage issues in the diagnostic as this is not the point. */
7027 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7028 {
7029 if (complain & tf_error)
7030 error ("%qE is not a valid template argument for type %qT "
7031 "because string literals can never be used in this context",
7032 expr, type);
7033 return NULL_TREE;
7034 }
7035
7036 /* Add the ADDR_EXPR now for the benefit of
7037 value_dependent_expression_p. */
7038 if (TYPE_PTROBV_P (type)
7039 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7040 {
7041 expr = decay_conversion (expr, complain);
7042 if (expr == error_mark_node)
7043 return error_mark_node;
7044 }
7045
7046 /* If we are in a template, EXPR may be non-dependent, but still
7047 have a syntactic, rather than semantic, form. For example, EXPR
7048 might be a SCOPE_REF, rather than the VAR_DECL to which the
7049 SCOPE_REF refers. Preserving the qualifying scope is necessary
7050 so that access checking can be performed when the template is
7051 instantiated -- but here we need the resolved form so that we can
7052 convert the argument. */
7053 bool non_dep = false;
7054 if (TYPE_REF_OBJ_P (type)
7055 && has_value_dependent_address (expr))
7056 /* If we want the address and it's value-dependent, don't fold. */;
7057 else if (processing_template_decl
7058 && is_nondependent_constant_expression (expr))
7059 non_dep = true;
7060 if (error_operand_p (expr))
7061 return error_mark_node;
7062 expr_type = TREE_TYPE (expr);
7063
7064 /* If the argument is non-dependent, perform any conversions in
7065 non-dependent context as well. */
7066 processing_template_decl_sentinel s (non_dep);
7067 if (non_dep)
7068 expr = instantiate_non_dependent_expr_internal (expr, complain);
7069
7070 if (value_dependent_expression_p (expr))
7071 expr = canonicalize_expr_argument (expr, complain);
7072
7073 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7074 to a non-type argument of "nullptr". */
7075 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7076 expr = fold_simple (convert (type, expr));
7077
7078 /* In C++11, integral or enumeration non-type template arguments can be
7079 arbitrary constant expressions. Pointer and pointer to
7080 member arguments can be general constant expressions that evaluate
7081 to a null value, but otherwise still need to be of a specific form. */
7082 if (cxx_dialect >= cxx11)
7083 {
7084 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7085 /* A PTRMEM_CST is already constant, and a valid template
7086 argument for a parameter of pointer to member type, we just want
7087 to leave it in that form rather than lower it to a
7088 CONSTRUCTOR. */;
7089 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7090 || cxx_dialect >= cxx17)
7091 {
7092 /* Calling build_converted_constant_expr might create a call to
7093 a conversion function with a value-dependent argument, which
7094 could invoke taking the address of a temporary representing
7095 the result of the conversion. */
7096 if (COMPOUND_LITERAL_P (expr)
7097 && CONSTRUCTOR_IS_DEPENDENT (expr)
7098 && MAYBE_CLASS_TYPE_P (expr_type)
7099 && TYPE_HAS_CONVERSION (expr_type))
7100 {
7101 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
7102 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7103 return expr;
7104 }
7105 /* C++17: A template-argument for a non-type template-parameter shall
7106 be a converted constant expression (8.20) of the type of the
7107 template-parameter. */
7108 expr = build_converted_constant_expr (type, expr, complain);
7109 if (expr == error_mark_node)
7110 /* Make sure we return NULL_TREE only if we have really issued
7111 an error, as described above. */
7112 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7113 expr = maybe_constant_value (expr, NULL_TREE,
7114 /*manifestly_const_eval=*/true);
7115 expr = convert_from_reference (expr);
7116 }
7117 else if (TYPE_PTR_OR_PTRMEM_P (type))
7118 {
7119 tree folded = maybe_constant_value (expr, NULL_TREE,
7120 /*manifestly_const_eval=*/true);
7121 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7122 : null_member_pointer_value_p (folded))
7123 expr = folded;
7124 }
7125 }
7126
7127 if (TYPE_REF_P (type))
7128 expr = mark_lvalue_use (expr);
7129 else
7130 expr = mark_rvalue_use (expr);
7131
7132 /* HACK: Due to double coercion, we can get a
7133 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7134 which is the tree that we built on the first call (see
7135 below when coercing to reference to object or to reference to
7136 function). We just strip everything and get to the arg.
7137 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7138 for examples. */
7139 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7140 {
7141 tree probe_type, probe = expr;
7142 if (REFERENCE_REF_P (probe))
7143 probe = TREE_OPERAND (probe, 0);
7144 probe_type = TREE_TYPE (probe);
7145 if (TREE_CODE (probe) == NOP_EXPR)
7146 {
7147 /* ??? Maybe we could use convert_from_reference here, but we
7148 would need to relax its constraints because the NOP_EXPR
7149 could actually change the type to something more cv-qualified,
7150 and this is not folded by convert_from_reference. */
7151 tree addr = TREE_OPERAND (probe, 0);
7152 if (TYPE_REF_P (probe_type)
7153 && TREE_CODE (addr) == ADDR_EXPR
7154 && TYPE_PTR_P (TREE_TYPE (addr))
7155 && (same_type_ignoring_top_level_qualifiers_p
7156 (TREE_TYPE (probe_type),
7157 TREE_TYPE (TREE_TYPE (addr)))))
7158 {
7159 expr = TREE_OPERAND (addr, 0);
7160 expr_type = TREE_TYPE (probe_type);
7161 }
7162 }
7163 }
7164
7165 /* [temp.arg.nontype]/5, bullet 1
7166
7167 For a non-type template-parameter of integral or enumeration type,
7168 integral promotions (_conv.prom_) and integral conversions
7169 (_conv.integral_) are applied. */
7170 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7171 {
7172 if (cxx_dialect < cxx11)
7173 {
7174 tree t = build_converted_constant_expr (type, expr, complain);
7175 t = maybe_constant_value (t);
7176 if (t != error_mark_node)
7177 expr = t;
7178 }
7179
7180 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7181 return error_mark_node;
7182
7183 /* Notice that there are constant expressions like '4 % 0' which
7184 do not fold into integer constants. */
7185 if (TREE_CODE (expr) != INTEGER_CST
7186 && !value_dependent_expression_p (expr))
7187 {
7188 if (complain & tf_error)
7189 {
7190 int errs = errorcount, warns = warningcount + werrorcount;
7191 if (!require_potential_constant_expression (expr))
7192 expr = error_mark_node;
7193 else
7194 expr = cxx_constant_value (expr);
7195 if (errorcount > errs || warningcount + werrorcount > warns)
7196 inform (loc, "in template argument for type %qT", type);
7197 if (expr == error_mark_node)
7198 return NULL_TREE;
7199 /* else cxx_constant_value complained but gave us
7200 a real constant, so go ahead. */
7201 if (TREE_CODE (expr) != INTEGER_CST)
7202 {
7203 /* Some assemble time constant expressions like
7204 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7205 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7206 as we can emit them into .rodata initializers of
7207 variables, yet they can't fold into an INTEGER_CST at
7208 compile time. Refuse them here. */
7209 gcc_checking_assert (reduced_constant_expression_p (expr));
7210 error_at (loc, "template argument %qE for type %qT not "
7211 "a constant integer", expr, type);
7212 return NULL_TREE;
7213 }
7214 }
7215 else
7216 return NULL_TREE;
7217 }
7218
7219 /* Avoid typedef problems. */
7220 if (TREE_TYPE (expr) != type)
7221 expr = fold_convert (type, expr);
7222 }
7223 /* [temp.arg.nontype]/5, bullet 2
7224
7225 For a non-type template-parameter of type pointer to object,
7226 qualification conversions (_conv.qual_) and the array-to-pointer
7227 conversion (_conv.array_) are applied. */
7228 else if (TYPE_PTROBV_P (type))
7229 {
7230 tree decayed = expr;
7231
7232 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7233 decay_conversion or an explicit cast. If it's a problematic cast,
7234 we'll complain about it below. */
7235 if (TREE_CODE (expr) == NOP_EXPR)
7236 {
7237 tree probe = expr;
7238 STRIP_NOPS (probe);
7239 if (TREE_CODE (probe) == ADDR_EXPR
7240 && TYPE_PTR_P (TREE_TYPE (probe)))
7241 {
7242 expr = probe;
7243 expr_type = TREE_TYPE (expr);
7244 }
7245 }
7246
7247 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7248
7249 A template-argument for a non-type, non-template template-parameter
7250 shall be one of: [...]
7251
7252 -- the name of a non-type template-parameter;
7253 -- the address of an object or function with external linkage, [...]
7254 expressed as "& id-expression" where the & is optional if the name
7255 refers to a function or array, or if the corresponding
7256 template-parameter is a reference.
7257
7258 Here, we do not care about functions, as they are invalid anyway
7259 for a parameter of type pointer-to-object. */
7260
7261 if (value_dependent_expression_p (expr))
7262 /* Non-type template parameters are OK. */
7263 ;
7264 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7265 /* Null pointer values are OK in C++11. */;
7266 else if (TREE_CODE (expr) != ADDR_EXPR
7267 && !INDIRECT_TYPE_P (expr_type))
7268 /* Other values, like integer constants, might be valid
7269 non-type arguments of some other type. */
7270 return error_mark_node;
7271 else if (invalid_tparm_referent_p (type, expr, complain))
7272 return NULL_TREE;
7273
7274 expr = decayed;
7275
7276 expr = perform_qualification_conversions (type, expr);
7277 if (expr == error_mark_node)
7278 return error_mark_node;
7279 }
7280 /* [temp.arg.nontype]/5, bullet 3
7281
7282 For a non-type template-parameter of type reference to object, no
7283 conversions apply. The type referred to by the reference may be more
7284 cv-qualified than the (otherwise identical) type of the
7285 template-argument. The template-parameter is bound directly to the
7286 template-argument, which must be an lvalue. */
7287 else if (TYPE_REF_OBJ_P (type))
7288 {
7289 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7290 expr_type))
7291 return error_mark_node;
7292
7293 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7294 {
7295 if (complain & tf_error)
7296 error ("%qE is not a valid template argument for type %qT "
7297 "because of conflicts in cv-qualification", expr, type);
7298 return NULL_TREE;
7299 }
7300
7301 if (!lvalue_p (expr))
7302 {
7303 if (complain & tf_error)
7304 error ("%qE is not a valid template argument for type %qT "
7305 "because it is not an lvalue", expr, type);
7306 return NULL_TREE;
7307 }
7308
7309 /* [temp.arg.nontype]/1
7310
7311 A template-argument for a non-type, non-template template-parameter
7312 shall be one of: [...]
7313
7314 -- the address of an object or function with external linkage. */
7315 if (INDIRECT_REF_P (expr)
7316 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7317 {
7318 expr = TREE_OPERAND (expr, 0);
7319 if (DECL_P (expr))
7320 {
7321 if (complain & tf_error)
7322 error ("%q#D is not a valid template argument for type %qT "
7323 "because a reference variable does not have a constant "
7324 "address", expr, type);
7325 return NULL_TREE;
7326 }
7327 }
7328
7329 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7330 && value_dependent_expression_p (expr))
7331 /* OK, dependent reference. We don't want to ask whether a DECL is
7332 itself value-dependent, since what we want here is its address. */;
7333 else
7334 {
7335 expr = build_address (expr);
7336
7337 if (invalid_tparm_referent_p (type, expr, complain))
7338 return NULL_TREE;
7339 }
7340
7341 if (!same_type_p (type, TREE_TYPE (expr)))
7342 expr = build_nop (type, expr);
7343 }
7344 /* [temp.arg.nontype]/5, bullet 4
7345
7346 For a non-type template-parameter of type pointer to function, only
7347 the function-to-pointer conversion (_conv.func_) is applied. If the
7348 template-argument represents a set of overloaded functions (or a
7349 pointer to such), the matching function is selected from the set
7350 (_over.over_). */
7351 else if (TYPE_PTRFN_P (type))
7352 {
7353 /* If the argument is a template-id, we might not have enough
7354 context information to decay the pointer. */
7355 if (!type_unknown_p (expr_type))
7356 {
7357 expr = decay_conversion (expr, complain);
7358 if (expr == error_mark_node)
7359 return error_mark_node;
7360 }
7361
7362 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7363 /* Null pointer values are OK in C++11. */
7364 return perform_qualification_conversions (type, expr);
7365
7366 expr = convert_nontype_argument_function (type, expr, complain);
7367 if (!expr || expr == error_mark_node)
7368 return expr;
7369 }
7370 /* [temp.arg.nontype]/5, bullet 5
7371
7372 For a non-type template-parameter of type reference to function, no
7373 conversions apply. If the template-argument represents a set of
7374 overloaded functions, the matching function is selected from the set
7375 (_over.over_). */
7376 else if (TYPE_REFFN_P (type))
7377 {
7378 if (TREE_CODE (expr) == ADDR_EXPR)
7379 {
7380 if (complain & tf_error)
7381 {
7382 error ("%qE is not a valid template argument for type %qT "
7383 "because it is a pointer", expr, type);
7384 inform (input_location, "try using %qE instead",
7385 TREE_OPERAND (expr, 0));
7386 }
7387 return NULL_TREE;
7388 }
7389
7390 expr = convert_nontype_argument_function (type, expr, complain);
7391 if (!expr || expr == error_mark_node)
7392 return expr;
7393 }
7394 /* [temp.arg.nontype]/5, bullet 6
7395
7396 For a non-type template-parameter of type pointer to member function,
7397 no conversions apply. If the template-argument represents a set of
7398 overloaded member functions, the matching member function is selected
7399 from the set (_over.over_). */
7400 else if (TYPE_PTRMEMFUNC_P (type))
7401 {
7402 expr = instantiate_type (type, expr, tf_none);
7403 if (expr == error_mark_node)
7404 return error_mark_node;
7405
7406 /* [temp.arg.nontype] bullet 1 says the pointer to member
7407 expression must be a pointer-to-member constant. */
7408 if (!value_dependent_expression_p (expr)
7409 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7410 return NULL_TREE;
7411
7412 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7413 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7414 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7415 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7416 }
7417 /* [temp.arg.nontype]/5, bullet 7
7418
7419 For a non-type template-parameter of type pointer to data member,
7420 qualification conversions (_conv.qual_) are applied. */
7421 else if (TYPE_PTRDATAMEM_P (type))
7422 {
7423 /* [temp.arg.nontype] bullet 1 says the pointer to member
7424 expression must be a pointer-to-member constant. */
7425 if (!value_dependent_expression_p (expr)
7426 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7427 return NULL_TREE;
7428
7429 expr = perform_qualification_conversions (type, expr);
7430 if (expr == error_mark_node)
7431 return expr;
7432 }
7433 else if (NULLPTR_TYPE_P (type))
7434 {
7435 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7436 {
7437 if (complain & tf_error)
7438 error ("%qE is not a valid template argument for type %qT "
7439 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7440 return NULL_TREE;
7441 }
7442 return expr;
7443 }
7444 else if (CLASS_TYPE_P (type))
7445 {
7446 /* Replace the argument with a reference to the corresponding template
7447 parameter object. */
7448 if (!value_dependent_expression_p (expr))
7449 expr = get_template_parm_object (expr, complain);
7450 if (expr == error_mark_node)
7451 return NULL_TREE;
7452 }
7453 /* A template non-type parameter must be one of the above. */
7454 else
7455 gcc_unreachable ();
7456
7457 /* Sanity check: did we actually convert the argument to the
7458 right type? */
7459 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7460 (type, TREE_TYPE (expr)));
7461 return convert_from_reference (expr);
7462 }
7463
7464 /* Subroutine of coerce_template_template_parms, which returns 1 if
7465 PARM_PARM and ARG_PARM match using the rule for the template
7466 parameters of template template parameters. Both PARM and ARG are
7467 template parameters; the rest of the arguments are the same as for
7468 coerce_template_template_parms.
7469 */
7470 static int
7471 coerce_template_template_parm (tree parm,
7472 tree arg,
7473 tsubst_flags_t complain,
7474 tree in_decl,
7475 tree outer_args)
7476 {
7477 if (arg == NULL_TREE || error_operand_p (arg)
7478 || parm == NULL_TREE || error_operand_p (parm))
7479 return 0;
7480
7481 if (TREE_CODE (arg) != TREE_CODE (parm))
7482 return 0;
7483
7484 switch (TREE_CODE (parm))
7485 {
7486 case TEMPLATE_DECL:
7487 /* We encounter instantiations of templates like
7488 template <template <template <class> class> class TT>
7489 class C; */
7490 {
7491 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7492 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7493
7494 if (!coerce_template_template_parms
7495 (parmparm, argparm, complain, in_decl, outer_args))
7496 return 0;
7497 }
7498 /* Fall through. */
7499
7500 case TYPE_DECL:
7501 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7502 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7503 /* Argument is a parameter pack but parameter is not. */
7504 return 0;
7505 break;
7506
7507 case PARM_DECL:
7508 /* The tsubst call is used to handle cases such as
7509
7510 template <int> class C {};
7511 template <class T, template <T> class TT> class D {};
7512 D<int, C> d;
7513
7514 i.e. the parameter list of TT depends on earlier parameters. */
7515 if (!uses_template_parms (TREE_TYPE (arg)))
7516 {
7517 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7518 if (!uses_template_parms (t)
7519 && !same_type_p (t, TREE_TYPE (arg)))
7520 return 0;
7521 }
7522
7523 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7524 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7525 /* Argument is a parameter pack but parameter is not. */
7526 return 0;
7527
7528 break;
7529
7530 default:
7531 gcc_unreachable ();
7532 }
7533
7534 return 1;
7535 }
7536
7537 /* Coerce template argument list ARGLIST for use with template
7538 template-parameter TEMPL. */
7539
7540 static tree
7541 coerce_template_args_for_ttp (tree templ, tree arglist,
7542 tsubst_flags_t complain)
7543 {
7544 /* Consider an example where a template template parameter declared as
7545
7546 template <class T, class U = std::allocator<T> > class TT
7547
7548 The template parameter level of T and U are one level larger than
7549 of TT. To proper process the default argument of U, say when an
7550 instantiation `TT<int>' is seen, we need to build the full
7551 arguments containing {int} as the innermost level. Outer levels,
7552 available when not appearing as default template argument, can be
7553 obtained from the arguments of the enclosing template.
7554
7555 Suppose that TT is later substituted with std::vector. The above
7556 instantiation is `TT<int, std::allocator<T> >' with TT at
7557 level 1, and T at level 2, while the template arguments at level 1
7558 becomes {std::vector} and the inner level 2 is {int}. */
7559
7560 tree outer = DECL_CONTEXT (templ);
7561 if (outer)
7562 outer = generic_targs_for (outer);
7563 else if (current_template_parms)
7564 {
7565 /* This is an argument of the current template, so we haven't set
7566 DECL_CONTEXT yet. */
7567 tree relevant_template_parms;
7568
7569 /* Parameter levels that are greater than the level of the given
7570 template template parm are irrelevant. */
7571 relevant_template_parms = current_template_parms;
7572 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7573 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7574 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7575
7576 outer = template_parms_to_args (relevant_template_parms);
7577 }
7578
7579 if (outer)
7580 arglist = add_to_template_args (outer, arglist);
7581
7582 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7583 return coerce_template_parms (parmlist, arglist, templ,
7584 complain,
7585 /*require_all_args=*/true,
7586 /*use_default_args=*/true);
7587 }
7588
7589 /* A cache of template template parameters with match-all default
7590 arguments. */
7591 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7592
7593 /* T is a bound template template-parameter. Copy its arguments into default
7594 arguments of the template template-parameter's template parameters. */
7595
7596 static tree
7597 add_defaults_to_ttp (tree otmpl)
7598 {
7599 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7600 return *c;
7601
7602 tree ntmpl = copy_node (otmpl);
7603
7604 tree ntype = copy_node (TREE_TYPE (otmpl));
7605 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7606 TYPE_MAIN_VARIANT (ntype) = ntype;
7607 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7608 TYPE_NAME (ntype) = ntmpl;
7609 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7610
7611 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7612 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7613 TEMPLATE_PARM_DECL (idx) = ntmpl;
7614 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7615
7616 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7617 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7618 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7619 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7620 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7621 {
7622 tree o = TREE_VEC_ELT (vec, i);
7623 if (!template_parameter_pack_p (TREE_VALUE (o)))
7624 {
7625 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7626 TREE_PURPOSE (n) = any_targ_node;
7627 }
7628 }
7629
7630 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7631 return ntmpl;
7632 }
7633
7634 /* ARG is a bound potential template template-argument, and PARGS is a list
7635 of arguments for the corresponding template template-parameter. Adjust
7636 PARGS as appropriate for application to ARG's template, and if ARG is a
7637 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7638 arguments to the template template parameter. */
7639
7640 static tree
7641 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7642 {
7643 ++processing_template_decl;
7644 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7645 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7646 {
7647 /* When comparing two template template-parameters in partial ordering,
7648 rewrite the one currently being used as an argument to have default
7649 arguments for all parameters. */
7650 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7651 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7652 if (pargs != error_mark_node)
7653 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7654 TYPE_TI_ARGS (arg));
7655 }
7656 else
7657 {
7658 tree aparms
7659 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7660 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7661 /*require_all*/true,
7662 /*use_default*/true);
7663 }
7664 --processing_template_decl;
7665 return pargs;
7666 }
7667
7668 /* Subroutine of unify for the case when PARM is a
7669 BOUND_TEMPLATE_TEMPLATE_PARM. */
7670
7671 static int
7672 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7673 bool explain_p)
7674 {
7675 tree parmvec = TYPE_TI_ARGS (parm);
7676 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7677
7678 /* The template template parm might be variadic and the argument
7679 not, so flatten both argument lists. */
7680 parmvec = expand_template_argument_pack (parmvec);
7681 argvec = expand_template_argument_pack (argvec);
7682
7683 if (flag_new_ttp)
7684 {
7685 /* In keeping with P0522R0, adjust P's template arguments
7686 to apply to A's template; then flatten it again. */
7687 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7688 nparmvec = expand_template_argument_pack (nparmvec);
7689
7690 if (unify (tparms, targs, nparmvec, argvec,
7691 UNIFY_ALLOW_NONE, explain_p))
7692 return 1;
7693
7694 /* If the P0522 adjustment eliminated a pack expansion, deduce
7695 empty packs. */
7696 if (flag_new_ttp
7697 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7698 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7699 DEDUCE_EXACT, /*sub*/true, explain_p))
7700 return 1;
7701 }
7702 else
7703 {
7704 /* Deduce arguments T, i from TT<T> or TT<i>.
7705 We check each element of PARMVEC and ARGVEC individually
7706 rather than the whole TREE_VEC since they can have
7707 different number of elements, which is allowed under N2555. */
7708
7709 int len = TREE_VEC_LENGTH (parmvec);
7710
7711 /* Check if the parameters end in a pack, making them
7712 variadic. */
7713 int parm_variadic_p = 0;
7714 if (len > 0
7715 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7716 parm_variadic_p = 1;
7717
7718 for (int i = 0; i < len - parm_variadic_p; ++i)
7719 /* If the template argument list of P contains a pack
7720 expansion that is not the last template argument, the
7721 entire template argument list is a non-deduced
7722 context. */
7723 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7724 return unify_success (explain_p);
7725
7726 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7727 return unify_too_few_arguments (explain_p,
7728 TREE_VEC_LENGTH (argvec), len);
7729
7730 for (int i = 0; i < len - parm_variadic_p; ++i)
7731 if (unify (tparms, targs,
7732 TREE_VEC_ELT (parmvec, i),
7733 TREE_VEC_ELT (argvec, i),
7734 UNIFY_ALLOW_NONE, explain_p))
7735 return 1;
7736
7737 if (parm_variadic_p
7738 && unify_pack_expansion (tparms, targs,
7739 parmvec, argvec,
7740 DEDUCE_EXACT,
7741 /*subr=*/true, explain_p))
7742 return 1;
7743 }
7744
7745 return 0;
7746 }
7747
7748 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7749 template template parameters. Both PARM_PARMS and ARG_PARMS are
7750 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7751 or PARM_DECL.
7752
7753 Consider the example:
7754 template <class T> class A;
7755 template<template <class U> class TT> class B;
7756
7757 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7758 the parameters to A, and OUTER_ARGS contains A. */
7759
7760 static int
7761 coerce_template_template_parms (tree parm_parms,
7762 tree arg_parms,
7763 tsubst_flags_t complain,
7764 tree in_decl,
7765 tree outer_args)
7766 {
7767 int nparms, nargs, i;
7768 tree parm, arg;
7769 int variadic_p = 0;
7770
7771 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7772 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7773
7774 nparms = TREE_VEC_LENGTH (parm_parms);
7775 nargs = TREE_VEC_LENGTH (arg_parms);
7776
7777 if (flag_new_ttp)
7778 {
7779 /* P0522R0: A template template-parameter P is at least as specialized as
7780 a template template-argument A if, given the following rewrite to two
7781 function templates, the function template corresponding to P is at
7782 least as specialized as the function template corresponding to A
7783 according to the partial ordering rules for function templates
7784 ([temp.func.order]). Given an invented class template X with the
7785 template parameter list of A (including default arguments):
7786
7787 * Each of the two function templates has the same template parameters,
7788 respectively, as P or A.
7789
7790 * Each function template has a single function parameter whose type is
7791 a specialization of X with template arguments corresponding to the
7792 template parameters from the respective function template where, for
7793 each template parameter PP in the template parameter list of the
7794 function template, a corresponding template argument AA is formed. If
7795 PP declares a parameter pack, then AA is the pack expansion
7796 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7797
7798 If the rewrite produces an invalid type, then P is not at least as
7799 specialized as A. */
7800
7801 /* So coerce P's args to apply to A's parms, and then deduce between A's
7802 args and the converted args. If that succeeds, A is at least as
7803 specialized as P, so they match.*/
7804 tree pargs = template_parms_level_to_args (parm_parms);
7805 pargs = add_outermost_template_args (outer_args, pargs);
7806 ++processing_template_decl;
7807 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7808 /*require_all*/true, /*use_default*/true);
7809 --processing_template_decl;
7810 if (pargs != error_mark_node)
7811 {
7812 tree targs = make_tree_vec (nargs);
7813 tree aargs = template_parms_level_to_args (arg_parms);
7814 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7815 /*explain*/false))
7816 return 1;
7817 }
7818 }
7819
7820 /* Determine whether we have a parameter pack at the end of the
7821 template template parameter's template parameter list. */
7822 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7823 {
7824 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7825
7826 if (error_operand_p (parm))
7827 return 0;
7828
7829 switch (TREE_CODE (parm))
7830 {
7831 case TEMPLATE_DECL:
7832 case TYPE_DECL:
7833 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7834 variadic_p = 1;
7835 break;
7836
7837 case PARM_DECL:
7838 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7839 variadic_p = 1;
7840 break;
7841
7842 default:
7843 gcc_unreachable ();
7844 }
7845 }
7846
7847 if (nargs != nparms
7848 && !(variadic_p && nargs >= nparms - 1))
7849 return 0;
7850
7851 /* Check all of the template parameters except the parameter pack at
7852 the end (if any). */
7853 for (i = 0; i < nparms - variadic_p; ++i)
7854 {
7855 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7856 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7857 continue;
7858
7859 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7860 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7861
7862 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7863 outer_args))
7864 return 0;
7865
7866 }
7867
7868 if (variadic_p)
7869 {
7870 /* Check each of the template parameters in the template
7871 argument against the template parameter pack at the end of
7872 the template template parameter. */
7873 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7874 return 0;
7875
7876 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7877
7878 for (; i < nargs; ++i)
7879 {
7880 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7881 continue;
7882
7883 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7884
7885 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7886 outer_args))
7887 return 0;
7888 }
7889 }
7890
7891 return 1;
7892 }
7893
7894 /* Verifies that the deduced template arguments (in TARGS) for the
7895 template template parameters (in TPARMS) represent valid bindings,
7896 by comparing the template parameter list of each template argument
7897 to the template parameter list of its corresponding template
7898 template parameter, in accordance with DR150. This
7899 routine can only be called after all template arguments have been
7900 deduced. It will return TRUE if all of the template template
7901 parameter bindings are okay, FALSE otherwise. */
7902 bool
7903 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7904 {
7905 int i, ntparms = TREE_VEC_LENGTH (tparms);
7906 bool ret = true;
7907
7908 /* We're dealing with template parms in this process. */
7909 ++processing_template_decl;
7910
7911 targs = INNERMOST_TEMPLATE_ARGS (targs);
7912
7913 for (i = 0; i < ntparms; ++i)
7914 {
7915 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7916 tree targ = TREE_VEC_ELT (targs, i);
7917
7918 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7919 {
7920 tree packed_args = NULL_TREE;
7921 int idx, len = 1;
7922
7923 if (ARGUMENT_PACK_P (targ))
7924 {
7925 /* Look inside the argument pack. */
7926 packed_args = ARGUMENT_PACK_ARGS (targ);
7927 len = TREE_VEC_LENGTH (packed_args);
7928 }
7929
7930 for (idx = 0; idx < len; ++idx)
7931 {
7932 tree targ_parms = NULL_TREE;
7933
7934 if (packed_args)
7935 /* Extract the next argument from the argument
7936 pack. */
7937 targ = TREE_VEC_ELT (packed_args, idx);
7938
7939 if (PACK_EXPANSION_P (targ))
7940 /* Look at the pattern of the pack expansion. */
7941 targ = PACK_EXPANSION_PATTERN (targ);
7942
7943 /* Extract the template parameters from the template
7944 argument. */
7945 if (TREE_CODE (targ) == TEMPLATE_DECL)
7946 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7947 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7948 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7949
7950 /* Verify that we can coerce the template template
7951 parameters from the template argument to the template
7952 parameter. This requires an exact match. */
7953 if (targ_parms
7954 && !coerce_template_template_parms
7955 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7956 targ_parms,
7957 tf_none,
7958 tparm,
7959 targs))
7960 {
7961 ret = false;
7962 goto out;
7963 }
7964 }
7965 }
7966 }
7967
7968 out:
7969
7970 --processing_template_decl;
7971 return ret;
7972 }
7973
7974 /* Since type attributes aren't mangled, we need to strip them from
7975 template type arguments. */
7976
7977 static tree
7978 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7979 {
7980 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7981 return arg;
7982 bool removed_attributes = false;
7983 tree canon = strip_typedefs (arg, &removed_attributes);
7984 if (removed_attributes
7985 && (complain & tf_warning))
7986 warning (OPT_Wignored_attributes,
7987 "ignoring attributes on template argument %qT", arg);
7988 return canon;
7989 }
7990
7991 /* And from inside dependent non-type arguments like sizeof(Type). */
7992
7993 static tree
7994 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7995 {
7996 if (!arg || arg == error_mark_node)
7997 return arg;
7998 bool removed_attributes = false;
7999 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8000 if (removed_attributes
8001 && (complain & tf_warning))
8002 warning (OPT_Wignored_attributes,
8003 "ignoring attributes in template argument %qE", arg);
8004 return canon;
8005 }
8006
8007 // A template declaration can be substituted for a constrained
8008 // template template parameter only when the argument is more
8009 // constrained than the parameter.
8010 static bool
8011 is_compatible_template_arg (tree parm, tree arg)
8012 {
8013 tree parm_cons = get_constraints (parm);
8014
8015 /* For now, allow constrained template template arguments
8016 and unconstrained template template parameters. */
8017 if (parm_cons == NULL_TREE)
8018 return true;
8019
8020 /* If the template parameter is constrained, we need to rewrite its
8021 constraints in terms of the ARG's template parameters. This ensures
8022 that all of the template parameter types will have the same depth.
8023
8024 Note that this is only valid when coerce_template_template_parm is
8025 true for the innermost template parameters of PARM and ARG. In other
8026 words, because coercion is successful, this conversion will be valid. */
8027 tree new_args = NULL_TREE;
8028 if (parm_cons)
8029 {
8030 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8031 new_args = template_parms_level_to_args (aparms);
8032 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8033 tf_none, NULL_TREE);
8034 if (parm_cons == error_mark_node)
8035 return false;
8036 }
8037
8038 return weakly_subsumes (parm_cons, new_args, arg);
8039 }
8040
8041 // Convert a placeholder argument into a binding to the original
8042 // parameter. The original parameter is saved as the TREE_TYPE of
8043 // ARG.
8044 static inline tree
8045 convert_wildcard_argument (tree parm, tree arg)
8046 {
8047 TREE_TYPE (arg) = parm;
8048 return arg;
8049 }
8050
8051 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8052 because one of them is dependent. But we need to represent the
8053 conversion for the benefit of cp_tree_equal. */
8054
8055 static tree
8056 maybe_convert_nontype_argument (tree type, tree arg)
8057 {
8058 /* Auto parms get no conversion. */
8059 if (type_uses_auto (type))
8060 return arg;
8061 /* We don't need or want to add this conversion now if we're going to use the
8062 argument for deduction. */
8063 if (value_dependent_expression_p (arg))
8064 return arg;
8065
8066 type = cv_unqualified (type);
8067 tree argtype = TREE_TYPE (arg);
8068 if (same_type_p (type, argtype))
8069 return arg;
8070
8071 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8072 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8073 return arg;
8074 }
8075
8076 /* Convert the indicated template ARG as necessary to match the
8077 indicated template PARM. Returns the converted ARG, or
8078 error_mark_node if the conversion was unsuccessful. Error and
8079 warning messages are issued under control of COMPLAIN. This
8080 conversion is for the Ith parameter in the parameter list. ARGS is
8081 the full set of template arguments deduced so far. */
8082
8083 static tree
8084 convert_template_argument (tree parm,
8085 tree arg,
8086 tree args,
8087 tsubst_flags_t complain,
8088 int i,
8089 tree in_decl)
8090 {
8091 tree orig_arg;
8092 tree val;
8093 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8094
8095 if (parm == error_mark_node || error_operand_p (arg))
8096 return error_mark_node;
8097
8098 /* Trivially convert placeholders. */
8099 if (TREE_CODE (arg) == WILDCARD_DECL)
8100 return convert_wildcard_argument (parm, arg);
8101
8102 if (arg == any_targ_node)
8103 return arg;
8104
8105 if (TREE_CODE (arg) == TREE_LIST
8106 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8107 {
8108 /* The template argument was the name of some
8109 member function. That's usually
8110 invalid, but static members are OK. In any
8111 case, grab the underlying fields/functions
8112 and issue an error later if required. */
8113 TREE_TYPE (arg) = unknown_type_node;
8114 }
8115
8116 orig_arg = arg;
8117
8118 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8119 requires_type = (TREE_CODE (parm) == TYPE_DECL
8120 || requires_tmpl_type);
8121
8122 /* When determining whether an argument pack expansion is a template,
8123 look at the pattern. */
8124 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8125 arg = PACK_EXPANSION_PATTERN (arg);
8126
8127 /* Deal with an injected-class-name used as a template template arg. */
8128 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8129 {
8130 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8131 if (TREE_CODE (t) == TEMPLATE_DECL)
8132 {
8133 if (cxx_dialect >= cxx11)
8134 /* OK under DR 1004. */;
8135 else if (complain & tf_warning_or_error)
8136 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8137 " used as template template argument", TYPE_NAME (arg));
8138 else if (flag_pedantic_errors)
8139 t = arg;
8140
8141 arg = t;
8142 }
8143 }
8144
8145 is_tmpl_type =
8146 ((TREE_CODE (arg) == TEMPLATE_DECL
8147 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8148 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8149 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8150 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8151
8152 if (is_tmpl_type
8153 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8154 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8155 arg = TYPE_STUB_DECL (arg);
8156
8157 is_type = TYPE_P (arg) || is_tmpl_type;
8158
8159 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8160 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8161 {
8162 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8163 {
8164 if (complain & tf_error)
8165 error ("invalid use of destructor %qE as a type", orig_arg);
8166 return error_mark_node;
8167 }
8168
8169 permerror (input_location,
8170 "to refer to a type member of a template parameter, "
8171 "use %<typename %E%>", orig_arg);
8172
8173 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8174 TREE_OPERAND (arg, 1),
8175 typename_type,
8176 complain);
8177 arg = orig_arg;
8178 is_type = 1;
8179 }
8180 if (is_type != requires_type)
8181 {
8182 if (in_decl)
8183 {
8184 if (complain & tf_error)
8185 {
8186 error ("type/value mismatch at argument %d in template "
8187 "parameter list for %qD",
8188 i + 1, in_decl);
8189 if (is_type)
8190 {
8191 /* The template argument is a type, but we're expecting
8192 an expression. */
8193 inform (input_location,
8194 " expected a constant of type %qT, got %qT",
8195 TREE_TYPE (parm),
8196 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8197 /* [temp.arg]/2: "In a template-argument, an ambiguity
8198 between a type-id and an expression is resolved to a
8199 type-id, regardless of the form of the corresponding
8200 template-parameter." So give the user a clue. */
8201 if (TREE_CODE (arg) == FUNCTION_TYPE)
8202 inform (input_location, " ambiguous template argument "
8203 "for non-type template parameter is treated as "
8204 "function type");
8205 }
8206 else if (requires_tmpl_type)
8207 inform (input_location,
8208 " expected a class template, got %qE", orig_arg);
8209 else
8210 inform (input_location,
8211 " expected a type, got %qE", orig_arg);
8212 }
8213 }
8214 return error_mark_node;
8215 }
8216 if (is_tmpl_type ^ requires_tmpl_type)
8217 {
8218 if (in_decl && (complain & tf_error))
8219 {
8220 error ("type/value mismatch at argument %d in template "
8221 "parameter list for %qD",
8222 i + 1, in_decl);
8223 if (is_tmpl_type)
8224 inform (input_location,
8225 " expected a type, got %qT", DECL_NAME (arg));
8226 else
8227 inform (input_location,
8228 " expected a class template, got %qT", orig_arg);
8229 }
8230 return error_mark_node;
8231 }
8232
8233 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8234 /* We already did the appropriate conversion when packing args. */
8235 val = orig_arg;
8236 else if (is_type)
8237 {
8238 if (requires_tmpl_type)
8239 {
8240 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8241 /* The number of argument required is not known yet.
8242 Just accept it for now. */
8243 val = orig_arg;
8244 else
8245 {
8246 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8247 tree argparm;
8248
8249 /* Strip alias templates that are equivalent to another
8250 template. */
8251 arg = get_underlying_template (arg);
8252 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8253
8254 if (coerce_template_template_parms (parmparm, argparm,
8255 complain, in_decl,
8256 args))
8257 {
8258 val = arg;
8259
8260 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8261 TEMPLATE_DECL. */
8262 if (val != error_mark_node)
8263 {
8264 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8265 val = TREE_TYPE (val);
8266 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8267 val = make_pack_expansion (val, complain);
8268 }
8269 }
8270 else
8271 {
8272 if (in_decl && (complain & tf_error))
8273 {
8274 error ("type/value mismatch at argument %d in "
8275 "template parameter list for %qD",
8276 i + 1, in_decl);
8277 inform (input_location,
8278 " expected a template of type %qD, got %qT",
8279 parm, orig_arg);
8280 }
8281
8282 val = error_mark_node;
8283 }
8284
8285 // Check that the constraints are compatible before allowing the
8286 // substitution.
8287 if (val != error_mark_node)
8288 if (!is_compatible_template_arg (parm, arg))
8289 {
8290 if (in_decl && (complain & tf_error))
8291 {
8292 error ("constraint mismatch at argument %d in "
8293 "template parameter list for %qD",
8294 i + 1, in_decl);
8295 inform (input_location, " expected %qD but got %qD",
8296 parm, arg);
8297 }
8298 val = error_mark_node;
8299 }
8300 }
8301 }
8302 else
8303 val = orig_arg;
8304 /* We only form one instance of each template specialization.
8305 Therefore, if we use a non-canonical variant (i.e., a
8306 typedef), any future messages referring to the type will use
8307 the typedef, which is confusing if those future uses do not
8308 themselves also use the typedef. */
8309 if (TYPE_P (val))
8310 val = canonicalize_type_argument (val, complain);
8311 }
8312 else
8313 {
8314 tree t = TREE_TYPE (parm);
8315
8316 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8317 > TMPL_ARGS_DEPTH (args))
8318 /* We don't have enough levels of args to do any substitution. This
8319 can happen in the context of -fnew-ttp-matching. */;
8320 else if (tree a = type_uses_auto (t))
8321 {
8322 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8323 if (t == error_mark_node)
8324 return error_mark_node;
8325 }
8326 else
8327 t = tsubst (t, args, complain, in_decl);
8328
8329 if (invalid_nontype_parm_type_p (t, complain))
8330 return error_mark_node;
8331
8332 if (t != TREE_TYPE (parm))
8333 t = canonicalize_type_argument (t, complain);
8334
8335 if (!type_dependent_expression_p (orig_arg)
8336 && !uses_template_parms (t))
8337 /* We used to call digest_init here. However, digest_init
8338 will report errors, which we don't want when complain
8339 is zero. More importantly, digest_init will try too
8340 hard to convert things: for example, `0' should not be
8341 converted to pointer type at this point according to
8342 the standard. Accepting this is not merely an
8343 extension, since deciding whether or not these
8344 conversions can occur is part of determining which
8345 function template to call, or whether a given explicit
8346 argument specification is valid. */
8347 val = convert_nontype_argument (t, orig_arg, complain);
8348 else
8349 {
8350 val = canonicalize_expr_argument (orig_arg, complain);
8351 val = maybe_convert_nontype_argument (t, val);
8352 }
8353
8354
8355 if (val == NULL_TREE)
8356 val = error_mark_node;
8357 else if (val == error_mark_node && (complain & tf_error))
8358 error_at (cp_expr_loc_or_input_loc (orig_arg),
8359 "could not convert template argument %qE from %qT to %qT",
8360 orig_arg, TREE_TYPE (orig_arg), t);
8361
8362 if (INDIRECT_REF_P (val))
8363 {
8364 /* Reject template arguments that are references to built-in
8365 functions with no library fallbacks. */
8366 const_tree inner = TREE_OPERAND (val, 0);
8367 const_tree innertype = TREE_TYPE (inner);
8368 if (innertype
8369 && TYPE_REF_P (innertype)
8370 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8371 && TREE_OPERAND_LENGTH (inner) > 0
8372 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8373 return error_mark_node;
8374 }
8375
8376 if (TREE_CODE (val) == SCOPE_REF)
8377 {
8378 /* Strip typedefs from the SCOPE_REF. */
8379 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8380 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8381 complain);
8382 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8383 QUALIFIED_NAME_IS_TEMPLATE (val));
8384 }
8385 }
8386
8387 return val;
8388 }
8389
8390 /* Coerces the remaining template arguments in INNER_ARGS (from
8391 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8392 Returns the coerced argument pack. PARM_IDX is the position of this
8393 parameter in the template parameter list. ARGS is the original
8394 template argument list. */
8395 static tree
8396 coerce_template_parameter_pack (tree parms,
8397 int parm_idx,
8398 tree args,
8399 tree inner_args,
8400 int arg_idx,
8401 tree new_args,
8402 int* lost,
8403 tree in_decl,
8404 tsubst_flags_t complain)
8405 {
8406 tree parm = TREE_VEC_ELT (parms, parm_idx);
8407 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8408 tree packed_args;
8409 tree argument_pack;
8410 tree packed_parms = NULL_TREE;
8411
8412 if (arg_idx > nargs)
8413 arg_idx = nargs;
8414
8415 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8416 {
8417 /* When the template parameter is a non-type template parameter pack
8418 or template template parameter pack whose type or template
8419 parameters use parameter packs, we know exactly how many arguments
8420 we are looking for. Build a vector of the instantiated decls for
8421 these template parameters in PACKED_PARMS. */
8422 /* We can't use make_pack_expansion here because it would interpret a
8423 _DECL as a use rather than a declaration. */
8424 tree decl = TREE_VALUE (parm);
8425 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8426 SET_PACK_EXPANSION_PATTERN (exp, decl);
8427 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8428 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8429
8430 TREE_VEC_LENGTH (args)--;
8431 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8432 TREE_VEC_LENGTH (args)++;
8433
8434 if (packed_parms == error_mark_node)
8435 return error_mark_node;
8436
8437 /* If we're doing a partial instantiation of a member template,
8438 verify that all of the types used for the non-type
8439 template parameter pack are, in fact, valid for non-type
8440 template parameters. */
8441 if (arg_idx < nargs
8442 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8443 {
8444 int j, len = TREE_VEC_LENGTH (packed_parms);
8445 for (j = 0; j < len; ++j)
8446 {
8447 tree t = TREE_VEC_ELT (packed_parms, j);
8448 if (TREE_CODE (t) == PARM_DECL
8449 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8450 return error_mark_node;
8451 }
8452 /* We don't know how many args we have yet, just
8453 use the unconverted ones for now. */
8454 return NULL_TREE;
8455 }
8456
8457 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8458 }
8459 /* Check if we have a placeholder pack, which indicates we're
8460 in the context of a introduction list. In that case we want
8461 to match this pack to the single placeholder. */
8462 else if (arg_idx < nargs
8463 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8464 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8465 {
8466 nargs = arg_idx + 1;
8467 packed_args = make_tree_vec (1);
8468 }
8469 else
8470 packed_args = make_tree_vec (nargs - arg_idx);
8471
8472 /* Convert the remaining arguments, which will be a part of the
8473 parameter pack "parm". */
8474 int first_pack_arg = arg_idx;
8475 for (; arg_idx < nargs; ++arg_idx)
8476 {
8477 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8478 tree actual_parm = TREE_VALUE (parm);
8479 int pack_idx = arg_idx - first_pack_arg;
8480
8481 if (packed_parms)
8482 {
8483 /* Once we've packed as many args as we have types, stop. */
8484 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8485 break;
8486 else if (PACK_EXPANSION_P (arg))
8487 /* We don't know how many args we have yet, just
8488 use the unconverted ones for now. */
8489 return NULL_TREE;
8490 else
8491 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8492 }
8493
8494 if (arg == error_mark_node)
8495 {
8496 if (complain & tf_error)
8497 error ("template argument %d is invalid", arg_idx + 1);
8498 }
8499 else
8500 arg = convert_template_argument (actual_parm,
8501 arg, new_args, complain, parm_idx,
8502 in_decl);
8503 if (arg == error_mark_node)
8504 (*lost)++;
8505 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8506 }
8507
8508 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8509 && TREE_VEC_LENGTH (packed_args) > 0)
8510 {
8511 if (complain & tf_error)
8512 error ("wrong number of template arguments (%d, should be %d)",
8513 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8514 return error_mark_node;
8515 }
8516
8517 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8518 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8519 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8520 else
8521 {
8522 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8523 TREE_CONSTANT (argument_pack) = 1;
8524 }
8525
8526 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8527 if (CHECKING_P)
8528 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8529 TREE_VEC_LENGTH (packed_args));
8530 return argument_pack;
8531 }
8532
8533 /* Returns the number of pack expansions in the template argument vector
8534 ARGS. */
8535
8536 static int
8537 pack_expansion_args_count (tree args)
8538 {
8539 int i;
8540 int count = 0;
8541 if (args)
8542 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8543 {
8544 tree elt = TREE_VEC_ELT (args, i);
8545 if (elt && PACK_EXPANSION_P (elt))
8546 ++count;
8547 }
8548 return count;
8549 }
8550
8551 /* Convert all template arguments to their appropriate types, and
8552 return a vector containing the innermost resulting template
8553 arguments. If any error occurs, return error_mark_node. Error and
8554 warning messages are issued under control of COMPLAIN.
8555
8556 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8557 for arguments not specified in ARGS. Otherwise, if
8558 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8559 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8560 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8561 ARGS. */
8562
8563 static tree
8564 coerce_template_parms (tree parms,
8565 tree args,
8566 tree in_decl,
8567 tsubst_flags_t complain,
8568 bool require_all_args,
8569 bool use_default_args)
8570 {
8571 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8572 tree orig_inner_args;
8573 tree inner_args;
8574 tree new_args;
8575 tree new_inner_args;
8576
8577 /* When used as a boolean value, indicates whether this is a
8578 variadic template parameter list. Since it's an int, we can also
8579 subtract it from nparms to get the number of non-variadic
8580 parameters. */
8581 int variadic_p = 0;
8582 int variadic_args_p = 0;
8583 int post_variadic_parms = 0;
8584
8585 /* Adjustment to nparms for fixed parameter packs. */
8586 int fixed_pack_adjust = 0;
8587 int fixed_packs = 0;
8588 int missing = 0;
8589
8590 /* Likewise for parameters with default arguments. */
8591 int default_p = 0;
8592
8593 if (args == error_mark_node)
8594 return error_mark_node;
8595
8596 nparms = TREE_VEC_LENGTH (parms);
8597
8598 /* Determine if there are any parameter packs or default arguments. */
8599 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8600 {
8601 tree parm = TREE_VEC_ELT (parms, parm_idx);
8602 if (variadic_p)
8603 ++post_variadic_parms;
8604 if (template_parameter_pack_p (TREE_VALUE (parm)))
8605 ++variadic_p;
8606 if (TREE_PURPOSE (parm))
8607 ++default_p;
8608 }
8609
8610 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8611 /* If there are no parameters that follow a parameter pack, we need to
8612 expand any argument packs so that we can deduce a parameter pack from
8613 some non-packed args followed by an argument pack, as in variadic85.C.
8614 If there are such parameters, we need to leave argument packs intact
8615 so the arguments are assigned properly. This can happen when dealing
8616 with a nested class inside a partial specialization of a class
8617 template, as in variadic92.C, or when deducing a template parameter pack
8618 from a sub-declarator, as in variadic114.C. */
8619 if (!post_variadic_parms)
8620 inner_args = expand_template_argument_pack (inner_args);
8621
8622 /* Count any pack expansion args. */
8623 variadic_args_p = pack_expansion_args_count (inner_args);
8624
8625 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8626 if ((nargs - variadic_args_p > nparms && !variadic_p)
8627 || (nargs < nparms - variadic_p
8628 && require_all_args
8629 && !variadic_args_p
8630 && (!use_default_args
8631 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8632 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8633 {
8634 bad_nargs:
8635 if (complain & tf_error)
8636 {
8637 if (variadic_p || default_p)
8638 {
8639 nparms -= variadic_p + default_p;
8640 error ("wrong number of template arguments "
8641 "(%d, should be at least %d)", nargs, nparms);
8642 }
8643 else
8644 error ("wrong number of template arguments "
8645 "(%d, should be %d)", nargs, nparms);
8646
8647 if (in_decl)
8648 inform (DECL_SOURCE_LOCATION (in_decl),
8649 "provided for %qD", in_decl);
8650 }
8651
8652 return error_mark_node;
8653 }
8654 /* We can't pass a pack expansion to a non-pack parameter of an alias
8655 template (DR 1430). */
8656 else if (in_decl
8657 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8658 || concept_definition_p (in_decl))
8659 && variadic_args_p
8660 && nargs - variadic_args_p < nparms - variadic_p)
8661 {
8662 if (complain & tf_error)
8663 {
8664 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8665 {
8666 tree arg = TREE_VEC_ELT (inner_args, i);
8667 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8668
8669 if (PACK_EXPANSION_P (arg)
8670 && !template_parameter_pack_p (parm))
8671 {
8672 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8673 error_at (location_of (arg),
8674 "pack expansion argument for non-pack parameter "
8675 "%qD of alias template %qD", parm, in_decl);
8676 else
8677 error_at (location_of (arg),
8678 "pack expansion argument for non-pack parameter "
8679 "%qD of concept %qD", parm, in_decl);
8680 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8681 goto found;
8682 }
8683 }
8684 gcc_unreachable ();
8685 found:;
8686 }
8687 return error_mark_node;
8688 }
8689
8690 /* We need to evaluate the template arguments, even though this
8691 template-id may be nested within a "sizeof". */
8692 cp_evaluated ev;
8693
8694 new_inner_args = make_tree_vec (nparms);
8695 new_args = add_outermost_template_args (args, new_inner_args);
8696 int pack_adjust = 0;
8697 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8698 {
8699 tree arg;
8700 tree parm;
8701
8702 /* Get the Ith template parameter. */
8703 parm = TREE_VEC_ELT (parms, parm_idx);
8704
8705 if (parm == error_mark_node)
8706 {
8707 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8708 continue;
8709 }
8710
8711 /* Calculate the next argument. */
8712 if (arg_idx < nargs)
8713 arg = TREE_VEC_ELT (inner_args, arg_idx);
8714 else
8715 arg = NULL_TREE;
8716
8717 if (template_parameter_pack_p (TREE_VALUE (parm))
8718 && (arg || require_all_args || !(complain & tf_partial))
8719 && !(arg && ARGUMENT_PACK_P (arg)))
8720 {
8721 /* Some arguments will be placed in the
8722 template parameter pack PARM. */
8723 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8724 inner_args, arg_idx,
8725 new_args, &lost,
8726 in_decl, complain);
8727
8728 if (arg == NULL_TREE)
8729 {
8730 /* We don't know how many args we have yet, just use the
8731 unconverted (and still packed) ones for now. */
8732 new_inner_args = orig_inner_args;
8733 arg_idx = nargs;
8734 break;
8735 }
8736
8737 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8738
8739 /* Store this argument. */
8740 if (arg == error_mark_node)
8741 {
8742 lost++;
8743 /* We are done with all of the arguments. */
8744 arg_idx = nargs;
8745 break;
8746 }
8747 else
8748 {
8749 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8750 arg_idx += pack_adjust;
8751 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8752 {
8753 ++fixed_packs;
8754 fixed_pack_adjust += pack_adjust;
8755 }
8756 }
8757
8758 continue;
8759 }
8760 else if (arg)
8761 {
8762 if (PACK_EXPANSION_P (arg))
8763 {
8764 /* "If every valid specialization of a variadic template
8765 requires an empty template parameter pack, the template is
8766 ill-formed, no diagnostic required." So check that the
8767 pattern works with this parameter. */
8768 tree pattern = PACK_EXPANSION_PATTERN (arg);
8769 tree conv = convert_template_argument (TREE_VALUE (parm),
8770 pattern, new_args,
8771 complain, parm_idx,
8772 in_decl);
8773 if (conv == error_mark_node)
8774 {
8775 if (complain & tf_error)
8776 inform (input_location, "so any instantiation with a "
8777 "non-empty parameter pack would be ill-formed");
8778 ++lost;
8779 }
8780 else if (TYPE_P (conv) && !TYPE_P (pattern))
8781 /* Recover from missing typename. */
8782 TREE_VEC_ELT (inner_args, arg_idx)
8783 = make_pack_expansion (conv, complain);
8784
8785 /* We don't know how many args we have yet, just
8786 use the unconverted ones for now. */
8787 new_inner_args = inner_args;
8788 arg_idx = nargs;
8789 break;
8790 }
8791 }
8792 else if (require_all_args)
8793 {
8794 /* There must be a default arg in this case. */
8795 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8796 complain, in_decl);
8797 /* The position of the first default template argument,
8798 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8799 Record that. */
8800 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8801 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8802 arg_idx - pack_adjust);
8803 }
8804 else
8805 break;
8806
8807 if (arg == error_mark_node)
8808 {
8809 if (complain & tf_error)
8810 error ("template argument %d is invalid", arg_idx + 1);
8811 }
8812 else if (!arg)
8813 {
8814 /* This can occur if there was an error in the template
8815 parameter list itself (which we would already have
8816 reported) that we are trying to recover from, e.g., a class
8817 template with a parameter list such as
8818 template<typename..., typename> (cpp0x/variadic150.C). */
8819 ++lost;
8820
8821 /* This can also happen with a fixed parameter pack (71834). */
8822 if (arg_idx >= nargs)
8823 ++missing;
8824 }
8825 else
8826 arg = convert_template_argument (TREE_VALUE (parm),
8827 arg, new_args, complain,
8828 parm_idx, in_decl);
8829
8830 if (arg == error_mark_node)
8831 lost++;
8832
8833 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8834 }
8835
8836 if (missing || arg_idx < nargs - variadic_args_p)
8837 {
8838 /* If we had fixed parameter packs, we didn't know how many arguments we
8839 actually needed earlier; now we do. */
8840 nparms += fixed_pack_adjust;
8841 variadic_p -= fixed_packs;
8842 goto bad_nargs;
8843 }
8844
8845 if (arg_idx < nargs)
8846 {
8847 /* We had some pack expansion arguments that will only work if the packs
8848 are empty, but wait until instantiation time to complain.
8849 See variadic-ttp3.C. */
8850
8851 /* Except that we can't provide empty packs to alias templates or
8852 concepts when there are no corresponding parameters. Basically,
8853 we can get here with this:
8854
8855 template<typename T> concept C = true;
8856
8857 template<typename... Args>
8858 requires C<Args...>
8859 void f();
8860
8861 When parsing C<Args...>, we try to form a concept check of
8862 C<?, Args...>. Without the extra check for substituting an empty
8863 pack past the last parameter, we can accept the check as valid.
8864
8865 FIXME: This may be valid for alias templates (but I doubt it).
8866
8867 FIXME: The error could be better also. */
8868 if (in_decl && concept_definition_p (in_decl))
8869 {
8870 if (complain & tf_error)
8871 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8872 "too many arguments");
8873 return error_mark_node;
8874 }
8875
8876 int len = nparms + (nargs - arg_idx);
8877 tree args = make_tree_vec (len);
8878 int i = 0;
8879 for (; i < nparms; ++i)
8880 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8881 for (; i < len; ++i, ++arg_idx)
8882 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8883 arg_idx - pack_adjust);
8884 new_inner_args = args;
8885 }
8886
8887 if (lost)
8888 {
8889 gcc_assert (!(complain & tf_error) || seen_error ());
8890 return error_mark_node;
8891 }
8892
8893 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8894 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8895 TREE_VEC_LENGTH (new_inner_args));
8896
8897 return new_inner_args;
8898 }
8899
8900 /* Convert all template arguments to their appropriate types, and
8901 return a vector containing the innermost resulting template
8902 arguments. If any error occurs, return error_mark_node. Error and
8903 warning messages are not issued.
8904
8905 Note that no function argument deduction is performed, and default
8906 arguments are used to fill in unspecified arguments. */
8907 tree
8908 coerce_template_parms (tree parms, tree args, tree in_decl)
8909 {
8910 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8911 }
8912
8913 /* Convert all template arguments to their appropriate type, and
8914 instantiate default arguments as needed. This returns a vector
8915 containing the innermost resulting template arguments, or
8916 error_mark_node if unsuccessful. */
8917 tree
8918 coerce_template_parms (tree parms, tree args, tree in_decl,
8919 tsubst_flags_t complain)
8920 {
8921 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8922 }
8923
8924 /* Like coerce_template_parms. If PARMS represents all template
8925 parameters levels, this function returns a vector of vectors
8926 representing all the resulting argument levels. Note that in this
8927 case, only the innermost arguments are coerced because the
8928 outermost ones are supposed to have been coerced already.
8929
8930 Otherwise, if PARMS represents only (the innermost) vector of
8931 parameters, this function returns a vector containing just the
8932 innermost resulting arguments. */
8933
8934 static tree
8935 coerce_innermost_template_parms (tree parms,
8936 tree args,
8937 tree in_decl,
8938 tsubst_flags_t complain,
8939 bool require_all_args,
8940 bool use_default_args)
8941 {
8942 int parms_depth = TMPL_PARMS_DEPTH (parms);
8943 int args_depth = TMPL_ARGS_DEPTH (args);
8944 tree coerced_args;
8945
8946 if (parms_depth > 1)
8947 {
8948 coerced_args = make_tree_vec (parms_depth);
8949 tree level;
8950 int cur_depth;
8951
8952 for (level = parms, cur_depth = parms_depth;
8953 parms_depth > 0 && level != NULL_TREE;
8954 level = TREE_CHAIN (level), --cur_depth)
8955 {
8956 tree l;
8957 if (cur_depth == args_depth)
8958 l = coerce_template_parms (TREE_VALUE (level),
8959 args, in_decl, complain,
8960 require_all_args,
8961 use_default_args);
8962 else
8963 l = TMPL_ARGS_LEVEL (args, cur_depth);
8964
8965 if (l == error_mark_node)
8966 return error_mark_node;
8967
8968 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8969 }
8970 }
8971 else
8972 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8973 args, in_decl, complain,
8974 require_all_args,
8975 use_default_args);
8976 return coerced_args;
8977 }
8978
8979 /* Returns 1 if template args OT and NT are equivalent. */
8980
8981 int
8982 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8983 {
8984 if (nt == ot)
8985 return 1;
8986 if (nt == NULL_TREE || ot == NULL_TREE)
8987 return false;
8988 if (nt == any_targ_node || ot == any_targ_node)
8989 return true;
8990
8991 if (TREE_CODE (nt) == TREE_VEC)
8992 /* For member templates */
8993 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8994 else if (PACK_EXPANSION_P (ot))
8995 return (PACK_EXPANSION_P (nt)
8996 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8997 PACK_EXPANSION_PATTERN (nt))
8998 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8999 PACK_EXPANSION_EXTRA_ARGS (nt)));
9000 else if (ARGUMENT_PACK_P (ot))
9001 {
9002 int i, len;
9003 tree opack, npack;
9004
9005 if (!ARGUMENT_PACK_P (nt))
9006 return 0;
9007
9008 opack = ARGUMENT_PACK_ARGS (ot);
9009 npack = ARGUMENT_PACK_ARGS (nt);
9010 len = TREE_VEC_LENGTH (opack);
9011 if (TREE_VEC_LENGTH (npack) != len)
9012 return 0;
9013 for (i = 0; i < len; ++i)
9014 if (!template_args_equal (TREE_VEC_ELT (opack, i),
9015 TREE_VEC_ELT (npack, i)))
9016 return 0;
9017 return 1;
9018 }
9019 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9020 gcc_unreachable ();
9021 else if (TYPE_P (nt))
9022 {
9023 if (!TYPE_P (ot))
9024 return false;
9025 /* Don't treat an alias template specialization with dependent
9026 arguments as equivalent to its underlying type when used as a
9027 template argument; we need them to be distinct so that we
9028 substitute into the specialization arguments at instantiation
9029 time. And aliases can't be equivalent without being ==, so
9030 we don't need to look any deeper.
9031
9032 During partial ordering, however, we need to treat them normally so
9033 that we can order uses of the same alias with different
9034 cv-qualification (79960). */
9035 if (!partial_order
9036 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9037 return false;
9038 else
9039 return same_type_p (ot, nt);
9040 }
9041 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
9042 return 0;
9043 else
9044 {
9045 /* Try to treat a template non-type argument that has been converted
9046 to the parameter type as equivalent to one that hasn't yet. */
9047 for (enum tree_code code1 = TREE_CODE (ot);
9048 CONVERT_EXPR_CODE_P (code1)
9049 || code1 == NON_LVALUE_EXPR;
9050 code1 = TREE_CODE (ot))
9051 ot = TREE_OPERAND (ot, 0);
9052 for (enum tree_code code2 = TREE_CODE (nt);
9053 CONVERT_EXPR_CODE_P (code2)
9054 || code2 == NON_LVALUE_EXPR;
9055 code2 = TREE_CODE (nt))
9056 nt = TREE_OPERAND (nt, 0);
9057
9058 return cp_tree_equal (ot, nt);
9059 }
9060 }
9061
9062 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9063 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9064 NEWARG_PTR with the offending arguments if they are non-NULL. */
9065
9066 int
9067 comp_template_args (tree oldargs, tree newargs,
9068 tree *oldarg_ptr, tree *newarg_ptr,
9069 bool partial_order)
9070 {
9071 int i;
9072
9073 if (oldargs == newargs)
9074 return 1;
9075
9076 if (!oldargs || !newargs)
9077 return 0;
9078
9079 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9080 return 0;
9081
9082 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9083 {
9084 tree nt = TREE_VEC_ELT (newargs, i);
9085 tree ot = TREE_VEC_ELT (oldargs, i);
9086
9087 if (! template_args_equal (ot, nt, partial_order))
9088 {
9089 if (oldarg_ptr != NULL)
9090 *oldarg_ptr = ot;
9091 if (newarg_ptr != NULL)
9092 *newarg_ptr = nt;
9093 return 0;
9094 }
9095 }
9096 return 1;
9097 }
9098
9099 inline bool
9100 comp_template_args_porder (tree oargs, tree nargs)
9101 {
9102 return comp_template_args (oargs, nargs, NULL, NULL, true);
9103 }
9104
9105 /* Implement a freelist interface for objects of type T.
9106
9107 Head is a separate object, rather than a regular member, so that we
9108 can define it as a GTY deletable pointer, which is highly
9109 desirable. A data member could be declared that way, but then the
9110 containing object would implicitly get GTY((user)), which would
9111 prevent us from instantiating freelists as global objects.
9112 Although this way we can create freelist global objects, they're
9113 such thin wrappers that instantiating temporaries at every use
9114 loses nothing and saves permanent storage for the freelist object.
9115
9116 Member functions next, anew, poison and reinit have default
9117 implementations that work for most of the types we're interested
9118 in, but if they don't work for some type, they should be explicitly
9119 specialized. See the comments before them for requirements, and
9120 the example specializations for the tree_list_freelist. */
9121 template <typename T>
9122 class freelist
9123 {
9124 /* Return the next object in a chain. We could just do type
9125 punning, but if we access the object with its underlying type, we
9126 avoid strict-aliasing trouble. This needs only work between
9127 poison and reinit. */
9128 static T *&next (T *obj) { return obj->next; }
9129
9130 /* Return a newly allocated, uninitialized or minimally-initialized
9131 object of type T. Any initialization performed by anew should
9132 either remain across the life of the object and the execution of
9133 poison, or be redone by reinit. */
9134 static T *anew () { return ggc_alloc<T> (); }
9135
9136 /* Optionally scribble all over the bits holding the object, so that
9137 they become (mostly?) uninitialized memory. This is called while
9138 preparing to make the object part of the free list. */
9139 static void poison (T *obj) {
9140 T *p ATTRIBUTE_UNUSED = obj;
9141 T **q ATTRIBUTE_UNUSED = &next (obj);
9142
9143 #ifdef ENABLE_GC_CHECKING
9144 /* Poison the data, to indicate the data is garbage. */
9145 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9146 memset (p, 0xa5, sizeof (*p));
9147 #endif
9148 /* Let valgrind know the object is free. */
9149 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9150
9151 /* Let valgrind know the next portion of the object is available,
9152 but uninitialized. */
9153 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9154 }
9155
9156 /* Bring an object that underwent at least one lifecycle after anew
9157 and before the most recent free and poison, back to a usable
9158 state, reinitializing whatever is needed for it to be
9159 functionally equivalent to an object just allocated and returned
9160 by anew. This may poison or clear the next field, used by
9161 freelist housekeeping after poison was called. */
9162 static void reinit (T *obj) {
9163 T **q ATTRIBUTE_UNUSED = &next (obj);
9164
9165 #ifdef ENABLE_GC_CHECKING
9166 memset (q, 0xa5, sizeof (*q));
9167 #endif
9168 /* Let valgrind know the entire object is available, but
9169 uninitialized. */
9170 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9171 }
9172
9173 /* Reference a GTY-deletable pointer that points to the first object
9174 in the free list proper. */
9175 T *&head;
9176 public:
9177 /* Construct a freelist object chaining objects off of HEAD. */
9178 freelist (T *&head) : head(head) {}
9179
9180 /* Add OBJ to the free object list. The former head becomes OBJ's
9181 successor. */
9182 void free (T *obj)
9183 {
9184 poison (obj);
9185 next (obj) = head;
9186 head = obj;
9187 }
9188
9189 /* Take an object from the free list, if one is available, or
9190 allocate a new one. Objects taken from the free list should be
9191 regarded as filled with garbage, except for bits that are
9192 configured to be preserved across free and alloc. */
9193 T *alloc ()
9194 {
9195 if (head)
9196 {
9197 T *obj = head;
9198 head = next (head);
9199 reinit (obj);
9200 return obj;
9201 }
9202 else
9203 return anew ();
9204 }
9205 };
9206
9207 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9208 want to allocate a TREE_LIST using the usual interface, and ensure
9209 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9210 build_tree_list logic in reinit, so this could go out of sync. */
9211 template <>
9212 inline tree &
9213 freelist<tree_node>::next (tree obj)
9214 {
9215 return TREE_CHAIN (obj);
9216 }
9217 template <>
9218 inline tree
9219 freelist<tree_node>::anew ()
9220 {
9221 return build_tree_list (NULL, NULL);
9222 }
9223 template <>
9224 inline void
9225 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9226 {
9227 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9228 tree p ATTRIBUTE_UNUSED = obj;
9229 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9230 tree *q ATTRIBUTE_UNUSED = &next (obj);
9231
9232 #ifdef ENABLE_GC_CHECKING
9233 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9234
9235 /* Poison the data, to indicate the data is garbage. */
9236 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9237 memset (p, 0xa5, size);
9238 #endif
9239 /* Let valgrind know the object is free. */
9240 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9241 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9242 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9243 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9244
9245 #ifdef ENABLE_GC_CHECKING
9246 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9247 /* Keep TREE_CHAIN functional. */
9248 TREE_SET_CODE (obj, TREE_LIST);
9249 #else
9250 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9251 #endif
9252 }
9253 template <>
9254 inline void
9255 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9256 {
9257 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9258
9259 #ifdef ENABLE_GC_CHECKING
9260 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9261 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9262 memset (obj, 0, sizeof (tree_list));
9263 #endif
9264
9265 /* Let valgrind know the entire object is available, but
9266 uninitialized. */
9267 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9268
9269 #ifdef ENABLE_GC_CHECKING
9270 TREE_SET_CODE (obj, TREE_LIST);
9271 #else
9272 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9273 #endif
9274 }
9275
9276 /* Point to the first object in the TREE_LIST freelist. */
9277 static GTY((deletable)) tree tree_list_freelist_head;
9278 /* Return the/an actual TREE_LIST freelist. */
9279 static inline freelist<tree_node>
9280 tree_list_freelist ()
9281 {
9282 return tree_list_freelist_head;
9283 }
9284
9285 /* Point to the first object in the tinst_level freelist. */
9286 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9287 /* Return the/an actual tinst_level freelist. */
9288 static inline freelist<tinst_level>
9289 tinst_level_freelist ()
9290 {
9291 return tinst_level_freelist_head;
9292 }
9293
9294 /* Point to the first object in the pending_template freelist. */
9295 static GTY((deletable)) pending_template *pending_template_freelist_head;
9296 /* Return the/an actual pending_template freelist. */
9297 static inline freelist<pending_template>
9298 pending_template_freelist ()
9299 {
9300 return pending_template_freelist_head;
9301 }
9302
9303 /* Build the TREE_LIST object out of a split list, store it
9304 permanently, and return it. */
9305 tree
9306 tinst_level::to_list ()
9307 {
9308 gcc_assert (split_list_p ());
9309 tree ret = tree_list_freelist ().alloc ();
9310 TREE_PURPOSE (ret) = tldcl;
9311 TREE_VALUE (ret) = targs;
9312 tldcl = ret;
9313 targs = NULL;
9314 gcc_assert (tree_list_p ());
9315 return ret;
9316 }
9317
9318 const unsigned short tinst_level::refcount_infinity;
9319
9320 /* Increment OBJ's refcount unless it is already infinite. */
9321 static tinst_level *
9322 inc_refcount_use (tinst_level *obj)
9323 {
9324 if (obj && obj->refcount != tinst_level::refcount_infinity)
9325 ++obj->refcount;
9326 return obj;
9327 }
9328
9329 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9330 void
9331 tinst_level::free (tinst_level *obj)
9332 {
9333 if (obj->tree_list_p ())
9334 tree_list_freelist ().free (obj->get_node ());
9335 tinst_level_freelist ().free (obj);
9336 }
9337
9338 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9339 OBJ's DECL and OBJ, and start over with the tinst_level object that
9340 used to be referenced by OBJ's NEXT. */
9341 static void
9342 dec_refcount_use (tinst_level *obj)
9343 {
9344 while (obj
9345 && obj->refcount != tinst_level::refcount_infinity
9346 && !--obj->refcount)
9347 {
9348 tinst_level *next = obj->next;
9349 tinst_level::free (obj);
9350 obj = next;
9351 }
9352 }
9353
9354 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9355 and of the former PTR. Omitting the second argument is equivalent
9356 to passing (T*)NULL; this is allowed because passing the
9357 zero-valued integral constant NULL confuses type deduction and/or
9358 overload resolution. */
9359 template <typename T>
9360 static void
9361 set_refcount_ptr (T *& ptr, T *obj = NULL)
9362 {
9363 T *save = ptr;
9364 ptr = inc_refcount_use (obj);
9365 dec_refcount_use (save);
9366 }
9367
9368 static void
9369 add_pending_template (tree d)
9370 {
9371 tree ti = (TYPE_P (d)
9372 ? CLASSTYPE_TEMPLATE_INFO (d)
9373 : DECL_TEMPLATE_INFO (d));
9374 struct pending_template *pt;
9375 int level;
9376
9377 if (TI_PENDING_TEMPLATE_FLAG (ti))
9378 return;
9379
9380 /* We are called both from instantiate_decl, where we've already had a
9381 tinst_level pushed, and instantiate_template, where we haven't.
9382 Compensate. */
9383 gcc_assert (TREE_CODE (d) != TREE_LIST);
9384 level = !current_tinst_level
9385 || current_tinst_level->maybe_get_node () != d;
9386
9387 if (level)
9388 push_tinst_level (d);
9389
9390 pt = pending_template_freelist ().alloc ();
9391 pt->next = NULL;
9392 pt->tinst = NULL;
9393 set_refcount_ptr (pt->tinst, current_tinst_level);
9394 if (last_pending_template)
9395 last_pending_template->next = pt;
9396 else
9397 pending_templates = pt;
9398
9399 last_pending_template = pt;
9400
9401 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9402
9403 if (level)
9404 pop_tinst_level ();
9405 }
9406
9407
9408 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9409 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9410 documentation for TEMPLATE_ID_EXPR. */
9411
9412 tree
9413 lookup_template_function (tree fns, tree arglist)
9414 {
9415 if (fns == error_mark_node || arglist == error_mark_node)
9416 return error_mark_node;
9417
9418 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9419
9420 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9421 {
9422 error ("%q#D is not a function template", fns);
9423 return error_mark_node;
9424 }
9425
9426 if (BASELINK_P (fns))
9427 {
9428 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9429 unknown_type_node,
9430 BASELINK_FUNCTIONS (fns),
9431 arglist);
9432 return fns;
9433 }
9434
9435 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9436 }
9437
9438 /* Within the scope of a template class S<T>, the name S gets bound
9439 (in build_self_reference) to a TYPE_DECL for the class, not a
9440 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9441 or one of its enclosing classes, and that type is a template,
9442 return the associated TEMPLATE_DECL. Otherwise, the original
9443 DECL is returned.
9444
9445 Also handle the case when DECL is a TREE_LIST of ambiguous
9446 injected-class-names from different bases. */
9447
9448 tree
9449 maybe_get_template_decl_from_type_decl (tree decl)
9450 {
9451 if (decl == NULL_TREE)
9452 return decl;
9453
9454 /* DR 176: A lookup that finds an injected-class-name (10.2
9455 [class.member.lookup]) can result in an ambiguity in certain cases
9456 (for example, if it is found in more than one base class). If all of
9457 the injected-class-names that are found refer to specializations of
9458 the same class template, and if the name is followed by a
9459 template-argument-list, the reference refers to the class template
9460 itself and not a specialization thereof, and is not ambiguous. */
9461 if (TREE_CODE (decl) == TREE_LIST)
9462 {
9463 tree t, tmpl = NULL_TREE;
9464 for (t = decl; t; t = TREE_CHAIN (t))
9465 {
9466 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9467 if (!tmpl)
9468 tmpl = elt;
9469 else if (tmpl != elt)
9470 break;
9471 }
9472 if (tmpl && t == NULL_TREE)
9473 return tmpl;
9474 else
9475 return decl;
9476 }
9477
9478 return (decl != NULL_TREE
9479 && DECL_SELF_REFERENCE_P (decl)
9480 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9481 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9482 }
9483
9484 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9485 parameters, find the desired type.
9486
9487 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9488
9489 IN_DECL, if non-NULL, is the template declaration we are trying to
9490 instantiate.
9491
9492 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9493 the class we are looking up.
9494
9495 Issue error and warning messages under control of COMPLAIN.
9496
9497 If the template class is really a local class in a template
9498 function, then the FUNCTION_CONTEXT is the function in which it is
9499 being instantiated.
9500
9501 ??? Note that this function is currently called *twice* for each
9502 template-id: the first time from the parser, while creating the
9503 incomplete type (finish_template_type), and the second type during the
9504 real instantiation (instantiate_template_class). This is surely something
9505 that we want to avoid. It also causes some problems with argument
9506 coercion (see convert_nontype_argument for more information on this). */
9507
9508 static tree
9509 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9510 int entering_scope, tsubst_flags_t complain)
9511 {
9512 tree templ = NULL_TREE, parmlist;
9513 tree t;
9514 spec_entry **slot;
9515 spec_entry *entry;
9516 spec_entry elt;
9517 hashval_t hash;
9518
9519 if (identifier_p (d1))
9520 {
9521 tree value = innermost_non_namespace_value (d1);
9522 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9523 templ = value;
9524 else
9525 {
9526 if (context)
9527 push_decl_namespace (context);
9528 templ = lookup_name (d1);
9529 templ = maybe_get_template_decl_from_type_decl (templ);
9530 if (context)
9531 pop_decl_namespace ();
9532 }
9533 if (templ)
9534 context = DECL_CONTEXT (templ);
9535 }
9536 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9537 {
9538 tree type = TREE_TYPE (d1);
9539
9540 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9541 an implicit typename for the second A. Deal with it. */
9542 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9543 type = TREE_TYPE (type);
9544
9545 if (CLASSTYPE_TEMPLATE_INFO (type))
9546 {
9547 templ = CLASSTYPE_TI_TEMPLATE (type);
9548 d1 = DECL_NAME (templ);
9549 }
9550 }
9551 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9552 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9553 {
9554 templ = TYPE_TI_TEMPLATE (d1);
9555 d1 = DECL_NAME (templ);
9556 }
9557 else if (DECL_TYPE_TEMPLATE_P (d1))
9558 {
9559 templ = d1;
9560 d1 = DECL_NAME (templ);
9561 context = DECL_CONTEXT (templ);
9562 }
9563 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9564 {
9565 templ = d1;
9566 d1 = DECL_NAME (templ);
9567 }
9568
9569 /* Issue an error message if we didn't find a template. */
9570 if (! templ)
9571 {
9572 if (complain & tf_error)
9573 error ("%qT is not a template", d1);
9574 return error_mark_node;
9575 }
9576
9577 if (TREE_CODE (templ) != TEMPLATE_DECL
9578 /* Make sure it's a user visible template, if it was named by
9579 the user. */
9580 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9581 && !PRIMARY_TEMPLATE_P (templ)))
9582 {
9583 if (complain & tf_error)
9584 {
9585 error ("non-template type %qT used as a template", d1);
9586 if (in_decl)
9587 error ("for template declaration %q+D", in_decl);
9588 }
9589 return error_mark_node;
9590 }
9591
9592 complain &= ~tf_user;
9593
9594 /* An alias that just changes the name of a template is equivalent to the
9595 other template, so if any of the arguments are pack expansions, strip
9596 the alias to avoid problems with a pack expansion passed to a non-pack
9597 alias template parameter (DR 1430). */
9598 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9599 templ = get_underlying_template (templ);
9600
9601 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9602 {
9603 tree parm;
9604 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9605 if (arglist2 == error_mark_node
9606 || (!uses_template_parms (arglist2)
9607 && check_instantiated_args (templ, arglist2, complain)))
9608 return error_mark_node;
9609
9610 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9611 return parm;
9612 }
9613 else
9614 {
9615 tree template_type = TREE_TYPE (templ);
9616 tree gen_tmpl;
9617 tree type_decl;
9618 tree found = NULL_TREE;
9619 int arg_depth;
9620 int parm_depth;
9621 int is_dependent_type;
9622 int use_partial_inst_tmpl = false;
9623
9624 if (template_type == error_mark_node)
9625 /* An error occurred while building the template TEMPL, and a
9626 diagnostic has most certainly been emitted for that
9627 already. Let's propagate that error. */
9628 return error_mark_node;
9629
9630 gen_tmpl = most_general_template (templ);
9631 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9632 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9633 arg_depth = TMPL_ARGS_DEPTH (arglist);
9634
9635 if (arg_depth == 1 && parm_depth > 1)
9636 {
9637 /* We've been given an incomplete set of template arguments.
9638 For example, given:
9639
9640 template <class T> struct S1 {
9641 template <class U> struct S2 {};
9642 template <class U> struct S2<U*> {};
9643 };
9644
9645 we will be called with an ARGLIST of `U*', but the
9646 TEMPLATE will be `template <class T> template
9647 <class U> struct S1<T>::S2'. We must fill in the missing
9648 arguments. */
9649 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9650 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9651 arg_depth = TMPL_ARGS_DEPTH (arglist);
9652 }
9653
9654 /* Now we should have enough arguments. */
9655 gcc_assert (parm_depth == arg_depth);
9656
9657 /* From here on, we're only interested in the most general
9658 template. */
9659
9660 /* Calculate the BOUND_ARGS. These will be the args that are
9661 actually tsubst'd into the definition to create the
9662 instantiation. */
9663 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9664 complain,
9665 /*require_all_args=*/true,
9666 /*use_default_args=*/true);
9667
9668 if (arglist == error_mark_node)
9669 /* We were unable to bind the arguments. */
9670 return error_mark_node;
9671
9672 /* In the scope of a template class, explicit references to the
9673 template class refer to the type of the template, not any
9674 instantiation of it. For example, in:
9675
9676 template <class T> class C { void f(C<T>); }
9677
9678 the `C<T>' is just the same as `C'. Outside of the
9679 class, however, such a reference is an instantiation. */
9680 if (entering_scope
9681 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9682 || currently_open_class (template_type))
9683 {
9684 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9685
9686 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9687 return template_type;
9688 }
9689
9690 /* If we already have this specialization, return it. */
9691 elt.tmpl = gen_tmpl;
9692 elt.args = arglist;
9693 elt.spec = NULL_TREE;
9694 hash = spec_hasher::hash (&elt);
9695 entry = type_specializations->find_with_hash (&elt, hash);
9696
9697 if (entry)
9698 return entry->spec;
9699
9700 /* If the the template's constraints are not satisfied,
9701 then we cannot form a valid type.
9702
9703 Note that the check is deferred until after the hash
9704 lookup. This prevents redundant checks on previously
9705 instantiated specializations. */
9706 if (flag_concepts
9707 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9708 && !constraints_satisfied_p (gen_tmpl, arglist))
9709 {
9710 if (complain & tf_error)
9711 {
9712 auto_diagnostic_group d;
9713 error ("template constraint failure for %qD", gen_tmpl);
9714 diagnose_constraints (input_location, gen_tmpl, arglist);
9715 }
9716 return error_mark_node;
9717 }
9718
9719 is_dependent_type = uses_template_parms (arglist);
9720
9721 /* If the deduced arguments are invalid, then the binding
9722 failed. */
9723 if (!is_dependent_type
9724 && check_instantiated_args (gen_tmpl,
9725 INNERMOST_TEMPLATE_ARGS (arglist),
9726 complain))
9727 return error_mark_node;
9728
9729 if (!is_dependent_type
9730 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9731 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9732 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9733 {
9734 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9735 DECL_NAME (gen_tmpl),
9736 /*tag_scope=*/ts_global);
9737 return found;
9738 }
9739
9740 context = DECL_CONTEXT (gen_tmpl);
9741 if (context && TYPE_P (context))
9742 {
9743 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9744 context = complete_type (context);
9745 }
9746 else
9747 context = tsubst (context, arglist, complain, in_decl);
9748
9749 if (context == error_mark_node)
9750 return error_mark_node;
9751
9752 if (!context)
9753 context = global_namespace;
9754
9755 /* Create the type. */
9756 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9757 {
9758 /* The user referred to a specialization of an alias
9759 template represented by GEN_TMPL.
9760
9761 [temp.alias]/2 says:
9762
9763 When a template-id refers to the specialization of an
9764 alias template, it is equivalent to the associated
9765 type obtained by substitution of its
9766 template-arguments for the template-parameters in the
9767 type-id of the alias template. */
9768
9769 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9770 /* Note that the call above (by indirectly calling
9771 register_specialization in tsubst_decl) registers the
9772 TYPE_DECL representing the specialization of the alias
9773 template. So next time someone substitutes ARGLIST for
9774 the template parms into the alias template (GEN_TMPL),
9775 she'll get that TYPE_DECL back. */
9776
9777 if (t == error_mark_node)
9778 return t;
9779 }
9780 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9781 {
9782 if (!is_dependent_type)
9783 {
9784 set_current_access_from_decl (TYPE_NAME (template_type));
9785 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9786 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9787 arglist, complain, in_decl),
9788 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9789 arglist, complain, in_decl),
9790 SCOPED_ENUM_P (template_type), NULL);
9791
9792 if (t == error_mark_node)
9793 return t;
9794 }
9795 else
9796 {
9797 /* We don't want to call start_enum for this type, since
9798 the values for the enumeration constants may involve
9799 template parameters. And, no one should be interested
9800 in the enumeration constants for such a type. */
9801 t = cxx_make_type (ENUMERAL_TYPE);
9802 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9803 }
9804 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9805 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9806 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9807 }
9808 else if (CLASS_TYPE_P (template_type))
9809 {
9810 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9811 instantiated here. */
9812 gcc_assert (!LAMBDA_TYPE_P (template_type));
9813
9814 t = make_class_type (TREE_CODE (template_type));
9815 CLASSTYPE_DECLARED_CLASS (t)
9816 = CLASSTYPE_DECLARED_CLASS (template_type);
9817 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9818
9819 /* A local class. Make sure the decl gets registered properly. */
9820 if (context == current_function_decl)
9821 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9822 == error_mark_node)
9823 return error_mark_node;
9824
9825 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9826 /* This instantiation is another name for the primary
9827 template type. Set the TYPE_CANONICAL field
9828 appropriately. */
9829 TYPE_CANONICAL (t) = template_type;
9830 else if (any_template_arguments_need_structural_equality_p (arglist))
9831 /* Some of the template arguments require structural
9832 equality testing, so this template class requires
9833 structural equality testing. */
9834 SET_TYPE_STRUCTURAL_EQUALITY (t);
9835 }
9836 else
9837 gcc_unreachable ();
9838
9839 /* If we called start_enum or pushtag above, this information
9840 will already be set up. */
9841 if (!TYPE_NAME (t))
9842 {
9843 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9844
9845 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9846 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9847 DECL_SOURCE_LOCATION (type_decl)
9848 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9849 }
9850 else
9851 type_decl = TYPE_NAME (t);
9852
9853 if (CLASS_TYPE_P (template_type))
9854 {
9855 TREE_PRIVATE (type_decl)
9856 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9857 TREE_PROTECTED (type_decl)
9858 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9859 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9860 {
9861 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9862 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9863 }
9864 }
9865
9866 if (OVERLOAD_TYPE_P (t)
9867 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9868 {
9869 static const char *tags[] = {"abi_tag", "may_alias"};
9870
9871 for (unsigned ix = 0; ix != 2; ix++)
9872 {
9873 tree attributes
9874 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9875
9876 if (attributes)
9877 TYPE_ATTRIBUTES (t)
9878 = tree_cons (TREE_PURPOSE (attributes),
9879 TREE_VALUE (attributes),
9880 TYPE_ATTRIBUTES (t));
9881 }
9882 }
9883
9884 /* Let's consider the explicit specialization of a member
9885 of a class template specialization that is implicitly instantiated,
9886 e.g.:
9887 template<class T>
9888 struct S
9889 {
9890 template<class U> struct M {}; //#0
9891 };
9892
9893 template<>
9894 template<>
9895 struct S<int>::M<char> //#1
9896 {
9897 int i;
9898 };
9899 [temp.expl.spec]/4 says this is valid.
9900
9901 In this case, when we write:
9902 S<int>::M<char> m;
9903
9904 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9905 the one of #0.
9906
9907 When we encounter #1, we want to store the partial instantiation
9908 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9909
9910 For all cases other than this "explicit specialization of member of a
9911 class template", we just want to store the most general template into
9912 the CLASSTYPE_TI_TEMPLATE of M.
9913
9914 This case of "explicit specialization of member of a class template"
9915 only happens when:
9916 1/ the enclosing class is an instantiation of, and therefore not
9917 the same as, the context of the most general template, and
9918 2/ we aren't looking at the partial instantiation itself, i.e.
9919 the innermost arguments are not the same as the innermost parms of
9920 the most general template.
9921
9922 So it's only when 1/ and 2/ happens that we want to use the partial
9923 instantiation of the member template in lieu of its most general
9924 template. */
9925
9926 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9927 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9928 /* the enclosing class must be an instantiation... */
9929 && CLASS_TYPE_P (context)
9930 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9931 {
9932 TREE_VEC_LENGTH (arglist)--;
9933 ++processing_template_decl;
9934 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9935 tree partial_inst_args =
9936 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9937 arglist, complain, NULL_TREE);
9938 --processing_template_decl;
9939 TREE_VEC_LENGTH (arglist)++;
9940 if (partial_inst_args == error_mark_node)
9941 return error_mark_node;
9942 use_partial_inst_tmpl =
9943 /*...and we must not be looking at the partial instantiation
9944 itself. */
9945 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9946 partial_inst_args);
9947 }
9948
9949 if (!use_partial_inst_tmpl)
9950 /* This case is easy; there are no member templates involved. */
9951 found = gen_tmpl;
9952 else
9953 {
9954 /* This is a full instantiation of a member template. Find
9955 the partial instantiation of which this is an instance. */
9956
9957 /* Temporarily reduce by one the number of levels in the ARGLIST
9958 so as to avoid comparing the last set of arguments. */
9959 TREE_VEC_LENGTH (arglist)--;
9960 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9961 TREE_VEC_LENGTH (arglist)++;
9962 /* FOUND is either a proper class type, or an alias
9963 template specialization. In the later case, it's a
9964 TYPE_DECL, resulting from the substituting of arguments
9965 for parameters in the TYPE_DECL of the alias template
9966 done earlier. So be careful while getting the template
9967 of FOUND. */
9968 found = (TREE_CODE (found) == TEMPLATE_DECL
9969 ? found
9970 : (TREE_CODE (found) == TYPE_DECL
9971 ? DECL_TI_TEMPLATE (found)
9972 : CLASSTYPE_TI_TEMPLATE (found)));
9973
9974 if (DECL_CLASS_TEMPLATE_P (found)
9975 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9976 {
9977 /* If this partial instantiation is specialized, we want to
9978 use it for hash table lookup. */
9979 elt.tmpl = found;
9980 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9981 hash = spec_hasher::hash (&elt);
9982 }
9983 }
9984
9985 // Build template info for the new specialization.
9986 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9987
9988 elt.spec = t;
9989 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9990 gcc_checking_assert (*slot == NULL);
9991 entry = ggc_alloc<spec_entry> ();
9992 *entry = elt;
9993 *slot = entry;
9994
9995 /* Note this use of the partial instantiation so we can check it
9996 later in maybe_process_partial_specialization. */
9997 DECL_TEMPLATE_INSTANTIATIONS (found)
9998 = tree_cons (arglist, t,
9999 DECL_TEMPLATE_INSTANTIATIONS (found));
10000
10001 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10002 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10003 /* Now that the type has been registered on the instantiations
10004 list, we set up the enumerators. Because the enumeration
10005 constants may involve the enumeration type itself, we make
10006 sure to register the type first, and then create the
10007 constants. That way, doing tsubst_expr for the enumeration
10008 constants won't result in recursive calls here; we'll find
10009 the instantiation and exit above. */
10010 tsubst_enum (template_type, t, arglist);
10011
10012 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10013 /* If the type makes use of template parameters, the
10014 code that generates debugging information will crash. */
10015 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10016
10017 /* Possibly limit visibility based on template args. */
10018 TREE_PUBLIC (type_decl) = 1;
10019 determine_visibility (type_decl);
10020
10021 inherit_targ_abi_tags (t);
10022
10023 return t;
10024 }
10025 }
10026
10027 /* Wrapper for lookup_template_class_1. */
10028
10029 tree
10030 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10031 int entering_scope, tsubst_flags_t complain)
10032 {
10033 tree ret;
10034 timevar_push (TV_TEMPLATE_INST);
10035 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10036 entering_scope, complain);
10037 timevar_pop (TV_TEMPLATE_INST);
10038 return ret;
10039 }
10040
10041 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10042
10043 tree
10044 lookup_template_variable (tree templ, tree arglist)
10045 {
10046 if (flag_concepts && variable_concept_p (templ))
10047 return build_concept_check (templ, arglist, tf_none);
10048
10049 /* The type of the expression is NULL_TREE since the template-id could refer
10050 to an explicit or partial specialization. */
10051 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10052 }
10053
10054 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10055
10056 tree
10057 finish_template_variable (tree var, tsubst_flags_t complain)
10058 {
10059 tree templ = TREE_OPERAND (var, 0);
10060 tree arglist = TREE_OPERAND (var, 1);
10061
10062 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10063 arglist = add_outermost_template_args (tmpl_args, arglist);
10064
10065 templ = most_general_template (templ);
10066 tree parms = DECL_TEMPLATE_PARMS (templ);
10067 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10068 /*req_all*/true,
10069 /*use_default*/true);
10070
10071 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10072 {
10073 if (complain & tf_error)
10074 {
10075 auto_diagnostic_group d;
10076 error ("use of invalid variable template %qE", var);
10077 diagnose_constraints (location_of (var), templ, arglist);
10078 }
10079 return error_mark_node;
10080 }
10081
10082 return instantiate_template (templ, arglist, complain);
10083 }
10084
10085 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10086 TARGS template args, and instantiate it if it's not dependent. */
10087
10088 tree
10089 lookup_and_finish_template_variable (tree templ, tree targs,
10090 tsubst_flags_t complain)
10091 {
10092 templ = lookup_template_variable (templ, targs);
10093 if (!any_dependent_template_arguments_p (targs))
10094 {
10095 templ = finish_template_variable (templ, complain);
10096 mark_used (templ);
10097 }
10098
10099 return convert_from_reference (templ);
10100 }
10101
10102 \f
10103 struct pair_fn_data
10104 {
10105 tree_fn_t fn;
10106 tree_fn_t any_fn;
10107 void *data;
10108 /* True when we should also visit template parameters that occur in
10109 non-deduced contexts. */
10110 bool include_nondeduced_p;
10111 hash_set<tree> *visited;
10112 };
10113
10114 /* Called from for_each_template_parm via walk_tree. */
10115
10116 static tree
10117 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10118 {
10119 tree t = *tp;
10120 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10121 tree_fn_t fn = pfd->fn;
10122 void *data = pfd->data;
10123 tree result = NULL_TREE;
10124
10125 #define WALK_SUBTREE(NODE) \
10126 do \
10127 { \
10128 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10129 pfd->include_nondeduced_p, \
10130 pfd->any_fn); \
10131 if (result) goto out; \
10132 } \
10133 while (0)
10134
10135 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10136 return t;
10137
10138 if (TYPE_P (t)
10139 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10140 WALK_SUBTREE (TYPE_CONTEXT (t));
10141
10142 switch (TREE_CODE (t))
10143 {
10144 case RECORD_TYPE:
10145 if (TYPE_PTRMEMFUNC_P (t))
10146 break;
10147 /* Fall through. */
10148
10149 case UNION_TYPE:
10150 case ENUMERAL_TYPE:
10151 if (!TYPE_TEMPLATE_INFO (t))
10152 *walk_subtrees = 0;
10153 else
10154 WALK_SUBTREE (TYPE_TI_ARGS (t));
10155 break;
10156
10157 case INTEGER_TYPE:
10158 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10159 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10160 break;
10161
10162 case METHOD_TYPE:
10163 /* Since we're not going to walk subtrees, we have to do this
10164 explicitly here. */
10165 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10166 /* Fall through. */
10167
10168 case FUNCTION_TYPE:
10169 /* Check the return type. */
10170 WALK_SUBTREE (TREE_TYPE (t));
10171
10172 /* Check the parameter types. Since default arguments are not
10173 instantiated until they are needed, the TYPE_ARG_TYPES may
10174 contain expressions that involve template parameters. But,
10175 no-one should be looking at them yet. And, once they're
10176 instantiated, they don't contain template parameters, so
10177 there's no point in looking at them then, either. */
10178 {
10179 tree parm;
10180
10181 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10182 WALK_SUBTREE (TREE_VALUE (parm));
10183
10184 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10185 want walk_tree walking into them itself. */
10186 *walk_subtrees = 0;
10187 }
10188
10189 if (flag_noexcept_type)
10190 {
10191 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10192 if (spec)
10193 WALK_SUBTREE (TREE_PURPOSE (spec));
10194 }
10195 break;
10196
10197 case TYPEOF_TYPE:
10198 case DECLTYPE_TYPE:
10199 case UNDERLYING_TYPE:
10200 if (pfd->include_nondeduced_p
10201 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10202 pfd->visited,
10203 pfd->include_nondeduced_p,
10204 pfd->any_fn))
10205 return error_mark_node;
10206 *walk_subtrees = false;
10207 break;
10208
10209 case FUNCTION_DECL:
10210 case VAR_DECL:
10211 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10212 WALK_SUBTREE (DECL_TI_ARGS (t));
10213 /* Fall through. */
10214
10215 case PARM_DECL:
10216 case CONST_DECL:
10217 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10218 WALK_SUBTREE (DECL_INITIAL (t));
10219 if (DECL_CONTEXT (t)
10220 && pfd->include_nondeduced_p)
10221 WALK_SUBTREE (DECL_CONTEXT (t));
10222 break;
10223
10224 case BOUND_TEMPLATE_TEMPLATE_PARM:
10225 /* Record template parameters such as `T' inside `TT<T>'. */
10226 WALK_SUBTREE (TYPE_TI_ARGS (t));
10227 /* Fall through. */
10228
10229 case TEMPLATE_TEMPLATE_PARM:
10230 case TEMPLATE_TYPE_PARM:
10231 case TEMPLATE_PARM_INDEX:
10232 if (fn && (*fn)(t, data))
10233 return t;
10234 else if (!fn)
10235 return t;
10236 break;
10237
10238 case TEMPLATE_DECL:
10239 /* A template template parameter is encountered. */
10240 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10241 WALK_SUBTREE (TREE_TYPE (t));
10242
10243 /* Already substituted template template parameter */
10244 *walk_subtrees = 0;
10245 break;
10246
10247 case TYPENAME_TYPE:
10248 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10249 partial instantiation. */
10250 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10251 break;
10252
10253 case CONSTRUCTOR:
10254 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10255 && pfd->include_nondeduced_p)
10256 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10257 break;
10258
10259 case INDIRECT_REF:
10260 case COMPONENT_REF:
10261 /* If there's no type, then this thing must be some expression
10262 involving template parameters. */
10263 if (!fn && !TREE_TYPE (t))
10264 return error_mark_node;
10265 break;
10266
10267 case MODOP_EXPR:
10268 case CAST_EXPR:
10269 case IMPLICIT_CONV_EXPR:
10270 case REINTERPRET_CAST_EXPR:
10271 case CONST_CAST_EXPR:
10272 case STATIC_CAST_EXPR:
10273 case DYNAMIC_CAST_EXPR:
10274 case ARROW_EXPR:
10275 case DOTSTAR_EXPR:
10276 case TYPEID_EXPR:
10277 case PSEUDO_DTOR_EXPR:
10278 if (!fn)
10279 return error_mark_node;
10280 break;
10281
10282 case SCOPE_REF:
10283 if (pfd->include_nondeduced_p)
10284 WALK_SUBTREE (TREE_OPERAND (t, 0));
10285 break;
10286
10287 case REQUIRES_EXPR:
10288 {
10289 if (!fn)
10290 return error_mark_node;
10291
10292 /* Recursively walk the type of each constraint variable. */
10293 tree p = TREE_OPERAND (t, 0);
10294 while (p)
10295 {
10296 WALK_SUBTREE (TREE_TYPE (p));
10297 p = TREE_CHAIN (p);
10298 }
10299 }
10300 break;
10301
10302 default:
10303 break;
10304 }
10305
10306 #undef WALK_SUBTREE
10307
10308 /* We didn't find any template parameters we liked. */
10309 out:
10310 return result;
10311 }
10312
10313 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10314 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10315 call FN with the parameter and the DATA.
10316 If FN returns nonzero, the iteration is terminated, and
10317 for_each_template_parm returns 1. Otherwise, the iteration
10318 continues. If FN never returns a nonzero value, the value
10319 returned by for_each_template_parm is 0. If FN is NULL, it is
10320 considered to be the function which always returns 1.
10321
10322 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10323 parameters that occur in non-deduced contexts. When false, only
10324 visits those template parameters that can be deduced. */
10325
10326 static tree
10327 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10328 hash_set<tree> *visited,
10329 bool include_nondeduced_p,
10330 tree_fn_t any_fn)
10331 {
10332 struct pair_fn_data pfd;
10333 tree result;
10334
10335 /* Set up. */
10336 pfd.fn = fn;
10337 pfd.any_fn = any_fn;
10338 pfd.data = data;
10339 pfd.include_nondeduced_p = include_nondeduced_p;
10340
10341 /* Walk the tree. (Conceptually, we would like to walk without
10342 duplicates, but for_each_template_parm_r recursively calls
10343 for_each_template_parm, so we would need to reorganize a fair
10344 bit to use walk_tree_without_duplicates, so we keep our own
10345 visited list.) */
10346 if (visited)
10347 pfd.visited = visited;
10348 else
10349 pfd.visited = new hash_set<tree>;
10350 result = cp_walk_tree (&t,
10351 for_each_template_parm_r,
10352 &pfd,
10353 pfd.visited);
10354
10355 /* Clean up. */
10356 if (!visited)
10357 {
10358 delete pfd.visited;
10359 pfd.visited = 0;
10360 }
10361
10362 return result;
10363 }
10364
10365 struct find_template_parameter_info
10366 {
10367 explicit find_template_parameter_info (int d)
10368 : max_depth (d)
10369 {}
10370
10371 hash_set<tree> visited;
10372 hash_set<tree> parms;
10373 int max_depth;
10374 };
10375
10376 /* Appends the declaration of T to the list in DATA. */
10377
10378 static int
10379 keep_template_parm (tree t, void* data)
10380 {
10381 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10382
10383 /* Template parameters declared within the expression are not part of
10384 the parameter mapping. For example, in this concept:
10385
10386 template<typename T>
10387 concept C = requires { <expr> } -> same_as<int>;
10388
10389 the return specifier same_as<int> declares a new decltype parameter
10390 that must not be part of the parameter mapping. The same is true
10391 for generic lambda parameters, lambda template parameters, etc. */
10392 int level;
10393 int index;
10394 template_parm_level_and_index (t, &level, &index);
10395 if (level > ftpi->max_depth)
10396 return 0;
10397
10398 /* Arguments like const T yield parameters like const T. This means that
10399 a template-id like X<T, const T> would yield two distinct parameters:
10400 T and const T. Adjust types to their unqualified versions. */
10401 if (TYPE_P (t))
10402 t = TYPE_MAIN_VARIANT (t);
10403 ftpi->parms.add (t);
10404
10405 return 0;
10406 }
10407
10408 /* Ensure that we recursively examine certain terms that are not normally
10409 visited in for_each_template_parm_r. */
10410
10411 static int
10412 any_template_parm_r (tree t, void *data)
10413 {
10414 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10415
10416 #define WALK_SUBTREE(NODE) \
10417 do \
10418 { \
10419 for_each_template_parm (NODE, keep_template_parm, data, \
10420 &ftpi->visited, true, \
10421 any_template_parm_r); \
10422 } \
10423 while (0)
10424
10425 switch (TREE_CODE (t))
10426 {
10427 case RECORD_TYPE:
10428 case UNION_TYPE:
10429 case ENUMERAL_TYPE:
10430 /* Search for template parameters in type aliases. */
10431 if (tree ats = alias_template_specialization_p (t, nt_opaque))
10432 {
10433 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (ats);
10434 WALK_SUBTREE (TI_ARGS (tinfo));
10435 }
10436 break;
10437
10438 case TEMPLATE_TYPE_PARM:
10439 /* Type constraints of a placeholder type may contain parameters. */
10440 if (is_auto (t))
10441 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10442 WALK_SUBTREE (constr);
10443 break;
10444
10445 case TEMPLATE_ID_EXPR:
10446 /* Search through references to variable templates. */
10447 WALK_SUBTREE (TREE_OPERAND (t, 0));
10448 WALK_SUBTREE (TREE_OPERAND (t, 1));
10449 break;
10450
10451 case CONSTRUCTOR:
10452 if (TREE_TYPE (t))
10453 WALK_SUBTREE (TREE_TYPE (t));
10454 break;
10455
10456 case PARM_DECL:
10457 /* A parameter or constraint variable may also depend on a template
10458 parameter without explicitly naming it. */
10459 WALK_SUBTREE (TREE_TYPE (t));
10460 break;
10461
10462 default:
10463 break;
10464 }
10465
10466 /* Keep walking. */
10467 return 0;
10468 }
10469
10470 /* Returns a list of unique template parameters found within T. */
10471
10472 tree
10473 find_template_parameters (tree t, int depth)
10474 {
10475 find_template_parameter_info ftpi (depth);
10476 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10477 /*include_nondeduced*/true, any_template_parm_r);
10478 tree list = NULL_TREE;
10479 for (hash_set<tree>::iterator iter = ftpi.parms.begin();
10480 iter != ftpi.parms.end(); ++iter)
10481 list = tree_cons (NULL_TREE, *iter, list);
10482 return list;
10483 }
10484
10485 /* Returns true if T depends on any template parameter. */
10486
10487 int
10488 uses_template_parms (tree t)
10489 {
10490 if (t == NULL_TREE)
10491 return false;
10492
10493 bool dependent_p;
10494 int saved_processing_template_decl;
10495
10496 saved_processing_template_decl = processing_template_decl;
10497 if (!saved_processing_template_decl)
10498 processing_template_decl = 1;
10499 if (TYPE_P (t))
10500 dependent_p = dependent_type_p (t);
10501 else if (TREE_CODE (t) == TREE_VEC)
10502 dependent_p = any_dependent_template_arguments_p (t);
10503 else if (TREE_CODE (t) == TREE_LIST)
10504 dependent_p = (uses_template_parms (TREE_VALUE (t))
10505 || uses_template_parms (TREE_CHAIN (t)));
10506 else if (TREE_CODE (t) == TYPE_DECL)
10507 dependent_p = dependent_type_p (TREE_TYPE (t));
10508 else if (DECL_P (t)
10509 || EXPR_P (t)
10510 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10511 || TREE_CODE (t) == OVERLOAD
10512 || BASELINK_P (t)
10513 || identifier_p (t)
10514 || TREE_CODE (t) == TRAIT_EXPR
10515 || TREE_CODE (t) == CONSTRUCTOR
10516 || CONSTANT_CLASS_P (t))
10517 dependent_p = (type_dependent_expression_p (t)
10518 || value_dependent_expression_p (t));
10519 else
10520 {
10521 gcc_assert (t == error_mark_node);
10522 dependent_p = false;
10523 }
10524
10525 processing_template_decl = saved_processing_template_decl;
10526
10527 return dependent_p;
10528 }
10529
10530 /* Returns true iff current_function_decl is an incompletely instantiated
10531 template. Useful instead of processing_template_decl because the latter
10532 is set to 0 during instantiate_non_dependent_expr. */
10533
10534 bool
10535 in_template_function (void)
10536 {
10537 tree fn = current_function_decl;
10538 bool ret;
10539 ++processing_template_decl;
10540 ret = (fn && DECL_LANG_SPECIFIC (fn)
10541 && DECL_TEMPLATE_INFO (fn)
10542 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10543 --processing_template_decl;
10544 return ret;
10545 }
10546
10547 /* Returns true if T depends on any template parameter with level LEVEL. */
10548
10549 bool
10550 uses_template_parms_level (tree t, int level)
10551 {
10552 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10553 /*include_nondeduced_p=*/true);
10554 }
10555
10556 /* Returns true if the signature of DECL depends on any template parameter from
10557 its enclosing class. */
10558
10559 bool
10560 uses_outer_template_parms (tree decl)
10561 {
10562 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10563 if (depth == 0)
10564 return false;
10565 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10566 &depth, NULL, /*include_nondeduced_p=*/true))
10567 return true;
10568 if (PRIMARY_TEMPLATE_P (decl)
10569 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10570 (DECL_TEMPLATE_PARMS (decl)),
10571 template_parm_outer_level,
10572 &depth, NULL, /*include_nondeduced_p=*/true))
10573 return true;
10574 tree ci = get_constraints (decl);
10575 if (ci)
10576 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10577 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10578 &depth, NULL, /*nondeduced*/true))
10579 return true;
10580 return false;
10581 }
10582
10583 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10584 ill-formed translation unit, i.e. a variable or function that isn't
10585 usable in a constant expression. */
10586
10587 static inline bool
10588 neglectable_inst_p (tree d)
10589 {
10590 return (d && DECL_P (d)
10591 && !undeduced_auto_decl (d)
10592 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10593 : decl_maybe_constant_var_p (d)));
10594 }
10595
10596 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10597 neglectable and instantiated from within an erroneous instantiation. */
10598
10599 static bool
10600 limit_bad_template_recursion (tree decl)
10601 {
10602 struct tinst_level *lev = current_tinst_level;
10603 int errs = errorcount + sorrycount;
10604 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10605 return false;
10606
10607 for (; lev; lev = lev->next)
10608 if (neglectable_inst_p (lev->maybe_get_node ()))
10609 break;
10610
10611 return (lev && errs > lev->errors);
10612 }
10613
10614 static int tinst_depth;
10615 extern int max_tinst_depth;
10616 int depth_reached;
10617
10618 static GTY(()) struct tinst_level *last_error_tinst_level;
10619
10620 /* We're starting to instantiate D; record the template instantiation context
10621 at LOC for diagnostics and to restore it later. */
10622
10623 static bool
10624 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10625 {
10626 struct tinst_level *new_level;
10627
10628 if (tinst_depth >= max_tinst_depth)
10629 {
10630 /* Tell error.c not to try to instantiate any templates. */
10631 at_eof = 2;
10632 fatal_error (input_location,
10633 "template instantiation depth exceeds maximum of %d"
10634 " (use %<-ftemplate-depth=%> to increase the maximum)",
10635 max_tinst_depth);
10636 return false;
10637 }
10638
10639 /* If the current instantiation caused problems, don't let it instantiate
10640 anything else. Do allow deduction substitution and decls usable in
10641 constant expressions. */
10642 if (!targs && limit_bad_template_recursion (tldcl))
10643 {
10644 /* Avoid no_linkage_errors and unused function warnings for this
10645 decl. */
10646 TREE_NO_WARNING (tldcl) = 1;
10647 return false;
10648 }
10649
10650 /* When not -quiet, dump template instantiations other than functions, since
10651 announce_function will take care of those. */
10652 if (!quiet_flag && !targs
10653 && TREE_CODE (tldcl) != TREE_LIST
10654 && TREE_CODE (tldcl) != FUNCTION_DECL)
10655 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10656
10657 new_level = tinst_level_freelist ().alloc ();
10658 new_level->tldcl = tldcl;
10659 new_level->targs = targs;
10660 new_level->locus = loc;
10661 new_level->errors = errorcount + sorrycount;
10662 new_level->next = NULL;
10663 new_level->refcount = 0;
10664 set_refcount_ptr (new_level->next, current_tinst_level);
10665 set_refcount_ptr (current_tinst_level, new_level);
10666
10667 ++tinst_depth;
10668 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10669 depth_reached = tinst_depth;
10670
10671 return true;
10672 }
10673
10674 /* We're starting substitution of TMPL<ARGS>; record the template
10675 substitution context for diagnostics and to restore it later. */
10676
10677 static bool
10678 push_tinst_level (tree tmpl, tree args)
10679 {
10680 return push_tinst_level_loc (tmpl, args, input_location);
10681 }
10682
10683 /* We're starting to instantiate D; record INPUT_LOCATION and the
10684 template instantiation context for diagnostics and to restore it
10685 later. */
10686
10687 bool
10688 push_tinst_level (tree d)
10689 {
10690 return push_tinst_level_loc (d, input_location);
10691 }
10692
10693 /* Likewise, but record LOC as the program location. */
10694
10695 bool
10696 push_tinst_level_loc (tree d, location_t loc)
10697 {
10698 gcc_assert (TREE_CODE (d) != TREE_LIST);
10699 return push_tinst_level_loc (d, NULL, loc);
10700 }
10701
10702 /* We're done instantiating this template; return to the instantiation
10703 context. */
10704
10705 void
10706 pop_tinst_level (void)
10707 {
10708 /* Restore the filename and line number stashed away when we started
10709 this instantiation. */
10710 input_location = current_tinst_level->locus;
10711 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10712 --tinst_depth;
10713 }
10714
10715 /* We're instantiating a deferred template; restore the template
10716 instantiation context in which the instantiation was requested, which
10717 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10718
10719 static tree
10720 reopen_tinst_level (struct tinst_level *level)
10721 {
10722 struct tinst_level *t;
10723
10724 tinst_depth = 0;
10725 for (t = level; t; t = t->next)
10726 ++tinst_depth;
10727
10728 set_refcount_ptr (current_tinst_level, level);
10729 pop_tinst_level ();
10730 if (current_tinst_level)
10731 current_tinst_level->errors = errorcount+sorrycount;
10732 return level->maybe_get_node ();
10733 }
10734
10735 /* Returns the TINST_LEVEL which gives the original instantiation
10736 context. */
10737
10738 struct tinst_level *
10739 outermost_tinst_level (void)
10740 {
10741 struct tinst_level *level = current_tinst_level;
10742 if (level)
10743 while (level->next)
10744 level = level->next;
10745 return level;
10746 }
10747
10748 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10749 vector of template arguments, as for tsubst.
10750
10751 Returns an appropriate tsubst'd friend declaration. */
10752
10753 static tree
10754 tsubst_friend_function (tree decl, tree args)
10755 {
10756 tree new_friend;
10757
10758 if (TREE_CODE (decl) == FUNCTION_DECL
10759 && DECL_TEMPLATE_INSTANTIATION (decl)
10760 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10761 /* This was a friend declared with an explicit template
10762 argument list, e.g.:
10763
10764 friend void f<>(T);
10765
10766 to indicate that f was a template instantiation, not a new
10767 function declaration. Now, we have to figure out what
10768 instantiation of what template. */
10769 {
10770 tree template_id, arglist, fns;
10771 tree new_args;
10772 tree tmpl;
10773 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10774
10775 /* Friend functions are looked up in the containing namespace scope.
10776 We must enter that scope, to avoid finding member functions of the
10777 current class with same name. */
10778 push_nested_namespace (ns);
10779 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10780 tf_warning_or_error, NULL_TREE,
10781 /*integral_constant_expression_p=*/false);
10782 pop_nested_namespace (ns);
10783 arglist = tsubst (DECL_TI_ARGS (decl), args,
10784 tf_warning_or_error, NULL_TREE);
10785 template_id = lookup_template_function (fns, arglist);
10786
10787 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10788 tmpl = determine_specialization (template_id, new_friend,
10789 &new_args,
10790 /*need_member_template=*/0,
10791 TREE_VEC_LENGTH (args),
10792 tsk_none);
10793 return instantiate_template (tmpl, new_args, tf_error);
10794 }
10795
10796 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10797
10798 /* The NEW_FRIEND will look like an instantiation, to the
10799 compiler, but is not an instantiation from the point of view of
10800 the language. For example, we might have had:
10801
10802 template <class T> struct S {
10803 template <class U> friend void f(T, U);
10804 };
10805
10806 Then, in S<int>, template <class U> void f(int, U) is not an
10807 instantiation of anything. */
10808 if (new_friend == error_mark_node)
10809 return error_mark_node;
10810
10811 DECL_USE_TEMPLATE (new_friend) = 0;
10812 if (TREE_CODE (decl) == TEMPLATE_DECL)
10813 {
10814 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10815 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10816 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10817
10818 /* Attach the template requirements to the new declaration
10819 for declaration matching. We need to rebuild the requirements
10820 so that parameter levels match. */
10821 if (tree ci = get_constraints (decl))
10822 {
10823 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10824 tree args = generic_targs_for (new_friend);
10825 tree treqs = tsubst_constraint (CI_TEMPLATE_REQS (ci), args,
10826 tf_warning_or_error, NULL_TREE);
10827 tree freqs = tsubst_constraint (CI_DECLARATOR_REQS (ci), args,
10828 tf_warning_or_error, NULL_TREE);
10829
10830 /* Update the constraints -- these won't really be valid for
10831 checking, but that's not what we need them for. These ensure
10832 that the declared function can find the friend during
10833 declaration matching. */
10834 tree new_ci = get_constraints (new_friend);
10835 CI_TEMPLATE_REQS (new_ci) = treqs;
10836 CI_DECLARATOR_REQS (new_ci) = freqs;
10837
10838 /* Also update the template parameter list. */
10839 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10840 }
10841 }
10842
10843 /* The mangled name for the NEW_FRIEND is incorrect. The function
10844 is not a template instantiation and should not be mangled like
10845 one. Therefore, we forget the mangling here; we'll recompute it
10846 later if we need it. */
10847 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10848 {
10849 SET_DECL_RTL (new_friend, NULL);
10850 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10851 }
10852
10853 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10854 {
10855 tree old_decl;
10856 tree new_friend_template_info;
10857 tree new_friend_result_template_info;
10858 tree ns;
10859 int new_friend_is_defn;
10860
10861 /* We must save some information from NEW_FRIEND before calling
10862 duplicate decls since that function will free NEW_FRIEND if
10863 possible. */
10864 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10865 new_friend_is_defn =
10866 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10867 (template_for_substitution (new_friend)))
10868 != NULL_TREE);
10869 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10870 {
10871 /* This declaration is a `primary' template. */
10872 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10873
10874 new_friend_result_template_info
10875 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10876 }
10877 else
10878 new_friend_result_template_info = NULL_TREE;
10879
10880 /* Inside pushdecl_namespace_level, we will push into the
10881 current namespace. However, the friend function should go
10882 into the namespace of the template. */
10883 ns = decl_namespace_context (new_friend);
10884 push_nested_namespace (ns);
10885 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10886 pop_nested_namespace (ns);
10887
10888 if (old_decl == error_mark_node)
10889 return error_mark_node;
10890
10891 if (old_decl != new_friend)
10892 {
10893 /* This new friend declaration matched an existing
10894 declaration. For example, given:
10895
10896 template <class T> void f(T);
10897 template <class U> class C {
10898 template <class T> friend void f(T) {}
10899 };
10900
10901 the friend declaration actually provides the definition
10902 of `f', once C has been instantiated for some type. So,
10903 old_decl will be the out-of-class template declaration,
10904 while new_friend is the in-class definition.
10905
10906 But, if `f' was called before this point, the
10907 instantiation of `f' will have DECL_TI_ARGS corresponding
10908 to `T' but not to `U', references to which might appear
10909 in the definition of `f'. Previously, the most general
10910 template for an instantiation of `f' was the out-of-class
10911 version; now it is the in-class version. Therefore, we
10912 run through all specialization of `f', adding to their
10913 DECL_TI_ARGS appropriately. In particular, they need a
10914 new set of outer arguments, corresponding to the
10915 arguments for this class instantiation.
10916
10917 The same situation can arise with something like this:
10918
10919 friend void f(int);
10920 template <class T> class C {
10921 friend void f(T) {}
10922 };
10923
10924 when `C<int>' is instantiated. Now, `f(int)' is defined
10925 in the class. */
10926
10927 if (!new_friend_is_defn)
10928 /* On the other hand, if the in-class declaration does
10929 *not* provide a definition, then we don't want to alter
10930 existing definitions. We can just leave everything
10931 alone. */
10932 ;
10933 else
10934 {
10935 tree new_template = TI_TEMPLATE (new_friend_template_info);
10936 tree new_args = TI_ARGS (new_friend_template_info);
10937
10938 /* Overwrite whatever template info was there before, if
10939 any, with the new template information pertaining to
10940 the declaration. */
10941 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10942
10943 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10944 {
10945 /* We should have called reregister_specialization in
10946 duplicate_decls. */
10947 gcc_assert (retrieve_specialization (new_template,
10948 new_args, 0)
10949 == old_decl);
10950
10951 /* Instantiate it if the global has already been used. */
10952 if (DECL_ODR_USED (old_decl))
10953 instantiate_decl (old_decl, /*defer_ok=*/true,
10954 /*expl_inst_class_mem_p=*/false);
10955 }
10956 else
10957 {
10958 tree t;
10959
10960 /* Indicate that the old function template is a partial
10961 instantiation. */
10962 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10963 = new_friend_result_template_info;
10964
10965 gcc_assert (new_template
10966 == most_general_template (new_template));
10967 gcc_assert (new_template != old_decl);
10968
10969 /* Reassign any specializations already in the hash table
10970 to the new more general template, and add the
10971 additional template args. */
10972 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10973 t != NULL_TREE;
10974 t = TREE_CHAIN (t))
10975 {
10976 tree spec = TREE_VALUE (t);
10977 spec_entry elt;
10978
10979 elt.tmpl = old_decl;
10980 elt.args = DECL_TI_ARGS (spec);
10981 elt.spec = NULL_TREE;
10982
10983 decl_specializations->remove_elt (&elt);
10984
10985 DECL_TI_ARGS (spec)
10986 = add_outermost_template_args (new_args,
10987 DECL_TI_ARGS (spec));
10988
10989 register_specialization
10990 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10991
10992 }
10993 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10994 }
10995 }
10996
10997 /* The information from NEW_FRIEND has been merged into OLD_DECL
10998 by duplicate_decls. */
10999 new_friend = old_decl;
11000 }
11001 }
11002 else
11003 {
11004 tree context = DECL_CONTEXT (new_friend);
11005 bool dependent_p;
11006
11007 /* In the code
11008 template <class T> class C {
11009 template <class U> friend void C1<U>::f (); // case 1
11010 friend void C2<T>::f (); // case 2
11011 };
11012 we only need to make sure CONTEXT is a complete type for
11013 case 2. To distinguish between the two cases, we note that
11014 CONTEXT of case 1 remains dependent type after tsubst while
11015 this isn't true for case 2. */
11016 ++processing_template_decl;
11017 dependent_p = dependent_type_p (context);
11018 --processing_template_decl;
11019
11020 if (!dependent_p
11021 && !complete_type_or_else (context, NULL_TREE))
11022 return error_mark_node;
11023
11024 if (COMPLETE_TYPE_P (context))
11025 {
11026 tree fn = new_friend;
11027 /* do_friend adds the TEMPLATE_DECL for any member friend
11028 template even if it isn't a member template, i.e.
11029 template <class T> friend A<T>::f();
11030 Look through it in that case. */
11031 if (TREE_CODE (fn) == TEMPLATE_DECL
11032 && !PRIMARY_TEMPLATE_P (fn))
11033 fn = DECL_TEMPLATE_RESULT (fn);
11034 /* Check to see that the declaration is really present, and,
11035 possibly obtain an improved declaration. */
11036 fn = check_classfn (context, fn, NULL_TREE);
11037
11038 if (fn)
11039 new_friend = fn;
11040 }
11041 }
11042
11043 return new_friend;
11044 }
11045
11046 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11047 template arguments, as for tsubst.
11048
11049 Returns an appropriate tsubst'd friend type or error_mark_node on
11050 failure. */
11051
11052 static tree
11053 tsubst_friend_class (tree friend_tmpl, tree args)
11054 {
11055 tree tmpl;
11056
11057 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11058 {
11059 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11060 return TREE_TYPE (tmpl);
11061 }
11062
11063 tree context = CP_DECL_CONTEXT (friend_tmpl);
11064 if (TREE_CODE (context) == NAMESPACE_DECL)
11065 push_nested_namespace (context);
11066 else
11067 {
11068 context = tsubst (context, args, tf_error, NULL_TREE);
11069 push_nested_class (context);
11070 }
11071
11072 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
11073 /*non_class=*/false, /*block_p=*/false,
11074 /*namespaces_only=*/false, LOOKUP_HIDDEN);
11075
11076 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11077 {
11078 /* The friend template has already been declared. Just
11079 check to see that the declarations match, and install any new
11080 default parameters. We must tsubst the default parameters,
11081 of course. We only need the innermost template parameters
11082 because that is all that redeclare_class_template will look
11083 at. */
11084 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11085 > TMPL_ARGS_DEPTH (args))
11086 {
11087 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11088 args, tf_warning_or_error);
11089 location_t saved_input_location = input_location;
11090 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11091 tree cons = get_constraints (tmpl);
11092 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11093 input_location = saved_input_location;
11094 }
11095 }
11096 else
11097 {
11098 /* The friend template has not already been declared. In this
11099 case, the instantiation of the template class will cause the
11100 injection of this template into the namespace scope. */
11101 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11102
11103 if (tmpl != error_mark_node)
11104 {
11105 /* The new TMPL is not an instantiation of anything, so we
11106 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11107 for the new type because that is supposed to be the
11108 corresponding template decl, i.e., TMPL. */
11109 DECL_USE_TEMPLATE (tmpl) = 0;
11110 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11111 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11112 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11113 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11114
11115 /* It is hidden. */
11116 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11117 DECL_ANTICIPATED (tmpl)
11118 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11119
11120 /* Inject this template into the enclosing namspace scope. */
11121 tmpl = pushdecl_namespace_level (tmpl, true);
11122 }
11123 }
11124
11125 if (TREE_CODE (context) == NAMESPACE_DECL)
11126 pop_nested_namespace (context);
11127 else
11128 pop_nested_class ();
11129
11130 return TREE_TYPE (tmpl);
11131 }
11132
11133 /* Returns zero if TYPE cannot be completed later due to circularity.
11134 Otherwise returns one. */
11135
11136 static int
11137 can_complete_type_without_circularity (tree type)
11138 {
11139 if (type == NULL_TREE || type == error_mark_node)
11140 return 0;
11141 else if (COMPLETE_TYPE_P (type))
11142 return 1;
11143 else if (TREE_CODE (type) == ARRAY_TYPE)
11144 return can_complete_type_without_circularity (TREE_TYPE (type));
11145 else if (CLASS_TYPE_P (type)
11146 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11147 return 0;
11148 else
11149 return 1;
11150 }
11151
11152 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11153 tsubst_flags_t, tree);
11154
11155 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11156 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11157
11158 static tree
11159 tsubst_attribute (tree t, tree *decl_p, tree args,
11160 tsubst_flags_t complain, tree in_decl)
11161 {
11162 gcc_assert (ATTR_IS_DEPENDENT (t));
11163
11164 tree val = TREE_VALUE (t);
11165 if (val == NULL_TREE)
11166 /* Nothing to do. */;
11167 else if ((flag_openmp || flag_openmp_simd)
11168 && is_attribute_p ("omp declare simd",
11169 get_attribute_name (t)))
11170 {
11171 tree clauses = TREE_VALUE (val);
11172 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11173 complain, in_decl);
11174 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11175 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11176 tree parms = DECL_ARGUMENTS (*decl_p);
11177 clauses
11178 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11179 if (clauses)
11180 val = build_tree_list (NULL_TREE, clauses);
11181 else
11182 val = NULL_TREE;
11183 }
11184 else if (flag_openmp
11185 && is_attribute_p ("omp declare variant base",
11186 get_attribute_name (t)))
11187 {
11188 ++cp_unevaluated_operand;
11189 tree varid
11190 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11191 in_decl, /*integral_constant_expression_p=*/false);
11192 --cp_unevaluated_operand;
11193 tree chain = TREE_CHAIN (val);
11194 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11195 tree ctx = copy_list (TREE_VALUE (val));
11196 tree simd = get_identifier ("simd");
11197 tree score = get_identifier (" score");
11198 tree condition = get_identifier ("condition");
11199 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11200 {
11201 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11202 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11203 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11204 {
11205 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11206 {
11207 tree clauses = TREE_VALUE (t2);
11208 clauses = tsubst_omp_clauses (clauses,
11209 C_ORT_OMP_DECLARE_SIMD, args,
11210 complain, in_decl);
11211 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11212 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11213 TREE_VALUE (t2) = clauses;
11214 }
11215 else
11216 {
11217 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11218 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11219 if (TREE_VALUE (t3))
11220 {
11221 bool allow_string
11222 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11223 && TREE_PURPOSE (t3) != score);
11224 tree v = TREE_VALUE (t3);
11225 if (TREE_CODE (v) == STRING_CST && allow_string)
11226 continue;
11227 v = tsubst_expr (v, args, complain, in_decl, true);
11228 v = fold_non_dependent_expr (v);
11229 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11230 || (TREE_PURPOSE (t3) == score
11231 ? TREE_CODE (v) != INTEGER_CST
11232 : !tree_fits_shwi_p (v)))
11233 {
11234 location_t loc
11235 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11236 match_loc);
11237 if (TREE_PURPOSE (t3) == score)
11238 error_at (loc, "score argument must be "
11239 "constant integer expression");
11240 else if (allow_string)
11241 error_at (loc, "property must be constant "
11242 "integer expression or string "
11243 "literal");
11244 else
11245 error_at (loc, "property must be constant "
11246 "integer expression");
11247 return NULL_TREE;
11248 }
11249 else if (TREE_PURPOSE (t3) == score
11250 && tree_int_cst_sgn (v) < 0)
11251 {
11252 location_t loc
11253 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11254 match_loc);
11255 error_at (loc, "score argument must be "
11256 "non-negative");
11257 return NULL_TREE;
11258 }
11259 TREE_VALUE (t3) = v;
11260 }
11261 }
11262 }
11263 }
11264 val = tree_cons (varid, ctx, chain);
11265 }
11266 /* If the first attribute argument is an identifier, don't
11267 pass it through tsubst. Attributes like mode, format,
11268 cleanup and several target specific attributes expect it
11269 unmodified. */
11270 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11271 {
11272 tree chain
11273 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11274 /*integral_constant_expression_p=*/false);
11275 if (chain != TREE_CHAIN (val))
11276 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11277 }
11278 else if (PACK_EXPANSION_P (val))
11279 {
11280 /* An attribute pack expansion. */
11281 tree purp = TREE_PURPOSE (t);
11282 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11283 if (pack == error_mark_node)
11284 return error_mark_node;
11285 int len = TREE_VEC_LENGTH (pack);
11286 tree list = NULL_TREE;
11287 tree *q = &list;
11288 for (int i = 0; i < len; ++i)
11289 {
11290 tree elt = TREE_VEC_ELT (pack, i);
11291 *q = build_tree_list (purp, elt);
11292 q = &TREE_CHAIN (*q);
11293 }
11294 return list;
11295 }
11296 else
11297 val = tsubst_expr (val, args, complain, in_decl,
11298 /*integral_constant_expression_p=*/false);
11299
11300 if (val != TREE_VALUE (t))
11301 return build_tree_list (TREE_PURPOSE (t), val);
11302 return t;
11303 }
11304
11305 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11306 unchanged or a new TREE_LIST chain. */
11307
11308 static tree
11309 tsubst_attributes (tree attributes, tree args,
11310 tsubst_flags_t complain, tree in_decl)
11311 {
11312 tree last_dep = NULL_TREE;
11313
11314 for (tree t = attributes; t; t = TREE_CHAIN (t))
11315 if (ATTR_IS_DEPENDENT (t))
11316 {
11317 last_dep = t;
11318 attributes = copy_list (attributes);
11319 break;
11320 }
11321
11322 if (last_dep)
11323 for (tree *p = &attributes; *p; )
11324 {
11325 tree t = *p;
11326 if (ATTR_IS_DEPENDENT (t))
11327 {
11328 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11329 if (subst != t)
11330 {
11331 *p = subst;
11332 while (*p)
11333 p = &TREE_CHAIN (*p);
11334 *p = TREE_CHAIN (t);
11335 continue;
11336 }
11337 }
11338 p = &TREE_CHAIN (*p);
11339 }
11340
11341 return attributes;
11342 }
11343
11344 /* Apply any attributes which had to be deferred until instantiation
11345 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11346 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11347
11348 static void
11349 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11350 tree args, tsubst_flags_t complain, tree in_decl)
11351 {
11352 tree last_dep = NULL_TREE;
11353 tree t;
11354 tree *p;
11355
11356 if (attributes == NULL_TREE)
11357 return;
11358
11359 if (DECL_P (*decl_p))
11360 {
11361 if (TREE_TYPE (*decl_p) == error_mark_node)
11362 return;
11363 p = &DECL_ATTRIBUTES (*decl_p);
11364 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11365 to our attributes parameter. */
11366 gcc_assert (*p == attributes);
11367 }
11368 else
11369 {
11370 p = &TYPE_ATTRIBUTES (*decl_p);
11371 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11372 lookup_template_class_1, and should be preserved. */
11373 gcc_assert (*p != attributes);
11374 while (*p)
11375 p = &TREE_CHAIN (*p);
11376 }
11377
11378 for (t = attributes; t; t = TREE_CHAIN (t))
11379 if (ATTR_IS_DEPENDENT (t))
11380 {
11381 last_dep = t;
11382 attributes = copy_list (attributes);
11383 break;
11384 }
11385
11386 *p = attributes;
11387 if (last_dep)
11388 {
11389 tree late_attrs = NULL_TREE;
11390 tree *q = &late_attrs;
11391
11392 for (; *p; )
11393 {
11394 t = *p;
11395 if (ATTR_IS_DEPENDENT (t))
11396 {
11397 *p = TREE_CHAIN (t);
11398 TREE_CHAIN (t) = NULL_TREE;
11399 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11400 while (*q)
11401 q = &TREE_CHAIN (*q);
11402 }
11403 else
11404 p = &TREE_CHAIN (t);
11405 }
11406
11407 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11408 }
11409 }
11410
11411 /* Perform (or defer) access check for typedefs that were referenced
11412 from within the template TMPL code.
11413 This is a subroutine of instantiate_decl and instantiate_class_template.
11414 TMPL is the template to consider and TARGS is the list of arguments of
11415 that template. */
11416
11417 static void
11418 perform_typedefs_access_check (tree tmpl, tree targs)
11419 {
11420 unsigned i;
11421 qualified_typedef_usage_t *iter;
11422
11423 if (!tmpl
11424 || (!CLASS_TYPE_P (tmpl)
11425 && TREE_CODE (tmpl) != FUNCTION_DECL))
11426 return;
11427
11428 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
11429 {
11430 tree type_decl = iter->typedef_decl;
11431 tree type_scope = iter->context;
11432
11433 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
11434 continue;
11435
11436 if (uses_template_parms (type_decl))
11437 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
11438 if (uses_template_parms (type_scope))
11439 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11440
11441 /* Make access check error messages point to the location
11442 of the use of the typedef. */
11443 iloc_sentinel ils (iter->locus);
11444 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11445 type_decl, type_decl,
11446 tf_warning_or_error);
11447 }
11448 }
11449
11450 static tree
11451 instantiate_class_template_1 (tree type)
11452 {
11453 tree templ, args, pattern, t, member;
11454 tree typedecl;
11455 tree pbinfo;
11456 tree base_list;
11457 unsigned int saved_maximum_field_alignment;
11458 tree fn_context;
11459
11460 if (type == error_mark_node)
11461 return error_mark_node;
11462
11463 if (COMPLETE_OR_OPEN_TYPE_P (type)
11464 || uses_template_parms (type))
11465 return type;
11466
11467 /* Figure out which template is being instantiated. */
11468 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11469 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11470
11471 /* Mark the type as in the process of being defined. */
11472 TYPE_BEING_DEFINED (type) = 1;
11473
11474 /* We may be in the middle of deferred access check. Disable
11475 it now. */
11476 deferring_access_check_sentinel acs (dk_no_deferred);
11477
11478 /* Determine what specialization of the original template to
11479 instantiate. */
11480 t = most_specialized_partial_spec (type, tf_warning_or_error);
11481 if (t == error_mark_node)
11482 return error_mark_node;
11483 else if (t)
11484 {
11485 /* This TYPE is actually an instantiation of a partial
11486 specialization. We replace the innermost set of ARGS with
11487 the arguments appropriate for substitution. For example,
11488 given:
11489
11490 template <class T> struct S {};
11491 template <class T> struct S<T*> {};
11492
11493 and supposing that we are instantiating S<int*>, ARGS will
11494 presently be {int*} -- but we need {int}. */
11495 pattern = TREE_TYPE (t);
11496 args = TREE_PURPOSE (t);
11497 }
11498 else
11499 {
11500 pattern = TREE_TYPE (templ);
11501 args = CLASSTYPE_TI_ARGS (type);
11502 }
11503
11504 /* If the template we're instantiating is incomplete, then clearly
11505 there's nothing we can do. */
11506 if (!COMPLETE_TYPE_P (pattern))
11507 {
11508 /* We can try again later. */
11509 TYPE_BEING_DEFINED (type) = 0;
11510 return type;
11511 }
11512
11513 /* If we've recursively instantiated too many templates, stop. */
11514 if (! push_tinst_level (type))
11515 return type;
11516
11517 int saved_unevaluated_operand = cp_unevaluated_operand;
11518 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11519
11520 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11521 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11522 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11523 fn_context = error_mark_node;
11524 if (!fn_context)
11525 push_to_top_level ();
11526 else
11527 {
11528 cp_unevaluated_operand = 0;
11529 c_inhibit_evaluation_warnings = 0;
11530 }
11531 /* Use #pragma pack from the template context. */
11532 saved_maximum_field_alignment = maximum_field_alignment;
11533 maximum_field_alignment = TYPE_PRECISION (pattern);
11534
11535 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11536
11537 /* Set the input location to the most specialized template definition.
11538 This is needed if tsubsting causes an error. */
11539 typedecl = TYPE_MAIN_DECL (pattern);
11540 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11541 DECL_SOURCE_LOCATION (typedecl);
11542
11543 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11544 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11545 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11546 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11547 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
11548 if (ANON_AGGR_TYPE_P (pattern))
11549 SET_ANON_AGGR_TYPE_P (type);
11550 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11551 {
11552 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11553 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11554 /* Adjust visibility for template arguments. */
11555 determine_visibility (TYPE_MAIN_DECL (type));
11556 }
11557 if (CLASS_TYPE_P (type))
11558 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11559
11560 pbinfo = TYPE_BINFO (pattern);
11561
11562 /* We should never instantiate a nested class before its enclosing
11563 class; we need to look up the nested class by name before we can
11564 instantiate it, and that lookup should instantiate the enclosing
11565 class. */
11566 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11567 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11568
11569 base_list = NULL_TREE;
11570 if (BINFO_N_BASE_BINFOS (pbinfo))
11571 {
11572 tree pbase_binfo;
11573 tree pushed_scope;
11574 int i;
11575
11576 /* We must enter the scope containing the type, as that is where
11577 the accessibility of types named in dependent bases are
11578 looked up from. */
11579 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11580
11581 /* Substitute into each of the bases to determine the actual
11582 basetypes. */
11583 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11584 {
11585 tree base;
11586 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11587 tree expanded_bases = NULL_TREE;
11588 int idx, len = 1;
11589
11590 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11591 {
11592 expanded_bases =
11593 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11594 args, tf_error, NULL_TREE);
11595 if (expanded_bases == error_mark_node)
11596 continue;
11597
11598 len = TREE_VEC_LENGTH (expanded_bases);
11599 }
11600
11601 for (idx = 0; idx < len; idx++)
11602 {
11603 if (expanded_bases)
11604 /* Extract the already-expanded base class. */
11605 base = TREE_VEC_ELT (expanded_bases, idx);
11606 else
11607 /* Substitute to figure out the base class. */
11608 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11609 NULL_TREE);
11610
11611 if (base == error_mark_node)
11612 continue;
11613
11614 base_list = tree_cons (access, base, base_list);
11615 if (BINFO_VIRTUAL_P (pbase_binfo))
11616 TREE_TYPE (base_list) = integer_type_node;
11617 }
11618 }
11619
11620 /* The list is now in reverse order; correct that. */
11621 base_list = nreverse (base_list);
11622
11623 if (pushed_scope)
11624 pop_scope (pushed_scope);
11625 }
11626 /* Now call xref_basetypes to set up all the base-class
11627 information. */
11628 xref_basetypes (type, base_list);
11629
11630 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11631 (int) ATTR_FLAG_TYPE_IN_PLACE,
11632 args, tf_error, NULL_TREE);
11633 fixup_attribute_variants (type);
11634
11635 /* Now that our base classes are set up, enter the scope of the
11636 class, so that name lookups into base classes, etc. will work
11637 correctly. This is precisely analogous to what we do in
11638 begin_class_definition when defining an ordinary non-template
11639 class, except we also need to push the enclosing classes. */
11640 push_nested_class (type);
11641
11642 /* Now members are processed in the order of declaration. */
11643 for (member = CLASSTYPE_DECL_LIST (pattern);
11644 member; member = TREE_CHAIN (member))
11645 {
11646 tree t = TREE_VALUE (member);
11647
11648 if (TREE_PURPOSE (member))
11649 {
11650 if (TYPE_P (t))
11651 {
11652 if (LAMBDA_TYPE_P (t))
11653 /* A closure type for a lambda in an NSDMI or default argument.
11654 Ignore it; it will be regenerated when needed. */
11655 continue;
11656
11657 /* Build new CLASSTYPE_NESTED_UTDS. */
11658
11659 tree newtag;
11660 bool class_template_p;
11661
11662 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11663 && TYPE_LANG_SPECIFIC (t)
11664 && CLASSTYPE_IS_TEMPLATE (t));
11665 /* If the member is a class template, then -- even after
11666 substitution -- there may be dependent types in the
11667 template argument list for the class. We increment
11668 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11669 that function will assume that no types are dependent
11670 when outside of a template. */
11671 if (class_template_p)
11672 ++processing_template_decl;
11673 newtag = tsubst (t, args, tf_error, NULL_TREE);
11674 if (class_template_p)
11675 --processing_template_decl;
11676 if (newtag == error_mark_node)
11677 continue;
11678
11679 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11680 {
11681 tree name = TYPE_IDENTIFIER (t);
11682
11683 if (class_template_p)
11684 /* Unfortunately, lookup_template_class sets
11685 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11686 instantiation (i.e., for the type of a member
11687 template class nested within a template class.)
11688 This behavior is required for
11689 maybe_process_partial_specialization to work
11690 correctly, but is not accurate in this case;
11691 the TAG is not an instantiation of anything.
11692 (The corresponding TEMPLATE_DECL is an
11693 instantiation, but the TYPE is not.) */
11694 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11695
11696 /* Now, we call pushtag to put this NEWTAG into the scope of
11697 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11698 pushtag calling push_template_decl. We don't have to do
11699 this for enums because it will already have been done in
11700 tsubst_enum. */
11701 if (name)
11702 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11703 pushtag (name, newtag, /*tag_scope=*/ts_current);
11704 }
11705 }
11706 else if (DECL_DECLARES_FUNCTION_P (t))
11707 {
11708 tree r;
11709
11710 if (TREE_CODE (t) == TEMPLATE_DECL)
11711 ++processing_template_decl;
11712 r = tsubst (t, args, tf_error, NULL_TREE);
11713 if (TREE_CODE (t) == TEMPLATE_DECL)
11714 --processing_template_decl;
11715 set_current_access_from_decl (r);
11716 finish_member_declaration (r);
11717 /* Instantiate members marked with attribute used. */
11718 if (r != error_mark_node && DECL_PRESERVE_P (r))
11719 mark_used (r);
11720 if (TREE_CODE (r) == FUNCTION_DECL
11721 && DECL_OMP_DECLARE_REDUCTION_P (r))
11722 cp_check_omp_declare_reduction (r);
11723 }
11724 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11725 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11726 /* A closure type for a lambda in an NSDMI or default argument.
11727 Ignore it; it will be regenerated when needed. */;
11728 else
11729 {
11730 /* Build new TYPE_FIELDS. */
11731 if (TREE_CODE (t) == STATIC_ASSERT)
11732 {
11733 tree condition;
11734
11735 ++c_inhibit_evaluation_warnings;
11736 condition =
11737 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11738 tf_warning_or_error, NULL_TREE,
11739 /*integral_constant_expression_p=*/true);
11740 --c_inhibit_evaluation_warnings;
11741
11742 finish_static_assert (condition,
11743 STATIC_ASSERT_MESSAGE (t),
11744 STATIC_ASSERT_SOURCE_LOCATION (t),
11745 /*member_p=*/true);
11746 }
11747 else if (TREE_CODE (t) != CONST_DECL)
11748 {
11749 tree r;
11750 tree vec = NULL_TREE;
11751 int len = 1;
11752
11753 /* The file and line for this declaration, to
11754 assist in error message reporting. Since we
11755 called push_tinst_level above, we don't need to
11756 restore these. */
11757 input_location = DECL_SOURCE_LOCATION (t);
11758
11759 if (TREE_CODE (t) == TEMPLATE_DECL)
11760 ++processing_template_decl;
11761 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11762 if (TREE_CODE (t) == TEMPLATE_DECL)
11763 --processing_template_decl;
11764
11765 if (TREE_CODE (r) == TREE_VEC)
11766 {
11767 /* A capture pack became multiple fields. */
11768 vec = r;
11769 len = TREE_VEC_LENGTH (vec);
11770 }
11771
11772 for (int i = 0; i < len; ++i)
11773 {
11774 if (vec)
11775 r = TREE_VEC_ELT (vec, i);
11776 if (VAR_P (r))
11777 {
11778 /* In [temp.inst]:
11779
11780 [t]he initialization (and any associated
11781 side-effects) of a static data member does
11782 not occur unless the static data member is
11783 itself used in a way that requires the
11784 definition of the static data member to
11785 exist.
11786
11787 Therefore, we do not substitute into the
11788 initialized for the static data member here. */
11789 finish_static_data_member_decl
11790 (r,
11791 /*init=*/NULL_TREE,
11792 /*init_const_expr_p=*/false,
11793 /*asmspec_tree=*/NULL_TREE,
11794 /*flags=*/0);
11795 /* Instantiate members marked with attribute used. */
11796 if (r != error_mark_node && DECL_PRESERVE_P (r))
11797 mark_used (r);
11798 }
11799 else if (TREE_CODE (r) == FIELD_DECL)
11800 {
11801 /* Determine whether R has a valid type and can be
11802 completed later. If R is invalid, then its type
11803 is replaced by error_mark_node. */
11804 tree rtype = TREE_TYPE (r);
11805 if (can_complete_type_without_circularity (rtype))
11806 complete_type (rtype);
11807
11808 if (!complete_or_array_type_p (rtype))
11809 {
11810 /* If R's type couldn't be completed and
11811 it isn't a flexible array member (whose
11812 type is incomplete by definition) give
11813 an error. */
11814 cxx_incomplete_type_error (r, rtype);
11815 TREE_TYPE (r) = error_mark_node;
11816 }
11817 else if (TREE_CODE (rtype) == ARRAY_TYPE
11818 && TYPE_DOMAIN (rtype) == NULL_TREE
11819 && (TREE_CODE (type) == UNION_TYPE
11820 || TREE_CODE (type) == QUAL_UNION_TYPE))
11821 {
11822 error ("flexible array member %qD in union", r);
11823 TREE_TYPE (r) = error_mark_node;
11824 }
11825 else if (!verify_type_context (input_location,
11826 TCTX_FIELD, rtype))
11827 TREE_TYPE (r) = error_mark_node;
11828 }
11829
11830 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11831 such a thing will already have been added to the field
11832 list by tsubst_enum in finish_member_declaration in the
11833 CLASSTYPE_NESTED_UTDS case above. */
11834 if (!(TREE_CODE (r) == TYPE_DECL
11835 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11836 && DECL_ARTIFICIAL (r)))
11837 {
11838 set_current_access_from_decl (r);
11839 finish_member_declaration (r);
11840 }
11841 }
11842 }
11843 }
11844 }
11845 else
11846 {
11847 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11848 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11849 {
11850 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11851
11852 tree friend_type = t;
11853 bool adjust_processing_template_decl = false;
11854
11855 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11856 {
11857 /* template <class T> friend class C; */
11858 friend_type = tsubst_friend_class (friend_type, args);
11859 adjust_processing_template_decl = true;
11860 }
11861 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11862 {
11863 /* template <class T> friend class C::D; */
11864 friend_type = tsubst (friend_type, args,
11865 tf_warning_or_error, NULL_TREE);
11866 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11867 friend_type = TREE_TYPE (friend_type);
11868 adjust_processing_template_decl = true;
11869 }
11870 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11871 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11872 {
11873 /* This could be either
11874
11875 friend class T::C;
11876
11877 when dependent_type_p is false or
11878
11879 template <class U> friend class T::C;
11880
11881 otherwise. */
11882 /* Bump processing_template_decl in case this is something like
11883 template <class T> friend struct A<T>::B. */
11884 ++processing_template_decl;
11885 friend_type = tsubst (friend_type, args,
11886 tf_warning_or_error, NULL_TREE);
11887 if (dependent_type_p (friend_type))
11888 adjust_processing_template_decl = true;
11889 --processing_template_decl;
11890 }
11891 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11892 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11893 && TYPE_HIDDEN_P (friend_type))
11894 {
11895 /* friend class C;
11896
11897 where C hasn't been declared yet. Let's lookup name
11898 from namespace scope directly, bypassing any name that
11899 come from dependent base class. */
11900 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11901
11902 /* The call to xref_tag_from_type does injection for friend
11903 classes. */
11904 push_nested_namespace (ns);
11905 friend_type =
11906 xref_tag_from_type (friend_type, NULL_TREE,
11907 /*tag_scope=*/ts_current);
11908 pop_nested_namespace (ns);
11909 }
11910 else if (uses_template_parms (friend_type))
11911 /* friend class C<T>; */
11912 friend_type = tsubst (friend_type, args,
11913 tf_warning_or_error, NULL_TREE);
11914 /* Otherwise it's
11915
11916 friend class C;
11917
11918 where C is already declared or
11919
11920 friend class C<int>;
11921
11922 We don't have to do anything in these cases. */
11923
11924 if (adjust_processing_template_decl)
11925 /* Trick make_friend_class into realizing that the friend
11926 we're adding is a template, not an ordinary class. It's
11927 important that we use make_friend_class since it will
11928 perform some error-checking and output cross-reference
11929 information. */
11930 ++processing_template_decl;
11931
11932 if (friend_type != error_mark_node)
11933 make_friend_class (type, friend_type, /*complain=*/false);
11934
11935 if (adjust_processing_template_decl)
11936 --processing_template_decl;
11937 }
11938 else
11939 {
11940 /* Build new DECL_FRIENDLIST. */
11941 tree r;
11942
11943 /* The file and line for this declaration, to
11944 assist in error message reporting. Since we
11945 called push_tinst_level above, we don't need to
11946 restore these. */
11947 input_location = DECL_SOURCE_LOCATION (t);
11948
11949 if (TREE_CODE (t) == TEMPLATE_DECL)
11950 {
11951 ++processing_template_decl;
11952 push_deferring_access_checks (dk_no_check);
11953 }
11954
11955 r = tsubst_friend_function (t, args);
11956 add_friend (type, r, /*complain=*/false);
11957 if (TREE_CODE (t) == TEMPLATE_DECL)
11958 {
11959 pop_deferring_access_checks ();
11960 --processing_template_decl;
11961 }
11962 }
11963 }
11964 }
11965
11966 if (fn_context)
11967 {
11968 /* Restore these before substituting into the lambda capture
11969 initializers. */
11970 cp_unevaluated_operand = saved_unevaluated_operand;
11971 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11972 }
11973
11974 /* Set the file and line number information to whatever is given for
11975 the class itself. This puts error messages involving generated
11976 implicit functions at a predictable point, and the same point
11977 that would be used for non-template classes. */
11978 input_location = DECL_SOURCE_LOCATION (typedecl);
11979
11980 unreverse_member_declarations (type);
11981 finish_struct_1 (type);
11982 TYPE_BEING_DEFINED (type) = 0;
11983
11984 /* We don't instantiate default arguments for member functions. 14.7.1:
11985
11986 The implicit instantiation of a class template specialization causes
11987 the implicit instantiation of the declarations, but not of the
11988 definitions or default arguments, of the class member functions,
11989 member classes, static data members and member templates.... */
11990
11991 /* Some typedefs referenced from within the template code need to be access
11992 checked at template instantiation time, i.e now. These types were
11993 added to the template at parsing time. Let's get those and perform
11994 the access checks then. */
11995 perform_typedefs_access_check (pattern, args);
11996 perform_deferred_access_checks (tf_warning_or_error);
11997 pop_nested_class ();
11998 maximum_field_alignment = saved_maximum_field_alignment;
11999 if (!fn_context)
12000 pop_from_top_level ();
12001 pop_tinst_level ();
12002
12003 /* The vtable for a template class can be emitted in any translation
12004 unit in which the class is instantiated. When there is no key
12005 method, however, finish_struct_1 will already have added TYPE to
12006 the keyed_classes. */
12007 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12008 vec_safe_push (keyed_classes, type);
12009
12010 return type;
12011 }
12012
12013 /* Wrapper for instantiate_class_template_1. */
12014
12015 tree
12016 instantiate_class_template (tree type)
12017 {
12018 tree ret;
12019 timevar_push (TV_TEMPLATE_INST);
12020 ret = instantiate_class_template_1 (type);
12021 timevar_pop (TV_TEMPLATE_INST);
12022 return ret;
12023 }
12024
12025 tree
12026 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12027 {
12028 tree r;
12029
12030 if (!t)
12031 r = t;
12032 else if (TYPE_P (t))
12033 r = tsubst (t, args, complain, in_decl);
12034 else
12035 {
12036 if (!(complain & tf_warning))
12037 ++c_inhibit_evaluation_warnings;
12038 r = tsubst_expr (t, args, complain, in_decl,
12039 /*integral_constant_expression_p=*/true);
12040 if (!(complain & tf_warning))
12041 --c_inhibit_evaluation_warnings;
12042 }
12043
12044 return r;
12045 }
12046
12047 /* Given a function parameter pack TMPL_PARM and some function parameters
12048 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12049 and set *SPEC_P to point at the next point in the list. */
12050
12051 tree
12052 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12053 {
12054 /* Collect all of the extra "packed" parameters into an
12055 argument pack. */
12056 tree parmvec;
12057 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12058 tree spec_parm = *spec_p;
12059 int i, len;
12060
12061 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12062 if (tmpl_parm
12063 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12064 break;
12065
12066 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12067 parmvec = make_tree_vec (len);
12068 spec_parm = *spec_p;
12069 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12070 {
12071 tree elt = spec_parm;
12072 if (DECL_PACK_P (elt))
12073 elt = make_pack_expansion (elt);
12074 TREE_VEC_ELT (parmvec, i) = elt;
12075 }
12076
12077 /* Build the argument packs. */
12078 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12079 *spec_p = spec_parm;
12080
12081 return argpack;
12082 }
12083
12084 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12085 NONTYPE_ARGUMENT_PACK. */
12086
12087 static tree
12088 make_fnparm_pack (tree spec_parm)
12089 {
12090 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12091 }
12092
12093 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12094 pack expansion with no extra args, 2 if it has extra args, or 0
12095 if it is not a pack expansion. */
12096
12097 static int
12098 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12099 {
12100 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12101 /* We're being called before this happens in tsubst_pack_expansion. */
12102 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12103 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12104 if (i >= TREE_VEC_LENGTH (vec))
12105 return 0;
12106 tree elt = TREE_VEC_ELT (vec, i);
12107 if (DECL_P (elt))
12108 /* A decl pack is itself an expansion. */
12109 elt = TREE_TYPE (elt);
12110 if (!PACK_EXPANSION_P (elt))
12111 return 0;
12112 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12113 return 2;
12114 return 1;
12115 }
12116
12117
12118 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12119
12120 static tree
12121 make_argument_pack_select (tree arg_pack, unsigned index)
12122 {
12123 tree aps = make_node (ARGUMENT_PACK_SELECT);
12124
12125 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12126 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12127
12128 return aps;
12129 }
12130
12131 /* This is a subroutine of tsubst_pack_expansion.
12132
12133 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12134 mechanism to store the (non complete list of) arguments of the
12135 substitution and return a non substituted pack expansion, in order
12136 to wait for when we have enough arguments to really perform the
12137 substitution. */
12138
12139 static bool
12140 use_pack_expansion_extra_args_p (tree parm_packs,
12141 int arg_pack_len,
12142 bool has_empty_arg)
12143 {
12144 /* If one pack has an expansion and another pack has a normal
12145 argument or if one pack has an empty argument and an another
12146 one hasn't then tsubst_pack_expansion cannot perform the
12147 substitution and need to fall back on the
12148 PACK_EXPANSION_EXTRA mechanism. */
12149 if (parm_packs == NULL_TREE)
12150 return false;
12151 else if (has_empty_arg)
12152 {
12153 /* If all the actual packs are pack expansions, we can still
12154 subsitute directly. */
12155 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12156 {
12157 tree a = TREE_VALUE (p);
12158 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12159 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12160 a = ARGUMENT_PACK_ARGS (a);
12161 if (TREE_VEC_LENGTH (a) == 1)
12162 a = TREE_VEC_ELT (a, 0);
12163 if (PACK_EXPANSION_P (a))
12164 continue;
12165 return true;
12166 }
12167 return false;
12168 }
12169
12170 bool has_expansion_arg = false;
12171 for (int i = 0 ; i < arg_pack_len; ++i)
12172 {
12173 bool has_non_expansion_arg = false;
12174 for (tree parm_pack = parm_packs;
12175 parm_pack;
12176 parm_pack = TREE_CHAIN (parm_pack))
12177 {
12178 tree arg = TREE_VALUE (parm_pack);
12179
12180 int exp = argument_pack_element_is_expansion_p (arg, i);
12181 if (exp == 2)
12182 /* We can't substitute a pack expansion with extra args into
12183 our pattern. */
12184 return true;
12185 else if (exp)
12186 has_expansion_arg = true;
12187 else
12188 has_non_expansion_arg = true;
12189 }
12190
12191 if (has_expansion_arg && has_non_expansion_arg)
12192 return true;
12193 }
12194 return false;
12195 }
12196
12197 /* [temp.variadic]/6 says that:
12198
12199 The instantiation of a pack expansion [...]
12200 produces a list E1,E2, ..., En, where N is the number of elements
12201 in the pack expansion parameters.
12202
12203 This subroutine of tsubst_pack_expansion produces one of these Ei.
12204
12205 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12206 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12207 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12208 INDEX is the index 'i' of the element Ei to produce. ARGS,
12209 COMPLAIN, and IN_DECL are the same parameters as for the
12210 tsubst_pack_expansion function.
12211
12212 The function returns the resulting Ei upon successful completion,
12213 or error_mark_node.
12214
12215 Note that this function possibly modifies the ARGS parameter, so
12216 it's the responsibility of the caller to restore it. */
12217
12218 static tree
12219 gen_elem_of_pack_expansion_instantiation (tree pattern,
12220 tree parm_packs,
12221 unsigned index,
12222 tree args /* This parm gets
12223 modified. */,
12224 tsubst_flags_t complain,
12225 tree in_decl)
12226 {
12227 tree t;
12228 bool ith_elem_is_expansion = false;
12229
12230 /* For each parameter pack, change the substitution of the parameter
12231 pack to the ith argument in its argument pack, then expand the
12232 pattern. */
12233 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12234 {
12235 tree parm = TREE_PURPOSE (pack);
12236 tree arg_pack = TREE_VALUE (pack);
12237 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12238
12239 ith_elem_is_expansion |=
12240 argument_pack_element_is_expansion_p (arg_pack, index);
12241
12242 /* Select the Ith argument from the pack. */
12243 if (TREE_CODE (parm) == PARM_DECL
12244 || VAR_P (parm)
12245 || TREE_CODE (parm) == FIELD_DECL)
12246 {
12247 if (index == 0)
12248 {
12249 aps = make_argument_pack_select (arg_pack, index);
12250 if (!mark_used (parm, complain) && !(complain & tf_error))
12251 return error_mark_node;
12252 register_local_specialization (aps, parm);
12253 }
12254 else
12255 aps = retrieve_local_specialization (parm);
12256 }
12257 else
12258 {
12259 int idx, level;
12260 template_parm_level_and_index (parm, &level, &idx);
12261
12262 if (index == 0)
12263 {
12264 aps = make_argument_pack_select (arg_pack, index);
12265 /* Update the corresponding argument. */
12266 TMPL_ARG (args, level, idx) = aps;
12267 }
12268 else
12269 /* Re-use the ARGUMENT_PACK_SELECT. */
12270 aps = TMPL_ARG (args, level, idx);
12271 }
12272 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12273 }
12274
12275 /* Substitute into the PATTERN with the (possibly altered)
12276 arguments. */
12277 if (pattern == in_decl)
12278 /* Expanding a fixed parameter pack from
12279 coerce_template_parameter_pack. */
12280 t = tsubst_decl (pattern, args, complain);
12281 else if (pattern == error_mark_node)
12282 t = error_mark_node;
12283 else if (!TYPE_P (pattern))
12284 t = tsubst_expr (pattern, args, complain, in_decl,
12285 /*integral_constant_expression_p=*/false);
12286 else
12287 t = tsubst (pattern, args, complain, in_decl);
12288
12289 /* If the Ith argument pack element is a pack expansion, then
12290 the Ith element resulting from the substituting is going to
12291 be a pack expansion as well. */
12292 if (ith_elem_is_expansion)
12293 t = make_pack_expansion (t, complain);
12294
12295 return t;
12296 }
12297
12298 /* When the unexpanded parameter pack in a fold expression expands to an empty
12299 sequence, the value of the expression is as follows; the program is
12300 ill-formed if the operator is not listed in this table.
12301
12302 && true
12303 || false
12304 , void() */
12305
12306 tree
12307 expand_empty_fold (tree t, tsubst_flags_t complain)
12308 {
12309 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12310 if (!FOLD_EXPR_MODIFY_P (t))
12311 switch (code)
12312 {
12313 case TRUTH_ANDIF_EXPR:
12314 return boolean_true_node;
12315 case TRUTH_ORIF_EXPR:
12316 return boolean_false_node;
12317 case COMPOUND_EXPR:
12318 return void_node;
12319 default:
12320 break;
12321 }
12322
12323 if (complain & tf_error)
12324 error_at (location_of (t),
12325 "fold of empty expansion over %O", code);
12326 return error_mark_node;
12327 }
12328
12329 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12330 form an expression that combines the two terms using the
12331 operator of T. */
12332
12333 static tree
12334 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12335 {
12336 tree op = FOLD_EXPR_OP (t);
12337 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12338
12339 // Handle compound assignment operators.
12340 if (FOLD_EXPR_MODIFY_P (t))
12341 return build_x_modify_expr (input_location, left, code, right, complain);
12342
12343 switch (code)
12344 {
12345 case COMPOUND_EXPR:
12346 return build_x_compound_expr (input_location, left, right, complain);
12347 default:
12348 return build_x_binary_op (input_location, code,
12349 left, TREE_CODE (left),
12350 right, TREE_CODE (right),
12351 /*overload=*/NULL,
12352 complain);
12353 }
12354 }
12355
12356 /* Substitute ARGS into the pack of a fold expression T. */
12357
12358 static inline tree
12359 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12360 {
12361 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12362 }
12363
12364 /* Substitute ARGS into the pack of a fold expression T. */
12365
12366 static inline tree
12367 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12368 {
12369 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12370 }
12371
12372 /* Expand a PACK of arguments into a grouped as left fold.
12373 Given a pack containing elements A0, A1, ..., An and an
12374 operator @, this builds the expression:
12375
12376 ((A0 @ A1) @ A2) ... @ An
12377
12378 Note that PACK must not be empty.
12379
12380 The operator is defined by the original fold expression T. */
12381
12382 static tree
12383 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12384 {
12385 tree left = TREE_VEC_ELT (pack, 0);
12386 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12387 {
12388 tree right = TREE_VEC_ELT (pack, i);
12389 left = fold_expression (t, left, right, complain);
12390 }
12391 return left;
12392 }
12393
12394 /* Substitute into a unary left fold expression. */
12395
12396 static tree
12397 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12398 tree in_decl)
12399 {
12400 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12401 if (pack == error_mark_node)
12402 return error_mark_node;
12403 if (PACK_EXPANSION_P (pack))
12404 {
12405 tree r = copy_node (t);
12406 FOLD_EXPR_PACK (r) = pack;
12407 return r;
12408 }
12409 if (TREE_VEC_LENGTH (pack) == 0)
12410 return expand_empty_fold (t, complain);
12411 else
12412 return expand_left_fold (t, pack, complain);
12413 }
12414
12415 /* Substitute into a binary left fold expression.
12416
12417 Do ths by building a single (non-empty) vector of argumnts and
12418 building the expression from those elements. */
12419
12420 static tree
12421 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12422 tree in_decl)
12423 {
12424 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12425 if (pack == error_mark_node)
12426 return error_mark_node;
12427 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12428 if (init == error_mark_node)
12429 return error_mark_node;
12430
12431 if (PACK_EXPANSION_P (pack))
12432 {
12433 tree r = copy_node (t);
12434 FOLD_EXPR_PACK (r) = pack;
12435 FOLD_EXPR_INIT (r) = init;
12436 return r;
12437 }
12438
12439 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12440 TREE_VEC_ELT (vec, 0) = init;
12441 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12442 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12443
12444 return expand_left_fold (t, vec, complain);
12445 }
12446
12447 /* Expand a PACK of arguments into a grouped as right fold.
12448 Given a pack containing elementns A0, A1, ..., and an
12449 operator @, this builds the expression:
12450
12451 A0@ ... (An-2 @ (An-1 @ An))
12452
12453 Note that PACK must not be empty.
12454
12455 The operator is defined by the original fold expression T. */
12456
12457 tree
12458 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12459 {
12460 // Build the expression.
12461 int n = TREE_VEC_LENGTH (pack);
12462 tree right = TREE_VEC_ELT (pack, n - 1);
12463 for (--n; n != 0; --n)
12464 {
12465 tree left = TREE_VEC_ELT (pack, n - 1);
12466 right = fold_expression (t, left, right, complain);
12467 }
12468 return right;
12469 }
12470
12471 /* Substitute into a unary right fold expression. */
12472
12473 static tree
12474 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12475 tree in_decl)
12476 {
12477 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12478 if (pack == error_mark_node)
12479 return error_mark_node;
12480 if (PACK_EXPANSION_P (pack))
12481 {
12482 tree r = copy_node (t);
12483 FOLD_EXPR_PACK (r) = pack;
12484 return r;
12485 }
12486 if (TREE_VEC_LENGTH (pack) == 0)
12487 return expand_empty_fold (t, complain);
12488 else
12489 return expand_right_fold (t, pack, complain);
12490 }
12491
12492 /* Substitute into a binary right fold expression.
12493
12494 Do ths by building a single (non-empty) vector of arguments and
12495 building the expression from those elements. */
12496
12497 static tree
12498 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12499 tree in_decl)
12500 {
12501 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12502 if (pack == error_mark_node)
12503 return error_mark_node;
12504 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12505 if (init == error_mark_node)
12506 return error_mark_node;
12507
12508 if (PACK_EXPANSION_P (pack))
12509 {
12510 tree r = copy_node (t);
12511 FOLD_EXPR_PACK (r) = pack;
12512 FOLD_EXPR_INIT (r) = init;
12513 return r;
12514 }
12515
12516 int n = TREE_VEC_LENGTH (pack);
12517 tree vec = make_tree_vec (n + 1);
12518 for (int i = 0; i < n; ++i)
12519 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12520 TREE_VEC_ELT (vec, n) = init;
12521
12522 return expand_right_fold (t, vec, complain);
12523 }
12524
12525 /* Walk through the pattern of a pack expansion, adding everything in
12526 local_specializations to a list. */
12527
12528 class el_data
12529 {
12530 public:
12531 hash_set<tree> internal;
12532 tree extra;
12533 tsubst_flags_t complain;
12534
12535 el_data (tsubst_flags_t c)
12536 : extra (NULL_TREE), complain (c) {}
12537 };
12538 static tree
12539 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12540 {
12541 el_data &data = *reinterpret_cast<el_data*>(data_);
12542 tree *extra = &data.extra;
12543 tsubst_flags_t complain = data.complain;
12544
12545 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12546 /* Remember local typedefs (85214). */
12547 tp = &TYPE_NAME (*tp);
12548
12549 if (TREE_CODE (*tp) == DECL_EXPR)
12550 data.internal.add (DECL_EXPR_DECL (*tp));
12551 else if (tree spec = retrieve_local_specialization (*tp))
12552 {
12553 if (data.internal.contains (*tp))
12554 /* Don't mess with variables declared within the pattern. */
12555 return NULL_TREE;
12556 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12557 {
12558 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12559 tree args = ARGUMENT_PACK_ARGS (spec);
12560 if (TREE_VEC_LENGTH (args) == 1)
12561 {
12562 tree elt = TREE_VEC_ELT (args, 0);
12563 if (PACK_EXPANSION_P (elt))
12564 elt = PACK_EXPANSION_PATTERN (elt);
12565 if (DECL_PACK_P (elt))
12566 spec = elt;
12567 }
12568 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12569 {
12570 /* Handle lambda capture here, since we aren't doing any
12571 substitution now, and so tsubst_copy won't call
12572 process_outer_var_ref. */
12573 tree args = ARGUMENT_PACK_ARGS (spec);
12574 int len = TREE_VEC_LENGTH (args);
12575 for (int i = 0; i < len; ++i)
12576 {
12577 tree arg = TREE_VEC_ELT (args, i);
12578 tree carg = arg;
12579 if (outer_automatic_var_p (arg))
12580 carg = process_outer_var_ref (arg, complain);
12581 if (carg != arg)
12582 {
12583 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12584 proxies. */
12585 if (i == 0)
12586 {
12587 spec = copy_node (spec);
12588 args = copy_node (args);
12589 SET_ARGUMENT_PACK_ARGS (spec, args);
12590 register_local_specialization (spec, *tp);
12591 }
12592 TREE_VEC_ELT (args, i) = carg;
12593 }
12594 }
12595 }
12596 }
12597 if (outer_automatic_var_p (spec))
12598 spec = process_outer_var_ref (spec, complain);
12599 *extra = tree_cons (*tp, spec, *extra);
12600 }
12601 return NULL_TREE;
12602 }
12603 static tree
12604 extract_local_specs (tree pattern, tsubst_flags_t complain)
12605 {
12606 el_data data (complain);
12607 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12608 return data.extra;
12609 }
12610
12611 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12612 for use in PACK_EXPANSION_EXTRA_ARGS. */
12613
12614 tree
12615 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12616 {
12617 tree extra = args;
12618 if (local_specializations)
12619 if (tree locals = extract_local_specs (pattern, complain))
12620 extra = tree_cons (NULL_TREE, extra, locals);
12621 return extra;
12622 }
12623
12624 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12625 normal template args to ARGS. */
12626
12627 tree
12628 add_extra_args (tree extra, tree args)
12629 {
12630 if (extra && TREE_CODE (extra) == TREE_LIST)
12631 {
12632 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12633 {
12634 /* The partial instantiation involved local declarations collected in
12635 extract_local_specs; map from the general template to our local
12636 context. */
12637 tree gen = TREE_PURPOSE (elt);
12638 tree inst = TREE_VALUE (elt);
12639 if (DECL_P (inst))
12640 if (tree local = retrieve_local_specialization (inst))
12641 inst = local;
12642 /* else inst is already a full instantiation of the pack. */
12643 register_local_specialization (inst, gen);
12644 }
12645 gcc_assert (!TREE_PURPOSE (extra));
12646 extra = TREE_VALUE (extra);
12647 }
12648 #if 1
12649 /* I think we should always be able to substitute dependent args into the
12650 pattern. If that turns out to be incorrect in some cases, enable the
12651 alternate code (and add complain/in_decl parms to this function). */
12652 gcc_checking_assert (!uses_template_parms (extra));
12653 #else
12654 if (!uses_template_parms (extra))
12655 {
12656 gcc_unreachable ();
12657 extra = tsubst_template_args (extra, args, complain, in_decl);
12658 args = add_outermost_template_args (args, extra);
12659 }
12660 else
12661 #endif
12662 args = add_to_template_args (extra, args);
12663 return args;
12664 }
12665
12666 /* Substitute ARGS into T, which is an pack expansion
12667 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12668 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12669 (if only a partial substitution could be performed) or
12670 ERROR_MARK_NODE if there was an error. */
12671 tree
12672 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12673 tree in_decl)
12674 {
12675 tree pattern;
12676 tree pack, packs = NULL_TREE;
12677 bool unsubstituted_packs = false;
12678 bool unsubstituted_fn_pack = false;
12679 int i, len = -1;
12680 tree result;
12681 hash_map<tree, tree> *saved_local_specializations = NULL;
12682 bool need_local_specializations = false;
12683 int levels;
12684
12685 gcc_assert (PACK_EXPANSION_P (t));
12686 pattern = PACK_EXPANSION_PATTERN (t);
12687
12688 /* Add in any args remembered from an earlier partial instantiation. */
12689 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12690
12691 levels = TMPL_ARGS_DEPTH (args);
12692
12693 /* Determine the argument packs that will instantiate the parameter
12694 packs used in the expansion expression. While we're at it,
12695 compute the number of arguments to be expanded and make sure it
12696 is consistent. */
12697 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12698 pack = TREE_CHAIN (pack))
12699 {
12700 tree parm_pack = TREE_VALUE (pack);
12701 tree arg_pack = NULL_TREE;
12702 tree orig_arg = NULL_TREE;
12703 int level = 0;
12704
12705 if (TREE_CODE (parm_pack) == BASES)
12706 {
12707 gcc_assert (parm_pack == pattern);
12708 if (BASES_DIRECT (parm_pack))
12709 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12710 args, complain,
12711 in_decl, false),
12712 complain);
12713 else
12714 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12715 args, complain, in_decl,
12716 false), complain);
12717 }
12718 else if (builtin_pack_call_p (parm_pack))
12719 {
12720 if (parm_pack != pattern)
12721 {
12722 if (complain & tf_error)
12723 sorry ("%qE is not the entire pattern of the pack expansion",
12724 parm_pack);
12725 return error_mark_node;
12726 }
12727 return expand_builtin_pack_call (parm_pack, args,
12728 complain, in_decl);
12729 }
12730 else if (TREE_CODE (parm_pack) == PARM_DECL)
12731 {
12732 /* We know we have correct local_specializations if this
12733 expansion is at function scope, or if we're dealing with a
12734 local parameter in a requires expression; for the latter,
12735 tsubst_requires_expr set it up appropriately. */
12736 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12737 arg_pack = retrieve_local_specialization (parm_pack);
12738 else
12739 /* We can't rely on local_specializations for a parameter
12740 name used later in a function declaration (such as in a
12741 late-specified return type). Even if it exists, it might
12742 have the wrong value for a recursive call. */
12743 need_local_specializations = true;
12744
12745 if (!arg_pack)
12746 {
12747 /* This parameter pack was used in an unevaluated context. Just
12748 make a dummy decl, since it's only used for its type. */
12749 ++cp_unevaluated_operand;
12750 arg_pack = tsubst_decl (parm_pack, args, complain);
12751 --cp_unevaluated_operand;
12752 if (arg_pack && DECL_PACK_P (arg_pack))
12753 /* Partial instantiation of the parm_pack, we can't build
12754 up an argument pack yet. */
12755 arg_pack = NULL_TREE;
12756 else
12757 arg_pack = make_fnparm_pack (arg_pack);
12758 }
12759 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12760 /* This argument pack isn't fully instantiated yet. We set this
12761 flag rather than clear arg_pack because we do want to do the
12762 optimization below, and we don't want to substitute directly
12763 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12764 where it isn't expected). */
12765 unsubstituted_fn_pack = true;
12766 }
12767 else if (is_capture_proxy (parm_pack))
12768 {
12769 arg_pack = retrieve_local_specialization (parm_pack);
12770 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12771 unsubstituted_fn_pack = true;
12772 }
12773 else
12774 {
12775 int idx;
12776 template_parm_level_and_index (parm_pack, &level, &idx);
12777 if (level <= levels)
12778 arg_pack = TMPL_ARG (args, level, idx);
12779 }
12780
12781 orig_arg = arg_pack;
12782 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12783 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12784
12785 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12786 /* This can only happen if we forget to expand an argument
12787 pack somewhere else. Just return an error, silently. */
12788 {
12789 result = make_tree_vec (1);
12790 TREE_VEC_ELT (result, 0) = error_mark_node;
12791 return result;
12792 }
12793
12794 if (arg_pack)
12795 {
12796 int my_len =
12797 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12798
12799 /* Don't bother trying to do a partial substitution with
12800 incomplete packs; we'll try again after deduction. */
12801 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12802 return t;
12803
12804 if (len < 0)
12805 len = my_len;
12806 else if (len != my_len
12807 && !unsubstituted_fn_pack)
12808 {
12809 if (!(complain & tf_error))
12810 /* Fail quietly. */;
12811 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12812 error ("mismatched argument pack lengths while expanding %qT",
12813 pattern);
12814 else
12815 error ("mismatched argument pack lengths while expanding %qE",
12816 pattern);
12817 return error_mark_node;
12818 }
12819
12820 /* Keep track of the parameter packs and their corresponding
12821 argument packs. */
12822 packs = tree_cons (parm_pack, arg_pack, packs);
12823 TREE_TYPE (packs) = orig_arg;
12824 }
12825 else
12826 {
12827 /* We can't substitute for this parameter pack. We use a flag as
12828 well as the missing_level counter because function parameter
12829 packs don't have a level. */
12830 if (!(processing_template_decl || is_auto (parm_pack)))
12831 {
12832 gcc_unreachable ();
12833 }
12834 gcc_assert (processing_template_decl || is_auto (parm_pack));
12835 unsubstituted_packs = true;
12836 }
12837 }
12838
12839 /* If the expansion is just T..., return the matching argument pack, unless
12840 we need to call convert_from_reference on all the elements. This is an
12841 important optimization; see c++/68422. */
12842 if (!unsubstituted_packs
12843 && TREE_PURPOSE (packs) == pattern)
12844 {
12845 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12846
12847 /* If the argument pack is a single pack expansion, pull it out. */
12848 if (TREE_VEC_LENGTH (args) == 1
12849 && pack_expansion_args_count (args))
12850 return TREE_VEC_ELT (args, 0);
12851
12852 /* Types need no adjustment, nor does sizeof..., and if we still have
12853 some pack expansion args we won't do anything yet. */
12854 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12855 || PACK_EXPANSION_SIZEOF_P (t)
12856 || pack_expansion_args_count (args))
12857 return args;
12858 /* Also optimize expression pack expansions if we can tell that the
12859 elements won't have reference type. */
12860 tree type = TREE_TYPE (pattern);
12861 if (type && !TYPE_REF_P (type)
12862 && !PACK_EXPANSION_P (type)
12863 && !WILDCARD_TYPE_P (type))
12864 return args;
12865 /* Otherwise use the normal path so we get convert_from_reference. */
12866 }
12867
12868 /* We cannot expand this expansion expression, because we don't have
12869 all of the argument packs we need. */
12870 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12871 {
12872 /* We got some full packs, but we can't substitute them in until we
12873 have values for all the packs. So remember these until then. */
12874
12875 t = make_pack_expansion (pattern, complain);
12876 PACK_EXPANSION_EXTRA_ARGS (t)
12877 = build_extra_args (pattern, args, complain);
12878 return t;
12879 }
12880 else if (unsubstituted_packs)
12881 {
12882 /* There were no real arguments, we're just replacing a parameter
12883 pack with another version of itself. Substitute into the
12884 pattern and return a PACK_EXPANSION_*. The caller will need to
12885 deal with that. */
12886 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12887 t = tsubst_expr (pattern, args, complain, in_decl,
12888 /*integral_constant_expression_p=*/false);
12889 else
12890 t = tsubst (pattern, args, complain, in_decl);
12891 t = make_pack_expansion (t, complain);
12892 return t;
12893 }
12894
12895 gcc_assert (len >= 0);
12896
12897 if (need_local_specializations)
12898 {
12899 /* We're in a late-specified return type, so create our own local
12900 specializations map; the current map is either NULL or (in the
12901 case of recursive unification) might have bindings that we don't
12902 want to use or alter. */
12903 saved_local_specializations = local_specializations;
12904 local_specializations = new hash_map<tree, tree>;
12905 }
12906
12907 /* For each argument in each argument pack, substitute into the
12908 pattern. */
12909 result = make_tree_vec (len);
12910 tree elem_args = copy_template_args (args);
12911 for (i = 0; i < len; ++i)
12912 {
12913 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12914 i,
12915 elem_args, complain,
12916 in_decl);
12917 TREE_VEC_ELT (result, i) = t;
12918 if (t == error_mark_node)
12919 {
12920 result = error_mark_node;
12921 break;
12922 }
12923 }
12924
12925 /* Update ARGS to restore the substitution from parameter packs to
12926 their argument packs. */
12927 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12928 {
12929 tree parm = TREE_PURPOSE (pack);
12930
12931 if (TREE_CODE (parm) == PARM_DECL
12932 || VAR_P (parm)
12933 || TREE_CODE (parm) == FIELD_DECL)
12934 register_local_specialization (TREE_TYPE (pack), parm);
12935 else
12936 {
12937 int idx, level;
12938
12939 if (TREE_VALUE (pack) == NULL_TREE)
12940 continue;
12941
12942 template_parm_level_and_index (parm, &level, &idx);
12943
12944 /* Update the corresponding argument. */
12945 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12946 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12947 TREE_TYPE (pack);
12948 else
12949 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12950 }
12951 }
12952
12953 if (need_local_specializations)
12954 {
12955 delete local_specializations;
12956 local_specializations = saved_local_specializations;
12957 }
12958
12959 /* If the dependent pack arguments were such that we end up with only a
12960 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12961 if (len == 1 && TREE_CODE (result) == TREE_VEC
12962 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12963 return TREE_VEC_ELT (result, 0);
12964
12965 return result;
12966 }
12967
12968 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12969 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12970 parameter packs; all parms generated from a function parameter pack will
12971 have the same DECL_PARM_INDEX. */
12972
12973 tree
12974 get_pattern_parm (tree parm, tree tmpl)
12975 {
12976 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12977 tree patparm;
12978
12979 if (DECL_ARTIFICIAL (parm))
12980 {
12981 for (patparm = DECL_ARGUMENTS (pattern);
12982 patparm; patparm = DECL_CHAIN (patparm))
12983 if (DECL_ARTIFICIAL (patparm)
12984 && DECL_NAME (parm) == DECL_NAME (patparm))
12985 break;
12986 }
12987 else
12988 {
12989 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12990 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12991 gcc_assert (DECL_PARM_INDEX (patparm)
12992 == DECL_PARM_INDEX (parm));
12993 }
12994
12995 return patparm;
12996 }
12997
12998 /* Make an argument pack out of the TREE_VEC VEC. */
12999
13000 static tree
13001 make_argument_pack (tree vec)
13002 {
13003 tree pack;
13004 tree elt = TREE_VEC_ELT (vec, 0);
13005 if (TYPE_P (elt))
13006 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13007 else
13008 {
13009 pack = make_node (NONTYPE_ARGUMENT_PACK);
13010 TREE_CONSTANT (pack) = 1;
13011 }
13012 SET_ARGUMENT_PACK_ARGS (pack, vec);
13013 return pack;
13014 }
13015
13016 /* Return an exact copy of template args T that can be modified
13017 independently. */
13018
13019 static tree
13020 copy_template_args (tree t)
13021 {
13022 if (t == error_mark_node)
13023 return t;
13024
13025 int len = TREE_VEC_LENGTH (t);
13026 tree new_vec = make_tree_vec (len);
13027
13028 for (int i = 0; i < len; ++i)
13029 {
13030 tree elt = TREE_VEC_ELT (t, i);
13031 if (elt && TREE_CODE (elt) == TREE_VEC)
13032 elt = copy_template_args (elt);
13033 TREE_VEC_ELT (new_vec, i) = elt;
13034 }
13035
13036 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13037 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13038
13039 return new_vec;
13040 }
13041
13042 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13043
13044 tree
13045 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13046 tree in_decl)
13047 {
13048 /* Substitute into each of the arguments. */
13049 tree new_arg = TYPE_P (orig_arg)
13050 ? cxx_make_type (TREE_CODE (orig_arg))
13051 : make_node (TREE_CODE (orig_arg));
13052
13053 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13054 args, complain, in_decl);
13055 if (pack_args == error_mark_node)
13056 new_arg = error_mark_node;
13057 else
13058 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13059
13060 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13061 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13062
13063 return new_arg;
13064 }
13065
13066 /* Substitute ARGS into the vector or list of template arguments T. */
13067
13068 tree
13069 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13070 {
13071 tree orig_t = t;
13072 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13073 tree *elts;
13074
13075 if (t == error_mark_node)
13076 return error_mark_node;
13077
13078 len = TREE_VEC_LENGTH (t);
13079 elts = XALLOCAVEC (tree, len);
13080
13081 for (i = 0; i < len; i++)
13082 {
13083 tree orig_arg = TREE_VEC_ELT (t, i);
13084 tree new_arg;
13085
13086 if (TREE_CODE (orig_arg) == TREE_VEC)
13087 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13088 else if (PACK_EXPANSION_P (orig_arg))
13089 {
13090 /* Substitute into an expansion expression. */
13091 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13092
13093 if (TREE_CODE (new_arg) == TREE_VEC)
13094 /* Add to the expanded length adjustment the number of
13095 expanded arguments. We subtract one from this
13096 measurement, because the argument pack expression
13097 itself is already counted as 1 in
13098 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13099 the argument pack is empty. */
13100 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13101 }
13102 else if (ARGUMENT_PACK_P (orig_arg))
13103 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13104 else
13105 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13106
13107 if (new_arg == error_mark_node)
13108 return error_mark_node;
13109
13110 elts[i] = new_arg;
13111 if (new_arg != orig_arg)
13112 need_new = 1;
13113 }
13114
13115 if (!need_new)
13116 return t;
13117
13118 /* Make space for the expanded arguments coming from template
13119 argument packs. */
13120 t = make_tree_vec (len + expanded_len_adjust);
13121 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13122 arguments for a member template.
13123 In that case each TREE_VEC in ORIG_T represents a level of template
13124 arguments, and ORIG_T won't carry any non defaulted argument count.
13125 It will rather be the nested TREE_VECs that will carry one.
13126 In other words, ORIG_T carries a non defaulted argument count only
13127 if it doesn't contain any nested TREE_VEC. */
13128 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13129 {
13130 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13131 count += expanded_len_adjust;
13132 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13133 }
13134 for (i = 0, out = 0; i < len; i++)
13135 {
13136 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13137 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13138 && TREE_CODE (elts[i]) == TREE_VEC)
13139 {
13140 int idx;
13141
13142 /* Now expand the template argument pack "in place". */
13143 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13144 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13145 }
13146 else
13147 {
13148 TREE_VEC_ELT (t, out) = elts[i];
13149 out++;
13150 }
13151 }
13152
13153 return t;
13154 }
13155
13156 /* Substitute ARGS into one level PARMS of template parameters. */
13157
13158 static tree
13159 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13160 {
13161 if (parms == error_mark_node)
13162 return error_mark_node;
13163
13164 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13165
13166 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13167 {
13168 tree tuple = TREE_VEC_ELT (parms, i);
13169
13170 if (tuple == error_mark_node)
13171 continue;
13172
13173 TREE_VEC_ELT (new_vec, i) =
13174 tsubst_template_parm (tuple, args, complain);
13175 }
13176
13177 return new_vec;
13178 }
13179
13180 /* Return the result of substituting ARGS into the template parameters
13181 given by PARMS. If there are m levels of ARGS and m + n levels of
13182 PARMS, then the result will contain n levels of PARMS. For
13183 example, if PARMS is `template <class T> template <class U>
13184 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13185 result will be `template <int*, double, class V>'. */
13186
13187 static tree
13188 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13189 {
13190 tree r = NULL_TREE;
13191 tree* new_parms;
13192
13193 /* When substituting into a template, we must set
13194 PROCESSING_TEMPLATE_DECL as the template parameters may be
13195 dependent if they are based on one-another, and the dependency
13196 predicates are short-circuit outside of templates. */
13197 ++processing_template_decl;
13198
13199 for (new_parms = &r;
13200 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13201 new_parms = &(TREE_CHAIN (*new_parms)),
13202 parms = TREE_CHAIN (parms))
13203 {
13204 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13205 args, complain);
13206 *new_parms =
13207 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13208 - TMPL_ARGS_DEPTH (args)),
13209 new_vec, NULL_TREE);
13210 }
13211
13212 --processing_template_decl;
13213
13214 return r;
13215 }
13216
13217 /* Return the result of substituting ARGS into one template parameter
13218 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13219 parameter and which TREE_PURPOSE is the default argument of the
13220 template parameter. */
13221
13222 static tree
13223 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13224 {
13225 tree default_value, parm_decl;
13226
13227 if (args == NULL_TREE
13228 || t == NULL_TREE
13229 || t == error_mark_node)
13230 return t;
13231
13232 gcc_assert (TREE_CODE (t) == TREE_LIST);
13233
13234 default_value = TREE_PURPOSE (t);
13235 parm_decl = TREE_VALUE (t);
13236 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13237
13238 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13239 if (TREE_CODE (parm_decl) == PARM_DECL
13240 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13241 parm_decl = error_mark_node;
13242 default_value = tsubst_template_arg (default_value, args,
13243 complain, NULL_TREE);
13244 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13245
13246 tree r = build_tree_list (default_value, parm_decl);
13247 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13248 return r;
13249 }
13250
13251 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13252 type T. If T is not an aggregate or enumeration type, it is
13253 handled as if by tsubst. IN_DECL is as for tsubst. If
13254 ENTERING_SCOPE is nonzero, T is the context for a template which
13255 we are presently tsubst'ing. Return the substituted value. */
13256
13257 static tree
13258 tsubst_aggr_type (tree t,
13259 tree args,
13260 tsubst_flags_t complain,
13261 tree in_decl,
13262 int entering_scope)
13263 {
13264 if (t == NULL_TREE)
13265 return NULL_TREE;
13266
13267 switch (TREE_CODE (t))
13268 {
13269 case RECORD_TYPE:
13270 if (TYPE_PTRMEMFUNC_P (t))
13271 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13272
13273 /* Fall through. */
13274 case ENUMERAL_TYPE:
13275 case UNION_TYPE:
13276 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13277 {
13278 tree argvec;
13279 tree context;
13280 tree r;
13281
13282 /* In "sizeof(X<I>)" we need to evaluate "I". */
13283 cp_evaluated ev;
13284
13285 /* First, determine the context for the type we are looking
13286 up. */
13287 context = TYPE_CONTEXT (t);
13288 if (context && TYPE_P (context))
13289 {
13290 context = tsubst_aggr_type (context, args, complain,
13291 in_decl, /*entering_scope=*/1);
13292 /* If context is a nested class inside a class template,
13293 it may still need to be instantiated (c++/33959). */
13294 context = complete_type (context);
13295 }
13296
13297 /* Then, figure out what arguments are appropriate for the
13298 type we are trying to find. For example, given:
13299
13300 template <class T> struct S;
13301 template <class T, class U> void f(T, U) { S<U> su; }
13302
13303 and supposing that we are instantiating f<int, double>,
13304 then our ARGS will be {int, double}, but, when looking up
13305 S we only want {double}. */
13306 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13307 complain, in_decl);
13308 if (argvec == error_mark_node)
13309 r = error_mark_node;
13310 else
13311 {
13312 r = lookup_template_class (t, argvec, in_decl, context,
13313 entering_scope, complain);
13314 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13315 }
13316
13317 return r;
13318 }
13319 else
13320 /* This is not a template type, so there's nothing to do. */
13321 return t;
13322
13323 default:
13324 return tsubst (t, args, complain, in_decl);
13325 }
13326 }
13327
13328 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13329
13330 /* Substitute into the default argument ARG (a default argument for
13331 FN), which has the indicated TYPE. */
13332
13333 tree
13334 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13335 tsubst_flags_t complain)
13336 {
13337 int errs = errorcount + sorrycount;
13338
13339 /* This can happen in invalid code. */
13340 if (TREE_CODE (arg) == DEFERRED_PARSE)
13341 return arg;
13342
13343 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13344 parm = chain_index (parmnum, parm);
13345 tree parmtype = TREE_TYPE (parm);
13346 if (DECL_BY_REFERENCE (parm))
13347 parmtype = TREE_TYPE (parmtype);
13348 if (parmtype == error_mark_node)
13349 return error_mark_node;
13350
13351 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13352
13353 tree *slot;
13354 if (defarg_inst && (slot = defarg_inst->get (parm)))
13355 return *slot;
13356
13357 /* This default argument came from a template. Instantiate the
13358 default argument here, not in tsubst. In the case of
13359 something like:
13360
13361 template <class T>
13362 struct S {
13363 static T t();
13364 void f(T = t());
13365 };
13366
13367 we must be careful to do name lookup in the scope of S<T>,
13368 rather than in the current class. */
13369 push_to_top_level ();
13370 push_access_scope (fn);
13371 push_deferring_access_checks (dk_no_deferred);
13372 start_lambda_scope (parm);
13373
13374 /* The default argument expression may cause implicitly defined
13375 member functions to be synthesized, which will result in garbage
13376 collection. We must treat this situation as if we were within
13377 the body of function so as to avoid collecting live data on the
13378 stack. */
13379 ++function_depth;
13380 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13381 complain, NULL_TREE,
13382 /*integral_constant_expression_p=*/false);
13383 --function_depth;
13384
13385 finish_lambda_scope ();
13386
13387 /* Make sure the default argument is reasonable. */
13388 arg = check_default_argument (type, arg, complain);
13389
13390 if (errorcount+sorrycount > errs
13391 && (complain & tf_warning_or_error))
13392 inform (input_location,
13393 " when instantiating default argument for call to %qD", fn);
13394
13395 pop_deferring_access_checks ();
13396 pop_access_scope (fn);
13397 pop_from_top_level ();
13398
13399 if (arg != error_mark_node && !cp_unevaluated_operand)
13400 {
13401 if (!defarg_inst)
13402 defarg_inst = decl_tree_cache_map::create_ggc (37);
13403 defarg_inst->put (parm, arg);
13404 }
13405
13406 return arg;
13407 }
13408
13409 /* Substitute into all the default arguments for FN. */
13410
13411 static void
13412 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13413 {
13414 tree arg;
13415 tree tmpl_args;
13416
13417 tmpl_args = DECL_TI_ARGS (fn);
13418
13419 /* If this function is not yet instantiated, we certainly don't need
13420 its default arguments. */
13421 if (uses_template_parms (tmpl_args))
13422 return;
13423 /* Don't do this again for clones. */
13424 if (DECL_CLONED_FUNCTION_P (fn))
13425 return;
13426
13427 int i = 0;
13428 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13429 arg;
13430 arg = TREE_CHAIN (arg), ++i)
13431 if (TREE_PURPOSE (arg))
13432 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13433 TREE_VALUE (arg),
13434 TREE_PURPOSE (arg),
13435 complain);
13436 }
13437
13438 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13439 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13440
13441 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13442
13443 void
13444 store_explicit_specifier (tree v, tree t)
13445 {
13446 if (!explicit_specifier_map)
13447 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13448 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13449 explicit_specifier_map->put (v, t);
13450 }
13451
13452 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13453
13454 static tree
13455 lookup_explicit_specifier (tree v)
13456 {
13457 return *explicit_specifier_map->get (v);
13458 }
13459
13460 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13461
13462 static tree
13463 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13464 tree lambda_fntype)
13465 {
13466 tree gen_tmpl, argvec;
13467 hashval_t hash = 0;
13468 tree in_decl = t;
13469
13470 /* Nobody should be tsubst'ing into non-template functions. */
13471 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
13472
13473 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13474 {
13475 /* If T is not dependent, just return it. */
13476 if (!uses_template_parms (DECL_TI_ARGS (t))
13477 && !LAMBDA_FUNCTION_P (t))
13478 return t;
13479
13480 /* Calculate the most general template of which R is a
13481 specialization. */
13482 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13483
13484 /* We're substituting a lambda function under tsubst_lambda_expr but not
13485 directly from it; find the matching function we're already inside.
13486 But don't do this if T is a generic lambda with a single level of
13487 template parms, as in that case we're doing a normal instantiation. */
13488 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13489 && (!generic_lambda_fn_p (t)
13490 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13491 return enclosing_instantiation_of (t);
13492
13493 /* Calculate the complete set of arguments used to
13494 specialize R. */
13495 argvec = tsubst_template_args (DECL_TI_ARGS
13496 (DECL_TEMPLATE_RESULT
13497 (DECL_TI_TEMPLATE (t))),
13498 args, complain, in_decl);
13499 if (argvec == error_mark_node)
13500 return error_mark_node;
13501
13502 /* Check to see if we already have this specialization. */
13503 if (!lambda_fntype)
13504 {
13505 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13506 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13507 return spec;
13508 }
13509
13510 /* We can see more levels of arguments than parameters if
13511 there was a specialization of a member template, like
13512 this:
13513
13514 template <class T> struct S { template <class U> void f(); }
13515 template <> template <class U> void S<int>::f(U);
13516
13517 Here, we'll be substituting into the specialization,
13518 because that's where we can find the code we actually
13519 want to generate, but we'll have enough arguments for
13520 the most general template.
13521
13522 We also deal with the peculiar case:
13523
13524 template <class T> struct S {
13525 template <class U> friend void f();
13526 };
13527 template <class U> void f() {}
13528 template S<int>;
13529 template void f<double>();
13530
13531 Here, the ARGS for the instantiation of will be {int,
13532 double}. But, we only need as many ARGS as there are
13533 levels of template parameters in CODE_PATTERN. We are
13534 careful not to get fooled into reducing the ARGS in
13535 situations like:
13536
13537 template <class T> struct S { template <class U> void f(U); }
13538 template <class T> template <> void S<T>::f(int) {}
13539
13540 which we can spot because the pattern will be a
13541 specialization in this case. */
13542 int args_depth = TMPL_ARGS_DEPTH (args);
13543 int parms_depth =
13544 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13545
13546 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13547 args = get_innermost_template_args (args, parms_depth);
13548 }
13549 else
13550 {
13551 /* This special case arises when we have something like this:
13552
13553 template <class T> struct S {
13554 friend void f<int>(int, double);
13555 };
13556
13557 Here, the DECL_TI_TEMPLATE for the friend declaration
13558 will be an IDENTIFIER_NODE. We are being called from
13559 tsubst_friend_function, and we want only to create a
13560 new decl (R) with appropriate types so that we can call
13561 determine_specialization. */
13562 gen_tmpl = NULL_TREE;
13563 argvec = NULL_TREE;
13564 }
13565
13566 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13567 : NULL_TREE);
13568 tree ctx = closure ? closure : DECL_CONTEXT (t);
13569 bool member = ctx && TYPE_P (ctx);
13570
13571 if (member && !closure)
13572 ctx = tsubst_aggr_type (ctx, args,
13573 complain, t, /*entering_scope=*/1);
13574
13575 tree type = (lambda_fntype ? lambda_fntype
13576 : tsubst (TREE_TYPE (t), args,
13577 complain | tf_fndecl_type, in_decl));
13578 if (type == error_mark_node)
13579 return error_mark_node;
13580
13581 /* If we hit excessive deduction depth, the type is bogus even if
13582 it isn't error_mark_node, so don't build a decl. */
13583 if (excessive_deduction_depth)
13584 return error_mark_node;
13585
13586 /* We do NOT check for matching decls pushed separately at this
13587 point, as they may not represent instantiations of this
13588 template, and in any case are considered separate under the
13589 discrete model. */
13590 tree r = copy_decl (t);
13591 DECL_USE_TEMPLATE (r) = 0;
13592 TREE_TYPE (r) = type;
13593 /* Clear out the mangled name and RTL for the instantiation. */
13594 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13595 SET_DECL_RTL (r, NULL);
13596 /* Leave DECL_INITIAL set on deleted instantiations. */
13597 if (!DECL_DELETED_FN (r))
13598 DECL_INITIAL (r) = NULL_TREE;
13599 DECL_CONTEXT (r) = ctx;
13600
13601 /* Handle explicit(dependent-expr). */
13602 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13603 {
13604 tree spec = lookup_explicit_specifier (t);
13605 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13606 /*function_p=*/false,
13607 /*i_c_e_p=*/true);
13608 spec = build_explicit_specifier (spec, complain);
13609 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13610 }
13611
13612 /* OpenMP UDRs have the only argument a reference to the declared
13613 type. We want to diagnose if the declared type is a reference,
13614 which is invalid, but as references to references are usually
13615 quietly merged, diagnose it here. */
13616 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13617 {
13618 tree argtype
13619 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13620 argtype = tsubst (argtype, args, complain, in_decl);
13621 if (TYPE_REF_P (argtype))
13622 error_at (DECL_SOURCE_LOCATION (t),
13623 "reference type %qT in "
13624 "%<#pragma omp declare reduction%>", argtype);
13625 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13626 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13627 argtype);
13628 }
13629
13630 if (member && DECL_CONV_FN_P (r))
13631 /* Type-conversion operator. Reconstruct the name, in
13632 case it's the name of one of the template's parameters. */
13633 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13634
13635 tree parms = DECL_ARGUMENTS (t);
13636 if (closure)
13637 parms = DECL_CHAIN (parms);
13638 parms = tsubst (parms, args, complain, t);
13639 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13640 DECL_CONTEXT (parm) = r;
13641 if (closure)
13642 {
13643 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13644 DECL_CHAIN (tparm) = parms;
13645 parms = tparm;
13646 }
13647 DECL_ARGUMENTS (r) = parms;
13648 DECL_RESULT (r) = NULL_TREE;
13649
13650 TREE_STATIC (r) = 0;
13651 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13652 DECL_EXTERNAL (r) = 1;
13653 /* If this is an instantiation of a function with internal
13654 linkage, we already know what object file linkage will be
13655 assigned to the instantiation. */
13656 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13657 DECL_DEFER_OUTPUT (r) = 0;
13658 DECL_CHAIN (r) = NULL_TREE;
13659 DECL_PENDING_INLINE_INFO (r) = 0;
13660 DECL_PENDING_INLINE_P (r) = 0;
13661 DECL_SAVED_TREE (r) = NULL_TREE;
13662 DECL_STRUCT_FUNCTION (r) = NULL;
13663 TREE_USED (r) = 0;
13664 /* We'll re-clone as appropriate in instantiate_template. */
13665 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13666
13667 /* If we aren't complaining now, return on error before we register
13668 the specialization so that we'll complain eventually. */
13669 if ((complain & tf_error) == 0
13670 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13671 && !grok_op_properties (r, /*complain=*/false))
13672 return error_mark_node;
13673
13674 /* Associate the constraints directly with the instantiation. We
13675 don't substitute through the constraints; that's only done when
13676 they are checked. */
13677 if (tree ci = get_constraints (t))
13678 set_constraints (r, ci);
13679
13680 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13681 SET_DECL_FRIEND_CONTEXT (r,
13682 tsubst (DECL_FRIEND_CONTEXT (t),
13683 args, complain, in_decl));
13684
13685 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13686 this in the special friend case mentioned above where
13687 GEN_TMPL is NULL. */
13688 if (gen_tmpl && !closure)
13689 {
13690 DECL_TEMPLATE_INFO (r)
13691 = build_template_info (gen_tmpl, argvec);
13692 SET_DECL_IMPLICIT_INSTANTIATION (r);
13693
13694 tree new_r
13695 = register_specialization (r, gen_tmpl, argvec, false, hash);
13696 if (new_r != r)
13697 /* We instantiated this while substituting into
13698 the type earlier (template/friend54.C). */
13699 return new_r;
13700
13701 /* We're not supposed to instantiate default arguments
13702 until they are called, for a template. But, for a
13703 declaration like:
13704
13705 template <class T> void f ()
13706 { extern void g(int i = T()); }
13707
13708 we should do the substitution when the template is
13709 instantiated. We handle the member function case in
13710 instantiate_class_template since the default arguments
13711 might refer to other members of the class. */
13712 if (!member
13713 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13714 && !uses_template_parms (argvec))
13715 tsubst_default_arguments (r, complain);
13716 }
13717 else
13718 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13719
13720 /* Copy the list of befriending classes. */
13721 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13722 *friends;
13723 friends = &TREE_CHAIN (*friends))
13724 {
13725 *friends = copy_node (*friends);
13726 TREE_VALUE (*friends)
13727 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13728 }
13729
13730 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13731 {
13732 maybe_retrofit_in_chrg (r);
13733 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13734 return error_mark_node;
13735 /* If this is an instantiation of a member template, clone it.
13736 If it isn't, that'll be handled by
13737 clone_constructors_and_destructors. */
13738 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13739 clone_function_decl (r, /*update_methods=*/false);
13740 }
13741 else if ((complain & tf_error) != 0
13742 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13743 && !grok_op_properties (r, /*complain=*/true))
13744 return error_mark_node;
13745
13746 /* Possibly limit visibility based on template args. */
13747 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13748 if (DECL_VISIBILITY_SPECIFIED (t))
13749 {
13750 DECL_VISIBILITY_SPECIFIED (r) = 0;
13751 DECL_ATTRIBUTES (r)
13752 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13753 }
13754 determine_visibility (r);
13755 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13756 && !processing_template_decl)
13757 defaulted_late_check (r);
13758
13759 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13760 args, complain, in_decl);
13761 if (flag_openmp)
13762 if (tree attr = lookup_attribute ("omp declare variant base",
13763 DECL_ATTRIBUTES (r)))
13764 omp_declare_variant_finalize (r, attr);
13765
13766 return r;
13767 }
13768
13769 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13770
13771 static tree
13772 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13773 tree lambda_fntype)
13774 {
13775 /* We can get here when processing a member function template,
13776 member class template, or template template parameter. */
13777 tree decl = DECL_TEMPLATE_RESULT (t);
13778 tree in_decl = t;
13779 tree spec;
13780 tree tmpl_args;
13781 tree full_args;
13782 tree r;
13783 hashval_t hash = 0;
13784
13785 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13786 {
13787 /* Template template parameter is treated here. */
13788 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13789 if (new_type == error_mark_node)
13790 r = error_mark_node;
13791 /* If we get a real template back, return it. This can happen in
13792 the context of most_specialized_partial_spec. */
13793 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13794 r = new_type;
13795 else
13796 /* The new TEMPLATE_DECL was built in
13797 reduce_template_parm_level. */
13798 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13799 return r;
13800 }
13801
13802 if (!lambda_fntype)
13803 {
13804 /* We might already have an instance of this template.
13805 The ARGS are for the surrounding class type, so the
13806 full args contain the tsubst'd args for the context,
13807 plus the innermost args from the template decl. */
13808 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13809 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13810 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13811 /* Because this is a template, the arguments will still be
13812 dependent, even after substitution. If
13813 PROCESSING_TEMPLATE_DECL is not set, the dependency
13814 predicates will short-circuit. */
13815 ++processing_template_decl;
13816 full_args = tsubst_template_args (tmpl_args, args,
13817 complain, in_decl);
13818 --processing_template_decl;
13819 if (full_args == error_mark_node)
13820 return error_mark_node;
13821
13822 /* If this is a default template template argument,
13823 tsubst might not have changed anything. */
13824 if (full_args == tmpl_args)
13825 return t;
13826
13827 hash = hash_tmpl_and_args (t, full_args);
13828 spec = retrieve_specialization (t, full_args, hash);
13829 if (spec != NULL_TREE)
13830 {
13831 if (TYPE_P (spec))
13832 /* Type partial instantiations are stored as the type by
13833 lookup_template_class_1, not here as the template. */
13834 spec = CLASSTYPE_TI_TEMPLATE (spec);
13835 return spec;
13836 }
13837 }
13838
13839 /* Make a new template decl. It will be similar to the
13840 original, but will record the current template arguments.
13841 We also create a new function declaration, which is just
13842 like the old one, but points to this new template, rather
13843 than the old one. */
13844 r = copy_decl (t);
13845 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13846 DECL_CHAIN (r) = NULL_TREE;
13847
13848 // Build new template info linking to the original template decl.
13849 if (!lambda_fntype)
13850 {
13851 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13852 SET_DECL_IMPLICIT_INSTANTIATION (r);
13853 }
13854 else
13855 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13856
13857 /* The template parameters for this new template are all the
13858 template parameters for the old template, except the
13859 outermost level of parameters. */
13860 DECL_TEMPLATE_PARMS (r)
13861 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13862 complain);
13863
13864 if (TREE_CODE (decl) == TYPE_DECL
13865 && !TYPE_DECL_ALIAS_P (decl))
13866 {
13867 tree new_type;
13868 ++processing_template_decl;
13869 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13870 --processing_template_decl;
13871 if (new_type == error_mark_node)
13872 return error_mark_node;
13873
13874 TREE_TYPE (r) = new_type;
13875 /* For a partial specialization, we need to keep pointing to
13876 the primary template. */
13877 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13878 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13879 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13880 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13881 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13882 }
13883 else
13884 {
13885 tree new_decl;
13886 ++processing_template_decl;
13887 if (TREE_CODE (decl) == FUNCTION_DECL)
13888 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13889 else
13890 new_decl = tsubst (decl, args, complain, in_decl);
13891 --processing_template_decl;
13892 if (new_decl == error_mark_node)
13893 return error_mark_node;
13894
13895 DECL_TEMPLATE_RESULT (r) = new_decl;
13896 TREE_TYPE (r) = TREE_TYPE (new_decl);
13897 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13898 if (lambda_fntype)
13899 {
13900 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13901 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13902 }
13903 else
13904 {
13905 DECL_TI_TEMPLATE (new_decl) = r;
13906 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13907 }
13908 }
13909
13910 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13911 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13912
13913 if (PRIMARY_TEMPLATE_P (t))
13914 DECL_PRIMARY_TEMPLATE (r) = r;
13915
13916 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13917 && !lambda_fntype)
13918 /* Record this non-type partial instantiation. */
13919 register_specialization (r, t,
13920 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13921 false, hash);
13922
13923 return r;
13924 }
13925
13926 /* True if FN is the op() for a lambda in an uninstantiated template. */
13927
13928 bool
13929 lambda_fn_in_template_p (tree fn)
13930 {
13931 if (!fn || !LAMBDA_FUNCTION_P (fn))
13932 return false;
13933 tree closure = DECL_CONTEXT (fn);
13934 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13935 }
13936
13937 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13938 which the above is true. */
13939
13940 bool
13941 instantiated_lambda_fn_p (tree fn)
13942 {
13943 if (!fn || !LAMBDA_FUNCTION_P (fn))
13944 return false;
13945 tree closure = DECL_CONTEXT (fn);
13946 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13947 return LAMBDA_EXPR_INSTANTIATED (lam);
13948 }
13949
13950 /* We're instantiating a variable from template function TCTX. Return the
13951 corresponding current enclosing scope. This gets complicated because lambda
13952 functions in templates are regenerated rather than instantiated, but generic
13953 lambda functions are subsequently instantiated. */
13954
13955 static tree
13956 enclosing_instantiation_of (tree otctx)
13957 {
13958 tree tctx = otctx;
13959 tree fn = current_function_decl;
13960 int lambda_count = 0;
13961
13962 for (; tctx && (lambda_fn_in_template_p (tctx)
13963 || instantiated_lambda_fn_p (tctx));
13964 tctx = decl_function_context (tctx))
13965 ++lambda_count;
13966 for (; fn; fn = decl_function_context (fn))
13967 {
13968 tree ofn = fn;
13969 int flambda_count = 0;
13970 for (; fn && instantiated_lambda_fn_p (fn);
13971 fn = decl_function_context (fn))
13972 ++flambda_count;
13973 if ((fn && DECL_TEMPLATE_INFO (fn))
13974 ? most_general_template (fn) != most_general_template (tctx)
13975 : fn != tctx)
13976 continue;
13977 if (flambda_count != lambda_count)
13978 {
13979 gcc_assert (flambda_count > lambda_count);
13980 for (; flambda_count > lambda_count; --flambda_count)
13981 ofn = decl_function_context (ofn);
13982 }
13983 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13984 || DECL_CONV_FN_P (ofn));
13985 return ofn;
13986 }
13987 gcc_unreachable ();
13988 }
13989
13990 /* Substitute the ARGS into the T, which is a _DECL. Return the
13991 result of the substitution. Issue error and warning messages under
13992 control of COMPLAIN. */
13993
13994 static tree
13995 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13996 {
13997 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13998 location_t saved_loc;
13999 tree r = NULL_TREE;
14000 tree in_decl = t;
14001 hashval_t hash = 0;
14002
14003 /* Set the filename and linenumber to improve error-reporting. */
14004 saved_loc = input_location;
14005 input_location = DECL_SOURCE_LOCATION (t);
14006
14007 switch (TREE_CODE (t))
14008 {
14009 case TEMPLATE_DECL:
14010 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14011 break;
14012
14013 case FUNCTION_DECL:
14014 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14015 break;
14016
14017 case PARM_DECL:
14018 {
14019 tree type = NULL_TREE;
14020 int i, len = 1;
14021 tree expanded_types = NULL_TREE;
14022 tree prev_r = NULL_TREE;
14023 tree first_r = NULL_TREE;
14024
14025 if (DECL_PACK_P (t))
14026 {
14027 /* If there is a local specialization that isn't a
14028 parameter pack, it means that we're doing a "simple"
14029 substitution from inside tsubst_pack_expansion. Just
14030 return the local specialization (which will be a single
14031 parm). */
14032 tree spec = retrieve_local_specialization (t);
14033 if (spec
14034 && TREE_CODE (spec) == PARM_DECL
14035 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14036 RETURN (spec);
14037
14038 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14039 the parameters in this function parameter pack. */
14040 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14041 complain, in_decl);
14042 if (TREE_CODE (expanded_types) == TREE_VEC)
14043 {
14044 len = TREE_VEC_LENGTH (expanded_types);
14045
14046 /* Zero-length parameter packs are boring. Just substitute
14047 into the chain. */
14048 if (len == 0)
14049 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14050 TREE_CHAIN (t)));
14051 }
14052 else
14053 {
14054 /* All we did was update the type. Make a note of that. */
14055 type = expanded_types;
14056 expanded_types = NULL_TREE;
14057 }
14058 }
14059
14060 /* Loop through all of the parameters we'll build. When T is
14061 a function parameter pack, LEN is the number of expanded
14062 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14063 r = NULL_TREE;
14064 for (i = 0; i < len; ++i)
14065 {
14066 prev_r = r;
14067 r = copy_node (t);
14068 if (DECL_TEMPLATE_PARM_P (t))
14069 SET_DECL_TEMPLATE_PARM_P (r);
14070
14071 if (expanded_types)
14072 /* We're on the Ith parameter of the function parameter
14073 pack. */
14074 {
14075 /* Get the Ith type. */
14076 type = TREE_VEC_ELT (expanded_types, i);
14077
14078 /* Rename the parameter to include the index. */
14079 DECL_NAME (r)
14080 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14081 }
14082 else if (!type)
14083 /* We're dealing with a normal parameter. */
14084 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14085
14086 type = type_decays_to (type);
14087 TREE_TYPE (r) = type;
14088 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14089
14090 if (DECL_INITIAL (r))
14091 {
14092 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14093 DECL_INITIAL (r) = TREE_TYPE (r);
14094 else
14095 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14096 complain, in_decl);
14097 }
14098
14099 DECL_CONTEXT (r) = NULL_TREE;
14100
14101 if (!DECL_TEMPLATE_PARM_P (r))
14102 DECL_ARG_TYPE (r) = type_passed_as (type);
14103
14104 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14105 args, complain, in_decl);
14106
14107 /* Keep track of the first new parameter we
14108 generate. That's what will be returned to the
14109 caller. */
14110 if (!first_r)
14111 first_r = r;
14112
14113 /* Build a proper chain of parameters when substituting
14114 into a function parameter pack. */
14115 if (prev_r)
14116 DECL_CHAIN (prev_r) = r;
14117 }
14118
14119 /* If cp_unevaluated_operand is set, we're just looking for a
14120 single dummy parameter, so don't keep going. */
14121 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14122 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14123 complain, DECL_CHAIN (t));
14124
14125 /* FIRST_R contains the start of the chain we've built. */
14126 r = first_r;
14127 }
14128 break;
14129
14130 case FIELD_DECL:
14131 {
14132 tree type = NULL_TREE;
14133 tree vec = NULL_TREE;
14134 tree expanded_types = NULL_TREE;
14135 int len = 1;
14136
14137 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14138 {
14139 /* This field is a lambda capture pack. Return a TREE_VEC of
14140 the expanded fields to instantiate_class_template_1. */
14141 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14142 complain, in_decl);
14143 if (TREE_CODE (expanded_types) == TREE_VEC)
14144 {
14145 len = TREE_VEC_LENGTH (expanded_types);
14146 vec = make_tree_vec (len);
14147 }
14148 else
14149 {
14150 /* All we did was update the type. Make a note of that. */
14151 type = expanded_types;
14152 expanded_types = NULL_TREE;
14153 }
14154 }
14155
14156 for (int i = 0; i < len; ++i)
14157 {
14158 r = copy_decl (t);
14159 if (expanded_types)
14160 {
14161 type = TREE_VEC_ELT (expanded_types, i);
14162 DECL_NAME (r)
14163 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14164 }
14165 else if (!type)
14166 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14167
14168 if (type == error_mark_node)
14169 RETURN (error_mark_node);
14170 TREE_TYPE (r) = type;
14171 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14172
14173 if (DECL_C_BIT_FIELD (r))
14174 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14175 number of bits. */
14176 DECL_BIT_FIELD_REPRESENTATIVE (r)
14177 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14178 complain, in_decl,
14179 /*integral_constant_expression_p=*/true);
14180 if (DECL_INITIAL (t))
14181 {
14182 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14183 NSDMI in perform_member_init. Still set DECL_INITIAL
14184 so that we know there is one. */
14185 DECL_INITIAL (r) = void_node;
14186 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14187 retrofit_lang_decl (r);
14188 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14189 }
14190 /* We don't have to set DECL_CONTEXT here; it is set by
14191 finish_member_declaration. */
14192 DECL_CHAIN (r) = NULL_TREE;
14193
14194 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14195 args, complain, in_decl);
14196
14197 if (vec)
14198 TREE_VEC_ELT (vec, i) = r;
14199 }
14200
14201 if (vec)
14202 r = vec;
14203 }
14204 break;
14205
14206 case USING_DECL:
14207 /* We reach here only for member using decls. We also need to check
14208 uses_template_parms because DECL_DEPENDENT_P is not set for a
14209 using-declaration that designates a member of the current
14210 instantiation (c++/53549). */
14211 if (DECL_DEPENDENT_P (t)
14212 || uses_template_parms (USING_DECL_SCOPE (t)))
14213 {
14214 tree scope = USING_DECL_SCOPE (t);
14215 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14216 if (PACK_EXPANSION_P (scope))
14217 {
14218 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14219 int len = TREE_VEC_LENGTH (vec);
14220 r = make_tree_vec (len);
14221 for (int i = 0; i < len; ++i)
14222 {
14223 tree escope = TREE_VEC_ELT (vec, i);
14224 tree elt = do_class_using_decl (escope, name);
14225 if (!elt)
14226 {
14227 r = error_mark_node;
14228 break;
14229 }
14230 else
14231 {
14232 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14233 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14234 }
14235 TREE_VEC_ELT (r, i) = elt;
14236 }
14237 }
14238 else
14239 {
14240 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14241 complain, in_decl);
14242 r = do_class_using_decl (inst_scope, name);
14243 if (!r)
14244 r = error_mark_node;
14245 else
14246 {
14247 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14248 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14249 }
14250 }
14251 }
14252 else
14253 {
14254 r = copy_node (t);
14255 DECL_CHAIN (r) = NULL_TREE;
14256 }
14257 break;
14258
14259 case TYPE_DECL:
14260 case VAR_DECL:
14261 {
14262 tree argvec = NULL_TREE;
14263 tree gen_tmpl = NULL_TREE;
14264 tree spec;
14265 tree tmpl = NULL_TREE;
14266 tree ctx;
14267 tree type = NULL_TREE;
14268 bool local_p;
14269
14270 if (TREE_TYPE (t) == error_mark_node)
14271 RETURN (error_mark_node);
14272
14273 if (TREE_CODE (t) == TYPE_DECL
14274 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14275 {
14276 /* If this is the canonical decl, we don't have to
14277 mess with instantiations, and often we can't (for
14278 typename, template type parms and such). Note that
14279 TYPE_NAME is not correct for the above test if
14280 we've copied the type for a typedef. */
14281 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14282 if (type == error_mark_node)
14283 RETURN (error_mark_node);
14284 r = TYPE_NAME (type);
14285 break;
14286 }
14287
14288 /* Check to see if we already have the specialization we
14289 need. */
14290 spec = NULL_TREE;
14291 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
14292 {
14293 /* T is a static data member or namespace-scope entity.
14294 We have to substitute into namespace-scope variables
14295 (not just variable templates) because of cases like:
14296
14297 template <class T> void f() { extern T t; }
14298
14299 where the entity referenced is not known until
14300 instantiation time. */
14301 local_p = false;
14302 ctx = DECL_CONTEXT (t);
14303 if (DECL_CLASS_SCOPE_P (t))
14304 {
14305 ctx = tsubst_aggr_type (ctx, args,
14306 complain,
14307 in_decl, /*entering_scope=*/1);
14308 /* If CTX is unchanged, then T is in fact the
14309 specialization we want. That situation occurs when
14310 referencing a static data member within in its own
14311 class. We can use pointer equality, rather than
14312 same_type_p, because DECL_CONTEXT is always
14313 canonical... */
14314 if (ctx == DECL_CONTEXT (t)
14315 /* ... unless T is a member template; in which
14316 case our caller can be willing to create a
14317 specialization of that template represented
14318 by T. */
14319 && !(DECL_TI_TEMPLATE (t)
14320 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14321 spec = t;
14322 }
14323
14324 if (!spec)
14325 {
14326 tmpl = DECL_TI_TEMPLATE (t);
14327 gen_tmpl = most_general_template (tmpl);
14328 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14329 if (argvec != error_mark_node)
14330 argvec = (coerce_innermost_template_parms
14331 (DECL_TEMPLATE_PARMS (gen_tmpl),
14332 argvec, t, complain,
14333 /*all*/true, /*defarg*/true));
14334 if (argvec == error_mark_node)
14335 RETURN (error_mark_node);
14336 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14337 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14338 }
14339 }
14340 else
14341 {
14342 /* A local variable. */
14343 local_p = true;
14344 /* Subsequent calls to pushdecl will fill this in. */
14345 ctx = NULL_TREE;
14346 /* Unless this is a reference to a static variable from an
14347 enclosing function, in which case we need to fill it in now. */
14348 if (TREE_STATIC (t))
14349 {
14350 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14351 if (fn != current_function_decl)
14352 ctx = fn;
14353 }
14354 spec = retrieve_local_specialization (t);
14355 }
14356 /* If we already have the specialization we need, there is
14357 nothing more to do. */
14358 if (spec)
14359 {
14360 r = spec;
14361 break;
14362 }
14363
14364 /* Create a new node for the specialization we need. */
14365 if (type == NULL_TREE)
14366 {
14367 if (is_typedef_decl (t))
14368 type = DECL_ORIGINAL_TYPE (t);
14369 else
14370 type = TREE_TYPE (t);
14371 if (VAR_P (t)
14372 && VAR_HAD_UNKNOWN_BOUND (t)
14373 && type != error_mark_node)
14374 type = strip_array_domain (type);
14375 tree sub_args = args;
14376 if (tree auto_node = type_uses_auto (type))
14377 {
14378 /* Mask off any template args past the variable's context so we
14379 don't replace the auto with an unrelated argument. */
14380 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14381 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14382 if (extra > 0)
14383 /* This should never happen with the new lambda instantiation
14384 model, but keep the handling just in case. */
14385 gcc_assert (!CHECKING_P),
14386 sub_args = strip_innermost_template_args (args, extra);
14387 }
14388 type = tsubst (type, sub_args, complain, in_decl);
14389 /* Substituting the type might have recursively instantiated this
14390 same alias (c++/86171). */
14391 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14392 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14393 {
14394 r = spec;
14395 break;
14396 }
14397 }
14398 r = copy_decl (t);
14399 if (VAR_P (r))
14400 {
14401 DECL_INITIALIZED_P (r) = 0;
14402 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14403 if (type == error_mark_node)
14404 RETURN (error_mark_node);
14405 if (TREE_CODE (type) == FUNCTION_TYPE)
14406 {
14407 /* It may seem that this case cannot occur, since:
14408
14409 typedef void f();
14410 void g() { f x; }
14411
14412 declares a function, not a variable. However:
14413
14414 typedef void f();
14415 template <typename T> void g() { T t; }
14416 template void g<f>();
14417
14418 is an attempt to declare a variable with function
14419 type. */
14420 error ("variable %qD has function type",
14421 /* R is not yet sufficiently initialized, so we
14422 just use its name. */
14423 DECL_NAME (r));
14424 RETURN (error_mark_node);
14425 }
14426 type = complete_type (type);
14427 /* Wait until cp_finish_decl to set this again, to handle
14428 circular dependency (template/instantiate6.C). */
14429 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14430 type = check_var_type (DECL_NAME (r), type,
14431 DECL_SOURCE_LOCATION (r));
14432 if (DECL_HAS_VALUE_EXPR_P (t))
14433 {
14434 tree ve = DECL_VALUE_EXPR (t);
14435 ve = tsubst_expr (ve, args, complain, in_decl,
14436 /*constant_expression_p=*/false);
14437 if (REFERENCE_REF_P (ve))
14438 {
14439 gcc_assert (TYPE_REF_P (type));
14440 ve = TREE_OPERAND (ve, 0);
14441 }
14442 SET_DECL_VALUE_EXPR (r, ve);
14443 }
14444 if (CP_DECL_THREAD_LOCAL_P (r)
14445 && !processing_template_decl)
14446 set_decl_tls_model (r, decl_default_tls_model (r));
14447 }
14448 else if (DECL_SELF_REFERENCE_P (t))
14449 SET_DECL_SELF_REFERENCE_P (r);
14450 TREE_TYPE (r) = type;
14451 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14452 DECL_CONTEXT (r) = ctx;
14453 /* Clear out the mangled name and RTL for the instantiation. */
14454 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14455 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14456 SET_DECL_RTL (r, NULL);
14457 /* The initializer must not be expanded until it is required;
14458 see [temp.inst]. */
14459 DECL_INITIAL (r) = NULL_TREE;
14460 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14461 if (VAR_P (r))
14462 {
14463 if (DECL_LANG_SPECIFIC (r))
14464 SET_DECL_DEPENDENT_INIT_P (r, false);
14465
14466 SET_DECL_MODE (r, VOIDmode);
14467
14468 /* Possibly limit visibility based on template args. */
14469 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14470 if (DECL_VISIBILITY_SPECIFIED (t))
14471 {
14472 DECL_VISIBILITY_SPECIFIED (r) = 0;
14473 DECL_ATTRIBUTES (r)
14474 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14475 }
14476 determine_visibility (r);
14477 }
14478
14479 if (!local_p)
14480 {
14481 /* A static data member declaration is always marked
14482 external when it is declared in-class, even if an
14483 initializer is present. We mimic the non-template
14484 processing here. */
14485 DECL_EXTERNAL (r) = 1;
14486 if (DECL_NAMESPACE_SCOPE_P (t))
14487 DECL_NOT_REALLY_EXTERN (r) = 1;
14488
14489 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14490 SET_DECL_IMPLICIT_INSTANTIATION (r);
14491 /* Remember whether we require constant initialization of
14492 a non-constant template variable. */
14493 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
14494 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
14495 if (!error_operand_p (r) || (complain & tf_error))
14496 register_specialization (r, gen_tmpl, argvec, false, hash);
14497 }
14498 else
14499 {
14500 if (DECL_LANG_SPECIFIC (r))
14501 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14502 if (!cp_unevaluated_operand)
14503 register_local_specialization (r, t);
14504 }
14505
14506 DECL_CHAIN (r) = NULL_TREE;
14507
14508 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14509 /*flags=*/0,
14510 args, complain, in_decl);
14511
14512 /* Preserve a typedef that names a type. */
14513 if (is_typedef_decl (r) && type != error_mark_node)
14514 {
14515 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14516 set_underlying_type (r);
14517 if (TYPE_DECL_ALIAS_P (r))
14518 /* An alias template specialization can be dependent
14519 even if its underlying type is not. */
14520 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14521 }
14522
14523 layout_decl (r, 0);
14524 }
14525 break;
14526
14527 default:
14528 gcc_unreachable ();
14529 }
14530 #undef RETURN
14531
14532 out:
14533 /* Restore the file and line information. */
14534 input_location = saved_loc;
14535
14536 return r;
14537 }
14538
14539 /* Substitute into the complete parameter type list PARMS. */
14540
14541 tree
14542 tsubst_function_parms (tree parms,
14543 tree args,
14544 tsubst_flags_t complain,
14545 tree in_decl)
14546 {
14547 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14548 }
14549
14550 /* Substitute into the ARG_TYPES of a function type.
14551 If END is a TREE_CHAIN, leave it and any following types
14552 un-substituted. */
14553
14554 static tree
14555 tsubst_arg_types (tree arg_types,
14556 tree args,
14557 tree end,
14558 tsubst_flags_t complain,
14559 tree in_decl)
14560 {
14561 tree remaining_arg_types;
14562 tree type = NULL_TREE;
14563 int i = 1;
14564 tree expanded_args = NULL_TREE;
14565 tree default_arg;
14566
14567 if (!arg_types || arg_types == void_list_node || arg_types == end)
14568 return arg_types;
14569
14570 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14571 args, end, complain, in_decl);
14572 if (remaining_arg_types == error_mark_node)
14573 return error_mark_node;
14574
14575 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14576 {
14577 /* For a pack expansion, perform substitution on the
14578 entire expression. Later on, we'll handle the arguments
14579 one-by-one. */
14580 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14581 args, complain, in_decl);
14582
14583 if (TREE_CODE (expanded_args) == TREE_VEC)
14584 /* So that we'll spin through the parameters, one by one. */
14585 i = TREE_VEC_LENGTH (expanded_args);
14586 else
14587 {
14588 /* We only partially substituted into the parameter
14589 pack. Our type is TYPE_PACK_EXPANSION. */
14590 type = expanded_args;
14591 expanded_args = NULL_TREE;
14592 }
14593 }
14594
14595 while (i > 0) {
14596 --i;
14597
14598 if (expanded_args)
14599 type = TREE_VEC_ELT (expanded_args, i);
14600 else if (!type)
14601 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14602
14603 if (type == error_mark_node)
14604 return error_mark_node;
14605 if (VOID_TYPE_P (type))
14606 {
14607 if (complain & tf_error)
14608 {
14609 error ("invalid parameter type %qT", type);
14610 if (in_decl)
14611 error ("in declaration %q+D", in_decl);
14612 }
14613 return error_mark_node;
14614 }
14615 /* DR 657. */
14616 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14617 return error_mark_node;
14618
14619 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14620 top-level qualifiers as required. */
14621 type = cv_unqualified (type_decays_to (type));
14622
14623 /* We do not substitute into default arguments here. The standard
14624 mandates that they be instantiated only when needed, which is
14625 done in build_over_call. */
14626 default_arg = TREE_PURPOSE (arg_types);
14627
14628 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14629 since the new op() won't have any associated template arguments for us
14630 to refer to later. */
14631 if (lambda_fn_in_template_p (in_decl))
14632 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14633 false/*fn*/, false/*constexpr*/);
14634
14635 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14636 {
14637 /* We've instantiated a template before its default arguments
14638 have been parsed. This can happen for a nested template
14639 class, and is not an error unless we require the default
14640 argument in a call of this function. */
14641 remaining_arg_types =
14642 tree_cons (default_arg, type, remaining_arg_types);
14643 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14644 remaining_arg_types);
14645 }
14646 else
14647 remaining_arg_types =
14648 hash_tree_cons (default_arg, type, remaining_arg_types);
14649 }
14650
14651 return remaining_arg_types;
14652 }
14653
14654 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14655 *not* handle the exception-specification for FNTYPE, because the
14656 initial substitution of explicitly provided template parameters
14657 during argument deduction forbids substitution into the
14658 exception-specification:
14659
14660 [temp.deduct]
14661
14662 All references in the function type of the function template to the
14663 corresponding template parameters are replaced by the specified tem-
14664 plate argument values. If a substitution in a template parameter or
14665 in the function type of the function template results in an invalid
14666 type, type deduction fails. [Note: The equivalent substitution in
14667 exception specifications is done only when the function is instanti-
14668 ated, at which point a program is ill-formed if the substitution
14669 results in an invalid type.] */
14670
14671 static tree
14672 tsubst_function_type (tree t,
14673 tree args,
14674 tsubst_flags_t complain,
14675 tree in_decl)
14676 {
14677 tree return_type;
14678 tree arg_types = NULL_TREE;
14679 tree fntype;
14680
14681 /* The TYPE_CONTEXT is not used for function/method types. */
14682 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14683
14684 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14685 failure. */
14686 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14687
14688 if (late_return_type_p)
14689 {
14690 /* Substitute the argument types. */
14691 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14692 complain, in_decl);
14693 if (arg_types == error_mark_node)
14694 return error_mark_node;
14695
14696 tree save_ccp = current_class_ptr;
14697 tree save_ccr = current_class_ref;
14698 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14699 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14700 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14701 if (do_inject)
14702 {
14703 /* DR 1207: 'this' is in scope in the trailing return type. */
14704 inject_this_parameter (this_type, cp_type_quals (this_type));
14705 }
14706
14707 /* Substitute the return type. */
14708 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14709
14710 if (do_inject)
14711 {
14712 current_class_ptr = save_ccp;
14713 current_class_ref = save_ccr;
14714 }
14715 }
14716 else
14717 /* Substitute the return type. */
14718 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14719
14720 if (return_type == error_mark_node)
14721 return error_mark_node;
14722 /* DR 486 clarifies that creation of a function type with an
14723 invalid return type is a deduction failure. */
14724 if (TREE_CODE (return_type) == ARRAY_TYPE
14725 || TREE_CODE (return_type) == FUNCTION_TYPE)
14726 {
14727 if (complain & tf_error)
14728 {
14729 if (TREE_CODE (return_type) == ARRAY_TYPE)
14730 error ("function returning an array");
14731 else
14732 error ("function returning a function");
14733 }
14734 return error_mark_node;
14735 }
14736 /* And DR 657. */
14737 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14738 return error_mark_node;
14739
14740 if (!late_return_type_p)
14741 {
14742 /* Substitute the argument types. */
14743 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14744 complain, in_decl);
14745 if (arg_types == error_mark_node)
14746 return error_mark_node;
14747 }
14748
14749 /* Construct a new type node and return it. */
14750 if (TREE_CODE (t) == FUNCTION_TYPE)
14751 {
14752 fntype = build_function_type (return_type, arg_types);
14753 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14754 }
14755 else
14756 {
14757 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14758 /* Don't pick up extra function qualifiers from the basetype. */
14759 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14760 if (! MAYBE_CLASS_TYPE_P (r))
14761 {
14762 /* [temp.deduct]
14763
14764 Type deduction may fail for any of the following
14765 reasons:
14766
14767 -- Attempting to create "pointer to member of T" when T
14768 is not a class type. */
14769 if (complain & tf_error)
14770 error ("creating pointer to member function of non-class type %qT",
14771 r);
14772 return error_mark_node;
14773 }
14774
14775 fntype = build_method_type_directly (r, return_type,
14776 TREE_CHAIN (arg_types));
14777 }
14778 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14779
14780 /* See comment above. */
14781 tree raises = NULL_TREE;
14782 cp_ref_qualifier rqual = type_memfn_rqual (t);
14783 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14784
14785 return fntype;
14786 }
14787
14788 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14789 ARGS into that specification, and return the substituted
14790 specification. If there is no specification, return NULL_TREE. */
14791
14792 static tree
14793 tsubst_exception_specification (tree fntype,
14794 tree args,
14795 tsubst_flags_t complain,
14796 tree in_decl,
14797 bool defer_ok)
14798 {
14799 tree specs;
14800 tree new_specs;
14801
14802 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14803 new_specs = NULL_TREE;
14804 if (specs && TREE_PURPOSE (specs))
14805 {
14806 /* A noexcept-specifier. */
14807 tree expr = TREE_PURPOSE (specs);
14808 if (TREE_CODE (expr) == INTEGER_CST)
14809 new_specs = expr;
14810 else if (defer_ok)
14811 {
14812 /* Defer instantiation of noexcept-specifiers to avoid
14813 excessive instantiations (c++/49107). */
14814 new_specs = make_node (DEFERRED_NOEXCEPT);
14815 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14816 {
14817 /* We already partially instantiated this member template,
14818 so combine the new args with the old. */
14819 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14820 = DEFERRED_NOEXCEPT_PATTERN (expr);
14821 DEFERRED_NOEXCEPT_ARGS (new_specs)
14822 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14823 }
14824 else
14825 {
14826 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14827 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14828 }
14829 }
14830 else
14831 {
14832 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14833 {
14834 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14835 args);
14836 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14837 }
14838 new_specs = tsubst_copy_and_build
14839 (expr, args, complain, in_decl, /*function_p=*/false,
14840 /*integral_constant_expression_p=*/true);
14841 }
14842 new_specs = build_noexcept_spec (new_specs, complain);
14843 }
14844 else if (specs)
14845 {
14846 if (! TREE_VALUE (specs))
14847 new_specs = specs;
14848 else
14849 while (specs)
14850 {
14851 tree spec;
14852 int i, len = 1;
14853 tree expanded_specs = NULL_TREE;
14854
14855 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14856 {
14857 /* Expand the pack expansion type. */
14858 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14859 args, complain,
14860 in_decl);
14861
14862 if (expanded_specs == error_mark_node)
14863 return error_mark_node;
14864 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14865 len = TREE_VEC_LENGTH (expanded_specs);
14866 else
14867 {
14868 /* We're substituting into a member template, so
14869 we got a TYPE_PACK_EXPANSION back. Add that
14870 expansion and move on. */
14871 gcc_assert (TREE_CODE (expanded_specs)
14872 == TYPE_PACK_EXPANSION);
14873 new_specs = add_exception_specifier (new_specs,
14874 expanded_specs,
14875 complain);
14876 specs = TREE_CHAIN (specs);
14877 continue;
14878 }
14879 }
14880
14881 for (i = 0; i < len; ++i)
14882 {
14883 if (expanded_specs)
14884 spec = TREE_VEC_ELT (expanded_specs, i);
14885 else
14886 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14887 if (spec == error_mark_node)
14888 return spec;
14889 new_specs = add_exception_specifier (new_specs, spec,
14890 complain);
14891 }
14892
14893 specs = TREE_CHAIN (specs);
14894 }
14895 }
14896 return new_specs;
14897 }
14898
14899 /* Take the tree structure T and replace template parameters used
14900 therein with the argument vector ARGS. IN_DECL is an associated
14901 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14902 Issue error and warning messages under control of COMPLAIN. Note
14903 that we must be relatively non-tolerant of extensions here, in
14904 order to preserve conformance; if we allow substitutions that
14905 should not be allowed, we may allow argument deductions that should
14906 not succeed, and therefore report ambiguous overload situations
14907 where there are none. In theory, we could allow the substitution,
14908 but indicate that it should have failed, and allow our caller to
14909 make sure that the right thing happens, but we don't try to do this
14910 yet.
14911
14912 This function is used for dealing with types, decls and the like;
14913 for expressions, use tsubst_expr or tsubst_copy. */
14914
14915 tree
14916 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14917 {
14918 enum tree_code code;
14919 tree type, r = NULL_TREE;
14920
14921 if (t == NULL_TREE || t == error_mark_node
14922 || t == integer_type_node
14923 || t == void_type_node
14924 || t == char_type_node
14925 || t == unknown_type_node
14926 || TREE_CODE (t) == NAMESPACE_DECL
14927 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14928 return t;
14929
14930 if (DECL_P (t))
14931 return tsubst_decl (t, args, complain);
14932
14933 if (args == NULL_TREE)
14934 return t;
14935
14936 code = TREE_CODE (t);
14937
14938 if (code == IDENTIFIER_NODE)
14939 type = IDENTIFIER_TYPE_VALUE (t);
14940 else
14941 type = TREE_TYPE (t);
14942
14943 gcc_assert (type != unknown_type_node);
14944
14945 /* Reuse typedefs. We need to do this to handle dependent attributes,
14946 such as attribute aligned. */
14947 if (TYPE_P (t)
14948 && typedef_variant_p (t))
14949 {
14950 tree decl = TYPE_NAME (t);
14951
14952 if (alias_template_specialization_p (t, nt_opaque))
14953 {
14954 /* DECL represents an alias template and we want to
14955 instantiate it. */
14956 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14957 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14958 r = instantiate_alias_template (tmpl, gen_args, complain);
14959 }
14960 else if (DECL_CLASS_SCOPE_P (decl)
14961 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14962 && uses_template_parms (DECL_CONTEXT (decl)))
14963 {
14964 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14965 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14966 r = retrieve_specialization (tmpl, gen_args, 0);
14967 }
14968 else if (DECL_FUNCTION_SCOPE_P (decl)
14969 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14970 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14971 r = retrieve_local_specialization (decl);
14972 else
14973 /* The typedef is from a non-template context. */
14974 return t;
14975
14976 if (r)
14977 {
14978 r = TREE_TYPE (r);
14979 r = cp_build_qualified_type_real
14980 (r, cp_type_quals (t) | cp_type_quals (r),
14981 complain | tf_ignore_bad_quals);
14982 return r;
14983 }
14984 else
14985 {
14986 /* We don't have an instantiation yet, so drop the typedef. */
14987 int quals = cp_type_quals (t);
14988 t = DECL_ORIGINAL_TYPE (decl);
14989 t = cp_build_qualified_type_real (t, quals,
14990 complain | tf_ignore_bad_quals);
14991 }
14992 }
14993
14994 bool fndecl_type = (complain & tf_fndecl_type);
14995 complain &= ~tf_fndecl_type;
14996
14997 if (type
14998 && code != TYPENAME_TYPE
14999 && code != TEMPLATE_TYPE_PARM
15000 && code != TEMPLATE_PARM_INDEX
15001 && code != IDENTIFIER_NODE
15002 && code != FUNCTION_TYPE
15003 && code != METHOD_TYPE)
15004 type = tsubst (type, args, complain, in_decl);
15005 if (type == error_mark_node)
15006 return error_mark_node;
15007
15008 switch (code)
15009 {
15010 case RECORD_TYPE:
15011 case UNION_TYPE:
15012 case ENUMERAL_TYPE:
15013 return tsubst_aggr_type (t, args, complain, in_decl,
15014 /*entering_scope=*/0);
15015
15016 case ERROR_MARK:
15017 case IDENTIFIER_NODE:
15018 case VOID_TYPE:
15019 case REAL_TYPE:
15020 case COMPLEX_TYPE:
15021 case VECTOR_TYPE:
15022 case BOOLEAN_TYPE:
15023 case NULLPTR_TYPE:
15024 case LANG_TYPE:
15025 return t;
15026
15027 case INTEGER_TYPE:
15028 if (t == integer_type_node)
15029 return t;
15030
15031 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15032 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15033 return t;
15034
15035 {
15036 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15037
15038 max = tsubst_expr (omax, args, complain, in_decl,
15039 /*integral_constant_expression_p=*/false);
15040
15041 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15042 needed. */
15043 if (TREE_CODE (max) == NOP_EXPR
15044 && TREE_SIDE_EFFECTS (omax)
15045 && !TREE_TYPE (max))
15046 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15047
15048 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15049 with TREE_SIDE_EFFECTS that indicates this is not an integral
15050 constant expression. */
15051 if (processing_template_decl
15052 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15053 {
15054 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15055 TREE_SIDE_EFFECTS (max) = 1;
15056 }
15057
15058 return compute_array_index_type (NULL_TREE, max, complain);
15059 }
15060
15061 case TEMPLATE_TYPE_PARM:
15062 case TEMPLATE_TEMPLATE_PARM:
15063 case BOUND_TEMPLATE_TEMPLATE_PARM:
15064 case TEMPLATE_PARM_INDEX:
15065 {
15066 int idx;
15067 int level;
15068 int levels;
15069 tree arg = NULL_TREE;
15070
15071 /* Early in template argument deduction substitution, we don't
15072 want to reduce the level of 'auto', or it will be confused
15073 with a normal template parm in subsequent deduction. */
15074 if (is_auto (t) && (complain & tf_partial))
15075 return t;
15076
15077 r = NULL_TREE;
15078
15079 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15080 template_parm_level_and_index (t, &level, &idx);
15081
15082 levels = TMPL_ARGS_DEPTH (args);
15083 if (level <= levels
15084 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15085 {
15086 arg = TMPL_ARG (args, level, idx);
15087
15088 /* See through ARGUMENT_PACK_SELECT arguments. */
15089 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15090 arg = argument_pack_select_arg (arg);
15091 }
15092
15093 if (arg == error_mark_node)
15094 return error_mark_node;
15095 else if (arg != NULL_TREE)
15096 {
15097 if (ARGUMENT_PACK_P (arg))
15098 /* If ARG is an argument pack, we don't actually want to
15099 perform a substitution here, because substitutions
15100 for argument packs are only done
15101 element-by-element. We can get to this point when
15102 substituting the type of a non-type template
15103 parameter pack, when that type actually contains
15104 template parameter packs from an outer template, e.g.,
15105
15106 template<typename... Types> struct A {
15107 template<Types... Values> struct B { };
15108 }; */
15109 return t;
15110
15111 if (code == TEMPLATE_TYPE_PARM)
15112 {
15113 int quals;
15114
15115 /* When building concept checks for the purpose of
15116 deducing placeholders, we can end up with wildcards
15117 where types are expected. Adjust this to the deduced
15118 value. */
15119 if (TREE_CODE (arg) == WILDCARD_DECL)
15120 arg = TREE_TYPE (TREE_TYPE (arg));
15121
15122 gcc_assert (TYPE_P (arg));
15123
15124 quals = cp_type_quals (arg) | cp_type_quals (t);
15125
15126 return cp_build_qualified_type_real
15127 (arg, quals, complain | tf_ignore_bad_quals);
15128 }
15129 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15130 {
15131 /* We are processing a type constructed from a
15132 template template parameter. */
15133 tree argvec = tsubst (TYPE_TI_ARGS (t),
15134 args, complain, in_decl);
15135 if (argvec == error_mark_node)
15136 return error_mark_node;
15137
15138 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15139 || TREE_CODE (arg) == TEMPLATE_DECL
15140 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15141
15142 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15143 /* Consider this code:
15144
15145 template <template <class> class Template>
15146 struct Internal {
15147 template <class Arg> using Bind = Template<Arg>;
15148 };
15149
15150 template <template <class> class Template, class Arg>
15151 using Instantiate = Template<Arg>; //#0
15152
15153 template <template <class> class Template,
15154 class Argument>
15155 using Bind =
15156 Instantiate<Internal<Template>::template Bind,
15157 Argument>; //#1
15158
15159 When #1 is parsed, the
15160 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15161 parameter `Template' in #0 matches the
15162 UNBOUND_CLASS_TEMPLATE representing the argument
15163 `Internal<Template>::template Bind'; We then want
15164 to assemble the type `Bind<Argument>' that can't
15165 be fully created right now, because
15166 `Internal<Template>' not being complete, the Bind
15167 template cannot be looked up in that context. So
15168 we need to "store" `Bind<Argument>' for later
15169 when the context of Bind becomes complete. Let's
15170 store that in a TYPENAME_TYPE. */
15171 return make_typename_type (TYPE_CONTEXT (arg),
15172 build_nt (TEMPLATE_ID_EXPR,
15173 TYPE_IDENTIFIER (arg),
15174 argvec),
15175 typename_type,
15176 complain);
15177
15178 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15179 are resolving nested-types in the signature of a
15180 member function templates. Otherwise ARG is a
15181 TEMPLATE_DECL and is the real template to be
15182 instantiated. */
15183 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15184 arg = TYPE_NAME (arg);
15185
15186 r = lookup_template_class (arg,
15187 argvec, in_decl,
15188 DECL_CONTEXT (arg),
15189 /*entering_scope=*/0,
15190 complain);
15191 return cp_build_qualified_type_real
15192 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15193 }
15194 else if (code == TEMPLATE_TEMPLATE_PARM)
15195 return arg;
15196 else
15197 /* TEMPLATE_PARM_INDEX. */
15198 return convert_from_reference (unshare_expr (arg));
15199 }
15200
15201 if (level == 1)
15202 /* This can happen during the attempted tsubst'ing in
15203 unify. This means that we don't yet have any information
15204 about the template parameter in question. */
15205 return t;
15206
15207 /* If we get here, we must have been looking at a parm for a
15208 more deeply nested template. Make a new version of this
15209 template parameter, but with a lower level. */
15210 switch (code)
15211 {
15212 case TEMPLATE_TYPE_PARM:
15213 case TEMPLATE_TEMPLATE_PARM:
15214 case BOUND_TEMPLATE_TEMPLATE_PARM:
15215 if (cp_type_quals (t))
15216 {
15217 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15218 r = cp_build_qualified_type_real
15219 (r, cp_type_quals (t),
15220 complain | (code == TEMPLATE_TYPE_PARM
15221 ? tf_ignore_bad_quals : 0));
15222 }
15223 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15224 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15225 && (r = (TEMPLATE_PARM_DESCENDANTS
15226 (TEMPLATE_TYPE_PARM_INDEX (t))))
15227 && (r = TREE_TYPE (r))
15228 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15229 /* Break infinite recursion when substituting the constraints
15230 of a constrained placeholder. */;
15231 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15232 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15233 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15234 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15235 r = TEMPLATE_PARM_DESCENDANTS (arg))
15236 && (TEMPLATE_PARM_LEVEL (r)
15237 == TEMPLATE_PARM_LEVEL (arg) - levels))
15238 /* Cache the simple case of lowering a type parameter. */
15239 r = TREE_TYPE (r);
15240 else
15241 {
15242 r = copy_type (t);
15243 TEMPLATE_TYPE_PARM_INDEX (r)
15244 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15245 r, levels, args, complain);
15246 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15247 TYPE_MAIN_VARIANT (r) = r;
15248 TYPE_POINTER_TO (r) = NULL_TREE;
15249 TYPE_REFERENCE_TO (r) = NULL_TREE;
15250
15251 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15252 {
15253 /* Propagate constraints on placeholders since they are
15254 only instantiated during satisfaction. */
15255 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15256 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15257 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15258 {
15259 pl = tsubst_copy (pl, args, complain, in_decl);
15260 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15261 }
15262 }
15263
15264 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15265 /* We have reduced the level of the template
15266 template parameter, but not the levels of its
15267 template parameters, so canonical_type_parameter
15268 will not be able to find the canonical template
15269 template parameter for this level. Thus, we
15270 require structural equality checking to compare
15271 TEMPLATE_TEMPLATE_PARMs. */
15272 SET_TYPE_STRUCTURAL_EQUALITY (r);
15273 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15274 SET_TYPE_STRUCTURAL_EQUALITY (r);
15275 else
15276 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15277
15278 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15279 {
15280 tree tinfo = TYPE_TEMPLATE_INFO (t);
15281 /* We might need to substitute into the types of non-type
15282 template parameters. */
15283 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15284 complain, in_decl);
15285 if (tmpl == error_mark_node)
15286 return error_mark_node;
15287 tree argvec = tsubst (TI_ARGS (tinfo), args,
15288 complain, in_decl);
15289 if (argvec == error_mark_node)
15290 return error_mark_node;
15291
15292 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15293 = build_template_info (tmpl, argvec);
15294 }
15295 }
15296 break;
15297
15298 case TEMPLATE_PARM_INDEX:
15299 /* OK, now substitute the type of the non-type parameter. We
15300 couldn't do it earlier because it might be an auto parameter,
15301 and we wouldn't need to if we had an argument. */
15302 type = tsubst (type, args, complain, in_decl);
15303 if (type == error_mark_node)
15304 return error_mark_node;
15305 r = reduce_template_parm_level (t, type, levels, args, complain);
15306 break;
15307
15308 default:
15309 gcc_unreachable ();
15310 }
15311
15312 return r;
15313 }
15314
15315 case TREE_LIST:
15316 {
15317 tree purpose, value, chain;
15318
15319 if (t == void_list_node)
15320 return t;
15321
15322 purpose = TREE_PURPOSE (t);
15323 if (purpose)
15324 {
15325 purpose = tsubst (purpose, args, complain, in_decl);
15326 if (purpose == error_mark_node)
15327 return error_mark_node;
15328 }
15329 value = TREE_VALUE (t);
15330 if (value)
15331 {
15332 value = tsubst (value, args, complain, in_decl);
15333 if (value == error_mark_node)
15334 return error_mark_node;
15335 }
15336 chain = TREE_CHAIN (t);
15337 if (chain && chain != void_type_node)
15338 {
15339 chain = tsubst (chain, args, complain, in_decl);
15340 if (chain == error_mark_node)
15341 return error_mark_node;
15342 }
15343 if (purpose == TREE_PURPOSE (t)
15344 && value == TREE_VALUE (t)
15345 && chain == TREE_CHAIN (t))
15346 return t;
15347 return hash_tree_cons (purpose, value, chain);
15348 }
15349
15350 case TREE_BINFO:
15351 /* We should never be tsubsting a binfo. */
15352 gcc_unreachable ();
15353
15354 case TREE_VEC:
15355 /* A vector of template arguments. */
15356 gcc_assert (!type);
15357 return tsubst_template_args (t, args, complain, in_decl);
15358
15359 case POINTER_TYPE:
15360 case REFERENCE_TYPE:
15361 {
15362 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15363 return t;
15364
15365 /* [temp.deduct]
15366
15367 Type deduction may fail for any of the following
15368 reasons:
15369
15370 -- Attempting to create a pointer to reference type.
15371 -- Attempting to create a reference to a reference type or
15372 a reference to void.
15373
15374 Core issue 106 says that creating a reference to a reference
15375 during instantiation is no longer a cause for failure. We
15376 only enforce this check in strict C++98 mode. */
15377 if ((TYPE_REF_P (type)
15378 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15379 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15380 {
15381 static location_t last_loc;
15382
15383 /* We keep track of the last time we issued this error
15384 message to avoid spewing a ton of messages during a
15385 single bad template instantiation. */
15386 if (complain & tf_error
15387 && last_loc != input_location)
15388 {
15389 if (VOID_TYPE_P (type))
15390 error ("forming reference to void");
15391 else if (code == POINTER_TYPE)
15392 error ("forming pointer to reference type %qT", type);
15393 else
15394 error ("forming reference to reference type %qT", type);
15395 last_loc = input_location;
15396 }
15397
15398 return error_mark_node;
15399 }
15400 else if (TREE_CODE (type) == FUNCTION_TYPE
15401 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15402 || type_memfn_rqual (type) != REF_QUAL_NONE))
15403 {
15404 if (complain & tf_error)
15405 {
15406 if (code == POINTER_TYPE)
15407 error ("forming pointer to qualified function type %qT",
15408 type);
15409 else
15410 error ("forming reference to qualified function type %qT",
15411 type);
15412 }
15413 return error_mark_node;
15414 }
15415 else if (code == POINTER_TYPE)
15416 {
15417 r = build_pointer_type (type);
15418 if (TREE_CODE (type) == METHOD_TYPE)
15419 r = build_ptrmemfunc_type (r);
15420 }
15421 else if (TYPE_REF_P (type))
15422 /* In C++0x, during template argument substitution, when there is an
15423 attempt to create a reference to a reference type, reference
15424 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15425
15426 "If a template-argument for a template-parameter T names a type
15427 that is a reference to a type A, an attempt to create the type
15428 'lvalue reference to cv T' creates the type 'lvalue reference to
15429 A,' while an attempt to create the type type rvalue reference to
15430 cv T' creates the type T"
15431 */
15432 r = cp_build_reference_type
15433 (TREE_TYPE (type),
15434 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15435 else
15436 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15437 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15438
15439 if (r != error_mark_node)
15440 /* Will this ever be needed for TYPE_..._TO values? */
15441 layout_type (r);
15442
15443 return r;
15444 }
15445 case OFFSET_TYPE:
15446 {
15447 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15448 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15449 {
15450 /* [temp.deduct]
15451
15452 Type deduction may fail for any of the following
15453 reasons:
15454
15455 -- Attempting to create "pointer to member of T" when T
15456 is not a class type. */
15457 if (complain & tf_error)
15458 error ("creating pointer to member of non-class type %qT", r);
15459 return error_mark_node;
15460 }
15461 if (TYPE_REF_P (type))
15462 {
15463 if (complain & tf_error)
15464 error ("creating pointer to member reference type %qT", type);
15465 return error_mark_node;
15466 }
15467 if (VOID_TYPE_P (type))
15468 {
15469 if (complain & tf_error)
15470 error ("creating pointer to member of type void");
15471 return error_mark_node;
15472 }
15473 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15474 if (TREE_CODE (type) == FUNCTION_TYPE)
15475 {
15476 /* The type of the implicit object parameter gets its
15477 cv-qualifiers from the FUNCTION_TYPE. */
15478 tree memptr;
15479 tree method_type
15480 = build_memfn_type (type, r, type_memfn_quals (type),
15481 type_memfn_rqual (type));
15482 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15483 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15484 complain);
15485 }
15486 else
15487 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15488 cp_type_quals (t),
15489 complain);
15490 }
15491 case FUNCTION_TYPE:
15492 case METHOD_TYPE:
15493 {
15494 tree fntype;
15495 tree specs;
15496 fntype = tsubst_function_type (t, args, complain, in_decl);
15497 if (fntype == error_mark_node)
15498 return error_mark_node;
15499
15500 /* Substitute the exception specification. */
15501 specs = tsubst_exception_specification (t, args, complain, in_decl,
15502 /*defer_ok*/fndecl_type);
15503 if (specs == error_mark_node)
15504 return error_mark_node;
15505 if (specs)
15506 fntype = build_exception_variant (fntype, specs);
15507 return fntype;
15508 }
15509 case ARRAY_TYPE:
15510 {
15511 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15512 if (domain == error_mark_node)
15513 return error_mark_node;
15514
15515 /* As an optimization, we avoid regenerating the array type if
15516 it will obviously be the same as T. */
15517 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15518 return t;
15519
15520 /* These checks should match the ones in create_array_type_for_decl.
15521
15522 [temp.deduct]
15523
15524 The deduction may fail for any of the following reasons:
15525
15526 -- Attempting to create an array with an element type that
15527 is void, a function type, or a reference type, or [DR337]
15528 an abstract class type. */
15529 if (VOID_TYPE_P (type)
15530 || TREE_CODE (type) == FUNCTION_TYPE
15531 || (TREE_CODE (type) == ARRAY_TYPE
15532 && TYPE_DOMAIN (type) == NULL_TREE)
15533 || TYPE_REF_P (type))
15534 {
15535 if (complain & tf_error)
15536 error ("creating array of %qT", type);
15537 return error_mark_node;
15538 }
15539
15540 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15541 return error_mark_node;
15542
15543 r = build_cplus_array_type (type, domain);
15544
15545 if (!valid_array_size_p (input_location, r, in_decl,
15546 (complain & tf_error)))
15547 return error_mark_node;
15548
15549 if (TYPE_USER_ALIGN (t))
15550 {
15551 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15552 TYPE_USER_ALIGN (r) = 1;
15553 }
15554
15555 return r;
15556 }
15557
15558 case TYPENAME_TYPE:
15559 {
15560 tree ctx = TYPE_CONTEXT (t);
15561 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15562 {
15563 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15564 if (ctx == error_mark_node
15565 || TREE_VEC_LENGTH (ctx) > 1)
15566 return error_mark_node;
15567 if (TREE_VEC_LENGTH (ctx) == 0)
15568 {
15569 if (complain & tf_error)
15570 error ("%qD is instantiated for an empty pack",
15571 TYPENAME_TYPE_FULLNAME (t));
15572 return error_mark_node;
15573 }
15574 ctx = TREE_VEC_ELT (ctx, 0);
15575 }
15576 else
15577 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15578 /*entering_scope=*/1);
15579 if (ctx == error_mark_node)
15580 return error_mark_node;
15581
15582 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15583 complain, in_decl);
15584 if (f == error_mark_node)
15585 return error_mark_node;
15586
15587 if (!MAYBE_CLASS_TYPE_P (ctx))
15588 {
15589 if (complain & tf_error)
15590 error ("%qT is not a class, struct, or union type", ctx);
15591 return error_mark_node;
15592 }
15593 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15594 {
15595 /* Normally, make_typename_type does not require that the CTX
15596 have complete type in order to allow things like:
15597
15598 template <class T> struct S { typename S<T>::X Y; };
15599
15600 But, such constructs have already been resolved by this
15601 point, so here CTX really should have complete type, unless
15602 it's a partial instantiation. */
15603 ctx = complete_type (ctx);
15604 if (!COMPLETE_TYPE_P (ctx))
15605 {
15606 if (complain & tf_error)
15607 cxx_incomplete_type_error (NULL_TREE, ctx);
15608 return error_mark_node;
15609 }
15610 }
15611
15612 f = make_typename_type (ctx, f, typename_type,
15613 complain | tf_keep_type_decl);
15614 if (f == error_mark_node)
15615 return f;
15616 if (TREE_CODE (f) == TYPE_DECL)
15617 {
15618 complain |= tf_ignore_bad_quals;
15619 f = TREE_TYPE (f);
15620 }
15621
15622 if (TREE_CODE (f) != TYPENAME_TYPE)
15623 {
15624 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15625 {
15626 if (complain & tf_error)
15627 error ("%qT resolves to %qT, which is not an enumeration type",
15628 t, f);
15629 else
15630 return error_mark_node;
15631 }
15632 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15633 {
15634 if (complain & tf_error)
15635 error ("%qT resolves to %qT, which is is not a class type",
15636 t, f);
15637 else
15638 return error_mark_node;
15639 }
15640 }
15641
15642 return cp_build_qualified_type_real
15643 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15644 }
15645
15646 case UNBOUND_CLASS_TEMPLATE:
15647 {
15648 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15649 in_decl, /*entering_scope=*/1);
15650 tree name = TYPE_IDENTIFIER (t);
15651 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15652
15653 if (ctx == error_mark_node || name == error_mark_node)
15654 return error_mark_node;
15655
15656 if (parm_list)
15657 parm_list = tsubst_template_parms (parm_list, args, complain);
15658 return make_unbound_class_template (ctx, name, parm_list, complain);
15659 }
15660
15661 case TYPEOF_TYPE:
15662 {
15663 tree type;
15664
15665 ++cp_unevaluated_operand;
15666 ++c_inhibit_evaluation_warnings;
15667
15668 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15669 complain, in_decl,
15670 /*integral_constant_expression_p=*/false);
15671
15672 --cp_unevaluated_operand;
15673 --c_inhibit_evaluation_warnings;
15674
15675 type = finish_typeof (type);
15676 return cp_build_qualified_type_real (type,
15677 cp_type_quals (t)
15678 | cp_type_quals (type),
15679 complain);
15680 }
15681
15682 case DECLTYPE_TYPE:
15683 {
15684 tree type;
15685
15686 ++cp_unevaluated_operand;
15687 ++c_inhibit_evaluation_warnings;
15688
15689 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15690 complain|tf_decltype, in_decl,
15691 /*function_p*/false,
15692 /*integral_constant_expression*/false);
15693
15694 --cp_unevaluated_operand;
15695 --c_inhibit_evaluation_warnings;
15696
15697 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15698 type = lambda_capture_field_type (type,
15699 false /*explicit_init*/,
15700 DECLTYPE_FOR_REF_CAPTURE (t));
15701 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15702 type = lambda_proxy_type (type);
15703 else
15704 {
15705 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15706 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15707 && EXPR_P (type))
15708 /* In a template ~id could be either a complement expression
15709 or an unqualified-id naming a destructor; if instantiating
15710 it produces an expression, it's not an id-expression or
15711 member access. */
15712 id = false;
15713 type = finish_decltype_type (type, id, complain);
15714 }
15715 return cp_build_qualified_type_real (type,
15716 cp_type_quals (t)
15717 | cp_type_quals (type),
15718 complain | tf_ignore_bad_quals);
15719 }
15720
15721 case UNDERLYING_TYPE:
15722 {
15723 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15724 complain, in_decl);
15725 return finish_underlying_type (type);
15726 }
15727
15728 case TYPE_ARGUMENT_PACK:
15729 case NONTYPE_ARGUMENT_PACK:
15730 {
15731 tree r;
15732
15733 if (code == NONTYPE_ARGUMENT_PACK)
15734 r = make_node (code);
15735 else
15736 r = cxx_make_type (code);
15737
15738 tree pack_args = ARGUMENT_PACK_ARGS (t);
15739 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15740 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15741
15742 return r;
15743 }
15744
15745 case VOID_CST:
15746 case INTEGER_CST:
15747 case REAL_CST:
15748 case STRING_CST:
15749 case PLUS_EXPR:
15750 case MINUS_EXPR:
15751 case NEGATE_EXPR:
15752 case NOP_EXPR:
15753 case INDIRECT_REF:
15754 case ADDR_EXPR:
15755 case CALL_EXPR:
15756 case ARRAY_REF:
15757 case SCOPE_REF:
15758 /* We should use one of the expression tsubsts for these codes. */
15759 gcc_unreachable ();
15760
15761 default:
15762 sorry ("use of %qs in template", get_tree_code_name (code));
15763 return error_mark_node;
15764 }
15765 }
15766
15767 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15768 expression on the left-hand side of the "." or "->" operator. We
15769 only do the lookup if we had a dependent BASELINK. Otherwise we
15770 adjust it onto the instantiated heirarchy. */
15771
15772 static tree
15773 tsubst_baselink (tree baselink, tree object_type,
15774 tree args, tsubst_flags_t complain, tree in_decl)
15775 {
15776 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15777 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15778 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15779
15780 tree optype = BASELINK_OPTYPE (baselink);
15781 optype = tsubst (optype, args, complain, in_decl);
15782
15783 tree template_args = NULL_TREE;
15784 bool template_id_p = false;
15785 tree fns = BASELINK_FUNCTIONS (baselink);
15786 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15787 {
15788 template_id_p = true;
15789 template_args = TREE_OPERAND (fns, 1);
15790 fns = TREE_OPERAND (fns, 0);
15791 if (template_args)
15792 template_args = tsubst_template_args (template_args, args,
15793 complain, in_decl);
15794 }
15795
15796 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15797 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15798 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15799
15800 if (dependent_p)
15801 {
15802 tree name = OVL_NAME (fns);
15803 if (IDENTIFIER_CONV_OP_P (name))
15804 name = make_conv_op_name (optype);
15805
15806 if (name == complete_dtor_identifier)
15807 /* Treat as-if non-dependent below. */
15808 dependent_p = false;
15809
15810 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15811 if (!baselink)
15812 {
15813 if ((complain & tf_error)
15814 && constructor_name_p (name, qualifying_scope))
15815 error ("cannot call constructor %<%T::%D%> directly",
15816 qualifying_scope, name);
15817 return error_mark_node;
15818 }
15819
15820 if (BASELINK_P (baselink))
15821 fns = BASELINK_FUNCTIONS (baselink);
15822 }
15823 else
15824 /* We're going to overwrite pieces below, make a duplicate. */
15825 baselink = copy_node (baselink);
15826
15827 /* If lookup found a single function, mark it as used at this point.
15828 (If lookup found multiple functions the one selected later by
15829 overload resolution will be marked as used at that point.) */
15830 if (!template_id_p && !really_overloaded_fn (fns))
15831 {
15832 tree fn = OVL_FIRST (fns);
15833 bool ok = mark_used (fn, complain);
15834 if (!ok && !(complain & tf_error))
15835 return error_mark_node;
15836 if (ok && BASELINK_P (baselink))
15837 /* We might have instantiated an auto function. */
15838 TREE_TYPE (baselink) = TREE_TYPE (fn);
15839 }
15840
15841 if (BASELINK_P (baselink))
15842 {
15843 /* Add back the template arguments, if present. */
15844 if (template_id_p)
15845 BASELINK_FUNCTIONS (baselink)
15846 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15847
15848 /* Update the conversion operator type. */
15849 BASELINK_OPTYPE (baselink) = optype;
15850 }
15851
15852 if (!object_type)
15853 object_type = current_class_type;
15854
15855 if (qualified_p || !dependent_p)
15856 {
15857 baselink = adjust_result_of_qualified_name_lookup (baselink,
15858 qualifying_scope,
15859 object_type);
15860 if (!qualified_p)
15861 /* We need to call adjust_result_of_qualified_name_lookup in case the
15862 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15863 so that we still get virtual function binding. */
15864 BASELINK_QUALIFIED_P (baselink) = false;
15865 }
15866
15867 return baselink;
15868 }
15869
15870 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15871 true if the qualified-id will be a postfix-expression in-and-of
15872 itself; false if more of the postfix-expression follows the
15873 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15874 of "&". */
15875
15876 static tree
15877 tsubst_qualified_id (tree qualified_id, tree args,
15878 tsubst_flags_t complain, tree in_decl,
15879 bool done, bool address_p)
15880 {
15881 tree expr;
15882 tree scope;
15883 tree name;
15884 bool is_template;
15885 tree template_args;
15886 location_t loc = UNKNOWN_LOCATION;
15887
15888 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15889
15890 /* Figure out what name to look up. */
15891 name = TREE_OPERAND (qualified_id, 1);
15892 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15893 {
15894 is_template = true;
15895 loc = EXPR_LOCATION (name);
15896 template_args = TREE_OPERAND (name, 1);
15897 if (template_args)
15898 template_args = tsubst_template_args (template_args, args,
15899 complain, in_decl);
15900 if (template_args == error_mark_node)
15901 return error_mark_node;
15902 name = TREE_OPERAND (name, 0);
15903 }
15904 else
15905 {
15906 is_template = false;
15907 template_args = NULL_TREE;
15908 }
15909
15910 /* Substitute into the qualifying scope. When there are no ARGS, we
15911 are just trying to simplify a non-dependent expression. In that
15912 case the qualifying scope may be dependent, and, in any case,
15913 substituting will not help. */
15914 scope = TREE_OPERAND (qualified_id, 0);
15915 if (args)
15916 {
15917 scope = tsubst (scope, args, complain, in_decl);
15918 expr = tsubst_copy (name, args, complain, in_decl);
15919 }
15920 else
15921 expr = name;
15922
15923 if (dependent_scope_p (scope))
15924 {
15925 if (is_template)
15926 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15927 tree r = build_qualified_name (NULL_TREE, scope, expr,
15928 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15929 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15930 return r;
15931 }
15932
15933 if (!BASELINK_P (name) && !DECL_P (expr))
15934 {
15935 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15936 {
15937 /* A BIT_NOT_EXPR is used to represent a destructor. */
15938 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15939 {
15940 error ("qualifying type %qT does not match destructor name ~%qT",
15941 scope, TREE_OPERAND (expr, 0));
15942 expr = error_mark_node;
15943 }
15944 else
15945 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15946 /*is_type_p=*/0, false);
15947 }
15948 else
15949 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15950 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15951 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15952 {
15953 if (complain & tf_error)
15954 {
15955 error ("dependent-name %qE is parsed as a non-type, but "
15956 "instantiation yields a type", qualified_id);
15957 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15958 }
15959 return error_mark_node;
15960 }
15961 }
15962
15963 if (DECL_P (expr))
15964 {
15965 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15966 scope);
15967 /* Remember that there was a reference to this entity. */
15968 if (!mark_used (expr, complain) && !(complain & tf_error))
15969 return error_mark_node;
15970 }
15971
15972 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15973 {
15974 if (complain & tf_error)
15975 qualified_name_lookup_error (scope,
15976 TREE_OPERAND (qualified_id, 1),
15977 expr, input_location);
15978 return error_mark_node;
15979 }
15980
15981 if (is_template)
15982 {
15983 /* We may be repeating a check already done during parsing, but
15984 if it was well-formed and passed then, it will pass again
15985 now, and if it didn't, we wouldn't have got here. The case
15986 we want to catch is when we couldn't tell then, and can now,
15987 namely when templ prior to substitution was an
15988 identifier. */
15989 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15990 return error_mark_node;
15991
15992 if (variable_template_p (expr))
15993 expr = lookup_and_finish_template_variable (expr, template_args,
15994 complain);
15995 else
15996 expr = lookup_template_function (expr, template_args);
15997 }
15998
15999 if (expr == error_mark_node && complain & tf_error)
16000 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16001 expr, input_location);
16002 else if (TYPE_P (scope))
16003 {
16004 expr = (adjust_result_of_qualified_name_lookup
16005 (expr, scope, current_nonlambda_class_type ()));
16006 expr = (finish_qualified_id_expr
16007 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16008 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16009 /*template_arg_p=*/false, complain));
16010 }
16011
16012 /* Expressions do not generally have reference type. */
16013 if (TREE_CODE (expr) != SCOPE_REF
16014 /* However, if we're about to form a pointer-to-member, we just
16015 want the referenced member referenced. */
16016 && TREE_CODE (expr) != OFFSET_REF)
16017 expr = convert_from_reference (expr);
16018
16019 if (REF_PARENTHESIZED_P (qualified_id))
16020 expr = force_paren_expr (expr);
16021
16022 return expr;
16023 }
16024
16025 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16026 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16027 for tsubst. */
16028
16029 static tree
16030 tsubst_init (tree init, tree decl, tree args,
16031 tsubst_flags_t complain, tree in_decl)
16032 {
16033 if (!init)
16034 return NULL_TREE;
16035
16036 init = tsubst_expr (init, args, complain, in_decl, false);
16037
16038 tree type = TREE_TYPE (decl);
16039
16040 if (!init && type != error_mark_node)
16041 {
16042 if (tree auto_node = type_uses_auto (type))
16043 {
16044 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16045 {
16046 if (complain & tf_error)
16047 error ("initializer for %q#D expands to an empty list "
16048 "of expressions", decl);
16049 return error_mark_node;
16050 }
16051 }
16052 else if (!dependent_type_p (type))
16053 {
16054 /* If we had an initializer but it
16055 instantiated to nothing,
16056 value-initialize the object. This will
16057 only occur when the initializer was a
16058 pack expansion where the parameter packs
16059 used in that expansion were of length
16060 zero. */
16061 init = build_value_init (type, complain);
16062 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16063 init = get_target_expr_sfinae (init, complain);
16064 if (TREE_CODE (init) == TARGET_EXPR)
16065 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16066 }
16067 }
16068
16069 return init;
16070 }
16071
16072 /* Like tsubst, but deals with expressions. This function just replaces
16073 template parms; to finish processing the resultant expression, use
16074 tsubst_copy_and_build or tsubst_expr. */
16075
16076 static tree
16077 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16078 {
16079 enum tree_code code;
16080 tree r;
16081
16082 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16083 return t;
16084
16085 code = TREE_CODE (t);
16086
16087 switch (code)
16088 {
16089 case PARM_DECL:
16090 r = retrieve_local_specialization (t);
16091
16092 if (r == NULL_TREE)
16093 {
16094 /* We get here for a use of 'this' in an NSDMI. */
16095 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16096 return current_class_ptr;
16097
16098 /* This can happen for a parameter name used later in a function
16099 declaration (such as in a late-specified return type). Just
16100 make a dummy decl, since it's only used for its type. */
16101 gcc_assert (cp_unevaluated_operand != 0);
16102 r = tsubst_decl (t, args, complain);
16103 /* Give it the template pattern as its context; its true context
16104 hasn't been instantiated yet and this is good enough for
16105 mangling. */
16106 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16107 }
16108
16109 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16110 r = argument_pack_select_arg (r);
16111 if (!mark_used (r, complain) && !(complain & tf_error))
16112 return error_mark_node;
16113 return r;
16114
16115 case CONST_DECL:
16116 {
16117 tree enum_type;
16118 tree v;
16119
16120 if (DECL_TEMPLATE_PARM_P (t))
16121 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16122 /* There is no need to substitute into namespace-scope
16123 enumerators. */
16124 if (DECL_NAMESPACE_SCOPE_P (t))
16125 return t;
16126 /* If ARGS is NULL, then T is known to be non-dependent. */
16127 if (args == NULL_TREE)
16128 return scalar_constant_value (t);
16129
16130 /* Unfortunately, we cannot just call lookup_name here.
16131 Consider:
16132
16133 template <int I> int f() {
16134 enum E { a = I };
16135 struct S { void g() { E e = a; } };
16136 };
16137
16138 When we instantiate f<7>::S::g(), say, lookup_name is not
16139 clever enough to find f<7>::a. */
16140 enum_type
16141 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16142 /*entering_scope=*/0);
16143
16144 for (v = TYPE_VALUES (enum_type);
16145 v != NULL_TREE;
16146 v = TREE_CHAIN (v))
16147 if (TREE_PURPOSE (v) == DECL_NAME (t))
16148 return TREE_VALUE (v);
16149
16150 /* We didn't find the name. That should never happen; if
16151 name-lookup found it during preliminary parsing, we
16152 should find it again here during instantiation. */
16153 gcc_unreachable ();
16154 }
16155 return t;
16156
16157 case FIELD_DECL:
16158 if (DECL_CONTEXT (t))
16159 {
16160 tree ctx;
16161
16162 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16163 /*entering_scope=*/1);
16164 if (ctx != DECL_CONTEXT (t))
16165 {
16166 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16167 if (!r)
16168 {
16169 if (complain & tf_error)
16170 error ("using invalid field %qD", t);
16171 return error_mark_node;
16172 }
16173 return r;
16174 }
16175 }
16176
16177 return t;
16178
16179 case VAR_DECL:
16180 case FUNCTION_DECL:
16181 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16182 r = tsubst (t, args, complain, in_decl);
16183 else if (local_variable_p (t)
16184 && uses_template_parms (DECL_CONTEXT (t)))
16185 {
16186 r = retrieve_local_specialization (t);
16187 if (r == NULL_TREE)
16188 {
16189 /* First try name lookup to find the instantiation. */
16190 r = lookup_name (DECL_NAME (t));
16191 if (r)
16192 {
16193 if (!VAR_P (r))
16194 {
16195 /* During error-recovery we may find a non-variable,
16196 even an OVERLOAD: just bail out and avoid ICEs and
16197 duplicate diagnostics (c++/62207). */
16198 gcc_assert (seen_error ());
16199 return error_mark_node;
16200 }
16201 if (!is_capture_proxy (r))
16202 {
16203 /* Make sure the one we found is the one we want. */
16204 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16205 if (ctx != DECL_CONTEXT (r))
16206 r = NULL_TREE;
16207 }
16208 }
16209
16210 if (r)
16211 /* OK */;
16212 else
16213 {
16214 /* This can happen for a variable used in a
16215 late-specified return type of a local lambda, or for a
16216 local static or constant. Building a new VAR_DECL
16217 should be OK in all those cases. */
16218 r = tsubst_decl (t, args, complain);
16219 if (local_specializations)
16220 /* Avoid infinite recursion (79640). */
16221 register_local_specialization (r, t);
16222 if (decl_maybe_constant_var_p (r))
16223 {
16224 /* We can't call cp_finish_decl, so handle the
16225 initializer by hand. */
16226 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16227 complain, in_decl);
16228 if (!processing_template_decl)
16229 init = maybe_constant_init (init);
16230 if (processing_template_decl
16231 ? potential_constant_expression (init)
16232 : reduced_constant_expression_p (init))
16233 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16234 = TREE_CONSTANT (r) = true;
16235 DECL_INITIAL (r) = init;
16236 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16237 TREE_TYPE (r)
16238 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16239 complain, adc_variable_type);
16240 }
16241 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16242 || decl_constant_var_p (r)
16243 || seen_error ());
16244 if (!processing_template_decl
16245 && !TREE_STATIC (r))
16246 r = process_outer_var_ref (r, complain);
16247 }
16248 /* Remember this for subsequent uses. */
16249 if (local_specializations)
16250 register_local_specialization (r, t);
16251 }
16252 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16253 r = argument_pack_select_arg (r);
16254 }
16255 else
16256 r = t;
16257 if (!mark_used (r, complain))
16258 return error_mark_node;
16259 return r;
16260
16261 case NAMESPACE_DECL:
16262 return t;
16263
16264 case OVERLOAD:
16265 return t;
16266
16267 case BASELINK:
16268 return tsubst_baselink (t, current_nonlambda_class_type (),
16269 args, complain, in_decl);
16270
16271 case TEMPLATE_DECL:
16272 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16273 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16274 args, complain, in_decl);
16275 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16276 return tsubst (t, args, complain, in_decl);
16277 else if (DECL_CLASS_SCOPE_P (t)
16278 && uses_template_parms (DECL_CONTEXT (t)))
16279 {
16280 /* Template template argument like the following example need
16281 special treatment:
16282
16283 template <template <class> class TT> struct C {};
16284 template <class T> struct D {
16285 template <class U> struct E {};
16286 C<E> c; // #1
16287 };
16288 D<int> d; // #2
16289
16290 We are processing the template argument `E' in #1 for
16291 the template instantiation #2. Originally, `E' is a
16292 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16293 have to substitute this with one having context `D<int>'. */
16294
16295 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16296 if (dependent_scope_p (context))
16297 {
16298 /* When rewriting a constructor into a deduction guide, a
16299 non-dependent name can become dependent, so memtmpl<args>
16300 becomes context::template memtmpl<args>. */
16301 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16302 return build_qualified_name (type, context, DECL_NAME (t),
16303 /*template*/true);
16304 }
16305 return lookup_field (context, DECL_NAME(t), 0, false);
16306 }
16307 else
16308 /* Ordinary template template argument. */
16309 return t;
16310
16311 case NON_LVALUE_EXPR:
16312 case VIEW_CONVERT_EXPR:
16313 {
16314 /* Handle location wrappers by substituting the wrapped node
16315 first, *then* reusing the resulting type. Doing the type
16316 first ensures that we handle template parameters and
16317 parameter pack expansions. */
16318 if (location_wrapper_p (t))
16319 {
16320 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16321 complain, in_decl);
16322 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16323 }
16324 tree op = TREE_OPERAND (t, 0);
16325 if (code == VIEW_CONVERT_EXPR
16326 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16327 {
16328 /* Wrapper to make a C++20 template parameter object const. */
16329 op = tsubst_copy (op, args, complain, in_decl);
16330 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16331 {
16332 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16333 return build1 (code, type, op);
16334 }
16335 else
16336 {
16337 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
16338 return op;
16339 }
16340 }
16341 /* We shouldn't see any other uses of these in templates. */
16342 gcc_unreachable ();
16343 }
16344
16345 case CAST_EXPR:
16346 case REINTERPRET_CAST_EXPR:
16347 case CONST_CAST_EXPR:
16348 case STATIC_CAST_EXPR:
16349 case DYNAMIC_CAST_EXPR:
16350 case IMPLICIT_CONV_EXPR:
16351 case CONVERT_EXPR:
16352 case NOP_EXPR:
16353 {
16354 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16355 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16356 return build1 (code, type, op0);
16357 }
16358
16359 case SIZEOF_EXPR:
16360 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16361 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16362 {
16363 tree expanded, op = TREE_OPERAND (t, 0);
16364 int len = 0;
16365
16366 if (SIZEOF_EXPR_TYPE_P (t))
16367 op = TREE_TYPE (op);
16368
16369 ++cp_unevaluated_operand;
16370 ++c_inhibit_evaluation_warnings;
16371 /* We only want to compute the number of arguments. */
16372 if (PACK_EXPANSION_P (op))
16373 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16374 else
16375 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16376 args, complain, in_decl);
16377 --cp_unevaluated_operand;
16378 --c_inhibit_evaluation_warnings;
16379
16380 if (TREE_CODE (expanded) == TREE_VEC)
16381 {
16382 len = TREE_VEC_LENGTH (expanded);
16383 /* Set TREE_USED for the benefit of -Wunused. */
16384 for (int i = 0; i < len; i++)
16385 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16386 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16387 }
16388
16389 if (expanded == error_mark_node)
16390 return error_mark_node;
16391 else if (PACK_EXPANSION_P (expanded)
16392 || (TREE_CODE (expanded) == TREE_VEC
16393 && pack_expansion_args_count (expanded)))
16394
16395 {
16396 if (PACK_EXPANSION_P (expanded))
16397 /* OK. */;
16398 else if (TREE_VEC_LENGTH (expanded) == 1)
16399 expanded = TREE_VEC_ELT (expanded, 0);
16400 else
16401 expanded = make_argument_pack (expanded);
16402
16403 if (TYPE_P (expanded))
16404 return cxx_sizeof_or_alignof_type (input_location,
16405 expanded, SIZEOF_EXPR,
16406 false,
16407 complain & tf_error);
16408 else
16409 return cxx_sizeof_or_alignof_expr (input_location,
16410 expanded, SIZEOF_EXPR,
16411 complain & tf_error);
16412 }
16413 else
16414 return build_int_cst (size_type_node, len);
16415 }
16416 if (SIZEOF_EXPR_TYPE_P (t))
16417 {
16418 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16419 args, complain, in_decl);
16420 r = build1 (NOP_EXPR, r, error_mark_node);
16421 r = build1 (SIZEOF_EXPR,
16422 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16423 SIZEOF_EXPR_TYPE_P (r) = 1;
16424 return r;
16425 }
16426 /* Fall through */
16427
16428 case INDIRECT_REF:
16429 case NEGATE_EXPR:
16430 case TRUTH_NOT_EXPR:
16431 case BIT_NOT_EXPR:
16432 case ADDR_EXPR:
16433 case UNARY_PLUS_EXPR: /* Unary + */
16434 case ALIGNOF_EXPR:
16435 case AT_ENCODE_EXPR:
16436 case ARROW_EXPR:
16437 case THROW_EXPR:
16438 case TYPEID_EXPR:
16439 case REALPART_EXPR:
16440 case IMAGPART_EXPR:
16441 case PAREN_EXPR:
16442 {
16443 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16444 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16445 r = build1 (code, type, op0);
16446 if (code == ALIGNOF_EXPR)
16447 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16448 return r;
16449 }
16450
16451 case COMPONENT_REF:
16452 {
16453 tree object;
16454 tree name;
16455
16456 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16457 name = TREE_OPERAND (t, 1);
16458 if (TREE_CODE (name) == BIT_NOT_EXPR)
16459 {
16460 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16461 complain, in_decl);
16462 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16463 }
16464 else if (TREE_CODE (name) == SCOPE_REF
16465 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16466 {
16467 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16468 complain, in_decl);
16469 name = TREE_OPERAND (name, 1);
16470 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16471 complain, in_decl);
16472 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16473 name = build_qualified_name (/*type=*/NULL_TREE,
16474 base, name,
16475 /*template_p=*/false);
16476 }
16477 else if (BASELINK_P (name))
16478 name = tsubst_baselink (name,
16479 non_reference (TREE_TYPE (object)),
16480 args, complain,
16481 in_decl);
16482 else
16483 name = tsubst_copy (name, args, complain, in_decl);
16484 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16485 }
16486
16487 case PLUS_EXPR:
16488 case MINUS_EXPR:
16489 case MULT_EXPR:
16490 case TRUNC_DIV_EXPR:
16491 case CEIL_DIV_EXPR:
16492 case FLOOR_DIV_EXPR:
16493 case ROUND_DIV_EXPR:
16494 case EXACT_DIV_EXPR:
16495 case BIT_AND_EXPR:
16496 case BIT_IOR_EXPR:
16497 case BIT_XOR_EXPR:
16498 case TRUNC_MOD_EXPR:
16499 case FLOOR_MOD_EXPR:
16500 case TRUTH_ANDIF_EXPR:
16501 case TRUTH_ORIF_EXPR:
16502 case TRUTH_AND_EXPR:
16503 case TRUTH_OR_EXPR:
16504 case RSHIFT_EXPR:
16505 case LSHIFT_EXPR:
16506 case EQ_EXPR:
16507 case NE_EXPR:
16508 case MAX_EXPR:
16509 case MIN_EXPR:
16510 case LE_EXPR:
16511 case GE_EXPR:
16512 case LT_EXPR:
16513 case GT_EXPR:
16514 case COMPOUND_EXPR:
16515 case DOTSTAR_EXPR:
16516 case MEMBER_REF:
16517 case PREDECREMENT_EXPR:
16518 case PREINCREMENT_EXPR:
16519 case POSTDECREMENT_EXPR:
16520 case POSTINCREMENT_EXPR:
16521 {
16522 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16523 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16524 return build_nt (code, op0, op1);
16525 }
16526
16527 case SCOPE_REF:
16528 {
16529 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16530 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16531 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16532 QUALIFIED_NAME_IS_TEMPLATE (t));
16533 }
16534
16535 case ARRAY_REF:
16536 {
16537 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16538 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16539 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16540 }
16541
16542 case CALL_EXPR:
16543 {
16544 int n = VL_EXP_OPERAND_LENGTH (t);
16545 tree result = build_vl_exp (CALL_EXPR, n);
16546 int i;
16547 for (i = 0; i < n; i++)
16548 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16549 complain, in_decl);
16550 return result;
16551 }
16552
16553 case COND_EXPR:
16554 case MODOP_EXPR:
16555 case PSEUDO_DTOR_EXPR:
16556 case VEC_PERM_EXPR:
16557 {
16558 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16559 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16560 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16561 r = build_nt (code, op0, op1, op2);
16562 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16563 return r;
16564 }
16565
16566 case NEW_EXPR:
16567 {
16568 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16569 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16570 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16571 r = build_nt (code, op0, op1, op2);
16572 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16573 return r;
16574 }
16575
16576 case DELETE_EXPR:
16577 {
16578 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16579 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16580 r = build_nt (code, op0, op1);
16581 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16582 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16583 return r;
16584 }
16585
16586 case TEMPLATE_ID_EXPR:
16587 {
16588 /* Substituted template arguments */
16589 tree fn = TREE_OPERAND (t, 0);
16590 tree targs = TREE_OPERAND (t, 1);
16591
16592 fn = tsubst_copy (fn, args, complain, in_decl);
16593 if (targs)
16594 targs = tsubst_template_args (targs, args, complain, in_decl);
16595
16596 return lookup_template_function (fn, targs);
16597 }
16598
16599 case TREE_LIST:
16600 {
16601 tree purpose, value, chain;
16602
16603 if (t == void_list_node)
16604 return t;
16605
16606 purpose = TREE_PURPOSE (t);
16607 if (purpose)
16608 purpose = tsubst_copy (purpose, args, complain, in_decl);
16609 value = TREE_VALUE (t);
16610 if (value)
16611 value = tsubst_copy (value, args, complain, in_decl);
16612 chain = TREE_CHAIN (t);
16613 if (chain && chain != void_type_node)
16614 chain = tsubst_copy (chain, args, complain, in_decl);
16615 if (purpose == TREE_PURPOSE (t)
16616 && value == TREE_VALUE (t)
16617 && chain == TREE_CHAIN (t))
16618 return t;
16619 return tree_cons (purpose, value, chain);
16620 }
16621
16622 case RECORD_TYPE:
16623 case UNION_TYPE:
16624 case ENUMERAL_TYPE:
16625 case INTEGER_TYPE:
16626 case TEMPLATE_TYPE_PARM:
16627 case TEMPLATE_TEMPLATE_PARM:
16628 case BOUND_TEMPLATE_TEMPLATE_PARM:
16629 case TEMPLATE_PARM_INDEX:
16630 case POINTER_TYPE:
16631 case REFERENCE_TYPE:
16632 case OFFSET_TYPE:
16633 case FUNCTION_TYPE:
16634 case METHOD_TYPE:
16635 case ARRAY_TYPE:
16636 case TYPENAME_TYPE:
16637 case UNBOUND_CLASS_TEMPLATE:
16638 case TYPEOF_TYPE:
16639 case DECLTYPE_TYPE:
16640 case TYPE_DECL:
16641 return tsubst (t, args, complain, in_decl);
16642
16643 case USING_DECL:
16644 t = DECL_NAME (t);
16645 /* Fall through. */
16646 case IDENTIFIER_NODE:
16647 if (IDENTIFIER_CONV_OP_P (t))
16648 {
16649 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16650 return make_conv_op_name (new_type);
16651 }
16652 else
16653 return t;
16654
16655 case CONSTRUCTOR:
16656 /* This is handled by tsubst_copy_and_build. */
16657 gcc_unreachable ();
16658
16659 case VA_ARG_EXPR:
16660 {
16661 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16662 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16663 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16664 }
16665
16666 case CLEANUP_POINT_EXPR:
16667 /* We shouldn't have built any of these during initial template
16668 generation. Instead, they should be built during instantiation
16669 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16670 gcc_unreachable ();
16671
16672 case OFFSET_REF:
16673 {
16674 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16675 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16676 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16677 r = build2 (code, type, op0, op1);
16678 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16679 if (!mark_used (TREE_OPERAND (r, 1), complain)
16680 && !(complain & tf_error))
16681 return error_mark_node;
16682 return r;
16683 }
16684
16685 case EXPR_PACK_EXPANSION:
16686 error ("invalid use of pack expansion expression");
16687 return error_mark_node;
16688
16689 case NONTYPE_ARGUMENT_PACK:
16690 error ("use %<...%> to expand argument pack");
16691 return error_mark_node;
16692
16693 case VOID_CST:
16694 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16695 return t;
16696
16697 case INTEGER_CST:
16698 case REAL_CST:
16699 case STRING_CST:
16700 case COMPLEX_CST:
16701 {
16702 /* Instantiate any typedefs in the type. */
16703 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16704 r = fold_convert (type, t);
16705 gcc_assert (TREE_CODE (r) == code);
16706 return r;
16707 }
16708
16709 case PTRMEM_CST:
16710 /* These can sometimes show up in a partial instantiation, but never
16711 involve template parms. */
16712 gcc_assert (!uses_template_parms (t));
16713 return t;
16714
16715 case UNARY_LEFT_FOLD_EXPR:
16716 return tsubst_unary_left_fold (t, args, complain, in_decl);
16717 case UNARY_RIGHT_FOLD_EXPR:
16718 return tsubst_unary_right_fold (t, args, complain, in_decl);
16719 case BINARY_LEFT_FOLD_EXPR:
16720 return tsubst_binary_left_fold (t, args, complain, in_decl);
16721 case BINARY_RIGHT_FOLD_EXPR:
16722 return tsubst_binary_right_fold (t, args, complain, in_decl);
16723 case PREDICT_EXPR:
16724 return t;
16725
16726 case DEBUG_BEGIN_STMT:
16727 /* ??? There's no point in copying it for now, but maybe some
16728 day it will contain more information, such as a pointer back
16729 to the containing function, inlined copy or so. */
16730 return t;
16731
16732 default:
16733 /* We shouldn't get here, but keep going if !flag_checking. */
16734 if (flag_checking)
16735 gcc_unreachable ();
16736 return t;
16737 }
16738 }
16739
16740 /* Helper function for tsubst_omp_clauses, used for instantiation of
16741 OMP_CLAUSE_DECL of clauses. */
16742
16743 static tree
16744 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16745 tree in_decl, tree *iterator_cache)
16746 {
16747 if (decl == NULL_TREE)
16748 return NULL_TREE;
16749
16750 /* Handle OpenMP iterators. */
16751 if (TREE_CODE (decl) == TREE_LIST
16752 && TREE_PURPOSE (decl)
16753 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16754 {
16755 tree ret;
16756 if (iterator_cache[0] == TREE_PURPOSE (decl))
16757 ret = iterator_cache[1];
16758 else
16759 {
16760 tree *tp = &ret;
16761 begin_scope (sk_omp, NULL);
16762 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16763 {
16764 *tp = copy_node (it);
16765 TREE_VEC_ELT (*tp, 0)
16766 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16767 TREE_VEC_ELT (*tp, 1)
16768 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16769 /*integral_constant_expression_p=*/false);
16770 TREE_VEC_ELT (*tp, 2)
16771 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16772 /*integral_constant_expression_p=*/false);
16773 TREE_VEC_ELT (*tp, 3)
16774 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16775 /*integral_constant_expression_p=*/false);
16776 TREE_CHAIN (*tp) = NULL_TREE;
16777 tp = &TREE_CHAIN (*tp);
16778 }
16779 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16780 iterator_cache[0] = TREE_PURPOSE (decl);
16781 iterator_cache[1] = ret;
16782 }
16783 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16784 args, complain,
16785 in_decl, NULL));
16786 }
16787
16788 /* Handle an OpenMP array section represented as a TREE_LIST (or
16789 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16790 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16791 TREE_LIST. We can handle it exactly the same as an array section
16792 (purpose, value, and a chain), even though the nomenclature
16793 (low_bound, length, etc) is different. */
16794 if (TREE_CODE (decl) == TREE_LIST)
16795 {
16796 tree low_bound
16797 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16798 /*integral_constant_expression_p=*/false);
16799 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16800 /*integral_constant_expression_p=*/false);
16801 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16802 in_decl, NULL);
16803 if (TREE_PURPOSE (decl) == low_bound
16804 && TREE_VALUE (decl) == length
16805 && TREE_CHAIN (decl) == chain)
16806 return decl;
16807 tree ret = tree_cons (low_bound, length, chain);
16808 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16809 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16810 return ret;
16811 }
16812 tree ret = tsubst_expr (decl, args, complain, in_decl,
16813 /*integral_constant_expression_p=*/false);
16814 /* Undo convert_from_reference tsubst_expr could have called. */
16815 if (decl
16816 && REFERENCE_REF_P (ret)
16817 && !REFERENCE_REF_P (decl))
16818 ret = TREE_OPERAND (ret, 0);
16819 return ret;
16820 }
16821
16822 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16823
16824 static tree
16825 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16826 tree args, tsubst_flags_t complain, tree in_decl)
16827 {
16828 tree new_clauses = NULL_TREE, nc, oc;
16829 tree linear_no_step = NULL_TREE;
16830 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16831
16832 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16833 {
16834 nc = copy_node (oc);
16835 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16836 new_clauses = nc;
16837
16838 switch (OMP_CLAUSE_CODE (nc))
16839 {
16840 case OMP_CLAUSE_LASTPRIVATE:
16841 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16842 {
16843 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16844 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16845 in_decl, /*integral_constant_expression_p=*/false);
16846 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16847 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16848 }
16849 /* FALLTHRU */
16850 case OMP_CLAUSE_PRIVATE:
16851 case OMP_CLAUSE_SHARED:
16852 case OMP_CLAUSE_FIRSTPRIVATE:
16853 case OMP_CLAUSE_COPYIN:
16854 case OMP_CLAUSE_COPYPRIVATE:
16855 case OMP_CLAUSE_UNIFORM:
16856 case OMP_CLAUSE_DEPEND:
16857 case OMP_CLAUSE_FROM:
16858 case OMP_CLAUSE_TO:
16859 case OMP_CLAUSE_MAP:
16860 case OMP_CLAUSE_NONTEMPORAL:
16861 case OMP_CLAUSE_USE_DEVICE_PTR:
16862 case OMP_CLAUSE_USE_DEVICE_ADDR:
16863 case OMP_CLAUSE_IS_DEVICE_PTR:
16864 case OMP_CLAUSE_INCLUSIVE:
16865 case OMP_CLAUSE_EXCLUSIVE:
16866 OMP_CLAUSE_DECL (nc)
16867 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16868 in_decl, iterator_cache);
16869 break;
16870 case OMP_CLAUSE_TILE:
16871 case OMP_CLAUSE_IF:
16872 case OMP_CLAUSE_NUM_THREADS:
16873 case OMP_CLAUSE_SCHEDULE:
16874 case OMP_CLAUSE_COLLAPSE:
16875 case OMP_CLAUSE_FINAL:
16876 case OMP_CLAUSE_DEVICE:
16877 case OMP_CLAUSE_DIST_SCHEDULE:
16878 case OMP_CLAUSE_NUM_TEAMS:
16879 case OMP_CLAUSE_THREAD_LIMIT:
16880 case OMP_CLAUSE_SAFELEN:
16881 case OMP_CLAUSE_SIMDLEN:
16882 case OMP_CLAUSE_NUM_TASKS:
16883 case OMP_CLAUSE_GRAINSIZE:
16884 case OMP_CLAUSE_PRIORITY:
16885 case OMP_CLAUSE_ORDERED:
16886 case OMP_CLAUSE_HINT:
16887 case OMP_CLAUSE_NUM_GANGS:
16888 case OMP_CLAUSE_NUM_WORKERS:
16889 case OMP_CLAUSE_VECTOR_LENGTH:
16890 case OMP_CLAUSE_WORKER:
16891 case OMP_CLAUSE_VECTOR:
16892 case OMP_CLAUSE_ASYNC:
16893 case OMP_CLAUSE_WAIT:
16894 OMP_CLAUSE_OPERAND (nc, 0)
16895 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16896 in_decl, /*integral_constant_expression_p=*/false);
16897 break;
16898 case OMP_CLAUSE_REDUCTION:
16899 case OMP_CLAUSE_IN_REDUCTION:
16900 case OMP_CLAUSE_TASK_REDUCTION:
16901 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16902 {
16903 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16904 if (TREE_CODE (placeholder) == SCOPE_REF)
16905 {
16906 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16907 complain, in_decl);
16908 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16909 = build_qualified_name (NULL_TREE, scope,
16910 TREE_OPERAND (placeholder, 1),
16911 false);
16912 }
16913 else
16914 gcc_assert (identifier_p (placeholder));
16915 }
16916 OMP_CLAUSE_DECL (nc)
16917 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16918 in_decl, NULL);
16919 break;
16920 case OMP_CLAUSE_GANG:
16921 case OMP_CLAUSE_ALIGNED:
16922 OMP_CLAUSE_DECL (nc)
16923 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16924 in_decl, NULL);
16925 OMP_CLAUSE_OPERAND (nc, 1)
16926 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16927 in_decl, /*integral_constant_expression_p=*/false);
16928 break;
16929 case OMP_CLAUSE_LINEAR:
16930 OMP_CLAUSE_DECL (nc)
16931 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16932 in_decl, NULL);
16933 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16934 {
16935 gcc_assert (!linear_no_step);
16936 linear_no_step = nc;
16937 }
16938 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16939 OMP_CLAUSE_LINEAR_STEP (nc)
16940 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16941 complain, in_decl, NULL);
16942 else
16943 OMP_CLAUSE_LINEAR_STEP (nc)
16944 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16945 in_decl,
16946 /*integral_constant_expression_p=*/false);
16947 break;
16948 case OMP_CLAUSE_NOWAIT:
16949 case OMP_CLAUSE_DEFAULT:
16950 case OMP_CLAUSE_UNTIED:
16951 case OMP_CLAUSE_MERGEABLE:
16952 case OMP_CLAUSE_INBRANCH:
16953 case OMP_CLAUSE_NOTINBRANCH:
16954 case OMP_CLAUSE_PROC_BIND:
16955 case OMP_CLAUSE_FOR:
16956 case OMP_CLAUSE_PARALLEL:
16957 case OMP_CLAUSE_SECTIONS:
16958 case OMP_CLAUSE_TASKGROUP:
16959 case OMP_CLAUSE_NOGROUP:
16960 case OMP_CLAUSE_THREADS:
16961 case OMP_CLAUSE_SIMD:
16962 case OMP_CLAUSE_DEFAULTMAP:
16963 case OMP_CLAUSE_ORDER:
16964 case OMP_CLAUSE_BIND:
16965 case OMP_CLAUSE_INDEPENDENT:
16966 case OMP_CLAUSE_AUTO:
16967 case OMP_CLAUSE_SEQ:
16968 case OMP_CLAUSE_IF_PRESENT:
16969 case OMP_CLAUSE_FINALIZE:
16970 break;
16971 default:
16972 gcc_unreachable ();
16973 }
16974 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16975 switch (OMP_CLAUSE_CODE (nc))
16976 {
16977 case OMP_CLAUSE_SHARED:
16978 case OMP_CLAUSE_PRIVATE:
16979 case OMP_CLAUSE_FIRSTPRIVATE:
16980 case OMP_CLAUSE_LASTPRIVATE:
16981 case OMP_CLAUSE_COPYPRIVATE:
16982 case OMP_CLAUSE_LINEAR:
16983 case OMP_CLAUSE_REDUCTION:
16984 case OMP_CLAUSE_IN_REDUCTION:
16985 case OMP_CLAUSE_TASK_REDUCTION:
16986 case OMP_CLAUSE_USE_DEVICE_PTR:
16987 case OMP_CLAUSE_USE_DEVICE_ADDR:
16988 case OMP_CLAUSE_IS_DEVICE_PTR:
16989 case OMP_CLAUSE_INCLUSIVE:
16990 case OMP_CLAUSE_EXCLUSIVE:
16991 /* tsubst_expr on SCOPE_REF results in returning
16992 finish_non_static_data_member result. Undo that here. */
16993 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16994 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16995 == IDENTIFIER_NODE))
16996 {
16997 tree t = OMP_CLAUSE_DECL (nc);
16998 tree v = t;
16999 while (v)
17000 switch (TREE_CODE (v))
17001 {
17002 case COMPONENT_REF:
17003 case MEM_REF:
17004 case INDIRECT_REF:
17005 CASE_CONVERT:
17006 case POINTER_PLUS_EXPR:
17007 v = TREE_OPERAND (v, 0);
17008 continue;
17009 case PARM_DECL:
17010 if (DECL_CONTEXT (v) == current_function_decl
17011 && DECL_ARTIFICIAL (v)
17012 && DECL_NAME (v) == this_identifier)
17013 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17014 /* FALLTHRU */
17015 default:
17016 v = NULL_TREE;
17017 break;
17018 }
17019 }
17020 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17021 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17022 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17023 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17024 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17025 {
17026 tree decl = OMP_CLAUSE_DECL (nc);
17027 if (VAR_P (decl))
17028 {
17029 retrofit_lang_decl (decl);
17030 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17031 }
17032 }
17033 break;
17034 default:
17035 break;
17036 }
17037 }
17038
17039 new_clauses = nreverse (new_clauses);
17040 if (ort != C_ORT_OMP_DECLARE_SIMD)
17041 {
17042 new_clauses = finish_omp_clauses (new_clauses, ort);
17043 if (linear_no_step)
17044 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17045 if (nc == linear_no_step)
17046 {
17047 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17048 break;
17049 }
17050 }
17051 return new_clauses;
17052 }
17053
17054 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17055
17056 static tree
17057 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17058 tree in_decl)
17059 {
17060 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17061
17062 tree purpose, value, chain;
17063
17064 if (t == NULL)
17065 return t;
17066
17067 if (TREE_CODE (t) != TREE_LIST)
17068 return tsubst_copy_and_build (t, args, complain, in_decl,
17069 /*function_p=*/false,
17070 /*integral_constant_expression_p=*/false);
17071
17072 if (t == void_list_node)
17073 return t;
17074
17075 purpose = TREE_PURPOSE (t);
17076 if (purpose)
17077 purpose = RECUR (purpose);
17078 value = TREE_VALUE (t);
17079 if (value)
17080 {
17081 if (TREE_CODE (value) != LABEL_DECL)
17082 value = RECUR (value);
17083 else
17084 {
17085 value = lookup_label (DECL_NAME (value));
17086 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17087 TREE_USED (value) = 1;
17088 }
17089 }
17090 chain = TREE_CHAIN (t);
17091 if (chain && chain != void_type_node)
17092 chain = RECUR (chain);
17093 return tree_cons (purpose, value, chain);
17094 #undef RECUR
17095 }
17096
17097 /* Used to temporarily communicate the list of #pragma omp parallel
17098 clauses to #pragma omp for instantiation if they are combined
17099 together. */
17100
17101 static tree *omp_parallel_combined_clauses;
17102
17103 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17104 tree *, unsigned int *);
17105
17106 /* Substitute one OMP_FOR iterator. */
17107
17108 static bool
17109 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17110 tree initv, tree condv, tree incrv, tree *clauses,
17111 tree args, tsubst_flags_t complain, tree in_decl,
17112 bool integral_constant_expression_p)
17113 {
17114 #define RECUR(NODE) \
17115 tsubst_expr ((NODE), args, complain, in_decl, \
17116 integral_constant_expression_p)
17117 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17118 bool ret = false;
17119
17120 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17121 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17122
17123 decl = TREE_OPERAND (init, 0);
17124 init = TREE_OPERAND (init, 1);
17125 tree decl_expr = NULL_TREE;
17126 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17127 if (range_for)
17128 {
17129 bool decomp = false;
17130 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17131 {
17132 tree v = DECL_VALUE_EXPR (decl);
17133 if (TREE_CODE (v) == ARRAY_REF
17134 && VAR_P (TREE_OPERAND (v, 0))
17135 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17136 {
17137 tree decomp_first = NULL_TREE;
17138 unsigned decomp_cnt = 0;
17139 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17140 maybe_push_decl (d);
17141 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17142 in_decl, &decomp_first, &decomp_cnt);
17143 decomp = true;
17144 if (d == error_mark_node)
17145 decl = error_mark_node;
17146 else
17147 for (unsigned int i = 0; i < decomp_cnt; i++)
17148 {
17149 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17150 {
17151 tree v = build_nt (ARRAY_REF, d,
17152 size_int (decomp_cnt - i - 1),
17153 NULL_TREE, NULL_TREE);
17154 SET_DECL_VALUE_EXPR (decomp_first, v);
17155 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17156 }
17157 fit_decomposition_lang_decl (decomp_first, d);
17158 decomp_first = DECL_CHAIN (decomp_first);
17159 }
17160 }
17161 }
17162 decl = tsubst_decl (decl, args, complain);
17163 if (!decomp)
17164 maybe_push_decl (decl);
17165 }
17166 else if (init && TREE_CODE (init) == DECL_EXPR)
17167 {
17168 /* We need to jump through some hoops to handle declarations in the
17169 init-statement, since we might need to handle auto deduction,
17170 but we need to keep control of initialization. */
17171 decl_expr = init;
17172 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17173 decl = tsubst_decl (decl, args, complain);
17174 }
17175 else
17176 {
17177 if (TREE_CODE (decl) == SCOPE_REF)
17178 {
17179 decl = RECUR (decl);
17180 if (TREE_CODE (decl) == COMPONENT_REF)
17181 {
17182 tree v = decl;
17183 while (v)
17184 switch (TREE_CODE (v))
17185 {
17186 case COMPONENT_REF:
17187 case MEM_REF:
17188 case INDIRECT_REF:
17189 CASE_CONVERT:
17190 case POINTER_PLUS_EXPR:
17191 v = TREE_OPERAND (v, 0);
17192 continue;
17193 case PARM_DECL:
17194 if (DECL_CONTEXT (v) == current_function_decl
17195 && DECL_ARTIFICIAL (v)
17196 && DECL_NAME (v) == this_identifier)
17197 {
17198 decl = TREE_OPERAND (decl, 1);
17199 decl = omp_privatize_field (decl, false);
17200 }
17201 /* FALLTHRU */
17202 default:
17203 v = NULL_TREE;
17204 break;
17205 }
17206 }
17207 }
17208 else
17209 decl = RECUR (decl);
17210 }
17211 init = RECUR (init);
17212
17213 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17214 {
17215 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17216 if (TREE_CODE (o) == TREE_LIST)
17217 TREE_VEC_ELT (orig_declv, i)
17218 = tree_cons (RECUR (TREE_PURPOSE (o)),
17219 RECUR (TREE_VALUE (o)),
17220 NULL_TREE);
17221 else
17222 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17223 }
17224
17225 if (range_for)
17226 {
17227 tree this_pre_body = NULL_TREE;
17228 tree orig_init = NULL_TREE;
17229 tree orig_decl = NULL_TREE;
17230 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17231 orig_init, cond, incr);
17232 if (orig_decl)
17233 {
17234 if (orig_declv == NULL_TREE)
17235 orig_declv = copy_node (declv);
17236 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17237 ret = true;
17238 }
17239 else if (orig_declv)
17240 TREE_VEC_ELT (orig_declv, i) = decl;
17241 }
17242
17243 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17244 if (!range_for && auto_node && init)
17245 TREE_TYPE (decl)
17246 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17247
17248 gcc_assert (!type_dependent_expression_p (decl));
17249
17250 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17251 {
17252 if (decl_expr)
17253 {
17254 /* Declare the variable, but don't let that initialize it. */
17255 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17256 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17257 RECUR (decl_expr);
17258 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17259 }
17260
17261 if (!range_for)
17262 {
17263 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
17264 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17265 if (TREE_CODE (incr) == MODIFY_EXPR)
17266 {
17267 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17268 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17269 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17270 NOP_EXPR, rhs, complain);
17271 }
17272 else
17273 incr = RECUR (incr);
17274 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17275 TREE_VEC_ELT (orig_declv, i) = decl;
17276 }
17277 TREE_VEC_ELT (declv, i) = decl;
17278 TREE_VEC_ELT (initv, i) = init;
17279 TREE_VEC_ELT (condv, i) = cond;
17280 TREE_VEC_ELT (incrv, i) = incr;
17281 return ret;
17282 }
17283
17284 if (decl_expr)
17285 {
17286 /* Declare and initialize the variable. */
17287 RECUR (decl_expr);
17288 init = NULL_TREE;
17289 }
17290 else if (init)
17291 {
17292 tree *pc;
17293 int j;
17294 for (j = ((omp_parallel_combined_clauses == NULL
17295 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17296 {
17297 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17298 {
17299 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17300 && OMP_CLAUSE_DECL (*pc) == decl)
17301 break;
17302 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17303 && OMP_CLAUSE_DECL (*pc) == decl)
17304 {
17305 if (j)
17306 break;
17307 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17308 tree c = *pc;
17309 *pc = OMP_CLAUSE_CHAIN (c);
17310 OMP_CLAUSE_CHAIN (c) = *clauses;
17311 *clauses = c;
17312 }
17313 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17314 && OMP_CLAUSE_DECL (*pc) == decl)
17315 {
17316 error ("iteration variable %qD should not be firstprivate",
17317 decl);
17318 *pc = OMP_CLAUSE_CHAIN (*pc);
17319 }
17320 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17321 && OMP_CLAUSE_DECL (*pc) == decl)
17322 {
17323 error ("iteration variable %qD should not be reduction",
17324 decl);
17325 *pc = OMP_CLAUSE_CHAIN (*pc);
17326 }
17327 else
17328 pc = &OMP_CLAUSE_CHAIN (*pc);
17329 }
17330 if (*pc)
17331 break;
17332 }
17333 if (*pc == NULL_TREE)
17334 {
17335 tree c = build_omp_clause (input_location,
17336 TREE_CODE (t) == OMP_LOOP
17337 ? OMP_CLAUSE_LASTPRIVATE
17338 : OMP_CLAUSE_PRIVATE);
17339 OMP_CLAUSE_DECL (c) = decl;
17340 c = finish_omp_clauses (c, C_ORT_OMP);
17341 if (c)
17342 {
17343 OMP_CLAUSE_CHAIN (c) = *clauses;
17344 *clauses = c;
17345 }
17346 }
17347 }
17348 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17349 if (COMPARISON_CLASS_P (cond))
17350 {
17351 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17352 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17353 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17354 }
17355 else
17356 cond = RECUR (cond);
17357 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17358 switch (TREE_CODE (incr))
17359 {
17360 case PREINCREMENT_EXPR:
17361 case PREDECREMENT_EXPR:
17362 case POSTINCREMENT_EXPR:
17363 case POSTDECREMENT_EXPR:
17364 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17365 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17366 break;
17367 case MODIFY_EXPR:
17368 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17369 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17370 {
17371 tree rhs = TREE_OPERAND (incr, 1);
17372 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17373 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17374 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17375 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17376 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17377 rhs0, rhs1));
17378 }
17379 else
17380 incr = RECUR (incr);
17381 break;
17382 case MODOP_EXPR:
17383 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17384 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17385 {
17386 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17387 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17388 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17389 TREE_TYPE (decl), lhs,
17390 RECUR (TREE_OPERAND (incr, 2))));
17391 }
17392 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17393 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17394 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17395 {
17396 tree rhs = TREE_OPERAND (incr, 2);
17397 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17398 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17399 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17400 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17401 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17402 rhs0, rhs1));
17403 }
17404 else
17405 incr = RECUR (incr);
17406 break;
17407 default:
17408 incr = RECUR (incr);
17409 break;
17410 }
17411
17412 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17413 TREE_VEC_ELT (orig_declv, i) = decl;
17414 TREE_VEC_ELT (declv, i) = decl;
17415 TREE_VEC_ELT (initv, i) = init;
17416 TREE_VEC_ELT (condv, i) = cond;
17417 TREE_VEC_ELT (incrv, i) = incr;
17418 return false;
17419 #undef RECUR
17420 }
17421
17422 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17423 of OMP_TARGET's body. */
17424
17425 static tree
17426 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17427 {
17428 *walk_subtrees = 0;
17429 switch (TREE_CODE (*tp))
17430 {
17431 case OMP_TEAMS:
17432 return *tp;
17433 case BIND_EXPR:
17434 case STATEMENT_LIST:
17435 *walk_subtrees = 1;
17436 break;
17437 default:
17438 break;
17439 }
17440 return NULL_TREE;
17441 }
17442
17443 /* Helper function for tsubst_expr. For decomposition declaration
17444 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17445 also the corresponding decls representing the identifiers
17446 of the decomposition declaration. Return DECL if successful
17447 or error_mark_node otherwise, set *FIRST to the first decl
17448 in the list chained through DECL_CHAIN and *CNT to the number
17449 of such decls. */
17450
17451 static tree
17452 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17453 tsubst_flags_t complain, tree in_decl, tree *first,
17454 unsigned int *cnt)
17455 {
17456 tree decl2, decl3, prev = decl;
17457 *cnt = 0;
17458 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17459 for (decl2 = DECL_CHAIN (pattern_decl);
17460 decl2
17461 && VAR_P (decl2)
17462 && DECL_DECOMPOSITION_P (decl2)
17463 && DECL_NAME (decl2);
17464 decl2 = DECL_CHAIN (decl2))
17465 {
17466 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17467 {
17468 gcc_assert (errorcount);
17469 return error_mark_node;
17470 }
17471 (*cnt)++;
17472 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17473 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17474 tree v = DECL_VALUE_EXPR (decl2);
17475 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17476 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17477 decl3 = tsubst (decl2, args, complain, in_decl);
17478 SET_DECL_VALUE_EXPR (decl2, v);
17479 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17480 if (VAR_P (decl3))
17481 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17482 else
17483 {
17484 gcc_assert (errorcount);
17485 decl = error_mark_node;
17486 continue;
17487 }
17488 maybe_push_decl (decl3);
17489 if (error_operand_p (decl3))
17490 decl = error_mark_node;
17491 else if (decl != error_mark_node
17492 && DECL_CHAIN (decl3) != prev
17493 && decl != prev)
17494 {
17495 gcc_assert (errorcount);
17496 decl = error_mark_node;
17497 }
17498 else
17499 prev = decl3;
17500 }
17501 *first = prev;
17502 return decl;
17503 }
17504
17505 /* Return the proper local_specialization for init-capture pack DECL. */
17506
17507 static tree
17508 lookup_init_capture_pack (tree decl)
17509 {
17510 /* We handle normal pack captures by forwarding to the specialization of the
17511 captured parameter. We can't do that for pack init-captures; we need them
17512 to have their own local_specialization. We created the individual
17513 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17514 when we process the DECL_EXPR for the pack init-capture in the template.
17515 So, how do we find them? We don't know the capture proxy pack when
17516 building the individual resulting proxies, and we don't know the
17517 individual proxies when instantiating the pack. What we have in common is
17518 the FIELD_DECL.
17519
17520 So...when we instantiate the FIELD_DECL, we stick the result in
17521 local_specializations. Then at the DECL_EXPR we look up that result, see
17522 how many elements it has, synthesize the names, and look them up. */
17523
17524 tree cname = DECL_NAME (decl);
17525 tree val = DECL_VALUE_EXPR (decl);
17526 tree field = TREE_OPERAND (val, 1);
17527 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17528 tree fpack = retrieve_local_specialization (field);
17529 if (fpack == error_mark_node)
17530 return error_mark_node;
17531
17532 int len = 1;
17533 tree vec = NULL_TREE;
17534 tree r = NULL_TREE;
17535 if (TREE_CODE (fpack) == TREE_VEC)
17536 {
17537 len = TREE_VEC_LENGTH (fpack);
17538 vec = make_tree_vec (len);
17539 r = make_node (NONTYPE_ARGUMENT_PACK);
17540 SET_ARGUMENT_PACK_ARGS (r, vec);
17541 }
17542 for (int i = 0; i < len; ++i)
17543 {
17544 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17545 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17546 if (vec)
17547 TREE_VEC_ELT (vec, i) = elt;
17548 else
17549 r = elt;
17550 }
17551 return r;
17552 }
17553
17554 /* Like tsubst_copy for expressions, etc. but also does semantic
17555 processing. */
17556
17557 tree
17558 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17559 bool integral_constant_expression_p)
17560 {
17561 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17562 #define RECUR(NODE) \
17563 tsubst_expr ((NODE), args, complain, in_decl, \
17564 integral_constant_expression_p)
17565
17566 tree stmt, tmp;
17567 tree r;
17568 location_t loc;
17569
17570 if (t == NULL_TREE || t == error_mark_node)
17571 return t;
17572
17573 loc = input_location;
17574 if (location_t eloc = cp_expr_location (t))
17575 input_location = eloc;
17576 if (STATEMENT_CODE_P (TREE_CODE (t)))
17577 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17578
17579 switch (TREE_CODE (t))
17580 {
17581 case STATEMENT_LIST:
17582 {
17583 tree_stmt_iterator i;
17584 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17585 RECUR (tsi_stmt (i));
17586 break;
17587 }
17588
17589 case CTOR_INITIALIZER:
17590 finish_mem_initializers (tsubst_initializer_list
17591 (TREE_OPERAND (t, 0), args));
17592 break;
17593
17594 case RETURN_EXPR:
17595 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17596 break;
17597
17598 case EXPR_STMT:
17599 tmp = RECUR (EXPR_STMT_EXPR (t));
17600 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17601 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17602 else
17603 finish_expr_stmt (tmp);
17604 break;
17605
17606 case USING_STMT:
17607 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17608 break;
17609
17610 case DECL_EXPR:
17611 {
17612 tree decl, pattern_decl;
17613 tree init;
17614
17615 pattern_decl = decl = DECL_EXPR_DECL (t);
17616 if (TREE_CODE (decl) == LABEL_DECL)
17617 finish_label_decl (DECL_NAME (decl));
17618 else if (TREE_CODE (decl) == USING_DECL)
17619 {
17620 tree scope = USING_DECL_SCOPE (decl);
17621 tree name = DECL_NAME (decl);
17622
17623 scope = tsubst (scope, args, complain, in_decl);
17624 finish_nonmember_using_decl (scope, name);
17625 }
17626 else if (is_capture_proxy (decl)
17627 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17628 {
17629 /* We're in tsubst_lambda_expr, we've already inserted a new
17630 capture proxy, so look it up and register it. */
17631 tree inst;
17632 if (!DECL_PACK_P (decl))
17633 {
17634 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17635 /*nonclass*/1, /*block_p=*/true,
17636 /*ns_only*/0, LOOKUP_HIDDEN);
17637 gcc_assert (inst != decl && is_capture_proxy (inst));
17638 }
17639 else if (is_normal_capture_proxy (decl))
17640 {
17641 inst = (retrieve_local_specialization
17642 (DECL_CAPTURED_VARIABLE (decl)));
17643 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17644 }
17645 else
17646 inst = lookup_init_capture_pack (decl);
17647
17648 register_local_specialization (inst, decl);
17649 break;
17650 }
17651 else if (DECL_PRETTY_FUNCTION_P (decl))
17652 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17653 DECL_NAME (decl),
17654 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17655 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17656 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17657 /* Don't copy the old closure; we'll create a new one in
17658 tsubst_lambda_expr. */
17659 break;
17660 else
17661 {
17662 init = DECL_INITIAL (decl);
17663 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17664 for local variables, so save if DECL was declared constinit. */
17665 const bool constinit_p
17666 = (VAR_P (decl)
17667 && DECL_LANG_SPECIFIC (decl)
17668 && DECL_TEMPLATE_INFO (decl)
17669 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17670 decl = tsubst (decl, args, complain, in_decl);
17671 if (decl != error_mark_node)
17672 {
17673 /* By marking the declaration as instantiated, we avoid
17674 trying to instantiate it. Since instantiate_decl can't
17675 handle local variables, and since we've already done
17676 all that needs to be done, that's the right thing to
17677 do. */
17678 if (VAR_P (decl))
17679 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17680 if (VAR_P (decl) && !DECL_NAME (decl)
17681 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17682 /* Anonymous aggregates are a special case. */
17683 finish_anon_union (decl);
17684 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17685 {
17686 DECL_CONTEXT (decl) = current_function_decl;
17687 if (DECL_NAME (decl) == this_identifier)
17688 {
17689 tree lam = DECL_CONTEXT (current_function_decl);
17690 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17691 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17692 }
17693 insert_capture_proxy (decl);
17694 }
17695 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17696 /* We already did a pushtag. */;
17697 else if (TREE_CODE (decl) == FUNCTION_DECL
17698 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17699 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17700 {
17701 DECL_CONTEXT (decl) = NULL_TREE;
17702 pushdecl (decl);
17703 DECL_CONTEXT (decl) = current_function_decl;
17704 cp_check_omp_declare_reduction (decl);
17705 }
17706 else
17707 {
17708 bool const_init = false;
17709 unsigned int cnt = 0;
17710 tree first = NULL_TREE, ndecl = error_mark_node;
17711 maybe_push_decl (decl);
17712
17713 if (VAR_P (decl)
17714 && DECL_DECOMPOSITION_P (decl)
17715 && TREE_TYPE (pattern_decl) != error_mark_node)
17716 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17717 complain, in_decl, &first,
17718 &cnt);
17719
17720 init = tsubst_init (init, decl, args, complain, in_decl);
17721
17722 if (VAR_P (decl))
17723 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17724 (pattern_decl));
17725
17726 if (ndecl != error_mark_node)
17727 cp_maybe_mangle_decomp (ndecl, first, cnt);
17728
17729 cp_finish_decl (decl, init, const_init, NULL_TREE,
17730 constinit_p ? LOOKUP_CONSTINIT : 0);
17731
17732 if (ndecl != error_mark_node)
17733 cp_finish_decomp (ndecl, first, cnt);
17734 }
17735 }
17736 }
17737
17738 break;
17739 }
17740
17741 case FOR_STMT:
17742 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17743 RECUR (FOR_INIT_STMT (t));
17744 finish_init_stmt (stmt);
17745 tmp = RECUR (FOR_COND (t));
17746 finish_for_cond (tmp, stmt, false, 0);
17747 tmp = RECUR (FOR_EXPR (t));
17748 finish_for_expr (tmp, stmt);
17749 {
17750 bool prev = note_iteration_stmt_body_start ();
17751 RECUR (FOR_BODY (t));
17752 note_iteration_stmt_body_end (prev);
17753 }
17754 finish_for_stmt (stmt);
17755 break;
17756
17757 case RANGE_FOR_STMT:
17758 {
17759 /* Construct another range_for, if this is not a final
17760 substitution (for inside inside a generic lambda of a
17761 template). Otherwise convert to a regular for. */
17762 tree decl, expr;
17763 stmt = (processing_template_decl
17764 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17765 : begin_for_stmt (NULL_TREE, NULL_TREE));
17766 RECUR (RANGE_FOR_INIT_STMT (t));
17767 decl = RANGE_FOR_DECL (t);
17768 decl = tsubst (decl, args, complain, in_decl);
17769 maybe_push_decl (decl);
17770 expr = RECUR (RANGE_FOR_EXPR (t));
17771
17772 tree decomp_first = NULL_TREE;
17773 unsigned decomp_cnt = 0;
17774 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17775 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17776 complain, in_decl,
17777 &decomp_first, &decomp_cnt);
17778
17779 if (processing_template_decl)
17780 {
17781 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17782 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17783 finish_range_for_decl (stmt, decl, expr);
17784 if (decomp_first && decl != error_mark_node)
17785 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17786 }
17787 else
17788 {
17789 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17790 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17791 stmt = cp_convert_range_for (stmt, decl, expr,
17792 decomp_first, decomp_cnt,
17793 RANGE_FOR_IVDEP (t), unroll);
17794 }
17795
17796 bool prev = note_iteration_stmt_body_start ();
17797 RECUR (RANGE_FOR_BODY (t));
17798 note_iteration_stmt_body_end (prev);
17799 finish_for_stmt (stmt);
17800 }
17801 break;
17802
17803 case WHILE_STMT:
17804 stmt = begin_while_stmt ();
17805 tmp = RECUR (WHILE_COND (t));
17806 finish_while_stmt_cond (tmp, stmt, false, 0);
17807 {
17808 bool prev = note_iteration_stmt_body_start ();
17809 RECUR (WHILE_BODY (t));
17810 note_iteration_stmt_body_end (prev);
17811 }
17812 finish_while_stmt (stmt);
17813 break;
17814
17815 case DO_STMT:
17816 stmt = begin_do_stmt ();
17817 {
17818 bool prev = note_iteration_stmt_body_start ();
17819 RECUR (DO_BODY (t));
17820 note_iteration_stmt_body_end (prev);
17821 }
17822 finish_do_body (stmt);
17823 tmp = RECUR (DO_COND (t));
17824 finish_do_stmt (tmp, stmt, false, 0);
17825 break;
17826
17827 case IF_STMT:
17828 stmt = begin_if_stmt ();
17829 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17830 if (IF_STMT_CONSTEXPR_P (t))
17831 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17832 tmp = RECUR (IF_COND (t));
17833 tmp = finish_if_stmt_cond (tmp, stmt);
17834 if (IF_STMT_CONSTEXPR_P (t)
17835 && instantiation_dependent_expression_p (tmp))
17836 {
17837 /* We're partially instantiating a generic lambda, but the condition
17838 of the constexpr if is still dependent. Don't substitute into the
17839 branches now, just remember the template arguments. */
17840 do_poplevel (IF_SCOPE (stmt));
17841 IF_COND (stmt) = IF_COND (t);
17842 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17843 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17844 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17845 add_stmt (stmt);
17846 break;
17847 }
17848 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17849 /* Don't instantiate the THEN_CLAUSE. */;
17850 else
17851 {
17852 tree folded = fold_non_dependent_expr (tmp, complain);
17853 bool inhibit = integer_zerop (folded);
17854 if (inhibit)
17855 ++c_inhibit_evaluation_warnings;
17856 RECUR (THEN_CLAUSE (t));
17857 if (inhibit)
17858 --c_inhibit_evaluation_warnings;
17859 }
17860 finish_then_clause (stmt);
17861
17862 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17863 /* Don't instantiate the ELSE_CLAUSE. */;
17864 else if (ELSE_CLAUSE (t))
17865 {
17866 tree folded = fold_non_dependent_expr (tmp, complain);
17867 bool inhibit = integer_nonzerop (folded);
17868 begin_else_clause (stmt);
17869 if (inhibit)
17870 ++c_inhibit_evaluation_warnings;
17871 RECUR (ELSE_CLAUSE (t));
17872 if (inhibit)
17873 --c_inhibit_evaluation_warnings;
17874 finish_else_clause (stmt);
17875 }
17876
17877 finish_if_stmt (stmt);
17878 break;
17879
17880 case BIND_EXPR:
17881 if (BIND_EXPR_BODY_BLOCK (t))
17882 stmt = begin_function_body ();
17883 else
17884 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17885 ? BCS_TRY_BLOCK : 0);
17886
17887 RECUR (BIND_EXPR_BODY (t));
17888
17889 if (BIND_EXPR_BODY_BLOCK (t))
17890 finish_function_body (stmt);
17891 else
17892 finish_compound_stmt (stmt);
17893 break;
17894
17895 case BREAK_STMT:
17896 finish_break_stmt ();
17897 break;
17898
17899 case CONTINUE_STMT:
17900 finish_continue_stmt ();
17901 break;
17902
17903 case SWITCH_STMT:
17904 stmt = begin_switch_stmt ();
17905 tmp = RECUR (SWITCH_STMT_COND (t));
17906 finish_switch_cond (tmp, stmt);
17907 RECUR (SWITCH_STMT_BODY (t));
17908 finish_switch_stmt (stmt);
17909 break;
17910
17911 case CASE_LABEL_EXPR:
17912 {
17913 tree decl = CASE_LABEL (t);
17914 tree low = RECUR (CASE_LOW (t));
17915 tree high = RECUR (CASE_HIGH (t));
17916 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17917 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17918 {
17919 tree label = CASE_LABEL (l);
17920 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17921 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17922 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17923 }
17924 }
17925 break;
17926
17927 case LABEL_EXPR:
17928 {
17929 tree decl = LABEL_EXPR_LABEL (t);
17930 tree label;
17931
17932 label = finish_label_stmt (DECL_NAME (decl));
17933 if (TREE_CODE (label) == LABEL_DECL)
17934 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17935 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17936 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17937 }
17938 break;
17939
17940 case GOTO_EXPR:
17941 tmp = GOTO_DESTINATION (t);
17942 if (TREE_CODE (tmp) != LABEL_DECL)
17943 /* Computed goto's must be tsubst'd into. On the other hand,
17944 non-computed gotos must not be; the identifier in question
17945 will have no binding. */
17946 tmp = RECUR (tmp);
17947 else
17948 tmp = DECL_NAME (tmp);
17949 finish_goto_stmt (tmp);
17950 break;
17951
17952 case ASM_EXPR:
17953 {
17954 tree string = RECUR (ASM_STRING (t));
17955 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17956 complain, in_decl);
17957 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17958 complain, in_decl);
17959 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17960 complain, in_decl);
17961 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17962 complain, in_decl);
17963 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
17964 outputs, inputs, clobbers, labels,
17965 ASM_INLINE_P (t));
17966 tree asm_expr = tmp;
17967 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17968 asm_expr = TREE_OPERAND (asm_expr, 0);
17969 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17970 }
17971 break;
17972
17973 case TRY_BLOCK:
17974 if (CLEANUP_P (t))
17975 {
17976 stmt = begin_try_block ();
17977 RECUR (TRY_STMTS (t));
17978 finish_cleanup_try_block (stmt);
17979 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17980 }
17981 else
17982 {
17983 tree compound_stmt = NULL_TREE;
17984
17985 if (FN_TRY_BLOCK_P (t))
17986 stmt = begin_function_try_block (&compound_stmt);
17987 else
17988 stmt = begin_try_block ();
17989
17990 RECUR (TRY_STMTS (t));
17991
17992 if (FN_TRY_BLOCK_P (t))
17993 finish_function_try_block (stmt);
17994 else
17995 finish_try_block (stmt);
17996
17997 RECUR (TRY_HANDLERS (t));
17998 if (FN_TRY_BLOCK_P (t))
17999 finish_function_handler_sequence (stmt, compound_stmt);
18000 else
18001 finish_handler_sequence (stmt);
18002 }
18003 break;
18004
18005 case HANDLER:
18006 {
18007 tree decl = HANDLER_PARMS (t);
18008
18009 if (decl)
18010 {
18011 decl = tsubst (decl, args, complain, in_decl);
18012 /* Prevent instantiate_decl from trying to instantiate
18013 this variable. We've already done all that needs to be
18014 done. */
18015 if (decl != error_mark_node)
18016 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18017 }
18018 stmt = begin_handler ();
18019 finish_handler_parms (decl, stmt);
18020 RECUR (HANDLER_BODY (t));
18021 finish_handler (stmt);
18022 }
18023 break;
18024
18025 case TAG_DEFN:
18026 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18027 if (CLASS_TYPE_P (tmp))
18028 {
18029 /* Local classes are not independent templates; they are
18030 instantiated along with their containing function. And this
18031 way we don't have to deal with pushing out of one local class
18032 to instantiate a member of another local class. */
18033 /* Closures are handled by the LAMBDA_EXPR. */
18034 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18035 complete_type (tmp);
18036 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18037 if ((VAR_P (fld)
18038 || (TREE_CODE (fld) == FUNCTION_DECL
18039 && !DECL_ARTIFICIAL (fld)))
18040 && DECL_TEMPLATE_INSTANTIATION (fld))
18041 instantiate_decl (fld, /*defer_ok=*/false,
18042 /*expl_inst_class=*/false);
18043 }
18044 break;
18045
18046 case STATIC_ASSERT:
18047 {
18048 tree condition;
18049
18050 ++c_inhibit_evaluation_warnings;
18051 condition =
18052 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18053 args,
18054 complain, in_decl,
18055 /*integral_constant_expression_p=*/true);
18056 --c_inhibit_evaluation_warnings;
18057
18058 finish_static_assert (condition,
18059 STATIC_ASSERT_MESSAGE (t),
18060 STATIC_ASSERT_SOURCE_LOCATION (t),
18061 /*member_p=*/false);
18062 }
18063 break;
18064
18065 case OACC_KERNELS:
18066 case OACC_PARALLEL:
18067 case OACC_SERIAL:
18068 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18069 in_decl);
18070 stmt = begin_omp_parallel ();
18071 RECUR (OMP_BODY (t));
18072 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18073 break;
18074
18075 case OMP_PARALLEL:
18076 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18077 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18078 complain, in_decl);
18079 if (OMP_PARALLEL_COMBINED (t))
18080 omp_parallel_combined_clauses = &tmp;
18081 stmt = begin_omp_parallel ();
18082 RECUR (OMP_PARALLEL_BODY (t));
18083 gcc_assert (omp_parallel_combined_clauses == NULL);
18084 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18085 = OMP_PARALLEL_COMBINED (t);
18086 pop_omp_privatization_clauses (r);
18087 break;
18088
18089 case OMP_TASK:
18090 if (OMP_TASK_BODY (t) == NULL_TREE)
18091 {
18092 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18093 complain, in_decl);
18094 t = copy_node (t);
18095 OMP_TASK_CLAUSES (t) = tmp;
18096 add_stmt (t);
18097 break;
18098 }
18099 r = push_omp_privatization_clauses (false);
18100 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18101 complain, in_decl);
18102 stmt = begin_omp_task ();
18103 RECUR (OMP_TASK_BODY (t));
18104 finish_omp_task (tmp, stmt);
18105 pop_omp_privatization_clauses (r);
18106 break;
18107
18108 case OMP_FOR:
18109 case OMP_LOOP:
18110 case OMP_SIMD:
18111 case OMP_DISTRIBUTE:
18112 case OMP_TASKLOOP:
18113 case OACC_LOOP:
18114 {
18115 tree clauses, body, pre_body;
18116 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18117 tree orig_declv = NULL_TREE;
18118 tree incrv = NULL_TREE;
18119 enum c_omp_region_type ort = C_ORT_OMP;
18120 bool any_range_for = false;
18121 int i;
18122
18123 if (TREE_CODE (t) == OACC_LOOP)
18124 ort = C_ORT_ACC;
18125
18126 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18127 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18128 in_decl);
18129 if (OMP_FOR_INIT (t) != NULL_TREE)
18130 {
18131 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18132 if (OMP_FOR_ORIG_DECLS (t))
18133 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18134 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18135 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18136 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18137 }
18138
18139 keep_next_level (true);
18140 stmt = begin_omp_structured_block ();
18141
18142 pre_body = push_stmt_list ();
18143 RECUR (OMP_FOR_PRE_BODY (t));
18144 pre_body = pop_stmt_list (pre_body);
18145
18146 if (OMP_FOR_INIT (t) != NULL_TREE)
18147 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18148 any_range_for
18149 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18150 condv, incrv, &clauses, args,
18151 complain, in_decl,
18152 integral_constant_expression_p);
18153 omp_parallel_combined_clauses = NULL;
18154
18155 if (any_range_for)
18156 {
18157 gcc_assert (orig_declv);
18158 body = begin_omp_structured_block ();
18159 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18160 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18161 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18162 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18163 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18164 TREE_VEC_ELT (declv, i));
18165 }
18166 else
18167 body = push_stmt_list ();
18168 RECUR (OMP_FOR_BODY (t));
18169 if (any_range_for)
18170 body = finish_omp_structured_block (body);
18171 else
18172 body = pop_stmt_list (body);
18173
18174 if (OMP_FOR_INIT (t) != NULL_TREE)
18175 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18176 orig_declv, initv, condv, incrv, body, pre_body,
18177 NULL, clauses);
18178 else
18179 {
18180 t = make_node (TREE_CODE (t));
18181 TREE_TYPE (t) = void_type_node;
18182 OMP_FOR_BODY (t) = body;
18183 OMP_FOR_PRE_BODY (t) = pre_body;
18184 OMP_FOR_CLAUSES (t) = clauses;
18185 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18186 add_stmt (t);
18187 }
18188
18189 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18190 t));
18191 pop_omp_privatization_clauses (r);
18192 }
18193 break;
18194
18195 case OMP_SECTIONS:
18196 omp_parallel_combined_clauses = NULL;
18197 /* FALLTHRU */
18198 case OMP_SINGLE:
18199 case OMP_TEAMS:
18200 case OMP_CRITICAL:
18201 case OMP_TASKGROUP:
18202 case OMP_SCAN:
18203 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18204 && OMP_TEAMS_COMBINED (t));
18205 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18206 in_decl);
18207 if (TREE_CODE (t) == OMP_TEAMS)
18208 {
18209 keep_next_level (true);
18210 stmt = begin_omp_structured_block ();
18211 RECUR (OMP_BODY (t));
18212 stmt = finish_omp_structured_block (stmt);
18213 }
18214 else
18215 {
18216 stmt = push_stmt_list ();
18217 RECUR (OMP_BODY (t));
18218 stmt = pop_stmt_list (stmt);
18219 }
18220
18221 t = copy_node (t);
18222 OMP_BODY (t) = stmt;
18223 OMP_CLAUSES (t) = tmp;
18224 add_stmt (t);
18225 pop_omp_privatization_clauses (r);
18226 break;
18227
18228 case OMP_DEPOBJ:
18229 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18230 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18231 {
18232 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18233 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18234 {
18235 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18236 args, complain, in_decl);
18237 if (tmp == NULL_TREE)
18238 tmp = error_mark_node;
18239 }
18240 else
18241 {
18242 kind = (enum omp_clause_depend_kind)
18243 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18244 tmp = NULL_TREE;
18245 }
18246 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18247 }
18248 else
18249 finish_omp_depobj (EXPR_LOCATION (t), r,
18250 OMP_CLAUSE_DEPEND_SOURCE,
18251 OMP_DEPOBJ_CLAUSES (t));
18252 break;
18253
18254 case OACC_DATA:
18255 case OMP_TARGET_DATA:
18256 case OMP_TARGET:
18257 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18258 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18259 in_decl);
18260 keep_next_level (true);
18261 stmt = begin_omp_structured_block ();
18262
18263 RECUR (OMP_BODY (t));
18264 stmt = finish_omp_structured_block (stmt);
18265
18266 t = copy_node (t);
18267 OMP_BODY (t) = stmt;
18268 OMP_CLAUSES (t) = tmp;
18269 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18270 {
18271 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18272 if (teams)
18273 {
18274 /* For combined target teams, ensure the num_teams and
18275 thread_limit clause expressions are evaluated on the host,
18276 before entering the target construct. */
18277 tree c;
18278 for (c = OMP_TEAMS_CLAUSES (teams);
18279 c; c = OMP_CLAUSE_CHAIN (c))
18280 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18281 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18282 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18283 {
18284 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18285 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18286 if (expr == error_mark_node)
18287 continue;
18288 tmp = TARGET_EXPR_SLOT (expr);
18289 add_stmt (expr);
18290 OMP_CLAUSE_OPERAND (c, 0) = expr;
18291 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18292 OMP_CLAUSE_FIRSTPRIVATE);
18293 OMP_CLAUSE_DECL (tc) = tmp;
18294 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18295 OMP_TARGET_CLAUSES (t) = tc;
18296 }
18297 }
18298 }
18299 add_stmt (t);
18300 break;
18301
18302 case OACC_DECLARE:
18303 t = copy_node (t);
18304 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18305 complain, in_decl);
18306 OACC_DECLARE_CLAUSES (t) = tmp;
18307 add_stmt (t);
18308 break;
18309
18310 case OMP_TARGET_UPDATE:
18311 case OMP_TARGET_ENTER_DATA:
18312 case OMP_TARGET_EXIT_DATA:
18313 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18314 complain, in_decl);
18315 t = copy_node (t);
18316 OMP_STANDALONE_CLAUSES (t) = tmp;
18317 add_stmt (t);
18318 break;
18319
18320 case OACC_ENTER_DATA:
18321 case OACC_EXIT_DATA:
18322 case OACC_UPDATE:
18323 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18324 complain, in_decl);
18325 t = copy_node (t);
18326 OMP_STANDALONE_CLAUSES (t) = tmp;
18327 add_stmt (t);
18328 break;
18329
18330 case OMP_ORDERED:
18331 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18332 complain, in_decl);
18333 stmt = push_stmt_list ();
18334 RECUR (OMP_BODY (t));
18335 stmt = pop_stmt_list (stmt);
18336
18337 t = copy_node (t);
18338 OMP_BODY (t) = stmt;
18339 OMP_ORDERED_CLAUSES (t) = tmp;
18340 add_stmt (t);
18341 break;
18342
18343 case OMP_SECTION:
18344 case OMP_MASTER:
18345 stmt = push_stmt_list ();
18346 RECUR (OMP_BODY (t));
18347 stmt = pop_stmt_list (stmt);
18348
18349 t = copy_node (t);
18350 OMP_BODY (t) = stmt;
18351 add_stmt (t);
18352 break;
18353
18354 case OMP_ATOMIC:
18355 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18356 tmp = NULL_TREE;
18357 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18358 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18359 complain, in_decl);
18360 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18361 {
18362 tree op1 = TREE_OPERAND (t, 1);
18363 tree rhs1 = NULL_TREE;
18364 tree lhs, rhs;
18365 if (TREE_CODE (op1) == COMPOUND_EXPR)
18366 {
18367 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18368 op1 = TREE_OPERAND (op1, 1);
18369 }
18370 lhs = RECUR (TREE_OPERAND (op1, 0));
18371 rhs = RECUR (TREE_OPERAND (op1, 1));
18372 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18373 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18374 OMP_ATOMIC_MEMORY_ORDER (t));
18375 }
18376 else
18377 {
18378 tree op1 = TREE_OPERAND (t, 1);
18379 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18380 tree rhs1 = NULL_TREE;
18381 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18382 enum tree_code opcode = NOP_EXPR;
18383 if (code == OMP_ATOMIC_READ)
18384 {
18385 v = RECUR (TREE_OPERAND (op1, 0));
18386 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18387 }
18388 else if (code == OMP_ATOMIC_CAPTURE_OLD
18389 || code == OMP_ATOMIC_CAPTURE_NEW)
18390 {
18391 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18392 v = RECUR (TREE_OPERAND (op1, 0));
18393 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18394 if (TREE_CODE (op11) == COMPOUND_EXPR)
18395 {
18396 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18397 op11 = TREE_OPERAND (op11, 1);
18398 }
18399 lhs = RECUR (TREE_OPERAND (op11, 0));
18400 rhs = RECUR (TREE_OPERAND (op11, 1));
18401 opcode = TREE_CODE (op11);
18402 if (opcode == MODIFY_EXPR)
18403 opcode = NOP_EXPR;
18404 }
18405 else
18406 {
18407 code = OMP_ATOMIC;
18408 lhs = RECUR (TREE_OPERAND (op1, 0));
18409 rhs = RECUR (TREE_OPERAND (op1, 1));
18410 }
18411 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18412 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18413 }
18414 break;
18415
18416 case TRANSACTION_EXPR:
18417 {
18418 int flags = 0;
18419 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18420 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18421
18422 if (TRANSACTION_EXPR_IS_STMT (t))
18423 {
18424 tree body = TRANSACTION_EXPR_BODY (t);
18425 tree noex = NULL_TREE;
18426 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18427 {
18428 noex = MUST_NOT_THROW_COND (body);
18429 if (noex == NULL_TREE)
18430 noex = boolean_true_node;
18431 body = TREE_OPERAND (body, 0);
18432 }
18433 stmt = begin_transaction_stmt (input_location, NULL, flags);
18434 RECUR (body);
18435 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18436 }
18437 else
18438 {
18439 stmt = build_transaction_expr (EXPR_LOCATION (t),
18440 RECUR (TRANSACTION_EXPR_BODY (t)),
18441 flags, NULL_TREE);
18442 RETURN (stmt);
18443 }
18444 }
18445 break;
18446
18447 case MUST_NOT_THROW_EXPR:
18448 {
18449 tree op0 = RECUR (TREE_OPERAND (t, 0));
18450 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18451 RETURN (build_must_not_throw_expr (op0, cond));
18452 }
18453
18454 case EXPR_PACK_EXPANSION:
18455 error ("invalid use of pack expansion expression");
18456 RETURN (error_mark_node);
18457
18458 case NONTYPE_ARGUMENT_PACK:
18459 error ("use %<...%> to expand argument pack");
18460 RETURN (error_mark_node);
18461
18462 case COMPOUND_EXPR:
18463 tmp = RECUR (TREE_OPERAND (t, 0));
18464 if (tmp == NULL_TREE)
18465 /* If the first operand was a statement, we're done with it. */
18466 RETURN (RECUR (TREE_OPERAND (t, 1)));
18467 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18468 RECUR (TREE_OPERAND (t, 1)),
18469 complain));
18470
18471 case ANNOTATE_EXPR:
18472 tmp = RECUR (TREE_OPERAND (t, 0));
18473 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18474 TREE_TYPE (tmp), tmp,
18475 RECUR (TREE_OPERAND (t, 1)),
18476 RECUR (TREE_OPERAND (t, 2))));
18477
18478 case PREDICT_EXPR:
18479 RETURN (add_stmt (copy_node (t)));
18480
18481 default:
18482 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18483
18484 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18485 /*function_p=*/false,
18486 integral_constant_expression_p));
18487 }
18488
18489 RETURN (NULL_TREE);
18490 out:
18491 input_location = loc;
18492 return r;
18493 #undef RECUR
18494 #undef RETURN
18495 }
18496
18497 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18498 function. For description of the body see comment above
18499 cp_parser_omp_declare_reduction_exprs. */
18500
18501 static void
18502 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18503 {
18504 if (t == NULL_TREE || t == error_mark_node)
18505 return;
18506
18507 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
18508
18509 tree_stmt_iterator tsi;
18510 int i;
18511 tree stmts[7];
18512 memset (stmts, 0, sizeof stmts);
18513 for (i = 0, tsi = tsi_start (t);
18514 i < 7 && !tsi_end_p (tsi);
18515 i++, tsi_next (&tsi))
18516 stmts[i] = tsi_stmt (tsi);
18517 gcc_assert (tsi_end_p (tsi));
18518
18519 if (i >= 3)
18520 {
18521 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18522 && TREE_CODE (stmts[1]) == DECL_EXPR);
18523 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18524 args, complain, in_decl);
18525 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18526 args, complain, in_decl);
18527 DECL_CONTEXT (omp_out) = current_function_decl;
18528 DECL_CONTEXT (omp_in) = current_function_decl;
18529 keep_next_level (true);
18530 tree block = begin_omp_structured_block ();
18531 tsubst_expr (stmts[2], args, complain, in_decl, false);
18532 block = finish_omp_structured_block (block);
18533 block = maybe_cleanup_point_expr_void (block);
18534 add_decl_expr (omp_out);
18535 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18536 TREE_NO_WARNING (omp_out) = 1;
18537 add_decl_expr (omp_in);
18538 finish_expr_stmt (block);
18539 }
18540 if (i >= 6)
18541 {
18542 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18543 && TREE_CODE (stmts[4]) == DECL_EXPR);
18544 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18545 args, complain, in_decl);
18546 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18547 args, complain, in_decl);
18548 DECL_CONTEXT (omp_priv) = current_function_decl;
18549 DECL_CONTEXT (omp_orig) = current_function_decl;
18550 keep_next_level (true);
18551 tree block = begin_omp_structured_block ();
18552 tsubst_expr (stmts[5], args, complain, in_decl, false);
18553 block = finish_omp_structured_block (block);
18554 block = maybe_cleanup_point_expr_void (block);
18555 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18556 add_decl_expr (omp_priv);
18557 add_decl_expr (omp_orig);
18558 finish_expr_stmt (block);
18559 if (i == 7)
18560 add_decl_expr (omp_orig);
18561 }
18562 }
18563
18564 /* T is a postfix-expression that is not being used in a function
18565 call. Return the substituted version of T. */
18566
18567 static tree
18568 tsubst_non_call_postfix_expression (tree t, tree args,
18569 tsubst_flags_t complain,
18570 tree in_decl)
18571 {
18572 if (TREE_CODE (t) == SCOPE_REF)
18573 t = tsubst_qualified_id (t, args, complain, in_decl,
18574 /*done=*/false, /*address_p=*/false);
18575 else
18576 t = tsubst_copy_and_build (t, args, complain, in_decl,
18577 /*function_p=*/false,
18578 /*integral_constant_expression_p=*/false);
18579
18580 return t;
18581 }
18582
18583 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18584 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18585 dependent init-capture. */
18586
18587 static void
18588 prepend_one_capture (tree field, tree init, tree &list,
18589 tsubst_flags_t complain)
18590 {
18591 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18592 {
18593 tree type = NULL_TREE;
18594 if (!init)
18595 {
18596 if (complain & tf_error)
18597 error ("empty initializer in lambda init-capture");
18598 init = error_mark_node;
18599 }
18600 else if (TREE_CODE (init) == TREE_LIST)
18601 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18602 if (!type)
18603 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18604 TREE_TYPE (field) = type;
18605 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18606 }
18607 list = tree_cons (field, init, list);
18608 }
18609
18610 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18611 instantiation context. Instantiating a pack expansion containing a lambda
18612 might result in multiple lambdas all based on the same lambda in the
18613 template. */
18614
18615 tree
18616 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18617 {
18618 tree oldfn = lambda_function (t);
18619 in_decl = oldfn;
18620
18621 tree r = build_lambda_expr ();
18622
18623 LAMBDA_EXPR_LOCATION (r)
18624 = LAMBDA_EXPR_LOCATION (t);
18625 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18626 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18627 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18628 LAMBDA_EXPR_INSTANTIATED (r) = true;
18629
18630 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18631 /* A lambda in a default argument outside a class gets no
18632 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18633 tsubst_default_argument calls start_lambda_scope, so we need to
18634 specifically ignore it here, and use the global scope. */
18635 record_null_lambda_scope (r);
18636 else
18637 record_lambda_scope (r);
18638
18639 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18640 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18641
18642 vec<tree,va_gc>* field_packs = NULL;
18643
18644 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18645 cap = TREE_CHAIN (cap))
18646 {
18647 tree ofield = TREE_PURPOSE (cap);
18648 if (PACK_EXPANSION_P (ofield))
18649 ofield = PACK_EXPANSION_PATTERN (ofield);
18650 tree field = tsubst_decl (ofield, args, complain);
18651
18652 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18653 {
18654 /* Remember these for when we've pushed local_specializations. */
18655 vec_safe_push (field_packs, ofield);
18656 vec_safe_push (field_packs, field);
18657 }
18658
18659 if (field == error_mark_node)
18660 return error_mark_node;
18661
18662 tree init = TREE_VALUE (cap);
18663 if (PACK_EXPANSION_P (init))
18664 init = tsubst_pack_expansion (init, args, complain, in_decl);
18665 else
18666 init = tsubst_copy_and_build (init, args, complain, in_decl,
18667 /*fn*/false, /*constexpr*/false);
18668
18669 if (TREE_CODE (field) == TREE_VEC)
18670 {
18671 int len = TREE_VEC_LENGTH (field);
18672 gcc_assert (TREE_CODE (init) == TREE_VEC
18673 && TREE_VEC_LENGTH (init) == len);
18674 for (int i = 0; i < len; ++i)
18675 prepend_one_capture (TREE_VEC_ELT (field, i),
18676 TREE_VEC_ELT (init, i),
18677 LAMBDA_EXPR_CAPTURE_LIST (r),
18678 complain);
18679 }
18680 else
18681 {
18682 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
18683 complain);
18684
18685 if (id_equal (DECL_NAME (field), "__this"))
18686 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18687 }
18688 }
18689
18690 tree type = begin_lambda_type (r);
18691 if (type == error_mark_node)
18692 return error_mark_node;
18693
18694 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18695 determine_visibility (TYPE_NAME (type));
18696
18697 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18698
18699 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18700 ? DECL_TI_TEMPLATE (oldfn)
18701 : NULL_TREE);
18702
18703 tree fntype = static_fn_type (oldfn);
18704 if (oldtmpl)
18705 ++processing_template_decl;
18706 fntype = tsubst (fntype, args, complain, in_decl);
18707 if (oldtmpl)
18708 --processing_template_decl;
18709
18710 if (fntype == error_mark_node)
18711 r = error_mark_node;
18712 else
18713 {
18714 /* The body of a lambda-expression is not a subexpression of the
18715 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18716 which would be skipped if cp_unevaluated_operand. */
18717 cp_evaluated ev;
18718
18719 /* Fix the type of 'this'. */
18720 fntype = build_memfn_type (fntype, type,
18721 type_memfn_quals (fntype),
18722 type_memfn_rqual (fntype));
18723 tree fn, tmpl;
18724 if (oldtmpl)
18725 {
18726 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18727 fn = DECL_TEMPLATE_RESULT (tmpl);
18728 finish_member_declaration (tmpl);
18729 }
18730 else
18731 {
18732 tmpl = NULL_TREE;
18733 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18734 finish_member_declaration (fn);
18735 }
18736
18737 /* Let finish_function set this. */
18738 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18739
18740 bool nested = cfun;
18741 if (nested)
18742 push_function_context ();
18743 else
18744 /* Still increment function_depth so that we don't GC in the
18745 middle of an expression. */
18746 ++function_depth;
18747
18748 local_specialization_stack s (lss_copy);
18749
18750 tree body = start_lambda_function (fn, r);
18751
18752 /* Now record them for lookup_init_capture_pack. */
18753 int fplen = vec_safe_length (field_packs);
18754 for (int i = 0; i < fplen; )
18755 {
18756 tree pack = (*field_packs)[i++];
18757 tree inst = (*field_packs)[i++];
18758 register_local_specialization (inst, pack);
18759 }
18760 release_tree_vector (field_packs);
18761
18762 register_parameter_specializations (oldfn, fn);
18763
18764 if (oldtmpl)
18765 {
18766 /* We might not partially instantiate some parts of the function, so
18767 copy these flags from the original template. */
18768 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18769 current_function_returns_value = ol->returns_value;
18770 current_function_returns_null = ol->returns_null;
18771 current_function_returns_abnormally = ol->returns_abnormally;
18772 current_function_infinite_loop = ol->infinite_loop;
18773 }
18774
18775 /* [temp.deduct] A lambda-expression appearing in a function type or a
18776 template parameter is not considered part of the immediate context for
18777 the purposes of template argument deduction. */
18778 complain = tf_warning_or_error;
18779
18780 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18781 /*constexpr*/false);
18782
18783 finish_lambda_function (body);
18784
18785 if (nested)
18786 pop_function_context ();
18787 else
18788 --function_depth;
18789
18790 /* The capture list was built up in reverse order; fix that now. */
18791 LAMBDA_EXPR_CAPTURE_LIST (r)
18792 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18793
18794 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18795
18796 maybe_add_lambda_conv_op (type);
18797 }
18798
18799 finish_struct (type, /*attr*/NULL_TREE);
18800
18801 insert_pending_capture_proxies ();
18802
18803 return r;
18804 }
18805
18806 /* Like tsubst but deals with expressions and performs semantic
18807 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18808
18809 tree
18810 tsubst_copy_and_build (tree t,
18811 tree args,
18812 tsubst_flags_t complain,
18813 tree in_decl,
18814 bool function_p,
18815 bool integral_constant_expression_p)
18816 {
18817 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18818 #define RECUR(NODE) \
18819 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18820 /*function_p=*/false, \
18821 integral_constant_expression_p)
18822
18823 tree retval, op1;
18824 location_t save_loc;
18825
18826 if (t == NULL_TREE || t == error_mark_node)
18827 return t;
18828
18829 save_loc = input_location;
18830 if (location_t eloc = cp_expr_location (t))
18831 input_location = eloc;
18832
18833 /* N3276 decltype magic only applies to calls at the top level or on the
18834 right side of a comma. */
18835 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18836 complain &= ~tf_decltype;
18837
18838 switch (TREE_CODE (t))
18839 {
18840 case USING_DECL:
18841 t = DECL_NAME (t);
18842 /* Fall through. */
18843 case IDENTIFIER_NODE:
18844 {
18845 tree decl;
18846 cp_id_kind idk;
18847 bool non_integral_constant_expression_p;
18848 const char *error_msg;
18849
18850 if (IDENTIFIER_CONV_OP_P (t))
18851 {
18852 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18853 t = make_conv_op_name (new_type);
18854 }
18855
18856 /* Look up the name. */
18857 decl = lookup_name (t);
18858
18859 /* By convention, expressions use ERROR_MARK_NODE to indicate
18860 failure, not NULL_TREE. */
18861 if (decl == NULL_TREE)
18862 decl = error_mark_node;
18863
18864 decl = finish_id_expression (t, decl, NULL_TREE,
18865 &idk,
18866 integral_constant_expression_p,
18867 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18868 &non_integral_constant_expression_p,
18869 /*template_p=*/false,
18870 /*done=*/true,
18871 /*address_p=*/false,
18872 /*template_arg_p=*/false,
18873 &error_msg,
18874 input_location);
18875 if (error_msg)
18876 error (error_msg);
18877 if (!function_p && identifier_p (decl))
18878 {
18879 if (complain & tf_error)
18880 unqualified_name_lookup_error (decl);
18881 decl = error_mark_node;
18882 }
18883 RETURN (decl);
18884 }
18885
18886 case TEMPLATE_ID_EXPR:
18887 {
18888 tree object;
18889 tree templ = RECUR (TREE_OPERAND (t, 0));
18890 tree targs = TREE_OPERAND (t, 1);
18891
18892 if (targs)
18893 targs = tsubst_template_args (targs, args, complain, in_decl);
18894 if (targs == error_mark_node)
18895 RETURN (error_mark_node);
18896
18897 if (TREE_CODE (templ) == SCOPE_REF)
18898 {
18899 tree name = TREE_OPERAND (templ, 1);
18900 tree tid = lookup_template_function (name, targs);
18901 TREE_OPERAND (templ, 1) = tid;
18902 RETURN (templ);
18903 }
18904
18905 if (concept_definition_p (templ))
18906 {
18907 tree check = build_concept_check (templ, targs, complain);
18908 if (check == error_mark_node)
18909 RETURN (error_mark_node);
18910
18911 tree id = unpack_concept_check (check);
18912
18913 /* If we built a function concept check, return the underlying
18914 template-id. So we can evaluate it as a function call. */
18915 if (function_concept_p (TREE_OPERAND (id, 0)))
18916 RETURN (id);
18917
18918 RETURN (check);
18919 }
18920
18921 if (variable_template_p (templ))
18922 {
18923 tree r = lookup_and_finish_template_variable (templ, targs,
18924 complain);
18925 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
18926 RETURN (r);
18927 }
18928
18929 if (TREE_CODE (templ) == COMPONENT_REF)
18930 {
18931 object = TREE_OPERAND (templ, 0);
18932 templ = TREE_OPERAND (templ, 1);
18933 }
18934 else
18935 object = NULL_TREE;
18936 templ = lookup_template_function (templ, targs);
18937
18938 if (object)
18939 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18940 object, templ, NULL_TREE));
18941 else
18942 RETURN (baselink_for_fns (templ));
18943 }
18944
18945 case INDIRECT_REF:
18946 {
18947 tree r = RECUR (TREE_OPERAND (t, 0));
18948
18949 if (REFERENCE_REF_P (t))
18950 {
18951 /* A type conversion to reference type will be enclosed in
18952 such an indirect ref, but the substitution of the cast
18953 will have also added such an indirect ref. */
18954 r = convert_from_reference (r);
18955 }
18956 else
18957 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18958 complain|decltype_flag);
18959
18960 if (REF_PARENTHESIZED_P (t))
18961 r = force_paren_expr (r);
18962
18963 RETURN (r);
18964 }
18965
18966 case NOP_EXPR:
18967 {
18968 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18969 tree op0 = RECUR (TREE_OPERAND (t, 0));
18970 RETURN (build_nop (type, op0));
18971 }
18972
18973 case IMPLICIT_CONV_EXPR:
18974 {
18975 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18976 tree expr = RECUR (TREE_OPERAND (t, 0));
18977 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18978 {
18979 retval = copy_node (t);
18980 TREE_TYPE (retval) = type;
18981 TREE_OPERAND (retval, 0) = expr;
18982 RETURN (retval);
18983 }
18984 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18985 /* We'll pass this to convert_nontype_argument again, we don't need
18986 to actually perform any conversion here. */
18987 RETURN (expr);
18988 int flags = LOOKUP_IMPLICIT;
18989 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18990 flags = LOOKUP_NORMAL;
18991 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18992 flags |= LOOKUP_NO_NARROWING;
18993 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18994 flags));
18995 }
18996
18997 case CONVERT_EXPR:
18998 {
18999 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19000 tree op0 = RECUR (TREE_OPERAND (t, 0));
19001 if (op0 == error_mark_node)
19002 RETURN (error_mark_node);
19003 RETURN (build1 (CONVERT_EXPR, type, op0));
19004 }
19005
19006 case CAST_EXPR:
19007 case REINTERPRET_CAST_EXPR:
19008 case CONST_CAST_EXPR:
19009 case DYNAMIC_CAST_EXPR:
19010 case STATIC_CAST_EXPR:
19011 {
19012 tree type;
19013 tree op, r = NULL_TREE;
19014
19015 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19016 if (integral_constant_expression_p
19017 && !cast_valid_in_integral_constant_expression_p (type))
19018 {
19019 if (complain & tf_error)
19020 error ("a cast to a type other than an integral or "
19021 "enumeration type cannot appear in a constant-expression");
19022 RETURN (error_mark_node);
19023 }
19024
19025 op = RECUR (TREE_OPERAND (t, 0));
19026
19027 warning_sentinel s(warn_useless_cast);
19028 warning_sentinel s2(warn_ignored_qualifiers);
19029 switch (TREE_CODE (t))
19030 {
19031 case CAST_EXPR:
19032 r = build_functional_cast (input_location, type, op, complain);
19033 break;
19034 case REINTERPRET_CAST_EXPR:
19035 r = build_reinterpret_cast (input_location, type, op, complain);
19036 break;
19037 case CONST_CAST_EXPR:
19038 r = build_const_cast (input_location, type, op, complain);
19039 break;
19040 case DYNAMIC_CAST_EXPR:
19041 r = build_dynamic_cast (input_location, type, op, complain);
19042 break;
19043 case STATIC_CAST_EXPR:
19044 r = build_static_cast (input_location, type, op, complain);
19045 break;
19046 default:
19047 gcc_unreachable ();
19048 }
19049
19050 RETURN (r);
19051 }
19052
19053 case POSTDECREMENT_EXPR:
19054 case POSTINCREMENT_EXPR:
19055 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19056 args, complain, in_decl);
19057 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19058 complain|decltype_flag));
19059
19060 case PREDECREMENT_EXPR:
19061 case PREINCREMENT_EXPR:
19062 case NEGATE_EXPR:
19063 case BIT_NOT_EXPR:
19064 case ABS_EXPR:
19065 case TRUTH_NOT_EXPR:
19066 case UNARY_PLUS_EXPR: /* Unary + */
19067 case REALPART_EXPR:
19068 case IMAGPART_EXPR:
19069 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19070 RECUR (TREE_OPERAND (t, 0)),
19071 complain|decltype_flag));
19072
19073 case FIX_TRUNC_EXPR:
19074 gcc_unreachable ();
19075
19076 case ADDR_EXPR:
19077 op1 = TREE_OPERAND (t, 0);
19078 if (TREE_CODE (op1) == LABEL_DECL)
19079 RETURN (finish_label_address_expr (DECL_NAME (op1),
19080 EXPR_LOCATION (op1)));
19081 if (TREE_CODE (op1) == SCOPE_REF)
19082 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19083 /*done=*/true, /*address_p=*/true);
19084 else
19085 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19086 in_decl);
19087 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19088 complain|decltype_flag));
19089
19090 case PLUS_EXPR:
19091 case MINUS_EXPR:
19092 case MULT_EXPR:
19093 case TRUNC_DIV_EXPR:
19094 case CEIL_DIV_EXPR:
19095 case FLOOR_DIV_EXPR:
19096 case ROUND_DIV_EXPR:
19097 case EXACT_DIV_EXPR:
19098 case BIT_AND_EXPR:
19099 case BIT_IOR_EXPR:
19100 case BIT_XOR_EXPR:
19101 case TRUNC_MOD_EXPR:
19102 case FLOOR_MOD_EXPR:
19103 case TRUTH_ANDIF_EXPR:
19104 case TRUTH_ORIF_EXPR:
19105 case TRUTH_AND_EXPR:
19106 case TRUTH_OR_EXPR:
19107 case RSHIFT_EXPR:
19108 case LSHIFT_EXPR:
19109 case EQ_EXPR:
19110 case NE_EXPR:
19111 case MAX_EXPR:
19112 case MIN_EXPR:
19113 case LE_EXPR:
19114 case GE_EXPR:
19115 case LT_EXPR:
19116 case GT_EXPR:
19117 case SPACESHIP_EXPR:
19118 case MEMBER_REF:
19119 case DOTSTAR_EXPR:
19120 {
19121 warning_sentinel s1(warn_type_limits);
19122 warning_sentinel s2(warn_div_by_zero);
19123 warning_sentinel s3(warn_logical_op);
19124 warning_sentinel s4(warn_tautological_compare);
19125 tree op0 = RECUR (TREE_OPERAND (t, 0));
19126 tree op1 = RECUR (TREE_OPERAND (t, 1));
19127 tree r = build_x_binary_op
19128 (input_location, TREE_CODE (t),
19129 op0,
19130 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19131 ? ERROR_MARK
19132 : TREE_CODE (TREE_OPERAND (t, 0))),
19133 op1,
19134 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19135 ? ERROR_MARK
19136 : TREE_CODE (TREE_OPERAND (t, 1))),
19137 /*overload=*/NULL,
19138 complain|decltype_flag);
19139 if (EXPR_P (r) && TREE_NO_WARNING (t))
19140 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19141
19142 RETURN (r);
19143 }
19144
19145 case POINTER_PLUS_EXPR:
19146 {
19147 tree op0 = RECUR (TREE_OPERAND (t, 0));
19148 tree op1 = RECUR (TREE_OPERAND (t, 1));
19149 RETURN (fold_build_pointer_plus (op0, op1));
19150 }
19151
19152 case SCOPE_REF:
19153 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19154 /*address_p=*/false));
19155 case ARRAY_REF:
19156 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19157 args, complain, in_decl);
19158 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19159 RECUR (TREE_OPERAND (t, 1)),
19160 complain|decltype_flag));
19161
19162 case SIZEOF_EXPR:
19163 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19164 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19165 RETURN (tsubst_copy (t, args, complain, in_decl));
19166 /* Fall through */
19167
19168 case ALIGNOF_EXPR:
19169 {
19170 tree r;
19171
19172 op1 = TREE_OPERAND (t, 0);
19173 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19174 op1 = TREE_TYPE (op1);
19175 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19176 && ALIGNOF_EXPR_STD_P (t));
19177 if (!args)
19178 {
19179 /* When there are no ARGS, we are trying to evaluate a
19180 non-dependent expression from the parser. Trying to do
19181 the substitutions may not work. */
19182 if (!TYPE_P (op1))
19183 op1 = TREE_TYPE (op1);
19184 }
19185 else
19186 {
19187 ++cp_unevaluated_operand;
19188 ++c_inhibit_evaluation_warnings;
19189 if (TYPE_P (op1))
19190 op1 = tsubst (op1, args, complain, in_decl);
19191 else
19192 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19193 /*function_p=*/false,
19194 /*integral_constant_expression_p=*/
19195 false);
19196 --cp_unevaluated_operand;
19197 --c_inhibit_evaluation_warnings;
19198 }
19199 if (TYPE_P (op1))
19200 r = cxx_sizeof_or_alignof_type (input_location,
19201 op1, TREE_CODE (t), std_alignof,
19202 complain & tf_error);
19203 else
19204 r = cxx_sizeof_or_alignof_expr (input_location,
19205 op1, TREE_CODE (t),
19206 complain & tf_error);
19207 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19208 {
19209 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19210 {
19211 if (!processing_template_decl && TYPE_P (op1))
19212 {
19213 r = build_min (SIZEOF_EXPR, size_type_node,
19214 build1 (NOP_EXPR, op1, error_mark_node));
19215 SIZEOF_EXPR_TYPE_P (r) = 1;
19216 }
19217 else
19218 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19219 TREE_SIDE_EFFECTS (r) = 0;
19220 TREE_READONLY (r) = 1;
19221 }
19222 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19223 }
19224 RETURN (r);
19225 }
19226
19227 case AT_ENCODE_EXPR:
19228 {
19229 op1 = TREE_OPERAND (t, 0);
19230 ++cp_unevaluated_operand;
19231 ++c_inhibit_evaluation_warnings;
19232 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19233 /*function_p=*/false,
19234 /*integral_constant_expression_p=*/false);
19235 --cp_unevaluated_operand;
19236 --c_inhibit_evaluation_warnings;
19237 RETURN (objc_build_encode_expr (op1));
19238 }
19239
19240 case NOEXCEPT_EXPR:
19241 op1 = TREE_OPERAND (t, 0);
19242 ++cp_unevaluated_operand;
19243 ++c_inhibit_evaluation_warnings;
19244 ++cp_noexcept_operand;
19245 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19246 /*function_p=*/false,
19247 /*integral_constant_expression_p=*/false);
19248 --cp_unevaluated_operand;
19249 --c_inhibit_evaluation_warnings;
19250 --cp_noexcept_operand;
19251 RETURN (finish_noexcept_expr (op1, complain));
19252
19253 case MODOP_EXPR:
19254 {
19255 warning_sentinel s(warn_div_by_zero);
19256 tree lhs = RECUR (TREE_OPERAND (t, 0));
19257 tree rhs = RECUR (TREE_OPERAND (t, 2));
19258 tree r = build_x_modify_expr
19259 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19260 complain|decltype_flag);
19261 /* TREE_NO_WARNING must be set if either the expression was
19262 parenthesized or it uses an operator such as >>= rather
19263 than plain assignment. In the former case, it was already
19264 set and must be copied. In the latter case,
19265 build_x_modify_expr sets it and it must not be reset
19266 here. */
19267 if (TREE_NO_WARNING (t))
19268 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19269
19270 RETURN (r);
19271 }
19272
19273 case ARROW_EXPR:
19274 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19275 args, complain, in_decl);
19276 /* Remember that there was a reference to this entity. */
19277 if (DECL_P (op1)
19278 && !mark_used (op1, complain) && !(complain & tf_error))
19279 RETURN (error_mark_node);
19280 RETURN (build_x_arrow (input_location, op1, complain));
19281
19282 case NEW_EXPR:
19283 {
19284 tree placement = RECUR (TREE_OPERAND (t, 0));
19285 tree init = RECUR (TREE_OPERAND (t, 3));
19286 vec<tree, va_gc> *placement_vec;
19287 vec<tree, va_gc> *init_vec;
19288 tree ret;
19289 location_t loc = EXPR_LOCATION (t);
19290
19291 if (placement == NULL_TREE)
19292 placement_vec = NULL;
19293 else
19294 {
19295 placement_vec = make_tree_vector ();
19296 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19297 vec_safe_push (placement_vec, TREE_VALUE (placement));
19298 }
19299
19300 /* If there was an initializer in the original tree, but it
19301 instantiated to an empty list, then we should pass a
19302 non-NULL empty vector to tell build_new that it was an
19303 empty initializer() rather than no initializer. This can
19304 only happen when the initializer is a pack expansion whose
19305 parameter packs are of length zero. */
19306 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19307 init_vec = NULL;
19308 else
19309 {
19310 init_vec = make_tree_vector ();
19311 if (init == void_node)
19312 gcc_assert (init_vec != NULL);
19313 else
19314 {
19315 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19316 vec_safe_push (init_vec, TREE_VALUE (init));
19317 }
19318 }
19319
19320 /* Avoid passing an enclosing decl to valid_array_size_p. */
19321 in_decl = NULL_TREE;
19322
19323 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19324 tree op2 = RECUR (TREE_OPERAND (t, 2));
19325 ret = build_new (loc, &placement_vec, op1, op2,
19326 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19327 complain);
19328
19329 if (placement_vec != NULL)
19330 release_tree_vector (placement_vec);
19331 if (init_vec != NULL)
19332 release_tree_vector (init_vec);
19333
19334 RETURN (ret);
19335 }
19336
19337 case DELETE_EXPR:
19338 {
19339 tree op0 = RECUR (TREE_OPERAND (t, 0));
19340 tree op1 = RECUR (TREE_OPERAND (t, 1));
19341 RETURN (delete_sanity (input_location, op0, op1,
19342 DELETE_EXPR_USE_VEC (t),
19343 DELETE_EXPR_USE_GLOBAL (t),
19344 complain));
19345 }
19346
19347 case COMPOUND_EXPR:
19348 {
19349 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19350 complain & ~tf_decltype, in_decl,
19351 /*function_p=*/false,
19352 integral_constant_expression_p);
19353 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19354 op0,
19355 RECUR (TREE_OPERAND (t, 1)),
19356 complain|decltype_flag));
19357 }
19358
19359 case CALL_EXPR:
19360 {
19361 tree function;
19362 unsigned int nargs, i;
19363 bool qualified_p;
19364 bool koenig_p;
19365 tree ret;
19366
19367 function = CALL_EXPR_FN (t);
19368 /* Internal function with no arguments. */
19369 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19370 RETURN (t);
19371
19372 /* When we parsed the expression, we determined whether or
19373 not Koenig lookup should be performed. */
19374 koenig_p = KOENIG_LOOKUP_P (t);
19375 if (function == NULL_TREE)
19376 {
19377 koenig_p = false;
19378 qualified_p = false;
19379 }
19380 else if (TREE_CODE (function) == SCOPE_REF)
19381 {
19382 qualified_p = true;
19383 function = tsubst_qualified_id (function, args, complain, in_decl,
19384 /*done=*/false,
19385 /*address_p=*/false);
19386 }
19387 else if (koenig_p && identifier_p (function))
19388 {
19389 /* Do nothing; calling tsubst_copy_and_build on an identifier
19390 would incorrectly perform unqualified lookup again.
19391
19392 Note that we can also have an IDENTIFIER_NODE if the earlier
19393 unqualified lookup found a member function; in that case
19394 koenig_p will be false and we do want to do the lookup
19395 again to find the instantiated member function.
19396
19397 FIXME but doing that causes c++/15272, so we need to stop
19398 using IDENTIFIER_NODE in that situation. */
19399 qualified_p = false;
19400 }
19401 else
19402 {
19403 if (TREE_CODE (function) == COMPONENT_REF)
19404 {
19405 tree op = TREE_OPERAND (function, 1);
19406
19407 qualified_p = (TREE_CODE (op) == SCOPE_REF
19408 || (BASELINK_P (op)
19409 && BASELINK_QUALIFIED_P (op)));
19410 }
19411 else
19412 qualified_p = false;
19413
19414 if (TREE_CODE (function) == ADDR_EXPR
19415 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19416 /* Avoid error about taking the address of a constructor. */
19417 function = TREE_OPERAND (function, 0);
19418
19419 function = tsubst_copy_and_build (function, args, complain,
19420 in_decl,
19421 !qualified_p,
19422 integral_constant_expression_p);
19423
19424 if (BASELINK_P (function))
19425 qualified_p = true;
19426 }
19427
19428 nargs = call_expr_nargs (t);
19429 releasing_vec call_args;
19430 for (i = 0; i < nargs; ++i)
19431 {
19432 tree arg = CALL_EXPR_ARG (t, i);
19433
19434 if (!PACK_EXPANSION_P (arg))
19435 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19436 else
19437 {
19438 /* Expand the pack expansion and push each entry onto
19439 CALL_ARGS. */
19440 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19441 if (TREE_CODE (arg) == TREE_VEC)
19442 {
19443 unsigned int len, j;
19444
19445 len = TREE_VEC_LENGTH (arg);
19446 for (j = 0; j < len; ++j)
19447 {
19448 tree value = TREE_VEC_ELT (arg, j);
19449 if (value != NULL_TREE)
19450 value = convert_from_reference (value);
19451 vec_safe_push (call_args, value);
19452 }
19453 }
19454 else
19455 {
19456 /* A partial substitution. Add one entry. */
19457 vec_safe_push (call_args, arg);
19458 }
19459 }
19460 }
19461
19462 /* Stripped-down processing for a call in a thunk. Specifically, in
19463 the thunk template for a generic lambda. */
19464 if (CALL_FROM_THUNK_P (t))
19465 {
19466 /* Now that we've expanded any packs, the number of call args
19467 might be different. */
19468 unsigned int cargs = call_args->length ();
19469 tree thisarg = NULL_TREE;
19470 if (TREE_CODE (function) == COMPONENT_REF)
19471 {
19472 thisarg = TREE_OPERAND (function, 0);
19473 if (TREE_CODE (thisarg) == INDIRECT_REF)
19474 thisarg = TREE_OPERAND (thisarg, 0);
19475 function = TREE_OPERAND (function, 1);
19476 if (TREE_CODE (function) == BASELINK)
19477 function = BASELINK_FUNCTIONS (function);
19478 }
19479 /* We aren't going to do normal overload resolution, so force the
19480 template-id to resolve. */
19481 function = resolve_nondeduced_context (function, complain);
19482 for (unsigned i = 0; i < cargs; ++i)
19483 {
19484 /* In a thunk, pass through args directly, without any
19485 conversions. */
19486 tree arg = (*call_args)[i];
19487 while (TREE_CODE (arg) != PARM_DECL)
19488 arg = TREE_OPERAND (arg, 0);
19489 (*call_args)[i] = arg;
19490 }
19491 if (thisarg)
19492 {
19493 /* If there are no other args, just push 'this'. */
19494 if (cargs == 0)
19495 vec_safe_push (call_args, thisarg);
19496 else
19497 {
19498 /* Otherwise, shift the other args over to make room. */
19499 tree last = (*call_args)[cargs - 1];
19500 vec_safe_push (call_args, last);
19501 for (int i = cargs - 1; i > 0; --i)
19502 (*call_args)[i] = (*call_args)[i - 1];
19503 (*call_args)[0] = thisarg;
19504 }
19505 }
19506 ret = build_call_a (function, call_args->length (),
19507 call_args->address ());
19508 /* The thunk location is not interesting. */
19509 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19510 CALL_FROM_THUNK_P (ret) = true;
19511 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19512 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19513
19514 RETURN (ret);
19515 }
19516
19517 /* We do not perform argument-dependent lookup if normal
19518 lookup finds a non-function, in accordance with the
19519 expected resolution of DR 218. */
19520 if (koenig_p
19521 && ((is_overloaded_fn (function)
19522 /* If lookup found a member function, the Koenig lookup is
19523 not appropriate, even if an unqualified-name was used
19524 to denote the function. */
19525 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19526 || identifier_p (function))
19527 /* Only do this when substitution turns a dependent call
19528 into a non-dependent call. */
19529 && type_dependent_expression_p_push (t)
19530 && !any_type_dependent_arguments_p (call_args))
19531 function = perform_koenig_lookup (function, call_args, tf_none);
19532
19533 if (function != NULL_TREE
19534 && identifier_p (function)
19535 && !any_type_dependent_arguments_p (call_args))
19536 {
19537 if (koenig_p && (complain & tf_warning_or_error))
19538 {
19539 /* For backwards compatibility and good diagnostics, try
19540 the unqualified lookup again if we aren't in SFINAE
19541 context. */
19542 tree unq = (tsubst_copy_and_build
19543 (function, args, complain, in_decl, true,
19544 integral_constant_expression_p));
19545 if (unq == error_mark_node)
19546 RETURN (error_mark_node);
19547
19548 if (unq != function)
19549 {
19550 /* In a lambda fn, we have to be careful to not
19551 introduce new this captures. Legacy code can't
19552 be using lambdas anyway, so it's ok to be
19553 stricter. */
19554 bool in_lambda = (current_class_type
19555 && LAMBDA_TYPE_P (current_class_type));
19556 char const *const msg
19557 = G_("%qD was not declared in this scope, "
19558 "and no declarations were found by "
19559 "argument-dependent lookup at the point "
19560 "of instantiation");
19561
19562 bool diag = true;
19563 if (in_lambda)
19564 error_at (cp_expr_loc_or_input_loc (t),
19565 msg, function);
19566 else
19567 diag = permerror (cp_expr_loc_or_input_loc (t),
19568 msg, function);
19569 if (diag)
19570 {
19571 tree fn = unq;
19572
19573 if (INDIRECT_REF_P (fn))
19574 fn = TREE_OPERAND (fn, 0);
19575 if (is_overloaded_fn (fn))
19576 fn = get_first_fn (fn);
19577
19578 if (!DECL_P (fn))
19579 /* Can't say anything more. */;
19580 else if (DECL_CLASS_SCOPE_P (fn))
19581 {
19582 location_t loc = cp_expr_loc_or_input_loc (t);
19583 inform (loc,
19584 "declarations in dependent base %qT are "
19585 "not found by unqualified lookup",
19586 DECL_CLASS_CONTEXT (fn));
19587 if (current_class_ptr)
19588 inform (loc,
19589 "use %<this->%D%> instead", function);
19590 else
19591 inform (loc,
19592 "use %<%T::%D%> instead",
19593 current_class_name, function);
19594 }
19595 else
19596 inform (DECL_SOURCE_LOCATION (fn),
19597 "%qD declared here, later in the "
19598 "translation unit", fn);
19599 if (in_lambda)
19600 RETURN (error_mark_node);
19601 }
19602
19603 function = unq;
19604 }
19605 }
19606 if (identifier_p (function))
19607 {
19608 if (complain & tf_error)
19609 unqualified_name_lookup_error (function);
19610 RETURN (error_mark_node);
19611 }
19612 }
19613
19614 /* Remember that there was a reference to this entity. */
19615 if (function != NULL_TREE
19616 && DECL_P (function)
19617 && !mark_used (function, complain) && !(complain & tf_error))
19618 RETURN (error_mark_node);
19619
19620 /* Put back tf_decltype for the actual call. */
19621 complain |= decltype_flag;
19622
19623 if (function == NULL_TREE)
19624 switch (CALL_EXPR_IFN (t))
19625 {
19626 case IFN_LAUNDER:
19627 gcc_assert (nargs == 1);
19628 if (vec_safe_length (call_args) != 1)
19629 {
19630 error_at (cp_expr_loc_or_input_loc (t),
19631 "wrong number of arguments to "
19632 "%<__builtin_launder%>");
19633 ret = error_mark_node;
19634 }
19635 else
19636 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
19637 (*call_args)[0], complain);
19638 break;
19639
19640 case IFN_VEC_CONVERT:
19641 gcc_assert (nargs == 1);
19642 if (vec_safe_length (call_args) != 1)
19643 {
19644 error_at (cp_expr_loc_or_input_loc (t),
19645 "wrong number of arguments to "
19646 "%<__builtin_convertvector%>");
19647 ret = error_mark_node;
19648 break;
19649 }
19650 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19651 tsubst (TREE_TYPE (t), args,
19652 complain, in_decl),
19653 complain);
19654 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19655 RETURN (ret);
19656 break;
19657
19658 default:
19659 /* Unsupported internal function with arguments. */
19660 gcc_unreachable ();
19661 }
19662 else if (TREE_CODE (function) == OFFSET_REF
19663 || TREE_CODE (function) == DOTSTAR_EXPR
19664 || TREE_CODE (function) == MEMBER_REF)
19665 ret = build_offset_ref_call_from_tree (function, &call_args,
19666 complain);
19667 else if (TREE_CODE (function) == COMPONENT_REF)
19668 {
19669 tree instance = TREE_OPERAND (function, 0);
19670 tree fn = TREE_OPERAND (function, 1);
19671
19672 if (processing_template_decl
19673 && (type_dependent_expression_p (instance)
19674 || (!BASELINK_P (fn)
19675 && TREE_CODE (fn) != FIELD_DECL)
19676 || type_dependent_expression_p (fn)
19677 || any_type_dependent_arguments_p (call_args)))
19678 ret = build_min_nt_call_vec (function, call_args);
19679 else if (!BASELINK_P (fn))
19680 ret = finish_call_expr (function, &call_args,
19681 /*disallow_virtual=*/false,
19682 /*koenig_p=*/false,
19683 complain);
19684 else
19685 ret = (build_new_method_call
19686 (instance, fn,
19687 &call_args, NULL_TREE,
19688 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19689 /*fn_p=*/NULL,
19690 complain));
19691 }
19692 else if (concept_check_p (function))
19693 {
19694 /* FUNCTION is a template-id referring to a concept definition. */
19695 tree id = unpack_concept_check (function);
19696 tree tmpl = TREE_OPERAND (id, 0);
19697 tree args = TREE_OPERAND (id, 1);
19698
19699 /* Calls to standard and variable concepts should have been
19700 previously diagnosed. */
19701 gcc_assert (function_concept_p (tmpl));
19702
19703 /* Ensure the result is wrapped as a call expression. */
19704 ret = build_concept_check (tmpl, args, tf_warning_or_error);
19705 }
19706 else
19707 ret = finish_call_expr (function, &call_args,
19708 /*disallow_virtual=*/qualified_p,
19709 koenig_p,
19710 complain);
19711
19712 if (ret != error_mark_node)
19713 {
19714 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19715 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19716 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19717 if (op || ord || rev)
19718 {
19719 function = extract_call_expr (ret);
19720 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19721 CALL_EXPR_ORDERED_ARGS (function) = ord;
19722 CALL_EXPR_REVERSE_ARGS (function) = rev;
19723 }
19724 }
19725
19726 RETURN (ret);
19727 }
19728
19729 case COND_EXPR:
19730 {
19731 tree cond = RECUR (TREE_OPERAND (t, 0));
19732 cond = mark_rvalue_use (cond);
19733 tree folded_cond = fold_non_dependent_expr (cond, complain);
19734 tree exp1, exp2;
19735
19736 if (TREE_CODE (folded_cond) == INTEGER_CST)
19737 {
19738 if (integer_zerop (folded_cond))
19739 {
19740 ++c_inhibit_evaluation_warnings;
19741 exp1 = RECUR (TREE_OPERAND (t, 1));
19742 --c_inhibit_evaluation_warnings;
19743 exp2 = RECUR (TREE_OPERAND (t, 2));
19744 }
19745 else
19746 {
19747 exp1 = RECUR (TREE_OPERAND (t, 1));
19748 ++c_inhibit_evaluation_warnings;
19749 exp2 = RECUR (TREE_OPERAND (t, 2));
19750 --c_inhibit_evaluation_warnings;
19751 }
19752 cond = folded_cond;
19753 }
19754 else
19755 {
19756 exp1 = RECUR (TREE_OPERAND (t, 1));
19757 exp2 = RECUR (TREE_OPERAND (t, 2));
19758 }
19759
19760 warning_sentinel s(warn_duplicated_branches);
19761 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19762 cond, exp1, exp2, complain));
19763 }
19764
19765 case PSEUDO_DTOR_EXPR:
19766 {
19767 tree op0 = RECUR (TREE_OPERAND (t, 0));
19768 tree op1 = RECUR (TREE_OPERAND (t, 1));
19769 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19770 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19771 input_location));
19772 }
19773
19774 case TREE_LIST:
19775 {
19776 tree purpose, value, chain;
19777
19778 if (t == void_list_node)
19779 RETURN (t);
19780
19781 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19782 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19783 {
19784 /* We have pack expansions, so expand those and
19785 create a new list out of it. */
19786 tree purposevec = NULL_TREE;
19787 tree valuevec = NULL_TREE;
19788 tree chain;
19789 int i, len = -1;
19790
19791 /* Expand the argument expressions. */
19792 if (TREE_PURPOSE (t))
19793 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19794 complain, in_decl);
19795 if (TREE_VALUE (t))
19796 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19797 complain, in_decl);
19798
19799 /* Build the rest of the list. */
19800 chain = TREE_CHAIN (t);
19801 if (chain && chain != void_type_node)
19802 chain = RECUR (chain);
19803
19804 /* Determine the number of arguments. */
19805 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19806 {
19807 len = TREE_VEC_LENGTH (purposevec);
19808 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19809 }
19810 else if (TREE_CODE (valuevec) == TREE_VEC)
19811 len = TREE_VEC_LENGTH (valuevec);
19812 else
19813 {
19814 /* Since we only performed a partial substitution into
19815 the argument pack, we only RETURN (a single list
19816 node. */
19817 if (purposevec == TREE_PURPOSE (t)
19818 && valuevec == TREE_VALUE (t)
19819 && chain == TREE_CHAIN (t))
19820 RETURN (t);
19821
19822 RETURN (tree_cons (purposevec, valuevec, chain));
19823 }
19824
19825 /* Convert the argument vectors into a TREE_LIST */
19826 i = len;
19827 while (i > 0)
19828 {
19829 /* Grab the Ith values. */
19830 i--;
19831 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19832 : NULL_TREE;
19833 value
19834 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19835 : NULL_TREE;
19836
19837 /* Build the list (backwards). */
19838 chain = tree_cons (purpose, value, chain);
19839 }
19840
19841 RETURN (chain);
19842 }
19843
19844 purpose = TREE_PURPOSE (t);
19845 if (purpose)
19846 purpose = RECUR (purpose);
19847 value = TREE_VALUE (t);
19848 if (value)
19849 value = RECUR (value);
19850 chain = TREE_CHAIN (t);
19851 if (chain && chain != void_type_node)
19852 chain = RECUR (chain);
19853 if (purpose == TREE_PURPOSE (t)
19854 && value == TREE_VALUE (t)
19855 && chain == TREE_CHAIN (t))
19856 RETURN (t);
19857 RETURN (tree_cons (purpose, value, chain));
19858 }
19859
19860 case COMPONENT_REF:
19861 {
19862 tree object;
19863 tree object_type;
19864 tree member;
19865 tree r;
19866
19867 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19868 args, complain, in_decl);
19869 /* Remember that there was a reference to this entity. */
19870 if (DECL_P (object)
19871 && !mark_used (object, complain) && !(complain & tf_error))
19872 RETURN (error_mark_node);
19873 object_type = TREE_TYPE (object);
19874
19875 member = TREE_OPERAND (t, 1);
19876 if (BASELINK_P (member))
19877 member = tsubst_baselink (member,
19878 non_reference (TREE_TYPE (object)),
19879 args, complain, in_decl);
19880 else
19881 member = tsubst_copy (member, args, complain, in_decl);
19882 if (member == error_mark_node)
19883 RETURN (error_mark_node);
19884
19885 if (TREE_CODE (member) == FIELD_DECL)
19886 {
19887 r = finish_non_static_data_member (member, object, NULL_TREE);
19888 if (TREE_CODE (r) == COMPONENT_REF)
19889 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19890 RETURN (r);
19891 }
19892 else if (type_dependent_expression_p (object))
19893 /* We can't do much here. */;
19894 else if (!CLASS_TYPE_P (object_type))
19895 {
19896 if (scalarish_type_p (object_type))
19897 {
19898 tree s = NULL_TREE;
19899 tree dtor = member;
19900
19901 if (TREE_CODE (dtor) == SCOPE_REF)
19902 {
19903 s = TREE_OPERAND (dtor, 0);
19904 dtor = TREE_OPERAND (dtor, 1);
19905 }
19906 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19907 {
19908 dtor = TREE_OPERAND (dtor, 0);
19909 if (TYPE_P (dtor))
19910 RETURN (finish_pseudo_destructor_expr
19911 (object, s, dtor, input_location));
19912 }
19913 }
19914 }
19915 else if (TREE_CODE (member) == SCOPE_REF
19916 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19917 {
19918 /* Lookup the template functions now that we know what the
19919 scope is. */
19920 tree scope = TREE_OPERAND (member, 0);
19921 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19922 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19923 member = lookup_qualified_name (scope, tmpl,
19924 /*is_type_p=*/false,
19925 /*complain=*/false);
19926 if (BASELINK_P (member))
19927 {
19928 BASELINK_FUNCTIONS (member)
19929 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19930 args);
19931 member = (adjust_result_of_qualified_name_lookup
19932 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19933 object_type));
19934 }
19935 else
19936 {
19937 qualified_name_lookup_error (scope, tmpl, member,
19938 input_location);
19939 RETURN (error_mark_node);
19940 }
19941 }
19942 else if (TREE_CODE (member) == SCOPE_REF
19943 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19944 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19945 {
19946 if (complain & tf_error)
19947 {
19948 if (TYPE_P (TREE_OPERAND (member, 0)))
19949 error ("%qT is not a class or namespace",
19950 TREE_OPERAND (member, 0));
19951 else
19952 error ("%qD is not a class or namespace",
19953 TREE_OPERAND (member, 0));
19954 }
19955 RETURN (error_mark_node);
19956 }
19957
19958 r = finish_class_member_access_expr (object, member,
19959 /*template_p=*/false,
19960 complain);
19961 if (TREE_CODE (r) == COMPONENT_REF)
19962 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19963 RETURN (r);
19964 }
19965
19966 case THROW_EXPR:
19967 RETURN (build_throw
19968 (input_location, RECUR (TREE_OPERAND (t, 0))));
19969
19970 case CONSTRUCTOR:
19971 {
19972 vec<constructor_elt, va_gc> *n;
19973 constructor_elt *ce;
19974 unsigned HOST_WIDE_INT idx;
19975 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19976 bool process_index_p;
19977 int newlen;
19978 bool need_copy_p = false;
19979 tree r;
19980
19981 if (type == error_mark_node)
19982 RETURN (error_mark_node);
19983
19984 /* We do not want to process the index of aggregate
19985 initializers as they are identifier nodes which will be
19986 looked up by digest_init. */
19987 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19988
19989 if (null_member_pointer_value_p (t))
19990 {
19991 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19992 RETURN (t);
19993 }
19994
19995 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19996 newlen = vec_safe_length (n);
19997 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19998 {
19999 if (ce->index && process_index_p
20000 /* An identifier index is looked up in the type
20001 being initialized, not the current scope. */
20002 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20003 ce->index = RECUR (ce->index);
20004
20005 if (PACK_EXPANSION_P (ce->value))
20006 {
20007 /* Substitute into the pack expansion. */
20008 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20009 in_decl);
20010
20011 if (ce->value == error_mark_node
20012 || PACK_EXPANSION_P (ce->value))
20013 ;
20014 else if (TREE_VEC_LENGTH (ce->value) == 1)
20015 /* Just move the argument into place. */
20016 ce->value = TREE_VEC_ELT (ce->value, 0);
20017 else
20018 {
20019 /* Update the length of the final CONSTRUCTOR
20020 arguments vector, and note that we will need to
20021 copy.*/
20022 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20023 need_copy_p = true;
20024 }
20025 }
20026 else
20027 ce->value = RECUR (ce->value);
20028 }
20029
20030 if (need_copy_p)
20031 {
20032 vec<constructor_elt, va_gc> *old_n = n;
20033
20034 vec_alloc (n, newlen);
20035 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20036 {
20037 if (TREE_CODE (ce->value) == TREE_VEC)
20038 {
20039 int i, len = TREE_VEC_LENGTH (ce->value);
20040 for (i = 0; i < len; ++i)
20041 CONSTRUCTOR_APPEND_ELT (n, 0,
20042 TREE_VEC_ELT (ce->value, i));
20043 }
20044 else
20045 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20046 }
20047 }
20048
20049 r = build_constructor (init_list_type_node, n);
20050 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20051 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20052 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20053
20054 if (TREE_HAS_CONSTRUCTOR (t))
20055 {
20056 fcl_t cl = fcl_functional;
20057 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20058 cl = fcl_c99;
20059 RETURN (finish_compound_literal (type, r, complain, cl));
20060 }
20061
20062 TREE_TYPE (r) = type;
20063 RETURN (r);
20064 }
20065
20066 case TYPEID_EXPR:
20067 {
20068 tree operand_0 = TREE_OPERAND (t, 0);
20069 if (TYPE_P (operand_0))
20070 {
20071 operand_0 = tsubst (operand_0, args, complain, in_decl);
20072 RETURN (get_typeid (operand_0, complain));
20073 }
20074 else
20075 {
20076 operand_0 = RECUR (operand_0);
20077 RETURN (build_typeid (operand_0, complain));
20078 }
20079 }
20080
20081 case VAR_DECL:
20082 if (!args)
20083 RETURN (t);
20084 /* Fall through */
20085
20086 case PARM_DECL:
20087 {
20088 tree r = tsubst_copy (t, args, complain, in_decl);
20089 /* ??? We're doing a subset of finish_id_expression here. */
20090 if (tree wrap = maybe_get_tls_wrapper_call (r))
20091 /* Replace an evaluated use of the thread_local variable with
20092 a call to its wrapper. */
20093 r = wrap;
20094 else if (outer_automatic_var_p (r))
20095 r = process_outer_var_ref (r, complain);
20096
20097 if (!TYPE_REF_P (TREE_TYPE (t)))
20098 /* If the original type was a reference, we'll be wrapped in
20099 the appropriate INDIRECT_REF. */
20100 r = convert_from_reference (r);
20101 RETURN (r);
20102 }
20103
20104 case VA_ARG_EXPR:
20105 {
20106 tree op0 = RECUR (TREE_OPERAND (t, 0));
20107 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20108 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20109 }
20110
20111 case OFFSETOF_EXPR:
20112 {
20113 tree object_ptr
20114 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20115 in_decl, /*function_p=*/false,
20116 /*integral_constant_expression_p=*/false);
20117 RETURN (finish_offsetof (object_ptr,
20118 RECUR (TREE_OPERAND (t, 0)),
20119 EXPR_LOCATION (t)));
20120 }
20121
20122 case ADDRESSOF_EXPR:
20123 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20124 RECUR (TREE_OPERAND (t, 0)), complain));
20125
20126 case TRAIT_EXPR:
20127 {
20128 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20129 complain, in_decl);
20130
20131 tree type2 = TRAIT_EXPR_TYPE2 (t);
20132 if (type2 && TREE_CODE (type2) == TREE_LIST)
20133 type2 = RECUR (type2);
20134 else if (type2)
20135 type2 = tsubst (type2, args, complain, in_decl);
20136
20137 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20138 TRAIT_EXPR_KIND (t), type1, type2));
20139 }
20140
20141 case STMT_EXPR:
20142 {
20143 tree old_stmt_expr = cur_stmt_expr;
20144 tree stmt_expr = begin_stmt_expr ();
20145
20146 cur_stmt_expr = stmt_expr;
20147 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20148 integral_constant_expression_p);
20149 stmt_expr = finish_stmt_expr (stmt_expr, false);
20150 cur_stmt_expr = old_stmt_expr;
20151
20152 /* If the resulting list of expression statement is empty,
20153 fold it further into void_node. */
20154 if (empty_expr_stmt_p (stmt_expr))
20155 stmt_expr = void_node;
20156
20157 RETURN (stmt_expr);
20158 }
20159
20160 case LAMBDA_EXPR:
20161 {
20162 if (complain & tf_partial)
20163 {
20164 /* We don't have a full set of template arguments yet; don't touch
20165 the lambda at all. */
20166 gcc_assert (processing_template_decl);
20167 return t;
20168 }
20169 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20170
20171 RETURN (build_lambda_object (r));
20172 }
20173
20174 case TARGET_EXPR:
20175 /* We can get here for a constant initializer of non-dependent type.
20176 FIXME stop folding in cp_parser_initializer_clause. */
20177 {
20178 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20179 complain);
20180 RETURN (r);
20181 }
20182
20183 case TRANSACTION_EXPR:
20184 RETURN (tsubst_expr(t, args, complain, in_decl,
20185 integral_constant_expression_p));
20186
20187 case PAREN_EXPR:
20188 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20189
20190 case VEC_PERM_EXPR:
20191 {
20192 tree op0 = RECUR (TREE_OPERAND (t, 0));
20193 tree op1 = RECUR (TREE_OPERAND (t, 1));
20194 tree op2 = RECUR (TREE_OPERAND (t, 2));
20195 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20196 complain));
20197 }
20198
20199 case REQUIRES_EXPR:
20200 {
20201 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20202 if (r == error_mark_node && (complain & tf_error))
20203 tsubst_requires_expr (t, args, complain, in_decl);
20204 RETURN (r);
20205 }
20206
20207 case RANGE_EXPR:
20208 /* No need to substitute further, a RANGE_EXPR will always be built
20209 with constant operands. */
20210 RETURN (t);
20211
20212 case NON_LVALUE_EXPR:
20213 case VIEW_CONVERT_EXPR:
20214 if (location_wrapper_p (t))
20215 /* We need to do this here as well as in tsubst_copy so we get the
20216 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20217 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20218 EXPR_LOCATION (t)));
20219 /* fallthrough. */
20220
20221 default:
20222 /* Handle Objective-C++ constructs, if appropriate. */
20223 {
20224 tree subst
20225 = objcp_tsubst_copy_and_build (t, args, complain,
20226 in_decl, /*function_p=*/false);
20227 if (subst)
20228 RETURN (subst);
20229 }
20230 RETURN (tsubst_copy (t, args, complain, in_decl));
20231 }
20232
20233 #undef RECUR
20234 #undef RETURN
20235 out:
20236 input_location = save_loc;
20237 return retval;
20238 }
20239
20240 /* Verify that the instantiated ARGS are valid. For type arguments,
20241 make sure that the type's linkage is ok. For non-type arguments,
20242 make sure they are constants if they are integral or enumerations.
20243 Emit an error under control of COMPLAIN, and return TRUE on error. */
20244
20245 static bool
20246 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20247 {
20248 if (dependent_template_arg_p (t))
20249 return false;
20250 if (ARGUMENT_PACK_P (t))
20251 {
20252 tree vec = ARGUMENT_PACK_ARGS (t);
20253 int len = TREE_VEC_LENGTH (vec);
20254 bool result = false;
20255 int i;
20256
20257 for (i = 0; i < len; ++i)
20258 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20259 result = true;
20260 return result;
20261 }
20262 else if (TYPE_P (t))
20263 {
20264 /* [basic.link]: A name with no linkage (notably, the name
20265 of a class or enumeration declared in a local scope)
20266 shall not be used to declare an entity with linkage.
20267 This implies that names with no linkage cannot be used as
20268 template arguments
20269
20270 DR 757 relaxes this restriction for C++0x. */
20271 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20272 : no_linkage_check (t, /*relaxed_p=*/false));
20273
20274 if (nt)
20275 {
20276 /* DR 488 makes use of a type with no linkage cause
20277 type deduction to fail. */
20278 if (complain & tf_error)
20279 {
20280 if (TYPE_UNNAMED_P (nt))
20281 error ("%qT is/uses unnamed type", t);
20282 else
20283 error ("template argument for %qD uses local type %qT",
20284 tmpl, t);
20285 }
20286 return true;
20287 }
20288 /* In order to avoid all sorts of complications, we do not
20289 allow variably-modified types as template arguments. */
20290 else if (variably_modified_type_p (t, NULL_TREE))
20291 {
20292 if (complain & tf_error)
20293 error ("%qT is a variably modified type", t);
20294 return true;
20295 }
20296 }
20297 /* Class template and alias template arguments should be OK. */
20298 else if (DECL_TYPE_TEMPLATE_P (t))
20299 ;
20300 /* A non-type argument of integral or enumerated type must be a
20301 constant. */
20302 else if (TREE_TYPE (t)
20303 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20304 && !REFERENCE_REF_P (t)
20305 && !TREE_CONSTANT (t))
20306 {
20307 if (complain & tf_error)
20308 error ("integral expression %qE is not constant", t);
20309 return true;
20310 }
20311 return false;
20312 }
20313
20314 static bool
20315 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20316 {
20317 int ix, len = DECL_NTPARMS (tmpl);
20318 bool result = false;
20319
20320 for (ix = 0; ix != len; ix++)
20321 {
20322 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20323 result = true;
20324 }
20325 if (result && (complain & tf_error))
20326 error (" trying to instantiate %qD", tmpl);
20327 return result;
20328 }
20329
20330 /* We're out of SFINAE context now, so generate diagnostics for the access
20331 errors we saw earlier when instantiating D from TMPL and ARGS. */
20332
20333 static void
20334 recheck_decl_substitution (tree d, tree tmpl, tree args)
20335 {
20336 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20337 tree type = TREE_TYPE (pattern);
20338 location_t loc = input_location;
20339
20340 push_access_scope (d);
20341 push_deferring_access_checks (dk_no_deferred);
20342 input_location = DECL_SOURCE_LOCATION (pattern);
20343 tsubst (type, args, tf_warning_or_error, d);
20344 input_location = loc;
20345 pop_deferring_access_checks ();
20346 pop_access_scope (d);
20347 }
20348
20349 /* Instantiate the indicated variable, function, or alias template TMPL with
20350 the template arguments in TARG_PTR. */
20351
20352 static tree
20353 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20354 {
20355 tree targ_ptr = orig_args;
20356 tree fndecl;
20357 tree gen_tmpl;
20358 tree spec;
20359 bool access_ok = true;
20360
20361 if (tmpl == error_mark_node)
20362 return error_mark_node;
20363
20364 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20365
20366 /* If this function is a clone, handle it specially. */
20367 if (DECL_CLONED_FUNCTION_P (tmpl))
20368 {
20369 tree spec;
20370 tree clone;
20371
20372 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20373 DECL_CLONED_FUNCTION. */
20374 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20375 targ_ptr, complain);
20376 if (spec == error_mark_node)
20377 return error_mark_node;
20378
20379 /* Look for the clone. */
20380 FOR_EACH_CLONE (clone, spec)
20381 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20382 return clone;
20383 /* We should always have found the clone by now. */
20384 gcc_unreachable ();
20385 return NULL_TREE;
20386 }
20387
20388 if (targ_ptr == error_mark_node)
20389 return error_mark_node;
20390
20391 /* Check to see if we already have this specialization. */
20392 gen_tmpl = most_general_template (tmpl);
20393 if (TMPL_ARGS_DEPTH (targ_ptr)
20394 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20395 /* targ_ptr only has the innermost template args, so add the outer ones
20396 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20397 the case of a non-dependent call within a template definition). */
20398 targ_ptr = (add_outermost_template_args
20399 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20400 targ_ptr));
20401
20402 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20403 but it doesn't seem to be on the hot path. */
20404 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20405
20406 gcc_assert (tmpl == gen_tmpl
20407 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
20408 == spec)
20409 || fndecl == NULL_TREE);
20410
20411 if (spec != NULL_TREE)
20412 {
20413 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20414 {
20415 if (complain & tf_error)
20416 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20417 return error_mark_node;
20418 }
20419 return spec;
20420 }
20421
20422 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20423 complain))
20424 return error_mark_node;
20425
20426 /* We are building a FUNCTION_DECL, during which the access of its
20427 parameters and return types have to be checked. However this
20428 FUNCTION_DECL which is the desired context for access checking
20429 is not built yet. We solve this chicken-and-egg problem by
20430 deferring all checks until we have the FUNCTION_DECL. */
20431 push_deferring_access_checks (dk_deferred);
20432
20433 /* Instantiation of the function happens in the context of the function
20434 template, not the context of the overload resolution we're doing. */
20435 push_to_top_level ();
20436 /* If there are dependent arguments, e.g. because we're doing partial
20437 ordering, make sure processing_template_decl stays set. */
20438 if (uses_template_parms (targ_ptr))
20439 ++processing_template_decl;
20440 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20441 {
20442 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20443 complain, gen_tmpl, true);
20444 push_nested_class (ctx);
20445 }
20446
20447 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20448
20449 fndecl = NULL_TREE;
20450 if (VAR_P (pattern))
20451 {
20452 /* We need to determine if we're using a partial or explicit
20453 specialization now, because the type of the variable could be
20454 different. */
20455 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20456 tree elt = most_specialized_partial_spec (tid, complain);
20457 if (elt == error_mark_node)
20458 pattern = error_mark_node;
20459 else if (elt)
20460 {
20461 tree partial_tmpl = TREE_VALUE (elt);
20462 tree partial_args = TREE_PURPOSE (elt);
20463 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20464 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20465 }
20466 }
20467
20468 /* Substitute template parameters to obtain the specialization. */
20469 if (fndecl == NULL_TREE)
20470 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20471 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20472 pop_nested_class ();
20473 pop_from_top_level ();
20474
20475 if (fndecl == error_mark_node)
20476 {
20477 pop_deferring_access_checks ();
20478 return error_mark_node;
20479 }
20480
20481 /* The DECL_TI_TEMPLATE should always be the immediate parent
20482 template, not the most general template. */
20483 DECL_TI_TEMPLATE (fndecl) = tmpl;
20484 DECL_TI_ARGS (fndecl) = targ_ptr;
20485
20486 /* Now we know the specialization, compute access previously
20487 deferred. Do no access control for inheriting constructors,
20488 as we already checked access for the inherited constructor. */
20489 if (!(flag_new_inheriting_ctors
20490 && DECL_INHERITED_CTOR (fndecl)))
20491 {
20492 push_access_scope (fndecl);
20493 if (!perform_deferred_access_checks (complain))
20494 access_ok = false;
20495 pop_access_scope (fndecl);
20496 }
20497 pop_deferring_access_checks ();
20498
20499 /* If we've just instantiated the main entry point for a function,
20500 instantiate all the alternate entry points as well. We do this
20501 by cloning the instantiation of the main entry point, not by
20502 instantiating the template clones. */
20503 if (tree chain = DECL_CHAIN (gen_tmpl))
20504 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20505 clone_function_decl (fndecl, /*update_methods=*/false);
20506
20507 if (!access_ok)
20508 {
20509 if (!(complain & tf_error))
20510 {
20511 /* Remember to reinstantiate when we're out of SFINAE so the user
20512 can see the errors. */
20513 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20514 }
20515 return error_mark_node;
20516 }
20517 return fndecl;
20518 }
20519
20520 /* Wrapper for instantiate_template_1. */
20521
20522 tree
20523 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20524 {
20525 tree ret;
20526 timevar_push (TV_TEMPLATE_INST);
20527 ret = instantiate_template_1 (tmpl, orig_args, complain);
20528 timevar_pop (TV_TEMPLATE_INST);
20529 return ret;
20530 }
20531
20532 /* Instantiate the alias template TMPL with ARGS. Also push a template
20533 instantiation level, which instantiate_template doesn't do because
20534 functions and variables have sufficient context established by the
20535 callers. */
20536
20537 static tree
20538 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20539 {
20540 if (tmpl == error_mark_node || args == error_mark_node)
20541 return error_mark_node;
20542
20543 args =
20544 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20545 args, tmpl, complain,
20546 /*require_all_args=*/true,
20547 /*use_default_args=*/true);
20548
20549 /* FIXME check for satisfaction in check_instantiated_args. */
20550 if (flag_concepts
20551 && !any_dependent_template_arguments_p (args)
20552 && !constraints_satisfied_p (tmpl, args))
20553 {
20554 if (complain & tf_error)
20555 {
20556 auto_diagnostic_group d;
20557 error ("template constraint failure for %qD", tmpl);
20558 diagnose_constraints (input_location, tmpl, args);
20559 }
20560 return error_mark_node;
20561 }
20562
20563 if (!push_tinst_level (tmpl, args))
20564 return error_mark_node;
20565 tree r = instantiate_template (tmpl, args, complain);
20566 pop_tinst_level ();
20567
20568 return r;
20569 }
20570
20571 /* PARM is a template parameter pack for FN. Returns true iff
20572 PARM is used in a deducible way in the argument list of FN. */
20573
20574 static bool
20575 pack_deducible_p (tree parm, tree fn)
20576 {
20577 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20578 for (; t; t = TREE_CHAIN (t))
20579 {
20580 tree type = TREE_VALUE (t);
20581 tree packs;
20582 if (!PACK_EXPANSION_P (type))
20583 continue;
20584 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20585 packs; packs = TREE_CHAIN (packs))
20586 if (template_args_equal (TREE_VALUE (packs), parm))
20587 {
20588 /* The template parameter pack is used in a function parameter
20589 pack. If this is the end of the parameter list, the
20590 template parameter pack is deducible. */
20591 if (TREE_CHAIN (t) == void_list_node)
20592 return true;
20593 else
20594 /* Otherwise, not. Well, it could be deduced from
20595 a non-pack parameter, but doing so would end up with
20596 a deduction mismatch, so don't bother. */
20597 return false;
20598 }
20599 }
20600 /* The template parameter pack isn't used in any function parameter
20601 packs, but it might be used deeper, e.g. tuple<Args...>. */
20602 return true;
20603 }
20604
20605 /* Subroutine of fn_type_unification: check non-dependent parms for
20606 convertibility. */
20607
20608 static int
20609 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20610 tree fn, unification_kind_t strict, int flags,
20611 struct conversion **convs, bool explain_p)
20612 {
20613 /* Non-constructor methods need to leave a conversion for 'this', which
20614 isn't included in nargs here. */
20615 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20616 && !DECL_CONSTRUCTOR_P (fn));
20617
20618 for (unsigned ia = 0;
20619 parms && parms != void_list_node && ia < nargs; )
20620 {
20621 tree parm = TREE_VALUE (parms);
20622
20623 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20624 && (!TREE_CHAIN (parms)
20625 || TREE_CHAIN (parms) == void_list_node))
20626 /* For a function parameter pack that occurs at the end of the
20627 parameter-declaration-list, the type A of each remaining
20628 argument of the call is compared with the type P of the
20629 declarator-id of the function parameter pack. */
20630 break;
20631
20632 parms = TREE_CHAIN (parms);
20633
20634 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20635 /* For a function parameter pack that does not occur at the
20636 end of the parameter-declaration-list, the type of the
20637 parameter pack is a non-deduced context. */
20638 continue;
20639
20640 if (!uses_template_parms (parm))
20641 {
20642 tree arg = args[ia];
20643 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20644 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20645
20646 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20647 conv_p, explain_p))
20648 return 1;
20649 }
20650
20651 ++ia;
20652 }
20653
20654 return 0;
20655 }
20656
20657 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20658 NARGS elements of the arguments that are being used when calling
20659 it. TARGS is a vector into which the deduced template arguments
20660 are placed.
20661
20662 Returns either a FUNCTION_DECL for the matching specialization of FN or
20663 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20664 true, diagnostics will be printed to explain why it failed.
20665
20666 If FN is a conversion operator, or we are trying to produce a specific
20667 specialization, RETURN_TYPE is the return type desired.
20668
20669 The EXPLICIT_TARGS are explicit template arguments provided via a
20670 template-id.
20671
20672 The parameter STRICT is one of:
20673
20674 DEDUCE_CALL:
20675 We are deducing arguments for a function call, as in
20676 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20677 deducing arguments for a call to the result of a conversion
20678 function template, as in [over.call.object].
20679
20680 DEDUCE_CONV:
20681 We are deducing arguments for a conversion function, as in
20682 [temp.deduct.conv].
20683
20684 DEDUCE_EXACT:
20685 We are deducing arguments when doing an explicit instantiation
20686 as in [temp.explicit], when determining an explicit specialization
20687 as in [temp.expl.spec], or when taking the address of a function
20688 template, as in [temp.deduct.funcaddr]. */
20689
20690 tree
20691 fn_type_unification (tree fn,
20692 tree explicit_targs,
20693 tree targs,
20694 const tree *args,
20695 unsigned int nargs,
20696 tree return_type,
20697 unification_kind_t strict,
20698 int flags,
20699 struct conversion **convs,
20700 bool explain_p,
20701 bool decltype_p)
20702 {
20703 tree parms;
20704 tree fntype;
20705 tree decl = NULL_TREE;
20706 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20707 bool ok;
20708 static int deduction_depth;
20709 /* type_unification_real will pass back any access checks from default
20710 template argument substitution. */
20711 vec<deferred_access_check, va_gc> *checks = NULL;
20712 /* We don't have all the template args yet. */
20713 bool incomplete = true;
20714
20715 tree orig_fn = fn;
20716 if (flag_new_inheriting_ctors)
20717 fn = strip_inheriting_ctors (fn);
20718
20719 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20720 tree r = error_mark_node;
20721
20722 tree full_targs = targs;
20723 if (TMPL_ARGS_DEPTH (targs)
20724 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20725 full_targs = (add_outermost_template_args
20726 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20727 targs));
20728
20729 if (decltype_p)
20730 complain |= tf_decltype;
20731
20732 /* In C++0x, it's possible to have a function template whose type depends
20733 on itself recursively. This is most obvious with decltype, but can also
20734 occur with enumeration scope (c++/48969). So we need to catch infinite
20735 recursion and reject the substitution at deduction time; this function
20736 will return error_mark_node for any repeated substitution.
20737
20738 This also catches excessive recursion such as when f<N> depends on
20739 f<N-1> across all integers, and returns error_mark_node for all the
20740 substitutions back up to the initial one.
20741
20742 This is, of course, not reentrant. */
20743 if (excessive_deduction_depth)
20744 return error_mark_node;
20745 ++deduction_depth;
20746
20747 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20748
20749 fntype = TREE_TYPE (fn);
20750 if (explicit_targs)
20751 {
20752 /* [temp.deduct]
20753
20754 The specified template arguments must match the template
20755 parameters in kind (i.e., type, nontype, template), and there
20756 must not be more arguments than there are parameters;
20757 otherwise type deduction fails.
20758
20759 Nontype arguments must match the types of the corresponding
20760 nontype template parameters, or must be convertible to the
20761 types of the corresponding nontype parameters as specified in
20762 _temp.arg.nontype_, otherwise type deduction fails.
20763
20764 All references in the function type of the function template
20765 to the corresponding template parameters are replaced by the
20766 specified template argument values. If a substitution in a
20767 template parameter or in the function type of the function
20768 template results in an invalid type, type deduction fails. */
20769 int i, len = TREE_VEC_LENGTH (tparms);
20770 location_t loc = input_location;
20771 incomplete = false;
20772
20773 if (explicit_targs == error_mark_node)
20774 goto fail;
20775
20776 if (TMPL_ARGS_DEPTH (explicit_targs)
20777 < TMPL_ARGS_DEPTH (full_targs))
20778 explicit_targs = add_outermost_template_args (full_targs,
20779 explicit_targs);
20780
20781 /* Adjust any explicit template arguments before entering the
20782 substitution context. */
20783 explicit_targs
20784 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20785 complain|tf_partial,
20786 /*require_all_args=*/false,
20787 /*use_default_args=*/false));
20788 if (explicit_targs == error_mark_node)
20789 goto fail;
20790
20791 /* Substitute the explicit args into the function type. This is
20792 necessary so that, for instance, explicitly declared function
20793 arguments can match null pointed constants. If we were given
20794 an incomplete set of explicit args, we must not do semantic
20795 processing during substitution as we could create partial
20796 instantiations. */
20797 for (i = 0; i < len; i++)
20798 {
20799 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20800 bool parameter_pack = false;
20801 tree targ = TREE_VEC_ELT (explicit_targs, i);
20802
20803 /* Dig out the actual parm. */
20804 if (TREE_CODE (parm) == TYPE_DECL
20805 || TREE_CODE (parm) == TEMPLATE_DECL)
20806 {
20807 parm = TREE_TYPE (parm);
20808 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20809 }
20810 else if (TREE_CODE (parm) == PARM_DECL)
20811 {
20812 parm = DECL_INITIAL (parm);
20813 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20814 }
20815
20816 if (targ == NULL_TREE)
20817 /* No explicit argument for this template parameter. */
20818 incomplete = true;
20819 else if (parameter_pack && pack_deducible_p (parm, fn))
20820 {
20821 /* Mark the argument pack as "incomplete". We could
20822 still deduce more arguments during unification.
20823 We remove this mark in type_unification_real. */
20824 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20825 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20826 = ARGUMENT_PACK_ARGS (targ);
20827
20828 /* We have some incomplete argument packs. */
20829 incomplete = true;
20830 }
20831 }
20832
20833 if (incomplete)
20834 {
20835 if (!push_tinst_level (fn, explicit_targs))
20836 {
20837 excessive_deduction_depth = true;
20838 goto fail;
20839 }
20840 ++processing_template_decl;
20841 input_location = DECL_SOURCE_LOCATION (fn);
20842 /* Ignore any access checks; we'll see them again in
20843 instantiate_template and they might have the wrong
20844 access path at this point. */
20845 push_deferring_access_checks (dk_deferred);
20846 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20847 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20848 pop_deferring_access_checks ();
20849 input_location = loc;
20850 --processing_template_decl;
20851 pop_tinst_level ();
20852
20853 if (fntype == error_mark_node)
20854 goto fail;
20855 }
20856
20857 /* Place the explicitly specified arguments in TARGS. */
20858 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20859 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20860 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20861 if (!incomplete && CHECKING_P
20862 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20863 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20864 (targs, NUM_TMPL_ARGS (explicit_targs));
20865 }
20866
20867 if (return_type && strict != DEDUCE_CALL)
20868 {
20869 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20870 new_args[0] = return_type;
20871 memcpy (new_args + 1, args, nargs * sizeof (tree));
20872 args = new_args;
20873 ++nargs;
20874 }
20875
20876 if (!incomplete)
20877 goto deduced;
20878
20879 /* Never do unification on the 'this' parameter. */
20880 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20881
20882 if (return_type && strict == DEDUCE_CALL)
20883 {
20884 /* We're deducing for a call to the result of a template conversion
20885 function. The parms we really want are in return_type. */
20886 if (INDIRECT_TYPE_P (return_type))
20887 return_type = TREE_TYPE (return_type);
20888 parms = TYPE_ARG_TYPES (return_type);
20889 }
20890 else if (return_type)
20891 {
20892 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20893 }
20894
20895 /* We allow incomplete unification without an error message here
20896 because the standard doesn't seem to explicitly prohibit it. Our
20897 callers must be ready to deal with unification failures in any
20898 event. */
20899
20900 /* If we aren't explaining yet, push tinst context so we can see where
20901 any errors (e.g. from class instantiations triggered by instantiation
20902 of default template arguments) come from. If we are explaining, this
20903 context is redundant. */
20904 if (!explain_p && !push_tinst_level (fn, targs))
20905 {
20906 excessive_deduction_depth = true;
20907 goto fail;
20908 }
20909
20910 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20911 full_targs, parms, args, nargs, /*subr=*/0,
20912 strict, &checks, explain_p);
20913 if (!explain_p)
20914 pop_tinst_level ();
20915 if (!ok)
20916 goto fail;
20917
20918 /* Now that we have bindings for all of the template arguments,
20919 ensure that the arguments deduced for the template template
20920 parameters have compatible template parameter lists. We cannot
20921 check this property before we have deduced all template
20922 arguments, because the template parameter types of a template
20923 template parameter might depend on prior template parameters
20924 deduced after the template template parameter. The following
20925 ill-formed example illustrates this issue:
20926
20927 template<typename T, template<T> class C> void f(C<5>, T);
20928
20929 template<int N> struct X {};
20930
20931 void g() {
20932 f(X<5>(), 5l); // error: template argument deduction fails
20933 }
20934
20935 The template parameter list of 'C' depends on the template type
20936 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20937 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20938 time that we deduce 'C'. */
20939 if (!template_template_parm_bindings_ok_p
20940 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20941 {
20942 unify_inconsistent_template_template_parameters (explain_p);
20943 goto fail;
20944 }
20945
20946 /* DR 1391: All parameters have args, now check non-dependent parms for
20947 convertibility. */
20948 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20949 convs, explain_p))
20950 goto fail;
20951
20952 deduced:
20953 /* All is well so far. Now, check:
20954
20955 [temp.deduct]
20956
20957 When all template arguments have been deduced, all uses of
20958 template parameters in nondeduced contexts are replaced with
20959 the corresponding deduced argument values. If the
20960 substitution results in an invalid type, as described above,
20961 type deduction fails. */
20962 if (!push_tinst_level (fn, targs))
20963 {
20964 excessive_deduction_depth = true;
20965 goto fail;
20966 }
20967
20968 /* Also collect access checks from the instantiation. */
20969 reopen_deferring_access_checks (checks);
20970
20971 decl = instantiate_template (fn, targs, complain);
20972
20973 checks = get_deferred_access_checks ();
20974 pop_deferring_access_checks ();
20975
20976 pop_tinst_level ();
20977
20978 if (decl == error_mark_node)
20979 goto fail;
20980
20981 /* Now perform any access checks encountered during substitution. */
20982 push_access_scope (decl);
20983 ok = perform_access_checks (checks, complain);
20984 pop_access_scope (decl);
20985 if (!ok)
20986 goto fail;
20987
20988 /* If we're looking for an exact match, check that what we got
20989 is indeed an exact match. It might not be if some template
20990 parameters are used in non-deduced contexts. But don't check
20991 for an exact match if we have dependent template arguments;
20992 in that case we're doing partial ordering, and we already know
20993 that we have two candidates that will provide the actual type. */
20994 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20995 {
20996 tree substed = TREE_TYPE (decl);
20997 unsigned int i;
20998
20999 tree sarg
21000 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21001 if (return_type)
21002 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21003 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21004 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21005 {
21006 unify_type_mismatch (explain_p, args[i],
21007 TREE_VALUE (sarg));
21008 goto fail;
21009 }
21010 }
21011
21012 /* After doing deduction with the inherited constructor, actually return an
21013 instantiation of the inheriting constructor. */
21014 if (orig_fn != fn)
21015 decl = instantiate_template (orig_fn, targs, complain);
21016
21017 r = decl;
21018
21019 fail:
21020 --deduction_depth;
21021 if (excessive_deduction_depth)
21022 {
21023 if (deduction_depth == 0)
21024 /* Reset once we're all the way out. */
21025 excessive_deduction_depth = false;
21026 }
21027
21028 return r;
21029 }
21030
21031 /* Adjust types before performing type deduction, as described in
21032 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21033 sections are symmetric. PARM is the type of a function parameter
21034 or the return type of the conversion function. ARG is the type of
21035 the argument passed to the call, or the type of the value
21036 initialized with the result of the conversion function.
21037 ARG_EXPR is the original argument expression, which may be null. */
21038
21039 static int
21040 maybe_adjust_types_for_deduction (unification_kind_t strict,
21041 tree* parm,
21042 tree* arg,
21043 tree arg_expr)
21044 {
21045 int result = 0;
21046
21047 switch (strict)
21048 {
21049 case DEDUCE_CALL:
21050 break;
21051
21052 case DEDUCE_CONV:
21053 /* Swap PARM and ARG throughout the remainder of this
21054 function; the handling is precisely symmetric since PARM
21055 will initialize ARG rather than vice versa. */
21056 std::swap (parm, arg);
21057 break;
21058
21059 case DEDUCE_EXACT:
21060 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21061 too, but here handle it by stripping the reference from PARM
21062 rather than by adding it to ARG. */
21063 if (TYPE_REF_P (*parm)
21064 && TYPE_REF_IS_RVALUE (*parm)
21065 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21066 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21067 && TYPE_REF_P (*arg)
21068 && !TYPE_REF_IS_RVALUE (*arg))
21069 *parm = TREE_TYPE (*parm);
21070 /* Nothing else to do in this case. */
21071 return 0;
21072
21073 default:
21074 gcc_unreachable ();
21075 }
21076
21077 if (!TYPE_REF_P (*parm))
21078 {
21079 /* [temp.deduct.call]
21080
21081 If P is not a reference type:
21082
21083 --If A is an array type, the pointer type produced by the
21084 array-to-pointer standard conversion (_conv.array_) is
21085 used in place of A for type deduction; otherwise,
21086
21087 --If A is a function type, the pointer type produced by
21088 the function-to-pointer standard conversion
21089 (_conv.func_) is used in place of A for type deduction;
21090 otherwise,
21091
21092 --If A is a cv-qualified type, the top level
21093 cv-qualifiers of A's type are ignored for type
21094 deduction. */
21095 if (TREE_CODE (*arg) == ARRAY_TYPE)
21096 *arg = build_pointer_type (TREE_TYPE (*arg));
21097 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21098 *arg = build_pointer_type (*arg);
21099 else
21100 *arg = TYPE_MAIN_VARIANT (*arg);
21101 }
21102
21103 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21104 reference to a cv-unqualified template parameter that does not represent a
21105 template parameter of a class template (during class template argument
21106 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21107 an lvalue, the type "lvalue reference to A" is used in place of A for type
21108 deduction. */
21109 if (TYPE_REF_P (*parm)
21110 && TYPE_REF_IS_RVALUE (*parm)
21111 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21112 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21113 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21114 && (arg_expr ? lvalue_p (arg_expr)
21115 /* try_one_overload doesn't provide an arg_expr, but
21116 functions are always lvalues. */
21117 : TREE_CODE (*arg) == FUNCTION_TYPE))
21118 *arg = build_reference_type (*arg);
21119
21120 /* [temp.deduct.call]
21121
21122 If P is a cv-qualified type, the top level cv-qualifiers
21123 of P's type are ignored for type deduction. If P is a
21124 reference type, the type referred to by P is used for
21125 type deduction. */
21126 *parm = TYPE_MAIN_VARIANT (*parm);
21127 if (TYPE_REF_P (*parm))
21128 {
21129 *parm = TREE_TYPE (*parm);
21130 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21131 }
21132
21133 /* DR 322. For conversion deduction, remove a reference type on parm
21134 too (which has been swapped into ARG). */
21135 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21136 *arg = TREE_TYPE (*arg);
21137
21138 return result;
21139 }
21140
21141 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21142 template which doesn't contain any deducible template parameters; check if
21143 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21144 unify_one_argument. */
21145
21146 static int
21147 check_non_deducible_conversion (tree parm, tree arg, int strict,
21148 int flags, struct conversion **conv_p,
21149 bool explain_p)
21150 {
21151 tree type;
21152
21153 if (!TYPE_P (arg))
21154 type = TREE_TYPE (arg);
21155 else
21156 type = arg;
21157
21158 if (same_type_p (parm, type))
21159 return unify_success (explain_p);
21160
21161 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21162 if (strict == DEDUCE_CONV)
21163 {
21164 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21165 return unify_success (explain_p);
21166 }
21167 else if (strict != DEDUCE_EXACT)
21168 {
21169 bool ok = false;
21170 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21171 if (conv_p)
21172 /* Avoid recalculating this in add_function_candidate. */
21173 ok = (*conv_p
21174 = good_conversion (parm, type, conv_arg, flags, complain));
21175 else
21176 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21177 if (ok)
21178 return unify_success (explain_p);
21179 }
21180
21181 if (strict == DEDUCE_EXACT)
21182 return unify_type_mismatch (explain_p, parm, arg);
21183 else
21184 return unify_arg_conversion (explain_p, parm, type, arg);
21185 }
21186
21187 static bool uses_deducible_template_parms (tree type);
21188
21189 /* Returns true iff the expression EXPR is one from which a template
21190 argument can be deduced. In other words, if it's an undecorated
21191 use of a template non-type parameter. */
21192
21193 static bool
21194 deducible_expression (tree expr)
21195 {
21196 /* Strip implicit conversions. */
21197 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21198 expr = TREE_OPERAND (expr, 0);
21199 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21200 }
21201
21202 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21203 deducible way; that is, if it has a max value of <PARM> - 1. */
21204
21205 static bool
21206 deducible_array_bound (tree domain)
21207 {
21208 if (domain == NULL_TREE)
21209 return false;
21210
21211 tree max = TYPE_MAX_VALUE (domain);
21212 if (TREE_CODE (max) != MINUS_EXPR)
21213 return false;
21214
21215 return deducible_expression (TREE_OPERAND (max, 0));
21216 }
21217
21218 /* Returns true iff the template arguments ARGS use a template parameter
21219 in a deducible way. */
21220
21221 static bool
21222 deducible_template_args (tree args)
21223 {
21224 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21225 {
21226 bool deducible;
21227 tree elt = TREE_VEC_ELT (args, i);
21228 if (ARGUMENT_PACK_P (elt))
21229 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21230 else
21231 {
21232 if (PACK_EXPANSION_P (elt))
21233 elt = PACK_EXPANSION_PATTERN (elt);
21234 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21235 deducible = true;
21236 else if (TYPE_P (elt))
21237 deducible = uses_deducible_template_parms (elt);
21238 else
21239 deducible = deducible_expression (elt);
21240 }
21241 if (deducible)
21242 return true;
21243 }
21244 return false;
21245 }
21246
21247 /* Returns true iff TYPE contains any deducible references to template
21248 parameters, as per 14.8.2.5. */
21249
21250 static bool
21251 uses_deducible_template_parms (tree type)
21252 {
21253 if (PACK_EXPANSION_P (type))
21254 type = PACK_EXPANSION_PATTERN (type);
21255
21256 /* T
21257 cv-list T
21258 TT<T>
21259 TT<i>
21260 TT<> */
21261 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21262 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21263 return true;
21264
21265 /* T*
21266 T&
21267 T&& */
21268 if (INDIRECT_TYPE_P (type))
21269 return uses_deducible_template_parms (TREE_TYPE (type));
21270
21271 /* T[integer-constant ]
21272 type [i] */
21273 if (TREE_CODE (type) == ARRAY_TYPE)
21274 return (uses_deducible_template_parms (TREE_TYPE (type))
21275 || deducible_array_bound (TYPE_DOMAIN (type)));
21276
21277 /* T type ::*
21278 type T::*
21279 T T::*
21280 T (type ::*)()
21281 type (T::*)()
21282 type (type ::*)(T)
21283 type (T::*)(T)
21284 T (type ::*)(T)
21285 T (T::*)()
21286 T (T::*)(T) */
21287 if (TYPE_PTRMEM_P (type))
21288 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21289 || (uses_deducible_template_parms
21290 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21291
21292 /* template-name <T> (where template-name refers to a class template)
21293 template-name <i> (where template-name refers to a class template) */
21294 if (CLASS_TYPE_P (type)
21295 && CLASSTYPE_TEMPLATE_INFO (type)
21296 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21297 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21298 (CLASSTYPE_TI_ARGS (type)));
21299
21300 /* type (T)
21301 T()
21302 T(T) */
21303 if (FUNC_OR_METHOD_TYPE_P (type))
21304 {
21305 if (uses_deducible_template_parms (TREE_TYPE (type)))
21306 return true;
21307 tree parm = TYPE_ARG_TYPES (type);
21308 if (TREE_CODE (type) == METHOD_TYPE)
21309 parm = TREE_CHAIN (parm);
21310 for (; parm; parm = TREE_CHAIN (parm))
21311 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21312 return true;
21313 }
21314
21315 return false;
21316 }
21317
21318 /* Subroutine of type_unification_real and unify_pack_expansion to
21319 handle unification of a single P/A pair. Parameters are as
21320 for those functions. */
21321
21322 static int
21323 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21324 int subr, unification_kind_t strict,
21325 bool explain_p)
21326 {
21327 tree arg_expr = NULL_TREE;
21328 int arg_strict;
21329
21330 if (arg == error_mark_node || parm == error_mark_node)
21331 return unify_invalid (explain_p);
21332 if (arg == unknown_type_node)
21333 /* We can't deduce anything from this, but we might get all the
21334 template args from other function args. */
21335 return unify_success (explain_p);
21336
21337 /* Implicit conversions (Clause 4) will be performed on a function
21338 argument to convert it to the type of the corresponding function
21339 parameter if the parameter type contains no template-parameters that
21340 participate in template argument deduction. */
21341 if (strict != DEDUCE_EXACT
21342 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21343 /* For function parameters with no deducible template parameters,
21344 just return. We'll check non-dependent conversions later. */
21345 return unify_success (explain_p);
21346
21347 switch (strict)
21348 {
21349 case DEDUCE_CALL:
21350 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21351 | UNIFY_ALLOW_MORE_CV_QUAL
21352 | UNIFY_ALLOW_DERIVED);
21353 break;
21354
21355 case DEDUCE_CONV:
21356 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21357 break;
21358
21359 case DEDUCE_EXACT:
21360 arg_strict = UNIFY_ALLOW_NONE;
21361 break;
21362
21363 default:
21364 gcc_unreachable ();
21365 }
21366
21367 /* We only do these transformations if this is the top-level
21368 parameter_type_list in a call or declaration matching; in other
21369 situations (nested function declarators, template argument lists) we
21370 won't be comparing a type to an expression, and we don't do any type
21371 adjustments. */
21372 if (!subr)
21373 {
21374 if (!TYPE_P (arg))
21375 {
21376 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21377 if (type_unknown_p (arg))
21378 {
21379 /* [temp.deduct.type] A template-argument can be
21380 deduced from a pointer to function or pointer
21381 to member function argument if the set of
21382 overloaded functions does not contain function
21383 templates and at most one of a set of
21384 overloaded functions provides a unique
21385 match. */
21386 resolve_overloaded_unification (tparms, targs, parm,
21387 arg, strict,
21388 arg_strict, explain_p);
21389 /* If a unique match was not found, this is a
21390 non-deduced context, so we still succeed. */
21391 return unify_success (explain_p);
21392 }
21393
21394 arg_expr = arg;
21395 arg = unlowered_expr_type (arg);
21396 if (arg == error_mark_node)
21397 return unify_invalid (explain_p);
21398 }
21399
21400 arg_strict |=
21401 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21402 }
21403 else
21404 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21405 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21406 return unify_template_argument_mismatch (explain_p, parm, arg);
21407
21408 /* For deduction from an init-list we need the actual list. */
21409 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21410 arg = arg_expr;
21411 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21412 }
21413
21414 /* for_each_template_parm callback that always returns 0. */
21415
21416 static int
21417 zero_r (tree, void *)
21418 {
21419 return 0;
21420 }
21421
21422 /* for_each_template_parm any_fn callback to handle deduction of a template
21423 type argument from the type of an array bound. */
21424
21425 static int
21426 array_deduction_r (tree t, void *data)
21427 {
21428 tree_pair_p d = (tree_pair_p)data;
21429 tree &tparms = d->purpose;
21430 tree &targs = d->value;
21431
21432 if (TREE_CODE (t) == ARRAY_TYPE)
21433 if (tree dom = TYPE_DOMAIN (t))
21434 if (tree max = TYPE_MAX_VALUE (dom))
21435 {
21436 if (TREE_CODE (max) == MINUS_EXPR)
21437 max = TREE_OPERAND (max, 0);
21438 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21439 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21440 UNIFY_ALLOW_NONE, /*explain*/false);
21441 }
21442
21443 /* Keep walking. */
21444 return 0;
21445 }
21446
21447 /* Try to deduce any not-yet-deduced template type arguments from the type of
21448 an array bound. This is handled separately from unify because 14.8.2.5 says
21449 "The type of a type parameter is only deduced from an array bound if it is
21450 not otherwise deduced." */
21451
21452 static void
21453 try_array_deduction (tree tparms, tree targs, tree parm)
21454 {
21455 tree_pair_s data = { tparms, targs };
21456 hash_set<tree> visited;
21457 for_each_template_parm (parm, zero_r, &data, &visited,
21458 /*nondeduced*/false, array_deduction_r);
21459 }
21460
21461 /* Most parms like fn_type_unification.
21462
21463 If SUBR is 1, we're being called recursively (to unify the
21464 arguments of a function or method parameter of a function
21465 template).
21466
21467 CHECKS is a pointer to a vector of access checks encountered while
21468 substituting default template arguments. */
21469
21470 static int
21471 type_unification_real (tree tparms,
21472 tree full_targs,
21473 tree xparms,
21474 const tree *xargs,
21475 unsigned int xnargs,
21476 int subr,
21477 unification_kind_t strict,
21478 vec<deferred_access_check, va_gc> **checks,
21479 bool explain_p)
21480 {
21481 tree parm, arg;
21482 int i;
21483 int ntparms = TREE_VEC_LENGTH (tparms);
21484 int saw_undeduced = 0;
21485 tree parms;
21486 const tree *args;
21487 unsigned int nargs;
21488 unsigned int ia;
21489
21490 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21491 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21492 gcc_assert (ntparms > 0);
21493
21494 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21495
21496 /* Reset the number of non-defaulted template arguments contained
21497 in TARGS. */
21498 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21499
21500 again:
21501 parms = xparms;
21502 args = xargs;
21503 nargs = xnargs;
21504
21505 ia = 0;
21506 while (parms && parms != void_list_node
21507 && ia < nargs)
21508 {
21509 parm = TREE_VALUE (parms);
21510
21511 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21512 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21513 /* For a function parameter pack that occurs at the end of the
21514 parameter-declaration-list, the type A of each remaining
21515 argument of the call is compared with the type P of the
21516 declarator-id of the function parameter pack. */
21517 break;
21518
21519 parms = TREE_CHAIN (parms);
21520
21521 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21522 /* For a function parameter pack that does not occur at the
21523 end of the parameter-declaration-list, the type of the
21524 parameter pack is a non-deduced context. */
21525 continue;
21526
21527 arg = args[ia];
21528 ++ia;
21529
21530 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21531 explain_p))
21532 return 1;
21533 }
21534
21535 if (parms
21536 && parms != void_list_node
21537 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21538 {
21539 /* Unify the remaining arguments with the pack expansion type. */
21540 tree argvec;
21541 tree parmvec = make_tree_vec (1);
21542
21543 /* Allocate a TREE_VEC and copy in all of the arguments */
21544 argvec = make_tree_vec (nargs - ia);
21545 for (i = 0; ia < nargs; ++ia, ++i)
21546 TREE_VEC_ELT (argvec, i) = args[ia];
21547
21548 /* Copy the parameter into parmvec. */
21549 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21550 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21551 /*subr=*/subr, explain_p))
21552 return 1;
21553
21554 /* Advance to the end of the list of parameters. */
21555 parms = TREE_CHAIN (parms);
21556 }
21557
21558 /* Fail if we've reached the end of the parm list, and more args
21559 are present, and the parm list isn't variadic. */
21560 if (ia < nargs && parms == void_list_node)
21561 return unify_too_many_arguments (explain_p, nargs, ia);
21562 /* Fail if parms are left and they don't have default values and
21563 they aren't all deduced as empty packs (c++/57397). This is
21564 consistent with sufficient_parms_p. */
21565 if (parms && parms != void_list_node
21566 && TREE_PURPOSE (parms) == NULL_TREE)
21567 {
21568 unsigned int count = nargs;
21569 tree p = parms;
21570 bool type_pack_p;
21571 do
21572 {
21573 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21574 if (!type_pack_p)
21575 count++;
21576 p = TREE_CHAIN (p);
21577 }
21578 while (p && p != void_list_node);
21579 if (count != nargs)
21580 return unify_too_few_arguments (explain_p, ia, count,
21581 type_pack_p);
21582 }
21583
21584 if (!subr)
21585 {
21586 tsubst_flags_t complain = (explain_p
21587 ? tf_warning_or_error
21588 : tf_none);
21589 bool tried_array_deduction = (cxx_dialect < cxx17);
21590
21591 for (i = 0; i < ntparms; i++)
21592 {
21593 tree targ = TREE_VEC_ELT (targs, i);
21594 tree tparm = TREE_VEC_ELT (tparms, i);
21595
21596 /* Clear the "incomplete" flags on all argument packs now so that
21597 substituting them into later default arguments works. */
21598 if (targ && ARGUMENT_PACK_P (targ))
21599 {
21600 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21601 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21602 }
21603
21604 if (targ || tparm == error_mark_node)
21605 continue;
21606 tparm = TREE_VALUE (tparm);
21607
21608 if (TREE_CODE (tparm) == TYPE_DECL
21609 && !tried_array_deduction)
21610 {
21611 try_array_deduction (tparms, targs, xparms);
21612 tried_array_deduction = true;
21613 if (TREE_VEC_ELT (targs, i))
21614 continue;
21615 }
21616
21617 /* If this is an undeduced nontype parameter that depends on
21618 a type parameter, try another pass; its type may have been
21619 deduced from a later argument than the one from which
21620 this parameter can be deduced. */
21621 if (TREE_CODE (tparm) == PARM_DECL
21622 && uses_template_parms (TREE_TYPE (tparm))
21623 && saw_undeduced < 2)
21624 {
21625 saw_undeduced = 1;
21626 continue;
21627 }
21628
21629 /* Core issue #226 (C++0x) [temp.deduct]:
21630
21631 If a template argument has not been deduced, its
21632 default template argument, if any, is used.
21633
21634 When we are in C++98 mode, TREE_PURPOSE will either
21635 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21636 to explicitly check cxx_dialect here. */
21637 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21638 /* OK, there is a default argument. Wait until after the
21639 conversion check to do substitution. */
21640 continue;
21641
21642 /* If the type parameter is a parameter pack, then it will
21643 be deduced to an empty parameter pack. */
21644 if (template_parameter_pack_p (tparm))
21645 {
21646 tree arg;
21647
21648 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21649 {
21650 arg = make_node (NONTYPE_ARGUMENT_PACK);
21651 TREE_CONSTANT (arg) = 1;
21652 }
21653 else
21654 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21655
21656 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21657
21658 TREE_VEC_ELT (targs, i) = arg;
21659 continue;
21660 }
21661
21662 return unify_parameter_deduction_failure (explain_p, tparm);
21663 }
21664
21665 /* Now substitute into the default template arguments. */
21666 for (i = 0; i < ntparms; i++)
21667 {
21668 tree targ = TREE_VEC_ELT (targs, i);
21669 tree tparm = TREE_VEC_ELT (tparms, i);
21670
21671 if (targ || tparm == error_mark_node)
21672 continue;
21673 tree parm = TREE_VALUE (tparm);
21674 tree arg = TREE_PURPOSE (tparm);
21675 reopen_deferring_access_checks (*checks);
21676 location_t save_loc = input_location;
21677 if (DECL_P (parm))
21678 input_location = DECL_SOURCE_LOCATION (parm);
21679
21680 if (saw_undeduced == 1
21681 && TREE_CODE (parm) == PARM_DECL
21682 && uses_template_parms (TREE_TYPE (parm)))
21683 {
21684 /* The type of this non-type parameter depends on undeduced
21685 parameters. Don't try to use its default argument yet,
21686 since we might deduce an argument for it on the next pass,
21687 but do check whether the arguments we already have cause
21688 substitution failure, so that that happens before we try
21689 later default arguments (78489). */
21690 ++processing_template_decl;
21691 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21692 NULL_TREE);
21693 --processing_template_decl;
21694 if (type == error_mark_node)
21695 arg = error_mark_node;
21696 else
21697 arg = NULL_TREE;
21698 }
21699 else
21700 {
21701 /* Even if the call is happening in template context, getting
21702 here means it's non-dependent, and a default argument is
21703 considered a separate definition under [temp.decls], so we can
21704 do this substitution without processing_template_decl. This
21705 is important if the default argument contains something that
21706 might be instantiation-dependent like access (87480). */
21707 processing_template_decl_sentinel s;
21708 tree substed = NULL_TREE;
21709 if (saw_undeduced == 1)
21710 {
21711 /* First instatiate in template context, in case we still
21712 depend on undeduced template parameters. */
21713 ++processing_template_decl;
21714 substed = tsubst_template_arg (arg, full_targs, complain,
21715 NULL_TREE);
21716 --processing_template_decl;
21717 if (substed != error_mark_node
21718 && !uses_template_parms (substed))
21719 /* We replaced all the tparms, substitute again out of
21720 template context. */
21721 substed = NULL_TREE;
21722 }
21723 if (!substed)
21724 substed = tsubst_template_arg (arg, full_targs, complain,
21725 NULL_TREE);
21726
21727 if (!uses_template_parms (substed))
21728 arg = convert_template_argument (parm, substed, full_targs,
21729 complain, i, NULL_TREE);
21730 else if (saw_undeduced == 1)
21731 arg = NULL_TREE;
21732 else
21733 arg = error_mark_node;
21734 }
21735
21736 input_location = save_loc;
21737 *checks = get_deferred_access_checks ();
21738 pop_deferring_access_checks ();
21739
21740 if (arg == error_mark_node)
21741 return 1;
21742 else if (arg)
21743 {
21744 TREE_VEC_ELT (targs, i) = arg;
21745 /* The position of the first default template argument,
21746 is also the number of non-defaulted arguments in TARGS.
21747 Record that. */
21748 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21749 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21750 }
21751 }
21752
21753 if (saw_undeduced++ == 1)
21754 goto again;
21755 }
21756
21757 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21758 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21759
21760 return unify_success (explain_p);
21761 }
21762
21763 /* Subroutine of type_unification_real. Args are like the variables
21764 at the call site. ARG is an overloaded function (or template-id);
21765 we try deducing template args from each of the overloads, and if
21766 only one succeeds, we go with that. Modifies TARGS and returns
21767 true on success. */
21768
21769 static bool
21770 resolve_overloaded_unification (tree tparms,
21771 tree targs,
21772 tree parm,
21773 tree arg,
21774 unification_kind_t strict,
21775 int sub_strict,
21776 bool explain_p)
21777 {
21778 tree tempargs = copy_node (targs);
21779 int good = 0;
21780 tree goodfn = NULL_TREE;
21781 bool addr_p;
21782
21783 if (TREE_CODE (arg) == ADDR_EXPR)
21784 {
21785 arg = TREE_OPERAND (arg, 0);
21786 addr_p = true;
21787 }
21788 else
21789 addr_p = false;
21790
21791 if (TREE_CODE (arg) == COMPONENT_REF)
21792 /* Handle `&x' where `x' is some static or non-static member
21793 function name. */
21794 arg = TREE_OPERAND (arg, 1);
21795
21796 if (TREE_CODE (arg) == OFFSET_REF)
21797 arg = TREE_OPERAND (arg, 1);
21798
21799 /* Strip baselink information. */
21800 if (BASELINK_P (arg))
21801 arg = BASELINK_FUNCTIONS (arg);
21802
21803 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21804 {
21805 /* If we got some explicit template args, we need to plug them into
21806 the affected templates before we try to unify, in case the
21807 explicit args will completely resolve the templates in question. */
21808
21809 int ok = 0;
21810 tree expl_subargs = TREE_OPERAND (arg, 1);
21811 arg = TREE_OPERAND (arg, 0);
21812
21813 for (lkp_iterator iter (arg); iter; ++iter)
21814 {
21815 tree fn = *iter;
21816 tree subargs, elem;
21817
21818 if (TREE_CODE (fn) != TEMPLATE_DECL)
21819 continue;
21820
21821 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21822 expl_subargs, NULL_TREE, tf_none,
21823 /*require_all_args=*/true,
21824 /*use_default_args=*/true);
21825 if (subargs != error_mark_node
21826 && !any_dependent_template_arguments_p (subargs))
21827 {
21828 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21829 if (try_one_overload (tparms, targs, tempargs, parm,
21830 elem, strict, sub_strict, addr_p, explain_p)
21831 && (!goodfn || !same_type_p (goodfn, elem)))
21832 {
21833 goodfn = elem;
21834 ++good;
21835 }
21836 }
21837 else if (subargs)
21838 ++ok;
21839 }
21840 /* If no templates (or more than one) are fully resolved by the
21841 explicit arguments, this template-id is a non-deduced context; it
21842 could still be OK if we deduce all template arguments for the
21843 enclosing call through other arguments. */
21844 if (good != 1)
21845 good = ok;
21846 }
21847 else if (!OVL_P (arg))
21848 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21849 -- but the deduction does not succeed because the expression is
21850 not just the function on its own. */
21851 return false;
21852 else
21853 for (lkp_iterator iter (arg); iter; ++iter)
21854 {
21855 tree fn = *iter;
21856 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21857 strict, sub_strict, addr_p, explain_p)
21858 && (!goodfn || !decls_match (goodfn, fn)))
21859 {
21860 goodfn = fn;
21861 ++good;
21862 }
21863 }
21864
21865 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21866 to function or pointer to member function argument if the set of
21867 overloaded functions does not contain function templates and at most
21868 one of a set of overloaded functions provides a unique match.
21869
21870 So if we found multiple possibilities, we return success but don't
21871 deduce anything. */
21872
21873 if (good == 1)
21874 {
21875 int i = TREE_VEC_LENGTH (targs);
21876 for (; i--; )
21877 if (TREE_VEC_ELT (tempargs, i))
21878 {
21879 tree old = TREE_VEC_ELT (targs, i);
21880 tree new_ = TREE_VEC_ELT (tempargs, i);
21881 if (new_ && old && ARGUMENT_PACK_P (old)
21882 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21883 /* Don't forget explicit template arguments in a pack. */
21884 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21885 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21886 TREE_VEC_ELT (targs, i) = new_;
21887 }
21888 }
21889 if (good)
21890 return true;
21891
21892 return false;
21893 }
21894
21895 /* Core DR 115: In contexts where deduction is done and fails, or in
21896 contexts where deduction is not done, if a template argument list is
21897 specified and it, along with any default template arguments, identifies
21898 a single function template specialization, then the template-id is an
21899 lvalue for the function template specialization. */
21900
21901 tree
21902 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21903 {
21904 tree expr, offset, baselink;
21905 bool addr;
21906
21907 if (!type_unknown_p (orig_expr))
21908 return orig_expr;
21909
21910 expr = orig_expr;
21911 addr = false;
21912 offset = NULL_TREE;
21913 baselink = NULL_TREE;
21914
21915 if (TREE_CODE (expr) == ADDR_EXPR)
21916 {
21917 expr = TREE_OPERAND (expr, 0);
21918 addr = true;
21919 }
21920 if (TREE_CODE (expr) == OFFSET_REF)
21921 {
21922 offset = expr;
21923 expr = TREE_OPERAND (expr, 1);
21924 }
21925 if (BASELINK_P (expr))
21926 {
21927 baselink = expr;
21928 expr = BASELINK_FUNCTIONS (expr);
21929 }
21930
21931 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21932 {
21933 int good = 0;
21934 tree goodfn = NULL_TREE;
21935
21936 /* If we got some explicit template args, we need to plug them into
21937 the affected templates before we try to unify, in case the
21938 explicit args will completely resolve the templates in question. */
21939
21940 tree expl_subargs = TREE_OPERAND (expr, 1);
21941 tree arg = TREE_OPERAND (expr, 0);
21942 tree badfn = NULL_TREE;
21943 tree badargs = NULL_TREE;
21944
21945 for (lkp_iterator iter (arg); iter; ++iter)
21946 {
21947 tree fn = *iter;
21948 tree subargs, elem;
21949
21950 if (TREE_CODE (fn) != TEMPLATE_DECL)
21951 continue;
21952
21953 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21954 expl_subargs, NULL_TREE, tf_none,
21955 /*require_all_args=*/true,
21956 /*use_default_args=*/true);
21957 if (subargs != error_mark_node
21958 && !any_dependent_template_arguments_p (subargs))
21959 {
21960 elem = instantiate_template (fn, subargs, tf_none);
21961 if (elem == error_mark_node)
21962 {
21963 badfn = fn;
21964 badargs = subargs;
21965 }
21966 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21967 {
21968 goodfn = elem;
21969 ++good;
21970 }
21971 }
21972 }
21973 if (good == 1)
21974 {
21975 mark_used (goodfn);
21976 expr = goodfn;
21977 if (baselink)
21978 expr = build_baselink (BASELINK_BINFO (baselink),
21979 BASELINK_ACCESS_BINFO (baselink),
21980 expr, BASELINK_OPTYPE (baselink));
21981 if (offset)
21982 {
21983 tree base
21984 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21985 expr = build_offset_ref (base, expr, addr, complain);
21986 }
21987 if (addr)
21988 expr = cp_build_addr_expr (expr, complain);
21989 return expr;
21990 }
21991 else if (good == 0 && badargs && (complain & tf_error))
21992 /* There were no good options and at least one bad one, so let the
21993 user know what the problem is. */
21994 instantiate_template (badfn, badargs, complain);
21995 }
21996 return orig_expr;
21997 }
21998
21999 /* As above, but error out if the expression remains overloaded. */
22000
22001 tree
22002 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22003 {
22004 exp = resolve_nondeduced_context (exp, complain);
22005 if (type_unknown_p (exp))
22006 {
22007 if (complain & tf_error)
22008 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22009 return error_mark_node;
22010 }
22011 return exp;
22012 }
22013
22014 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22015 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22016 different overloads deduce different arguments for a given parm.
22017 ADDR_P is true if the expression for which deduction is being
22018 performed was of the form "& fn" rather than simply "fn".
22019
22020 Returns 1 on success. */
22021
22022 static int
22023 try_one_overload (tree tparms,
22024 tree orig_targs,
22025 tree targs,
22026 tree parm,
22027 tree arg,
22028 unification_kind_t strict,
22029 int sub_strict,
22030 bool addr_p,
22031 bool explain_p)
22032 {
22033 int nargs;
22034 tree tempargs;
22035 int i;
22036
22037 if (arg == error_mark_node)
22038 return 0;
22039
22040 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22041 to function or pointer to member function argument if the set of
22042 overloaded functions does not contain function templates and at most
22043 one of a set of overloaded functions provides a unique match.
22044
22045 So if this is a template, just return success. */
22046
22047 if (uses_template_parms (arg))
22048 return 1;
22049
22050 if (TREE_CODE (arg) == METHOD_TYPE)
22051 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22052 else if (addr_p)
22053 arg = build_pointer_type (arg);
22054
22055 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22056
22057 /* We don't copy orig_targs for this because if we have already deduced
22058 some template args from previous args, unify would complain when we
22059 try to deduce a template parameter for the same argument, even though
22060 there isn't really a conflict. */
22061 nargs = TREE_VEC_LENGTH (targs);
22062 tempargs = make_tree_vec (nargs);
22063
22064 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22065 return 0;
22066
22067 /* First make sure we didn't deduce anything that conflicts with
22068 explicitly specified args. */
22069 for (i = nargs; i--; )
22070 {
22071 tree elt = TREE_VEC_ELT (tempargs, i);
22072 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22073
22074 if (!elt)
22075 /*NOP*/;
22076 else if (uses_template_parms (elt))
22077 /* Since we're unifying against ourselves, we will fill in
22078 template args used in the function parm list with our own
22079 template parms. Discard them. */
22080 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22081 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22082 {
22083 /* Check that the argument at each index of the deduced argument pack
22084 is equivalent to the corresponding explicitly specified argument.
22085 We may have deduced more arguments than were explicitly specified,
22086 and that's OK. */
22087
22088 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22089 that's wrong if we deduce the same argument pack from multiple
22090 function arguments: it's only incomplete the first time. */
22091
22092 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22093 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22094
22095 if (TREE_VEC_LENGTH (deduced_pack)
22096 < TREE_VEC_LENGTH (explicit_pack))
22097 return 0;
22098
22099 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22100 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22101 TREE_VEC_ELT (deduced_pack, j)))
22102 return 0;
22103 }
22104 else if (oldelt && !template_args_equal (oldelt, elt))
22105 return 0;
22106 }
22107
22108 for (i = nargs; i--; )
22109 {
22110 tree elt = TREE_VEC_ELT (tempargs, i);
22111
22112 if (elt)
22113 TREE_VEC_ELT (targs, i) = elt;
22114 }
22115
22116 return 1;
22117 }
22118
22119 /* PARM is a template class (perhaps with unbound template
22120 parameters). ARG is a fully instantiated type. If ARG can be
22121 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22122 TARGS are as for unify. */
22123
22124 static tree
22125 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22126 bool explain_p)
22127 {
22128 tree copy_of_targs;
22129
22130 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22131 return NULL_TREE;
22132 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22133 /* Matches anything. */;
22134 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22135 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22136 return NULL_TREE;
22137
22138 /* We need to make a new template argument vector for the call to
22139 unify. If we used TARGS, we'd clutter it up with the result of
22140 the attempted unification, even if this class didn't work out.
22141 We also don't want to commit ourselves to all the unifications
22142 we've already done, since unification is supposed to be done on
22143 an argument-by-argument basis. In other words, consider the
22144 following pathological case:
22145
22146 template <int I, int J, int K>
22147 struct S {};
22148
22149 template <int I, int J>
22150 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22151
22152 template <int I, int J, int K>
22153 void f(S<I, J, K>, S<I, I, I>);
22154
22155 void g() {
22156 S<0, 0, 0> s0;
22157 S<0, 1, 2> s2;
22158
22159 f(s0, s2);
22160 }
22161
22162 Now, by the time we consider the unification involving `s2', we
22163 already know that we must have `f<0, 0, 0>'. But, even though
22164 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22165 because there are two ways to unify base classes of S<0, 1, 2>
22166 with S<I, I, I>. If we kept the already deduced knowledge, we
22167 would reject the possibility I=1. */
22168 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22169
22170 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22171 {
22172 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22173 return NULL_TREE;
22174 return arg;
22175 }
22176
22177 /* If unification failed, we're done. */
22178 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22179 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22180 return NULL_TREE;
22181
22182 return arg;
22183 }
22184
22185 /* Given a template type PARM and a class type ARG, find the unique
22186 base type in ARG that is an instance of PARM. We do not examine
22187 ARG itself; only its base-classes. If there is not exactly one
22188 appropriate base class, return NULL_TREE. PARM may be the type of
22189 a partial specialization, as well as a plain template type. Used
22190 by unify. */
22191
22192 static enum template_base_result
22193 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22194 bool explain_p, tree *result)
22195 {
22196 tree rval = NULL_TREE;
22197 tree binfo;
22198
22199 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22200
22201 binfo = TYPE_BINFO (complete_type (arg));
22202 if (!binfo)
22203 {
22204 /* The type could not be completed. */
22205 *result = NULL_TREE;
22206 return tbr_incomplete_type;
22207 }
22208
22209 /* Walk in inheritance graph order. The search order is not
22210 important, and this avoids multiple walks of virtual bases. */
22211 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22212 {
22213 tree r = try_class_unification (tparms, targs, parm,
22214 BINFO_TYPE (binfo), explain_p);
22215
22216 if (r)
22217 {
22218 /* If there is more than one satisfactory baseclass, then:
22219
22220 [temp.deduct.call]
22221
22222 If they yield more than one possible deduced A, the type
22223 deduction fails.
22224
22225 applies. */
22226 if (rval && !same_type_p (r, rval))
22227 {
22228 *result = NULL_TREE;
22229 return tbr_ambiguous_baseclass;
22230 }
22231
22232 rval = r;
22233 }
22234 }
22235
22236 *result = rval;
22237 return tbr_success;
22238 }
22239
22240 /* Returns the level of DECL, which declares a template parameter. */
22241
22242 static int
22243 template_decl_level (tree decl)
22244 {
22245 switch (TREE_CODE (decl))
22246 {
22247 case TYPE_DECL:
22248 case TEMPLATE_DECL:
22249 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22250
22251 case PARM_DECL:
22252 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22253
22254 default:
22255 gcc_unreachable ();
22256 }
22257 return 0;
22258 }
22259
22260 /* Decide whether ARG can be unified with PARM, considering only the
22261 cv-qualifiers of each type, given STRICT as documented for unify.
22262 Returns nonzero iff the unification is OK on that basis. */
22263
22264 static int
22265 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22266 {
22267 int arg_quals = cp_type_quals (arg);
22268 int parm_quals = cp_type_quals (parm);
22269
22270 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22271 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22272 {
22273 /* Although a CVR qualifier is ignored when being applied to a
22274 substituted template parameter ([8.3.2]/1 for example), that
22275 does not allow us to unify "const T" with "int&" because both
22276 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22277 It is ok when we're allowing additional CV qualifiers
22278 at the outer level [14.8.2.1]/3,1st bullet. */
22279 if ((TYPE_REF_P (arg)
22280 || FUNC_OR_METHOD_TYPE_P (arg))
22281 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22282 return 0;
22283
22284 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22285 && (parm_quals & TYPE_QUAL_RESTRICT))
22286 return 0;
22287 }
22288
22289 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22290 && (arg_quals & parm_quals) != parm_quals)
22291 return 0;
22292
22293 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22294 && (parm_quals & arg_quals) != arg_quals)
22295 return 0;
22296
22297 return 1;
22298 }
22299
22300 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22301 void
22302 template_parm_level_and_index (tree parm, int* level, int* index)
22303 {
22304 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22305 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22306 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22307 {
22308 *index = TEMPLATE_TYPE_IDX (parm);
22309 *level = TEMPLATE_TYPE_LEVEL (parm);
22310 }
22311 else
22312 {
22313 *index = TEMPLATE_PARM_IDX (parm);
22314 *level = TEMPLATE_PARM_LEVEL (parm);
22315 }
22316 }
22317
22318 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22319 do { \
22320 if (unify (TP, TA, P, A, S, EP)) \
22321 return 1; \
22322 } while (0)
22323
22324 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22325 expansion at the end of PACKED_PARMS. Returns 0 if the type
22326 deduction succeeds, 1 otherwise. STRICT is the same as in
22327 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22328 function call argument list. We'll need to adjust the arguments to make them
22329 types. SUBR tells us if this is from a recursive call to
22330 type_unification_real, or for comparing two template argument
22331 lists. */
22332
22333 static int
22334 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22335 tree packed_args, unification_kind_t strict,
22336 bool subr, bool explain_p)
22337 {
22338 tree parm
22339 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22340 tree pattern = PACK_EXPANSION_PATTERN (parm);
22341 tree pack, packs = NULL_TREE;
22342 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22343
22344 /* Add in any args remembered from an earlier partial instantiation. */
22345 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22346 int levels = TMPL_ARGS_DEPTH (targs);
22347
22348 packed_args = expand_template_argument_pack (packed_args);
22349
22350 int len = TREE_VEC_LENGTH (packed_args);
22351
22352 /* Determine the parameter packs we will be deducing from the
22353 pattern, and record their current deductions. */
22354 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22355 pack; pack = TREE_CHAIN (pack))
22356 {
22357 tree parm_pack = TREE_VALUE (pack);
22358 int idx, level;
22359
22360 /* Only template parameter packs can be deduced, not e.g. function
22361 parameter packs or __bases or __integer_pack. */
22362 if (!TEMPLATE_PARM_P (parm_pack))
22363 continue;
22364
22365 /* Determine the index and level of this parameter pack. */
22366 template_parm_level_and_index (parm_pack, &level, &idx);
22367 if (level < levels)
22368 continue;
22369
22370 /* Keep track of the parameter packs and their corresponding
22371 argument packs. */
22372 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22373 TREE_TYPE (packs) = make_tree_vec (len - start);
22374 }
22375
22376 /* Loop through all of the arguments that have not yet been
22377 unified and unify each with the pattern. */
22378 for (i = start; i < len; i++)
22379 {
22380 tree parm;
22381 bool any_explicit = false;
22382 tree arg = TREE_VEC_ELT (packed_args, i);
22383
22384 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22385 or the element of its argument pack at the current index if
22386 this argument was explicitly specified. */
22387 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22388 {
22389 int idx, level;
22390 tree arg, pargs;
22391 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22392
22393 arg = NULL_TREE;
22394 if (TREE_VALUE (pack)
22395 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22396 && (i - start < TREE_VEC_LENGTH (pargs)))
22397 {
22398 any_explicit = true;
22399 arg = TREE_VEC_ELT (pargs, i - start);
22400 }
22401 TMPL_ARG (targs, level, idx) = arg;
22402 }
22403
22404 /* If we had explicit template arguments, substitute them into the
22405 pattern before deduction. */
22406 if (any_explicit)
22407 {
22408 /* Some arguments might still be unspecified or dependent. */
22409 bool dependent;
22410 ++processing_template_decl;
22411 dependent = any_dependent_template_arguments_p (targs);
22412 if (!dependent)
22413 --processing_template_decl;
22414 parm = tsubst (pattern, targs,
22415 explain_p ? tf_warning_or_error : tf_none,
22416 NULL_TREE);
22417 if (dependent)
22418 --processing_template_decl;
22419 if (parm == error_mark_node)
22420 return 1;
22421 }
22422 else
22423 parm = pattern;
22424
22425 /* Unify the pattern with the current argument. */
22426 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22427 explain_p))
22428 return 1;
22429
22430 /* For each parameter pack, collect the deduced value. */
22431 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22432 {
22433 int idx, level;
22434 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22435
22436 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22437 TMPL_ARG (targs, level, idx);
22438 }
22439 }
22440
22441 /* Verify that the results of unification with the parameter packs
22442 produce results consistent with what we've seen before, and make
22443 the deduced argument packs available. */
22444 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22445 {
22446 tree old_pack = TREE_VALUE (pack);
22447 tree new_args = TREE_TYPE (pack);
22448 int i, len = TREE_VEC_LENGTH (new_args);
22449 int idx, level;
22450 bool nondeduced_p = false;
22451
22452 /* By default keep the original deduced argument pack.
22453 If necessary, more specific code is going to update the
22454 resulting deduced argument later down in this function. */
22455 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22456 TMPL_ARG (targs, level, idx) = old_pack;
22457
22458 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22459 actually deduce anything. */
22460 for (i = 0; i < len && !nondeduced_p; ++i)
22461 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22462 nondeduced_p = true;
22463 if (nondeduced_p)
22464 continue;
22465
22466 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22467 {
22468 /* If we had fewer function args than explicit template args,
22469 just use the explicits. */
22470 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22471 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22472 if (len < explicit_len)
22473 new_args = explicit_args;
22474 }
22475
22476 if (!old_pack)
22477 {
22478 tree result;
22479 /* Build the deduced *_ARGUMENT_PACK. */
22480 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22481 {
22482 result = make_node (NONTYPE_ARGUMENT_PACK);
22483 TREE_CONSTANT (result) = 1;
22484 }
22485 else
22486 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22487
22488 SET_ARGUMENT_PACK_ARGS (result, new_args);
22489
22490 /* Note the deduced argument packs for this parameter
22491 pack. */
22492 TMPL_ARG (targs, level, idx) = result;
22493 }
22494 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22495 && (ARGUMENT_PACK_ARGS (old_pack)
22496 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22497 {
22498 /* We only had the explicitly-provided arguments before, but
22499 now we have a complete set of arguments. */
22500 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22501
22502 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22503 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22504 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22505 }
22506 else
22507 {
22508 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22509 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22510
22511 if (!comp_template_args (old_args, new_args,
22512 &bad_old_arg, &bad_new_arg))
22513 /* Inconsistent unification of this parameter pack. */
22514 return unify_parameter_pack_inconsistent (explain_p,
22515 bad_old_arg,
22516 bad_new_arg);
22517 }
22518 }
22519
22520 return unify_success (explain_p);
22521 }
22522
22523 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22524 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22525 parameters and return value are as for unify. */
22526
22527 static int
22528 unify_array_domain (tree tparms, tree targs,
22529 tree parm_dom, tree arg_dom,
22530 bool explain_p)
22531 {
22532 tree parm_max;
22533 tree arg_max;
22534 bool parm_cst;
22535 bool arg_cst;
22536
22537 /* Our representation of array types uses "N - 1" as the
22538 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22539 not an integer constant. We cannot unify arbitrarily
22540 complex expressions, so we eliminate the MINUS_EXPRs
22541 here. */
22542 parm_max = TYPE_MAX_VALUE (parm_dom);
22543 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22544 if (!parm_cst)
22545 {
22546 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22547 parm_max = TREE_OPERAND (parm_max, 0);
22548 }
22549 arg_max = TYPE_MAX_VALUE (arg_dom);
22550 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22551 if (!arg_cst)
22552 {
22553 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22554 trying to unify the type of a variable with the type
22555 of a template parameter. For example:
22556
22557 template <unsigned int N>
22558 void f (char (&) [N]);
22559 int g();
22560 void h(int i) {
22561 char a[g(i)];
22562 f(a);
22563 }
22564
22565 Here, the type of the ARG will be "int [g(i)]", and
22566 may be a SAVE_EXPR, etc. */
22567 if (TREE_CODE (arg_max) != MINUS_EXPR)
22568 return unify_vla_arg (explain_p, arg_dom);
22569 arg_max = TREE_OPERAND (arg_max, 0);
22570 }
22571
22572 /* If only one of the bounds used a MINUS_EXPR, compensate
22573 by adding one to the other bound. */
22574 if (parm_cst && !arg_cst)
22575 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22576 integer_type_node,
22577 parm_max,
22578 integer_one_node);
22579 else if (arg_cst && !parm_cst)
22580 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22581 integer_type_node,
22582 arg_max,
22583 integer_one_node);
22584
22585 return unify (tparms, targs, parm_max, arg_max,
22586 UNIFY_ALLOW_INTEGER, explain_p);
22587 }
22588
22589 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22590
22591 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22592
22593 static pa_kind_t
22594 pa_kind (tree t)
22595 {
22596 if (PACK_EXPANSION_P (t))
22597 t = PACK_EXPANSION_PATTERN (t);
22598 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22599 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22600 || DECL_TYPE_TEMPLATE_P (t))
22601 return pa_tmpl;
22602 else if (TYPE_P (t))
22603 return pa_type;
22604 else
22605 return pa_expr;
22606 }
22607
22608 /* Deduce the value of template parameters. TPARMS is the (innermost)
22609 set of template parameters to a template. TARGS is the bindings
22610 for those template parameters, as determined thus far; TARGS may
22611 include template arguments for outer levels of template parameters
22612 as well. PARM is a parameter to a template function, or a
22613 subcomponent of that parameter; ARG is the corresponding argument.
22614 This function attempts to match PARM with ARG in a manner
22615 consistent with the existing assignments in TARGS. If more values
22616 are deduced, then TARGS is updated.
22617
22618 Returns 0 if the type deduction succeeds, 1 otherwise. The
22619 parameter STRICT is a bitwise or of the following flags:
22620
22621 UNIFY_ALLOW_NONE:
22622 Require an exact match between PARM and ARG.
22623 UNIFY_ALLOW_MORE_CV_QUAL:
22624 Allow the deduced ARG to be more cv-qualified (by qualification
22625 conversion) than ARG.
22626 UNIFY_ALLOW_LESS_CV_QUAL:
22627 Allow the deduced ARG to be less cv-qualified than ARG.
22628 UNIFY_ALLOW_DERIVED:
22629 Allow the deduced ARG to be a template base class of ARG,
22630 or a pointer to a template base class of the type pointed to by
22631 ARG.
22632 UNIFY_ALLOW_INTEGER:
22633 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22634 case for more information.
22635 UNIFY_ALLOW_OUTER_LEVEL:
22636 This is the outermost level of a deduction. Used to determine validity
22637 of qualification conversions. A valid qualification conversion must
22638 have const qualified pointers leading up to the inner type which
22639 requires additional CV quals, except at the outer level, where const
22640 is not required [conv.qual]. It would be normal to set this flag in
22641 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22642 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22643 This is the outermost level of a deduction, and PARM can be more CV
22644 qualified at this point.
22645 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22646 This is the outermost level of a deduction, and PARM can be less CV
22647 qualified at this point. */
22648
22649 static int
22650 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22651 bool explain_p)
22652 {
22653 int idx;
22654 tree targ;
22655 tree tparm;
22656 int strict_in = strict;
22657 tsubst_flags_t complain = (explain_p
22658 ? tf_warning_or_error
22659 : tf_none);
22660
22661 /* I don't think this will do the right thing with respect to types.
22662 But the only case I've seen it in so far has been array bounds, where
22663 signedness is the only information lost, and I think that will be
22664 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22665 finish_id_expression_1, and are also OK. */
22666 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
22667 parm = TREE_OPERAND (parm, 0);
22668
22669 if (arg == error_mark_node)
22670 return unify_invalid (explain_p);
22671 if (arg == unknown_type_node
22672 || arg == init_list_type_node)
22673 /* We can't deduce anything from this, but we might get all the
22674 template args from other function args. */
22675 return unify_success (explain_p);
22676
22677 if (parm == any_targ_node || arg == any_targ_node)
22678 return unify_success (explain_p);
22679
22680 /* If PARM uses template parameters, then we can't bail out here,
22681 even if ARG == PARM, since we won't record unifications for the
22682 template parameters. We might need them if we're trying to
22683 figure out which of two things is more specialized. */
22684 if (arg == parm && !uses_template_parms (parm))
22685 return unify_success (explain_p);
22686
22687 /* Handle init lists early, so the rest of the function can assume
22688 we're dealing with a type. */
22689 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22690 {
22691 tree elt, elttype;
22692 unsigned i;
22693 tree orig_parm = parm;
22694
22695 if (!is_std_init_list (parm)
22696 && TREE_CODE (parm) != ARRAY_TYPE)
22697 /* We can only deduce from an initializer list argument if the
22698 parameter is std::initializer_list or an array; otherwise this
22699 is a non-deduced context. */
22700 return unify_success (explain_p);
22701
22702 if (TREE_CODE (parm) == ARRAY_TYPE)
22703 elttype = TREE_TYPE (parm);
22704 else
22705 {
22706 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22707 /* Deduction is defined in terms of a single type, so just punt
22708 on the (bizarre) std::initializer_list<T...>. */
22709 if (PACK_EXPANSION_P (elttype))
22710 return unify_success (explain_p);
22711 }
22712
22713 if (strict != DEDUCE_EXACT
22714 && TYPE_P (elttype)
22715 && !uses_deducible_template_parms (elttype))
22716 /* If ELTTYPE has no deducible template parms, skip deduction from
22717 the list elements. */;
22718 else
22719 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22720 {
22721 int elt_strict = strict;
22722
22723 if (elt == error_mark_node)
22724 return unify_invalid (explain_p);
22725
22726 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22727 {
22728 tree type = TREE_TYPE (elt);
22729 if (type == error_mark_node)
22730 return unify_invalid (explain_p);
22731 /* It should only be possible to get here for a call. */
22732 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22733 elt_strict |= maybe_adjust_types_for_deduction
22734 (DEDUCE_CALL, &elttype, &type, elt);
22735 elt = type;
22736 }
22737
22738 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22739 explain_p);
22740 }
22741
22742 if (TREE_CODE (parm) == ARRAY_TYPE
22743 && deducible_array_bound (TYPE_DOMAIN (parm)))
22744 {
22745 /* Also deduce from the length of the initializer list. */
22746 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22747 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22748 if (idx == error_mark_node)
22749 return unify_invalid (explain_p);
22750 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22751 idx, explain_p);
22752 }
22753
22754 /* If the std::initializer_list<T> deduction worked, replace the
22755 deduced A with std::initializer_list<A>. */
22756 if (orig_parm != parm)
22757 {
22758 idx = TEMPLATE_TYPE_IDX (orig_parm);
22759 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22760 targ = listify (targ);
22761 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22762 }
22763 return unify_success (explain_p);
22764 }
22765
22766 /* If parm and arg aren't the same kind of thing (template, type, or
22767 expression), fail early. */
22768 if (pa_kind (parm) != pa_kind (arg))
22769 return unify_invalid (explain_p);
22770
22771 /* Immediately reject some pairs that won't unify because of
22772 cv-qualification mismatches. */
22773 if (TREE_CODE (arg) == TREE_CODE (parm)
22774 && TYPE_P (arg)
22775 /* It is the elements of the array which hold the cv quals of an array
22776 type, and the elements might be template type parms. We'll check
22777 when we recurse. */
22778 && TREE_CODE (arg) != ARRAY_TYPE
22779 /* We check the cv-qualifiers when unifying with template type
22780 parameters below. We want to allow ARG `const T' to unify with
22781 PARM `T' for example, when computing which of two templates
22782 is more specialized, for example. */
22783 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22784 && !check_cv_quals_for_unify (strict_in, arg, parm))
22785 return unify_cv_qual_mismatch (explain_p, parm, arg);
22786
22787 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22788 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22789 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22790 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22791 strict &= ~UNIFY_ALLOW_DERIVED;
22792 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22793 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22794
22795 switch (TREE_CODE (parm))
22796 {
22797 case TYPENAME_TYPE:
22798 case SCOPE_REF:
22799 case UNBOUND_CLASS_TEMPLATE:
22800 /* In a type which contains a nested-name-specifier, template
22801 argument values cannot be deduced for template parameters used
22802 within the nested-name-specifier. */
22803 return unify_success (explain_p);
22804
22805 case TEMPLATE_TYPE_PARM:
22806 case TEMPLATE_TEMPLATE_PARM:
22807 case BOUND_TEMPLATE_TEMPLATE_PARM:
22808 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22809 if (error_operand_p (tparm))
22810 return unify_invalid (explain_p);
22811
22812 if (TEMPLATE_TYPE_LEVEL (parm)
22813 != template_decl_level (tparm))
22814 /* The PARM is not one we're trying to unify. Just check
22815 to see if it matches ARG. */
22816 {
22817 if (TREE_CODE (arg) == TREE_CODE (parm)
22818 && (is_auto (parm) ? is_auto (arg)
22819 : same_type_p (parm, arg)))
22820 return unify_success (explain_p);
22821 else
22822 return unify_type_mismatch (explain_p, parm, arg);
22823 }
22824 idx = TEMPLATE_TYPE_IDX (parm);
22825 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22826 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22827 if (error_operand_p (tparm))
22828 return unify_invalid (explain_p);
22829
22830 /* Check for mixed types and values. */
22831 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22832 && TREE_CODE (tparm) != TYPE_DECL)
22833 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22834 && TREE_CODE (tparm) != TEMPLATE_DECL))
22835 gcc_unreachable ();
22836
22837 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22838 {
22839 if ((strict_in & UNIFY_ALLOW_DERIVED)
22840 && CLASS_TYPE_P (arg))
22841 {
22842 /* First try to match ARG directly. */
22843 tree t = try_class_unification (tparms, targs, parm, arg,
22844 explain_p);
22845 if (!t)
22846 {
22847 /* Otherwise, look for a suitable base of ARG, as below. */
22848 enum template_base_result r;
22849 r = get_template_base (tparms, targs, parm, arg,
22850 explain_p, &t);
22851 if (!t)
22852 return unify_no_common_base (explain_p, r, parm, arg);
22853 arg = t;
22854 }
22855 }
22856 /* ARG must be constructed from a template class or a template
22857 template parameter. */
22858 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22859 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22860 return unify_template_deduction_failure (explain_p, parm, arg);
22861
22862 /* Deduce arguments T, i from TT<T> or TT<i>. */
22863 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22864 return 1;
22865
22866 arg = TYPE_TI_TEMPLATE (arg);
22867
22868 /* Fall through to deduce template name. */
22869 }
22870
22871 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22872 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22873 {
22874 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22875
22876 /* Simple cases: Value already set, does match or doesn't. */
22877 if (targ != NULL_TREE && template_args_equal (targ, arg))
22878 return unify_success (explain_p);
22879 else if (targ)
22880 return unify_inconsistency (explain_p, parm, targ, arg);
22881 }
22882 else
22883 {
22884 /* If PARM is `const T' and ARG is only `int', we don't have
22885 a match unless we are allowing additional qualification.
22886 If ARG is `const int' and PARM is just `T' that's OK;
22887 that binds `const int' to `T'. */
22888 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22889 arg, parm))
22890 return unify_cv_qual_mismatch (explain_p, parm, arg);
22891
22892 /* Consider the case where ARG is `const volatile int' and
22893 PARM is `const T'. Then, T should be `volatile int'. */
22894 arg = cp_build_qualified_type_real
22895 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22896 if (arg == error_mark_node)
22897 return unify_invalid (explain_p);
22898
22899 /* Simple cases: Value already set, does match or doesn't. */
22900 if (targ != NULL_TREE && same_type_p (targ, arg))
22901 return unify_success (explain_p);
22902 else if (targ)
22903 return unify_inconsistency (explain_p, parm, targ, arg);
22904
22905 /* Make sure that ARG is not a variable-sized array. (Note
22906 that were talking about variable-sized arrays (like
22907 `int[n]'), rather than arrays of unknown size (like
22908 `int[]').) We'll get very confused by such a type since
22909 the bound of the array is not constant, and therefore
22910 not mangleable. Besides, such types are not allowed in
22911 ISO C++, so we can do as we please here. We do allow
22912 them for 'auto' deduction, since that isn't ABI-exposed. */
22913 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22914 return unify_vla_arg (explain_p, arg);
22915
22916 /* Strip typedefs as in convert_template_argument. */
22917 arg = canonicalize_type_argument (arg, tf_none);
22918 }
22919
22920 /* If ARG is a parameter pack or an expansion, we cannot unify
22921 against it unless PARM is also a parameter pack. */
22922 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22923 && !template_parameter_pack_p (parm))
22924 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22925
22926 /* If the argument deduction results is a METHOD_TYPE,
22927 then there is a problem.
22928 METHOD_TYPE doesn't map to any real C++ type the result of
22929 the deduction cannot be of that type. */
22930 if (TREE_CODE (arg) == METHOD_TYPE)
22931 return unify_method_type_error (explain_p, arg);
22932
22933 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22934 return unify_success (explain_p);
22935
22936 case TEMPLATE_PARM_INDEX:
22937 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22938 if (error_operand_p (tparm))
22939 return unify_invalid (explain_p);
22940
22941 if (TEMPLATE_PARM_LEVEL (parm)
22942 != template_decl_level (tparm))
22943 {
22944 /* The PARM is not one we're trying to unify. Just check
22945 to see if it matches ARG. */
22946 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22947 && cp_tree_equal (parm, arg));
22948 if (result)
22949 unify_expression_unequal (explain_p, parm, arg);
22950 return result;
22951 }
22952
22953 idx = TEMPLATE_PARM_IDX (parm);
22954 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22955
22956 if (targ)
22957 {
22958 if ((strict & UNIFY_ALLOW_INTEGER)
22959 && TREE_TYPE (targ) && TREE_TYPE (arg)
22960 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22961 /* We're deducing from an array bound, the type doesn't matter. */
22962 arg = fold_convert (TREE_TYPE (targ), arg);
22963 int x = !cp_tree_equal (targ, arg);
22964 if (x)
22965 unify_inconsistency (explain_p, parm, targ, arg);
22966 return x;
22967 }
22968
22969 /* [temp.deduct.type] If, in the declaration of a function template
22970 with a non-type template-parameter, the non-type
22971 template-parameter is used in an expression in the function
22972 parameter-list and, if the corresponding template-argument is
22973 deduced, the template-argument type shall match the type of the
22974 template-parameter exactly, except that a template-argument
22975 deduced from an array bound may be of any integral type.
22976 The non-type parameter might use already deduced type parameters. */
22977 tparm = TREE_TYPE (parm);
22978 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22979 /* We don't have enough levels of args to do any substitution. This
22980 can happen in the context of -fnew-ttp-matching. */;
22981 else
22982 {
22983 ++processing_template_decl;
22984 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22985 --processing_template_decl;
22986
22987 if (tree a = type_uses_auto (tparm))
22988 {
22989 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22990 if (tparm == error_mark_node)
22991 return 1;
22992 }
22993 }
22994
22995 if (!TREE_TYPE (arg))
22996 /* Template-parameter dependent expression. Just accept it for now.
22997 It will later be processed in convert_template_argument. */
22998 ;
22999 else if (same_type_ignoring_top_level_qualifiers_p
23000 (non_reference (TREE_TYPE (arg)),
23001 non_reference (tparm)))
23002 /* OK. Ignore top-level quals here because a class-type template
23003 parameter object is const. */;
23004 else if ((strict & UNIFY_ALLOW_INTEGER)
23005 && CP_INTEGRAL_TYPE_P (tparm))
23006 /* Convert the ARG to the type of PARM; the deduced non-type
23007 template argument must exactly match the types of the
23008 corresponding parameter. */
23009 arg = fold (build_nop (tparm, arg));
23010 else if (uses_template_parms (tparm))
23011 {
23012 /* We haven't deduced the type of this parameter yet. */
23013 if (cxx_dialect >= cxx17
23014 /* We deduce from array bounds in try_array_deduction. */
23015 && !(strict & UNIFY_ALLOW_INTEGER))
23016 {
23017 /* Deduce it from the non-type argument. */
23018 tree atype = TREE_TYPE (arg);
23019 RECUR_AND_CHECK_FAILURE (tparms, targs,
23020 tparm, atype,
23021 UNIFY_ALLOW_NONE, explain_p);
23022 }
23023 else
23024 /* Try again later. */
23025 return unify_success (explain_p);
23026 }
23027 else
23028 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23029
23030 /* If ARG is a parameter pack or an expansion, we cannot unify
23031 against it unless PARM is also a parameter pack. */
23032 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23033 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23034 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23035
23036 {
23037 bool removed_attr = false;
23038 arg = strip_typedefs_expr (arg, &removed_attr);
23039 }
23040 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23041 return unify_success (explain_p);
23042
23043 case PTRMEM_CST:
23044 {
23045 /* A pointer-to-member constant can be unified only with
23046 another constant. */
23047 if (TREE_CODE (arg) != PTRMEM_CST)
23048 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23049
23050 /* Just unify the class member. It would be useless (and possibly
23051 wrong, depending on the strict flags) to unify also
23052 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23053 arg refer to the same variable, even if through different
23054 classes. For instance:
23055
23056 struct A { int x; };
23057 struct B : A { };
23058
23059 Unification of &A::x and &B::x must succeed. */
23060 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23061 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23062 }
23063
23064 case POINTER_TYPE:
23065 {
23066 if (!TYPE_PTR_P (arg))
23067 return unify_type_mismatch (explain_p, parm, arg);
23068
23069 /* [temp.deduct.call]
23070
23071 A can be another pointer or pointer to member type that can
23072 be converted to the deduced A via a qualification
23073 conversion (_conv.qual_).
23074
23075 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23076 This will allow for additional cv-qualification of the
23077 pointed-to types if appropriate. */
23078
23079 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23080 /* The derived-to-base conversion only persists through one
23081 level of pointers. */
23082 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23083
23084 return unify (tparms, targs, TREE_TYPE (parm),
23085 TREE_TYPE (arg), strict, explain_p);
23086 }
23087
23088 case REFERENCE_TYPE:
23089 if (!TYPE_REF_P (arg))
23090 return unify_type_mismatch (explain_p, parm, arg);
23091 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23092 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23093
23094 case ARRAY_TYPE:
23095 if (TREE_CODE (arg) != ARRAY_TYPE)
23096 return unify_type_mismatch (explain_p, parm, arg);
23097 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23098 != (TYPE_DOMAIN (arg) == NULL_TREE))
23099 return unify_type_mismatch (explain_p, parm, arg);
23100 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23101 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23102 if (TYPE_DOMAIN (parm) != NULL_TREE)
23103 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23104 TYPE_DOMAIN (arg), explain_p);
23105 return unify_success (explain_p);
23106
23107 case REAL_TYPE:
23108 case COMPLEX_TYPE:
23109 case VECTOR_TYPE:
23110 case INTEGER_TYPE:
23111 case BOOLEAN_TYPE:
23112 case ENUMERAL_TYPE:
23113 case VOID_TYPE:
23114 case NULLPTR_TYPE:
23115 if (TREE_CODE (arg) != TREE_CODE (parm))
23116 return unify_type_mismatch (explain_p, parm, arg);
23117
23118 /* We have already checked cv-qualification at the top of the
23119 function. */
23120 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23121 return unify_type_mismatch (explain_p, parm, arg);
23122
23123 /* As far as unification is concerned, this wins. Later checks
23124 will invalidate it if necessary. */
23125 return unify_success (explain_p);
23126
23127 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23128 /* Type INTEGER_CST can come from ordinary constant template args. */
23129 case INTEGER_CST:
23130 while (CONVERT_EXPR_P (arg))
23131 arg = TREE_OPERAND (arg, 0);
23132
23133 if (TREE_CODE (arg) != INTEGER_CST)
23134 return unify_template_argument_mismatch (explain_p, parm, arg);
23135 return (tree_int_cst_equal (parm, arg)
23136 ? unify_success (explain_p)
23137 : unify_template_argument_mismatch (explain_p, parm, arg));
23138
23139 case TREE_VEC:
23140 {
23141 int i, len, argslen;
23142 int parm_variadic_p = 0;
23143
23144 if (TREE_CODE (arg) != TREE_VEC)
23145 return unify_template_argument_mismatch (explain_p, parm, arg);
23146
23147 len = TREE_VEC_LENGTH (parm);
23148 argslen = TREE_VEC_LENGTH (arg);
23149
23150 /* Check for pack expansions in the parameters. */
23151 for (i = 0; i < len; ++i)
23152 {
23153 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23154 {
23155 if (i == len - 1)
23156 /* We can unify against something with a trailing
23157 parameter pack. */
23158 parm_variadic_p = 1;
23159 else
23160 /* [temp.deduct.type]/9: If the template argument list of
23161 P contains a pack expansion that is not the last
23162 template argument, the entire template argument list
23163 is a non-deduced context. */
23164 return unify_success (explain_p);
23165 }
23166 }
23167
23168 /* If we don't have enough arguments to satisfy the parameters
23169 (not counting the pack expression at the end), or we have
23170 too many arguments for a parameter list that doesn't end in
23171 a pack expression, we can't unify. */
23172 if (parm_variadic_p
23173 ? argslen < len - parm_variadic_p
23174 : argslen != len)
23175 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23176
23177 /* Unify all of the parameters that precede the (optional)
23178 pack expression. */
23179 for (i = 0; i < len - parm_variadic_p; ++i)
23180 {
23181 RECUR_AND_CHECK_FAILURE (tparms, targs,
23182 TREE_VEC_ELT (parm, i),
23183 TREE_VEC_ELT (arg, i),
23184 UNIFY_ALLOW_NONE, explain_p);
23185 }
23186 if (parm_variadic_p)
23187 return unify_pack_expansion (tparms, targs, parm, arg,
23188 DEDUCE_EXACT,
23189 /*subr=*/true, explain_p);
23190 return unify_success (explain_p);
23191 }
23192
23193 case RECORD_TYPE:
23194 case UNION_TYPE:
23195 if (TREE_CODE (arg) != TREE_CODE (parm))
23196 return unify_type_mismatch (explain_p, parm, arg);
23197
23198 if (TYPE_PTRMEMFUNC_P (parm))
23199 {
23200 if (!TYPE_PTRMEMFUNC_P (arg))
23201 return unify_type_mismatch (explain_p, parm, arg);
23202
23203 return unify (tparms, targs,
23204 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23205 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23206 strict, explain_p);
23207 }
23208 else if (TYPE_PTRMEMFUNC_P (arg))
23209 return unify_type_mismatch (explain_p, parm, arg);
23210
23211 if (CLASSTYPE_TEMPLATE_INFO (parm))
23212 {
23213 tree t = NULL_TREE;
23214
23215 if (strict_in & UNIFY_ALLOW_DERIVED)
23216 {
23217 /* First, we try to unify the PARM and ARG directly. */
23218 t = try_class_unification (tparms, targs,
23219 parm, arg, explain_p);
23220
23221 if (!t)
23222 {
23223 /* Fallback to the special case allowed in
23224 [temp.deduct.call]:
23225
23226 If P is a class, and P has the form
23227 template-id, then A can be a derived class of
23228 the deduced A. Likewise, if P is a pointer to
23229 a class of the form template-id, A can be a
23230 pointer to a derived class pointed to by the
23231 deduced A. */
23232 enum template_base_result r;
23233 r = get_template_base (tparms, targs, parm, arg,
23234 explain_p, &t);
23235
23236 if (!t)
23237 {
23238 /* Don't give the derived diagnostic if we're
23239 already dealing with the same template. */
23240 bool same_template
23241 = (CLASSTYPE_TEMPLATE_INFO (arg)
23242 && (CLASSTYPE_TI_TEMPLATE (parm)
23243 == CLASSTYPE_TI_TEMPLATE (arg)));
23244 return unify_no_common_base (explain_p && !same_template,
23245 r, parm, arg);
23246 }
23247 }
23248 }
23249 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23250 && (CLASSTYPE_TI_TEMPLATE (parm)
23251 == CLASSTYPE_TI_TEMPLATE (arg)))
23252 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23253 Then, we should unify `int' and `U'. */
23254 t = arg;
23255 else
23256 /* There's no chance of unification succeeding. */
23257 return unify_type_mismatch (explain_p, parm, arg);
23258
23259 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23260 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23261 }
23262 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23263 return unify_type_mismatch (explain_p, parm, arg);
23264 return unify_success (explain_p);
23265
23266 case METHOD_TYPE:
23267 case FUNCTION_TYPE:
23268 {
23269 unsigned int nargs;
23270 tree *args;
23271 tree a;
23272 unsigned int i;
23273
23274 if (TREE_CODE (arg) != TREE_CODE (parm))
23275 return unify_type_mismatch (explain_p, parm, arg);
23276
23277 /* CV qualifications for methods can never be deduced, they must
23278 match exactly. We need to check them explicitly here,
23279 because type_unification_real treats them as any other
23280 cv-qualified parameter. */
23281 if (TREE_CODE (parm) == METHOD_TYPE
23282 && (!check_cv_quals_for_unify
23283 (UNIFY_ALLOW_NONE,
23284 class_of_this_parm (arg),
23285 class_of_this_parm (parm))))
23286 return unify_cv_qual_mismatch (explain_p, parm, arg);
23287 if (TREE_CODE (arg) == FUNCTION_TYPE
23288 && type_memfn_quals (parm) != type_memfn_quals (arg))
23289 return unify_cv_qual_mismatch (explain_p, parm, arg);
23290 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23291 return unify_type_mismatch (explain_p, parm, arg);
23292
23293 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23294 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23295
23296 nargs = list_length (TYPE_ARG_TYPES (arg));
23297 args = XALLOCAVEC (tree, nargs);
23298 for (a = TYPE_ARG_TYPES (arg), i = 0;
23299 a != NULL_TREE && a != void_list_node;
23300 a = TREE_CHAIN (a), ++i)
23301 args[i] = TREE_VALUE (a);
23302 nargs = i;
23303
23304 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23305 args, nargs, 1, DEDUCE_EXACT,
23306 NULL, explain_p))
23307 return 1;
23308
23309 if (flag_noexcept_type)
23310 {
23311 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23312 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23313 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23314 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23315 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23316 && uses_template_parms (TREE_PURPOSE (pspec)))
23317 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23318 TREE_PURPOSE (aspec),
23319 UNIFY_ALLOW_NONE, explain_p);
23320 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23321 return unify_type_mismatch (explain_p, parm, arg);
23322 }
23323
23324 return 0;
23325 }
23326
23327 case OFFSET_TYPE:
23328 /* Unify a pointer to member with a pointer to member function, which
23329 deduces the type of the member as a function type. */
23330 if (TYPE_PTRMEMFUNC_P (arg))
23331 {
23332 /* Check top-level cv qualifiers */
23333 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23334 return unify_cv_qual_mismatch (explain_p, parm, arg);
23335
23336 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23337 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23338 UNIFY_ALLOW_NONE, explain_p);
23339
23340 /* Determine the type of the function we are unifying against. */
23341 tree fntype = static_fn_type (arg);
23342
23343 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23344 }
23345
23346 if (TREE_CODE (arg) != OFFSET_TYPE)
23347 return unify_type_mismatch (explain_p, parm, arg);
23348 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23349 TYPE_OFFSET_BASETYPE (arg),
23350 UNIFY_ALLOW_NONE, explain_p);
23351 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23352 strict, explain_p);
23353
23354 case CONST_DECL:
23355 if (DECL_TEMPLATE_PARM_P (parm))
23356 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23357 if (arg != scalar_constant_value (parm))
23358 return unify_template_argument_mismatch (explain_p, parm, arg);
23359 return unify_success (explain_p);
23360
23361 case FIELD_DECL:
23362 case TEMPLATE_DECL:
23363 /* Matched cases are handled by the ARG == PARM test above. */
23364 return unify_template_argument_mismatch (explain_p, parm, arg);
23365
23366 case VAR_DECL:
23367 /* We might get a variable as a non-type template argument in parm if the
23368 corresponding parameter is type-dependent. Make any necessary
23369 adjustments based on whether arg is a reference. */
23370 if (CONSTANT_CLASS_P (arg))
23371 parm = fold_non_dependent_expr (parm, complain);
23372 else if (REFERENCE_REF_P (arg))
23373 {
23374 tree sub = TREE_OPERAND (arg, 0);
23375 STRIP_NOPS (sub);
23376 if (TREE_CODE (sub) == ADDR_EXPR)
23377 arg = TREE_OPERAND (sub, 0);
23378 }
23379 /* Now use the normal expression code to check whether they match. */
23380 goto expr;
23381
23382 case TYPE_ARGUMENT_PACK:
23383 case NONTYPE_ARGUMENT_PACK:
23384 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23385 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23386
23387 case TYPEOF_TYPE:
23388 case DECLTYPE_TYPE:
23389 case UNDERLYING_TYPE:
23390 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23391 or UNDERLYING_TYPE nodes. */
23392 return unify_success (explain_p);
23393
23394 case ERROR_MARK:
23395 /* Unification fails if we hit an error node. */
23396 return unify_invalid (explain_p);
23397
23398 case INDIRECT_REF:
23399 if (REFERENCE_REF_P (parm))
23400 {
23401 bool pexp = PACK_EXPANSION_P (arg);
23402 if (pexp)
23403 arg = PACK_EXPANSION_PATTERN (arg);
23404 if (REFERENCE_REF_P (arg))
23405 arg = TREE_OPERAND (arg, 0);
23406 if (pexp)
23407 arg = make_pack_expansion (arg, complain);
23408 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23409 strict, explain_p);
23410 }
23411 /* FALLTHRU */
23412
23413 default:
23414 /* An unresolved overload is a nondeduced context. */
23415 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23416 return unify_success (explain_p);
23417 gcc_assert (EXPR_P (parm)
23418 || COMPOUND_LITERAL_P (parm)
23419 || TREE_CODE (parm) == TRAIT_EXPR);
23420 expr:
23421 /* We must be looking at an expression. This can happen with
23422 something like:
23423
23424 template <int I>
23425 void foo(S<I>, S<I + 2>);
23426
23427 or
23428
23429 template<typename T>
23430 void foo(A<T, T{}>);
23431
23432 This is a "non-deduced context":
23433
23434 [deduct.type]
23435
23436 The non-deduced contexts are:
23437
23438 --A non-type template argument or an array bound in which
23439 a subexpression references a template parameter.
23440
23441 In these cases, we assume deduction succeeded, but don't
23442 actually infer any unifications. */
23443
23444 if (!uses_template_parms (parm)
23445 && !template_args_equal (parm, arg))
23446 return unify_expression_unequal (explain_p, parm, arg);
23447 else
23448 return unify_success (explain_p);
23449 }
23450 }
23451 #undef RECUR_AND_CHECK_FAILURE
23452 \f
23453 /* Note that DECL can be defined in this translation unit, if
23454 required. */
23455
23456 static void
23457 mark_definable (tree decl)
23458 {
23459 tree clone;
23460 DECL_NOT_REALLY_EXTERN (decl) = 1;
23461 FOR_EACH_CLONE (clone, decl)
23462 DECL_NOT_REALLY_EXTERN (clone) = 1;
23463 }
23464
23465 /* Called if RESULT is explicitly instantiated, or is a member of an
23466 explicitly instantiated class. */
23467
23468 void
23469 mark_decl_instantiated (tree result, int extern_p)
23470 {
23471 SET_DECL_EXPLICIT_INSTANTIATION (result);
23472
23473 /* If this entity has already been written out, it's too late to
23474 make any modifications. */
23475 if (TREE_ASM_WRITTEN (result))
23476 return;
23477
23478 /* For anonymous namespace we don't need to do anything. */
23479 if (decl_anon_ns_mem_p (result))
23480 {
23481 gcc_assert (!TREE_PUBLIC (result));
23482 return;
23483 }
23484
23485 if (TREE_CODE (result) != FUNCTION_DECL)
23486 /* The TREE_PUBLIC flag for function declarations will have been
23487 set correctly by tsubst. */
23488 TREE_PUBLIC (result) = 1;
23489
23490 /* This might have been set by an earlier implicit instantiation. */
23491 DECL_COMDAT (result) = 0;
23492
23493 if (extern_p)
23494 DECL_NOT_REALLY_EXTERN (result) = 0;
23495 else
23496 {
23497 mark_definable (result);
23498 mark_needed (result);
23499 /* Always make artificials weak. */
23500 if (DECL_ARTIFICIAL (result) && flag_weak)
23501 comdat_linkage (result);
23502 /* For WIN32 we also want to put explicit instantiations in
23503 linkonce sections. */
23504 else if (TREE_PUBLIC (result))
23505 maybe_make_one_only (result);
23506 if (TREE_CODE (result) == FUNCTION_DECL
23507 && DECL_TEMPLATE_INSTANTIATED (result))
23508 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23509 since start_preparsed_function wouldn't have if we had an earlier
23510 extern explicit instantiation. */
23511 DECL_EXTERNAL (result) = 0;
23512 }
23513
23514 /* If EXTERN_P, then this function will not be emitted -- unless
23515 followed by an explicit instantiation, at which point its linkage
23516 will be adjusted. If !EXTERN_P, then this function will be
23517 emitted here. In neither circumstance do we want
23518 import_export_decl to adjust the linkage. */
23519 DECL_INTERFACE_KNOWN (result) = 1;
23520 }
23521
23522 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23523 important template arguments. If any are missing, we check whether
23524 they're important by using error_mark_node for substituting into any
23525 args that were used for partial ordering (the ones between ARGS and END)
23526 and seeing if it bubbles up. */
23527
23528 static bool
23529 check_undeduced_parms (tree targs, tree args, tree end)
23530 {
23531 bool found = false;
23532 int i;
23533 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23534 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23535 {
23536 found = true;
23537 TREE_VEC_ELT (targs, i) = error_mark_node;
23538 }
23539 if (found)
23540 {
23541 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23542 if (substed == error_mark_node)
23543 return true;
23544 }
23545 return false;
23546 }
23547
23548 /* Given two function templates PAT1 and PAT2, return:
23549
23550 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23551 -1 if PAT2 is more specialized than PAT1.
23552 0 if neither is more specialized.
23553
23554 LEN indicates the number of parameters we should consider
23555 (defaulted parameters should not be considered).
23556
23557 The 1998 std underspecified function template partial ordering, and
23558 DR214 addresses the issue. We take pairs of arguments, one from
23559 each of the templates, and deduce them against each other. One of
23560 the templates will be more specialized if all the *other*
23561 template's arguments deduce against its arguments and at least one
23562 of its arguments *does* *not* deduce against the other template's
23563 corresponding argument. Deduction is done as for class templates.
23564 The arguments used in deduction have reference and top level cv
23565 qualifiers removed. Iff both arguments were originally reference
23566 types *and* deduction succeeds in both directions, an lvalue reference
23567 wins against an rvalue reference and otherwise the template
23568 with the more cv-qualified argument wins for that pairing (if
23569 neither is more cv-qualified, they both are equal). Unlike regular
23570 deduction, after all the arguments have been deduced in this way,
23571 we do *not* verify the deduced template argument values can be
23572 substituted into non-deduced contexts.
23573
23574 The logic can be a bit confusing here, because we look at deduce1 and
23575 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23576 can find template arguments for pat1 to make arg1 look like arg2, that
23577 means that arg2 is at least as specialized as arg1. */
23578
23579 int
23580 more_specialized_fn (tree pat1, tree pat2, int len)
23581 {
23582 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23583 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23584 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23585 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23586 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23587 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23588 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23589 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23590 tree origs1, origs2;
23591 bool lose1 = false;
23592 bool lose2 = false;
23593
23594 /* Remove the this parameter from non-static member functions. If
23595 one is a non-static member function and the other is not a static
23596 member function, remove the first parameter from that function
23597 also. This situation occurs for operator functions where we
23598 locate both a member function (with this pointer) and non-member
23599 operator (with explicit first operand). */
23600 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23601 {
23602 len--; /* LEN is the number of significant arguments for DECL1 */
23603 args1 = TREE_CHAIN (args1);
23604 if (!DECL_STATIC_FUNCTION_P (decl2))
23605 args2 = TREE_CHAIN (args2);
23606 }
23607 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
23608 {
23609 args2 = TREE_CHAIN (args2);
23610 if (!DECL_STATIC_FUNCTION_P (decl1))
23611 {
23612 len--;
23613 args1 = TREE_CHAIN (args1);
23614 }
23615 }
23616
23617 /* If only one is a conversion operator, they are unordered. */
23618 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23619 return 0;
23620
23621 /* Consider the return type for a conversion function */
23622 if (DECL_CONV_FN_P (decl1))
23623 {
23624 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23625 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23626 len++;
23627 }
23628
23629 processing_template_decl++;
23630
23631 origs1 = args1;
23632 origs2 = args2;
23633
23634 while (len--
23635 /* Stop when an ellipsis is seen. */
23636 && args1 != NULL_TREE && args2 != NULL_TREE)
23637 {
23638 tree arg1 = TREE_VALUE (args1);
23639 tree arg2 = TREE_VALUE (args2);
23640 int deduce1, deduce2;
23641 int quals1 = -1;
23642 int quals2 = -1;
23643 int ref1 = 0;
23644 int ref2 = 0;
23645
23646 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23647 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23648 {
23649 /* When both arguments are pack expansions, we need only
23650 unify the patterns themselves. */
23651 arg1 = PACK_EXPANSION_PATTERN (arg1);
23652 arg2 = PACK_EXPANSION_PATTERN (arg2);
23653
23654 /* This is the last comparison we need to do. */
23655 len = 0;
23656 }
23657
23658 /* DR 1847: If a particular P contains no template-parameters that
23659 participate in template argument deduction, that P is not used to
23660 determine the ordering. */
23661 if (!uses_deducible_template_parms (arg1)
23662 && !uses_deducible_template_parms (arg2))
23663 goto next;
23664
23665 if (TYPE_REF_P (arg1))
23666 {
23667 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23668 arg1 = TREE_TYPE (arg1);
23669 quals1 = cp_type_quals (arg1);
23670 }
23671
23672 if (TYPE_REF_P (arg2))
23673 {
23674 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23675 arg2 = TREE_TYPE (arg2);
23676 quals2 = cp_type_quals (arg2);
23677 }
23678
23679 arg1 = TYPE_MAIN_VARIANT (arg1);
23680 arg2 = TYPE_MAIN_VARIANT (arg2);
23681
23682 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23683 {
23684 int i, len2 = remaining_arguments (args2);
23685 tree parmvec = make_tree_vec (1);
23686 tree argvec = make_tree_vec (len2);
23687 tree ta = args2;
23688
23689 /* Setup the parameter vector, which contains only ARG1. */
23690 TREE_VEC_ELT (parmvec, 0) = arg1;
23691
23692 /* Setup the argument vector, which contains the remaining
23693 arguments. */
23694 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23695 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23696
23697 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23698 argvec, DEDUCE_EXACT,
23699 /*subr=*/true, /*explain_p=*/false)
23700 == 0);
23701
23702 /* We cannot deduce in the other direction, because ARG1 is
23703 a pack expansion but ARG2 is not. */
23704 deduce2 = 0;
23705 }
23706 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23707 {
23708 int i, len1 = remaining_arguments (args1);
23709 tree parmvec = make_tree_vec (1);
23710 tree argvec = make_tree_vec (len1);
23711 tree ta = args1;
23712
23713 /* Setup the parameter vector, which contains only ARG1. */
23714 TREE_VEC_ELT (parmvec, 0) = arg2;
23715
23716 /* Setup the argument vector, which contains the remaining
23717 arguments. */
23718 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23719 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23720
23721 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23722 argvec, DEDUCE_EXACT,
23723 /*subr=*/true, /*explain_p=*/false)
23724 == 0);
23725
23726 /* We cannot deduce in the other direction, because ARG2 is
23727 a pack expansion but ARG1 is not.*/
23728 deduce1 = 0;
23729 }
23730
23731 else
23732 {
23733 /* The normal case, where neither argument is a pack
23734 expansion. */
23735 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23736 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23737 == 0);
23738 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23739 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23740 == 0);
23741 }
23742
23743 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23744 arg2, then arg2 is not as specialized as arg1. */
23745 if (!deduce1)
23746 lose2 = true;
23747 if (!deduce2)
23748 lose1 = true;
23749
23750 /* "If, for a given type, deduction succeeds in both directions
23751 (i.e., the types are identical after the transformations above)
23752 and both P and A were reference types (before being replaced with
23753 the type referred to above):
23754 - if the type from the argument template was an lvalue reference and
23755 the type from the parameter template was not, the argument type is
23756 considered to be more specialized than the other; otherwise,
23757 - if the type from the argument template is more cv-qualified
23758 than the type from the parameter template (as described above),
23759 the argument type is considered to be more specialized than the other;
23760 otherwise,
23761 - neither type is more specialized than the other." */
23762
23763 if (deduce1 && deduce2)
23764 {
23765 if (ref1 && ref2 && ref1 != ref2)
23766 {
23767 if (ref1 > ref2)
23768 lose1 = true;
23769 else
23770 lose2 = true;
23771 }
23772 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23773 {
23774 if ((quals1 & quals2) == quals2)
23775 lose2 = true;
23776 if ((quals1 & quals2) == quals1)
23777 lose1 = true;
23778 }
23779 }
23780
23781 if (lose1 && lose2)
23782 /* We've failed to deduce something in either direction.
23783 These must be unordered. */
23784 break;
23785
23786 next:
23787
23788 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23789 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23790 /* We have already processed all of the arguments in our
23791 handing of the pack expansion type. */
23792 len = 0;
23793
23794 args1 = TREE_CHAIN (args1);
23795 args2 = TREE_CHAIN (args2);
23796 }
23797
23798 /* "In most cases, all template parameters must have values in order for
23799 deduction to succeed, but for partial ordering purposes a template
23800 parameter may remain without a value provided it is not used in the
23801 types being used for partial ordering."
23802
23803 Thus, if we are missing any of the targs1 we need to substitute into
23804 origs1, then pat2 is not as specialized as pat1. This can happen when
23805 there is a nondeduced context. */
23806 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23807 lose2 = true;
23808 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23809 lose1 = true;
23810
23811 processing_template_decl--;
23812
23813 /* If both deductions succeed, the partial ordering selects the more
23814 constrained template. */
23815 if (!lose1 && !lose2)
23816 {
23817 int winner = more_constrained (decl1, decl2);
23818 if (winner > 0)
23819 lose2 = true;
23820 else if (winner < 0)
23821 lose1 = true;
23822 }
23823
23824 /* All things being equal, if the next argument is a pack expansion
23825 for one function but not for the other, prefer the
23826 non-variadic function. FIXME this is bogus; see c++/41958. */
23827 if (lose1 == lose2
23828 && args1 && TREE_VALUE (args1)
23829 && args2 && TREE_VALUE (args2))
23830 {
23831 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23832 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23833 }
23834
23835 if (lose1 == lose2)
23836 return 0;
23837 else if (!lose1)
23838 return 1;
23839 else
23840 return -1;
23841 }
23842
23843 /* Determine which of two partial specializations of TMPL is more
23844 specialized.
23845
23846 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23847 to the first partial specialization. The TREE_PURPOSE is the
23848 innermost set of template parameters for the partial
23849 specialization. PAT2 is similar, but for the second template.
23850
23851 Return 1 if the first partial specialization is more specialized;
23852 -1 if the second is more specialized; 0 if neither is more
23853 specialized.
23854
23855 See [temp.class.order] for information about determining which of
23856 two templates is more specialized. */
23857
23858 static int
23859 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23860 {
23861 tree targs;
23862 int winner = 0;
23863 bool any_deductions = false;
23864
23865 tree tmpl1 = TREE_VALUE (pat1);
23866 tree tmpl2 = TREE_VALUE (pat2);
23867 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23868 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23869
23870 /* Just like what happens for functions, if we are ordering between
23871 different template specializations, we may encounter dependent
23872 types in the arguments, and we need our dependency check functions
23873 to behave correctly. */
23874 ++processing_template_decl;
23875 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23876 if (targs)
23877 {
23878 --winner;
23879 any_deductions = true;
23880 }
23881
23882 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23883 if (targs)
23884 {
23885 ++winner;
23886 any_deductions = true;
23887 }
23888 --processing_template_decl;
23889
23890 /* If both deductions succeed, the partial ordering selects the more
23891 constrained template. */
23892 if (!winner && any_deductions)
23893 winner = more_constrained (tmpl1, tmpl2);
23894
23895 /* In the case of a tie where at least one of the templates
23896 has a parameter pack at the end, the template with the most
23897 non-packed parameters wins. */
23898 if (winner == 0
23899 && any_deductions
23900 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23901 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23902 {
23903 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23904 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23905 int len1 = TREE_VEC_LENGTH (args1);
23906 int len2 = TREE_VEC_LENGTH (args2);
23907
23908 /* We don't count the pack expansion at the end. */
23909 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23910 --len1;
23911 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23912 --len2;
23913
23914 if (len1 > len2)
23915 return 1;
23916 else if (len1 < len2)
23917 return -1;
23918 }
23919
23920 return winner;
23921 }
23922
23923 /* Return the template arguments that will produce the function signature
23924 DECL from the function template FN, with the explicit template
23925 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23926 also match. Return NULL_TREE if no satisfactory arguments could be
23927 found. */
23928
23929 static tree
23930 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23931 {
23932 int ntparms = DECL_NTPARMS (fn);
23933 tree targs = make_tree_vec (ntparms);
23934 tree decl_type = TREE_TYPE (decl);
23935 tree decl_arg_types;
23936 tree *args;
23937 unsigned int nargs, ix;
23938 tree arg;
23939
23940 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23941
23942 /* Never do unification on the 'this' parameter. */
23943 decl_arg_types = skip_artificial_parms_for (decl,
23944 TYPE_ARG_TYPES (decl_type));
23945
23946 nargs = list_length (decl_arg_types);
23947 args = XALLOCAVEC (tree, nargs);
23948 for (arg = decl_arg_types, ix = 0;
23949 arg != NULL_TREE && arg != void_list_node;
23950 arg = TREE_CHAIN (arg), ++ix)
23951 args[ix] = TREE_VALUE (arg);
23952
23953 if (fn_type_unification (fn, explicit_args, targs,
23954 args, ix,
23955 (check_rettype || DECL_CONV_FN_P (fn)
23956 ? TREE_TYPE (decl_type) : NULL_TREE),
23957 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23958 /*explain_p=*/false,
23959 /*decltype*/false)
23960 == error_mark_node)
23961 return NULL_TREE;
23962
23963 return targs;
23964 }
23965
23966 /* Return the innermost template arguments that, when applied to a partial
23967 specialization SPEC_TMPL of TMPL, yield the ARGS.
23968
23969 For example, suppose we have:
23970
23971 template <class T, class U> struct S {};
23972 template <class T> struct S<T*, int> {};
23973
23974 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23975 partial specialization and the ARGS will be {double*, int}. The resulting
23976 vector will be {double}, indicating that `T' is bound to `double'. */
23977
23978 static tree
23979 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23980 {
23981 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23982 tree spec_args
23983 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23984 int i, ntparms = TREE_VEC_LENGTH (tparms);
23985 tree deduced_args;
23986 tree innermost_deduced_args;
23987
23988 innermost_deduced_args = make_tree_vec (ntparms);
23989 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23990 {
23991 deduced_args = copy_node (args);
23992 SET_TMPL_ARGS_LEVEL (deduced_args,
23993 TMPL_ARGS_DEPTH (deduced_args),
23994 innermost_deduced_args);
23995 }
23996 else
23997 deduced_args = innermost_deduced_args;
23998
23999 bool tried_array_deduction = (cxx_dialect < cxx17);
24000 again:
24001 if (unify (tparms, deduced_args,
24002 INNERMOST_TEMPLATE_ARGS (spec_args),
24003 INNERMOST_TEMPLATE_ARGS (args),
24004 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24005 return NULL_TREE;
24006
24007 for (i = 0; i < ntparms; ++i)
24008 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24009 {
24010 if (!tried_array_deduction)
24011 {
24012 try_array_deduction (tparms, innermost_deduced_args,
24013 INNERMOST_TEMPLATE_ARGS (spec_args));
24014 tried_array_deduction = true;
24015 if (TREE_VEC_ELT (innermost_deduced_args, i))
24016 goto again;
24017 }
24018 return NULL_TREE;
24019 }
24020
24021 if (!push_tinst_level (spec_tmpl, deduced_args))
24022 {
24023 excessive_deduction_depth = true;
24024 return NULL_TREE;
24025 }
24026
24027 /* Verify that nondeduced template arguments agree with the type
24028 obtained from argument deduction.
24029
24030 For example:
24031
24032 struct A { typedef int X; };
24033 template <class T, class U> struct C {};
24034 template <class T> struct C<T, typename T::X> {};
24035
24036 Then with the instantiation `C<A, int>', we can deduce that
24037 `T' is `A' but unify () does not check whether `typename T::X'
24038 is `int'. */
24039 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24040
24041 if (spec_args != error_mark_node)
24042 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24043 INNERMOST_TEMPLATE_ARGS (spec_args),
24044 tmpl, tf_none, false, false);
24045
24046 pop_tinst_level ();
24047
24048 if (spec_args == error_mark_node
24049 /* We only need to check the innermost arguments; the other
24050 arguments will always agree. */
24051 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24052 INNERMOST_TEMPLATE_ARGS (args)))
24053 return NULL_TREE;
24054
24055 /* Now that we have bindings for all of the template arguments,
24056 ensure that the arguments deduced for the template template
24057 parameters have compatible template parameter lists. See the use
24058 of template_template_parm_bindings_ok_p in fn_type_unification
24059 for more information. */
24060 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24061 return NULL_TREE;
24062
24063 return deduced_args;
24064 }
24065
24066 // Compare two function templates T1 and T2 by deducing bindings
24067 // from one against the other. If both deductions succeed, compare
24068 // constraints to see which is more constrained.
24069 static int
24070 more_specialized_inst (tree t1, tree t2)
24071 {
24072 int fate = 0;
24073 int count = 0;
24074
24075 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24076 {
24077 --fate;
24078 ++count;
24079 }
24080
24081 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24082 {
24083 ++fate;
24084 ++count;
24085 }
24086
24087 // If both deductions succeed, then one may be more constrained.
24088 if (count == 2 && fate == 0)
24089 fate = more_constrained (t1, t2);
24090
24091 return fate;
24092 }
24093
24094 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24095 Return the TREE_LIST node with the most specialized template, if
24096 any. If there is no most specialized template, the error_mark_node
24097 is returned.
24098
24099 Note that this function does not look at, or modify, the
24100 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24101 returned is one of the elements of INSTANTIATIONS, callers may
24102 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24103 and retrieve it from the value returned. */
24104
24105 tree
24106 most_specialized_instantiation (tree templates)
24107 {
24108 tree fn, champ;
24109
24110 ++processing_template_decl;
24111
24112 champ = templates;
24113 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24114 {
24115 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24116 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24117 if (fate == -1)
24118 champ = fn;
24119 else if (!fate)
24120 {
24121 /* Equally specialized, move to next function. If there
24122 is no next function, nothing's most specialized. */
24123 fn = TREE_CHAIN (fn);
24124 champ = fn;
24125 if (!fn)
24126 break;
24127 }
24128 }
24129
24130 if (champ)
24131 /* Now verify that champ is better than everything earlier in the
24132 instantiation list. */
24133 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24134 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24135 {
24136 champ = NULL_TREE;
24137 break;
24138 }
24139 }
24140
24141 processing_template_decl--;
24142
24143 if (!champ)
24144 return error_mark_node;
24145
24146 return champ;
24147 }
24148
24149 /* If DECL is a specialization of some template, return the most
24150 general such template. Otherwise, returns NULL_TREE.
24151
24152 For example, given:
24153
24154 template <class T> struct S { template <class U> void f(U); };
24155
24156 if TMPL is `template <class U> void S<int>::f(U)' this will return
24157 the full template. This function will not trace past partial
24158 specializations, however. For example, given in addition:
24159
24160 template <class T> struct S<T*> { template <class U> void f(U); };
24161
24162 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24163 `template <class T> template <class U> S<T*>::f(U)'. */
24164
24165 tree
24166 most_general_template (tree decl)
24167 {
24168 if (TREE_CODE (decl) != TEMPLATE_DECL)
24169 {
24170 if (tree tinfo = get_template_info (decl))
24171 decl = TI_TEMPLATE (tinfo);
24172 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24173 template friend, or a FIELD_DECL for a capture pack. */
24174 if (TREE_CODE (decl) != TEMPLATE_DECL)
24175 return NULL_TREE;
24176 }
24177
24178 /* Look for more and more general templates. */
24179 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24180 {
24181 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24182 (See cp-tree.h for details.) */
24183 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24184 break;
24185
24186 if (CLASS_TYPE_P (TREE_TYPE (decl))
24187 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24188 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24189 break;
24190
24191 /* Stop if we run into an explicitly specialized class template. */
24192 if (!DECL_NAMESPACE_SCOPE_P (decl)
24193 && DECL_CONTEXT (decl)
24194 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24195 break;
24196
24197 decl = DECL_TI_TEMPLATE (decl);
24198 }
24199
24200 return decl;
24201 }
24202
24203 /* Return the most specialized of the template partial specializations
24204 which can produce TARGET, a specialization of some class or variable
24205 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24206 a TEMPLATE_DECL node corresponding to the partial specialization, while
24207 the TREE_PURPOSE is the set of template arguments that must be
24208 substituted into the template pattern in order to generate TARGET.
24209
24210 If the choice of partial specialization is ambiguous, a diagnostic
24211 is issued, and the error_mark_node is returned. If there are no
24212 partial specializations matching TARGET, then NULL_TREE is
24213 returned, indicating that the primary template should be used. */
24214
24215 static tree
24216 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24217 {
24218 tree list = NULL_TREE;
24219 tree t;
24220 tree champ;
24221 int fate;
24222 bool ambiguous_p;
24223 tree outer_args = NULL_TREE;
24224 tree tmpl, args;
24225
24226 if (TYPE_P (target))
24227 {
24228 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24229 tmpl = TI_TEMPLATE (tinfo);
24230 args = TI_ARGS (tinfo);
24231 }
24232 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24233 {
24234 tmpl = TREE_OPERAND (target, 0);
24235 args = TREE_OPERAND (target, 1);
24236 }
24237 else if (VAR_P (target))
24238 {
24239 tree tinfo = DECL_TEMPLATE_INFO (target);
24240 tmpl = TI_TEMPLATE (tinfo);
24241 args = TI_ARGS (tinfo);
24242 }
24243 else
24244 gcc_unreachable ();
24245
24246 tree main_tmpl = most_general_template (tmpl);
24247
24248 /* For determining which partial specialization to use, only the
24249 innermost args are interesting. */
24250 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24251 {
24252 outer_args = strip_innermost_template_args (args, 1);
24253 args = INNERMOST_TEMPLATE_ARGS (args);
24254 }
24255
24256 /* The caller hasn't called push_to_top_level yet, but we need
24257 get_partial_spec_bindings to be done in non-template context so that we'll
24258 fully resolve everything. */
24259 processing_template_decl_sentinel ptds;
24260
24261 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24262 {
24263 tree spec_args;
24264 tree spec_tmpl = TREE_VALUE (t);
24265
24266 if (outer_args)
24267 {
24268 /* Substitute in the template args from the enclosing class. */
24269 ++processing_template_decl;
24270 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
24271 --processing_template_decl;
24272 }
24273
24274 if (spec_tmpl == error_mark_node)
24275 return error_mark_node;
24276
24277 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24278 if (spec_args)
24279 {
24280 if (outer_args)
24281 spec_args = add_to_template_args (outer_args, spec_args);
24282
24283 /* Keep the candidate only if the constraints are satisfied,
24284 or if we're not compiling with concepts. */
24285 if (!flag_concepts
24286 || constraints_satisfied_p (spec_tmpl, spec_args))
24287 {
24288 list = tree_cons (spec_args, TREE_VALUE (t), list);
24289 TREE_TYPE (list) = TREE_TYPE (t);
24290 }
24291 }
24292 }
24293
24294 if (! list)
24295 return NULL_TREE;
24296
24297 ambiguous_p = false;
24298 t = list;
24299 champ = t;
24300 t = TREE_CHAIN (t);
24301 for (; t; t = TREE_CHAIN (t))
24302 {
24303 fate = more_specialized_partial_spec (tmpl, champ, t);
24304 if (fate == 1)
24305 ;
24306 else
24307 {
24308 if (fate == 0)
24309 {
24310 t = TREE_CHAIN (t);
24311 if (! t)
24312 {
24313 ambiguous_p = true;
24314 break;
24315 }
24316 }
24317 champ = t;
24318 }
24319 }
24320
24321 if (!ambiguous_p)
24322 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24323 {
24324 fate = more_specialized_partial_spec (tmpl, champ, t);
24325 if (fate != 1)
24326 {
24327 ambiguous_p = true;
24328 break;
24329 }
24330 }
24331
24332 if (ambiguous_p)
24333 {
24334 const char *str;
24335 char *spaces = NULL;
24336 if (!(complain & tf_error))
24337 return error_mark_node;
24338 if (TYPE_P (target))
24339 error ("ambiguous template instantiation for %q#T", target);
24340 else
24341 error ("ambiguous template instantiation for %q#D", target);
24342 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24343 for (t = list; t; t = TREE_CHAIN (t))
24344 {
24345 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24346 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24347 "%s %#qS", spaces ? spaces : str, subst);
24348 spaces = spaces ? spaces : get_spaces (str);
24349 }
24350 free (spaces);
24351 return error_mark_node;
24352 }
24353
24354 return champ;
24355 }
24356
24357 /* Explicitly instantiate DECL. */
24358
24359 void
24360 do_decl_instantiation (tree decl, tree storage)
24361 {
24362 tree result = NULL_TREE;
24363 int extern_p = 0;
24364
24365 if (!decl || decl == error_mark_node)
24366 /* An error occurred, for which grokdeclarator has already issued
24367 an appropriate message. */
24368 return;
24369 else if (! DECL_LANG_SPECIFIC (decl))
24370 {
24371 error ("explicit instantiation of non-template %q#D", decl);
24372 return;
24373 }
24374 else if (DECL_DECLARED_CONCEPT_P (decl))
24375 {
24376 if (VAR_P (decl))
24377 error ("explicit instantiation of variable concept %q#D", decl);
24378 else
24379 error ("explicit instantiation of function concept %q#D", decl);
24380 return;
24381 }
24382
24383 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24384 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24385
24386 if (VAR_P (decl) && !var_templ)
24387 {
24388 /* There is an asymmetry here in the way VAR_DECLs and
24389 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24390 the latter, the DECL we get back will be marked as a
24391 template instantiation, and the appropriate
24392 DECL_TEMPLATE_INFO will be set up. This does not happen for
24393 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24394 should handle VAR_DECLs as it currently handles
24395 FUNCTION_DECLs. */
24396 if (!DECL_CLASS_SCOPE_P (decl))
24397 {
24398 error ("%qD is not a static data member of a class template", decl);
24399 return;
24400 }
24401 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24402 if (!result || !VAR_P (result))
24403 {
24404 error ("no matching template for %qD found", decl);
24405 return;
24406 }
24407 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24408 {
24409 error ("type %qT for explicit instantiation %qD does not match "
24410 "declared type %qT", TREE_TYPE (result), decl,
24411 TREE_TYPE (decl));
24412 return;
24413 }
24414 }
24415 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24416 {
24417 error ("explicit instantiation of %q#D", decl);
24418 return;
24419 }
24420 else
24421 result = decl;
24422
24423 /* Check for various error cases. Note that if the explicit
24424 instantiation is valid the RESULT will currently be marked as an
24425 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24426 until we get here. */
24427
24428 if (DECL_TEMPLATE_SPECIALIZATION (result))
24429 {
24430 /* DR 259 [temp.spec].
24431
24432 Both an explicit instantiation and a declaration of an explicit
24433 specialization shall not appear in a program unless the explicit
24434 instantiation follows a declaration of the explicit specialization.
24435
24436 For a given set of template parameters, if an explicit
24437 instantiation of a template appears after a declaration of an
24438 explicit specialization for that template, the explicit
24439 instantiation has no effect. */
24440 return;
24441 }
24442 else if (DECL_EXPLICIT_INSTANTIATION (result))
24443 {
24444 /* [temp.spec]
24445
24446 No program shall explicitly instantiate any template more
24447 than once.
24448
24449 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24450 the first instantiation was `extern' and the second is not,
24451 and EXTERN_P for the opposite case. */
24452 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24453 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24454 /* If an "extern" explicit instantiation follows an ordinary
24455 explicit instantiation, the template is instantiated. */
24456 if (extern_p)
24457 return;
24458 }
24459 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24460 {
24461 error ("no matching template for %qD found", result);
24462 return;
24463 }
24464 else if (!DECL_TEMPLATE_INFO (result))
24465 {
24466 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24467 return;
24468 }
24469
24470 if (storage == NULL_TREE)
24471 ;
24472 else if (storage == ridpointers[(int) RID_EXTERN])
24473 {
24474 if (cxx_dialect == cxx98)
24475 pedwarn (input_location, OPT_Wpedantic,
24476 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24477 "instantiations");
24478 extern_p = 1;
24479 }
24480 else
24481 error ("storage class %qD applied to template instantiation", storage);
24482
24483 check_explicit_instantiation_namespace (result);
24484 mark_decl_instantiated (result, extern_p);
24485 if (! extern_p)
24486 instantiate_decl (result, /*defer_ok=*/true,
24487 /*expl_inst_class_mem_p=*/false);
24488 }
24489
24490 static void
24491 mark_class_instantiated (tree t, int extern_p)
24492 {
24493 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24494 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24495 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24496 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24497 if (! extern_p)
24498 {
24499 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24500 rest_of_type_compilation (t, 1);
24501 }
24502 }
24503
24504 /* Called from do_type_instantiation through binding_table_foreach to
24505 do recursive instantiation for the type bound in ENTRY. */
24506 static void
24507 bt_instantiate_type_proc (binding_entry entry, void *data)
24508 {
24509 tree storage = *(tree *) data;
24510
24511 if (MAYBE_CLASS_TYPE_P (entry->type)
24512 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24513 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24514 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24515 }
24516
24517 /* Perform an explicit instantiation of template class T. STORAGE, if
24518 non-null, is the RID for extern, inline or static. COMPLAIN is
24519 nonzero if this is called from the parser, zero if called recursively,
24520 since the standard is unclear (as detailed below). */
24521
24522 void
24523 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24524 {
24525 int extern_p = 0;
24526 int nomem_p = 0;
24527 int static_p = 0;
24528 int previous_instantiation_extern_p = 0;
24529
24530 if (TREE_CODE (t) == TYPE_DECL)
24531 t = TREE_TYPE (t);
24532
24533 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24534 {
24535 tree tmpl =
24536 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24537 if (tmpl)
24538 error ("explicit instantiation of non-class template %qD", tmpl);
24539 else
24540 error ("explicit instantiation of non-template type %qT", t);
24541 return;
24542 }
24543
24544 complete_type (t);
24545
24546 if (!COMPLETE_TYPE_P (t))
24547 {
24548 if (complain & tf_error)
24549 error ("explicit instantiation of %q#T before definition of template",
24550 t);
24551 return;
24552 }
24553
24554 if (storage != NULL_TREE)
24555 {
24556 if (storage == ridpointers[(int) RID_EXTERN])
24557 {
24558 if (cxx_dialect == cxx98)
24559 pedwarn (input_location, OPT_Wpedantic,
24560 "ISO C++ 1998 forbids the use of %<extern%> on "
24561 "explicit instantiations");
24562 }
24563 else
24564 pedwarn (input_location, OPT_Wpedantic,
24565 "ISO C++ forbids the use of %qE"
24566 " on explicit instantiations", storage);
24567
24568 if (storage == ridpointers[(int) RID_INLINE])
24569 nomem_p = 1;
24570 else if (storage == ridpointers[(int) RID_EXTERN])
24571 extern_p = 1;
24572 else if (storage == ridpointers[(int) RID_STATIC])
24573 static_p = 1;
24574 else
24575 {
24576 error ("storage class %qD applied to template instantiation",
24577 storage);
24578 extern_p = 0;
24579 }
24580 }
24581
24582 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24583 {
24584 /* DR 259 [temp.spec].
24585
24586 Both an explicit instantiation and a declaration of an explicit
24587 specialization shall not appear in a program unless the explicit
24588 instantiation follows a declaration of the explicit specialization.
24589
24590 For a given set of template parameters, if an explicit
24591 instantiation of a template appears after a declaration of an
24592 explicit specialization for that template, the explicit
24593 instantiation has no effect. */
24594 return;
24595 }
24596 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24597 {
24598 /* [temp.spec]
24599
24600 No program shall explicitly instantiate any template more
24601 than once.
24602
24603 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24604 instantiation was `extern'. If EXTERN_P then the second is.
24605 These cases are OK. */
24606 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
24607
24608 if (!previous_instantiation_extern_p && !extern_p
24609 && (complain & tf_error))
24610 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24611
24612 /* If we've already instantiated the template, just return now. */
24613 if (!CLASSTYPE_INTERFACE_ONLY (t))
24614 return;
24615 }
24616
24617 check_explicit_instantiation_namespace (TYPE_NAME (t));
24618 mark_class_instantiated (t, extern_p);
24619
24620 if (nomem_p)
24621 return;
24622
24623 /* In contrast to implicit instantiation, where only the
24624 declarations, and not the definitions, of members are
24625 instantiated, we have here:
24626
24627 [temp.explicit]
24628
24629 The explicit instantiation of a class template specialization
24630 implies the instantiation of all of its members not
24631 previously explicitly specialized in the translation unit
24632 containing the explicit instantiation.
24633
24634 Of course, we can't instantiate member template classes, since we
24635 don't have any arguments for them. Note that the standard is
24636 unclear on whether the instantiation of the members are
24637 *explicit* instantiations or not. However, the most natural
24638 interpretation is that it should be an explicit
24639 instantiation. */
24640 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24641 if ((VAR_P (fld)
24642 || (TREE_CODE (fld) == FUNCTION_DECL
24643 && !static_p
24644 && user_provided_p (fld)))
24645 && DECL_TEMPLATE_INSTANTIATION (fld))
24646 {
24647 mark_decl_instantiated (fld, extern_p);
24648 if (! extern_p)
24649 instantiate_decl (fld, /*defer_ok=*/true,
24650 /*expl_inst_class_mem_p=*/true);
24651 }
24652
24653 if (CLASSTYPE_NESTED_UTDS (t))
24654 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24655 bt_instantiate_type_proc, &storage);
24656 }
24657
24658 /* Given a function DECL, which is a specialization of TMPL, modify
24659 DECL to be a re-instantiation of TMPL with the same template
24660 arguments. TMPL should be the template into which tsubst'ing
24661 should occur for DECL, not the most general template.
24662
24663 One reason for doing this is a scenario like this:
24664
24665 template <class T>
24666 void f(const T&, int i);
24667
24668 void g() { f(3, 7); }
24669
24670 template <class T>
24671 void f(const T& t, const int i) { }
24672
24673 Note that when the template is first instantiated, with
24674 instantiate_template, the resulting DECL will have no name for the
24675 first parameter, and the wrong type for the second. So, when we go
24676 to instantiate the DECL, we regenerate it. */
24677
24678 static void
24679 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24680 {
24681 /* The arguments used to instantiate DECL, from the most general
24682 template. */
24683 tree code_pattern;
24684
24685 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24686
24687 /* Make sure that we can see identifiers, and compute access
24688 correctly. */
24689 push_access_scope (decl);
24690
24691 if (TREE_CODE (decl) == FUNCTION_DECL)
24692 {
24693 tree decl_parm;
24694 tree pattern_parm;
24695 tree specs;
24696 int args_depth;
24697 int parms_depth;
24698
24699 args_depth = TMPL_ARGS_DEPTH (args);
24700 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24701 if (args_depth > parms_depth)
24702 args = get_innermost_template_args (args, parms_depth);
24703
24704 /* Instantiate a dynamic exception-specification. noexcept will be
24705 handled below. */
24706 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24707 if (TREE_VALUE (raises))
24708 {
24709 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24710 args, tf_error, NULL_TREE,
24711 /*defer_ok*/false);
24712 if (specs && specs != error_mark_node)
24713 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24714 specs);
24715 }
24716
24717 /* Merge parameter declarations. */
24718 decl_parm = skip_artificial_parms_for (decl,
24719 DECL_ARGUMENTS (decl));
24720 pattern_parm
24721 = skip_artificial_parms_for (code_pattern,
24722 DECL_ARGUMENTS (code_pattern));
24723 while (decl_parm && !DECL_PACK_P (pattern_parm))
24724 {
24725 tree parm_type;
24726 tree attributes;
24727
24728 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24729 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24730 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24731 NULL_TREE);
24732 parm_type = type_decays_to (parm_type);
24733 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24734 TREE_TYPE (decl_parm) = parm_type;
24735 attributes = DECL_ATTRIBUTES (pattern_parm);
24736 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24737 {
24738 DECL_ATTRIBUTES (decl_parm) = attributes;
24739 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24740 }
24741 decl_parm = DECL_CHAIN (decl_parm);
24742 pattern_parm = DECL_CHAIN (pattern_parm);
24743 }
24744 /* Merge any parameters that match with the function parameter
24745 pack. */
24746 if (pattern_parm && DECL_PACK_P (pattern_parm))
24747 {
24748 int i, len;
24749 tree expanded_types;
24750 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24751 the parameters in this function parameter pack. */
24752 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24753 args, tf_error, NULL_TREE);
24754 len = TREE_VEC_LENGTH (expanded_types);
24755 for (i = 0; i < len; i++)
24756 {
24757 tree parm_type;
24758 tree attributes;
24759
24760 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24761 /* Rename the parameter to include the index. */
24762 DECL_NAME (decl_parm) =
24763 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24764 parm_type = TREE_VEC_ELT (expanded_types, i);
24765 parm_type = type_decays_to (parm_type);
24766 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24767 TREE_TYPE (decl_parm) = parm_type;
24768 attributes = DECL_ATTRIBUTES (pattern_parm);
24769 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24770 {
24771 DECL_ATTRIBUTES (decl_parm) = attributes;
24772 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24773 }
24774 decl_parm = DECL_CHAIN (decl_parm);
24775 }
24776 }
24777 /* Merge additional specifiers from the CODE_PATTERN. */
24778 if (DECL_DECLARED_INLINE_P (code_pattern)
24779 && !DECL_DECLARED_INLINE_P (decl))
24780 DECL_DECLARED_INLINE_P (decl) = 1;
24781
24782 maybe_instantiate_noexcept (decl, tf_error);
24783 }
24784 else if (VAR_P (decl))
24785 {
24786 start_lambda_scope (decl);
24787 DECL_INITIAL (decl) =
24788 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24789 tf_error, DECL_TI_TEMPLATE (decl));
24790 finish_lambda_scope ();
24791 if (VAR_HAD_UNKNOWN_BOUND (decl))
24792 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24793 tf_error, DECL_TI_TEMPLATE (decl));
24794 }
24795 else
24796 gcc_unreachable ();
24797
24798 pop_access_scope (decl);
24799 }
24800
24801 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24802 substituted to get DECL. */
24803
24804 tree
24805 template_for_substitution (tree decl)
24806 {
24807 tree tmpl = DECL_TI_TEMPLATE (decl);
24808
24809 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24810 for the instantiation. This is not always the most general
24811 template. Consider, for example:
24812
24813 template <class T>
24814 struct S { template <class U> void f();
24815 template <> void f<int>(); };
24816
24817 and an instantiation of S<double>::f<int>. We want TD to be the
24818 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24819 while (/* An instantiation cannot have a definition, so we need a
24820 more general template. */
24821 DECL_TEMPLATE_INSTANTIATION (tmpl)
24822 /* We must also deal with friend templates. Given:
24823
24824 template <class T> struct S {
24825 template <class U> friend void f() {};
24826 };
24827
24828 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24829 so far as the language is concerned, but that's still
24830 where we get the pattern for the instantiation from. On
24831 other hand, if the definition comes outside the class, say:
24832
24833 template <class T> struct S {
24834 template <class U> friend void f();
24835 };
24836 template <class U> friend void f() {}
24837
24838 we don't need to look any further. That's what the check for
24839 DECL_INITIAL is for. */
24840 || (TREE_CODE (decl) == FUNCTION_DECL
24841 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24842 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24843 {
24844 /* The present template, TD, should not be a definition. If it
24845 were a definition, we should be using it! Note that we
24846 cannot restructure the loop to just keep going until we find
24847 a template with a definition, since that might go too far if
24848 a specialization was declared, but not defined. */
24849
24850 /* Fetch the more general template. */
24851 tmpl = DECL_TI_TEMPLATE (tmpl);
24852 }
24853
24854 return tmpl;
24855 }
24856
24857 /* Returns true if we need to instantiate this template instance even if we
24858 know we aren't going to emit it. */
24859
24860 bool
24861 always_instantiate_p (tree decl)
24862 {
24863 /* We always instantiate inline functions so that we can inline them. An
24864 explicit instantiation declaration prohibits implicit instantiation of
24865 non-inline functions. With high levels of optimization, we would
24866 normally inline non-inline functions -- but we're not allowed to do
24867 that for "extern template" functions. Therefore, we check
24868 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24869 return ((TREE_CODE (decl) == FUNCTION_DECL
24870 && (DECL_DECLARED_INLINE_P (decl)
24871 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24872 /* And we need to instantiate static data members so that
24873 their initializers are available in integral constant
24874 expressions. */
24875 || (VAR_P (decl)
24876 && decl_maybe_constant_var_p (decl)));
24877 }
24878
24879 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24880 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24881 error, true otherwise. */
24882
24883 bool
24884 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24885 {
24886 tree fntype, spec, noex, clone;
24887
24888 /* Don't instantiate a noexcept-specification from template context. */
24889 if (processing_template_decl
24890 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24891 return true;
24892
24893 if (DECL_CLONED_FUNCTION_P (fn))
24894 fn = DECL_CLONED_FUNCTION (fn);
24895
24896 tree orig_fn = NULL_TREE;
24897 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24898 its FUNCTION_DECL for the rest of this function -- push_access_scope
24899 doesn't accept TEMPLATE_DECLs. */
24900 if (DECL_FUNCTION_TEMPLATE_P (fn))
24901 {
24902 orig_fn = fn;
24903 fn = DECL_TEMPLATE_RESULT (fn);
24904 }
24905
24906 fntype = TREE_TYPE (fn);
24907 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24908
24909 if (!spec || !TREE_PURPOSE (spec))
24910 return true;
24911
24912 noex = TREE_PURPOSE (spec);
24913
24914 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24915 {
24916 static hash_set<tree>* fns = new hash_set<tree>;
24917 bool added = false;
24918 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24919 {
24920 spec = get_defaulted_eh_spec (fn, complain);
24921 if (spec == error_mark_node)
24922 /* This might have failed because of an unparsed DMI, so
24923 let's try again later. */
24924 return false;
24925 }
24926 else if (!(added = !fns->add (fn)))
24927 {
24928 /* If hash_set::add returns true, the element was already there. */
24929 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24930 DECL_SOURCE_LOCATION (fn));
24931 error_at (loc,
24932 "exception specification of %qD depends on itself",
24933 fn);
24934 spec = noexcept_false_spec;
24935 }
24936 else if (push_tinst_level (fn))
24937 {
24938 push_to_top_level ();
24939 push_access_scope (fn);
24940 push_deferring_access_checks (dk_no_deferred);
24941 input_location = DECL_SOURCE_LOCATION (fn);
24942
24943 /* If needed, set current_class_ptr for the benefit of
24944 tsubst_copy/PARM_DECL. */
24945 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24946 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24947 {
24948 tree this_parm = DECL_ARGUMENTS (tdecl);
24949 current_class_ptr = NULL_TREE;
24950 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24951 current_class_ptr = this_parm;
24952 }
24953
24954 /* If this function is represented by a TEMPLATE_DECL, then
24955 the deferred noexcept-specification might still contain
24956 dependent types, even after substitution. And we need the
24957 dependency check functions to work in build_noexcept_spec. */
24958 if (orig_fn)
24959 ++processing_template_decl;
24960
24961 /* Do deferred instantiation of the noexcept-specifier. */
24962 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24963 DEFERRED_NOEXCEPT_ARGS (noex),
24964 tf_warning_or_error, fn,
24965 /*function_p=*/false,
24966 /*i_c_e_p=*/true);
24967
24968 /* Build up the noexcept-specification. */
24969 spec = build_noexcept_spec (noex, tf_warning_or_error);
24970
24971 if (orig_fn)
24972 --processing_template_decl;
24973
24974 pop_deferring_access_checks ();
24975 pop_access_scope (fn);
24976 pop_tinst_level ();
24977 pop_from_top_level ();
24978 }
24979 else
24980 spec = noexcept_false_spec;
24981
24982 if (added)
24983 fns->remove (fn);
24984
24985 if (spec == error_mark_node)
24986 {
24987 /* This failed with a hard error, so let's go with false. */
24988 gcc_assert (seen_error ());
24989 spec = noexcept_false_spec;
24990 }
24991
24992 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24993 if (orig_fn)
24994 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24995
24996 FOR_EACH_CLONE (clone, fn)
24997 {
24998 if (TREE_TYPE (clone) == fntype)
24999 TREE_TYPE (clone) = TREE_TYPE (fn);
25000 else
25001 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
25002 }
25003 }
25004
25005 return true;
25006 }
25007
25008 /* We're starting to process the function INST, an instantiation of PATTERN;
25009 add their parameters to local_specializations. */
25010
25011 static void
25012 register_parameter_specializations (tree pattern, tree inst)
25013 {
25014 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25015 tree spec_parm = DECL_ARGUMENTS (inst);
25016 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25017 {
25018 register_local_specialization (spec_parm, tmpl_parm);
25019 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25020 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25021 }
25022 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25023 {
25024 if (!DECL_PACK_P (tmpl_parm))
25025 {
25026 register_local_specialization (spec_parm, tmpl_parm);
25027 spec_parm = DECL_CHAIN (spec_parm);
25028 }
25029 else
25030 {
25031 /* Register the (value) argument pack as a specialization of
25032 TMPL_PARM, then move on. */
25033 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25034 register_local_specialization (argpack, tmpl_parm);
25035 }
25036 }
25037 gcc_assert (!spec_parm);
25038 }
25039
25040 /* Produce the definition of D, a _DECL generated from a template. If
25041 DEFER_OK is true, then we don't have to actually do the
25042 instantiation now; we just have to do it sometime. Normally it is
25043 an error if this is an explicit instantiation but D is undefined.
25044 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25045 instantiated class template. */
25046
25047 tree
25048 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25049 {
25050 tree tmpl = DECL_TI_TEMPLATE (d);
25051 tree gen_args;
25052 tree args;
25053 tree td;
25054 tree code_pattern;
25055 tree spec;
25056 tree gen_tmpl;
25057 bool pattern_defined;
25058 location_t saved_loc = input_location;
25059 int saved_unevaluated_operand = cp_unevaluated_operand;
25060 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25061 bool external_p;
25062 bool deleted_p;
25063
25064 /* This function should only be used to instantiate templates for
25065 functions and static member variables. */
25066 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25067
25068 /* A concept is never instantiated. */
25069 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25070
25071 /* Variables are never deferred; if instantiation is required, they
25072 are instantiated right away. That allows for better code in the
25073 case that an expression refers to the value of the variable --
25074 if the variable has a constant value the referring expression can
25075 take advantage of that fact. */
25076 if (VAR_P (d))
25077 defer_ok = false;
25078
25079 /* Don't instantiate cloned functions. Instead, instantiate the
25080 functions they cloned. */
25081 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25082 d = DECL_CLONED_FUNCTION (d);
25083
25084 if (DECL_TEMPLATE_INSTANTIATED (d)
25085 || (TREE_CODE (d) == FUNCTION_DECL
25086 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25087 || DECL_TEMPLATE_SPECIALIZATION (d))
25088 /* D has already been instantiated or explicitly specialized, so
25089 there's nothing for us to do here.
25090
25091 It might seem reasonable to check whether or not D is an explicit
25092 instantiation, and, if so, stop here. But when an explicit
25093 instantiation is deferred until the end of the compilation,
25094 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25095 the instantiation. */
25096 return d;
25097
25098 /* Check to see whether we know that this template will be
25099 instantiated in some other file, as with "extern template"
25100 extension. */
25101 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25102
25103 /* In general, we do not instantiate such templates. */
25104 if (external_p && !always_instantiate_p (d))
25105 return d;
25106
25107 gen_tmpl = most_general_template (tmpl);
25108 gen_args = DECL_TI_ARGS (d);
25109
25110 if (tmpl != gen_tmpl)
25111 /* We should already have the extra args. */
25112 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25113 == TMPL_ARGS_DEPTH (gen_args));
25114 /* And what's in the hash table should match D. */
25115 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
25116 || spec == NULL_TREE);
25117
25118 /* This needs to happen before any tsubsting. */
25119 if (! push_tinst_level (d))
25120 return d;
25121
25122 timevar_push (TV_TEMPLATE_INST);
25123
25124 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25125 for the instantiation. */
25126 td = template_for_substitution (d);
25127 args = gen_args;
25128
25129 if (VAR_P (d))
25130 {
25131 /* Look up an explicit specialization, if any. */
25132 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25133 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25134 if (elt && elt != error_mark_node)
25135 {
25136 td = TREE_VALUE (elt);
25137 args = TREE_PURPOSE (elt);
25138 }
25139 }
25140
25141 code_pattern = DECL_TEMPLATE_RESULT (td);
25142
25143 /* We should never be trying to instantiate a member of a class
25144 template or partial specialization. */
25145 gcc_assert (d != code_pattern);
25146
25147 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25148 || DECL_TEMPLATE_SPECIALIZATION (td))
25149 /* In the case of a friend template whose definition is provided
25150 outside the class, we may have too many arguments. Drop the
25151 ones we don't need. The same is true for specializations. */
25152 args = get_innermost_template_args
25153 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25154
25155 if (TREE_CODE (d) == FUNCTION_DECL)
25156 {
25157 deleted_p = DECL_DELETED_FN (code_pattern);
25158 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25159 && DECL_INITIAL (code_pattern) != error_mark_node)
25160 || DECL_DEFAULTED_FN (code_pattern)
25161 || deleted_p);
25162 }
25163 else
25164 {
25165 deleted_p = false;
25166 if (DECL_CLASS_SCOPE_P (code_pattern))
25167 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25168 else
25169 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25170 }
25171
25172 /* We may be in the middle of deferred access check. Disable it now. */
25173 push_deferring_access_checks (dk_no_deferred);
25174
25175 /* Unless an explicit instantiation directive has already determined
25176 the linkage of D, remember that a definition is available for
25177 this entity. */
25178 if (pattern_defined
25179 && !DECL_INTERFACE_KNOWN (d)
25180 && !DECL_NOT_REALLY_EXTERN (d))
25181 mark_definable (d);
25182
25183 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25184 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25185 input_location = DECL_SOURCE_LOCATION (d);
25186
25187 /* If D is a member of an explicitly instantiated class template,
25188 and no definition is available, treat it like an implicit
25189 instantiation. */
25190 if (!pattern_defined && expl_inst_class_mem_p
25191 && DECL_EXPLICIT_INSTANTIATION (d))
25192 {
25193 /* Leave linkage flags alone on instantiations with anonymous
25194 visibility. */
25195 if (TREE_PUBLIC (d))
25196 {
25197 DECL_NOT_REALLY_EXTERN (d) = 0;
25198 DECL_INTERFACE_KNOWN (d) = 0;
25199 }
25200 SET_DECL_IMPLICIT_INSTANTIATION (d);
25201 }
25202
25203 /* Defer all other templates, unless we have been explicitly
25204 forbidden from doing so. */
25205 if (/* If there is no definition, we cannot instantiate the
25206 template. */
25207 ! pattern_defined
25208 /* If it's OK to postpone instantiation, do so. */
25209 || defer_ok
25210 /* If this is a static data member that will be defined
25211 elsewhere, we don't want to instantiate the entire data
25212 member, but we do want to instantiate the initializer so that
25213 we can substitute that elsewhere. */
25214 || (external_p && VAR_P (d))
25215 /* Handle here a deleted function too, avoid generating
25216 its body (c++/61080). */
25217 || deleted_p)
25218 {
25219 /* The definition of the static data member is now required so
25220 we must substitute the initializer. */
25221 if (VAR_P (d)
25222 && !DECL_INITIAL (d)
25223 && DECL_INITIAL (code_pattern))
25224 {
25225 tree ns;
25226 tree init;
25227 bool const_init = false;
25228 bool enter_context = DECL_CLASS_SCOPE_P (d);
25229
25230 ns = decl_namespace_context (d);
25231 push_nested_namespace (ns);
25232 if (enter_context)
25233 push_nested_class (DECL_CONTEXT (d));
25234 init = tsubst_expr (DECL_INITIAL (code_pattern),
25235 args,
25236 tf_warning_or_error, NULL_TREE,
25237 /*integral_constant_expression_p=*/false);
25238 /* If instantiating the initializer involved instantiating this
25239 again, don't call cp_finish_decl twice. */
25240 if (!DECL_INITIAL (d))
25241 {
25242 /* Make sure the initializer is still constant, in case of
25243 circular dependency (template/instantiate6.C). */
25244 const_init
25245 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25246 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25247 /*asmspec_tree=*/NULL_TREE,
25248 LOOKUP_ONLYCONVERTING);
25249 }
25250 if (enter_context)
25251 pop_nested_class ();
25252 pop_nested_namespace (ns);
25253 }
25254
25255 /* We restore the source position here because it's used by
25256 add_pending_template. */
25257 input_location = saved_loc;
25258
25259 if (at_eof && !pattern_defined
25260 && DECL_EXPLICIT_INSTANTIATION (d)
25261 && DECL_NOT_REALLY_EXTERN (d))
25262 /* [temp.explicit]
25263
25264 The definition of a non-exported function template, a
25265 non-exported member function template, or a non-exported
25266 member function or static data member of a class template
25267 shall be present in every translation unit in which it is
25268 explicitly instantiated. */
25269 permerror (input_location, "explicit instantiation of %qD "
25270 "but no definition available", d);
25271
25272 /* If we're in unevaluated context, we just wanted to get the
25273 constant value; this isn't an odr use, so don't queue
25274 a full instantiation. */
25275 if (cp_unevaluated_operand != 0)
25276 goto out;
25277 /* ??? Historically, we have instantiated inline functions, even
25278 when marked as "extern template". */
25279 if (!(external_p && VAR_P (d)))
25280 add_pending_template (d);
25281 goto out;
25282 }
25283
25284 bool push_to_top, nested;
25285 tree fn_context;
25286 fn_context = decl_function_context (d);
25287 if (LAMBDA_FUNCTION_P (d))
25288 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25289 fn_context = NULL_TREE;
25290 nested = current_function_decl != NULL_TREE;
25291 push_to_top = !(nested && fn_context == current_function_decl);
25292
25293 vec<tree> omp_privatization_save;
25294 if (nested)
25295 save_omp_privatization_clauses (omp_privatization_save);
25296
25297 if (push_to_top)
25298 push_to_top_level ();
25299 else
25300 {
25301 gcc_assert (!processing_template_decl);
25302 push_function_context ();
25303 cp_unevaluated_operand = 0;
25304 c_inhibit_evaluation_warnings = 0;
25305 }
25306
25307 /* Mark D as instantiated so that recursive calls to
25308 instantiate_decl do not try to instantiate it again. */
25309 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25310
25311 /* Regenerate the declaration in case the template has been modified
25312 by a subsequent redeclaration. */
25313 regenerate_decl_from_template (d, td, args);
25314
25315 /* We already set the file and line above. Reset them now in case
25316 they changed as a result of calling regenerate_decl_from_template. */
25317 input_location = DECL_SOURCE_LOCATION (d);
25318
25319 if (VAR_P (d))
25320 {
25321 tree init;
25322 bool const_init = false;
25323
25324 /* Clear out DECL_RTL; whatever was there before may not be right
25325 since we've reset the type of the declaration. */
25326 SET_DECL_RTL (d, NULL);
25327 DECL_IN_AGGR_P (d) = 0;
25328
25329 /* The initializer is placed in DECL_INITIAL by
25330 regenerate_decl_from_template so we don't need to
25331 push/pop_access_scope again here. Pull it out so that
25332 cp_finish_decl can process it. */
25333 init = DECL_INITIAL (d);
25334 DECL_INITIAL (d) = NULL_TREE;
25335 DECL_INITIALIZED_P (d) = 0;
25336
25337 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25338 initializer. That function will defer actual emission until
25339 we have a chance to determine linkage. */
25340 DECL_EXTERNAL (d) = 0;
25341
25342 /* Enter the scope of D so that access-checking works correctly. */
25343 bool enter_context = DECL_CLASS_SCOPE_P (d);
25344 if (enter_context)
25345 push_nested_class (DECL_CONTEXT (d));
25346
25347 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25348 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
25349 ? LOOKUP_CONSTINIT : 0);
25350 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
25351
25352 if (enter_context)
25353 pop_nested_class ();
25354
25355 if (variable_template_p (gen_tmpl))
25356 note_variable_template_instantiation (d);
25357 }
25358 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25359 synthesize_method (d);
25360 else if (TREE_CODE (d) == FUNCTION_DECL)
25361 {
25362 /* Set up the list of local specializations. */
25363 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25364 tree block = NULL_TREE;
25365
25366 /* Set up context. */
25367 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25368 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25369 block = push_stmt_list ();
25370 else
25371 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25372
25373 /* Some typedefs referenced from within the template code need to be
25374 access checked at template instantiation time, i.e now. These
25375 types were added to the template at parsing time. Let's get those
25376 and perform the access checks then. */
25377 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
25378 args);
25379
25380 /* Create substitution entries for the parameters. */
25381 register_parameter_specializations (code_pattern, d);
25382
25383 /* Substitute into the body of the function. */
25384 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25385 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25386 tf_warning_or_error, tmpl);
25387 else
25388 {
25389 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25390 tf_warning_or_error, tmpl,
25391 /*integral_constant_expression_p=*/false);
25392
25393 /* Set the current input_location to the end of the function
25394 so that finish_function knows where we are. */
25395 input_location
25396 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25397
25398 /* Remember if we saw an infinite loop in the template. */
25399 current_function_infinite_loop
25400 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25401 }
25402
25403 /* Finish the function. */
25404 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25405 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25406 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25407 else
25408 {
25409 d = finish_function (/*inline_p=*/false);
25410 expand_or_defer_fn (d);
25411 }
25412
25413 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25414 cp_check_omp_declare_reduction (d);
25415 }
25416
25417 /* We're not deferring instantiation any more. */
25418 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25419
25420 if (push_to_top)
25421 pop_from_top_level ();
25422 else
25423 pop_function_context ();
25424
25425 if (nested)
25426 restore_omp_privatization_clauses (omp_privatization_save);
25427
25428 out:
25429 pop_deferring_access_checks ();
25430 timevar_pop (TV_TEMPLATE_INST);
25431 pop_tinst_level ();
25432 input_location = saved_loc;
25433 cp_unevaluated_operand = saved_unevaluated_operand;
25434 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25435
25436 return d;
25437 }
25438
25439 /* Run through the list of templates that we wish we could
25440 instantiate, and instantiate any we can. RETRIES is the
25441 number of times we retry pending template instantiation. */
25442
25443 void
25444 instantiate_pending_templates (int retries)
25445 {
25446 int reconsider;
25447 location_t saved_loc = input_location;
25448
25449 /* Instantiating templates may trigger vtable generation. This in turn
25450 may require further template instantiations. We place a limit here
25451 to avoid infinite loop. */
25452 if (pending_templates && retries >= max_tinst_depth)
25453 {
25454 tree decl = pending_templates->tinst->maybe_get_node ();
25455
25456 fatal_error (input_location,
25457 "template instantiation depth exceeds maximum of %d"
25458 " instantiating %q+D, possibly from virtual table generation"
25459 " (use %<-ftemplate-depth=%> to increase the maximum)",
25460 max_tinst_depth, decl);
25461 if (TREE_CODE (decl) == FUNCTION_DECL)
25462 /* Pretend that we defined it. */
25463 DECL_INITIAL (decl) = error_mark_node;
25464 return;
25465 }
25466
25467 do
25468 {
25469 struct pending_template **t = &pending_templates;
25470 struct pending_template *last = NULL;
25471 reconsider = 0;
25472 while (*t)
25473 {
25474 tree instantiation = reopen_tinst_level ((*t)->tinst);
25475 bool complete = false;
25476
25477 if (TYPE_P (instantiation))
25478 {
25479 if (!COMPLETE_TYPE_P (instantiation))
25480 {
25481 instantiate_class_template (instantiation);
25482 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25483 for (tree fld = TYPE_FIELDS (instantiation);
25484 fld; fld = TREE_CHAIN (fld))
25485 if ((VAR_P (fld)
25486 || (TREE_CODE (fld) == FUNCTION_DECL
25487 && !DECL_ARTIFICIAL (fld)))
25488 && DECL_TEMPLATE_INSTANTIATION (fld))
25489 instantiate_decl (fld,
25490 /*defer_ok=*/false,
25491 /*expl_inst_class_mem_p=*/false);
25492
25493 if (COMPLETE_TYPE_P (instantiation))
25494 reconsider = 1;
25495 }
25496
25497 complete = COMPLETE_TYPE_P (instantiation);
25498 }
25499 else
25500 {
25501 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25502 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25503 {
25504 instantiation
25505 = instantiate_decl (instantiation,
25506 /*defer_ok=*/false,
25507 /*expl_inst_class_mem_p=*/false);
25508 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25509 reconsider = 1;
25510 }
25511
25512 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25513 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25514 }
25515
25516 if (complete)
25517 {
25518 /* If INSTANTIATION has been instantiated, then we don't
25519 need to consider it again in the future. */
25520 struct pending_template *drop = *t;
25521 *t = (*t)->next;
25522 set_refcount_ptr (drop->tinst);
25523 pending_template_freelist ().free (drop);
25524 }
25525 else
25526 {
25527 last = *t;
25528 t = &(*t)->next;
25529 }
25530 tinst_depth = 0;
25531 set_refcount_ptr (current_tinst_level);
25532 }
25533 last_pending_template = last;
25534 }
25535 while (reconsider);
25536
25537 input_location = saved_loc;
25538 }
25539
25540 /* Substitute ARGVEC into T, which is a list of initializers for
25541 either base class or a non-static data member. The TREE_PURPOSEs
25542 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25543 instantiate_decl. */
25544
25545 static tree
25546 tsubst_initializer_list (tree t, tree argvec)
25547 {
25548 tree inits = NULL_TREE;
25549 tree target_ctor = error_mark_node;
25550
25551 for (; t; t = TREE_CHAIN (t))
25552 {
25553 tree decl;
25554 tree init;
25555 tree expanded_bases = NULL_TREE;
25556 tree expanded_arguments = NULL_TREE;
25557 int i, len = 1;
25558
25559 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25560 {
25561 tree expr;
25562 tree arg;
25563
25564 /* Expand the base class expansion type into separate base
25565 classes. */
25566 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25567 tf_warning_or_error,
25568 NULL_TREE);
25569 if (expanded_bases == error_mark_node)
25570 continue;
25571
25572 /* We'll be building separate TREE_LISTs of arguments for
25573 each base. */
25574 len = TREE_VEC_LENGTH (expanded_bases);
25575 expanded_arguments = make_tree_vec (len);
25576 for (i = 0; i < len; i++)
25577 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
25578
25579 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25580 expand each argument in the TREE_VALUE of t. */
25581 expr = make_node (EXPR_PACK_EXPANSION);
25582 PACK_EXPANSION_LOCAL_P (expr) = true;
25583 PACK_EXPANSION_PARAMETER_PACKS (expr) =
25584 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
25585
25586 if (TREE_VALUE (t) == void_type_node)
25587 /* VOID_TYPE_NODE is used to indicate
25588 value-initialization. */
25589 {
25590 for (i = 0; i < len; i++)
25591 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
25592 }
25593 else
25594 {
25595 /* Substitute parameter packs into each argument in the
25596 TREE_LIST. */
25597 in_base_initializer = 1;
25598 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
25599 {
25600 tree expanded_exprs;
25601
25602 /* Expand the argument. */
25603 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25604 expanded_exprs
25605 = tsubst_pack_expansion (expr, argvec,
25606 tf_warning_or_error,
25607 NULL_TREE);
25608 if (expanded_exprs == error_mark_node)
25609 continue;
25610
25611 /* Prepend each of the expanded expressions to the
25612 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25613 for (i = 0; i < len; i++)
25614 {
25615 TREE_VEC_ELT (expanded_arguments, i) =
25616 tree_cons (NULL_TREE,
25617 TREE_VEC_ELT (expanded_exprs, i),
25618 TREE_VEC_ELT (expanded_arguments, i));
25619 }
25620 }
25621 in_base_initializer = 0;
25622
25623 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25624 since we built them backwards. */
25625 for (i = 0; i < len; i++)
25626 {
25627 TREE_VEC_ELT (expanded_arguments, i) =
25628 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25629 }
25630 }
25631 }
25632
25633 for (i = 0; i < len; ++i)
25634 {
25635 if (expanded_bases)
25636 {
25637 decl = TREE_VEC_ELT (expanded_bases, i);
25638 decl = expand_member_init (decl);
25639 init = TREE_VEC_ELT (expanded_arguments, i);
25640 }
25641 else
25642 {
25643 tree tmp;
25644 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25645 tf_warning_or_error, NULL_TREE);
25646
25647 decl = expand_member_init (decl);
25648 if (decl && !DECL_P (decl))
25649 in_base_initializer = 1;
25650
25651 init = TREE_VALUE (t);
25652 tmp = init;
25653 if (init != void_type_node)
25654 init = tsubst_expr (init, argvec,
25655 tf_warning_or_error, NULL_TREE,
25656 /*integral_constant_expression_p=*/false);
25657 if (init == NULL_TREE && tmp != NULL_TREE)
25658 /* If we had an initializer but it instantiated to nothing,
25659 value-initialize the object. This will only occur when
25660 the initializer was a pack expansion where the parameter
25661 packs used in that expansion were of length zero. */
25662 init = void_type_node;
25663 in_base_initializer = 0;
25664 }
25665
25666 if (target_ctor != error_mark_node
25667 && init != error_mark_node)
25668 {
25669 error ("mem-initializer for %qD follows constructor delegation",
25670 decl);
25671 return inits;
25672 }
25673 /* Look for a target constructor. */
25674 if (init != error_mark_node
25675 && decl && CLASS_TYPE_P (decl)
25676 && same_type_p (decl, current_class_type))
25677 {
25678 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25679 if (inits)
25680 {
25681 error ("constructor delegation follows mem-initializer for %qD",
25682 TREE_PURPOSE (inits));
25683 continue;
25684 }
25685 target_ctor = init;
25686 }
25687
25688 if (decl)
25689 {
25690 init = build_tree_list (decl, init);
25691 TREE_CHAIN (init) = inits;
25692 inits = init;
25693 }
25694 }
25695 }
25696 return inits;
25697 }
25698
25699 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25700
25701 static void
25702 set_current_access_from_decl (tree decl)
25703 {
25704 if (TREE_PRIVATE (decl))
25705 current_access_specifier = access_private_node;
25706 else if (TREE_PROTECTED (decl))
25707 current_access_specifier = access_protected_node;
25708 else
25709 current_access_specifier = access_public_node;
25710 }
25711
25712 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25713 is the instantiation (which should have been created with
25714 start_enum) and ARGS are the template arguments to use. */
25715
25716 static void
25717 tsubst_enum (tree tag, tree newtag, tree args)
25718 {
25719 tree e;
25720
25721 if (SCOPED_ENUM_P (newtag))
25722 begin_scope (sk_scoped_enum, newtag);
25723
25724 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25725 {
25726 tree value;
25727 tree decl;
25728
25729 decl = TREE_VALUE (e);
25730 /* Note that in a template enum, the TREE_VALUE is the
25731 CONST_DECL, not the corresponding INTEGER_CST. */
25732 value = tsubst_expr (DECL_INITIAL (decl),
25733 args, tf_warning_or_error, NULL_TREE,
25734 /*integral_constant_expression_p=*/true);
25735
25736 /* Give this enumeration constant the correct access. */
25737 set_current_access_from_decl (decl);
25738
25739 /* Actually build the enumerator itself. Here we're assuming that
25740 enumerators can't have dependent attributes. */
25741 build_enumerator (DECL_NAME (decl), value, newtag,
25742 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25743 }
25744
25745 if (SCOPED_ENUM_P (newtag))
25746 finish_scope ();
25747
25748 finish_enum_value_list (newtag);
25749 finish_enum (newtag);
25750
25751 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25752 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25753 }
25754
25755 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25756 its type -- but without substituting the innermost set of template
25757 arguments. So, innermost set of template parameters will appear in
25758 the type. */
25759
25760 tree
25761 get_mostly_instantiated_function_type (tree decl)
25762 {
25763 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25764 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25765 }
25766
25767 /* Return truthvalue if we're processing a template different from
25768 the last one involved in diagnostics. */
25769 bool
25770 problematic_instantiation_changed (void)
25771 {
25772 return current_tinst_level != last_error_tinst_level;
25773 }
25774
25775 /* Remember current template involved in diagnostics. */
25776 void
25777 record_last_problematic_instantiation (void)
25778 {
25779 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25780 }
25781
25782 struct tinst_level *
25783 current_instantiation (void)
25784 {
25785 return current_tinst_level;
25786 }
25787
25788 /* Return TRUE if current_function_decl is being instantiated, false
25789 otherwise. */
25790
25791 bool
25792 instantiating_current_function_p (void)
25793 {
25794 return (current_instantiation ()
25795 && (current_instantiation ()->maybe_get_node ()
25796 == current_function_decl));
25797 }
25798
25799 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25800 type. Return false for ok, true for disallowed. Issue error and
25801 inform messages under control of COMPLAIN. */
25802
25803 static bool
25804 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25805 {
25806 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25807 return false;
25808 else if (TYPE_PTR_P (type))
25809 return false;
25810 else if (TYPE_REF_P (type)
25811 && !TYPE_REF_IS_RVALUE (type))
25812 return false;
25813 else if (TYPE_PTRMEM_P (type))
25814 return false;
25815 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25816 {
25817 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx2a)
25818 {
25819 if (complain & tf_error)
25820 error ("non-type template parameters of deduced class type only "
25821 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25822 return true;
25823 }
25824 return false;
25825 }
25826 else if (TREE_CODE (type) == TYPENAME_TYPE)
25827 return false;
25828 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25829 return false;
25830 else if (TREE_CODE (type) == NULLPTR_TYPE)
25831 return false;
25832 /* A bound template template parm could later be instantiated to have a valid
25833 nontype parm type via an alias template. */
25834 else if (cxx_dialect >= cxx11
25835 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25836 return false;
25837 else if (CLASS_TYPE_P (type))
25838 {
25839 if (cxx_dialect < cxx2a)
25840 {
25841 if (complain & tf_error)
25842 error ("non-type template parameters of class type only available "
25843 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25844 return true;
25845 }
25846 if (dependent_type_p (type))
25847 return false;
25848 if (!complete_type_or_else (type, NULL_TREE))
25849 return true;
25850 if (!structural_type_p (type))
25851 {
25852 if (complain & tf_error)
25853 {
25854 auto_diagnostic_group d;
25855 error ("%qT is not a valid type for a template non-type "
25856 "parameter because it is not structural", type);
25857 structural_type_p (type, true);
25858 }
25859 return true;
25860 }
25861 return false;
25862 }
25863
25864 if (complain & tf_error)
25865 {
25866 if (type == error_mark_node)
25867 inform (input_location, "invalid template non-type parameter");
25868 else
25869 error ("%q#T is not a valid type for a template non-type parameter",
25870 type);
25871 }
25872 return true;
25873 }
25874
25875 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25876 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25877
25878 static bool
25879 dependent_type_p_r (tree type)
25880 {
25881 tree scope;
25882
25883 /* [temp.dep.type]
25884
25885 A type is dependent if it is:
25886
25887 -- a template parameter. Template template parameters are types
25888 for us (since TYPE_P holds true for them) so we handle
25889 them here. */
25890 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25891 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25892 return true;
25893 /* -- a qualified-id with a nested-name-specifier which contains a
25894 class-name that names a dependent type or whose unqualified-id
25895 names a dependent type. */
25896 if (TREE_CODE (type) == TYPENAME_TYPE)
25897 return true;
25898
25899 /* An alias template specialization can be dependent even if the
25900 resulting type is not. */
25901 if (dependent_alias_template_spec_p (type, nt_transparent))
25902 return true;
25903
25904 /* -- a cv-qualified type where the cv-unqualified type is
25905 dependent.
25906 No code is necessary for this bullet; the code below handles
25907 cv-qualified types, and we don't want to strip aliases with
25908 TYPE_MAIN_VARIANT because of DR 1558. */
25909 /* -- a compound type constructed from any dependent type. */
25910 if (TYPE_PTRMEM_P (type))
25911 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25912 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25913 (type)));
25914 else if (INDIRECT_TYPE_P (type))
25915 return dependent_type_p (TREE_TYPE (type));
25916 else if (FUNC_OR_METHOD_TYPE_P (type))
25917 {
25918 tree arg_type;
25919
25920 if (dependent_type_p (TREE_TYPE (type)))
25921 return true;
25922 for (arg_type = TYPE_ARG_TYPES (type);
25923 arg_type;
25924 arg_type = TREE_CHAIN (arg_type))
25925 if (dependent_type_p (TREE_VALUE (arg_type)))
25926 return true;
25927 if (cxx_dialect >= cxx17)
25928 /* A value-dependent noexcept-specifier makes the type dependent. */
25929 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25930 if (tree noex = TREE_PURPOSE (spec))
25931 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25932 affect overload resolution and treating it as dependent breaks
25933 things. Same for an unparsed noexcept expression. */
25934 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25935 && TREE_CODE (noex) != DEFERRED_PARSE
25936 && value_dependent_expression_p (noex))
25937 return true;
25938 return false;
25939 }
25940 /* -- an array type constructed from any dependent type or whose
25941 size is specified by a constant expression that is
25942 value-dependent.
25943
25944 We checked for type- and value-dependence of the bounds in
25945 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25946 if (TREE_CODE (type) == ARRAY_TYPE)
25947 {
25948 if (TYPE_DOMAIN (type)
25949 && dependent_type_p (TYPE_DOMAIN (type)))
25950 return true;
25951 return dependent_type_p (TREE_TYPE (type));
25952 }
25953
25954 /* -- a template-id in which either the template name is a template
25955 parameter ... */
25956 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25957 return true;
25958 /* ... or any of the template arguments is a dependent type or
25959 an expression that is type-dependent or value-dependent. */
25960 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25961 && (any_dependent_template_arguments_p
25962 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25963 return true;
25964
25965 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25966 dependent; if the argument of the `typeof' expression is not
25967 type-dependent, then it should already been have resolved. */
25968 if (TREE_CODE (type) == TYPEOF_TYPE
25969 || TREE_CODE (type) == DECLTYPE_TYPE
25970 || TREE_CODE (type) == UNDERLYING_TYPE)
25971 return true;
25972
25973 /* A template argument pack is dependent if any of its packed
25974 arguments are. */
25975 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25976 {
25977 tree args = ARGUMENT_PACK_ARGS (type);
25978 int i, len = TREE_VEC_LENGTH (args);
25979 for (i = 0; i < len; ++i)
25980 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25981 return true;
25982 }
25983
25984 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25985 be template parameters. */
25986 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25987 return true;
25988
25989 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25990 return true;
25991
25992 /* The standard does not specifically mention types that are local
25993 to template functions or local classes, but they should be
25994 considered dependent too. For example:
25995
25996 template <int I> void f() {
25997 enum E { a = I };
25998 S<sizeof (E)> s;
25999 }
26000
26001 The size of `E' cannot be known until the value of `I' has been
26002 determined. Therefore, `E' must be considered dependent. */
26003 scope = TYPE_CONTEXT (type);
26004 if (scope && TYPE_P (scope))
26005 return dependent_type_p (scope);
26006 /* Don't use type_dependent_expression_p here, as it can lead
26007 to infinite recursion trying to determine whether a lambda
26008 nested in a lambda is dependent (c++/47687). */
26009 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26010 && DECL_LANG_SPECIFIC (scope)
26011 && DECL_TEMPLATE_INFO (scope)
26012 && (any_dependent_template_arguments_p
26013 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26014 return true;
26015
26016 /* Other types are non-dependent. */
26017 return false;
26018 }
26019
26020 /* Returns TRUE if TYPE is dependent, in the sense of
26021 [temp.dep.type]. Note that a NULL type is considered dependent. */
26022
26023 bool
26024 dependent_type_p (tree type)
26025 {
26026 /* If there are no template parameters in scope, then there can't be
26027 any dependent types. */
26028 if (!processing_template_decl)
26029 {
26030 /* If we are not processing a template, then nobody should be
26031 providing us with a dependent type. */
26032 gcc_assert (type);
26033 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26034 return false;
26035 }
26036
26037 /* If the type is NULL, we have not computed a type for the entity
26038 in question; in that case, the type is dependent. */
26039 if (!type)
26040 return true;
26041
26042 /* Erroneous types can be considered non-dependent. */
26043 if (type == error_mark_node)
26044 return false;
26045
26046 /* Getting here with global_type_node means we improperly called this
26047 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26048 gcc_checking_assert (type != global_type_node);
26049
26050 /* If we have not already computed the appropriate value for TYPE,
26051 do so now. */
26052 if (!TYPE_DEPENDENT_P_VALID (type))
26053 {
26054 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26055 TYPE_DEPENDENT_P_VALID (type) = 1;
26056 }
26057
26058 return TYPE_DEPENDENT_P (type);
26059 }
26060
26061 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26062 lookup. In other words, a dependent type that is not the current
26063 instantiation. */
26064
26065 bool
26066 dependent_scope_p (tree scope)
26067 {
26068 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26069 && !currently_open_class (scope));
26070 }
26071
26072 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26073 an unknown base of 'this' (and is therefore instantiation-dependent). */
26074
26075 static bool
26076 unknown_base_ref_p (tree t)
26077 {
26078 if (!current_class_ptr)
26079 return false;
26080
26081 tree mem = TREE_OPERAND (t, 1);
26082 if (shared_member_p (mem))
26083 return false;
26084
26085 tree cur = current_nonlambda_class_type ();
26086 if (!any_dependent_bases_p (cur))
26087 return false;
26088
26089 tree ctx = TREE_OPERAND (t, 0);
26090 if (DERIVED_FROM_P (ctx, cur))
26091 return false;
26092
26093 return true;
26094 }
26095
26096 /* T is a SCOPE_REF; return whether we need to consider it
26097 instantiation-dependent so that we can check access at instantiation
26098 time even though we know which member it resolves to. */
26099
26100 static bool
26101 instantiation_dependent_scope_ref_p (tree t)
26102 {
26103 if (DECL_P (TREE_OPERAND (t, 1))
26104 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26105 && !unknown_base_ref_p (t)
26106 && accessible_in_template_p (TREE_OPERAND (t, 0),
26107 TREE_OPERAND (t, 1)))
26108 return false;
26109 else
26110 return true;
26111 }
26112
26113 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26114 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26115 expression. */
26116
26117 /* Note that this predicate is not appropriate for general expressions;
26118 only constant expressions (that satisfy potential_constant_expression)
26119 can be tested for value dependence. */
26120
26121 bool
26122 value_dependent_expression_p (tree expression)
26123 {
26124 if (!processing_template_decl || expression == NULL_TREE)
26125 return false;
26126
26127 /* A type-dependent expression is also value-dependent. */
26128 if (type_dependent_expression_p (expression))
26129 return true;
26130
26131 switch (TREE_CODE (expression))
26132 {
26133 case BASELINK:
26134 /* A dependent member function of the current instantiation. */
26135 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26136
26137 case FUNCTION_DECL:
26138 /* A dependent member function of the current instantiation. */
26139 if (DECL_CLASS_SCOPE_P (expression)
26140 && dependent_type_p (DECL_CONTEXT (expression)))
26141 return true;
26142 break;
26143
26144 case IDENTIFIER_NODE:
26145 /* A name that has not been looked up -- must be dependent. */
26146 return true;
26147
26148 case TEMPLATE_PARM_INDEX:
26149 /* A non-type template parm. */
26150 return true;
26151
26152 case CONST_DECL:
26153 /* A non-type template parm. */
26154 if (DECL_TEMPLATE_PARM_P (expression))
26155 return true;
26156 return value_dependent_expression_p (DECL_INITIAL (expression));
26157
26158 case VAR_DECL:
26159 /* A constant with literal type and is initialized
26160 with an expression that is value-dependent. */
26161 if (DECL_DEPENDENT_INIT_P (expression)
26162 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26163 || TYPE_REF_P (TREE_TYPE (expression)))
26164 return true;
26165 if (DECL_HAS_VALUE_EXPR_P (expression))
26166 {
26167 tree value_expr = DECL_VALUE_EXPR (expression);
26168 if (value_dependent_expression_p (value_expr)
26169 /* __PRETTY_FUNCTION__ inside a template function is dependent
26170 on the name of the function. */
26171 || (DECL_PRETTY_FUNCTION_P (expression)
26172 /* It might be used in a template, but not a template
26173 function, in which case its DECL_VALUE_EXPR will be
26174 "top level". */
26175 && value_expr == error_mark_node))
26176 return true;
26177 }
26178 return false;
26179
26180 case DYNAMIC_CAST_EXPR:
26181 case STATIC_CAST_EXPR:
26182 case CONST_CAST_EXPR:
26183 case REINTERPRET_CAST_EXPR:
26184 case CAST_EXPR:
26185 case IMPLICIT_CONV_EXPR:
26186 /* These expressions are value-dependent if the type to which
26187 the cast occurs is dependent or the expression being casted
26188 is value-dependent. */
26189 {
26190 tree type = TREE_TYPE (expression);
26191
26192 if (dependent_type_p (type))
26193 return true;
26194
26195 /* A functional cast has a list of operands. */
26196 expression = TREE_OPERAND (expression, 0);
26197 if (!expression)
26198 {
26199 /* If there are no operands, it must be an expression such
26200 as "int()". This should not happen for aggregate types
26201 because it would form non-constant expressions. */
26202 gcc_assert (cxx_dialect >= cxx11
26203 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26204
26205 return false;
26206 }
26207
26208 if (TREE_CODE (expression) == TREE_LIST)
26209 return any_value_dependent_elements_p (expression);
26210
26211 return value_dependent_expression_p (expression);
26212 }
26213
26214 case SIZEOF_EXPR:
26215 if (SIZEOF_EXPR_TYPE_P (expression))
26216 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26217 /* FALLTHRU */
26218 case ALIGNOF_EXPR:
26219 case TYPEID_EXPR:
26220 /* A `sizeof' expression is value-dependent if the operand is
26221 type-dependent or is a pack expansion. */
26222 expression = TREE_OPERAND (expression, 0);
26223 if (PACK_EXPANSION_P (expression))
26224 return true;
26225 else if (TYPE_P (expression))
26226 return dependent_type_p (expression);
26227 return instantiation_dependent_uneval_expression_p (expression);
26228
26229 case AT_ENCODE_EXPR:
26230 /* An 'encode' expression is value-dependent if the operand is
26231 type-dependent. */
26232 expression = TREE_OPERAND (expression, 0);
26233 return dependent_type_p (expression);
26234
26235 case NOEXCEPT_EXPR:
26236 expression = TREE_OPERAND (expression, 0);
26237 return instantiation_dependent_uneval_expression_p (expression);
26238
26239 case SCOPE_REF:
26240 /* All instantiation-dependent expressions should also be considered
26241 value-dependent. */
26242 return instantiation_dependent_scope_ref_p (expression);
26243
26244 case COMPONENT_REF:
26245 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26246 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26247
26248 case NONTYPE_ARGUMENT_PACK:
26249 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26250 is value-dependent. */
26251 {
26252 tree values = ARGUMENT_PACK_ARGS (expression);
26253 int i, len = TREE_VEC_LENGTH (values);
26254
26255 for (i = 0; i < len; ++i)
26256 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26257 return true;
26258
26259 return false;
26260 }
26261
26262 case TRAIT_EXPR:
26263 {
26264 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26265
26266 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26267 return true;
26268
26269 if (!type2)
26270 return false;
26271
26272 if (TREE_CODE (type2) != TREE_LIST)
26273 return dependent_type_p (type2);
26274
26275 for (; type2; type2 = TREE_CHAIN (type2))
26276 if (dependent_type_p (TREE_VALUE (type2)))
26277 return true;
26278
26279 return false;
26280 }
26281
26282 case MODOP_EXPR:
26283 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26284 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26285
26286 case ARRAY_REF:
26287 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26288 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26289
26290 case ADDR_EXPR:
26291 {
26292 tree op = TREE_OPERAND (expression, 0);
26293 return (value_dependent_expression_p (op)
26294 || has_value_dependent_address (op));
26295 }
26296
26297 case REQUIRES_EXPR:
26298 /* Treat all requires-expressions as value-dependent so
26299 we don't try to fold them. */
26300 return true;
26301
26302 case TYPE_REQ:
26303 return dependent_type_p (TREE_OPERAND (expression, 0));
26304
26305 case CALL_EXPR:
26306 {
26307 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26308 return true;
26309 tree fn = get_callee_fndecl (expression);
26310 int i, nargs;
26311 nargs = call_expr_nargs (expression);
26312 for (i = 0; i < nargs; ++i)
26313 {
26314 tree op = CALL_EXPR_ARG (expression, i);
26315 /* In a call to a constexpr member function, look through the
26316 implicit ADDR_EXPR on the object argument so that it doesn't
26317 cause the call to be considered value-dependent. We also
26318 look through it in potential_constant_expression. */
26319 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26320 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26321 && TREE_CODE (op) == ADDR_EXPR)
26322 op = TREE_OPERAND (op, 0);
26323 if (value_dependent_expression_p (op))
26324 return true;
26325 }
26326 return false;
26327 }
26328
26329 case TEMPLATE_ID_EXPR:
26330 return concept_definition_p (TREE_OPERAND (expression, 0));
26331
26332 case CONSTRUCTOR:
26333 {
26334 unsigned ix;
26335 tree val;
26336 if (dependent_type_p (TREE_TYPE (expression)))
26337 return true;
26338 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26339 if (value_dependent_expression_p (val))
26340 return true;
26341 return false;
26342 }
26343
26344 case STMT_EXPR:
26345 /* Treat a GNU statement expression as dependent to avoid crashing
26346 under instantiate_non_dependent_expr; it can't be constant. */
26347 return true;
26348
26349 default:
26350 /* A constant expression is value-dependent if any subexpression is
26351 value-dependent. */
26352 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26353 {
26354 case tcc_reference:
26355 case tcc_unary:
26356 case tcc_comparison:
26357 case tcc_binary:
26358 case tcc_expression:
26359 case tcc_vl_exp:
26360 {
26361 int i, len = cp_tree_operand_length (expression);
26362
26363 for (i = 0; i < len; i++)
26364 {
26365 tree t = TREE_OPERAND (expression, i);
26366
26367 /* In some cases, some of the operands may be missing.
26368 (For example, in the case of PREDECREMENT_EXPR, the
26369 amount to increment by may be missing.) That doesn't
26370 make the expression dependent. */
26371 if (t && value_dependent_expression_p (t))
26372 return true;
26373 }
26374 }
26375 break;
26376 default:
26377 break;
26378 }
26379 break;
26380 }
26381
26382 /* The expression is not value-dependent. */
26383 return false;
26384 }
26385
26386 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26387 [temp.dep.expr]. Note that an expression with no type is
26388 considered dependent. Other parts of the compiler arrange for an
26389 expression with type-dependent subexpressions to have no type, so
26390 this function doesn't have to be fully recursive. */
26391
26392 bool
26393 type_dependent_expression_p (tree expression)
26394 {
26395 if (!processing_template_decl)
26396 return false;
26397
26398 if (expression == NULL_TREE || expression == error_mark_node)
26399 return false;
26400
26401 STRIP_ANY_LOCATION_WRAPPER (expression);
26402
26403 /* An unresolved name is always dependent. */
26404 if (identifier_p (expression)
26405 || TREE_CODE (expression) == USING_DECL
26406 || TREE_CODE (expression) == WILDCARD_DECL)
26407 return true;
26408
26409 /* A lambda-expression in template context is dependent. dependent_type_p is
26410 true for a lambda in the scope of a class or function template, but that
26411 doesn't cover all template contexts, like a default template argument. */
26412 if (TREE_CODE (expression) == LAMBDA_EXPR)
26413 return true;
26414
26415 /* A fold expression is type-dependent. */
26416 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26417 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26418 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26419 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26420 return true;
26421
26422 /* Some expression forms are never type-dependent. */
26423 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
26424 || TREE_CODE (expression) == SIZEOF_EXPR
26425 || TREE_CODE (expression) == ALIGNOF_EXPR
26426 || TREE_CODE (expression) == AT_ENCODE_EXPR
26427 || TREE_CODE (expression) == NOEXCEPT_EXPR
26428 || TREE_CODE (expression) == TRAIT_EXPR
26429 || TREE_CODE (expression) == TYPEID_EXPR
26430 || TREE_CODE (expression) == DELETE_EXPR
26431 || TREE_CODE (expression) == VEC_DELETE_EXPR
26432 || TREE_CODE (expression) == THROW_EXPR
26433 || TREE_CODE (expression) == REQUIRES_EXPR)
26434 return false;
26435
26436 /* The types of these expressions depends only on the type to which
26437 the cast occurs. */
26438 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26439 || TREE_CODE (expression) == STATIC_CAST_EXPR
26440 || TREE_CODE (expression) == CONST_CAST_EXPR
26441 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26442 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26443 || TREE_CODE (expression) == CAST_EXPR)
26444 return dependent_type_p (TREE_TYPE (expression));
26445
26446 /* The types of these expressions depends only on the type created
26447 by the expression. */
26448 if (TREE_CODE (expression) == NEW_EXPR
26449 || TREE_CODE (expression) == VEC_NEW_EXPR)
26450 {
26451 /* For NEW_EXPR tree nodes created inside a template, either
26452 the object type itself or a TREE_LIST may appear as the
26453 operand 1. */
26454 tree type = TREE_OPERAND (expression, 1);
26455 if (TREE_CODE (type) == TREE_LIST)
26456 /* This is an array type. We need to check array dimensions
26457 as well. */
26458 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26459 || value_dependent_expression_p
26460 (TREE_OPERAND (TREE_VALUE (type), 1));
26461 else
26462 return dependent_type_p (type);
26463 }
26464
26465 if (TREE_CODE (expression) == SCOPE_REF)
26466 {
26467 tree scope = TREE_OPERAND (expression, 0);
26468 tree name = TREE_OPERAND (expression, 1);
26469
26470 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26471 contains an identifier associated by name lookup with one or more
26472 declarations declared with a dependent type, or...a
26473 nested-name-specifier or qualified-id that names a member of an
26474 unknown specialization. */
26475 return (type_dependent_expression_p (name)
26476 || dependent_scope_p (scope));
26477 }
26478
26479 if (TREE_CODE (expression) == TEMPLATE_DECL
26480 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26481 return uses_outer_template_parms (expression);
26482
26483 if (TREE_CODE (expression) == STMT_EXPR)
26484 expression = stmt_expr_value_expr (expression);
26485
26486 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26487 {
26488 tree elt;
26489 unsigned i;
26490
26491 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26492 {
26493 if (type_dependent_expression_p (elt))
26494 return true;
26495 }
26496 return false;
26497 }
26498
26499 /* A static data member of the current instantiation with incomplete
26500 array type is type-dependent, as the definition and specializations
26501 can have different bounds. */
26502 if (VAR_P (expression)
26503 && DECL_CLASS_SCOPE_P (expression)
26504 && dependent_type_p (DECL_CONTEXT (expression))
26505 && VAR_HAD_UNKNOWN_BOUND (expression))
26506 return true;
26507
26508 /* An array of unknown bound depending on a variadic parameter, eg:
26509
26510 template<typename... Args>
26511 void foo (Args... args)
26512 {
26513 int arr[] = { args... };
26514 }
26515
26516 template<int... vals>
26517 void bar ()
26518 {
26519 int arr[] = { vals... };
26520 }
26521
26522 If the array has no length and has an initializer, it must be that
26523 we couldn't determine its length in cp_complete_array_type because
26524 it is dependent. */
26525 if (VAR_P (expression)
26526 && TREE_TYPE (expression) != NULL_TREE
26527 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26528 && !TYPE_DOMAIN (TREE_TYPE (expression))
26529 && DECL_INITIAL (expression))
26530 return true;
26531
26532 /* A function or variable template-id is type-dependent if it has any
26533 dependent template arguments. */
26534 if (VAR_OR_FUNCTION_DECL_P (expression)
26535 && DECL_LANG_SPECIFIC (expression)
26536 && DECL_TEMPLATE_INFO (expression))
26537 {
26538 /* Consider the innermost template arguments, since those are the ones
26539 that come from the template-id; the template arguments for the
26540 enclosing class do not make it type-dependent unless they are used in
26541 the type of the decl. */
26542 if (instantiates_primary_template_p (expression)
26543 && (any_dependent_template_arguments_p
26544 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26545 return true;
26546 }
26547
26548 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26549 type-dependent. Checking this is important for functions with auto return
26550 type, which looks like a dependent type. */
26551 if (TREE_CODE (expression) == FUNCTION_DECL
26552 && !(DECL_CLASS_SCOPE_P (expression)
26553 && dependent_type_p (DECL_CONTEXT (expression)))
26554 && !(DECL_LANG_SPECIFIC (expression)
26555 && DECL_FRIEND_P (expression)
26556 && (!DECL_FRIEND_CONTEXT (expression)
26557 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26558 && !DECL_LOCAL_FUNCTION_P (expression))
26559 {
26560 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26561 || undeduced_auto_decl (expression));
26562 return false;
26563 }
26564
26565 /* Always dependent, on the number of arguments if nothing else. */
26566 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
26567 return true;
26568
26569 if (TREE_TYPE (expression) == unknown_type_node)
26570 {
26571 if (TREE_CODE (expression) == ADDR_EXPR)
26572 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
26573 if (TREE_CODE (expression) == COMPONENT_REF
26574 || TREE_CODE (expression) == OFFSET_REF)
26575 {
26576 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
26577 return true;
26578 expression = TREE_OPERAND (expression, 1);
26579 if (identifier_p (expression))
26580 return false;
26581 }
26582 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26583 if (TREE_CODE (expression) == SCOPE_REF)
26584 return false;
26585
26586 if (BASELINK_P (expression))
26587 {
26588 if (BASELINK_OPTYPE (expression)
26589 && dependent_type_p (BASELINK_OPTYPE (expression)))
26590 return true;
26591 expression = BASELINK_FUNCTIONS (expression);
26592 }
26593
26594 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
26595 {
26596 if (any_dependent_template_arguments_p
26597 (TREE_OPERAND (expression, 1)))
26598 return true;
26599 expression = TREE_OPERAND (expression, 0);
26600 if (identifier_p (expression))
26601 return true;
26602 }
26603
26604 gcc_assert (OVL_P (expression));
26605
26606 for (lkp_iterator iter (expression); iter; ++iter)
26607 if (type_dependent_expression_p (*iter))
26608 return true;
26609
26610 return false;
26611 }
26612
26613 /* The type of a non-type template parm declared with a placeholder type
26614 depends on the corresponding template argument, even though
26615 placeholders are not normally considered dependent. */
26616 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26617 && is_auto (TREE_TYPE (expression)))
26618 return true;
26619
26620 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26621
26622 /* Dependent type attributes might not have made it from the decl to
26623 the type yet. */
26624 if (DECL_P (expression)
26625 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26626 return true;
26627
26628 return (dependent_type_p (TREE_TYPE (expression)));
26629 }
26630
26631 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26632 type-dependent if the expression refers to a member of the current
26633 instantiation and the type of the referenced member is dependent, or the
26634 class member access expression refers to a member of an unknown
26635 specialization.
26636
26637 This function returns true if the OBJECT in such a class member access
26638 expression is of an unknown specialization. */
26639
26640 bool
26641 type_dependent_object_expression_p (tree object)
26642 {
26643 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26644 dependent. */
26645 if (TREE_CODE (object) == IDENTIFIER_NODE)
26646 return true;
26647 tree scope = TREE_TYPE (object);
26648 return (!scope || dependent_scope_p (scope));
26649 }
26650
26651 /* walk_tree callback function for instantiation_dependent_expression_p,
26652 below. Returns non-zero if a dependent subexpression is found. */
26653
26654 static tree
26655 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26656 void * /*data*/)
26657 {
26658 if (TYPE_P (*tp))
26659 {
26660 /* We don't have to worry about decltype currently because decltype
26661 of an instantiation-dependent expr is a dependent type. This
26662 might change depending on the resolution of DR 1172. */
26663 *walk_subtrees = false;
26664 return NULL_TREE;
26665 }
26666 enum tree_code code = TREE_CODE (*tp);
26667 switch (code)
26668 {
26669 /* Don't treat an argument list as dependent just because it has no
26670 TREE_TYPE. */
26671 case TREE_LIST:
26672 case TREE_VEC:
26673 case NONTYPE_ARGUMENT_PACK:
26674 return NULL_TREE;
26675
26676 case TEMPLATE_PARM_INDEX:
26677 if (dependent_type_p (TREE_TYPE (*tp)))
26678 return *tp;
26679 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26680 return *tp;
26681 /* We'll check value-dependence separately. */
26682 return NULL_TREE;
26683
26684 /* Handle expressions with type operands. */
26685 case SIZEOF_EXPR:
26686 case ALIGNOF_EXPR:
26687 case TYPEID_EXPR:
26688 case AT_ENCODE_EXPR:
26689 {
26690 tree op = TREE_OPERAND (*tp, 0);
26691 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26692 op = TREE_TYPE (op);
26693 if (TYPE_P (op))
26694 {
26695 if (dependent_type_p (op))
26696 return *tp;
26697 else
26698 {
26699 *walk_subtrees = false;
26700 return NULL_TREE;
26701 }
26702 }
26703 break;
26704 }
26705
26706 case COMPONENT_REF:
26707 if (identifier_p (TREE_OPERAND (*tp, 1)))
26708 /* In a template, finish_class_member_access_expr creates a
26709 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26710 type-dependent, so that we can check access control at
26711 instantiation time (PR 42277). See also Core issue 1273. */
26712 return *tp;
26713 break;
26714
26715 case SCOPE_REF:
26716 if (instantiation_dependent_scope_ref_p (*tp))
26717 return *tp;
26718 else
26719 break;
26720
26721 /* Treat statement-expressions as dependent. */
26722 case BIND_EXPR:
26723 return *tp;
26724
26725 /* Treat requires-expressions as dependent. */
26726 case REQUIRES_EXPR:
26727 return *tp;
26728
26729 case CALL_EXPR:
26730 /* Treat concept checks as dependent. */
26731 if (concept_check_p (*tp))
26732 return *tp;
26733 break;
26734
26735 case TEMPLATE_ID_EXPR:
26736 /* Treat concept checks as dependent. */
26737 if (concept_check_p (*tp))
26738 return *tp;
26739 break;
26740
26741 case CONSTRUCTOR:
26742 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26743 return *tp;
26744 break;
26745
26746 default:
26747 break;
26748 }
26749
26750 if (type_dependent_expression_p (*tp))
26751 return *tp;
26752 else
26753 return NULL_TREE;
26754 }
26755
26756 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26757 sense defined by the ABI:
26758
26759 "An expression is instantiation-dependent if it is type-dependent
26760 or value-dependent, or it has a subexpression that is type-dependent
26761 or value-dependent."
26762
26763 Except don't actually check value-dependence for unevaluated expressions,
26764 because in sizeof(i) we don't care about the value of i. Checking
26765 type-dependence will in turn check value-dependence of array bounds/template
26766 arguments as needed. */
26767
26768 bool
26769 instantiation_dependent_uneval_expression_p (tree expression)
26770 {
26771 tree result;
26772
26773 if (!processing_template_decl)
26774 return false;
26775
26776 if (expression == error_mark_node)
26777 return false;
26778
26779 result = cp_walk_tree_without_duplicates (&expression,
26780 instantiation_dependent_r, NULL);
26781 return result != NULL_TREE;
26782 }
26783
26784 /* As above, but also check value-dependence of the expression as a whole. */
26785
26786 bool
26787 instantiation_dependent_expression_p (tree expression)
26788 {
26789 return (instantiation_dependent_uneval_expression_p (expression)
26790 || value_dependent_expression_p (expression));
26791 }
26792
26793 /* Like type_dependent_expression_p, but it also works while not processing
26794 a template definition, i.e. during substitution or mangling. */
26795
26796 bool
26797 type_dependent_expression_p_push (tree expr)
26798 {
26799 bool b;
26800 ++processing_template_decl;
26801 b = type_dependent_expression_p (expr);
26802 --processing_template_decl;
26803 return b;
26804 }
26805
26806 /* Returns TRUE if ARGS contains a type-dependent expression. */
26807
26808 bool
26809 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26810 {
26811 unsigned int i;
26812 tree arg;
26813
26814 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26815 {
26816 if (type_dependent_expression_p (arg))
26817 return true;
26818 }
26819 return false;
26820 }
26821
26822 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26823 expressions) contains any type-dependent expressions. */
26824
26825 bool
26826 any_type_dependent_elements_p (const_tree list)
26827 {
26828 for (; list; list = TREE_CHAIN (list))
26829 if (type_dependent_expression_p (TREE_VALUE (list)))
26830 return true;
26831
26832 return false;
26833 }
26834
26835 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26836 expressions) contains any value-dependent expressions. */
26837
26838 bool
26839 any_value_dependent_elements_p (const_tree list)
26840 {
26841 for (; list; list = TREE_CHAIN (list))
26842 if (value_dependent_expression_p (TREE_VALUE (list)))
26843 return true;
26844
26845 return false;
26846 }
26847
26848 /* Returns TRUE if the ARG (a template argument) is dependent. */
26849
26850 bool
26851 dependent_template_arg_p (tree arg)
26852 {
26853 if (!processing_template_decl)
26854 return false;
26855
26856 /* Assume a template argument that was wrongly written by the user
26857 is dependent. This is consistent with what
26858 any_dependent_template_arguments_p [that calls this function]
26859 does. */
26860 if (!arg || arg == error_mark_node)
26861 return true;
26862
26863 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26864 arg = argument_pack_select_arg (arg);
26865
26866 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26867 return true;
26868 if (TREE_CODE (arg) == TEMPLATE_DECL)
26869 {
26870 if (DECL_TEMPLATE_PARM_P (arg))
26871 return true;
26872 /* A member template of a dependent class is not necessarily
26873 type-dependent, but it is a dependent template argument because it
26874 will be a member of an unknown specialization to that template. */
26875 tree scope = CP_DECL_CONTEXT (arg);
26876 return TYPE_P (scope) && dependent_type_p (scope);
26877 }
26878 else if (ARGUMENT_PACK_P (arg))
26879 {
26880 tree args = ARGUMENT_PACK_ARGS (arg);
26881 int i, len = TREE_VEC_LENGTH (args);
26882 for (i = 0; i < len; ++i)
26883 {
26884 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26885 return true;
26886 }
26887
26888 return false;
26889 }
26890 else if (TYPE_P (arg))
26891 return dependent_type_p (arg);
26892 else
26893 return (type_dependent_expression_p (arg)
26894 || value_dependent_expression_p (arg));
26895 }
26896
26897 /* Returns true if ARGS (a collection of template arguments) contains
26898 any types that require structural equality testing. */
26899
26900 bool
26901 any_template_arguments_need_structural_equality_p (tree args)
26902 {
26903 int i;
26904 int j;
26905
26906 if (!args)
26907 return false;
26908 if (args == error_mark_node)
26909 return true;
26910
26911 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26912 {
26913 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26914 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26915 {
26916 tree arg = TREE_VEC_ELT (level, j);
26917 tree packed_args = NULL_TREE;
26918 int k, len = 1;
26919
26920 if (ARGUMENT_PACK_P (arg))
26921 {
26922 /* Look inside the argument pack. */
26923 packed_args = ARGUMENT_PACK_ARGS (arg);
26924 len = TREE_VEC_LENGTH (packed_args);
26925 }
26926
26927 for (k = 0; k < len; ++k)
26928 {
26929 if (packed_args)
26930 arg = TREE_VEC_ELT (packed_args, k);
26931
26932 if (error_operand_p (arg))
26933 return true;
26934 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26935 continue;
26936 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26937 return true;
26938 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26939 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26940 return true;
26941 }
26942 }
26943 }
26944
26945 return false;
26946 }
26947
26948 /* Returns true if ARGS (a collection of template arguments) contains
26949 any dependent arguments. */
26950
26951 bool
26952 any_dependent_template_arguments_p (const_tree args)
26953 {
26954 int i;
26955 int j;
26956
26957 if (!args)
26958 return false;
26959 if (args == error_mark_node)
26960 return true;
26961
26962 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26963 {
26964 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26965 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26966 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26967 return true;
26968 }
26969
26970 return false;
26971 }
26972
26973 /* Returns true if ARGS contains any errors. */
26974
26975 bool
26976 any_erroneous_template_args_p (const_tree args)
26977 {
26978 int i;
26979 int j;
26980
26981 if (args == error_mark_node)
26982 return true;
26983
26984 if (args && TREE_CODE (args) != TREE_VEC)
26985 {
26986 if (tree ti = get_template_info (args))
26987 args = TI_ARGS (ti);
26988 else
26989 args = NULL_TREE;
26990 }
26991
26992 if (!args)
26993 return false;
26994
26995 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26996 {
26997 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26998 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26999 if (error_operand_p (TREE_VEC_ELT (level, j)))
27000 return true;
27001 }
27002
27003 return false;
27004 }
27005
27006 /* Returns TRUE if the template TMPL is type-dependent. */
27007
27008 bool
27009 dependent_template_p (tree tmpl)
27010 {
27011 if (TREE_CODE (tmpl) == OVERLOAD)
27012 {
27013 for (lkp_iterator iter (tmpl); iter; ++iter)
27014 if (dependent_template_p (*iter))
27015 return true;
27016 return false;
27017 }
27018
27019 /* Template template parameters are dependent. */
27020 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27021 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27022 return true;
27023 /* So are names that have not been looked up. */
27024 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27025 return true;
27026 return false;
27027 }
27028
27029 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27030
27031 bool
27032 dependent_template_id_p (tree tmpl, tree args)
27033 {
27034 return (dependent_template_p (tmpl)
27035 || any_dependent_template_arguments_p (args));
27036 }
27037
27038 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27039 are dependent. */
27040
27041 bool
27042 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27043 {
27044 int i;
27045
27046 if (!processing_template_decl)
27047 return false;
27048
27049 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27050 {
27051 tree decl = TREE_VEC_ELT (declv, i);
27052 tree init = TREE_VEC_ELT (initv, i);
27053 tree cond = TREE_VEC_ELT (condv, i);
27054 tree incr = TREE_VEC_ELT (incrv, i);
27055
27056 if (type_dependent_expression_p (decl)
27057 || TREE_CODE (decl) == SCOPE_REF)
27058 return true;
27059
27060 if (init && type_dependent_expression_p (init))
27061 return true;
27062
27063 if (cond == global_namespace)
27064 return true;
27065
27066 if (type_dependent_expression_p (cond))
27067 return true;
27068
27069 if (COMPARISON_CLASS_P (cond)
27070 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27071 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27072 return true;
27073
27074 if (TREE_CODE (incr) == MODOP_EXPR)
27075 {
27076 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27077 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27078 return true;
27079 }
27080 else if (type_dependent_expression_p (incr))
27081 return true;
27082 else if (TREE_CODE (incr) == MODIFY_EXPR)
27083 {
27084 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27085 return true;
27086 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27087 {
27088 tree t = TREE_OPERAND (incr, 1);
27089 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27090 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27091 return true;
27092
27093 /* If this loop has a class iterator with != comparison
27094 with increment other than i++/++i/i--/--i, make sure the
27095 increment is constant. */
27096 if (CLASS_TYPE_P (TREE_TYPE (decl))
27097 && TREE_CODE (cond) == NE_EXPR)
27098 {
27099 if (TREE_OPERAND (t, 0) == decl)
27100 t = TREE_OPERAND (t, 1);
27101 else
27102 t = TREE_OPERAND (t, 0);
27103 if (TREE_CODE (t) != INTEGER_CST)
27104 return true;
27105 }
27106 }
27107 }
27108 }
27109
27110 return false;
27111 }
27112
27113 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27114 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27115 no such TYPE can be found. Note that this function peers inside
27116 uninstantiated templates and therefore should be used only in
27117 extremely limited situations. ONLY_CURRENT_P restricts this
27118 peering to the currently open classes hierarchy (which is required
27119 when comparing types). */
27120
27121 tree
27122 resolve_typename_type (tree type, bool only_current_p)
27123 {
27124 tree scope;
27125 tree name;
27126 tree decl;
27127 int quals;
27128 tree pushed_scope;
27129 tree result;
27130
27131 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27132
27133 scope = TYPE_CONTEXT (type);
27134 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27135 gcc_checking_assert (uses_template_parms (scope));
27136
27137 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27138 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27139 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27140 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27141 identifier of the TYPENAME_TYPE anymore.
27142 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27143 TYPENAME_TYPE instead, we avoid messing up with a possible
27144 typedef variant case. */
27145 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27146
27147 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27148 it first before we can figure out what NAME refers to. */
27149 if (TREE_CODE (scope) == TYPENAME_TYPE)
27150 {
27151 if (TYPENAME_IS_RESOLVING_P (scope))
27152 /* Given a class template A with a dependent base with nested type C,
27153 typedef typename A::C::C C will land us here, as trying to resolve
27154 the initial A::C leads to the local C typedef, which leads back to
27155 A::C::C. So we break the recursion now. */
27156 return type;
27157 else
27158 scope = resolve_typename_type (scope, only_current_p);
27159 }
27160 /* If we don't know what SCOPE refers to, then we cannot resolve the
27161 TYPENAME_TYPE. */
27162 if (!CLASS_TYPE_P (scope))
27163 return type;
27164 /* If this is a typedef, we don't want to look inside (c++/11987). */
27165 if (typedef_variant_p (type))
27166 return type;
27167 /* If SCOPE isn't the template itself, it will not have a valid
27168 TYPE_FIELDS list. */
27169 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27170 /* scope is either the template itself or a compatible instantiation
27171 like X<T>, so look up the name in the original template. */
27172 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27173 /* If scope has no fields, it can't be a current instantiation. Check this
27174 before currently_open_class to avoid infinite recursion (71515). */
27175 if (!TYPE_FIELDS (scope))
27176 return type;
27177 /* If the SCOPE is not the current instantiation, there's no reason
27178 to look inside it. */
27179 if (only_current_p && !currently_open_class (scope))
27180 return type;
27181 /* Enter the SCOPE so that name lookup will be resolved as if we
27182 were in the class definition. In particular, SCOPE will no
27183 longer be considered a dependent type. */
27184 pushed_scope = push_scope (scope);
27185 /* Look up the declaration. */
27186 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27187 tf_warning_or_error);
27188
27189 result = NULL_TREE;
27190
27191 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27192 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27193 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27194 if (!decl)
27195 /*nop*/;
27196 else if (identifier_p (fullname)
27197 && TREE_CODE (decl) == TYPE_DECL)
27198 {
27199 result = TREE_TYPE (decl);
27200 if (result == error_mark_node)
27201 result = NULL_TREE;
27202 }
27203 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27204 && DECL_CLASS_TEMPLATE_P (decl))
27205 {
27206 /* Obtain the template and the arguments. */
27207 tree tmpl = TREE_OPERAND (fullname, 0);
27208 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27209 {
27210 /* We get here with a plain identifier because a previous tentative
27211 parse of the nested-name-specifier as part of a ptr-operator saw
27212 ::template X<A>. The use of ::template is necessary in a
27213 ptr-operator, but wrong in a declarator-id.
27214
27215 [temp.names]: In a qualified-id of a declarator-id, the keyword
27216 template shall not appear at the top level. */
27217 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27218 "keyword %<template%> not allowed in declarator-id");
27219 tmpl = decl;
27220 }
27221 tree args = TREE_OPERAND (fullname, 1);
27222 /* Instantiate the template. */
27223 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27224 /*entering_scope=*/true,
27225 tf_error | tf_user);
27226 if (result == error_mark_node)
27227 result = NULL_TREE;
27228 }
27229
27230 /* Leave the SCOPE. */
27231 if (pushed_scope)
27232 pop_scope (pushed_scope);
27233
27234 /* If we failed to resolve it, return the original typename. */
27235 if (!result)
27236 return type;
27237
27238 /* If lookup found a typename type, resolve that too. */
27239 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27240 {
27241 /* Ill-formed programs can cause infinite recursion here, so we
27242 must catch that. */
27243 TYPENAME_IS_RESOLVING_P (result) = 1;
27244 result = resolve_typename_type (result, only_current_p);
27245 TYPENAME_IS_RESOLVING_P (result) = 0;
27246 }
27247
27248 /* Qualify the resulting type. */
27249 quals = cp_type_quals (type);
27250 if (quals)
27251 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27252
27253 return result;
27254 }
27255
27256 /* EXPR is an expression which is not type-dependent. Return a proxy
27257 for EXPR that can be used to compute the types of larger
27258 expressions containing EXPR. */
27259
27260 tree
27261 build_non_dependent_expr (tree expr)
27262 {
27263 tree orig_expr = expr;
27264 tree inner_expr;
27265
27266 /* When checking, try to get a constant value for all non-dependent
27267 expressions in order to expose bugs in *_dependent_expression_p
27268 and constexpr. This can affect code generation, see PR70704, so
27269 only do this for -fchecking=2. */
27270 if (flag_checking > 1
27271 && cxx_dialect >= cxx11
27272 /* Don't do this during nsdmi parsing as it can lead to
27273 unexpected recursive instantiations. */
27274 && !parsing_nsdmi ()
27275 /* Don't do this during concept processing either and for
27276 the same reason. */
27277 && !processing_constraint_expression_p ())
27278 fold_non_dependent_expr (expr, tf_none);
27279
27280 STRIP_ANY_LOCATION_WRAPPER (expr);
27281
27282 /* Preserve OVERLOADs; the functions must be available to resolve
27283 types. */
27284 inner_expr = expr;
27285 if (TREE_CODE (inner_expr) == STMT_EXPR)
27286 inner_expr = stmt_expr_value_expr (inner_expr);
27287 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27288 inner_expr = TREE_OPERAND (inner_expr, 0);
27289 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27290 inner_expr = TREE_OPERAND (inner_expr, 1);
27291 if (is_overloaded_fn (inner_expr)
27292 || TREE_CODE (inner_expr) == OFFSET_REF)
27293 return orig_expr;
27294 /* There is no need to return a proxy for a variable or enumerator. */
27295 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27296 return orig_expr;
27297 /* Preserve string constants; conversions from string constants to
27298 "char *" are allowed, even though normally a "const char *"
27299 cannot be used to initialize a "char *". */
27300 if (TREE_CODE (expr) == STRING_CST)
27301 return orig_expr;
27302 /* Preserve void and arithmetic constants, as an optimization -- there is no
27303 reason to create a new node. */
27304 if (TREE_CODE (expr) == VOID_CST
27305 || TREE_CODE (expr) == INTEGER_CST
27306 || TREE_CODE (expr) == REAL_CST)
27307 return orig_expr;
27308 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27309 There is at least one place where we want to know that a
27310 particular expression is a throw-expression: when checking a ?:
27311 expression, there are special rules if the second or third
27312 argument is a throw-expression. */
27313 if (TREE_CODE (expr) == THROW_EXPR)
27314 return orig_expr;
27315
27316 /* Don't wrap an initializer list, we need to be able to look inside. */
27317 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27318 return orig_expr;
27319
27320 /* Don't wrap a dummy object, we need to be able to test for it. */
27321 if (is_dummy_object (expr))
27322 return orig_expr;
27323
27324 if (TREE_CODE (expr) == COND_EXPR)
27325 return build3 (COND_EXPR,
27326 TREE_TYPE (expr),
27327 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27328 (TREE_OPERAND (expr, 1)
27329 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27330 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27331 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27332 if (TREE_CODE (expr) == COMPOUND_EXPR
27333 && !COMPOUND_EXPR_OVERLOADED (expr))
27334 return build2 (COMPOUND_EXPR,
27335 TREE_TYPE (expr),
27336 TREE_OPERAND (expr, 0),
27337 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27338
27339 /* If the type is unknown, it can't really be non-dependent */
27340 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27341
27342 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27343 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27344 TREE_TYPE (expr), expr);
27345 }
27346
27347 /* ARGS is a vector of expressions as arguments to a function call.
27348 Replace the arguments with equivalent non-dependent expressions.
27349 This modifies ARGS in place. */
27350
27351 void
27352 make_args_non_dependent (vec<tree, va_gc> *args)
27353 {
27354 unsigned int ix;
27355 tree arg;
27356
27357 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27358 {
27359 tree newarg = build_non_dependent_expr (arg);
27360 if (newarg != arg)
27361 (*args)[ix] = newarg;
27362 }
27363 }
27364
27365 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27366 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27367 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27368
27369 static tree
27370 make_auto_1 (tree name, bool set_canonical)
27371 {
27372 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27373 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27374 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27375 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27376 (0, processing_template_decl + 1, processing_template_decl + 1,
27377 TYPE_NAME (au), NULL_TREE);
27378 if (set_canonical)
27379 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27380 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27381 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27382
27383 return au;
27384 }
27385
27386 tree
27387 make_decltype_auto (void)
27388 {
27389 return make_auto_1 (decltype_auto_identifier, true);
27390 }
27391
27392 tree
27393 make_auto (void)
27394 {
27395 return make_auto_1 (auto_identifier, true);
27396 }
27397
27398 /* Return a C++17 deduction placeholder for class template TMPL. */
27399
27400 tree
27401 make_template_placeholder (tree tmpl)
27402 {
27403 tree t = make_auto_1 (auto_identifier, false);
27404 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27405 /* Our canonical type depends on the placeholder. */
27406 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27407 return t;
27408 }
27409
27410 /* True iff T is a C++17 class template deduction placeholder. */
27411
27412 bool
27413 template_placeholder_p (tree t)
27414 {
27415 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27416 }
27417
27418 /* Make a "constrained auto" type-specifier. This is an auto or
27419 decltype(auto) type with constraints that must be associated after
27420 deduction. The constraint is formed from the given concept CON
27421 and its optional sequence of template arguments ARGS.
27422
27423 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27424
27425 static tree
27426 make_constrained_placeholder_type (tree type, tree con, tree args)
27427 {
27428 /* Build the constraint. */
27429 tree tmpl = DECL_TI_TEMPLATE (con);
27430 tree expr = tmpl;
27431 if (TREE_CODE (con) == FUNCTION_DECL)
27432 expr = ovl_make (tmpl);
27433 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27434
27435 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27436
27437 /* Our canonical type depends on the constraint. */
27438 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27439
27440 /* Attach the constraint to the type declaration. */
27441 return TYPE_NAME (type);
27442 }
27443
27444 /* Make a "constrained auto" type-specifier. */
27445
27446 tree
27447 make_constrained_auto (tree con, tree args)
27448 {
27449 tree type = make_auto_1 (auto_identifier, false);
27450 return make_constrained_placeholder_type (type, con, args);
27451 }
27452
27453 /* Make a "constrained decltype(auto)" type-specifier. */
27454
27455 tree
27456 make_constrained_decltype_auto (tree con, tree args)
27457 {
27458 tree type = make_auto_1 (decltype_auto_identifier, false);
27459 /* FIXME: I don't know why this isn't done in make_auto_1. */
27460 AUTO_IS_DECLTYPE (type) = true;
27461 return make_constrained_placeholder_type (type, con, args);
27462 }
27463
27464 /* Build and return a concept definition. Like other templates, the
27465 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27466 the TEMPLATE_DECL. */
27467
27468 tree
27469 finish_concept_definition (cp_expr id, tree init)
27470 {
27471 gcc_assert (identifier_p (id));
27472 gcc_assert (processing_template_decl);
27473
27474 location_t loc = id.get_location();
27475
27476 /* A concept-definition shall not have associated constraints. */
27477 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27478 {
27479 error_at (loc, "a concept cannot be constrained");
27480 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27481 }
27482
27483 /* A concept-definition shall appear in namespace scope. Templates
27484 aren't allowed in block scope, so we only need to check for class
27485 scope. */
27486 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27487 {
27488 error_at (loc, "concept %qE not in namespace scope", *id);
27489 return error_mark_node;
27490 }
27491
27492 /* Initially build the concept declaration; it's type is bool. */
27493 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27494 DECL_CONTEXT (decl) = current_scope ();
27495 DECL_INITIAL (decl) = init;
27496
27497 /* Push the enclosing template. */
27498 return push_template_decl (decl);
27499 }
27500
27501 /* Given type ARG, return std::initializer_list<ARG>. */
27502
27503 static tree
27504 listify (tree arg)
27505 {
27506 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27507
27508 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27509 {
27510 gcc_rich_location richloc (input_location);
27511 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27512 error_at (&richloc,
27513 "deducing from brace-enclosed initializer list"
27514 " requires %<#include <initializer_list>%>");
27515
27516 return error_mark_node;
27517 }
27518 tree argvec = make_tree_vec (1);
27519 TREE_VEC_ELT (argvec, 0) = arg;
27520
27521 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27522 NULL_TREE, 0, tf_warning_or_error);
27523 }
27524
27525 /* Replace auto in TYPE with std::initializer_list<auto>. */
27526
27527 static tree
27528 listify_autos (tree type, tree auto_node)
27529 {
27530 tree init_auto = listify (strip_top_quals (auto_node));
27531 tree argvec = make_tree_vec (1);
27532 TREE_VEC_ELT (argvec, 0) = init_auto;
27533 if (processing_template_decl)
27534 argvec = add_to_template_args (current_template_args (), argvec);
27535 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27536 }
27537
27538 /* Hash traits for hashing possibly constrained 'auto'
27539 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27540
27541 struct auto_hash : default_hash_traits<tree>
27542 {
27543 static inline hashval_t hash (tree);
27544 static inline bool equal (tree, tree);
27545 };
27546
27547 /* Hash the 'auto' T. */
27548
27549 inline hashval_t
27550 auto_hash::hash (tree t)
27551 {
27552 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27553 /* Matching constrained-type-specifiers denote the same template
27554 parameter, so hash the constraint. */
27555 return hash_placeholder_constraint (c);
27556 else
27557 /* But unconstrained autos are all separate, so just hash the pointer. */
27558 return iterative_hash_object (t, 0);
27559 }
27560
27561 /* Compare two 'auto's. */
27562
27563 inline bool
27564 auto_hash::equal (tree t1, tree t2)
27565 {
27566 if (t1 == t2)
27567 return true;
27568
27569 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
27570 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
27571
27572 /* Two unconstrained autos are distinct. */
27573 if (!c1 || !c2)
27574 return false;
27575
27576 return equivalent_placeholder_constraints (c1, c2);
27577 }
27578
27579 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27580 constrained) auto, add it to the vector. */
27581
27582 static int
27583 extract_autos_r (tree t, void *data)
27584 {
27585 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
27586 if (is_auto (t))
27587 {
27588 /* All the autos were built with index 0; fix that up now. */
27589 tree *p = hash.find_slot (t, INSERT);
27590 unsigned idx;
27591 if (*p)
27592 /* If this is a repeated constrained-type-specifier, use the index we
27593 chose before. */
27594 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
27595 else
27596 {
27597 /* Otherwise this is new, so use the current count. */
27598 *p = t;
27599 idx = hash.elements () - 1;
27600 }
27601 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
27602 }
27603
27604 /* Always keep walking. */
27605 return 0;
27606 }
27607
27608 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27609 says they can appear anywhere in the type. */
27610
27611 static tree
27612 extract_autos (tree type)
27613 {
27614 hash_set<tree> visited;
27615 hash_table<auto_hash> hash (2);
27616
27617 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
27618
27619 tree tree_vec = make_tree_vec (hash.elements());
27620 for (hash_table<auto_hash>::iterator iter = hash.begin();
27621 iter != hash.end(); ++iter)
27622 {
27623 tree elt = *iter;
27624 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
27625 TREE_VEC_ELT (tree_vec, i)
27626 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
27627 }
27628
27629 return tree_vec;
27630 }
27631
27632 /* The stem for deduction guide names. */
27633 const char *const dguide_base = "__dguide_";
27634
27635 /* Return the name for a deduction guide for class template TMPL. */
27636
27637 tree
27638 dguide_name (tree tmpl)
27639 {
27640 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
27641 tree tname = TYPE_IDENTIFIER (type);
27642 char *buf = (char *) alloca (1 + strlen (dguide_base)
27643 + IDENTIFIER_LENGTH (tname));
27644 memcpy (buf, dguide_base, strlen (dguide_base));
27645 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
27646 IDENTIFIER_LENGTH (tname) + 1);
27647 tree dname = get_identifier (buf);
27648 TREE_TYPE (dname) = type;
27649 return dname;
27650 }
27651
27652 /* True if NAME is the name of a deduction guide. */
27653
27654 bool
27655 dguide_name_p (tree name)
27656 {
27657 return (TREE_CODE (name) == IDENTIFIER_NODE
27658 && TREE_TYPE (name)
27659 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
27660 strlen (dguide_base)));
27661 }
27662
27663 /* True if FN is a deduction guide. */
27664
27665 bool
27666 deduction_guide_p (const_tree fn)
27667 {
27668 if (DECL_P (fn))
27669 if (tree name = DECL_NAME (fn))
27670 return dguide_name_p (name);
27671 return false;
27672 }
27673
27674 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27675
27676 bool
27677 copy_guide_p (const_tree fn)
27678 {
27679 gcc_assert (deduction_guide_p (fn));
27680 if (!DECL_ARTIFICIAL (fn))
27681 return false;
27682 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27683 return (TREE_CHAIN (parms) == void_list_node
27684 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27685 }
27686
27687 /* True if FN is a guide generated from a constructor template. */
27688
27689 bool
27690 template_guide_p (const_tree fn)
27691 {
27692 gcc_assert (deduction_guide_p (fn));
27693 if (!DECL_ARTIFICIAL (fn))
27694 return false;
27695 tree tmpl = DECL_TI_TEMPLATE (fn);
27696 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27697 return PRIMARY_TEMPLATE_P (org);
27698 return false;
27699 }
27700
27701 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27702 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27703 template parameter types. Note that the handling of template template
27704 parameters relies on current_template_parms being set appropriately for the
27705 new template. */
27706
27707 static tree
27708 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27709 tree tsubst_args, tsubst_flags_t complain)
27710 {
27711 if (olddecl == error_mark_node)
27712 return error_mark_node;
27713
27714 tree oldidx = get_template_parm_index (olddecl);
27715
27716 tree newtype;
27717 if (TREE_CODE (olddecl) == TYPE_DECL
27718 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27719 {
27720 tree oldtype = TREE_TYPE (olddecl);
27721 newtype = cxx_make_type (TREE_CODE (oldtype));
27722 TYPE_MAIN_VARIANT (newtype) = newtype;
27723 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27724 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27725 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27726 }
27727 else
27728 {
27729 newtype = TREE_TYPE (olddecl);
27730 if (type_uses_auto (newtype))
27731 {
27732 // Substitute once to fix references to other template parameters.
27733 newtype = tsubst (newtype, tsubst_args,
27734 complain|tf_partial, NULL_TREE);
27735 // Now substitute again to reduce the level of the auto.
27736 newtype = tsubst (newtype, current_template_args (),
27737 complain, NULL_TREE);
27738 }
27739 else
27740 newtype = tsubst (newtype, tsubst_args,
27741 complain, NULL_TREE);
27742 }
27743
27744 tree newdecl
27745 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27746 DECL_NAME (olddecl), newtype);
27747 SET_DECL_TEMPLATE_PARM_P (newdecl);
27748
27749 tree newidx;
27750 if (TREE_CODE (olddecl) == TYPE_DECL
27751 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27752 {
27753 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27754 = build_template_parm_index (index, level, level,
27755 newdecl, newtype);
27756 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27757 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27758 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27759 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27760
27761 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27762 {
27763 DECL_TEMPLATE_RESULT (newdecl)
27764 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27765 DECL_NAME (olddecl), newtype);
27766 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27767 // First create a copy (ttargs) of tsubst_args with an
27768 // additional level for the template template parameter's own
27769 // template parameters (ttparms).
27770 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27771 (DECL_TEMPLATE_PARMS (olddecl)));
27772 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27773 tree ttargs = make_tree_vec (depth + 1);
27774 for (int i = 0; i < depth; ++i)
27775 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27776 TREE_VEC_ELT (ttargs, depth)
27777 = template_parms_level_to_args (ttparms);
27778 // Substitute ttargs into ttparms to fix references to
27779 // other template parameters.
27780 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27781 complain|tf_partial);
27782 // Now substitute again with args based on tparms, to reduce
27783 // the level of the ttparms.
27784 ttargs = current_template_args ();
27785 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27786 complain);
27787 // Finally, tack the adjusted parms onto tparms.
27788 ttparms = tree_cons (size_int (depth), ttparms,
27789 current_template_parms);
27790 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27791 }
27792 }
27793 else
27794 {
27795 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27796 tree newconst
27797 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27798 TREE_CODE (oldconst),
27799 DECL_NAME (oldconst), newtype);
27800 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27801 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27802 SET_DECL_TEMPLATE_PARM_P (newconst);
27803 newidx = build_template_parm_index (index, level, level,
27804 newconst, newtype);
27805 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27806 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27807 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27808 }
27809
27810 return newdecl;
27811 }
27812
27813 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
27814 template parameter. */
27815
27816 static tree
27817 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
27818 tree targs, unsigned targs_index, tsubst_flags_t complain)
27819 {
27820 tree olddecl = TREE_VALUE (oldelt);
27821 tree newdecl = rewrite_template_parm (olddecl, index, level,
27822 targs, complain);
27823 if (newdecl == error_mark_node)
27824 return error_mark_node;
27825 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27826 targs, complain, NULL_TREE);
27827 tree list = build_tree_list (newdef, newdecl);
27828 TEMPLATE_PARM_CONSTRAINTS (list)
27829 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27830 targs, complain, NULL_TREE);
27831 int depth = TMPL_ARGS_DEPTH (targs);
27832 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
27833 return list;
27834 }
27835
27836 /* Returns a C++17 class deduction guide template based on the constructor
27837 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27838 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
27839 aggregate initialization guide. */
27840
27841 static tree
27842 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
27843 {
27844 tree tparms, targs, fparms, fargs, ci;
27845 bool memtmpl = false;
27846 bool explicit_p;
27847 location_t loc;
27848 tree fn_tmpl = NULL_TREE;
27849
27850 if (outer_args)
27851 {
27852 ++processing_template_decl;
27853 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
27854 --processing_template_decl;
27855 }
27856
27857 if (!DECL_DECLARES_FUNCTION_P (ctor))
27858 {
27859 if (TYPE_P (ctor))
27860 {
27861 bool copy_p = TYPE_REF_P (ctor);
27862 if (copy_p)
27863 fparms = tree_cons (NULL_TREE, type, void_list_node);
27864 else
27865 fparms = void_list_node;
27866 }
27867 else if (TREE_CODE (ctor) == TREE_LIST)
27868 fparms = ctor;
27869 else
27870 gcc_unreachable ();
27871
27872 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27873 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27874 targs = CLASSTYPE_TI_ARGS (type);
27875 ci = NULL_TREE;
27876 fargs = NULL_TREE;
27877 loc = DECL_SOURCE_LOCATION (ctmpl);
27878 explicit_p = false;
27879 }
27880 else
27881 {
27882 ++processing_template_decl;
27883 bool ok = true;
27884
27885 fn_tmpl
27886 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27887 : DECL_TI_TEMPLATE (ctor));
27888 if (outer_args)
27889 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27890 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27891
27892 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27893 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27894 fully specialized args for the enclosing class. Strip those off, as
27895 the deduction guide won't have those template parameters. */
27896 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27897 TMPL_PARMS_DEPTH (tparms));
27898 /* Discard the 'this' parameter. */
27899 fparms = FUNCTION_ARG_CHAIN (ctor);
27900 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27901 ci = get_constraints (ctor);
27902 loc = DECL_SOURCE_LOCATION (ctor);
27903 explicit_p = DECL_NONCONVERTING_P (ctor);
27904
27905 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27906 {
27907 memtmpl = true;
27908
27909 /* For a member template constructor, we need to flatten the two
27910 template parameter lists into one, and then adjust the function
27911 signature accordingly. This gets...complicated. */
27912 tree save_parms = current_template_parms;
27913
27914 /* For a member template we should have two levels of parms/args, one
27915 for the class and one for the constructor. We stripped
27916 specialized args for further enclosing classes above. */
27917 const int depth = 2;
27918 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27919
27920 /* Template args for translating references to the two-level template
27921 parameters into references to the one-level template parameters we
27922 are creating. */
27923 tree tsubst_args = copy_node (targs);
27924 TMPL_ARGS_LEVEL (tsubst_args, depth)
27925 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27926
27927 /* Template parms for the constructor template. */
27928 tree ftparms = TREE_VALUE (tparms);
27929 unsigned flen = TREE_VEC_LENGTH (ftparms);
27930 /* Template parms for the class template. */
27931 tparms = TREE_CHAIN (tparms);
27932 tree ctparms = TREE_VALUE (tparms);
27933 unsigned clen = TREE_VEC_LENGTH (ctparms);
27934 /* Template parms for the deduction guide start as a copy of the
27935 template parms for the class. We set current_template_parms for
27936 lookup_template_class_1. */
27937 current_template_parms = tparms = copy_node (tparms);
27938 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27939 for (unsigned i = 0; i < clen; ++i)
27940 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27941
27942 /* Now we need to rewrite the constructor parms to append them to the
27943 class parms. */
27944 for (unsigned i = 0; i < flen; ++i)
27945 {
27946 unsigned index = i + clen;
27947 unsigned level = 1;
27948 tree oldelt = TREE_VEC_ELT (ftparms, i);
27949 tree newelt
27950 = rewrite_tparm_list (oldelt, index, level,
27951 tsubst_args, i, complain);
27952 if (newelt == error_mark_node)
27953 ok = false;
27954 TREE_VEC_ELT (new_vec, index) = newelt;
27955 }
27956
27957 /* Now we have a final set of template parms to substitute into the
27958 function signature. */
27959 targs = template_parms_to_args (tparms);
27960 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27961 complain, ctor);
27962 if (fparms == error_mark_node)
27963 ok = false;
27964 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27965 if (ci)
27966 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27967
27968 current_template_parms = save_parms;
27969 }
27970
27971 --processing_template_decl;
27972 if (!ok)
27973 return error_mark_node;
27974 }
27975
27976 if (!memtmpl)
27977 {
27978 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27979 tparms = copy_node (tparms);
27980 INNERMOST_TEMPLATE_PARMS (tparms)
27981 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27982 }
27983
27984 tree fntype = build_function_type (type, fparms);
27985 tree ded_fn = build_lang_decl_loc (loc,
27986 FUNCTION_DECL,
27987 dguide_name (type), fntype);
27988 DECL_ARGUMENTS (ded_fn) = fargs;
27989 DECL_ARTIFICIAL (ded_fn) = true;
27990 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27991 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27992 DECL_ARTIFICIAL (ded_tmpl) = true;
27993 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27994 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27995 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27996 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27997 if (DECL_P (ctor))
27998 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27999 if (ci)
28000 set_constraints (ded_tmpl, ci);
28001
28002 return ded_tmpl;
28003 }
28004
28005 /* Add to LIST the member types for the reshaped initializer CTOR. */
28006
28007 static tree
28008 collect_ctor_idx_types (tree ctor, tree list)
28009 {
28010 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28011 tree idx, val; unsigned i;
28012 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28013 {
28014 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28015 && CONSTRUCTOR_NELTS (val))
28016 if (tree subidx = CONSTRUCTOR_ELT (val, 0)->index)
28017 if (TREE_CODE (subidx) == FIELD_DECL)
28018 {
28019 list = collect_ctor_idx_types (val, list);
28020 continue;
28021 }
28022 tree ftype = finish_decltype_type (idx, true, tf_none);
28023 list = tree_cons (NULL_TREE, ftype, list);
28024 }
28025
28026 return list;
28027 }
28028
28029 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28030
28031 static bool
28032 is_spec_or_derived (tree etype, tree tmpl)
28033 {
28034 if (!etype || !CLASS_TYPE_P (etype))
28035 return false;
28036
28037 tree type = TREE_TYPE (tmpl);
28038 tree tparms = (INNERMOST_TEMPLATE_PARMS
28039 (DECL_TEMPLATE_PARMS (tmpl)));
28040 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28041 int err = unify (tparms, targs, type, etype,
28042 UNIFY_ALLOW_DERIVED, /*explain*/false);
28043 ggc_free (targs);
28044 return !err;
28045 }
28046
28047 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28048 INIT. */
28049
28050 static tree
28051 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28052 {
28053 if (cxx_dialect < cxx2a)
28054 return NULL_TREE;
28055
28056 if (init == NULL_TREE)
28057 return NULL_TREE;
28058
28059 tree type = TREE_TYPE (tmpl);
28060 if (!CP_AGGREGATE_TYPE_P (type))
28061 return NULL_TREE;
28062
28063 /* No aggregate candidate for copy-initialization. */
28064 if (args->length() == 1)
28065 {
28066 tree val = (*args)[0];
28067 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28068 return NULL_TREE;
28069 }
28070
28071 /* If we encounter a problem, we just won't add the candidate. */
28072 tsubst_flags_t complain = tf_none;
28073
28074 tree parms = NULL_TREE;
28075 if (TREE_CODE (init) == CONSTRUCTOR)
28076 {
28077 init = reshape_init (type, init, complain);
28078 if (init == error_mark_node)
28079 return NULL_TREE;
28080 parms = collect_ctor_idx_types (init, parms);
28081 }
28082 else if (TREE_CODE (init) == TREE_LIST)
28083 {
28084 int len = list_length (init);
28085 for (tree field = TYPE_FIELDS (type);
28086 len;
28087 --len, field = DECL_CHAIN (field))
28088 {
28089 field = next_initializable_field (field);
28090 if (!field)
28091 return NULL_TREE;
28092 tree ftype = finish_decltype_type (field, true, complain);
28093 parms = tree_cons (NULL_TREE, ftype, parms);
28094 }
28095 }
28096 else
28097 /* Aggregate initialization doesn't apply to an initializer expression. */
28098 return NULL_TREE;
28099
28100 if (parms)
28101 {
28102 tree last = parms;
28103 parms = nreverse (parms);
28104 TREE_CHAIN (last) = void_list_node;
28105 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28106 return guide;
28107 }
28108
28109 return NULL_TREE;
28110 }
28111
28112 /* UGUIDES are the deduction guides for the underlying template of alias
28113 template TMPL; adjust them to be deduction guides for TMPL. */
28114
28115 static tree
28116 alias_ctad_tweaks (tree tmpl, tree uguides)
28117 {
28118 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28119 class type (9.2.8.2) where the template-name names an alias template A,
28120 the defining-type-id of A must be of the form
28121
28122 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28123
28124 as specified in 9.2.8.2. The guides of A are the set of functions or
28125 function templates formed as follows. For each function or function
28126 template f in the guides of the template named by the simple-template-id
28127 of the defining-type-id, the template arguments of the return type of f
28128 are deduced from the defining-type-id of A according to the process in
28129 13.10.2.5 with the exception that deduction does not fail if not all
28130 template arguments are deduced. Let g denote the result of substituting
28131 these deductions into f. If substitution succeeds, form a function or
28132 function template f' with the following properties and add it to the set
28133 of guides of A:
28134
28135 * The function type of f' is the function type of g.
28136
28137 * If f is a function template, f' is a function template whose template
28138 parameter list consists of all the template parameters of A (including
28139 their default template arguments) that appear in the above deductions or
28140 (recursively) in their default template arguments, followed by the
28141 template parameters of f that were not deduced (including their default
28142 template arguments), otherwise f' is not a function template.
28143
28144 * The associated constraints (13.5.2) are the conjunction of the
28145 associated constraints of g and a constraint that is satisfied if and only
28146 if the arguments of A are deducible (see below) from the return type.
28147
28148 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28149 be so as well.
28150
28151 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28152 considered to be so as well.
28153
28154 * The explicit-specifier of f' is the explicit-specifier of g (if
28155 any). */
28156
28157 /* This implementation differs from the above in two significant ways:
28158
28159 1) We include all template parameters of A, not just some.
28160 2) The added constraint is same_type instead of deducible.
28161
28162 I believe that while it's probably possible to construct a testcase that
28163 behaves differently with this simplification, it should have the same
28164 effect for real uses. Including all template parameters means that we
28165 deduce all parameters of A when resolving the call, so when we're in the
28166 constraint we don't need to deduce them again, we can just check whether
28167 the deduction produced the desired result. */
28168
28169 tsubst_flags_t complain = tf_warning_or_error;
28170 tree atype = TREE_TYPE (tmpl);
28171 tree aguides = NULL_TREE;
28172 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28173 unsigned natparms = TREE_VEC_LENGTH (atparms);
28174 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28175 for (ovl_iterator iter (uguides); iter; ++iter)
28176 {
28177 tree f = *iter;
28178 tree in_decl = f;
28179 location_t loc = DECL_SOURCE_LOCATION (f);
28180 tree ret = TREE_TYPE (TREE_TYPE (f));
28181 tree fprime = f;
28182 if (TREE_CODE (f) == TEMPLATE_DECL)
28183 {
28184 processing_template_decl_sentinel ptds (/*reset*/false);
28185 ++processing_template_decl;
28186
28187 /* Deduce template arguments for f from the type-id of A. */
28188 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28189 unsigned len = TREE_VEC_LENGTH (ftparms);
28190 tree targs = make_tree_vec (len);
28191 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28192 gcc_assert (!err);
28193
28194 /* The number of parms for f' is the number of parms for A plus
28195 non-deduced parms of f. */
28196 unsigned ndlen = 0;
28197 unsigned j;
28198 for (unsigned i = 0; i < len; ++i)
28199 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28200 ++ndlen;
28201 tree gtparms = make_tree_vec (natparms + ndlen);
28202
28203 /* First copy over the parms of A. */
28204 for (j = 0; j < natparms; ++j)
28205 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28206 /* Now rewrite the non-deduced parms of f. */
28207 for (unsigned i = 0; ndlen && i < len; ++i)
28208 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28209 {
28210 --ndlen;
28211 unsigned index = j++;
28212 unsigned level = 1;
28213 tree oldlist = TREE_VEC_ELT (ftparms, i);
28214 tree list = rewrite_tparm_list (oldlist, index, level,
28215 targs, i, complain);
28216 TREE_VEC_ELT (gtparms, index) = list;
28217 }
28218 gtparms = build_tree_list (size_one_node, gtparms);
28219
28220 /* Substitute the deduced arguments plus the rewritten template
28221 parameters into f to get g. This covers the type, copyness,
28222 guideness, and explicit-specifier. */
28223 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28224 if (g == error_mark_node)
28225 return error_mark_node;
28226 DECL_USE_TEMPLATE (g) = 0;
28227 fprime = build_template_decl (g, gtparms, false);
28228 DECL_TEMPLATE_RESULT (fprime) = g;
28229 TREE_TYPE (fprime) = TREE_TYPE (g);
28230 tree gtargs = template_parms_to_args (gtparms);
28231 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28232 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28233
28234 /* Substitute the associated constraints. */
28235 tree ci = get_constraints (f);
28236 if (ci)
28237 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28238 if (ci == error_mark_node)
28239 return error_mark_node;
28240
28241 /* Add a constraint that the return type matches the instantiation of
28242 A with the same template arguments. */
28243 ret = TREE_TYPE (TREE_TYPE (fprime));
28244 if (!same_type_p (atype, ret)
28245 /* FIXME this should mean they don't compare as equivalent. */
28246 || dependent_alias_template_spec_p (atype, nt_opaque))
28247 {
28248 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28249 ci = append_constraint (ci, same);
28250 }
28251
28252 if (ci)
28253 set_constraints (fprime, ci);
28254 }
28255 else
28256 {
28257 /* For a non-template deduction guide, if the arguments of A aren't
28258 deducible from the return type, don't add the candidate. */
28259 tree targs = make_tree_vec (natparms);
28260 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28261 for (unsigned i = 0; !err && i < natparms; ++i)
28262 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28263 err = true;
28264 if (err)
28265 continue;
28266 }
28267
28268 aguides = lookup_add (fprime, aguides);
28269 }
28270
28271 return aguides;
28272 }
28273
28274 /* Return artificial deduction guides built from the constructors of class
28275 template TMPL. */
28276
28277 static tree
28278 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28279 {
28280 tree type = TREE_TYPE (tmpl);
28281 tree outer_args = NULL_TREE;
28282 if (DECL_CLASS_SCOPE_P (tmpl)
28283 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28284 {
28285 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28286 type = TREE_TYPE (most_general_template (tmpl));
28287 }
28288
28289 tree cands = NULL_TREE;
28290
28291 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28292 {
28293 /* Skip inherited constructors. */
28294 if (iter.using_p ())
28295 continue;
28296
28297 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28298 cands = lookup_add (guide, cands);
28299 }
28300
28301 /* Add implicit default constructor deduction guide. */
28302 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28303 {
28304 tree guide = build_deduction_guide (type, type, outer_args,
28305 complain);
28306 cands = lookup_add (guide, cands);
28307 }
28308
28309 /* Add copy guide. */
28310 {
28311 tree gtype = build_reference_type (type);
28312 tree guide = build_deduction_guide (type, gtype, outer_args,
28313 complain);
28314 cands = lookup_add (guide, cands);
28315 }
28316
28317 return cands;
28318 }
28319
28320 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28321
28322 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28323 aggregate candidate is added separately because it depends on the
28324 initializer. */
28325
28326 static tree
28327 deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28328 {
28329 tree guides = NULL_TREE;
28330 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28331 {
28332 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28333 tree tinfo = get_template_info (under);
28334 guides = deduction_guides_for (TI_TEMPLATE (tinfo), complain);
28335 }
28336 else
28337 {
28338 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28339 dguide_name (tmpl),
28340 /*type*/false, /*complain*/false,
28341 /*hidden*/false);
28342 if (guides == error_mark_node)
28343 guides = NULL_TREE;
28344 }
28345
28346 /* Cache the deduction guides for a template. We also remember the result of
28347 lookup, and rebuild everything if it changes; should be very rare. */
28348 tree_pair_p cache = NULL;
28349 if (tree_pair_p &r
28350 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28351 {
28352 cache = r;
28353 if (cache->purpose == guides)
28354 return cache->value;
28355 }
28356 else
28357 {
28358 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28359 cache->purpose = guides;
28360 }
28361
28362 tree cands = NULL_TREE;
28363 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28364 cands = alias_ctad_tweaks (tmpl, guides);
28365 else
28366 {
28367 cands = ctor_deduction_guides_for (tmpl, complain);
28368 for (ovl_iterator it (guides); it; ++it)
28369 cands = lookup_add (*it, cands);
28370 }
28371
28372 cache->value = cands;
28373 return cands;
28374 }
28375
28376 /* Return whether TMPL is a (class template argument-) deducible template. */
28377
28378 bool
28379 ctad_template_p (tree tmpl)
28380 {
28381 /* A deducible template is either a class template or is an alias template
28382 whose defining-type-id is of the form
28383
28384 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28385
28386 where the nested-name-specifier (if any) is non-dependent and the
28387 template-name of the simple-template-id names a deducible template. */
28388
28389 if (DECL_CLASS_TEMPLATE_P (tmpl)
28390 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28391 return true;
28392 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28393 return false;
28394 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28395 if (tree tinfo = get_template_info (orig))
28396 return ctad_template_p (TI_TEMPLATE (tinfo));
28397 return false;
28398 }
28399
28400 /* Deduce template arguments for the class template placeholder PTYPE for
28401 template TMPL based on the initializer INIT, and return the resulting
28402 type. */
28403
28404 static tree
28405 do_class_deduction (tree ptype, tree tmpl, tree init,
28406 int flags, tsubst_flags_t complain)
28407 {
28408 /* We should have handled this in the caller. */
28409 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28410 return ptype;
28411
28412 /* Look through alias templates that just rename another template. */
28413 tmpl = get_underlying_template (tmpl);
28414 if (!ctad_template_p (tmpl))
28415 {
28416 if (complain & tf_error)
28417 error ("non-deducible template %qT used without template arguments", tmpl);
28418 return error_mark_node;
28419 }
28420 else if (cxx_dialect < cxx2a && DECL_ALIAS_TEMPLATE_P (tmpl))
28421 {
28422 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28423 if (complain & tf_warning_or_error)
28424 pedwarn (input_location, 0, "alias template deduction only available "
28425 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
28426 }
28427
28428 if (init && TREE_TYPE (init) == ptype)
28429 /* Using the template parm as its own argument. */
28430 return ptype;
28431
28432 tree type = TREE_TYPE (tmpl);
28433
28434 bool try_list_ctor = false;
28435
28436 releasing_vec rv_args = NULL;
28437 vec<tree,va_gc> *&args = *&rv_args;
28438 if (init == NULL_TREE)
28439 args = make_tree_vector ();
28440 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28441 {
28442 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28443 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28444 {
28445 /* As an exception, the first phase in 16.3.1.7 (considering the
28446 initializer list as a single argument) is omitted if the
28447 initializer list consists of a single expression of type cv U,
28448 where U is a specialization of C or a class derived from a
28449 specialization of C. */
28450 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28451 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28452 try_list_ctor = false;
28453 }
28454 if (try_list_ctor || is_std_init_list (type))
28455 args = make_tree_vector_single (init);
28456 else
28457 args = make_tree_vector_from_ctor (init);
28458 }
28459 else if (TREE_CODE (init) == TREE_LIST)
28460 args = make_tree_vector_from_list (init);
28461 else
28462 args = make_tree_vector_single (init);
28463
28464 /* Do this now to avoid problems with erroneous args later on. */
28465 args = resolve_args (args, complain);
28466 if (args == NULL)
28467 return error_mark_node;
28468
28469 tree cands = deduction_guides_for (tmpl, complain);
28470 if (cands == error_mark_node)
28471 return error_mark_node;
28472
28473 /* Prune explicit deduction guides in copy-initialization context. */
28474 bool elided = false;
28475 if (flags & LOOKUP_ONLYCONVERTING)
28476 {
28477 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28478 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28479 elided = true;
28480
28481 if (elided)
28482 {
28483 /* Found a nonconverting guide, prune the candidates. */
28484 tree pruned = NULL_TREE;
28485 for (lkp_iterator iter (cands); iter; ++iter)
28486 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28487 pruned = lookup_add (*iter, pruned);
28488
28489 cands = pruned;
28490 }
28491 }
28492
28493 if (tree guide = maybe_aggr_guide (tmpl, init, args))
28494 cands = lookup_add (guide, cands);
28495
28496 tree call = error_mark_node;
28497
28498 /* If this is list-initialization and the class has a list constructor, first
28499 try deducing from the list as a single argument, as [over.match.list]. */
28500 tree list_cands = NULL_TREE;
28501 if (try_list_ctor && cands)
28502 for (lkp_iterator iter (cands); iter; ++iter)
28503 {
28504 tree dg = *iter;
28505 if (is_list_ctor (dg))
28506 list_cands = lookup_add (dg, list_cands);
28507 }
28508 if (list_cands)
28509 {
28510 ++cp_unevaluated_operand;
28511 call = build_new_function_call (list_cands, &args, tf_decltype);
28512 --cp_unevaluated_operand;
28513
28514 if (call == error_mark_node)
28515 {
28516 /* That didn't work, now try treating the list as a sequence of
28517 arguments. */
28518 release_tree_vector (args);
28519 args = make_tree_vector_from_ctor (init);
28520 }
28521 }
28522
28523 if (elided && !cands)
28524 {
28525 error ("cannot deduce template arguments for copy-initialization"
28526 " of %qT, as it has no non-explicit deduction guides or "
28527 "user-declared constructors", type);
28528 return error_mark_node;
28529 }
28530 else if (!cands && call == error_mark_node)
28531 {
28532 error ("cannot deduce template arguments of %qT, as it has no viable "
28533 "deduction guides", type);
28534 return error_mark_node;
28535 }
28536
28537 if (call == error_mark_node)
28538 {
28539 ++cp_unevaluated_operand;
28540 call = build_new_function_call (cands, &args, tf_decltype);
28541 --cp_unevaluated_operand;
28542 }
28543
28544 if (call == error_mark_node
28545 && (complain & tf_warning_or_error))
28546 {
28547 error ("class template argument deduction failed:");
28548
28549 ++cp_unevaluated_operand;
28550 call = build_new_function_call (cands, &args, complain | tf_decltype);
28551 --cp_unevaluated_operand;
28552
28553 if (elided)
28554 inform (input_location, "explicit deduction guides not considered "
28555 "for copy-initialization");
28556 }
28557
28558 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
28559 }
28560
28561 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28562 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28563 The CONTEXT determines the context in which auto deduction is performed
28564 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28565 OUTER_TARGS are used during template argument deduction
28566 (context == adc_unify) to properly substitute the result, and is ignored
28567 in other contexts.
28568
28569 For partial-concept-ids, extra args may be appended to the list of deduced
28570 template arguments prior to determining constraint satisfaction. */
28571
28572 tree
28573 do_auto_deduction (tree type, tree init, tree auto_node,
28574 tsubst_flags_t complain, auto_deduction_context context,
28575 tree outer_targs, int flags)
28576 {
28577 tree targs;
28578
28579 if (init == error_mark_node)
28580 return error_mark_node;
28581
28582 if (init && type_dependent_expression_p (init)
28583 && context != adc_unify)
28584 /* Defining a subset of type-dependent expressions that we can deduce
28585 from ahead of time isn't worth the trouble. */
28586 return type;
28587
28588 /* Similarly, we can't deduce from another undeduced decl. */
28589 if (init && undeduced_auto_decl (init))
28590 return type;
28591
28592 /* We may be doing a partial substitution, but we still want to replace
28593 auto_node. */
28594 complain &= ~tf_partial;
28595
28596 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
28597 /* C++17 class template argument deduction. */
28598 return do_class_deduction (type, tmpl, init, flags, complain);
28599
28600 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
28601 /* Nothing we can do with this, even in deduction context. */
28602 return type;
28603
28604 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28605 with either a new invented type template parameter U or, if the
28606 initializer is a braced-init-list (8.5.4), with
28607 std::initializer_list<U>. */
28608 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28609 {
28610 if (!DIRECT_LIST_INIT_P (init))
28611 type = listify_autos (type, auto_node);
28612 else if (CONSTRUCTOR_NELTS (init) == 1)
28613 init = CONSTRUCTOR_ELT (init, 0)->value;
28614 else
28615 {
28616 if (complain & tf_warning_or_error)
28617 {
28618 if (permerror (input_location, "direct-list-initialization of "
28619 "%<auto%> requires exactly one element"))
28620 inform (input_location,
28621 "for deduction to %<std::initializer_list%>, use copy-"
28622 "list-initialization (i.e. add %<=%> before the %<{%>)");
28623 }
28624 type = listify_autos (type, auto_node);
28625 }
28626 }
28627
28628 if (type == error_mark_node)
28629 return error_mark_node;
28630
28631 init = resolve_nondeduced_context (init, complain);
28632
28633 if (context == adc_decomp_type
28634 && auto_node == type
28635 && init != error_mark_node
28636 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
28637 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28638 and initializer has array type, deduce cv-qualified array type. */
28639 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
28640 complain);
28641 else if (AUTO_IS_DECLTYPE (auto_node))
28642 {
28643 tree stripped_init = tree_strip_any_location_wrapper (init);
28644 bool id = (DECL_P (stripped_init)
28645 || ((TREE_CODE (init) == COMPONENT_REF
28646 || TREE_CODE (init) == SCOPE_REF)
28647 && !REF_PARENTHESIZED_P (init)));
28648 targs = make_tree_vec (1);
28649 TREE_VEC_ELT (targs, 0)
28650 = finish_decltype_type (init, id, tf_warning_or_error);
28651 if (type != auto_node)
28652 {
28653 if (complain & tf_error)
28654 error ("%qT as type rather than plain %<decltype(auto)%>", type);
28655 return error_mark_node;
28656 }
28657 }
28658 else
28659 {
28660 if (error_operand_p (init))
28661 return error_mark_node;
28662
28663 tree parms = build_tree_list (NULL_TREE, type);
28664 tree tparms;
28665
28666 if (flag_concepts)
28667 tparms = extract_autos (type);
28668 else
28669 {
28670 tparms = make_tree_vec (1);
28671 TREE_VEC_ELT (tparms, 0)
28672 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
28673 }
28674
28675 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28676 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
28677 DEDUCE_CALL,
28678 NULL, /*explain_p=*/false);
28679 if (val > 0)
28680 {
28681 if (processing_template_decl)
28682 /* Try again at instantiation time. */
28683 return type;
28684 if (type && type != error_mark_node
28685 && (complain & tf_error))
28686 /* If type is error_mark_node a diagnostic must have been
28687 emitted by now. Also, having a mention to '<type error>'
28688 in the diagnostic is not really useful to the user. */
28689 {
28690 if (cfun
28691 && FNDECL_USED_AUTO (current_function_decl)
28692 && (auto_node
28693 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
28694 && LAMBDA_FUNCTION_P (current_function_decl))
28695 error ("unable to deduce lambda return type from %qE", init);
28696 else
28697 error ("unable to deduce %qT from %qE", type, init);
28698 type_unification_real (tparms, targs, parms, &init, 1, 0,
28699 DEDUCE_CALL,
28700 NULL, /*explain_p=*/true);
28701 }
28702 return error_mark_node;
28703 }
28704 }
28705
28706 /* Check any placeholder constraints against the deduced type. */
28707 if (flag_concepts && !processing_template_decl)
28708 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
28709 {
28710 /* Use the deduced type to check the associated constraints. If we
28711 have a partial-concept-id, rebuild the argument list so that
28712 we check using the extra arguments. */
28713 check = unpack_concept_check (check);
28714 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
28715 tree cdecl = TREE_OPERAND (check, 0);
28716 if (OVL_P (cdecl))
28717 cdecl = OVL_FIRST (cdecl);
28718 tree cargs = TREE_OPERAND (check, 1);
28719 if (TREE_VEC_LENGTH (cargs) > 1)
28720 {
28721 cargs = copy_node (cargs);
28722 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
28723 }
28724 else
28725 cargs = targs;
28726
28727 /* Rebuild the check using the deduced arguments. */
28728 check = build_concept_check (cdecl, cargs, tf_none);
28729
28730 if (!constraints_satisfied_p (check))
28731 {
28732 if (complain & tf_warning_or_error)
28733 {
28734 auto_diagnostic_group d;
28735 switch (context)
28736 {
28737 case adc_unspecified:
28738 case adc_unify:
28739 error("placeholder constraints not satisfied");
28740 break;
28741 case adc_variable_type:
28742 case adc_decomp_type:
28743 error ("deduced initializer does not satisfy "
28744 "placeholder constraints");
28745 break;
28746 case adc_return_type:
28747 error ("deduced return type does not satisfy "
28748 "placeholder constraints");
28749 break;
28750 case adc_requirement:
28751 error ("deduced expression type does not satisfy "
28752 "placeholder constraints");
28753 break;
28754 }
28755 diagnose_constraints (input_location, check, targs);
28756 }
28757 return error_mark_node;
28758 }
28759 }
28760
28761 if (processing_template_decl && context != adc_unify)
28762 outer_targs = current_template_args ();
28763 targs = add_to_template_args (outer_targs, targs);
28764 return tsubst (type, targs, complain, NULL_TREE);
28765 }
28766
28767 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
28768 result. */
28769
28770 tree
28771 splice_late_return_type (tree type, tree late_return_type)
28772 {
28773 if (is_auto (type))
28774 {
28775 if (late_return_type)
28776 return late_return_type;
28777
28778 tree idx = get_template_parm_index (type);
28779 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
28780 /* In an abbreviated function template we didn't know we were dealing
28781 with a function template when we saw the auto return type, so update
28782 it to have the correct level. */
28783 return make_auto_1 (TYPE_IDENTIFIER (type), true);
28784 }
28785 return type;
28786 }
28787
28788 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
28789 'decltype(auto)' or a deduced class template. */
28790
28791 bool
28792 is_auto (const_tree type)
28793 {
28794 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
28795 && (TYPE_IDENTIFIER (type) == auto_identifier
28796 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
28797 return true;
28798 else
28799 return false;
28800 }
28801
28802 /* for_each_template_parm callback for type_uses_auto. */
28803
28804 int
28805 is_auto_r (tree tp, void */*data*/)
28806 {
28807 return is_auto (tp);
28808 }
28809
28810 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
28811 a use of `auto'. Returns NULL_TREE otherwise. */
28812
28813 tree
28814 type_uses_auto (tree type)
28815 {
28816 if (type == NULL_TREE)
28817 return NULL_TREE;
28818 else if (flag_concepts)
28819 {
28820 /* The Concepts TS allows multiple autos in one type-specifier; just
28821 return the first one we find, do_auto_deduction will collect all of
28822 them. */
28823 if (uses_template_parms (type))
28824 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
28825 /*visited*/NULL, /*nondeduced*/false);
28826 else
28827 return NULL_TREE;
28828 }
28829 else
28830 return find_type_usage (type, is_auto);
28831 }
28832
28833 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
28834 concepts are enabled, auto is acceptable in template arguments, but
28835 only when TEMPL identifies a template class. Return TRUE if any
28836 such errors were reported. */
28837
28838 bool
28839 check_auto_in_tmpl_args (tree tmpl, tree args)
28840 {
28841 /* If there were previous errors, nevermind. */
28842 if (!args || TREE_CODE (args) != TREE_VEC)
28843 return false;
28844
28845 /* If TMPL is an identifier, we're parsing and we can't tell yet
28846 whether TMPL is supposed to be a type, a function or a variable.
28847 We'll only be able to tell during template substitution, so we
28848 expect to be called again then. If concepts are enabled and we
28849 know we have a type, we're ok. */
28850 if (flag_concepts
28851 && (identifier_p (tmpl)
28852 || (DECL_P (tmpl)
28853 && (DECL_TYPE_TEMPLATE_P (tmpl)
28854 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
28855 return false;
28856
28857 /* Quickly search for any occurrences of auto; usually there won't
28858 be any, and then we'll avoid allocating the vector. */
28859 if (!type_uses_auto (args))
28860 return false;
28861
28862 bool errors = false;
28863
28864 tree vec = extract_autos (args);
28865 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
28866 {
28867 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
28868 error_at (DECL_SOURCE_LOCATION (xauto),
28869 "invalid use of %qT in template argument", xauto);
28870 errors = true;
28871 }
28872
28873 return errors;
28874 }
28875
28876 /* For a given template T, return the vector of typedefs referenced
28877 in T for which access check is needed at T instantiation time.
28878 T is either a FUNCTION_DECL or a RECORD_TYPE.
28879 Those typedefs were added to T by the function
28880 append_type_to_template_for_access_check. */
28881
28882 vec<qualified_typedef_usage_t, va_gc> *
28883 get_types_needing_access_check (tree t)
28884 {
28885 tree ti;
28886 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
28887
28888 if (!t || t == error_mark_node)
28889 return NULL;
28890
28891 if (!(ti = get_template_info (t)))
28892 return NULL;
28893
28894 if (CLASS_TYPE_P (t)
28895 || TREE_CODE (t) == FUNCTION_DECL)
28896 {
28897 if (!TI_TEMPLATE (ti))
28898 return NULL;
28899
28900 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
28901 }
28902
28903 return result;
28904 }
28905
28906 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
28907 tied to T. That list of typedefs will be access checked at
28908 T instantiation time.
28909 T is either a FUNCTION_DECL or a RECORD_TYPE.
28910 TYPE_DECL is a TYPE_DECL node representing a typedef.
28911 SCOPE is the scope through which TYPE_DECL is accessed.
28912 LOCATION is the location of the usage point of TYPE_DECL.
28913
28914 This function is a subroutine of
28915 append_type_to_template_for_access_check. */
28916
28917 static void
28918 append_type_to_template_for_access_check_1 (tree t,
28919 tree type_decl,
28920 tree scope,
28921 location_t location)
28922 {
28923 qualified_typedef_usage_t typedef_usage;
28924 tree ti;
28925
28926 if (!t || t == error_mark_node)
28927 return;
28928
28929 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
28930 || CLASS_TYPE_P (t))
28931 && type_decl
28932 && TREE_CODE (type_decl) == TYPE_DECL
28933 && scope);
28934
28935 if (!(ti = get_template_info (t)))
28936 return;
28937
28938 gcc_assert (TI_TEMPLATE (ti));
28939
28940 typedef_usage.typedef_decl = type_decl;
28941 typedef_usage.context = scope;
28942 typedef_usage.locus = location;
28943
28944 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
28945 }
28946
28947 /* Append TYPE_DECL to the template TEMPL.
28948 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
28949 At TEMPL instanciation time, TYPE_DECL will be checked to see
28950 if it can be accessed through SCOPE.
28951 LOCATION is the location of the usage point of TYPE_DECL.
28952
28953 e.g. consider the following code snippet:
28954
28955 class C
28956 {
28957 typedef int myint;
28958 };
28959
28960 template<class U> struct S
28961 {
28962 C::myint mi; // <-- usage point of the typedef C::myint
28963 };
28964
28965 S<char> s;
28966
28967 At S<char> instantiation time, we need to check the access of C::myint
28968 In other words, we need to check the access of the myint typedef through
28969 the C scope. For that purpose, this function will add the myint typedef
28970 and the scope C through which its being accessed to a list of typedefs
28971 tied to the template S. That list will be walked at template instantiation
28972 time and access check performed on each typedefs it contains.
28973 Note that this particular code snippet should yield an error because
28974 myint is private to C. */
28975
28976 void
28977 append_type_to_template_for_access_check (tree templ,
28978 tree type_decl,
28979 tree scope,
28980 location_t location)
28981 {
28982 qualified_typedef_usage_t *iter;
28983 unsigned i;
28984
28985 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
28986
28987 /* Make sure we don't append the type to the template twice. */
28988 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
28989 if (iter->typedef_decl == type_decl && scope == iter->context)
28990 return;
28991
28992 append_type_to_template_for_access_check_1 (templ, type_decl,
28993 scope, location);
28994 }
28995
28996 /* Recursively walk over && expressions searching for EXPR. Return a reference
28997 to that expression. */
28998
28999 static tree *find_template_requirement (tree *t, tree key)
29000 {
29001 if (*t == key)
29002 return t;
29003 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29004 {
29005 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29006 return p;
29007 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29008 return p;
29009 }
29010 return 0;
29011 }
29012
29013 /* Convert the generic type parameters in PARM that match the types given in the
29014 range [START_IDX, END_IDX) from the current_template_parms into generic type
29015 packs. */
29016
29017 tree
29018 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29019 {
29020 tree current = current_template_parms;
29021 int depth = TMPL_PARMS_DEPTH (current);
29022 current = INNERMOST_TEMPLATE_PARMS (current);
29023 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29024
29025 for (int i = 0; i < start_idx; ++i)
29026 TREE_VEC_ELT (replacement, i)
29027 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29028
29029 for (int i = start_idx; i < end_idx; ++i)
29030 {
29031 /* Create a distinct parameter pack type from the current parm and add it
29032 to the replacement args to tsubst below into the generic function
29033 parameter. */
29034 tree node = TREE_VEC_ELT (current, i);
29035 tree o = TREE_TYPE (TREE_VALUE (node));
29036 tree t = copy_type (o);
29037 TEMPLATE_TYPE_PARM_INDEX (t)
29038 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29039 t, 0, 0, tf_none);
29040 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29041 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29042 TYPE_MAIN_VARIANT (t) = t;
29043 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29044 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29045 TREE_VEC_ELT (replacement, i) = t;
29046
29047 /* Replace the current template parameter with new pack. */
29048 TREE_VALUE (node) = TREE_CHAIN (t);
29049
29050 /* Surgically adjust the associated constraint of adjusted parameter
29051 and it's corresponding contribution to the current template
29052 requirements. */
29053 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29054 {
29055 tree id = unpack_concept_check (constr);
29056 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
29057 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29058 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29059
29060 /* If there was a constraint, we also need to replace that in
29061 the template requirements, which we've already built. */
29062 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29063 reqs = find_template_requirement (reqs, constr);
29064 *reqs = fold;
29065 }
29066 }
29067
29068 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29069 TREE_VEC_ELT (replacement, i)
29070 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29071
29072 /* If there are more levels then build up the replacement with the outer
29073 template parms. */
29074 if (depth > 1)
29075 replacement = add_to_template_args (template_parms_to_args
29076 (TREE_CHAIN (current_template_parms)),
29077 replacement);
29078
29079 return tsubst (parm, replacement, tf_none, NULL_TREE);
29080 }
29081
29082 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29083 0..N-1. */
29084
29085 void
29086 declare_integer_pack (void)
29087 {
29088 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29089 build_function_type_list (integer_type_node,
29090 integer_type_node,
29091 NULL_TREE),
29092 NULL_TREE, ECF_CONST);
29093 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29094 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29095 CP_BUILT_IN_INTEGER_PACK);
29096 }
29097
29098 /* Set up the hash tables for template instantiations. */
29099
29100 void
29101 init_template_processing (void)
29102 {
29103 /* FIXME: enable sanitization (PR87847) */
29104 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
29105 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
29106
29107 if (cxx_dialect >= cxx11)
29108 declare_integer_pack ();
29109 }
29110
29111 /* Print stats about the template hash tables for -fstats. */
29112
29113 void
29114 print_template_statistics (void)
29115 {
29116 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29117 "%f collisions\n", (long) decl_specializations->size (),
29118 (long) decl_specializations->elements (),
29119 decl_specializations->collisions ());
29120 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29121 "%f collisions\n", (long) type_specializations->size (),
29122 (long) type_specializations->elements (),
29123 type_specializations->collisions ());
29124 }
29125
29126 #if CHECKING_P
29127
29128 namespace selftest {
29129
29130 /* Verify that build_non_dependent_expr () works, for various expressions,
29131 and that location wrappers don't affect the results. */
29132
29133 static void
29134 test_build_non_dependent_expr ()
29135 {
29136 location_t loc = BUILTINS_LOCATION;
29137
29138 /* Verify constants, without and with location wrappers. */
29139 tree int_cst = build_int_cst (integer_type_node, 42);
29140 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29141
29142 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29143 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29144 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29145
29146 tree string_lit = build_string (4, "foo");
29147 TREE_TYPE (string_lit) = char_array_type_node;
29148 string_lit = fix_string_type (string_lit);
29149 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29150
29151 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29152 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29153 ASSERT_EQ (wrapped_string_lit,
29154 build_non_dependent_expr (wrapped_string_lit));
29155 }
29156
29157 /* Verify that type_dependent_expression_p () works correctly, even
29158 in the presence of location wrapper nodes. */
29159
29160 static void
29161 test_type_dependent_expression_p ()
29162 {
29163 location_t loc = BUILTINS_LOCATION;
29164
29165 tree name = get_identifier ("foo");
29166
29167 /* If no templates are involved, nothing is type-dependent. */
29168 gcc_assert (!processing_template_decl);
29169 ASSERT_FALSE (type_dependent_expression_p (name));
29170
29171 ++processing_template_decl;
29172
29173 /* Within a template, an unresolved name is always type-dependent. */
29174 ASSERT_TRUE (type_dependent_expression_p (name));
29175
29176 /* Ensure it copes with NULL_TREE and errors. */
29177 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29178 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29179
29180 /* A USING_DECL in a template should be type-dependent, even if wrapped
29181 with a location wrapper (PR c++/83799). */
29182 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29183 TREE_TYPE (using_decl) = integer_type_node;
29184 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29185 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29186 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29187 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29188
29189 --processing_template_decl;
29190 }
29191
29192 /* Run all of the selftests within this file. */
29193
29194 void
29195 cp_pt_c_tests ()
29196 {
29197 test_build_non_dependent_expr ();
29198 test_type_dependent_expression_p ();
29199 }
29200
29201 } // namespace selftest
29202
29203 #endif /* #if CHECKING_P */
29204
29205 #include "gt-cp-pt.h"