c++: Improve print_tree of static_assert.
[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_nop)
87 ;
88 else if (policy == lss_blank || !saved)
89 local_specializations = new hash_map<tree, tree>;
90 else
91 local_specializations = new hash_map<tree, tree>(*saved);
92 }
93
94 local_specialization_stack::~local_specialization_stack ()
95 {
96 if (local_specializations != saved)
97 {
98 delete local_specializations;
99 local_specializations = saved;
100 }
101 }
102
103 /* True if we've recursed into fn_type_unification too many times. */
104 static bool excessive_deduction_depth;
105
106 struct GTY((for_user)) spec_entry
107 {
108 tree tmpl;
109 tree args;
110 tree spec;
111 };
112
113 struct spec_hasher : ggc_ptr_hash<spec_entry>
114 {
115 static hashval_t hash (spec_entry *);
116 static bool equal (spec_entry *, spec_entry *);
117 };
118
119 static GTY (()) hash_table<spec_hasher> *decl_specializations;
120
121 static GTY (()) hash_table<spec_hasher> *type_specializations;
122
123 /* Contains canonical template parameter types. The vector is indexed by
124 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
125 TREE_LIST, whose TREE_VALUEs contain the canonical template
126 parameters of various types and levels. */
127 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
128
129 #define UNIFY_ALLOW_NONE 0
130 #define UNIFY_ALLOW_MORE_CV_QUAL 1
131 #define UNIFY_ALLOW_LESS_CV_QUAL 2
132 #define UNIFY_ALLOW_DERIVED 4
133 #define UNIFY_ALLOW_INTEGER 8
134 #define UNIFY_ALLOW_OUTER_LEVEL 16
135 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
136 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
137
138 enum template_base_result {
139 tbr_incomplete_type,
140 tbr_ambiguous_baseclass,
141 tbr_success
142 };
143
144 static bool resolve_overloaded_unification (tree, tree, tree, tree,
145 unification_kind_t, int,
146 bool);
147 static int try_one_overload (tree, tree, tree, tree, tree,
148 unification_kind_t, int, bool, bool);
149 static int unify (tree, tree, tree, tree, int, bool);
150 static void add_pending_template (tree);
151 static tree reopen_tinst_level (struct tinst_level *);
152 static tree tsubst_initializer_list (tree, tree);
153 static tree get_partial_spec_bindings (tree, tree, tree);
154 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
155 bool, bool);
156 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
157 bool, bool);
158 static void tsubst_enum (tree, tree, tree);
159 static tree add_to_template_args (tree, tree);
160 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
161 static int check_non_deducible_conversion (tree, tree, int, int,
162 struct conversion **, bool);
163 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
164 tree);
165 static int type_unification_real (tree, tree, tree, const tree *,
166 unsigned int, int, unification_kind_t,
167 vec<deferred_access_check, va_gc> **,
168 bool);
169 static void note_template_header (int);
170 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
171 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
172 static tree convert_template_argument (tree, tree, tree,
173 tsubst_flags_t, int, tree);
174 static tree for_each_template_parm (tree, tree_fn_t, void*,
175 hash_set<tree> *, bool, tree_fn_t = NULL);
176 static tree expand_template_argument_pack (tree);
177 static tree build_template_parm_index (int, int, int, tree, tree);
178 static bool inline_needs_template_parms (tree, bool);
179 static void push_inline_template_parms_recursive (tree, int);
180 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
181 static int mark_template_parm (tree, void *);
182 static int template_parm_this_level_p (tree, void *);
183 static tree tsubst_friend_function (tree, tree);
184 static tree tsubst_friend_class (tree, tree);
185 static int can_complete_type_without_circularity (tree);
186 static tree get_bindings (tree, tree, tree, bool);
187 static int template_decl_level (tree);
188 static int check_cv_quals_for_unify (int, tree, tree);
189 static int unify_pack_expansion (tree, tree, tree,
190 tree, unification_kind_t, bool, bool);
191 static tree copy_template_args (tree);
192 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
193 tree most_specialized_partial_spec (tree, tsubst_flags_t);
194 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
195 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
196 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
197 static bool check_specialization_scope (void);
198 static tree process_partial_specialization (tree);
199 static void set_current_access_from_decl (tree);
200 static enum template_base_result get_template_base (tree, tree, tree, tree,
201 bool , tree *);
202 static tree try_class_unification (tree, tree, tree, tree, bool);
203 static bool class_nttp_const_wrapper_p (tree t);
204 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
205 tree, tree);
206 static bool template_template_parm_bindings_ok_p (tree, tree);
207 static void tsubst_default_arguments (tree, tsubst_flags_t);
208 static tree for_each_template_parm_r (tree *, int *, void *);
209 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
210 static void copy_default_args_to_explicit_spec (tree);
211 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
212 static bool dependent_template_arg_p (tree);
213 static bool any_template_arguments_need_structural_equality_p (tree);
214 static bool dependent_type_p_r (tree);
215 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
216 static tree tsubst_decl (tree, tree, tsubst_flags_t);
217 static void perform_typedefs_access_check (tree tmpl, tree targs);
218 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
219 location_t);
220 static tree listify (tree);
221 static tree listify_autos (tree, tree);
222 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
223 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
224 static bool complex_alias_template_p (const_tree tmpl);
225 static tree get_underlying_template (tree);
226 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
227 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
228 static tree make_argument_pack (tree);
229 static void register_parameter_specializations (tree, tree);
230 static tree enclosing_instantiation_of (tree tctx);
231
232 /* Make the current scope suitable for access checking when we are
233 processing T. T can be FUNCTION_DECL for instantiated function
234 template, VAR_DECL for static member variable, or TYPE_DECL for
235 alias template (needed by instantiate_decl). */
236
237 void
238 push_access_scope (tree t)
239 {
240 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
241 || TREE_CODE (t) == TYPE_DECL);
242
243 if (DECL_FRIEND_CONTEXT (t))
244 push_nested_class (DECL_FRIEND_CONTEXT (t));
245 else if (DECL_CLASS_SCOPE_P (t))
246 push_nested_class (DECL_CONTEXT (t));
247 else
248 push_to_top_level ();
249
250 if (TREE_CODE (t) == FUNCTION_DECL)
251 {
252 vec_safe_push (saved_access_scope, current_function_decl);
253 current_function_decl = t;
254 }
255 }
256
257 /* Restore the scope set up by push_access_scope. T is the node we
258 are processing. */
259
260 void
261 pop_access_scope (tree t)
262 {
263 if (TREE_CODE (t) == FUNCTION_DECL)
264 current_function_decl = saved_access_scope->pop();
265
266 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
267 pop_nested_class ();
268 else
269 pop_from_top_level ();
270 }
271
272 /* Do any processing required when DECL (a member template
273 declaration) is finished. Returns the TEMPLATE_DECL corresponding
274 to DECL, unless it is a specialization, in which case the DECL
275 itself is returned. */
276
277 tree
278 finish_member_template_decl (tree decl)
279 {
280 if (decl == error_mark_node)
281 return error_mark_node;
282
283 gcc_assert (DECL_P (decl));
284
285 if (TREE_CODE (decl) == TYPE_DECL)
286 {
287 tree type;
288
289 type = TREE_TYPE (decl);
290 if (type == error_mark_node)
291 return error_mark_node;
292 if (MAYBE_CLASS_TYPE_P (type)
293 && CLASSTYPE_TEMPLATE_INFO (type)
294 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
295 {
296 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
297 check_member_template (tmpl);
298 return tmpl;
299 }
300 return NULL_TREE;
301 }
302 else if (TREE_CODE (decl) == FIELD_DECL)
303 error_at (DECL_SOURCE_LOCATION (decl),
304 "data member %qD cannot be a member template", decl);
305 else if (DECL_TEMPLATE_INFO (decl))
306 {
307 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
308 {
309 check_member_template (DECL_TI_TEMPLATE (decl));
310 return DECL_TI_TEMPLATE (decl);
311 }
312 else
313 return decl;
314 }
315 else
316 error_at (DECL_SOURCE_LOCATION (decl),
317 "invalid member template declaration %qD", decl);
318
319 return error_mark_node;
320 }
321
322 /* Create a template info node. */
323
324 tree
325 build_template_info (tree template_decl, tree template_args)
326 {
327 tree result = make_node (TEMPLATE_INFO);
328 TI_TEMPLATE (result) = template_decl;
329 TI_ARGS (result) = template_args;
330 return result;
331 }
332
333 /* Return the template info node corresponding to T, whatever T is. */
334
335 tree
336 get_template_info (const_tree t)
337 {
338 tree tinfo = NULL_TREE;
339
340 if (!t || t == error_mark_node)
341 return NULL;
342
343 if (TREE_CODE (t) == NAMESPACE_DECL
344 || TREE_CODE (t) == PARM_DECL)
345 return NULL;
346
347 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
348 tinfo = DECL_TEMPLATE_INFO (t);
349
350 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
351 t = TREE_TYPE (t);
352
353 if (OVERLOAD_TYPE_P (t))
354 tinfo = TYPE_TEMPLATE_INFO (t);
355 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
356 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
357
358 return tinfo;
359 }
360
361 /* Returns the template nesting level of the indicated class TYPE.
362
363 For example, in:
364 template <class T>
365 struct A
366 {
367 template <class U>
368 struct B {};
369 };
370
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
374
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
377 always safe. */
378
379 int
380 template_class_depth (tree type)
381 {
382 int depth;
383
384 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
385 {
386 tree tinfo = get_template_info (type);
387
388 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
390 ++depth;
391
392 if (DECL_P (type))
393 {
394 if (tree fctx = DECL_FRIEND_CONTEXT (type))
395 type = fctx;
396 else
397 type = CP_DECL_CONTEXT (type);
398 }
399 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
400 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
401 else
402 type = CP_TYPE_CONTEXT (type);
403 }
404
405 return depth;
406 }
407
408 /* Return TRUE if NODE instantiates a template that has arguments of
409 its own, be it directly a primary template or indirectly through a
410 partial specializations. */
411 static bool
412 instantiates_primary_template_p (tree node)
413 {
414 tree tinfo = get_template_info (node);
415 if (!tinfo)
416 return false;
417
418 tree tmpl = TI_TEMPLATE (tinfo);
419 if (PRIMARY_TEMPLATE_P (tmpl))
420 return true;
421
422 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
423 return false;
424
425 /* So now we know we have a specialization, but it could be a full
426 or a partial specialization. To tell which, compare the depth of
427 its template arguments with those of its context. */
428
429 tree ctxt = DECL_CONTEXT (tmpl);
430 tree ctinfo = get_template_info (ctxt);
431 if (!ctinfo)
432 return true;
433
434 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
435 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
436 }
437
438 /* Subroutine of maybe_begin_member_template_processing.
439 Returns true if processing DECL needs us to push template parms. */
440
441 static bool
442 inline_needs_template_parms (tree decl, bool nsdmi)
443 {
444 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
445 return false;
446
447 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
448 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
449 }
450
451 /* Subroutine of maybe_begin_member_template_processing.
452 Push the template parms in PARMS, starting from LEVELS steps into the
453 chain, and ending at the beginning, since template parms are listed
454 innermost first. */
455
456 static void
457 push_inline_template_parms_recursive (tree parmlist, int levels)
458 {
459 tree parms = TREE_VALUE (parmlist);
460 int i;
461
462 if (levels > 1)
463 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
464
465 ++processing_template_decl;
466 current_template_parms
467 = tree_cons (size_int (processing_template_decl),
468 parms, current_template_parms);
469 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
470
471 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
472 NULL);
473 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
474 {
475 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
476
477 if (error_operand_p (parm))
478 continue;
479
480 gcc_assert (DECL_P (parm));
481
482 switch (TREE_CODE (parm))
483 {
484 case TYPE_DECL:
485 case TEMPLATE_DECL:
486 pushdecl (parm);
487 break;
488
489 case PARM_DECL:
490 /* Push the CONST_DECL. */
491 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
492 break;
493
494 default:
495 gcc_unreachable ();
496 }
497 }
498 }
499
500 /* Restore the template parameter context for a member template, a
501 friend template defined in a class definition, or a non-template
502 member of template class. */
503
504 void
505 maybe_begin_member_template_processing (tree decl)
506 {
507 tree parms;
508 int levels = 0;
509 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
510
511 if (nsdmi)
512 {
513 tree ctx = DECL_CONTEXT (decl);
514 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
515 /* Disregard full specializations (c++/60999). */
516 && uses_template_parms (ctx)
517 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
518 }
519
520 if (inline_needs_template_parms (decl, nsdmi))
521 {
522 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
523 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
524
525 if (DECL_TEMPLATE_SPECIALIZATION (decl))
526 {
527 --levels;
528 parms = TREE_CHAIN (parms);
529 }
530
531 push_inline_template_parms_recursive (parms, levels);
532 }
533
534 /* Remember how many levels of template parameters we pushed so that
535 we can pop them later. */
536 inline_parm_levels.safe_push (levels);
537 }
538
539 /* Undo the effects of maybe_begin_member_template_processing. */
540
541 void
542 maybe_end_member_template_processing (void)
543 {
544 int i;
545 int last;
546
547 if (inline_parm_levels.length () == 0)
548 return;
549
550 last = inline_parm_levels.pop ();
551 for (i = 0; i < last; ++i)
552 {
553 --processing_template_decl;
554 current_template_parms = TREE_CHAIN (current_template_parms);
555 poplevel (0, 0, 0);
556 }
557 }
558
559 /* Return a new template argument vector which contains all of ARGS,
560 but has as its innermost set of arguments the EXTRA_ARGS. */
561
562 static tree
563 add_to_template_args (tree args, tree extra_args)
564 {
565 tree new_args;
566 int extra_depth;
567 int i;
568 int j;
569
570 if (args == NULL_TREE || extra_args == error_mark_node)
571 return extra_args;
572
573 extra_depth = TMPL_ARGS_DEPTH (extra_args);
574 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
575
576 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
577 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
578
579 for (j = 1; j <= extra_depth; ++j, ++i)
580 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
581
582 return new_args;
583 }
584
585 /* Like add_to_template_args, but only the outermost ARGS are added to
586 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
587 (EXTRA_ARGS) levels are added. This function is used to combine
588 the template arguments from a partial instantiation with the
589 template arguments used to attain the full instantiation from the
590 partial instantiation. */
591
592 tree
593 add_outermost_template_args (tree args, tree extra_args)
594 {
595 tree new_args;
596
597 /* If there are more levels of EXTRA_ARGS than there are ARGS,
598 something very fishy is going on. */
599 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
600
601 /* If *all* the new arguments will be the EXTRA_ARGS, just return
602 them. */
603 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
604 return extra_args;
605
606 /* For the moment, we make ARGS look like it contains fewer levels. */
607 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
608
609 new_args = add_to_template_args (args, extra_args);
610
611 /* Now, we restore ARGS to its full dimensions. */
612 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
613
614 return new_args;
615 }
616
617 /* Return the N levels of innermost template arguments from the ARGS. */
618
619 tree
620 get_innermost_template_args (tree args, int n)
621 {
622 tree new_args;
623 int extra_levels;
624 int i;
625
626 gcc_assert (n >= 0);
627
628 /* If N is 1, just return the innermost set of template arguments. */
629 if (n == 1)
630 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
631
632 /* If we're not removing anything, just return the arguments we were
633 given. */
634 extra_levels = TMPL_ARGS_DEPTH (args) - n;
635 gcc_assert (extra_levels >= 0);
636 if (extra_levels == 0)
637 return args;
638
639 /* Make a new set of arguments, not containing the outer arguments. */
640 new_args = make_tree_vec (n);
641 for (i = 1; i <= n; ++i)
642 SET_TMPL_ARGS_LEVEL (new_args, i,
643 TMPL_ARGS_LEVEL (args, i + extra_levels));
644
645 return new_args;
646 }
647
648 /* The inverse of get_innermost_template_args: Return all but the innermost
649 EXTRA_LEVELS levels of template arguments from the ARGS. */
650
651 static tree
652 strip_innermost_template_args (tree args, int extra_levels)
653 {
654 tree new_args;
655 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
656 int i;
657
658 gcc_assert (n >= 0);
659
660 /* If N is 1, just return the outermost set of template arguments. */
661 if (n == 1)
662 return TMPL_ARGS_LEVEL (args, 1);
663
664 /* If we're not removing anything, just return the arguments we were
665 given. */
666 gcc_assert (extra_levels >= 0);
667 if (extra_levels == 0)
668 return args;
669
670 /* Make a new set of arguments, not containing the inner arguments. */
671 new_args = make_tree_vec (n);
672 for (i = 1; i <= n; ++i)
673 SET_TMPL_ARGS_LEVEL (new_args, i,
674 TMPL_ARGS_LEVEL (args, i));
675
676 return new_args;
677 }
678
679 /* We've got a template header coming up; push to a new level for storing
680 the parms. */
681
682 void
683 begin_template_parm_list (void)
684 {
685 /* We use a non-tag-transparent scope here, which causes pushtag to
686 put tags in this scope, rather than in the enclosing class or
687 namespace scope. This is the right thing, since we want
688 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
689 global template class, push_template_decl handles putting the
690 TEMPLATE_DECL into top-level scope. For a nested template class,
691 e.g.:
692
693 template <class T> struct S1 {
694 template <class T> struct S2 {};
695 };
696
697 pushtag contains special code to insert the TEMPLATE_DECL for S2
698 at the right scope. */
699 begin_scope (sk_template_parms, NULL);
700 ++processing_template_decl;
701 ++processing_template_parmlist;
702 note_template_header (0);
703
704 /* Add a dummy parameter level while we process the parameter list. */
705 current_template_parms
706 = tree_cons (size_int (processing_template_decl),
707 make_tree_vec (0),
708 current_template_parms);
709 }
710
711 /* This routine is called when a specialization is declared. If it is
712 invalid to declare a specialization here, an error is reported and
713 false is returned, otherwise this routine will return true. */
714
715 static bool
716 check_specialization_scope (void)
717 {
718 tree scope = current_scope ();
719
720 /* [temp.expl.spec]
721
722 An explicit specialization shall be declared in the namespace of
723 which the template is a member, or, for member templates, in the
724 namespace of which the enclosing class or enclosing class
725 template is a member. An explicit specialization of a member
726 function, member class or static data member of a class template
727 shall be declared in the namespace of which the class template
728 is a member. */
729 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
730 {
731 error ("explicit specialization in non-namespace scope %qD", scope);
732 return false;
733 }
734
735 /* [temp.expl.spec]
736
737 In an explicit specialization declaration for a member of a class
738 template or a member template that appears in namespace scope,
739 the member template and some of its enclosing class templates may
740 remain unspecialized, except that the declaration shall not
741 explicitly specialize a class member template if its enclosing
742 class templates are not explicitly specialized as well. */
743 if (current_template_parms)
744 {
745 error ("enclosing class templates are not explicitly specialized");
746 return false;
747 }
748
749 return true;
750 }
751
752 /* We've just seen template <>. */
753
754 bool
755 begin_specialization (void)
756 {
757 begin_scope (sk_template_spec, NULL);
758 note_template_header (1);
759 return check_specialization_scope ();
760 }
761
762 /* Called at then end of processing a declaration preceded by
763 template<>. */
764
765 void
766 end_specialization (void)
767 {
768 finish_scope ();
769 reset_specialization ();
770 }
771
772 /* Any template <>'s that we have seen thus far are not referring to a
773 function specialization. */
774
775 void
776 reset_specialization (void)
777 {
778 processing_specialization = 0;
779 template_header_count = 0;
780 }
781
782 /* We've just seen a template header. If SPECIALIZATION is nonzero,
783 it was of the form template <>. */
784
785 static void
786 note_template_header (int specialization)
787 {
788 processing_specialization = specialization;
789 template_header_count++;
790 }
791
792 /* We're beginning an explicit instantiation. */
793
794 void
795 begin_explicit_instantiation (void)
796 {
797 gcc_assert (!processing_explicit_instantiation);
798 processing_explicit_instantiation = true;
799 }
800
801
802 void
803 end_explicit_instantiation (void)
804 {
805 gcc_assert (processing_explicit_instantiation);
806 processing_explicit_instantiation = false;
807 }
808
809 /* An explicit specialization or partial specialization of TMPL is being
810 declared. Check that the namespace in which the specialization is
811 occurring is permissible. Returns false iff it is invalid to
812 specialize TMPL in the current namespace. */
813
814 static bool
815 check_specialization_namespace (tree tmpl)
816 {
817 tree tpl_ns = decl_namespace_context (tmpl);
818
819 /* [tmpl.expl.spec]
820
821 An explicit specialization shall be declared in a namespace enclosing the
822 specialized template. An explicit specialization whose declarator-id is
823 not qualified shall be declared in the nearest enclosing namespace of the
824 template, or, if the namespace is inline (7.3.1), any namespace from its
825 enclosing namespace set. */
826 if (current_scope() != DECL_CONTEXT (tmpl)
827 && !at_namespace_scope_p ())
828 {
829 error ("specialization of %qD must appear at namespace scope", tmpl);
830 return false;
831 }
832
833 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
834 /* Same or enclosing namespace. */
835 return true;
836 else
837 {
838 auto_diagnostic_group d;
839 if (permerror (input_location,
840 "specialization of %qD in different namespace", tmpl))
841 inform (DECL_SOURCE_LOCATION (tmpl),
842 " from definition of %q#D", tmpl);
843 return false;
844 }
845 }
846
847 /* SPEC is an explicit instantiation. Check that it is valid to
848 perform this explicit instantiation in the current namespace. */
849
850 static void
851 check_explicit_instantiation_namespace (tree spec)
852 {
853 tree ns;
854
855 /* DR 275: An explicit instantiation shall appear in an enclosing
856 namespace of its template. */
857 ns = decl_namespace_context (spec);
858 if (!is_nested_namespace (current_namespace, ns))
859 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
860 "(which does not enclose namespace %qD)",
861 spec, current_namespace, ns);
862 }
863
864 /* Returns the type of a template specialization only if that
865 specialization needs to be defined. Otherwise (e.g., if the type has
866 already been defined), the function returns NULL_TREE. */
867
868 static tree
869 maybe_new_partial_specialization (tree type)
870 {
871 /* An implicit instantiation of an incomplete type implies
872 the definition of a new class template.
873
874 template<typename T>
875 struct S;
876
877 template<typename T>
878 struct S<T*>;
879
880 Here, S<T*> is an implicit instantiation of S whose type
881 is incomplete. */
882 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
883 return type;
884
885 /* It can also be the case that TYPE is a completed specialization.
886 Continuing the previous example, suppose we also declare:
887
888 template<typename T>
889 requires Integral<T>
890 struct S<T*>;
891
892 Here, S<T*> refers to the specialization S<T*> defined
893 above. However, we need to differentiate definitions because
894 we intend to define a new partial specialization. In this case,
895 we rely on the fact that the constraints are different for
896 this declaration than that above.
897
898 Note that we also get here for injected class names and
899 late-parsed template definitions. We must ensure that we
900 do not create new type declarations for those cases. */
901 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
902 {
903 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
904 tree args = CLASSTYPE_TI_ARGS (type);
905
906 /* If there are no template parameters, this cannot be a new
907 partial template specialization? */
908 if (!current_template_parms)
909 return NULL_TREE;
910
911 /* The injected-class-name is not a new partial specialization. */
912 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
913 return NULL_TREE;
914
915 /* If the constraints are not the same as those of the primary
916 then, we can probably create a new specialization. */
917 tree type_constr = current_template_constraints ();
918
919 if (type == TREE_TYPE (tmpl))
920 {
921 tree main_constr = get_constraints (tmpl);
922 if (equivalent_constraints (type_constr, main_constr))
923 return NULL_TREE;
924 }
925
926 /* Also, if there's a pre-existing specialization with matching
927 constraints, then this also isn't new. */
928 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
929 while (specs)
930 {
931 tree spec_tmpl = TREE_VALUE (specs);
932 tree spec_args = TREE_PURPOSE (specs);
933 tree spec_constr = get_constraints (spec_tmpl);
934 if (comp_template_args (args, spec_args)
935 && equivalent_constraints (type_constr, spec_constr))
936 return NULL_TREE;
937 specs = TREE_CHAIN (specs);
938 }
939
940 /* Create a new type node (and corresponding type decl)
941 for the newly declared specialization. */
942 tree t = make_class_type (TREE_CODE (type));
943 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
944 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
945
946 /* We only need a separate type node for storing the definition of this
947 partial specialization; uses of S<T*> are unconstrained, so all are
948 equivalent. So keep TYPE_CANONICAL the same. */
949 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
950
951 /* Build the corresponding type decl. */
952 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
953 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
954 DECL_SOURCE_LOCATION (d) = input_location;
955 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
956 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
957
958 return t;
959 }
960
961 return NULL_TREE;
962 }
963
964 /* The TYPE is being declared. If it is a template type, that means it
965 is a partial specialization. Do appropriate error-checking. */
966
967 tree
968 maybe_process_partial_specialization (tree type)
969 {
970 tree context;
971
972 if (type == error_mark_node)
973 return error_mark_node;
974
975 /* A lambda that appears in specialization context is not itself a
976 specialization. */
977 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
978 return type;
979
980 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
981 {
982 error ("name of class shadows template template parameter %qD",
983 TYPE_NAME (type));
984 return error_mark_node;
985 }
986
987 context = TYPE_CONTEXT (type);
988
989 if (TYPE_ALIAS_P (type))
990 {
991 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
992
993 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
994 error ("specialization of alias template %qD",
995 TI_TEMPLATE (tinfo));
996 else
997 error ("explicit specialization of non-template %qT", type);
998 return error_mark_node;
999 }
1000 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1001 {
1002 /* This is for ordinary explicit specialization and partial
1003 specialization of a template class such as:
1004
1005 template <> class C<int>;
1006
1007 or:
1008
1009 template <class T> class C<T*>;
1010
1011 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1012
1013 if (tree t = maybe_new_partial_specialization (type))
1014 {
1015 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1016 && !at_namespace_scope_p ())
1017 return error_mark_node;
1018 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1019 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1020 if (processing_template_decl)
1021 {
1022 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1023 if (decl == error_mark_node)
1024 return error_mark_node;
1025 return TREE_TYPE (decl);
1026 }
1027 }
1028 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1029 error ("specialization of %qT after instantiation", type);
1030 else if (errorcount && !processing_specialization
1031 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1032 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1033 /* Trying to define a specialization either without a template<> header
1034 or in an inappropriate place. We've already given an error, so just
1035 bail now so we don't actually define the specialization. */
1036 return error_mark_node;
1037 }
1038 else if (CLASS_TYPE_P (type)
1039 && !CLASSTYPE_USE_TEMPLATE (type)
1040 && CLASSTYPE_TEMPLATE_INFO (type)
1041 && context && CLASS_TYPE_P (context)
1042 && CLASSTYPE_TEMPLATE_INFO (context))
1043 {
1044 /* This is for an explicit specialization of member class
1045 template according to [temp.expl.spec/18]:
1046
1047 template <> template <class U> class C<int>::D;
1048
1049 The context `C<int>' must be an implicit instantiation.
1050 Otherwise this is just a member class template declared
1051 earlier like:
1052
1053 template <> class C<int> { template <class U> class D; };
1054 template <> template <class U> class C<int>::D;
1055
1056 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1057 while in the second case, `C<int>::D' is a primary template
1058 and `C<T>::D' may not exist. */
1059
1060 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1061 && !COMPLETE_TYPE_P (type))
1062 {
1063 tree t;
1064 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1065
1066 if (current_namespace
1067 != decl_namespace_context (tmpl))
1068 {
1069 if (permerror (input_location,
1070 "specialization of %qD in different namespace",
1071 type))
1072 inform (DECL_SOURCE_LOCATION (tmpl),
1073 "from definition of %q#D", tmpl);
1074 }
1075
1076 /* Check for invalid specialization after instantiation:
1077
1078 template <> template <> class C<int>::D<int>;
1079 template <> template <class U> class C<int>::D; */
1080
1081 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1082 t; t = TREE_CHAIN (t))
1083 {
1084 tree inst = TREE_VALUE (t);
1085 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1086 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1087 {
1088 /* We already have a full specialization of this partial
1089 instantiation, or a full specialization has been
1090 looked up but not instantiated. Reassign it to the
1091 new member specialization template. */
1092 spec_entry elt;
1093 spec_entry *entry;
1094
1095 elt.tmpl = most_general_template (tmpl);
1096 elt.args = CLASSTYPE_TI_ARGS (inst);
1097 elt.spec = inst;
1098
1099 type_specializations->remove_elt (&elt);
1100
1101 elt.tmpl = tmpl;
1102 CLASSTYPE_TI_ARGS (inst)
1103 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1104
1105 spec_entry **slot
1106 = type_specializations->find_slot (&elt, INSERT);
1107 entry = ggc_alloc<spec_entry> ();
1108 *entry = elt;
1109 *slot = entry;
1110 }
1111 else
1112 /* But if we've had an implicit instantiation, that's a
1113 problem ([temp.expl.spec]/6). */
1114 error ("specialization %qT after instantiation %qT",
1115 type, inst);
1116 }
1117
1118 /* Mark TYPE as a specialization. And as a result, we only
1119 have one level of template argument for the innermost
1120 class template. */
1121 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1122 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1123 CLASSTYPE_TI_ARGS (type)
1124 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1125 }
1126 }
1127 else if (processing_specialization)
1128 {
1129 /* Someday C++0x may allow for enum template specialization. */
1130 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1131 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1132 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1133 "of %qD not allowed by ISO C++", type);
1134 else
1135 {
1136 error ("explicit specialization of non-template %qT", type);
1137 return error_mark_node;
1138 }
1139 }
1140
1141 return type;
1142 }
1143
1144 /* Returns nonzero if we can optimize the retrieval of specializations
1145 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1146 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1147
1148 static inline bool
1149 optimize_specialization_lookup_p (tree tmpl)
1150 {
1151 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1152 && DECL_CLASS_SCOPE_P (tmpl)
1153 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1154 parameter. */
1155 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1156 /* The optimized lookup depends on the fact that the
1157 template arguments for the member function template apply
1158 purely to the containing class, which is not true if the
1159 containing class is an explicit or partial
1160 specialization. */
1161 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1162 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1163 && !DECL_CONV_FN_P (tmpl)
1164 /* It is possible to have a template that is not a member
1165 template and is not a member of a template class:
1166
1167 template <typename T>
1168 struct S { friend A::f(); };
1169
1170 Here, the friend function is a template, but the context does
1171 not have template information. The optimized lookup relies
1172 on having ARGS be the template arguments for both the class
1173 and the function template. */
1174 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1175 }
1176
1177 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1178 gone through coerce_template_parms by now. */
1179
1180 static void
1181 verify_unstripped_args_1 (tree inner)
1182 {
1183 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1184 {
1185 tree arg = TREE_VEC_ELT (inner, i);
1186 if (TREE_CODE (arg) == TEMPLATE_DECL)
1187 /* OK */;
1188 else if (TYPE_P (arg))
1189 gcc_assert (strip_typedefs (arg, NULL) == arg);
1190 else if (ARGUMENT_PACK_P (arg))
1191 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1192 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1193 /* Allow typedefs on the type of a non-type argument, since a
1194 parameter can have them. */;
1195 else
1196 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1197 }
1198 }
1199
1200 static void
1201 verify_unstripped_args (tree args)
1202 {
1203 ++processing_template_decl;
1204 if (!any_dependent_template_arguments_p (args))
1205 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1206 --processing_template_decl;
1207 }
1208
1209 /* Retrieve the specialization (in the sense of [temp.spec] - a
1210 specialization is either an instantiation or an explicit
1211 specialization) of TMPL for the given template ARGS. If there is
1212 no such specialization, return NULL_TREE. The ARGS are a vector of
1213 arguments, or a vector of vectors of arguments, in the case of
1214 templates with more than one level of parameters.
1215
1216 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1217 then we search for a partial specialization matching ARGS. This
1218 parameter is ignored if TMPL is not a class template.
1219
1220 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1221 result is a NONTYPE_ARGUMENT_PACK. */
1222
1223 static tree
1224 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1225 {
1226 if (tmpl == NULL_TREE)
1227 return NULL_TREE;
1228
1229 if (args == error_mark_node)
1230 return NULL_TREE;
1231
1232 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1233 || TREE_CODE (tmpl) == FIELD_DECL);
1234
1235 /* There should be as many levels of arguments as there are
1236 levels of parameters. */
1237 gcc_assert (TMPL_ARGS_DEPTH (args)
1238 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1239 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1240 : template_class_depth (DECL_CONTEXT (tmpl))));
1241
1242 if (flag_checking)
1243 verify_unstripped_args (args);
1244
1245 /* Lambda functions in templates aren't instantiated normally, but through
1246 tsubst_lambda_expr. */
1247 if (lambda_fn_in_template_p (tmpl))
1248 return NULL_TREE;
1249
1250 if (optimize_specialization_lookup_p (tmpl))
1251 {
1252 /* The template arguments actually apply to the containing
1253 class. Find the class specialization with those
1254 arguments. */
1255 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1256 tree class_specialization
1257 = retrieve_specialization (class_template, args, 0);
1258 if (!class_specialization)
1259 return NULL_TREE;
1260
1261 /* Find the instance of TMPL. */
1262 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1263 for (ovl_iterator iter (fns); iter; ++iter)
1264 {
1265 tree fn = *iter;
1266 if (tree ti = get_template_info (fn))
1267 if (TI_TEMPLATE (ti) == tmpl
1268 /* using-declarations can bring in a different
1269 instantiation of tmpl as a member of a different
1270 instantiation of tmpl's class. We don't want those
1271 here. */
1272 && DECL_CONTEXT (fn) == class_specialization)
1273 return fn;
1274 }
1275 return NULL_TREE;
1276 }
1277 else
1278 {
1279 spec_entry *found;
1280 spec_entry elt;
1281 hash_table<spec_hasher> *specializations;
1282
1283 elt.tmpl = tmpl;
1284 elt.args = args;
1285 elt.spec = NULL_TREE;
1286
1287 if (DECL_CLASS_TEMPLATE_P (tmpl))
1288 specializations = type_specializations;
1289 else
1290 specializations = decl_specializations;
1291
1292 if (hash == 0)
1293 hash = spec_hasher::hash (&elt);
1294 found = specializations->find_with_hash (&elt, hash);
1295 if (found)
1296 return found->spec;
1297 }
1298
1299 return NULL_TREE;
1300 }
1301
1302 /* Like retrieve_specialization, but for local declarations. */
1303
1304 tree
1305 retrieve_local_specialization (tree tmpl)
1306 {
1307 if (local_specializations == NULL)
1308 return NULL_TREE;
1309
1310 tree *slot = local_specializations->get (tmpl);
1311 return slot ? *slot : NULL_TREE;
1312 }
1313
1314 /* Returns nonzero iff DECL is a specialization of TMPL. */
1315
1316 int
1317 is_specialization_of (tree decl, tree tmpl)
1318 {
1319 tree t;
1320
1321 if (TREE_CODE (decl) == FUNCTION_DECL)
1322 {
1323 for (t = decl;
1324 t != NULL_TREE;
1325 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1326 if (t == tmpl)
1327 return 1;
1328 }
1329 else
1330 {
1331 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1332
1333 for (t = TREE_TYPE (decl);
1334 t != NULL_TREE;
1335 t = CLASSTYPE_USE_TEMPLATE (t)
1336 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1337 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1338 return 1;
1339 }
1340
1341 return 0;
1342 }
1343
1344 /* Returns nonzero iff DECL is a specialization of friend declaration
1345 FRIEND_DECL according to [temp.friend]. */
1346
1347 bool
1348 is_specialization_of_friend (tree decl, tree friend_decl)
1349 {
1350 bool need_template = true;
1351 int template_depth;
1352
1353 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1354 || TREE_CODE (decl) == TYPE_DECL);
1355
1356 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1357 of a template class, we want to check if DECL is a specialization
1358 if this. */
1359 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1360 && DECL_TEMPLATE_INFO (friend_decl)
1361 && !DECL_USE_TEMPLATE (friend_decl))
1362 {
1363 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1364 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1365 need_template = false;
1366 }
1367 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1368 && !PRIMARY_TEMPLATE_P (friend_decl))
1369 need_template = false;
1370
1371 /* There is nothing to do if this is not a template friend. */
1372 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1373 return false;
1374
1375 if (is_specialization_of (decl, friend_decl))
1376 return true;
1377
1378 /* [temp.friend/6]
1379 A member of a class template may be declared to be a friend of a
1380 non-template class. In this case, the corresponding member of
1381 every specialization of the class template is a friend of the
1382 class granting friendship.
1383
1384 For example, given a template friend declaration
1385
1386 template <class T> friend void A<T>::f();
1387
1388 the member function below is considered a friend
1389
1390 template <> struct A<int> {
1391 void f();
1392 };
1393
1394 For this type of template friend, TEMPLATE_DEPTH below will be
1395 nonzero. To determine if DECL is a friend of FRIEND, we first
1396 check if the enclosing class is a specialization of another. */
1397
1398 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1399 if (template_depth
1400 && DECL_CLASS_SCOPE_P (decl)
1401 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1402 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1403 {
1404 /* Next, we check the members themselves. In order to handle
1405 a few tricky cases, such as when FRIEND_DECL's are
1406
1407 template <class T> friend void A<T>::g(T t);
1408 template <class T> template <T t> friend void A<T>::h();
1409
1410 and DECL's are
1411
1412 void A<int>::g(int);
1413 template <int> void A<int>::h();
1414
1415 we need to figure out ARGS, the template arguments from
1416 the context of DECL. This is required for template substitution
1417 of `T' in the function parameter of `g' and template parameter
1418 of `h' in the above examples. Here ARGS corresponds to `int'. */
1419
1420 tree context = DECL_CONTEXT (decl);
1421 tree args = NULL_TREE;
1422 int current_depth = 0;
1423
1424 while (current_depth < template_depth)
1425 {
1426 if (CLASSTYPE_TEMPLATE_INFO (context))
1427 {
1428 if (current_depth == 0)
1429 args = TYPE_TI_ARGS (context);
1430 else
1431 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1432 current_depth++;
1433 }
1434 context = TYPE_CONTEXT (context);
1435 }
1436
1437 if (TREE_CODE (decl) == FUNCTION_DECL)
1438 {
1439 bool is_template;
1440 tree friend_type;
1441 tree decl_type;
1442 tree friend_args_type;
1443 tree decl_args_type;
1444
1445 /* Make sure that both DECL and FRIEND_DECL are templates or
1446 non-templates. */
1447 is_template = DECL_TEMPLATE_INFO (decl)
1448 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1449 if (need_template ^ is_template)
1450 return false;
1451 else if (is_template)
1452 {
1453 /* If both are templates, check template parameter list. */
1454 tree friend_parms
1455 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1456 args, tf_none);
1457 if (!comp_template_parms
1458 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1459 friend_parms))
1460 return false;
1461
1462 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1463 }
1464 else
1465 decl_type = TREE_TYPE (decl);
1466
1467 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1468 tf_none, NULL_TREE);
1469 if (friend_type == error_mark_node)
1470 return false;
1471
1472 /* Check if return types match. */
1473 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1474 return false;
1475
1476 /* Check if function parameter types match, ignoring the
1477 `this' parameter. */
1478 friend_args_type = TYPE_ARG_TYPES (friend_type);
1479 decl_args_type = TYPE_ARG_TYPES (decl_type);
1480 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1481 friend_args_type = TREE_CHAIN (friend_args_type);
1482 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1483 decl_args_type = TREE_CHAIN (decl_args_type);
1484
1485 return compparms (decl_args_type, friend_args_type);
1486 }
1487 else
1488 {
1489 /* DECL is a TYPE_DECL */
1490 bool is_template;
1491 tree decl_type = TREE_TYPE (decl);
1492
1493 /* Make sure that both DECL and FRIEND_DECL are templates or
1494 non-templates. */
1495 is_template
1496 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1497 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1498
1499 if (need_template ^ is_template)
1500 return false;
1501 else if (is_template)
1502 {
1503 tree friend_parms;
1504 /* If both are templates, check the name of the two
1505 TEMPLATE_DECL's first because is_friend didn't. */
1506 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1507 != DECL_NAME (friend_decl))
1508 return false;
1509
1510 /* Now check template parameter list. */
1511 friend_parms
1512 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1513 args, tf_none);
1514 return comp_template_parms
1515 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1516 friend_parms);
1517 }
1518 else
1519 return (DECL_NAME (decl)
1520 == DECL_NAME (friend_decl));
1521 }
1522 }
1523 return false;
1524 }
1525
1526 /* Register the specialization SPEC as a specialization of TMPL with
1527 the indicated ARGS. IS_FRIEND indicates whether the specialization
1528 is actually just a friend declaration. ATTRLIST is the list of
1529 attributes that the specialization is declared with or NULL when
1530 it isn't. Returns SPEC, or an equivalent prior declaration, if
1531 available.
1532
1533 We also store instantiations of field packs in the hash table, even
1534 though they are not themselves templates, to make lookup easier. */
1535
1536 static tree
1537 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1538 hashval_t hash)
1539 {
1540 tree fn;
1541 spec_entry **slot = NULL;
1542 spec_entry elt;
1543
1544 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1545 || (TREE_CODE (tmpl) == FIELD_DECL
1546 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1547
1548 if (TREE_CODE (spec) == FUNCTION_DECL
1549 && uses_template_parms (DECL_TI_ARGS (spec)))
1550 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1551 register it; we want the corresponding TEMPLATE_DECL instead.
1552 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1553 the more obvious `uses_template_parms (spec)' to avoid problems
1554 with default function arguments. In particular, given
1555 something like this:
1556
1557 template <class T> void f(T t1, T t = T())
1558
1559 the default argument expression is not substituted for in an
1560 instantiation unless and until it is actually needed. */
1561 return spec;
1562
1563 if (optimize_specialization_lookup_p (tmpl))
1564 /* We don't put these specializations in the hash table, but we might
1565 want to give an error about a mismatch. */
1566 fn = retrieve_specialization (tmpl, args, 0);
1567 else
1568 {
1569 elt.tmpl = tmpl;
1570 elt.args = args;
1571 elt.spec = spec;
1572
1573 if (hash == 0)
1574 hash = spec_hasher::hash (&elt);
1575
1576 slot =
1577 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1578 if (*slot)
1579 fn = ((spec_entry *) *slot)->spec;
1580 else
1581 fn = NULL_TREE;
1582 }
1583
1584 /* We can sometimes try to re-register a specialization that we've
1585 already got. In particular, regenerate_decl_from_template calls
1586 duplicate_decls which will update the specialization list. But,
1587 we'll still get called again here anyhow. It's more convenient
1588 to simply allow this than to try to prevent it. */
1589 if (fn == spec)
1590 return spec;
1591 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1592 {
1593 if (DECL_TEMPLATE_INSTANTIATION (fn))
1594 {
1595 if (DECL_ODR_USED (fn)
1596 || DECL_EXPLICIT_INSTANTIATION (fn))
1597 {
1598 error ("specialization of %qD after instantiation",
1599 fn);
1600 return error_mark_node;
1601 }
1602 else
1603 {
1604 tree clone;
1605 /* This situation should occur only if the first
1606 specialization is an implicit instantiation, the
1607 second is an explicit specialization, and the
1608 implicit instantiation has not yet been used. That
1609 situation can occur if we have implicitly
1610 instantiated a member function and then specialized
1611 it later.
1612
1613 We can also wind up here if a friend declaration that
1614 looked like an instantiation turns out to be a
1615 specialization:
1616
1617 template <class T> void foo(T);
1618 class S { friend void foo<>(int) };
1619 template <> void foo(int);
1620
1621 We transform the existing DECL in place so that any
1622 pointers to it become pointers to the updated
1623 declaration.
1624
1625 If there was a definition for the template, but not
1626 for the specialization, we want this to look as if
1627 there were no definition, and vice versa. */
1628 DECL_INITIAL (fn) = NULL_TREE;
1629 duplicate_decls (spec, fn, is_friend);
1630 /* The call to duplicate_decls will have applied
1631 [temp.expl.spec]:
1632
1633 An explicit specialization of a function template
1634 is inline only if it is explicitly declared to be,
1635 and independently of whether its function template
1636 is.
1637
1638 to the primary function; now copy the inline bits to
1639 the various clones. */
1640 FOR_EACH_CLONE (clone, fn)
1641 {
1642 DECL_DECLARED_INLINE_P (clone)
1643 = DECL_DECLARED_INLINE_P (fn);
1644 DECL_SOURCE_LOCATION (clone)
1645 = DECL_SOURCE_LOCATION (fn);
1646 DECL_DELETED_FN (clone)
1647 = DECL_DELETED_FN (fn);
1648 }
1649 check_specialization_namespace (tmpl);
1650
1651 return fn;
1652 }
1653 }
1654 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1655 {
1656 tree dd = duplicate_decls (spec, fn, is_friend);
1657 if (dd == error_mark_node)
1658 /* We've already complained in duplicate_decls. */
1659 return error_mark_node;
1660
1661 if (dd == NULL_TREE && DECL_INITIAL (spec))
1662 /* Dup decl failed, but this is a new definition. Set the
1663 line number so any errors match this new
1664 definition. */
1665 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1666
1667 return fn;
1668 }
1669 }
1670 else if (fn)
1671 return duplicate_decls (spec, fn, is_friend);
1672
1673 /* A specialization must be declared in the same namespace as the
1674 template it is specializing. */
1675 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1676 && !check_specialization_namespace (tmpl))
1677 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1678
1679 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1680 {
1681 spec_entry *entry = ggc_alloc<spec_entry> ();
1682 gcc_assert (tmpl && args && spec);
1683 *entry = elt;
1684 *slot = entry;
1685 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1686 && PRIMARY_TEMPLATE_P (tmpl)
1687 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1688 || variable_template_p (tmpl))
1689 /* If TMPL is a forward declaration of a template function, keep a list
1690 of all specializations in case we need to reassign them to a friend
1691 template later in tsubst_friend_function.
1692
1693 Also keep a list of all variable template instantiations so that
1694 process_partial_specialization can check whether a later partial
1695 specialization would have used it. */
1696 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1697 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1698 }
1699
1700 return spec;
1701 }
1702
1703 /* Returns true iff two spec_entry nodes are equivalent. */
1704
1705 int comparing_specializations;
1706
1707 bool
1708 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1709 {
1710 int equal;
1711
1712 ++comparing_specializations;
1713 equal = (e1->tmpl == e2->tmpl
1714 && comp_template_args (e1->args, e2->args));
1715 if (equal && flag_concepts
1716 /* tmpl could be a FIELD_DECL for a capture pack. */
1717 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1718 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1719 && uses_template_parms (e1->args))
1720 {
1721 /* Partial specializations of a variable template can be distinguished by
1722 constraints. */
1723 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1724 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1725 equal = equivalent_constraints (c1, c2);
1726 }
1727 --comparing_specializations;
1728
1729 return equal;
1730 }
1731
1732 /* Returns a hash for a template TMPL and template arguments ARGS. */
1733
1734 static hashval_t
1735 hash_tmpl_and_args (tree tmpl, tree args)
1736 {
1737 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1738 return iterative_hash_template_arg (args, val);
1739 }
1740
1741 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1742 ignoring SPEC. */
1743
1744 hashval_t
1745 spec_hasher::hash (spec_entry *e)
1746 {
1747 return hash_tmpl_and_args (e->tmpl, e->args);
1748 }
1749
1750 /* Recursively calculate a hash value for a template argument ARG, for use
1751 in the hash tables of template specializations. We must be
1752 careful to (at least) skip the same entities template_args_equal
1753 does. */
1754
1755 hashval_t
1756 iterative_hash_template_arg (tree arg, hashval_t val)
1757 {
1758 if (arg == NULL_TREE)
1759 return iterative_hash_object (arg, val);
1760
1761 if (!TYPE_P (arg))
1762 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1763 while (CONVERT_EXPR_P (arg)
1764 || TREE_CODE (arg) == NON_LVALUE_EXPR
1765 || class_nttp_const_wrapper_p (arg))
1766 arg = TREE_OPERAND (arg, 0);
1767
1768 enum tree_code code = TREE_CODE (arg);
1769
1770 val = iterative_hash_object (code, val);
1771
1772 switch (code)
1773 {
1774 case ARGUMENT_PACK_SELECT:
1775 gcc_unreachable ();
1776
1777 case ERROR_MARK:
1778 return val;
1779
1780 case IDENTIFIER_NODE:
1781 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1782
1783 case TREE_VEC:
1784 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1785 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1786 return val;
1787
1788 case TYPE_PACK_EXPANSION:
1789 case EXPR_PACK_EXPANSION:
1790 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1791 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1792
1793 case TYPE_ARGUMENT_PACK:
1794 case NONTYPE_ARGUMENT_PACK:
1795 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1796
1797 case TREE_LIST:
1798 for (; arg; arg = TREE_CHAIN (arg))
1799 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1800 return val;
1801
1802 case OVERLOAD:
1803 for (lkp_iterator iter (arg); iter; ++iter)
1804 val = iterative_hash_template_arg (*iter, val);
1805 return val;
1806
1807 case CONSTRUCTOR:
1808 {
1809 tree field, value;
1810 unsigned i;
1811 iterative_hash_template_arg (TREE_TYPE (arg), val);
1812 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1813 {
1814 val = iterative_hash_template_arg (field, val);
1815 val = iterative_hash_template_arg (value, val);
1816 }
1817 return val;
1818 }
1819
1820 case PARM_DECL:
1821 if (!DECL_ARTIFICIAL (arg))
1822 {
1823 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1824 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1825 }
1826 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1827
1828 case TARGET_EXPR:
1829 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1830
1831 case PTRMEM_CST:
1832 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1833 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1834
1835 case TEMPLATE_PARM_INDEX:
1836 val = iterative_hash_template_arg
1837 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1838 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1839 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1840
1841 case TRAIT_EXPR:
1842 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1843 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1844 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1845
1846 case BASELINK:
1847 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1848 val);
1849 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1850 val);
1851
1852 case MODOP_EXPR:
1853 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1854 code = TREE_CODE (TREE_OPERAND (arg, 1));
1855 val = iterative_hash_object (code, val);
1856 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1857
1858 case LAMBDA_EXPR:
1859 /* [temp.over.link] Two lambda-expressions are never considered
1860 equivalent.
1861
1862 So just hash the closure type. */
1863 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1864
1865 case CAST_EXPR:
1866 case IMPLICIT_CONV_EXPR:
1867 case STATIC_CAST_EXPR:
1868 case REINTERPRET_CAST_EXPR:
1869 case CONST_CAST_EXPR:
1870 case DYNAMIC_CAST_EXPR:
1871 case NEW_EXPR:
1872 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1873 /* Now hash operands as usual. */
1874 break;
1875
1876 case CALL_EXPR:
1877 {
1878 tree fn = CALL_EXPR_FN (arg);
1879 if (tree name = dependent_name (fn))
1880 {
1881 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1882 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1883 fn = name;
1884 }
1885 val = iterative_hash_template_arg (fn, val);
1886 call_expr_arg_iterator ai;
1887 for (tree x = first_call_expr_arg (arg, &ai); x;
1888 x = next_call_expr_arg (&ai))
1889 val = iterative_hash_template_arg (x, val);
1890 return val;
1891 }
1892
1893 default:
1894 break;
1895 }
1896
1897 char tclass = TREE_CODE_CLASS (code);
1898 switch (tclass)
1899 {
1900 case tcc_type:
1901 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1902 {
1903 // We want an alias specialization that survived strip_typedefs
1904 // to hash differently from its TYPE_CANONICAL, to avoid hash
1905 // collisions that compare as different in template_args_equal.
1906 // These could be dependent specializations that strip_typedefs
1907 // left alone, or untouched specializations because
1908 // coerce_template_parms returns the unconverted template
1909 // arguments if it sees incomplete argument packs.
1910 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1911 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1912 }
1913
1914 switch (TREE_CODE (arg))
1915 {
1916 case TEMPLATE_TEMPLATE_PARM:
1917 {
1918 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1919
1920 /* Do not recurse with TPI directly, as that is unbounded
1921 recursion. */
1922 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1923 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1924 }
1925 break;
1926
1927 case DECLTYPE_TYPE:
1928 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1929 break;
1930
1931 default:
1932 if (tree canonical = TYPE_CANONICAL (arg))
1933 val = iterative_hash_object (TYPE_HASH (canonical), val);
1934 break;
1935 }
1936
1937 return val;
1938
1939 case tcc_declaration:
1940 case tcc_constant:
1941 return iterative_hash_expr (arg, val);
1942
1943 default:
1944 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1945 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1946 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1947 return val;
1948 }
1949
1950 gcc_unreachable ();
1951 return 0;
1952 }
1953
1954 /* Unregister the specialization SPEC as a specialization of TMPL.
1955 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1956 if the SPEC was listed as a specialization of TMPL.
1957
1958 Note that SPEC has been ggc_freed, so we can't look inside it. */
1959
1960 bool
1961 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1962 {
1963 spec_entry *entry;
1964 spec_entry elt;
1965
1966 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1967 elt.args = TI_ARGS (tinfo);
1968 elt.spec = NULL_TREE;
1969
1970 entry = decl_specializations->find (&elt);
1971 if (entry != NULL)
1972 {
1973 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1974 gcc_assert (new_spec != NULL_TREE);
1975 entry->spec = new_spec;
1976 return 1;
1977 }
1978
1979 return 0;
1980 }
1981
1982 /* Like register_specialization, but for local declarations. We are
1983 registering SPEC, an instantiation of TMPL. */
1984
1985 void
1986 register_local_specialization (tree spec, tree tmpl)
1987 {
1988 gcc_assert (tmpl != spec);
1989 local_specializations->put (tmpl, spec);
1990 }
1991
1992 /* TYPE is a class type. Returns true if TYPE is an explicitly
1993 specialized class. */
1994
1995 bool
1996 explicit_class_specialization_p (tree type)
1997 {
1998 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1999 return false;
2000 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2001 }
2002
2003 /* Print the list of functions at FNS, going through all the overloads
2004 for each element of the list. Alternatively, FNS cannot be a
2005 TREE_LIST, in which case it will be printed together with all the
2006 overloads.
2007
2008 MORE and *STR should respectively be FALSE and NULL when the function
2009 is called from the outside. They are used internally on recursive
2010 calls. print_candidates manages the two parameters and leaves NULL
2011 in *STR when it ends. */
2012
2013 static void
2014 print_candidates_1 (tree fns, char **str, bool more = false)
2015 {
2016 if (TREE_CODE (fns) == TREE_LIST)
2017 for (; fns; fns = TREE_CHAIN (fns))
2018 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2019 else
2020 for (lkp_iterator iter (fns); iter;)
2021 {
2022 tree cand = *iter;
2023 ++iter;
2024
2025 const char *pfx = *str;
2026 if (!pfx)
2027 {
2028 if (more || iter)
2029 pfx = _("candidates are:");
2030 else
2031 pfx = _("candidate is:");
2032 *str = get_spaces (pfx);
2033 }
2034 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2035 }
2036 }
2037
2038 /* Print the list of candidate FNS in an error message. FNS can also
2039 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2040
2041 void
2042 print_candidates (tree fns)
2043 {
2044 char *str = NULL;
2045 print_candidates_1 (fns, &str);
2046 free (str);
2047 }
2048
2049 /* Get a (possibly) constrained template declaration for the
2050 purpose of ordering candidates. */
2051 static tree
2052 get_template_for_ordering (tree list)
2053 {
2054 gcc_assert (TREE_CODE (list) == TREE_LIST);
2055 tree f = TREE_VALUE (list);
2056 if (tree ti = DECL_TEMPLATE_INFO (f))
2057 return TI_TEMPLATE (ti);
2058 return f;
2059 }
2060
2061 /* Among candidates having the same signature, return the
2062 most constrained or NULL_TREE if there is no best candidate.
2063 If the signatures of candidates vary (e.g., template
2064 specialization vs. member function), then there can be no
2065 most constrained.
2066
2067 Note that we don't compare constraints on the functions
2068 themselves, but rather those of their templates. */
2069 static tree
2070 most_constrained_function (tree candidates)
2071 {
2072 // Try to find the best candidate in a first pass.
2073 tree champ = candidates;
2074 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2075 {
2076 int winner = more_constrained (get_template_for_ordering (champ),
2077 get_template_for_ordering (c));
2078 if (winner == -1)
2079 champ = c; // The candidate is more constrained
2080 else if (winner == 0)
2081 return NULL_TREE; // Neither is more constrained
2082 }
2083
2084 // Verify that the champ is better than previous candidates.
2085 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2086 if (!more_constrained (get_template_for_ordering (champ),
2087 get_template_for_ordering (c)))
2088 return NULL_TREE;
2089 }
2090
2091 return champ;
2092 }
2093
2094
2095 /* Returns the template (one of the functions given by TEMPLATE_ID)
2096 which can be specialized to match the indicated DECL with the
2097 explicit template args given in TEMPLATE_ID. The DECL may be
2098 NULL_TREE if none is available. In that case, the functions in
2099 TEMPLATE_ID are non-members.
2100
2101 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2102 specialization of a member template.
2103
2104 The TEMPLATE_COUNT is the number of references to qualifying
2105 template classes that appeared in the name of the function. See
2106 check_explicit_specialization for a more accurate description.
2107
2108 TSK indicates what kind of template declaration (if any) is being
2109 declared. TSK_TEMPLATE indicates that the declaration given by
2110 DECL, though a FUNCTION_DECL, has template parameters, and is
2111 therefore a template function.
2112
2113 The template args (those explicitly specified and those deduced)
2114 are output in a newly created vector *TARGS_OUT.
2115
2116 If it is impossible to determine the result, an error message is
2117 issued. The error_mark_node is returned to indicate failure. */
2118
2119 static tree
2120 determine_specialization (tree template_id,
2121 tree decl,
2122 tree* targs_out,
2123 int need_member_template,
2124 int template_count,
2125 tmpl_spec_kind tsk)
2126 {
2127 tree fns;
2128 tree targs;
2129 tree explicit_targs;
2130 tree candidates = NULL_TREE;
2131
2132 /* A TREE_LIST of templates of which DECL may be a specialization.
2133 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2134 corresponding TREE_PURPOSE is the set of template arguments that,
2135 when used to instantiate the template, would produce a function
2136 with the signature of DECL. */
2137 tree templates = NULL_TREE;
2138 int header_count;
2139 cp_binding_level *b;
2140
2141 *targs_out = NULL_TREE;
2142
2143 if (template_id == error_mark_node || decl == error_mark_node)
2144 return error_mark_node;
2145
2146 /* We shouldn't be specializing a member template of an
2147 unspecialized class template; we already gave an error in
2148 check_specialization_scope, now avoid crashing. */
2149 if (!VAR_P (decl)
2150 && template_count && DECL_CLASS_SCOPE_P (decl)
2151 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2152 {
2153 gcc_assert (errorcount);
2154 return error_mark_node;
2155 }
2156
2157 fns = TREE_OPERAND (template_id, 0);
2158 explicit_targs = TREE_OPERAND (template_id, 1);
2159
2160 if (fns == error_mark_node)
2161 return error_mark_node;
2162
2163 /* Check for baselinks. */
2164 if (BASELINK_P (fns))
2165 fns = BASELINK_FUNCTIONS (fns);
2166
2167 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2168 {
2169 error_at (DECL_SOURCE_LOCATION (decl),
2170 "%qD is not a function template", fns);
2171 return error_mark_node;
2172 }
2173 else if (VAR_P (decl) && !variable_template_p (fns))
2174 {
2175 error ("%qD is not a variable template", fns);
2176 return error_mark_node;
2177 }
2178
2179 /* Count the number of template headers specified for this
2180 specialization. */
2181 header_count = 0;
2182 for (b = current_binding_level;
2183 b->kind == sk_template_parms;
2184 b = b->level_chain)
2185 ++header_count;
2186
2187 tree orig_fns = fns;
2188
2189 if (variable_template_p (fns))
2190 {
2191 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2192 targs = coerce_template_parms (parms, explicit_targs, fns,
2193 tf_warning_or_error,
2194 /*req_all*/true, /*use_defarg*/true);
2195 if (targs != error_mark_node)
2196 templates = tree_cons (targs, fns, templates);
2197 }
2198 else for (lkp_iterator iter (fns); iter; ++iter)
2199 {
2200 tree fn = *iter;
2201
2202 if (TREE_CODE (fn) == TEMPLATE_DECL)
2203 {
2204 tree decl_arg_types;
2205 tree fn_arg_types;
2206 tree insttype;
2207
2208 /* In case of explicit specialization, we need to check if
2209 the number of template headers appearing in the specialization
2210 is correct. This is usually done in check_explicit_specialization,
2211 but the check done there cannot be exhaustive when specializing
2212 member functions. Consider the following code:
2213
2214 template <> void A<int>::f(int);
2215 template <> template <> void A<int>::f(int);
2216
2217 Assuming that A<int> is not itself an explicit specialization
2218 already, the first line specializes "f" which is a non-template
2219 member function, whilst the second line specializes "f" which
2220 is a template member function. So both lines are syntactically
2221 correct, and check_explicit_specialization does not reject
2222 them.
2223
2224 Here, we can do better, as we are matching the specialization
2225 against the declarations. We count the number of template
2226 headers, and we check if they match TEMPLATE_COUNT + 1
2227 (TEMPLATE_COUNT is the number of qualifying template classes,
2228 plus there must be another header for the member template
2229 itself).
2230
2231 Notice that if header_count is zero, this is not a
2232 specialization but rather a template instantiation, so there
2233 is no check we can perform here. */
2234 if (header_count && header_count != template_count + 1)
2235 continue;
2236
2237 /* Check that the number of template arguments at the
2238 innermost level for DECL is the same as for FN. */
2239 if (current_binding_level->kind == sk_template_parms
2240 && !current_binding_level->explicit_spec_p
2241 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2242 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2243 (current_template_parms))))
2244 continue;
2245
2246 /* DECL might be a specialization of FN. */
2247 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2248 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2249
2250 /* For a non-static member function, we need to make sure
2251 that the const qualification is the same. Since
2252 get_bindings does not try to merge the "this" parameter,
2253 we must do the comparison explicitly. */
2254 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2255 {
2256 if (!same_type_p (TREE_VALUE (fn_arg_types),
2257 TREE_VALUE (decl_arg_types)))
2258 continue;
2259
2260 /* And the ref-qualification. */
2261 if (type_memfn_rqual (TREE_TYPE (decl))
2262 != type_memfn_rqual (TREE_TYPE (fn)))
2263 continue;
2264 }
2265
2266 /* Skip the "this" parameter and, for constructors of
2267 classes with virtual bases, the VTT parameter. A
2268 full specialization of a constructor will have a VTT
2269 parameter, but a template never will. */
2270 decl_arg_types
2271 = skip_artificial_parms_for (decl, decl_arg_types);
2272 fn_arg_types
2273 = skip_artificial_parms_for (fn, fn_arg_types);
2274
2275 /* Function templates cannot be specializations; there are
2276 no partial specializations of functions. Therefore, if
2277 the type of DECL does not match FN, there is no
2278 match.
2279
2280 Note that it should never be the case that we have both
2281 candidates added here, and for regular member functions
2282 below. */
2283 if (tsk == tsk_template)
2284 {
2285 if (compparms (fn_arg_types, decl_arg_types))
2286 candidates = tree_cons (NULL_TREE, fn, candidates);
2287 continue;
2288 }
2289
2290 /* See whether this function might be a specialization of this
2291 template. Suppress access control because we might be trying
2292 to make this specialization a friend, and we have already done
2293 access control for the declaration of the specialization. */
2294 push_deferring_access_checks (dk_no_check);
2295 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2296 pop_deferring_access_checks ();
2297
2298 if (!targs)
2299 /* We cannot deduce template arguments that when used to
2300 specialize TMPL will produce DECL. */
2301 continue;
2302
2303 if (uses_template_parms (targs))
2304 /* We deduced something involving 'auto', which isn't a valid
2305 template argument. */
2306 continue;
2307
2308 /* Remove, from the set of candidates, all those functions
2309 whose constraints are not satisfied. */
2310 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2311 continue;
2312
2313 // Then, try to form the new function type.
2314 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2315 if (insttype == error_mark_node)
2316 continue;
2317 fn_arg_types
2318 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2319 if (!compparms (fn_arg_types, decl_arg_types))
2320 continue;
2321
2322 /* Save this template, and the arguments deduced. */
2323 templates = tree_cons (targs, fn, templates);
2324 }
2325 else if (need_member_template)
2326 /* FN is an ordinary member function, and we need a
2327 specialization of a member template. */
2328 ;
2329 else if (TREE_CODE (fn) != FUNCTION_DECL)
2330 /* We can get IDENTIFIER_NODEs here in certain erroneous
2331 cases. */
2332 ;
2333 else if (!DECL_FUNCTION_MEMBER_P (fn))
2334 /* This is just an ordinary non-member function. Nothing can
2335 be a specialization of that. */
2336 ;
2337 else if (DECL_ARTIFICIAL (fn))
2338 /* Cannot specialize functions that are created implicitly. */
2339 ;
2340 else
2341 {
2342 tree decl_arg_types;
2343
2344 /* This is an ordinary member function. However, since
2345 we're here, we can assume its enclosing class is a
2346 template class. For example,
2347
2348 template <typename T> struct S { void f(); };
2349 template <> void S<int>::f() {}
2350
2351 Here, S<int>::f is a non-template, but S<int> is a
2352 template class. If FN has the same type as DECL, we
2353 might be in business. */
2354
2355 if (!DECL_TEMPLATE_INFO (fn))
2356 /* Its enclosing class is an explicit specialization
2357 of a template class. This is not a candidate. */
2358 continue;
2359
2360 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2361 TREE_TYPE (TREE_TYPE (fn))))
2362 /* The return types differ. */
2363 continue;
2364
2365 /* Adjust the type of DECL in case FN is a static member. */
2366 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2367 if (DECL_STATIC_FUNCTION_P (fn)
2368 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2369 decl_arg_types = TREE_CHAIN (decl_arg_types);
2370
2371 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2372 decl_arg_types))
2373 continue;
2374
2375 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2376 && (type_memfn_rqual (TREE_TYPE (decl))
2377 != type_memfn_rqual (TREE_TYPE (fn))))
2378 continue;
2379
2380 // If the deduced arguments do not satisfy the constraints,
2381 // this is not a candidate.
2382 if (flag_concepts && !constraints_satisfied_p (fn))
2383 continue;
2384
2385 // Add the candidate.
2386 candidates = tree_cons (NULL_TREE, fn, candidates);
2387 }
2388 }
2389
2390 if (templates && TREE_CHAIN (templates))
2391 {
2392 /* We have:
2393
2394 [temp.expl.spec]
2395
2396 It is possible for a specialization with a given function
2397 signature to be instantiated from more than one function
2398 template. In such cases, explicit specification of the
2399 template arguments must be used to uniquely identify the
2400 function template specialization being specialized.
2401
2402 Note that here, there's no suggestion that we're supposed to
2403 determine which of the candidate templates is most
2404 specialized. However, we, also have:
2405
2406 [temp.func.order]
2407
2408 Partial ordering of overloaded function template
2409 declarations is used in the following contexts to select
2410 the function template to which a function template
2411 specialization refers:
2412
2413 -- when an explicit specialization refers to a function
2414 template.
2415
2416 So, we do use the partial ordering rules, at least for now.
2417 This extension can only serve to make invalid programs valid,
2418 so it's safe. And, there is strong anecdotal evidence that
2419 the committee intended the partial ordering rules to apply;
2420 the EDG front end has that behavior, and John Spicer claims
2421 that the committee simply forgot to delete the wording in
2422 [temp.expl.spec]. */
2423 tree tmpl = most_specialized_instantiation (templates);
2424 if (tmpl != error_mark_node)
2425 {
2426 templates = tmpl;
2427 TREE_CHAIN (templates) = NULL_TREE;
2428 }
2429 }
2430
2431 // Concepts allows multiple declarations of member functions
2432 // with the same signature. Like above, we need to rely on
2433 // on the partial ordering of those candidates to determine which
2434 // is the best.
2435 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2436 {
2437 if (tree cand = most_constrained_function (candidates))
2438 {
2439 candidates = cand;
2440 TREE_CHAIN (cand) = NULL_TREE;
2441 }
2442 }
2443
2444 if (templates == NULL_TREE && candidates == NULL_TREE)
2445 {
2446 error ("template-id %qD for %q+D does not match any template "
2447 "declaration", template_id, decl);
2448 if (header_count && header_count != template_count + 1)
2449 inform (DECL_SOURCE_LOCATION (decl),
2450 "saw %d %<template<>%>, need %d for "
2451 "specializing a member function template",
2452 header_count, template_count + 1);
2453 else
2454 print_candidates (orig_fns);
2455 return error_mark_node;
2456 }
2457 else if ((templates && TREE_CHAIN (templates))
2458 || (candidates && TREE_CHAIN (candidates))
2459 || (templates && candidates))
2460 {
2461 error ("ambiguous template specialization %qD for %q+D",
2462 template_id, decl);
2463 candidates = chainon (candidates, templates);
2464 print_candidates (candidates);
2465 return error_mark_node;
2466 }
2467
2468 /* We have one, and exactly one, match. */
2469 if (candidates)
2470 {
2471 tree fn = TREE_VALUE (candidates);
2472 *targs_out = copy_node (DECL_TI_ARGS (fn));
2473
2474 /* Propagate the candidate's constraints to the declaration. */
2475 set_constraints (decl, get_constraints (fn));
2476
2477 /* DECL is a re-declaration or partial instantiation of a template
2478 function. */
2479 if (TREE_CODE (fn) == TEMPLATE_DECL)
2480 return fn;
2481 /* It was a specialization of an ordinary member function in a
2482 template class. */
2483 return DECL_TI_TEMPLATE (fn);
2484 }
2485
2486 /* It was a specialization of a template. */
2487 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2488 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2489 {
2490 *targs_out = copy_node (targs);
2491 SET_TMPL_ARGS_LEVEL (*targs_out,
2492 TMPL_ARGS_DEPTH (*targs_out),
2493 TREE_PURPOSE (templates));
2494 }
2495 else
2496 *targs_out = TREE_PURPOSE (templates);
2497 return TREE_VALUE (templates);
2498 }
2499
2500 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2501 but with the default argument values filled in from those in the
2502 TMPL_TYPES. */
2503
2504 static tree
2505 copy_default_args_to_explicit_spec_1 (tree spec_types,
2506 tree tmpl_types)
2507 {
2508 tree new_spec_types;
2509
2510 if (!spec_types)
2511 return NULL_TREE;
2512
2513 if (spec_types == void_list_node)
2514 return void_list_node;
2515
2516 /* Substitute into the rest of the list. */
2517 new_spec_types =
2518 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2519 TREE_CHAIN (tmpl_types));
2520
2521 /* Add the default argument for this parameter. */
2522 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2523 TREE_VALUE (spec_types),
2524 new_spec_types);
2525 }
2526
2527 /* DECL is an explicit specialization. Replicate default arguments
2528 from the template it specializes. (That way, code like:
2529
2530 template <class T> void f(T = 3);
2531 template <> void f(double);
2532 void g () { f (); }
2533
2534 works, as required.) An alternative approach would be to look up
2535 the correct default arguments at the call-site, but this approach
2536 is consistent with how implicit instantiations are handled. */
2537
2538 static void
2539 copy_default_args_to_explicit_spec (tree decl)
2540 {
2541 tree tmpl;
2542 tree spec_types;
2543 tree tmpl_types;
2544 tree new_spec_types;
2545 tree old_type;
2546 tree new_type;
2547 tree t;
2548 tree object_type = NULL_TREE;
2549 tree in_charge = NULL_TREE;
2550 tree vtt = NULL_TREE;
2551
2552 /* See if there's anything we need to do. */
2553 tmpl = DECL_TI_TEMPLATE (decl);
2554 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2555 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2556 if (TREE_PURPOSE (t))
2557 break;
2558 if (!t)
2559 return;
2560
2561 old_type = TREE_TYPE (decl);
2562 spec_types = TYPE_ARG_TYPES (old_type);
2563
2564 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2565 {
2566 /* Remove the this pointer, but remember the object's type for
2567 CV quals. */
2568 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2569 spec_types = TREE_CHAIN (spec_types);
2570 tmpl_types = TREE_CHAIN (tmpl_types);
2571
2572 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2573 {
2574 /* DECL may contain more parameters than TMPL due to the extra
2575 in-charge parameter in constructors and destructors. */
2576 in_charge = spec_types;
2577 spec_types = TREE_CHAIN (spec_types);
2578 }
2579 if (DECL_HAS_VTT_PARM_P (decl))
2580 {
2581 vtt = spec_types;
2582 spec_types = TREE_CHAIN (spec_types);
2583 }
2584 }
2585
2586 /* Compute the merged default arguments. */
2587 new_spec_types =
2588 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2589
2590 /* Compute the new FUNCTION_TYPE. */
2591 if (object_type)
2592 {
2593 if (vtt)
2594 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2595 TREE_VALUE (vtt),
2596 new_spec_types);
2597
2598 if (in_charge)
2599 /* Put the in-charge parameter back. */
2600 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2601 TREE_VALUE (in_charge),
2602 new_spec_types);
2603
2604 new_type = build_method_type_directly (object_type,
2605 TREE_TYPE (old_type),
2606 new_spec_types);
2607 }
2608 else
2609 new_type = build_function_type (TREE_TYPE (old_type),
2610 new_spec_types);
2611 new_type = cp_build_type_attribute_variant (new_type,
2612 TYPE_ATTRIBUTES (old_type));
2613 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2614
2615 TREE_TYPE (decl) = new_type;
2616 }
2617
2618 /* Return the number of template headers we expect to see for a definition
2619 or specialization of CTYPE or one of its non-template members. */
2620
2621 int
2622 num_template_headers_for_class (tree ctype)
2623 {
2624 int num_templates = 0;
2625
2626 while (ctype && CLASS_TYPE_P (ctype))
2627 {
2628 /* You're supposed to have one `template <...>' for every
2629 template class, but you don't need one for a full
2630 specialization. For example:
2631
2632 template <class T> struct S{};
2633 template <> struct S<int> { void f(); };
2634 void S<int>::f () {}
2635
2636 is correct; there shouldn't be a `template <>' for the
2637 definition of `S<int>::f'. */
2638 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2639 /* If CTYPE does not have template information of any
2640 kind, then it is not a template, nor is it nested
2641 within a template. */
2642 break;
2643 if (explicit_class_specialization_p (ctype))
2644 break;
2645 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2646 ++num_templates;
2647
2648 ctype = TYPE_CONTEXT (ctype);
2649 }
2650
2651 return num_templates;
2652 }
2653
2654 /* Do a simple sanity check on the template headers that precede the
2655 variable declaration DECL. */
2656
2657 void
2658 check_template_variable (tree decl)
2659 {
2660 tree ctx = CP_DECL_CONTEXT (decl);
2661 int wanted = num_template_headers_for_class (ctx);
2662 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2663 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2664 {
2665 if (cxx_dialect < cxx14)
2666 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2667 "variable templates only available with "
2668 "%<-std=c++14%> or %<-std=gnu++14%>");
2669
2670 // Namespace-scope variable templates should have a template header.
2671 ++wanted;
2672 }
2673 if (template_header_count > wanted)
2674 {
2675 auto_diagnostic_group d;
2676 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2677 "too many template headers for %qD "
2678 "(should be %d)",
2679 decl, wanted);
2680 if (warned && CLASS_TYPE_P (ctx)
2681 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2682 inform (DECL_SOURCE_LOCATION (decl),
2683 "members of an explicitly specialized class are defined "
2684 "without a template header");
2685 }
2686 }
2687
2688 /* An explicit specialization whose declarator-id or class-head-name is not
2689 qualified shall be declared in the nearest enclosing namespace of the
2690 template, or, if the namespace is inline (7.3.1), any namespace from its
2691 enclosing namespace set.
2692
2693 If the name declared in the explicit instantiation is an unqualified name,
2694 the explicit instantiation shall appear in the namespace where its template
2695 is declared or, if that namespace is inline (7.3.1), any namespace from its
2696 enclosing namespace set. */
2697
2698 void
2699 check_unqualified_spec_or_inst (tree t, location_t loc)
2700 {
2701 tree tmpl = most_general_template (t);
2702 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2703 && !is_nested_namespace (current_namespace,
2704 CP_DECL_CONTEXT (tmpl), true))
2705 {
2706 if (processing_specialization)
2707 permerror (loc, "explicit specialization of %qD outside its "
2708 "namespace must use a nested-name-specifier", tmpl);
2709 else if (processing_explicit_instantiation
2710 && cxx_dialect >= cxx11)
2711 /* This was allowed in C++98, so only pedwarn. */
2712 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2713 "outside its namespace must use a nested-name-"
2714 "specifier", tmpl);
2715 }
2716 }
2717
2718 /* Warn for a template specialization SPEC that is missing some of a set
2719 of function or type attributes that the template TEMPL is declared with.
2720 ATTRLIST is a list of additional attributes that SPEC should be taken
2721 to ultimately be declared with. */
2722
2723 static void
2724 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2725 {
2726 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2727 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2728
2729 /* Avoid warning if the difference between the primary and
2730 the specialization is not in one of the attributes below. */
2731 const char* const blacklist[] = {
2732 "alloc_align", "alloc_size", "assume_aligned", "format",
2733 "format_arg", "malloc", "nonnull", NULL
2734 };
2735
2736 /* Put together a list of the black listed attributes that the primary
2737 template is declared with that the specialization is not, in case
2738 it's not apparent from the most recent declaration of the primary. */
2739 pretty_printer str;
2740 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2741 blacklist, &str);
2742
2743 if (!nattrs)
2744 return;
2745
2746 auto_diagnostic_group d;
2747 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2748 "explicit specialization %q#D may be missing attributes",
2749 spec))
2750 inform (DECL_SOURCE_LOCATION (tmpl),
2751 nattrs > 1
2752 ? G_("missing primary template attributes %s")
2753 : G_("missing primary template attribute %s"),
2754 pp_formatted_text (&str));
2755 }
2756
2757 /* Check to see if the function just declared, as indicated in
2758 DECLARATOR, and in DECL, is a specialization of a function
2759 template. We may also discover that the declaration is an explicit
2760 instantiation at this point.
2761
2762 Returns DECL, or an equivalent declaration that should be used
2763 instead if all goes well. Issues an error message if something is
2764 amiss. Returns error_mark_node if the error is not easily
2765 recoverable.
2766
2767 FLAGS is a bitmask consisting of the following flags:
2768
2769 2: The function has a definition.
2770 4: The function is a friend.
2771
2772 The TEMPLATE_COUNT is the number of references to qualifying
2773 template classes that appeared in the name of the function. For
2774 example, in
2775
2776 template <class T> struct S { void f(); };
2777 void S<int>::f();
2778
2779 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2780 classes are not counted in the TEMPLATE_COUNT, so that in
2781
2782 template <class T> struct S {};
2783 template <> struct S<int> { void f(); }
2784 template <> void S<int>::f();
2785
2786 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2787 invalid; there should be no template <>.)
2788
2789 If the function is a specialization, it is marked as such via
2790 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2791 is set up correctly, and it is added to the list of specializations
2792 for that template. */
2793
2794 tree
2795 check_explicit_specialization (tree declarator,
2796 tree decl,
2797 int template_count,
2798 int flags,
2799 tree attrlist)
2800 {
2801 int have_def = flags & 2;
2802 int is_friend = flags & 4;
2803 bool is_concept = flags & 8;
2804 int specialization = 0;
2805 int explicit_instantiation = 0;
2806 int member_specialization = 0;
2807 tree ctype = DECL_CLASS_CONTEXT (decl);
2808 tree dname = DECL_NAME (decl);
2809 tmpl_spec_kind tsk;
2810
2811 if (is_friend)
2812 {
2813 if (!processing_specialization)
2814 tsk = tsk_none;
2815 else
2816 tsk = tsk_excessive_parms;
2817 }
2818 else
2819 tsk = current_tmpl_spec_kind (template_count);
2820
2821 switch (tsk)
2822 {
2823 case tsk_none:
2824 if (processing_specialization && !VAR_P (decl))
2825 {
2826 specialization = 1;
2827 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2828 }
2829 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2830 {
2831 if (is_friend)
2832 /* This could be something like:
2833
2834 template <class T> void f(T);
2835 class S { friend void f<>(int); } */
2836 specialization = 1;
2837 else
2838 {
2839 /* This case handles bogus declarations like template <>
2840 template <class T> void f<int>(); */
2841
2842 error_at (cp_expr_loc_or_input_loc (declarator),
2843 "template-id %qE in declaration of primary template",
2844 declarator);
2845 return decl;
2846 }
2847 }
2848 break;
2849
2850 case tsk_invalid_member_spec:
2851 /* The error has already been reported in
2852 check_specialization_scope. */
2853 return error_mark_node;
2854
2855 case tsk_invalid_expl_inst:
2856 error ("template parameter list used in explicit instantiation");
2857
2858 /* Fall through. */
2859
2860 case tsk_expl_inst:
2861 if (have_def)
2862 error ("definition provided for explicit instantiation");
2863
2864 explicit_instantiation = 1;
2865 break;
2866
2867 case tsk_excessive_parms:
2868 case tsk_insufficient_parms:
2869 if (tsk == tsk_excessive_parms)
2870 error ("too many template parameter lists in declaration of %qD",
2871 decl);
2872 else if (template_header_count)
2873 error("too few template parameter lists in declaration of %qD", decl);
2874 else
2875 error("explicit specialization of %qD must be introduced by "
2876 "%<template <>%>", decl);
2877
2878 /* Fall through. */
2879 case tsk_expl_spec:
2880 if (is_concept)
2881 error ("explicit specialization declared %<concept%>");
2882
2883 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2884 /* In cases like template<> constexpr bool v = true;
2885 We'll give an error in check_template_variable. */
2886 break;
2887
2888 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2889 if (ctype)
2890 member_specialization = 1;
2891 else
2892 specialization = 1;
2893 break;
2894
2895 case tsk_template:
2896 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2897 {
2898 /* This case handles bogus declarations like template <>
2899 template <class T> void f<int>(); */
2900
2901 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2902 error_at (cp_expr_loc_or_input_loc (declarator),
2903 "template-id %qE in declaration of primary template",
2904 declarator);
2905 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2906 {
2907 /* Partial specialization of variable template. */
2908 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2909 specialization = 1;
2910 goto ok;
2911 }
2912 else if (cxx_dialect < cxx14)
2913 error_at (cp_expr_loc_or_input_loc (declarator),
2914 "non-type partial specialization %qE "
2915 "is not allowed", declarator);
2916 else
2917 error_at (cp_expr_loc_or_input_loc (declarator),
2918 "non-class, non-variable partial specialization %qE "
2919 "is not allowed", declarator);
2920 return decl;
2921 ok:;
2922 }
2923
2924 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2925 /* This is a specialization of a member template, without
2926 specialization the containing class. Something like:
2927
2928 template <class T> struct S {
2929 template <class U> void f (U);
2930 };
2931 template <> template <class U> void S<int>::f(U) {}
2932
2933 That's a specialization -- but of the entire template. */
2934 specialization = 1;
2935 break;
2936
2937 default:
2938 gcc_unreachable ();
2939 }
2940
2941 if ((specialization || member_specialization)
2942 /* This doesn't apply to variable templates. */
2943 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2944 {
2945 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2946 for (; t; t = TREE_CHAIN (t))
2947 if (TREE_PURPOSE (t))
2948 {
2949 permerror (input_location,
2950 "default argument specified in explicit specialization");
2951 break;
2952 }
2953 }
2954
2955 if (specialization || member_specialization || explicit_instantiation)
2956 {
2957 tree tmpl = NULL_TREE;
2958 tree targs = NULL_TREE;
2959 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2960
2961 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2962 if (!was_template_id)
2963 {
2964 tree fns;
2965
2966 gcc_assert (identifier_p (declarator));
2967 if (ctype)
2968 fns = dname;
2969 else
2970 {
2971 /* If there is no class context, the explicit instantiation
2972 must be at namespace scope. */
2973 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2974
2975 /* Find the namespace binding, using the declaration
2976 context. */
2977 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2978 false, true);
2979 if (fns == error_mark_node)
2980 /* If lookup fails, look for a friend declaration so we can
2981 give a better diagnostic. */
2982 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2983 /*type*/false, /*complain*/true,
2984 /*hidden*/true);
2985
2986 if (fns == error_mark_node || !is_overloaded_fn (fns))
2987 {
2988 error ("%qD is not a template function", dname);
2989 fns = error_mark_node;
2990 }
2991 }
2992
2993 declarator = lookup_template_function (fns, NULL_TREE);
2994 }
2995
2996 if (declarator == error_mark_node)
2997 return error_mark_node;
2998
2999 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3000 {
3001 if (!explicit_instantiation)
3002 /* A specialization in class scope. This is invalid,
3003 but the error will already have been flagged by
3004 check_specialization_scope. */
3005 return error_mark_node;
3006 else
3007 {
3008 /* It's not valid to write an explicit instantiation in
3009 class scope, e.g.:
3010
3011 class C { template void f(); }
3012
3013 This case is caught by the parser. However, on
3014 something like:
3015
3016 template class C { void f(); };
3017
3018 (which is invalid) we can get here. The error will be
3019 issued later. */
3020 ;
3021 }
3022
3023 return decl;
3024 }
3025 else if (ctype != NULL_TREE
3026 && (identifier_p (TREE_OPERAND (declarator, 0))))
3027 {
3028 // We'll match variable templates in start_decl.
3029 if (VAR_P (decl))
3030 return decl;
3031
3032 /* Find the list of functions in ctype that have the same
3033 name as the declared function. */
3034 tree name = TREE_OPERAND (declarator, 0);
3035
3036 if (constructor_name_p (name, ctype))
3037 {
3038 if (DECL_CONSTRUCTOR_P (decl)
3039 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3040 : !CLASSTYPE_DESTRUCTOR (ctype))
3041 {
3042 /* From [temp.expl.spec]:
3043
3044 If such an explicit specialization for the member
3045 of a class template names an implicitly-declared
3046 special member function (clause _special_), the
3047 program is ill-formed.
3048
3049 Similar language is found in [temp.explicit]. */
3050 error ("specialization of implicitly-declared special member function");
3051 return error_mark_node;
3052 }
3053
3054 name = DECL_NAME (decl);
3055 }
3056
3057 /* For a type-conversion operator, We might be looking for
3058 `operator int' which will be a specialization of
3059 `operator T'. Grab all the conversion operators, and
3060 then select from them. */
3061 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3062 ? conv_op_identifier : name);
3063
3064 if (fns == NULL_TREE)
3065 {
3066 error ("no member function %qD declared in %qT", name, ctype);
3067 return error_mark_node;
3068 }
3069 else
3070 TREE_OPERAND (declarator, 0) = fns;
3071 }
3072
3073 /* Figure out what exactly is being specialized at this point.
3074 Note that for an explicit instantiation, even one for a
3075 member function, we cannot tell a priori whether the
3076 instantiation is for a member template, or just a member
3077 function of a template class. Even if a member template is
3078 being instantiated, the member template arguments may be
3079 elided if they can be deduced from the rest of the
3080 declaration. */
3081 tmpl = determine_specialization (declarator, decl,
3082 &targs,
3083 member_specialization,
3084 template_count,
3085 tsk);
3086
3087 if (!tmpl || tmpl == error_mark_node)
3088 /* We couldn't figure out what this declaration was
3089 specializing. */
3090 return error_mark_node;
3091 else
3092 {
3093 if (TREE_CODE (decl) == FUNCTION_DECL
3094 && DECL_HIDDEN_FRIEND_P (tmpl))
3095 {
3096 auto_diagnostic_group d;
3097 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3098 "friend declaration %qD is not visible to "
3099 "explicit specialization", tmpl))
3100 inform (DECL_SOURCE_LOCATION (tmpl),
3101 "friend declaration here");
3102 }
3103 else if (!ctype && !is_friend
3104 && CP_DECL_CONTEXT (decl) == current_namespace)
3105 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3106
3107 tree gen_tmpl = most_general_template (tmpl);
3108
3109 if (explicit_instantiation)
3110 {
3111 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3112 is done by do_decl_instantiation later. */
3113
3114 int arg_depth = TMPL_ARGS_DEPTH (targs);
3115 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3116
3117 if (arg_depth > parm_depth)
3118 {
3119 /* If TMPL is not the most general template (for
3120 example, if TMPL is a friend template that is
3121 injected into namespace scope), then there will
3122 be too many levels of TARGS. Remove some of them
3123 here. */
3124 int i;
3125 tree new_targs;
3126
3127 new_targs = make_tree_vec (parm_depth);
3128 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3129 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3130 = TREE_VEC_ELT (targs, i);
3131 targs = new_targs;
3132 }
3133
3134 return instantiate_template (tmpl, targs, tf_error);
3135 }
3136
3137 /* If we thought that the DECL was a member function, but it
3138 turns out to be specializing a static member function,
3139 make DECL a static member function as well. */
3140 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3141 && DECL_STATIC_FUNCTION_P (tmpl)
3142 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3143 revert_static_member_fn (decl);
3144
3145 /* If this is a specialization of a member template of a
3146 template class, we want to return the TEMPLATE_DECL, not
3147 the specialization of it. */
3148 if (tsk == tsk_template && !was_template_id)
3149 {
3150 tree result = DECL_TEMPLATE_RESULT (tmpl);
3151 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3152 DECL_INITIAL (result) = NULL_TREE;
3153 if (have_def)
3154 {
3155 tree parm;
3156 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3157 DECL_SOURCE_LOCATION (result)
3158 = DECL_SOURCE_LOCATION (decl);
3159 /* We want to use the argument list specified in the
3160 definition, not in the original declaration. */
3161 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3162 for (parm = DECL_ARGUMENTS (result); parm;
3163 parm = DECL_CHAIN (parm))
3164 DECL_CONTEXT (parm) = result;
3165 }
3166 return register_specialization (tmpl, gen_tmpl, targs,
3167 is_friend, 0);
3168 }
3169
3170 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3171 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3172
3173 if (was_template_id)
3174 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3175
3176 /* Inherit default function arguments from the template
3177 DECL is specializing. */
3178 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3179 copy_default_args_to_explicit_spec (decl);
3180
3181 /* This specialization has the same protection as the
3182 template it specializes. */
3183 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3184 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3185
3186 /* 7.1.1-1 [dcl.stc]
3187
3188 A storage-class-specifier shall not be specified in an
3189 explicit specialization...
3190
3191 The parser rejects these, so unless action is taken here,
3192 explicit function specializations will always appear with
3193 global linkage.
3194
3195 The action recommended by the C++ CWG in response to C++
3196 defect report 605 is to make the storage class and linkage
3197 of the explicit specialization match the templated function:
3198
3199 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3200 */
3201 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3202 {
3203 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3204 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3205
3206 /* A concept cannot be specialized. */
3207 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3208 {
3209 error ("explicit specialization of function concept %qD",
3210 gen_tmpl);
3211 return error_mark_node;
3212 }
3213
3214 /* This specialization has the same linkage and visibility as
3215 the function template it specializes. */
3216 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3217 if (! TREE_PUBLIC (decl))
3218 {
3219 DECL_INTERFACE_KNOWN (decl) = 1;
3220 DECL_NOT_REALLY_EXTERN (decl) = 1;
3221 }
3222 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3223 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3224 {
3225 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3226 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3227 }
3228 }
3229
3230 /* If DECL is a friend declaration, declared using an
3231 unqualified name, the namespace associated with DECL may
3232 have been set incorrectly. For example, in:
3233
3234 template <typename T> void f(T);
3235 namespace N {
3236 struct S { friend void f<int>(int); }
3237 }
3238
3239 we will have set the DECL_CONTEXT for the friend
3240 declaration to N, rather than to the global namespace. */
3241 if (DECL_NAMESPACE_SCOPE_P (decl))
3242 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3243
3244 if (is_friend && !have_def)
3245 /* This is not really a declaration of a specialization.
3246 It's just the name of an instantiation. But, it's not
3247 a request for an instantiation, either. */
3248 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3249 else if (TREE_CODE (decl) == FUNCTION_DECL)
3250 /* A specialization is not necessarily COMDAT. */
3251 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3252 && DECL_DECLARED_INLINE_P (decl));
3253 else if (VAR_P (decl))
3254 DECL_COMDAT (decl) = false;
3255
3256 /* If this is a full specialization, register it so that we can find
3257 it again. Partial specializations will be registered in
3258 process_partial_specialization. */
3259 if (!processing_template_decl)
3260 {
3261 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3262
3263 decl = register_specialization (decl, gen_tmpl, targs,
3264 is_friend, 0);
3265 }
3266
3267
3268 /* A 'structor should already have clones. */
3269 gcc_assert (decl == error_mark_node
3270 || variable_template_p (tmpl)
3271 || !(DECL_CONSTRUCTOR_P (decl)
3272 || DECL_DESTRUCTOR_P (decl))
3273 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3274 }
3275 }
3276
3277 return decl;
3278 }
3279
3280 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3281 parameters. These are represented in the same format used for
3282 DECL_TEMPLATE_PARMS. */
3283
3284 int
3285 comp_template_parms (const_tree parms1, const_tree parms2)
3286 {
3287 const_tree p1;
3288 const_tree p2;
3289
3290 if (parms1 == parms2)
3291 return 1;
3292
3293 for (p1 = parms1, p2 = parms2;
3294 p1 != NULL_TREE && p2 != NULL_TREE;
3295 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3296 {
3297 tree t1 = TREE_VALUE (p1);
3298 tree t2 = TREE_VALUE (p2);
3299 int i;
3300
3301 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3302 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3303
3304 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3305 return 0;
3306
3307 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3308 {
3309 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3310 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3311
3312 /* If either of the template parameters are invalid, assume
3313 they match for the sake of error recovery. */
3314 if (error_operand_p (parm1) || error_operand_p (parm2))
3315 return 1;
3316
3317 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3318 return 0;
3319
3320 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3321 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3322 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3323 continue;
3324 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3325 return 0;
3326 }
3327 }
3328
3329 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3330 /* One set of parameters has more parameters lists than the
3331 other. */
3332 return 0;
3333
3334 return 1;
3335 }
3336
3337 /* Returns true if two template parameters are declared with
3338 equivalent constraints. */
3339
3340 static bool
3341 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3342 {
3343 tree req1 = TREE_TYPE (parm1);
3344 tree req2 = TREE_TYPE (parm2);
3345 if (!req1 != !req2)
3346 return false;
3347 if (req1)
3348 return cp_tree_equal (req1, req2);
3349 return true;
3350 }
3351
3352 /* Returns true when two template parameters are equivalent. */
3353
3354 static bool
3355 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3356 {
3357 tree decl1 = TREE_VALUE (parm1);
3358 tree decl2 = TREE_VALUE (parm2);
3359
3360 /* If either of the template parameters are invalid, assume
3361 they match for the sake of error recovery. */
3362 if (error_operand_p (decl1) || error_operand_p (decl2))
3363 return true;
3364
3365 /* ... they declare parameters of the same kind. */
3366 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3367 return false;
3368
3369 /* ... one parameter was introduced by a parameter declaration, then
3370 both are. This case arises as a result of eagerly rewriting declarations
3371 during parsing. */
3372 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3373 return false;
3374
3375 /* ... if either declares a pack, they both do. */
3376 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3377 return false;
3378
3379 if (TREE_CODE (decl1) == PARM_DECL)
3380 {
3381 /* ... if they declare non-type parameters, the types are equivalent. */
3382 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3383 return false;
3384 }
3385 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3386 {
3387 /* ... if they declare template template parameters, their template
3388 parameter lists are equivalent. */
3389 if (!template_heads_equivalent_p (decl1, decl2))
3390 return false;
3391 }
3392
3393 /* ... if they are declared with a qualified-concept name, they both
3394 are, and those names are equivalent. */
3395 return template_parameter_constraints_equivalent_p (parm1, parm2);
3396 }
3397
3398 /* Returns true if two template parameters lists are equivalent.
3399 Two template parameter lists are equivalent if they have the
3400 same length and their corresponding parameters are equivalent.
3401
3402 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3403 data structure returned by DECL_TEMPLATE_PARMS.
3404
3405 This is generally the same implementation as comp_template_parms
3406 except that it also the concept names and arguments used to
3407 introduce parameters. */
3408
3409 static bool
3410 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3411 {
3412 if (parms1 == parms2)
3413 return true;
3414
3415 const_tree p1 = parms1;
3416 const_tree p2 = parms2;
3417 while (p1 != NULL_TREE && p2 != NULL_TREE)
3418 {
3419 tree list1 = TREE_VALUE (p1);
3420 tree list2 = TREE_VALUE (p2);
3421
3422 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3423 return 0;
3424
3425 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3426 {
3427 tree parm1 = TREE_VEC_ELT (list1, i);
3428 tree parm2 = TREE_VEC_ELT (list2, i);
3429 if (!template_parameters_equivalent_p (parm1, parm2))
3430 return false;
3431 }
3432
3433 p1 = TREE_CHAIN (p1);
3434 p2 = TREE_CHAIN (p2);
3435 }
3436
3437 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3438 return false;
3439
3440 return true;
3441 }
3442
3443 /* Return true if the requires-clause of the template parameter lists are
3444 equivalent and false otherwise. */
3445 static bool
3446 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3447 {
3448 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3449 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3450 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3451 return false;
3452 if (!cp_tree_equal (req1, req2))
3453 return false;
3454 return true;
3455 }
3456
3457 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3458 Two template heads are equivalent if their template parameter
3459 lists are equivalent and their requires clauses are equivalent.
3460
3461 In pre-C++20, this is equivalent to calling comp_template_parms
3462 for the template parameters of TMPL1 and TMPL2. */
3463
3464 bool
3465 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3466 {
3467 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3468 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3469
3470 /* Don't change the matching rules for pre-C++20. */
3471 if (cxx_dialect < cxx2a)
3472 return comp_template_parms (parms1, parms2);
3473
3474 /* ... have the same number of template parameters, and their
3475 corresponding parameters are equivalent. */
3476 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3477 return false;
3478
3479 /* ... if either has a requires-clause, they both do and their
3480 corresponding constraint-expressions are equivalent. */
3481 return template_requirements_equivalent_p (parms1, parms2);
3482 }
3483
3484 /* Determine whether PARM is a parameter pack. */
3485
3486 bool
3487 template_parameter_pack_p (const_tree parm)
3488 {
3489 /* Determine if we have a non-type template parameter pack. */
3490 if (TREE_CODE (parm) == PARM_DECL)
3491 return (DECL_TEMPLATE_PARM_P (parm)
3492 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3493 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3494 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3495
3496 /* If this is a list of template parameters, we could get a
3497 TYPE_DECL or a TEMPLATE_DECL. */
3498 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3499 parm = TREE_TYPE (parm);
3500
3501 /* Otherwise it must be a type template parameter. */
3502 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3503 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3504 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3505 }
3506
3507 /* Determine if T is a function parameter pack. */
3508
3509 bool
3510 function_parameter_pack_p (const_tree t)
3511 {
3512 if (t && TREE_CODE (t) == PARM_DECL)
3513 return DECL_PACK_P (t);
3514 return false;
3515 }
3516
3517 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3518 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3519
3520 tree
3521 get_function_template_decl (const_tree primary_func_tmpl_inst)
3522 {
3523 if (! primary_func_tmpl_inst
3524 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3525 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3526 return NULL;
3527
3528 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3529 }
3530
3531 /* Return true iff the function parameter PARAM_DECL was expanded
3532 from the function parameter pack PACK. */
3533
3534 bool
3535 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3536 {
3537 if (DECL_ARTIFICIAL (param_decl)
3538 || !function_parameter_pack_p (pack))
3539 return false;
3540
3541 /* The parameter pack and its pack arguments have the same
3542 DECL_PARM_INDEX. */
3543 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3544 }
3545
3546 /* Determine whether ARGS describes a variadic template args list,
3547 i.e., one that is terminated by a template argument pack. */
3548
3549 static bool
3550 template_args_variadic_p (tree args)
3551 {
3552 int nargs;
3553 tree last_parm;
3554
3555 if (args == NULL_TREE)
3556 return false;
3557
3558 args = INNERMOST_TEMPLATE_ARGS (args);
3559 nargs = TREE_VEC_LENGTH (args);
3560
3561 if (nargs == 0)
3562 return false;
3563
3564 last_parm = TREE_VEC_ELT (args, nargs - 1);
3565
3566 return ARGUMENT_PACK_P (last_parm);
3567 }
3568
3569 /* Generate a new name for the parameter pack name NAME (an
3570 IDENTIFIER_NODE) that incorporates its */
3571
3572 static tree
3573 make_ith_pack_parameter_name (tree name, int i)
3574 {
3575 /* Munge the name to include the parameter index. */
3576 #define NUMBUF_LEN 128
3577 char numbuf[NUMBUF_LEN];
3578 char* newname;
3579 int newname_len;
3580
3581 if (name == NULL_TREE)
3582 return name;
3583 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3584 newname_len = IDENTIFIER_LENGTH (name)
3585 + strlen (numbuf) + 2;
3586 newname = (char*)alloca (newname_len);
3587 snprintf (newname, newname_len,
3588 "%s#%i", IDENTIFIER_POINTER (name), i);
3589 return get_identifier (newname);
3590 }
3591
3592 /* Return true if T is a primary function, class or alias template
3593 specialization, not including the template pattern. */
3594
3595 bool
3596 primary_template_specialization_p (const_tree t)
3597 {
3598 if (!t)
3599 return false;
3600
3601 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3602 return (DECL_LANG_SPECIFIC (t)
3603 && DECL_USE_TEMPLATE (t)
3604 && DECL_TEMPLATE_INFO (t)
3605 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3606 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3607 return (CLASSTYPE_TEMPLATE_INFO (t)
3608 && CLASSTYPE_USE_TEMPLATE (t)
3609 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3610 else if (alias_template_specialization_p (t, nt_transparent))
3611 return true;
3612 return false;
3613 }
3614
3615 /* Return true if PARM is a template template parameter. */
3616
3617 bool
3618 template_template_parameter_p (const_tree parm)
3619 {
3620 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3621 }
3622
3623 /* Return true iff PARM is a DECL representing a type template
3624 parameter. */
3625
3626 bool
3627 template_type_parameter_p (const_tree parm)
3628 {
3629 return (parm
3630 && (TREE_CODE (parm) == TYPE_DECL
3631 || TREE_CODE (parm) == TEMPLATE_DECL)
3632 && DECL_TEMPLATE_PARM_P (parm));
3633 }
3634
3635 /* Return the template parameters of T if T is a
3636 primary template instantiation, NULL otherwise. */
3637
3638 tree
3639 get_primary_template_innermost_parameters (const_tree t)
3640 {
3641 tree parms = NULL, template_info = NULL;
3642
3643 if ((template_info = get_template_info (t))
3644 && primary_template_specialization_p (t))
3645 parms = INNERMOST_TEMPLATE_PARMS
3646 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3647
3648 return parms;
3649 }
3650
3651 /* Return the template parameters of the LEVELth level from the full list
3652 of template parameters PARMS. */
3653
3654 tree
3655 get_template_parms_at_level (tree parms, int level)
3656 {
3657 tree p;
3658 if (!parms
3659 || TREE_CODE (parms) != TREE_LIST
3660 || level > TMPL_PARMS_DEPTH (parms))
3661 return NULL_TREE;
3662
3663 for (p = parms; p; p = TREE_CHAIN (p))
3664 if (TMPL_PARMS_DEPTH (p) == level)
3665 return p;
3666
3667 return NULL_TREE;
3668 }
3669
3670 /* Returns the template arguments of T if T is a template instantiation,
3671 NULL otherwise. */
3672
3673 tree
3674 get_template_innermost_arguments (const_tree t)
3675 {
3676 tree args = NULL, template_info = NULL;
3677
3678 if ((template_info = get_template_info (t))
3679 && TI_ARGS (template_info))
3680 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3681
3682 return args;
3683 }
3684
3685 /* Return the argument pack elements of T if T is a template argument pack,
3686 NULL otherwise. */
3687
3688 tree
3689 get_template_argument_pack_elems (const_tree t)
3690 {
3691 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3692 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3693 return NULL;
3694
3695 return ARGUMENT_PACK_ARGS (t);
3696 }
3697
3698 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3699 ARGUMENT_PACK_SELECT represents. */
3700
3701 static tree
3702 argument_pack_select_arg (tree t)
3703 {
3704 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3705 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3706
3707 /* If the selected argument is an expansion E, that most likely means we were
3708 called from gen_elem_of_pack_expansion_instantiation during the
3709 substituting of an argument pack (of which the Ith element is a pack
3710 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3711 In this case, the Ith element resulting from this substituting is going to
3712 be a pack expansion, which pattern is the pattern of E. Let's return the
3713 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3714 resulting pack expansion from it. */
3715 if (PACK_EXPANSION_P (arg))
3716 {
3717 /* Make sure we aren't throwing away arg info. */
3718 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3719 arg = PACK_EXPANSION_PATTERN (arg);
3720 }
3721
3722 return arg;
3723 }
3724
3725
3726 /* True iff FN is a function representing a built-in variadic parameter
3727 pack. */
3728
3729 bool
3730 builtin_pack_fn_p (tree fn)
3731 {
3732 if (!fn
3733 || TREE_CODE (fn) != FUNCTION_DECL
3734 || !DECL_IS_BUILTIN (fn))
3735 return false;
3736
3737 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3738 return true;
3739
3740 return false;
3741 }
3742
3743 /* True iff CALL is a call to a function representing a built-in variadic
3744 parameter pack. */
3745
3746 static bool
3747 builtin_pack_call_p (tree call)
3748 {
3749 if (TREE_CODE (call) != CALL_EXPR)
3750 return false;
3751 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3752 }
3753
3754 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3755
3756 static tree
3757 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3758 tree in_decl)
3759 {
3760 tree ohi = CALL_EXPR_ARG (call, 0);
3761 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3762 false/*fn*/, true/*int_cst*/);
3763
3764 if (value_dependent_expression_p (hi))
3765 {
3766 if (hi != ohi)
3767 {
3768 call = copy_node (call);
3769 CALL_EXPR_ARG (call, 0) = hi;
3770 }
3771 tree ex = make_pack_expansion (call, complain);
3772 tree vec = make_tree_vec (1);
3773 TREE_VEC_ELT (vec, 0) = ex;
3774 return vec;
3775 }
3776 else
3777 {
3778 hi = cxx_constant_value (hi);
3779 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3780
3781 /* Calculate the largest value of len that won't make the size of the vec
3782 overflow an int. The compiler will exceed resource limits long before
3783 this, but it seems a decent place to diagnose. */
3784 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3785
3786 if (len < 0 || len > max)
3787 {
3788 if ((complain & tf_error)
3789 && hi != error_mark_node)
3790 error ("argument to %<__integer_pack%> must be between 0 and %d",
3791 max);
3792 return error_mark_node;
3793 }
3794
3795 tree vec = make_tree_vec (len);
3796
3797 for (int i = 0; i < len; ++i)
3798 TREE_VEC_ELT (vec, i) = size_int (i);
3799
3800 return vec;
3801 }
3802 }
3803
3804 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3805 CALL. */
3806
3807 static tree
3808 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3809 tree in_decl)
3810 {
3811 if (!builtin_pack_call_p (call))
3812 return NULL_TREE;
3813
3814 tree fn = CALL_EXPR_FN (call);
3815
3816 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3817 return expand_integer_pack (call, args, complain, in_decl);
3818
3819 return NULL_TREE;
3820 }
3821
3822 /* Structure used to track the progress of find_parameter_packs_r. */
3823 struct find_parameter_pack_data
3824 {
3825 /* TREE_LIST that will contain all of the parameter packs found by
3826 the traversal. */
3827 tree* parameter_packs;
3828
3829 /* Set of AST nodes that have been visited by the traversal. */
3830 hash_set<tree> *visited;
3831
3832 /* True iff we're making a type pack expansion. */
3833 bool type_pack_expansion_p;
3834 };
3835
3836 /* Identifies all of the argument packs that occur in a template
3837 argument and appends them to the TREE_LIST inside DATA, which is a
3838 find_parameter_pack_data structure. This is a subroutine of
3839 make_pack_expansion and uses_parameter_packs. */
3840 static tree
3841 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3842 {
3843 tree t = *tp;
3844 struct find_parameter_pack_data* ppd =
3845 (struct find_parameter_pack_data*)data;
3846 bool parameter_pack_p = false;
3847
3848 /* Don't look through typedefs; we are interested in whether a
3849 parameter pack is actually written in the expression/type we're
3850 looking at, not the target type. */
3851 if (TYPE_P (t) && typedef_variant_p (t))
3852 {
3853 /* But do look at arguments for an alias template. */
3854 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3855 cp_walk_tree (&TI_ARGS (tinfo),
3856 &find_parameter_packs_r,
3857 ppd, ppd->visited);
3858 *walk_subtrees = 0;
3859 return NULL_TREE;
3860 }
3861
3862 /* Identify whether this is a parameter pack or not. */
3863 switch (TREE_CODE (t))
3864 {
3865 case TEMPLATE_PARM_INDEX:
3866 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3867 parameter_pack_p = true;
3868 break;
3869
3870 case TEMPLATE_TYPE_PARM:
3871 t = TYPE_MAIN_VARIANT (t);
3872 /* FALLTHRU */
3873 case TEMPLATE_TEMPLATE_PARM:
3874 /* If the placeholder appears in the decl-specifier-seq of a function
3875 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3876 is a pack expansion, the invented template parameter is a template
3877 parameter pack. */
3878 if (ppd->type_pack_expansion_p && is_auto (t))
3879 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3880 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3881 parameter_pack_p = true;
3882 break;
3883
3884 case FIELD_DECL:
3885 case PARM_DECL:
3886 if (DECL_PACK_P (t))
3887 {
3888 /* We don't want to walk into the type of a PARM_DECL,
3889 because we don't want to see the type parameter pack. */
3890 *walk_subtrees = 0;
3891 parameter_pack_p = true;
3892 }
3893 break;
3894
3895 case VAR_DECL:
3896 if (DECL_PACK_P (t))
3897 {
3898 /* We don't want to walk into the type of a variadic capture proxy,
3899 because we don't want to see the type parameter pack. */
3900 *walk_subtrees = 0;
3901 parameter_pack_p = true;
3902 }
3903 else if (variable_template_specialization_p (t))
3904 {
3905 cp_walk_tree (&DECL_TI_ARGS (t),
3906 find_parameter_packs_r,
3907 ppd, ppd->visited);
3908 *walk_subtrees = 0;
3909 }
3910 break;
3911
3912 case CALL_EXPR:
3913 if (builtin_pack_call_p (t))
3914 parameter_pack_p = true;
3915 break;
3916
3917 case BASES:
3918 parameter_pack_p = true;
3919 break;
3920 default:
3921 /* Not a parameter pack. */
3922 break;
3923 }
3924
3925 if (parameter_pack_p)
3926 {
3927 /* Add this parameter pack to the list. */
3928 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3929 }
3930
3931 if (TYPE_P (t))
3932 cp_walk_tree (&TYPE_CONTEXT (t),
3933 &find_parameter_packs_r, ppd, ppd->visited);
3934
3935 /* This switch statement will return immediately if we don't find a
3936 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3937 switch (TREE_CODE (t))
3938 {
3939 case BOUND_TEMPLATE_TEMPLATE_PARM:
3940 /* Check the template itself. */
3941 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3942 &find_parameter_packs_r, ppd, ppd->visited);
3943 return NULL_TREE;
3944
3945 case DECL_EXPR:
3946 {
3947 tree decl = DECL_EXPR_DECL (t);
3948 /* Ignore the declaration of a capture proxy for a parameter pack. */
3949 if (is_capture_proxy (decl))
3950 *walk_subtrees = 0;
3951 if (is_typedef_decl (decl))
3952 /* Since we stop at typedefs above, we need to look through them at
3953 the point of the DECL_EXPR. */
3954 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3955 &find_parameter_packs_r, ppd, ppd->visited);
3956 return NULL_TREE;
3957 }
3958
3959 case TEMPLATE_DECL:
3960 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3961 return NULL_TREE;
3962 cp_walk_tree (&TREE_TYPE (t),
3963 &find_parameter_packs_r, ppd, ppd->visited);
3964 return NULL_TREE;
3965
3966 case TYPENAME_TYPE:
3967 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3968 ppd, ppd->visited);
3969 *walk_subtrees = 0;
3970 return NULL_TREE;
3971
3972 case TYPE_PACK_EXPANSION:
3973 case EXPR_PACK_EXPANSION:
3974 *walk_subtrees = 0;
3975 return NULL_TREE;
3976
3977 case INTEGER_TYPE:
3978 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3979 ppd, ppd->visited);
3980 *walk_subtrees = 0;
3981 return NULL_TREE;
3982
3983 case IDENTIFIER_NODE:
3984 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3985 ppd->visited);
3986 *walk_subtrees = 0;
3987 return NULL_TREE;
3988
3989 case LAMBDA_EXPR:
3990 {
3991 /* Since we defer implicit capture, look in the parms and body. */
3992 tree fn = lambda_function (t);
3993 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3994 ppd->visited);
3995 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3996 ppd->visited);
3997 return NULL_TREE;
3998 }
3999
4000 case DECLTYPE_TYPE:
4001 {
4002 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4003 type_pack_expansion_p to false so that any placeholders
4004 within the expression don't get marked as parameter packs. */
4005 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4006 ppd->type_pack_expansion_p = false;
4007 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4008 ppd, ppd->visited);
4009 ppd->type_pack_expansion_p = type_pack_expansion_p;
4010 *walk_subtrees = 0;
4011 return NULL_TREE;
4012 }
4013
4014 case IF_STMT:
4015 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4016 ppd, ppd->visited);
4017 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4018 ppd, ppd->visited);
4019 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4020 ppd, ppd->visited);
4021 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4022 *walk_subtrees = 0;
4023 return NULL_TREE;
4024
4025 default:
4026 return NULL_TREE;
4027 }
4028
4029 return NULL_TREE;
4030 }
4031
4032 /* Determines if the expression or type T uses any parameter packs. */
4033 tree
4034 uses_parameter_packs (tree t)
4035 {
4036 tree parameter_packs = NULL_TREE;
4037 struct find_parameter_pack_data ppd;
4038 ppd.parameter_packs = &parameter_packs;
4039 ppd.visited = new hash_set<tree>;
4040 ppd.type_pack_expansion_p = false;
4041 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4042 delete ppd.visited;
4043 return parameter_packs;
4044 }
4045
4046 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4047 representation a base-class initializer into a parameter pack
4048 expansion. If all goes well, the resulting node will be an
4049 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4050 respectively. */
4051 tree
4052 make_pack_expansion (tree arg, tsubst_flags_t complain)
4053 {
4054 tree result;
4055 tree parameter_packs = NULL_TREE;
4056 bool for_types = false;
4057 struct find_parameter_pack_data ppd;
4058
4059 if (!arg || arg == error_mark_node)
4060 return arg;
4061
4062 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4063 {
4064 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4065 class initializer. In this case, the TREE_PURPOSE will be a
4066 _TYPE node (representing the base class expansion we're
4067 initializing) and the TREE_VALUE will be a TREE_LIST
4068 containing the initialization arguments.
4069
4070 The resulting expansion looks somewhat different from most
4071 expansions. Rather than returning just one _EXPANSION, we
4072 return a TREE_LIST whose TREE_PURPOSE is a
4073 TYPE_PACK_EXPANSION containing the bases that will be
4074 initialized. The TREE_VALUE will be identical to the
4075 original TREE_VALUE, which is a list of arguments that will
4076 be passed to each base. We do not introduce any new pack
4077 expansion nodes into the TREE_VALUE (although it is possible
4078 that some already exist), because the TREE_PURPOSE and
4079 TREE_VALUE all need to be expanded together with the same
4080 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4081 resulting TREE_PURPOSE will mention the parameter packs in
4082 both the bases and the arguments to the bases. */
4083 tree purpose;
4084 tree value;
4085 tree parameter_packs = NULL_TREE;
4086
4087 /* Determine which parameter packs will be used by the base
4088 class expansion. */
4089 ppd.visited = new hash_set<tree>;
4090 ppd.parameter_packs = &parameter_packs;
4091 ppd.type_pack_expansion_p = false;
4092 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4093 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4094 &ppd, ppd.visited);
4095
4096 if (parameter_packs == NULL_TREE)
4097 {
4098 if (complain & tf_error)
4099 error ("base initializer expansion %qT contains no parameter packs",
4100 arg);
4101 delete ppd.visited;
4102 return error_mark_node;
4103 }
4104
4105 if (TREE_VALUE (arg) != void_type_node)
4106 {
4107 /* Collect the sets of parameter packs used in each of the
4108 initialization arguments. */
4109 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4110 {
4111 /* Determine which parameter packs will be expanded in this
4112 argument. */
4113 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4114 &ppd, ppd.visited);
4115 }
4116 }
4117
4118 delete ppd.visited;
4119
4120 /* Create the pack expansion type for the base type. */
4121 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4122 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4123 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4124 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4125
4126 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4127 they will rarely be compared to anything. */
4128 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4129
4130 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4131 }
4132
4133 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4134 for_types = true;
4135
4136 /* Build the PACK_EXPANSION_* node. */
4137 result = for_types
4138 ? cxx_make_type (TYPE_PACK_EXPANSION)
4139 : make_node (EXPR_PACK_EXPANSION);
4140 SET_PACK_EXPANSION_PATTERN (result, arg);
4141 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4142 {
4143 /* Propagate type and const-expression information. */
4144 TREE_TYPE (result) = TREE_TYPE (arg);
4145 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4146 /* Mark this read now, since the expansion might be length 0. */
4147 mark_exp_read (arg);
4148 }
4149 else
4150 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4151 they will rarely be compared to anything. */
4152 SET_TYPE_STRUCTURAL_EQUALITY (result);
4153
4154 /* Determine which parameter packs will be expanded. */
4155 ppd.parameter_packs = &parameter_packs;
4156 ppd.visited = new hash_set<tree>;
4157 ppd.type_pack_expansion_p = TYPE_P (arg);
4158 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4159 delete ppd.visited;
4160
4161 /* Make sure we found some parameter packs. */
4162 if (parameter_packs == NULL_TREE)
4163 {
4164 if (complain & tf_error)
4165 {
4166 if (TYPE_P (arg))
4167 error ("expansion pattern %qT contains no parameter packs", arg);
4168 else
4169 error ("expansion pattern %qE contains no parameter packs", arg);
4170 }
4171 return error_mark_node;
4172 }
4173 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4174
4175 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4176
4177 return result;
4178 }
4179
4180 /* Checks T for any "bare" parameter packs, which have not yet been
4181 expanded, and issues an error if any are found. This operation can
4182 only be done on full expressions or types (e.g., an expression
4183 statement, "if" condition, etc.), because we could have expressions like:
4184
4185 foo(f(g(h(args)))...)
4186
4187 where "args" is a parameter pack. check_for_bare_parameter_packs
4188 should not be called for the subexpressions args, h(args),
4189 g(h(args)), or f(g(h(args))), because we would produce erroneous
4190 error messages.
4191
4192 Returns TRUE and emits an error if there were bare parameter packs,
4193 returns FALSE otherwise. */
4194 bool
4195 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4196 {
4197 tree parameter_packs = NULL_TREE;
4198 struct find_parameter_pack_data ppd;
4199
4200 if (!processing_template_decl || !t || t == error_mark_node)
4201 return false;
4202
4203 /* A lambda might use a parameter pack from the containing context. */
4204 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4205 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4206 return false;
4207
4208 if (TREE_CODE (t) == TYPE_DECL)
4209 t = TREE_TYPE (t);
4210
4211 ppd.parameter_packs = &parameter_packs;
4212 ppd.visited = new hash_set<tree>;
4213 ppd.type_pack_expansion_p = false;
4214 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4215 delete ppd.visited;
4216
4217 if (parameter_packs)
4218 {
4219 if (loc == UNKNOWN_LOCATION)
4220 loc = cp_expr_loc_or_input_loc (t);
4221 error_at (loc, "parameter packs not expanded with %<...%>:");
4222 while (parameter_packs)
4223 {
4224 tree pack = TREE_VALUE (parameter_packs);
4225 tree name = NULL_TREE;
4226
4227 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4228 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4229 name = TYPE_NAME (pack);
4230 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4231 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4232 else if (TREE_CODE (pack) == CALL_EXPR)
4233 name = DECL_NAME (CALL_EXPR_FN (pack));
4234 else
4235 name = DECL_NAME (pack);
4236
4237 if (name)
4238 inform (loc, " %qD", name);
4239 else
4240 inform (loc, " %s", "<anonymous>");
4241
4242 parameter_packs = TREE_CHAIN (parameter_packs);
4243 }
4244
4245 return true;
4246 }
4247
4248 return false;
4249 }
4250
4251 /* Expand any parameter packs that occur in the template arguments in
4252 ARGS. */
4253 tree
4254 expand_template_argument_pack (tree args)
4255 {
4256 if (args == error_mark_node)
4257 return error_mark_node;
4258
4259 tree result_args = NULL_TREE;
4260 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4261 int num_result_args = -1;
4262 int non_default_args_count = -1;
4263
4264 /* First, determine if we need to expand anything, and the number of
4265 slots we'll need. */
4266 for (in_arg = 0; in_arg < nargs; ++in_arg)
4267 {
4268 tree arg = TREE_VEC_ELT (args, in_arg);
4269 if (arg == NULL_TREE)
4270 return args;
4271 if (ARGUMENT_PACK_P (arg))
4272 {
4273 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4274 if (num_result_args < 0)
4275 num_result_args = in_arg + num_packed;
4276 else
4277 num_result_args += num_packed;
4278 }
4279 else
4280 {
4281 if (num_result_args >= 0)
4282 num_result_args++;
4283 }
4284 }
4285
4286 /* If no expansion is necessary, we're done. */
4287 if (num_result_args < 0)
4288 return args;
4289
4290 /* Expand arguments. */
4291 result_args = make_tree_vec (num_result_args);
4292 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4293 non_default_args_count =
4294 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4295 for (in_arg = 0; in_arg < nargs; ++in_arg)
4296 {
4297 tree arg = TREE_VEC_ELT (args, in_arg);
4298 if (ARGUMENT_PACK_P (arg))
4299 {
4300 tree packed = ARGUMENT_PACK_ARGS (arg);
4301 int i, num_packed = TREE_VEC_LENGTH (packed);
4302 for (i = 0; i < num_packed; ++i, ++out_arg)
4303 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4304 if (non_default_args_count > 0)
4305 non_default_args_count += num_packed - 1;
4306 }
4307 else
4308 {
4309 TREE_VEC_ELT (result_args, out_arg) = arg;
4310 ++out_arg;
4311 }
4312 }
4313 if (non_default_args_count >= 0)
4314 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4315 return result_args;
4316 }
4317
4318 /* Checks if DECL shadows a template parameter.
4319
4320 [temp.local]: A template-parameter shall not be redeclared within its
4321 scope (including nested scopes).
4322
4323 Emits an error and returns TRUE if the DECL shadows a parameter,
4324 returns FALSE otherwise. */
4325
4326 bool
4327 check_template_shadow (tree decl)
4328 {
4329 tree olddecl;
4330
4331 /* If we're not in a template, we can't possibly shadow a template
4332 parameter. */
4333 if (!current_template_parms)
4334 return true;
4335
4336 /* Figure out what we're shadowing. */
4337 decl = OVL_FIRST (decl);
4338 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4339
4340 /* If there's no previous binding for this name, we're not shadowing
4341 anything, let alone a template parameter. */
4342 if (!olddecl)
4343 return true;
4344
4345 /* If we're not shadowing a template parameter, we're done. Note
4346 that OLDDECL might be an OVERLOAD (or perhaps even an
4347 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4348 node. */
4349 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4350 return true;
4351
4352 /* We check for decl != olddecl to avoid bogus errors for using a
4353 name inside a class. We check TPFI to avoid duplicate errors for
4354 inline member templates. */
4355 if (decl == olddecl
4356 || (DECL_TEMPLATE_PARM_P (decl)
4357 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4358 return true;
4359
4360 /* Don't complain about the injected class name, as we've already
4361 complained about the class itself. */
4362 if (DECL_SELF_REFERENCE_P (decl))
4363 return false;
4364
4365 if (DECL_TEMPLATE_PARM_P (decl))
4366 error ("declaration of template parameter %q+D shadows "
4367 "template parameter", decl);
4368 else
4369 error ("declaration of %q+#D shadows template parameter", decl);
4370 inform (DECL_SOURCE_LOCATION (olddecl),
4371 "template parameter %qD declared here", olddecl);
4372 return false;
4373 }
4374
4375 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4376 ORIG_LEVEL, DECL, and TYPE. */
4377
4378 static tree
4379 build_template_parm_index (int index,
4380 int level,
4381 int orig_level,
4382 tree decl,
4383 tree type)
4384 {
4385 tree t = make_node (TEMPLATE_PARM_INDEX);
4386 TEMPLATE_PARM_IDX (t) = index;
4387 TEMPLATE_PARM_LEVEL (t) = level;
4388 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4389 TEMPLATE_PARM_DECL (t) = decl;
4390 TREE_TYPE (t) = type;
4391 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4392 TREE_READONLY (t) = TREE_READONLY (decl);
4393
4394 return t;
4395 }
4396
4397 /* Find the canonical type parameter for the given template type
4398 parameter. Returns the canonical type parameter, which may be TYPE
4399 if no such parameter existed. */
4400
4401 static tree
4402 canonical_type_parameter (tree type)
4403 {
4404 tree list;
4405 int idx = TEMPLATE_TYPE_IDX (type);
4406
4407 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4408
4409 if (!canonical_template_parms)
4410 vec_alloc (canonical_template_parms, idx + 1);
4411
4412 if (canonical_template_parms->length () <= (unsigned) idx)
4413 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4414
4415 list = (*canonical_template_parms)[idx];
4416 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4417 list = TREE_CHAIN (list);
4418
4419 if (list)
4420 return TREE_VALUE (list);
4421 else
4422 {
4423 (*canonical_template_parms)[idx]
4424 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4425 return type;
4426 }
4427 }
4428
4429 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4430 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4431 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4432 new one is created. */
4433
4434 static tree
4435 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4436 tsubst_flags_t complain)
4437 {
4438 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4439 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4440 != TEMPLATE_PARM_LEVEL (index) - levels)
4441 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4442 {
4443 tree orig_decl = TEMPLATE_PARM_DECL (index);
4444
4445 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4446 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4447 type);
4448 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4449 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4450 DECL_ARTIFICIAL (decl) = 1;
4451 SET_DECL_TEMPLATE_PARM_P (decl);
4452
4453 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4454 TEMPLATE_PARM_LEVEL (index) - levels,
4455 TEMPLATE_PARM_ORIG_LEVEL (index),
4456 decl, type);
4457 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4458 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4459 = TEMPLATE_PARM_PARAMETER_PACK (index);
4460
4461 /* Template template parameters need this. */
4462 tree inner = decl;
4463 if (TREE_CODE (decl) == TEMPLATE_DECL)
4464 {
4465 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4466 TYPE_DECL, DECL_NAME (decl), type);
4467 DECL_TEMPLATE_RESULT (decl) = inner;
4468 DECL_ARTIFICIAL (inner) = true;
4469 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4470 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4471 }
4472
4473 /* Attach the TPI to the decl. */
4474 if (TREE_CODE (inner) == TYPE_DECL)
4475 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4476 else
4477 DECL_INITIAL (decl) = tpi;
4478 }
4479
4480 return TEMPLATE_PARM_DESCENDANTS (index);
4481 }
4482
4483 /* Process information from new template parameter PARM and append it
4484 to the LIST being built. This new parameter is a non-type
4485 parameter iff IS_NON_TYPE is true. This new parameter is a
4486 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4487 is in PARM_LOC. */
4488
4489 tree
4490 process_template_parm (tree list, location_t parm_loc, tree parm,
4491 bool is_non_type, bool is_parameter_pack)
4492 {
4493 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4494 tree prev = NULL_TREE;
4495 int idx = 0;
4496
4497 if (list)
4498 {
4499 prev = tree_last (list);
4500
4501 tree p = TREE_VALUE (prev);
4502 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4503 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4504 else if (TREE_CODE (p) == PARM_DECL)
4505 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4506
4507 ++idx;
4508 }
4509
4510 tree decl = NULL_TREE;
4511 tree defval = TREE_PURPOSE (parm);
4512 tree constr = TREE_TYPE (parm);
4513
4514 if (is_non_type)
4515 {
4516 parm = TREE_VALUE (parm);
4517
4518 SET_DECL_TEMPLATE_PARM_P (parm);
4519
4520 if (TREE_TYPE (parm) != error_mark_node)
4521 {
4522 /* [temp.param]
4523
4524 The top-level cv-qualifiers on the template-parameter are
4525 ignored when determining its type. */
4526 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4527 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4528 TREE_TYPE (parm) = error_mark_node;
4529 else if (uses_parameter_packs (TREE_TYPE (parm))
4530 && !is_parameter_pack
4531 /* If we're in a nested template parameter list, the template
4532 template parameter could be a parameter pack. */
4533 && processing_template_parmlist == 1)
4534 {
4535 /* This template parameter is not a parameter pack, but it
4536 should be. Complain about "bare" parameter packs. */
4537 check_for_bare_parameter_packs (TREE_TYPE (parm));
4538
4539 /* Recover by calling this a parameter pack. */
4540 is_parameter_pack = true;
4541 }
4542 }
4543
4544 /* A template parameter is not modifiable. */
4545 TREE_CONSTANT (parm) = 1;
4546 TREE_READONLY (parm) = 1;
4547 decl = build_decl (parm_loc,
4548 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4549 TREE_CONSTANT (decl) = 1;
4550 TREE_READONLY (decl) = 1;
4551 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4552 = build_template_parm_index (idx, processing_template_decl,
4553 processing_template_decl,
4554 decl, TREE_TYPE (parm));
4555
4556 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4557 = is_parameter_pack;
4558 }
4559 else
4560 {
4561 tree t;
4562 parm = TREE_VALUE (TREE_VALUE (parm));
4563
4564 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4565 {
4566 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4567 /* This is for distinguishing between real templates and template
4568 template parameters */
4569 TREE_TYPE (parm) = t;
4570 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4571 decl = parm;
4572 }
4573 else
4574 {
4575 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4576 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4577 decl = build_decl (parm_loc,
4578 TYPE_DECL, parm, t);
4579 }
4580
4581 TYPE_NAME (t) = decl;
4582 TYPE_STUB_DECL (t) = decl;
4583 parm = decl;
4584 TEMPLATE_TYPE_PARM_INDEX (t)
4585 = build_template_parm_index (idx, processing_template_decl,
4586 processing_template_decl,
4587 decl, TREE_TYPE (parm));
4588 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4589 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4590 SET_TYPE_STRUCTURAL_EQUALITY (t);
4591 else
4592 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4593 }
4594 DECL_ARTIFICIAL (decl) = 1;
4595 SET_DECL_TEMPLATE_PARM_P (decl);
4596
4597 /* Build requirements for the type/template parameter.
4598 This must be done after SET_DECL_TEMPLATE_PARM_P or
4599 process_template_parm could fail. */
4600 tree reqs = finish_shorthand_constraint (parm, constr);
4601
4602 decl = pushdecl (decl);
4603 if (!is_non_type)
4604 parm = decl;
4605
4606 /* Build the parameter node linking the parameter declaration,
4607 its default argument (if any), and its constraints (if any). */
4608 parm = build_tree_list (defval, parm);
4609 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4610
4611 if (prev)
4612 TREE_CHAIN (prev) = parm;
4613 else
4614 list = parm;
4615
4616 return list;
4617 }
4618
4619 /* The end of a template parameter list has been reached. Process the
4620 tree list into a parameter vector, converting each parameter into a more
4621 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4622 as PARM_DECLs. */
4623
4624 tree
4625 end_template_parm_list (tree parms)
4626 {
4627 tree saved_parmlist = make_tree_vec (list_length (parms));
4628
4629 /* Pop the dummy parameter level and add the real one. We do not
4630 morph the dummy parameter in place, as it might have been
4631 captured by a (nested) template-template-parm. */
4632 current_template_parms = TREE_CHAIN (current_template_parms);
4633
4634 current_template_parms
4635 = tree_cons (size_int (processing_template_decl),
4636 saved_parmlist, current_template_parms);
4637
4638 for (unsigned ix = 0; parms; ix++)
4639 {
4640 tree parm = parms;
4641 parms = TREE_CHAIN (parms);
4642 TREE_CHAIN (parm) = NULL_TREE;
4643
4644 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4645 }
4646
4647 --processing_template_parmlist;
4648
4649 return saved_parmlist;
4650 }
4651
4652 // Explicitly indicate the end of the template parameter list. We assume
4653 // that the current template parameters have been constructed and/or
4654 // managed explicitly, as when creating new template template parameters
4655 // from a shorthand constraint.
4656 void
4657 end_template_parm_list ()
4658 {
4659 --processing_template_parmlist;
4660 }
4661
4662 /* end_template_decl is called after a template declaration is seen. */
4663
4664 void
4665 end_template_decl (void)
4666 {
4667 reset_specialization ();
4668
4669 if (! processing_template_decl)
4670 return;
4671
4672 /* This matches the pushlevel in begin_template_parm_list. */
4673 finish_scope ();
4674
4675 --processing_template_decl;
4676 current_template_parms = TREE_CHAIN (current_template_parms);
4677 }
4678
4679 /* Takes a TREE_LIST representing a template parameter and convert it
4680 into an argument suitable to be passed to the type substitution
4681 functions. Note that If the TREE_LIST contains an error_mark
4682 node, the returned argument is error_mark_node. */
4683
4684 tree
4685 template_parm_to_arg (tree t)
4686 {
4687
4688 if (t == NULL_TREE
4689 || TREE_CODE (t) != TREE_LIST)
4690 return t;
4691
4692 if (error_operand_p (TREE_VALUE (t)))
4693 return error_mark_node;
4694
4695 t = TREE_VALUE (t);
4696
4697 if (TREE_CODE (t) == TYPE_DECL
4698 || TREE_CODE (t) == TEMPLATE_DECL)
4699 {
4700 t = TREE_TYPE (t);
4701
4702 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4703 {
4704 /* Turn this argument into a TYPE_ARGUMENT_PACK
4705 with a single element, which expands T. */
4706 tree vec = make_tree_vec (1);
4707 if (CHECKING_P)
4708 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4709
4710 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4711
4712 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4713 SET_ARGUMENT_PACK_ARGS (t, vec);
4714 }
4715 }
4716 else
4717 {
4718 t = DECL_INITIAL (t);
4719
4720 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4721 {
4722 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4723 with a single element, which expands T. */
4724 tree vec = make_tree_vec (1);
4725 if (CHECKING_P)
4726 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4727
4728 t = convert_from_reference (t);
4729 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4730
4731 t = make_node (NONTYPE_ARGUMENT_PACK);
4732 SET_ARGUMENT_PACK_ARGS (t, vec);
4733 }
4734 else
4735 t = convert_from_reference (t);
4736 }
4737 return t;
4738 }
4739
4740 /* Given a single level of template parameters (a TREE_VEC), return it
4741 as a set of template arguments. */
4742
4743 tree
4744 template_parms_level_to_args (tree parms)
4745 {
4746 tree a = copy_node (parms);
4747 TREE_TYPE (a) = NULL_TREE;
4748 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4749 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4750
4751 if (CHECKING_P)
4752 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4753
4754 return a;
4755 }
4756
4757 /* Given a set of template parameters, return them as a set of template
4758 arguments. The template parameters are represented as a TREE_VEC, in
4759 the form documented in cp-tree.h for template arguments. */
4760
4761 tree
4762 template_parms_to_args (tree parms)
4763 {
4764 tree header;
4765 tree args = NULL_TREE;
4766 int length = TMPL_PARMS_DEPTH (parms);
4767 int l = length;
4768
4769 /* If there is only one level of template parameters, we do not
4770 create a TREE_VEC of TREE_VECs. Instead, we return a single
4771 TREE_VEC containing the arguments. */
4772 if (length > 1)
4773 args = make_tree_vec (length);
4774
4775 for (header = parms; header; header = TREE_CHAIN (header))
4776 {
4777 tree a = template_parms_level_to_args (TREE_VALUE (header));
4778
4779 if (length > 1)
4780 TREE_VEC_ELT (args, --l) = a;
4781 else
4782 args = a;
4783 }
4784
4785 return args;
4786 }
4787
4788 /* Within the declaration of a template, return the currently active
4789 template parameters as an argument TREE_VEC. */
4790
4791 static tree
4792 current_template_args (void)
4793 {
4794 return template_parms_to_args (current_template_parms);
4795 }
4796
4797 /* Return the fully generic arguments for of TMPL, i.e. what
4798 current_template_args would be while parsing it. */
4799
4800 tree
4801 generic_targs_for (tree tmpl)
4802 {
4803 if (tmpl == NULL_TREE)
4804 return NULL_TREE;
4805 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4806 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4807 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4808 template parameter, it has no TEMPLATE_INFO; for a partial
4809 specialization, it has the arguments for the primary template, and we
4810 want the arguments for the partial specialization. */;
4811 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4812 if (tree ti = get_template_info (result))
4813 return TI_ARGS (ti);
4814 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4815 }
4816
4817 /* Update the declared TYPE by doing any lookups which were thought to be
4818 dependent, but are not now that we know the SCOPE of the declarator. */
4819
4820 tree
4821 maybe_update_decl_type (tree orig_type, tree scope)
4822 {
4823 tree type = orig_type;
4824
4825 if (type == NULL_TREE)
4826 return type;
4827
4828 if (TREE_CODE (orig_type) == TYPE_DECL)
4829 type = TREE_TYPE (type);
4830
4831 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4832 && dependent_type_p (type)
4833 /* Don't bother building up the args in this case. */
4834 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4835 {
4836 /* tsubst in the args corresponding to the template parameters,
4837 including auto if present. Most things will be unchanged, but
4838 make_typename_type and tsubst_qualified_id will resolve
4839 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4840 tree args = current_template_args ();
4841 tree auto_node = type_uses_auto (type);
4842 tree pushed;
4843 if (auto_node)
4844 {
4845 tree auto_vec = make_tree_vec (1);
4846 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4847 args = add_to_template_args (args, auto_vec);
4848 }
4849 pushed = push_scope (scope);
4850 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4851 if (pushed)
4852 pop_scope (scope);
4853 }
4854
4855 if (type == error_mark_node)
4856 return orig_type;
4857
4858 if (TREE_CODE (orig_type) == TYPE_DECL)
4859 {
4860 if (same_type_p (type, TREE_TYPE (orig_type)))
4861 type = orig_type;
4862 else
4863 type = TYPE_NAME (type);
4864 }
4865 return type;
4866 }
4867
4868 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4869 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4870 the new template is a member template. */
4871
4872 static tree
4873 build_template_decl (tree decl, tree parms, bool member_template_p)
4874 {
4875 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4876 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4877 DECL_TEMPLATE_PARMS (tmpl) = parms;
4878 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4879 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4880 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4881
4882 return tmpl;
4883 }
4884
4885 struct template_parm_data
4886 {
4887 /* The level of the template parameters we are currently
4888 processing. */
4889 int level;
4890
4891 /* The index of the specialization argument we are currently
4892 processing. */
4893 int current_arg;
4894
4895 /* An array whose size is the number of template parameters. The
4896 elements are nonzero if the parameter has been used in any one
4897 of the arguments processed so far. */
4898 int* parms;
4899
4900 /* An array whose size is the number of template arguments. The
4901 elements are nonzero if the argument makes use of template
4902 parameters of this level. */
4903 int* arg_uses_template_parms;
4904 };
4905
4906 /* Subroutine of push_template_decl used to see if each template
4907 parameter in a partial specialization is used in the explicit
4908 argument list. If T is of the LEVEL given in DATA (which is
4909 treated as a template_parm_data*), then DATA->PARMS is marked
4910 appropriately. */
4911
4912 static int
4913 mark_template_parm (tree t, void* data)
4914 {
4915 int level;
4916 int idx;
4917 struct template_parm_data* tpd = (struct template_parm_data*) data;
4918
4919 template_parm_level_and_index (t, &level, &idx);
4920
4921 if (level == tpd->level)
4922 {
4923 tpd->parms[idx] = 1;
4924 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4925 }
4926
4927 /* In C++17 the type of a non-type argument is a deduced context. */
4928 if (cxx_dialect >= cxx17
4929 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4930 for_each_template_parm (TREE_TYPE (t),
4931 &mark_template_parm,
4932 data,
4933 NULL,
4934 /*include_nondeduced_p=*/false);
4935
4936 /* Return zero so that for_each_template_parm will continue the
4937 traversal of the tree; we want to mark *every* template parm. */
4938 return 0;
4939 }
4940
4941 /* Process the partial specialization DECL. */
4942
4943 static tree
4944 process_partial_specialization (tree decl)
4945 {
4946 tree type = TREE_TYPE (decl);
4947 tree tinfo = get_template_info (decl);
4948 tree maintmpl = TI_TEMPLATE (tinfo);
4949 tree specargs = TI_ARGS (tinfo);
4950 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4951 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4952 tree inner_parms;
4953 tree inst;
4954 int nargs = TREE_VEC_LENGTH (inner_args);
4955 int ntparms;
4956 int i;
4957 bool did_error_intro = false;
4958 struct template_parm_data tpd;
4959 struct template_parm_data tpd2;
4960
4961 gcc_assert (current_template_parms);
4962
4963 /* A concept cannot be specialized. */
4964 if (flag_concepts && variable_concept_p (maintmpl))
4965 {
4966 error ("specialization of variable concept %q#D", maintmpl);
4967 return error_mark_node;
4968 }
4969
4970 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4971 ntparms = TREE_VEC_LENGTH (inner_parms);
4972
4973 /* We check that each of the template parameters given in the
4974 partial specialization is used in the argument list to the
4975 specialization. For example:
4976
4977 template <class T> struct S;
4978 template <class T> struct S<T*>;
4979
4980 The second declaration is OK because `T*' uses the template
4981 parameter T, whereas
4982
4983 template <class T> struct S<int>;
4984
4985 is no good. Even trickier is:
4986
4987 template <class T>
4988 struct S1
4989 {
4990 template <class U>
4991 struct S2;
4992 template <class U>
4993 struct S2<T>;
4994 };
4995
4996 The S2<T> declaration is actually invalid; it is a
4997 full-specialization. Of course,
4998
4999 template <class U>
5000 struct S2<T (*)(U)>;
5001
5002 or some such would have been OK. */
5003 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5004 tpd.parms = XALLOCAVEC (int, ntparms);
5005 memset (tpd.parms, 0, sizeof (int) * ntparms);
5006
5007 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5008 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5009 for (i = 0; i < nargs; ++i)
5010 {
5011 tpd.current_arg = i;
5012 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5013 &mark_template_parm,
5014 &tpd,
5015 NULL,
5016 /*include_nondeduced_p=*/false);
5017 }
5018 for (i = 0; i < ntparms; ++i)
5019 if (tpd.parms[i] == 0)
5020 {
5021 /* One of the template parms was not used in a deduced context in the
5022 specialization. */
5023 if (!did_error_intro)
5024 {
5025 error ("template parameters not deducible in "
5026 "partial specialization:");
5027 did_error_intro = true;
5028 }
5029
5030 inform (input_location, " %qD",
5031 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5032 }
5033
5034 if (did_error_intro)
5035 return error_mark_node;
5036
5037 /* [temp.class.spec]
5038
5039 The argument list of the specialization shall not be identical to
5040 the implicit argument list of the primary template. */
5041 tree main_args
5042 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5043 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5044 && (!flag_concepts
5045 || !strictly_subsumes (current_template_constraints (),
5046 inner_args, maintmpl)))
5047 {
5048 if (!flag_concepts)
5049 error ("partial specialization %q+D does not specialize "
5050 "any template arguments; to define the primary template, "
5051 "remove the template argument list", decl);
5052 else
5053 error ("partial specialization %q+D does not specialize any "
5054 "template arguments and is not more constrained than "
5055 "the primary template; to define the primary template, "
5056 "remove the template argument list", decl);
5057 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5058 }
5059
5060 /* A partial specialization that replaces multiple parameters of the
5061 primary template with a pack expansion is less specialized for those
5062 parameters. */
5063 if (nargs < DECL_NTPARMS (maintmpl))
5064 {
5065 error ("partial specialization is not more specialized than the "
5066 "primary template because it replaces multiple parameters "
5067 "with a pack expansion");
5068 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5069 /* Avoid crash in process_partial_specialization. */
5070 return decl;
5071 }
5072
5073 else if (nargs > DECL_NTPARMS (maintmpl))
5074 {
5075 error ("too many arguments for partial specialization %qT", type);
5076 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5077 /* Avoid crash below. */
5078 return decl;
5079 }
5080
5081 /* If we aren't in a dependent class, we can actually try deduction. */
5082 else if (tpd.level == 1
5083 /* FIXME we should be able to handle a partial specialization of a
5084 partial instantiation, but currently we can't (c++/41727). */
5085 && TMPL_ARGS_DEPTH (specargs) == 1
5086 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5087 {
5088 auto_diagnostic_group d;
5089 if (permerror (input_location, "partial specialization %qD is not "
5090 "more specialized than", decl))
5091 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5092 maintmpl);
5093 }
5094
5095 /* [temp.class.spec]
5096
5097 A partially specialized non-type argument expression shall not
5098 involve template parameters of the partial specialization except
5099 when the argument expression is a simple identifier.
5100
5101 The type of a template parameter corresponding to a specialized
5102 non-type argument shall not be dependent on a parameter of the
5103 specialization.
5104
5105 Also, we verify that pack expansions only occur at the
5106 end of the argument list. */
5107 tpd2.parms = 0;
5108 for (i = 0; i < nargs; ++i)
5109 {
5110 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5111 tree arg = TREE_VEC_ELT (inner_args, i);
5112 tree packed_args = NULL_TREE;
5113 int j, len = 1;
5114
5115 if (ARGUMENT_PACK_P (arg))
5116 {
5117 /* Extract the arguments from the argument pack. We'll be
5118 iterating over these in the following loop. */
5119 packed_args = ARGUMENT_PACK_ARGS (arg);
5120 len = TREE_VEC_LENGTH (packed_args);
5121 }
5122
5123 for (j = 0; j < len; j++)
5124 {
5125 if (packed_args)
5126 /* Get the Jth argument in the parameter pack. */
5127 arg = TREE_VEC_ELT (packed_args, j);
5128
5129 if (PACK_EXPANSION_P (arg))
5130 {
5131 /* Pack expansions must come at the end of the
5132 argument list. */
5133 if ((packed_args && j < len - 1)
5134 || (!packed_args && i < nargs - 1))
5135 {
5136 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5137 error ("parameter pack argument %qE must be at the "
5138 "end of the template argument list", arg);
5139 else
5140 error ("parameter pack argument %qT must be at the "
5141 "end of the template argument list", arg);
5142 }
5143 }
5144
5145 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5146 /* We only care about the pattern. */
5147 arg = PACK_EXPANSION_PATTERN (arg);
5148
5149 if (/* These first two lines are the `non-type' bit. */
5150 !TYPE_P (arg)
5151 && TREE_CODE (arg) != TEMPLATE_DECL
5152 /* This next two lines are the `argument expression is not just a
5153 simple identifier' condition and also the `specialized
5154 non-type argument' bit. */
5155 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5156 && !((REFERENCE_REF_P (arg)
5157 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5158 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5159 {
5160 if ((!packed_args && tpd.arg_uses_template_parms[i])
5161 || (packed_args && uses_template_parms (arg)))
5162 error_at (cp_expr_loc_or_input_loc (arg),
5163 "template argument %qE involves template "
5164 "parameter(s)", arg);
5165 else
5166 {
5167 /* Look at the corresponding template parameter,
5168 marking which template parameters its type depends
5169 upon. */
5170 tree type = TREE_TYPE (parm);
5171
5172 if (!tpd2.parms)
5173 {
5174 /* We haven't yet initialized TPD2. Do so now. */
5175 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5176 /* The number of parameters here is the number in the
5177 main template, which, as checked in the assertion
5178 above, is NARGS. */
5179 tpd2.parms = XALLOCAVEC (int, nargs);
5180 tpd2.level =
5181 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5182 }
5183
5184 /* Mark the template parameters. But this time, we're
5185 looking for the template parameters of the main
5186 template, not in the specialization. */
5187 tpd2.current_arg = i;
5188 tpd2.arg_uses_template_parms[i] = 0;
5189 memset (tpd2.parms, 0, sizeof (int) * nargs);
5190 for_each_template_parm (type,
5191 &mark_template_parm,
5192 &tpd2,
5193 NULL,
5194 /*include_nondeduced_p=*/false);
5195
5196 if (tpd2.arg_uses_template_parms [i])
5197 {
5198 /* The type depended on some template parameters.
5199 If they are fully specialized in the
5200 specialization, that's OK. */
5201 int j;
5202 int count = 0;
5203 for (j = 0; j < nargs; ++j)
5204 if (tpd2.parms[j] != 0
5205 && tpd.arg_uses_template_parms [j])
5206 ++count;
5207 if (count != 0)
5208 error_n (input_location, count,
5209 "type %qT of template argument %qE depends "
5210 "on a template parameter",
5211 "type %qT of template argument %qE depends "
5212 "on template parameters",
5213 type,
5214 arg);
5215 }
5216 }
5217 }
5218 }
5219 }
5220
5221 /* We should only get here once. */
5222 if (TREE_CODE (decl) == TYPE_DECL)
5223 gcc_assert (!COMPLETE_TYPE_P (type));
5224
5225 // Build the template decl.
5226 tree tmpl = build_template_decl (decl, current_template_parms,
5227 DECL_MEMBER_TEMPLATE_P (maintmpl));
5228 TREE_TYPE (tmpl) = type;
5229 DECL_TEMPLATE_RESULT (tmpl) = decl;
5230 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5231 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5232 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5233
5234 /* Give template template parms a DECL_CONTEXT of the template
5235 for which they are a parameter. */
5236 for (i = 0; i < ntparms; ++i)
5237 {
5238 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5239 if (TREE_CODE (parm) == TEMPLATE_DECL)
5240 DECL_CONTEXT (parm) = tmpl;
5241 }
5242
5243 if (VAR_P (decl))
5244 /* We didn't register this in check_explicit_specialization so we could
5245 wait until the constraints were set. */
5246 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5247 else
5248 associate_classtype_constraints (type);
5249
5250 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5251 = tree_cons (specargs, tmpl,
5252 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5253 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5254
5255 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5256 inst = TREE_CHAIN (inst))
5257 {
5258 tree instance = TREE_VALUE (inst);
5259 if (TYPE_P (instance)
5260 ? (COMPLETE_TYPE_P (instance)
5261 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5262 : DECL_TEMPLATE_INSTANTIATION (instance))
5263 {
5264 tree spec = most_specialized_partial_spec (instance, tf_none);
5265 tree inst_decl = (DECL_P (instance)
5266 ? instance : TYPE_NAME (instance));
5267 if (!spec)
5268 /* OK */;
5269 else if (spec == error_mark_node)
5270 permerror (input_location,
5271 "declaration of %qD ambiguates earlier template "
5272 "instantiation for %qD", decl, inst_decl);
5273 else if (TREE_VALUE (spec) == tmpl)
5274 permerror (input_location,
5275 "partial specialization of %qD after instantiation "
5276 "of %qD", decl, inst_decl);
5277 }
5278 }
5279
5280 return decl;
5281 }
5282
5283 /* PARM is a template parameter of some form; return the corresponding
5284 TEMPLATE_PARM_INDEX. */
5285
5286 static tree
5287 get_template_parm_index (tree parm)
5288 {
5289 if (TREE_CODE (parm) == PARM_DECL
5290 || TREE_CODE (parm) == CONST_DECL)
5291 parm = DECL_INITIAL (parm);
5292 else if (TREE_CODE (parm) == TYPE_DECL
5293 || TREE_CODE (parm) == TEMPLATE_DECL)
5294 parm = TREE_TYPE (parm);
5295 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5296 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5297 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5298 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5299 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5300 return parm;
5301 }
5302
5303 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5304 parameter packs used by the template parameter PARM. */
5305
5306 static void
5307 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5308 {
5309 /* A type parm can't refer to another parm. */
5310 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5311 return;
5312 else if (TREE_CODE (parm) == PARM_DECL)
5313 {
5314 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5315 ppd, ppd->visited);
5316 return;
5317 }
5318
5319 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5320
5321 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5322 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5323 {
5324 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5325 if (template_parameter_pack_p (p))
5326 /* Any packs in the type are expanded by this parameter. */;
5327 else
5328 fixed_parameter_pack_p_1 (p, ppd);
5329 }
5330 }
5331
5332 /* PARM is a template parameter pack. Return any parameter packs used in
5333 its type or the type of any of its template parameters. If there are
5334 any such packs, it will be instantiated into a fixed template parameter
5335 list by partial instantiation rather than be fully deduced. */
5336
5337 tree
5338 fixed_parameter_pack_p (tree parm)
5339 {
5340 /* This can only be true in a member template. */
5341 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5342 return NULL_TREE;
5343 /* This can only be true for a parameter pack. */
5344 if (!template_parameter_pack_p (parm))
5345 return NULL_TREE;
5346 /* A type parm can't refer to another parm. */
5347 if (TREE_CODE (parm) == TYPE_DECL)
5348 return NULL_TREE;
5349
5350 tree parameter_packs = NULL_TREE;
5351 struct find_parameter_pack_data ppd;
5352 ppd.parameter_packs = &parameter_packs;
5353 ppd.visited = new hash_set<tree>;
5354 ppd.type_pack_expansion_p = false;
5355
5356 fixed_parameter_pack_p_1 (parm, &ppd);
5357
5358 delete ppd.visited;
5359 return parameter_packs;
5360 }
5361
5362 /* Check that a template declaration's use of default arguments and
5363 parameter packs is not invalid. Here, PARMS are the template
5364 parameters. IS_PRIMARY is true if DECL is the thing declared by
5365 a primary template. IS_PARTIAL is true if DECL is a partial
5366 specialization.
5367
5368 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5369 function template declaration or a friend class template
5370 declaration. In the function case, 1 indicates a declaration, 2
5371 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5372 emitted for extraneous default arguments.
5373
5374 Returns TRUE if there were no errors found, FALSE otherwise. */
5375
5376 bool
5377 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5378 bool is_partial, int is_friend_decl)
5379 {
5380 const char *msg;
5381 int last_level_to_check;
5382 tree parm_level;
5383 bool no_errors = true;
5384
5385 /* [temp.param]
5386
5387 A default template-argument shall not be specified in a
5388 function template declaration or a function template definition, nor
5389 in the template-parameter-list of the definition of a member of a
5390 class template. */
5391
5392 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5393 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5394 /* You can't have a function template declaration in a local
5395 scope, nor you can you define a member of a class template in a
5396 local scope. */
5397 return true;
5398
5399 if ((TREE_CODE (decl) == TYPE_DECL
5400 && TREE_TYPE (decl)
5401 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5402 || (TREE_CODE (decl) == FUNCTION_DECL
5403 && LAMBDA_FUNCTION_P (decl)))
5404 /* A lambda doesn't have an explicit declaration; don't complain
5405 about the parms of the enclosing class. */
5406 return true;
5407
5408 if (current_class_type
5409 && !TYPE_BEING_DEFINED (current_class_type)
5410 && DECL_LANG_SPECIFIC (decl)
5411 && DECL_DECLARES_FUNCTION_P (decl)
5412 /* If this is either a friend defined in the scope of the class
5413 or a member function. */
5414 && (DECL_FUNCTION_MEMBER_P (decl)
5415 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5416 : DECL_FRIEND_CONTEXT (decl)
5417 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5418 : false)
5419 /* And, if it was a member function, it really was defined in
5420 the scope of the class. */
5421 && (!DECL_FUNCTION_MEMBER_P (decl)
5422 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5423 /* We already checked these parameters when the template was
5424 declared, so there's no need to do it again now. This function
5425 was defined in class scope, but we're processing its body now
5426 that the class is complete. */
5427 return true;
5428
5429 /* Core issue 226 (C++0x only): the following only applies to class
5430 templates. */
5431 if (is_primary
5432 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5433 {
5434 /* [temp.param]
5435
5436 If a template-parameter has a default template-argument, all
5437 subsequent template-parameters shall have a default
5438 template-argument supplied. */
5439 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5440 {
5441 tree inner_parms = TREE_VALUE (parm_level);
5442 int ntparms = TREE_VEC_LENGTH (inner_parms);
5443 int seen_def_arg_p = 0;
5444 int i;
5445
5446 for (i = 0; i < ntparms; ++i)
5447 {
5448 tree parm = TREE_VEC_ELT (inner_parms, i);
5449
5450 if (parm == error_mark_node)
5451 continue;
5452
5453 if (TREE_PURPOSE (parm))
5454 seen_def_arg_p = 1;
5455 else if (seen_def_arg_p
5456 && !template_parameter_pack_p (TREE_VALUE (parm)))
5457 {
5458 error ("no default argument for %qD", TREE_VALUE (parm));
5459 /* For better subsequent error-recovery, we indicate that
5460 there should have been a default argument. */
5461 TREE_PURPOSE (parm) = error_mark_node;
5462 no_errors = false;
5463 }
5464 else if (!is_partial
5465 && !is_friend_decl
5466 /* Don't complain about an enclosing partial
5467 specialization. */
5468 && parm_level == parms
5469 && TREE_CODE (decl) == TYPE_DECL
5470 && i < ntparms - 1
5471 && template_parameter_pack_p (TREE_VALUE (parm))
5472 /* A fixed parameter pack will be partially
5473 instantiated into a fixed length list. */
5474 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5475 {
5476 /* A primary class template can only have one
5477 parameter pack, at the end of the template
5478 parameter list. */
5479
5480 error ("parameter pack %q+D must be at the end of the"
5481 " template parameter list", TREE_VALUE (parm));
5482
5483 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5484 = error_mark_node;
5485 no_errors = false;
5486 }
5487 }
5488 }
5489 }
5490
5491 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5492 || is_partial
5493 || !is_primary
5494 || is_friend_decl)
5495 /* For an ordinary class template, default template arguments are
5496 allowed at the innermost level, e.g.:
5497 template <class T = int>
5498 struct S {};
5499 but, in a partial specialization, they're not allowed even
5500 there, as we have in [temp.class.spec]:
5501
5502 The template parameter list of a specialization shall not
5503 contain default template argument values.
5504
5505 So, for a partial specialization, or for a function template
5506 (in C++98/C++03), we look at all of them. */
5507 ;
5508 else
5509 /* But, for a primary class template that is not a partial
5510 specialization we look at all template parameters except the
5511 innermost ones. */
5512 parms = TREE_CHAIN (parms);
5513
5514 /* Figure out what error message to issue. */
5515 if (is_friend_decl == 2)
5516 msg = G_("default template arguments may not be used in function template "
5517 "friend re-declaration");
5518 else if (is_friend_decl)
5519 msg = G_("default template arguments may not be used in template "
5520 "friend declarations");
5521 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5522 msg = G_("default template arguments may not be used in function templates "
5523 "without %<-std=c++11%> or %<-std=gnu++11%>");
5524 else if (is_partial)
5525 msg = G_("default template arguments may not be used in "
5526 "partial specializations");
5527 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5528 msg = G_("default argument for template parameter for class enclosing %qD");
5529 else
5530 /* Per [temp.param]/9, "A default template-argument shall not be
5531 specified in the template-parameter-lists of the definition of
5532 a member of a class template that appears outside of the member's
5533 class.", thus if we aren't handling a member of a class template
5534 there is no need to examine the parameters. */
5535 return true;
5536
5537 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5538 /* If we're inside a class definition, there's no need to
5539 examine the parameters to the class itself. On the one
5540 hand, they will be checked when the class is defined, and,
5541 on the other, default arguments are valid in things like:
5542 template <class T = double>
5543 struct S { template <class U> void f(U); };
5544 Here the default argument for `S' has no bearing on the
5545 declaration of `f'. */
5546 last_level_to_check = template_class_depth (current_class_type) + 1;
5547 else
5548 /* Check everything. */
5549 last_level_to_check = 0;
5550
5551 for (parm_level = parms;
5552 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5553 parm_level = TREE_CHAIN (parm_level))
5554 {
5555 tree inner_parms = TREE_VALUE (parm_level);
5556 int i;
5557 int ntparms;
5558
5559 ntparms = TREE_VEC_LENGTH (inner_parms);
5560 for (i = 0; i < ntparms; ++i)
5561 {
5562 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5563 continue;
5564
5565 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5566 {
5567 if (msg)
5568 {
5569 no_errors = false;
5570 if (is_friend_decl == 2)
5571 return no_errors;
5572
5573 error (msg, decl);
5574 msg = 0;
5575 }
5576
5577 /* Clear out the default argument so that we are not
5578 confused later. */
5579 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5580 }
5581 }
5582
5583 /* At this point, if we're still interested in issuing messages,
5584 they must apply to classes surrounding the object declared. */
5585 if (msg)
5586 msg = G_("default argument for template parameter for class "
5587 "enclosing %qD");
5588 }
5589
5590 return no_errors;
5591 }
5592
5593 /* Worker for push_template_decl_real, called via
5594 for_each_template_parm. DATA is really an int, indicating the
5595 level of the parameters we are interested in. If T is a template
5596 parameter of that level, return nonzero. */
5597
5598 static int
5599 template_parm_this_level_p (tree t, void* data)
5600 {
5601 int this_level = *(int *)data;
5602 int level;
5603
5604 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5605 level = TEMPLATE_PARM_LEVEL (t);
5606 else
5607 level = TEMPLATE_TYPE_LEVEL (t);
5608 return level == this_level;
5609 }
5610
5611 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5612 DATA is really an int, indicating the innermost outer level of parameters.
5613 If T is a template parameter of that level or further out, return
5614 nonzero. */
5615
5616 static int
5617 template_parm_outer_level (tree t, void *data)
5618 {
5619 int this_level = *(int *)data;
5620 int level;
5621
5622 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5623 level = TEMPLATE_PARM_LEVEL (t);
5624 else
5625 level = TEMPLATE_TYPE_LEVEL (t);
5626 return level <= this_level;
5627 }
5628
5629 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5630 parameters given by current_template_args, or reuses a
5631 previously existing one, if appropriate. Returns the DECL, or an
5632 equivalent one, if it is replaced via a call to duplicate_decls.
5633
5634 If IS_FRIEND is true, DECL is a friend declaration. */
5635
5636 tree
5637 push_template_decl_real (tree decl, bool is_friend)
5638 {
5639 tree tmpl;
5640 tree args;
5641 tree info;
5642 tree ctx;
5643 bool is_primary;
5644 bool is_partial;
5645 int new_template_p = 0;
5646 /* True if the template is a member template, in the sense of
5647 [temp.mem]. */
5648 bool member_template_p = false;
5649
5650 if (decl == error_mark_node || !current_template_parms)
5651 return error_mark_node;
5652
5653 /* See if this is a partial specialization. */
5654 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5655 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5656 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5657 || (VAR_P (decl)
5658 && DECL_LANG_SPECIFIC (decl)
5659 && DECL_TEMPLATE_SPECIALIZATION (decl)
5660 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5661
5662 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5663 is_friend = true;
5664
5665 if (is_friend)
5666 /* For a friend, we want the context of the friend, not
5667 the type of which it is a friend. */
5668 ctx = CP_DECL_CONTEXT (decl);
5669 else if (CP_DECL_CONTEXT (decl)
5670 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5671 /* In the case of a virtual function, we want the class in which
5672 it is defined. */
5673 ctx = CP_DECL_CONTEXT (decl);
5674 else
5675 /* Otherwise, if we're currently defining some class, the DECL
5676 is assumed to be a member of the class. */
5677 ctx = current_scope ();
5678
5679 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5680 ctx = NULL_TREE;
5681
5682 if (!DECL_CONTEXT (decl))
5683 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5684
5685 /* See if this is a primary template. */
5686 if (is_friend && ctx
5687 && uses_template_parms_level (ctx, processing_template_decl))
5688 /* A friend template that specifies a class context, i.e.
5689 template <typename T> friend void A<T>::f();
5690 is not primary. */
5691 is_primary = false;
5692 else if (TREE_CODE (decl) == TYPE_DECL
5693 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5694 is_primary = false;
5695 else
5696 is_primary = template_parm_scope_p ();
5697
5698 if (is_primary)
5699 {
5700 warning (OPT_Wtemplates, "template %qD declared", decl);
5701
5702 if (DECL_CLASS_SCOPE_P (decl))
5703 member_template_p = true;
5704 if (TREE_CODE (decl) == TYPE_DECL
5705 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5706 {
5707 error ("template class without a name");
5708 return error_mark_node;
5709 }
5710 else if (TREE_CODE (decl) == FUNCTION_DECL)
5711 {
5712 if (member_template_p)
5713 {
5714 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5715 error ("member template %qD may not have virt-specifiers", decl);
5716 }
5717 if (DECL_DESTRUCTOR_P (decl))
5718 {
5719 /* [temp.mem]
5720
5721 A destructor shall not be a member template. */
5722 error_at (DECL_SOURCE_LOCATION (decl),
5723 "destructor %qD declared as member template", decl);
5724 return error_mark_node;
5725 }
5726 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5727 && (!prototype_p (TREE_TYPE (decl))
5728 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5729 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5730 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5731 == void_list_node)))
5732 {
5733 /* [basic.stc.dynamic.allocation]
5734
5735 An allocation function can be a function
5736 template. ... Template allocation functions shall
5737 have two or more parameters. */
5738 error ("invalid template declaration of %qD", decl);
5739 return error_mark_node;
5740 }
5741 }
5742 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5743 && CLASS_TYPE_P (TREE_TYPE (decl)))
5744 {
5745 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5746 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5747 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5748 {
5749 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5750 if (TREE_CODE (t) == TYPE_DECL)
5751 t = TREE_TYPE (t);
5752 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5753 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5754 }
5755 }
5756 else if (TREE_CODE (decl) == TYPE_DECL
5757 && TYPE_DECL_ALIAS_P (decl))
5758 /* alias-declaration */
5759 gcc_assert (!DECL_ARTIFICIAL (decl));
5760 else if (VAR_P (decl))
5761 /* C++14 variable template. */;
5762 else if (TREE_CODE (decl) == CONCEPT_DECL)
5763 /* C++2a concept definitions. */;
5764 else
5765 {
5766 error ("template declaration of %q#D", decl);
5767 return error_mark_node;
5768 }
5769 }
5770
5771 /* Check to see that the rules regarding the use of default
5772 arguments are not being violated. We check args for a friend
5773 functions when we know whether it's a definition, introducing
5774 declaration or re-declaration. */
5775 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5776 check_default_tmpl_args (decl, current_template_parms,
5777 is_primary, is_partial, is_friend);
5778
5779 /* Ensure that there are no parameter packs in the type of this
5780 declaration that have not been expanded. */
5781 if (TREE_CODE (decl) == FUNCTION_DECL)
5782 {
5783 /* Check each of the arguments individually to see if there are
5784 any bare parameter packs. */
5785 tree type = TREE_TYPE (decl);
5786 tree arg = DECL_ARGUMENTS (decl);
5787 tree argtype = TYPE_ARG_TYPES (type);
5788
5789 while (arg && argtype)
5790 {
5791 if (!DECL_PACK_P (arg)
5792 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5793 {
5794 /* This is a PARM_DECL that contains unexpanded parameter
5795 packs. We have already complained about this in the
5796 check_for_bare_parameter_packs call, so just replace
5797 these types with ERROR_MARK_NODE. */
5798 TREE_TYPE (arg) = error_mark_node;
5799 TREE_VALUE (argtype) = error_mark_node;
5800 }
5801
5802 arg = DECL_CHAIN (arg);
5803 argtype = TREE_CHAIN (argtype);
5804 }
5805
5806 /* Check for bare parameter packs in the return type and the
5807 exception specifiers. */
5808 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5809 /* Errors were already issued, set return type to int
5810 as the frontend doesn't expect error_mark_node as
5811 the return type. */
5812 TREE_TYPE (type) = integer_type_node;
5813 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5814 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5815 }
5816 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5817 ? DECL_ORIGINAL_TYPE (decl)
5818 : TREE_TYPE (decl)))
5819 {
5820 TREE_TYPE (decl) = error_mark_node;
5821 return error_mark_node;
5822 }
5823
5824 if (is_partial)
5825 return process_partial_specialization (decl);
5826
5827 args = current_template_args ();
5828
5829 if (!ctx
5830 || TREE_CODE (ctx) == FUNCTION_DECL
5831 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5832 || (TREE_CODE (decl) == TYPE_DECL
5833 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5834 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5835 {
5836 if (DECL_LANG_SPECIFIC (decl)
5837 && DECL_TEMPLATE_INFO (decl)
5838 && DECL_TI_TEMPLATE (decl))
5839 tmpl = DECL_TI_TEMPLATE (decl);
5840 /* If DECL is a TYPE_DECL for a class-template, then there won't
5841 be DECL_LANG_SPECIFIC. The information equivalent to
5842 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5843 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5844 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5845 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5846 {
5847 /* Since a template declaration already existed for this
5848 class-type, we must be redeclaring it here. Make sure
5849 that the redeclaration is valid. */
5850 redeclare_class_template (TREE_TYPE (decl),
5851 current_template_parms,
5852 current_template_constraints ());
5853 /* We don't need to create a new TEMPLATE_DECL; just use the
5854 one we already had. */
5855 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5856 }
5857 else
5858 {
5859 tmpl = build_template_decl (decl, current_template_parms,
5860 member_template_p);
5861 new_template_p = 1;
5862
5863 if (DECL_LANG_SPECIFIC (decl)
5864 && DECL_TEMPLATE_SPECIALIZATION (decl))
5865 {
5866 /* A specialization of a member template of a template
5867 class. */
5868 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5869 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5870 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5871 }
5872 }
5873 }
5874 else
5875 {
5876 tree a, t, current, parms;
5877 int i;
5878 tree tinfo = get_template_info (decl);
5879
5880 if (!tinfo)
5881 {
5882 error ("template definition of non-template %q#D", decl);
5883 return error_mark_node;
5884 }
5885
5886 tmpl = TI_TEMPLATE (tinfo);
5887
5888 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5889 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5890 && DECL_TEMPLATE_SPECIALIZATION (decl)
5891 && DECL_MEMBER_TEMPLATE_P (tmpl))
5892 {
5893 tree new_tmpl;
5894
5895 /* The declaration is a specialization of a member
5896 template, declared outside the class. Therefore, the
5897 innermost template arguments will be NULL, so we
5898 replace them with the arguments determined by the
5899 earlier call to check_explicit_specialization. */
5900 args = DECL_TI_ARGS (decl);
5901
5902 new_tmpl
5903 = build_template_decl (decl, current_template_parms,
5904 member_template_p);
5905 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5906 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5907 DECL_TI_TEMPLATE (decl) = new_tmpl;
5908 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5909 DECL_TEMPLATE_INFO (new_tmpl)
5910 = build_template_info (tmpl, args);
5911
5912 register_specialization (new_tmpl,
5913 most_general_template (tmpl),
5914 args,
5915 is_friend, 0);
5916 return decl;
5917 }
5918
5919 /* Make sure the template headers we got make sense. */
5920
5921 parms = DECL_TEMPLATE_PARMS (tmpl);
5922 i = TMPL_PARMS_DEPTH (parms);
5923 if (TMPL_ARGS_DEPTH (args) != i)
5924 {
5925 error ("expected %d levels of template parms for %q#D, got %d",
5926 i, decl, TMPL_ARGS_DEPTH (args));
5927 DECL_INTERFACE_KNOWN (decl) = 1;
5928 return error_mark_node;
5929 }
5930 else
5931 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5932 {
5933 a = TMPL_ARGS_LEVEL (args, i);
5934 t = INNERMOST_TEMPLATE_PARMS (parms);
5935
5936 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5937 {
5938 if (current == decl)
5939 error ("got %d template parameters for %q#D",
5940 TREE_VEC_LENGTH (a), decl);
5941 else
5942 error ("got %d template parameters for %q#T",
5943 TREE_VEC_LENGTH (a), current);
5944 error (" but %d required", TREE_VEC_LENGTH (t));
5945 /* Avoid crash in import_export_decl. */
5946 DECL_INTERFACE_KNOWN (decl) = 1;
5947 return error_mark_node;
5948 }
5949
5950 if (current == decl)
5951 current = ctx;
5952 else if (current == NULL_TREE)
5953 /* Can happen in erroneous input. */
5954 break;
5955 else
5956 current = get_containing_scope (current);
5957 }
5958
5959 /* Check that the parms are used in the appropriate qualifying scopes
5960 in the declarator. */
5961 if (!comp_template_args
5962 (TI_ARGS (tinfo),
5963 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5964 {
5965 error ("template arguments to %qD do not match original "
5966 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5967 if (!uses_template_parms (TI_ARGS (tinfo)))
5968 inform (input_location, "use %<template<>%> for"
5969 " an explicit specialization");
5970 /* Avoid crash in import_export_decl. */
5971 DECL_INTERFACE_KNOWN (decl) = 1;
5972 return error_mark_node;
5973 }
5974 }
5975
5976 DECL_TEMPLATE_RESULT (tmpl) = decl;
5977 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5978
5979 /* Push template declarations for global functions and types. Note
5980 that we do not try to push a global template friend declared in a
5981 template class; such a thing may well depend on the template
5982 parameters of the class. */
5983 if (new_template_p && !ctx
5984 && !(is_friend && template_class_depth (current_class_type) > 0))
5985 {
5986 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5987 if (tmpl == error_mark_node)
5988 return error_mark_node;
5989
5990 /* Hide template friend classes that haven't been declared yet. */
5991 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5992 {
5993 DECL_ANTICIPATED (tmpl) = 1;
5994 DECL_FRIEND_P (tmpl) = 1;
5995 }
5996 }
5997
5998 if (is_primary)
5999 {
6000 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6001
6002 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6003
6004 /* Give template template parms a DECL_CONTEXT of the template
6005 for which they are a parameter. */
6006 parms = INNERMOST_TEMPLATE_PARMS (parms);
6007 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6008 {
6009 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6010 if (TREE_CODE (parm) == TEMPLATE_DECL)
6011 DECL_CONTEXT (parm) = tmpl;
6012 }
6013
6014 if (TREE_CODE (decl) == TYPE_DECL
6015 && TYPE_DECL_ALIAS_P (decl))
6016 {
6017 if (tree constr
6018 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6019 {
6020 /* ??? Why don't we do this here for all templates? */
6021 constr = build_constraints (constr, NULL_TREE);
6022 set_constraints (decl, constr);
6023 }
6024 if (complex_alias_template_p (tmpl))
6025 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6026 }
6027 }
6028
6029 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6030 back to its most general template. If TMPL is a specialization,
6031 ARGS may only have the innermost set of arguments. Add the missing
6032 argument levels if necessary. */
6033 if (DECL_TEMPLATE_INFO (tmpl))
6034 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6035
6036 info = build_template_info (tmpl, args);
6037
6038 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6039 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6040 else
6041 {
6042 if (is_primary)
6043 retrofit_lang_decl (decl);
6044 if (DECL_LANG_SPECIFIC (decl))
6045 DECL_TEMPLATE_INFO (decl) = info;
6046 }
6047
6048 if (flag_implicit_templates
6049 && !is_friend
6050 && TREE_PUBLIC (decl)
6051 && VAR_OR_FUNCTION_DECL_P (decl))
6052 /* Set DECL_COMDAT on template instantiations; if we force
6053 them to be emitted by explicit instantiation,
6054 mark_needed will tell cgraph to do the right thing. */
6055 DECL_COMDAT (decl) = true;
6056
6057 return DECL_TEMPLATE_RESULT (tmpl);
6058 }
6059
6060 tree
6061 push_template_decl (tree decl)
6062 {
6063 return push_template_decl_real (decl, false);
6064 }
6065
6066 /* FN is an inheriting constructor that inherits from the constructor
6067 template INHERITED; turn FN into a constructor template with a matching
6068 template header. */
6069
6070 tree
6071 add_inherited_template_parms (tree fn, tree inherited)
6072 {
6073 tree inner_parms
6074 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6075 inner_parms = copy_node (inner_parms);
6076 tree parms
6077 = tree_cons (size_int (processing_template_decl + 1),
6078 inner_parms, current_template_parms);
6079 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6080 tree args = template_parms_to_args (parms);
6081 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6082 TREE_TYPE (tmpl) = TREE_TYPE (fn);
6083 DECL_TEMPLATE_RESULT (tmpl) = fn;
6084 DECL_ARTIFICIAL (tmpl) = true;
6085 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6086 return tmpl;
6087 }
6088
6089 /* Called when a class template TYPE is redeclared with the indicated
6090 template PARMS, e.g.:
6091
6092 template <class T> struct S;
6093 template <class T> struct S {}; */
6094
6095 bool
6096 redeclare_class_template (tree type, tree parms, tree cons)
6097 {
6098 tree tmpl;
6099 tree tmpl_parms;
6100 int i;
6101
6102 if (!TYPE_TEMPLATE_INFO (type))
6103 {
6104 error ("%qT is not a template type", type);
6105 return false;
6106 }
6107
6108 tmpl = TYPE_TI_TEMPLATE (type);
6109 if (!PRIMARY_TEMPLATE_P (tmpl))
6110 /* The type is nested in some template class. Nothing to worry
6111 about here; there are no new template parameters for the nested
6112 type. */
6113 return true;
6114
6115 if (!parms)
6116 {
6117 error ("template specifiers not specified in declaration of %qD",
6118 tmpl);
6119 return false;
6120 }
6121
6122 parms = INNERMOST_TEMPLATE_PARMS (parms);
6123 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6124
6125 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6126 {
6127 error_n (input_location, TREE_VEC_LENGTH (parms),
6128 "redeclared with %d template parameter",
6129 "redeclared with %d template parameters",
6130 TREE_VEC_LENGTH (parms));
6131 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6132 "previous declaration %qD used %d template parameter",
6133 "previous declaration %qD used %d template parameters",
6134 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6135 return false;
6136 }
6137
6138 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6139 {
6140 tree tmpl_parm;
6141 tree parm;
6142 tree tmpl_default;
6143 tree parm_default;
6144
6145 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6146 || TREE_VEC_ELT (parms, i) == error_mark_node)
6147 continue;
6148
6149 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6150 if (error_operand_p (tmpl_parm))
6151 return false;
6152
6153 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6154 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6155 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6156
6157 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6158 TEMPLATE_DECL. */
6159 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6160 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6161 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6162 || (TREE_CODE (tmpl_parm) != PARM_DECL
6163 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6164 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6165 || (TREE_CODE (tmpl_parm) == PARM_DECL
6166 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6167 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6168 {
6169 auto_diagnostic_group d;
6170 error ("template parameter %q+#D", tmpl_parm);
6171 inform (input_location, "redeclared here as %q#D", parm);
6172 return false;
6173 }
6174
6175 /* The parameters can be declared to introduce different
6176 constraints. */
6177 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6178 tree p2 = TREE_VEC_ELT (parms, i);
6179 if (!template_parameter_constraints_equivalent_p (p1, p2))
6180 {
6181 auto_diagnostic_group d;
6182 error ("declaration of template parameter %q+#D with different "
6183 "constraints", parm);
6184 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6185 "original declaration appeared here");
6186 return false;
6187 }
6188
6189 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6190 {
6191 /* We have in [temp.param]:
6192
6193 A template-parameter may not be given default arguments
6194 by two different declarations in the same scope. */
6195 auto_diagnostic_group d;
6196 error_at (input_location, "redefinition of default argument for %q#D", parm);
6197 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6198 "original definition appeared here");
6199 return false;
6200 }
6201
6202 if (parm_default != NULL_TREE)
6203 /* Update the previous template parameters (which are the ones
6204 that will really count) with the new default value. */
6205 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6206 else if (tmpl_default != NULL_TREE)
6207 /* Update the new parameters, too; they'll be used as the
6208 parameters for any members. */
6209 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6210
6211 /* Give each template template parm in this redeclaration a
6212 DECL_CONTEXT of the template for which they are a parameter. */
6213 if (TREE_CODE (parm) == TEMPLATE_DECL)
6214 {
6215 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6216 DECL_CONTEXT (parm) = tmpl;
6217 }
6218
6219 if (TREE_CODE (parm) == TYPE_DECL)
6220 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6221 }
6222
6223 tree ci = get_constraints (tmpl);
6224 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6225 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6226
6227 /* Two classes with different constraints declare different entities. */
6228 if (!cp_tree_equal (req1, req2))
6229 {
6230 auto_diagnostic_group d;
6231 error_at (input_location, "redeclaration %q#D with different "
6232 "constraints", tmpl);
6233 inform (DECL_SOURCE_LOCATION (tmpl),
6234 "original declaration appeared here");
6235 return false;
6236 }
6237
6238 return true;
6239 }
6240
6241 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6242 to be used when the caller has already checked
6243 (processing_template_decl
6244 && !instantiation_dependent_expression_p (expr)
6245 && potential_constant_expression (expr))
6246 and cleared processing_template_decl. */
6247
6248 tree
6249 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6250 {
6251 return tsubst_copy_and_build (expr,
6252 /*args=*/NULL_TREE,
6253 complain,
6254 /*in_decl=*/NULL_TREE,
6255 /*function_p=*/false,
6256 /*integral_constant_expression_p=*/true);
6257 }
6258
6259 /* Simplify EXPR if it is a non-dependent expression. Returns the
6260 (possibly simplified) expression. */
6261
6262 tree
6263 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6264 {
6265 if (expr == NULL_TREE)
6266 return NULL_TREE;
6267
6268 /* If we're in a template, but EXPR isn't value dependent, simplify
6269 it. We're supposed to treat:
6270
6271 template <typename T> void f(T[1 + 1]);
6272 template <typename T> void f(T[2]);
6273
6274 as two declarations of the same function, for example. */
6275 if (processing_template_decl
6276 && is_nondependent_constant_expression (expr))
6277 {
6278 processing_template_decl_sentinel s;
6279 expr = instantiate_non_dependent_expr_internal (expr, complain);
6280 }
6281 return expr;
6282 }
6283
6284 tree
6285 instantiate_non_dependent_expr (tree expr)
6286 {
6287 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6288 }
6289
6290 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6291 an uninstantiated expression. */
6292
6293 tree
6294 instantiate_non_dependent_or_null (tree expr)
6295 {
6296 if (expr == NULL_TREE)
6297 return NULL_TREE;
6298 if (processing_template_decl)
6299 {
6300 if (!is_nondependent_constant_expression (expr))
6301 expr = NULL_TREE;
6302 else
6303 {
6304 processing_template_decl_sentinel s;
6305 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6306 }
6307 }
6308 return expr;
6309 }
6310
6311 /* True iff T is a specialization of a variable template. */
6312
6313 bool
6314 variable_template_specialization_p (tree t)
6315 {
6316 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6317 return false;
6318 tree tmpl = DECL_TI_TEMPLATE (t);
6319 return variable_template_p (tmpl);
6320 }
6321
6322 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6323 template declaration, or a TYPE_DECL for an alias declaration. */
6324
6325 bool
6326 alias_type_or_template_p (tree t)
6327 {
6328 if (t == NULL_TREE)
6329 return false;
6330 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6331 || (TYPE_P (t)
6332 && TYPE_NAME (t)
6333 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6334 || DECL_ALIAS_TEMPLATE_P (t));
6335 }
6336
6337 /* If T is a specialization of an alias template, return it; otherwise return
6338 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6339
6340 tree
6341 alias_template_specialization_p (const_tree t,
6342 bool transparent_typedefs)
6343 {
6344 if (!TYPE_P (t))
6345 return NULL_TREE;
6346
6347 /* It's an alias template specialization if it's an alias and its
6348 TYPE_NAME is a specialization of a primary template. */
6349 if (typedef_variant_p (t))
6350 {
6351 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6352 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6353 return CONST_CAST_TREE (t);
6354 if (transparent_typedefs)
6355 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6356 (TYPE_NAME (t)),
6357 transparent_typedefs);
6358 }
6359
6360 return NULL_TREE;
6361 }
6362
6363 /* An alias template is complex from a SFINAE perspective if a template-id
6364 using that alias can be ill-formed when the expansion is not, as with
6365 the void_t template. We determine this by checking whether the
6366 expansion for the alias template uses all its template parameters. */
6367
6368 struct uses_all_template_parms_data
6369 {
6370 int level;
6371 bool *seen;
6372 };
6373
6374 static int
6375 uses_all_template_parms_r (tree t, void *data_)
6376 {
6377 struct uses_all_template_parms_data &data
6378 = *(struct uses_all_template_parms_data*)data_;
6379 tree idx = get_template_parm_index (t);
6380
6381 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6382 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6383 return 0;
6384 }
6385
6386 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6387
6388 static int
6389 complex_pack_expansion_r (tree t, void *data_)
6390 {
6391 /* An alias template with a pack expansion that expands a pack from the
6392 enclosing class needs to be considered complex, to avoid confusion with
6393 the same pack being used as an argument to the alias's own template
6394 parameter (91966). */
6395 if (!PACK_EXPANSION_P (t))
6396 return 0;
6397 struct uses_all_template_parms_data &data
6398 = *(struct uses_all_template_parms_data*)data_;
6399 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6400 pack = TREE_CHAIN (pack))
6401 {
6402 tree parm_pack = TREE_VALUE (pack);
6403 if (!TEMPLATE_PARM_P (parm_pack))
6404 continue;
6405 int idx, level;
6406 template_parm_level_and_index (parm_pack, &level, &idx);
6407 if (level < data.level)
6408 return 1;
6409 }
6410 return 0;
6411 }
6412
6413 static bool
6414 complex_alias_template_p (const_tree tmpl)
6415 {
6416 /* A renaming alias isn't complex. */
6417 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6418 return false;
6419
6420 /* Any other constrained alias is complex. */
6421 if (get_constraints (tmpl))
6422 return true;
6423
6424 struct uses_all_template_parms_data data;
6425 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6426 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6427 data.level = TMPL_PARMS_DEPTH (parms);
6428 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6429 data.seen = XALLOCAVEC (bool, len);
6430 for (int i = 0; i < len; ++i)
6431 data.seen[i] = false;
6432
6433 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6434 NULL, true, complex_pack_expansion_r))
6435 return true;
6436 for (int i = 0; i < len; ++i)
6437 if (!data.seen[i])
6438 return true;
6439 return false;
6440 }
6441
6442 /* If T is a specialization of a complex alias template with dependent
6443 template-arguments, return it; otherwise return NULL_TREE. If T is a
6444 typedef to such a specialization, return the specialization. */
6445
6446 tree
6447 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6448 {
6449 if (!TYPE_P (t) || !typedef_variant_p (t))
6450 return NULL_TREE;
6451
6452 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6453 if (tinfo
6454 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6455 && (any_dependent_template_arguments_p
6456 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6457 return CONST_CAST_TREE (t);
6458
6459 if (transparent_typedefs)
6460 {
6461 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6462 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6463 }
6464
6465 return NULL_TREE;
6466 }
6467
6468 /* Return the number of innermost template parameters in TMPL. */
6469
6470 static int
6471 num_innermost_template_parms (const_tree tmpl)
6472 {
6473 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6474 return TREE_VEC_LENGTH (parms);
6475 }
6476
6477 /* Return either TMPL or another template that it is equivalent to under DR
6478 1286: An alias that just changes the name of a template is equivalent to
6479 the other template. */
6480
6481 static tree
6482 get_underlying_template (tree tmpl)
6483 {
6484 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6485 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6486 {
6487 /* Determine if the alias is equivalent to an underlying template. */
6488 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6489 /* The underlying type may have been ill-formed. Don't proceed. */
6490 if (!orig_type)
6491 break;
6492 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6493 if (!tinfo)
6494 break;
6495
6496 tree underlying = TI_TEMPLATE (tinfo);
6497 if (!PRIMARY_TEMPLATE_P (underlying)
6498 || (num_innermost_template_parms (tmpl)
6499 != num_innermost_template_parms (underlying)))
6500 break;
6501
6502 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6503 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6504 break;
6505
6506 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6507 it's appropriate to treat a less-constrained alias as equivalent. */
6508 if (!at_least_as_constrained (underlying, tmpl))
6509 break;
6510
6511 /* Alias is equivalent. Strip it and repeat. */
6512 tmpl = underlying;
6513 }
6514
6515 return tmpl;
6516 }
6517
6518 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6519 must be a reference-to-function or a pointer-to-function type, as specified
6520 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6521 and check that the resulting function has external linkage. */
6522
6523 static tree
6524 convert_nontype_argument_function (tree type, tree expr,
6525 tsubst_flags_t complain)
6526 {
6527 tree fns = expr;
6528 tree fn, fn_no_ptr;
6529 linkage_kind linkage;
6530
6531 fn = instantiate_type (type, fns, tf_none);
6532 if (fn == error_mark_node)
6533 return error_mark_node;
6534
6535 if (value_dependent_expression_p (fn))
6536 goto accept;
6537
6538 fn_no_ptr = strip_fnptr_conv (fn);
6539 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6540 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6541 if (BASELINK_P (fn_no_ptr))
6542 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6543
6544 /* [temp.arg.nontype]/1
6545
6546 A template-argument for a non-type, non-template template-parameter
6547 shall be one of:
6548 [...]
6549 -- the address of an object or function with external [C++11: or
6550 internal] linkage. */
6551
6552 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6553 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6554 {
6555 if (complain & tf_error)
6556 {
6557 location_t loc = cp_expr_loc_or_input_loc (expr);
6558 error_at (loc, "%qE is not a valid template argument for type %qT",
6559 expr, type);
6560 if (TYPE_PTR_P (type))
6561 inform (loc, "it must be the address of a function "
6562 "with external linkage");
6563 else
6564 inform (loc, "it must be the name of a function with "
6565 "external linkage");
6566 }
6567 return NULL_TREE;
6568 }
6569
6570 linkage = decl_linkage (fn_no_ptr);
6571 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6572 {
6573 if (complain & tf_error)
6574 {
6575 location_t loc = cp_expr_loc_or_input_loc (expr);
6576 if (cxx_dialect >= cxx11)
6577 error_at (loc, "%qE is not a valid template argument for type "
6578 "%qT because %qD has no linkage",
6579 expr, type, fn_no_ptr);
6580 else
6581 error_at (loc, "%qE is not a valid template argument for type "
6582 "%qT because %qD does not have external linkage",
6583 expr, type, fn_no_ptr);
6584 }
6585 return NULL_TREE;
6586 }
6587
6588 accept:
6589 if (TYPE_REF_P (type))
6590 {
6591 if (REFERENCE_REF_P (fn))
6592 fn = TREE_OPERAND (fn, 0);
6593 else
6594 fn = build_address (fn);
6595 }
6596 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6597 fn = build_nop (type, fn);
6598
6599 return fn;
6600 }
6601
6602 /* Subroutine of convert_nontype_argument.
6603 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6604 Emit an error otherwise. */
6605
6606 static bool
6607 check_valid_ptrmem_cst_expr (tree type, tree expr,
6608 tsubst_flags_t complain)
6609 {
6610 tree orig_expr = expr;
6611 STRIP_NOPS (expr);
6612 if (null_ptr_cst_p (expr))
6613 return true;
6614 if (TREE_CODE (expr) == PTRMEM_CST
6615 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6616 PTRMEM_CST_CLASS (expr)))
6617 return true;
6618 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6619 return true;
6620 if (processing_template_decl
6621 && TREE_CODE (expr) == ADDR_EXPR
6622 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6623 return true;
6624 if (complain & tf_error)
6625 {
6626 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6627 error_at (loc, "%qE is not a valid template argument for type %qT",
6628 orig_expr, type);
6629 if (TREE_CODE (expr) != PTRMEM_CST)
6630 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6631 else
6632 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6633 }
6634 return false;
6635 }
6636
6637 /* Returns TRUE iff the address of OP is value-dependent.
6638
6639 14.6.2.4 [temp.dep.temp]:
6640 A non-integral non-type template-argument is dependent if its type is
6641 dependent or it has either of the following forms
6642 qualified-id
6643 & qualified-id
6644 and contains a nested-name-specifier which specifies a class-name that
6645 names a dependent type.
6646
6647 We generalize this to just say that the address of a member of a
6648 dependent class is value-dependent; the above doesn't cover the
6649 address of a static data member named with an unqualified-id. */
6650
6651 static bool
6652 has_value_dependent_address (tree op)
6653 {
6654 STRIP_ANY_LOCATION_WRAPPER (op);
6655
6656 /* We could use get_inner_reference here, but there's no need;
6657 this is only relevant for template non-type arguments, which
6658 can only be expressed as &id-expression. */
6659 if (DECL_P (op))
6660 {
6661 tree ctx = CP_DECL_CONTEXT (op);
6662 if (TYPE_P (ctx) && dependent_type_p (ctx))
6663 return true;
6664 }
6665
6666 return false;
6667 }
6668
6669 /* The next set of functions are used for providing helpful explanatory
6670 diagnostics for failed overload resolution. Their messages should be
6671 indented by two spaces for consistency with the messages in
6672 call.c */
6673
6674 static int
6675 unify_success (bool /*explain_p*/)
6676 {
6677 return 0;
6678 }
6679
6680 /* Other failure functions should call this one, to provide a single function
6681 for setting a breakpoint on. */
6682
6683 static int
6684 unify_invalid (bool /*explain_p*/)
6685 {
6686 return 1;
6687 }
6688
6689 static int
6690 unify_parameter_deduction_failure (bool explain_p, tree parm)
6691 {
6692 if (explain_p)
6693 inform (input_location,
6694 " couldn%'t deduce template parameter %qD", parm);
6695 return unify_invalid (explain_p);
6696 }
6697
6698 static int
6699 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6700 {
6701 if (explain_p)
6702 inform (input_location,
6703 " types %qT and %qT have incompatible cv-qualifiers",
6704 parm, arg);
6705 return unify_invalid (explain_p);
6706 }
6707
6708 static int
6709 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6710 {
6711 if (explain_p)
6712 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6713 return unify_invalid (explain_p);
6714 }
6715
6716 static int
6717 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6718 {
6719 if (explain_p)
6720 inform (input_location,
6721 " template parameter %qD is not a parameter pack, but "
6722 "argument %qD is",
6723 parm, arg);
6724 return unify_invalid (explain_p);
6725 }
6726
6727 static int
6728 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6729 {
6730 if (explain_p)
6731 inform (input_location,
6732 " template argument %qE does not match "
6733 "pointer-to-member constant %qE",
6734 arg, parm);
6735 return unify_invalid (explain_p);
6736 }
6737
6738 static int
6739 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6740 {
6741 if (explain_p)
6742 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6743 return unify_invalid (explain_p);
6744 }
6745
6746 static int
6747 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6748 {
6749 if (explain_p)
6750 inform (input_location,
6751 " inconsistent parameter pack deduction with %qT and %qT",
6752 old_arg, new_arg);
6753 return unify_invalid (explain_p);
6754 }
6755
6756 static int
6757 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6758 {
6759 if (explain_p)
6760 {
6761 if (TYPE_P (parm))
6762 inform (input_location,
6763 " deduced conflicting types for parameter %qT (%qT and %qT)",
6764 parm, first, second);
6765 else
6766 inform (input_location,
6767 " deduced conflicting values for non-type parameter "
6768 "%qE (%qE and %qE)", parm, first, second);
6769 }
6770 return unify_invalid (explain_p);
6771 }
6772
6773 static int
6774 unify_vla_arg (bool explain_p, tree arg)
6775 {
6776 if (explain_p)
6777 inform (input_location,
6778 " variable-sized array type %qT is not "
6779 "a valid template argument",
6780 arg);
6781 return unify_invalid (explain_p);
6782 }
6783
6784 static int
6785 unify_method_type_error (bool explain_p, tree arg)
6786 {
6787 if (explain_p)
6788 inform (input_location,
6789 " member function type %qT is not a valid template argument",
6790 arg);
6791 return unify_invalid (explain_p);
6792 }
6793
6794 static int
6795 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6796 {
6797 if (explain_p)
6798 {
6799 if (least_p)
6800 inform_n (input_location, wanted,
6801 " candidate expects at least %d argument, %d provided",
6802 " candidate expects at least %d arguments, %d provided",
6803 wanted, have);
6804 else
6805 inform_n (input_location, wanted,
6806 " candidate expects %d argument, %d provided",
6807 " candidate expects %d arguments, %d provided",
6808 wanted, have);
6809 }
6810 return unify_invalid (explain_p);
6811 }
6812
6813 static int
6814 unify_too_many_arguments (bool explain_p, int have, int wanted)
6815 {
6816 return unify_arity (explain_p, have, wanted);
6817 }
6818
6819 static int
6820 unify_too_few_arguments (bool explain_p, int have, int wanted,
6821 bool least_p = false)
6822 {
6823 return unify_arity (explain_p, have, wanted, least_p);
6824 }
6825
6826 static int
6827 unify_arg_conversion (bool explain_p, tree to_type,
6828 tree from_type, tree arg)
6829 {
6830 if (explain_p)
6831 inform (cp_expr_loc_or_input_loc (arg),
6832 " cannot convert %qE (type %qT) to type %qT",
6833 arg, from_type, to_type);
6834 return unify_invalid (explain_p);
6835 }
6836
6837 static int
6838 unify_no_common_base (bool explain_p, enum template_base_result r,
6839 tree parm, tree arg)
6840 {
6841 if (explain_p)
6842 switch (r)
6843 {
6844 case tbr_ambiguous_baseclass:
6845 inform (input_location, " %qT is an ambiguous base class of %qT",
6846 parm, arg);
6847 break;
6848 default:
6849 inform (input_location, " %qT is not derived from %qT", arg, parm);
6850 break;
6851 }
6852 return unify_invalid (explain_p);
6853 }
6854
6855 static int
6856 unify_inconsistent_template_template_parameters (bool explain_p)
6857 {
6858 if (explain_p)
6859 inform (input_location,
6860 " template parameters of a template template argument are "
6861 "inconsistent with other deduced template arguments");
6862 return unify_invalid (explain_p);
6863 }
6864
6865 static int
6866 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6867 {
6868 if (explain_p)
6869 inform (input_location,
6870 " cannot deduce a template for %qT from non-template type %qT",
6871 parm, arg);
6872 return unify_invalid (explain_p);
6873 }
6874
6875 static int
6876 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6877 {
6878 if (explain_p)
6879 inform (input_location,
6880 " template argument %qE does not match %qE", arg, parm);
6881 return unify_invalid (explain_p);
6882 }
6883
6884 /* True if T is a C++20 template parameter object to store the argument for a
6885 template parameter of class type. */
6886
6887 bool
6888 template_parm_object_p (const_tree t)
6889 {
6890 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6891 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6892 }
6893
6894 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6895 argument for TYPE, points to an unsuitable object. */
6896
6897 static bool
6898 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6899 {
6900 switch (TREE_CODE (expr))
6901 {
6902 CASE_CONVERT:
6903 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6904 complain);
6905
6906 case TARGET_EXPR:
6907 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6908 complain);
6909
6910 case CONSTRUCTOR:
6911 {
6912 unsigned i; tree elt;
6913 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6914 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6915 return true;
6916 }
6917 break;
6918
6919 case ADDR_EXPR:
6920 {
6921 tree decl = TREE_OPERAND (expr, 0);
6922
6923 if (!VAR_P (decl))
6924 {
6925 if (complain & tf_error)
6926 error_at (cp_expr_loc_or_input_loc (expr),
6927 "%qE is not a valid template argument of type %qT "
6928 "because %qE is not a variable", expr, type, decl);
6929 return true;
6930 }
6931 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6932 {
6933 if (complain & tf_error)
6934 error_at (cp_expr_loc_or_input_loc (expr),
6935 "%qE is not a valid template argument of type %qT "
6936 "in C++98 because %qD does not have external linkage",
6937 expr, type, decl);
6938 return true;
6939 }
6940 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6941 && decl_linkage (decl) == lk_none)
6942 {
6943 if (complain & tf_error)
6944 error_at (cp_expr_loc_or_input_loc (expr),
6945 "%qE is not a valid template argument of type %qT "
6946 "because %qD has no linkage", expr, type, decl);
6947 return true;
6948 }
6949 /* C++17: For a non-type template-parameter of reference or pointer
6950 type, the value of the constant expression shall not refer to (or
6951 for a pointer type, shall not be the address of):
6952 * a subobject (4.5),
6953 * a temporary object (15.2),
6954 * a string literal (5.13.5),
6955 * the result of a typeid expression (8.2.8), or
6956 * a predefined __func__ variable (11.4.1). */
6957 else if (DECL_ARTIFICIAL (decl))
6958 {
6959 if (complain & tf_error)
6960 error ("the address of %qD is not a valid template argument",
6961 decl);
6962 return true;
6963 }
6964 else if (!same_type_ignoring_top_level_qualifiers_p
6965 (strip_array_types (TREE_TYPE (type)),
6966 strip_array_types (TREE_TYPE (decl))))
6967 {
6968 if (complain & tf_error)
6969 error ("the address of the %qT subobject of %qD is not a "
6970 "valid template argument", TREE_TYPE (type), decl);
6971 return true;
6972 }
6973 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6974 {
6975 if (complain & tf_error)
6976 error ("the address of %qD is not a valid template argument "
6977 "because it does not have static storage duration",
6978 decl);
6979 return true;
6980 }
6981 }
6982 break;
6983
6984 default:
6985 if (!INDIRECT_TYPE_P (type))
6986 /* We're only concerned about pointers and references here. */;
6987 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6988 /* Null pointer values are OK in C++11. */;
6989 else
6990 {
6991 if (VAR_P (expr))
6992 {
6993 if (complain & tf_error)
6994 error ("%qD is not a valid template argument "
6995 "because %qD is a variable, not the address of "
6996 "a variable", expr, expr);
6997 return true;
6998 }
6999 else
7000 {
7001 if (complain & tf_error)
7002 error ("%qE is not a valid template argument for %qT "
7003 "because it is not the address of a variable",
7004 expr, type);
7005 return true;
7006 }
7007 }
7008 }
7009 return false;
7010
7011 }
7012
7013 /* The template arguments corresponding to template parameter objects of types
7014 that contain pointers to members. */
7015
7016 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7017
7018 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7019 template argument EXPR. */
7020
7021 static tree
7022 get_template_parm_object (tree expr, tsubst_flags_t complain)
7023 {
7024 if (TREE_CODE (expr) == TARGET_EXPR)
7025 expr = TARGET_EXPR_INITIAL (expr);
7026
7027 if (!TREE_CONSTANT (expr))
7028 {
7029 if ((complain & tf_error)
7030 && require_rvalue_constant_expression (expr))
7031 cxx_constant_value (expr);
7032 return error_mark_node;
7033 }
7034 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7035 return error_mark_node;
7036
7037 tree name = mangle_template_parm_object (expr);
7038 tree decl = get_global_binding (name);
7039 if (decl)
7040 return decl;
7041
7042 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7043 decl = create_temporary_var (type);
7044 TREE_STATIC (decl) = true;
7045 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7046 TREE_READONLY (decl) = true;
7047 DECL_NAME (decl) = name;
7048 SET_DECL_ASSEMBLER_NAME (decl, name);
7049 DECL_CONTEXT (decl) = global_namespace;
7050 comdat_linkage (decl);
7051
7052 if (!zero_init_p (type))
7053 {
7054 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7055 lower_var_init before we're done mangling. So store the original
7056 value elsewhere. */
7057 tree copy = unshare_constructor (expr);
7058 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7059 }
7060
7061 pushdecl_top_level_and_finish (decl, expr);
7062
7063 return decl;
7064 }
7065
7066 /* Return the actual template argument corresponding to template parameter
7067 object VAR. */
7068
7069 tree
7070 tparm_object_argument (tree var)
7071 {
7072 if (zero_init_p (TREE_TYPE (var)))
7073 return DECL_INITIAL (var);
7074 return *(tparm_obj_values->get (var));
7075 }
7076
7077 /* Attempt to convert the non-type template parameter EXPR to the
7078 indicated TYPE. If the conversion is successful, return the
7079 converted value. If the conversion is unsuccessful, return
7080 NULL_TREE if we issued an error message, or error_mark_node if we
7081 did not. We issue error messages for out-and-out bad template
7082 parameters, but not simply because the conversion failed, since we
7083 might be just trying to do argument deduction. Both TYPE and EXPR
7084 must be non-dependent.
7085
7086 The conversion follows the special rules described in
7087 [temp.arg.nontype], and it is much more strict than an implicit
7088 conversion.
7089
7090 This function is called twice for each template argument (see
7091 lookup_template_class for a more accurate description of this
7092 problem). This means that we need to handle expressions which
7093 are not valid in a C++ source, but can be created from the
7094 first call (for instance, casts to perform conversions). These
7095 hacks can go away after we fix the double coercion problem. */
7096
7097 static tree
7098 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7099 {
7100 tree expr_type;
7101 location_t loc = cp_expr_loc_or_input_loc (expr);
7102
7103 /* Detect immediately string literals as invalid non-type argument.
7104 This special-case is not needed for correctness (we would easily
7105 catch this later), but only to provide better diagnostic for this
7106 common user mistake. As suggested by DR 100, we do not mention
7107 linkage issues in the diagnostic as this is not the point. */
7108 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7109 {
7110 if (complain & tf_error)
7111 error ("%qE is not a valid template argument for type %qT "
7112 "because string literals can never be used in this context",
7113 expr, type);
7114 return NULL_TREE;
7115 }
7116
7117 /* Add the ADDR_EXPR now for the benefit of
7118 value_dependent_expression_p. */
7119 if (TYPE_PTROBV_P (type)
7120 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7121 {
7122 expr = decay_conversion (expr, complain);
7123 if (expr == error_mark_node)
7124 return error_mark_node;
7125 }
7126
7127 /* If we are in a template, EXPR may be non-dependent, but still
7128 have a syntactic, rather than semantic, form. For example, EXPR
7129 might be a SCOPE_REF, rather than the VAR_DECL to which the
7130 SCOPE_REF refers. Preserving the qualifying scope is necessary
7131 so that access checking can be performed when the template is
7132 instantiated -- but here we need the resolved form so that we can
7133 convert the argument. */
7134 bool non_dep = false;
7135 if (TYPE_REF_OBJ_P (type)
7136 && has_value_dependent_address (expr))
7137 /* If we want the address and it's value-dependent, don't fold. */;
7138 else if (processing_template_decl
7139 && is_nondependent_constant_expression (expr))
7140 non_dep = true;
7141 if (error_operand_p (expr))
7142 return error_mark_node;
7143 expr_type = TREE_TYPE (expr);
7144
7145 /* If the argument is non-dependent, perform any conversions in
7146 non-dependent context as well. */
7147 processing_template_decl_sentinel s (non_dep);
7148 if (non_dep)
7149 expr = instantiate_non_dependent_expr_internal (expr, complain);
7150
7151 const bool val_dep_p = value_dependent_expression_p (expr);
7152 if (val_dep_p)
7153 expr = canonicalize_expr_argument (expr, complain);
7154
7155 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7156 to a non-type argument of "nullptr". */
7157 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7158 expr = fold_simple (convert (type, expr));
7159
7160 /* In C++11, integral or enumeration non-type template arguments can be
7161 arbitrary constant expressions. Pointer and pointer to
7162 member arguments can be general constant expressions that evaluate
7163 to a null value, but otherwise still need to be of a specific form. */
7164 if (cxx_dialect >= cxx11)
7165 {
7166 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7167 /* A PTRMEM_CST is already constant, and a valid template
7168 argument for a parameter of pointer to member type, we just want
7169 to leave it in that form rather than lower it to a
7170 CONSTRUCTOR. */;
7171 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7172 || cxx_dialect >= cxx17)
7173 {
7174 /* C++17: A template-argument for a non-type template-parameter shall
7175 be a converted constant expression (8.20) of the type of the
7176 template-parameter. */
7177 expr = build_converted_constant_expr (type, expr, complain);
7178 if (expr == error_mark_node)
7179 /* Make sure we return NULL_TREE only if we have really issued
7180 an error, as described above. */
7181 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7182 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7183 {
7184 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7185 return expr;
7186 }
7187 expr = maybe_constant_value (expr, NULL_TREE,
7188 /*manifestly_const_eval=*/true);
7189 expr = convert_from_reference (expr);
7190 }
7191 else if (TYPE_PTR_OR_PTRMEM_P (type))
7192 {
7193 tree folded = maybe_constant_value (expr, NULL_TREE,
7194 /*manifestly_const_eval=*/true);
7195 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7196 : null_member_pointer_value_p (folded))
7197 expr = folded;
7198 }
7199 }
7200
7201 if (TYPE_REF_P (type))
7202 expr = mark_lvalue_use (expr);
7203 else
7204 expr = mark_rvalue_use (expr);
7205
7206 /* HACK: Due to double coercion, we can get a
7207 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7208 which is the tree that we built on the first call (see
7209 below when coercing to reference to object or to reference to
7210 function). We just strip everything and get to the arg.
7211 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7212 for examples. */
7213 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7214 {
7215 tree probe_type, probe = expr;
7216 if (REFERENCE_REF_P (probe))
7217 probe = TREE_OPERAND (probe, 0);
7218 probe_type = TREE_TYPE (probe);
7219 if (TREE_CODE (probe) == NOP_EXPR)
7220 {
7221 /* ??? Maybe we could use convert_from_reference here, but we
7222 would need to relax its constraints because the NOP_EXPR
7223 could actually change the type to something more cv-qualified,
7224 and this is not folded by convert_from_reference. */
7225 tree addr = TREE_OPERAND (probe, 0);
7226 if (TYPE_REF_P (probe_type)
7227 && TREE_CODE (addr) == ADDR_EXPR
7228 && TYPE_PTR_P (TREE_TYPE (addr))
7229 && (same_type_ignoring_top_level_qualifiers_p
7230 (TREE_TYPE (probe_type),
7231 TREE_TYPE (TREE_TYPE (addr)))))
7232 {
7233 expr = TREE_OPERAND (addr, 0);
7234 expr_type = TREE_TYPE (probe_type);
7235 }
7236 }
7237 }
7238
7239 /* [temp.arg.nontype]/5, bullet 1
7240
7241 For a non-type template-parameter of integral or enumeration type,
7242 integral promotions (_conv.prom_) and integral conversions
7243 (_conv.integral_) are applied. */
7244 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7245 {
7246 if (cxx_dialect < cxx11)
7247 {
7248 tree t = build_converted_constant_expr (type, expr, complain);
7249 t = maybe_constant_value (t);
7250 if (t != error_mark_node)
7251 expr = t;
7252 }
7253
7254 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7255 return error_mark_node;
7256
7257 /* Notice that there are constant expressions like '4 % 0' which
7258 do not fold into integer constants. */
7259 if (TREE_CODE (expr) != INTEGER_CST && !val_dep_p)
7260 {
7261 if (complain & tf_error)
7262 {
7263 int errs = errorcount, warns = warningcount + werrorcount;
7264 if (!require_potential_constant_expression (expr))
7265 expr = error_mark_node;
7266 else
7267 expr = cxx_constant_value (expr);
7268 if (errorcount > errs || warningcount + werrorcount > warns)
7269 inform (loc, "in template argument for type %qT", type);
7270 if (expr == error_mark_node)
7271 return NULL_TREE;
7272 /* else cxx_constant_value complained but gave us
7273 a real constant, so go ahead. */
7274 if (TREE_CODE (expr) != INTEGER_CST)
7275 {
7276 /* Some assemble time constant expressions like
7277 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7278 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7279 as we can emit them into .rodata initializers of
7280 variables, yet they can't fold into an INTEGER_CST at
7281 compile time. Refuse them here. */
7282 gcc_checking_assert (reduced_constant_expression_p (expr));
7283 error_at (loc, "template argument %qE for type %qT not "
7284 "a constant integer", expr, type);
7285 return NULL_TREE;
7286 }
7287 }
7288 else
7289 return NULL_TREE;
7290 }
7291
7292 /* Avoid typedef problems. */
7293 if (TREE_TYPE (expr) != type)
7294 expr = fold_convert (type, expr);
7295 }
7296 /* [temp.arg.nontype]/5, bullet 2
7297
7298 For a non-type template-parameter of type pointer to object,
7299 qualification conversions (_conv.qual_) and the array-to-pointer
7300 conversion (_conv.array_) are applied. */
7301 else if (TYPE_PTROBV_P (type))
7302 {
7303 tree decayed = expr;
7304
7305 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7306 decay_conversion or an explicit cast. If it's a problematic cast,
7307 we'll complain about it below. */
7308 if (TREE_CODE (expr) == NOP_EXPR)
7309 {
7310 tree probe = expr;
7311 STRIP_NOPS (probe);
7312 if (TREE_CODE (probe) == ADDR_EXPR
7313 && TYPE_PTR_P (TREE_TYPE (probe)))
7314 {
7315 expr = probe;
7316 expr_type = TREE_TYPE (expr);
7317 }
7318 }
7319
7320 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7321
7322 A template-argument for a non-type, non-template template-parameter
7323 shall be one of: [...]
7324
7325 -- the name of a non-type template-parameter;
7326 -- the address of an object or function with external linkage, [...]
7327 expressed as "& id-expression" where the & is optional if the name
7328 refers to a function or array, or if the corresponding
7329 template-parameter is a reference.
7330
7331 Here, we do not care about functions, as they are invalid anyway
7332 for a parameter of type pointer-to-object. */
7333
7334 if (val_dep_p)
7335 /* Non-type template parameters are OK. */
7336 ;
7337 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7338 /* Null pointer values are OK in C++11. */;
7339 else if (TREE_CODE (expr) != ADDR_EXPR
7340 && !INDIRECT_TYPE_P (expr_type))
7341 /* Other values, like integer constants, might be valid
7342 non-type arguments of some other type. */
7343 return error_mark_node;
7344 else if (invalid_tparm_referent_p (type, expr, complain))
7345 return NULL_TREE;
7346
7347 expr = decayed;
7348
7349 expr = perform_qualification_conversions (type, expr);
7350 if (expr == error_mark_node)
7351 return error_mark_node;
7352 }
7353 /* [temp.arg.nontype]/5, bullet 3
7354
7355 For a non-type template-parameter of type reference to object, no
7356 conversions apply. The type referred to by the reference may be more
7357 cv-qualified than the (otherwise identical) type of the
7358 template-argument. The template-parameter is bound directly to the
7359 template-argument, which must be an lvalue. */
7360 else if (TYPE_REF_OBJ_P (type))
7361 {
7362 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7363 expr_type))
7364 return error_mark_node;
7365
7366 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7367 {
7368 if (complain & tf_error)
7369 error ("%qE is not a valid template argument for type %qT "
7370 "because of conflicts in cv-qualification", expr, type);
7371 return NULL_TREE;
7372 }
7373
7374 if (!lvalue_p (expr))
7375 {
7376 if (complain & tf_error)
7377 error ("%qE is not a valid template argument for type %qT "
7378 "because it is not an lvalue", expr, type);
7379 return NULL_TREE;
7380 }
7381
7382 /* [temp.arg.nontype]/1
7383
7384 A template-argument for a non-type, non-template template-parameter
7385 shall be one of: [...]
7386
7387 -- the address of an object or function with external linkage. */
7388 if (INDIRECT_REF_P (expr)
7389 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7390 {
7391 expr = TREE_OPERAND (expr, 0);
7392 if (DECL_P (expr))
7393 {
7394 if (complain & tf_error)
7395 error ("%q#D is not a valid template argument for type %qT "
7396 "because a reference variable does not have a constant "
7397 "address", expr, type);
7398 return NULL_TREE;
7399 }
7400 }
7401
7402 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7403 /* OK, dependent reference. We don't want to ask whether a DECL is
7404 itself value-dependent, since what we want here is its address. */;
7405 else
7406 {
7407 expr = build_address (expr);
7408
7409 if (invalid_tparm_referent_p (type, expr, complain))
7410 return NULL_TREE;
7411 }
7412
7413 if (!same_type_p (type, TREE_TYPE (expr)))
7414 expr = build_nop (type, expr);
7415 }
7416 /* [temp.arg.nontype]/5, bullet 4
7417
7418 For a non-type template-parameter of type pointer to function, only
7419 the function-to-pointer conversion (_conv.func_) is applied. If the
7420 template-argument represents a set of overloaded functions (or a
7421 pointer to such), the matching function is selected from the set
7422 (_over.over_). */
7423 else if (TYPE_PTRFN_P (type))
7424 {
7425 /* If the argument is a template-id, we might not have enough
7426 context information to decay the pointer. */
7427 if (!type_unknown_p (expr_type))
7428 {
7429 expr = decay_conversion (expr, complain);
7430 if (expr == error_mark_node)
7431 return error_mark_node;
7432 }
7433
7434 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7435 /* Null pointer values are OK in C++11. */
7436 return perform_qualification_conversions (type, expr);
7437
7438 expr = convert_nontype_argument_function (type, expr, complain);
7439 if (!expr || expr == error_mark_node)
7440 return expr;
7441 }
7442 /* [temp.arg.nontype]/5, bullet 5
7443
7444 For a non-type template-parameter of type reference to function, no
7445 conversions apply. If the template-argument represents a set of
7446 overloaded functions, the matching function is selected from the set
7447 (_over.over_). */
7448 else if (TYPE_REFFN_P (type))
7449 {
7450 if (TREE_CODE (expr) == ADDR_EXPR)
7451 {
7452 if (complain & tf_error)
7453 {
7454 error ("%qE is not a valid template argument for type %qT "
7455 "because it is a pointer", expr, type);
7456 inform (input_location, "try using %qE instead",
7457 TREE_OPERAND (expr, 0));
7458 }
7459 return NULL_TREE;
7460 }
7461
7462 expr = convert_nontype_argument_function (type, expr, complain);
7463 if (!expr || expr == error_mark_node)
7464 return expr;
7465 }
7466 /* [temp.arg.nontype]/5, bullet 6
7467
7468 For a non-type template-parameter of type pointer to member function,
7469 no conversions apply. If the template-argument represents a set of
7470 overloaded member functions, the matching member function is selected
7471 from the set (_over.over_). */
7472 else if (TYPE_PTRMEMFUNC_P (type))
7473 {
7474 expr = instantiate_type (type, expr, tf_none);
7475 if (expr == error_mark_node)
7476 return error_mark_node;
7477
7478 /* [temp.arg.nontype] bullet 1 says the pointer to member
7479 expression must be a pointer-to-member constant. */
7480 if (!val_dep_p
7481 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7482 return NULL_TREE;
7483
7484 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7485 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7486 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7487 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7488 }
7489 /* [temp.arg.nontype]/5, bullet 7
7490
7491 For a non-type template-parameter of type pointer to data member,
7492 qualification conversions (_conv.qual_) are applied. */
7493 else if (TYPE_PTRDATAMEM_P (type))
7494 {
7495 /* [temp.arg.nontype] bullet 1 says the pointer to member
7496 expression must be a pointer-to-member constant. */
7497 if (!val_dep_p
7498 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7499 return NULL_TREE;
7500
7501 expr = perform_qualification_conversions (type, expr);
7502 if (expr == error_mark_node)
7503 return expr;
7504 }
7505 else if (NULLPTR_TYPE_P (type))
7506 {
7507 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7508 {
7509 if (complain & tf_error)
7510 error ("%qE is not a valid template argument for type %qT "
7511 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7512 return NULL_TREE;
7513 }
7514 return expr;
7515 }
7516 else if (CLASS_TYPE_P (type))
7517 {
7518 /* Replace the argument with a reference to the corresponding template
7519 parameter object. */
7520 if (!val_dep_p)
7521 expr = get_template_parm_object (expr, complain);
7522 if (expr == error_mark_node)
7523 return NULL_TREE;
7524 }
7525 /* A template non-type parameter must be one of the above. */
7526 else
7527 gcc_unreachable ();
7528
7529 /* Sanity check: did we actually convert the argument to the
7530 right type? */
7531 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7532 (type, TREE_TYPE (expr)));
7533 return convert_from_reference (expr);
7534 }
7535
7536 /* Subroutine of coerce_template_template_parms, which returns 1 if
7537 PARM_PARM and ARG_PARM match using the rule for the template
7538 parameters of template template parameters. Both PARM and ARG are
7539 template parameters; the rest of the arguments are the same as for
7540 coerce_template_template_parms.
7541 */
7542 static int
7543 coerce_template_template_parm (tree parm,
7544 tree arg,
7545 tsubst_flags_t complain,
7546 tree in_decl,
7547 tree outer_args)
7548 {
7549 if (arg == NULL_TREE || error_operand_p (arg)
7550 || parm == NULL_TREE || error_operand_p (parm))
7551 return 0;
7552
7553 if (TREE_CODE (arg) != TREE_CODE (parm))
7554 return 0;
7555
7556 switch (TREE_CODE (parm))
7557 {
7558 case TEMPLATE_DECL:
7559 /* We encounter instantiations of templates like
7560 template <template <template <class> class> class TT>
7561 class C; */
7562 {
7563 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7564 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7565
7566 if (!coerce_template_template_parms
7567 (parmparm, argparm, complain, in_decl, outer_args))
7568 return 0;
7569 }
7570 /* Fall through. */
7571
7572 case TYPE_DECL:
7573 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7574 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7575 /* Argument is a parameter pack but parameter is not. */
7576 return 0;
7577 break;
7578
7579 case PARM_DECL:
7580 /* The tsubst call is used to handle cases such as
7581
7582 template <int> class C {};
7583 template <class T, template <T> class TT> class D {};
7584 D<int, C> d;
7585
7586 i.e. the parameter list of TT depends on earlier parameters. */
7587 if (!uses_template_parms (TREE_TYPE (arg)))
7588 {
7589 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7590 if (!uses_template_parms (t)
7591 && !same_type_p (t, TREE_TYPE (arg)))
7592 return 0;
7593 }
7594
7595 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7596 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7597 /* Argument is a parameter pack but parameter is not. */
7598 return 0;
7599
7600 break;
7601
7602 default:
7603 gcc_unreachable ();
7604 }
7605
7606 return 1;
7607 }
7608
7609 /* Coerce template argument list ARGLIST for use with template
7610 template-parameter TEMPL. */
7611
7612 static tree
7613 coerce_template_args_for_ttp (tree templ, tree arglist,
7614 tsubst_flags_t complain)
7615 {
7616 /* Consider an example where a template template parameter declared as
7617
7618 template <class T, class U = std::allocator<T> > class TT
7619
7620 The template parameter level of T and U are one level larger than
7621 of TT. To proper process the default argument of U, say when an
7622 instantiation `TT<int>' is seen, we need to build the full
7623 arguments containing {int} as the innermost level. Outer levels,
7624 available when not appearing as default template argument, can be
7625 obtained from the arguments of the enclosing template.
7626
7627 Suppose that TT is later substituted with std::vector. The above
7628 instantiation is `TT<int, std::allocator<T> >' with TT at
7629 level 1, and T at level 2, while the template arguments at level 1
7630 becomes {std::vector} and the inner level 2 is {int}. */
7631
7632 tree outer = DECL_CONTEXT (templ);
7633 if (outer)
7634 outer = generic_targs_for (outer);
7635 else if (current_template_parms)
7636 {
7637 /* This is an argument of the current template, so we haven't set
7638 DECL_CONTEXT yet. */
7639 tree relevant_template_parms;
7640
7641 /* Parameter levels that are greater than the level of the given
7642 template template parm are irrelevant. */
7643 relevant_template_parms = current_template_parms;
7644 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7645 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7646 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7647
7648 outer = template_parms_to_args (relevant_template_parms);
7649 }
7650
7651 if (outer)
7652 arglist = add_to_template_args (outer, arglist);
7653
7654 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7655 return coerce_template_parms (parmlist, arglist, templ,
7656 complain,
7657 /*require_all_args=*/true,
7658 /*use_default_args=*/true);
7659 }
7660
7661 /* A cache of template template parameters with match-all default
7662 arguments. */
7663 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7664
7665 /* T is a bound template template-parameter. Copy its arguments into default
7666 arguments of the template template-parameter's template parameters. */
7667
7668 static tree
7669 add_defaults_to_ttp (tree otmpl)
7670 {
7671 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7672 return *c;
7673
7674 tree ntmpl = copy_node (otmpl);
7675
7676 tree ntype = copy_node (TREE_TYPE (otmpl));
7677 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7678 TYPE_MAIN_VARIANT (ntype) = ntype;
7679 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7680 TYPE_NAME (ntype) = ntmpl;
7681 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7682
7683 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7684 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7685 TEMPLATE_PARM_DECL (idx) = ntmpl;
7686 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7687
7688 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7689 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7690 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7691 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7692 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7693 {
7694 tree o = TREE_VEC_ELT (vec, i);
7695 if (!template_parameter_pack_p (TREE_VALUE (o)))
7696 {
7697 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7698 TREE_PURPOSE (n) = any_targ_node;
7699 }
7700 }
7701
7702 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7703 return ntmpl;
7704 }
7705
7706 /* ARG is a bound potential template template-argument, and PARGS is a list
7707 of arguments for the corresponding template template-parameter. Adjust
7708 PARGS as appropriate for application to ARG's template, and if ARG is a
7709 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7710 arguments to the template template parameter. */
7711
7712 static tree
7713 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7714 {
7715 ++processing_template_decl;
7716 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7717 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7718 {
7719 /* When comparing two template template-parameters in partial ordering,
7720 rewrite the one currently being used as an argument to have default
7721 arguments for all parameters. */
7722 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7723 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7724 if (pargs != error_mark_node)
7725 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7726 TYPE_TI_ARGS (arg));
7727 }
7728 else
7729 {
7730 tree aparms
7731 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7732 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7733 /*require_all*/true,
7734 /*use_default*/true);
7735 }
7736 --processing_template_decl;
7737 return pargs;
7738 }
7739
7740 /* Subroutine of unify for the case when PARM is a
7741 BOUND_TEMPLATE_TEMPLATE_PARM. */
7742
7743 static int
7744 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7745 bool explain_p)
7746 {
7747 tree parmvec = TYPE_TI_ARGS (parm);
7748 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7749
7750 /* The template template parm might be variadic and the argument
7751 not, so flatten both argument lists. */
7752 parmvec = expand_template_argument_pack (parmvec);
7753 argvec = expand_template_argument_pack (argvec);
7754
7755 if (flag_new_ttp)
7756 {
7757 /* In keeping with P0522R0, adjust P's template arguments
7758 to apply to A's template; then flatten it again. */
7759 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7760 nparmvec = expand_template_argument_pack (nparmvec);
7761
7762 if (unify (tparms, targs, nparmvec, argvec,
7763 UNIFY_ALLOW_NONE, explain_p))
7764 return 1;
7765
7766 /* If the P0522 adjustment eliminated a pack expansion, deduce
7767 empty packs. */
7768 if (flag_new_ttp
7769 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7770 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7771 DEDUCE_EXACT, /*sub*/true, explain_p))
7772 return 1;
7773 }
7774 else
7775 {
7776 /* Deduce arguments T, i from TT<T> or TT<i>.
7777 We check each element of PARMVEC and ARGVEC individually
7778 rather than the whole TREE_VEC since they can have
7779 different number of elements, which is allowed under N2555. */
7780
7781 int len = TREE_VEC_LENGTH (parmvec);
7782
7783 /* Check if the parameters end in a pack, making them
7784 variadic. */
7785 int parm_variadic_p = 0;
7786 if (len > 0
7787 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7788 parm_variadic_p = 1;
7789
7790 for (int i = 0; i < len - parm_variadic_p; ++i)
7791 /* If the template argument list of P contains a pack
7792 expansion that is not the last template argument, the
7793 entire template argument list is a non-deduced
7794 context. */
7795 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7796 return unify_success (explain_p);
7797
7798 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7799 return unify_too_few_arguments (explain_p,
7800 TREE_VEC_LENGTH (argvec), len);
7801
7802 for (int i = 0; i < len - parm_variadic_p; ++i)
7803 if (unify (tparms, targs,
7804 TREE_VEC_ELT (parmvec, i),
7805 TREE_VEC_ELT (argvec, i),
7806 UNIFY_ALLOW_NONE, explain_p))
7807 return 1;
7808
7809 if (parm_variadic_p
7810 && unify_pack_expansion (tparms, targs,
7811 parmvec, argvec,
7812 DEDUCE_EXACT,
7813 /*subr=*/true, explain_p))
7814 return 1;
7815 }
7816
7817 return 0;
7818 }
7819
7820 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7821 template template parameters. Both PARM_PARMS and ARG_PARMS are
7822 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7823 or PARM_DECL.
7824
7825 Consider the example:
7826 template <class T> class A;
7827 template<template <class U> class TT> class B;
7828
7829 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7830 the parameters to A, and OUTER_ARGS contains A. */
7831
7832 static int
7833 coerce_template_template_parms (tree parm_parms,
7834 tree arg_parms,
7835 tsubst_flags_t complain,
7836 tree in_decl,
7837 tree outer_args)
7838 {
7839 int nparms, nargs, i;
7840 tree parm, arg;
7841 int variadic_p = 0;
7842
7843 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7844 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7845
7846 nparms = TREE_VEC_LENGTH (parm_parms);
7847 nargs = TREE_VEC_LENGTH (arg_parms);
7848
7849 if (flag_new_ttp)
7850 {
7851 /* P0522R0: A template template-parameter P is at least as specialized as
7852 a template template-argument A if, given the following rewrite to two
7853 function templates, the function template corresponding to P is at
7854 least as specialized as the function template corresponding to A
7855 according to the partial ordering rules for function templates
7856 ([temp.func.order]). Given an invented class template X with the
7857 template parameter list of A (including default arguments):
7858
7859 * Each of the two function templates has the same template parameters,
7860 respectively, as P or A.
7861
7862 * Each function template has a single function parameter whose type is
7863 a specialization of X with template arguments corresponding to the
7864 template parameters from the respective function template where, for
7865 each template parameter PP in the template parameter list of the
7866 function template, a corresponding template argument AA is formed. If
7867 PP declares a parameter pack, then AA is the pack expansion
7868 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7869
7870 If the rewrite produces an invalid type, then P is not at least as
7871 specialized as A. */
7872
7873 /* So coerce P's args to apply to A's parms, and then deduce between A's
7874 args and the converted args. If that succeeds, A is at least as
7875 specialized as P, so they match.*/
7876 tree pargs = template_parms_level_to_args (parm_parms);
7877 pargs = add_outermost_template_args (outer_args, pargs);
7878 ++processing_template_decl;
7879 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7880 /*require_all*/true, /*use_default*/true);
7881 --processing_template_decl;
7882 if (pargs != error_mark_node)
7883 {
7884 tree targs = make_tree_vec (nargs);
7885 tree aargs = template_parms_level_to_args (arg_parms);
7886 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7887 /*explain*/false))
7888 return 1;
7889 }
7890 }
7891
7892 /* Determine whether we have a parameter pack at the end of the
7893 template template parameter's template parameter list. */
7894 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7895 {
7896 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7897
7898 if (error_operand_p (parm))
7899 return 0;
7900
7901 switch (TREE_CODE (parm))
7902 {
7903 case TEMPLATE_DECL:
7904 case TYPE_DECL:
7905 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7906 variadic_p = 1;
7907 break;
7908
7909 case PARM_DECL:
7910 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7911 variadic_p = 1;
7912 break;
7913
7914 default:
7915 gcc_unreachable ();
7916 }
7917 }
7918
7919 if (nargs != nparms
7920 && !(variadic_p && nargs >= nparms - 1))
7921 return 0;
7922
7923 /* Check all of the template parameters except the parameter pack at
7924 the end (if any). */
7925 for (i = 0; i < nparms - variadic_p; ++i)
7926 {
7927 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7928 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7929 continue;
7930
7931 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7932 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7933
7934 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7935 outer_args))
7936 return 0;
7937
7938 }
7939
7940 if (variadic_p)
7941 {
7942 /* Check each of the template parameters in the template
7943 argument against the template parameter pack at the end of
7944 the template template parameter. */
7945 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7946 return 0;
7947
7948 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7949
7950 for (; i < nargs; ++i)
7951 {
7952 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7953 continue;
7954
7955 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7956
7957 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7958 outer_args))
7959 return 0;
7960 }
7961 }
7962
7963 return 1;
7964 }
7965
7966 /* Verifies that the deduced template arguments (in TARGS) for the
7967 template template parameters (in TPARMS) represent valid bindings,
7968 by comparing the template parameter list of each template argument
7969 to the template parameter list of its corresponding template
7970 template parameter, in accordance with DR150. This
7971 routine can only be called after all template arguments have been
7972 deduced. It will return TRUE if all of the template template
7973 parameter bindings are okay, FALSE otherwise. */
7974 bool
7975 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7976 {
7977 int i, ntparms = TREE_VEC_LENGTH (tparms);
7978 bool ret = true;
7979
7980 /* We're dealing with template parms in this process. */
7981 ++processing_template_decl;
7982
7983 targs = INNERMOST_TEMPLATE_ARGS (targs);
7984
7985 for (i = 0; i < ntparms; ++i)
7986 {
7987 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7988 tree targ = TREE_VEC_ELT (targs, i);
7989
7990 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7991 {
7992 tree packed_args = NULL_TREE;
7993 int idx, len = 1;
7994
7995 if (ARGUMENT_PACK_P (targ))
7996 {
7997 /* Look inside the argument pack. */
7998 packed_args = ARGUMENT_PACK_ARGS (targ);
7999 len = TREE_VEC_LENGTH (packed_args);
8000 }
8001
8002 for (idx = 0; idx < len; ++idx)
8003 {
8004 tree targ_parms = NULL_TREE;
8005
8006 if (packed_args)
8007 /* Extract the next argument from the argument
8008 pack. */
8009 targ = TREE_VEC_ELT (packed_args, idx);
8010
8011 if (PACK_EXPANSION_P (targ))
8012 /* Look at the pattern of the pack expansion. */
8013 targ = PACK_EXPANSION_PATTERN (targ);
8014
8015 /* Extract the template parameters from the template
8016 argument. */
8017 if (TREE_CODE (targ) == TEMPLATE_DECL)
8018 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8019 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8020 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8021
8022 /* Verify that we can coerce the template template
8023 parameters from the template argument to the template
8024 parameter. This requires an exact match. */
8025 if (targ_parms
8026 && !coerce_template_template_parms
8027 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8028 targ_parms,
8029 tf_none,
8030 tparm,
8031 targs))
8032 {
8033 ret = false;
8034 goto out;
8035 }
8036 }
8037 }
8038 }
8039
8040 out:
8041
8042 --processing_template_decl;
8043 return ret;
8044 }
8045
8046 /* Since type attributes aren't mangled, we need to strip them from
8047 template type arguments. */
8048
8049 tree
8050 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8051 {
8052 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8053 return arg;
8054 bool removed_attributes = false;
8055 tree canon = strip_typedefs (arg, &removed_attributes);
8056 if (removed_attributes
8057 && (complain & tf_warning))
8058 warning (OPT_Wignored_attributes,
8059 "ignoring attributes on template argument %qT", arg);
8060 return canon;
8061 }
8062
8063 /* And from inside dependent non-type arguments like sizeof(Type). */
8064
8065 static tree
8066 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8067 {
8068 if (!arg || arg == error_mark_node)
8069 return arg;
8070 bool removed_attributes = false;
8071 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8072 if (removed_attributes
8073 && (complain & tf_warning))
8074 warning (OPT_Wignored_attributes,
8075 "ignoring attributes in template argument %qE", arg);
8076 return canon;
8077 }
8078
8079 // A template declaration can be substituted for a constrained
8080 // template template parameter only when the argument is more
8081 // constrained than the parameter.
8082 static bool
8083 is_compatible_template_arg (tree parm, tree arg)
8084 {
8085 tree parm_cons = get_constraints (parm);
8086
8087 /* For now, allow constrained template template arguments
8088 and unconstrained template template parameters. */
8089 if (parm_cons == NULL_TREE)
8090 return true;
8091
8092 /* If the template parameter is constrained, we need to rewrite its
8093 constraints in terms of the ARG's template parameters. This ensures
8094 that all of the template parameter types will have the same depth.
8095
8096 Note that this is only valid when coerce_template_template_parm is
8097 true for the innermost template parameters of PARM and ARG. In other
8098 words, because coercion is successful, this conversion will be valid. */
8099 tree new_args = NULL_TREE;
8100 if (parm_cons)
8101 {
8102 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8103 new_args = template_parms_level_to_args (aparms);
8104 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8105 tf_none, NULL_TREE);
8106 if (parm_cons == error_mark_node)
8107 return false;
8108 }
8109
8110 return weakly_subsumes (parm_cons, new_args, arg);
8111 }
8112
8113 // Convert a placeholder argument into a binding to the original
8114 // parameter. The original parameter is saved as the TREE_TYPE of
8115 // ARG.
8116 static inline tree
8117 convert_wildcard_argument (tree parm, tree arg)
8118 {
8119 TREE_TYPE (arg) = parm;
8120 return arg;
8121 }
8122
8123 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8124 because one of them is dependent. But we need to represent the
8125 conversion for the benefit of cp_tree_equal. */
8126
8127 static tree
8128 maybe_convert_nontype_argument (tree type, tree arg)
8129 {
8130 /* Auto parms get no conversion. */
8131 if (type_uses_auto (type))
8132 return arg;
8133 /* We don't need or want to add this conversion now if we're going to use the
8134 argument for deduction. */
8135 if (value_dependent_expression_p (arg))
8136 return arg;
8137
8138 type = cv_unqualified (type);
8139 tree argtype = TREE_TYPE (arg);
8140 if (same_type_p (type, argtype))
8141 return arg;
8142
8143 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8144 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8145 return arg;
8146 }
8147
8148 /* Convert the indicated template ARG as necessary to match the
8149 indicated template PARM. Returns the converted ARG, or
8150 error_mark_node if the conversion was unsuccessful. Error and
8151 warning messages are issued under control of COMPLAIN. This
8152 conversion is for the Ith parameter in the parameter list. ARGS is
8153 the full set of template arguments deduced so far. */
8154
8155 static tree
8156 convert_template_argument (tree parm,
8157 tree arg,
8158 tree args,
8159 tsubst_flags_t complain,
8160 int i,
8161 tree in_decl)
8162 {
8163 tree orig_arg;
8164 tree val;
8165 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8166
8167 if (parm == error_mark_node || error_operand_p (arg))
8168 return error_mark_node;
8169
8170 /* Trivially convert placeholders. */
8171 if (TREE_CODE (arg) == WILDCARD_DECL)
8172 return convert_wildcard_argument (parm, arg);
8173
8174 if (arg == any_targ_node)
8175 return arg;
8176
8177 if (TREE_CODE (arg) == TREE_LIST
8178 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8179 {
8180 /* The template argument was the name of some
8181 member function. That's usually
8182 invalid, but static members are OK. In any
8183 case, grab the underlying fields/functions
8184 and issue an error later if required. */
8185 TREE_TYPE (arg) = unknown_type_node;
8186 }
8187
8188 orig_arg = arg;
8189
8190 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8191 requires_type = (TREE_CODE (parm) == TYPE_DECL
8192 || requires_tmpl_type);
8193
8194 /* When determining whether an argument pack expansion is a template,
8195 look at the pattern. */
8196 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8197 arg = PACK_EXPANSION_PATTERN (arg);
8198
8199 /* Deal with an injected-class-name used as a template template arg. */
8200 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8201 {
8202 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8203 if (TREE_CODE (t) == TEMPLATE_DECL)
8204 {
8205 if (cxx_dialect >= cxx11)
8206 /* OK under DR 1004. */;
8207 else if (complain & tf_warning_or_error)
8208 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8209 " used as template template argument", TYPE_NAME (arg));
8210 else if (flag_pedantic_errors)
8211 t = arg;
8212
8213 arg = t;
8214 }
8215 }
8216
8217 is_tmpl_type =
8218 ((TREE_CODE (arg) == TEMPLATE_DECL
8219 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8220 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8221 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8222 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8223
8224 if (is_tmpl_type
8225 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8226 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8227 arg = TYPE_STUB_DECL (arg);
8228
8229 is_type = TYPE_P (arg) || is_tmpl_type;
8230
8231 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8232 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8233 {
8234 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8235 {
8236 if (complain & tf_error)
8237 error ("invalid use of destructor %qE as a type", orig_arg);
8238 return error_mark_node;
8239 }
8240
8241 permerror (input_location,
8242 "to refer to a type member of a template parameter, "
8243 "use %<typename %E%>", orig_arg);
8244
8245 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8246 TREE_OPERAND (arg, 1),
8247 typename_type,
8248 complain);
8249 arg = orig_arg;
8250 is_type = 1;
8251 }
8252 if (is_type != requires_type)
8253 {
8254 if (in_decl)
8255 {
8256 if (complain & tf_error)
8257 {
8258 error ("type/value mismatch at argument %d in template "
8259 "parameter list for %qD",
8260 i + 1, in_decl);
8261 if (is_type)
8262 {
8263 /* The template argument is a type, but we're expecting
8264 an expression. */
8265 inform (input_location,
8266 " expected a constant of type %qT, got %qT",
8267 TREE_TYPE (parm),
8268 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8269 /* [temp.arg]/2: "In a template-argument, an ambiguity
8270 between a type-id and an expression is resolved to a
8271 type-id, regardless of the form of the corresponding
8272 template-parameter." So give the user a clue. */
8273 if (TREE_CODE (arg) == FUNCTION_TYPE)
8274 inform (input_location, " ambiguous template argument "
8275 "for non-type template parameter is treated as "
8276 "function type");
8277 }
8278 else if (requires_tmpl_type)
8279 inform (input_location,
8280 " expected a class template, got %qE", orig_arg);
8281 else
8282 inform (input_location,
8283 " expected a type, got %qE", orig_arg);
8284 }
8285 }
8286 return error_mark_node;
8287 }
8288 if (is_tmpl_type ^ requires_tmpl_type)
8289 {
8290 if (in_decl && (complain & tf_error))
8291 {
8292 error ("type/value mismatch at argument %d in template "
8293 "parameter list for %qD",
8294 i + 1, in_decl);
8295 if (is_tmpl_type)
8296 inform (input_location,
8297 " expected a type, got %qT", DECL_NAME (arg));
8298 else
8299 inform (input_location,
8300 " expected a class template, got %qT", orig_arg);
8301 }
8302 return error_mark_node;
8303 }
8304
8305 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8306 /* We already did the appropriate conversion when packing args. */
8307 val = orig_arg;
8308 else if (is_type)
8309 {
8310 if (requires_tmpl_type)
8311 {
8312 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8313 /* The number of argument required is not known yet.
8314 Just accept it for now. */
8315 val = orig_arg;
8316 else
8317 {
8318 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8319 tree argparm;
8320
8321 /* Strip alias templates that are equivalent to another
8322 template. */
8323 arg = get_underlying_template (arg);
8324 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8325
8326 if (coerce_template_template_parms (parmparm, argparm,
8327 complain, in_decl,
8328 args))
8329 {
8330 val = arg;
8331
8332 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8333 TEMPLATE_DECL. */
8334 if (val != error_mark_node)
8335 {
8336 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8337 val = TREE_TYPE (val);
8338 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8339 val = make_pack_expansion (val, complain);
8340 }
8341 }
8342 else
8343 {
8344 if (in_decl && (complain & tf_error))
8345 {
8346 error ("type/value mismatch at argument %d in "
8347 "template parameter list for %qD",
8348 i + 1, in_decl);
8349 inform (input_location,
8350 " expected a template of type %qD, got %qT",
8351 parm, orig_arg);
8352 }
8353
8354 val = error_mark_node;
8355 }
8356
8357 // Check that the constraints are compatible before allowing the
8358 // substitution.
8359 if (val != error_mark_node)
8360 if (!is_compatible_template_arg (parm, arg))
8361 {
8362 if (in_decl && (complain & tf_error))
8363 {
8364 error ("constraint mismatch at argument %d in "
8365 "template parameter list for %qD",
8366 i + 1, in_decl);
8367 inform (input_location, " expected %qD but got %qD",
8368 parm, arg);
8369 }
8370 val = error_mark_node;
8371 }
8372 }
8373 }
8374 else
8375 val = orig_arg;
8376 /* We only form one instance of each template specialization.
8377 Therefore, if we use a non-canonical variant (i.e., a
8378 typedef), any future messages referring to the type will use
8379 the typedef, which is confusing if those future uses do not
8380 themselves also use the typedef. */
8381 if (TYPE_P (val))
8382 val = canonicalize_type_argument (val, complain);
8383 }
8384 else
8385 {
8386 tree t = TREE_TYPE (parm);
8387
8388 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8389 > TMPL_ARGS_DEPTH (args))
8390 /* We don't have enough levels of args to do any substitution. This
8391 can happen in the context of -fnew-ttp-matching. */;
8392 else if (tree a = type_uses_auto (t))
8393 {
8394 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8395 if (t == error_mark_node)
8396 return error_mark_node;
8397 }
8398 else
8399 t = tsubst (t, args, complain, in_decl);
8400
8401 if (invalid_nontype_parm_type_p (t, complain))
8402 return error_mark_node;
8403
8404 if (t != TREE_TYPE (parm))
8405 t = canonicalize_type_argument (t, complain);
8406
8407 if (!type_dependent_expression_p (orig_arg)
8408 && !uses_template_parms (t))
8409 /* We used to call digest_init here. However, digest_init
8410 will report errors, which we don't want when complain
8411 is zero. More importantly, digest_init will try too
8412 hard to convert things: for example, `0' should not be
8413 converted to pointer type at this point according to
8414 the standard. Accepting this is not merely an
8415 extension, since deciding whether or not these
8416 conversions can occur is part of determining which
8417 function template to call, or whether a given explicit
8418 argument specification is valid. */
8419 val = convert_nontype_argument (t, orig_arg, complain);
8420 else
8421 {
8422 val = canonicalize_expr_argument (orig_arg, complain);
8423 val = maybe_convert_nontype_argument (t, val);
8424 }
8425
8426
8427 if (val == NULL_TREE)
8428 val = error_mark_node;
8429 else if (val == error_mark_node && (complain & tf_error))
8430 error_at (cp_expr_loc_or_input_loc (orig_arg),
8431 "could not convert template argument %qE from %qT to %qT",
8432 orig_arg, TREE_TYPE (orig_arg), t);
8433
8434 if (INDIRECT_REF_P (val))
8435 {
8436 /* Reject template arguments that are references to built-in
8437 functions with no library fallbacks. */
8438 const_tree inner = TREE_OPERAND (val, 0);
8439 const_tree innertype = TREE_TYPE (inner);
8440 if (innertype
8441 && TYPE_REF_P (innertype)
8442 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8443 && TREE_OPERAND_LENGTH (inner) > 0
8444 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8445 return error_mark_node;
8446 }
8447
8448 if (TREE_CODE (val) == SCOPE_REF)
8449 {
8450 /* Strip typedefs from the SCOPE_REF. */
8451 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8452 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8453 complain);
8454 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8455 QUALIFIED_NAME_IS_TEMPLATE (val));
8456 }
8457 }
8458
8459 return val;
8460 }
8461
8462 /* Coerces the remaining template arguments in INNER_ARGS (from
8463 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8464 Returns the coerced argument pack. PARM_IDX is the position of this
8465 parameter in the template parameter list. ARGS is the original
8466 template argument list. */
8467 static tree
8468 coerce_template_parameter_pack (tree parms,
8469 int parm_idx,
8470 tree args,
8471 tree inner_args,
8472 int arg_idx,
8473 tree new_args,
8474 int* lost,
8475 tree in_decl,
8476 tsubst_flags_t complain)
8477 {
8478 tree parm = TREE_VEC_ELT (parms, parm_idx);
8479 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8480 tree packed_args;
8481 tree argument_pack;
8482 tree packed_parms = NULL_TREE;
8483
8484 if (arg_idx > nargs)
8485 arg_idx = nargs;
8486
8487 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8488 {
8489 /* When the template parameter is a non-type template parameter pack
8490 or template template parameter pack whose type or template
8491 parameters use parameter packs, we know exactly how many arguments
8492 we are looking for. Build a vector of the instantiated decls for
8493 these template parameters in PACKED_PARMS. */
8494 /* We can't use make_pack_expansion here because it would interpret a
8495 _DECL as a use rather than a declaration. */
8496 tree decl = TREE_VALUE (parm);
8497 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8498 SET_PACK_EXPANSION_PATTERN (exp, decl);
8499 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8500 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8501
8502 TREE_VEC_LENGTH (args)--;
8503 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8504 TREE_VEC_LENGTH (args)++;
8505
8506 if (packed_parms == error_mark_node)
8507 return error_mark_node;
8508
8509 /* If we're doing a partial instantiation of a member template,
8510 verify that all of the types used for the non-type
8511 template parameter pack are, in fact, valid for non-type
8512 template parameters. */
8513 if (arg_idx < nargs
8514 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8515 {
8516 int j, len = TREE_VEC_LENGTH (packed_parms);
8517 for (j = 0; j < len; ++j)
8518 {
8519 tree t = TREE_VEC_ELT (packed_parms, j);
8520 if (TREE_CODE (t) == PARM_DECL
8521 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8522 return error_mark_node;
8523 }
8524 /* We don't know how many args we have yet, just
8525 use the unconverted ones for now. */
8526 return NULL_TREE;
8527 }
8528
8529 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8530 }
8531 /* Check if we have a placeholder pack, which indicates we're
8532 in the context of a introduction list. In that case we want
8533 to match this pack to the single placeholder. */
8534 else if (arg_idx < nargs
8535 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8536 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8537 {
8538 nargs = arg_idx + 1;
8539 packed_args = make_tree_vec (1);
8540 }
8541 else
8542 packed_args = make_tree_vec (nargs - arg_idx);
8543
8544 /* Convert the remaining arguments, which will be a part of the
8545 parameter pack "parm". */
8546 int first_pack_arg = arg_idx;
8547 for (; arg_idx < nargs; ++arg_idx)
8548 {
8549 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8550 tree actual_parm = TREE_VALUE (parm);
8551 int pack_idx = arg_idx - first_pack_arg;
8552
8553 if (packed_parms)
8554 {
8555 /* Once we've packed as many args as we have types, stop. */
8556 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8557 break;
8558 else if (PACK_EXPANSION_P (arg))
8559 /* We don't know how many args we have yet, just
8560 use the unconverted ones for now. */
8561 return NULL_TREE;
8562 else
8563 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8564 }
8565
8566 if (arg == error_mark_node)
8567 {
8568 if (complain & tf_error)
8569 error ("template argument %d is invalid", arg_idx + 1);
8570 }
8571 else
8572 arg = convert_template_argument (actual_parm,
8573 arg, new_args, complain, parm_idx,
8574 in_decl);
8575 if (arg == error_mark_node)
8576 (*lost)++;
8577 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8578 }
8579
8580 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8581 && TREE_VEC_LENGTH (packed_args) > 0)
8582 {
8583 if (complain & tf_error)
8584 error ("wrong number of template arguments (%d, should be %d)",
8585 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8586 return error_mark_node;
8587 }
8588
8589 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8590 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8591 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8592 else
8593 {
8594 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8595 TREE_CONSTANT (argument_pack) = 1;
8596 }
8597
8598 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8599 if (CHECKING_P)
8600 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8601 TREE_VEC_LENGTH (packed_args));
8602 return argument_pack;
8603 }
8604
8605 /* Returns the number of pack expansions in the template argument vector
8606 ARGS. */
8607
8608 static int
8609 pack_expansion_args_count (tree args)
8610 {
8611 int i;
8612 int count = 0;
8613 if (args)
8614 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8615 {
8616 tree elt = TREE_VEC_ELT (args, i);
8617 if (elt && PACK_EXPANSION_P (elt))
8618 ++count;
8619 }
8620 return count;
8621 }
8622
8623 /* Convert all template arguments to their appropriate types, and
8624 return a vector containing the innermost resulting template
8625 arguments. If any error occurs, return error_mark_node. Error and
8626 warning messages are issued under control of COMPLAIN.
8627
8628 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8629 for arguments not specified in ARGS. Otherwise, if
8630 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8631 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8632 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8633 ARGS. */
8634
8635 static tree
8636 coerce_template_parms (tree parms,
8637 tree args,
8638 tree in_decl,
8639 tsubst_flags_t complain,
8640 bool require_all_args,
8641 bool use_default_args)
8642 {
8643 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8644 tree orig_inner_args;
8645 tree inner_args;
8646 tree new_args;
8647 tree new_inner_args;
8648
8649 /* When used as a boolean value, indicates whether this is a
8650 variadic template parameter list. Since it's an int, we can also
8651 subtract it from nparms to get the number of non-variadic
8652 parameters. */
8653 int variadic_p = 0;
8654 int variadic_args_p = 0;
8655 int post_variadic_parms = 0;
8656
8657 /* Adjustment to nparms for fixed parameter packs. */
8658 int fixed_pack_adjust = 0;
8659 int fixed_packs = 0;
8660 int missing = 0;
8661
8662 /* Likewise for parameters with default arguments. */
8663 int default_p = 0;
8664
8665 if (args == error_mark_node)
8666 return error_mark_node;
8667
8668 nparms = TREE_VEC_LENGTH (parms);
8669
8670 /* Determine if there are any parameter packs or default arguments. */
8671 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8672 {
8673 tree parm = TREE_VEC_ELT (parms, parm_idx);
8674 if (variadic_p)
8675 ++post_variadic_parms;
8676 if (template_parameter_pack_p (TREE_VALUE (parm)))
8677 ++variadic_p;
8678 if (TREE_PURPOSE (parm))
8679 ++default_p;
8680 }
8681
8682 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8683 /* If there are no parameters that follow a parameter pack, we need to
8684 expand any argument packs so that we can deduce a parameter pack from
8685 some non-packed args followed by an argument pack, as in variadic85.C.
8686 If there are such parameters, we need to leave argument packs intact
8687 so the arguments are assigned properly. This can happen when dealing
8688 with a nested class inside a partial specialization of a class
8689 template, as in variadic92.C, or when deducing a template parameter pack
8690 from a sub-declarator, as in variadic114.C. */
8691 if (!post_variadic_parms)
8692 inner_args = expand_template_argument_pack (inner_args);
8693
8694 /* Count any pack expansion args. */
8695 variadic_args_p = pack_expansion_args_count (inner_args);
8696
8697 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8698 if ((nargs - variadic_args_p > nparms && !variadic_p)
8699 || (nargs < nparms - variadic_p
8700 && require_all_args
8701 && !variadic_args_p
8702 && (!use_default_args
8703 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8704 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8705 {
8706 bad_nargs:
8707 if (complain & tf_error)
8708 {
8709 if (variadic_p || default_p)
8710 {
8711 nparms -= variadic_p + default_p;
8712 error ("wrong number of template arguments "
8713 "(%d, should be at least %d)", nargs, nparms);
8714 }
8715 else
8716 error ("wrong number of template arguments "
8717 "(%d, should be %d)", nargs, nparms);
8718
8719 if (in_decl)
8720 inform (DECL_SOURCE_LOCATION (in_decl),
8721 "provided for %qD", in_decl);
8722 }
8723
8724 return error_mark_node;
8725 }
8726 /* We can't pass a pack expansion to a non-pack parameter of an alias
8727 template (DR 1430). */
8728 else if (in_decl
8729 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8730 || concept_definition_p (in_decl))
8731 && variadic_args_p
8732 && nargs - variadic_args_p < nparms - variadic_p)
8733 {
8734 if (complain & tf_error)
8735 {
8736 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8737 {
8738 tree arg = TREE_VEC_ELT (inner_args, i);
8739 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8740
8741 if (PACK_EXPANSION_P (arg)
8742 && !template_parameter_pack_p (parm))
8743 {
8744 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8745 error_at (location_of (arg),
8746 "pack expansion argument for non-pack parameter "
8747 "%qD of alias template %qD", parm, in_decl);
8748 else
8749 error_at (location_of (arg),
8750 "pack expansion argument for non-pack parameter "
8751 "%qD of concept %qD", parm, in_decl);
8752 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8753 goto found;
8754 }
8755 }
8756 gcc_unreachable ();
8757 found:;
8758 }
8759 return error_mark_node;
8760 }
8761
8762 /* We need to evaluate the template arguments, even though this
8763 template-id may be nested within a "sizeof". */
8764 cp_evaluated ev;
8765
8766 new_inner_args = make_tree_vec (nparms);
8767 new_args = add_outermost_template_args (args, new_inner_args);
8768 int pack_adjust = 0;
8769 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8770 {
8771 tree arg;
8772 tree parm;
8773
8774 /* Get the Ith template parameter. */
8775 parm = TREE_VEC_ELT (parms, parm_idx);
8776
8777 if (parm == error_mark_node)
8778 {
8779 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8780 continue;
8781 }
8782
8783 /* Calculate the next argument. */
8784 if (arg_idx < nargs)
8785 arg = TREE_VEC_ELT (inner_args, arg_idx);
8786 else
8787 arg = NULL_TREE;
8788
8789 if (template_parameter_pack_p (TREE_VALUE (parm))
8790 && (arg || require_all_args || !(complain & tf_partial))
8791 && !(arg && ARGUMENT_PACK_P (arg)))
8792 {
8793 /* Some arguments will be placed in the
8794 template parameter pack PARM. */
8795 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8796 inner_args, arg_idx,
8797 new_args, &lost,
8798 in_decl, complain);
8799
8800 if (arg == NULL_TREE)
8801 {
8802 /* We don't know how many args we have yet, just use the
8803 unconverted (and still packed) ones for now. */
8804 new_inner_args = orig_inner_args;
8805 arg_idx = nargs;
8806 break;
8807 }
8808
8809 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8810
8811 /* Store this argument. */
8812 if (arg == error_mark_node)
8813 {
8814 lost++;
8815 /* We are done with all of the arguments. */
8816 arg_idx = nargs;
8817 break;
8818 }
8819 else
8820 {
8821 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8822 arg_idx += pack_adjust;
8823 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8824 {
8825 ++fixed_packs;
8826 fixed_pack_adjust += pack_adjust;
8827 }
8828 }
8829
8830 continue;
8831 }
8832 else if (arg)
8833 {
8834 if (PACK_EXPANSION_P (arg))
8835 {
8836 /* "If every valid specialization of a variadic template
8837 requires an empty template parameter pack, the template is
8838 ill-formed, no diagnostic required." So check that the
8839 pattern works with this parameter. */
8840 tree pattern = PACK_EXPANSION_PATTERN (arg);
8841 tree conv = convert_template_argument (TREE_VALUE (parm),
8842 pattern, new_args,
8843 complain, parm_idx,
8844 in_decl);
8845 if (conv == error_mark_node)
8846 {
8847 if (complain & tf_error)
8848 inform (input_location, "so any instantiation with a "
8849 "non-empty parameter pack would be ill-formed");
8850 ++lost;
8851 }
8852 else if (TYPE_P (conv) && !TYPE_P (pattern))
8853 /* Recover from missing typename. */
8854 TREE_VEC_ELT (inner_args, arg_idx)
8855 = make_pack_expansion (conv, complain);
8856
8857 /* We don't know how many args we have yet, just
8858 use the unconverted ones for now. */
8859 new_inner_args = inner_args;
8860 arg_idx = nargs;
8861 break;
8862 }
8863 }
8864 else if (require_all_args)
8865 {
8866 /* There must be a default arg in this case. */
8867 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8868 complain, in_decl);
8869 /* The position of the first default template argument,
8870 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8871 Record that. */
8872 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8873 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8874 arg_idx - pack_adjust);
8875 }
8876 else
8877 break;
8878
8879 if (arg == error_mark_node)
8880 {
8881 if (complain & tf_error)
8882 error ("template argument %d is invalid", arg_idx + 1);
8883 }
8884 else if (!arg)
8885 {
8886 /* This can occur if there was an error in the template
8887 parameter list itself (which we would already have
8888 reported) that we are trying to recover from, e.g., a class
8889 template with a parameter list such as
8890 template<typename..., typename> (cpp0x/variadic150.C). */
8891 ++lost;
8892
8893 /* This can also happen with a fixed parameter pack (71834). */
8894 if (arg_idx >= nargs)
8895 ++missing;
8896 }
8897 else
8898 arg = convert_template_argument (TREE_VALUE (parm),
8899 arg, new_args, complain,
8900 parm_idx, in_decl);
8901
8902 if (arg == error_mark_node)
8903 lost++;
8904
8905 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8906 }
8907
8908 if (missing || arg_idx < nargs - variadic_args_p)
8909 {
8910 /* If we had fixed parameter packs, we didn't know how many arguments we
8911 actually needed earlier; now we do. */
8912 nparms += fixed_pack_adjust;
8913 variadic_p -= fixed_packs;
8914 goto bad_nargs;
8915 }
8916
8917 if (arg_idx < nargs)
8918 {
8919 /* We had some pack expansion arguments that will only work if the packs
8920 are empty, but wait until instantiation time to complain.
8921 See variadic-ttp3.C. */
8922
8923 /* Except that we can't provide empty packs to alias templates or
8924 concepts when there are no corresponding parameters. Basically,
8925 we can get here with this:
8926
8927 template<typename T> concept C = true;
8928
8929 template<typename... Args>
8930 requires C<Args...>
8931 void f();
8932
8933 When parsing C<Args...>, we try to form a concept check of
8934 C<?, Args...>. Without the extra check for substituting an empty
8935 pack past the last parameter, we can accept the check as valid.
8936
8937 FIXME: This may be valid for alias templates (but I doubt it).
8938
8939 FIXME: The error could be better also. */
8940 if (in_decl && concept_definition_p (in_decl))
8941 {
8942 if (complain & tf_error)
8943 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8944 "too many arguments");
8945 return error_mark_node;
8946 }
8947
8948 int len = nparms + (nargs - arg_idx);
8949 tree args = make_tree_vec (len);
8950 int i = 0;
8951 for (; i < nparms; ++i)
8952 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8953 for (; i < len; ++i, ++arg_idx)
8954 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8955 arg_idx - pack_adjust);
8956 new_inner_args = args;
8957 }
8958
8959 if (lost)
8960 {
8961 gcc_assert (!(complain & tf_error) || seen_error ());
8962 return error_mark_node;
8963 }
8964
8965 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8966 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8967 TREE_VEC_LENGTH (new_inner_args));
8968
8969 return new_inner_args;
8970 }
8971
8972 /* Convert all template arguments to their appropriate types, and
8973 return a vector containing the innermost resulting template
8974 arguments. If any error occurs, return error_mark_node. Error and
8975 warning messages are not issued.
8976
8977 Note that no function argument deduction is performed, and default
8978 arguments are used to fill in unspecified arguments. */
8979 tree
8980 coerce_template_parms (tree parms, tree args, tree in_decl)
8981 {
8982 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8983 }
8984
8985 /* Convert all template arguments to their appropriate type, and
8986 instantiate default arguments as needed. This returns a vector
8987 containing the innermost resulting template arguments, or
8988 error_mark_node if unsuccessful. */
8989 tree
8990 coerce_template_parms (tree parms, tree args, tree in_decl,
8991 tsubst_flags_t complain)
8992 {
8993 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8994 }
8995
8996 /* Like coerce_template_parms. If PARMS represents all template
8997 parameters levels, this function returns a vector of vectors
8998 representing all the resulting argument levels. Note that in this
8999 case, only the innermost arguments are coerced because the
9000 outermost ones are supposed to have been coerced already.
9001
9002 Otherwise, if PARMS represents only (the innermost) vector of
9003 parameters, this function returns a vector containing just the
9004 innermost resulting arguments. */
9005
9006 static tree
9007 coerce_innermost_template_parms (tree parms,
9008 tree args,
9009 tree in_decl,
9010 tsubst_flags_t complain,
9011 bool require_all_args,
9012 bool use_default_args)
9013 {
9014 int parms_depth = TMPL_PARMS_DEPTH (parms);
9015 int args_depth = TMPL_ARGS_DEPTH (args);
9016 tree coerced_args;
9017
9018 if (parms_depth > 1)
9019 {
9020 coerced_args = make_tree_vec (parms_depth);
9021 tree level;
9022 int cur_depth;
9023
9024 for (level = parms, cur_depth = parms_depth;
9025 parms_depth > 0 && level != NULL_TREE;
9026 level = TREE_CHAIN (level), --cur_depth)
9027 {
9028 tree l;
9029 if (cur_depth == args_depth)
9030 l = coerce_template_parms (TREE_VALUE (level),
9031 args, in_decl, complain,
9032 require_all_args,
9033 use_default_args);
9034 else
9035 l = TMPL_ARGS_LEVEL (args, cur_depth);
9036
9037 if (l == error_mark_node)
9038 return error_mark_node;
9039
9040 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9041 }
9042 }
9043 else
9044 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9045 args, in_decl, complain,
9046 require_all_args,
9047 use_default_args);
9048 return coerced_args;
9049 }
9050
9051 /* Returns true if T is a wrapper to make a C++20 template parameter
9052 object const. */
9053
9054 static bool
9055 class_nttp_const_wrapper_p (tree t)
9056 {
9057 if (cxx_dialect < cxx2a)
9058 return false;
9059 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9060 && CP_TYPE_CONST_P (TREE_TYPE (t))
9061 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9062 }
9063
9064 /* Returns 1 if template args OT and NT are equivalent. */
9065
9066 int
9067 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9068 {
9069 if (nt == ot)
9070 return 1;
9071 if (nt == NULL_TREE || ot == NULL_TREE)
9072 return false;
9073 if (nt == any_targ_node || ot == any_targ_node)
9074 return true;
9075
9076 if (class_nttp_const_wrapper_p (nt))
9077 nt = TREE_OPERAND (nt, 0);
9078 if (class_nttp_const_wrapper_p (ot))
9079 ot = TREE_OPERAND (ot, 0);
9080
9081 if (TREE_CODE (nt) == TREE_VEC)
9082 /* For member templates */
9083 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
9084 else if (PACK_EXPANSION_P (ot))
9085 return (PACK_EXPANSION_P (nt)
9086 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9087 PACK_EXPANSION_PATTERN (nt))
9088 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9089 PACK_EXPANSION_EXTRA_ARGS (nt)));
9090 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9091 return cp_tree_equal (ot, nt);
9092 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9093 gcc_unreachable ();
9094 else if (TYPE_P (nt))
9095 {
9096 if (!TYPE_P (ot))
9097 return false;
9098 /* Don't treat an alias template specialization with dependent
9099 arguments as equivalent to its underlying type when used as a
9100 template argument; we need them to be distinct so that we
9101 substitute into the specialization arguments at instantiation
9102 time. And aliases can't be equivalent without being ==, so
9103 we don't need to look any deeper.
9104
9105 During partial ordering, however, we need to treat them normally so
9106 that we can order uses of the same alias with different
9107 cv-qualification (79960). */
9108 if (!partial_order
9109 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9110 return false;
9111 else
9112 return same_type_p (ot, nt);
9113 }
9114 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
9115 return 0;
9116 else
9117 {
9118 /* Try to treat a template non-type argument that has been converted
9119 to the parameter type as equivalent to one that hasn't yet. */
9120 for (enum tree_code code1 = TREE_CODE (ot);
9121 CONVERT_EXPR_CODE_P (code1)
9122 || code1 == NON_LVALUE_EXPR;
9123 code1 = TREE_CODE (ot))
9124 ot = TREE_OPERAND (ot, 0);
9125 for (enum tree_code code2 = TREE_CODE (nt);
9126 CONVERT_EXPR_CODE_P (code2)
9127 || code2 == NON_LVALUE_EXPR;
9128 code2 = TREE_CODE (nt))
9129 nt = TREE_OPERAND (nt, 0);
9130
9131 return cp_tree_equal (ot, nt);
9132 }
9133 }
9134
9135 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9136 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9137 NEWARG_PTR with the offending arguments if they are non-NULL. */
9138
9139 int
9140 comp_template_args (tree oldargs, tree newargs,
9141 tree *oldarg_ptr, tree *newarg_ptr,
9142 bool partial_order)
9143 {
9144 int i;
9145
9146 if (oldargs == newargs)
9147 return 1;
9148
9149 if (!oldargs || !newargs)
9150 return 0;
9151
9152 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9153 return 0;
9154
9155 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9156 {
9157 tree nt = TREE_VEC_ELT (newargs, i);
9158 tree ot = TREE_VEC_ELT (oldargs, i);
9159
9160 if (! template_args_equal (ot, nt, partial_order))
9161 {
9162 if (oldarg_ptr != NULL)
9163 *oldarg_ptr = ot;
9164 if (newarg_ptr != NULL)
9165 *newarg_ptr = nt;
9166 return 0;
9167 }
9168 }
9169 return 1;
9170 }
9171
9172 inline bool
9173 comp_template_args_porder (tree oargs, tree nargs)
9174 {
9175 return comp_template_args (oargs, nargs, NULL, NULL, true);
9176 }
9177
9178 /* Implement a freelist interface for objects of type T.
9179
9180 Head is a separate object, rather than a regular member, so that we
9181 can define it as a GTY deletable pointer, which is highly
9182 desirable. A data member could be declared that way, but then the
9183 containing object would implicitly get GTY((user)), which would
9184 prevent us from instantiating freelists as global objects.
9185 Although this way we can create freelist global objects, they're
9186 such thin wrappers that instantiating temporaries at every use
9187 loses nothing and saves permanent storage for the freelist object.
9188
9189 Member functions next, anew, poison and reinit have default
9190 implementations that work for most of the types we're interested
9191 in, but if they don't work for some type, they should be explicitly
9192 specialized. See the comments before them for requirements, and
9193 the example specializations for the tree_list_freelist. */
9194 template <typename T>
9195 class freelist
9196 {
9197 /* Return the next object in a chain. We could just do type
9198 punning, but if we access the object with its underlying type, we
9199 avoid strict-aliasing trouble. This needs only work between
9200 poison and reinit. */
9201 static T *&next (T *obj) { return obj->next; }
9202
9203 /* Return a newly allocated, uninitialized or minimally-initialized
9204 object of type T. Any initialization performed by anew should
9205 either remain across the life of the object and the execution of
9206 poison, or be redone by reinit. */
9207 static T *anew () { return ggc_alloc<T> (); }
9208
9209 /* Optionally scribble all over the bits holding the object, so that
9210 they become (mostly?) uninitialized memory. This is called while
9211 preparing to make the object part of the free list. */
9212 static void poison (T *obj) {
9213 T *p ATTRIBUTE_UNUSED = obj;
9214 T **q ATTRIBUTE_UNUSED = &next (obj);
9215
9216 #ifdef ENABLE_GC_CHECKING
9217 /* Poison the data, to indicate the data is garbage. */
9218 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9219 memset (p, 0xa5, sizeof (*p));
9220 #endif
9221 /* Let valgrind know the object is free. */
9222 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9223
9224 /* Let valgrind know the next portion of the object is available,
9225 but uninitialized. */
9226 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9227 }
9228
9229 /* Bring an object that underwent at least one lifecycle after anew
9230 and before the most recent free and poison, back to a usable
9231 state, reinitializing whatever is needed for it to be
9232 functionally equivalent to an object just allocated and returned
9233 by anew. This may poison or clear the next field, used by
9234 freelist housekeeping after poison was called. */
9235 static void reinit (T *obj) {
9236 T **q ATTRIBUTE_UNUSED = &next (obj);
9237
9238 #ifdef ENABLE_GC_CHECKING
9239 memset (q, 0xa5, sizeof (*q));
9240 #endif
9241 /* Let valgrind know the entire object is available, but
9242 uninitialized. */
9243 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9244 }
9245
9246 /* Reference a GTY-deletable pointer that points to the first object
9247 in the free list proper. */
9248 T *&head;
9249 public:
9250 /* Construct a freelist object chaining objects off of HEAD. */
9251 freelist (T *&head) : head(head) {}
9252
9253 /* Add OBJ to the free object list. The former head becomes OBJ's
9254 successor. */
9255 void free (T *obj)
9256 {
9257 poison (obj);
9258 next (obj) = head;
9259 head = obj;
9260 }
9261
9262 /* Take an object from the free list, if one is available, or
9263 allocate a new one. Objects taken from the free list should be
9264 regarded as filled with garbage, except for bits that are
9265 configured to be preserved across free and alloc. */
9266 T *alloc ()
9267 {
9268 if (head)
9269 {
9270 T *obj = head;
9271 head = next (head);
9272 reinit (obj);
9273 return obj;
9274 }
9275 else
9276 return anew ();
9277 }
9278 };
9279
9280 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9281 want to allocate a TREE_LIST using the usual interface, and ensure
9282 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9283 build_tree_list logic in reinit, so this could go out of sync. */
9284 template <>
9285 inline tree &
9286 freelist<tree_node>::next (tree obj)
9287 {
9288 return TREE_CHAIN (obj);
9289 }
9290 template <>
9291 inline tree
9292 freelist<tree_node>::anew ()
9293 {
9294 return build_tree_list (NULL, NULL);
9295 }
9296 template <>
9297 inline void
9298 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9299 {
9300 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9301 tree p ATTRIBUTE_UNUSED = obj;
9302 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9303 tree *q ATTRIBUTE_UNUSED = &next (obj);
9304
9305 #ifdef ENABLE_GC_CHECKING
9306 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9307
9308 /* Poison the data, to indicate the data is garbage. */
9309 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9310 memset (p, 0xa5, size);
9311 #endif
9312 /* Let valgrind know the object is free. */
9313 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9314 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9315 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9316 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9317
9318 #ifdef ENABLE_GC_CHECKING
9319 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9320 /* Keep TREE_CHAIN functional. */
9321 TREE_SET_CODE (obj, TREE_LIST);
9322 #else
9323 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9324 #endif
9325 }
9326 template <>
9327 inline void
9328 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9329 {
9330 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9331
9332 #ifdef ENABLE_GC_CHECKING
9333 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9334 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9335 memset (obj, 0, sizeof (tree_list));
9336 #endif
9337
9338 /* Let valgrind know the entire object is available, but
9339 uninitialized. */
9340 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9341
9342 #ifdef ENABLE_GC_CHECKING
9343 TREE_SET_CODE (obj, TREE_LIST);
9344 #else
9345 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9346 #endif
9347 }
9348
9349 /* Point to the first object in the TREE_LIST freelist. */
9350 static GTY((deletable)) tree tree_list_freelist_head;
9351 /* Return the/an actual TREE_LIST freelist. */
9352 static inline freelist<tree_node>
9353 tree_list_freelist ()
9354 {
9355 return tree_list_freelist_head;
9356 }
9357
9358 /* Point to the first object in the tinst_level freelist. */
9359 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9360 /* Return the/an actual tinst_level freelist. */
9361 static inline freelist<tinst_level>
9362 tinst_level_freelist ()
9363 {
9364 return tinst_level_freelist_head;
9365 }
9366
9367 /* Point to the first object in the pending_template freelist. */
9368 static GTY((deletable)) pending_template *pending_template_freelist_head;
9369 /* Return the/an actual pending_template freelist. */
9370 static inline freelist<pending_template>
9371 pending_template_freelist ()
9372 {
9373 return pending_template_freelist_head;
9374 }
9375
9376 /* Build the TREE_LIST object out of a split list, store it
9377 permanently, and return it. */
9378 tree
9379 tinst_level::to_list ()
9380 {
9381 gcc_assert (split_list_p ());
9382 tree ret = tree_list_freelist ().alloc ();
9383 TREE_PURPOSE (ret) = tldcl;
9384 TREE_VALUE (ret) = targs;
9385 tldcl = ret;
9386 targs = NULL;
9387 gcc_assert (tree_list_p ());
9388 return ret;
9389 }
9390
9391 const unsigned short tinst_level::refcount_infinity;
9392
9393 /* Increment OBJ's refcount unless it is already infinite. */
9394 static tinst_level *
9395 inc_refcount_use (tinst_level *obj)
9396 {
9397 if (obj && obj->refcount != tinst_level::refcount_infinity)
9398 ++obj->refcount;
9399 return obj;
9400 }
9401
9402 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9403 void
9404 tinst_level::free (tinst_level *obj)
9405 {
9406 if (obj->tree_list_p ())
9407 tree_list_freelist ().free (obj->get_node ());
9408 tinst_level_freelist ().free (obj);
9409 }
9410
9411 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9412 OBJ's DECL and OBJ, and start over with the tinst_level object that
9413 used to be referenced by OBJ's NEXT. */
9414 static void
9415 dec_refcount_use (tinst_level *obj)
9416 {
9417 while (obj
9418 && obj->refcount != tinst_level::refcount_infinity
9419 && !--obj->refcount)
9420 {
9421 tinst_level *next = obj->next;
9422 tinst_level::free (obj);
9423 obj = next;
9424 }
9425 }
9426
9427 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9428 and of the former PTR. Omitting the second argument is equivalent
9429 to passing (T*)NULL; this is allowed because passing the
9430 zero-valued integral constant NULL confuses type deduction and/or
9431 overload resolution. */
9432 template <typename T>
9433 static void
9434 set_refcount_ptr (T *& ptr, T *obj = NULL)
9435 {
9436 T *save = ptr;
9437 ptr = inc_refcount_use (obj);
9438 dec_refcount_use (save);
9439 }
9440
9441 static void
9442 add_pending_template (tree d)
9443 {
9444 tree ti = (TYPE_P (d)
9445 ? CLASSTYPE_TEMPLATE_INFO (d)
9446 : DECL_TEMPLATE_INFO (d));
9447 struct pending_template *pt;
9448 int level;
9449
9450 if (TI_PENDING_TEMPLATE_FLAG (ti))
9451 return;
9452
9453 /* We are called both from instantiate_decl, where we've already had a
9454 tinst_level pushed, and instantiate_template, where we haven't.
9455 Compensate. */
9456 gcc_assert (TREE_CODE (d) != TREE_LIST);
9457 level = !current_tinst_level
9458 || current_tinst_level->maybe_get_node () != d;
9459
9460 if (level)
9461 push_tinst_level (d);
9462
9463 pt = pending_template_freelist ().alloc ();
9464 pt->next = NULL;
9465 pt->tinst = NULL;
9466 set_refcount_ptr (pt->tinst, current_tinst_level);
9467 if (last_pending_template)
9468 last_pending_template->next = pt;
9469 else
9470 pending_templates = pt;
9471
9472 last_pending_template = pt;
9473
9474 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9475
9476 if (level)
9477 pop_tinst_level ();
9478 }
9479
9480
9481 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9482 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9483 documentation for TEMPLATE_ID_EXPR. */
9484
9485 tree
9486 lookup_template_function (tree fns, tree arglist)
9487 {
9488 if (fns == error_mark_node || arglist == error_mark_node)
9489 return error_mark_node;
9490
9491 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9492
9493 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9494 {
9495 error ("%q#D is not a function template", fns);
9496 return error_mark_node;
9497 }
9498
9499 if (BASELINK_P (fns))
9500 {
9501 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9502 unknown_type_node,
9503 BASELINK_FUNCTIONS (fns),
9504 arglist);
9505 return fns;
9506 }
9507
9508 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9509 }
9510
9511 /* Within the scope of a template class S<T>, the name S gets bound
9512 (in build_self_reference) to a TYPE_DECL for the class, not a
9513 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9514 or one of its enclosing classes, and that type is a template,
9515 return the associated TEMPLATE_DECL. Otherwise, the original
9516 DECL is returned.
9517
9518 Also handle the case when DECL is a TREE_LIST of ambiguous
9519 injected-class-names from different bases. */
9520
9521 tree
9522 maybe_get_template_decl_from_type_decl (tree decl)
9523 {
9524 if (decl == NULL_TREE)
9525 return decl;
9526
9527 /* DR 176: A lookup that finds an injected-class-name (10.2
9528 [class.member.lookup]) can result in an ambiguity in certain cases
9529 (for example, if it is found in more than one base class). If all of
9530 the injected-class-names that are found refer to specializations of
9531 the same class template, and if the name is followed by a
9532 template-argument-list, the reference refers to the class template
9533 itself and not a specialization thereof, and is not ambiguous. */
9534 if (TREE_CODE (decl) == TREE_LIST)
9535 {
9536 tree t, tmpl = NULL_TREE;
9537 for (t = decl; t; t = TREE_CHAIN (t))
9538 {
9539 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9540 if (!tmpl)
9541 tmpl = elt;
9542 else if (tmpl != elt)
9543 break;
9544 }
9545 if (tmpl && t == NULL_TREE)
9546 return tmpl;
9547 else
9548 return decl;
9549 }
9550
9551 return (decl != NULL_TREE
9552 && DECL_SELF_REFERENCE_P (decl)
9553 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9554 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9555 }
9556
9557 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9558 parameters, find the desired type.
9559
9560 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9561
9562 IN_DECL, if non-NULL, is the template declaration we are trying to
9563 instantiate.
9564
9565 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9566 the class we are looking up.
9567
9568 Issue error and warning messages under control of COMPLAIN.
9569
9570 If the template class is really a local class in a template
9571 function, then the FUNCTION_CONTEXT is the function in which it is
9572 being instantiated.
9573
9574 ??? Note that this function is currently called *twice* for each
9575 template-id: the first time from the parser, while creating the
9576 incomplete type (finish_template_type), and the second type during the
9577 real instantiation (instantiate_template_class). This is surely something
9578 that we want to avoid. It also causes some problems with argument
9579 coercion (see convert_nontype_argument for more information on this). */
9580
9581 static tree
9582 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9583 int entering_scope, tsubst_flags_t complain)
9584 {
9585 tree templ = NULL_TREE, parmlist;
9586 tree t;
9587 spec_entry **slot;
9588 spec_entry *entry;
9589 spec_entry elt;
9590 hashval_t hash;
9591
9592 if (identifier_p (d1))
9593 {
9594 tree value = innermost_non_namespace_value (d1);
9595 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9596 templ = value;
9597 else
9598 {
9599 if (context)
9600 push_decl_namespace (context);
9601 templ = lookup_name (d1);
9602 templ = maybe_get_template_decl_from_type_decl (templ);
9603 if (context)
9604 pop_decl_namespace ();
9605 }
9606 if (templ)
9607 context = DECL_CONTEXT (templ);
9608 }
9609 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9610 {
9611 tree type = TREE_TYPE (d1);
9612
9613 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9614 an implicit typename for the second A. Deal with it. */
9615 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9616 type = TREE_TYPE (type);
9617
9618 if (CLASSTYPE_TEMPLATE_INFO (type))
9619 {
9620 templ = CLASSTYPE_TI_TEMPLATE (type);
9621 d1 = DECL_NAME (templ);
9622 }
9623 }
9624 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9625 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9626 {
9627 templ = TYPE_TI_TEMPLATE (d1);
9628 d1 = DECL_NAME (templ);
9629 }
9630 else if (DECL_TYPE_TEMPLATE_P (d1))
9631 {
9632 templ = d1;
9633 d1 = DECL_NAME (templ);
9634 context = DECL_CONTEXT (templ);
9635 }
9636 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9637 {
9638 templ = d1;
9639 d1 = DECL_NAME (templ);
9640 }
9641
9642 /* Issue an error message if we didn't find a template. */
9643 if (! templ)
9644 {
9645 if (complain & tf_error)
9646 error ("%qT is not a template", d1);
9647 return error_mark_node;
9648 }
9649
9650 if (TREE_CODE (templ) != TEMPLATE_DECL
9651 /* Make sure it's a user visible template, if it was named by
9652 the user. */
9653 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9654 && !PRIMARY_TEMPLATE_P (templ)))
9655 {
9656 if (complain & tf_error)
9657 {
9658 error ("non-template type %qT used as a template", d1);
9659 if (in_decl)
9660 error ("for template declaration %q+D", in_decl);
9661 }
9662 return error_mark_node;
9663 }
9664
9665 complain &= ~tf_user;
9666
9667 /* An alias that just changes the name of a template is equivalent to the
9668 other template, so if any of the arguments are pack expansions, strip
9669 the alias to avoid problems with a pack expansion passed to a non-pack
9670 alias template parameter (DR 1430). */
9671 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9672 templ = get_underlying_template (templ);
9673
9674 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9675 {
9676 tree parm;
9677 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9678 if (arglist2 == error_mark_node
9679 || (!uses_template_parms (arglist2)
9680 && check_instantiated_args (templ, arglist2, complain)))
9681 return error_mark_node;
9682
9683 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9684 return parm;
9685 }
9686 else
9687 {
9688 tree template_type = TREE_TYPE (templ);
9689 tree gen_tmpl;
9690 tree type_decl;
9691 tree found = NULL_TREE;
9692 int arg_depth;
9693 int parm_depth;
9694 int is_dependent_type;
9695 int use_partial_inst_tmpl = false;
9696
9697 if (template_type == error_mark_node)
9698 /* An error occurred while building the template TEMPL, and a
9699 diagnostic has most certainly been emitted for that
9700 already. Let's propagate that error. */
9701 return error_mark_node;
9702
9703 gen_tmpl = most_general_template (templ);
9704 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9705 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9706 arg_depth = TMPL_ARGS_DEPTH (arglist);
9707
9708 if (arg_depth == 1 && parm_depth > 1)
9709 {
9710 /* We've been given an incomplete set of template arguments.
9711 For example, given:
9712
9713 template <class T> struct S1 {
9714 template <class U> struct S2 {};
9715 template <class U> struct S2<U*> {};
9716 };
9717
9718 we will be called with an ARGLIST of `U*', but the
9719 TEMPLATE will be `template <class T> template
9720 <class U> struct S1<T>::S2'. We must fill in the missing
9721 arguments. */
9722 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9723 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9724 arg_depth = TMPL_ARGS_DEPTH (arglist);
9725 }
9726
9727 /* Now we should have enough arguments. */
9728 gcc_assert (parm_depth == arg_depth);
9729
9730 /* From here on, we're only interested in the most general
9731 template. */
9732
9733 /* Calculate the BOUND_ARGS. These will be the args that are
9734 actually tsubst'd into the definition to create the
9735 instantiation. */
9736 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9737 complain,
9738 /*require_all_args=*/true,
9739 /*use_default_args=*/true);
9740
9741 if (arglist == error_mark_node)
9742 /* We were unable to bind the arguments. */
9743 return error_mark_node;
9744
9745 /* In the scope of a template class, explicit references to the
9746 template class refer to the type of the template, not any
9747 instantiation of it. For example, in:
9748
9749 template <class T> class C { void f(C<T>); }
9750
9751 the `C<T>' is just the same as `C'. Outside of the
9752 class, however, such a reference is an instantiation. */
9753 if (entering_scope
9754 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9755 || currently_open_class (template_type))
9756 {
9757 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9758
9759 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9760 return template_type;
9761 }
9762
9763 /* If we already have this specialization, return it. */
9764 elt.tmpl = gen_tmpl;
9765 elt.args = arglist;
9766 elt.spec = NULL_TREE;
9767 hash = spec_hasher::hash (&elt);
9768 entry = type_specializations->find_with_hash (&elt, hash);
9769
9770 if (entry)
9771 return entry->spec;
9772
9773 /* If the template's constraints are not satisfied,
9774 then we cannot form a valid type.
9775
9776 Note that the check is deferred until after the hash
9777 lookup. This prevents redundant checks on previously
9778 instantiated specializations. */
9779 if (flag_concepts
9780 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9781 && !constraints_satisfied_p (gen_tmpl, arglist))
9782 {
9783 if (complain & tf_error)
9784 {
9785 auto_diagnostic_group d;
9786 error ("template constraint failure for %qD", gen_tmpl);
9787 diagnose_constraints (input_location, gen_tmpl, arglist);
9788 }
9789 return error_mark_node;
9790 }
9791
9792 is_dependent_type = uses_template_parms (arglist);
9793
9794 /* If the deduced arguments are invalid, then the binding
9795 failed. */
9796 if (!is_dependent_type
9797 && check_instantiated_args (gen_tmpl,
9798 INNERMOST_TEMPLATE_ARGS (arglist),
9799 complain))
9800 return error_mark_node;
9801
9802 if (!is_dependent_type
9803 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9804 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9805 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9806 {
9807 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9808 DECL_NAME (gen_tmpl),
9809 /*tag_scope=*/ts_global);
9810 return found;
9811 }
9812
9813 context = DECL_CONTEXT (gen_tmpl);
9814 if (context && TYPE_P (context))
9815 {
9816 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9817 context = complete_type (context);
9818 }
9819 else
9820 context = tsubst (context, arglist, complain, in_decl);
9821
9822 if (context == error_mark_node)
9823 return error_mark_node;
9824
9825 if (!context)
9826 context = global_namespace;
9827
9828 /* Create the type. */
9829 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9830 {
9831 /* The user referred to a specialization of an alias
9832 template represented by GEN_TMPL.
9833
9834 [temp.alias]/2 says:
9835
9836 When a template-id refers to the specialization of an
9837 alias template, it is equivalent to the associated
9838 type obtained by substitution of its
9839 template-arguments for the template-parameters in the
9840 type-id of the alias template. */
9841
9842 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9843 /* Note that the call above (by indirectly calling
9844 register_specialization in tsubst_decl) registers the
9845 TYPE_DECL representing the specialization of the alias
9846 template. So next time someone substitutes ARGLIST for
9847 the template parms into the alias template (GEN_TMPL),
9848 she'll get that TYPE_DECL back. */
9849
9850 if (t == error_mark_node)
9851 return t;
9852 }
9853 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9854 {
9855 if (!is_dependent_type)
9856 {
9857 set_current_access_from_decl (TYPE_NAME (template_type));
9858 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9859 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9860 arglist, complain, in_decl),
9861 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9862 arglist, complain, in_decl),
9863 SCOPED_ENUM_P (template_type), NULL);
9864
9865 if (t == error_mark_node)
9866 return t;
9867 }
9868 else
9869 {
9870 /* We don't want to call start_enum for this type, since
9871 the values for the enumeration constants may involve
9872 template parameters. And, no one should be interested
9873 in the enumeration constants for such a type. */
9874 t = cxx_make_type (ENUMERAL_TYPE);
9875 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9876 }
9877 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9878 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9879 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9880 }
9881 else if (CLASS_TYPE_P (template_type))
9882 {
9883 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9884 instantiated here. */
9885 gcc_assert (!LAMBDA_TYPE_P (template_type));
9886
9887 t = make_class_type (TREE_CODE (template_type));
9888 CLASSTYPE_DECLARED_CLASS (t)
9889 = CLASSTYPE_DECLARED_CLASS (template_type);
9890 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9891
9892 /* A local class. Make sure the decl gets registered properly. */
9893 if (context == current_function_decl)
9894 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9895 == error_mark_node)
9896 return error_mark_node;
9897
9898 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9899 /* This instantiation is another name for the primary
9900 template type. Set the TYPE_CANONICAL field
9901 appropriately. */
9902 TYPE_CANONICAL (t) = template_type;
9903 else if (any_template_arguments_need_structural_equality_p (arglist))
9904 /* Some of the template arguments require structural
9905 equality testing, so this template class requires
9906 structural equality testing. */
9907 SET_TYPE_STRUCTURAL_EQUALITY (t);
9908 }
9909 else
9910 gcc_unreachable ();
9911
9912 /* If we called start_enum or pushtag above, this information
9913 will already be set up. */
9914 if (!TYPE_NAME (t))
9915 {
9916 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9917
9918 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9919 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9920 DECL_SOURCE_LOCATION (type_decl)
9921 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9922 }
9923 else
9924 type_decl = TYPE_NAME (t);
9925
9926 if (CLASS_TYPE_P (template_type))
9927 {
9928 TREE_PRIVATE (type_decl)
9929 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9930 TREE_PROTECTED (type_decl)
9931 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9932 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9933 {
9934 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9935 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9936 }
9937 }
9938
9939 if (OVERLOAD_TYPE_P (t)
9940 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9941 {
9942 static const char *tags[] = {"abi_tag", "may_alias"};
9943
9944 for (unsigned ix = 0; ix != 2; ix++)
9945 {
9946 tree attributes
9947 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9948
9949 if (attributes)
9950 TYPE_ATTRIBUTES (t)
9951 = tree_cons (TREE_PURPOSE (attributes),
9952 TREE_VALUE (attributes),
9953 TYPE_ATTRIBUTES (t));
9954 }
9955 }
9956
9957 /* Let's consider the explicit specialization of a member
9958 of a class template specialization that is implicitly instantiated,
9959 e.g.:
9960 template<class T>
9961 struct S
9962 {
9963 template<class U> struct M {}; //#0
9964 };
9965
9966 template<>
9967 template<>
9968 struct S<int>::M<char> //#1
9969 {
9970 int i;
9971 };
9972 [temp.expl.spec]/4 says this is valid.
9973
9974 In this case, when we write:
9975 S<int>::M<char> m;
9976
9977 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9978 the one of #0.
9979
9980 When we encounter #1, we want to store the partial instantiation
9981 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9982
9983 For all cases other than this "explicit specialization of member of a
9984 class template", we just want to store the most general template into
9985 the CLASSTYPE_TI_TEMPLATE of M.
9986
9987 This case of "explicit specialization of member of a class template"
9988 only happens when:
9989 1/ the enclosing class is an instantiation of, and therefore not
9990 the same as, the context of the most general template, and
9991 2/ we aren't looking at the partial instantiation itself, i.e.
9992 the innermost arguments are not the same as the innermost parms of
9993 the most general template.
9994
9995 So it's only when 1/ and 2/ happens that we want to use the partial
9996 instantiation of the member template in lieu of its most general
9997 template. */
9998
9999 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10000 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10001 /* the enclosing class must be an instantiation... */
10002 && CLASS_TYPE_P (context)
10003 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10004 {
10005 TREE_VEC_LENGTH (arglist)--;
10006 ++processing_template_decl;
10007 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10008 tree partial_inst_args =
10009 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10010 arglist, complain, NULL_TREE);
10011 --processing_template_decl;
10012 TREE_VEC_LENGTH (arglist)++;
10013 if (partial_inst_args == error_mark_node)
10014 return error_mark_node;
10015 use_partial_inst_tmpl =
10016 /*...and we must not be looking at the partial instantiation
10017 itself. */
10018 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10019 partial_inst_args);
10020 }
10021
10022 if (!use_partial_inst_tmpl)
10023 /* This case is easy; there are no member templates involved. */
10024 found = gen_tmpl;
10025 else
10026 {
10027 /* This is a full instantiation of a member template. Find
10028 the partial instantiation of which this is an instance. */
10029
10030 /* Temporarily reduce by one the number of levels in the ARGLIST
10031 so as to avoid comparing the last set of arguments. */
10032 TREE_VEC_LENGTH (arglist)--;
10033 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
10034 TREE_VEC_LENGTH (arglist)++;
10035 /* FOUND is either a proper class type, or an alias
10036 template specialization. In the later case, it's a
10037 TYPE_DECL, resulting from the substituting of arguments
10038 for parameters in the TYPE_DECL of the alias template
10039 done earlier. So be careful while getting the template
10040 of FOUND. */
10041 found = (TREE_CODE (found) == TEMPLATE_DECL
10042 ? found
10043 : (TREE_CODE (found) == TYPE_DECL
10044 ? DECL_TI_TEMPLATE (found)
10045 : CLASSTYPE_TI_TEMPLATE (found)));
10046
10047 if (DECL_CLASS_TEMPLATE_P (found)
10048 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10049 {
10050 /* If this partial instantiation is specialized, we want to
10051 use it for hash table lookup. */
10052 elt.tmpl = found;
10053 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10054 hash = spec_hasher::hash (&elt);
10055 }
10056 }
10057
10058 // Build template info for the new specialization.
10059 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10060
10061 elt.spec = t;
10062 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10063 gcc_checking_assert (*slot == NULL);
10064 entry = ggc_alloc<spec_entry> ();
10065 *entry = elt;
10066 *slot = entry;
10067
10068 /* Note this use of the partial instantiation so we can check it
10069 later in maybe_process_partial_specialization. */
10070 DECL_TEMPLATE_INSTANTIATIONS (found)
10071 = tree_cons (arglist, t,
10072 DECL_TEMPLATE_INSTANTIATIONS (found));
10073
10074 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10075 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10076 /* Now that the type has been registered on the instantiations
10077 list, we set up the enumerators. Because the enumeration
10078 constants may involve the enumeration type itself, we make
10079 sure to register the type first, and then create the
10080 constants. That way, doing tsubst_expr for the enumeration
10081 constants won't result in recursive calls here; we'll find
10082 the instantiation and exit above. */
10083 tsubst_enum (template_type, t, arglist);
10084
10085 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10086 /* If the type makes use of template parameters, the
10087 code that generates debugging information will crash. */
10088 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10089
10090 /* Possibly limit visibility based on template args. */
10091 TREE_PUBLIC (type_decl) = 1;
10092 determine_visibility (type_decl);
10093
10094 inherit_targ_abi_tags (t);
10095
10096 return t;
10097 }
10098 }
10099
10100 /* Wrapper for lookup_template_class_1. */
10101
10102 tree
10103 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10104 int entering_scope, tsubst_flags_t complain)
10105 {
10106 tree ret;
10107 timevar_push (TV_TEMPLATE_INST);
10108 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10109 entering_scope, complain);
10110 timevar_pop (TV_TEMPLATE_INST);
10111 return ret;
10112 }
10113
10114 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10115
10116 tree
10117 lookup_template_variable (tree templ, tree arglist)
10118 {
10119 if (flag_concepts && variable_concept_p (templ))
10120 return build_concept_check (templ, arglist, tf_none);
10121
10122 /* The type of the expression is NULL_TREE since the template-id could refer
10123 to an explicit or partial specialization. */
10124 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10125 }
10126
10127 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10128
10129 tree
10130 finish_template_variable (tree var, tsubst_flags_t complain)
10131 {
10132 tree templ = TREE_OPERAND (var, 0);
10133 tree arglist = TREE_OPERAND (var, 1);
10134
10135 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10136 arglist = add_outermost_template_args (tmpl_args, arglist);
10137
10138 templ = most_general_template (templ);
10139 tree parms = DECL_TEMPLATE_PARMS (templ);
10140 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10141 /*req_all*/true,
10142 /*use_default*/true);
10143
10144 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10145 {
10146 if (complain & tf_error)
10147 {
10148 auto_diagnostic_group d;
10149 error ("use of invalid variable template %qE", var);
10150 diagnose_constraints (location_of (var), templ, arglist);
10151 }
10152 return error_mark_node;
10153 }
10154
10155 return instantiate_template (templ, arglist, complain);
10156 }
10157
10158 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10159 TARGS template args, and instantiate it if it's not dependent. */
10160
10161 tree
10162 lookup_and_finish_template_variable (tree templ, tree targs,
10163 tsubst_flags_t complain)
10164 {
10165 templ = lookup_template_variable (templ, targs);
10166 if (!any_dependent_template_arguments_p (targs))
10167 {
10168 templ = finish_template_variable (templ, complain);
10169 mark_used (templ);
10170 }
10171
10172 return convert_from_reference (templ);
10173 }
10174
10175 \f
10176 struct pair_fn_data
10177 {
10178 tree_fn_t fn;
10179 tree_fn_t any_fn;
10180 void *data;
10181 /* True when we should also visit template parameters that occur in
10182 non-deduced contexts. */
10183 bool include_nondeduced_p;
10184 hash_set<tree> *visited;
10185 };
10186
10187 /* Called from for_each_template_parm via walk_tree. */
10188
10189 static tree
10190 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10191 {
10192 tree t = *tp;
10193 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10194 tree_fn_t fn = pfd->fn;
10195 void *data = pfd->data;
10196 tree result = NULL_TREE;
10197
10198 #define WALK_SUBTREE(NODE) \
10199 do \
10200 { \
10201 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10202 pfd->include_nondeduced_p, \
10203 pfd->any_fn); \
10204 if (result) goto out; \
10205 } \
10206 while (0)
10207
10208 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10209 return t;
10210
10211 if (TYPE_P (t)
10212 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10213 WALK_SUBTREE (TYPE_CONTEXT (t));
10214
10215 switch (TREE_CODE (t))
10216 {
10217 case RECORD_TYPE:
10218 if (TYPE_PTRMEMFUNC_P (t))
10219 break;
10220 /* Fall through. */
10221
10222 case UNION_TYPE:
10223 case ENUMERAL_TYPE:
10224 if (!TYPE_TEMPLATE_INFO (t))
10225 *walk_subtrees = 0;
10226 else
10227 WALK_SUBTREE (TYPE_TI_ARGS (t));
10228 break;
10229
10230 case INTEGER_TYPE:
10231 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10232 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10233 break;
10234
10235 case METHOD_TYPE:
10236 /* Since we're not going to walk subtrees, we have to do this
10237 explicitly here. */
10238 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10239 /* Fall through. */
10240
10241 case FUNCTION_TYPE:
10242 /* Check the return type. */
10243 WALK_SUBTREE (TREE_TYPE (t));
10244
10245 /* Check the parameter types. Since default arguments are not
10246 instantiated until they are needed, the TYPE_ARG_TYPES may
10247 contain expressions that involve template parameters. But,
10248 no-one should be looking at them yet. And, once they're
10249 instantiated, they don't contain template parameters, so
10250 there's no point in looking at them then, either. */
10251 {
10252 tree parm;
10253
10254 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10255 WALK_SUBTREE (TREE_VALUE (parm));
10256
10257 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10258 want walk_tree walking into them itself. */
10259 *walk_subtrees = 0;
10260 }
10261
10262 if (flag_noexcept_type)
10263 {
10264 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10265 if (spec)
10266 WALK_SUBTREE (TREE_PURPOSE (spec));
10267 }
10268 break;
10269
10270 case TYPEOF_TYPE:
10271 case DECLTYPE_TYPE:
10272 case UNDERLYING_TYPE:
10273 if (pfd->include_nondeduced_p
10274 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10275 pfd->visited,
10276 pfd->include_nondeduced_p,
10277 pfd->any_fn))
10278 return error_mark_node;
10279 *walk_subtrees = false;
10280 break;
10281
10282 case FUNCTION_DECL:
10283 case VAR_DECL:
10284 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10285 WALK_SUBTREE (DECL_TI_ARGS (t));
10286 /* Fall through. */
10287
10288 case PARM_DECL:
10289 case CONST_DECL:
10290 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10291 WALK_SUBTREE (DECL_INITIAL (t));
10292 if (DECL_CONTEXT (t)
10293 && pfd->include_nondeduced_p)
10294 WALK_SUBTREE (DECL_CONTEXT (t));
10295 break;
10296
10297 case BOUND_TEMPLATE_TEMPLATE_PARM:
10298 /* Record template parameters such as `T' inside `TT<T>'. */
10299 WALK_SUBTREE (TYPE_TI_ARGS (t));
10300 /* Fall through. */
10301
10302 case TEMPLATE_TEMPLATE_PARM:
10303 case TEMPLATE_TYPE_PARM:
10304 case TEMPLATE_PARM_INDEX:
10305 if (fn && (*fn)(t, data))
10306 return t;
10307 else if (!fn)
10308 return t;
10309 break;
10310
10311 case TEMPLATE_DECL:
10312 /* A template template parameter is encountered. */
10313 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10314 WALK_SUBTREE (TREE_TYPE (t));
10315
10316 /* Already substituted template template parameter */
10317 *walk_subtrees = 0;
10318 break;
10319
10320 case TYPENAME_TYPE:
10321 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10322 partial instantiation. */
10323 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10324 break;
10325
10326 case CONSTRUCTOR:
10327 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10328 && pfd->include_nondeduced_p)
10329 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10330 break;
10331
10332 case INDIRECT_REF:
10333 case COMPONENT_REF:
10334 /* If there's no type, then this thing must be some expression
10335 involving template parameters. */
10336 if (!fn && !TREE_TYPE (t))
10337 return error_mark_node;
10338 break;
10339
10340 case MODOP_EXPR:
10341 case CAST_EXPR:
10342 case IMPLICIT_CONV_EXPR:
10343 case REINTERPRET_CAST_EXPR:
10344 case CONST_CAST_EXPR:
10345 case STATIC_CAST_EXPR:
10346 case DYNAMIC_CAST_EXPR:
10347 case ARROW_EXPR:
10348 case DOTSTAR_EXPR:
10349 case TYPEID_EXPR:
10350 case PSEUDO_DTOR_EXPR:
10351 if (!fn)
10352 return error_mark_node;
10353 break;
10354
10355 case SCOPE_REF:
10356 if (pfd->include_nondeduced_p)
10357 WALK_SUBTREE (TREE_OPERAND (t, 0));
10358 break;
10359
10360 case REQUIRES_EXPR:
10361 {
10362 if (!fn)
10363 return error_mark_node;
10364
10365 /* Recursively walk the type of each constraint variable. */
10366 tree p = TREE_OPERAND (t, 0);
10367 while (p)
10368 {
10369 WALK_SUBTREE (TREE_TYPE (p));
10370 p = TREE_CHAIN (p);
10371 }
10372 }
10373 break;
10374
10375 default:
10376 break;
10377 }
10378
10379 #undef WALK_SUBTREE
10380
10381 /* We didn't find any template parameters we liked. */
10382 out:
10383 return result;
10384 }
10385
10386 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10387 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10388 call FN with the parameter and the DATA.
10389 If FN returns nonzero, the iteration is terminated, and
10390 for_each_template_parm returns 1. Otherwise, the iteration
10391 continues. If FN never returns a nonzero value, the value
10392 returned by for_each_template_parm is 0. If FN is NULL, it is
10393 considered to be the function which always returns 1.
10394
10395 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10396 parameters that occur in non-deduced contexts. When false, only
10397 visits those template parameters that can be deduced. */
10398
10399 static tree
10400 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10401 hash_set<tree> *visited,
10402 bool include_nondeduced_p,
10403 tree_fn_t any_fn)
10404 {
10405 struct pair_fn_data pfd;
10406 tree result;
10407
10408 /* Set up. */
10409 pfd.fn = fn;
10410 pfd.any_fn = any_fn;
10411 pfd.data = data;
10412 pfd.include_nondeduced_p = include_nondeduced_p;
10413
10414 /* Walk the tree. (Conceptually, we would like to walk without
10415 duplicates, but for_each_template_parm_r recursively calls
10416 for_each_template_parm, so we would need to reorganize a fair
10417 bit to use walk_tree_without_duplicates, so we keep our own
10418 visited list.) */
10419 if (visited)
10420 pfd.visited = visited;
10421 else
10422 pfd.visited = new hash_set<tree>;
10423 result = cp_walk_tree (&t,
10424 for_each_template_parm_r,
10425 &pfd,
10426 pfd.visited);
10427
10428 /* Clean up. */
10429 if (!visited)
10430 {
10431 delete pfd.visited;
10432 pfd.visited = 0;
10433 }
10434
10435 return result;
10436 }
10437
10438 struct find_template_parameter_info
10439 {
10440 explicit find_template_parameter_info (tree ctx_parms)
10441 : parm_list (NULL_TREE),
10442 ctx_parms (ctx_parms),
10443 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10444 {}
10445
10446 hash_set<tree> visited;
10447 hash_set<tree> parms;
10448 tree parm_list;
10449 tree ctx_parms;
10450 int max_depth;
10451 };
10452
10453 /* Appends the declaration of T to the list in DATA. */
10454
10455 static int
10456 keep_template_parm (tree t, void* data)
10457 {
10458 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10459
10460 /* Template parameters declared within the expression are not part of
10461 the parameter mapping. For example, in this concept:
10462
10463 template<typename T>
10464 concept C = requires { <expr> } -> same_as<int>;
10465
10466 the return specifier same_as<int> declares a new decltype parameter
10467 that must not be part of the parameter mapping. The same is true
10468 for generic lambda parameters, lambda template parameters, etc. */
10469 int level;
10470 int index;
10471 template_parm_level_and_index (t, &level, &index);
10472 if (level > ftpi->max_depth)
10473 return 0;
10474
10475 /* Arguments like const T yield parameters like const T. This means that
10476 a template-id like X<T, const T> would yield two distinct parameters:
10477 T and const T. Adjust types to their unqualified versions. */
10478 if (TYPE_P (t))
10479 t = TYPE_MAIN_VARIANT (t);
10480 if (!ftpi->parms.add (t))
10481 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10482
10483 return 0;
10484 }
10485
10486 /* Ensure that we recursively examine certain terms that are not normally
10487 visited in for_each_template_parm_r. */
10488
10489 static int
10490 any_template_parm_r (tree t, void *data)
10491 {
10492 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10493
10494 #define WALK_SUBTREE(NODE) \
10495 do \
10496 { \
10497 for_each_template_parm (NODE, keep_template_parm, data, \
10498 &ftpi->visited, true, \
10499 any_template_parm_r); \
10500 } \
10501 while (0)
10502
10503 /* A mention of a member alias/typedef is a use of all of its template
10504 arguments, including those from the enclosing class, so we don't use
10505 alias_template_specialization_p here. */
10506 if (TYPE_P (t) && typedef_variant_p (t))
10507 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10508 WALK_SUBTREE (TI_ARGS (tinfo));
10509
10510 switch (TREE_CODE (t))
10511 {
10512 case TEMPLATE_TYPE_PARM:
10513 /* Type constraints of a placeholder type may contain parameters. */
10514 if (is_auto (t))
10515 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10516 WALK_SUBTREE (constr);
10517 break;
10518
10519 case TEMPLATE_ID_EXPR:
10520 /* Search through references to variable templates. */
10521 WALK_SUBTREE (TREE_OPERAND (t, 0));
10522 WALK_SUBTREE (TREE_OPERAND (t, 1));
10523 break;
10524
10525 case TEMPLATE_PARM_INDEX:
10526 case PARM_DECL:
10527 /* A parameter or constraint variable may also depend on a template
10528 parameter without explicitly naming it. */
10529 WALK_SUBTREE (TREE_TYPE (t));
10530 break;
10531
10532 case TEMPLATE_DECL:
10533 {
10534 /* If T is a member template that shares template parameters with
10535 ctx_parms, we need to mark all those parameters for mapping. */
10536 tree dparms = DECL_TEMPLATE_PARMS (t);
10537 tree cparms = ftpi->ctx_parms;
10538 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10539 dparms = TREE_CHAIN (dparms);
10540 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10541 cparms = TREE_CHAIN (cparms);
10542 while (dparms
10543 && (TREE_TYPE (TREE_VALUE (dparms))
10544 != TREE_TYPE (TREE_VALUE (cparms))))
10545 dparms = TREE_CHAIN (dparms),
10546 cparms = TREE_CHAIN (cparms);
10547 if (dparms)
10548 {
10549 int ddepth = TMPL_PARMS_DEPTH (dparms);
10550 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10551 for (int i = 0; i < ddepth; ++i)
10552 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10553 }
10554 }
10555 break;
10556
10557 case LAMBDA_EXPR:
10558 {
10559 /* Look in the parms and body. */
10560 tree fn = lambda_function (t);
10561 WALK_SUBTREE (TREE_TYPE (fn));
10562 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10563 }
10564 break;
10565
10566 case IDENTIFIER_NODE:
10567 if (IDENTIFIER_CONV_OP_P (t))
10568 /* The conversion-type-id of a conversion operator may be dependent. */
10569 WALK_SUBTREE (TREE_TYPE (t));
10570 break;
10571
10572 default:
10573 break;
10574 }
10575
10576 /* Keep walking. */
10577 return 0;
10578 }
10579
10580 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10581 are the template parameters in scope. */
10582
10583 tree
10584 find_template_parameters (tree t, tree ctx_parms)
10585 {
10586 if (!ctx_parms)
10587 return NULL_TREE;
10588
10589 find_template_parameter_info ftpi (ctx_parms);
10590 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10591 /*include_nondeduced*/true, any_template_parm_r);
10592 return ftpi.parm_list;
10593 }
10594
10595 /* Returns true if T depends on any template parameter. */
10596
10597 int
10598 uses_template_parms (tree t)
10599 {
10600 if (t == NULL_TREE)
10601 return false;
10602
10603 bool dependent_p;
10604 int saved_processing_template_decl;
10605
10606 saved_processing_template_decl = processing_template_decl;
10607 if (!saved_processing_template_decl)
10608 processing_template_decl = 1;
10609 if (TYPE_P (t))
10610 dependent_p = dependent_type_p (t);
10611 else if (TREE_CODE (t) == TREE_VEC)
10612 dependent_p = any_dependent_template_arguments_p (t);
10613 else if (TREE_CODE (t) == TREE_LIST)
10614 dependent_p = (uses_template_parms (TREE_VALUE (t))
10615 || uses_template_parms (TREE_CHAIN (t)));
10616 else if (TREE_CODE (t) == TYPE_DECL)
10617 dependent_p = dependent_type_p (TREE_TYPE (t));
10618 else if (t == error_mark_node)
10619 dependent_p = false;
10620 else
10621 dependent_p = value_dependent_expression_p (t);
10622
10623 processing_template_decl = saved_processing_template_decl;
10624
10625 return dependent_p;
10626 }
10627
10628 /* Returns true iff current_function_decl is an incompletely instantiated
10629 template. Useful instead of processing_template_decl because the latter
10630 is set to 0 during instantiate_non_dependent_expr. */
10631
10632 bool
10633 in_template_function (void)
10634 {
10635 tree fn = current_function_decl;
10636 bool ret;
10637 ++processing_template_decl;
10638 ret = (fn && DECL_LANG_SPECIFIC (fn)
10639 && DECL_TEMPLATE_INFO (fn)
10640 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10641 --processing_template_decl;
10642 return ret;
10643 }
10644
10645 /* Returns true if T depends on any template parameter with level LEVEL. */
10646
10647 bool
10648 uses_template_parms_level (tree t, int level)
10649 {
10650 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10651 /*include_nondeduced_p=*/true);
10652 }
10653
10654 /* Returns true if the signature of DECL depends on any template parameter from
10655 its enclosing class. */
10656
10657 bool
10658 uses_outer_template_parms (tree decl)
10659 {
10660 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10661 if (depth == 0)
10662 return false;
10663 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10664 &depth, NULL, /*include_nondeduced_p=*/true))
10665 return true;
10666 if (PRIMARY_TEMPLATE_P (decl)
10667 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10668 (DECL_TEMPLATE_PARMS (decl)),
10669 template_parm_outer_level,
10670 &depth, NULL, /*include_nondeduced_p=*/true))
10671 return true;
10672 tree ci = get_constraints (decl);
10673 if (ci)
10674 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10675 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10676 &depth, NULL, /*nondeduced*/true))
10677 return true;
10678 return false;
10679 }
10680
10681 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10682 ill-formed translation unit, i.e. a variable or function that isn't
10683 usable in a constant expression. */
10684
10685 static inline bool
10686 neglectable_inst_p (tree d)
10687 {
10688 return (d && DECL_P (d)
10689 && !undeduced_auto_decl (d)
10690 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10691 : decl_maybe_constant_var_p (d)));
10692 }
10693
10694 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10695 neglectable and instantiated from within an erroneous instantiation. */
10696
10697 static bool
10698 limit_bad_template_recursion (tree decl)
10699 {
10700 struct tinst_level *lev = current_tinst_level;
10701 int errs = errorcount + sorrycount;
10702 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10703 return false;
10704
10705 for (; lev; lev = lev->next)
10706 if (neglectable_inst_p (lev->maybe_get_node ()))
10707 break;
10708
10709 return (lev && errs > lev->errors);
10710 }
10711
10712 static int tinst_depth;
10713 extern int max_tinst_depth;
10714 int depth_reached;
10715
10716 static GTY(()) struct tinst_level *last_error_tinst_level;
10717
10718 /* We're starting to instantiate D; record the template instantiation context
10719 at LOC for diagnostics and to restore it later. */
10720
10721 static bool
10722 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10723 {
10724 struct tinst_level *new_level;
10725
10726 if (tinst_depth >= max_tinst_depth)
10727 {
10728 /* Tell error.c not to try to instantiate any templates. */
10729 at_eof = 2;
10730 fatal_error (input_location,
10731 "template instantiation depth exceeds maximum of %d"
10732 " (use %<-ftemplate-depth=%> to increase the maximum)",
10733 max_tinst_depth);
10734 return false;
10735 }
10736
10737 /* If the current instantiation caused problems, don't let it instantiate
10738 anything else. Do allow deduction substitution and decls usable in
10739 constant expressions. */
10740 if (!targs && limit_bad_template_recursion (tldcl))
10741 {
10742 /* Avoid no_linkage_errors and unused function warnings for this
10743 decl. */
10744 TREE_NO_WARNING (tldcl) = 1;
10745 return false;
10746 }
10747
10748 /* When not -quiet, dump template instantiations other than functions, since
10749 announce_function will take care of those. */
10750 if (!quiet_flag && !targs
10751 && TREE_CODE (tldcl) != TREE_LIST
10752 && TREE_CODE (tldcl) != FUNCTION_DECL)
10753 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10754
10755 new_level = tinst_level_freelist ().alloc ();
10756 new_level->tldcl = tldcl;
10757 new_level->targs = targs;
10758 new_level->locus = loc;
10759 new_level->errors = errorcount + sorrycount;
10760 new_level->next = NULL;
10761 new_level->refcount = 0;
10762 set_refcount_ptr (new_level->next, current_tinst_level);
10763 set_refcount_ptr (current_tinst_level, new_level);
10764
10765 ++tinst_depth;
10766 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10767 depth_reached = tinst_depth;
10768
10769 return true;
10770 }
10771
10772 /* We're starting substitution of TMPL<ARGS>; record the template
10773 substitution context for diagnostics and to restore it later. */
10774
10775 static bool
10776 push_tinst_level (tree tmpl, tree args)
10777 {
10778 return push_tinst_level_loc (tmpl, args, input_location);
10779 }
10780
10781 /* We're starting to instantiate D; record INPUT_LOCATION and the
10782 template instantiation context for diagnostics and to restore it
10783 later. */
10784
10785 bool
10786 push_tinst_level (tree d)
10787 {
10788 return push_tinst_level_loc (d, input_location);
10789 }
10790
10791 /* Likewise, but record LOC as the program location. */
10792
10793 bool
10794 push_tinst_level_loc (tree d, location_t loc)
10795 {
10796 gcc_assert (TREE_CODE (d) != TREE_LIST);
10797 return push_tinst_level_loc (d, NULL, loc);
10798 }
10799
10800 /* We're done instantiating this template; return to the instantiation
10801 context. */
10802
10803 void
10804 pop_tinst_level (void)
10805 {
10806 /* Restore the filename and line number stashed away when we started
10807 this instantiation. */
10808 input_location = current_tinst_level->locus;
10809 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10810 --tinst_depth;
10811 }
10812
10813 /* We're instantiating a deferred template; restore the template
10814 instantiation context in which the instantiation was requested, which
10815 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10816
10817 static tree
10818 reopen_tinst_level (struct tinst_level *level)
10819 {
10820 struct tinst_level *t;
10821
10822 tinst_depth = 0;
10823 for (t = level; t; t = t->next)
10824 ++tinst_depth;
10825
10826 set_refcount_ptr (current_tinst_level, level);
10827 pop_tinst_level ();
10828 if (current_tinst_level)
10829 current_tinst_level->errors = errorcount+sorrycount;
10830 return level->maybe_get_node ();
10831 }
10832
10833 /* Returns the TINST_LEVEL which gives the original instantiation
10834 context. */
10835
10836 struct tinst_level *
10837 outermost_tinst_level (void)
10838 {
10839 struct tinst_level *level = current_tinst_level;
10840 if (level)
10841 while (level->next)
10842 level = level->next;
10843 return level;
10844 }
10845
10846 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10847 vector of template arguments, as for tsubst.
10848
10849 Returns an appropriate tsubst'd friend declaration. */
10850
10851 static tree
10852 tsubst_friend_function (tree decl, tree args)
10853 {
10854 tree new_friend;
10855
10856 if (TREE_CODE (decl) == FUNCTION_DECL
10857 && DECL_TEMPLATE_INSTANTIATION (decl)
10858 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10859 /* This was a friend declared with an explicit template
10860 argument list, e.g.:
10861
10862 friend void f<>(T);
10863
10864 to indicate that f was a template instantiation, not a new
10865 function declaration. Now, we have to figure out what
10866 instantiation of what template. */
10867 {
10868 tree template_id, arglist, fns;
10869 tree new_args;
10870 tree tmpl;
10871 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10872
10873 /* Friend functions are looked up in the containing namespace scope.
10874 We must enter that scope, to avoid finding member functions of the
10875 current class with same name. */
10876 push_nested_namespace (ns);
10877 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10878 tf_warning_or_error, NULL_TREE,
10879 /*integral_constant_expression_p=*/false);
10880 pop_nested_namespace (ns);
10881 arglist = tsubst (DECL_TI_ARGS (decl), args,
10882 tf_warning_or_error, NULL_TREE);
10883 template_id = lookup_template_function (fns, arglist);
10884
10885 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10886 tmpl = determine_specialization (template_id, new_friend,
10887 &new_args,
10888 /*need_member_template=*/0,
10889 TREE_VEC_LENGTH (args),
10890 tsk_none);
10891 return instantiate_template (tmpl, new_args, tf_error);
10892 }
10893
10894 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10895
10896 /* The NEW_FRIEND will look like an instantiation, to the
10897 compiler, but is not an instantiation from the point of view of
10898 the language. For example, we might have had:
10899
10900 template <class T> struct S {
10901 template <class U> friend void f(T, U);
10902 };
10903
10904 Then, in S<int>, template <class U> void f(int, U) is not an
10905 instantiation of anything. */
10906 if (new_friend == error_mark_node)
10907 return error_mark_node;
10908
10909 DECL_USE_TEMPLATE (new_friend) = 0;
10910 if (TREE_CODE (decl) == TEMPLATE_DECL)
10911 {
10912 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10913 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10914 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10915
10916 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
10917 match in decls_match. */
10918 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10919 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
10920 treqs = maybe_substitute_reqs_for (treqs, new_friend);
10921 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10922 }
10923
10924 /* The mangled name for the NEW_FRIEND is incorrect. The function
10925 is not a template instantiation and should not be mangled like
10926 one. Therefore, we forget the mangling here; we'll recompute it
10927 later if we need it. */
10928 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10929 {
10930 SET_DECL_RTL (new_friend, NULL);
10931 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10932 }
10933
10934 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10935 {
10936 tree old_decl;
10937 tree new_friend_template_info;
10938 tree new_friend_result_template_info;
10939 tree ns;
10940 int new_friend_is_defn;
10941
10942 /* We must save some information from NEW_FRIEND before calling
10943 duplicate decls since that function will free NEW_FRIEND if
10944 possible. */
10945 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10946 new_friend_is_defn =
10947 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10948 (template_for_substitution (new_friend)))
10949 != NULL_TREE);
10950 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10951 {
10952 /* This declaration is a `primary' template. */
10953 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10954
10955 new_friend_result_template_info
10956 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10957 }
10958 else
10959 new_friend_result_template_info = NULL_TREE;
10960
10961 /* Inside pushdecl_namespace_level, we will push into the
10962 current namespace. However, the friend function should go
10963 into the namespace of the template. */
10964 ns = decl_namespace_context (new_friend);
10965 push_nested_namespace (ns);
10966 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10967 pop_nested_namespace (ns);
10968
10969 if (old_decl == error_mark_node)
10970 return error_mark_node;
10971
10972 if (old_decl != new_friend)
10973 {
10974 /* This new friend declaration matched an existing
10975 declaration. For example, given:
10976
10977 template <class T> void f(T);
10978 template <class U> class C {
10979 template <class T> friend void f(T) {}
10980 };
10981
10982 the friend declaration actually provides the definition
10983 of `f', once C has been instantiated for some type. So,
10984 old_decl will be the out-of-class template declaration,
10985 while new_friend is the in-class definition.
10986
10987 But, if `f' was called before this point, the
10988 instantiation of `f' will have DECL_TI_ARGS corresponding
10989 to `T' but not to `U', references to which might appear
10990 in the definition of `f'. Previously, the most general
10991 template for an instantiation of `f' was the out-of-class
10992 version; now it is the in-class version. Therefore, we
10993 run through all specialization of `f', adding to their
10994 DECL_TI_ARGS appropriately. In particular, they need a
10995 new set of outer arguments, corresponding to the
10996 arguments for this class instantiation.
10997
10998 The same situation can arise with something like this:
10999
11000 friend void f(int);
11001 template <class T> class C {
11002 friend void f(T) {}
11003 };
11004
11005 when `C<int>' is instantiated. Now, `f(int)' is defined
11006 in the class. */
11007
11008 if (!new_friend_is_defn)
11009 /* On the other hand, if the in-class declaration does
11010 *not* provide a definition, then we don't want to alter
11011 existing definitions. We can just leave everything
11012 alone. */
11013 ;
11014 else
11015 {
11016 tree new_template = TI_TEMPLATE (new_friend_template_info);
11017 tree new_args = TI_ARGS (new_friend_template_info);
11018
11019 /* Overwrite whatever template info was there before, if
11020 any, with the new template information pertaining to
11021 the declaration. */
11022 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11023
11024 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11025 {
11026 /* We should have called reregister_specialization in
11027 duplicate_decls. */
11028 gcc_assert (retrieve_specialization (new_template,
11029 new_args, 0)
11030 == old_decl);
11031
11032 /* Instantiate it if the global has already been used. */
11033 if (DECL_ODR_USED (old_decl))
11034 instantiate_decl (old_decl, /*defer_ok=*/true,
11035 /*expl_inst_class_mem_p=*/false);
11036 }
11037 else
11038 {
11039 tree t;
11040
11041 /* Indicate that the old function template is a partial
11042 instantiation. */
11043 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11044 = new_friend_result_template_info;
11045
11046 gcc_assert (new_template
11047 == most_general_template (new_template));
11048 gcc_assert (new_template != old_decl);
11049
11050 /* Reassign any specializations already in the hash table
11051 to the new more general template, and add the
11052 additional template args. */
11053 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11054 t != NULL_TREE;
11055 t = TREE_CHAIN (t))
11056 {
11057 tree spec = TREE_VALUE (t);
11058 spec_entry elt;
11059
11060 elt.tmpl = old_decl;
11061 elt.args = DECL_TI_ARGS (spec);
11062 elt.spec = NULL_TREE;
11063
11064 decl_specializations->remove_elt (&elt);
11065
11066 DECL_TI_ARGS (spec)
11067 = add_outermost_template_args (new_args,
11068 DECL_TI_ARGS (spec));
11069
11070 register_specialization
11071 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11072
11073 }
11074 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11075 }
11076 }
11077
11078 /* The information from NEW_FRIEND has been merged into OLD_DECL
11079 by duplicate_decls. */
11080 new_friend = old_decl;
11081 }
11082 }
11083 else
11084 {
11085 tree context = DECL_CONTEXT (new_friend);
11086 bool dependent_p;
11087
11088 /* In the code
11089 template <class T> class C {
11090 template <class U> friend void C1<U>::f (); // case 1
11091 friend void C2<T>::f (); // case 2
11092 };
11093 we only need to make sure CONTEXT is a complete type for
11094 case 2. To distinguish between the two cases, we note that
11095 CONTEXT of case 1 remains dependent type after tsubst while
11096 this isn't true for case 2. */
11097 ++processing_template_decl;
11098 dependent_p = dependent_type_p (context);
11099 --processing_template_decl;
11100
11101 if (!dependent_p
11102 && !complete_type_or_else (context, NULL_TREE))
11103 return error_mark_node;
11104
11105 if (COMPLETE_TYPE_P (context))
11106 {
11107 tree fn = new_friend;
11108 /* do_friend adds the TEMPLATE_DECL for any member friend
11109 template even if it isn't a member template, i.e.
11110 template <class T> friend A<T>::f();
11111 Look through it in that case. */
11112 if (TREE_CODE (fn) == TEMPLATE_DECL
11113 && !PRIMARY_TEMPLATE_P (fn))
11114 fn = DECL_TEMPLATE_RESULT (fn);
11115 /* Check to see that the declaration is really present, and,
11116 possibly obtain an improved declaration. */
11117 fn = check_classfn (context, fn, NULL_TREE);
11118
11119 if (fn)
11120 new_friend = fn;
11121 }
11122 }
11123
11124 return new_friend;
11125 }
11126
11127 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11128 template arguments, as for tsubst.
11129
11130 Returns an appropriate tsubst'd friend type or error_mark_node on
11131 failure. */
11132
11133 static tree
11134 tsubst_friend_class (tree friend_tmpl, tree args)
11135 {
11136 tree tmpl;
11137
11138 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11139 {
11140 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11141 return TREE_TYPE (tmpl);
11142 }
11143
11144 tree context = CP_DECL_CONTEXT (friend_tmpl);
11145 if (TREE_CODE (context) == NAMESPACE_DECL)
11146 push_nested_namespace (context);
11147 else
11148 {
11149 context = tsubst (context, args, tf_error, NULL_TREE);
11150 push_nested_class (context);
11151 }
11152
11153 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
11154 /*non_class=*/false, /*block_p=*/false,
11155 /*namespaces_only=*/false, LOOKUP_HIDDEN);
11156
11157 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11158 {
11159 /* The friend template has already been declared. Just
11160 check to see that the declarations match, and install any new
11161 default parameters. We must tsubst the default parameters,
11162 of course. We only need the innermost template parameters
11163 because that is all that redeclare_class_template will look
11164 at. */
11165 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11166 > TMPL_ARGS_DEPTH (args))
11167 {
11168 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11169 args, tf_warning_or_error);
11170 location_t saved_input_location = input_location;
11171 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11172 tree cons = get_constraints (tmpl);
11173 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11174 input_location = saved_input_location;
11175 }
11176 }
11177 else
11178 {
11179 /* The friend template has not already been declared. In this
11180 case, the instantiation of the template class will cause the
11181 injection of this template into the namespace scope. */
11182 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11183
11184 if (tmpl != error_mark_node)
11185 {
11186 /* The new TMPL is not an instantiation of anything, so we
11187 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11188 for the new type because that is supposed to be the
11189 corresponding template decl, i.e., TMPL. */
11190 DECL_USE_TEMPLATE (tmpl) = 0;
11191 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11192 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11193 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11194 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11195
11196 /* It is hidden. */
11197 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11198 DECL_ANTICIPATED (tmpl)
11199 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11200
11201 /* Inject this template into the enclosing namspace scope. */
11202 tmpl = pushdecl_namespace_level (tmpl, true);
11203 }
11204 }
11205
11206 if (TREE_CODE (context) == NAMESPACE_DECL)
11207 pop_nested_namespace (context);
11208 else
11209 pop_nested_class ();
11210
11211 return TREE_TYPE (tmpl);
11212 }
11213
11214 /* Returns zero if TYPE cannot be completed later due to circularity.
11215 Otherwise returns one. */
11216
11217 static int
11218 can_complete_type_without_circularity (tree type)
11219 {
11220 if (type == NULL_TREE || type == error_mark_node)
11221 return 0;
11222 else if (COMPLETE_TYPE_P (type))
11223 return 1;
11224 else if (TREE_CODE (type) == ARRAY_TYPE)
11225 return can_complete_type_without_circularity (TREE_TYPE (type));
11226 else if (CLASS_TYPE_P (type)
11227 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11228 return 0;
11229 else
11230 return 1;
11231 }
11232
11233 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11234 tsubst_flags_t, tree);
11235
11236 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11237 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11238
11239 static tree
11240 tsubst_attribute (tree t, tree *decl_p, tree args,
11241 tsubst_flags_t complain, tree in_decl)
11242 {
11243 gcc_assert (ATTR_IS_DEPENDENT (t));
11244
11245 tree val = TREE_VALUE (t);
11246 if (val == NULL_TREE)
11247 /* Nothing to do. */;
11248 else if ((flag_openmp || flag_openmp_simd)
11249 && is_attribute_p ("omp declare simd",
11250 get_attribute_name (t)))
11251 {
11252 tree clauses = TREE_VALUE (val);
11253 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11254 complain, in_decl);
11255 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11256 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11257 tree parms = DECL_ARGUMENTS (*decl_p);
11258 clauses
11259 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11260 if (clauses)
11261 val = build_tree_list (NULL_TREE, clauses);
11262 else
11263 val = NULL_TREE;
11264 }
11265 else if (flag_openmp
11266 && is_attribute_p ("omp declare variant base",
11267 get_attribute_name (t)))
11268 {
11269 ++cp_unevaluated_operand;
11270 tree varid
11271 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11272 in_decl, /*integral_constant_expression_p=*/false);
11273 --cp_unevaluated_operand;
11274 tree chain = TREE_CHAIN (val);
11275 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11276 tree ctx = copy_list (TREE_VALUE (val));
11277 tree simd = get_identifier ("simd");
11278 tree score = get_identifier (" score");
11279 tree condition = get_identifier ("condition");
11280 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11281 {
11282 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11283 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11284 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11285 {
11286 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11287 {
11288 tree clauses = TREE_VALUE (t2);
11289 clauses = tsubst_omp_clauses (clauses,
11290 C_ORT_OMP_DECLARE_SIMD, args,
11291 complain, in_decl);
11292 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11293 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11294 TREE_VALUE (t2) = clauses;
11295 }
11296 else
11297 {
11298 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11299 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11300 if (TREE_VALUE (t3))
11301 {
11302 bool allow_string
11303 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11304 && TREE_PURPOSE (t3) != score);
11305 tree v = TREE_VALUE (t3);
11306 if (TREE_CODE (v) == STRING_CST && allow_string)
11307 continue;
11308 v = tsubst_expr (v, args, complain, in_decl, true);
11309 v = fold_non_dependent_expr (v);
11310 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11311 || (TREE_PURPOSE (t3) == score
11312 ? TREE_CODE (v) != INTEGER_CST
11313 : !tree_fits_shwi_p (v)))
11314 {
11315 location_t loc
11316 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11317 match_loc);
11318 if (TREE_PURPOSE (t3) == score)
11319 error_at (loc, "score argument must be "
11320 "constant integer expression");
11321 else if (allow_string)
11322 error_at (loc, "property must be constant "
11323 "integer expression or string "
11324 "literal");
11325 else
11326 error_at (loc, "property must be constant "
11327 "integer expression");
11328 return NULL_TREE;
11329 }
11330 else if (TREE_PURPOSE (t3) == score
11331 && tree_int_cst_sgn (v) < 0)
11332 {
11333 location_t loc
11334 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11335 match_loc);
11336 error_at (loc, "score argument must be "
11337 "non-negative");
11338 return NULL_TREE;
11339 }
11340 TREE_VALUE (t3) = v;
11341 }
11342 }
11343 }
11344 }
11345 val = tree_cons (varid, ctx, chain);
11346 }
11347 /* If the first attribute argument is an identifier, don't
11348 pass it through tsubst. Attributes like mode, format,
11349 cleanup and several target specific attributes expect it
11350 unmodified. */
11351 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11352 {
11353 tree chain
11354 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11355 /*integral_constant_expression_p=*/false);
11356 if (chain != TREE_CHAIN (val))
11357 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11358 }
11359 else if (PACK_EXPANSION_P (val))
11360 {
11361 /* An attribute pack expansion. */
11362 tree purp = TREE_PURPOSE (t);
11363 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11364 if (pack == error_mark_node)
11365 return error_mark_node;
11366 int len = TREE_VEC_LENGTH (pack);
11367 tree list = NULL_TREE;
11368 tree *q = &list;
11369 for (int i = 0; i < len; ++i)
11370 {
11371 tree elt = TREE_VEC_ELT (pack, i);
11372 *q = build_tree_list (purp, elt);
11373 q = &TREE_CHAIN (*q);
11374 }
11375 return list;
11376 }
11377 else
11378 val = tsubst_expr (val, args, complain, in_decl,
11379 /*integral_constant_expression_p=*/false);
11380
11381 if (val != TREE_VALUE (t))
11382 return build_tree_list (TREE_PURPOSE (t), val);
11383 return t;
11384 }
11385
11386 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11387 unchanged or a new TREE_LIST chain. */
11388
11389 static tree
11390 tsubst_attributes (tree attributes, tree args,
11391 tsubst_flags_t complain, tree in_decl)
11392 {
11393 tree last_dep = NULL_TREE;
11394
11395 for (tree t = attributes; t; t = TREE_CHAIN (t))
11396 if (ATTR_IS_DEPENDENT (t))
11397 {
11398 last_dep = t;
11399 attributes = copy_list (attributes);
11400 break;
11401 }
11402
11403 if (last_dep)
11404 for (tree *p = &attributes; *p; )
11405 {
11406 tree t = *p;
11407 if (ATTR_IS_DEPENDENT (t))
11408 {
11409 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11410 if (subst != t)
11411 {
11412 *p = subst;
11413 while (*p)
11414 p = &TREE_CHAIN (*p);
11415 *p = TREE_CHAIN (t);
11416 continue;
11417 }
11418 }
11419 p = &TREE_CHAIN (*p);
11420 }
11421
11422 return attributes;
11423 }
11424
11425 /* Apply any attributes which had to be deferred until instantiation
11426 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11427 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11428
11429 static void
11430 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11431 tree args, tsubst_flags_t complain, tree in_decl)
11432 {
11433 tree last_dep = NULL_TREE;
11434 tree t;
11435 tree *p;
11436
11437 if (attributes == NULL_TREE)
11438 return;
11439
11440 if (DECL_P (*decl_p))
11441 {
11442 if (TREE_TYPE (*decl_p) == error_mark_node)
11443 return;
11444 p = &DECL_ATTRIBUTES (*decl_p);
11445 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11446 to our attributes parameter. */
11447 gcc_assert (*p == attributes);
11448 }
11449 else
11450 {
11451 p = &TYPE_ATTRIBUTES (*decl_p);
11452 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11453 lookup_template_class_1, and should be preserved. */
11454 gcc_assert (*p != attributes);
11455 while (*p)
11456 p = &TREE_CHAIN (*p);
11457 }
11458
11459 for (t = attributes; t; t = TREE_CHAIN (t))
11460 if (ATTR_IS_DEPENDENT (t))
11461 {
11462 last_dep = t;
11463 attributes = copy_list (attributes);
11464 break;
11465 }
11466
11467 *p = attributes;
11468 if (last_dep)
11469 {
11470 tree late_attrs = NULL_TREE;
11471 tree *q = &late_attrs;
11472
11473 for (; *p; )
11474 {
11475 t = *p;
11476 if (ATTR_IS_DEPENDENT (t))
11477 {
11478 *p = TREE_CHAIN (t);
11479 TREE_CHAIN (t) = NULL_TREE;
11480 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11481 while (*q)
11482 q = &TREE_CHAIN (*q);
11483 }
11484 else
11485 p = &TREE_CHAIN (t);
11486 }
11487
11488 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11489 }
11490 }
11491
11492 /* Perform (or defer) access check for typedefs that were referenced
11493 from within the template TMPL code.
11494 This is a subroutine of instantiate_decl and instantiate_class_template.
11495 TMPL is the template to consider and TARGS is the list of arguments of
11496 that template. */
11497
11498 static void
11499 perform_typedefs_access_check (tree tmpl, tree targs)
11500 {
11501 unsigned i;
11502 qualified_typedef_usage_t *iter;
11503
11504 if (!tmpl
11505 || (!CLASS_TYPE_P (tmpl)
11506 && TREE_CODE (tmpl) != FUNCTION_DECL))
11507 return;
11508
11509 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
11510 {
11511 tree type_decl = iter->typedef_decl;
11512 tree type_scope = iter->context;
11513
11514 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
11515 continue;
11516
11517 if (uses_template_parms (type_decl))
11518 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
11519 if (uses_template_parms (type_scope))
11520 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11521
11522 /* Make access check error messages point to the location
11523 of the use of the typedef. */
11524 iloc_sentinel ils (iter->locus);
11525 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11526 type_decl, type_decl,
11527 tf_warning_or_error);
11528 }
11529 }
11530
11531 static tree
11532 instantiate_class_template_1 (tree type)
11533 {
11534 tree templ, args, pattern, t, member;
11535 tree typedecl;
11536 tree pbinfo;
11537 tree base_list;
11538 unsigned int saved_maximum_field_alignment;
11539 tree fn_context;
11540
11541 if (type == error_mark_node)
11542 return error_mark_node;
11543
11544 if (COMPLETE_OR_OPEN_TYPE_P (type)
11545 || uses_template_parms (type))
11546 return type;
11547
11548 /* Figure out which template is being instantiated. */
11549 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11550 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11551
11552 /* Mark the type as in the process of being defined. */
11553 TYPE_BEING_DEFINED (type) = 1;
11554
11555 /* We may be in the middle of deferred access check. Disable
11556 it now. */
11557 deferring_access_check_sentinel acs (dk_no_deferred);
11558
11559 /* Determine what specialization of the original template to
11560 instantiate. */
11561 t = most_specialized_partial_spec (type, tf_warning_or_error);
11562 if (t == error_mark_node)
11563 return error_mark_node;
11564 else if (t)
11565 {
11566 /* This TYPE is actually an instantiation of a partial
11567 specialization. We replace the innermost set of ARGS with
11568 the arguments appropriate for substitution. For example,
11569 given:
11570
11571 template <class T> struct S {};
11572 template <class T> struct S<T*> {};
11573
11574 and supposing that we are instantiating S<int*>, ARGS will
11575 presently be {int*} -- but we need {int}. */
11576 pattern = TREE_TYPE (t);
11577 args = TREE_PURPOSE (t);
11578 }
11579 else
11580 {
11581 pattern = TREE_TYPE (templ);
11582 args = CLASSTYPE_TI_ARGS (type);
11583 }
11584
11585 /* If the template we're instantiating is incomplete, then clearly
11586 there's nothing we can do. */
11587 if (!COMPLETE_TYPE_P (pattern))
11588 {
11589 /* We can try again later. */
11590 TYPE_BEING_DEFINED (type) = 0;
11591 return type;
11592 }
11593
11594 /* If we've recursively instantiated too many templates, stop. */
11595 if (! push_tinst_level (type))
11596 return type;
11597
11598 int saved_unevaluated_operand = cp_unevaluated_operand;
11599 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11600
11601 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11602 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11603 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11604 fn_context = error_mark_node;
11605 if (!fn_context)
11606 push_to_top_level ();
11607 else
11608 {
11609 cp_unevaluated_operand = 0;
11610 c_inhibit_evaluation_warnings = 0;
11611 }
11612 /* Use #pragma pack from the template context. */
11613 saved_maximum_field_alignment = maximum_field_alignment;
11614 maximum_field_alignment = TYPE_PRECISION (pattern);
11615
11616 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11617
11618 /* Set the input location to the most specialized template definition.
11619 This is needed if tsubsting causes an error. */
11620 typedecl = TYPE_MAIN_DECL (pattern);
11621 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11622 DECL_SOURCE_LOCATION (typedecl);
11623
11624 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11625 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11626 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11627 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11628 if (ANON_AGGR_TYPE_P (pattern))
11629 SET_ANON_AGGR_TYPE_P (type);
11630 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11631 {
11632 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11633 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11634 /* Adjust visibility for template arguments. */
11635 determine_visibility (TYPE_MAIN_DECL (type));
11636 }
11637 if (CLASS_TYPE_P (type))
11638 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11639
11640 pbinfo = TYPE_BINFO (pattern);
11641
11642 /* We should never instantiate a nested class before its enclosing
11643 class; we need to look up the nested class by name before we can
11644 instantiate it, and that lookup should instantiate the enclosing
11645 class. */
11646 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11647 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11648
11649 base_list = NULL_TREE;
11650 if (BINFO_N_BASE_BINFOS (pbinfo))
11651 {
11652 tree pbase_binfo;
11653 tree pushed_scope;
11654 int i;
11655
11656 /* We must enter the scope containing the type, as that is where
11657 the accessibility of types named in dependent bases are
11658 looked up from. */
11659 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11660
11661 /* Substitute into each of the bases to determine the actual
11662 basetypes. */
11663 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11664 {
11665 tree base;
11666 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11667 tree expanded_bases = NULL_TREE;
11668 int idx, len = 1;
11669
11670 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11671 {
11672 expanded_bases =
11673 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11674 args, tf_error, NULL_TREE);
11675 if (expanded_bases == error_mark_node)
11676 continue;
11677
11678 len = TREE_VEC_LENGTH (expanded_bases);
11679 }
11680
11681 for (idx = 0; idx < len; idx++)
11682 {
11683 if (expanded_bases)
11684 /* Extract the already-expanded base class. */
11685 base = TREE_VEC_ELT (expanded_bases, idx);
11686 else
11687 /* Substitute to figure out the base class. */
11688 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11689 NULL_TREE);
11690
11691 if (base == error_mark_node)
11692 continue;
11693
11694 base_list = tree_cons (access, base, base_list);
11695 if (BINFO_VIRTUAL_P (pbase_binfo))
11696 TREE_TYPE (base_list) = integer_type_node;
11697 }
11698 }
11699
11700 /* The list is now in reverse order; correct that. */
11701 base_list = nreverse (base_list);
11702
11703 if (pushed_scope)
11704 pop_scope (pushed_scope);
11705 }
11706 /* Now call xref_basetypes to set up all the base-class
11707 information. */
11708 xref_basetypes (type, base_list);
11709
11710 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11711 (int) ATTR_FLAG_TYPE_IN_PLACE,
11712 args, tf_error, NULL_TREE);
11713 fixup_attribute_variants (type);
11714
11715 /* Now that our base classes are set up, enter the scope of the
11716 class, so that name lookups into base classes, etc. will work
11717 correctly. This is precisely analogous to what we do in
11718 begin_class_definition when defining an ordinary non-template
11719 class, except we also need to push the enclosing classes. */
11720 push_nested_class (type);
11721
11722 /* Now members are processed in the order of declaration. */
11723 for (member = CLASSTYPE_DECL_LIST (pattern);
11724 member; member = TREE_CHAIN (member))
11725 {
11726 tree t = TREE_VALUE (member);
11727
11728 if (TREE_PURPOSE (member))
11729 {
11730 if (TYPE_P (t))
11731 {
11732 if (LAMBDA_TYPE_P (t))
11733 /* A closure type for a lambda in an NSDMI or default argument.
11734 Ignore it; it will be regenerated when needed. */
11735 continue;
11736
11737 /* Build new CLASSTYPE_NESTED_UTDS. */
11738
11739 tree newtag;
11740 bool class_template_p;
11741
11742 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11743 && TYPE_LANG_SPECIFIC (t)
11744 && CLASSTYPE_IS_TEMPLATE (t));
11745 /* If the member is a class template, then -- even after
11746 substitution -- there may be dependent types in the
11747 template argument list for the class. We increment
11748 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11749 that function will assume that no types are dependent
11750 when outside of a template. */
11751 if (class_template_p)
11752 ++processing_template_decl;
11753 newtag = tsubst (t, args, tf_error, NULL_TREE);
11754 if (class_template_p)
11755 --processing_template_decl;
11756 if (newtag == error_mark_node)
11757 continue;
11758
11759 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11760 {
11761 tree name = TYPE_IDENTIFIER (t);
11762
11763 if (class_template_p)
11764 /* Unfortunately, lookup_template_class sets
11765 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11766 instantiation (i.e., for the type of a member
11767 template class nested within a template class.)
11768 This behavior is required for
11769 maybe_process_partial_specialization to work
11770 correctly, but is not accurate in this case;
11771 the TAG is not an instantiation of anything.
11772 (The corresponding TEMPLATE_DECL is an
11773 instantiation, but the TYPE is not.) */
11774 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11775
11776 /* Now, we call pushtag to put this NEWTAG into the scope of
11777 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11778 pushtag calling push_template_decl. We don't have to do
11779 this for enums because it will already have been done in
11780 tsubst_enum. */
11781 if (name)
11782 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11783 pushtag (name, newtag, /*tag_scope=*/ts_current);
11784 }
11785 }
11786 else if (DECL_DECLARES_FUNCTION_P (t))
11787 {
11788 tree r;
11789
11790 if (TREE_CODE (t) == TEMPLATE_DECL)
11791 ++processing_template_decl;
11792 r = tsubst (t, args, tf_error, NULL_TREE);
11793 if (TREE_CODE (t) == TEMPLATE_DECL)
11794 --processing_template_decl;
11795 set_current_access_from_decl (r);
11796 finish_member_declaration (r);
11797 /* Instantiate members marked with attribute used. */
11798 if (r != error_mark_node && DECL_PRESERVE_P (r))
11799 mark_used (r);
11800 if (TREE_CODE (r) == FUNCTION_DECL
11801 && DECL_OMP_DECLARE_REDUCTION_P (r))
11802 cp_check_omp_declare_reduction (r);
11803 }
11804 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11805 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11806 /* A closure type for a lambda in an NSDMI or default argument.
11807 Ignore it; it will be regenerated when needed. */;
11808 else
11809 {
11810 /* Build new TYPE_FIELDS. */
11811 if (TREE_CODE (t) == STATIC_ASSERT)
11812 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
11813 /*integral_constant_expression_p=*/true);
11814 else if (TREE_CODE (t) != CONST_DECL)
11815 {
11816 tree r;
11817 tree vec = NULL_TREE;
11818 int len = 1;
11819
11820 /* The file and line for this declaration, to
11821 assist in error message reporting. Since we
11822 called push_tinst_level above, we don't need to
11823 restore these. */
11824 input_location = DECL_SOURCE_LOCATION (t);
11825
11826 if (TREE_CODE (t) == TEMPLATE_DECL)
11827 ++processing_template_decl;
11828 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11829 if (TREE_CODE (t) == TEMPLATE_DECL)
11830 --processing_template_decl;
11831
11832 if (TREE_CODE (r) == TREE_VEC)
11833 {
11834 /* A capture pack became multiple fields. */
11835 vec = r;
11836 len = TREE_VEC_LENGTH (vec);
11837 }
11838
11839 for (int i = 0; i < len; ++i)
11840 {
11841 if (vec)
11842 r = TREE_VEC_ELT (vec, i);
11843 if (VAR_P (r))
11844 {
11845 /* In [temp.inst]:
11846
11847 [t]he initialization (and any associated
11848 side-effects) of a static data member does
11849 not occur unless the static data member is
11850 itself used in a way that requires the
11851 definition of the static data member to
11852 exist.
11853
11854 Therefore, we do not substitute into the
11855 initialized for the static data member here. */
11856 finish_static_data_member_decl
11857 (r,
11858 /*init=*/NULL_TREE,
11859 /*init_const_expr_p=*/false,
11860 /*asmspec_tree=*/NULL_TREE,
11861 /*flags=*/0);
11862 /* Instantiate members marked with attribute used. */
11863 if (r != error_mark_node && DECL_PRESERVE_P (r))
11864 mark_used (r);
11865 }
11866 else if (TREE_CODE (r) == FIELD_DECL)
11867 {
11868 /* Determine whether R has a valid type and can be
11869 completed later. If R is invalid, then its type
11870 is replaced by error_mark_node. */
11871 tree rtype = TREE_TYPE (r);
11872 if (can_complete_type_without_circularity (rtype))
11873 complete_type (rtype);
11874
11875 if (!complete_or_array_type_p (rtype))
11876 {
11877 /* If R's type couldn't be completed and
11878 it isn't a flexible array member (whose
11879 type is incomplete by definition) give
11880 an error. */
11881 cxx_incomplete_type_error (r, rtype);
11882 TREE_TYPE (r) = error_mark_node;
11883 }
11884 else if (TREE_CODE (rtype) == ARRAY_TYPE
11885 && TYPE_DOMAIN (rtype) == NULL_TREE
11886 && (TREE_CODE (type) == UNION_TYPE
11887 || TREE_CODE (type) == QUAL_UNION_TYPE))
11888 {
11889 error ("flexible array member %qD in union", r);
11890 TREE_TYPE (r) = error_mark_node;
11891 }
11892 else if (!verify_type_context (input_location,
11893 TCTX_FIELD, rtype))
11894 TREE_TYPE (r) = error_mark_node;
11895 }
11896
11897 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11898 such a thing will already have been added to the field
11899 list by tsubst_enum in finish_member_declaration in the
11900 CLASSTYPE_NESTED_UTDS case above. */
11901 if (!(TREE_CODE (r) == TYPE_DECL
11902 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11903 && DECL_ARTIFICIAL (r)))
11904 {
11905 set_current_access_from_decl (r);
11906 finish_member_declaration (r);
11907 }
11908 }
11909 }
11910 }
11911 }
11912 else
11913 {
11914 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11915 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11916 {
11917 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11918
11919 tree friend_type = t;
11920 bool adjust_processing_template_decl = false;
11921
11922 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11923 {
11924 /* template <class T> friend class C; */
11925 friend_type = tsubst_friend_class (friend_type, args);
11926 adjust_processing_template_decl = true;
11927 }
11928 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11929 {
11930 /* template <class T> friend class C::D; */
11931 friend_type = tsubst (friend_type, args,
11932 tf_warning_or_error, NULL_TREE);
11933 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11934 friend_type = TREE_TYPE (friend_type);
11935 adjust_processing_template_decl = true;
11936 }
11937 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11938 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11939 {
11940 /* This could be either
11941
11942 friend class T::C;
11943
11944 when dependent_type_p is false or
11945
11946 template <class U> friend class T::C;
11947
11948 otherwise. */
11949 /* Bump processing_template_decl in case this is something like
11950 template <class T> friend struct A<T>::B. */
11951 ++processing_template_decl;
11952 friend_type = tsubst (friend_type, args,
11953 tf_warning_or_error, NULL_TREE);
11954 if (dependent_type_p (friend_type))
11955 adjust_processing_template_decl = true;
11956 --processing_template_decl;
11957 }
11958 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11959 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11960 && TYPE_HIDDEN_P (friend_type))
11961 {
11962 /* friend class C;
11963
11964 where C hasn't been declared yet. Let's lookup name
11965 from namespace scope directly, bypassing any name that
11966 come from dependent base class. */
11967 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11968
11969 /* The call to xref_tag_from_type does injection for friend
11970 classes. */
11971 push_nested_namespace (ns);
11972 friend_type =
11973 xref_tag_from_type (friend_type, NULL_TREE,
11974 /*tag_scope=*/ts_current);
11975 pop_nested_namespace (ns);
11976 }
11977 else if (uses_template_parms (friend_type))
11978 /* friend class C<T>; */
11979 friend_type = tsubst (friend_type, args,
11980 tf_warning_or_error, NULL_TREE);
11981 /* Otherwise it's
11982
11983 friend class C;
11984
11985 where C is already declared or
11986
11987 friend class C<int>;
11988
11989 We don't have to do anything in these cases. */
11990
11991 if (adjust_processing_template_decl)
11992 /* Trick make_friend_class into realizing that the friend
11993 we're adding is a template, not an ordinary class. It's
11994 important that we use make_friend_class since it will
11995 perform some error-checking and output cross-reference
11996 information. */
11997 ++processing_template_decl;
11998
11999 if (friend_type != error_mark_node)
12000 make_friend_class (type, friend_type, /*complain=*/false);
12001
12002 if (adjust_processing_template_decl)
12003 --processing_template_decl;
12004 }
12005 else
12006 {
12007 /* Build new DECL_FRIENDLIST. */
12008 tree r;
12009
12010 /* The file and line for this declaration, to
12011 assist in error message reporting. Since we
12012 called push_tinst_level above, we don't need to
12013 restore these. */
12014 input_location = DECL_SOURCE_LOCATION (t);
12015
12016 if (TREE_CODE (t) == TEMPLATE_DECL)
12017 {
12018 ++processing_template_decl;
12019 push_deferring_access_checks (dk_no_check);
12020 }
12021
12022 r = tsubst_friend_function (t, args);
12023 add_friend (type, r, /*complain=*/false);
12024 if (TREE_CODE (t) == TEMPLATE_DECL)
12025 {
12026 pop_deferring_access_checks ();
12027 --processing_template_decl;
12028 }
12029 }
12030 }
12031 }
12032
12033 if (fn_context)
12034 {
12035 /* Restore these before substituting into the lambda capture
12036 initializers. */
12037 cp_unevaluated_operand = saved_unevaluated_operand;
12038 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12039 }
12040
12041 /* Set the file and line number information to whatever is given for
12042 the class itself. This puts error messages involving generated
12043 implicit functions at a predictable point, and the same point
12044 that would be used for non-template classes. */
12045 input_location = DECL_SOURCE_LOCATION (typedecl);
12046
12047 unreverse_member_declarations (type);
12048 finish_struct_1 (type);
12049 TYPE_BEING_DEFINED (type) = 0;
12050
12051 /* We don't instantiate default arguments for member functions. 14.7.1:
12052
12053 The implicit instantiation of a class template specialization causes
12054 the implicit instantiation of the declarations, but not of the
12055 definitions or default arguments, of the class member functions,
12056 member classes, static data members and member templates.... */
12057
12058 /* Some typedefs referenced from within the template code need to be access
12059 checked at template instantiation time, i.e now. These types were
12060 added to the template at parsing time. Let's get those and perform
12061 the access checks then. */
12062 perform_typedefs_access_check (pattern, args);
12063 perform_deferred_access_checks (tf_warning_or_error);
12064 pop_nested_class ();
12065 maximum_field_alignment = saved_maximum_field_alignment;
12066 if (!fn_context)
12067 pop_from_top_level ();
12068 pop_tinst_level ();
12069
12070 /* The vtable for a template class can be emitted in any translation
12071 unit in which the class is instantiated. When there is no key
12072 method, however, finish_struct_1 will already have added TYPE to
12073 the keyed_classes. */
12074 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12075 vec_safe_push (keyed_classes, type);
12076
12077 return type;
12078 }
12079
12080 /* Wrapper for instantiate_class_template_1. */
12081
12082 tree
12083 instantiate_class_template (tree type)
12084 {
12085 tree ret;
12086 timevar_push (TV_TEMPLATE_INST);
12087 ret = instantiate_class_template_1 (type);
12088 timevar_pop (TV_TEMPLATE_INST);
12089 return ret;
12090 }
12091
12092 tree
12093 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12094 {
12095 tree r;
12096
12097 if (!t)
12098 r = t;
12099 else if (TYPE_P (t))
12100 r = tsubst (t, args, complain, in_decl);
12101 else
12102 {
12103 if (!(complain & tf_warning))
12104 ++c_inhibit_evaluation_warnings;
12105 r = tsubst_expr (t, args, complain, in_decl,
12106 /*integral_constant_expression_p=*/true);
12107 if (!(complain & tf_warning))
12108 --c_inhibit_evaluation_warnings;
12109 }
12110
12111 return r;
12112 }
12113
12114 /* Given a function parameter pack TMPL_PARM and some function parameters
12115 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12116 and set *SPEC_P to point at the next point in the list. */
12117
12118 tree
12119 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12120 {
12121 /* Collect all of the extra "packed" parameters into an
12122 argument pack. */
12123 tree parmvec;
12124 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12125 tree spec_parm = *spec_p;
12126 int i, len;
12127
12128 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12129 if (tmpl_parm
12130 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12131 break;
12132
12133 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12134 parmvec = make_tree_vec (len);
12135 spec_parm = *spec_p;
12136 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12137 {
12138 tree elt = spec_parm;
12139 if (DECL_PACK_P (elt))
12140 elt = make_pack_expansion (elt);
12141 TREE_VEC_ELT (parmvec, i) = elt;
12142 }
12143
12144 /* Build the argument packs. */
12145 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12146 *spec_p = spec_parm;
12147
12148 return argpack;
12149 }
12150
12151 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12152 NONTYPE_ARGUMENT_PACK. */
12153
12154 static tree
12155 make_fnparm_pack (tree spec_parm)
12156 {
12157 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12158 }
12159
12160 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12161 pack expansion with no extra args, 2 if it has extra args, or 0
12162 if it is not a pack expansion. */
12163
12164 static int
12165 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12166 {
12167 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12168 /* We're being called before this happens in tsubst_pack_expansion. */
12169 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12170 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12171 if (i >= TREE_VEC_LENGTH (vec))
12172 return 0;
12173 tree elt = TREE_VEC_ELT (vec, i);
12174 if (DECL_P (elt))
12175 /* A decl pack is itself an expansion. */
12176 elt = TREE_TYPE (elt);
12177 if (!PACK_EXPANSION_P (elt))
12178 return 0;
12179 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12180 return 2;
12181 return 1;
12182 }
12183
12184
12185 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12186
12187 static tree
12188 make_argument_pack_select (tree arg_pack, unsigned index)
12189 {
12190 tree aps = make_node (ARGUMENT_PACK_SELECT);
12191
12192 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12193 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12194
12195 return aps;
12196 }
12197
12198 /* This is a subroutine of tsubst_pack_expansion.
12199
12200 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12201 mechanism to store the (non complete list of) arguments of the
12202 substitution and return a non substituted pack expansion, in order
12203 to wait for when we have enough arguments to really perform the
12204 substitution. */
12205
12206 static bool
12207 use_pack_expansion_extra_args_p (tree parm_packs,
12208 int arg_pack_len,
12209 bool has_empty_arg)
12210 {
12211 /* If one pack has an expansion and another pack has a normal
12212 argument or if one pack has an empty argument and an another
12213 one hasn't then tsubst_pack_expansion cannot perform the
12214 substitution and need to fall back on the
12215 PACK_EXPANSION_EXTRA mechanism. */
12216 if (parm_packs == NULL_TREE)
12217 return false;
12218 else if (has_empty_arg)
12219 {
12220 /* If all the actual packs are pack expansions, we can still
12221 subsitute directly. */
12222 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12223 {
12224 tree a = TREE_VALUE (p);
12225 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12226 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12227 a = ARGUMENT_PACK_ARGS (a);
12228 if (TREE_VEC_LENGTH (a) == 1)
12229 a = TREE_VEC_ELT (a, 0);
12230 if (PACK_EXPANSION_P (a))
12231 continue;
12232 return true;
12233 }
12234 return false;
12235 }
12236
12237 bool has_expansion_arg = false;
12238 for (int i = 0 ; i < arg_pack_len; ++i)
12239 {
12240 bool has_non_expansion_arg = false;
12241 for (tree parm_pack = parm_packs;
12242 parm_pack;
12243 parm_pack = TREE_CHAIN (parm_pack))
12244 {
12245 tree arg = TREE_VALUE (parm_pack);
12246
12247 int exp = argument_pack_element_is_expansion_p (arg, i);
12248 if (exp == 2)
12249 /* We can't substitute a pack expansion with extra args into
12250 our pattern. */
12251 return true;
12252 else if (exp)
12253 has_expansion_arg = true;
12254 else
12255 has_non_expansion_arg = true;
12256 }
12257
12258 if (has_expansion_arg && has_non_expansion_arg)
12259 return true;
12260 }
12261 return false;
12262 }
12263
12264 /* [temp.variadic]/6 says that:
12265
12266 The instantiation of a pack expansion [...]
12267 produces a list E1,E2, ..., En, where N is the number of elements
12268 in the pack expansion parameters.
12269
12270 This subroutine of tsubst_pack_expansion produces one of these Ei.
12271
12272 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12273 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12274 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12275 INDEX is the index 'i' of the element Ei to produce. ARGS,
12276 COMPLAIN, and IN_DECL are the same parameters as for the
12277 tsubst_pack_expansion function.
12278
12279 The function returns the resulting Ei upon successful completion,
12280 or error_mark_node.
12281
12282 Note that this function possibly modifies the ARGS parameter, so
12283 it's the responsibility of the caller to restore it. */
12284
12285 static tree
12286 gen_elem_of_pack_expansion_instantiation (tree pattern,
12287 tree parm_packs,
12288 unsigned index,
12289 tree args /* This parm gets
12290 modified. */,
12291 tsubst_flags_t complain,
12292 tree in_decl)
12293 {
12294 tree t;
12295 bool ith_elem_is_expansion = false;
12296
12297 /* For each parameter pack, change the substitution of the parameter
12298 pack to the ith argument in its argument pack, then expand the
12299 pattern. */
12300 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12301 {
12302 tree parm = TREE_PURPOSE (pack);
12303 tree arg_pack = TREE_VALUE (pack);
12304 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12305
12306 ith_elem_is_expansion |=
12307 argument_pack_element_is_expansion_p (arg_pack, index);
12308
12309 /* Select the Ith argument from the pack. */
12310 if (TREE_CODE (parm) == PARM_DECL
12311 || VAR_P (parm)
12312 || TREE_CODE (parm) == FIELD_DECL)
12313 {
12314 if (index == 0)
12315 {
12316 aps = make_argument_pack_select (arg_pack, index);
12317 if (!mark_used (parm, complain) && !(complain & tf_error))
12318 return error_mark_node;
12319 register_local_specialization (aps, parm);
12320 }
12321 else
12322 aps = retrieve_local_specialization (parm);
12323 }
12324 else
12325 {
12326 int idx, level;
12327 template_parm_level_and_index (parm, &level, &idx);
12328
12329 if (index == 0)
12330 {
12331 aps = make_argument_pack_select (arg_pack, index);
12332 /* Update the corresponding argument. */
12333 TMPL_ARG (args, level, idx) = aps;
12334 }
12335 else
12336 /* Re-use the ARGUMENT_PACK_SELECT. */
12337 aps = TMPL_ARG (args, level, idx);
12338 }
12339 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12340 }
12341
12342 /* Substitute into the PATTERN with the (possibly altered)
12343 arguments. */
12344 if (pattern == in_decl)
12345 /* Expanding a fixed parameter pack from
12346 coerce_template_parameter_pack. */
12347 t = tsubst_decl (pattern, args, complain);
12348 else if (pattern == error_mark_node)
12349 t = error_mark_node;
12350 else if (!TYPE_P (pattern))
12351 t = tsubst_expr (pattern, args, complain, in_decl,
12352 /*integral_constant_expression_p=*/false);
12353 else
12354 t = tsubst (pattern, args, complain, in_decl);
12355
12356 /* If the Ith argument pack element is a pack expansion, then
12357 the Ith element resulting from the substituting is going to
12358 be a pack expansion as well. */
12359 if (ith_elem_is_expansion)
12360 t = make_pack_expansion (t, complain);
12361
12362 return t;
12363 }
12364
12365 /* When the unexpanded parameter pack in a fold expression expands to an empty
12366 sequence, the value of the expression is as follows; the program is
12367 ill-formed if the operator is not listed in this table.
12368
12369 && true
12370 || false
12371 , void() */
12372
12373 tree
12374 expand_empty_fold (tree t, tsubst_flags_t complain)
12375 {
12376 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12377 if (!FOLD_EXPR_MODIFY_P (t))
12378 switch (code)
12379 {
12380 case TRUTH_ANDIF_EXPR:
12381 return boolean_true_node;
12382 case TRUTH_ORIF_EXPR:
12383 return boolean_false_node;
12384 case COMPOUND_EXPR:
12385 return void_node;
12386 default:
12387 break;
12388 }
12389
12390 if (complain & tf_error)
12391 error_at (location_of (t),
12392 "fold of empty expansion over %O", code);
12393 return error_mark_node;
12394 }
12395
12396 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12397 form an expression that combines the two terms using the
12398 operator of T. */
12399
12400 static tree
12401 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12402 {
12403 tree op = FOLD_EXPR_OP (t);
12404 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12405
12406 // Handle compound assignment operators.
12407 if (FOLD_EXPR_MODIFY_P (t))
12408 return build_x_modify_expr (input_location, left, code, right, complain);
12409
12410 warning_sentinel s(warn_parentheses);
12411 switch (code)
12412 {
12413 case COMPOUND_EXPR:
12414 return build_x_compound_expr (input_location, left, right, complain);
12415 default:
12416 return build_x_binary_op (input_location, code,
12417 left, TREE_CODE (left),
12418 right, TREE_CODE (right),
12419 /*overload=*/NULL,
12420 complain);
12421 }
12422 }
12423
12424 /* Substitute ARGS into the pack of a fold expression T. */
12425
12426 static inline tree
12427 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12428 {
12429 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12430 }
12431
12432 /* Substitute ARGS into the pack of a fold expression T. */
12433
12434 static inline tree
12435 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12436 {
12437 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12438 }
12439
12440 /* Expand a PACK of arguments into a grouped as left fold.
12441 Given a pack containing elements A0, A1, ..., An and an
12442 operator @, this builds the expression:
12443
12444 ((A0 @ A1) @ A2) ... @ An
12445
12446 Note that PACK must not be empty.
12447
12448 The operator is defined by the original fold expression T. */
12449
12450 static tree
12451 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12452 {
12453 tree left = TREE_VEC_ELT (pack, 0);
12454 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12455 {
12456 tree right = TREE_VEC_ELT (pack, i);
12457 left = fold_expression (t, left, right, complain);
12458 }
12459 return left;
12460 }
12461
12462 /* Substitute into a unary left fold expression. */
12463
12464 static tree
12465 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12466 tree in_decl)
12467 {
12468 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12469 if (pack == error_mark_node)
12470 return error_mark_node;
12471 if (PACK_EXPANSION_P (pack))
12472 {
12473 tree r = copy_node (t);
12474 FOLD_EXPR_PACK (r) = pack;
12475 return r;
12476 }
12477 if (TREE_VEC_LENGTH (pack) == 0)
12478 return expand_empty_fold (t, complain);
12479 else
12480 return expand_left_fold (t, pack, complain);
12481 }
12482
12483 /* Substitute into a binary left fold expression.
12484
12485 Do ths by building a single (non-empty) vector of argumnts and
12486 building the expression from those elements. */
12487
12488 static tree
12489 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12490 tree in_decl)
12491 {
12492 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12493 if (pack == error_mark_node)
12494 return error_mark_node;
12495 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12496 if (init == error_mark_node)
12497 return error_mark_node;
12498
12499 if (PACK_EXPANSION_P (pack))
12500 {
12501 tree r = copy_node (t);
12502 FOLD_EXPR_PACK (r) = pack;
12503 FOLD_EXPR_INIT (r) = init;
12504 return r;
12505 }
12506
12507 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12508 TREE_VEC_ELT (vec, 0) = init;
12509 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12510 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12511
12512 return expand_left_fold (t, vec, complain);
12513 }
12514
12515 /* Expand a PACK of arguments into a grouped as right fold.
12516 Given a pack containing elementns A0, A1, ..., and an
12517 operator @, this builds the expression:
12518
12519 A0@ ... (An-2 @ (An-1 @ An))
12520
12521 Note that PACK must not be empty.
12522
12523 The operator is defined by the original fold expression T. */
12524
12525 tree
12526 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12527 {
12528 // Build the expression.
12529 int n = TREE_VEC_LENGTH (pack);
12530 tree right = TREE_VEC_ELT (pack, n - 1);
12531 for (--n; n != 0; --n)
12532 {
12533 tree left = TREE_VEC_ELT (pack, n - 1);
12534 right = fold_expression (t, left, right, complain);
12535 }
12536 return right;
12537 }
12538
12539 /* Substitute into a unary right fold expression. */
12540
12541 static tree
12542 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12543 tree in_decl)
12544 {
12545 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12546 if (pack == error_mark_node)
12547 return error_mark_node;
12548 if (PACK_EXPANSION_P (pack))
12549 {
12550 tree r = copy_node (t);
12551 FOLD_EXPR_PACK (r) = pack;
12552 return r;
12553 }
12554 if (TREE_VEC_LENGTH (pack) == 0)
12555 return expand_empty_fold (t, complain);
12556 else
12557 return expand_right_fold (t, pack, complain);
12558 }
12559
12560 /* Substitute into a binary right fold expression.
12561
12562 Do ths by building a single (non-empty) vector of arguments and
12563 building the expression from those elements. */
12564
12565 static tree
12566 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12567 tree in_decl)
12568 {
12569 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12570 if (pack == error_mark_node)
12571 return error_mark_node;
12572 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12573 if (init == error_mark_node)
12574 return error_mark_node;
12575
12576 if (PACK_EXPANSION_P (pack))
12577 {
12578 tree r = copy_node (t);
12579 FOLD_EXPR_PACK (r) = pack;
12580 FOLD_EXPR_INIT (r) = init;
12581 return r;
12582 }
12583
12584 int n = TREE_VEC_LENGTH (pack);
12585 tree vec = make_tree_vec (n + 1);
12586 for (int i = 0; i < n; ++i)
12587 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12588 TREE_VEC_ELT (vec, n) = init;
12589
12590 return expand_right_fold (t, vec, complain);
12591 }
12592
12593 /* Walk through the pattern of a pack expansion, adding everything in
12594 local_specializations to a list. */
12595
12596 class el_data
12597 {
12598 public:
12599 hash_set<tree> internal;
12600 tree extra;
12601 tsubst_flags_t complain;
12602
12603 el_data (tsubst_flags_t c)
12604 : extra (NULL_TREE), complain (c) {}
12605 };
12606 static tree
12607 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12608 {
12609 el_data &data = *reinterpret_cast<el_data*>(data_);
12610 tree *extra = &data.extra;
12611 tsubst_flags_t complain = data.complain;
12612
12613 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12614 /* Remember local typedefs (85214). */
12615 tp = &TYPE_NAME (*tp);
12616
12617 if (TREE_CODE (*tp) == DECL_EXPR)
12618 data.internal.add (DECL_EXPR_DECL (*tp));
12619 else if (tree spec = retrieve_local_specialization (*tp))
12620 {
12621 if (data.internal.contains (*tp))
12622 /* Don't mess with variables declared within the pattern. */
12623 return NULL_TREE;
12624 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12625 {
12626 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12627 tree args = ARGUMENT_PACK_ARGS (spec);
12628 if (TREE_VEC_LENGTH (args) == 1)
12629 {
12630 tree elt = TREE_VEC_ELT (args, 0);
12631 if (PACK_EXPANSION_P (elt))
12632 elt = PACK_EXPANSION_PATTERN (elt);
12633 if (DECL_PACK_P (elt))
12634 spec = elt;
12635 }
12636 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12637 {
12638 /* Handle lambda capture here, since we aren't doing any
12639 substitution now, and so tsubst_copy won't call
12640 process_outer_var_ref. */
12641 tree args = ARGUMENT_PACK_ARGS (spec);
12642 int len = TREE_VEC_LENGTH (args);
12643 for (int i = 0; i < len; ++i)
12644 {
12645 tree arg = TREE_VEC_ELT (args, i);
12646 tree carg = arg;
12647 if (outer_automatic_var_p (arg))
12648 carg = process_outer_var_ref (arg, complain);
12649 if (carg != arg)
12650 {
12651 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12652 proxies. */
12653 if (i == 0)
12654 {
12655 spec = copy_node (spec);
12656 args = copy_node (args);
12657 SET_ARGUMENT_PACK_ARGS (spec, args);
12658 register_local_specialization (spec, *tp);
12659 }
12660 TREE_VEC_ELT (args, i) = carg;
12661 }
12662 }
12663 }
12664 }
12665 if (outer_automatic_var_p (spec))
12666 spec = process_outer_var_ref (spec, complain);
12667 *extra = tree_cons (*tp, spec, *extra);
12668 }
12669 return NULL_TREE;
12670 }
12671 static tree
12672 extract_local_specs (tree pattern, tsubst_flags_t complain)
12673 {
12674 el_data data (complain);
12675 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12676 return data.extra;
12677 }
12678
12679 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12680 for use in PACK_EXPANSION_EXTRA_ARGS. */
12681
12682 tree
12683 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12684 {
12685 tree extra = args;
12686 if (local_specializations)
12687 if (tree locals = extract_local_specs (pattern, complain))
12688 extra = tree_cons (NULL_TREE, extra, locals);
12689 return extra;
12690 }
12691
12692 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12693 normal template args to ARGS. */
12694
12695 tree
12696 add_extra_args (tree extra, tree args)
12697 {
12698 if (extra && TREE_CODE (extra) == TREE_LIST)
12699 {
12700 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12701 {
12702 /* The partial instantiation involved local declarations collected in
12703 extract_local_specs; map from the general template to our local
12704 context. */
12705 tree gen = TREE_PURPOSE (elt);
12706 tree inst = TREE_VALUE (elt);
12707 if (DECL_P (inst))
12708 if (tree local = retrieve_local_specialization (inst))
12709 inst = local;
12710 /* else inst is already a full instantiation of the pack. */
12711 register_local_specialization (inst, gen);
12712 }
12713 gcc_assert (!TREE_PURPOSE (extra));
12714 extra = TREE_VALUE (extra);
12715 }
12716 #if 1
12717 /* I think we should always be able to substitute dependent args into the
12718 pattern. If that turns out to be incorrect in some cases, enable the
12719 alternate code (and add complain/in_decl parms to this function). */
12720 gcc_checking_assert (!uses_template_parms (extra));
12721 #else
12722 if (!uses_template_parms (extra))
12723 {
12724 gcc_unreachable ();
12725 extra = tsubst_template_args (extra, args, complain, in_decl);
12726 args = add_outermost_template_args (args, extra);
12727 }
12728 else
12729 #endif
12730 args = add_to_template_args (extra, args);
12731 return args;
12732 }
12733
12734 /* Substitute ARGS into T, which is an pack expansion
12735 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12736 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12737 (if only a partial substitution could be performed) or
12738 ERROR_MARK_NODE if there was an error. */
12739 tree
12740 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12741 tree in_decl)
12742 {
12743 tree pattern;
12744 tree pack, packs = NULL_TREE;
12745 bool unsubstituted_packs = false;
12746 int i, len = -1;
12747 tree result;
12748 bool need_local_specializations = false;
12749 int levels;
12750
12751 gcc_assert (PACK_EXPANSION_P (t));
12752 pattern = PACK_EXPANSION_PATTERN (t);
12753
12754 /* Add in any args remembered from an earlier partial instantiation. */
12755 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12756
12757 levels = TMPL_ARGS_DEPTH (args);
12758
12759 /* Determine the argument packs that will instantiate the parameter
12760 packs used in the expansion expression. While we're at it,
12761 compute the number of arguments to be expanded and make sure it
12762 is consistent. */
12763 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12764 pack = TREE_CHAIN (pack))
12765 {
12766 tree parm_pack = TREE_VALUE (pack);
12767 tree arg_pack = NULL_TREE;
12768 tree orig_arg = NULL_TREE;
12769 int level = 0;
12770
12771 if (TREE_CODE (parm_pack) == BASES)
12772 {
12773 gcc_assert (parm_pack == pattern);
12774 if (BASES_DIRECT (parm_pack))
12775 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12776 args, complain,
12777 in_decl, false),
12778 complain);
12779 else
12780 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12781 args, complain, in_decl,
12782 false), complain);
12783 }
12784 else if (builtin_pack_call_p (parm_pack))
12785 {
12786 if (parm_pack != pattern)
12787 {
12788 if (complain & tf_error)
12789 sorry ("%qE is not the entire pattern of the pack expansion",
12790 parm_pack);
12791 return error_mark_node;
12792 }
12793 return expand_builtin_pack_call (parm_pack, args,
12794 complain, in_decl);
12795 }
12796 else if (TREE_CODE (parm_pack) == PARM_DECL)
12797 {
12798 /* We know we have correct local_specializations if this
12799 expansion is at function scope, or if we're dealing with a
12800 local parameter in a requires expression; for the latter,
12801 tsubst_requires_expr set it up appropriately. */
12802 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12803 arg_pack = retrieve_local_specialization (parm_pack);
12804 else
12805 /* We can't rely on local_specializations for a parameter
12806 name used later in a function declaration (such as in a
12807 late-specified return type). Even if it exists, it might
12808 have the wrong value for a recursive call. */
12809 need_local_specializations = true;
12810
12811 if (!arg_pack)
12812 {
12813 /* This parameter pack was used in an unevaluated context. Just
12814 make a dummy decl, since it's only used for its type. */
12815 ++cp_unevaluated_operand;
12816 arg_pack = tsubst_decl (parm_pack, args, complain);
12817 --cp_unevaluated_operand;
12818 if (arg_pack && DECL_PACK_P (arg_pack))
12819 /* Partial instantiation of the parm_pack, we can't build
12820 up an argument pack yet. */
12821 arg_pack = NULL_TREE;
12822 else
12823 arg_pack = make_fnparm_pack (arg_pack);
12824 }
12825 else if (DECL_PACK_P (arg_pack))
12826 /* This argument pack isn't fully instantiated yet. */
12827 arg_pack = NULL_TREE;
12828 }
12829 else if (is_capture_proxy (parm_pack))
12830 {
12831 arg_pack = retrieve_local_specialization (parm_pack);
12832 if (DECL_PACK_P (arg_pack))
12833 arg_pack = NULL_TREE;
12834 }
12835 else
12836 {
12837 int idx;
12838 template_parm_level_and_index (parm_pack, &level, &idx);
12839 if (level <= levels)
12840 arg_pack = TMPL_ARG (args, level, idx);
12841 }
12842
12843 orig_arg = arg_pack;
12844 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12845 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12846
12847 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12848 /* This can only happen if we forget to expand an argument
12849 pack somewhere else. Just return an error, silently. */
12850 {
12851 result = make_tree_vec (1);
12852 TREE_VEC_ELT (result, 0) = error_mark_node;
12853 return result;
12854 }
12855
12856 if (arg_pack)
12857 {
12858 int my_len =
12859 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12860
12861 /* Don't bother trying to do a partial substitution with
12862 incomplete packs; we'll try again after deduction. */
12863 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12864 return t;
12865
12866 if (len < 0)
12867 len = my_len;
12868 else if (len != my_len)
12869 {
12870 if (!(complain & tf_error))
12871 /* Fail quietly. */;
12872 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12873 error ("mismatched argument pack lengths while expanding %qT",
12874 pattern);
12875 else
12876 error ("mismatched argument pack lengths while expanding %qE",
12877 pattern);
12878 return error_mark_node;
12879 }
12880
12881 /* Keep track of the parameter packs and their corresponding
12882 argument packs. */
12883 packs = tree_cons (parm_pack, arg_pack, packs);
12884 TREE_TYPE (packs) = orig_arg;
12885 }
12886 else
12887 {
12888 /* We can't substitute for this parameter pack. We use a flag as
12889 well as the missing_level counter because function parameter
12890 packs don't have a level. */
12891 gcc_assert (processing_template_decl || is_auto (parm_pack));
12892 unsubstituted_packs = true;
12893 }
12894 }
12895
12896 /* If the expansion is just T..., return the matching argument pack, unless
12897 we need to call convert_from_reference on all the elements. This is an
12898 important optimization; see c++/68422. */
12899 if (!unsubstituted_packs
12900 && TREE_PURPOSE (packs) == pattern)
12901 {
12902 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12903
12904 /* If the argument pack is a single pack expansion, pull it out. */
12905 if (TREE_VEC_LENGTH (args) == 1
12906 && pack_expansion_args_count (args))
12907 return TREE_VEC_ELT (args, 0);
12908
12909 /* Types need no adjustment, nor does sizeof..., and if we still have
12910 some pack expansion args we won't do anything yet. */
12911 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12912 || PACK_EXPANSION_SIZEOF_P (t)
12913 || pack_expansion_args_count (args))
12914 return args;
12915 /* Also optimize expression pack expansions if we can tell that the
12916 elements won't have reference type. */
12917 tree type = TREE_TYPE (pattern);
12918 if (type && !TYPE_REF_P (type)
12919 && !PACK_EXPANSION_P (type)
12920 && !WILDCARD_TYPE_P (type))
12921 return args;
12922 /* Otherwise use the normal path so we get convert_from_reference. */
12923 }
12924
12925 /* We cannot expand this expansion expression, because we don't have
12926 all of the argument packs we need. */
12927 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12928 {
12929 /* We got some full packs, but we can't substitute them in until we
12930 have values for all the packs. So remember these until then. */
12931
12932 t = make_pack_expansion (pattern, complain);
12933 PACK_EXPANSION_EXTRA_ARGS (t)
12934 = build_extra_args (pattern, args, complain);
12935 return t;
12936 }
12937
12938 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
12939 type, so create our own local specializations map; the current map is
12940 either NULL or (in the case of recursive unification) might have
12941 bindings that we don't want to use or alter. */
12942 local_specialization_stack lss (need_local_specializations
12943 ? lss_blank : lss_nop);
12944
12945 if (unsubstituted_packs)
12946 {
12947 /* There were no real arguments, we're just replacing a parameter
12948 pack with another version of itself. Substitute into the
12949 pattern and return a PACK_EXPANSION_*. The caller will need to
12950 deal with that. */
12951 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12952 t = tsubst_expr (pattern, args, complain, in_decl,
12953 /*integral_constant_expression_p=*/false);
12954 else
12955 t = tsubst (pattern, args, complain, in_decl);
12956 t = make_pack_expansion (t, complain);
12957 return t;
12958 }
12959
12960 gcc_assert (len >= 0);
12961
12962 /* For each argument in each argument pack, substitute into the
12963 pattern. */
12964 result = make_tree_vec (len);
12965 tree elem_args = copy_template_args (args);
12966 for (i = 0; i < len; ++i)
12967 {
12968 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12969 i,
12970 elem_args, complain,
12971 in_decl);
12972 TREE_VEC_ELT (result, i) = t;
12973 if (t == error_mark_node)
12974 {
12975 result = error_mark_node;
12976 break;
12977 }
12978 }
12979
12980 /* Update ARGS to restore the substitution from parameter packs to
12981 their argument packs. */
12982 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12983 {
12984 tree parm = TREE_PURPOSE (pack);
12985
12986 if (TREE_CODE (parm) == PARM_DECL
12987 || VAR_P (parm)
12988 || TREE_CODE (parm) == FIELD_DECL)
12989 register_local_specialization (TREE_TYPE (pack), parm);
12990 else
12991 {
12992 int idx, level;
12993
12994 if (TREE_VALUE (pack) == NULL_TREE)
12995 continue;
12996
12997 template_parm_level_and_index (parm, &level, &idx);
12998
12999 /* Update the corresponding argument. */
13000 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13001 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13002 TREE_TYPE (pack);
13003 else
13004 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13005 }
13006 }
13007
13008 /* If the dependent pack arguments were such that we end up with only a
13009 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13010 if (len == 1 && TREE_CODE (result) == TREE_VEC
13011 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13012 return TREE_VEC_ELT (result, 0);
13013
13014 return result;
13015 }
13016
13017 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
13018 TMPL. We do this using DECL_PARM_INDEX, which should work even with
13019 parameter packs; all parms generated from a function parameter pack will
13020 have the same DECL_PARM_INDEX. */
13021
13022 tree
13023 get_pattern_parm (tree parm, tree tmpl)
13024 {
13025 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
13026 tree patparm;
13027
13028 if (DECL_ARTIFICIAL (parm))
13029 {
13030 for (patparm = DECL_ARGUMENTS (pattern);
13031 patparm; patparm = DECL_CHAIN (patparm))
13032 if (DECL_ARTIFICIAL (patparm)
13033 && DECL_NAME (parm) == DECL_NAME (patparm))
13034 break;
13035 }
13036 else
13037 {
13038 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
13039 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
13040 gcc_assert (DECL_PARM_INDEX (patparm)
13041 == DECL_PARM_INDEX (parm));
13042 }
13043
13044 return patparm;
13045 }
13046
13047 /* Make an argument pack out of the TREE_VEC VEC. */
13048
13049 static tree
13050 make_argument_pack (tree vec)
13051 {
13052 tree pack;
13053 tree elt = TREE_VEC_ELT (vec, 0);
13054 if (TYPE_P (elt))
13055 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13056 else
13057 {
13058 pack = make_node (NONTYPE_ARGUMENT_PACK);
13059 TREE_CONSTANT (pack) = 1;
13060 }
13061 SET_ARGUMENT_PACK_ARGS (pack, vec);
13062 return pack;
13063 }
13064
13065 /* Return an exact copy of template args T that can be modified
13066 independently. */
13067
13068 static tree
13069 copy_template_args (tree t)
13070 {
13071 if (t == error_mark_node)
13072 return t;
13073
13074 int len = TREE_VEC_LENGTH (t);
13075 tree new_vec = make_tree_vec (len);
13076
13077 for (int i = 0; i < len; ++i)
13078 {
13079 tree elt = TREE_VEC_ELT (t, i);
13080 if (elt && TREE_CODE (elt) == TREE_VEC)
13081 elt = copy_template_args (elt);
13082 TREE_VEC_ELT (new_vec, i) = elt;
13083 }
13084
13085 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13086 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13087
13088 return new_vec;
13089 }
13090
13091 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13092
13093 tree
13094 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13095 tree in_decl)
13096 {
13097 /* Substitute into each of the arguments. */
13098 tree new_arg = TYPE_P (orig_arg)
13099 ? cxx_make_type (TREE_CODE (orig_arg))
13100 : make_node (TREE_CODE (orig_arg));
13101
13102 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13103 args, complain, in_decl);
13104 if (pack_args == error_mark_node)
13105 new_arg = error_mark_node;
13106 else
13107 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13108
13109 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13110 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13111
13112 return new_arg;
13113 }
13114
13115 /* Substitute ARGS into the vector or list of template arguments T. */
13116
13117 tree
13118 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13119 {
13120 tree orig_t = t;
13121 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13122 tree *elts;
13123
13124 if (t == error_mark_node)
13125 return error_mark_node;
13126
13127 len = TREE_VEC_LENGTH (t);
13128 elts = XALLOCAVEC (tree, len);
13129
13130 for (i = 0; i < len; i++)
13131 {
13132 tree orig_arg = TREE_VEC_ELT (t, i);
13133 tree new_arg;
13134
13135 if (TREE_CODE (orig_arg) == TREE_VEC)
13136 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13137 else if (PACK_EXPANSION_P (orig_arg))
13138 {
13139 /* Substitute into an expansion expression. */
13140 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13141
13142 if (TREE_CODE (new_arg) == TREE_VEC)
13143 /* Add to the expanded length adjustment the number of
13144 expanded arguments. We subtract one from this
13145 measurement, because the argument pack expression
13146 itself is already counted as 1 in
13147 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13148 the argument pack is empty. */
13149 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13150 }
13151 else if (ARGUMENT_PACK_P (orig_arg))
13152 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13153 else
13154 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13155
13156 if (new_arg == error_mark_node)
13157 return error_mark_node;
13158
13159 elts[i] = new_arg;
13160 if (new_arg != orig_arg)
13161 need_new = 1;
13162 }
13163
13164 if (!need_new)
13165 return t;
13166
13167 /* Make space for the expanded arguments coming from template
13168 argument packs. */
13169 t = make_tree_vec (len + expanded_len_adjust);
13170 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13171 arguments for a member template.
13172 In that case each TREE_VEC in ORIG_T represents a level of template
13173 arguments, and ORIG_T won't carry any non defaulted argument count.
13174 It will rather be the nested TREE_VECs that will carry one.
13175 In other words, ORIG_T carries a non defaulted argument count only
13176 if it doesn't contain any nested TREE_VEC. */
13177 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13178 {
13179 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13180 count += expanded_len_adjust;
13181 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13182 }
13183 for (i = 0, out = 0; i < len; i++)
13184 {
13185 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13186 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13187 && TREE_CODE (elts[i]) == TREE_VEC)
13188 {
13189 int idx;
13190
13191 /* Now expand the template argument pack "in place". */
13192 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13193 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13194 }
13195 else
13196 {
13197 TREE_VEC_ELT (t, out) = elts[i];
13198 out++;
13199 }
13200 }
13201
13202 return t;
13203 }
13204
13205 /* Substitute ARGS into one level PARMS of template parameters. */
13206
13207 static tree
13208 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13209 {
13210 if (parms == error_mark_node)
13211 return error_mark_node;
13212
13213 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13214
13215 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13216 {
13217 tree tuple = TREE_VEC_ELT (parms, i);
13218
13219 if (tuple == error_mark_node)
13220 continue;
13221
13222 TREE_VEC_ELT (new_vec, i) =
13223 tsubst_template_parm (tuple, args, complain);
13224 }
13225
13226 return new_vec;
13227 }
13228
13229 /* Return the result of substituting ARGS into the template parameters
13230 given by PARMS. If there are m levels of ARGS and m + n levels of
13231 PARMS, then the result will contain n levels of PARMS. For
13232 example, if PARMS is `template <class T> template <class U>
13233 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13234 result will be `template <int*, double, class V>'. */
13235
13236 static tree
13237 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13238 {
13239 tree r = NULL_TREE;
13240 tree* new_parms;
13241
13242 /* When substituting into a template, we must set
13243 PROCESSING_TEMPLATE_DECL as the template parameters may be
13244 dependent if they are based on one-another, and the dependency
13245 predicates are short-circuit outside of templates. */
13246 ++processing_template_decl;
13247
13248 for (new_parms = &r;
13249 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13250 new_parms = &(TREE_CHAIN (*new_parms)),
13251 parms = TREE_CHAIN (parms))
13252 {
13253 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13254 args, complain);
13255 *new_parms =
13256 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13257 - TMPL_ARGS_DEPTH (args)),
13258 new_vec, NULL_TREE);
13259 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13260 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13261 }
13262
13263 --processing_template_decl;
13264
13265 return r;
13266 }
13267
13268 /* Return the result of substituting ARGS into one template parameter
13269 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13270 parameter and which TREE_PURPOSE is the default argument of the
13271 template parameter. */
13272
13273 static tree
13274 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13275 {
13276 tree default_value, parm_decl;
13277
13278 if (args == NULL_TREE
13279 || t == NULL_TREE
13280 || t == error_mark_node)
13281 return t;
13282
13283 gcc_assert (TREE_CODE (t) == TREE_LIST);
13284
13285 default_value = TREE_PURPOSE (t);
13286 parm_decl = TREE_VALUE (t);
13287 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13288
13289 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13290 if (TREE_CODE (parm_decl) == PARM_DECL
13291 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13292 parm_decl = error_mark_node;
13293 default_value = tsubst_template_arg (default_value, args,
13294 complain, NULL_TREE);
13295 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13296
13297 tree r = build_tree_list (default_value, parm_decl);
13298 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13299 return r;
13300 }
13301
13302 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13303 type T. If T is not an aggregate or enumeration type, it is
13304 handled as if by tsubst. IN_DECL is as for tsubst. If
13305 ENTERING_SCOPE is nonzero, T is the context for a template which
13306 we are presently tsubst'ing. Return the substituted value. */
13307
13308 static tree
13309 tsubst_aggr_type (tree t,
13310 tree args,
13311 tsubst_flags_t complain,
13312 tree in_decl,
13313 int entering_scope)
13314 {
13315 if (t == NULL_TREE)
13316 return NULL_TREE;
13317
13318 switch (TREE_CODE (t))
13319 {
13320 case RECORD_TYPE:
13321 if (TYPE_PTRMEMFUNC_P (t))
13322 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13323
13324 /* Fall through. */
13325 case ENUMERAL_TYPE:
13326 case UNION_TYPE:
13327 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13328 {
13329 tree argvec;
13330 tree context;
13331 tree r;
13332
13333 /* In "sizeof(X<I>)" we need to evaluate "I". */
13334 cp_evaluated ev;
13335
13336 /* First, determine the context for the type we are looking
13337 up. */
13338 context = TYPE_CONTEXT (t);
13339 if (context && TYPE_P (context))
13340 {
13341 context = tsubst_aggr_type (context, args, complain,
13342 in_decl, /*entering_scope=*/1);
13343 /* If context is a nested class inside a class template,
13344 it may still need to be instantiated (c++/33959). */
13345 context = complete_type (context);
13346 }
13347
13348 /* Then, figure out what arguments are appropriate for the
13349 type we are trying to find. For example, given:
13350
13351 template <class T> struct S;
13352 template <class T, class U> void f(T, U) { S<U> su; }
13353
13354 and supposing that we are instantiating f<int, double>,
13355 then our ARGS will be {int, double}, but, when looking up
13356 S we only want {double}. */
13357 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13358 complain, in_decl);
13359 if (argvec == error_mark_node)
13360 r = error_mark_node;
13361 else
13362 {
13363 r = lookup_template_class (t, argvec, in_decl, context,
13364 entering_scope, complain);
13365 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13366 }
13367
13368 return r;
13369 }
13370 else
13371 /* This is not a template type, so there's nothing to do. */
13372 return t;
13373
13374 default:
13375 return tsubst (t, args, complain, in_decl);
13376 }
13377 }
13378
13379 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13380
13381 /* Substitute into the default argument ARG (a default argument for
13382 FN), which has the indicated TYPE. */
13383
13384 tree
13385 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13386 tsubst_flags_t complain)
13387 {
13388 int errs = errorcount + sorrycount;
13389
13390 /* This can happen in invalid code. */
13391 if (TREE_CODE (arg) == DEFERRED_PARSE)
13392 return arg;
13393
13394 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13395 parm = chain_index (parmnum, parm);
13396 tree parmtype = TREE_TYPE (parm);
13397 if (DECL_BY_REFERENCE (parm))
13398 parmtype = TREE_TYPE (parmtype);
13399 if (parmtype == error_mark_node)
13400 return error_mark_node;
13401
13402 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13403
13404 tree *slot;
13405 if (defarg_inst && (slot = defarg_inst->get (parm)))
13406 return *slot;
13407
13408 /* This default argument came from a template. Instantiate the
13409 default argument here, not in tsubst. In the case of
13410 something like:
13411
13412 template <class T>
13413 struct S {
13414 static T t();
13415 void f(T = t());
13416 };
13417
13418 we must be careful to do name lookup in the scope of S<T>,
13419 rather than in the current class. */
13420 push_to_top_level ();
13421 push_access_scope (fn);
13422 push_deferring_access_checks (dk_no_deferred);
13423 start_lambda_scope (parm);
13424
13425 /* The default argument expression may cause implicitly defined
13426 member functions to be synthesized, which will result in garbage
13427 collection. We must treat this situation as if we were within
13428 the body of function so as to avoid collecting live data on the
13429 stack. */
13430 ++function_depth;
13431 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13432 complain, NULL_TREE,
13433 /*integral_constant_expression_p=*/false);
13434 --function_depth;
13435
13436 finish_lambda_scope ();
13437
13438 /* Make sure the default argument is reasonable. */
13439 arg = check_default_argument (type, arg, complain);
13440
13441 if (errorcount+sorrycount > errs
13442 && (complain & tf_warning_or_error))
13443 inform (input_location,
13444 " when instantiating default argument for call to %qD", fn);
13445
13446 pop_deferring_access_checks ();
13447 pop_access_scope (fn);
13448 pop_from_top_level ();
13449
13450 if (arg != error_mark_node && !cp_unevaluated_operand)
13451 {
13452 if (!defarg_inst)
13453 defarg_inst = decl_tree_cache_map::create_ggc (37);
13454 defarg_inst->put (parm, arg);
13455 }
13456
13457 return arg;
13458 }
13459
13460 /* Substitute into all the default arguments for FN. */
13461
13462 static void
13463 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13464 {
13465 tree arg;
13466 tree tmpl_args;
13467
13468 tmpl_args = DECL_TI_ARGS (fn);
13469
13470 /* If this function is not yet instantiated, we certainly don't need
13471 its default arguments. */
13472 if (uses_template_parms (tmpl_args))
13473 return;
13474 /* Don't do this again for clones. */
13475 if (DECL_CLONED_FUNCTION_P (fn))
13476 return;
13477
13478 int i = 0;
13479 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13480 arg;
13481 arg = TREE_CHAIN (arg), ++i)
13482 if (TREE_PURPOSE (arg))
13483 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13484 TREE_VALUE (arg),
13485 TREE_PURPOSE (arg),
13486 complain);
13487 }
13488
13489 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13490 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13491
13492 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13493
13494 void
13495 store_explicit_specifier (tree v, tree t)
13496 {
13497 if (!explicit_specifier_map)
13498 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13499 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13500 explicit_specifier_map->put (v, t);
13501 }
13502
13503 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13504
13505 static tree
13506 lookup_explicit_specifier (tree v)
13507 {
13508 return *explicit_specifier_map->get (v);
13509 }
13510
13511 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13512 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13513 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13514 identical to T. */
13515
13516 static tree
13517 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13518 tree raises, tsubst_flags_t complain)
13519 {
13520 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13521
13522 tree new_type;
13523 if (TREE_CODE (t) == FUNCTION_TYPE)
13524 {
13525 new_type = build_function_type (return_type, arg_types);
13526 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13527 }
13528 else
13529 {
13530 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13531 /* Don't pick up extra function qualifiers from the basetype. */
13532 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13533 if (! MAYBE_CLASS_TYPE_P (r))
13534 {
13535 /* [temp.deduct]
13536
13537 Type deduction may fail for any of the following
13538 reasons:
13539
13540 -- Attempting to create "pointer to member of T" when T
13541 is not a class type. */
13542 if (complain & tf_error)
13543 error ("creating pointer to member function of non-class type %qT",
13544 r);
13545 return error_mark_node;
13546 }
13547
13548 new_type = build_method_type_directly (r, return_type,
13549 TREE_CHAIN (arg_types));
13550 }
13551 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13552
13553 cp_ref_qualifier rqual = type_memfn_rqual (t);
13554 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13555 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13556 }
13557
13558 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13559 each of its formal parameters. If there is a disagreement then rebuild
13560 DECL's function type according to its formal parameter types, as part of a
13561 resolution for Core issues 1001/1322. */
13562
13563 static void
13564 maybe_rebuild_function_decl_type (tree decl)
13565 {
13566 bool function_type_needs_rebuilding = false;
13567 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13568 {
13569 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13570 while (parm_type_list && parm_type_list != void_list_node)
13571 {
13572 tree parm_type = TREE_VALUE (parm_type_list);
13573 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13574 if (!same_type_p (parm_type, formal_parm_type_unqual))
13575 {
13576 function_type_needs_rebuilding = true;
13577 break;
13578 }
13579
13580 parm_list = DECL_CHAIN (parm_list);
13581 parm_type_list = TREE_CHAIN (parm_type_list);
13582 }
13583 }
13584
13585 if (!function_type_needs_rebuilding)
13586 return;
13587
13588 const tree fntype = TREE_TYPE (decl);
13589 tree parm_list = DECL_ARGUMENTS (decl);
13590 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13591 tree new_parm_type_list = NULL_TREE;
13592 tree *q = &new_parm_type_list;
13593 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13594 {
13595 *q = copy_node (old_parm_type_list);
13596 parm_list = DECL_CHAIN (parm_list);
13597 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13598 q = &TREE_CHAIN (*q);
13599 }
13600 while (old_parm_type_list && old_parm_type_list != void_list_node)
13601 {
13602 *q = copy_node (old_parm_type_list);
13603 tree *new_parm_type = &TREE_VALUE (*q);
13604 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13605 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13606 *new_parm_type = formal_parm_type_unqual;
13607
13608 parm_list = DECL_CHAIN (parm_list);
13609 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13610 q = &TREE_CHAIN (*q);
13611 }
13612 if (old_parm_type_list == void_list_node)
13613 *q = void_list_node;
13614
13615 TREE_TYPE (decl)
13616 = rebuild_function_or_method_type (fntype,
13617 TREE_TYPE (fntype), new_parm_type_list,
13618 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13619 }
13620
13621 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13622
13623 static tree
13624 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13625 tree lambda_fntype)
13626 {
13627 tree gen_tmpl, argvec;
13628 hashval_t hash = 0;
13629 tree in_decl = t;
13630
13631 /* Nobody should be tsubst'ing into non-template functions. */
13632 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
13633
13634 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13635 {
13636 /* If T is not dependent, just return it. */
13637 if (!uses_template_parms (DECL_TI_ARGS (t))
13638 && !LAMBDA_FUNCTION_P (t))
13639 return t;
13640
13641 /* Calculate the most general template of which R is a
13642 specialization. */
13643 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13644
13645 /* We're substituting a lambda function under tsubst_lambda_expr but not
13646 directly from it; find the matching function we're already inside.
13647 But don't do this if T is a generic lambda with a single level of
13648 template parms, as in that case we're doing a normal instantiation. */
13649 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13650 && (!generic_lambda_fn_p (t)
13651 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13652 return enclosing_instantiation_of (t);
13653
13654 /* Calculate the complete set of arguments used to
13655 specialize R. */
13656 argvec = tsubst_template_args (DECL_TI_ARGS
13657 (DECL_TEMPLATE_RESULT
13658 (DECL_TI_TEMPLATE (t))),
13659 args, complain, in_decl);
13660 if (argvec == error_mark_node)
13661 return error_mark_node;
13662
13663 /* Check to see if we already have this specialization. */
13664 if (!lambda_fntype)
13665 {
13666 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13667 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13668 return spec;
13669 }
13670
13671 /* We can see more levels of arguments than parameters if
13672 there was a specialization of a member template, like
13673 this:
13674
13675 template <class T> struct S { template <class U> void f(); }
13676 template <> template <class U> void S<int>::f(U);
13677
13678 Here, we'll be substituting into the specialization,
13679 because that's where we can find the code we actually
13680 want to generate, but we'll have enough arguments for
13681 the most general template.
13682
13683 We also deal with the peculiar case:
13684
13685 template <class T> struct S {
13686 template <class U> friend void f();
13687 };
13688 template <class U> void f() {}
13689 template S<int>;
13690 template void f<double>();
13691
13692 Here, the ARGS for the instantiation of will be {int,
13693 double}. But, we only need as many ARGS as there are
13694 levels of template parameters in CODE_PATTERN. We are
13695 careful not to get fooled into reducing the ARGS in
13696 situations like:
13697
13698 template <class T> struct S { template <class U> void f(U); }
13699 template <class T> template <> void S<T>::f(int) {}
13700
13701 which we can spot because the pattern will be a
13702 specialization in this case. */
13703 int args_depth = TMPL_ARGS_DEPTH (args);
13704 int parms_depth =
13705 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13706
13707 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13708 args = get_innermost_template_args (args, parms_depth);
13709 }
13710 else
13711 {
13712 /* This special case arises when we have something like this:
13713
13714 template <class T> struct S {
13715 friend void f<int>(int, double);
13716 };
13717
13718 Here, the DECL_TI_TEMPLATE for the friend declaration
13719 will be an IDENTIFIER_NODE. We are being called from
13720 tsubst_friend_function, and we want only to create a
13721 new decl (R) with appropriate types so that we can call
13722 determine_specialization. */
13723 gen_tmpl = NULL_TREE;
13724 argvec = NULL_TREE;
13725 }
13726
13727 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13728 : NULL_TREE);
13729 tree ctx = closure ? closure : DECL_CONTEXT (t);
13730 bool member = ctx && TYPE_P (ctx);
13731
13732 if (member && !closure)
13733 ctx = tsubst_aggr_type (ctx, args,
13734 complain, t, /*entering_scope=*/1);
13735
13736 tree type = (lambda_fntype ? lambda_fntype
13737 : tsubst (TREE_TYPE (t), args,
13738 complain | tf_fndecl_type, in_decl));
13739 if (type == error_mark_node)
13740 return error_mark_node;
13741
13742 /* If we hit excessive deduction depth, the type is bogus even if
13743 it isn't error_mark_node, so don't build a decl. */
13744 if (excessive_deduction_depth)
13745 return error_mark_node;
13746
13747 /* We do NOT check for matching decls pushed separately at this
13748 point, as they may not represent instantiations of this
13749 template, and in any case are considered separate under the
13750 discrete model. */
13751 tree r = copy_decl (t);
13752 DECL_USE_TEMPLATE (r) = 0;
13753 TREE_TYPE (r) = type;
13754 /* Clear out the mangled name and RTL for the instantiation. */
13755 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13756 SET_DECL_RTL (r, NULL);
13757 /* Leave DECL_INITIAL set on deleted instantiations. */
13758 if (!DECL_DELETED_FN (r))
13759 DECL_INITIAL (r) = NULL_TREE;
13760 DECL_CONTEXT (r) = ctx;
13761
13762 /* Handle explicit(dependent-expr). */
13763 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13764 {
13765 tree spec = lookup_explicit_specifier (t);
13766 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13767 /*function_p=*/false,
13768 /*i_c_e_p=*/true);
13769 spec = build_explicit_specifier (spec, complain);
13770 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13771 }
13772
13773 /* OpenMP UDRs have the only argument a reference to the declared
13774 type. We want to diagnose if the declared type is a reference,
13775 which is invalid, but as references to references are usually
13776 quietly merged, diagnose it here. */
13777 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13778 {
13779 tree argtype
13780 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13781 argtype = tsubst (argtype, args, complain, in_decl);
13782 if (TYPE_REF_P (argtype))
13783 error_at (DECL_SOURCE_LOCATION (t),
13784 "reference type %qT in "
13785 "%<#pragma omp declare reduction%>", argtype);
13786 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13787 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13788 argtype);
13789 }
13790
13791 if (member && DECL_CONV_FN_P (r))
13792 /* Type-conversion operator. Reconstruct the name, in
13793 case it's the name of one of the template's parameters. */
13794 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13795
13796 tree parms = DECL_ARGUMENTS (t);
13797 if (closure)
13798 parms = DECL_CHAIN (parms);
13799 parms = tsubst (parms, args, complain, t);
13800 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13801 DECL_CONTEXT (parm) = r;
13802 if (closure)
13803 {
13804 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13805 DECL_NAME (tparm) = closure_identifier;
13806 DECL_CHAIN (tparm) = parms;
13807 parms = tparm;
13808 }
13809 DECL_ARGUMENTS (r) = parms;
13810 DECL_RESULT (r) = NULL_TREE;
13811
13812 maybe_rebuild_function_decl_type (r);
13813
13814 TREE_STATIC (r) = 0;
13815 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13816 DECL_EXTERNAL (r) = 1;
13817 /* If this is an instantiation of a function with internal
13818 linkage, we already know what object file linkage will be
13819 assigned to the instantiation. */
13820 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13821 DECL_DEFER_OUTPUT (r) = 0;
13822 DECL_CHAIN (r) = NULL_TREE;
13823 DECL_PENDING_INLINE_INFO (r) = 0;
13824 DECL_PENDING_INLINE_P (r) = 0;
13825 DECL_SAVED_TREE (r) = NULL_TREE;
13826 DECL_STRUCT_FUNCTION (r) = NULL;
13827 TREE_USED (r) = 0;
13828 /* We'll re-clone as appropriate in instantiate_template. */
13829 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13830
13831 /* If we aren't complaining now, return on error before we register
13832 the specialization so that we'll complain eventually. */
13833 if ((complain & tf_error) == 0
13834 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13835 && !grok_op_properties (r, /*complain=*/false))
13836 return error_mark_node;
13837
13838 /* Associate the constraints directly with the instantiation. We
13839 don't substitute through the constraints; that's only done when
13840 they are checked. */
13841 if (tree ci = get_constraints (t))
13842 set_constraints (r, ci);
13843
13844 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13845 SET_DECL_FRIEND_CONTEXT (r,
13846 tsubst (DECL_FRIEND_CONTEXT (t),
13847 args, complain, in_decl));
13848
13849 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13850 this in the special friend case mentioned above where
13851 GEN_TMPL is NULL. */
13852 if (gen_tmpl && !closure)
13853 {
13854 DECL_TEMPLATE_INFO (r)
13855 = build_template_info (gen_tmpl, argvec);
13856 SET_DECL_IMPLICIT_INSTANTIATION (r);
13857
13858 tree new_r
13859 = register_specialization (r, gen_tmpl, argvec, false, hash);
13860 if (new_r != r)
13861 /* We instantiated this while substituting into
13862 the type earlier (template/friend54.C). */
13863 return new_r;
13864
13865 /* We're not supposed to instantiate default arguments
13866 until they are called, for a template. But, for a
13867 declaration like:
13868
13869 template <class T> void f ()
13870 { extern void g(int i = T()); }
13871
13872 we should do the substitution when the template is
13873 instantiated. We handle the member function case in
13874 instantiate_class_template since the default arguments
13875 might refer to other members of the class. */
13876 if (!member
13877 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13878 && !uses_template_parms (argvec))
13879 tsubst_default_arguments (r, complain);
13880 }
13881 else
13882 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13883
13884 /* Copy the list of befriending classes. */
13885 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13886 *friends;
13887 friends = &TREE_CHAIN (*friends))
13888 {
13889 *friends = copy_node (*friends);
13890 TREE_VALUE (*friends)
13891 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13892 }
13893
13894 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13895 {
13896 maybe_retrofit_in_chrg (r);
13897 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13898 return error_mark_node;
13899 /* If this is an instantiation of a member template, clone it.
13900 If it isn't, that'll be handled by
13901 clone_constructors_and_destructors. */
13902 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13903 clone_function_decl (r, /*update_methods=*/false);
13904 }
13905 else if ((complain & tf_error) != 0
13906 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13907 && !grok_op_properties (r, /*complain=*/true))
13908 return error_mark_node;
13909
13910 /* Possibly limit visibility based on template args. */
13911 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13912 if (DECL_VISIBILITY_SPECIFIED (t))
13913 {
13914 DECL_VISIBILITY_SPECIFIED (r) = 0;
13915 DECL_ATTRIBUTES (r)
13916 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13917 }
13918 determine_visibility (r);
13919 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13920 && !processing_template_decl)
13921 defaulted_late_check (r);
13922
13923 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13924 args, complain, in_decl);
13925 if (flag_openmp)
13926 if (tree attr = lookup_attribute ("omp declare variant base",
13927 DECL_ATTRIBUTES (r)))
13928 omp_declare_variant_finalize (r, attr);
13929
13930 return r;
13931 }
13932
13933 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13934
13935 static tree
13936 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13937 tree lambda_fntype)
13938 {
13939 /* We can get here when processing a member function template,
13940 member class template, or template template parameter. */
13941 tree decl = DECL_TEMPLATE_RESULT (t);
13942 tree in_decl = t;
13943 tree spec;
13944 tree tmpl_args;
13945 tree full_args;
13946 tree r;
13947 hashval_t hash = 0;
13948
13949 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13950 {
13951 /* Template template parameter is treated here. */
13952 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13953 if (new_type == error_mark_node)
13954 r = error_mark_node;
13955 /* If we get a real template back, return it. This can happen in
13956 the context of most_specialized_partial_spec. */
13957 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13958 r = new_type;
13959 else
13960 /* The new TEMPLATE_DECL was built in
13961 reduce_template_parm_level. */
13962 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13963 return r;
13964 }
13965
13966 if (!lambda_fntype)
13967 {
13968 /* We might already have an instance of this template.
13969 The ARGS are for the surrounding class type, so the
13970 full args contain the tsubst'd args for the context,
13971 plus the innermost args from the template decl. */
13972 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13973 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13974 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13975 /* Because this is a template, the arguments will still be
13976 dependent, even after substitution. If
13977 PROCESSING_TEMPLATE_DECL is not set, the dependency
13978 predicates will short-circuit. */
13979 ++processing_template_decl;
13980 full_args = tsubst_template_args (tmpl_args, args,
13981 complain, in_decl);
13982 --processing_template_decl;
13983 if (full_args == error_mark_node)
13984 return error_mark_node;
13985
13986 /* If this is a default template template argument,
13987 tsubst might not have changed anything. */
13988 if (full_args == tmpl_args)
13989 return t;
13990
13991 hash = hash_tmpl_and_args (t, full_args);
13992 spec = retrieve_specialization (t, full_args, hash);
13993 if (spec != NULL_TREE)
13994 {
13995 if (TYPE_P (spec))
13996 /* Type partial instantiations are stored as the type by
13997 lookup_template_class_1, not here as the template. */
13998 spec = CLASSTYPE_TI_TEMPLATE (spec);
13999 return spec;
14000 }
14001 }
14002
14003 /* Make a new template decl. It will be similar to the
14004 original, but will record the current template arguments.
14005 We also create a new function declaration, which is just
14006 like the old one, but points to this new template, rather
14007 than the old one. */
14008 r = copy_decl (t);
14009 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14010 DECL_CHAIN (r) = NULL_TREE;
14011
14012 // Build new template info linking to the original template decl.
14013 if (!lambda_fntype)
14014 {
14015 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14016 SET_DECL_IMPLICIT_INSTANTIATION (r);
14017 }
14018 else
14019 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14020
14021 /* The template parameters for this new template are all the
14022 template parameters for the old template, except the
14023 outermost level of parameters. */
14024 DECL_TEMPLATE_PARMS (r)
14025 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14026 complain);
14027
14028 if (TREE_CODE (decl) == TYPE_DECL
14029 && !TYPE_DECL_ALIAS_P (decl))
14030 {
14031 tree new_type;
14032 ++processing_template_decl;
14033 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14034 --processing_template_decl;
14035 if (new_type == error_mark_node)
14036 return error_mark_node;
14037
14038 TREE_TYPE (r) = new_type;
14039 /* For a partial specialization, we need to keep pointing to
14040 the primary template. */
14041 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14042 CLASSTYPE_TI_TEMPLATE (new_type) = r;
14043 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
14044 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
14045 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
14046 }
14047 else
14048 {
14049 tree new_decl;
14050 ++processing_template_decl;
14051 if (TREE_CODE (decl) == FUNCTION_DECL)
14052 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
14053 else
14054 new_decl = tsubst (decl, args, complain, in_decl);
14055 --processing_template_decl;
14056 if (new_decl == error_mark_node)
14057 return error_mark_node;
14058
14059 DECL_TEMPLATE_RESULT (r) = new_decl;
14060 TREE_TYPE (r) = TREE_TYPE (new_decl);
14061 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
14062 if (lambda_fntype)
14063 {
14064 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14065 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
14066 }
14067 else
14068 {
14069 DECL_TI_TEMPLATE (new_decl) = r;
14070 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
14071 }
14072 }
14073
14074 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14075 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14076
14077 if (PRIMARY_TEMPLATE_P (t))
14078 DECL_PRIMARY_TEMPLATE (r) = r;
14079
14080 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
14081 && !lambda_fntype)
14082 /* Record this non-type partial instantiation. */
14083 register_specialization (r, t,
14084 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14085 false, hash);
14086
14087 return r;
14088 }
14089
14090 /* True if FN is the op() for a lambda in an uninstantiated template. */
14091
14092 bool
14093 lambda_fn_in_template_p (tree fn)
14094 {
14095 if (!fn || !LAMBDA_FUNCTION_P (fn))
14096 return false;
14097 tree closure = DECL_CONTEXT (fn);
14098 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14099 }
14100
14101 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14102 which the above is true. */
14103
14104 bool
14105 instantiated_lambda_fn_p (tree fn)
14106 {
14107 if (!fn || !LAMBDA_FUNCTION_P (fn))
14108 return false;
14109 tree closure = DECL_CONTEXT (fn);
14110 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14111 return LAMBDA_EXPR_INSTANTIATED (lam);
14112 }
14113
14114 /* We're instantiating a variable from template function TCTX. Return the
14115 corresponding current enclosing scope. This gets complicated because lambda
14116 functions in templates are regenerated rather than instantiated, but generic
14117 lambda functions are subsequently instantiated. */
14118
14119 static tree
14120 enclosing_instantiation_of (tree otctx)
14121 {
14122 tree tctx = otctx;
14123 tree fn = current_function_decl;
14124 int lambda_count = 0;
14125
14126 for (; tctx && (lambda_fn_in_template_p (tctx)
14127 || instantiated_lambda_fn_p (tctx));
14128 tctx = decl_function_context (tctx))
14129 ++lambda_count;
14130 for (; fn; fn = decl_function_context (fn))
14131 {
14132 tree ofn = fn;
14133 int flambda_count = 0;
14134 for (; fn && instantiated_lambda_fn_p (fn);
14135 fn = decl_function_context (fn))
14136 ++flambda_count;
14137 if ((fn && DECL_TEMPLATE_INFO (fn))
14138 ? most_general_template (fn) != most_general_template (tctx)
14139 : fn != tctx)
14140 continue;
14141 if (flambda_count != lambda_count)
14142 {
14143 gcc_assert (flambda_count > lambda_count);
14144 for (; flambda_count > lambda_count; --flambda_count)
14145 ofn = decl_function_context (ofn);
14146 }
14147 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
14148 || DECL_CONV_FN_P (ofn));
14149 return ofn;
14150 }
14151 gcc_unreachable ();
14152 }
14153
14154 /* Substitute the ARGS into the T, which is a _DECL. Return the
14155 result of the substitution. Issue error and warning messages under
14156 control of COMPLAIN. */
14157
14158 static tree
14159 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14160 {
14161 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14162 location_t saved_loc;
14163 tree r = NULL_TREE;
14164 tree in_decl = t;
14165 hashval_t hash = 0;
14166
14167 /* Set the filename and linenumber to improve error-reporting. */
14168 saved_loc = input_location;
14169 input_location = DECL_SOURCE_LOCATION (t);
14170
14171 switch (TREE_CODE (t))
14172 {
14173 case TEMPLATE_DECL:
14174 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14175 break;
14176
14177 case FUNCTION_DECL:
14178 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14179 break;
14180
14181 case PARM_DECL:
14182 {
14183 tree type = NULL_TREE;
14184 int i, len = 1;
14185 tree expanded_types = NULL_TREE;
14186 tree prev_r = NULL_TREE;
14187 tree first_r = NULL_TREE;
14188
14189 if (DECL_PACK_P (t))
14190 {
14191 /* If there is a local specialization that isn't a
14192 parameter pack, it means that we're doing a "simple"
14193 substitution from inside tsubst_pack_expansion. Just
14194 return the local specialization (which will be a single
14195 parm). */
14196 tree spec = retrieve_local_specialization (t);
14197 if (spec
14198 && TREE_CODE (spec) == PARM_DECL
14199 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14200 RETURN (spec);
14201
14202 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14203 the parameters in this function parameter pack. */
14204 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14205 complain, in_decl);
14206 if (TREE_CODE (expanded_types) == TREE_VEC)
14207 {
14208 len = TREE_VEC_LENGTH (expanded_types);
14209
14210 /* Zero-length parameter packs are boring. Just substitute
14211 into the chain. */
14212 if (len == 0 && !cp_unevaluated_operand)
14213 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14214 TREE_CHAIN (t)));
14215 }
14216 else
14217 {
14218 /* All we did was update the type. Make a note of that. */
14219 type = expanded_types;
14220 expanded_types = NULL_TREE;
14221 }
14222 }
14223
14224 /* Loop through all of the parameters we'll build. When T is
14225 a function parameter pack, LEN is the number of expanded
14226 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14227 r = NULL_TREE;
14228 for (i = 0; i < len; ++i)
14229 {
14230 prev_r = r;
14231 r = copy_node (t);
14232 if (DECL_TEMPLATE_PARM_P (t))
14233 SET_DECL_TEMPLATE_PARM_P (r);
14234
14235 if (expanded_types)
14236 /* We're on the Ith parameter of the function parameter
14237 pack. */
14238 {
14239 /* Get the Ith type. */
14240 type = TREE_VEC_ELT (expanded_types, i);
14241
14242 /* Rename the parameter to include the index. */
14243 DECL_NAME (r)
14244 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14245 }
14246 else if (!type)
14247 /* We're dealing with a normal parameter. */
14248 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14249
14250 type = type_decays_to (type);
14251 TREE_TYPE (r) = type;
14252 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14253
14254 if (DECL_INITIAL (r))
14255 {
14256 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14257 DECL_INITIAL (r) = TREE_TYPE (r);
14258 else
14259 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14260 complain, in_decl);
14261 }
14262
14263 DECL_CONTEXT (r) = NULL_TREE;
14264
14265 if (!DECL_TEMPLATE_PARM_P (r))
14266 DECL_ARG_TYPE (r) = type_passed_as (type);
14267
14268 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14269 args, complain, in_decl);
14270
14271 /* Keep track of the first new parameter we
14272 generate. That's what will be returned to the
14273 caller. */
14274 if (!first_r)
14275 first_r = r;
14276
14277 /* Build a proper chain of parameters when substituting
14278 into a function parameter pack. */
14279 if (prev_r)
14280 DECL_CHAIN (prev_r) = r;
14281 }
14282
14283 /* If cp_unevaluated_operand is set, we're just looking for a
14284 single dummy parameter, so don't keep going. */
14285 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14286 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14287 complain, DECL_CHAIN (t));
14288
14289 /* FIRST_R contains the start of the chain we've built. */
14290 r = first_r;
14291 }
14292 break;
14293
14294 case FIELD_DECL:
14295 {
14296 tree type = NULL_TREE;
14297 tree vec = NULL_TREE;
14298 tree expanded_types = NULL_TREE;
14299 int len = 1;
14300
14301 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14302 {
14303 /* This field is a lambda capture pack. Return a TREE_VEC of
14304 the expanded fields to instantiate_class_template_1. */
14305 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14306 complain, in_decl);
14307 if (TREE_CODE (expanded_types) == TREE_VEC)
14308 {
14309 len = TREE_VEC_LENGTH (expanded_types);
14310 vec = make_tree_vec (len);
14311 }
14312 else
14313 {
14314 /* All we did was update the type. Make a note of that. */
14315 type = expanded_types;
14316 expanded_types = NULL_TREE;
14317 }
14318 }
14319
14320 for (int i = 0; i < len; ++i)
14321 {
14322 r = copy_decl (t);
14323 if (expanded_types)
14324 {
14325 type = TREE_VEC_ELT (expanded_types, i);
14326 DECL_NAME (r)
14327 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14328 }
14329 else if (!type)
14330 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14331
14332 if (type == error_mark_node)
14333 RETURN (error_mark_node);
14334 TREE_TYPE (r) = type;
14335 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14336
14337 if (DECL_C_BIT_FIELD (r))
14338 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14339 number of bits. */
14340 DECL_BIT_FIELD_REPRESENTATIVE (r)
14341 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14342 complain, in_decl,
14343 /*integral_constant_expression_p=*/true);
14344 if (DECL_INITIAL (t))
14345 {
14346 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14347 NSDMI in perform_member_init. Still set DECL_INITIAL
14348 so that we know there is one. */
14349 DECL_INITIAL (r) = void_node;
14350 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14351 retrofit_lang_decl (r);
14352 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14353 }
14354 /* We don't have to set DECL_CONTEXT here; it is set by
14355 finish_member_declaration. */
14356 DECL_CHAIN (r) = NULL_TREE;
14357
14358 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14359 args, complain, in_decl);
14360
14361 if (vec)
14362 TREE_VEC_ELT (vec, i) = r;
14363 }
14364
14365 if (vec)
14366 r = vec;
14367 }
14368 break;
14369
14370 case USING_DECL:
14371 /* We reach here only for member using decls. We also need to check
14372 uses_template_parms because DECL_DEPENDENT_P is not set for a
14373 using-declaration that designates a member of the current
14374 instantiation (c++/53549). */
14375 if (DECL_DEPENDENT_P (t)
14376 || uses_template_parms (USING_DECL_SCOPE (t)))
14377 {
14378 tree scope = USING_DECL_SCOPE (t);
14379 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14380 if (PACK_EXPANSION_P (scope))
14381 {
14382 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14383 int len = TREE_VEC_LENGTH (vec);
14384 r = make_tree_vec (len);
14385 for (int i = 0; i < len; ++i)
14386 {
14387 tree escope = TREE_VEC_ELT (vec, i);
14388 tree elt = do_class_using_decl (escope, name);
14389 if (!elt)
14390 {
14391 r = error_mark_node;
14392 break;
14393 }
14394 else
14395 {
14396 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14397 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14398 }
14399 TREE_VEC_ELT (r, i) = elt;
14400 }
14401 }
14402 else
14403 {
14404 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14405 complain, in_decl);
14406 r = do_class_using_decl (inst_scope, name);
14407 if (!r)
14408 r = error_mark_node;
14409 else
14410 {
14411 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14412 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14413 }
14414 }
14415 }
14416 else
14417 {
14418 r = copy_node (t);
14419 DECL_CHAIN (r) = NULL_TREE;
14420 }
14421 break;
14422
14423 case TYPE_DECL:
14424 case VAR_DECL:
14425 {
14426 tree argvec = NULL_TREE;
14427 tree gen_tmpl = NULL_TREE;
14428 tree spec;
14429 tree tmpl = NULL_TREE;
14430 tree ctx;
14431 tree type = NULL_TREE;
14432 bool local_p;
14433
14434 if (TREE_TYPE (t) == error_mark_node)
14435 RETURN (error_mark_node);
14436
14437 if (TREE_CODE (t) == TYPE_DECL
14438 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14439 {
14440 /* If this is the canonical decl, we don't have to
14441 mess with instantiations, and often we can't (for
14442 typename, template type parms and such). Note that
14443 TYPE_NAME is not correct for the above test if
14444 we've copied the type for a typedef. */
14445 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14446 if (type == error_mark_node)
14447 RETURN (error_mark_node);
14448 r = TYPE_NAME (type);
14449 break;
14450 }
14451
14452 /* Check to see if we already have the specialization we
14453 need. */
14454 spec = NULL_TREE;
14455 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
14456 {
14457 /* T is a static data member or namespace-scope entity.
14458 We have to substitute into namespace-scope variables
14459 (not just variable templates) because of cases like:
14460
14461 template <class T> void f() { extern T t; }
14462
14463 where the entity referenced is not known until
14464 instantiation time. */
14465 local_p = false;
14466 ctx = DECL_CONTEXT (t);
14467 if (DECL_CLASS_SCOPE_P (t))
14468 {
14469 ctx = tsubst_aggr_type (ctx, args,
14470 complain,
14471 in_decl, /*entering_scope=*/1);
14472 /* If CTX is unchanged, then T is in fact the
14473 specialization we want. That situation occurs when
14474 referencing a static data member within in its own
14475 class. We can use pointer equality, rather than
14476 same_type_p, because DECL_CONTEXT is always
14477 canonical... */
14478 if (ctx == DECL_CONTEXT (t)
14479 /* ... unless T is a member template; in which
14480 case our caller can be willing to create a
14481 specialization of that template represented
14482 by T. */
14483 && !(DECL_TI_TEMPLATE (t)
14484 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14485 spec = t;
14486 }
14487
14488 if (!spec)
14489 {
14490 tmpl = DECL_TI_TEMPLATE (t);
14491 gen_tmpl = most_general_template (tmpl);
14492 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14493 if (argvec != error_mark_node)
14494 argvec = (coerce_innermost_template_parms
14495 (DECL_TEMPLATE_PARMS (gen_tmpl),
14496 argvec, t, complain,
14497 /*all*/true, /*defarg*/true));
14498 if (argvec == error_mark_node)
14499 RETURN (error_mark_node);
14500 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14501 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14502 }
14503 }
14504 else
14505 {
14506 /* A local variable. */
14507 local_p = true;
14508 /* Subsequent calls to pushdecl will fill this in. */
14509 ctx = NULL_TREE;
14510 /* Unless this is a reference to a static variable from an
14511 enclosing function, in which case we need to fill it in now. */
14512 if (TREE_STATIC (t))
14513 {
14514 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14515 if (fn != current_function_decl)
14516 ctx = fn;
14517 }
14518 spec = retrieve_local_specialization (t);
14519 }
14520 /* If we already have the specialization we need, there is
14521 nothing more to do. */
14522 if (spec)
14523 {
14524 r = spec;
14525 break;
14526 }
14527
14528 /* Create a new node for the specialization we need. */
14529 if (type == NULL_TREE)
14530 {
14531 if (is_typedef_decl (t))
14532 type = DECL_ORIGINAL_TYPE (t);
14533 else
14534 type = TREE_TYPE (t);
14535 if (VAR_P (t)
14536 && VAR_HAD_UNKNOWN_BOUND (t)
14537 && type != error_mark_node)
14538 type = strip_array_domain (type);
14539 tree sub_args = args;
14540 if (tree auto_node = type_uses_auto (type))
14541 {
14542 /* Mask off any template args past the variable's context so we
14543 don't replace the auto with an unrelated argument. */
14544 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14545 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14546 if (extra > 0)
14547 /* This should never happen with the new lambda instantiation
14548 model, but keep the handling just in case. */
14549 gcc_assert (!CHECKING_P),
14550 sub_args = strip_innermost_template_args (args, extra);
14551 }
14552 type = tsubst (type, sub_args, complain, in_decl);
14553 /* Substituting the type might have recursively instantiated this
14554 same alias (c++/86171). */
14555 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14556 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14557 {
14558 r = spec;
14559 break;
14560 }
14561 }
14562 r = copy_decl (t);
14563 if (VAR_P (r))
14564 {
14565 DECL_INITIALIZED_P (r) = 0;
14566 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14567 if (type == error_mark_node)
14568 RETURN (error_mark_node);
14569 if (TREE_CODE (type) == FUNCTION_TYPE)
14570 {
14571 /* It may seem that this case cannot occur, since:
14572
14573 typedef void f();
14574 void g() { f x; }
14575
14576 declares a function, not a variable. However:
14577
14578 typedef void f();
14579 template <typename T> void g() { T t; }
14580 template void g<f>();
14581
14582 is an attempt to declare a variable with function
14583 type. */
14584 error ("variable %qD has function type",
14585 /* R is not yet sufficiently initialized, so we
14586 just use its name. */
14587 DECL_NAME (r));
14588 RETURN (error_mark_node);
14589 }
14590 type = complete_type (type);
14591 /* Wait until cp_finish_decl to set this again, to handle
14592 circular dependency (template/instantiate6.C). */
14593 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14594 type = check_var_type (DECL_NAME (r), type,
14595 DECL_SOURCE_LOCATION (r));
14596 if (DECL_HAS_VALUE_EXPR_P (t))
14597 {
14598 tree ve = DECL_VALUE_EXPR (t);
14599 /* If the DECL_VALUE_EXPR is converted to the declared type,
14600 preserve the identity so that gimplify_type_sizes works. */
14601 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14602 if (nop)
14603 ve = TREE_OPERAND (ve, 0);
14604 ve = tsubst_expr (ve, args, complain, in_decl,
14605 /*constant_expression_p=*/false);
14606 if (REFERENCE_REF_P (ve))
14607 {
14608 gcc_assert (TYPE_REF_P (type));
14609 ve = TREE_OPERAND (ve, 0);
14610 }
14611 if (nop)
14612 ve = build_nop (type, ve);
14613 else
14614 gcc_checking_assert (TREE_TYPE (ve) == type);
14615 SET_DECL_VALUE_EXPR (r, ve);
14616 }
14617 if (CP_DECL_THREAD_LOCAL_P (r)
14618 && !processing_template_decl)
14619 set_decl_tls_model (r, decl_default_tls_model (r));
14620 }
14621 else if (DECL_SELF_REFERENCE_P (t))
14622 SET_DECL_SELF_REFERENCE_P (r);
14623 TREE_TYPE (r) = type;
14624 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14625 DECL_CONTEXT (r) = ctx;
14626 /* Clear out the mangled name and RTL for the instantiation. */
14627 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14628 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14629 SET_DECL_RTL (r, NULL);
14630 /* The initializer must not be expanded until it is required;
14631 see [temp.inst]. */
14632 DECL_INITIAL (r) = NULL_TREE;
14633 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14634 if (VAR_P (r))
14635 {
14636 if (DECL_LANG_SPECIFIC (r))
14637 SET_DECL_DEPENDENT_INIT_P (r, false);
14638
14639 SET_DECL_MODE (r, VOIDmode);
14640
14641 /* Possibly limit visibility based on template args. */
14642 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14643 if (DECL_VISIBILITY_SPECIFIED (t))
14644 {
14645 DECL_VISIBILITY_SPECIFIED (r) = 0;
14646 DECL_ATTRIBUTES (r)
14647 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14648 }
14649 determine_visibility (r);
14650 }
14651
14652 if (!local_p)
14653 {
14654 /* A static data member declaration is always marked
14655 external when it is declared in-class, even if an
14656 initializer is present. We mimic the non-template
14657 processing here. */
14658 DECL_EXTERNAL (r) = 1;
14659 if (DECL_NAMESPACE_SCOPE_P (t))
14660 DECL_NOT_REALLY_EXTERN (r) = 1;
14661
14662 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14663 SET_DECL_IMPLICIT_INSTANTIATION (r);
14664 /* Remember whether we require constant initialization of
14665 a non-constant template variable. */
14666 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
14667 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
14668 if (!error_operand_p (r) || (complain & tf_error))
14669 register_specialization (r, gen_tmpl, argvec, false, hash);
14670 }
14671 else
14672 {
14673 if (DECL_LANG_SPECIFIC (r))
14674 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14675 if (!cp_unevaluated_operand)
14676 register_local_specialization (r, t);
14677 }
14678
14679 DECL_CHAIN (r) = NULL_TREE;
14680
14681 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14682 /*flags=*/0,
14683 args, complain, in_decl);
14684
14685 /* Preserve a typedef that names a type. */
14686 if (is_typedef_decl (r) && type != error_mark_node)
14687 {
14688 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14689 set_underlying_type (r);
14690 if (TYPE_DECL_ALIAS_P (r))
14691 /* An alias template specialization can be dependent
14692 even if its underlying type is not. */
14693 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14694 }
14695
14696 layout_decl (r, 0);
14697 }
14698 break;
14699
14700 default:
14701 gcc_unreachable ();
14702 }
14703 #undef RETURN
14704
14705 out:
14706 /* Restore the file and line information. */
14707 input_location = saved_loc;
14708
14709 return r;
14710 }
14711
14712 /* Substitute into the complete parameter type list PARMS. */
14713
14714 tree
14715 tsubst_function_parms (tree parms,
14716 tree args,
14717 tsubst_flags_t complain,
14718 tree in_decl)
14719 {
14720 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14721 }
14722
14723 /* Substitute into the ARG_TYPES of a function type.
14724 If END is a TREE_CHAIN, leave it and any following types
14725 un-substituted. */
14726
14727 static tree
14728 tsubst_arg_types (tree arg_types,
14729 tree args,
14730 tree end,
14731 tsubst_flags_t complain,
14732 tree in_decl)
14733 {
14734 tree remaining_arg_types;
14735 tree type = NULL_TREE;
14736 int i = 1;
14737 tree expanded_args = NULL_TREE;
14738 tree default_arg;
14739
14740 if (!arg_types || arg_types == void_list_node || arg_types == end)
14741 return arg_types;
14742
14743 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14744 args, end, complain, in_decl);
14745 if (remaining_arg_types == error_mark_node)
14746 return error_mark_node;
14747
14748 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14749 {
14750 /* For a pack expansion, perform substitution on the
14751 entire expression. Later on, we'll handle the arguments
14752 one-by-one. */
14753 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14754 args, complain, in_decl);
14755
14756 if (TREE_CODE (expanded_args) == TREE_VEC)
14757 /* So that we'll spin through the parameters, one by one. */
14758 i = TREE_VEC_LENGTH (expanded_args);
14759 else
14760 {
14761 /* We only partially substituted into the parameter
14762 pack. Our type is TYPE_PACK_EXPANSION. */
14763 type = expanded_args;
14764 expanded_args = NULL_TREE;
14765 }
14766 }
14767
14768 while (i > 0) {
14769 --i;
14770
14771 if (expanded_args)
14772 type = TREE_VEC_ELT (expanded_args, i);
14773 else if (!type)
14774 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14775
14776 if (type == error_mark_node)
14777 return error_mark_node;
14778 if (VOID_TYPE_P (type))
14779 {
14780 if (complain & tf_error)
14781 {
14782 error ("invalid parameter type %qT", type);
14783 if (in_decl)
14784 error ("in declaration %q+D", in_decl);
14785 }
14786 return error_mark_node;
14787 }
14788 /* DR 657. */
14789 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14790 return error_mark_node;
14791
14792 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14793 top-level qualifiers as required. */
14794 type = cv_unqualified (type_decays_to (type));
14795
14796 /* We do not substitute into default arguments here. The standard
14797 mandates that they be instantiated only when needed, which is
14798 done in build_over_call. */
14799 default_arg = TREE_PURPOSE (arg_types);
14800
14801 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14802 since the new op() won't have any associated template arguments for us
14803 to refer to later. */
14804 if (lambda_fn_in_template_p (in_decl))
14805 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14806 false/*fn*/, false/*constexpr*/);
14807
14808 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14809 {
14810 /* We've instantiated a template before its default arguments
14811 have been parsed. This can happen for a nested template
14812 class, and is not an error unless we require the default
14813 argument in a call of this function. */
14814 remaining_arg_types =
14815 tree_cons (default_arg, type, remaining_arg_types);
14816 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14817 remaining_arg_types);
14818 }
14819 else
14820 remaining_arg_types =
14821 hash_tree_cons (default_arg, type, remaining_arg_types);
14822 }
14823
14824 return remaining_arg_types;
14825 }
14826
14827 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14828 *not* handle the exception-specification for FNTYPE, because the
14829 initial substitution of explicitly provided template parameters
14830 during argument deduction forbids substitution into the
14831 exception-specification:
14832
14833 [temp.deduct]
14834
14835 All references in the function type of the function template to the
14836 corresponding template parameters are replaced by the specified tem-
14837 plate argument values. If a substitution in a template parameter or
14838 in the function type of the function template results in an invalid
14839 type, type deduction fails. [Note: The equivalent substitution in
14840 exception specifications is done only when the function is instanti-
14841 ated, at which point a program is ill-formed if the substitution
14842 results in an invalid type.] */
14843
14844 static tree
14845 tsubst_function_type (tree t,
14846 tree args,
14847 tsubst_flags_t complain,
14848 tree in_decl)
14849 {
14850 tree return_type;
14851 tree arg_types = NULL_TREE;
14852
14853 /* The TYPE_CONTEXT is not used for function/method types. */
14854 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14855
14856 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14857 failure. */
14858 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14859
14860 if (late_return_type_p)
14861 {
14862 /* Substitute the argument types. */
14863 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14864 complain, in_decl);
14865 if (arg_types == error_mark_node)
14866 return error_mark_node;
14867
14868 tree save_ccp = current_class_ptr;
14869 tree save_ccr = current_class_ref;
14870 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14871 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14872 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14873 if (do_inject)
14874 {
14875 /* DR 1207: 'this' is in scope in the trailing return type. */
14876 inject_this_parameter (this_type, cp_type_quals (this_type));
14877 }
14878
14879 /* Substitute the return type. */
14880 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14881
14882 if (do_inject)
14883 {
14884 current_class_ptr = save_ccp;
14885 current_class_ref = save_ccr;
14886 }
14887 }
14888 else
14889 /* Substitute the return type. */
14890 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14891
14892 if (return_type == error_mark_node)
14893 return error_mark_node;
14894 /* DR 486 clarifies that creation of a function type with an
14895 invalid return type is a deduction failure. */
14896 if (TREE_CODE (return_type) == ARRAY_TYPE
14897 || TREE_CODE (return_type) == FUNCTION_TYPE)
14898 {
14899 if (complain & tf_error)
14900 {
14901 if (TREE_CODE (return_type) == ARRAY_TYPE)
14902 error ("function returning an array");
14903 else
14904 error ("function returning a function");
14905 }
14906 return error_mark_node;
14907 }
14908 /* And DR 657. */
14909 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14910 return error_mark_node;
14911
14912 if (!late_return_type_p)
14913 {
14914 /* Substitute the argument types. */
14915 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14916 complain, in_decl);
14917 if (arg_types == error_mark_node)
14918 return error_mark_node;
14919 }
14920
14921 /* Construct a new type node and return it. */
14922 return rebuild_function_or_method_type (t, return_type, arg_types,
14923 /*raises=*/NULL_TREE, complain);
14924 }
14925
14926 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14927 ARGS into that specification, and return the substituted
14928 specification. If there is no specification, return NULL_TREE. */
14929
14930 static tree
14931 tsubst_exception_specification (tree fntype,
14932 tree args,
14933 tsubst_flags_t complain,
14934 tree in_decl,
14935 bool defer_ok)
14936 {
14937 tree specs;
14938 tree new_specs;
14939
14940 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14941 new_specs = NULL_TREE;
14942 if (specs && TREE_PURPOSE (specs))
14943 {
14944 /* A noexcept-specifier. */
14945 tree expr = TREE_PURPOSE (specs);
14946 if (TREE_CODE (expr) == INTEGER_CST)
14947 new_specs = expr;
14948 else if (defer_ok)
14949 {
14950 /* Defer instantiation of noexcept-specifiers to avoid
14951 excessive instantiations (c++/49107). */
14952 new_specs = make_node (DEFERRED_NOEXCEPT);
14953 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14954 {
14955 /* We already partially instantiated this member template,
14956 so combine the new args with the old. */
14957 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14958 = DEFERRED_NOEXCEPT_PATTERN (expr);
14959 DEFERRED_NOEXCEPT_ARGS (new_specs)
14960 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14961 }
14962 else
14963 {
14964 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14965 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14966 }
14967 }
14968 else
14969 {
14970 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14971 {
14972 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14973 args);
14974 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14975 }
14976 new_specs = tsubst_copy_and_build
14977 (expr, args, complain, in_decl, /*function_p=*/false,
14978 /*integral_constant_expression_p=*/true);
14979 }
14980 new_specs = build_noexcept_spec (new_specs, complain);
14981 }
14982 else if (specs)
14983 {
14984 if (! TREE_VALUE (specs))
14985 new_specs = specs;
14986 else
14987 while (specs)
14988 {
14989 tree spec;
14990 int i, len = 1;
14991 tree expanded_specs = NULL_TREE;
14992
14993 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14994 {
14995 /* Expand the pack expansion type. */
14996 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14997 args, complain,
14998 in_decl);
14999
15000 if (expanded_specs == error_mark_node)
15001 return error_mark_node;
15002 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15003 len = TREE_VEC_LENGTH (expanded_specs);
15004 else
15005 {
15006 /* We're substituting into a member template, so
15007 we got a TYPE_PACK_EXPANSION back. Add that
15008 expansion and move on. */
15009 gcc_assert (TREE_CODE (expanded_specs)
15010 == TYPE_PACK_EXPANSION);
15011 new_specs = add_exception_specifier (new_specs,
15012 expanded_specs,
15013 complain);
15014 specs = TREE_CHAIN (specs);
15015 continue;
15016 }
15017 }
15018
15019 for (i = 0; i < len; ++i)
15020 {
15021 if (expanded_specs)
15022 spec = TREE_VEC_ELT (expanded_specs, i);
15023 else
15024 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15025 if (spec == error_mark_node)
15026 return spec;
15027 new_specs = add_exception_specifier (new_specs, spec,
15028 complain);
15029 }
15030
15031 specs = TREE_CHAIN (specs);
15032 }
15033 }
15034 return new_specs;
15035 }
15036
15037 /* Take the tree structure T and replace template parameters used
15038 therein with the argument vector ARGS. IN_DECL is an associated
15039 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15040 Issue error and warning messages under control of COMPLAIN. Note
15041 that we must be relatively non-tolerant of extensions here, in
15042 order to preserve conformance; if we allow substitutions that
15043 should not be allowed, we may allow argument deductions that should
15044 not succeed, and therefore report ambiguous overload situations
15045 where there are none. In theory, we could allow the substitution,
15046 but indicate that it should have failed, and allow our caller to
15047 make sure that the right thing happens, but we don't try to do this
15048 yet.
15049
15050 This function is used for dealing with types, decls and the like;
15051 for expressions, use tsubst_expr or tsubst_copy. */
15052
15053 tree
15054 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15055 {
15056 enum tree_code code;
15057 tree type, r = NULL_TREE;
15058
15059 if (t == NULL_TREE || t == error_mark_node
15060 || t == integer_type_node
15061 || t == void_type_node
15062 || t == char_type_node
15063 || t == unknown_type_node
15064 || TREE_CODE (t) == NAMESPACE_DECL
15065 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15066 return t;
15067
15068 if (DECL_P (t))
15069 return tsubst_decl (t, args, complain);
15070
15071 if (args == NULL_TREE)
15072 return t;
15073
15074 code = TREE_CODE (t);
15075
15076 if (code == IDENTIFIER_NODE)
15077 type = IDENTIFIER_TYPE_VALUE (t);
15078 else
15079 type = TREE_TYPE (t);
15080
15081 gcc_assert (type != unknown_type_node);
15082
15083 /* Reuse typedefs. We need to do this to handle dependent attributes,
15084 such as attribute aligned. */
15085 if (TYPE_P (t)
15086 && typedef_variant_p (t))
15087 {
15088 tree decl = TYPE_NAME (t);
15089
15090 if (alias_template_specialization_p (t, nt_opaque))
15091 {
15092 /* DECL represents an alias template and we want to
15093 instantiate it. */
15094 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15095 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15096 r = instantiate_alias_template (tmpl, gen_args, complain);
15097 }
15098 else if (DECL_CLASS_SCOPE_P (decl)
15099 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15100 && uses_template_parms (DECL_CONTEXT (decl)))
15101 {
15102 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15103 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15104 r = retrieve_specialization (tmpl, gen_args, 0);
15105 }
15106 else if (DECL_FUNCTION_SCOPE_P (decl)
15107 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15108 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15109 r = retrieve_local_specialization (decl);
15110 else
15111 /* The typedef is from a non-template context. */
15112 return t;
15113
15114 if (r)
15115 {
15116 r = TREE_TYPE (r);
15117 r = cp_build_qualified_type_real
15118 (r, cp_type_quals (t) | cp_type_quals (r),
15119 complain | tf_ignore_bad_quals);
15120 return r;
15121 }
15122 else
15123 {
15124 /* We don't have an instantiation yet, so drop the typedef. */
15125 int quals = cp_type_quals (t);
15126 t = DECL_ORIGINAL_TYPE (decl);
15127 t = cp_build_qualified_type_real (t, quals,
15128 complain | tf_ignore_bad_quals);
15129 }
15130 }
15131
15132 bool fndecl_type = (complain & tf_fndecl_type);
15133 complain &= ~tf_fndecl_type;
15134
15135 if (type
15136 && code != TYPENAME_TYPE
15137 && code != TEMPLATE_TYPE_PARM
15138 && code != TEMPLATE_PARM_INDEX
15139 && code != IDENTIFIER_NODE
15140 && code != FUNCTION_TYPE
15141 && code != METHOD_TYPE)
15142 type = tsubst (type, args, complain, in_decl);
15143 if (type == error_mark_node)
15144 return error_mark_node;
15145
15146 switch (code)
15147 {
15148 case RECORD_TYPE:
15149 case UNION_TYPE:
15150 case ENUMERAL_TYPE:
15151 return tsubst_aggr_type (t, args, complain, in_decl,
15152 /*entering_scope=*/0);
15153
15154 case ERROR_MARK:
15155 case IDENTIFIER_NODE:
15156 case VOID_TYPE:
15157 case REAL_TYPE:
15158 case COMPLEX_TYPE:
15159 case VECTOR_TYPE:
15160 case BOOLEAN_TYPE:
15161 case NULLPTR_TYPE:
15162 case LANG_TYPE:
15163 return t;
15164
15165 case INTEGER_TYPE:
15166 if (t == integer_type_node)
15167 return t;
15168
15169 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15170 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15171 return t;
15172
15173 {
15174 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15175
15176 max = tsubst_expr (omax, args, complain, in_decl,
15177 /*integral_constant_expression_p=*/false);
15178
15179 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15180 needed. */
15181 if (TREE_CODE (max) == NOP_EXPR
15182 && TREE_SIDE_EFFECTS (omax)
15183 && !TREE_TYPE (max))
15184 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15185
15186 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15187 with TREE_SIDE_EFFECTS that indicates this is not an integral
15188 constant expression. */
15189 if (processing_template_decl
15190 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15191 {
15192 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15193 TREE_SIDE_EFFECTS (max) = 1;
15194 }
15195
15196 return compute_array_index_type (NULL_TREE, max, complain);
15197 }
15198
15199 case TEMPLATE_TYPE_PARM:
15200 case TEMPLATE_TEMPLATE_PARM:
15201 case BOUND_TEMPLATE_TEMPLATE_PARM:
15202 case TEMPLATE_PARM_INDEX:
15203 {
15204 int idx;
15205 int level;
15206 int levels;
15207 tree arg = NULL_TREE;
15208
15209 r = NULL_TREE;
15210
15211 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15212 template_parm_level_and_index (t, &level, &idx);
15213
15214 levels = TMPL_ARGS_DEPTH (args);
15215 if (level <= levels
15216 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15217 {
15218 arg = TMPL_ARG (args, level, idx);
15219
15220 /* See through ARGUMENT_PACK_SELECT arguments. */
15221 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15222 arg = argument_pack_select_arg (arg);
15223 }
15224
15225 if (arg == error_mark_node)
15226 return error_mark_node;
15227 else if (arg != NULL_TREE)
15228 {
15229 if (ARGUMENT_PACK_P (arg))
15230 /* If ARG is an argument pack, we don't actually want to
15231 perform a substitution here, because substitutions
15232 for argument packs are only done
15233 element-by-element. We can get to this point when
15234 substituting the type of a non-type template
15235 parameter pack, when that type actually contains
15236 template parameter packs from an outer template, e.g.,
15237
15238 template<typename... Types> struct A {
15239 template<Types... Values> struct B { };
15240 }; */
15241 return t;
15242
15243 if (code == TEMPLATE_TYPE_PARM)
15244 {
15245 int quals;
15246
15247 /* When building concept checks for the purpose of
15248 deducing placeholders, we can end up with wildcards
15249 where types are expected. Adjust this to the deduced
15250 value. */
15251 if (TREE_CODE (arg) == WILDCARD_DECL)
15252 arg = TREE_TYPE (TREE_TYPE (arg));
15253
15254 gcc_assert (TYPE_P (arg));
15255
15256 quals = cp_type_quals (arg) | cp_type_quals (t);
15257
15258 return cp_build_qualified_type_real
15259 (arg, quals, complain | tf_ignore_bad_quals);
15260 }
15261 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15262 {
15263 /* We are processing a type constructed from a
15264 template template parameter. */
15265 tree argvec = tsubst (TYPE_TI_ARGS (t),
15266 args, complain, in_decl);
15267 if (argvec == error_mark_node)
15268 return error_mark_node;
15269
15270 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15271 || TREE_CODE (arg) == TEMPLATE_DECL
15272 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15273
15274 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15275 /* Consider this code:
15276
15277 template <template <class> class Template>
15278 struct Internal {
15279 template <class Arg> using Bind = Template<Arg>;
15280 };
15281
15282 template <template <class> class Template, class Arg>
15283 using Instantiate = Template<Arg>; //#0
15284
15285 template <template <class> class Template,
15286 class Argument>
15287 using Bind =
15288 Instantiate<Internal<Template>::template Bind,
15289 Argument>; //#1
15290
15291 When #1 is parsed, the
15292 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15293 parameter `Template' in #0 matches the
15294 UNBOUND_CLASS_TEMPLATE representing the argument
15295 `Internal<Template>::template Bind'; We then want
15296 to assemble the type `Bind<Argument>' that can't
15297 be fully created right now, because
15298 `Internal<Template>' not being complete, the Bind
15299 template cannot be looked up in that context. So
15300 we need to "store" `Bind<Argument>' for later
15301 when the context of Bind becomes complete. Let's
15302 store that in a TYPENAME_TYPE. */
15303 return make_typename_type (TYPE_CONTEXT (arg),
15304 build_nt (TEMPLATE_ID_EXPR,
15305 TYPE_IDENTIFIER (arg),
15306 argvec),
15307 typename_type,
15308 complain);
15309
15310 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15311 are resolving nested-types in the signature of a
15312 member function templates. Otherwise ARG is a
15313 TEMPLATE_DECL and is the real template to be
15314 instantiated. */
15315 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15316 arg = TYPE_NAME (arg);
15317
15318 r = lookup_template_class (arg,
15319 argvec, in_decl,
15320 DECL_CONTEXT (arg),
15321 /*entering_scope=*/0,
15322 complain);
15323 return cp_build_qualified_type_real
15324 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15325 }
15326 else if (code == TEMPLATE_TEMPLATE_PARM)
15327 return arg;
15328 else
15329 /* TEMPLATE_PARM_INDEX. */
15330 return convert_from_reference (unshare_expr (arg));
15331 }
15332
15333 if (level == 1)
15334 /* This can happen during the attempted tsubst'ing in
15335 unify. This means that we don't yet have any information
15336 about the template parameter in question. */
15337 return t;
15338
15339 /* Early in template argument deduction substitution, we don't
15340 want to reduce the level of 'auto', or it will be confused
15341 with a normal template parm in subsequent deduction.
15342 Similarly, don't reduce the level of template parameters to
15343 avoid mismatches when deducing their types. */
15344 if (complain & tf_partial)
15345 return t;
15346
15347 /* If we get here, we must have been looking at a parm for a
15348 more deeply nested template. Make a new version of this
15349 template parameter, but with a lower level. */
15350 switch (code)
15351 {
15352 case TEMPLATE_TYPE_PARM:
15353 case TEMPLATE_TEMPLATE_PARM:
15354 case BOUND_TEMPLATE_TEMPLATE_PARM:
15355 if (cp_type_quals (t))
15356 {
15357 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15358 r = cp_build_qualified_type_real
15359 (r, cp_type_quals (t),
15360 complain | (code == TEMPLATE_TYPE_PARM
15361 ? tf_ignore_bad_quals : 0));
15362 }
15363 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15364 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15365 && (r = (TEMPLATE_PARM_DESCENDANTS
15366 (TEMPLATE_TYPE_PARM_INDEX (t))))
15367 && (r = TREE_TYPE (r))
15368 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15369 /* Break infinite recursion when substituting the constraints
15370 of a constrained placeholder. */;
15371 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15372 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15373 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15374 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15375 r = TEMPLATE_PARM_DESCENDANTS (arg))
15376 && (TEMPLATE_PARM_LEVEL (r)
15377 == TEMPLATE_PARM_LEVEL (arg) - levels))
15378 /* Cache the simple case of lowering a type parameter. */
15379 r = TREE_TYPE (r);
15380 else
15381 {
15382 r = copy_type (t);
15383 TEMPLATE_TYPE_PARM_INDEX (r)
15384 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15385 r, levels, args, complain);
15386 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15387 TYPE_MAIN_VARIANT (r) = r;
15388 TYPE_POINTER_TO (r) = NULL_TREE;
15389 TYPE_REFERENCE_TO (r) = NULL_TREE;
15390
15391 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15392 {
15393 /* Propagate constraints on placeholders since they are
15394 only instantiated during satisfaction. */
15395 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15396 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15397 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15398 {
15399 pl = tsubst_copy (pl, args, complain, in_decl);
15400 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15401 }
15402 }
15403
15404 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15405 /* We have reduced the level of the template
15406 template parameter, but not the levels of its
15407 template parameters, so canonical_type_parameter
15408 will not be able to find the canonical template
15409 template parameter for this level. Thus, we
15410 require structural equality checking to compare
15411 TEMPLATE_TEMPLATE_PARMs. */
15412 SET_TYPE_STRUCTURAL_EQUALITY (r);
15413 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15414 SET_TYPE_STRUCTURAL_EQUALITY (r);
15415 else
15416 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15417
15418 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15419 {
15420 tree tinfo = TYPE_TEMPLATE_INFO (t);
15421 /* We might need to substitute into the types of non-type
15422 template parameters. */
15423 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15424 complain, in_decl);
15425 if (tmpl == error_mark_node)
15426 return error_mark_node;
15427 tree argvec = tsubst (TI_ARGS (tinfo), args,
15428 complain, in_decl);
15429 if (argvec == error_mark_node)
15430 return error_mark_node;
15431
15432 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15433 = build_template_info (tmpl, argvec);
15434 }
15435 }
15436 break;
15437
15438 case TEMPLATE_PARM_INDEX:
15439 /* OK, now substitute the type of the non-type parameter. We
15440 couldn't do it earlier because it might be an auto parameter,
15441 and we wouldn't need to if we had an argument. */
15442 type = tsubst (type, args, complain, in_decl);
15443 if (type == error_mark_node)
15444 return error_mark_node;
15445 r = reduce_template_parm_level (t, type, levels, args, complain);
15446 break;
15447
15448 default:
15449 gcc_unreachable ();
15450 }
15451
15452 return r;
15453 }
15454
15455 case TREE_LIST:
15456 {
15457 tree purpose, value, chain;
15458
15459 if (t == void_list_node)
15460 return t;
15461
15462 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
15463 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
15464 {
15465 /* We have pack expansions, so expand those and
15466 create a new list out of it. */
15467
15468 /* Expand the argument expressions. */
15469 tree purposevec = NULL_TREE;
15470 if (TREE_PURPOSE (t))
15471 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
15472 complain, in_decl);
15473 if (purposevec == error_mark_node)
15474 return error_mark_node;
15475
15476 tree valuevec = NULL_TREE;
15477 if (TREE_VALUE (t))
15478 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
15479 complain, in_decl);
15480 if (valuevec == error_mark_node)
15481 return error_mark_node;
15482
15483 /* Build the rest of the list. */
15484 tree chain = TREE_CHAIN (t);
15485 if (chain && chain != void_type_node)
15486 chain = tsubst (chain, args, complain, in_decl);
15487 if (chain == error_mark_node)
15488 return error_mark_node;
15489
15490 /* Determine the number of arguments. */
15491 int len = -1;
15492 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
15493 {
15494 len = TREE_VEC_LENGTH (purposevec);
15495 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15496 }
15497 else if (TREE_CODE (valuevec) == TREE_VEC)
15498 len = TREE_VEC_LENGTH (valuevec);
15499 else
15500 {
15501 /* Since we only performed a partial substitution into
15502 the argument pack, we only RETURN (a single list
15503 node. */
15504 if (purposevec == TREE_PURPOSE (t)
15505 && valuevec == TREE_VALUE (t)
15506 && chain == TREE_CHAIN (t))
15507 return t;
15508
15509 return tree_cons (purposevec, valuevec, chain);
15510 }
15511
15512 /* Convert the argument vectors into a TREE_LIST. */
15513 for (int i = len; i-- > 0; )
15514 {
15515 purpose = (purposevec ? TREE_VEC_ELT (purposevec, i)
15516 : NULL_TREE);
15517 value = (valuevec ? TREE_VEC_ELT (valuevec, i)
15518 : NULL_TREE);
15519
15520 /* Build the list (backwards). */
15521 chain = hash_tree_cons (purpose, value, chain);
15522 }
15523
15524 return chain;
15525 }
15526
15527 purpose = TREE_PURPOSE (t);
15528 if (purpose)
15529 {
15530 purpose = tsubst (purpose, args, complain, in_decl);
15531 if (purpose == error_mark_node)
15532 return error_mark_node;
15533 }
15534 value = TREE_VALUE (t);
15535 if (value)
15536 {
15537 value = tsubst (value, args, complain, in_decl);
15538 if (value == error_mark_node)
15539 return error_mark_node;
15540 }
15541 chain = TREE_CHAIN (t);
15542 if (chain && chain != void_type_node)
15543 {
15544 chain = tsubst (chain, args, complain, in_decl);
15545 if (chain == error_mark_node)
15546 return error_mark_node;
15547 }
15548 if (purpose == TREE_PURPOSE (t)
15549 && value == TREE_VALUE (t)
15550 && chain == TREE_CHAIN (t))
15551 return t;
15552 return hash_tree_cons (purpose, value, chain);
15553 }
15554
15555 case TREE_BINFO:
15556 /* We should never be tsubsting a binfo. */
15557 gcc_unreachable ();
15558
15559 case TREE_VEC:
15560 /* A vector of template arguments. */
15561 gcc_assert (!type);
15562 return tsubst_template_args (t, args, complain, in_decl);
15563
15564 case POINTER_TYPE:
15565 case REFERENCE_TYPE:
15566 {
15567 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15568 return t;
15569
15570 /* [temp.deduct]
15571
15572 Type deduction may fail for any of the following
15573 reasons:
15574
15575 -- Attempting to create a pointer to reference type.
15576 -- Attempting to create a reference to a reference type or
15577 a reference to void.
15578
15579 Core issue 106 says that creating a reference to a reference
15580 during instantiation is no longer a cause for failure. We
15581 only enforce this check in strict C++98 mode. */
15582 if ((TYPE_REF_P (type)
15583 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15584 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15585 {
15586 static location_t last_loc;
15587
15588 /* We keep track of the last time we issued this error
15589 message to avoid spewing a ton of messages during a
15590 single bad template instantiation. */
15591 if (complain & tf_error
15592 && last_loc != input_location)
15593 {
15594 if (VOID_TYPE_P (type))
15595 error ("forming reference to void");
15596 else if (code == POINTER_TYPE)
15597 error ("forming pointer to reference type %qT", type);
15598 else
15599 error ("forming reference to reference type %qT", type);
15600 last_loc = input_location;
15601 }
15602
15603 return error_mark_node;
15604 }
15605 else if (TREE_CODE (type) == FUNCTION_TYPE
15606 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15607 || type_memfn_rqual (type) != REF_QUAL_NONE))
15608 {
15609 if (complain & tf_error)
15610 {
15611 if (code == POINTER_TYPE)
15612 error ("forming pointer to qualified function type %qT",
15613 type);
15614 else
15615 error ("forming reference to qualified function type %qT",
15616 type);
15617 }
15618 return error_mark_node;
15619 }
15620 else if (code == POINTER_TYPE)
15621 {
15622 r = build_pointer_type (type);
15623 if (TREE_CODE (type) == METHOD_TYPE)
15624 r = build_ptrmemfunc_type (r);
15625 }
15626 else if (TYPE_REF_P (type))
15627 /* In C++0x, during template argument substitution, when there is an
15628 attempt to create a reference to a reference type, reference
15629 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15630
15631 "If a template-argument for a template-parameter T names a type
15632 that is a reference to a type A, an attempt to create the type
15633 'lvalue reference to cv T' creates the type 'lvalue reference to
15634 A,' while an attempt to create the type type rvalue reference to
15635 cv T' creates the type T"
15636 */
15637 r = cp_build_reference_type
15638 (TREE_TYPE (type),
15639 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15640 else
15641 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15642 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15643
15644 if (r != error_mark_node)
15645 /* Will this ever be needed for TYPE_..._TO values? */
15646 layout_type (r);
15647
15648 return r;
15649 }
15650 case OFFSET_TYPE:
15651 {
15652 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15653 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15654 {
15655 /* [temp.deduct]
15656
15657 Type deduction may fail for any of the following
15658 reasons:
15659
15660 -- Attempting to create "pointer to member of T" when T
15661 is not a class type. */
15662 if (complain & tf_error)
15663 error ("creating pointer to member of non-class type %qT", r);
15664 return error_mark_node;
15665 }
15666 if (TYPE_REF_P (type))
15667 {
15668 if (complain & tf_error)
15669 error ("creating pointer to member reference type %qT", type);
15670 return error_mark_node;
15671 }
15672 if (VOID_TYPE_P (type))
15673 {
15674 if (complain & tf_error)
15675 error ("creating pointer to member of type void");
15676 return error_mark_node;
15677 }
15678 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15679 if (TREE_CODE (type) == FUNCTION_TYPE)
15680 {
15681 /* The type of the implicit object parameter gets its
15682 cv-qualifiers from the FUNCTION_TYPE. */
15683 tree memptr;
15684 tree method_type
15685 = build_memfn_type (type, r, type_memfn_quals (type),
15686 type_memfn_rqual (type));
15687 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15688 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15689 complain);
15690 }
15691 else
15692 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15693 cp_type_quals (t),
15694 complain);
15695 }
15696 case FUNCTION_TYPE:
15697 case METHOD_TYPE:
15698 {
15699 tree fntype;
15700 tree specs;
15701 fntype = tsubst_function_type (t, args, complain, in_decl);
15702 if (fntype == error_mark_node)
15703 return error_mark_node;
15704
15705 /* Substitute the exception specification. */
15706 specs = tsubst_exception_specification (t, args, complain, in_decl,
15707 /*defer_ok*/fndecl_type);
15708 if (specs == error_mark_node)
15709 return error_mark_node;
15710 if (specs)
15711 fntype = build_exception_variant (fntype, specs);
15712 return fntype;
15713 }
15714 case ARRAY_TYPE:
15715 {
15716 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15717 if (domain == error_mark_node)
15718 return error_mark_node;
15719
15720 /* As an optimization, we avoid regenerating the array type if
15721 it will obviously be the same as T. */
15722 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15723 return t;
15724
15725 /* These checks should match the ones in create_array_type_for_decl.
15726
15727 [temp.deduct]
15728
15729 The deduction may fail for any of the following reasons:
15730
15731 -- Attempting to create an array with an element type that
15732 is void, a function type, or a reference type, or [DR337]
15733 an abstract class type. */
15734 if (VOID_TYPE_P (type)
15735 || TREE_CODE (type) == FUNCTION_TYPE
15736 || (TREE_CODE (type) == ARRAY_TYPE
15737 && TYPE_DOMAIN (type) == NULL_TREE)
15738 || TYPE_REF_P (type))
15739 {
15740 if (complain & tf_error)
15741 error ("creating array of %qT", type);
15742 return error_mark_node;
15743 }
15744
15745 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15746 return error_mark_node;
15747
15748 r = build_cplus_array_type (type, domain);
15749
15750 if (!valid_array_size_p (input_location, r, in_decl,
15751 (complain & tf_error)))
15752 return error_mark_node;
15753
15754 if (TYPE_USER_ALIGN (t))
15755 {
15756 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15757 TYPE_USER_ALIGN (r) = 1;
15758 }
15759
15760 return r;
15761 }
15762
15763 case TYPENAME_TYPE:
15764 {
15765 tree ctx = TYPE_CONTEXT (t);
15766 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15767 {
15768 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15769 if (ctx == error_mark_node
15770 || TREE_VEC_LENGTH (ctx) > 1)
15771 return error_mark_node;
15772 if (TREE_VEC_LENGTH (ctx) == 0)
15773 {
15774 if (complain & tf_error)
15775 error ("%qD is instantiated for an empty pack",
15776 TYPENAME_TYPE_FULLNAME (t));
15777 return error_mark_node;
15778 }
15779 ctx = TREE_VEC_ELT (ctx, 0);
15780 }
15781 else
15782 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15783 /*entering_scope=*/1);
15784 if (ctx == error_mark_node)
15785 return error_mark_node;
15786
15787 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15788 complain, in_decl);
15789 if (f == error_mark_node)
15790 return error_mark_node;
15791
15792 if (!MAYBE_CLASS_TYPE_P (ctx))
15793 {
15794 if (complain & tf_error)
15795 error ("%qT is not a class, struct, or union type", ctx);
15796 return error_mark_node;
15797 }
15798 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15799 {
15800 /* Normally, make_typename_type does not require that the CTX
15801 have complete type in order to allow things like:
15802
15803 template <class T> struct S { typename S<T>::X Y; };
15804
15805 But, such constructs have already been resolved by this
15806 point, so here CTX really should have complete type, unless
15807 it's a partial instantiation. */
15808 ctx = complete_type (ctx);
15809 if (!COMPLETE_TYPE_P (ctx))
15810 {
15811 if (complain & tf_error)
15812 cxx_incomplete_type_error (NULL_TREE, ctx);
15813 return error_mark_node;
15814 }
15815 }
15816
15817 f = make_typename_type (ctx, f, typename_type,
15818 complain | tf_keep_type_decl);
15819 if (f == error_mark_node)
15820 return f;
15821 if (TREE_CODE (f) == TYPE_DECL)
15822 {
15823 complain |= tf_ignore_bad_quals;
15824 f = TREE_TYPE (f);
15825 }
15826
15827 if (TREE_CODE (f) != TYPENAME_TYPE)
15828 {
15829 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15830 {
15831 if (complain & tf_error)
15832 error ("%qT resolves to %qT, which is not an enumeration type",
15833 t, f);
15834 else
15835 return error_mark_node;
15836 }
15837 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15838 {
15839 if (complain & tf_error)
15840 error ("%qT resolves to %qT, which is not a class type",
15841 t, f);
15842 else
15843 return error_mark_node;
15844 }
15845 }
15846
15847 return cp_build_qualified_type_real
15848 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15849 }
15850
15851 case UNBOUND_CLASS_TEMPLATE:
15852 {
15853 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15854 in_decl, /*entering_scope=*/1);
15855 tree name = TYPE_IDENTIFIER (t);
15856 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15857
15858 if (ctx == error_mark_node || name == error_mark_node)
15859 return error_mark_node;
15860
15861 if (parm_list)
15862 parm_list = tsubst_template_parms (parm_list, args, complain);
15863 return make_unbound_class_template (ctx, name, parm_list, complain);
15864 }
15865
15866 case TYPEOF_TYPE:
15867 {
15868 tree type;
15869
15870 ++cp_unevaluated_operand;
15871 ++c_inhibit_evaluation_warnings;
15872
15873 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15874 complain, in_decl,
15875 /*integral_constant_expression_p=*/false);
15876
15877 --cp_unevaluated_operand;
15878 --c_inhibit_evaluation_warnings;
15879
15880 type = finish_typeof (type);
15881 return cp_build_qualified_type_real (type,
15882 cp_type_quals (t)
15883 | cp_type_quals (type),
15884 complain);
15885 }
15886
15887 case DECLTYPE_TYPE:
15888 {
15889 tree type;
15890
15891 ++cp_unevaluated_operand;
15892 ++c_inhibit_evaluation_warnings;
15893
15894 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15895 complain|tf_decltype, in_decl,
15896 /*function_p*/false,
15897 /*integral_constant_expression*/false);
15898
15899 --cp_unevaluated_operand;
15900 --c_inhibit_evaluation_warnings;
15901
15902 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15903 type = lambda_capture_field_type (type,
15904 false /*explicit_init*/,
15905 DECLTYPE_FOR_REF_CAPTURE (t));
15906 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15907 type = lambda_proxy_type (type);
15908 else
15909 {
15910 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15911 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15912 && EXPR_P (type))
15913 /* In a template ~id could be either a complement expression
15914 or an unqualified-id naming a destructor; if instantiating
15915 it produces an expression, it's not an id-expression or
15916 member access. */
15917 id = false;
15918 type = finish_decltype_type (type, id, complain);
15919 }
15920 return cp_build_qualified_type_real (type,
15921 cp_type_quals (t)
15922 | cp_type_quals (type),
15923 complain | tf_ignore_bad_quals);
15924 }
15925
15926 case UNDERLYING_TYPE:
15927 {
15928 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15929 complain, in_decl);
15930 return finish_underlying_type (type);
15931 }
15932
15933 case TYPE_ARGUMENT_PACK:
15934 case NONTYPE_ARGUMENT_PACK:
15935 {
15936 tree r;
15937
15938 if (code == NONTYPE_ARGUMENT_PACK)
15939 r = make_node (code);
15940 else
15941 r = cxx_make_type (code);
15942
15943 tree pack_args = ARGUMENT_PACK_ARGS (t);
15944 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15945 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15946
15947 return r;
15948 }
15949
15950 case VOID_CST:
15951 case INTEGER_CST:
15952 case REAL_CST:
15953 case STRING_CST:
15954 case PLUS_EXPR:
15955 case MINUS_EXPR:
15956 case NEGATE_EXPR:
15957 case NOP_EXPR:
15958 case INDIRECT_REF:
15959 case ADDR_EXPR:
15960 case CALL_EXPR:
15961 case ARRAY_REF:
15962 case SCOPE_REF:
15963 /* We should use one of the expression tsubsts for these codes. */
15964 gcc_unreachable ();
15965
15966 default:
15967 sorry ("use of %qs in template", get_tree_code_name (code));
15968 return error_mark_node;
15969 }
15970 }
15971
15972 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15973 expression on the left-hand side of the "." or "->" operator. We
15974 only do the lookup if we had a dependent BASELINK. Otherwise we
15975 adjust it onto the instantiated heirarchy. */
15976
15977 static tree
15978 tsubst_baselink (tree baselink, tree object_type,
15979 tree args, tsubst_flags_t complain, tree in_decl)
15980 {
15981 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15982 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15983 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15984
15985 tree optype = BASELINK_OPTYPE (baselink);
15986 optype = tsubst (optype, args, complain, in_decl);
15987
15988 tree template_args = NULL_TREE;
15989 bool template_id_p = false;
15990 tree fns = BASELINK_FUNCTIONS (baselink);
15991 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15992 {
15993 template_id_p = true;
15994 template_args = TREE_OPERAND (fns, 1);
15995 fns = TREE_OPERAND (fns, 0);
15996 if (template_args)
15997 template_args = tsubst_template_args (template_args, args,
15998 complain, in_decl);
15999 }
16000
16001 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16002 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16003 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16004
16005 if (dependent_p)
16006 {
16007 tree name = OVL_NAME (fns);
16008 if (IDENTIFIER_CONV_OP_P (name))
16009 name = make_conv_op_name (optype);
16010
16011 if (name == complete_dtor_identifier)
16012 /* Treat as-if non-dependent below. */
16013 dependent_p = false;
16014
16015 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
16016 if (!baselink)
16017 {
16018 if ((complain & tf_error)
16019 && constructor_name_p (name, qualifying_scope))
16020 error ("cannot call constructor %<%T::%D%> directly",
16021 qualifying_scope, name);
16022 return error_mark_node;
16023 }
16024
16025 if (BASELINK_P (baselink))
16026 fns = BASELINK_FUNCTIONS (baselink);
16027 }
16028 else
16029 /* We're going to overwrite pieces below, make a duplicate. */
16030 baselink = copy_node (baselink);
16031
16032 /* If lookup found a single function, mark it as used at this point.
16033 (If lookup found multiple functions the one selected later by
16034 overload resolution will be marked as used at that point.) */
16035 if (!template_id_p && !really_overloaded_fn (fns))
16036 {
16037 tree fn = OVL_FIRST (fns);
16038 bool ok = mark_used (fn, complain);
16039 if (!ok && !(complain & tf_error))
16040 return error_mark_node;
16041 if (ok && BASELINK_P (baselink))
16042 /* We might have instantiated an auto function. */
16043 TREE_TYPE (baselink) = TREE_TYPE (fn);
16044 }
16045
16046 if (BASELINK_P (baselink))
16047 {
16048 /* Add back the template arguments, if present. */
16049 if (template_id_p)
16050 BASELINK_FUNCTIONS (baselink)
16051 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16052
16053 /* Update the conversion operator type. */
16054 BASELINK_OPTYPE (baselink) = optype;
16055 }
16056
16057 if (!object_type)
16058 object_type = current_class_type;
16059
16060 if (qualified_p || !dependent_p)
16061 {
16062 baselink = adjust_result_of_qualified_name_lookup (baselink,
16063 qualifying_scope,
16064 object_type);
16065 if (!qualified_p)
16066 /* We need to call adjust_result_of_qualified_name_lookup in case the
16067 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16068 so that we still get virtual function binding. */
16069 BASELINK_QUALIFIED_P (baselink) = false;
16070 }
16071
16072 return baselink;
16073 }
16074
16075 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16076 true if the qualified-id will be a postfix-expression in-and-of
16077 itself; false if more of the postfix-expression follows the
16078 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16079 of "&". */
16080
16081 static tree
16082 tsubst_qualified_id (tree qualified_id, tree args,
16083 tsubst_flags_t complain, tree in_decl,
16084 bool done, bool address_p)
16085 {
16086 tree expr;
16087 tree scope;
16088 tree name;
16089 bool is_template;
16090 tree template_args;
16091 location_t loc = UNKNOWN_LOCATION;
16092
16093 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16094
16095 /* Figure out what name to look up. */
16096 name = TREE_OPERAND (qualified_id, 1);
16097 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16098 {
16099 is_template = true;
16100 loc = EXPR_LOCATION (name);
16101 template_args = TREE_OPERAND (name, 1);
16102 if (template_args)
16103 template_args = tsubst_template_args (template_args, args,
16104 complain, in_decl);
16105 if (template_args == error_mark_node)
16106 return error_mark_node;
16107 name = TREE_OPERAND (name, 0);
16108 }
16109 else
16110 {
16111 is_template = false;
16112 template_args = NULL_TREE;
16113 }
16114
16115 /* Substitute into the qualifying scope. When there are no ARGS, we
16116 are just trying to simplify a non-dependent expression. In that
16117 case the qualifying scope may be dependent, and, in any case,
16118 substituting will not help. */
16119 scope = TREE_OPERAND (qualified_id, 0);
16120 if (args)
16121 {
16122 scope = tsubst (scope, args, complain, in_decl);
16123 expr = tsubst_copy (name, args, complain, in_decl);
16124 }
16125 else
16126 expr = name;
16127
16128 if (dependent_scope_p (scope))
16129 {
16130 if (is_template)
16131 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16132 tree r = build_qualified_name (NULL_TREE, scope, expr,
16133 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16134 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16135 return r;
16136 }
16137
16138 if (!BASELINK_P (name) && !DECL_P (expr))
16139 {
16140 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16141 {
16142 /* A BIT_NOT_EXPR is used to represent a destructor. */
16143 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16144 {
16145 error ("qualifying type %qT does not match destructor name ~%qT",
16146 scope, TREE_OPERAND (expr, 0));
16147 expr = error_mark_node;
16148 }
16149 else
16150 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16151 /*is_type_p=*/0, false);
16152 }
16153 else
16154 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
16155 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16156 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16157 {
16158 if (complain & tf_error)
16159 {
16160 error ("dependent-name %qE is parsed as a non-type, but "
16161 "instantiation yields a type", qualified_id);
16162 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16163 }
16164 return error_mark_node;
16165 }
16166 }
16167
16168 if (DECL_P (expr))
16169 {
16170 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16171 scope, complain))
16172 return error_mark_node;
16173 /* Remember that there was a reference to this entity. */
16174 if (!mark_used (expr, complain) && !(complain & tf_error))
16175 return error_mark_node;
16176 }
16177
16178 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16179 {
16180 if (complain & tf_error)
16181 qualified_name_lookup_error (scope,
16182 TREE_OPERAND (qualified_id, 1),
16183 expr, input_location);
16184 return error_mark_node;
16185 }
16186
16187 if (is_template)
16188 {
16189 /* We may be repeating a check already done during parsing, but
16190 if it was well-formed and passed then, it will pass again
16191 now, and if it didn't, we wouldn't have got here. The case
16192 we want to catch is when we couldn't tell then, and can now,
16193 namely when templ prior to substitution was an
16194 identifier. */
16195 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16196 return error_mark_node;
16197
16198 if (variable_template_p (expr))
16199 expr = lookup_and_finish_template_variable (expr, template_args,
16200 complain);
16201 else
16202 expr = lookup_template_function (expr, template_args);
16203 }
16204
16205 if (expr == error_mark_node && complain & tf_error)
16206 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16207 expr, input_location);
16208 else if (TYPE_P (scope))
16209 {
16210 expr = (adjust_result_of_qualified_name_lookup
16211 (expr, scope, current_nonlambda_class_type ()));
16212 expr = (finish_qualified_id_expr
16213 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16214 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16215 /*template_arg_p=*/false, complain));
16216 }
16217
16218 /* Expressions do not generally have reference type. */
16219 if (TREE_CODE (expr) != SCOPE_REF
16220 /* However, if we're about to form a pointer-to-member, we just
16221 want the referenced member referenced. */
16222 && TREE_CODE (expr) != OFFSET_REF)
16223 expr = convert_from_reference (expr);
16224
16225 if (REF_PARENTHESIZED_P (qualified_id))
16226 expr = force_paren_expr (expr);
16227
16228 return expr;
16229 }
16230
16231 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16232 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16233 for tsubst. */
16234
16235 static tree
16236 tsubst_init (tree init, tree decl, tree args,
16237 tsubst_flags_t complain, tree in_decl)
16238 {
16239 if (!init)
16240 return NULL_TREE;
16241
16242 init = tsubst_expr (init, args, complain, in_decl, false);
16243
16244 tree type = TREE_TYPE (decl);
16245
16246 if (!init && type != error_mark_node)
16247 {
16248 if (tree auto_node = type_uses_auto (type))
16249 {
16250 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16251 {
16252 if (complain & tf_error)
16253 error ("initializer for %q#D expands to an empty list "
16254 "of expressions", decl);
16255 return error_mark_node;
16256 }
16257 }
16258 else if (!dependent_type_p (type))
16259 {
16260 /* If we had an initializer but it
16261 instantiated to nothing,
16262 value-initialize the object. This will
16263 only occur when the initializer was a
16264 pack expansion where the parameter packs
16265 used in that expansion were of length
16266 zero. */
16267 init = build_value_init (type, complain);
16268 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16269 init = get_target_expr_sfinae (init, complain);
16270 if (TREE_CODE (init) == TARGET_EXPR)
16271 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16272 }
16273 }
16274
16275 return init;
16276 }
16277
16278 /* Like tsubst, but deals with expressions. This function just replaces
16279 template parms; to finish processing the resultant expression, use
16280 tsubst_copy_and_build or tsubst_expr. */
16281
16282 static tree
16283 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16284 {
16285 enum tree_code code;
16286 tree r;
16287
16288 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16289 return t;
16290
16291 code = TREE_CODE (t);
16292
16293 switch (code)
16294 {
16295 case PARM_DECL:
16296 r = retrieve_local_specialization (t);
16297
16298 if (r == NULL_TREE)
16299 {
16300 /* We get here for a use of 'this' in an NSDMI. */
16301 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16302 return current_class_ptr;
16303
16304 /* This can happen for a parameter name used later in a function
16305 declaration (such as in a late-specified return type). Just
16306 make a dummy decl, since it's only used for its type. */
16307 gcc_assert (cp_unevaluated_operand != 0);
16308 r = tsubst_decl (t, args, complain);
16309 /* Give it the template pattern as its context; its true context
16310 hasn't been instantiated yet and this is good enough for
16311 mangling. */
16312 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16313 }
16314
16315 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16316 r = argument_pack_select_arg (r);
16317 if (!mark_used (r, complain) && !(complain & tf_error))
16318 return error_mark_node;
16319 return r;
16320
16321 case CONST_DECL:
16322 {
16323 tree enum_type;
16324 tree v;
16325
16326 if (DECL_TEMPLATE_PARM_P (t))
16327 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16328 /* There is no need to substitute into namespace-scope
16329 enumerators. */
16330 if (DECL_NAMESPACE_SCOPE_P (t))
16331 return t;
16332 /* If ARGS is NULL, then T is known to be non-dependent. */
16333 if (args == NULL_TREE)
16334 return scalar_constant_value (t);
16335
16336 /* Unfortunately, we cannot just call lookup_name here.
16337 Consider:
16338
16339 template <int I> int f() {
16340 enum E { a = I };
16341 struct S { void g() { E e = a; } };
16342 };
16343
16344 When we instantiate f<7>::S::g(), say, lookup_name is not
16345 clever enough to find f<7>::a. */
16346 enum_type
16347 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16348 /*entering_scope=*/0);
16349
16350 for (v = TYPE_VALUES (enum_type);
16351 v != NULL_TREE;
16352 v = TREE_CHAIN (v))
16353 if (TREE_PURPOSE (v) == DECL_NAME (t))
16354 return TREE_VALUE (v);
16355
16356 /* We didn't find the name. That should never happen; if
16357 name-lookup found it during preliminary parsing, we
16358 should find it again here during instantiation. */
16359 gcc_unreachable ();
16360 }
16361 return t;
16362
16363 case FIELD_DECL:
16364 if (DECL_CONTEXT (t))
16365 {
16366 tree ctx;
16367
16368 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16369 /*entering_scope=*/1);
16370 if (ctx != DECL_CONTEXT (t))
16371 {
16372 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16373 if (!r)
16374 {
16375 if (complain & tf_error)
16376 error ("using invalid field %qD", t);
16377 return error_mark_node;
16378 }
16379 return r;
16380 }
16381 }
16382
16383 return t;
16384
16385 case VAR_DECL:
16386 case FUNCTION_DECL:
16387 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16388 r = tsubst (t, args, complain, in_decl);
16389 else if (local_variable_p (t)
16390 && uses_template_parms (DECL_CONTEXT (t)))
16391 {
16392 r = retrieve_local_specialization (t);
16393 if (r == NULL_TREE)
16394 {
16395 /* First try name lookup to find the instantiation. */
16396 r = lookup_name (DECL_NAME (t));
16397 if (r)
16398 {
16399 if (!VAR_P (r))
16400 {
16401 /* During error-recovery we may find a non-variable,
16402 even an OVERLOAD: just bail out and avoid ICEs and
16403 duplicate diagnostics (c++/62207). */
16404 gcc_assert (seen_error ());
16405 return error_mark_node;
16406 }
16407 if (!is_capture_proxy (r))
16408 {
16409 /* Make sure the one we found is the one we want. */
16410 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16411 if (ctx != DECL_CONTEXT (r))
16412 r = NULL_TREE;
16413 }
16414 }
16415
16416 if (r)
16417 /* OK */;
16418 else
16419 {
16420 /* This can happen for a variable used in a
16421 late-specified return type of a local lambda, or for a
16422 local static or constant. Building a new VAR_DECL
16423 should be OK in all those cases. */
16424 r = tsubst_decl (t, args, complain);
16425 if (local_specializations)
16426 /* Avoid infinite recursion (79640). */
16427 register_local_specialization (r, t);
16428 if (decl_maybe_constant_var_p (r))
16429 {
16430 /* We can't call cp_finish_decl, so handle the
16431 initializer by hand. */
16432 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16433 complain, in_decl);
16434 if (!processing_template_decl)
16435 init = maybe_constant_init (init);
16436 if (processing_template_decl
16437 ? potential_constant_expression (init)
16438 : reduced_constant_expression_p (init))
16439 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16440 = TREE_CONSTANT (r) = true;
16441 DECL_INITIAL (r) = init;
16442 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16443 TREE_TYPE (r)
16444 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16445 complain, adc_variable_type);
16446 }
16447 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16448 || decl_constant_var_p (r)
16449 || seen_error ());
16450 if (!processing_template_decl
16451 && !TREE_STATIC (r))
16452 r = process_outer_var_ref (r, complain);
16453 }
16454 /* Remember this for subsequent uses. */
16455 if (local_specializations)
16456 register_local_specialization (r, t);
16457 }
16458 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16459 r = argument_pack_select_arg (r);
16460 }
16461 else
16462 r = t;
16463 if (!mark_used (r, complain))
16464 return error_mark_node;
16465 return r;
16466
16467 case NAMESPACE_DECL:
16468 return t;
16469
16470 case OVERLOAD:
16471 return t;
16472
16473 case BASELINK:
16474 return tsubst_baselink (t, current_nonlambda_class_type (),
16475 args, complain, in_decl);
16476
16477 case TEMPLATE_DECL:
16478 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16479 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16480 args, complain, in_decl);
16481 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16482 return tsubst (t, args, complain, in_decl);
16483 else if (DECL_CLASS_SCOPE_P (t)
16484 && uses_template_parms (DECL_CONTEXT (t)))
16485 {
16486 /* Template template argument like the following example need
16487 special treatment:
16488
16489 template <template <class> class TT> struct C {};
16490 template <class T> struct D {
16491 template <class U> struct E {};
16492 C<E> c; // #1
16493 };
16494 D<int> d; // #2
16495
16496 We are processing the template argument `E' in #1 for
16497 the template instantiation #2. Originally, `E' is a
16498 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16499 have to substitute this with one having context `D<int>'. */
16500
16501 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16502 if (dependent_scope_p (context))
16503 {
16504 /* When rewriting a constructor into a deduction guide, a
16505 non-dependent name can become dependent, so memtmpl<args>
16506 becomes context::template memtmpl<args>. */
16507 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16508 return build_qualified_name (type, context, DECL_NAME (t),
16509 /*template*/true);
16510 }
16511 return lookup_field (context, DECL_NAME(t), 0, false);
16512 }
16513 else
16514 /* Ordinary template template argument. */
16515 return t;
16516
16517 case NON_LVALUE_EXPR:
16518 case VIEW_CONVERT_EXPR:
16519 {
16520 /* Handle location wrappers by substituting the wrapped node
16521 first, *then* reusing the resulting type. Doing the type
16522 first ensures that we handle template parameters and
16523 parameter pack expansions. */
16524 if (location_wrapper_p (t))
16525 {
16526 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16527 complain, in_decl);
16528 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16529 }
16530 tree op = TREE_OPERAND (t, 0);
16531 if (code == VIEW_CONVERT_EXPR
16532 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16533 {
16534 /* Wrapper to make a C++20 template parameter object const. */
16535 op = tsubst_copy (op, args, complain, in_decl);
16536 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16537 {
16538 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16539 return build1 (code, type, op);
16540 }
16541 else
16542 {
16543 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op))
16544 || (TREE_CODE (op) == IMPLICIT_CONV_EXPR
16545 && IMPLICIT_CONV_EXPR_NONTYPE_ARG (op)));
16546 return op;
16547 }
16548 }
16549 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16550 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16551 {
16552 op = tsubst_copy (op, args, complain, in_decl);
16553 op = build1 (code, TREE_TYPE (op), op);
16554 REF_PARENTHESIZED_P (op) = true;
16555 return op;
16556 }
16557 /* We shouldn't see any other uses of these in templates. */
16558 gcc_unreachable ();
16559 }
16560
16561 case CAST_EXPR:
16562 case REINTERPRET_CAST_EXPR:
16563 case CONST_CAST_EXPR:
16564 case STATIC_CAST_EXPR:
16565 case DYNAMIC_CAST_EXPR:
16566 case IMPLICIT_CONV_EXPR:
16567 case CONVERT_EXPR:
16568 case NOP_EXPR:
16569 {
16570 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16571 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16572 return build1 (code, type, op0);
16573 }
16574
16575 case SIZEOF_EXPR:
16576 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16577 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16578 {
16579 tree expanded, op = TREE_OPERAND (t, 0);
16580 int len = 0;
16581
16582 if (SIZEOF_EXPR_TYPE_P (t))
16583 op = TREE_TYPE (op);
16584
16585 ++cp_unevaluated_operand;
16586 ++c_inhibit_evaluation_warnings;
16587 /* We only want to compute the number of arguments. */
16588 if (PACK_EXPANSION_P (op))
16589 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16590 else
16591 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16592 args, complain, in_decl);
16593 --cp_unevaluated_operand;
16594 --c_inhibit_evaluation_warnings;
16595
16596 if (TREE_CODE (expanded) == TREE_VEC)
16597 {
16598 len = TREE_VEC_LENGTH (expanded);
16599 /* Set TREE_USED for the benefit of -Wunused. */
16600 for (int i = 0; i < len; i++)
16601 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16602 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16603 }
16604
16605 if (expanded == error_mark_node)
16606 return error_mark_node;
16607 else if (PACK_EXPANSION_P (expanded)
16608 || (TREE_CODE (expanded) == TREE_VEC
16609 && pack_expansion_args_count (expanded)))
16610
16611 {
16612 if (PACK_EXPANSION_P (expanded))
16613 /* OK. */;
16614 else if (TREE_VEC_LENGTH (expanded) == 1)
16615 expanded = TREE_VEC_ELT (expanded, 0);
16616 else
16617 expanded = make_argument_pack (expanded);
16618
16619 if (TYPE_P (expanded))
16620 return cxx_sizeof_or_alignof_type (input_location,
16621 expanded, SIZEOF_EXPR,
16622 false,
16623 complain & tf_error);
16624 else
16625 return cxx_sizeof_or_alignof_expr (input_location,
16626 expanded, SIZEOF_EXPR,
16627 complain & tf_error);
16628 }
16629 else
16630 return build_int_cst (size_type_node, len);
16631 }
16632 if (SIZEOF_EXPR_TYPE_P (t))
16633 {
16634 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16635 args, complain, in_decl);
16636 r = build1 (NOP_EXPR, r, error_mark_node);
16637 r = build1 (SIZEOF_EXPR,
16638 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16639 SIZEOF_EXPR_TYPE_P (r) = 1;
16640 return r;
16641 }
16642 /* Fall through */
16643
16644 case INDIRECT_REF:
16645 case NEGATE_EXPR:
16646 case TRUTH_NOT_EXPR:
16647 case BIT_NOT_EXPR:
16648 case ADDR_EXPR:
16649 case UNARY_PLUS_EXPR: /* Unary + */
16650 case ALIGNOF_EXPR:
16651 case AT_ENCODE_EXPR:
16652 case ARROW_EXPR:
16653 case THROW_EXPR:
16654 case TYPEID_EXPR:
16655 case REALPART_EXPR:
16656 case IMAGPART_EXPR:
16657 case PAREN_EXPR:
16658 {
16659 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16660 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16661 r = build1 (code, type, op0);
16662 if (code == ALIGNOF_EXPR)
16663 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16664 return r;
16665 }
16666
16667 case COMPONENT_REF:
16668 {
16669 tree object;
16670 tree name;
16671
16672 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16673 name = TREE_OPERAND (t, 1);
16674 if (TREE_CODE (name) == BIT_NOT_EXPR)
16675 {
16676 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16677 complain, in_decl);
16678 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16679 }
16680 else if (TREE_CODE (name) == SCOPE_REF
16681 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16682 {
16683 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16684 complain, in_decl);
16685 name = TREE_OPERAND (name, 1);
16686 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16687 complain, in_decl);
16688 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16689 name = build_qualified_name (/*type=*/NULL_TREE,
16690 base, name,
16691 /*template_p=*/false);
16692 }
16693 else if (BASELINK_P (name))
16694 name = tsubst_baselink (name,
16695 non_reference (TREE_TYPE (object)),
16696 args, complain,
16697 in_decl);
16698 else
16699 name = tsubst_copy (name, args, complain, in_decl);
16700 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16701 }
16702
16703 case PLUS_EXPR:
16704 case MINUS_EXPR:
16705 case MULT_EXPR:
16706 case TRUNC_DIV_EXPR:
16707 case CEIL_DIV_EXPR:
16708 case FLOOR_DIV_EXPR:
16709 case ROUND_DIV_EXPR:
16710 case EXACT_DIV_EXPR:
16711 case BIT_AND_EXPR:
16712 case BIT_IOR_EXPR:
16713 case BIT_XOR_EXPR:
16714 case TRUNC_MOD_EXPR:
16715 case FLOOR_MOD_EXPR:
16716 case TRUTH_ANDIF_EXPR:
16717 case TRUTH_ORIF_EXPR:
16718 case TRUTH_AND_EXPR:
16719 case TRUTH_OR_EXPR:
16720 case RSHIFT_EXPR:
16721 case LSHIFT_EXPR:
16722 case EQ_EXPR:
16723 case NE_EXPR:
16724 case MAX_EXPR:
16725 case MIN_EXPR:
16726 case LE_EXPR:
16727 case GE_EXPR:
16728 case LT_EXPR:
16729 case GT_EXPR:
16730 case COMPOUND_EXPR:
16731 case DOTSTAR_EXPR:
16732 case MEMBER_REF:
16733 case PREDECREMENT_EXPR:
16734 case PREINCREMENT_EXPR:
16735 case POSTDECREMENT_EXPR:
16736 case POSTINCREMENT_EXPR:
16737 {
16738 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16739 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16740 return build_nt (code, op0, op1);
16741 }
16742
16743 case SCOPE_REF:
16744 {
16745 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16746 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16747 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16748 QUALIFIED_NAME_IS_TEMPLATE (t));
16749 }
16750
16751 case ARRAY_REF:
16752 {
16753 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16754 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16755 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16756 }
16757
16758 case CALL_EXPR:
16759 {
16760 int n = VL_EXP_OPERAND_LENGTH (t);
16761 tree result = build_vl_exp (CALL_EXPR, n);
16762 int i;
16763 for (i = 0; i < n; i++)
16764 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16765 complain, in_decl);
16766 return result;
16767 }
16768
16769 case COND_EXPR:
16770 case MODOP_EXPR:
16771 case PSEUDO_DTOR_EXPR:
16772 case VEC_PERM_EXPR:
16773 {
16774 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16775 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16776 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16777 r = build_nt (code, op0, op1, op2);
16778 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16779 return r;
16780 }
16781
16782 case NEW_EXPR:
16783 {
16784 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16785 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16786 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16787 r = build_nt (code, op0, op1, op2);
16788 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16789 return r;
16790 }
16791
16792 case DELETE_EXPR:
16793 {
16794 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16795 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16796 r = build_nt (code, op0, op1);
16797 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16798 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16799 return r;
16800 }
16801
16802 case TEMPLATE_ID_EXPR:
16803 {
16804 /* Substituted template arguments */
16805 tree fn = TREE_OPERAND (t, 0);
16806 tree targs = TREE_OPERAND (t, 1);
16807
16808 fn = tsubst_copy (fn, args, complain, in_decl);
16809 if (targs)
16810 targs = tsubst_template_args (targs, args, complain, in_decl);
16811
16812 return lookup_template_function (fn, targs);
16813 }
16814
16815 case TREE_LIST:
16816 {
16817 tree purpose, value, chain;
16818
16819 if (t == void_list_node)
16820 return t;
16821
16822 purpose = TREE_PURPOSE (t);
16823 if (purpose)
16824 purpose = tsubst_copy (purpose, args, complain, in_decl);
16825 value = TREE_VALUE (t);
16826 if (value)
16827 value = tsubst_copy (value, args, complain, in_decl);
16828 chain = TREE_CHAIN (t);
16829 if (chain && chain != void_type_node)
16830 chain = tsubst_copy (chain, args, complain, in_decl);
16831 if (purpose == TREE_PURPOSE (t)
16832 && value == TREE_VALUE (t)
16833 && chain == TREE_CHAIN (t))
16834 return t;
16835 return tree_cons (purpose, value, chain);
16836 }
16837
16838 case RECORD_TYPE:
16839 case UNION_TYPE:
16840 case ENUMERAL_TYPE:
16841 case INTEGER_TYPE:
16842 case TEMPLATE_TYPE_PARM:
16843 case TEMPLATE_TEMPLATE_PARM:
16844 case BOUND_TEMPLATE_TEMPLATE_PARM:
16845 case TEMPLATE_PARM_INDEX:
16846 case POINTER_TYPE:
16847 case REFERENCE_TYPE:
16848 case OFFSET_TYPE:
16849 case FUNCTION_TYPE:
16850 case METHOD_TYPE:
16851 case ARRAY_TYPE:
16852 case TYPENAME_TYPE:
16853 case UNBOUND_CLASS_TEMPLATE:
16854 case TYPEOF_TYPE:
16855 case DECLTYPE_TYPE:
16856 case TYPE_DECL:
16857 return tsubst (t, args, complain, in_decl);
16858
16859 case USING_DECL:
16860 t = DECL_NAME (t);
16861 /* Fall through. */
16862 case IDENTIFIER_NODE:
16863 if (IDENTIFIER_CONV_OP_P (t))
16864 {
16865 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16866 return make_conv_op_name (new_type);
16867 }
16868 else
16869 return t;
16870
16871 case CONSTRUCTOR:
16872 /* This is handled by tsubst_copy_and_build. */
16873 gcc_unreachable ();
16874
16875 case VA_ARG_EXPR:
16876 {
16877 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16878 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16879 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16880 }
16881
16882 case CLEANUP_POINT_EXPR:
16883 /* We shouldn't have built any of these during initial template
16884 generation. Instead, they should be built during instantiation
16885 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16886 gcc_unreachable ();
16887
16888 case OFFSET_REF:
16889 {
16890 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16891 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16892 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16893 r = build2 (code, type, op0, op1);
16894 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16895 if (!mark_used (TREE_OPERAND (r, 1), complain)
16896 && !(complain & tf_error))
16897 return error_mark_node;
16898 return r;
16899 }
16900
16901 case EXPR_PACK_EXPANSION:
16902 error ("invalid use of pack expansion expression");
16903 return error_mark_node;
16904
16905 case NONTYPE_ARGUMENT_PACK:
16906 error ("use %<...%> to expand argument pack");
16907 return error_mark_node;
16908
16909 case VOID_CST:
16910 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16911 return t;
16912
16913 case INTEGER_CST:
16914 case REAL_CST:
16915 case COMPLEX_CST:
16916 {
16917 /* Instantiate any typedefs in the type. */
16918 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16919 r = fold_convert (type, t);
16920 gcc_assert (TREE_CODE (r) == code);
16921 return r;
16922 }
16923
16924 case STRING_CST:
16925 {
16926 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16927 r = t;
16928 if (type != TREE_TYPE (t))
16929 {
16930 r = copy_node (t);
16931 TREE_TYPE (r) = type;
16932 }
16933 return r;
16934 }
16935
16936 case PTRMEM_CST:
16937 /* These can sometimes show up in a partial instantiation, but never
16938 involve template parms. */
16939 gcc_assert (!uses_template_parms (t));
16940 return t;
16941
16942 case UNARY_LEFT_FOLD_EXPR:
16943 return tsubst_unary_left_fold (t, args, complain, in_decl);
16944 case UNARY_RIGHT_FOLD_EXPR:
16945 return tsubst_unary_right_fold (t, args, complain, in_decl);
16946 case BINARY_LEFT_FOLD_EXPR:
16947 return tsubst_binary_left_fold (t, args, complain, in_decl);
16948 case BINARY_RIGHT_FOLD_EXPR:
16949 return tsubst_binary_right_fold (t, args, complain, in_decl);
16950 case PREDICT_EXPR:
16951 return t;
16952
16953 case DEBUG_BEGIN_STMT:
16954 /* ??? There's no point in copying it for now, but maybe some
16955 day it will contain more information, such as a pointer back
16956 to the containing function, inlined copy or so. */
16957 return t;
16958
16959 case CO_AWAIT_EXPR:
16960 return tsubst_expr (t, args, complain, in_decl,
16961 /*integral_constant_expression_p=*/false);
16962 break;
16963
16964 default:
16965 /* We shouldn't get here, but keep going if !flag_checking. */
16966 if (flag_checking)
16967 gcc_unreachable ();
16968 return t;
16969 }
16970 }
16971
16972 /* Helper function for tsubst_omp_clauses, used for instantiation of
16973 OMP_CLAUSE_DECL of clauses. */
16974
16975 static tree
16976 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16977 tree in_decl, tree *iterator_cache)
16978 {
16979 if (decl == NULL_TREE)
16980 return NULL_TREE;
16981
16982 /* Handle OpenMP iterators. */
16983 if (TREE_CODE (decl) == TREE_LIST
16984 && TREE_PURPOSE (decl)
16985 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16986 {
16987 tree ret;
16988 if (iterator_cache[0] == TREE_PURPOSE (decl))
16989 ret = iterator_cache[1];
16990 else
16991 {
16992 tree *tp = &ret;
16993 begin_scope (sk_omp, NULL);
16994 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16995 {
16996 *tp = copy_node (it);
16997 TREE_VEC_ELT (*tp, 0)
16998 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16999 TREE_VEC_ELT (*tp, 1)
17000 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17001 /*integral_constant_expression_p=*/false);
17002 TREE_VEC_ELT (*tp, 2)
17003 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17004 /*integral_constant_expression_p=*/false);
17005 TREE_VEC_ELT (*tp, 3)
17006 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17007 /*integral_constant_expression_p=*/false);
17008 TREE_CHAIN (*tp) = NULL_TREE;
17009 tp = &TREE_CHAIN (*tp);
17010 }
17011 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17012 iterator_cache[0] = TREE_PURPOSE (decl);
17013 iterator_cache[1] = ret;
17014 }
17015 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17016 args, complain,
17017 in_decl, NULL));
17018 }
17019
17020 /* Handle an OpenMP array section represented as a TREE_LIST (or
17021 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17022 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17023 TREE_LIST. We can handle it exactly the same as an array section
17024 (purpose, value, and a chain), even though the nomenclature
17025 (low_bound, length, etc) is different. */
17026 if (TREE_CODE (decl) == TREE_LIST)
17027 {
17028 tree low_bound
17029 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17030 /*integral_constant_expression_p=*/false);
17031 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17032 /*integral_constant_expression_p=*/false);
17033 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17034 in_decl, NULL);
17035 if (TREE_PURPOSE (decl) == low_bound
17036 && TREE_VALUE (decl) == length
17037 && TREE_CHAIN (decl) == chain)
17038 return decl;
17039 tree ret = tree_cons (low_bound, length, chain);
17040 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17041 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17042 return ret;
17043 }
17044 tree ret = tsubst_expr (decl, args, complain, in_decl,
17045 /*integral_constant_expression_p=*/false);
17046 /* Undo convert_from_reference tsubst_expr could have called. */
17047 if (decl
17048 && REFERENCE_REF_P (ret)
17049 && !REFERENCE_REF_P (decl))
17050 ret = TREE_OPERAND (ret, 0);
17051 return ret;
17052 }
17053
17054 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17055
17056 static tree
17057 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17058 tree args, tsubst_flags_t complain, tree in_decl)
17059 {
17060 tree new_clauses = NULL_TREE, nc, oc;
17061 tree linear_no_step = NULL_TREE;
17062 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17063
17064 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17065 {
17066 nc = copy_node (oc);
17067 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17068 new_clauses = nc;
17069
17070 switch (OMP_CLAUSE_CODE (nc))
17071 {
17072 case OMP_CLAUSE_LASTPRIVATE:
17073 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17074 {
17075 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17076 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17077 in_decl, /*integral_constant_expression_p=*/false);
17078 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17079 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17080 }
17081 /* FALLTHRU */
17082 case OMP_CLAUSE_PRIVATE:
17083 case OMP_CLAUSE_SHARED:
17084 case OMP_CLAUSE_FIRSTPRIVATE:
17085 case OMP_CLAUSE_COPYIN:
17086 case OMP_CLAUSE_COPYPRIVATE:
17087 case OMP_CLAUSE_UNIFORM:
17088 case OMP_CLAUSE_DEPEND:
17089 case OMP_CLAUSE_FROM:
17090 case OMP_CLAUSE_TO:
17091 case OMP_CLAUSE_MAP:
17092 case OMP_CLAUSE_NONTEMPORAL:
17093 case OMP_CLAUSE_USE_DEVICE_PTR:
17094 case OMP_CLAUSE_USE_DEVICE_ADDR:
17095 case OMP_CLAUSE_IS_DEVICE_PTR:
17096 case OMP_CLAUSE_INCLUSIVE:
17097 case OMP_CLAUSE_EXCLUSIVE:
17098 OMP_CLAUSE_DECL (nc)
17099 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17100 in_decl, iterator_cache);
17101 break;
17102 case OMP_CLAUSE_TILE:
17103 case OMP_CLAUSE_IF:
17104 case OMP_CLAUSE_NUM_THREADS:
17105 case OMP_CLAUSE_SCHEDULE:
17106 case OMP_CLAUSE_COLLAPSE:
17107 case OMP_CLAUSE_FINAL:
17108 case OMP_CLAUSE_DEVICE:
17109 case OMP_CLAUSE_DIST_SCHEDULE:
17110 case OMP_CLAUSE_NUM_TEAMS:
17111 case OMP_CLAUSE_THREAD_LIMIT:
17112 case OMP_CLAUSE_SAFELEN:
17113 case OMP_CLAUSE_SIMDLEN:
17114 case OMP_CLAUSE_NUM_TASKS:
17115 case OMP_CLAUSE_GRAINSIZE:
17116 case OMP_CLAUSE_PRIORITY:
17117 case OMP_CLAUSE_ORDERED:
17118 case OMP_CLAUSE_HINT:
17119 case OMP_CLAUSE_NUM_GANGS:
17120 case OMP_CLAUSE_NUM_WORKERS:
17121 case OMP_CLAUSE_VECTOR_LENGTH:
17122 case OMP_CLAUSE_WORKER:
17123 case OMP_CLAUSE_VECTOR:
17124 case OMP_CLAUSE_ASYNC:
17125 case OMP_CLAUSE_WAIT:
17126 OMP_CLAUSE_OPERAND (nc, 0)
17127 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17128 in_decl, /*integral_constant_expression_p=*/false);
17129 break;
17130 case OMP_CLAUSE_REDUCTION:
17131 case OMP_CLAUSE_IN_REDUCTION:
17132 case OMP_CLAUSE_TASK_REDUCTION:
17133 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17134 {
17135 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17136 if (TREE_CODE (placeholder) == SCOPE_REF)
17137 {
17138 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17139 complain, in_decl);
17140 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17141 = build_qualified_name (NULL_TREE, scope,
17142 TREE_OPERAND (placeholder, 1),
17143 false);
17144 }
17145 else
17146 gcc_assert (identifier_p (placeholder));
17147 }
17148 OMP_CLAUSE_DECL (nc)
17149 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17150 in_decl, NULL);
17151 break;
17152 case OMP_CLAUSE_GANG:
17153 case OMP_CLAUSE_ALIGNED:
17154 OMP_CLAUSE_DECL (nc)
17155 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17156 in_decl, NULL);
17157 OMP_CLAUSE_OPERAND (nc, 1)
17158 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17159 in_decl, /*integral_constant_expression_p=*/false);
17160 break;
17161 case OMP_CLAUSE_LINEAR:
17162 OMP_CLAUSE_DECL (nc)
17163 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17164 in_decl, NULL);
17165 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17166 {
17167 gcc_assert (!linear_no_step);
17168 linear_no_step = nc;
17169 }
17170 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17171 OMP_CLAUSE_LINEAR_STEP (nc)
17172 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17173 complain, in_decl, NULL);
17174 else
17175 OMP_CLAUSE_LINEAR_STEP (nc)
17176 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17177 in_decl,
17178 /*integral_constant_expression_p=*/false);
17179 break;
17180 case OMP_CLAUSE_NOWAIT:
17181 case OMP_CLAUSE_DEFAULT:
17182 case OMP_CLAUSE_UNTIED:
17183 case OMP_CLAUSE_MERGEABLE:
17184 case OMP_CLAUSE_INBRANCH:
17185 case OMP_CLAUSE_NOTINBRANCH:
17186 case OMP_CLAUSE_PROC_BIND:
17187 case OMP_CLAUSE_FOR:
17188 case OMP_CLAUSE_PARALLEL:
17189 case OMP_CLAUSE_SECTIONS:
17190 case OMP_CLAUSE_TASKGROUP:
17191 case OMP_CLAUSE_NOGROUP:
17192 case OMP_CLAUSE_THREADS:
17193 case OMP_CLAUSE_SIMD:
17194 case OMP_CLAUSE_DEFAULTMAP:
17195 case OMP_CLAUSE_ORDER:
17196 case OMP_CLAUSE_BIND:
17197 case OMP_CLAUSE_INDEPENDENT:
17198 case OMP_CLAUSE_AUTO:
17199 case OMP_CLAUSE_SEQ:
17200 case OMP_CLAUSE_IF_PRESENT:
17201 case OMP_CLAUSE_FINALIZE:
17202 break;
17203 default:
17204 gcc_unreachable ();
17205 }
17206 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17207 switch (OMP_CLAUSE_CODE (nc))
17208 {
17209 case OMP_CLAUSE_SHARED:
17210 case OMP_CLAUSE_PRIVATE:
17211 case OMP_CLAUSE_FIRSTPRIVATE:
17212 case OMP_CLAUSE_LASTPRIVATE:
17213 case OMP_CLAUSE_COPYPRIVATE:
17214 case OMP_CLAUSE_LINEAR:
17215 case OMP_CLAUSE_REDUCTION:
17216 case OMP_CLAUSE_IN_REDUCTION:
17217 case OMP_CLAUSE_TASK_REDUCTION:
17218 case OMP_CLAUSE_USE_DEVICE_PTR:
17219 case OMP_CLAUSE_USE_DEVICE_ADDR:
17220 case OMP_CLAUSE_IS_DEVICE_PTR:
17221 case OMP_CLAUSE_INCLUSIVE:
17222 case OMP_CLAUSE_EXCLUSIVE:
17223 /* tsubst_expr on SCOPE_REF results in returning
17224 finish_non_static_data_member result. Undo that here. */
17225 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17226 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17227 == IDENTIFIER_NODE))
17228 {
17229 tree t = OMP_CLAUSE_DECL (nc);
17230 tree v = t;
17231 while (v)
17232 switch (TREE_CODE (v))
17233 {
17234 case COMPONENT_REF:
17235 case MEM_REF:
17236 case INDIRECT_REF:
17237 CASE_CONVERT:
17238 case POINTER_PLUS_EXPR:
17239 v = TREE_OPERAND (v, 0);
17240 continue;
17241 case PARM_DECL:
17242 if (DECL_CONTEXT (v) == current_function_decl
17243 && DECL_ARTIFICIAL (v)
17244 && DECL_NAME (v) == this_identifier)
17245 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17246 /* FALLTHRU */
17247 default:
17248 v = NULL_TREE;
17249 break;
17250 }
17251 }
17252 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17253 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17254 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17255 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17256 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17257 {
17258 tree decl = OMP_CLAUSE_DECL (nc);
17259 if (VAR_P (decl))
17260 {
17261 retrofit_lang_decl (decl);
17262 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17263 }
17264 }
17265 break;
17266 default:
17267 break;
17268 }
17269 }
17270
17271 new_clauses = nreverse (new_clauses);
17272 if (ort != C_ORT_OMP_DECLARE_SIMD)
17273 {
17274 new_clauses = finish_omp_clauses (new_clauses, ort);
17275 if (linear_no_step)
17276 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17277 if (nc == linear_no_step)
17278 {
17279 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17280 break;
17281 }
17282 }
17283 return new_clauses;
17284 }
17285
17286 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17287
17288 static tree
17289 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17290 tree in_decl)
17291 {
17292 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17293
17294 tree purpose, value, chain;
17295
17296 if (t == NULL)
17297 return t;
17298
17299 if (TREE_CODE (t) != TREE_LIST)
17300 return tsubst_copy_and_build (t, args, complain, in_decl,
17301 /*function_p=*/false,
17302 /*integral_constant_expression_p=*/false);
17303
17304 if (t == void_list_node)
17305 return t;
17306
17307 purpose = TREE_PURPOSE (t);
17308 if (purpose)
17309 purpose = RECUR (purpose);
17310 value = TREE_VALUE (t);
17311 if (value)
17312 {
17313 if (TREE_CODE (value) != LABEL_DECL)
17314 value = RECUR (value);
17315 else
17316 {
17317 value = lookup_label (DECL_NAME (value));
17318 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17319 TREE_USED (value) = 1;
17320 }
17321 }
17322 chain = TREE_CHAIN (t);
17323 if (chain && chain != void_type_node)
17324 chain = RECUR (chain);
17325 return tree_cons (purpose, value, chain);
17326 #undef RECUR
17327 }
17328
17329 /* Used to temporarily communicate the list of #pragma omp parallel
17330 clauses to #pragma omp for instantiation if they are combined
17331 together. */
17332
17333 static tree *omp_parallel_combined_clauses;
17334
17335 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17336 tree *, unsigned int *);
17337
17338 /* Substitute one OMP_FOR iterator. */
17339
17340 static bool
17341 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17342 tree initv, tree condv, tree incrv, tree *clauses,
17343 tree args, tsubst_flags_t complain, tree in_decl,
17344 bool integral_constant_expression_p)
17345 {
17346 #define RECUR(NODE) \
17347 tsubst_expr ((NODE), args, complain, in_decl, \
17348 integral_constant_expression_p)
17349 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17350 bool ret = false;
17351
17352 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17353 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17354
17355 decl = TREE_OPERAND (init, 0);
17356 init = TREE_OPERAND (init, 1);
17357 tree decl_expr = NULL_TREE;
17358 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17359 if (range_for)
17360 {
17361 bool decomp = false;
17362 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17363 {
17364 tree v = DECL_VALUE_EXPR (decl);
17365 if (TREE_CODE (v) == ARRAY_REF
17366 && VAR_P (TREE_OPERAND (v, 0))
17367 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17368 {
17369 tree decomp_first = NULL_TREE;
17370 unsigned decomp_cnt = 0;
17371 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17372 maybe_push_decl (d);
17373 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17374 in_decl, &decomp_first, &decomp_cnt);
17375 decomp = true;
17376 if (d == error_mark_node)
17377 decl = error_mark_node;
17378 else
17379 for (unsigned int i = 0; i < decomp_cnt; i++)
17380 {
17381 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17382 {
17383 tree v = build_nt (ARRAY_REF, d,
17384 size_int (decomp_cnt - i - 1),
17385 NULL_TREE, NULL_TREE);
17386 SET_DECL_VALUE_EXPR (decomp_first, v);
17387 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17388 }
17389 fit_decomposition_lang_decl (decomp_first, d);
17390 decomp_first = DECL_CHAIN (decomp_first);
17391 }
17392 }
17393 }
17394 decl = tsubst_decl (decl, args, complain);
17395 if (!decomp)
17396 maybe_push_decl (decl);
17397 }
17398 else if (init && TREE_CODE (init) == DECL_EXPR)
17399 {
17400 /* We need to jump through some hoops to handle declarations in the
17401 init-statement, since we might need to handle auto deduction,
17402 but we need to keep control of initialization. */
17403 decl_expr = init;
17404 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17405 decl = tsubst_decl (decl, args, complain);
17406 }
17407 else
17408 {
17409 if (TREE_CODE (decl) == SCOPE_REF)
17410 {
17411 decl = RECUR (decl);
17412 if (TREE_CODE (decl) == COMPONENT_REF)
17413 {
17414 tree v = decl;
17415 while (v)
17416 switch (TREE_CODE (v))
17417 {
17418 case COMPONENT_REF:
17419 case MEM_REF:
17420 case INDIRECT_REF:
17421 CASE_CONVERT:
17422 case POINTER_PLUS_EXPR:
17423 v = TREE_OPERAND (v, 0);
17424 continue;
17425 case PARM_DECL:
17426 if (DECL_CONTEXT (v) == current_function_decl
17427 && DECL_ARTIFICIAL (v)
17428 && DECL_NAME (v) == this_identifier)
17429 {
17430 decl = TREE_OPERAND (decl, 1);
17431 decl = omp_privatize_field (decl, false);
17432 }
17433 /* FALLTHRU */
17434 default:
17435 v = NULL_TREE;
17436 break;
17437 }
17438 }
17439 }
17440 else
17441 decl = RECUR (decl);
17442 }
17443 init = RECUR (init);
17444
17445 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17446 {
17447 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17448 if (TREE_CODE (o) == TREE_LIST)
17449 TREE_VEC_ELT (orig_declv, i)
17450 = tree_cons (RECUR (TREE_PURPOSE (o)),
17451 RECUR (TREE_VALUE (o)),
17452 NULL_TREE);
17453 else
17454 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17455 }
17456
17457 if (range_for)
17458 {
17459 tree this_pre_body = NULL_TREE;
17460 tree orig_init = NULL_TREE;
17461 tree orig_decl = NULL_TREE;
17462 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17463 orig_init, cond, incr);
17464 if (orig_decl)
17465 {
17466 if (orig_declv == NULL_TREE)
17467 orig_declv = copy_node (declv);
17468 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17469 ret = true;
17470 }
17471 else if (orig_declv)
17472 TREE_VEC_ELT (orig_declv, i) = decl;
17473 }
17474
17475 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17476 if (!range_for && auto_node && init)
17477 TREE_TYPE (decl)
17478 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17479
17480 gcc_assert (!type_dependent_expression_p (decl));
17481
17482 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17483 {
17484 if (decl_expr)
17485 {
17486 /* Declare the variable, but don't let that initialize it. */
17487 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17488 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17489 RECUR (decl_expr);
17490 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17491 }
17492
17493 if (!range_for)
17494 {
17495 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
17496 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17497 if (TREE_CODE (incr) == MODIFY_EXPR)
17498 {
17499 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17500 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17501 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17502 NOP_EXPR, rhs, complain);
17503 }
17504 else
17505 incr = RECUR (incr);
17506 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17507 TREE_VEC_ELT (orig_declv, i) = decl;
17508 }
17509 TREE_VEC_ELT (declv, i) = decl;
17510 TREE_VEC_ELT (initv, i) = init;
17511 TREE_VEC_ELT (condv, i) = cond;
17512 TREE_VEC_ELT (incrv, i) = incr;
17513 return ret;
17514 }
17515
17516 if (decl_expr)
17517 {
17518 /* Declare and initialize the variable. */
17519 RECUR (decl_expr);
17520 init = NULL_TREE;
17521 }
17522 else if (init)
17523 {
17524 tree *pc;
17525 int j;
17526 for (j = ((omp_parallel_combined_clauses == NULL
17527 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17528 {
17529 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17530 {
17531 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17532 && OMP_CLAUSE_DECL (*pc) == decl)
17533 break;
17534 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17535 && OMP_CLAUSE_DECL (*pc) == decl)
17536 {
17537 if (j)
17538 break;
17539 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17540 tree c = *pc;
17541 *pc = OMP_CLAUSE_CHAIN (c);
17542 OMP_CLAUSE_CHAIN (c) = *clauses;
17543 *clauses = c;
17544 }
17545 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17546 && OMP_CLAUSE_DECL (*pc) == decl)
17547 {
17548 error ("iteration variable %qD should not be firstprivate",
17549 decl);
17550 *pc = OMP_CLAUSE_CHAIN (*pc);
17551 }
17552 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17553 && OMP_CLAUSE_DECL (*pc) == decl)
17554 {
17555 error ("iteration variable %qD should not be reduction",
17556 decl);
17557 *pc = OMP_CLAUSE_CHAIN (*pc);
17558 }
17559 else
17560 pc = &OMP_CLAUSE_CHAIN (*pc);
17561 }
17562 if (*pc)
17563 break;
17564 }
17565 if (*pc == NULL_TREE)
17566 {
17567 tree c = build_omp_clause (input_location,
17568 TREE_CODE (t) == OMP_LOOP
17569 ? OMP_CLAUSE_LASTPRIVATE
17570 : OMP_CLAUSE_PRIVATE);
17571 OMP_CLAUSE_DECL (c) = decl;
17572 c = finish_omp_clauses (c, C_ORT_OMP);
17573 if (c)
17574 {
17575 OMP_CLAUSE_CHAIN (c) = *clauses;
17576 *clauses = c;
17577 }
17578 }
17579 }
17580 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17581 if (COMPARISON_CLASS_P (cond))
17582 {
17583 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17584 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17585 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17586 }
17587 else
17588 cond = RECUR (cond);
17589 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17590 switch (TREE_CODE (incr))
17591 {
17592 case PREINCREMENT_EXPR:
17593 case PREDECREMENT_EXPR:
17594 case POSTINCREMENT_EXPR:
17595 case POSTDECREMENT_EXPR:
17596 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17597 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17598 break;
17599 case MODIFY_EXPR:
17600 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17601 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17602 {
17603 tree rhs = TREE_OPERAND (incr, 1);
17604 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17605 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17606 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17607 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17608 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17609 rhs0, rhs1));
17610 }
17611 else
17612 incr = RECUR (incr);
17613 break;
17614 case MODOP_EXPR:
17615 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17616 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17617 {
17618 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17619 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17620 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17621 TREE_TYPE (decl), lhs,
17622 RECUR (TREE_OPERAND (incr, 2))));
17623 }
17624 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17625 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17626 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17627 {
17628 tree rhs = TREE_OPERAND (incr, 2);
17629 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17630 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17631 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17632 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17633 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17634 rhs0, rhs1));
17635 }
17636 else
17637 incr = RECUR (incr);
17638 break;
17639 default:
17640 incr = RECUR (incr);
17641 break;
17642 }
17643
17644 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17645 TREE_VEC_ELT (orig_declv, i) = decl;
17646 TREE_VEC_ELT (declv, i) = decl;
17647 TREE_VEC_ELT (initv, i) = init;
17648 TREE_VEC_ELT (condv, i) = cond;
17649 TREE_VEC_ELT (incrv, i) = incr;
17650 return false;
17651 #undef RECUR
17652 }
17653
17654 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17655 of OMP_TARGET's body. */
17656
17657 static tree
17658 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17659 {
17660 *walk_subtrees = 0;
17661 switch (TREE_CODE (*tp))
17662 {
17663 case OMP_TEAMS:
17664 return *tp;
17665 case BIND_EXPR:
17666 case STATEMENT_LIST:
17667 *walk_subtrees = 1;
17668 break;
17669 default:
17670 break;
17671 }
17672 return NULL_TREE;
17673 }
17674
17675 /* Helper function for tsubst_expr. For decomposition declaration
17676 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17677 also the corresponding decls representing the identifiers
17678 of the decomposition declaration. Return DECL if successful
17679 or error_mark_node otherwise, set *FIRST to the first decl
17680 in the list chained through DECL_CHAIN and *CNT to the number
17681 of such decls. */
17682
17683 static tree
17684 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17685 tsubst_flags_t complain, tree in_decl, tree *first,
17686 unsigned int *cnt)
17687 {
17688 tree decl2, decl3, prev = decl;
17689 *cnt = 0;
17690 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17691 for (decl2 = DECL_CHAIN (pattern_decl);
17692 decl2
17693 && VAR_P (decl2)
17694 && DECL_DECOMPOSITION_P (decl2)
17695 && DECL_NAME (decl2);
17696 decl2 = DECL_CHAIN (decl2))
17697 {
17698 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17699 {
17700 gcc_assert (errorcount);
17701 return error_mark_node;
17702 }
17703 (*cnt)++;
17704 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17705 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17706 tree v = DECL_VALUE_EXPR (decl2);
17707 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17708 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17709 decl3 = tsubst (decl2, args, complain, in_decl);
17710 SET_DECL_VALUE_EXPR (decl2, v);
17711 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17712 if (VAR_P (decl3))
17713 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17714 else
17715 {
17716 gcc_assert (errorcount);
17717 decl = error_mark_node;
17718 continue;
17719 }
17720 maybe_push_decl (decl3);
17721 if (error_operand_p (decl3))
17722 decl = error_mark_node;
17723 else if (decl != error_mark_node
17724 && DECL_CHAIN (decl3) != prev
17725 && decl != prev)
17726 {
17727 gcc_assert (errorcount);
17728 decl = error_mark_node;
17729 }
17730 else
17731 prev = decl3;
17732 }
17733 *first = prev;
17734 return decl;
17735 }
17736
17737 /* Return the proper local_specialization for init-capture pack DECL. */
17738
17739 static tree
17740 lookup_init_capture_pack (tree decl)
17741 {
17742 /* We handle normal pack captures by forwarding to the specialization of the
17743 captured parameter. We can't do that for pack init-captures; we need them
17744 to have their own local_specialization. We created the individual
17745 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17746 when we process the DECL_EXPR for the pack init-capture in the template.
17747 So, how do we find them? We don't know the capture proxy pack when
17748 building the individual resulting proxies, and we don't know the
17749 individual proxies when instantiating the pack. What we have in common is
17750 the FIELD_DECL.
17751
17752 So...when we instantiate the FIELD_DECL, we stick the result in
17753 local_specializations. Then at the DECL_EXPR we look up that result, see
17754 how many elements it has, synthesize the names, and look them up. */
17755
17756 tree cname = DECL_NAME (decl);
17757 tree val = DECL_VALUE_EXPR (decl);
17758 tree field = TREE_OPERAND (val, 1);
17759 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17760 tree fpack = retrieve_local_specialization (field);
17761 if (fpack == error_mark_node)
17762 return error_mark_node;
17763
17764 int len = 1;
17765 tree vec = NULL_TREE;
17766 tree r = NULL_TREE;
17767 if (TREE_CODE (fpack) == TREE_VEC)
17768 {
17769 len = TREE_VEC_LENGTH (fpack);
17770 vec = make_tree_vec (len);
17771 r = make_node (NONTYPE_ARGUMENT_PACK);
17772 SET_ARGUMENT_PACK_ARGS (r, vec);
17773 }
17774 for (int i = 0; i < len; ++i)
17775 {
17776 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17777 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17778 if (vec)
17779 TREE_VEC_ELT (vec, i) = elt;
17780 else
17781 r = elt;
17782 }
17783 return r;
17784 }
17785
17786 /* Like tsubst_copy for expressions, etc. but also does semantic
17787 processing. */
17788
17789 tree
17790 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17791 bool integral_constant_expression_p)
17792 {
17793 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17794 #define RECUR(NODE) \
17795 tsubst_expr ((NODE), args, complain, in_decl, \
17796 integral_constant_expression_p)
17797
17798 tree stmt, tmp;
17799 tree r;
17800 location_t loc;
17801
17802 if (t == NULL_TREE || t == error_mark_node)
17803 return t;
17804
17805 loc = input_location;
17806 if (location_t eloc = cp_expr_location (t))
17807 input_location = eloc;
17808 if (STATEMENT_CODE_P (TREE_CODE (t)))
17809 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17810
17811 switch (TREE_CODE (t))
17812 {
17813 case STATEMENT_LIST:
17814 {
17815 tree_stmt_iterator i;
17816 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17817 RECUR (tsi_stmt (i));
17818 break;
17819 }
17820
17821 case CTOR_INITIALIZER:
17822 finish_mem_initializers (tsubst_initializer_list
17823 (TREE_OPERAND (t, 0), args));
17824 break;
17825
17826 case RETURN_EXPR:
17827 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17828 break;
17829
17830 case CO_RETURN_EXPR:
17831 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
17832 break;
17833
17834 case CO_YIELD_EXPR:
17835 stmt = finish_co_yield_expr (input_location,
17836 RECUR (TREE_OPERAND (t, 0)));
17837 RETURN (stmt);
17838 break;
17839
17840 case CO_AWAIT_EXPR:
17841 stmt = finish_co_await_expr (input_location,
17842 RECUR (TREE_OPERAND (t, 0)));
17843 RETURN (stmt);
17844 break;
17845
17846 case EXPR_STMT:
17847 tmp = RECUR (EXPR_STMT_EXPR (t));
17848 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17849 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17850 else
17851 finish_expr_stmt (tmp);
17852 break;
17853
17854 case USING_STMT:
17855 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17856 break;
17857
17858 case DECL_EXPR:
17859 {
17860 tree decl, pattern_decl;
17861 tree init;
17862
17863 pattern_decl = decl = DECL_EXPR_DECL (t);
17864 if (TREE_CODE (decl) == LABEL_DECL)
17865 finish_label_decl (DECL_NAME (decl));
17866 else if (TREE_CODE (decl) == USING_DECL)
17867 {
17868 tree scope = USING_DECL_SCOPE (decl);
17869 tree name = DECL_NAME (decl);
17870
17871 scope = tsubst (scope, args, complain, in_decl);
17872 finish_nonmember_using_decl (scope, name);
17873 }
17874 else if (is_capture_proxy (decl)
17875 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17876 {
17877 /* We're in tsubst_lambda_expr, we've already inserted a new
17878 capture proxy, so look it up and register it. */
17879 tree inst;
17880 if (!DECL_PACK_P (decl))
17881 {
17882 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17883 /*nonclass*/1, /*block_p=*/true,
17884 /*ns_only*/0, LOOKUP_HIDDEN);
17885 gcc_assert (inst != decl && is_capture_proxy (inst));
17886 }
17887 else if (is_normal_capture_proxy (decl))
17888 {
17889 inst = (retrieve_local_specialization
17890 (DECL_CAPTURED_VARIABLE (decl)));
17891 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
17892 || DECL_PACK_P (inst));
17893 }
17894 else
17895 inst = lookup_init_capture_pack (decl);
17896
17897 register_local_specialization (inst, decl);
17898 break;
17899 }
17900 else if (DECL_PRETTY_FUNCTION_P (decl))
17901 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17902 DECL_NAME (decl),
17903 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17904 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17905 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17906 /* Don't copy the old closure; we'll create a new one in
17907 tsubst_lambda_expr. */
17908 break;
17909 else
17910 {
17911 init = DECL_INITIAL (decl);
17912 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17913 for local variables, so save if DECL was declared constinit. */
17914 const bool constinit_p
17915 = (VAR_P (decl)
17916 && DECL_LANG_SPECIFIC (decl)
17917 && DECL_TEMPLATE_INFO (decl)
17918 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17919 decl = tsubst (decl, args, complain, in_decl);
17920 if (decl != error_mark_node)
17921 {
17922 /* By marking the declaration as instantiated, we avoid
17923 trying to instantiate it. Since instantiate_decl can't
17924 handle local variables, and since we've already done
17925 all that needs to be done, that's the right thing to
17926 do. */
17927 if (VAR_P (decl))
17928 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17929 if (VAR_P (decl) && !DECL_NAME (decl)
17930 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17931 /* Anonymous aggregates are a special case. */
17932 finish_anon_union (decl);
17933 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17934 {
17935 DECL_CONTEXT (decl) = current_function_decl;
17936 if (DECL_NAME (decl) == this_identifier)
17937 {
17938 tree lam = DECL_CONTEXT (current_function_decl);
17939 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17940 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17941 }
17942 insert_capture_proxy (decl);
17943 }
17944 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17945 /* We already did a pushtag. */;
17946 else if (TREE_CODE (decl) == FUNCTION_DECL
17947 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17948 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17949 {
17950 DECL_CONTEXT (decl) = NULL_TREE;
17951 pushdecl (decl);
17952 DECL_CONTEXT (decl) = current_function_decl;
17953 cp_check_omp_declare_reduction (decl);
17954 }
17955 else
17956 {
17957 bool const_init = false;
17958 unsigned int cnt = 0;
17959 tree first = NULL_TREE, ndecl = error_mark_node;
17960 maybe_push_decl (decl);
17961
17962 if (VAR_P (decl)
17963 && DECL_DECOMPOSITION_P (decl)
17964 && TREE_TYPE (pattern_decl) != error_mark_node)
17965 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17966 complain, in_decl, &first,
17967 &cnt);
17968
17969 init = tsubst_init (init, decl, args, complain, in_decl);
17970
17971 if (VAR_P (decl))
17972 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17973 (pattern_decl));
17974
17975 if (ndecl != error_mark_node)
17976 cp_maybe_mangle_decomp (ndecl, first, cnt);
17977
17978 cp_finish_decl (decl, init, const_init, NULL_TREE,
17979 constinit_p ? LOOKUP_CONSTINIT : 0);
17980
17981 if (ndecl != error_mark_node)
17982 cp_finish_decomp (ndecl, first, cnt);
17983 }
17984 }
17985 }
17986
17987 break;
17988 }
17989
17990 case FOR_STMT:
17991 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17992 RECUR (FOR_INIT_STMT (t));
17993 finish_init_stmt (stmt);
17994 tmp = RECUR (FOR_COND (t));
17995 finish_for_cond (tmp, stmt, false, 0);
17996 tmp = RECUR (FOR_EXPR (t));
17997 finish_for_expr (tmp, stmt);
17998 {
17999 bool prev = note_iteration_stmt_body_start ();
18000 RECUR (FOR_BODY (t));
18001 note_iteration_stmt_body_end (prev);
18002 }
18003 finish_for_stmt (stmt);
18004 break;
18005
18006 case RANGE_FOR_STMT:
18007 {
18008 /* Construct another range_for, if this is not a final
18009 substitution (for inside a generic lambda of a
18010 template). Otherwise convert to a regular for. */
18011 tree decl, expr;
18012 stmt = (processing_template_decl
18013 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18014 : begin_for_stmt (NULL_TREE, NULL_TREE));
18015 RECUR (RANGE_FOR_INIT_STMT (t));
18016 decl = RANGE_FOR_DECL (t);
18017 decl = tsubst (decl, args, complain, in_decl);
18018 maybe_push_decl (decl);
18019 expr = RECUR (RANGE_FOR_EXPR (t));
18020
18021 tree decomp_first = NULL_TREE;
18022 unsigned decomp_cnt = 0;
18023 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18024 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18025 complain, in_decl,
18026 &decomp_first, &decomp_cnt);
18027
18028 if (processing_template_decl)
18029 {
18030 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18031 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18032 finish_range_for_decl (stmt, decl, expr);
18033 if (decomp_first && decl != error_mark_node)
18034 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18035 }
18036 else
18037 {
18038 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18039 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18040 stmt = cp_convert_range_for (stmt, decl, expr,
18041 decomp_first, decomp_cnt,
18042 RANGE_FOR_IVDEP (t), unroll);
18043 }
18044
18045 bool prev = note_iteration_stmt_body_start ();
18046 RECUR (RANGE_FOR_BODY (t));
18047 note_iteration_stmt_body_end (prev);
18048 finish_for_stmt (stmt);
18049 }
18050 break;
18051
18052 case WHILE_STMT:
18053 stmt = begin_while_stmt ();
18054 tmp = RECUR (WHILE_COND (t));
18055 finish_while_stmt_cond (tmp, stmt, false, 0);
18056 {
18057 bool prev = note_iteration_stmt_body_start ();
18058 RECUR (WHILE_BODY (t));
18059 note_iteration_stmt_body_end (prev);
18060 }
18061 finish_while_stmt (stmt);
18062 break;
18063
18064 case DO_STMT:
18065 stmt = begin_do_stmt ();
18066 {
18067 bool prev = note_iteration_stmt_body_start ();
18068 RECUR (DO_BODY (t));
18069 note_iteration_stmt_body_end (prev);
18070 }
18071 finish_do_body (stmt);
18072 tmp = RECUR (DO_COND (t));
18073 finish_do_stmt (tmp, stmt, false, 0);
18074 break;
18075
18076 case IF_STMT:
18077 stmt = begin_if_stmt ();
18078 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18079 if (IF_STMT_CONSTEXPR_P (t))
18080 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18081 tmp = RECUR (IF_COND (t));
18082 tmp = finish_if_stmt_cond (tmp, stmt);
18083 if (IF_STMT_CONSTEXPR_P (t)
18084 && instantiation_dependent_expression_p (tmp))
18085 {
18086 /* We're partially instantiating a generic lambda, but the condition
18087 of the constexpr if is still dependent. Don't substitute into the
18088 branches now, just remember the template arguments. */
18089 do_poplevel (IF_SCOPE (stmt));
18090 IF_COND (stmt) = IF_COND (t);
18091 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18092 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18093 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18094 add_stmt (stmt);
18095 break;
18096 }
18097 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18098 /* Don't instantiate the THEN_CLAUSE. */;
18099 else
18100 {
18101 tree folded = fold_non_dependent_expr (tmp, complain);
18102 bool inhibit = integer_zerop (folded);
18103 if (inhibit)
18104 ++c_inhibit_evaluation_warnings;
18105 RECUR (THEN_CLAUSE (t));
18106 if (inhibit)
18107 --c_inhibit_evaluation_warnings;
18108 }
18109 finish_then_clause (stmt);
18110
18111 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18112 /* Don't instantiate the ELSE_CLAUSE. */;
18113 else if (ELSE_CLAUSE (t))
18114 {
18115 tree folded = fold_non_dependent_expr (tmp, complain);
18116 bool inhibit = integer_nonzerop (folded);
18117 begin_else_clause (stmt);
18118 if (inhibit)
18119 ++c_inhibit_evaluation_warnings;
18120 RECUR (ELSE_CLAUSE (t));
18121 if (inhibit)
18122 --c_inhibit_evaluation_warnings;
18123 finish_else_clause (stmt);
18124 }
18125
18126 finish_if_stmt (stmt);
18127 break;
18128
18129 case BIND_EXPR:
18130 if (BIND_EXPR_BODY_BLOCK (t))
18131 stmt = begin_function_body ();
18132 else
18133 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18134 ? BCS_TRY_BLOCK : 0);
18135
18136 RECUR (BIND_EXPR_BODY (t));
18137
18138 if (BIND_EXPR_BODY_BLOCK (t))
18139 finish_function_body (stmt);
18140 else
18141 finish_compound_stmt (stmt);
18142 break;
18143
18144 case BREAK_STMT:
18145 finish_break_stmt ();
18146 break;
18147
18148 case CONTINUE_STMT:
18149 finish_continue_stmt ();
18150 break;
18151
18152 case SWITCH_STMT:
18153 stmt = begin_switch_stmt ();
18154 tmp = RECUR (SWITCH_STMT_COND (t));
18155 finish_switch_cond (tmp, stmt);
18156 RECUR (SWITCH_STMT_BODY (t));
18157 finish_switch_stmt (stmt);
18158 break;
18159
18160 case CASE_LABEL_EXPR:
18161 {
18162 tree decl = CASE_LABEL (t);
18163 tree low = RECUR (CASE_LOW (t));
18164 tree high = RECUR (CASE_HIGH (t));
18165 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18166 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18167 {
18168 tree label = CASE_LABEL (l);
18169 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18170 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18171 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18172 }
18173 }
18174 break;
18175
18176 case LABEL_EXPR:
18177 {
18178 tree decl = LABEL_EXPR_LABEL (t);
18179 tree label;
18180
18181 label = finish_label_stmt (DECL_NAME (decl));
18182 if (TREE_CODE (label) == LABEL_DECL)
18183 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18184 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18185 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18186 }
18187 break;
18188
18189 case GOTO_EXPR:
18190 tmp = GOTO_DESTINATION (t);
18191 if (TREE_CODE (tmp) != LABEL_DECL)
18192 /* Computed goto's must be tsubst'd into. On the other hand,
18193 non-computed gotos must not be; the identifier in question
18194 will have no binding. */
18195 tmp = RECUR (tmp);
18196 else
18197 tmp = DECL_NAME (tmp);
18198 finish_goto_stmt (tmp);
18199 break;
18200
18201 case ASM_EXPR:
18202 {
18203 tree string = RECUR (ASM_STRING (t));
18204 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18205 complain, in_decl);
18206 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18207 complain, in_decl);
18208 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18209 complain, in_decl);
18210 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18211 complain, in_decl);
18212 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18213 outputs, inputs, clobbers, labels,
18214 ASM_INLINE_P (t));
18215 tree asm_expr = tmp;
18216 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18217 asm_expr = TREE_OPERAND (asm_expr, 0);
18218 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18219 }
18220 break;
18221
18222 case TRY_BLOCK:
18223 if (CLEANUP_P (t))
18224 {
18225 stmt = begin_try_block ();
18226 RECUR (TRY_STMTS (t));
18227 finish_cleanup_try_block (stmt);
18228 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18229 }
18230 else
18231 {
18232 tree compound_stmt = NULL_TREE;
18233
18234 if (FN_TRY_BLOCK_P (t))
18235 stmt = begin_function_try_block (&compound_stmt);
18236 else
18237 stmt = begin_try_block ();
18238
18239 RECUR (TRY_STMTS (t));
18240
18241 if (FN_TRY_BLOCK_P (t))
18242 finish_function_try_block (stmt);
18243 else
18244 finish_try_block (stmt);
18245
18246 RECUR (TRY_HANDLERS (t));
18247 if (FN_TRY_BLOCK_P (t))
18248 finish_function_handler_sequence (stmt, compound_stmt);
18249 else
18250 finish_handler_sequence (stmt);
18251 }
18252 break;
18253
18254 case HANDLER:
18255 {
18256 tree decl = HANDLER_PARMS (t);
18257
18258 if (decl)
18259 {
18260 decl = tsubst (decl, args, complain, in_decl);
18261 /* Prevent instantiate_decl from trying to instantiate
18262 this variable. We've already done all that needs to be
18263 done. */
18264 if (decl != error_mark_node)
18265 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18266 }
18267 stmt = begin_handler ();
18268 finish_handler_parms (decl, stmt);
18269 RECUR (HANDLER_BODY (t));
18270 finish_handler (stmt);
18271 }
18272 break;
18273
18274 case TAG_DEFN:
18275 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18276 if (CLASS_TYPE_P (tmp))
18277 {
18278 /* Local classes are not independent templates; they are
18279 instantiated along with their containing function. And this
18280 way we don't have to deal with pushing out of one local class
18281 to instantiate a member of another local class. */
18282 /* Closures are handled by the LAMBDA_EXPR. */
18283 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18284 complete_type (tmp);
18285 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18286 if ((VAR_P (fld)
18287 || (TREE_CODE (fld) == FUNCTION_DECL
18288 && !DECL_ARTIFICIAL (fld)))
18289 && DECL_TEMPLATE_INSTANTIATION (fld))
18290 instantiate_decl (fld, /*defer_ok=*/false,
18291 /*expl_inst_class=*/false);
18292 }
18293 break;
18294
18295 case STATIC_ASSERT:
18296 {
18297 tree condition;
18298
18299 ++c_inhibit_evaluation_warnings;
18300 condition =
18301 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18302 args,
18303 complain, in_decl,
18304 /*integral_constant_expression_p=*/true);
18305 --c_inhibit_evaluation_warnings;
18306
18307 finish_static_assert (condition,
18308 STATIC_ASSERT_MESSAGE (t),
18309 STATIC_ASSERT_SOURCE_LOCATION (t),
18310 /*member_p=*/false);
18311 }
18312 break;
18313
18314 case OACC_KERNELS:
18315 case OACC_PARALLEL:
18316 case OACC_SERIAL:
18317 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18318 in_decl);
18319 stmt = begin_omp_parallel ();
18320 RECUR (OMP_BODY (t));
18321 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18322 break;
18323
18324 case OMP_PARALLEL:
18325 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18326 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18327 complain, in_decl);
18328 if (OMP_PARALLEL_COMBINED (t))
18329 omp_parallel_combined_clauses = &tmp;
18330 stmt = begin_omp_parallel ();
18331 RECUR (OMP_PARALLEL_BODY (t));
18332 gcc_assert (omp_parallel_combined_clauses == NULL);
18333 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18334 = OMP_PARALLEL_COMBINED (t);
18335 pop_omp_privatization_clauses (r);
18336 break;
18337
18338 case OMP_TASK:
18339 if (OMP_TASK_BODY (t) == NULL_TREE)
18340 {
18341 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18342 complain, in_decl);
18343 t = copy_node (t);
18344 OMP_TASK_CLAUSES (t) = tmp;
18345 add_stmt (t);
18346 break;
18347 }
18348 r = push_omp_privatization_clauses (false);
18349 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18350 complain, in_decl);
18351 stmt = begin_omp_task ();
18352 RECUR (OMP_TASK_BODY (t));
18353 finish_omp_task (tmp, stmt);
18354 pop_omp_privatization_clauses (r);
18355 break;
18356
18357 case OMP_FOR:
18358 case OMP_LOOP:
18359 case OMP_SIMD:
18360 case OMP_DISTRIBUTE:
18361 case OMP_TASKLOOP:
18362 case OACC_LOOP:
18363 {
18364 tree clauses, body, pre_body;
18365 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18366 tree orig_declv = NULL_TREE;
18367 tree incrv = NULL_TREE;
18368 enum c_omp_region_type ort = C_ORT_OMP;
18369 bool any_range_for = false;
18370 int i;
18371
18372 if (TREE_CODE (t) == OACC_LOOP)
18373 ort = C_ORT_ACC;
18374
18375 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18376 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18377 in_decl);
18378 if (OMP_FOR_INIT (t) != NULL_TREE)
18379 {
18380 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18381 if (OMP_FOR_ORIG_DECLS (t))
18382 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18383 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18384 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18385 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18386 }
18387
18388 keep_next_level (true);
18389 stmt = begin_omp_structured_block ();
18390
18391 pre_body = push_stmt_list ();
18392 RECUR (OMP_FOR_PRE_BODY (t));
18393 pre_body = pop_stmt_list (pre_body);
18394
18395 if (OMP_FOR_INIT (t) != NULL_TREE)
18396 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18397 any_range_for
18398 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18399 condv, incrv, &clauses, args,
18400 complain, in_decl,
18401 integral_constant_expression_p);
18402 omp_parallel_combined_clauses = NULL;
18403
18404 if (any_range_for)
18405 {
18406 gcc_assert (orig_declv);
18407 body = begin_omp_structured_block ();
18408 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18409 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18410 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18411 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18412 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18413 TREE_VEC_ELT (declv, i));
18414 }
18415 else
18416 body = push_stmt_list ();
18417 RECUR (OMP_FOR_BODY (t));
18418 if (any_range_for)
18419 body = finish_omp_structured_block (body);
18420 else
18421 body = pop_stmt_list (body);
18422
18423 if (OMP_FOR_INIT (t) != NULL_TREE)
18424 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18425 orig_declv, initv, condv, incrv, body, pre_body,
18426 NULL, clauses);
18427 else
18428 {
18429 t = make_node (TREE_CODE (t));
18430 TREE_TYPE (t) = void_type_node;
18431 OMP_FOR_BODY (t) = body;
18432 OMP_FOR_PRE_BODY (t) = pre_body;
18433 OMP_FOR_CLAUSES (t) = clauses;
18434 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18435 add_stmt (t);
18436 }
18437
18438 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18439 t));
18440 pop_omp_privatization_clauses (r);
18441 }
18442 break;
18443
18444 case OMP_SECTIONS:
18445 omp_parallel_combined_clauses = NULL;
18446 /* FALLTHRU */
18447 case OMP_SINGLE:
18448 case OMP_TEAMS:
18449 case OMP_CRITICAL:
18450 case OMP_TASKGROUP:
18451 case OMP_SCAN:
18452 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18453 && OMP_TEAMS_COMBINED (t));
18454 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18455 in_decl);
18456 if (TREE_CODE (t) == OMP_TEAMS)
18457 {
18458 keep_next_level (true);
18459 stmt = begin_omp_structured_block ();
18460 RECUR (OMP_BODY (t));
18461 stmt = finish_omp_structured_block (stmt);
18462 }
18463 else
18464 {
18465 stmt = push_stmt_list ();
18466 RECUR (OMP_BODY (t));
18467 stmt = pop_stmt_list (stmt);
18468 }
18469
18470 t = copy_node (t);
18471 OMP_BODY (t) = stmt;
18472 OMP_CLAUSES (t) = tmp;
18473 add_stmt (t);
18474 pop_omp_privatization_clauses (r);
18475 break;
18476
18477 case OMP_DEPOBJ:
18478 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18479 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18480 {
18481 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18482 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18483 {
18484 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18485 args, complain, in_decl);
18486 if (tmp == NULL_TREE)
18487 tmp = error_mark_node;
18488 }
18489 else
18490 {
18491 kind = (enum omp_clause_depend_kind)
18492 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18493 tmp = NULL_TREE;
18494 }
18495 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18496 }
18497 else
18498 finish_omp_depobj (EXPR_LOCATION (t), r,
18499 OMP_CLAUSE_DEPEND_SOURCE,
18500 OMP_DEPOBJ_CLAUSES (t));
18501 break;
18502
18503 case OACC_DATA:
18504 case OMP_TARGET_DATA:
18505 case OMP_TARGET:
18506 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18507 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18508 in_decl);
18509 keep_next_level (true);
18510 stmt = begin_omp_structured_block ();
18511
18512 RECUR (OMP_BODY (t));
18513 stmt = finish_omp_structured_block (stmt);
18514
18515 t = copy_node (t);
18516 OMP_BODY (t) = stmt;
18517 OMP_CLAUSES (t) = tmp;
18518 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18519 {
18520 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18521 if (teams)
18522 {
18523 /* For combined target teams, ensure the num_teams and
18524 thread_limit clause expressions are evaluated on the host,
18525 before entering the target construct. */
18526 tree c;
18527 for (c = OMP_TEAMS_CLAUSES (teams);
18528 c; c = OMP_CLAUSE_CHAIN (c))
18529 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18530 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18531 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18532 {
18533 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18534 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18535 if (expr == error_mark_node)
18536 continue;
18537 tmp = TARGET_EXPR_SLOT (expr);
18538 add_stmt (expr);
18539 OMP_CLAUSE_OPERAND (c, 0) = expr;
18540 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18541 OMP_CLAUSE_FIRSTPRIVATE);
18542 OMP_CLAUSE_DECL (tc) = tmp;
18543 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18544 OMP_TARGET_CLAUSES (t) = tc;
18545 }
18546 }
18547 }
18548 add_stmt (t);
18549 break;
18550
18551 case OACC_DECLARE:
18552 t = copy_node (t);
18553 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18554 complain, in_decl);
18555 OACC_DECLARE_CLAUSES (t) = tmp;
18556 add_stmt (t);
18557 break;
18558
18559 case OMP_TARGET_UPDATE:
18560 case OMP_TARGET_ENTER_DATA:
18561 case OMP_TARGET_EXIT_DATA:
18562 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18563 complain, in_decl);
18564 t = copy_node (t);
18565 OMP_STANDALONE_CLAUSES (t) = tmp;
18566 add_stmt (t);
18567 break;
18568
18569 case OACC_ENTER_DATA:
18570 case OACC_EXIT_DATA:
18571 case OACC_UPDATE:
18572 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18573 complain, in_decl);
18574 t = copy_node (t);
18575 OMP_STANDALONE_CLAUSES (t) = tmp;
18576 add_stmt (t);
18577 break;
18578
18579 case OMP_ORDERED:
18580 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18581 complain, in_decl);
18582 stmt = push_stmt_list ();
18583 RECUR (OMP_BODY (t));
18584 stmt = pop_stmt_list (stmt);
18585
18586 t = copy_node (t);
18587 OMP_BODY (t) = stmt;
18588 OMP_ORDERED_CLAUSES (t) = tmp;
18589 add_stmt (t);
18590 break;
18591
18592 case OMP_MASTER:
18593 omp_parallel_combined_clauses = NULL;
18594 /* FALLTHRU */
18595 case OMP_SECTION:
18596 stmt = push_stmt_list ();
18597 RECUR (OMP_BODY (t));
18598 stmt = pop_stmt_list (stmt);
18599
18600 t = copy_node (t);
18601 OMP_BODY (t) = stmt;
18602 add_stmt (t);
18603 break;
18604
18605 case OMP_ATOMIC:
18606 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18607 tmp = NULL_TREE;
18608 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18609 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18610 complain, in_decl);
18611 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18612 {
18613 tree op1 = TREE_OPERAND (t, 1);
18614 tree rhs1 = NULL_TREE;
18615 tree lhs, rhs;
18616 if (TREE_CODE (op1) == COMPOUND_EXPR)
18617 {
18618 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18619 op1 = TREE_OPERAND (op1, 1);
18620 }
18621 lhs = RECUR (TREE_OPERAND (op1, 0));
18622 rhs = RECUR (TREE_OPERAND (op1, 1));
18623 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18624 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18625 OMP_ATOMIC_MEMORY_ORDER (t));
18626 }
18627 else
18628 {
18629 tree op1 = TREE_OPERAND (t, 1);
18630 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18631 tree rhs1 = NULL_TREE;
18632 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18633 enum tree_code opcode = NOP_EXPR;
18634 if (code == OMP_ATOMIC_READ)
18635 {
18636 v = RECUR (TREE_OPERAND (op1, 0));
18637 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18638 }
18639 else if (code == OMP_ATOMIC_CAPTURE_OLD
18640 || code == OMP_ATOMIC_CAPTURE_NEW)
18641 {
18642 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18643 v = RECUR (TREE_OPERAND (op1, 0));
18644 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18645 if (TREE_CODE (op11) == COMPOUND_EXPR)
18646 {
18647 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18648 op11 = TREE_OPERAND (op11, 1);
18649 }
18650 lhs = RECUR (TREE_OPERAND (op11, 0));
18651 rhs = RECUR (TREE_OPERAND (op11, 1));
18652 opcode = TREE_CODE (op11);
18653 if (opcode == MODIFY_EXPR)
18654 opcode = NOP_EXPR;
18655 }
18656 else
18657 {
18658 code = OMP_ATOMIC;
18659 lhs = RECUR (TREE_OPERAND (op1, 0));
18660 rhs = RECUR (TREE_OPERAND (op1, 1));
18661 }
18662 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18663 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18664 }
18665 break;
18666
18667 case TRANSACTION_EXPR:
18668 {
18669 int flags = 0;
18670 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18671 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18672
18673 if (TRANSACTION_EXPR_IS_STMT (t))
18674 {
18675 tree body = TRANSACTION_EXPR_BODY (t);
18676 tree noex = NULL_TREE;
18677 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18678 {
18679 noex = MUST_NOT_THROW_COND (body);
18680 if (noex == NULL_TREE)
18681 noex = boolean_true_node;
18682 body = TREE_OPERAND (body, 0);
18683 }
18684 stmt = begin_transaction_stmt (input_location, NULL, flags);
18685 RECUR (body);
18686 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18687 }
18688 else
18689 {
18690 stmt = build_transaction_expr (EXPR_LOCATION (t),
18691 RECUR (TRANSACTION_EXPR_BODY (t)),
18692 flags, NULL_TREE);
18693 RETURN (stmt);
18694 }
18695 }
18696 break;
18697
18698 case MUST_NOT_THROW_EXPR:
18699 {
18700 tree op0 = RECUR (TREE_OPERAND (t, 0));
18701 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18702 RETURN (build_must_not_throw_expr (op0, cond));
18703 }
18704
18705 case EXPR_PACK_EXPANSION:
18706 error ("invalid use of pack expansion expression");
18707 RETURN (error_mark_node);
18708
18709 case NONTYPE_ARGUMENT_PACK:
18710 error ("use %<...%> to expand argument pack");
18711 RETURN (error_mark_node);
18712
18713 case COMPOUND_EXPR:
18714 tmp = RECUR (TREE_OPERAND (t, 0));
18715 if (tmp == NULL_TREE)
18716 /* If the first operand was a statement, we're done with it. */
18717 RETURN (RECUR (TREE_OPERAND (t, 1)));
18718 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18719 RECUR (TREE_OPERAND (t, 1)),
18720 complain));
18721
18722 case ANNOTATE_EXPR:
18723 tmp = RECUR (TREE_OPERAND (t, 0));
18724 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18725 TREE_TYPE (tmp), tmp,
18726 RECUR (TREE_OPERAND (t, 1)),
18727 RECUR (TREE_OPERAND (t, 2))));
18728
18729 case PREDICT_EXPR:
18730 RETURN (add_stmt (copy_node (t)));
18731
18732 default:
18733 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18734
18735 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18736 /*function_p=*/false,
18737 integral_constant_expression_p));
18738 }
18739
18740 RETURN (NULL_TREE);
18741 out:
18742 input_location = loc;
18743 return r;
18744 #undef RECUR
18745 #undef RETURN
18746 }
18747
18748 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18749 function. For description of the body see comment above
18750 cp_parser_omp_declare_reduction_exprs. */
18751
18752 static void
18753 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18754 {
18755 if (t == NULL_TREE || t == error_mark_node)
18756 return;
18757
18758 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
18759
18760 tree_stmt_iterator tsi;
18761 int i;
18762 tree stmts[7];
18763 memset (stmts, 0, sizeof stmts);
18764 for (i = 0, tsi = tsi_start (t);
18765 i < 7 && !tsi_end_p (tsi);
18766 i++, tsi_next (&tsi))
18767 stmts[i] = tsi_stmt (tsi);
18768 gcc_assert (tsi_end_p (tsi));
18769
18770 if (i >= 3)
18771 {
18772 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18773 && TREE_CODE (stmts[1]) == DECL_EXPR);
18774 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18775 args, complain, in_decl);
18776 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18777 args, complain, in_decl);
18778 DECL_CONTEXT (omp_out) = current_function_decl;
18779 DECL_CONTEXT (omp_in) = current_function_decl;
18780 keep_next_level (true);
18781 tree block = begin_omp_structured_block ();
18782 tsubst_expr (stmts[2], args, complain, in_decl, false);
18783 block = finish_omp_structured_block (block);
18784 block = maybe_cleanup_point_expr_void (block);
18785 add_decl_expr (omp_out);
18786 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18787 TREE_NO_WARNING (omp_out) = 1;
18788 add_decl_expr (omp_in);
18789 finish_expr_stmt (block);
18790 }
18791 if (i >= 6)
18792 {
18793 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18794 && TREE_CODE (stmts[4]) == DECL_EXPR);
18795 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18796 args, complain, in_decl);
18797 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18798 args, complain, in_decl);
18799 DECL_CONTEXT (omp_priv) = current_function_decl;
18800 DECL_CONTEXT (omp_orig) = current_function_decl;
18801 keep_next_level (true);
18802 tree block = begin_omp_structured_block ();
18803 tsubst_expr (stmts[5], args, complain, in_decl, false);
18804 block = finish_omp_structured_block (block);
18805 block = maybe_cleanup_point_expr_void (block);
18806 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18807 add_decl_expr (omp_priv);
18808 add_decl_expr (omp_orig);
18809 finish_expr_stmt (block);
18810 if (i == 7)
18811 add_decl_expr (omp_orig);
18812 }
18813 }
18814
18815 /* T is a postfix-expression that is not being used in a function
18816 call. Return the substituted version of T. */
18817
18818 static tree
18819 tsubst_non_call_postfix_expression (tree t, tree args,
18820 tsubst_flags_t complain,
18821 tree in_decl)
18822 {
18823 if (TREE_CODE (t) == SCOPE_REF)
18824 t = tsubst_qualified_id (t, args, complain, in_decl,
18825 /*done=*/false, /*address_p=*/false);
18826 else
18827 t = tsubst_copy_and_build (t, args, complain, in_decl,
18828 /*function_p=*/false,
18829 /*integral_constant_expression_p=*/false);
18830
18831 return t;
18832 }
18833
18834 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18835 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18836 dependent init-capture. */
18837
18838 static void
18839 prepend_one_capture (tree field, tree init, tree &list,
18840 tsubst_flags_t complain)
18841 {
18842 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18843 {
18844 tree type = NULL_TREE;
18845 if (!init)
18846 {
18847 if (complain & tf_error)
18848 error ("empty initializer in lambda init-capture");
18849 init = error_mark_node;
18850 }
18851 else if (TREE_CODE (init) == TREE_LIST)
18852 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18853 if (!type)
18854 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18855 TREE_TYPE (field) = type;
18856 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18857 }
18858 list = tree_cons (field, init, list);
18859 }
18860
18861 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18862 instantiation context. Instantiating a pack expansion containing a lambda
18863 might result in multiple lambdas all based on the same lambda in the
18864 template. */
18865
18866 tree
18867 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18868 {
18869 tree oldfn = lambda_function (t);
18870 in_decl = oldfn;
18871
18872 tree r = build_lambda_expr ();
18873
18874 LAMBDA_EXPR_LOCATION (r)
18875 = LAMBDA_EXPR_LOCATION (t);
18876 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18877 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18878 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18879 LAMBDA_EXPR_INSTANTIATED (r) = true;
18880
18881 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18882 /* A lambda in a default argument outside a class gets no
18883 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18884 tsubst_default_argument calls start_lambda_scope, so we need to
18885 specifically ignore it here, and use the global scope. */
18886 record_null_lambda_scope (r);
18887 else
18888 record_lambda_scope (r);
18889
18890 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18891 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18892
18893 vec<tree,va_gc>* field_packs = NULL;
18894
18895 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18896 cap = TREE_CHAIN (cap))
18897 {
18898 tree ofield = TREE_PURPOSE (cap);
18899 tree init = TREE_VALUE (cap);
18900 if (PACK_EXPANSION_P (init))
18901 init = tsubst_pack_expansion (init, args, complain, in_decl);
18902 else
18903 init = tsubst_copy_and_build (init, args, complain, in_decl,
18904 /*fn*/false, /*constexpr*/false);
18905
18906 if (init == error_mark_node)
18907 return error_mark_node;
18908
18909 if (init && TREE_CODE (init) == TREE_LIST)
18910 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18911
18912 if (!processing_template_decl
18913 && init && TREE_CODE (init) != TREE_VEC
18914 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
18915 {
18916 /* For a VLA, simply tsubsting the field type won't work, we need to
18917 go through add_capture again. XXX do we want to do this for all
18918 captures? */
18919 tree name = (get_identifier
18920 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
18921 tree ftype = TREE_TYPE (ofield);
18922 bool by_ref = (TYPE_REF_P (ftype)
18923 || (TREE_CODE (ftype) == DECLTYPE_TYPE
18924 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
18925 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
18926 continue;
18927 }
18928
18929 if (PACK_EXPANSION_P (ofield))
18930 ofield = PACK_EXPANSION_PATTERN (ofield);
18931 tree field = tsubst_decl (ofield, args, complain);
18932
18933 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18934 {
18935 /* Remember these for when we've pushed local_specializations. */
18936 vec_safe_push (field_packs, ofield);
18937 vec_safe_push (field_packs, field);
18938 }
18939
18940 if (field == error_mark_node)
18941 return error_mark_node;
18942
18943 if (TREE_CODE (field) == TREE_VEC)
18944 {
18945 int len = TREE_VEC_LENGTH (field);
18946 gcc_assert (TREE_CODE (init) == TREE_VEC
18947 && TREE_VEC_LENGTH (init) == len);
18948 for (int i = 0; i < len; ++i)
18949 prepend_one_capture (TREE_VEC_ELT (field, i),
18950 TREE_VEC_ELT (init, i),
18951 LAMBDA_EXPR_CAPTURE_LIST (r),
18952 complain);
18953 }
18954 else
18955 {
18956 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
18957 complain);
18958
18959 if (id_equal (DECL_NAME (field), "__this"))
18960 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18961 }
18962 }
18963
18964 tree type = begin_lambda_type (r);
18965 if (type == error_mark_node)
18966 return error_mark_node;
18967
18968 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18969 determine_visibility (TYPE_NAME (type));
18970
18971 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18972
18973 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18974 ? DECL_TI_TEMPLATE (oldfn)
18975 : NULL_TREE);
18976
18977 tree fntype = static_fn_type (oldfn);
18978 if (oldtmpl)
18979 ++processing_template_decl;
18980 fntype = tsubst (fntype, args, complain, in_decl);
18981 if (oldtmpl)
18982 --processing_template_decl;
18983
18984 if (fntype == error_mark_node)
18985 r = error_mark_node;
18986 else
18987 {
18988 /* The body of a lambda-expression is not a subexpression of the
18989 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18990 which would be skipped if cp_unevaluated_operand. */
18991 cp_evaluated ev;
18992
18993 /* Fix the type of 'this'. */
18994 fntype = build_memfn_type (fntype, type,
18995 type_memfn_quals (fntype),
18996 type_memfn_rqual (fntype));
18997 tree fn, tmpl;
18998 if (oldtmpl)
18999 {
19000 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19001 if (tmpl == error_mark_node)
19002 {
19003 r = error_mark_node;
19004 goto out;
19005 }
19006 fn = DECL_TEMPLATE_RESULT (tmpl);
19007 finish_member_declaration (tmpl);
19008 }
19009 else
19010 {
19011 tmpl = NULL_TREE;
19012 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19013 if (fn == error_mark_node)
19014 {
19015 r = error_mark_node;
19016 goto out;
19017 }
19018 finish_member_declaration (fn);
19019 }
19020
19021 /* Let finish_function set this. */
19022 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19023
19024 bool nested = cfun;
19025 if (nested)
19026 push_function_context ();
19027 else
19028 /* Still increment function_depth so that we don't GC in the
19029 middle of an expression. */
19030 ++function_depth;
19031
19032 local_specialization_stack s (lss_copy);
19033
19034 tree body = start_lambda_function (fn, r);
19035
19036 /* Now record them for lookup_init_capture_pack. */
19037 int fplen = vec_safe_length (field_packs);
19038 for (int i = 0; i < fplen; )
19039 {
19040 tree pack = (*field_packs)[i++];
19041 tree inst = (*field_packs)[i++];
19042 register_local_specialization (inst, pack);
19043 }
19044 release_tree_vector (field_packs);
19045
19046 register_parameter_specializations (oldfn, fn);
19047
19048 if (oldtmpl)
19049 {
19050 /* We might not partially instantiate some parts of the function, so
19051 copy these flags from the original template. */
19052 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19053 current_function_returns_value = ol->returns_value;
19054 current_function_returns_null = ol->returns_null;
19055 current_function_returns_abnormally = ol->returns_abnormally;
19056 current_function_infinite_loop = ol->infinite_loop;
19057 }
19058
19059 /* [temp.deduct] A lambda-expression appearing in a function type or a
19060 template parameter is not considered part of the immediate context for
19061 the purposes of template argument deduction. */
19062 complain = tf_warning_or_error;
19063
19064 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
19065 /*constexpr*/false);
19066
19067 finish_lambda_function (body);
19068
19069 if (nested)
19070 pop_function_context ();
19071 else
19072 --function_depth;
19073
19074 /* The capture list was built up in reverse order; fix that now. */
19075 LAMBDA_EXPR_CAPTURE_LIST (r)
19076 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19077
19078 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19079
19080 maybe_add_lambda_conv_op (type);
19081 }
19082
19083 out:
19084 finish_struct (type, /*attr*/NULL_TREE);
19085
19086 insert_pending_capture_proxies ();
19087
19088 return r;
19089 }
19090
19091 /* Like tsubst but deals with expressions and performs semantic
19092 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
19093
19094 tree
19095 tsubst_copy_and_build (tree t,
19096 tree args,
19097 tsubst_flags_t complain,
19098 tree in_decl,
19099 bool function_p,
19100 bool integral_constant_expression_p)
19101 {
19102 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19103 #define RECUR(NODE) \
19104 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19105 /*function_p=*/false, \
19106 integral_constant_expression_p)
19107
19108 tree retval, op1;
19109 location_t save_loc;
19110
19111 if (t == NULL_TREE || t == error_mark_node)
19112 return t;
19113
19114 save_loc = input_location;
19115 if (location_t eloc = cp_expr_location (t))
19116 input_location = eloc;
19117
19118 /* N3276 decltype magic only applies to calls at the top level or on the
19119 right side of a comma. */
19120 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19121 complain &= ~tf_decltype;
19122
19123 switch (TREE_CODE (t))
19124 {
19125 case USING_DECL:
19126 t = DECL_NAME (t);
19127 /* Fall through. */
19128 case IDENTIFIER_NODE:
19129 {
19130 tree decl;
19131 cp_id_kind idk;
19132 bool non_integral_constant_expression_p;
19133 const char *error_msg;
19134
19135 if (IDENTIFIER_CONV_OP_P (t))
19136 {
19137 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19138 t = make_conv_op_name (new_type);
19139 }
19140
19141 /* Look up the name. */
19142 decl = lookup_name (t);
19143
19144 /* By convention, expressions use ERROR_MARK_NODE to indicate
19145 failure, not NULL_TREE. */
19146 if (decl == NULL_TREE)
19147 decl = error_mark_node;
19148
19149 decl = finish_id_expression (t, decl, NULL_TREE,
19150 &idk,
19151 integral_constant_expression_p,
19152 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19153 &non_integral_constant_expression_p,
19154 /*template_p=*/false,
19155 /*done=*/true,
19156 /*address_p=*/false,
19157 /*template_arg_p=*/false,
19158 &error_msg,
19159 input_location);
19160 if (error_msg)
19161 error (error_msg);
19162 if (!function_p && identifier_p (decl))
19163 {
19164 if (complain & tf_error)
19165 unqualified_name_lookup_error (decl);
19166 decl = error_mark_node;
19167 }
19168 RETURN (decl);
19169 }
19170
19171 case TEMPLATE_ID_EXPR:
19172 {
19173 tree object;
19174 tree templ = RECUR (TREE_OPERAND (t, 0));
19175 tree targs = TREE_OPERAND (t, 1);
19176
19177 if (targs)
19178 targs = tsubst_template_args (targs, args, complain, in_decl);
19179 if (targs == error_mark_node)
19180 RETURN (error_mark_node);
19181
19182 if (TREE_CODE (templ) == SCOPE_REF)
19183 {
19184 tree name = TREE_OPERAND (templ, 1);
19185 tree tid = lookup_template_function (name, targs);
19186 TREE_OPERAND (templ, 1) = tid;
19187 RETURN (templ);
19188 }
19189
19190 if (concept_definition_p (templ))
19191 {
19192 tree check = build_concept_check (templ, targs, complain);
19193 if (check == error_mark_node)
19194 RETURN (error_mark_node);
19195
19196 tree id = unpack_concept_check (check);
19197
19198 /* If we built a function concept check, return the underlying
19199 template-id. So we can evaluate it as a function call. */
19200 if (function_concept_p (TREE_OPERAND (id, 0)))
19201 RETURN (id);
19202
19203 RETURN (check);
19204 }
19205
19206 if (variable_template_p (templ))
19207 {
19208 tree r = lookup_and_finish_template_variable (templ, targs,
19209 complain);
19210 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19211 RETURN (r);
19212 }
19213
19214 if (TREE_CODE (templ) == COMPONENT_REF)
19215 {
19216 object = TREE_OPERAND (templ, 0);
19217 templ = TREE_OPERAND (templ, 1);
19218 }
19219 else
19220 object = NULL_TREE;
19221 templ = lookup_template_function (templ, targs);
19222
19223 if (object)
19224 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
19225 object, templ, NULL_TREE));
19226 else
19227 RETURN (baselink_for_fns (templ));
19228 }
19229
19230 case INDIRECT_REF:
19231 {
19232 tree r = RECUR (TREE_OPERAND (t, 0));
19233
19234 if (REFERENCE_REF_P (t))
19235 {
19236 /* A type conversion to reference type will be enclosed in
19237 such an indirect ref, but the substitution of the cast
19238 will have also added such an indirect ref. */
19239 r = convert_from_reference (r);
19240 }
19241 else
19242 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19243 complain|decltype_flag);
19244
19245 if (REF_PARENTHESIZED_P (t))
19246 r = force_paren_expr (r);
19247
19248 RETURN (r);
19249 }
19250
19251 case NOP_EXPR:
19252 {
19253 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19254 tree op0 = RECUR (TREE_OPERAND (t, 0));
19255 RETURN (build_nop (type, op0));
19256 }
19257
19258 case IMPLICIT_CONV_EXPR:
19259 {
19260 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19261 tree expr = RECUR (TREE_OPERAND (t, 0));
19262 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19263 {
19264 retval = copy_node (t);
19265 TREE_TYPE (retval) = type;
19266 TREE_OPERAND (retval, 0) = expr;
19267 RETURN (retval);
19268 }
19269 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19270 /* We'll pass this to convert_nontype_argument again, we don't need
19271 to actually perform any conversion here. */
19272 RETURN (expr);
19273 int flags = LOOKUP_IMPLICIT;
19274 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19275 flags = LOOKUP_NORMAL;
19276 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19277 flags |= LOOKUP_NO_NARROWING;
19278 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19279 flags));
19280 }
19281
19282 case CONVERT_EXPR:
19283 {
19284 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19285 tree op0 = RECUR (TREE_OPERAND (t, 0));
19286 if (op0 == error_mark_node)
19287 RETURN (error_mark_node);
19288 RETURN (build1 (CONVERT_EXPR, type, op0));
19289 }
19290
19291 case CAST_EXPR:
19292 case REINTERPRET_CAST_EXPR:
19293 case CONST_CAST_EXPR:
19294 case DYNAMIC_CAST_EXPR:
19295 case STATIC_CAST_EXPR:
19296 {
19297 tree type;
19298 tree op, r = NULL_TREE;
19299
19300 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19301 if (integral_constant_expression_p
19302 && !cast_valid_in_integral_constant_expression_p (type))
19303 {
19304 if (complain & tf_error)
19305 error ("a cast to a type other than an integral or "
19306 "enumeration type cannot appear in a constant-expression");
19307 RETURN (error_mark_node);
19308 }
19309
19310 op = RECUR (TREE_OPERAND (t, 0));
19311
19312 warning_sentinel s(warn_useless_cast);
19313 warning_sentinel s2(warn_ignored_qualifiers);
19314 switch (TREE_CODE (t))
19315 {
19316 case CAST_EXPR:
19317 r = build_functional_cast (input_location, type, op, complain);
19318 break;
19319 case REINTERPRET_CAST_EXPR:
19320 r = build_reinterpret_cast (input_location, type, op, complain);
19321 break;
19322 case CONST_CAST_EXPR:
19323 r = build_const_cast (input_location, type, op, complain);
19324 break;
19325 case DYNAMIC_CAST_EXPR:
19326 r = build_dynamic_cast (input_location, type, op, complain);
19327 break;
19328 case STATIC_CAST_EXPR:
19329 r = build_static_cast (input_location, type, op, complain);
19330 break;
19331 default:
19332 gcc_unreachable ();
19333 }
19334
19335 RETURN (r);
19336 }
19337
19338 case POSTDECREMENT_EXPR:
19339 case POSTINCREMENT_EXPR:
19340 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19341 args, complain, in_decl);
19342 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19343 complain|decltype_flag));
19344
19345 case PREDECREMENT_EXPR:
19346 case PREINCREMENT_EXPR:
19347 case NEGATE_EXPR:
19348 case BIT_NOT_EXPR:
19349 case ABS_EXPR:
19350 case TRUTH_NOT_EXPR:
19351 case UNARY_PLUS_EXPR: /* Unary + */
19352 case REALPART_EXPR:
19353 case IMAGPART_EXPR:
19354 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19355 RECUR (TREE_OPERAND (t, 0)),
19356 complain|decltype_flag));
19357
19358 case FIX_TRUNC_EXPR:
19359 gcc_unreachable ();
19360
19361 case ADDR_EXPR:
19362 op1 = TREE_OPERAND (t, 0);
19363 if (TREE_CODE (op1) == LABEL_DECL)
19364 RETURN (finish_label_address_expr (DECL_NAME (op1),
19365 EXPR_LOCATION (op1)));
19366 if (TREE_CODE (op1) == SCOPE_REF)
19367 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19368 /*done=*/true, /*address_p=*/true);
19369 else
19370 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19371 in_decl);
19372 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19373 complain|decltype_flag));
19374
19375 case PLUS_EXPR:
19376 case MINUS_EXPR:
19377 case MULT_EXPR:
19378 case TRUNC_DIV_EXPR:
19379 case CEIL_DIV_EXPR:
19380 case FLOOR_DIV_EXPR:
19381 case ROUND_DIV_EXPR:
19382 case EXACT_DIV_EXPR:
19383 case BIT_AND_EXPR:
19384 case BIT_IOR_EXPR:
19385 case BIT_XOR_EXPR:
19386 case TRUNC_MOD_EXPR:
19387 case FLOOR_MOD_EXPR:
19388 case TRUTH_ANDIF_EXPR:
19389 case TRUTH_ORIF_EXPR:
19390 case TRUTH_AND_EXPR:
19391 case TRUTH_OR_EXPR:
19392 case RSHIFT_EXPR:
19393 case LSHIFT_EXPR:
19394 case EQ_EXPR:
19395 case NE_EXPR:
19396 case MAX_EXPR:
19397 case MIN_EXPR:
19398 case LE_EXPR:
19399 case GE_EXPR:
19400 case LT_EXPR:
19401 case GT_EXPR:
19402 case SPACESHIP_EXPR:
19403 case MEMBER_REF:
19404 case DOTSTAR_EXPR:
19405 {
19406 /* If T was type-dependent, suppress warnings that depend on the range
19407 of the types involved. */
19408 bool was_dep = type_dependent_expression_p_push (t);
19409
19410 tree op0 = RECUR (TREE_OPERAND (t, 0));
19411 tree op1 = RECUR (TREE_OPERAND (t, 1));
19412
19413 warning_sentinel s1(warn_type_limits, was_dep);
19414 warning_sentinel s2(warn_div_by_zero, was_dep);
19415 warning_sentinel s3(warn_logical_op, was_dep);
19416 warning_sentinel s4(warn_tautological_compare, was_dep);
19417
19418 tree r = build_x_binary_op
19419 (input_location, TREE_CODE (t),
19420 op0,
19421 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19422 ? ERROR_MARK
19423 : TREE_CODE (TREE_OPERAND (t, 0))),
19424 op1,
19425 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19426 ? ERROR_MARK
19427 : TREE_CODE (TREE_OPERAND (t, 1))),
19428 /*overload=*/NULL,
19429 complain|decltype_flag);
19430 if (EXPR_P (r) && TREE_NO_WARNING (t))
19431 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19432
19433 RETURN (r);
19434 }
19435
19436 case POINTER_PLUS_EXPR:
19437 {
19438 tree op0 = RECUR (TREE_OPERAND (t, 0));
19439 if (op0 == error_mark_node)
19440 RETURN (error_mark_node);
19441 tree op1 = RECUR (TREE_OPERAND (t, 1));
19442 if (op1 == error_mark_node)
19443 RETURN (error_mark_node);
19444 RETURN (fold_build_pointer_plus (op0, op1));
19445 }
19446
19447 case SCOPE_REF:
19448 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19449 /*address_p=*/false));
19450 case ARRAY_REF:
19451 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19452 args, complain, in_decl);
19453 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19454 RECUR (TREE_OPERAND (t, 1)),
19455 complain|decltype_flag));
19456
19457 case SIZEOF_EXPR:
19458 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19459 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19460 RETURN (tsubst_copy (t, args, complain, in_decl));
19461 /* Fall through */
19462
19463 case ALIGNOF_EXPR:
19464 {
19465 tree r;
19466
19467 op1 = TREE_OPERAND (t, 0);
19468 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19469 op1 = TREE_TYPE (op1);
19470 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19471 && ALIGNOF_EXPR_STD_P (t));
19472 if (!args)
19473 {
19474 /* When there are no ARGS, we are trying to evaluate a
19475 non-dependent expression from the parser. Trying to do
19476 the substitutions may not work. */
19477 if (!TYPE_P (op1))
19478 op1 = TREE_TYPE (op1);
19479 }
19480 else
19481 {
19482 ++cp_unevaluated_operand;
19483 ++c_inhibit_evaluation_warnings;
19484 if (TYPE_P (op1))
19485 op1 = tsubst (op1, args, complain, in_decl);
19486 else
19487 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19488 /*function_p=*/false,
19489 /*integral_constant_expression_p=*/
19490 false);
19491 --cp_unevaluated_operand;
19492 --c_inhibit_evaluation_warnings;
19493 }
19494 if (TYPE_P (op1))
19495 r = cxx_sizeof_or_alignof_type (input_location,
19496 op1, TREE_CODE (t), std_alignof,
19497 complain & tf_error);
19498 else
19499 r = cxx_sizeof_or_alignof_expr (input_location,
19500 op1, TREE_CODE (t),
19501 complain & tf_error);
19502 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19503 {
19504 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19505 {
19506 if (!processing_template_decl && TYPE_P (op1))
19507 {
19508 r = build_min (SIZEOF_EXPR, size_type_node,
19509 build1 (NOP_EXPR, op1, error_mark_node));
19510 SIZEOF_EXPR_TYPE_P (r) = 1;
19511 }
19512 else
19513 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19514 TREE_SIDE_EFFECTS (r) = 0;
19515 TREE_READONLY (r) = 1;
19516 }
19517 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19518 }
19519 RETURN (r);
19520 }
19521
19522 case AT_ENCODE_EXPR:
19523 {
19524 op1 = TREE_OPERAND (t, 0);
19525 ++cp_unevaluated_operand;
19526 ++c_inhibit_evaluation_warnings;
19527 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19528 /*function_p=*/false,
19529 /*integral_constant_expression_p=*/false);
19530 --cp_unevaluated_operand;
19531 --c_inhibit_evaluation_warnings;
19532 RETURN (objc_build_encode_expr (op1));
19533 }
19534
19535 case NOEXCEPT_EXPR:
19536 op1 = TREE_OPERAND (t, 0);
19537 ++cp_unevaluated_operand;
19538 ++c_inhibit_evaluation_warnings;
19539 ++cp_noexcept_operand;
19540 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19541 /*function_p=*/false,
19542 /*integral_constant_expression_p=*/false);
19543 --cp_unevaluated_operand;
19544 --c_inhibit_evaluation_warnings;
19545 --cp_noexcept_operand;
19546 RETURN (finish_noexcept_expr (op1, complain));
19547
19548 case MODOP_EXPR:
19549 {
19550 warning_sentinel s(warn_div_by_zero);
19551 tree lhs = RECUR (TREE_OPERAND (t, 0));
19552 tree rhs = RECUR (TREE_OPERAND (t, 2));
19553 tree r = build_x_modify_expr
19554 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19555 complain|decltype_flag);
19556 /* TREE_NO_WARNING must be set if either the expression was
19557 parenthesized or it uses an operator such as >>= rather
19558 than plain assignment. In the former case, it was already
19559 set and must be copied. In the latter case,
19560 build_x_modify_expr sets it and it must not be reset
19561 here. */
19562 if (TREE_NO_WARNING (t))
19563 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19564
19565 RETURN (r);
19566 }
19567
19568 case ARROW_EXPR:
19569 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19570 args, complain, in_decl);
19571 /* Remember that there was a reference to this entity. */
19572 if (DECL_P (op1)
19573 && !mark_used (op1, complain) && !(complain & tf_error))
19574 RETURN (error_mark_node);
19575 RETURN (build_x_arrow (input_location, op1, complain));
19576
19577 case NEW_EXPR:
19578 {
19579 tree placement = RECUR (TREE_OPERAND (t, 0));
19580 tree init = RECUR (TREE_OPERAND (t, 3));
19581 vec<tree, va_gc> *placement_vec;
19582 vec<tree, va_gc> *init_vec;
19583 tree ret;
19584 location_t loc = EXPR_LOCATION (t);
19585
19586 if (placement == NULL_TREE)
19587 placement_vec = NULL;
19588 else
19589 {
19590 placement_vec = make_tree_vector ();
19591 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19592 vec_safe_push (placement_vec, TREE_VALUE (placement));
19593 }
19594
19595 /* If there was an initializer in the original tree, but it
19596 instantiated to an empty list, then we should pass a
19597 non-NULL empty vector to tell build_new that it was an
19598 empty initializer() rather than no initializer. This can
19599 only happen when the initializer is a pack expansion whose
19600 parameter packs are of length zero. */
19601 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19602 init_vec = NULL;
19603 else
19604 {
19605 init_vec = make_tree_vector ();
19606 if (init == void_node)
19607 gcc_assert (init_vec != NULL);
19608 else
19609 {
19610 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19611 vec_safe_push (init_vec, TREE_VALUE (init));
19612 }
19613 }
19614
19615 /* Avoid passing an enclosing decl to valid_array_size_p. */
19616 in_decl = NULL_TREE;
19617
19618 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19619 tree op2 = RECUR (TREE_OPERAND (t, 2));
19620 ret = build_new (loc, &placement_vec, op1, op2,
19621 &init_vec, NEW_EXPR_USE_GLOBAL (t),
19622 complain);
19623
19624 if (placement_vec != NULL)
19625 release_tree_vector (placement_vec);
19626 if (init_vec != NULL)
19627 release_tree_vector (init_vec);
19628
19629 RETURN (ret);
19630 }
19631
19632 case DELETE_EXPR:
19633 {
19634 tree op0 = RECUR (TREE_OPERAND (t, 0));
19635 tree op1 = RECUR (TREE_OPERAND (t, 1));
19636 RETURN (delete_sanity (input_location, op0, op1,
19637 DELETE_EXPR_USE_VEC (t),
19638 DELETE_EXPR_USE_GLOBAL (t),
19639 complain));
19640 }
19641
19642 case COMPOUND_EXPR:
19643 {
19644 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19645 complain & ~tf_decltype, in_decl,
19646 /*function_p=*/false,
19647 integral_constant_expression_p);
19648 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19649 op0,
19650 RECUR (TREE_OPERAND (t, 1)),
19651 complain|decltype_flag));
19652 }
19653
19654 case CALL_EXPR:
19655 {
19656 tree function;
19657 unsigned int nargs, i;
19658 bool qualified_p;
19659 bool koenig_p;
19660 tree ret;
19661
19662 function = CALL_EXPR_FN (t);
19663 /* Internal function with no arguments. */
19664 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19665 RETURN (t);
19666
19667 /* When we parsed the expression, we determined whether or
19668 not Koenig lookup should be performed. */
19669 koenig_p = KOENIG_LOOKUP_P (t);
19670 if (function == NULL_TREE)
19671 {
19672 koenig_p = false;
19673 qualified_p = false;
19674 }
19675 else if (TREE_CODE (function) == SCOPE_REF)
19676 {
19677 qualified_p = true;
19678 function = tsubst_qualified_id (function, args, complain, in_decl,
19679 /*done=*/false,
19680 /*address_p=*/false);
19681 }
19682 else if (koenig_p && identifier_p (function))
19683 {
19684 /* Do nothing; calling tsubst_copy_and_build on an identifier
19685 would incorrectly perform unqualified lookup again.
19686
19687 Note that we can also have an IDENTIFIER_NODE if the earlier
19688 unqualified lookup found a member function; in that case
19689 koenig_p will be false and we do want to do the lookup
19690 again to find the instantiated member function.
19691
19692 FIXME but doing that causes c++/15272, so we need to stop
19693 using IDENTIFIER_NODE in that situation. */
19694 qualified_p = false;
19695 }
19696 else
19697 {
19698 if (TREE_CODE (function) == COMPONENT_REF)
19699 {
19700 tree op = TREE_OPERAND (function, 1);
19701
19702 qualified_p = (TREE_CODE (op) == SCOPE_REF
19703 || (BASELINK_P (op)
19704 && BASELINK_QUALIFIED_P (op)));
19705 }
19706 else
19707 qualified_p = false;
19708
19709 if (TREE_CODE (function) == ADDR_EXPR
19710 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19711 /* Avoid error about taking the address of a constructor. */
19712 function = TREE_OPERAND (function, 0);
19713
19714 function = tsubst_copy_and_build (function, args, complain,
19715 in_decl,
19716 !qualified_p,
19717 integral_constant_expression_p);
19718
19719 if (BASELINK_P (function))
19720 qualified_p = true;
19721 }
19722
19723 nargs = call_expr_nargs (t);
19724 releasing_vec call_args;
19725 for (i = 0; i < nargs; ++i)
19726 {
19727 tree arg = CALL_EXPR_ARG (t, i);
19728
19729 if (!PACK_EXPANSION_P (arg))
19730 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19731 else
19732 {
19733 /* Expand the pack expansion and push each entry onto
19734 CALL_ARGS. */
19735 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19736 if (TREE_CODE (arg) == TREE_VEC)
19737 {
19738 unsigned int len, j;
19739
19740 len = TREE_VEC_LENGTH (arg);
19741 for (j = 0; j < len; ++j)
19742 {
19743 tree value = TREE_VEC_ELT (arg, j);
19744 if (value != NULL_TREE)
19745 value = convert_from_reference (value);
19746 vec_safe_push (call_args, value);
19747 }
19748 }
19749 else
19750 {
19751 /* A partial substitution. Add one entry. */
19752 vec_safe_push (call_args, arg);
19753 }
19754 }
19755 }
19756
19757 /* Stripped-down processing for a call in a thunk. Specifically, in
19758 the thunk template for a generic lambda. */
19759 if (CALL_FROM_THUNK_P (t))
19760 {
19761 /* Now that we've expanded any packs, the number of call args
19762 might be different. */
19763 unsigned int cargs = call_args->length ();
19764 tree thisarg = NULL_TREE;
19765 if (TREE_CODE (function) == COMPONENT_REF)
19766 {
19767 thisarg = TREE_OPERAND (function, 0);
19768 if (TREE_CODE (thisarg) == INDIRECT_REF)
19769 thisarg = TREE_OPERAND (thisarg, 0);
19770 function = TREE_OPERAND (function, 1);
19771 if (TREE_CODE (function) == BASELINK)
19772 function = BASELINK_FUNCTIONS (function);
19773 }
19774 /* We aren't going to do normal overload resolution, so force the
19775 template-id to resolve. */
19776 function = resolve_nondeduced_context (function, complain);
19777 for (unsigned i = 0; i < cargs; ++i)
19778 {
19779 /* In a thunk, pass through args directly, without any
19780 conversions. */
19781 tree arg = (*call_args)[i];
19782 while (TREE_CODE (arg) != PARM_DECL)
19783 arg = TREE_OPERAND (arg, 0);
19784 (*call_args)[i] = arg;
19785 }
19786 if (thisarg)
19787 {
19788 /* If there are no other args, just push 'this'. */
19789 if (cargs == 0)
19790 vec_safe_push (call_args, thisarg);
19791 else
19792 {
19793 /* Otherwise, shift the other args over to make room. */
19794 tree last = (*call_args)[cargs - 1];
19795 vec_safe_push (call_args, last);
19796 for (int i = cargs - 1; i > 0; --i)
19797 (*call_args)[i] = (*call_args)[i - 1];
19798 (*call_args)[0] = thisarg;
19799 }
19800 }
19801 ret = build_call_a (function, call_args->length (),
19802 call_args->address ());
19803 /* The thunk location is not interesting. */
19804 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19805 CALL_FROM_THUNK_P (ret) = true;
19806 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19807 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19808
19809 RETURN (ret);
19810 }
19811
19812 /* We do not perform argument-dependent lookup if normal
19813 lookup finds a non-function, in accordance with the
19814 expected resolution of DR 218. */
19815 if (koenig_p
19816 && ((is_overloaded_fn (function)
19817 /* If lookup found a member function, the Koenig lookup is
19818 not appropriate, even if an unqualified-name was used
19819 to denote the function. */
19820 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19821 || identifier_p (function))
19822 /* Only do this when substitution turns a dependent call
19823 into a non-dependent call. */
19824 && type_dependent_expression_p_push (t)
19825 && !any_type_dependent_arguments_p (call_args))
19826 function = perform_koenig_lookup (function, call_args, tf_none);
19827
19828 if (function != NULL_TREE
19829 && identifier_p (function)
19830 && !any_type_dependent_arguments_p (call_args))
19831 {
19832 if (koenig_p && (complain & tf_warning_or_error))
19833 {
19834 /* For backwards compatibility and good diagnostics, try
19835 the unqualified lookup again if we aren't in SFINAE
19836 context. */
19837 tree unq = (tsubst_copy_and_build
19838 (function, args, complain, in_decl, true,
19839 integral_constant_expression_p));
19840 if (unq == error_mark_node)
19841 RETURN (error_mark_node);
19842
19843 if (unq != function)
19844 {
19845 /* In a lambda fn, we have to be careful to not
19846 introduce new this captures. Legacy code can't
19847 be using lambdas anyway, so it's ok to be
19848 stricter. */
19849 bool in_lambda = (current_class_type
19850 && LAMBDA_TYPE_P (current_class_type));
19851 char const *const msg
19852 = G_("%qD was not declared in this scope, "
19853 "and no declarations were found by "
19854 "argument-dependent lookup at the point "
19855 "of instantiation");
19856
19857 bool diag = true;
19858 if (in_lambda)
19859 error_at (cp_expr_loc_or_input_loc (t),
19860 msg, function);
19861 else
19862 diag = permerror (cp_expr_loc_or_input_loc (t),
19863 msg, function);
19864 if (diag)
19865 {
19866 tree fn = unq;
19867
19868 if (INDIRECT_REF_P (fn))
19869 fn = TREE_OPERAND (fn, 0);
19870 if (is_overloaded_fn (fn))
19871 fn = get_first_fn (fn);
19872
19873 if (!DECL_P (fn))
19874 /* Can't say anything more. */;
19875 else if (DECL_CLASS_SCOPE_P (fn))
19876 {
19877 location_t loc = cp_expr_loc_or_input_loc (t);
19878 inform (loc,
19879 "declarations in dependent base %qT are "
19880 "not found by unqualified lookup",
19881 DECL_CLASS_CONTEXT (fn));
19882 if (current_class_ptr)
19883 inform (loc,
19884 "use %<this->%D%> instead", function);
19885 else
19886 inform (loc,
19887 "use %<%T::%D%> instead",
19888 current_class_name, function);
19889 }
19890 else
19891 inform (DECL_SOURCE_LOCATION (fn),
19892 "%qD declared here, later in the "
19893 "translation unit", fn);
19894 if (in_lambda)
19895 RETURN (error_mark_node);
19896 }
19897
19898 function = unq;
19899 }
19900 }
19901 if (identifier_p (function))
19902 {
19903 if (complain & tf_error)
19904 unqualified_name_lookup_error (function);
19905 RETURN (error_mark_node);
19906 }
19907 }
19908
19909 /* Remember that there was a reference to this entity. */
19910 if (function != NULL_TREE
19911 && DECL_P (function)
19912 && !mark_used (function, complain) && !(complain & tf_error))
19913 RETURN (error_mark_node);
19914
19915 /* Put back tf_decltype for the actual call. */
19916 complain |= decltype_flag;
19917
19918 if (function == NULL_TREE)
19919 switch (CALL_EXPR_IFN (t))
19920 {
19921 case IFN_LAUNDER:
19922 gcc_assert (nargs == 1);
19923 if (vec_safe_length (call_args) != 1)
19924 {
19925 error_at (cp_expr_loc_or_input_loc (t),
19926 "wrong number of arguments to "
19927 "%<__builtin_launder%>");
19928 ret = error_mark_node;
19929 }
19930 else
19931 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
19932 (*call_args)[0], complain);
19933 break;
19934
19935 case IFN_VEC_CONVERT:
19936 gcc_assert (nargs == 1);
19937 if (vec_safe_length (call_args) != 1)
19938 {
19939 error_at (cp_expr_loc_or_input_loc (t),
19940 "wrong number of arguments to "
19941 "%<__builtin_convertvector%>");
19942 ret = error_mark_node;
19943 break;
19944 }
19945 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19946 tsubst (TREE_TYPE (t), args,
19947 complain, in_decl),
19948 complain);
19949 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19950 RETURN (ret);
19951 break;
19952
19953 default:
19954 /* Unsupported internal function with arguments. */
19955 gcc_unreachable ();
19956 }
19957 else if (TREE_CODE (function) == OFFSET_REF
19958 || TREE_CODE (function) == DOTSTAR_EXPR
19959 || TREE_CODE (function) == MEMBER_REF)
19960 ret = build_offset_ref_call_from_tree (function, &call_args,
19961 complain);
19962 else if (TREE_CODE (function) == COMPONENT_REF)
19963 {
19964 tree instance = TREE_OPERAND (function, 0);
19965 tree fn = TREE_OPERAND (function, 1);
19966
19967 if (processing_template_decl
19968 && (type_dependent_expression_p (instance)
19969 || (!BASELINK_P (fn)
19970 && TREE_CODE (fn) != FIELD_DECL)
19971 || type_dependent_expression_p (fn)
19972 || any_type_dependent_arguments_p (call_args)))
19973 ret = build_min_nt_call_vec (function, call_args);
19974 else if (!BASELINK_P (fn))
19975 ret = finish_call_expr (function, &call_args,
19976 /*disallow_virtual=*/false,
19977 /*koenig_p=*/false,
19978 complain);
19979 else
19980 ret = (build_new_method_call
19981 (instance, fn,
19982 &call_args, NULL_TREE,
19983 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19984 /*fn_p=*/NULL,
19985 complain));
19986 }
19987 else if (concept_check_p (function))
19988 {
19989 /* FUNCTION is a template-id referring to a concept definition. */
19990 tree id = unpack_concept_check (function);
19991 tree tmpl = TREE_OPERAND (id, 0);
19992 tree args = TREE_OPERAND (id, 1);
19993
19994 /* Calls to standard and variable concepts should have been
19995 previously diagnosed. */
19996 gcc_assert (function_concept_p (tmpl));
19997
19998 /* Ensure the result is wrapped as a call expression. */
19999 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20000 }
20001 else
20002 ret = finish_call_expr (function, &call_args,
20003 /*disallow_virtual=*/qualified_p,
20004 koenig_p,
20005 complain);
20006
20007 if (ret != error_mark_node)
20008 {
20009 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20010 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20011 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20012 if (op || ord || rev)
20013 {
20014 function = extract_call_expr (ret);
20015 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20016 CALL_EXPR_ORDERED_ARGS (function) = ord;
20017 CALL_EXPR_REVERSE_ARGS (function) = rev;
20018 }
20019 }
20020
20021 RETURN (ret);
20022 }
20023
20024 case COND_EXPR:
20025 {
20026 tree cond = RECUR (TREE_OPERAND (t, 0));
20027 cond = mark_rvalue_use (cond);
20028 tree folded_cond = fold_non_dependent_expr (cond, complain);
20029 tree exp1, exp2;
20030
20031 if (TREE_CODE (folded_cond) == INTEGER_CST)
20032 {
20033 if (integer_zerop (folded_cond))
20034 {
20035 ++c_inhibit_evaluation_warnings;
20036 exp1 = RECUR (TREE_OPERAND (t, 1));
20037 --c_inhibit_evaluation_warnings;
20038 exp2 = RECUR (TREE_OPERAND (t, 2));
20039 }
20040 else
20041 {
20042 exp1 = RECUR (TREE_OPERAND (t, 1));
20043 ++c_inhibit_evaluation_warnings;
20044 exp2 = RECUR (TREE_OPERAND (t, 2));
20045 --c_inhibit_evaluation_warnings;
20046 }
20047 cond = folded_cond;
20048 }
20049 else
20050 {
20051 exp1 = RECUR (TREE_OPERAND (t, 1));
20052 exp2 = RECUR (TREE_OPERAND (t, 2));
20053 }
20054
20055 warning_sentinel s(warn_duplicated_branches);
20056 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20057 cond, exp1, exp2, complain));
20058 }
20059
20060 case PSEUDO_DTOR_EXPR:
20061 {
20062 tree op0 = RECUR (TREE_OPERAND (t, 0));
20063 tree op1 = RECUR (TREE_OPERAND (t, 1));
20064 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20065 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20066 input_location));
20067 }
20068
20069 case TREE_LIST:
20070 {
20071 tree purpose, value, chain;
20072
20073 if (t == void_list_node)
20074 RETURN (t);
20075
20076 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
20077 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
20078 {
20079 /* We have pack expansions, so expand those and
20080 create a new list out of it. */
20081 tree purposevec = NULL_TREE;
20082 tree valuevec = NULL_TREE;
20083 tree chain;
20084 int i, len = -1;
20085
20086 /* Expand the argument expressions. */
20087 if (TREE_PURPOSE (t))
20088 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
20089 complain, in_decl);
20090 if (TREE_VALUE (t))
20091 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
20092 complain, in_decl);
20093
20094 /* Build the rest of the list. */
20095 chain = TREE_CHAIN (t);
20096 if (chain && chain != void_type_node)
20097 chain = RECUR (chain);
20098
20099 /* Determine the number of arguments. */
20100 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
20101 {
20102 len = TREE_VEC_LENGTH (purposevec);
20103 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
20104 }
20105 else if (TREE_CODE (valuevec) == TREE_VEC)
20106 len = TREE_VEC_LENGTH (valuevec);
20107 else
20108 {
20109 /* Since we only performed a partial substitution into
20110 the argument pack, we only RETURN (a single list
20111 node. */
20112 if (purposevec == TREE_PURPOSE (t)
20113 && valuevec == TREE_VALUE (t)
20114 && chain == TREE_CHAIN (t))
20115 RETURN (t);
20116
20117 RETURN (tree_cons (purposevec, valuevec, chain));
20118 }
20119
20120 /* Convert the argument vectors into a TREE_LIST */
20121 i = len;
20122 while (i > 0)
20123 {
20124 /* Grab the Ith values. */
20125 i--;
20126 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
20127 : NULL_TREE;
20128 value
20129 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
20130 : NULL_TREE;
20131
20132 /* Build the list (backwards). */
20133 chain = tree_cons (purpose, value, chain);
20134 }
20135
20136 RETURN (chain);
20137 }
20138
20139 purpose = TREE_PURPOSE (t);
20140 if (purpose)
20141 purpose = RECUR (purpose);
20142 value = TREE_VALUE (t);
20143 if (value)
20144 value = RECUR (value);
20145 chain = TREE_CHAIN (t);
20146 if (chain && chain != void_type_node)
20147 chain = RECUR (chain);
20148 if (purpose == TREE_PURPOSE (t)
20149 && value == TREE_VALUE (t)
20150 && chain == TREE_CHAIN (t))
20151 RETURN (t);
20152 RETURN (tree_cons (purpose, value, chain));
20153 }
20154
20155 case COMPONENT_REF:
20156 {
20157 tree object;
20158 tree object_type;
20159 tree member;
20160 tree r;
20161
20162 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20163 args, complain, in_decl);
20164 /* Remember that there was a reference to this entity. */
20165 if (DECL_P (object)
20166 && !mark_used (object, complain) && !(complain & tf_error))
20167 RETURN (error_mark_node);
20168 object_type = TREE_TYPE (object);
20169
20170 member = TREE_OPERAND (t, 1);
20171 if (BASELINK_P (member))
20172 member = tsubst_baselink (member,
20173 non_reference (TREE_TYPE (object)),
20174 args, complain, in_decl);
20175 else
20176 member = tsubst_copy (member, args, complain, in_decl);
20177 if (member == error_mark_node)
20178 RETURN (error_mark_node);
20179
20180 if (TREE_CODE (member) == FIELD_DECL)
20181 {
20182 r = finish_non_static_data_member (member, object, NULL_TREE);
20183 if (TREE_CODE (r) == COMPONENT_REF)
20184 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20185 RETURN (r);
20186 }
20187 else if (type_dependent_expression_p (object))
20188 /* We can't do much here. */;
20189 else if (!CLASS_TYPE_P (object_type))
20190 {
20191 if (scalarish_type_p (object_type))
20192 {
20193 tree s = NULL_TREE;
20194 tree dtor = member;
20195
20196 if (TREE_CODE (dtor) == SCOPE_REF)
20197 {
20198 s = TREE_OPERAND (dtor, 0);
20199 dtor = TREE_OPERAND (dtor, 1);
20200 }
20201 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20202 {
20203 dtor = TREE_OPERAND (dtor, 0);
20204 if (TYPE_P (dtor))
20205 RETURN (finish_pseudo_destructor_expr
20206 (object, s, dtor, input_location));
20207 }
20208 }
20209 }
20210 else if (TREE_CODE (member) == SCOPE_REF
20211 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20212 {
20213 /* Lookup the template functions now that we know what the
20214 scope is. */
20215 tree scope = TREE_OPERAND (member, 0);
20216 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20217 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20218 member = lookup_qualified_name (scope, tmpl,
20219 /*is_type_p=*/false,
20220 /*complain=*/false);
20221 if (BASELINK_P (member))
20222 {
20223 BASELINK_FUNCTIONS (member)
20224 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20225 args);
20226 member = (adjust_result_of_qualified_name_lookup
20227 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20228 object_type));
20229 }
20230 else
20231 {
20232 qualified_name_lookup_error (scope, tmpl, member,
20233 input_location);
20234 RETURN (error_mark_node);
20235 }
20236 }
20237 else if (TREE_CODE (member) == SCOPE_REF
20238 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20239 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20240 {
20241 if (complain & tf_error)
20242 {
20243 if (TYPE_P (TREE_OPERAND (member, 0)))
20244 error ("%qT is not a class or namespace",
20245 TREE_OPERAND (member, 0));
20246 else
20247 error ("%qD is not a class or namespace",
20248 TREE_OPERAND (member, 0));
20249 }
20250 RETURN (error_mark_node);
20251 }
20252
20253 r = finish_class_member_access_expr (object, member,
20254 /*template_p=*/false,
20255 complain);
20256 if (TREE_CODE (r) == COMPONENT_REF)
20257 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20258 RETURN (r);
20259 }
20260
20261 case THROW_EXPR:
20262 RETURN (build_throw
20263 (input_location, RECUR (TREE_OPERAND (t, 0))));
20264
20265 case CONSTRUCTOR:
20266 {
20267 vec<constructor_elt, va_gc> *n;
20268 constructor_elt *ce;
20269 unsigned HOST_WIDE_INT idx;
20270 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20271 bool process_index_p;
20272 int newlen;
20273 bool need_copy_p = false;
20274 tree r;
20275
20276 if (type == error_mark_node)
20277 RETURN (error_mark_node);
20278
20279 /* We do not want to process the index of aggregate
20280 initializers as they are identifier nodes which will be
20281 looked up by digest_init. */
20282 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20283
20284 if (null_member_pointer_value_p (t))
20285 {
20286 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20287 RETURN (t);
20288 }
20289
20290 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20291 newlen = vec_safe_length (n);
20292 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20293 {
20294 if (ce->index && process_index_p
20295 /* An identifier index is looked up in the type
20296 being initialized, not the current scope. */
20297 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20298 ce->index = RECUR (ce->index);
20299
20300 if (PACK_EXPANSION_P (ce->value))
20301 {
20302 /* Substitute into the pack expansion. */
20303 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20304 in_decl);
20305
20306 if (ce->value == error_mark_node
20307 || PACK_EXPANSION_P (ce->value))
20308 ;
20309 else if (TREE_VEC_LENGTH (ce->value) == 1)
20310 /* Just move the argument into place. */
20311 ce->value = TREE_VEC_ELT (ce->value, 0);
20312 else
20313 {
20314 /* Update the length of the final CONSTRUCTOR
20315 arguments vector, and note that we will need to
20316 copy.*/
20317 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20318 need_copy_p = true;
20319 }
20320 }
20321 else
20322 ce->value = RECUR (ce->value);
20323 }
20324
20325 if (need_copy_p)
20326 {
20327 vec<constructor_elt, va_gc> *old_n = n;
20328
20329 vec_alloc (n, newlen);
20330 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20331 {
20332 if (TREE_CODE (ce->value) == TREE_VEC)
20333 {
20334 int i, len = TREE_VEC_LENGTH (ce->value);
20335 for (i = 0; i < len; ++i)
20336 CONSTRUCTOR_APPEND_ELT (n, 0,
20337 TREE_VEC_ELT (ce->value, i));
20338 }
20339 else
20340 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20341 }
20342 }
20343
20344 r = build_constructor (init_list_type_node, n);
20345 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20346 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20347 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20348
20349 if (TREE_HAS_CONSTRUCTOR (t))
20350 {
20351 fcl_t cl = fcl_functional;
20352 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20353 cl = fcl_c99;
20354 RETURN (finish_compound_literal (type, r, complain, cl));
20355 }
20356
20357 TREE_TYPE (r) = type;
20358 RETURN (r);
20359 }
20360
20361 case TYPEID_EXPR:
20362 {
20363 tree operand_0 = TREE_OPERAND (t, 0);
20364 if (TYPE_P (operand_0))
20365 {
20366 operand_0 = tsubst (operand_0, args, complain, in_decl);
20367 RETURN (get_typeid (operand_0, complain));
20368 }
20369 else
20370 {
20371 operand_0 = RECUR (operand_0);
20372 RETURN (build_typeid (operand_0, complain));
20373 }
20374 }
20375
20376 case VAR_DECL:
20377 if (!args)
20378 RETURN (t);
20379 /* Fall through */
20380
20381 case PARM_DECL:
20382 {
20383 tree r = tsubst_copy (t, args, complain, in_decl);
20384 /* ??? We're doing a subset of finish_id_expression here. */
20385 if (tree wrap = maybe_get_tls_wrapper_call (r))
20386 /* Replace an evaluated use of the thread_local variable with
20387 a call to its wrapper. */
20388 r = wrap;
20389 else if (outer_automatic_var_p (r))
20390 r = process_outer_var_ref (r, complain);
20391
20392 if (!TYPE_REF_P (TREE_TYPE (t)))
20393 /* If the original type was a reference, we'll be wrapped in
20394 the appropriate INDIRECT_REF. */
20395 r = convert_from_reference (r);
20396 RETURN (r);
20397 }
20398
20399 case VA_ARG_EXPR:
20400 {
20401 tree op0 = RECUR (TREE_OPERAND (t, 0));
20402 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20403 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20404 }
20405
20406 case OFFSETOF_EXPR:
20407 {
20408 tree object_ptr
20409 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20410 in_decl, /*function_p=*/false,
20411 /*integral_constant_expression_p=*/false);
20412 RETURN (finish_offsetof (object_ptr,
20413 RECUR (TREE_OPERAND (t, 0)),
20414 EXPR_LOCATION (t)));
20415 }
20416
20417 case ADDRESSOF_EXPR:
20418 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20419 RECUR (TREE_OPERAND (t, 0)), complain));
20420
20421 case TRAIT_EXPR:
20422 {
20423 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20424 complain, in_decl);
20425 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20426 complain, in_decl);
20427 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20428 TRAIT_EXPR_KIND (t), type1, type2));
20429 }
20430
20431 case STMT_EXPR:
20432 {
20433 tree old_stmt_expr = cur_stmt_expr;
20434 tree stmt_expr = begin_stmt_expr ();
20435
20436 cur_stmt_expr = stmt_expr;
20437 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20438 integral_constant_expression_p);
20439 stmt_expr = finish_stmt_expr (stmt_expr, false);
20440 cur_stmt_expr = old_stmt_expr;
20441
20442 /* If the resulting list of expression statement is empty,
20443 fold it further into void_node. */
20444 if (empty_expr_stmt_p (stmt_expr))
20445 stmt_expr = void_node;
20446
20447 RETURN (stmt_expr);
20448 }
20449
20450 case LAMBDA_EXPR:
20451 {
20452 if (complain & tf_partial)
20453 {
20454 /* We don't have a full set of template arguments yet; don't touch
20455 the lambda at all. */
20456 gcc_assert (processing_template_decl);
20457 return t;
20458 }
20459 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20460
20461 RETURN (build_lambda_object (r));
20462 }
20463
20464 case TARGET_EXPR:
20465 /* We can get here for a constant initializer of non-dependent type.
20466 FIXME stop folding in cp_parser_initializer_clause. */
20467 {
20468 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20469 complain);
20470 RETURN (r);
20471 }
20472
20473 case TRANSACTION_EXPR:
20474 RETURN (tsubst_expr(t, args, complain, in_decl,
20475 integral_constant_expression_p));
20476
20477 case PAREN_EXPR:
20478 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20479
20480 case VEC_PERM_EXPR:
20481 {
20482 tree op0 = RECUR (TREE_OPERAND (t, 0));
20483 tree op1 = RECUR (TREE_OPERAND (t, 1));
20484 tree op2 = RECUR (TREE_OPERAND (t, 2));
20485 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20486 complain));
20487 }
20488
20489 case REQUIRES_EXPR:
20490 {
20491 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20492 RETURN (r);
20493 }
20494
20495 case RANGE_EXPR:
20496 /* No need to substitute further, a RANGE_EXPR will always be built
20497 with constant operands. */
20498 RETURN (t);
20499
20500 case NON_LVALUE_EXPR:
20501 case VIEW_CONVERT_EXPR:
20502 if (location_wrapper_p (t))
20503 /* We need to do this here as well as in tsubst_copy so we get the
20504 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20505 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20506 EXPR_LOCATION (t)));
20507 /* fallthrough. */
20508
20509 default:
20510 /* Handle Objective-C++ constructs, if appropriate. */
20511 {
20512 tree subst
20513 = objcp_tsubst_copy_and_build (t, args, complain,
20514 in_decl, /*function_p=*/false);
20515 if (subst)
20516 RETURN (subst);
20517 }
20518 RETURN (tsubst_copy (t, args, complain, in_decl));
20519 }
20520
20521 #undef RECUR
20522 #undef RETURN
20523 out:
20524 input_location = save_loc;
20525 return retval;
20526 }
20527
20528 /* Verify that the instantiated ARGS are valid. For type arguments,
20529 make sure that the type's linkage is ok. For non-type arguments,
20530 make sure they are constants if they are integral or enumerations.
20531 Emit an error under control of COMPLAIN, and return TRUE on error. */
20532
20533 static bool
20534 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20535 {
20536 if (dependent_template_arg_p (t))
20537 return false;
20538 if (ARGUMENT_PACK_P (t))
20539 {
20540 tree vec = ARGUMENT_PACK_ARGS (t);
20541 int len = TREE_VEC_LENGTH (vec);
20542 bool result = false;
20543 int i;
20544
20545 for (i = 0; i < len; ++i)
20546 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20547 result = true;
20548 return result;
20549 }
20550 else if (TYPE_P (t))
20551 {
20552 /* [basic.link]: A name with no linkage (notably, the name
20553 of a class or enumeration declared in a local scope)
20554 shall not be used to declare an entity with linkage.
20555 This implies that names with no linkage cannot be used as
20556 template arguments
20557
20558 DR 757 relaxes this restriction for C++0x. */
20559 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20560 : no_linkage_check (t, /*relaxed_p=*/false));
20561
20562 if (nt)
20563 {
20564 /* DR 488 makes use of a type with no linkage cause
20565 type deduction to fail. */
20566 if (complain & tf_error)
20567 {
20568 if (TYPE_UNNAMED_P (nt))
20569 error ("%qT is/uses unnamed type", t);
20570 else
20571 error ("template argument for %qD uses local type %qT",
20572 tmpl, t);
20573 }
20574 return true;
20575 }
20576 /* In order to avoid all sorts of complications, we do not
20577 allow variably-modified types as template arguments. */
20578 else if (variably_modified_type_p (t, NULL_TREE))
20579 {
20580 if (complain & tf_error)
20581 error ("%qT is a variably modified type", t);
20582 return true;
20583 }
20584 }
20585 /* Class template and alias template arguments should be OK. */
20586 else if (DECL_TYPE_TEMPLATE_P (t))
20587 ;
20588 /* A non-type argument of integral or enumerated type must be a
20589 constant. */
20590 else if (TREE_TYPE (t)
20591 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20592 && !REFERENCE_REF_P (t)
20593 && !TREE_CONSTANT (t))
20594 {
20595 if (complain & tf_error)
20596 error ("integral expression %qE is not constant", t);
20597 return true;
20598 }
20599 return false;
20600 }
20601
20602 static bool
20603 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20604 {
20605 int ix, len = DECL_NTPARMS (tmpl);
20606 bool result = false;
20607
20608 for (ix = 0; ix != len; ix++)
20609 {
20610 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20611 result = true;
20612 }
20613 if (result && (complain & tf_error))
20614 error (" trying to instantiate %qD", tmpl);
20615 return result;
20616 }
20617
20618 /* We're out of SFINAE context now, so generate diagnostics for the access
20619 errors we saw earlier when instantiating D from TMPL and ARGS. */
20620
20621 static void
20622 recheck_decl_substitution (tree d, tree tmpl, tree args)
20623 {
20624 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20625 tree type = TREE_TYPE (pattern);
20626 location_t loc = input_location;
20627
20628 push_access_scope (d);
20629 push_deferring_access_checks (dk_no_deferred);
20630 input_location = DECL_SOURCE_LOCATION (pattern);
20631 tsubst (type, args, tf_warning_or_error, d);
20632 input_location = loc;
20633 pop_deferring_access_checks ();
20634 pop_access_scope (d);
20635 }
20636
20637 /* Instantiate the indicated variable, function, or alias template TMPL with
20638 the template arguments in TARG_PTR. */
20639
20640 static tree
20641 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20642 {
20643 tree targ_ptr = orig_args;
20644 tree fndecl;
20645 tree gen_tmpl;
20646 tree spec;
20647 bool access_ok = true;
20648
20649 if (tmpl == error_mark_node)
20650 return error_mark_node;
20651
20652 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20653
20654 /* If this function is a clone, handle it specially. */
20655 if (DECL_CLONED_FUNCTION_P (tmpl))
20656 {
20657 tree spec;
20658 tree clone;
20659
20660 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20661 DECL_CLONED_FUNCTION. */
20662 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20663 targ_ptr, complain);
20664 if (spec == error_mark_node)
20665 return error_mark_node;
20666
20667 /* Look for the clone. */
20668 FOR_EACH_CLONE (clone, spec)
20669 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20670 return clone;
20671 /* We should always have found the clone by now. */
20672 gcc_unreachable ();
20673 return NULL_TREE;
20674 }
20675
20676 if (targ_ptr == error_mark_node)
20677 return error_mark_node;
20678
20679 /* Check to see if we already have this specialization. */
20680 gen_tmpl = most_general_template (tmpl);
20681 if (TMPL_ARGS_DEPTH (targ_ptr)
20682 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20683 /* targ_ptr only has the innermost template args, so add the outer ones
20684 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20685 the case of a non-dependent call within a template definition). */
20686 targ_ptr = (add_outermost_template_args
20687 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20688 targ_ptr));
20689
20690 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20691 but it doesn't seem to be on the hot path. */
20692 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20693
20694 gcc_assert (tmpl == gen_tmpl
20695 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
20696 == spec)
20697 || fndecl == NULL_TREE);
20698
20699 if (spec != NULL_TREE)
20700 {
20701 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20702 {
20703 if (complain & tf_error)
20704 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20705 return error_mark_node;
20706 }
20707 return spec;
20708 }
20709
20710 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20711 complain))
20712 return error_mark_node;
20713
20714 /* We are building a FUNCTION_DECL, during which the access of its
20715 parameters and return types have to be checked. However this
20716 FUNCTION_DECL which is the desired context for access checking
20717 is not built yet. We solve this chicken-and-egg problem by
20718 deferring all checks until we have the FUNCTION_DECL. */
20719 push_deferring_access_checks (dk_deferred);
20720
20721 /* Instantiation of the function happens in the context of the function
20722 template, not the context of the overload resolution we're doing. */
20723 push_to_top_level ();
20724 /* If there are dependent arguments, e.g. because we're doing partial
20725 ordering, make sure processing_template_decl stays set. */
20726 if (uses_template_parms (targ_ptr))
20727 ++processing_template_decl;
20728 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20729 {
20730 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20731 complain, gen_tmpl, true);
20732 push_nested_class (ctx);
20733 }
20734
20735 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20736
20737 fndecl = NULL_TREE;
20738 if (VAR_P (pattern))
20739 {
20740 /* We need to determine if we're using a partial or explicit
20741 specialization now, because the type of the variable could be
20742 different. */
20743 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20744 tree elt = most_specialized_partial_spec (tid, complain);
20745 if (elt == error_mark_node)
20746 pattern = error_mark_node;
20747 else if (elt)
20748 {
20749 tree partial_tmpl = TREE_VALUE (elt);
20750 tree partial_args = TREE_PURPOSE (elt);
20751 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20752 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20753 }
20754 }
20755
20756 /* Substitute template parameters to obtain the specialization. */
20757 if (fndecl == NULL_TREE)
20758 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20759 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20760 pop_nested_class ();
20761 pop_from_top_level ();
20762
20763 if (fndecl == error_mark_node)
20764 {
20765 pop_deferring_access_checks ();
20766 return error_mark_node;
20767 }
20768
20769 /* The DECL_TI_TEMPLATE should always be the immediate parent
20770 template, not the most general template. */
20771 DECL_TI_TEMPLATE (fndecl) = tmpl;
20772 DECL_TI_ARGS (fndecl) = targ_ptr;
20773
20774 /* Now we know the specialization, compute access previously
20775 deferred. Do no access control for inheriting constructors,
20776 as we already checked access for the inherited constructor. */
20777 if (!(flag_new_inheriting_ctors
20778 && DECL_INHERITED_CTOR (fndecl)))
20779 {
20780 push_access_scope (fndecl);
20781 if (!perform_deferred_access_checks (complain))
20782 access_ok = false;
20783 pop_access_scope (fndecl);
20784 }
20785 pop_deferring_access_checks ();
20786
20787 /* If we've just instantiated the main entry point for a function,
20788 instantiate all the alternate entry points as well. We do this
20789 by cloning the instantiation of the main entry point, not by
20790 instantiating the template clones. */
20791 if (tree chain = DECL_CHAIN (gen_tmpl))
20792 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20793 clone_function_decl (fndecl, /*update_methods=*/false);
20794
20795 if (!access_ok)
20796 {
20797 if (!(complain & tf_error))
20798 {
20799 /* Remember to reinstantiate when we're out of SFINAE so the user
20800 can see the errors. */
20801 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20802 }
20803 return error_mark_node;
20804 }
20805 return fndecl;
20806 }
20807
20808 /* Wrapper for instantiate_template_1. */
20809
20810 tree
20811 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20812 {
20813 tree ret;
20814 timevar_push (TV_TEMPLATE_INST);
20815 ret = instantiate_template_1 (tmpl, orig_args, complain);
20816 timevar_pop (TV_TEMPLATE_INST);
20817 return ret;
20818 }
20819
20820 /* Instantiate the alias template TMPL with ARGS. Also push a template
20821 instantiation level, which instantiate_template doesn't do because
20822 functions and variables have sufficient context established by the
20823 callers. */
20824
20825 static tree
20826 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20827 {
20828 if (tmpl == error_mark_node || args == error_mark_node)
20829 return error_mark_node;
20830
20831 args =
20832 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20833 args, tmpl, complain,
20834 /*require_all_args=*/true,
20835 /*use_default_args=*/true);
20836
20837 /* FIXME check for satisfaction in check_instantiated_args. */
20838 if (flag_concepts
20839 && !any_dependent_template_arguments_p (args)
20840 && !constraints_satisfied_p (tmpl, args))
20841 {
20842 if (complain & tf_error)
20843 {
20844 auto_diagnostic_group d;
20845 error ("template constraint failure for %qD", tmpl);
20846 diagnose_constraints (input_location, tmpl, args);
20847 }
20848 return error_mark_node;
20849 }
20850
20851 if (!push_tinst_level (tmpl, args))
20852 return error_mark_node;
20853 tree r = instantiate_template (tmpl, args, complain);
20854 pop_tinst_level ();
20855
20856 return r;
20857 }
20858
20859 /* PARM is a template parameter pack for FN. Returns true iff
20860 PARM is used in a deducible way in the argument list of FN. */
20861
20862 static bool
20863 pack_deducible_p (tree parm, tree fn)
20864 {
20865 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20866 for (; t; t = TREE_CHAIN (t))
20867 {
20868 tree type = TREE_VALUE (t);
20869 tree packs;
20870 if (!PACK_EXPANSION_P (type))
20871 continue;
20872 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20873 packs; packs = TREE_CHAIN (packs))
20874 if (template_args_equal (TREE_VALUE (packs), parm))
20875 {
20876 /* The template parameter pack is used in a function parameter
20877 pack. If this is the end of the parameter list, the
20878 template parameter pack is deducible. */
20879 if (TREE_CHAIN (t) == void_list_node)
20880 return true;
20881 else
20882 /* Otherwise, not. Well, it could be deduced from
20883 a non-pack parameter, but doing so would end up with
20884 a deduction mismatch, so don't bother. */
20885 return false;
20886 }
20887 }
20888 /* The template parameter pack isn't used in any function parameter
20889 packs, but it might be used deeper, e.g. tuple<Args...>. */
20890 return true;
20891 }
20892
20893 /* Subroutine of fn_type_unification: check non-dependent parms for
20894 convertibility. */
20895
20896 static int
20897 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20898 tree fn, unification_kind_t strict, int flags,
20899 struct conversion **convs, bool explain_p)
20900 {
20901 /* Non-constructor methods need to leave a conversion for 'this', which
20902 isn't included in nargs here. */
20903 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20904 && !DECL_CONSTRUCTOR_P (fn));
20905
20906 for (unsigned ia = 0;
20907 parms && parms != void_list_node && ia < nargs; )
20908 {
20909 tree parm = TREE_VALUE (parms);
20910
20911 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20912 && (!TREE_CHAIN (parms)
20913 || TREE_CHAIN (parms) == void_list_node))
20914 /* For a function parameter pack that occurs at the end of the
20915 parameter-declaration-list, the type A of each remaining
20916 argument of the call is compared with the type P of the
20917 declarator-id of the function parameter pack. */
20918 break;
20919
20920 parms = TREE_CHAIN (parms);
20921
20922 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20923 /* For a function parameter pack that does not occur at the
20924 end of the parameter-declaration-list, the type of the
20925 parameter pack is a non-deduced context. */
20926 continue;
20927
20928 if (!uses_template_parms (parm))
20929 {
20930 tree arg = args[ia];
20931 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20932 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20933
20934 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20935 conv_p, explain_p))
20936 return 1;
20937 }
20938
20939 ++ia;
20940 }
20941
20942 return 0;
20943 }
20944
20945 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20946 NARGS elements of the arguments that are being used when calling
20947 it. TARGS is a vector into which the deduced template arguments
20948 are placed.
20949
20950 Returns either a FUNCTION_DECL for the matching specialization of FN or
20951 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20952 true, diagnostics will be printed to explain why it failed.
20953
20954 If FN is a conversion operator, or we are trying to produce a specific
20955 specialization, RETURN_TYPE is the return type desired.
20956
20957 The EXPLICIT_TARGS are explicit template arguments provided via a
20958 template-id.
20959
20960 The parameter STRICT is one of:
20961
20962 DEDUCE_CALL:
20963 We are deducing arguments for a function call, as in
20964 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20965 deducing arguments for a call to the result of a conversion
20966 function template, as in [over.call.object].
20967
20968 DEDUCE_CONV:
20969 We are deducing arguments for a conversion function, as in
20970 [temp.deduct.conv].
20971
20972 DEDUCE_EXACT:
20973 We are deducing arguments when doing an explicit instantiation
20974 as in [temp.explicit], when determining an explicit specialization
20975 as in [temp.expl.spec], or when taking the address of a function
20976 template, as in [temp.deduct.funcaddr]. */
20977
20978 tree
20979 fn_type_unification (tree fn,
20980 tree explicit_targs,
20981 tree targs,
20982 const tree *args,
20983 unsigned int nargs,
20984 tree return_type,
20985 unification_kind_t strict,
20986 int flags,
20987 struct conversion **convs,
20988 bool explain_p,
20989 bool decltype_p)
20990 {
20991 tree parms;
20992 tree fntype;
20993 tree decl = NULL_TREE;
20994 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20995 bool ok;
20996 static int deduction_depth;
20997 /* type_unification_real will pass back any access checks from default
20998 template argument substitution. */
20999 vec<deferred_access_check, va_gc> *checks = NULL;
21000 /* We don't have all the template args yet. */
21001 bool incomplete = true;
21002
21003 tree orig_fn = fn;
21004 if (flag_new_inheriting_ctors)
21005 fn = strip_inheriting_ctors (fn);
21006
21007 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21008 tree r = error_mark_node;
21009
21010 tree full_targs = targs;
21011 if (TMPL_ARGS_DEPTH (targs)
21012 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21013 full_targs = (add_outermost_template_args
21014 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21015 targs));
21016
21017 if (decltype_p)
21018 complain |= tf_decltype;
21019
21020 /* In C++0x, it's possible to have a function template whose type depends
21021 on itself recursively. This is most obvious with decltype, but can also
21022 occur with enumeration scope (c++/48969). So we need to catch infinite
21023 recursion and reject the substitution at deduction time; this function
21024 will return error_mark_node for any repeated substitution.
21025
21026 This also catches excessive recursion such as when f<N> depends on
21027 f<N-1> across all integers, and returns error_mark_node for all the
21028 substitutions back up to the initial one.
21029
21030 This is, of course, not reentrant. */
21031 if (excessive_deduction_depth)
21032 return error_mark_node;
21033 ++deduction_depth;
21034
21035 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21036
21037 fntype = TREE_TYPE (fn);
21038 if (explicit_targs)
21039 {
21040 /* [temp.deduct]
21041
21042 The specified template arguments must match the template
21043 parameters in kind (i.e., type, nontype, template), and there
21044 must not be more arguments than there are parameters;
21045 otherwise type deduction fails.
21046
21047 Nontype arguments must match the types of the corresponding
21048 nontype template parameters, or must be convertible to the
21049 types of the corresponding nontype parameters as specified in
21050 _temp.arg.nontype_, otherwise type deduction fails.
21051
21052 All references in the function type of the function template
21053 to the corresponding template parameters are replaced by the
21054 specified template argument values. If a substitution in a
21055 template parameter or in the function type of the function
21056 template results in an invalid type, type deduction fails. */
21057 int i, len = TREE_VEC_LENGTH (tparms);
21058 location_t loc = input_location;
21059 incomplete = false;
21060
21061 if (explicit_targs == error_mark_node)
21062 goto fail;
21063
21064 if (TMPL_ARGS_DEPTH (explicit_targs)
21065 < TMPL_ARGS_DEPTH (full_targs))
21066 explicit_targs = add_outermost_template_args (full_targs,
21067 explicit_targs);
21068
21069 /* Adjust any explicit template arguments before entering the
21070 substitution context. */
21071 explicit_targs
21072 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
21073 complain|tf_partial,
21074 /*require_all_args=*/false,
21075 /*use_default_args=*/false));
21076 if (explicit_targs == error_mark_node)
21077 goto fail;
21078
21079 /* Substitute the explicit args into the function type. This is
21080 necessary so that, for instance, explicitly declared function
21081 arguments can match null pointed constants. If we were given
21082 an incomplete set of explicit args, we must not do semantic
21083 processing during substitution as we could create partial
21084 instantiations. */
21085 for (i = 0; i < len; i++)
21086 {
21087 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21088 bool parameter_pack = false;
21089 tree targ = TREE_VEC_ELT (explicit_targs, i);
21090
21091 /* Dig out the actual parm. */
21092 if (TREE_CODE (parm) == TYPE_DECL
21093 || TREE_CODE (parm) == TEMPLATE_DECL)
21094 {
21095 parm = TREE_TYPE (parm);
21096 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21097 }
21098 else if (TREE_CODE (parm) == PARM_DECL)
21099 {
21100 parm = DECL_INITIAL (parm);
21101 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21102 }
21103
21104 if (targ == NULL_TREE)
21105 /* No explicit argument for this template parameter. */
21106 incomplete = true;
21107 else if (parameter_pack && pack_deducible_p (parm, fn))
21108 {
21109 /* Mark the argument pack as "incomplete". We could
21110 still deduce more arguments during unification.
21111 We remove this mark in type_unification_real. */
21112 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21113 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21114 = ARGUMENT_PACK_ARGS (targ);
21115
21116 /* We have some incomplete argument packs. */
21117 incomplete = true;
21118 }
21119 }
21120
21121 if (incomplete)
21122 {
21123 if (!push_tinst_level (fn, explicit_targs))
21124 {
21125 excessive_deduction_depth = true;
21126 goto fail;
21127 }
21128 ++processing_template_decl;
21129 input_location = DECL_SOURCE_LOCATION (fn);
21130 /* Ignore any access checks; we'll see them again in
21131 instantiate_template and they might have the wrong
21132 access path at this point. */
21133 push_deferring_access_checks (dk_deferred);
21134 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21135 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21136 pop_deferring_access_checks ();
21137 input_location = loc;
21138 --processing_template_decl;
21139 pop_tinst_level ();
21140
21141 if (fntype == error_mark_node)
21142 goto fail;
21143 }
21144
21145 /* Place the explicitly specified arguments in TARGS. */
21146 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21147 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21148 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21149 if (!incomplete && CHECKING_P
21150 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21151 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21152 (targs, NUM_TMPL_ARGS (explicit_targs));
21153 }
21154
21155 if (return_type && strict != DEDUCE_CALL)
21156 {
21157 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21158 new_args[0] = return_type;
21159 memcpy (new_args + 1, args, nargs * sizeof (tree));
21160 args = new_args;
21161 ++nargs;
21162 }
21163
21164 if (!incomplete)
21165 goto deduced;
21166
21167 /* Never do unification on the 'this' parameter. */
21168 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21169
21170 if (return_type && strict == DEDUCE_CALL)
21171 {
21172 /* We're deducing for a call to the result of a template conversion
21173 function. The parms we really want are in return_type. */
21174 if (INDIRECT_TYPE_P (return_type))
21175 return_type = TREE_TYPE (return_type);
21176 parms = TYPE_ARG_TYPES (return_type);
21177 }
21178 else if (return_type)
21179 {
21180 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21181 }
21182
21183 /* We allow incomplete unification without an error message here
21184 because the standard doesn't seem to explicitly prohibit it. Our
21185 callers must be ready to deal with unification failures in any
21186 event. */
21187
21188 /* If we aren't explaining yet, push tinst context so we can see where
21189 any errors (e.g. from class instantiations triggered by instantiation
21190 of default template arguments) come from. If we are explaining, this
21191 context is redundant. */
21192 if (!explain_p && !push_tinst_level (fn, targs))
21193 {
21194 excessive_deduction_depth = true;
21195 goto fail;
21196 }
21197
21198 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21199 full_targs, parms, args, nargs, /*subr=*/0,
21200 strict, &checks, explain_p);
21201 if (!explain_p)
21202 pop_tinst_level ();
21203 if (!ok)
21204 goto fail;
21205
21206 /* Now that we have bindings for all of the template arguments,
21207 ensure that the arguments deduced for the template template
21208 parameters have compatible template parameter lists. We cannot
21209 check this property before we have deduced all template
21210 arguments, because the template parameter types of a template
21211 template parameter might depend on prior template parameters
21212 deduced after the template template parameter. The following
21213 ill-formed example illustrates this issue:
21214
21215 template<typename T, template<T> class C> void f(C<5>, T);
21216
21217 template<int N> struct X {};
21218
21219 void g() {
21220 f(X<5>(), 5l); // error: template argument deduction fails
21221 }
21222
21223 The template parameter list of 'C' depends on the template type
21224 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21225 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21226 time that we deduce 'C'. */
21227 if (!template_template_parm_bindings_ok_p
21228 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21229 {
21230 unify_inconsistent_template_template_parameters (explain_p);
21231 goto fail;
21232 }
21233
21234 /* DR 1391: All parameters have args, now check non-dependent parms for
21235 convertibility. */
21236 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21237 convs, explain_p))
21238 goto fail;
21239
21240 deduced:
21241 /* All is well so far. Now, check:
21242
21243 [temp.deduct]
21244
21245 When all template arguments have been deduced, all uses of
21246 template parameters in nondeduced contexts are replaced with
21247 the corresponding deduced argument values. If the
21248 substitution results in an invalid type, as described above,
21249 type deduction fails. */
21250 if (!push_tinst_level (fn, targs))
21251 {
21252 excessive_deduction_depth = true;
21253 goto fail;
21254 }
21255
21256 /* Also collect access checks from the instantiation. */
21257 reopen_deferring_access_checks (checks);
21258
21259 decl = instantiate_template (fn, targs, complain);
21260
21261 checks = get_deferred_access_checks ();
21262 pop_deferring_access_checks ();
21263
21264 pop_tinst_level ();
21265
21266 if (decl == error_mark_node)
21267 goto fail;
21268
21269 /* Now perform any access checks encountered during substitution. */
21270 push_access_scope (decl);
21271 ok = perform_access_checks (checks, complain);
21272 pop_access_scope (decl);
21273 if (!ok)
21274 goto fail;
21275
21276 /* If we're looking for an exact match, check that what we got
21277 is indeed an exact match. It might not be if some template
21278 parameters are used in non-deduced contexts. But don't check
21279 for an exact match if we have dependent template arguments;
21280 in that case we're doing partial ordering, and we already know
21281 that we have two candidates that will provide the actual type. */
21282 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21283 {
21284 tree substed = TREE_TYPE (decl);
21285 unsigned int i;
21286
21287 tree sarg
21288 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21289 if (return_type)
21290 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21291 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21292 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21293 {
21294 unify_type_mismatch (explain_p, args[i],
21295 TREE_VALUE (sarg));
21296 goto fail;
21297 }
21298 }
21299
21300 /* After doing deduction with the inherited constructor, actually return an
21301 instantiation of the inheriting constructor. */
21302 if (orig_fn != fn)
21303 decl = instantiate_template (orig_fn, targs, complain);
21304
21305 r = decl;
21306
21307 fail:
21308 --deduction_depth;
21309 if (excessive_deduction_depth)
21310 {
21311 if (deduction_depth == 0)
21312 /* Reset once we're all the way out. */
21313 excessive_deduction_depth = false;
21314 }
21315
21316 return r;
21317 }
21318
21319 /* Adjust types before performing type deduction, as described in
21320 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21321 sections are symmetric. PARM is the type of a function parameter
21322 or the return type of the conversion function. ARG is the type of
21323 the argument passed to the call, or the type of the value
21324 initialized with the result of the conversion function.
21325 ARG_EXPR is the original argument expression, which may be null. */
21326
21327 static int
21328 maybe_adjust_types_for_deduction (unification_kind_t strict,
21329 tree* parm,
21330 tree* arg,
21331 tree arg_expr)
21332 {
21333 int result = 0;
21334
21335 switch (strict)
21336 {
21337 case DEDUCE_CALL:
21338 break;
21339
21340 case DEDUCE_CONV:
21341 /* Swap PARM and ARG throughout the remainder of this
21342 function; the handling is precisely symmetric since PARM
21343 will initialize ARG rather than vice versa. */
21344 std::swap (parm, arg);
21345 break;
21346
21347 case DEDUCE_EXACT:
21348 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21349 too, but here handle it by stripping the reference from PARM
21350 rather than by adding it to ARG. */
21351 if (TYPE_REF_P (*parm)
21352 && TYPE_REF_IS_RVALUE (*parm)
21353 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21354 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21355 && TYPE_REF_P (*arg)
21356 && !TYPE_REF_IS_RVALUE (*arg))
21357 *parm = TREE_TYPE (*parm);
21358 /* Nothing else to do in this case. */
21359 return 0;
21360
21361 default:
21362 gcc_unreachable ();
21363 }
21364
21365 if (!TYPE_REF_P (*parm))
21366 {
21367 /* [temp.deduct.call]
21368
21369 If P is not a reference type:
21370
21371 --If A is an array type, the pointer type produced by the
21372 array-to-pointer standard conversion (_conv.array_) is
21373 used in place of A for type deduction; otherwise,
21374
21375 --If A is a function type, the pointer type produced by
21376 the function-to-pointer standard conversion
21377 (_conv.func_) is used in place of A for type deduction;
21378 otherwise,
21379
21380 --If A is a cv-qualified type, the top level
21381 cv-qualifiers of A's type are ignored for type
21382 deduction. */
21383 if (TREE_CODE (*arg) == ARRAY_TYPE)
21384 *arg = build_pointer_type (TREE_TYPE (*arg));
21385 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21386 *arg = build_pointer_type (*arg);
21387 else
21388 *arg = TYPE_MAIN_VARIANT (*arg);
21389 }
21390
21391 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21392 reference to a cv-unqualified template parameter that does not represent a
21393 template parameter of a class template (during class template argument
21394 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21395 an lvalue, the type "lvalue reference to A" is used in place of A for type
21396 deduction. */
21397 if (TYPE_REF_P (*parm)
21398 && TYPE_REF_IS_RVALUE (*parm)
21399 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21400 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21401 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21402 && (arg_expr ? lvalue_p (arg_expr)
21403 /* try_one_overload doesn't provide an arg_expr, but
21404 functions are always lvalues. */
21405 : TREE_CODE (*arg) == FUNCTION_TYPE))
21406 *arg = build_reference_type (*arg);
21407
21408 /* [temp.deduct.call]
21409
21410 If P is a cv-qualified type, the top level cv-qualifiers
21411 of P's type are ignored for type deduction. If P is a
21412 reference type, the type referred to by P is used for
21413 type deduction. */
21414 *parm = TYPE_MAIN_VARIANT (*parm);
21415 if (TYPE_REF_P (*parm))
21416 {
21417 *parm = TREE_TYPE (*parm);
21418 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21419 }
21420
21421 /* DR 322. For conversion deduction, remove a reference type on parm
21422 too (which has been swapped into ARG). */
21423 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21424 *arg = TREE_TYPE (*arg);
21425
21426 return result;
21427 }
21428
21429 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21430 template which doesn't contain any deducible template parameters; check if
21431 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21432 unify_one_argument. */
21433
21434 static int
21435 check_non_deducible_conversion (tree parm, tree arg, int strict,
21436 int flags, struct conversion **conv_p,
21437 bool explain_p)
21438 {
21439 tree type;
21440
21441 if (!TYPE_P (arg))
21442 type = TREE_TYPE (arg);
21443 else
21444 type = arg;
21445
21446 if (same_type_p (parm, type))
21447 return unify_success (explain_p);
21448
21449 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21450 if (strict == DEDUCE_CONV)
21451 {
21452 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21453 return unify_success (explain_p);
21454 }
21455 else if (strict != DEDUCE_EXACT)
21456 {
21457 bool ok = false;
21458 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21459 if (conv_p)
21460 /* Avoid recalculating this in add_function_candidate. */
21461 ok = (*conv_p
21462 = good_conversion (parm, type, conv_arg, flags, complain));
21463 else
21464 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21465 if (ok)
21466 return unify_success (explain_p);
21467 }
21468
21469 if (strict == DEDUCE_EXACT)
21470 return unify_type_mismatch (explain_p, parm, arg);
21471 else
21472 return unify_arg_conversion (explain_p, parm, type, arg);
21473 }
21474
21475 static bool uses_deducible_template_parms (tree type);
21476
21477 /* Returns true iff the expression EXPR is one from which a template
21478 argument can be deduced. In other words, if it's an undecorated
21479 use of a template non-type parameter. */
21480
21481 static bool
21482 deducible_expression (tree expr)
21483 {
21484 /* Strip implicit conversions. */
21485 while (CONVERT_EXPR_P (expr) || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
21486 expr = TREE_OPERAND (expr, 0);
21487 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21488 }
21489
21490 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21491 deducible way; that is, if it has a max value of <PARM> - 1. */
21492
21493 static bool
21494 deducible_array_bound (tree domain)
21495 {
21496 if (domain == NULL_TREE)
21497 return false;
21498
21499 tree max = TYPE_MAX_VALUE (domain);
21500 if (TREE_CODE (max) != MINUS_EXPR)
21501 return false;
21502
21503 return deducible_expression (TREE_OPERAND (max, 0));
21504 }
21505
21506 /* Returns true iff the template arguments ARGS use a template parameter
21507 in a deducible way. */
21508
21509 static bool
21510 deducible_template_args (tree args)
21511 {
21512 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21513 {
21514 bool deducible;
21515 tree elt = TREE_VEC_ELT (args, i);
21516 if (ARGUMENT_PACK_P (elt))
21517 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21518 else
21519 {
21520 if (PACK_EXPANSION_P (elt))
21521 elt = PACK_EXPANSION_PATTERN (elt);
21522 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21523 deducible = true;
21524 else if (TYPE_P (elt))
21525 deducible = uses_deducible_template_parms (elt);
21526 else
21527 deducible = deducible_expression (elt);
21528 }
21529 if (deducible)
21530 return true;
21531 }
21532 return false;
21533 }
21534
21535 /* Returns true iff TYPE contains any deducible references to template
21536 parameters, as per 14.8.2.5. */
21537
21538 static bool
21539 uses_deducible_template_parms (tree type)
21540 {
21541 if (PACK_EXPANSION_P (type))
21542 type = PACK_EXPANSION_PATTERN (type);
21543
21544 /* T
21545 cv-list T
21546 TT<T>
21547 TT<i>
21548 TT<> */
21549 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21550 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21551 return true;
21552
21553 /* T*
21554 T&
21555 T&& */
21556 if (INDIRECT_TYPE_P (type))
21557 return uses_deducible_template_parms (TREE_TYPE (type));
21558
21559 /* T[integer-constant ]
21560 type [i] */
21561 if (TREE_CODE (type) == ARRAY_TYPE)
21562 return (uses_deducible_template_parms (TREE_TYPE (type))
21563 || deducible_array_bound (TYPE_DOMAIN (type)));
21564
21565 /* T type ::*
21566 type T::*
21567 T T::*
21568 T (type ::*)()
21569 type (T::*)()
21570 type (type ::*)(T)
21571 type (T::*)(T)
21572 T (type ::*)(T)
21573 T (T::*)()
21574 T (T::*)(T) */
21575 if (TYPE_PTRMEM_P (type))
21576 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21577 || (uses_deducible_template_parms
21578 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21579
21580 /* template-name <T> (where template-name refers to a class template)
21581 template-name <i> (where template-name refers to a class template) */
21582 if (CLASS_TYPE_P (type)
21583 && CLASSTYPE_TEMPLATE_INFO (type)
21584 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21585 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21586 (CLASSTYPE_TI_ARGS (type)));
21587
21588 /* type (T)
21589 T()
21590 T(T) */
21591 if (FUNC_OR_METHOD_TYPE_P (type))
21592 {
21593 if (uses_deducible_template_parms (TREE_TYPE (type)))
21594 return true;
21595 tree parm = TYPE_ARG_TYPES (type);
21596 if (TREE_CODE (type) == METHOD_TYPE)
21597 parm = TREE_CHAIN (parm);
21598 for (; parm; parm = TREE_CHAIN (parm))
21599 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21600 return true;
21601 }
21602
21603 return false;
21604 }
21605
21606 /* Subroutine of type_unification_real and unify_pack_expansion to
21607 handle unification of a single P/A pair. Parameters are as
21608 for those functions. */
21609
21610 static int
21611 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21612 int subr, unification_kind_t strict,
21613 bool explain_p)
21614 {
21615 tree arg_expr = NULL_TREE;
21616 int arg_strict;
21617
21618 if (arg == error_mark_node || parm == error_mark_node)
21619 return unify_invalid (explain_p);
21620 if (arg == unknown_type_node)
21621 /* We can't deduce anything from this, but we might get all the
21622 template args from other function args. */
21623 return unify_success (explain_p);
21624
21625 /* Implicit conversions (Clause 4) will be performed on a function
21626 argument to convert it to the type of the corresponding function
21627 parameter if the parameter type contains no template-parameters that
21628 participate in template argument deduction. */
21629 if (strict != DEDUCE_EXACT
21630 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21631 /* For function parameters with no deducible template parameters,
21632 just return. We'll check non-dependent conversions later. */
21633 return unify_success (explain_p);
21634
21635 switch (strict)
21636 {
21637 case DEDUCE_CALL:
21638 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21639 | UNIFY_ALLOW_MORE_CV_QUAL
21640 | UNIFY_ALLOW_DERIVED);
21641 break;
21642
21643 case DEDUCE_CONV:
21644 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21645 break;
21646
21647 case DEDUCE_EXACT:
21648 arg_strict = UNIFY_ALLOW_NONE;
21649 break;
21650
21651 default:
21652 gcc_unreachable ();
21653 }
21654
21655 /* We only do these transformations if this is the top-level
21656 parameter_type_list in a call or declaration matching; in other
21657 situations (nested function declarators, template argument lists) we
21658 won't be comparing a type to an expression, and we don't do any type
21659 adjustments. */
21660 if (!subr)
21661 {
21662 if (!TYPE_P (arg))
21663 {
21664 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21665 if (type_unknown_p (arg))
21666 {
21667 /* [temp.deduct.type] A template-argument can be
21668 deduced from a pointer to function or pointer
21669 to member function argument if the set of
21670 overloaded functions does not contain function
21671 templates and at most one of a set of
21672 overloaded functions provides a unique
21673 match. */
21674 resolve_overloaded_unification (tparms, targs, parm,
21675 arg, strict,
21676 arg_strict, explain_p);
21677 /* If a unique match was not found, this is a
21678 non-deduced context, so we still succeed. */
21679 return unify_success (explain_p);
21680 }
21681
21682 arg_expr = arg;
21683 arg = unlowered_expr_type (arg);
21684 if (arg == error_mark_node)
21685 return unify_invalid (explain_p);
21686 }
21687
21688 arg_strict |=
21689 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21690 }
21691 else
21692 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21693 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21694 return unify_template_argument_mismatch (explain_p, parm, arg);
21695
21696 /* For deduction from an init-list we need the actual list. */
21697 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21698 arg = arg_expr;
21699 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21700 }
21701
21702 /* for_each_template_parm callback that always returns 0. */
21703
21704 static int
21705 zero_r (tree, void *)
21706 {
21707 return 0;
21708 }
21709
21710 /* for_each_template_parm any_fn callback to handle deduction of a template
21711 type argument from the type of an array bound. */
21712
21713 static int
21714 array_deduction_r (tree t, void *data)
21715 {
21716 tree_pair_p d = (tree_pair_p)data;
21717 tree &tparms = d->purpose;
21718 tree &targs = d->value;
21719
21720 if (TREE_CODE (t) == ARRAY_TYPE)
21721 if (tree dom = TYPE_DOMAIN (t))
21722 if (tree max = TYPE_MAX_VALUE (dom))
21723 {
21724 if (TREE_CODE (max) == MINUS_EXPR)
21725 max = TREE_OPERAND (max, 0);
21726 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21727 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21728 UNIFY_ALLOW_NONE, /*explain*/false);
21729 }
21730
21731 /* Keep walking. */
21732 return 0;
21733 }
21734
21735 /* Try to deduce any not-yet-deduced template type arguments from the type of
21736 an array bound. This is handled separately from unify because 14.8.2.5 says
21737 "The type of a type parameter is only deduced from an array bound if it is
21738 not otherwise deduced." */
21739
21740 static void
21741 try_array_deduction (tree tparms, tree targs, tree parm)
21742 {
21743 tree_pair_s data = { tparms, targs };
21744 hash_set<tree> visited;
21745 for_each_template_parm (parm, zero_r, &data, &visited,
21746 /*nondeduced*/false, array_deduction_r);
21747 }
21748
21749 /* Most parms like fn_type_unification.
21750
21751 If SUBR is 1, we're being called recursively (to unify the
21752 arguments of a function or method parameter of a function
21753 template).
21754
21755 CHECKS is a pointer to a vector of access checks encountered while
21756 substituting default template arguments. */
21757
21758 static int
21759 type_unification_real (tree tparms,
21760 tree full_targs,
21761 tree xparms,
21762 const tree *xargs,
21763 unsigned int xnargs,
21764 int subr,
21765 unification_kind_t strict,
21766 vec<deferred_access_check, va_gc> **checks,
21767 bool explain_p)
21768 {
21769 tree parm, arg;
21770 int i;
21771 int ntparms = TREE_VEC_LENGTH (tparms);
21772 int saw_undeduced = 0;
21773 tree parms;
21774 const tree *args;
21775 unsigned int nargs;
21776 unsigned int ia;
21777
21778 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21779 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21780 gcc_assert (ntparms > 0);
21781
21782 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21783
21784 /* Reset the number of non-defaulted template arguments contained
21785 in TARGS. */
21786 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21787
21788 again:
21789 parms = xparms;
21790 args = xargs;
21791 nargs = xnargs;
21792
21793 ia = 0;
21794 while (parms && parms != void_list_node
21795 && ia < nargs)
21796 {
21797 parm = TREE_VALUE (parms);
21798
21799 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21800 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21801 /* For a function parameter pack that occurs at the end of the
21802 parameter-declaration-list, the type A of each remaining
21803 argument of the call is compared with the type P of the
21804 declarator-id of the function parameter pack. */
21805 break;
21806
21807 parms = TREE_CHAIN (parms);
21808
21809 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21810 /* For a function parameter pack that does not occur at the
21811 end of the parameter-declaration-list, the type of the
21812 parameter pack is a non-deduced context. */
21813 continue;
21814
21815 arg = args[ia];
21816 ++ia;
21817
21818 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21819 explain_p))
21820 return 1;
21821 }
21822
21823 if (parms
21824 && parms != void_list_node
21825 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21826 {
21827 /* Unify the remaining arguments with the pack expansion type. */
21828 tree argvec;
21829 tree parmvec = make_tree_vec (1);
21830
21831 /* Allocate a TREE_VEC and copy in all of the arguments */
21832 argvec = make_tree_vec (nargs - ia);
21833 for (i = 0; ia < nargs; ++ia, ++i)
21834 TREE_VEC_ELT (argvec, i) = args[ia];
21835
21836 /* Copy the parameter into parmvec. */
21837 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21838 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21839 /*subr=*/subr, explain_p))
21840 return 1;
21841
21842 /* Advance to the end of the list of parameters. */
21843 parms = TREE_CHAIN (parms);
21844 }
21845
21846 /* Fail if we've reached the end of the parm list, and more args
21847 are present, and the parm list isn't variadic. */
21848 if (ia < nargs && parms == void_list_node)
21849 return unify_too_many_arguments (explain_p, nargs, ia);
21850 /* Fail if parms are left and they don't have default values and
21851 they aren't all deduced as empty packs (c++/57397). This is
21852 consistent with sufficient_parms_p. */
21853 if (parms && parms != void_list_node
21854 && TREE_PURPOSE (parms) == NULL_TREE)
21855 {
21856 unsigned int count = nargs;
21857 tree p = parms;
21858 bool type_pack_p;
21859 do
21860 {
21861 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21862 if (!type_pack_p)
21863 count++;
21864 p = TREE_CHAIN (p);
21865 }
21866 while (p && p != void_list_node);
21867 if (count != nargs)
21868 return unify_too_few_arguments (explain_p, ia, count,
21869 type_pack_p);
21870 }
21871
21872 if (!subr)
21873 {
21874 tsubst_flags_t complain = (explain_p
21875 ? tf_warning_or_error
21876 : tf_none);
21877 bool tried_array_deduction = (cxx_dialect < cxx17);
21878
21879 for (i = 0; i < ntparms; i++)
21880 {
21881 tree targ = TREE_VEC_ELT (targs, i);
21882 tree tparm = TREE_VEC_ELT (tparms, i);
21883
21884 /* Clear the "incomplete" flags on all argument packs now so that
21885 substituting them into later default arguments works. */
21886 if (targ && ARGUMENT_PACK_P (targ))
21887 {
21888 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21889 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21890 }
21891
21892 if (targ || tparm == error_mark_node)
21893 continue;
21894 tparm = TREE_VALUE (tparm);
21895
21896 if (TREE_CODE (tparm) == TYPE_DECL
21897 && !tried_array_deduction)
21898 {
21899 try_array_deduction (tparms, targs, xparms);
21900 tried_array_deduction = true;
21901 if (TREE_VEC_ELT (targs, i))
21902 continue;
21903 }
21904
21905 /* If this is an undeduced nontype parameter that depends on
21906 a type parameter, try another pass; its type may have been
21907 deduced from a later argument than the one from which
21908 this parameter can be deduced. */
21909 if (TREE_CODE (tparm) == PARM_DECL
21910 && uses_template_parms (TREE_TYPE (tparm))
21911 && saw_undeduced < 2)
21912 {
21913 saw_undeduced = 1;
21914 continue;
21915 }
21916
21917 /* Core issue #226 (C++0x) [temp.deduct]:
21918
21919 If a template argument has not been deduced, its
21920 default template argument, if any, is used.
21921
21922 When we are in C++98 mode, TREE_PURPOSE will either
21923 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21924 to explicitly check cxx_dialect here. */
21925 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21926 /* OK, there is a default argument. Wait until after the
21927 conversion check to do substitution. */
21928 continue;
21929
21930 /* If the type parameter is a parameter pack, then it will
21931 be deduced to an empty parameter pack. */
21932 if (template_parameter_pack_p (tparm))
21933 {
21934 tree arg;
21935
21936 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21937 {
21938 arg = make_node (NONTYPE_ARGUMENT_PACK);
21939 TREE_CONSTANT (arg) = 1;
21940 }
21941 else
21942 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21943
21944 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21945
21946 TREE_VEC_ELT (targs, i) = arg;
21947 continue;
21948 }
21949
21950 return unify_parameter_deduction_failure (explain_p, tparm);
21951 }
21952
21953 /* Now substitute into the default template arguments. */
21954 for (i = 0; i < ntparms; i++)
21955 {
21956 tree targ = TREE_VEC_ELT (targs, i);
21957 tree tparm = TREE_VEC_ELT (tparms, i);
21958
21959 if (targ || tparm == error_mark_node)
21960 continue;
21961 tree parm = TREE_VALUE (tparm);
21962 tree arg = TREE_PURPOSE (tparm);
21963 reopen_deferring_access_checks (*checks);
21964 location_t save_loc = input_location;
21965 if (DECL_P (parm))
21966 input_location = DECL_SOURCE_LOCATION (parm);
21967
21968 if (saw_undeduced == 1
21969 && TREE_CODE (parm) == PARM_DECL
21970 && uses_template_parms (TREE_TYPE (parm)))
21971 {
21972 /* The type of this non-type parameter depends on undeduced
21973 parameters. Don't try to use its default argument yet,
21974 since we might deduce an argument for it on the next pass,
21975 but do check whether the arguments we already have cause
21976 substitution failure, so that that happens before we try
21977 later default arguments (78489). */
21978 ++processing_template_decl;
21979 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21980 NULL_TREE);
21981 --processing_template_decl;
21982 if (type == error_mark_node)
21983 arg = error_mark_node;
21984 else
21985 arg = NULL_TREE;
21986 }
21987 else
21988 {
21989 /* Even if the call is happening in template context, getting
21990 here means it's non-dependent, and a default argument is
21991 considered a separate definition under [temp.decls], so we can
21992 do this substitution without processing_template_decl. This
21993 is important if the default argument contains something that
21994 might be instantiation-dependent like access (87480). */
21995 processing_template_decl_sentinel s;
21996 tree substed = NULL_TREE;
21997 if (saw_undeduced == 1)
21998 {
21999 /* First instatiate in template context, in case we still
22000 depend on undeduced template parameters. */
22001 ++processing_template_decl;
22002 substed = tsubst_template_arg (arg, full_targs, complain,
22003 NULL_TREE);
22004 --processing_template_decl;
22005 if (substed != error_mark_node
22006 && !uses_template_parms (substed))
22007 /* We replaced all the tparms, substitute again out of
22008 template context. */
22009 substed = NULL_TREE;
22010 }
22011 if (!substed)
22012 substed = tsubst_template_arg (arg, full_targs, complain,
22013 NULL_TREE);
22014
22015 if (!uses_template_parms (substed))
22016 arg = convert_template_argument (parm, substed, full_targs,
22017 complain, i, NULL_TREE);
22018 else if (saw_undeduced == 1)
22019 arg = NULL_TREE;
22020 else
22021 arg = error_mark_node;
22022 }
22023
22024 input_location = save_loc;
22025 *checks = get_deferred_access_checks ();
22026 pop_deferring_access_checks ();
22027
22028 if (arg == error_mark_node)
22029 return 1;
22030 else if (arg)
22031 {
22032 TREE_VEC_ELT (targs, i) = arg;
22033 /* The position of the first default template argument,
22034 is also the number of non-defaulted arguments in TARGS.
22035 Record that. */
22036 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22037 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22038 }
22039 }
22040
22041 if (saw_undeduced++ == 1)
22042 goto again;
22043 }
22044
22045 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22046 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22047
22048 return unify_success (explain_p);
22049 }
22050
22051 /* Subroutine of type_unification_real. Args are like the variables
22052 at the call site. ARG is an overloaded function (or template-id);
22053 we try deducing template args from each of the overloads, and if
22054 only one succeeds, we go with that. Modifies TARGS and returns
22055 true on success. */
22056
22057 static bool
22058 resolve_overloaded_unification (tree tparms,
22059 tree targs,
22060 tree parm,
22061 tree arg,
22062 unification_kind_t strict,
22063 int sub_strict,
22064 bool explain_p)
22065 {
22066 tree tempargs = copy_node (targs);
22067 int good = 0;
22068 tree goodfn = NULL_TREE;
22069 bool addr_p;
22070
22071 if (TREE_CODE (arg) == ADDR_EXPR)
22072 {
22073 arg = TREE_OPERAND (arg, 0);
22074 addr_p = true;
22075 }
22076 else
22077 addr_p = false;
22078
22079 if (TREE_CODE (arg) == COMPONENT_REF)
22080 /* Handle `&x' where `x' is some static or non-static member
22081 function name. */
22082 arg = TREE_OPERAND (arg, 1);
22083
22084 if (TREE_CODE (arg) == OFFSET_REF)
22085 arg = TREE_OPERAND (arg, 1);
22086
22087 /* Strip baselink information. */
22088 if (BASELINK_P (arg))
22089 arg = BASELINK_FUNCTIONS (arg);
22090
22091 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22092 {
22093 /* If we got some explicit template args, we need to plug them into
22094 the affected templates before we try to unify, in case the
22095 explicit args will completely resolve the templates in question. */
22096
22097 int ok = 0;
22098 tree expl_subargs = TREE_OPERAND (arg, 1);
22099 arg = TREE_OPERAND (arg, 0);
22100
22101 for (lkp_iterator iter (arg); iter; ++iter)
22102 {
22103 tree fn = *iter;
22104 tree subargs, elem;
22105
22106 if (TREE_CODE (fn) != TEMPLATE_DECL)
22107 continue;
22108
22109 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22110 expl_subargs, NULL_TREE, tf_none,
22111 /*require_all_args=*/true,
22112 /*use_default_args=*/true);
22113 if (subargs != error_mark_node
22114 && !any_dependent_template_arguments_p (subargs))
22115 {
22116 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
22117 if (try_one_overload (tparms, targs, tempargs, parm,
22118 elem, strict, sub_strict, addr_p, explain_p)
22119 && (!goodfn || !same_type_p (goodfn, elem)))
22120 {
22121 goodfn = elem;
22122 ++good;
22123 }
22124 }
22125 else if (subargs)
22126 ++ok;
22127 }
22128 /* If no templates (or more than one) are fully resolved by the
22129 explicit arguments, this template-id is a non-deduced context; it
22130 could still be OK if we deduce all template arguments for the
22131 enclosing call through other arguments. */
22132 if (good != 1)
22133 good = ok;
22134 }
22135 else if (!OVL_P (arg))
22136 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22137 -- but the deduction does not succeed because the expression is
22138 not just the function on its own. */
22139 return false;
22140 else
22141 for (lkp_iterator iter (arg); iter; ++iter)
22142 {
22143 tree fn = *iter;
22144 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22145 strict, sub_strict, addr_p, explain_p)
22146 && (!goodfn || !decls_match (goodfn, fn)))
22147 {
22148 goodfn = fn;
22149 ++good;
22150 }
22151 }
22152
22153 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22154 to function or pointer to member function argument if the set of
22155 overloaded functions does not contain function templates and at most
22156 one of a set of overloaded functions provides a unique match.
22157
22158 So if we found multiple possibilities, we return success but don't
22159 deduce anything. */
22160
22161 if (good == 1)
22162 {
22163 int i = TREE_VEC_LENGTH (targs);
22164 for (; i--; )
22165 if (TREE_VEC_ELT (tempargs, i))
22166 {
22167 tree old = TREE_VEC_ELT (targs, i);
22168 tree new_ = TREE_VEC_ELT (tempargs, i);
22169 if (new_ && old && ARGUMENT_PACK_P (old)
22170 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22171 /* Don't forget explicit template arguments in a pack. */
22172 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22173 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22174 TREE_VEC_ELT (targs, i) = new_;
22175 }
22176 }
22177 if (good)
22178 return true;
22179
22180 return false;
22181 }
22182
22183 /* Core DR 115: In contexts where deduction is done and fails, or in
22184 contexts where deduction is not done, if a template argument list is
22185 specified and it, along with any default template arguments, identifies
22186 a single function template specialization, then the template-id is an
22187 lvalue for the function template specialization. */
22188
22189 tree
22190 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22191 {
22192 tree expr, offset, baselink;
22193 bool addr;
22194
22195 if (!type_unknown_p (orig_expr))
22196 return orig_expr;
22197
22198 expr = orig_expr;
22199 addr = false;
22200 offset = NULL_TREE;
22201 baselink = NULL_TREE;
22202
22203 if (TREE_CODE (expr) == ADDR_EXPR)
22204 {
22205 expr = TREE_OPERAND (expr, 0);
22206 addr = true;
22207 }
22208 if (TREE_CODE (expr) == OFFSET_REF)
22209 {
22210 offset = expr;
22211 expr = TREE_OPERAND (expr, 1);
22212 }
22213 if (BASELINK_P (expr))
22214 {
22215 baselink = expr;
22216 expr = BASELINK_FUNCTIONS (expr);
22217 }
22218
22219 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22220 {
22221 int good = 0;
22222 tree goodfn = NULL_TREE;
22223
22224 /* If we got some explicit template args, we need to plug them into
22225 the affected templates before we try to unify, in case the
22226 explicit args will completely resolve the templates in question. */
22227
22228 tree expl_subargs = TREE_OPERAND (expr, 1);
22229 tree arg = TREE_OPERAND (expr, 0);
22230 tree badfn = NULL_TREE;
22231 tree badargs = NULL_TREE;
22232
22233 for (lkp_iterator iter (arg); iter; ++iter)
22234 {
22235 tree fn = *iter;
22236 tree subargs, elem;
22237
22238 if (TREE_CODE (fn) != TEMPLATE_DECL)
22239 continue;
22240
22241 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22242 expl_subargs, NULL_TREE, tf_none,
22243 /*require_all_args=*/true,
22244 /*use_default_args=*/true);
22245 if (subargs != error_mark_node
22246 && !any_dependent_template_arguments_p (subargs))
22247 {
22248 elem = instantiate_template (fn, subargs, tf_none);
22249 if (elem == error_mark_node)
22250 {
22251 badfn = fn;
22252 badargs = subargs;
22253 }
22254 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
22255 {
22256 goodfn = elem;
22257 ++good;
22258 }
22259 }
22260 }
22261 if (good == 1)
22262 {
22263 mark_used (goodfn);
22264 expr = goodfn;
22265 if (baselink)
22266 expr = build_baselink (BASELINK_BINFO (baselink),
22267 BASELINK_ACCESS_BINFO (baselink),
22268 expr, BASELINK_OPTYPE (baselink));
22269 if (offset)
22270 {
22271 tree base
22272 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22273 expr = build_offset_ref (base, expr, addr, complain);
22274 }
22275 if (addr)
22276 expr = cp_build_addr_expr (expr, complain);
22277 return expr;
22278 }
22279 else if (good == 0 && badargs && (complain & tf_error))
22280 /* There were no good options and at least one bad one, so let the
22281 user know what the problem is. */
22282 instantiate_template (badfn, badargs, complain);
22283 }
22284 return orig_expr;
22285 }
22286
22287 /* As above, but error out if the expression remains overloaded. */
22288
22289 tree
22290 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22291 {
22292 exp = resolve_nondeduced_context (exp, complain);
22293 if (type_unknown_p (exp))
22294 {
22295 if (complain & tf_error)
22296 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22297 return error_mark_node;
22298 }
22299 return exp;
22300 }
22301
22302 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22303 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22304 different overloads deduce different arguments for a given parm.
22305 ADDR_P is true if the expression for which deduction is being
22306 performed was of the form "& fn" rather than simply "fn".
22307
22308 Returns 1 on success. */
22309
22310 static int
22311 try_one_overload (tree tparms,
22312 tree orig_targs,
22313 tree targs,
22314 tree parm,
22315 tree arg,
22316 unification_kind_t strict,
22317 int sub_strict,
22318 bool addr_p,
22319 bool explain_p)
22320 {
22321 int nargs;
22322 tree tempargs;
22323 int i;
22324
22325 if (arg == error_mark_node)
22326 return 0;
22327
22328 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22329 to function or pointer to member function argument if the set of
22330 overloaded functions does not contain function templates and at most
22331 one of a set of overloaded functions provides a unique match.
22332
22333 So if this is a template, just return success. */
22334
22335 if (uses_template_parms (arg))
22336 return 1;
22337
22338 if (TREE_CODE (arg) == METHOD_TYPE)
22339 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22340 else if (addr_p)
22341 arg = build_pointer_type (arg);
22342
22343 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22344
22345 /* We don't copy orig_targs for this because if we have already deduced
22346 some template args from previous args, unify would complain when we
22347 try to deduce a template parameter for the same argument, even though
22348 there isn't really a conflict. */
22349 nargs = TREE_VEC_LENGTH (targs);
22350 tempargs = make_tree_vec (nargs);
22351
22352 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22353 return 0;
22354
22355 /* First make sure we didn't deduce anything that conflicts with
22356 explicitly specified args. */
22357 for (i = nargs; i--; )
22358 {
22359 tree elt = TREE_VEC_ELT (tempargs, i);
22360 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22361
22362 if (!elt)
22363 /*NOP*/;
22364 else if (uses_template_parms (elt))
22365 /* Since we're unifying against ourselves, we will fill in
22366 template args used in the function parm list with our own
22367 template parms. Discard them. */
22368 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22369 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22370 {
22371 /* Check that the argument at each index of the deduced argument pack
22372 is equivalent to the corresponding explicitly specified argument.
22373 We may have deduced more arguments than were explicitly specified,
22374 and that's OK. */
22375
22376 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22377 that's wrong if we deduce the same argument pack from multiple
22378 function arguments: it's only incomplete the first time. */
22379
22380 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22381 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22382
22383 if (TREE_VEC_LENGTH (deduced_pack)
22384 < TREE_VEC_LENGTH (explicit_pack))
22385 return 0;
22386
22387 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22388 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22389 TREE_VEC_ELT (deduced_pack, j)))
22390 return 0;
22391 }
22392 else if (oldelt && !template_args_equal (oldelt, elt))
22393 return 0;
22394 }
22395
22396 for (i = nargs; i--; )
22397 {
22398 tree elt = TREE_VEC_ELT (tempargs, i);
22399
22400 if (elt)
22401 TREE_VEC_ELT (targs, i) = elt;
22402 }
22403
22404 return 1;
22405 }
22406
22407 /* PARM is a template class (perhaps with unbound template
22408 parameters). ARG is a fully instantiated type. If ARG can be
22409 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22410 TARGS are as for unify. */
22411
22412 static tree
22413 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22414 bool explain_p)
22415 {
22416 tree copy_of_targs;
22417
22418 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22419 return NULL_TREE;
22420 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22421 /* Matches anything. */;
22422 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22423 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22424 return NULL_TREE;
22425
22426 /* We need to make a new template argument vector for the call to
22427 unify. If we used TARGS, we'd clutter it up with the result of
22428 the attempted unification, even if this class didn't work out.
22429 We also don't want to commit ourselves to all the unifications
22430 we've already done, since unification is supposed to be done on
22431 an argument-by-argument basis. In other words, consider the
22432 following pathological case:
22433
22434 template <int I, int J, int K>
22435 struct S {};
22436
22437 template <int I, int J>
22438 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22439
22440 template <int I, int J, int K>
22441 void f(S<I, J, K>, S<I, I, I>);
22442
22443 void g() {
22444 S<0, 0, 0> s0;
22445 S<0, 1, 2> s2;
22446
22447 f(s0, s2);
22448 }
22449
22450 Now, by the time we consider the unification involving `s2', we
22451 already know that we must have `f<0, 0, 0>'. But, even though
22452 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22453 because there are two ways to unify base classes of S<0, 1, 2>
22454 with S<I, I, I>. If we kept the already deduced knowledge, we
22455 would reject the possibility I=1. */
22456 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22457
22458 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22459 {
22460 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22461 return NULL_TREE;
22462 return arg;
22463 }
22464
22465 /* If unification failed, we're done. */
22466 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22467 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22468 return NULL_TREE;
22469
22470 return arg;
22471 }
22472
22473 /* Given a template type PARM and a class type ARG, find the unique
22474 base type in ARG that is an instance of PARM. We do not examine
22475 ARG itself; only its base-classes. If there is not exactly one
22476 appropriate base class, return NULL_TREE. PARM may be the type of
22477 a partial specialization, as well as a plain template type. Used
22478 by unify. */
22479
22480 static enum template_base_result
22481 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22482 bool explain_p, tree *result)
22483 {
22484 tree rval = NULL_TREE;
22485 tree binfo;
22486
22487 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22488
22489 binfo = TYPE_BINFO (complete_type (arg));
22490 if (!binfo)
22491 {
22492 /* The type could not be completed. */
22493 *result = NULL_TREE;
22494 return tbr_incomplete_type;
22495 }
22496
22497 /* Walk in inheritance graph order. The search order is not
22498 important, and this avoids multiple walks of virtual bases. */
22499 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22500 {
22501 tree r = try_class_unification (tparms, targs, parm,
22502 BINFO_TYPE (binfo), explain_p);
22503
22504 if (r)
22505 {
22506 /* If there is more than one satisfactory baseclass, then:
22507
22508 [temp.deduct.call]
22509
22510 If they yield more than one possible deduced A, the type
22511 deduction fails.
22512
22513 applies. */
22514 if (rval && !same_type_p (r, rval))
22515 {
22516 *result = NULL_TREE;
22517 return tbr_ambiguous_baseclass;
22518 }
22519
22520 rval = r;
22521 }
22522 }
22523
22524 *result = rval;
22525 return tbr_success;
22526 }
22527
22528 /* Returns the level of DECL, which declares a template parameter. */
22529
22530 static int
22531 template_decl_level (tree decl)
22532 {
22533 switch (TREE_CODE (decl))
22534 {
22535 case TYPE_DECL:
22536 case TEMPLATE_DECL:
22537 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22538
22539 case PARM_DECL:
22540 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22541
22542 default:
22543 gcc_unreachable ();
22544 }
22545 return 0;
22546 }
22547
22548 /* Decide whether ARG can be unified with PARM, considering only the
22549 cv-qualifiers of each type, given STRICT as documented for unify.
22550 Returns nonzero iff the unification is OK on that basis. */
22551
22552 static int
22553 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22554 {
22555 int arg_quals = cp_type_quals (arg);
22556 int parm_quals = cp_type_quals (parm);
22557
22558 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22559 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22560 {
22561 /* Although a CVR qualifier is ignored when being applied to a
22562 substituted template parameter ([8.3.2]/1 for example), that
22563 does not allow us to unify "const T" with "int&" because both
22564 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22565 It is ok when we're allowing additional CV qualifiers
22566 at the outer level [14.8.2.1]/3,1st bullet. */
22567 if ((TYPE_REF_P (arg)
22568 || FUNC_OR_METHOD_TYPE_P (arg))
22569 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22570 return 0;
22571
22572 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22573 && (parm_quals & TYPE_QUAL_RESTRICT))
22574 return 0;
22575 }
22576
22577 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22578 && (arg_quals & parm_quals) != parm_quals)
22579 return 0;
22580
22581 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22582 && (parm_quals & arg_quals) != arg_quals)
22583 return 0;
22584
22585 return 1;
22586 }
22587
22588 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22589 void
22590 template_parm_level_and_index (tree parm, int* level, int* index)
22591 {
22592 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22593 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22594 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22595 {
22596 *index = TEMPLATE_TYPE_IDX (parm);
22597 *level = TEMPLATE_TYPE_LEVEL (parm);
22598 }
22599 else
22600 {
22601 *index = TEMPLATE_PARM_IDX (parm);
22602 *level = TEMPLATE_PARM_LEVEL (parm);
22603 }
22604 }
22605
22606 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22607 do { \
22608 if (unify (TP, TA, P, A, S, EP)) \
22609 return 1; \
22610 } while (0)
22611
22612 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22613 expansion at the end of PACKED_PARMS. Returns 0 if the type
22614 deduction succeeds, 1 otherwise. STRICT is the same as in
22615 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22616 function call argument list. We'll need to adjust the arguments to make them
22617 types. SUBR tells us if this is from a recursive call to
22618 type_unification_real, or for comparing two template argument
22619 lists. */
22620
22621 static int
22622 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22623 tree packed_args, unification_kind_t strict,
22624 bool subr, bool explain_p)
22625 {
22626 tree parm
22627 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22628 tree pattern = PACK_EXPANSION_PATTERN (parm);
22629 tree pack, packs = NULL_TREE;
22630 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22631
22632 /* Add in any args remembered from an earlier partial instantiation. */
22633 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22634 int levels = TMPL_ARGS_DEPTH (targs);
22635
22636 packed_args = expand_template_argument_pack (packed_args);
22637
22638 int len = TREE_VEC_LENGTH (packed_args);
22639
22640 /* Determine the parameter packs we will be deducing from the
22641 pattern, and record their current deductions. */
22642 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22643 pack; pack = TREE_CHAIN (pack))
22644 {
22645 tree parm_pack = TREE_VALUE (pack);
22646 int idx, level;
22647
22648 /* Only template parameter packs can be deduced, not e.g. function
22649 parameter packs or __bases or __integer_pack. */
22650 if (!TEMPLATE_PARM_P (parm_pack))
22651 continue;
22652
22653 /* Determine the index and level of this parameter pack. */
22654 template_parm_level_and_index (parm_pack, &level, &idx);
22655 if (level < levels)
22656 continue;
22657
22658 /* Keep track of the parameter packs and their corresponding
22659 argument packs. */
22660 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22661 TREE_TYPE (packs) = make_tree_vec (len - start);
22662 }
22663
22664 /* Loop through all of the arguments that have not yet been
22665 unified and unify each with the pattern. */
22666 for (i = start; i < len; i++)
22667 {
22668 tree parm;
22669 bool any_explicit = false;
22670 tree arg = TREE_VEC_ELT (packed_args, i);
22671
22672 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22673 or the element of its argument pack at the current index if
22674 this argument was explicitly specified. */
22675 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22676 {
22677 int idx, level;
22678 tree arg, pargs;
22679 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22680
22681 arg = NULL_TREE;
22682 if (TREE_VALUE (pack)
22683 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22684 && (i - start < TREE_VEC_LENGTH (pargs)))
22685 {
22686 any_explicit = true;
22687 arg = TREE_VEC_ELT (pargs, i - start);
22688 }
22689 TMPL_ARG (targs, level, idx) = arg;
22690 }
22691
22692 /* If we had explicit template arguments, substitute them into the
22693 pattern before deduction. */
22694 if (any_explicit)
22695 {
22696 /* Some arguments might still be unspecified or dependent. */
22697 bool dependent;
22698 ++processing_template_decl;
22699 dependent = any_dependent_template_arguments_p (targs);
22700 if (!dependent)
22701 --processing_template_decl;
22702 parm = tsubst (pattern, targs,
22703 explain_p ? tf_warning_or_error : tf_none,
22704 NULL_TREE);
22705 if (dependent)
22706 --processing_template_decl;
22707 if (parm == error_mark_node)
22708 return 1;
22709 }
22710 else
22711 parm = pattern;
22712
22713 /* Unify the pattern with the current argument. */
22714 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22715 explain_p))
22716 return 1;
22717
22718 /* For each parameter pack, collect the deduced value. */
22719 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22720 {
22721 int idx, level;
22722 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22723
22724 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22725 TMPL_ARG (targs, level, idx);
22726 }
22727 }
22728
22729 /* Verify that the results of unification with the parameter packs
22730 produce results consistent with what we've seen before, and make
22731 the deduced argument packs available. */
22732 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22733 {
22734 tree old_pack = TREE_VALUE (pack);
22735 tree new_args = TREE_TYPE (pack);
22736 int i, len = TREE_VEC_LENGTH (new_args);
22737 int idx, level;
22738 bool nondeduced_p = false;
22739
22740 /* By default keep the original deduced argument pack.
22741 If necessary, more specific code is going to update the
22742 resulting deduced argument later down in this function. */
22743 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22744 TMPL_ARG (targs, level, idx) = old_pack;
22745
22746 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22747 actually deduce anything. */
22748 for (i = 0; i < len && !nondeduced_p; ++i)
22749 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22750 nondeduced_p = true;
22751 if (nondeduced_p)
22752 continue;
22753
22754 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22755 {
22756 /* If we had fewer function args than explicit template args,
22757 just use the explicits. */
22758 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22759 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22760 if (len < explicit_len)
22761 new_args = explicit_args;
22762 }
22763
22764 if (!old_pack)
22765 {
22766 tree result;
22767 /* Build the deduced *_ARGUMENT_PACK. */
22768 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22769 {
22770 result = make_node (NONTYPE_ARGUMENT_PACK);
22771 TREE_CONSTANT (result) = 1;
22772 }
22773 else
22774 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22775
22776 SET_ARGUMENT_PACK_ARGS (result, new_args);
22777
22778 /* Note the deduced argument packs for this parameter
22779 pack. */
22780 TMPL_ARG (targs, level, idx) = result;
22781 }
22782 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22783 && (ARGUMENT_PACK_ARGS (old_pack)
22784 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22785 {
22786 /* We only had the explicitly-provided arguments before, but
22787 now we have a complete set of arguments. */
22788 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22789
22790 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22791 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22792 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22793 }
22794 else
22795 {
22796 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22797 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22798
22799 if (!comp_template_args (old_args, new_args,
22800 &bad_old_arg, &bad_new_arg))
22801 /* Inconsistent unification of this parameter pack. */
22802 return unify_parameter_pack_inconsistent (explain_p,
22803 bad_old_arg,
22804 bad_new_arg);
22805 }
22806 }
22807
22808 return unify_success (explain_p);
22809 }
22810
22811 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22812 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22813 parameters and return value are as for unify. */
22814
22815 static int
22816 unify_array_domain (tree tparms, tree targs,
22817 tree parm_dom, tree arg_dom,
22818 bool explain_p)
22819 {
22820 tree parm_max;
22821 tree arg_max;
22822 bool parm_cst;
22823 bool arg_cst;
22824
22825 /* Our representation of array types uses "N - 1" as the
22826 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22827 not an integer constant. We cannot unify arbitrarily
22828 complex expressions, so we eliminate the MINUS_EXPRs
22829 here. */
22830 parm_max = TYPE_MAX_VALUE (parm_dom);
22831 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22832 if (!parm_cst)
22833 {
22834 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22835 parm_max = TREE_OPERAND (parm_max, 0);
22836 }
22837 arg_max = TYPE_MAX_VALUE (arg_dom);
22838 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22839 if (!arg_cst)
22840 {
22841 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22842 trying to unify the type of a variable with the type
22843 of a template parameter. For example:
22844
22845 template <unsigned int N>
22846 void f (char (&) [N]);
22847 int g();
22848 void h(int i) {
22849 char a[g(i)];
22850 f(a);
22851 }
22852
22853 Here, the type of the ARG will be "int [g(i)]", and
22854 may be a SAVE_EXPR, etc. */
22855 if (TREE_CODE (arg_max) != MINUS_EXPR)
22856 return unify_vla_arg (explain_p, arg_dom);
22857 arg_max = TREE_OPERAND (arg_max, 0);
22858 }
22859
22860 /* If only one of the bounds used a MINUS_EXPR, compensate
22861 by adding one to the other bound. */
22862 if (parm_cst && !arg_cst)
22863 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22864 integer_type_node,
22865 parm_max,
22866 integer_one_node);
22867 else if (arg_cst && !parm_cst)
22868 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22869 integer_type_node,
22870 arg_max,
22871 integer_one_node);
22872
22873 return unify (tparms, targs, parm_max, arg_max,
22874 UNIFY_ALLOW_INTEGER, explain_p);
22875 }
22876
22877 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22878
22879 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22880
22881 static pa_kind_t
22882 pa_kind (tree t)
22883 {
22884 if (PACK_EXPANSION_P (t))
22885 t = PACK_EXPANSION_PATTERN (t);
22886 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22887 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22888 || DECL_TYPE_TEMPLATE_P (t))
22889 return pa_tmpl;
22890 else if (TYPE_P (t))
22891 return pa_type;
22892 else
22893 return pa_expr;
22894 }
22895
22896 /* Deduce the value of template parameters. TPARMS is the (innermost)
22897 set of template parameters to a template. TARGS is the bindings
22898 for those template parameters, as determined thus far; TARGS may
22899 include template arguments for outer levels of template parameters
22900 as well. PARM is a parameter to a template function, or a
22901 subcomponent of that parameter; ARG is the corresponding argument.
22902 This function attempts to match PARM with ARG in a manner
22903 consistent with the existing assignments in TARGS. If more values
22904 are deduced, then TARGS is updated.
22905
22906 Returns 0 if the type deduction succeeds, 1 otherwise. The
22907 parameter STRICT is a bitwise or of the following flags:
22908
22909 UNIFY_ALLOW_NONE:
22910 Require an exact match between PARM and ARG.
22911 UNIFY_ALLOW_MORE_CV_QUAL:
22912 Allow the deduced ARG to be more cv-qualified (by qualification
22913 conversion) than ARG.
22914 UNIFY_ALLOW_LESS_CV_QUAL:
22915 Allow the deduced ARG to be less cv-qualified than ARG.
22916 UNIFY_ALLOW_DERIVED:
22917 Allow the deduced ARG to be a template base class of ARG,
22918 or a pointer to a template base class of the type pointed to by
22919 ARG.
22920 UNIFY_ALLOW_INTEGER:
22921 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22922 case for more information.
22923 UNIFY_ALLOW_OUTER_LEVEL:
22924 This is the outermost level of a deduction. Used to determine validity
22925 of qualification conversions. A valid qualification conversion must
22926 have const qualified pointers leading up to the inner type which
22927 requires additional CV quals, except at the outer level, where const
22928 is not required [conv.qual]. It would be normal to set this flag in
22929 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22930 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22931 This is the outermost level of a deduction, and PARM can be more CV
22932 qualified at this point.
22933 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22934 This is the outermost level of a deduction, and PARM can be less CV
22935 qualified at this point. */
22936
22937 static int
22938 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22939 bool explain_p)
22940 {
22941 int idx;
22942 tree targ;
22943 tree tparm;
22944 int strict_in = strict;
22945 tsubst_flags_t complain = (explain_p
22946 ? tf_warning_or_error
22947 : tf_none);
22948
22949 /* I don't think this will do the right thing with respect to types.
22950 But the only case I've seen it in so far has been array bounds, where
22951 signedness is the only information lost, and I think that will be
22952 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22953 finish_id_expression_1, and are also OK. */
22954 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
22955 parm = TREE_OPERAND (parm, 0);
22956
22957 if (arg == error_mark_node)
22958 return unify_invalid (explain_p);
22959 if (arg == unknown_type_node
22960 || arg == init_list_type_node)
22961 /* We can't deduce anything from this, but we might get all the
22962 template args from other function args. */
22963 return unify_success (explain_p);
22964
22965 if (parm == any_targ_node || arg == any_targ_node)
22966 return unify_success (explain_p);
22967
22968 /* If PARM uses template parameters, then we can't bail out here,
22969 even if ARG == PARM, since we won't record unifications for the
22970 template parameters. We might need them if we're trying to
22971 figure out which of two things is more specialized. */
22972 if (arg == parm && !uses_template_parms (parm))
22973 return unify_success (explain_p);
22974
22975 /* Handle init lists early, so the rest of the function can assume
22976 we're dealing with a type. */
22977 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22978 {
22979 tree elt, elttype;
22980 unsigned i;
22981 tree orig_parm = parm;
22982
22983 if (!is_std_init_list (parm)
22984 && TREE_CODE (parm) != ARRAY_TYPE)
22985 /* We can only deduce from an initializer list argument if the
22986 parameter is std::initializer_list or an array; otherwise this
22987 is a non-deduced context. */
22988 return unify_success (explain_p);
22989
22990 if (TREE_CODE (parm) == ARRAY_TYPE)
22991 elttype = TREE_TYPE (parm);
22992 else
22993 {
22994 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22995 /* Deduction is defined in terms of a single type, so just punt
22996 on the (bizarre) std::initializer_list<T...>. */
22997 if (PACK_EXPANSION_P (elttype))
22998 return unify_success (explain_p);
22999 }
23000
23001 if (strict != DEDUCE_EXACT
23002 && TYPE_P (elttype)
23003 && !uses_deducible_template_parms (elttype))
23004 /* If ELTTYPE has no deducible template parms, skip deduction from
23005 the list elements. */;
23006 else
23007 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23008 {
23009 int elt_strict = strict;
23010
23011 if (elt == error_mark_node)
23012 return unify_invalid (explain_p);
23013
23014 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23015 {
23016 tree type = TREE_TYPE (elt);
23017 if (type == error_mark_node)
23018 return unify_invalid (explain_p);
23019 /* It should only be possible to get here for a call. */
23020 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23021 elt_strict |= maybe_adjust_types_for_deduction
23022 (DEDUCE_CALL, &elttype, &type, elt);
23023 elt = type;
23024 }
23025
23026 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23027 explain_p);
23028 }
23029
23030 if (TREE_CODE (parm) == ARRAY_TYPE
23031 && deducible_array_bound (TYPE_DOMAIN (parm)))
23032 {
23033 /* Also deduce from the length of the initializer list. */
23034 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23035 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23036 if (idx == error_mark_node)
23037 return unify_invalid (explain_p);
23038 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23039 idx, explain_p);
23040 }
23041
23042 /* If the std::initializer_list<T> deduction worked, replace the
23043 deduced A with std::initializer_list<A>. */
23044 if (orig_parm != parm)
23045 {
23046 idx = TEMPLATE_TYPE_IDX (orig_parm);
23047 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23048 targ = listify (targ);
23049 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23050 }
23051 return unify_success (explain_p);
23052 }
23053
23054 /* If parm and arg aren't the same kind of thing (template, type, or
23055 expression), fail early. */
23056 if (pa_kind (parm) != pa_kind (arg))
23057 return unify_invalid (explain_p);
23058
23059 /* Immediately reject some pairs that won't unify because of
23060 cv-qualification mismatches. */
23061 if (TREE_CODE (arg) == TREE_CODE (parm)
23062 && TYPE_P (arg)
23063 /* It is the elements of the array which hold the cv quals of an array
23064 type, and the elements might be template type parms. We'll check
23065 when we recurse. */
23066 && TREE_CODE (arg) != ARRAY_TYPE
23067 /* We check the cv-qualifiers when unifying with template type
23068 parameters below. We want to allow ARG `const T' to unify with
23069 PARM `T' for example, when computing which of two templates
23070 is more specialized, for example. */
23071 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23072 && !check_cv_quals_for_unify (strict_in, arg, parm))
23073 return unify_cv_qual_mismatch (explain_p, parm, arg);
23074
23075 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23076 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23077 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23078 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23079 strict &= ~UNIFY_ALLOW_DERIVED;
23080 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23081 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23082
23083 switch (TREE_CODE (parm))
23084 {
23085 case TYPENAME_TYPE:
23086 case SCOPE_REF:
23087 case UNBOUND_CLASS_TEMPLATE:
23088 /* In a type which contains a nested-name-specifier, template
23089 argument values cannot be deduced for template parameters used
23090 within the nested-name-specifier. */
23091 return unify_success (explain_p);
23092
23093 case TEMPLATE_TYPE_PARM:
23094 case TEMPLATE_TEMPLATE_PARM:
23095 case BOUND_TEMPLATE_TEMPLATE_PARM:
23096 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23097 if (error_operand_p (tparm))
23098 return unify_invalid (explain_p);
23099
23100 if (TEMPLATE_TYPE_LEVEL (parm)
23101 != template_decl_level (tparm))
23102 /* The PARM is not one we're trying to unify. Just check
23103 to see if it matches ARG. */
23104 {
23105 if (TREE_CODE (arg) == TREE_CODE (parm)
23106 && (is_auto (parm) ? is_auto (arg)
23107 : same_type_p (parm, arg)))
23108 return unify_success (explain_p);
23109 else
23110 return unify_type_mismatch (explain_p, parm, arg);
23111 }
23112 idx = TEMPLATE_TYPE_IDX (parm);
23113 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23114 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23115 if (error_operand_p (tparm))
23116 return unify_invalid (explain_p);
23117
23118 /* Check for mixed types and values. */
23119 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23120 && TREE_CODE (tparm) != TYPE_DECL)
23121 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23122 && TREE_CODE (tparm) != TEMPLATE_DECL))
23123 gcc_unreachable ();
23124
23125 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23126 {
23127 if ((strict_in & UNIFY_ALLOW_DERIVED)
23128 && CLASS_TYPE_P (arg))
23129 {
23130 /* First try to match ARG directly. */
23131 tree t = try_class_unification (tparms, targs, parm, arg,
23132 explain_p);
23133 if (!t)
23134 {
23135 /* Otherwise, look for a suitable base of ARG, as below. */
23136 enum template_base_result r;
23137 r = get_template_base (tparms, targs, parm, arg,
23138 explain_p, &t);
23139 if (!t)
23140 return unify_no_common_base (explain_p, r, parm, arg);
23141 arg = t;
23142 }
23143 }
23144 /* ARG must be constructed from a template class or a template
23145 template parameter. */
23146 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23147 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23148 return unify_template_deduction_failure (explain_p, parm, arg);
23149
23150 /* Deduce arguments T, i from TT<T> or TT<i>. */
23151 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23152 return 1;
23153
23154 arg = TYPE_TI_TEMPLATE (arg);
23155
23156 /* Fall through to deduce template name. */
23157 }
23158
23159 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23160 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23161 {
23162 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23163
23164 /* Simple cases: Value already set, does match or doesn't. */
23165 if (targ != NULL_TREE && template_args_equal (targ, arg))
23166 return unify_success (explain_p);
23167 else if (targ)
23168 return unify_inconsistency (explain_p, parm, targ, arg);
23169 }
23170 else
23171 {
23172 /* If PARM is `const T' and ARG is only `int', we don't have
23173 a match unless we are allowing additional qualification.
23174 If ARG is `const int' and PARM is just `T' that's OK;
23175 that binds `const int' to `T'. */
23176 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23177 arg, parm))
23178 return unify_cv_qual_mismatch (explain_p, parm, arg);
23179
23180 /* Consider the case where ARG is `const volatile int' and
23181 PARM is `const T'. Then, T should be `volatile int'. */
23182 arg = cp_build_qualified_type_real
23183 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23184 if (arg == error_mark_node)
23185 return unify_invalid (explain_p);
23186
23187 /* Simple cases: Value already set, does match or doesn't. */
23188 if (targ != NULL_TREE && same_type_p (targ, arg))
23189 return unify_success (explain_p);
23190 else if (targ)
23191 return unify_inconsistency (explain_p, parm, targ, arg);
23192
23193 /* Make sure that ARG is not a variable-sized array. (Note
23194 that were talking about variable-sized arrays (like
23195 `int[n]'), rather than arrays of unknown size (like
23196 `int[]').) We'll get very confused by such a type since
23197 the bound of the array is not constant, and therefore
23198 not mangleable. Besides, such types are not allowed in
23199 ISO C++, so we can do as we please here. We do allow
23200 them for 'auto' deduction, since that isn't ABI-exposed. */
23201 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23202 return unify_vla_arg (explain_p, arg);
23203
23204 /* Strip typedefs as in convert_template_argument. */
23205 arg = canonicalize_type_argument (arg, tf_none);
23206 }
23207
23208 /* If ARG is a parameter pack or an expansion, we cannot unify
23209 against it unless PARM is also a parameter pack. */
23210 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23211 && !template_parameter_pack_p (parm))
23212 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23213
23214 /* If the argument deduction results is a METHOD_TYPE,
23215 then there is a problem.
23216 METHOD_TYPE doesn't map to any real C++ type the result of
23217 the deduction cannot be of that type. */
23218 if (TREE_CODE (arg) == METHOD_TYPE)
23219 return unify_method_type_error (explain_p, arg);
23220
23221 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23222 return unify_success (explain_p);
23223
23224 case TEMPLATE_PARM_INDEX:
23225 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23226 if (error_operand_p (tparm))
23227 return unify_invalid (explain_p);
23228
23229 if (TEMPLATE_PARM_LEVEL (parm)
23230 != template_decl_level (tparm))
23231 {
23232 /* The PARM is not one we're trying to unify. Just check
23233 to see if it matches ARG. */
23234 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23235 && cp_tree_equal (parm, arg));
23236 if (result)
23237 unify_expression_unequal (explain_p, parm, arg);
23238 return result;
23239 }
23240
23241 idx = TEMPLATE_PARM_IDX (parm);
23242 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23243
23244 if (targ)
23245 {
23246 if ((strict & UNIFY_ALLOW_INTEGER)
23247 && TREE_TYPE (targ) && TREE_TYPE (arg)
23248 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23249 /* We're deducing from an array bound, the type doesn't matter. */
23250 arg = fold_convert (TREE_TYPE (targ), arg);
23251 int x = !cp_tree_equal (targ, arg);
23252 if (x)
23253 unify_inconsistency (explain_p, parm, targ, arg);
23254 return x;
23255 }
23256
23257 /* [temp.deduct.type] If, in the declaration of a function template
23258 with a non-type template-parameter, the non-type
23259 template-parameter is used in an expression in the function
23260 parameter-list and, if the corresponding template-argument is
23261 deduced, the template-argument type shall match the type of the
23262 template-parameter exactly, except that a template-argument
23263 deduced from an array bound may be of any integral type.
23264 The non-type parameter might use already deduced type parameters. */
23265 tparm = TREE_TYPE (parm);
23266 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23267 /* We don't have enough levels of args to do any substitution. This
23268 can happen in the context of -fnew-ttp-matching. */;
23269 else
23270 {
23271 ++processing_template_decl;
23272 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23273 --processing_template_decl;
23274
23275 if (tree a = type_uses_auto (tparm))
23276 {
23277 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
23278 if (tparm == error_mark_node)
23279 return 1;
23280 }
23281 }
23282
23283 if (!TREE_TYPE (arg))
23284 /* Template-parameter dependent expression. Just accept it for now.
23285 It will later be processed in convert_template_argument. */
23286 ;
23287 else if (same_type_ignoring_top_level_qualifiers_p
23288 (non_reference (TREE_TYPE (arg)),
23289 non_reference (tparm)))
23290 /* OK. Ignore top-level quals here because a class-type template
23291 parameter object is const. */;
23292 else if ((strict & UNIFY_ALLOW_INTEGER)
23293 && CP_INTEGRAL_TYPE_P (tparm))
23294 /* Convert the ARG to the type of PARM; the deduced non-type
23295 template argument must exactly match the types of the
23296 corresponding parameter. */
23297 arg = fold (build_nop (tparm, arg));
23298 else if (uses_template_parms (tparm))
23299 {
23300 /* We haven't deduced the type of this parameter yet. */
23301 if (cxx_dialect >= cxx17
23302 /* We deduce from array bounds in try_array_deduction. */
23303 && !(strict & UNIFY_ALLOW_INTEGER))
23304 {
23305 /* Deduce it from the non-type argument. */
23306 tree atype = TREE_TYPE (arg);
23307 RECUR_AND_CHECK_FAILURE (tparms, targs,
23308 tparm, atype,
23309 UNIFY_ALLOW_NONE, explain_p);
23310 }
23311 else
23312 /* Try again later. */
23313 return unify_success (explain_p);
23314 }
23315 else
23316 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23317
23318 /* If ARG is a parameter pack or an expansion, we cannot unify
23319 against it unless PARM is also a parameter pack. */
23320 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23321 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23322 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23323
23324 {
23325 bool removed_attr = false;
23326 arg = strip_typedefs_expr (arg, &removed_attr);
23327 }
23328 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23329 return unify_success (explain_p);
23330
23331 case PTRMEM_CST:
23332 {
23333 /* A pointer-to-member constant can be unified only with
23334 another constant. */
23335 if (TREE_CODE (arg) != PTRMEM_CST)
23336 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23337
23338 /* Just unify the class member. It would be useless (and possibly
23339 wrong, depending on the strict flags) to unify also
23340 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23341 arg refer to the same variable, even if through different
23342 classes. For instance:
23343
23344 struct A { int x; };
23345 struct B : A { };
23346
23347 Unification of &A::x and &B::x must succeed. */
23348 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23349 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23350 }
23351
23352 case POINTER_TYPE:
23353 {
23354 if (!TYPE_PTR_P (arg))
23355 return unify_type_mismatch (explain_p, parm, arg);
23356
23357 /* [temp.deduct.call]
23358
23359 A can be another pointer or pointer to member type that can
23360 be converted to the deduced A via a qualification
23361 conversion (_conv.qual_).
23362
23363 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23364 This will allow for additional cv-qualification of the
23365 pointed-to types if appropriate. */
23366
23367 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23368 /* The derived-to-base conversion only persists through one
23369 level of pointers. */
23370 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23371
23372 return unify (tparms, targs, TREE_TYPE (parm),
23373 TREE_TYPE (arg), strict, explain_p);
23374 }
23375
23376 case REFERENCE_TYPE:
23377 if (!TYPE_REF_P (arg))
23378 return unify_type_mismatch (explain_p, parm, arg);
23379 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23380 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23381
23382 case ARRAY_TYPE:
23383 if (TREE_CODE (arg) != ARRAY_TYPE)
23384 return unify_type_mismatch (explain_p, parm, arg);
23385 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23386 != (TYPE_DOMAIN (arg) == NULL_TREE))
23387 return unify_type_mismatch (explain_p, parm, arg);
23388 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23389 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23390 if (TYPE_DOMAIN (parm) != NULL_TREE)
23391 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23392 TYPE_DOMAIN (arg), explain_p);
23393 return unify_success (explain_p);
23394
23395 case REAL_TYPE:
23396 case COMPLEX_TYPE:
23397 case VECTOR_TYPE:
23398 case INTEGER_TYPE:
23399 case BOOLEAN_TYPE:
23400 case ENUMERAL_TYPE:
23401 case VOID_TYPE:
23402 case NULLPTR_TYPE:
23403 if (TREE_CODE (arg) != TREE_CODE (parm))
23404 return unify_type_mismatch (explain_p, parm, arg);
23405
23406 /* We have already checked cv-qualification at the top of the
23407 function. */
23408 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23409 return unify_type_mismatch (explain_p, parm, arg);
23410
23411 /* As far as unification is concerned, this wins. Later checks
23412 will invalidate it if necessary. */
23413 return unify_success (explain_p);
23414
23415 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23416 /* Type INTEGER_CST can come from ordinary constant template args. */
23417 case INTEGER_CST:
23418 while (CONVERT_EXPR_P (arg))
23419 arg = TREE_OPERAND (arg, 0);
23420
23421 if (TREE_CODE (arg) != INTEGER_CST)
23422 return unify_template_argument_mismatch (explain_p, parm, arg);
23423 return (tree_int_cst_equal (parm, arg)
23424 ? unify_success (explain_p)
23425 : unify_template_argument_mismatch (explain_p, parm, arg));
23426
23427 case TREE_VEC:
23428 {
23429 int i, len, argslen;
23430 int parm_variadic_p = 0;
23431
23432 if (TREE_CODE (arg) != TREE_VEC)
23433 return unify_template_argument_mismatch (explain_p, parm, arg);
23434
23435 len = TREE_VEC_LENGTH (parm);
23436 argslen = TREE_VEC_LENGTH (arg);
23437
23438 /* Check for pack expansions in the parameters. */
23439 for (i = 0; i < len; ++i)
23440 {
23441 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23442 {
23443 if (i == len - 1)
23444 /* We can unify against something with a trailing
23445 parameter pack. */
23446 parm_variadic_p = 1;
23447 else
23448 /* [temp.deduct.type]/9: If the template argument list of
23449 P contains a pack expansion that is not the last
23450 template argument, the entire template argument list
23451 is a non-deduced context. */
23452 return unify_success (explain_p);
23453 }
23454 }
23455
23456 /* If we don't have enough arguments to satisfy the parameters
23457 (not counting the pack expression at the end), or we have
23458 too many arguments for a parameter list that doesn't end in
23459 a pack expression, we can't unify. */
23460 if (parm_variadic_p
23461 ? argslen < len - parm_variadic_p
23462 : argslen != len)
23463 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23464
23465 /* Unify all of the parameters that precede the (optional)
23466 pack expression. */
23467 for (i = 0; i < len - parm_variadic_p; ++i)
23468 {
23469 RECUR_AND_CHECK_FAILURE (tparms, targs,
23470 TREE_VEC_ELT (parm, i),
23471 TREE_VEC_ELT (arg, i),
23472 UNIFY_ALLOW_NONE, explain_p);
23473 }
23474 if (parm_variadic_p)
23475 return unify_pack_expansion (tparms, targs, parm, arg,
23476 DEDUCE_EXACT,
23477 /*subr=*/true, explain_p);
23478 return unify_success (explain_p);
23479 }
23480
23481 case RECORD_TYPE:
23482 case UNION_TYPE:
23483 if (TREE_CODE (arg) != TREE_CODE (parm))
23484 return unify_type_mismatch (explain_p, parm, arg);
23485
23486 if (TYPE_PTRMEMFUNC_P (parm))
23487 {
23488 if (!TYPE_PTRMEMFUNC_P (arg))
23489 return unify_type_mismatch (explain_p, parm, arg);
23490
23491 return unify (tparms, targs,
23492 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23493 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23494 strict, explain_p);
23495 }
23496 else if (TYPE_PTRMEMFUNC_P (arg))
23497 return unify_type_mismatch (explain_p, parm, arg);
23498
23499 if (CLASSTYPE_TEMPLATE_INFO (parm))
23500 {
23501 tree t = NULL_TREE;
23502
23503 if (strict_in & UNIFY_ALLOW_DERIVED)
23504 {
23505 /* First, we try to unify the PARM and ARG directly. */
23506 t = try_class_unification (tparms, targs,
23507 parm, arg, explain_p);
23508
23509 if (!t)
23510 {
23511 /* Fallback to the special case allowed in
23512 [temp.deduct.call]:
23513
23514 If P is a class, and P has the form
23515 template-id, then A can be a derived class of
23516 the deduced A. Likewise, if P is a pointer to
23517 a class of the form template-id, A can be a
23518 pointer to a derived class pointed to by the
23519 deduced A. */
23520 enum template_base_result r;
23521 r = get_template_base (tparms, targs, parm, arg,
23522 explain_p, &t);
23523
23524 if (!t)
23525 {
23526 /* Don't give the derived diagnostic if we're
23527 already dealing with the same template. */
23528 bool same_template
23529 = (CLASSTYPE_TEMPLATE_INFO (arg)
23530 && (CLASSTYPE_TI_TEMPLATE (parm)
23531 == CLASSTYPE_TI_TEMPLATE (arg)));
23532 return unify_no_common_base (explain_p && !same_template,
23533 r, parm, arg);
23534 }
23535 }
23536 }
23537 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23538 && (CLASSTYPE_TI_TEMPLATE (parm)
23539 == CLASSTYPE_TI_TEMPLATE (arg)))
23540 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23541 Then, we should unify `int' and `U'. */
23542 t = arg;
23543 else
23544 /* There's no chance of unification succeeding. */
23545 return unify_type_mismatch (explain_p, parm, arg);
23546
23547 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23548 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23549 }
23550 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23551 return unify_type_mismatch (explain_p, parm, arg);
23552 return unify_success (explain_p);
23553
23554 case METHOD_TYPE:
23555 case FUNCTION_TYPE:
23556 {
23557 unsigned int nargs;
23558 tree *args;
23559 tree a;
23560 unsigned int i;
23561
23562 if (TREE_CODE (arg) != TREE_CODE (parm))
23563 return unify_type_mismatch (explain_p, parm, arg);
23564
23565 /* CV qualifications for methods can never be deduced, they must
23566 match exactly. We need to check them explicitly here,
23567 because type_unification_real treats them as any other
23568 cv-qualified parameter. */
23569 if (TREE_CODE (parm) == METHOD_TYPE
23570 && (!check_cv_quals_for_unify
23571 (UNIFY_ALLOW_NONE,
23572 class_of_this_parm (arg),
23573 class_of_this_parm (parm))))
23574 return unify_cv_qual_mismatch (explain_p, parm, arg);
23575 if (TREE_CODE (arg) == FUNCTION_TYPE
23576 && type_memfn_quals (parm) != type_memfn_quals (arg))
23577 return unify_cv_qual_mismatch (explain_p, parm, arg);
23578 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23579 return unify_type_mismatch (explain_p, parm, arg);
23580
23581 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23582 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23583
23584 nargs = list_length (TYPE_ARG_TYPES (arg));
23585 args = XALLOCAVEC (tree, nargs);
23586 for (a = TYPE_ARG_TYPES (arg), i = 0;
23587 a != NULL_TREE && a != void_list_node;
23588 a = TREE_CHAIN (a), ++i)
23589 args[i] = TREE_VALUE (a);
23590 nargs = i;
23591
23592 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23593 args, nargs, 1, DEDUCE_EXACT,
23594 NULL, explain_p))
23595 return 1;
23596
23597 if (flag_noexcept_type)
23598 {
23599 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23600 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23601 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23602 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23603 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23604 && uses_template_parms (TREE_PURPOSE (pspec)))
23605 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23606 TREE_PURPOSE (aspec),
23607 UNIFY_ALLOW_NONE, explain_p);
23608 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23609 return unify_type_mismatch (explain_p, parm, arg);
23610 }
23611
23612 return 0;
23613 }
23614
23615 case OFFSET_TYPE:
23616 /* Unify a pointer to member with a pointer to member function, which
23617 deduces the type of the member as a function type. */
23618 if (TYPE_PTRMEMFUNC_P (arg))
23619 {
23620 /* Check top-level cv qualifiers */
23621 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23622 return unify_cv_qual_mismatch (explain_p, parm, arg);
23623
23624 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23625 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23626 UNIFY_ALLOW_NONE, explain_p);
23627
23628 /* Determine the type of the function we are unifying against. */
23629 tree fntype = static_fn_type (arg);
23630
23631 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23632 }
23633
23634 if (TREE_CODE (arg) != OFFSET_TYPE)
23635 return unify_type_mismatch (explain_p, parm, arg);
23636 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23637 TYPE_OFFSET_BASETYPE (arg),
23638 UNIFY_ALLOW_NONE, explain_p);
23639 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23640 strict, explain_p);
23641
23642 case CONST_DECL:
23643 if (DECL_TEMPLATE_PARM_P (parm))
23644 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23645 if (arg != scalar_constant_value (parm))
23646 return unify_template_argument_mismatch (explain_p, parm, arg);
23647 return unify_success (explain_p);
23648
23649 case FIELD_DECL:
23650 case TEMPLATE_DECL:
23651 /* Matched cases are handled by the ARG == PARM test above. */
23652 return unify_template_argument_mismatch (explain_p, parm, arg);
23653
23654 case VAR_DECL:
23655 /* We might get a variable as a non-type template argument in parm if the
23656 corresponding parameter is type-dependent. Make any necessary
23657 adjustments based on whether arg is a reference. */
23658 if (CONSTANT_CLASS_P (arg))
23659 parm = fold_non_dependent_expr (parm, complain);
23660 else if (REFERENCE_REF_P (arg))
23661 {
23662 tree sub = TREE_OPERAND (arg, 0);
23663 STRIP_NOPS (sub);
23664 if (TREE_CODE (sub) == ADDR_EXPR)
23665 arg = TREE_OPERAND (sub, 0);
23666 }
23667 /* Now use the normal expression code to check whether they match. */
23668 goto expr;
23669
23670 case TYPE_ARGUMENT_PACK:
23671 case NONTYPE_ARGUMENT_PACK:
23672 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23673 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23674
23675 case TYPEOF_TYPE:
23676 case DECLTYPE_TYPE:
23677 case UNDERLYING_TYPE:
23678 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23679 or UNDERLYING_TYPE nodes. */
23680 return unify_success (explain_p);
23681
23682 case ERROR_MARK:
23683 /* Unification fails if we hit an error node. */
23684 return unify_invalid (explain_p);
23685
23686 case INDIRECT_REF:
23687 if (REFERENCE_REF_P (parm))
23688 {
23689 bool pexp = PACK_EXPANSION_P (arg);
23690 if (pexp)
23691 arg = PACK_EXPANSION_PATTERN (arg);
23692 if (REFERENCE_REF_P (arg))
23693 arg = TREE_OPERAND (arg, 0);
23694 if (pexp)
23695 arg = make_pack_expansion (arg, complain);
23696 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23697 strict, explain_p);
23698 }
23699 /* FALLTHRU */
23700
23701 default:
23702 /* An unresolved overload is a nondeduced context. */
23703 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23704 return unify_success (explain_p);
23705 gcc_assert (EXPR_P (parm)
23706 || COMPOUND_LITERAL_P (parm)
23707 || TREE_CODE (parm) == TRAIT_EXPR);
23708 expr:
23709 /* We must be looking at an expression. This can happen with
23710 something like:
23711
23712 template <int I>
23713 void foo(S<I>, S<I + 2>);
23714
23715 or
23716
23717 template<typename T>
23718 void foo(A<T, T{}>);
23719
23720 This is a "non-deduced context":
23721
23722 [deduct.type]
23723
23724 The non-deduced contexts are:
23725
23726 --A non-type template argument or an array bound in which
23727 a subexpression references a template parameter.
23728
23729 In these cases, we assume deduction succeeded, but don't
23730 actually infer any unifications. */
23731
23732 if (!uses_template_parms (parm)
23733 && !template_args_equal (parm, arg))
23734 return unify_expression_unequal (explain_p, parm, arg);
23735 else
23736 return unify_success (explain_p);
23737 }
23738 }
23739 #undef RECUR_AND_CHECK_FAILURE
23740 \f
23741 /* Note that DECL can be defined in this translation unit, if
23742 required. */
23743
23744 static void
23745 mark_definable (tree decl)
23746 {
23747 tree clone;
23748 DECL_NOT_REALLY_EXTERN (decl) = 1;
23749 FOR_EACH_CLONE (clone, decl)
23750 DECL_NOT_REALLY_EXTERN (clone) = 1;
23751 }
23752
23753 /* Called if RESULT is explicitly instantiated, or is a member of an
23754 explicitly instantiated class. */
23755
23756 void
23757 mark_decl_instantiated (tree result, int extern_p)
23758 {
23759 SET_DECL_EXPLICIT_INSTANTIATION (result);
23760
23761 /* If this entity has already been written out, it's too late to
23762 make any modifications. */
23763 if (TREE_ASM_WRITTEN (result))
23764 return;
23765
23766 /* For anonymous namespace we don't need to do anything. */
23767 if (decl_anon_ns_mem_p (result))
23768 {
23769 gcc_assert (!TREE_PUBLIC (result));
23770 return;
23771 }
23772
23773 if (TREE_CODE (result) != FUNCTION_DECL)
23774 /* The TREE_PUBLIC flag for function declarations will have been
23775 set correctly by tsubst. */
23776 TREE_PUBLIC (result) = 1;
23777
23778 /* This might have been set by an earlier implicit instantiation. */
23779 DECL_COMDAT (result) = 0;
23780
23781 if (extern_p)
23782 DECL_NOT_REALLY_EXTERN (result) = 0;
23783 else
23784 {
23785 mark_definable (result);
23786 mark_needed (result);
23787 /* Always make artificials weak. */
23788 if (DECL_ARTIFICIAL (result) && flag_weak)
23789 comdat_linkage (result);
23790 /* For WIN32 we also want to put explicit instantiations in
23791 linkonce sections. */
23792 else if (TREE_PUBLIC (result))
23793 maybe_make_one_only (result);
23794 if (TREE_CODE (result) == FUNCTION_DECL
23795 && DECL_TEMPLATE_INSTANTIATED (result))
23796 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23797 since start_preparsed_function wouldn't have if we had an earlier
23798 extern explicit instantiation. */
23799 DECL_EXTERNAL (result) = 0;
23800 }
23801
23802 /* If EXTERN_P, then this function will not be emitted -- unless
23803 followed by an explicit instantiation, at which point its linkage
23804 will be adjusted. If !EXTERN_P, then this function will be
23805 emitted here. In neither circumstance do we want
23806 import_export_decl to adjust the linkage. */
23807 DECL_INTERFACE_KNOWN (result) = 1;
23808 }
23809
23810 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23811 important template arguments. If any are missing, we check whether
23812 they're important by using error_mark_node for substituting into any
23813 args that were used for partial ordering (the ones between ARGS and END)
23814 and seeing if it bubbles up. */
23815
23816 static bool
23817 check_undeduced_parms (tree targs, tree args, tree end)
23818 {
23819 bool found = false;
23820 int i;
23821 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23822 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23823 {
23824 found = true;
23825 TREE_VEC_ELT (targs, i) = error_mark_node;
23826 }
23827 if (found)
23828 {
23829 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23830 if (substed == error_mark_node)
23831 return true;
23832 }
23833 return false;
23834 }
23835
23836 /* Given two function templates PAT1 and PAT2, return:
23837
23838 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23839 -1 if PAT2 is more specialized than PAT1.
23840 0 if neither is more specialized.
23841
23842 LEN indicates the number of parameters we should consider
23843 (defaulted parameters should not be considered).
23844
23845 The 1998 std underspecified function template partial ordering, and
23846 DR214 addresses the issue. We take pairs of arguments, one from
23847 each of the templates, and deduce them against each other. One of
23848 the templates will be more specialized if all the *other*
23849 template's arguments deduce against its arguments and at least one
23850 of its arguments *does* *not* deduce against the other template's
23851 corresponding argument. Deduction is done as for class templates.
23852 The arguments used in deduction have reference and top level cv
23853 qualifiers removed. Iff both arguments were originally reference
23854 types *and* deduction succeeds in both directions, an lvalue reference
23855 wins against an rvalue reference and otherwise the template
23856 with the more cv-qualified argument wins for that pairing (if
23857 neither is more cv-qualified, they both are equal). Unlike regular
23858 deduction, after all the arguments have been deduced in this way,
23859 we do *not* verify the deduced template argument values can be
23860 substituted into non-deduced contexts.
23861
23862 The logic can be a bit confusing here, because we look at deduce1 and
23863 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23864 can find template arguments for pat1 to make arg1 look like arg2, that
23865 means that arg2 is at least as specialized as arg1. */
23866
23867 int
23868 more_specialized_fn (tree pat1, tree pat2, int len)
23869 {
23870 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23871 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23872 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23873 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23874 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23875 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23876 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23877 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23878 tree origs1, origs2;
23879 bool lose1 = false;
23880 bool lose2 = false;
23881
23882 /* Remove the this parameter from non-static member functions. If
23883 one is a non-static member function and the other is not a static
23884 member function, remove the first parameter from that function
23885 also. This situation occurs for operator functions where we
23886 locate both a member function (with this pointer) and non-member
23887 operator (with explicit first operand). */
23888 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23889 {
23890 len--; /* LEN is the number of significant arguments for DECL1 */
23891 args1 = TREE_CHAIN (args1);
23892 if (!DECL_STATIC_FUNCTION_P (decl2))
23893 args2 = TREE_CHAIN (args2);
23894 }
23895 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
23896 {
23897 args2 = TREE_CHAIN (args2);
23898 if (!DECL_STATIC_FUNCTION_P (decl1))
23899 {
23900 len--;
23901 args1 = TREE_CHAIN (args1);
23902 }
23903 }
23904
23905 /* If only one is a conversion operator, they are unordered. */
23906 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23907 return 0;
23908
23909 /* Consider the return type for a conversion function */
23910 if (DECL_CONV_FN_P (decl1))
23911 {
23912 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23913 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23914 len++;
23915 }
23916
23917 processing_template_decl++;
23918
23919 origs1 = args1;
23920 origs2 = args2;
23921
23922 while (len--
23923 /* Stop when an ellipsis is seen. */
23924 && args1 != NULL_TREE && args2 != NULL_TREE)
23925 {
23926 tree arg1 = TREE_VALUE (args1);
23927 tree arg2 = TREE_VALUE (args2);
23928 int deduce1, deduce2;
23929 int quals1 = -1;
23930 int quals2 = -1;
23931 int ref1 = 0;
23932 int ref2 = 0;
23933
23934 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23935 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23936 {
23937 /* When both arguments are pack expansions, we need only
23938 unify the patterns themselves. */
23939 arg1 = PACK_EXPANSION_PATTERN (arg1);
23940 arg2 = PACK_EXPANSION_PATTERN (arg2);
23941
23942 /* This is the last comparison we need to do. */
23943 len = 0;
23944 }
23945
23946 if (TYPE_REF_P (arg1))
23947 {
23948 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23949 arg1 = TREE_TYPE (arg1);
23950 quals1 = cp_type_quals (arg1);
23951 }
23952
23953 if (TYPE_REF_P (arg2))
23954 {
23955 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23956 arg2 = TREE_TYPE (arg2);
23957 quals2 = cp_type_quals (arg2);
23958 }
23959
23960 arg1 = TYPE_MAIN_VARIANT (arg1);
23961 arg2 = TYPE_MAIN_VARIANT (arg2);
23962
23963 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23964 {
23965 int i, len2 = remaining_arguments (args2);
23966 tree parmvec = make_tree_vec (1);
23967 tree argvec = make_tree_vec (len2);
23968 tree ta = args2;
23969
23970 /* Setup the parameter vector, which contains only ARG1. */
23971 TREE_VEC_ELT (parmvec, 0) = arg1;
23972
23973 /* Setup the argument vector, which contains the remaining
23974 arguments. */
23975 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23976 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23977
23978 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23979 argvec, DEDUCE_EXACT,
23980 /*subr=*/true, /*explain_p=*/false)
23981 == 0);
23982
23983 /* We cannot deduce in the other direction, because ARG1 is
23984 a pack expansion but ARG2 is not. */
23985 deduce2 = 0;
23986 }
23987 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23988 {
23989 int i, len1 = remaining_arguments (args1);
23990 tree parmvec = make_tree_vec (1);
23991 tree argvec = make_tree_vec (len1);
23992 tree ta = args1;
23993
23994 /* Setup the parameter vector, which contains only ARG1. */
23995 TREE_VEC_ELT (parmvec, 0) = arg2;
23996
23997 /* Setup the argument vector, which contains the remaining
23998 arguments. */
23999 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24000 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24001
24002 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24003 argvec, DEDUCE_EXACT,
24004 /*subr=*/true, /*explain_p=*/false)
24005 == 0);
24006
24007 /* We cannot deduce in the other direction, because ARG2 is
24008 a pack expansion but ARG1 is not.*/
24009 deduce1 = 0;
24010 }
24011
24012 else
24013 {
24014 /* The normal case, where neither argument is a pack
24015 expansion. */
24016 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24017 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24018 == 0);
24019 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24020 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24021 == 0);
24022 }
24023
24024 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24025 arg2, then arg2 is not as specialized as arg1. */
24026 if (!deduce1)
24027 lose2 = true;
24028 if (!deduce2)
24029 lose1 = true;
24030
24031 /* "If, for a given type, deduction succeeds in both directions
24032 (i.e., the types are identical after the transformations above)
24033 and both P and A were reference types (before being replaced with
24034 the type referred to above):
24035 - if the type from the argument template was an lvalue reference and
24036 the type from the parameter template was not, the argument type is
24037 considered to be more specialized than the other; otherwise,
24038 - if the type from the argument template is more cv-qualified
24039 than the type from the parameter template (as described above),
24040 the argument type is considered to be more specialized than the other;
24041 otherwise,
24042 - neither type is more specialized than the other." */
24043
24044 if (deduce1 && deduce2)
24045 {
24046 if (ref1 && ref2 && ref1 != ref2)
24047 {
24048 if (ref1 > ref2)
24049 lose1 = true;
24050 else
24051 lose2 = true;
24052 }
24053 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24054 {
24055 if ((quals1 & quals2) == quals2)
24056 lose2 = true;
24057 if ((quals1 & quals2) == quals1)
24058 lose1 = true;
24059 }
24060 }
24061
24062 if (lose1 && lose2)
24063 /* We've failed to deduce something in either direction.
24064 These must be unordered. */
24065 break;
24066
24067 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24068 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24069 /* We have already processed all of the arguments in our
24070 handing of the pack expansion type. */
24071 len = 0;
24072
24073 args1 = TREE_CHAIN (args1);
24074 args2 = TREE_CHAIN (args2);
24075 }
24076
24077 /* "In most cases, all template parameters must have values in order for
24078 deduction to succeed, but for partial ordering purposes a template
24079 parameter may remain without a value provided it is not used in the
24080 types being used for partial ordering."
24081
24082 Thus, if we are missing any of the targs1 we need to substitute into
24083 origs1, then pat2 is not as specialized as pat1. This can happen when
24084 there is a nondeduced context. */
24085 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24086 lose2 = true;
24087 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24088 lose1 = true;
24089
24090 processing_template_decl--;
24091
24092 /* If both deductions succeed, the partial ordering selects the more
24093 constrained template. */
24094 if (!lose1 && !lose2)
24095 {
24096 int winner = more_constrained (decl1, decl2);
24097 if (winner > 0)
24098 lose2 = true;
24099 else if (winner < 0)
24100 lose1 = true;
24101 }
24102
24103 /* All things being equal, if the next argument is a pack expansion
24104 for one function but not for the other, prefer the
24105 non-variadic function. FIXME this is bogus; see c++/41958. */
24106 if (lose1 == lose2
24107 && args1 && TREE_VALUE (args1)
24108 && args2 && TREE_VALUE (args2))
24109 {
24110 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24111 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24112 }
24113
24114 if (lose1 == lose2)
24115 return 0;
24116 else if (!lose1)
24117 return 1;
24118 else
24119 return -1;
24120 }
24121
24122 /* Determine which of two partial specializations of TMPL is more
24123 specialized.
24124
24125 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24126 to the first partial specialization. The TREE_PURPOSE is the
24127 innermost set of template parameters for the partial
24128 specialization. PAT2 is similar, but for the second template.
24129
24130 Return 1 if the first partial specialization is more specialized;
24131 -1 if the second is more specialized; 0 if neither is more
24132 specialized.
24133
24134 See [temp.class.order] for information about determining which of
24135 two templates is more specialized. */
24136
24137 static int
24138 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24139 {
24140 tree targs;
24141 int winner = 0;
24142 bool any_deductions = false;
24143
24144 tree tmpl1 = TREE_VALUE (pat1);
24145 tree tmpl2 = TREE_VALUE (pat2);
24146 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24147 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24148
24149 /* Just like what happens for functions, if we are ordering between
24150 different template specializations, we may encounter dependent
24151 types in the arguments, and we need our dependency check functions
24152 to behave correctly. */
24153 ++processing_template_decl;
24154 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24155 if (targs)
24156 {
24157 --winner;
24158 any_deductions = true;
24159 }
24160
24161 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24162 if (targs)
24163 {
24164 ++winner;
24165 any_deductions = true;
24166 }
24167 --processing_template_decl;
24168
24169 /* If both deductions succeed, the partial ordering selects the more
24170 constrained template. */
24171 if (!winner && any_deductions)
24172 winner = more_constrained (tmpl1, tmpl2);
24173
24174 /* In the case of a tie where at least one of the templates
24175 has a parameter pack at the end, the template with the most
24176 non-packed parameters wins. */
24177 if (winner == 0
24178 && any_deductions
24179 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24180 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24181 {
24182 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24183 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24184 int len1 = TREE_VEC_LENGTH (args1);
24185 int len2 = TREE_VEC_LENGTH (args2);
24186
24187 /* We don't count the pack expansion at the end. */
24188 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24189 --len1;
24190 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24191 --len2;
24192
24193 if (len1 > len2)
24194 return 1;
24195 else if (len1 < len2)
24196 return -1;
24197 }
24198
24199 return winner;
24200 }
24201
24202 /* Return the template arguments that will produce the function signature
24203 DECL from the function template FN, with the explicit template
24204 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24205 also match. Return NULL_TREE if no satisfactory arguments could be
24206 found. */
24207
24208 static tree
24209 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24210 {
24211 int ntparms = DECL_NTPARMS (fn);
24212 tree targs = make_tree_vec (ntparms);
24213 tree decl_type = TREE_TYPE (decl);
24214 tree decl_arg_types;
24215 tree *args;
24216 unsigned int nargs, ix;
24217 tree arg;
24218
24219 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24220
24221 /* Never do unification on the 'this' parameter. */
24222 decl_arg_types = skip_artificial_parms_for (decl,
24223 TYPE_ARG_TYPES (decl_type));
24224
24225 nargs = list_length (decl_arg_types);
24226 args = XALLOCAVEC (tree, nargs);
24227 for (arg = decl_arg_types, ix = 0;
24228 arg != NULL_TREE && arg != void_list_node;
24229 arg = TREE_CHAIN (arg), ++ix)
24230 args[ix] = TREE_VALUE (arg);
24231
24232 if (fn_type_unification (fn, explicit_args, targs,
24233 args, ix,
24234 (check_rettype || DECL_CONV_FN_P (fn)
24235 ? TREE_TYPE (decl_type) : NULL_TREE),
24236 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24237 /*explain_p=*/false,
24238 /*decltype*/false)
24239 == error_mark_node)
24240 return NULL_TREE;
24241
24242 return targs;
24243 }
24244
24245 /* Return the innermost template arguments that, when applied to a partial
24246 specialization SPEC_TMPL of TMPL, yield the ARGS.
24247
24248 For example, suppose we have:
24249
24250 template <class T, class U> struct S {};
24251 template <class T> struct S<T*, int> {};
24252
24253 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24254 partial specialization and the ARGS will be {double*, int}. The resulting
24255 vector will be {double}, indicating that `T' is bound to `double'. */
24256
24257 static tree
24258 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24259 {
24260 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24261 tree spec_args
24262 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24263 int i, ntparms = TREE_VEC_LENGTH (tparms);
24264 tree deduced_args;
24265 tree innermost_deduced_args;
24266
24267 innermost_deduced_args = make_tree_vec (ntparms);
24268 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24269 {
24270 deduced_args = copy_node (args);
24271 SET_TMPL_ARGS_LEVEL (deduced_args,
24272 TMPL_ARGS_DEPTH (deduced_args),
24273 innermost_deduced_args);
24274 }
24275 else
24276 deduced_args = innermost_deduced_args;
24277
24278 bool tried_array_deduction = (cxx_dialect < cxx17);
24279 again:
24280 if (unify (tparms, deduced_args,
24281 INNERMOST_TEMPLATE_ARGS (spec_args),
24282 INNERMOST_TEMPLATE_ARGS (args),
24283 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24284 return NULL_TREE;
24285
24286 for (i = 0; i < ntparms; ++i)
24287 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24288 {
24289 if (!tried_array_deduction)
24290 {
24291 try_array_deduction (tparms, innermost_deduced_args,
24292 INNERMOST_TEMPLATE_ARGS (spec_args));
24293 tried_array_deduction = true;
24294 if (TREE_VEC_ELT (innermost_deduced_args, i))
24295 goto again;
24296 }
24297 return NULL_TREE;
24298 }
24299
24300 if (!push_tinst_level (spec_tmpl, deduced_args))
24301 {
24302 excessive_deduction_depth = true;
24303 return NULL_TREE;
24304 }
24305
24306 /* Verify that nondeduced template arguments agree with the type
24307 obtained from argument deduction.
24308
24309 For example:
24310
24311 struct A { typedef int X; };
24312 template <class T, class U> struct C {};
24313 template <class T> struct C<T, typename T::X> {};
24314
24315 Then with the instantiation `C<A, int>', we can deduce that
24316 `T' is `A' but unify () does not check whether `typename T::X'
24317 is `int'. */
24318 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24319
24320 if (spec_args != error_mark_node)
24321 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24322 INNERMOST_TEMPLATE_ARGS (spec_args),
24323 tmpl, tf_none, false, false);
24324
24325 pop_tinst_level ();
24326
24327 if (spec_args == error_mark_node
24328 /* We only need to check the innermost arguments; the other
24329 arguments will always agree. */
24330 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24331 INNERMOST_TEMPLATE_ARGS (args)))
24332 return NULL_TREE;
24333
24334 /* Now that we have bindings for all of the template arguments,
24335 ensure that the arguments deduced for the template template
24336 parameters have compatible template parameter lists. See the use
24337 of template_template_parm_bindings_ok_p in fn_type_unification
24338 for more information. */
24339 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24340 return NULL_TREE;
24341
24342 return deduced_args;
24343 }
24344
24345 // Compare two function templates T1 and T2 by deducing bindings
24346 // from one against the other. If both deductions succeed, compare
24347 // constraints to see which is more constrained.
24348 static int
24349 more_specialized_inst (tree t1, tree t2)
24350 {
24351 int fate = 0;
24352 int count = 0;
24353
24354 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24355 {
24356 --fate;
24357 ++count;
24358 }
24359
24360 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24361 {
24362 ++fate;
24363 ++count;
24364 }
24365
24366 // If both deductions succeed, then one may be more constrained.
24367 if (count == 2 && fate == 0)
24368 fate = more_constrained (t1, t2);
24369
24370 return fate;
24371 }
24372
24373 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24374 Return the TREE_LIST node with the most specialized template, if
24375 any. If there is no most specialized template, the error_mark_node
24376 is returned.
24377
24378 Note that this function does not look at, or modify, the
24379 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24380 returned is one of the elements of INSTANTIATIONS, callers may
24381 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24382 and retrieve it from the value returned. */
24383
24384 tree
24385 most_specialized_instantiation (tree templates)
24386 {
24387 tree fn, champ;
24388
24389 ++processing_template_decl;
24390
24391 champ = templates;
24392 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24393 {
24394 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24395 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24396 if (fate == -1)
24397 champ = fn;
24398 else if (!fate)
24399 {
24400 /* Equally specialized, move to next function. If there
24401 is no next function, nothing's most specialized. */
24402 fn = TREE_CHAIN (fn);
24403 champ = fn;
24404 if (!fn)
24405 break;
24406 }
24407 }
24408
24409 if (champ)
24410 /* Now verify that champ is better than everything earlier in the
24411 instantiation list. */
24412 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24413 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24414 {
24415 champ = NULL_TREE;
24416 break;
24417 }
24418 }
24419
24420 processing_template_decl--;
24421
24422 if (!champ)
24423 return error_mark_node;
24424
24425 return champ;
24426 }
24427
24428 /* If DECL is a specialization of some template, return the most
24429 general such template. Otherwise, returns NULL_TREE.
24430
24431 For example, given:
24432
24433 template <class T> struct S { template <class U> void f(U); };
24434
24435 if TMPL is `template <class U> void S<int>::f(U)' this will return
24436 the full template. This function will not trace past partial
24437 specializations, however. For example, given in addition:
24438
24439 template <class T> struct S<T*> { template <class U> void f(U); };
24440
24441 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24442 `template <class T> template <class U> S<T*>::f(U)'. */
24443
24444 tree
24445 most_general_template (tree decl)
24446 {
24447 if (TREE_CODE (decl) != TEMPLATE_DECL)
24448 {
24449 if (tree tinfo = get_template_info (decl))
24450 decl = TI_TEMPLATE (tinfo);
24451 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24452 template friend, or a FIELD_DECL for a capture pack. */
24453 if (TREE_CODE (decl) != TEMPLATE_DECL)
24454 return NULL_TREE;
24455 }
24456
24457 /* Look for more and more general templates. */
24458 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24459 {
24460 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24461 (See cp-tree.h for details.) */
24462 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24463 break;
24464
24465 if (CLASS_TYPE_P (TREE_TYPE (decl))
24466 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24467 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24468 break;
24469
24470 /* Stop if we run into an explicitly specialized class template. */
24471 if (!DECL_NAMESPACE_SCOPE_P (decl)
24472 && DECL_CONTEXT (decl)
24473 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24474 break;
24475
24476 decl = DECL_TI_TEMPLATE (decl);
24477 }
24478
24479 return decl;
24480 }
24481
24482 /* Return the most specialized of the template partial specializations
24483 which can produce TARGET, a specialization of some class or variable
24484 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24485 a TEMPLATE_DECL node corresponding to the partial specialization, while
24486 the TREE_PURPOSE is the set of template arguments that must be
24487 substituted into the template pattern in order to generate TARGET.
24488
24489 If the choice of partial specialization is ambiguous, a diagnostic
24490 is issued, and the error_mark_node is returned. If there are no
24491 partial specializations matching TARGET, then NULL_TREE is
24492 returned, indicating that the primary template should be used. */
24493
24494 tree
24495 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24496 {
24497 tree list = NULL_TREE;
24498 tree t;
24499 tree champ;
24500 int fate;
24501 bool ambiguous_p;
24502 tree outer_args = NULL_TREE;
24503 tree tmpl, args;
24504
24505 if (TYPE_P (target))
24506 {
24507 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24508 tmpl = TI_TEMPLATE (tinfo);
24509 args = TI_ARGS (tinfo);
24510 }
24511 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24512 {
24513 tmpl = TREE_OPERAND (target, 0);
24514 args = TREE_OPERAND (target, 1);
24515 }
24516 else if (VAR_P (target))
24517 {
24518 tree tinfo = DECL_TEMPLATE_INFO (target);
24519 tmpl = TI_TEMPLATE (tinfo);
24520 args = TI_ARGS (tinfo);
24521 }
24522 else
24523 gcc_unreachable ();
24524
24525 tree main_tmpl = most_general_template (tmpl);
24526
24527 /* For determining which partial specialization to use, only the
24528 innermost args are interesting. */
24529 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24530 {
24531 outer_args = strip_innermost_template_args (args, 1);
24532 args = INNERMOST_TEMPLATE_ARGS (args);
24533 }
24534
24535 /* The caller hasn't called push_to_top_level yet, but we need
24536 get_partial_spec_bindings to be done in non-template context so that we'll
24537 fully resolve everything. */
24538 processing_template_decl_sentinel ptds;
24539
24540 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24541 {
24542 tree spec_args;
24543 tree spec_tmpl = TREE_VALUE (t);
24544
24545 if (outer_args)
24546 {
24547 /* Substitute in the template args from the enclosing class. */
24548 ++processing_template_decl;
24549 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
24550 --processing_template_decl;
24551 }
24552
24553 if (spec_tmpl == error_mark_node)
24554 return error_mark_node;
24555
24556 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24557 if (spec_args)
24558 {
24559 if (outer_args)
24560 spec_args = add_to_template_args (outer_args, spec_args);
24561
24562 /* Keep the candidate only if the constraints are satisfied,
24563 or if we're not compiling with concepts. */
24564 if (!flag_concepts
24565 || constraints_satisfied_p (spec_tmpl, spec_args))
24566 {
24567 list = tree_cons (spec_args, TREE_VALUE (t), list);
24568 TREE_TYPE (list) = TREE_TYPE (t);
24569 }
24570 }
24571 }
24572
24573 if (! list)
24574 return NULL_TREE;
24575
24576 ambiguous_p = false;
24577 t = list;
24578 champ = t;
24579 t = TREE_CHAIN (t);
24580 for (; t; t = TREE_CHAIN (t))
24581 {
24582 fate = more_specialized_partial_spec (tmpl, champ, t);
24583 if (fate == 1)
24584 ;
24585 else
24586 {
24587 if (fate == 0)
24588 {
24589 t = TREE_CHAIN (t);
24590 if (! t)
24591 {
24592 ambiguous_p = true;
24593 break;
24594 }
24595 }
24596 champ = t;
24597 }
24598 }
24599
24600 if (!ambiguous_p)
24601 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24602 {
24603 fate = more_specialized_partial_spec (tmpl, champ, t);
24604 if (fate != 1)
24605 {
24606 ambiguous_p = true;
24607 break;
24608 }
24609 }
24610
24611 if (ambiguous_p)
24612 {
24613 const char *str;
24614 char *spaces = NULL;
24615 if (!(complain & tf_error))
24616 return error_mark_node;
24617 if (TYPE_P (target))
24618 error ("ambiguous template instantiation for %q#T", target);
24619 else
24620 error ("ambiguous template instantiation for %q#D", target);
24621 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24622 for (t = list; t; t = TREE_CHAIN (t))
24623 {
24624 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24625 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24626 "%s %#qS", spaces ? spaces : str, subst);
24627 spaces = spaces ? spaces : get_spaces (str);
24628 }
24629 free (spaces);
24630 return error_mark_node;
24631 }
24632
24633 return champ;
24634 }
24635
24636 /* Explicitly instantiate DECL. */
24637
24638 void
24639 do_decl_instantiation (tree decl, tree storage)
24640 {
24641 tree result = NULL_TREE;
24642 int extern_p = 0;
24643
24644 if (!decl || decl == error_mark_node)
24645 /* An error occurred, for which grokdeclarator has already issued
24646 an appropriate message. */
24647 return;
24648 else if (! DECL_LANG_SPECIFIC (decl))
24649 {
24650 error ("explicit instantiation of non-template %q#D", decl);
24651 return;
24652 }
24653 else if (DECL_DECLARED_CONCEPT_P (decl))
24654 {
24655 if (VAR_P (decl))
24656 error ("explicit instantiation of variable concept %q#D", decl);
24657 else
24658 error ("explicit instantiation of function concept %q#D", decl);
24659 return;
24660 }
24661
24662 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24663 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24664
24665 if (VAR_P (decl) && !var_templ)
24666 {
24667 /* There is an asymmetry here in the way VAR_DECLs and
24668 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24669 the latter, the DECL we get back will be marked as a
24670 template instantiation, and the appropriate
24671 DECL_TEMPLATE_INFO will be set up. This does not happen for
24672 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24673 should handle VAR_DECLs as it currently handles
24674 FUNCTION_DECLs. */
24675 if (!DECL_CLASS_SCOPE_P (decl))
24676 {
24677 error ("%qD is not a static data member of a class template", decl);
24678 return;
24679 }
24680 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24681 if (!result || !VAR_P (result))
24682 {
24683 error ("no matching template for %qD found", decl);
24684 return;
24685 }
24686 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24687 {
24688 error ("type %qT for explicit instantiation %qD does not match "
24689 "declared type %qT", TREE_TYPE (result), decl,
24690 TREE_TYPE (decl));
24691 return;
24692 }
24693 }
24694 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24695 {
24696 error ("explicit instantiation of %q#D", decl);
24697 return;
24698 }
24699 else
24700 result = decl;
24701
24702 /* Check for various error cases. Note that if the explicit
24703 instantiation is valid the RESULT will currently be marked as an
24704 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24705 until we get here. */
24706
24707 if (DECL_TEMPLATE_SPECIALIZATION (result))
24708 {
24709 /* DR 259 [temp.spec].
24710
24711 Both an explicit instantiation and a declaration of an explicit
24712 specialization shall not appear in a program unless the explicit
24713 instantiation follows a declaration of the explicit specialization.
24714
24715 For a given set of template parameters, if an explicit
24716 instantiation of a template appears after a declaration of an
24717 explicit specialization for that template, the explicit
24718 instantiation has no effect. */
24719 return;
24720 }
24721 else if (DECL_EXPLICIT_INSTANTIATION (result))
24722 {
24723 /* [temp.spec]
24724
24725 No program shall explicitly instantiate any template more
24726 than once.
24727
24728 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24729 the first instantiation was `extern' and the second is not,
24730 and EXTERN_P for the opposite case. */
24731 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24732 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24733 /* If an "extern" explicit instantiation follows an ordinary
24734 explicit instantiation, the template is instantiated. */
24735 if (extern_p)
24736 return;
24737 }
24738 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24739 {
24740 error ("no matching template for %qD found", result);
24741 return;
24742 }
24743 else if (!DECL_TEMPLATE_INFO (result))
24744 {
24745 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24746 return;
24747 }
24748
24749 if (storage == NULL_TREE)
24750 ;
24751 else if (storage == ridpointers[(int) RID_EXTERN])
24752 {
24753 if (cxx_dialect == cxx98)
24754 pedwarn (input_location, OPT_Wpedantic,
24755 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24756 "instantiations");
24757 extern_p = 1;
24758 }
24759 else
24760 error ("storage class %qD applied to template instantiation", storage);
24761
24762 check_explicit_instantiation_namespace (result);
24763 mark_decl_instantiated (result, extern_p);
24764 if (! extern_p)
24765 instantiate_decl (result, /*defer_ok=*/true,
24766 /*expl_inst_class_mem_p=*/false);
24767 }
24768
24769 static void
24770 mark_class_instantiated (tree t, int extern_p)
24771 {
24772 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24773 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24774 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24775 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24776 if (! extern_p)
24777 {
24778 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24779 rest_of_type_compilation (t, 1);
24780 }
24781 }
24782
24783 /* Called from do_type_instantiation through binding_table_foreach to
24784 do recursive instantiation for the type bound in ENTRY. */
24785 static void
24786 bt_instantiate_type_proc (binding_entry entry, void *data)
24787 {
24788 tree storage = *(tree *) data;
24789
24790 if (MAYBE_CLASS_TYPE_P (entry->type)
24791 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24792 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24793 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24794 }
24795
24796 /* Perform an explicit instantiation of template class T. STORAGE, if
24797 non-null, is the RID for extern, inline or static. COMPLAIN is
24798 nonzero if this is called from the parser, zero if called recursively,
24799 since the standard is unclear (as detailed below). */
24800
24801 void
24802 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24803 {
24804 int extern_p = 0;
24805 int nomem_p = 0;
24806 int static_p = 0;
24807 int previous_instantiation_extern_p = 0;
24808
24809 if (TREE_CODE (t) == TYPE_DECL)
24810 t = TREE_TYPE (t);
24811
24812 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24813 {
24814 tree tmpl =
24815 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24816 if (tmpl)
24817 error ("explicit instantiation of non-class template %qD", tmpl);
24818 else
24819 error ("explicit instantiation of non-template type %qT", t);
24820 return;
24821 }
24822
24823 complete_type (t);
24824
24825 if (!COMPLETE_TYPE_P (t))
24826 {
24827 if (complain & tf_error)
24828 error ("explicit instantiation of %q#T before definition of template",
24829 t);
24830 return;
24831 }
24832
24833 if (storage != NULL_TREE)
24834 {
24835 if (storage == ridpointers[(int) RID_EXTERN])
24836 {
24837 if (cxx_dialect == cxx98)
24838 pedwarn (input_location, OPT_Wpedantic,
24839 "ISO C++ 1998 forbids the use of %<extern%> on "
24840 "explicit instantiations");
24841 }
24842 else
24843 pedwarn (input_location, OPT_Wpedantic,
24844 "ISO C++ forbids the use of %qE"
24845 " on explicit instantiations", storage);
24846
24847 if (storage == ridpointers[(int) RID_INLINE])
24848 nomem_p = 1;
24849 else if (storage == ridpointers[(int) RID_EXTERN])
24850 extern_p = 1;
24851 else if (storage == ridpointers[(int) RID_STATIC])
24852 static_p = 1;
24853 else
24854 {
24855 error ("storage class %qD applied to template instantiation",
24856 storage);
24857 extern_p = 0;
24858 }
24859 }
24860
24861 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24862 {
24863 /* DR 259 [temp.spec].
24864
24865 Both an explicit instantiation and a declaration of an explicit
24866 specialization shall not appear in a program unless the explicit
24867 instantiation follows a declaration of the explicit specialization.
24868
24869 For a given set of template parameters, if an explicit
24870 instantiation of a template appears after a declaration of an
24871 explicit specialization for that template, the explicit
24872 instantiation has no effect. */
24873 return;
24874 }
24875 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24876 {
24877 /* [temp.spec]
24878
24879 No program shall explicitly instantiate any template more
24880 than once.
24881
24882 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24883 instantiation was `extern'. If EXTERN_P then the second is.
24884 These cases are OK. */
24885 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
24886
24887 if (!previous_instantiation_extern_p && !extern_p
24888 && (complain & tf_error))
24889 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24890
24891 /* If we've already instantiated the template, just return now. */
24892 if (!CLASSTYPE_INTERFACE_ONLY (t))
24893 return;
24894 }
24895
24896 check_explicit_instantiation_namespace (TYPE_NAME (t));
24897 mark_class_instantiated (t, extern_p);
24898
24899 if (nomem_p)
24900 return;
24901
24902 /* In contrast to implicit instantiation, where only the
24903 declarations, and not the definitions, of members are
24904 instantiated, we have here:
24905
24906 [temp.explicit]
24907
24908 The explicit instantiation of a class template specialization
24909 implies the instantiation of all of its members not
24910 previously explicitly specialized in the translation unit
24911 containing the explicit instantiation.
24912
24913 Of course, we can't instantiate member template classes, since we
24914 don't have any arguments for them. Note that the standard is
24915 unclear on whether the instantiation of the members are
24916 *explicit* instantiations or not. However, the most natural
24917 interpretation is that it should be an explicit
24918 instantiation. */
24919 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24920 if ((VAR_P (fld)
24921 || (TREE_CODE (fld) == FUNCTION_DECL
24922 && !static_p
24923 && user_provided_p (fld)))
24924 && DECL_TEMPLATE_INSTANTIATION (fld))
24925 {
24926 mark_decl_instantiated (fld, extern_p);
24927 if (! extern_p)
24928 instantiate_decl (fld, /*defer_ok=*/true,
24929 /*expl_inst_class_mem_p=*/true);
24930 }
24931
24932 if (CLASSTYPE_NESTED_UTDS (t))
24933 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24934 bt_instantiate_type_proc, &storage);
24935 }
24936
24937 /* Given a function DECL, which is a specialization of TMPL, modify
24938 DECL to be a re-instantiation of TMPL with the same template
24939 arguments. TMPL should be the template into which tsubst'ing
24940 should occur for DECL, not the most general template.
24941
24942 One reason for doing this is a scenario like this:
24943
24944 template <class T>
24945 void f(const T&, int i);
24946
24947 void g() { f(3, 7); }
24948
24949 template <class T>
24950 void f(const T& t, const int i) { }
24951
24952 Note that when the template is first instantiated, with
24953 instantiate_template, the resulting DECL will have no name for the
24954 first parameter, and the wrong type for the second. So, when we go
24955 to instantiate the DECL, we regenerate it. */
24956
24957 static void
24958 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24959 {
24960 /* The arguments used to instantiate DECL, from the most general
24961 template. */
24962 tree code_pattern;
24963
24964 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24965
24966 /* Make sure that we can see identifiers, and compute access
24967 correctly. */
24968 push_access_scope (decl);
24969
24970 if (TREE_CODE (decl) == FUNCTION_DECL)
24971 {
24972 tree decl_parm;
24973 tree pattern_parm;
24974 tree specs;
24975 int args_depth;
24976 int parms_depth;
24977
24978 args_depth = TMPL_ARGS_DEPTH (args);
24979 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24980 if (args_depth > parms_depth)
24981 args = get_innermost_template_args (args, parms_depth);
24982
24983 /* Instantiate a dynamic exception-specification. noexcept will be
24984 handled below. */
24985 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24986 if (TREE_VALUE (raises))
24987 {
24988 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24989 args, tf_error, NULL_TREE,
24990 /*defer_ok*/false);
24991 if (specs && specs != error_mark_node)
24992 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24993 specs);
24994 }
24995
24996 /* Merge parameter declarations. */
24997 decl_parm = skip_artificial_parms_for (decl,
24998 DECL_ARGUMENTS (decl));
24999 pattern_parm
25000 = skip_artificial_parms_for (code_pattern,
25001 DECL_ARGUMENTS (code_pattern));
25002 while (decl_parm && !DECL_PACK_P (pattern_parm))
25003 {
25004 tree parm_type;
25005 tree attributes;
25006
25007 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25008 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
25009 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
25010 NULL_TREE);
25011 parm_type = type_decays_to (parm_type);
25012 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25013 TREE_TYPE (decl_parm) = parm_type;
25014 attributes = DECL_ATTRIBUTES (pattern_parm);
25015 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25016 {
25017 DECL_ATTRIBUTES (decl_parm) = attributes;
25018 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25019 }
25020 decl_parm = DECL_CHAIN (decl_parm);
25021 pattern_parm = DECL_CHAIN (pattern_parm);
25022 }
25023 /* Merge any parameters that match with the function parameter
25024 pack. */
25025 if (pattern_parm && DECL_PACK_P (pattern_parm))
25026 {
25027 int i, len;
25028 tree expanded_types;
25029 /* Expand the TYPE_PACK_EXPANSION that provides the types for
25030 the parameters in this function parameter pack. */
25031 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
25032 args, tf_error, NULL_TREE);
25033 len = TREE_VEC_LENGTH (expanded_types);
25034 for (i = 0; i < len; i++)
25035 {
25036 tree parm_type;
25037 tree attributes;
25038
25039 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
25040 /* Rename the parameter to include the index. */
25041 DECL_NAME (decl_parm) =
25042 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
25043 parm_type = TREE_VEC_ELT (expanded_types, i);
25044 parm_type = type_decays_to (parm_type);
25045 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
25046 TREE_TYPE (decl_parm) = parm_type;
25047 attributes = DECL_ATTRIBUTES (pattern_parm);
25048 if (DECL_ATTRIBUTES (decl_parm) != attributes)
25049 {
25050 DECL_ATTRIBUTES (decl_parm) = attributes;
25051 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
25052 }
25053 decl_parm = DECL_CHAIN (decl_parm);
25054 }
25055 }
25056 /* Merge additional specifiers from the CODE_PATTERN. */
25057 if (DECL_DECLARED_INLINE_P (code_pattern)
25058 && !DECL_DECLARED_INLINE_P (decl))
25059 DECL_DECLARED_INLINE_P (decl) = 1;
25060
25061 maybe_instantiate_noexcept (decl, tf_error);
25062 }
25063 else if (VAR_P (decl))
25064 {
25065 start_lambda_scope (decl);
25066 DECL_INITIAL (decl) =
25067 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25068 tf_error, DECL_TI_TEMPLATE (decl));
25069 finish_lambda_scope ();
25070 if (VAR_HAD_UNKNOWN_BOUND (decl))
25071 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25072 tf_error, DECL_TI_TEMPLATE (decl));
25073 }
25074 else
25075 gcc_unreachable ();
25076
25077 pop_access_scope (decl);
25078 }
25079
25080 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25081 substituted to get DECL. */
25082
25083 tree
25084 template_for_substitution (tree decl)
25085 {
25086 tree tmpl = DECL_TI_TEMPLATE (decl);
25087
25088 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25089 for the instantiation. This is not always the most general
25090 template. Consider, for example:
25091
25092 template <class T>
25093 struct S { template <class U> void f();
25094 template <> void f<int>(); };
25095
25096 and an instantiation of S<double>::f<int>. We want TD to be the
25097 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25098 while (/* An instantiation cannot have a definition, so we need a
25099 more general template. */
25100 DECL_TEMPLATE_INSTANTIATION (tmpl)
25101 /* We must also deal with friend templates. Given:
25102
25103 template <class T> struct S {
25104 template <class U> friend void f() {};
25105 };
25106
25107 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25108 so far as the language is concerned, but that's still
25109 where we get the pattern for the instantiation from. On
25110 other hand, if the definition comes outside the class, say:
25111
25112 template <class T> struct S {
25113 template <class U> friend void f();
25114 };
25115 template <class U> friend void f() {}
25116
25117 we don't need to look any further. That's what the check for
25118 DECL_INITIAL is for. */
25119 || (TREE_CODE (decl) == FUNCTION_DECL
25120 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25121 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25122 {
25123 /* The present template, TD, should not be a definition. If it
25124 were a definition, we should be using it! Note that we
25125 cannot restructure the loop to just keep going until we find
25126 a template with a definition, since that might go too far if
25127 a specialization was declared, but not defined. */
25128
25129 /* Fetch the more general template. */
25130 tmpl = DECL_TI_TEMPLATE (tmpl);
25131 }
25132
25133 return tmpl;
25134 }
25135
25136 /* Returns true if we need to instantiate this template instance even if we
25137 know we aren't going to emit it. */
25138
25139 bool
25140 always_instantiate_p (tree decl)
25141 {
25142 /* We always instantiate inline functions so that we can inline them. An
25143 explicit instantiation declaration prohibits implicit instantiation of
25144 non-inline functions. With high levels of optimization, we would
25145 normally inline non-inline functions -- but we're not allowed to do
25146 that for "extern template" functions. Therefore, we check
25147 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25148 return ((TREE_CODE (decl) == FUNCTION_DECL
25149 && (DECL_DECLARED_INLINE_P (decl)
25150 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25151 /* And we need to instantiate static data members so that
25152 their initializers are available in integral constant
25153 expressions. */
25154 || (VAR_P (decl)
25155 && decl_maybe_constant_var_p (decl)));
25156 }
25157
25158 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25159 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25160 error, true otherwise. */
25161
25162 bool
25163 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25164 {
25165 tree fntype, spec, noex, clone;
25166
25167 /* Don't instantiate a noexcept-specification from template context. */
25168 if (processing_template_decl
25169 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25170 return true;
25171
25172 if (DECL_MAYBE_DELETED (fn))
25173 {
25174 if (fn == current_function_decl)
25175 /* We're in start_preparsed_function, keep going. */
25176 return true;
25177
25178 ++function_depth;
25179 synthesize_method (fn);
25180 --function_depth;
25181 return !DECL_MAYBE_DELETED (fn);
25182 }
25183
25184 if (DECL_CLONED_FUNCTION_P (fn))
25185 fn = DECL_CLONED_FUNCTION (fn);
25186
25187 tree orig_fn = NULL_TREE;
25188 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25189 its FUNCTION_DECL for the rest of this function -- push_access_scope
25190 doesn't accept TEMPLATE_DECLs. */
25191 if (DECL_FUNCTION_TEMPLATE_P (fn))
25192 {
25193 orig_fn = fn;
25194 fn = DECL_TEMPLATE_RESULT (fn);
25195 }
25196
25197 fntype = TREE_TYPE (fn);
25198 spec = TYPE_RAISES_EXCEPTIONS (fntype);
25199
25200 if (!spec || !TREE_PURPOSE (spec))
25201 return true;
25202
25203 noex = TREE_PURPOSE (spec);
25204
25205 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25206 {
25207 static hash_set<tree>* fns = new hash_set<tree>;
25208 bool added = false;
25209 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25210 {
25211 spec = get_defaulted_eh_spec (fn, complain);
25212 if (spec == error_mark_node)
25213 /* This might have failed because of an unparsed DMI, so
25214 let's try again later. */
25215 return false;
25216 }
25217 else if (!(added = !fns->add (fn)))
25218 {
25219 /* If hash_set::add returns true, the element was already there. */
25220 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25221 DECL_SOURCE_LOCATION (fn));
25222 error_at (loc,
25223 "exception specification of %qD depends on itself",
25224 fn);
25225 spec = noexcept_false_spec;
25226 }
25227 else if (push_tinst_level (fn))
25228 {
25229 push_to_top_level ();
25230 push_access_scope (fn);
25231 push_deferring_access_checks (dk_no_deferred);
25232 input_location = DECL_SOURCE_LOCATION (fn);
25233
25234 /* If needed, set current_class_ptr for the benefit of
25235 tsubst_copy/PARM_DECL. */
25236 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25237 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25238 {
25239 tree this_parm = DECL_ARGUMENTS (tdecl);
25240 current_class_ptr = NULL_TREE;
25241 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25242 current_class_ptr = this_parm;
25243 }
25244
25245 /* If this function is represented by a TEMPLATE_DECL, then
25246 the deferred noexcept-specification might still contain
25247 dependent types, even after substitution. And we need the
25248 dependency check functions to work in build_noexcept_spec. */
25249 if (orig_fn)
25250 ++processing_template_decl;
25251
25252 /* Do deferred instantiation of the noexcept-specifier. */
25253 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25254 DEFERRED_NOEXCEPT_ARGS (noex),
25255 tf_warning_or_error, fn,
25256 /*function_p=*/false,
25257 /*i_c_e_p=*/true);
25258
25259 /* Build up the noexcept-specification. */
25260 spec = build_noexcept_spec (noex, tf_warning_or_error);
25261
25262 if (orig_fn)
25263 --processing_template_decl;
25264
25265 pop_deferring_access_checks ();
25266 pop_access_scope (fn);
25267 pop_tinst_level ();
25268 pop_from_top_level ();
25269 }
25270 else
25271 spec = noexcept_false_spec;
25272
25273 if (added)
25274 fns->remove (fn);
25275
25276 if (spec == error_mark_node)
25277 {
25278 /* This failed with a hard error, so let's go with false. */
25279 gcc_assert (seen_error ());
25280 spec = noexcept_false_spec;
25281 }
25282
25283 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25284 if (orig_fn)
25285 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25286 }
25287
25288 FOR_EACH_CLONE (clone, fn)
25289 {
25290 if (TREE_TYPE (clone) == fntype)
25291 TREE_TYPE (clone) = TREE_TYPE (fn);
25292 else
25293 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
25294 }
25295
25296 return true;
25297 }
25298
25299 /* We're starting to process the function INST, an instantiation of PATTERN;
25300 add their parameters to local_specializations. */
25301
25302 static void
25303 register_parameter_specializations (tree pattern, tree inst)
25304 {
25305 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25306 tree spec_parm = DECL_ARGUMENTS (inst);
25307 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25308 {
25309 register_local_specialization (spec_parm, tmpl_parm);
25310 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25311 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25312 }
25313 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25314 {
25315 if (!DECL_PACK_P (tmpl_parm)
25316 || (spec_parm && DECL_PACK_P (spec_parm)))
25317 {
25318 register_local_specialization (spec_parm, tmpl_parm);
25319 spec_parm = DECL_CHAIN (spec_parm);
25320 }
25321 else
25322 {
25323 /* Register the (value) argument pack as a specialization of
25324 TMPL_PARM, then move on. */
25325 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25326 register_local_specialization (argpack, tmpl_parm);
25327 }
25328 }
25329 gcc_assert (!spec_parm);
25330 }
25331
25332 /* Produce the definition of D, a _DECL generated from a template. If
25333 DEFER_OK is true, then we don't have to actually do the
25334 instantiation now; we just have to do it sometime. Normally it is
25335 an error if this is an explicit instantiation but D is undefined.
25336 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25337 instantiated class template. */
25338
25339 tree
25340 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25341 {
25342 tree tmpl = DECL_TI_TEMPLATE (d);
25343 tree gen_args;
25344 tree args;
25345 tree td;
25346 tree code_pattern;
25347 tree spec;
25348 tree gen_tmpl;
25349 bool pattern_defined;
25350 location_t saved_loc = input_location;
25351 int saved_unevaluated_operand = cp_unevaluated_operand;
25352 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25353 bool external_p;
25354 bool deleted_p;
25355
25356 /* This function should only be used to instantiate templates for
25357 functions and static member variables. */
25358 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25359
25360 /* A concept is never instantiated. */
25361 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25362
25363 /* Variables are never deferred; if instantiation is required, they
25364 are instantiated right away. That allows for better code in the
25365 case that an expression refers to the value of the variable --
25366 if the variable has a constant value the referring expression can
25367 take advantage of that fact. */
25368 if (VAR_P (d))
25369 defer_ok = false;
25370
25371 /* Don't instantiate cloned functions. Instead, instantiate the
25372 functions they cloned. */
25373 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25374 d = DECL_CLONED_FUNCTION (d);
25375
25376 if (DECL_TEMPLATE_INSTANTIATED (d)
25377 || (TREE_CODE (d) == FUNCTION_DECL
25378 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25379 || DECL_TEMPLATE_SPECIALIZATION (d))
25380 /* D has already been instantiated or explicitly specialized, so
25381 there's nothing for us to do here.
25382
25383 It might seem reasonable to check whether or not D is an explicit
25384 instantiation, and, if so, stop here. But when an explicit
25385 instantiation is deferred until the end of the compilation,
25386 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25387 the instantiation. */
25388 return d;
25389
25390 /* Check to see whether we know that this template will be
25391 instantiated in some other file, as with "extern template"
25392 extension. */
25393 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25394
25395 /* In general, we do not instantiate such templates. */
25396 if (external_p && !always_instantiate_p (d))
25397 return d;
25398
25399 gen_tmpl = most_general_template (tmpl);
25400 gen_args = DECL_TI_ARGS (d);
25401
25402 if (tmpl != gen_tmpl)
25403 /* We should already have the extra args. */
25404 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25405 == TMPL_ARGS_DEPTH (gen_args));
25406 /* And what's in the hash table should match D. */
25407 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
25408 || spec == NULL_TREE);
25409
25410 /* This needs to happen before any tsubsting. */
25411 if (! push_tinst_level (d))
25412 return d;
25413
25414 timevar_push (TV_TEMPLATE_INST);
25415
25416 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25417 for the instantiation. */
25418 td = template_for_substitution (d);
25419 args = gen_args;
25420
25421 if (VAR_P (d))
25422 {
25423 /* Look up an explicit specialization, if any. */
25424 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25425 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25426 if (elt && elt != error_mark_node)
25427 {
25428 td = TREE_VALUE (elt);
25429 args = TREE_PURPOSE (elt);
25430 }
25431 }
25432
25433 code_pattern = DECL_TEMPLATE_RESULT (td);
25434
25435 /* We should never be trying to instantiate a member of a class
25436 template or partial specialization. */
25437 gcc_assert (d != code_pattern);
25438
25439 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25440 || DECL_TEMPLATE_SPECIALIZATION (td))
25441 /* In the case of a friend template whose definition is provided
25442 outside the class, we may have too many arguments. Drop the
25443 ones we don't need. The same is true for specializations. */
25444 args = get_innermost_template_args
25445 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25446
25447 if (TREE_CODE (d) == FUNCTION_DECL)
25448 {
25449 deleted_p = DECL_DELETED_FN (code_pattern);
25450 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25451 && DECL_INITIAL (code_pattern) != error_mark_node)
25452 || DECL_DEFAULTED_FN (code_pattern)
25453 || deleted_p);
25454 }
25455 else
25456 {
25457 deleted_p = false;
25458 if (DECL_CLASS_SCOPE_P (code_pattern))
25459 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25460 else
25461 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25462 }
25463
25464 /* We may be in the middle of deferred access check. Disable it now. */
25465 push_deferring_access_checks (dk_no_deferred);
25466
25467 /* Unless an explicit instantiation directive has already determined
25468 the linkage of D, remember that a definition is available for
25469 this entity. */
25470 if (pattern_defined
25471 && !DECL_INTERFACE_KNOWN (d)
25472 && !DECL_NOT_REALLY_EXTERN (d))
25473 mark_definable (d);
25474
25475 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25476 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25477 input_location = DECL_SOURCE_LOCATION (d);
25478
25479 /* If D is a member of an explicitly instantiated class template,
25480 and no definition is available, treat it like an implicit
25481 instantiation. */
25482 if (!pattern_defined && expl_inst_class_mem_p
25483 && DECL_EXPLICIT_INSTANTIATION (d))
25484 {
25485 /* Leave linkage flags alone on instantiations with anonymous
25486 visibility. */
25487 if (TREE_PUBLIC (d))
25488 {
25489 DECL_NOT_REALLY_EXTERN (d) = 0;
25490 DECL_INTERFACE_KNOWN (d) = 0;
25491 }
25492 SET_DECL_IMPLICIT_INSTANTIATION (d);
25493 }
25494
25495 /* Defer all other templates, unless we have been explicitly
25496 forbidden from doing so. */
25497 if (/* If there is no definition, we cannot instantiate the
25498 template. */
25499 ! pattern_defined
25500 /* If it's OK to postpone instantiation, do so. */
25501 || defer_ok
25502 /* If this is a static data member that will be defined
25503 elsewhere, we don't want to instantiate the entire data
25504 member, but we do want to instantiate the initializer so that
25505 we can substitute that elsewhere. */
25506 || (external_p && VAR_P (d))
25507 /* Handle here a deleted function too, avoid generating
25508 its body (c++/61080). */
25509 || deleted_p)
25510 {
25511 /* The definition of the static data member is now required so
25512 we must substitute the initializer. */
25513 if (VAR_P (d)
25514 && !DECL_INITIAL (d)
25515 && DECL_INITIAL (code_pattern))
25516 {
25517 tree ns;
25518 tree init;
25519 bool const_init = false;
25520 bool enter_context = DECL_CLASS_SCOPE_P (d);
25521
25522 ns = decl_namespace_context (d);
25523 push_nested_namespace (ns);
25524 if (enter_context)
25525 push_nested_class (DECL_CONTEXT (d));
25526 init = tsubst_expr (DECL_INITIAL (code_pattern),
25527 args,
25528 tf_warning_or_error, NULL_TREE,
25529 /*integral_constant_expression_p=*/false);
25530 /* If instantiating the initializer involved instantiating this
25531 again, don't call cp_finish_decl twice. */
25532 if (!DECL_INITIAL (d))
25533 {
25534 /* Make sure the initializer is still constant, in case of
25535 circular dependency (template/instantiate6.C). */
25536 const_init
25537 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25538 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25539 /*asmspec_tree=*/NULL_TREE,
25540 LOOKUP_ONLYCONVERTING);
25541 }
25542 if (enter_context)
25543 pop_nested_class ();
25544 pop_nested_namespace (ns);
25545 }
25546
25547 /* We restore the source position here because it's used by
25548 add_pending_template. */
25549 input_location = saved_loc;
25550
25551 if (at_eof && !pattern_defined
25552 && DECL_EXPLICIT_INSTANTIATION (d)
25553 && DECL_NOT_REALLY_EXTERN (d))
25554 /* [temp.explicit]
25555
25556 The definition of a non-exported function template, a
25557 non-exported member function template, or a non-exported
25558 member function or static data member of a class template
25559 shall be present in every translation unit in which it is
25560 explicitly instantiated. */
25561 permerror (input_location, "explicit instantiation of %qD "
25562 "but no definition available", d);
25563
25564 /* If we're in unevaluated context, we just wanted to get the
25565 constant value; this isn't an odr use, so don't queue
25566 a full instantiation. */
25567 if (cp_unevaluated_operand != 0)
25568 goto out;
25569 /* ??? Historically, we have instantiated inline functions, even
25570 when marked as "extern template". */
25571 if (!(external_p && VAR_P (d)))
25572 add_pending_template (d);
25573 goto out;
25574 }
25575
25576 bool push_to_top, nested;
25577 tree fn_context;
25578 fn_context = decl_function_context (d);
25579 if (LAMBDA_FUNCTION_P (d))
25580 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25581 fn_context = NULL_TREE;
25582 nested = current_function_decl != NULL_TREE;
25583 push_to_top = !(nested && fn_context == current_function_decl);
25584
25585 vec<tree> omp_privatization_save;
25586 if (nested)
25587 save_omp_privatization_clauses (omp_privatization_save);
25588
25589 if (push_to_top)
25590 push_to_top_level ();
25591 else
25592 {
25593 gcc_assert (!processing_template_decl);
25594 push_function_context ();
25595 cp_unevaluated_operand = 0;
25596 c_inhibit_evaluation_warnings = 0;
25597 }
25598
25599 if (VAR_P (d))
25600 {
25601 /* The variable might be a lambda's extra scope, and that
25602 lambda's visibility depends on D's. */
25603 maybe_commonize_var (d);
25604 determine_visibility (d);
25605 }
25606
25607 /* Mark D as instantiated so that recursive calls to
25608 instantiate_decl do not try to instantiate it again. */
25609 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25610
25611 /* Regenerate the declaration in case the template has been modified
25612 by a subsequent redeclaration. */
25613 regenerate_decl_from_template (d, td, args);
25614
25615 /* We already set the file and line above. Reset them now in case
25616 they changed as a result of calling regenerate_decl_from_template. */
25617 input_location = DECL_SOURCE_LOCATION (d);
25618
25619 if (VAR_P (d))
25620 {
25621 tree init;
25622 bool const_init = false;
25623
25624 /* Clear out DECL_RTL; whatever was there before may not be right
25625 since we've reset the type of the declaration. */
25626 SET_DECL_RTL (d, NULL);
25627 DECL_IN_AGGR_P (d) = 0;
25628
25629 /* The initializer is placed in DECL_INITIAL by
25630 regenerate_decl_from_template so we don't need to
25631 push/pop_access_scope again here. Pull it out so that
25632 cp_finish_decl can process it. */
25633 init = DECL_INITIAL (d);
25634 DECL_INITIAL (d) = NULL_TREE;
25635 DECL_INITIALIZED_P (d) = 0;
25636
25637 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25638 initializer. That function will defer actual emission until
25639 we have a chance to determine linkage. */
25640 DECL_EXTERNAL (d) = 0;
25641
25642 /* Enter the scope of D so that access-checking works correctly. */
25643 bool enter_context = DECL_CLASS_SCOPE_P (d);
25644 if (enter_context)
25645 push_nested_class (DECL_CONTEXT (d));
25646
25647 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25648 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
25649 ? LOOKUP_CONSTINIT : 0);
25650 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
25651
25652 if (enter_context)
25653 pop_nested_class ();
25654
25655 if (variable_template_p (gen_tmpl))
25656 note_variable_template_instantiation (d);
25657 }
25658 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25659 synthesize_method (d);
25660 else if (TREE_CODE (d) == FUNCTION_DECL)
25661 {
25662 /* Set up the list of local specializations. */
25663 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25664 tree block = NULL_TREE;
25665
25666 /* Set up context. */
25667 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25668 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25669 block = push_stmt_list ();
25670 else
25671 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25672
25673 /* Some typedefs referenced from within the template code need to be
25674 access checked at template instantiation time, i.e now. These
25675 types were added to the template at parsing time. Let's get those
25676 and perform the access checks then. */
25677 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
25678 args);
25679
25680 /* Create substitution entries for the parameters. */
25681 register_parameter_specializations (code_pattern, d);
25682
25683 /* Substitute into the body of the function. */
25684 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25685 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25686 tf_warning_or_error, tmpl);
25687 else
25688 {
25689 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25690 tf_warning_or_error, tmpl,
25691 /*integral_constant_expression_p=*/false);
25692
25693 /* Set the current input_location to the end of the function
25694 so that finish_function knows where we are. */
25695 input_location
25696 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25697
25698 /* Remember if we saw an infinite loop in the template. */
25699 current_function_infinite_loop
25700 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25701 }
25702
25703 /* Finish the function. */
25704 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25705 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25706 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25707 else
25708 {
25709 d = finish_function (/*inline_p=*/false);
25710 expand_or_defer_fn (d);
25711 }
25712
25713 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25714 cp_check_omp_declare_reduction (d);
25715 }
25716
25717 /* We're not deferring instantiation any more. */
25718 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25719
25720 if (push_to_top)
25721 pop_from_top_level ();
25722 else
25723 pop_function_context ();
25724
25725 if (nested)
25726 restore_omp_privatization_clauses (omp_privatization_save);
25727
25728 out:
25729 pop_deferring_access_checks ();
25730 timevar_pop (TV_TEMPLATE_INST);
25731 pop_tinst_level ();
25732 input_location = saved_loc;
25733 cp_unevaluated_operand = saved_unevaluated_operand;
25734 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25735
25736 return d;
25737 }
25738
25739 /* Run through the list of templates that we wish we could
25740 instantiate, and instantiate any we can. RETRIES is the
25741 number of times we retry pending template instantiation. */
25742
25743 void
25744 instantiate_pending_templates (int retries)
25745 {
25746 int reconsider;
25747 location_t saved_loc = input_location;
25748
25749 /* Instantiating templates may trigger vtable generation. This in turn
25750 may require further template instantiations. We place a limit here
25751 to avoid infinite loop. */
25752 if (pending_templates && retries >= max_tinst_depth)
25753 {
25754 tree decl = pending_templates->tinst->maybe_get_node ();
25755
25756 fatal_error (input_location,
25757 "template instantiation depth exceeds maximum of %d"
25758 " instantiating %q+D, possibly from virtual table generation"
25759 " (use %<-ftemplate-depth=%> to increase the maximum)",
25760 max_tinst_depth, decl);
25761 if (TREE_CODE (decl) == FUNCTION_DECL)
25762 /* Pretend that we defined it. */
25763 DECL_INITIAL (decl) = error_mark_node;
25764 return;
25765 }
25766
25767 do
25768 {
25769 struct pending_template **t = &pending_templates;
25770 struct pending_template *last = NULL;
25771 reconsider = 0;
25772 while (*t)
25773 {
25774 tree instantiation = reopen_tinst_level ((*t)->tinst);
25775 bool complete = false;
25776
25777 if (TYPE_P (instantiation))
25778 {
25779 if (!COMPLETE_TYPE_P (instantiation))
25780 {
25781 instantiate_class_template (instantiation);
25782 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25783 for (tree fld = TYPE_FIELDS (instantiation);
25784 fld; fld = TREE_CHAIN (fld))
25785 if ((VAR_P (fld)
25786 || (TREE_CODE (fld) == FUNCTION_DECL
25787 && !DECL_ARTIFICIAL (fld)))
25788 && DECL_TEMPLATE_INSTANTIATION (fld))
25789 instantiate_decl (fld,
25790 /*defer_ok=*/false,
25791 /*expl_inst_class_mem_p=*/false);
25792
25793 if (COMPLETE_TYPE_P (instantiation))
25794 reconsider = 1;
25795 }
25796
25797 complete = COMPLETE_TYPE_P (instantiation);
25798 }
25799 else
25800 {
25801 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25802 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25803 {
25804 instantiation
25805 = instantiate_decl (instantiation,
25806 /*defer_ok=*/false,
25807 /*expl_inst_class_mem_p=*/false);
25808 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25809 reconsider = 1;
25810 }
25811
25812 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25813 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25814 }
25815
25816 if (complete)
25817 {
25818 /* If INSTANTIATION has been instantiated, then we don't
25819 need to consider it again in the future. */
25820 struct pending_template *drop = *t;
25821 *t = (*t)->next;
25822 set_refcount_ptr (drop->tinst);
25823 pending_template_freelist ().free (drop);
25824 }
25825 else
25826 {
25827 last = *t;
25828 t = &(*t)->next;
25829 }
25830 tinst_depth = 0;
25831 set_refcount_ptr (current_tinst_level);
25832 }
25833 last_pending_template = last;
25834 }
25835 while (reconsider);
25836
25837 input_location = saved_loc;
25838 }
25839
25840 /* Substitute ARGVEC into T, which is a list of initializers for
25841 either base class or a non-static data member. The TREE_PURPOSEs
25842 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25843 instantiate_decl. */
25844
25845 static tree
25846 tsubst_initializer_list (tree t, tree argvec)
25847 {
25848 tree inits = NULL_TREE;
25849 tree target_ctor = error_mark_node;
25850
25851 for (; t; t = TREE_CHAIN (t))
25852 {
25853 tree decl;
25854 tree init;
25855 tree expanded_bases = NULL_TREE;
25856 tree expanded_arguments = NULL_TREE;
25857 int i, len = 1;
25858
25859 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25860 {
25861 tree expr;
25862 tree arg;
25863
25864 /* Expand the base class expansion type into separate base
25865 classes. */
25866 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25867 tf_warning_or_error,
25868 NULL_TREE);
25869 if (expanded_bases == error_mark_node)
25870 continue;
25871
25872 /* We'll be building separate TREE_LISTs of arguments for
25873 each base. */
25874 len = TREE_VEC_LENGTH (expanded_bases);
25875 expanded_arguments = make_tree_vec (len);
25876 for (i = 0; i < len; i++)
25877 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
25878
25879 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25880 expand each argument in the TREE_VALUE of t. */
25881 expr = make_node (EXPR_PACK_EXPANSION);
25882 PACK_EXPANSION_LOCAL_P (expr) = true;
25883 PACK_EXPANSION_PARAMETER_PACKS (expr) =
25884 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
25885
25886 if (TREE_VALUE (t) == void_type_node)
25887 /* VOID_TYPE_NODE is used to indicate
25888 value-initialization. */
25889 {
25890 for (i = 0; i < len; i++)
25891 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
25892 }
25893 else
25894 {
25895 /* Substitute parameter packs into each argument in the
25896 TREE_LIST. */
25897 in_base_initializer = 1;
25898 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
25899 {
25900 tree expanded_exprs;
25901
25902 /* Expand the argument. */
25903 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25904 expanded_exprs
25905 = tsubst_pack_expansion (expr, argvec,
25906 tf_warning_or_error,
25907 NULL_TREE);
25908 if (expanded_exprs == error_mark_node)
25909 continue;
25910
25911 /* Prepend each of the expanded expressions to the
25912 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25913 for (i = 0; i < len; i++)
25914 {
25915 TREE_VEC_ELT (expanded_arguments, i) =
25916 tree_cons (NULL_TREE,
25917 TREE_VEC_ELT (expanded_exprs, i),
25918 TREE_VEC_ELT (expanded_arguments, i));
25919 }
25920 }
25921 in_base_initializer = 0;
25922
25923 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25924 since we built them backwards. */
25925 for (i = 0; i < len; i++)
25926 {
25927 TREE_VEC_ELT (expanded_arguments, i) =
25928 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25929 }
25930 }
25931 }
25932
25933 for (i = 0; i < len; ++i)
25934 {
25935 if (expanded_bases)
25936 {
25937 decl = TREE_VEC_ELT (expanded_bases, i);
25938 decl = expand_member_init (decl);
25939 init = TREE_VEC_ELT (expanded_arguments, i);
25940 }
25941 else
25942 {
25943 tree tmp;
25944 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25945 tf_warning_or_error, NULL_TREE);
25946
25947 decl = expand_member_init (decl);
25948 if (decl && !DECL_P (decl))
25949 in_base_initializer = 1;
25950
25951 init = TREE_VALUE (t);
25952 tmp = init;
25953 if (init != void_type_node)
25954 init = tsubst_expr (init, argvec,
25955 tf_warning_or_error, NULL_TREE,
25956 /*integral_constant_expression_p=*/false);
25957 if (init == NULL_TREE && tmp != NULL_TREE)
25958 /* If we had an initializer but it instantiated to nothing,
25959 value-initialize the object. This will only occur when
25960 the initializer was a pack expansion where the parameter
25961 packs used in that expansion were of length zero. */
25962 init = void_type_node;
25963 in_base_initializer = 0;
25964 }
25965
25966 if (target_ctor != error_mark_node
25967 && init != error_mark_node)
25968 {
25969 error ("mem-initializer for %qD follows constructor delegation",
25970 decl);
25971 return inits;
25972 }
25973 /* Look for a target constructor. */
25974 if (init != error_mark_node
25975 && decl && CLASS_TYPE_P (decl)
25976 && same_type_p (decl, current_class_type))
25977 {
25978 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25979 if (inits)
25980 {
25981 error ("constructor delegation follows mem-initializer for %qD",
25982 TREE_PURPOSE (inits));
25983 continue;
25984 }
25985 target_ctor = init;
25986 }
25987
25988 if (decl)
25989 {
25990 init = build_tree_list (decl, init);
25991 TREE_CHAIN (init) = inits;
25992 inits = init;
25993 }
25994 }
25995 }
25996 return inits;
25997 }
25998
25999 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
26000
26001 static void
26002 set_current_access_from_decl (tree decl)
26003 {
26004 if (TREE_PRIVATE (decl))
26005 current_access_specifier = access_private_node;
26006 else if (TREE_PROTECTED (decl))
26007 current_access_specifier = access_protected_node;
26008 else
26009 current_access_specifier = access_public_node;
26010 }
26011
26012 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26013 is the instantiation (which should have been created with
26014 start_enum) and ARGS are the template arguments to use. */
26015
26016 static void
26017 tsubst_enum (tree tag, tree newtag, tree args)
26018 {
26019 tree e;
26020
26021 if (SCOPED_ENUM_P (newtag))
26022 begin_scope (sk_scoped_enum, newtag);
26023
26024 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26025 {
26026 tree value;
26027 tree decl;
26028
26029 decl = TREE_VALUE (e);
26030 /* Note that in a template enum, the TREE_VALUE is the
26031 CONST_DECL, not the corresponding INTEGER_CST. */
26032 value = tsubst_expr (DECL_INITIAL (decl),
26033 args, tf_warning_or_error, NULL_TREE,
26034 /*integral_constant_expression_p=*/true);
26035
26036 /* Give this enumeration constant the correct access. */
26037 set_current_access_from_decl (decl);
26038
26039 /* Actually build the enumerator itself. Here we're assuming that
26040 enumerators can't have dependent attributes. */
26041 build_enumerator (DECL_NAME (decl), value, newtag,
26042 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26043 }
26044
26045 if (SCOPED_ENUM_P (newtag))
26046 finish_scope ();
26047
26048 finish_enum_value_list (newtag);
26049 finish_enum (newtag);
26050
26051 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26052 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26053 }
26054
26055 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26056 its type -- but without substituting the innermost set of template
26057 arguments. So, innermost set of template parameters will appear in
26058 the type. */
26059
26060 tree
26061 get_mostly_instantiated_function_type (tree decl)
26062 {
26063 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26064 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26065 }
26066
26067 /* Return truthvalue if we're processing a template different from
26068 the last one involved in diagnostics. */
26069 bool
26070 problematic_instantiation_changed (void)
26071 {
26072 return current_tinst_level != last_error_tinst_level;
26073 }
26074
26075 /* Remember current template involved in diagnostics. */
26076 void
26077 record_last_problematic_instantiation (void)
26078 {
26079 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26080 }
26081
26082 struct tinst_level *
26083 current_instantiation (void)
26084 {
26085 return current_tinst_level;
26086 }
26087
26088 /* Return TRUE if current_function_decl is being instantiated, false
26089 otherwise. */
26090
26091 bool
26092 instantiating_current_function_p (void)
26093 {
26094 return (current_instantiation ()
26095 && (current_instantiation ()->maybe_get_node ()
26096 == current_function_decl));
26097 }
26098
26099 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26100 type. Return false for ok, true for disallowed. Issue error and
26101 inform messages under control of COMPLAIN. */
26102
26103 static bool
26104 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26105 {
26106 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26107 return false;
26108 else if (TYPE_PTR_P (type))
26109 return false;
26110 else if (TYPE_REF_P (type)
26111 && !TYPE_REF_IS_RVALUE (type))
26112 return false;
26113 else if (TYPE_PTRMEM_P (type))
26114 return false;
26115 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26116 {
26117 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx2a)
26118 {
26119 if (complain & tf_error)
26120 error ("non-type template parameters of deduced class type only "
26121 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
26122 return true;
26123 }
26124 return false;
26125 }
26126 else if (TREE_CODE (type) == TYPENAME_TYPE)
26127 return false;
26128 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26129 return false;
26130 else if (TREE_CODE (type) == NULLPTR_TYPE)
26131 return false;
26132 /* A bound template template parm could later be instantiated to have a valid
26133 nontype parm type via an alias template. */
26134 else if (cxx_dialect >= cxx11
26135 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26136 return false;
26137 else if (CLASS_TYPE_P (type))
26138 {
26139 if (cxx_dialect < cxx2a)
26140 {
26141 if (complain & tf_error)
26142 error ("non-type template parameters of class type only available "
26143 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
26144 return true;
26145 }
26146 if (dependent_type_p (type))
26147 return false;
26148 if (!complete_type_or_else (type, NULL_TREE))
26149 return true;
26150 if (!structural_type_p (type))
26151 {
26152 if (complain & tf_error)
26153 {
26154 auto_diagnostic_group d;
26155 error ("%qT is not a valid type for a template non-type "
26156 "parameter because it is not structural", type);
26157 structural_type_p (type, true);
26158 }
26159 return true;
26160 }
26161 return false;
26162 }
26163
26164 if (complain & tf_error)
26165 {
26166 if (type == error_mark_node)
26167 inform (input_location, "invalid template non-type parameter");
26168 else
26169 error ("%q#T is not a valid type for a template non-type parameter",
26170 type);
26171 }
26172 return true;
26173 }
26174
26175 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26176 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26177
26178 static bool
26179 dependent_type_p_r (tree type)
26180 {
26181 tree scope;
26182
26183 /* [temp.dep.type]
26184
26185 A type is dependent if it is:
26186
26187 -- a template parameter. Template template parameters are types
26188 for us (since TYPE_P holds true for them) so we handle
26189 them here. */
26190 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26191 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26192 return true;
26193 /* -- a qualified-id with a nested-name-specifier which contains a
26194 class-name that names a dependent type or whose unqualified-id
26195 names a dependent type. */
26196 if (TREE_CODE (type) == TYPENAME_TYPE)
26197 return true;
26198
26199 /* An alias template specialization can be dependent even if the
26200 resulting type is not. */
26201 if (dependent_alias_template_spec_p (type, nt_transparent))
26202 return true;
26203
26204 /* -- a cv-qualified type where the cv-unqualified type is
26205 dependent.
26206 No code is necessary for this bullet; the code below handles
26207 cv-qualified types, and we don't want to strip aliases with
26208 TYPE_MAIN_VARIANT because of DR 1558. */
26209 /* -- a compound type constructed from any dependent type. */
26210 if (TYPE_PTRMEM_P (type))
26211 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26212 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26213 (type)));
26214 else if (INDIRECT_TYPE_P (type))
26215 return dependent_type_p (TREE_TYPE (type));
26216 else if (FUNC_OR_METHOD_TYPE_P (type))
26217 {
26218 tree arg_type;
26219
26220 if (dependent_type_p (TREE_TYPE (type)))
26221 return true;
26222 for (arg_type = TYPE_ARG_TYPES (type);
26223 arg_type;
26224 arg_type = TREE_CHAIN (arg_type))
26225 if (dependent_type_p (TREE_VALUE (arg_type)))
26226 return true;
26227 if (cxx_dialect >= cxx17)
26228 /* A value-dependent noexcept-specifier makes the type dependent. */
26229 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26230 if (tree noex = TREE_PURPOSE (spec))
26231 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26232 affect overload resolution and treating it as dependent breaks
26233 things. Same for an unparsed noexcept expression. */
26234 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26235 && TREE_CODE (noex) != DEFERRED_PARSE
26236 && value_dependent_expression_p (noex))
26237 return true;
26238 return false;
26239 }
26240 /* -- an array type constructed from any dependent type or whose
26241 size is specified by a constant expression that is
26242 value-dependent.
26243
26244 We checked for type- and value-dependence of the bounds in
26245 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26246 if (TREE_CODE (type) == ARRAY_TYPE)
26247 {
26248 if (TYPE_DOMAIN (type)
26249 && dependent_type_p (TYPE_DOMAIN (type)))
26250 return true;
26251 return dependent_type_p (TREE_TYPE (type));
26252 }
26253
26254 /* -- a template-id in which either the template name is a template
26255 parameter ... */
26256 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26257 return true;
26258 /* ... or any of the template arguments is a dependent type or
26259 an expression that is type-dependent or value-dependent. */
26260 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26261 && (any_dependent_template_arguments_p
26262 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26263 return true;
26264
26265 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26266 dependent; if the argument of the `typeof' expression is not
26267 type-dependent, then it should already been have resolved. */
26268 if (TREE_CODE (type) == TYPEOF_TYPE
26269 || TREE_CODE (type) == DECLTYPE_TYPE
26270 || TREE_CODE (type) == UNDERLYING_TYPE)
26271 return true;
26272
26273 /* A template argument pack is dependent if any of its packed
26274 arguments are. */
26275 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26276 {
26277 tree args = ARGUMENT_PACK_ARGS (type);
26278 int i, len = TREE_VEC_LENGTH (args);
26279 for (i = 0; i < len; ++i)
26280 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26281 return true;
26282 }
26283
26284 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26285 be template parameters. */
26286 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26287 return true;
26288
26289 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26290 return true;
26291
26292 /* The standard does not specifically mention types that are local
26293 to template functions or local classes, but they should be
26294 considered dependent too. For example:
26295
26296 template <int I> void f() {
26297 enum E { a = I };
26298 S<sizeof (E)> s;
26299 }
26300
26301 The size of `E' cannot be known until the value of `I' has been
26302 determined. Therefore, `E' must be considered dependent. */
26303 scope = TYPE_CONTEXT (type);
26304 if (scope && TYPE_P (scope))
26305 return dependent_type_p (scope);
26306 /* Don't use type_dependent_expression_p here, as it can lead
26307 to infinite recursion trying to determine whether a lambda
26308 nested in a lambda is dependent (c++/47687). */
26309 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26310 && DECL_LANG_SPECIFIC (scope)
26311 && DECL_TEMPLATE_INFO (scope)
26312 && (any_dependent_template_arguments_p
26313 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26314 return true;
26315
26316 /* Other types are non-dependent. */
26317 return false;
26318 }
26319
26320 /* Returns TRUE if TYPE is dependent, in the sense of
26321 [temp.dep.type]. Note that a NULL type is considered dependent. */
26322
26323 bool
26324 dependent_type_p (tree type)
26325 {
26326 /* If there are no template parameters in scope, then there can't be
26327 any dependent types. */
26328 if (!processing_template_decl)
26329 {
26330 /* If we are not processing a template, then nobody should be
26331 providing us with a dependent type. */
26332 gcc_assert (type);
26333 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26334 return false;
26335 }
26336
26337 /* If the type is NULL, we have not computed a type for the entity
26338 in question; in that case, the type is dependent. */
26339 if (!type)
26340 return true;
26341
26342 /* Erroneous types can be considered non-dependent. */
26343 if (type == error_mark_node)
26344 return false;
26345
26346 /* Getting here with global_type_node means we improperly called this
26347 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26348 gcc_checking_assert (type != global_type_node);
26349
26350 /* If we have not already computed the appropriate value for TYPE,
26351 do so now. */
26352 if (!TYPE_DEPENDENT_P_VALID (type))
26353 {
26354 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26355 TYPE_DEPENDENT_P_VALID (type) = 1;
26356 }
26357
26358 return TYPE_DEPENDENT_P (type);
26359 }
26360
26361 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26362 lookup. In other words, a dependent type that is not the current
26363 instantiation. */
26364
26365 bool
26366 dependent_scope_p (tree scope)
26367 {
26368 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26369 && !currently_open_class (scope));
26370 }
26371
26372 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26373 an unknown base of 'this' (and is therefore instantiation-dependent). */
26374
26375 static bool
26376 unknown_base_ref_p (tree t)
26377 {
26378 if (!current_class_ptr)
26379 return false;
26380
26381 tree mem = TREE_OPERAND (t, 1);
26382 if (shared_member_p (mem))
26383 return false;
26384
26385 tree cur = current_nonlambda_class_type ();
26386 if (!any_dependent_bases_p (cur))
26387 return false;
26388
26389 tree ctx = TREE_OPERAND (t, 0);
26390 if (DERIVED_FROM_P (ctx, cur))
26391 return false;
26392
26393 return true;
26394 }
26395
26396 /* T is a SCOPE_REF; return whether we need to consider it
26397 instantiation-dependent so that we can check access at instantiation
26398 time even though we know which member it resolves to. */
26399
26400 static bool
26401 instantiation_dependent_scope_ref_p (tree t)
26402 {
26403 if (DECL_P (TREE_OPERAND (t, 1))
26404 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26405 && !unknown_base_ref_p (t)
26406 && accessible_in_template_p (TREE_OPERAND (t, 0),
26407 TREE_OPERAND (t, 1)))
26408 return false;
26409 else
26410 return true;
26411 }
26412
26413 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26414 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26415 expression. */
26416
26417 /* Note that this predicate is not appropriate for general expressions;
26418 only constant expressions (that satisfy potential_constant_expression)
26419 can be tested for value dependence. */
26420
26421 bool
26422 value_dependent_expression_p (tree expression)
26423 {
26424 if (!processing_template_decl || expression == NULL_TREE)
26425 return false;
26426
26427 /* A type-dependent expression is also value-dependent. */
26428 if (type_dependent_expression_p (expression))
26429 return true;
26430
26431 switch (TREE_CODE (expression))
26432 {
26433 case BASELINK:
26434 /* A dependent member function of the current instantiation. */
26435 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26436
26437 case FUNCTION_DECL:
26438 /* A dependent member function of the current instantiation. */
26439 if (DECL_CLASS_SCOPE_P (expression)
26440 && dependent_type_p (DECL_CONTEXT (expression)))
26441 return true;
26442 break;
26443
26444 case IDENTIFIER_NODE:
26445 /* A name that has not been looked up -- must be dependent. */
26446 return true;
26447
26448 case TEMPLATE_PARM_INDEX:
26449 /* A non-type template parm. */
26450 return true;
26451
26452 case CONST_DECL:
26453 /* A non-type template parm. */
26454 if (DECL_TEMPLATE_PARM_P (expression))
26455 return true;
26456 return value_dependent_expression_p (DECL_INITIAL (expression));
26457
26458 case VAR_DECL:
26459 /* A constant with literal type and is initialized
26460 with an expression that is value-dependent. */
26461 if (DECL_DEPENDENT_INIT_P (expression)
26462 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26463 || TYPE_REF_P (TREE_TYPE (expression)))
26464 return true;
26465 if (DECL_HAS_VALUE_EXPR_P (expression))
26466 {
26467 tree value_expr = DECL_VALUE_EXPR (expression);
26468 if (value_dependent_expression_p (value_expr)
26469 /* __PRETTY_FUNCTION__ inside a template function is dependent
26470 on the name of the function. */
26471 || (DECL_PRETTY_FUNCTION_P (expression)
26472 /* It might be used in a template, but not a template
26473 function, in which case its DECL_VALUE_EXPR will be
26474 "top level". */
26475 && value_expr == error_mark_node))
26476 return true;
26477 }
26478 return false;
26479
26480 case DYNAMIC_CAST_EXPR:
26481 case STATIC_CAST_EXPR:
26482 case CONST_CAST_EXPR:
26483 case REINTERPRET_CAST_EXPR:
26484 case CAST_EXPR:
26485 case IMPLICIT_CONV_EXPR:
26486 /* These expressions are value-dependent if the type to which
26487 the cast occurs is dependent or the expression being casted
26488 is value-dependent. */
26489 {
26490 tree type = TREE_TYPE (expression);
26491
26492 if (dependent_type_p (type))
26493 return true;
26494
26495 /* A functional cast has a list of operands. */
26496 expression = TREE_OPERAND (expression, 0);
26497 if (!expression)
26498 {
26499 /* If there are no operands, it must be an expression such
26500 as "int()". This should not happen for aggregate types
26501 because it would form non-constant expressions. */
26502 gcc_assert (cxx_dialect >= cxx11
26503 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26504
26505 return false;
26506 }
26507
26508 if (TREE_CODE (expression) == TREE_LIST)
26509 return any_value_dependent_elements_p (expression);
26510
26511 return value_dependent_expression_p (expression);
26512 }
26513
26514 case SIZEOF_EXPR:
26515 if (SIZEOF_EXPR_TYPE_P (expression))
26516 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26517 /* FALLTHRU */
26518 case ALIGNOF_EXPR:
26519 case TYPEID_EXPR:
26520 /* A `sizeof' expression is value-dependent if the operand is
26521 type-dependent or is a pack expansion. */
26522 expression = TREE_OPERAND (expression, 0);
26523 if (PACK_EXPANSION_P (expression))
26524 return true;
26525 else if (TYPE_P (expression))
26526 return dependent_type_p (expression);
26527 return instantiation_dependent_uneval_expression_p (expression);
26528
26529 case AT_ENCODE_EXPR:
26530 /* An 'encode' expression is value-dependent if the operand is
26531 type-dependent. */
26532 expression = TREE_OPERAND (expression, 0);
26533 return dependent_type_p (expression);
26534
26535 case NOEXCEPT_EXPR:
26536 expression = TREE_OPERAND (expression, 0);
26537 return instantiation_dependent_uneval_expression_p (expression);
26538
26539 case SCOPE_REF:
26540 /* All instantiation-dependent expressions should also be considered
26541 value-dependent. */
26542 return instantiation_dependent_scope_ref_p (expression);
26543
26544 case COMPONENT_REF:
26545 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26546 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26547
26548 case NONTYPE_ARGUMENT_PACK:
26549 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26550 is value-dependent. */
26551 {
26552 tree values = ARGUMENT_PACK_ARGS (expression);
26553 int i, len = TREE_VEC_LENGTH (values);
26554
26555 for (i = 0; i < len; ++i)
26556 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26557 return true;
26558
26559 return false;
26560 }
26561
26562 case TRAIT_EXPR:
26563 {
26564 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26565
26566 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26567 return true;
26568
26569 if (!type2)
26570 return false;
26571
26572 if (TREE_CODE (type2) != TREE_LIST)
26573 return dependent_type_p (type2);
26574
26575 for (; type2; type2 = TREE_CHAIN (type2))
26576 if (dependent_type_p (TREE_VALUE (type2)))
26577 return true;
26578
26579 return false;
26580 }
26581
26582 case MODOP_EXPR:
26583 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26584 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26585
26586 case ARRAY_REF:
26587 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26588 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26589
26590 case ADDR_EXPR:
26591 {
26592 tree op = TREE_OPERAND (expression, 0);
26593 return (value_dependent_expression_p (op)
26594 || has_value_dependent_address (op));
26595 }
26596
26597 case REQUIRES_EXPR:
26598 /* Treat all requires-expressions as value-dependent so
26599 we don't try to fold them. */
26600 return true;
26601
26602 case TYPE_REQ:
26603 return dependent_type_p (TREE_OPERAND (expression, 0));
26604
26605 case CALL_EXPR:
26606 {
26607 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26608 return true;
26609 tree fn = get_callee_fndecl (expression);
26610 int i, nargs;
26611 nargs = call_expr_nargs (expression);
26612 for (i = 0; i < nargs; ++i)
26613 {
26614 tree op = CALL_EXPR_ARG (expression, i);
26615 /* In a call to a constexpr member function, look through the
26616 implicit ADDR_EXPR on the object argument so that it doesn't
26617 cause the call to be considered value-dependent. We also
26618 look through it in potential_constant_expression. */
26619 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26620 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26621 && TREE_CODE (op) == ADDR_EXPR)
26622 op = TREE_OPERAND (op, 0);
26623 if (value_dependent_expression_p (op))
26624 return true;
26625 }
26626 return false;
26627 }
26628
26629 case TEMPLATE_ID_EXPR:
26630 return concept_definition_p (TREE_OPERAND (expression, 0));
26631
26632 case CONSTRUCTOR:
26633 {
26634 unsigned ix;
26635 tree val;
26636 if (dependent_type_p (TREE_TYPE (expression)))
26637 return true;
26638 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26639 if (value_dependent_expression_p (val))
26640 return true;
26641 return false;
26642 }
26643
26644 case STMT_EXPR:
26645 /* Treat a GNU statement expression as dependent to avoid crashing
26646 under instantiate_non_dependent_expr; it can't be constant. */
26647 return true;
26648
26649 default:
26650 /* A constant expression is value-dependent if any subexpression is
26651 value-dependent. */
26652 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26653 {
26654 case tcc_reference:
26655 case tcc_unary:
26656 case tcc_comparison:
26657 case tcc_binary:
26658 case tcc_expression:
26659 case tcc_vl_exp:
26660 {
26661 int i, len = cp_tree_operand_length (expression);
26662
26663 for (i = 0; i < len; i++)
26664 {
26665 tree t = TREE_OPERAND (expression, i);
26666
26667 /* In some cases, some of the operands may be missing.
26668 (For example, in the case of PREDECREMENT_EXPR, the
26669 amount to increment by may be missing.) That doesn't
26670 make the expression dependent. */
26671 if (t && value_dependent_expression_p (t))
26672 return true;
26673 }
26674 }
26675 break;
26676 default:
26677 break;
26678 }
26679 break;
26680 }
26681
26682 /* The expression is not value-dependent. */
26683 return false;
26684 }
26685
26686 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26687 [temp.dep.expr]. Note that an expression with no type is
26688 considered dependent. Other parts of the compiler arrange for an
26689 expression with type-dependent subexpressions to have no type, so
26690 this function doesn't have to be fully recursive. */
26691
26692 bool
26693 type_dependent_expression_p (tree expression)
26694 {
26695 if (!processing_template_decl)
26696 return false;
26697
26698 if (expression == NULL_TREE || expression == error_mark_node)
26699 return false;
26700
26701 STRIP_ANY_LOCATION_WRAPPER (expression);
26702
26703 /* An unresolved name is always dependent. */
26704 if (identifier_p (expression)
26705 || TREE_CODE (expression) == USING_DECL
26706 || TREE_CODE (expression) == WILDCARD_DECL)
26707 return true;
26708
26709 /* A lambda-expression in template context is dependent. dependent_type_p is
26710 true for a lambda in the scope of a class or function template, but that
26711 doesn't cover all template contexts, like a default template argument. */
26712 if (TREE_CODE (expression) == LAMBDA_EXPR)
26713 return true;
26714
26715 /* A fold expression is type-dependent. */
26716 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26717 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26718 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26719 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26720 return true;
26721
26722 /* Some expression forms are never type-dependent. */
26723 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
26724 || TREE_CODE (expression) == SIZEOF_EXPR
26725 || TREE_CODE (expression) == ALIGNOF_EXPR
26726 || TREE_CODE (expression) == AT_ENCODE_EXPR
26727 || TREE_CODE (expression) == NOEXCEPT_EXPR
26728 || TREE_CODE (expression) == TRAIT_EXPR
26729 || TREE_CODE (expression) == TYPEID_EXPR
26730 || TREE_CODE (expression) == DELETE_EXPR
26731 || TREE_CODE (expression) == VEC_DELETE_EXPR
26732 || TREE_CODE (expression) == THROW_EXPR
26733 || TREE_CODE (expression) == REQUIRES_EXPR)
26734 return false;
26735
26736 /* The types of these expressions depends only on the type to which
26737 the cast occurs. */
26738 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26739 || TREE_CODE (expression) == STATIC_CAST_EXPR
26740 || TREE_CODE (expression) == CONST_CAST_EXPR
26741 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26742 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26743 || TREE_CODE (expression) == CAST_EXPR)
26744 return dependent_type_p (TREE_TYPE (expression));
26745
26746 /* The types of these expressions depends only on the type created
26747 by the expression. */
26748 if (TREE_CODE (expression) == NEW_EXPR
26749 || TREE_CODE (expression) == VEC_NEW_EXPR)
26750 {
26751 /* For NEW_EXPR tree nodes created inside a template, either
26752 the object type itself or a TREE_LIST may appear as the
26753 operand 1. */
26754 tree type = TREE_OPERAND (expression, 1);
26755 if (TREE_CODE (type) == TREE_LIST)
26756 /* This is an array type. We need to check array dimensions
26757 as well. */
26758 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26759 || value_dependent_expression_p
26760 (TREE_OPERAND (TREE_VALUE (type), 1));
26761 else
26762 return dependent_type_p (type);
26763 }
26764
26765 if (TREE_CODE (expression) == SCOPE_REF)
26766 {
26767 tree scope = TREE_OPERAND (expression, 0);
26768 tree name = TREE_OPERAND (expression, 1);
26769
26770 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26771 contains an identifier associated by name lookup with one or more
26772 declarations declared with a dependent type, or...a
26773 nested-name-specifier or qualified-id that names a member of an
26774 unknown specialization. */
26775 return (type_dependent_expression_p (name)
26776 || dependent_scope_p (scope));
26777 }
26778
26779 if (TREE_CODE (expression) == TEMPLATE_DECL
26780 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26781 return uses_outer_template_parms (expression);
26782
26783 if (TREE_CODE (expression) == STMT_EXPR)
26784 expression = stmt_expr_value_expr (expression);
26785
26786 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26787 {
26788 tree elt;
26789 unsigned i;
26790
26791 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26792 {
26793 if (type_dependent_expression_p (elt))
26794 return true;
26795 }
26796 return false;
26797 }
26798
26799 /* A static data member of the current instantiation with incomplete
26800 array type is type-dependent, as the definition and specializations
26801 can have different bounds. */
26802 if (VAR_P (expression)
26803 && DECL_CLASS_SCOPE_P (expression)
26804 && dependent_type_p (DECL_CONTEXT (expression))
26805 && VAR_HAD_UNKNOWN_BOUND (expression))
26806 return true;
26807
26808 /* An array of unknown bound depending on a variadic parameter, eg:
26809
26810 template<typename... Args>
26811 void foo (Args... args)
26812 {
26813 int arr[] = { args... };
26814 }
26815
26816 template<int... vals>
26817 void bar ()
26818 {
26819 int arr[] = { vals... };
26820 }
26821
26822 If the array has no length and has an initializer, it must be that
26823 we couldn't determine its length in cp_complete_array_type because
26824 it is dependent. */
26825 if (VAR_P (expression)
26826 && TREE_TYPE (expression) != NULL_TREE
26827 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26828 && !TYPE_DOMAIN (TREE_TYPE (expression))
26829 && DECL_INITIAL (expression))
26830 return true;
26831
26832 /* A function or variable template-id is type-dependent if it has any
26833 dependent template arguments. */
26834 if (VAR_OR_FUNCTION_DECL_P (expression)
26835 && DECL_LANG_SPECIFIC (expression)
26836 && DECL_TEMPLATE_INFO (expression))
26837 {
26838 /* Consider the innermost template arguments, since those are the ones
26839 that come from the template-id; the template arguments for the
26840 enclosing class do not make it type-dependent unless they are used in
26841 the type of the decl. */
26842 if (instantiates_primary_template_p (expression)
26843 && (any_dependent_template_arguments_p
26844 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26845 return true;
26846 }
26847
26848 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26849 type-dependent. Checking this is important for functions with auto return
26850 type, which looks like a dependent type. */
26851 if (TREE_CODE (expression) == FUNCTION_DECL
26852 && !(DECL_CLASS_SCOPE_P (expression)
26853 && dependent_type_p (DECL_CONTEXT (expression)))
26854 && !(DECL_LANG_SPECIFIC (expression)
26855 && DECL_FRIEND_P (expression)
26856 && (!DECL_FRIEND_CONTEXT (expression)
26857 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26858 && !DECL_LOCAL_FUNCTION_P (expression))
26859 {
26860 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26861 || undeduced_auto_decl (expression));
26862 return false;
26863 }
26864
26865 /* Always dependent, on the number of arguments if nothing else. */
26866 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
26867 return true;
26868
26869 if (TREE_TYPE (expression) == unknown_type_node)
26870 {
26871 if (TREE_CODE (expression) == ADDR_EXPR)
26872 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
26873 if (TREE_CODE (expression) == COMPONENT_REF
26874 || TREE_CODE (expression) == OFFSET_REF)
26875 {
26876 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
26877 return true;
26878 expression = TREE_OPERAND (expression, 1);
26879 if (identifier_p (expression))
26880 return false;
26881 }
26882 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26883 if (TREE_CODE (expression) == SCOPE_REF)
26884 return false;
26885
26886 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
26887 if (TREE_CODE (expression) == CO_AWAIT_EXPR
26888 || TREE_CODE (expression) == CO_YIELD_EXPR)
26889 return true;
26890
26891 if (BASELINK_P (expression))
26892 {
26893 if (BASELINK_OPTYPE (expression)
26894 && dependent_type_p (BASELINK_OPTYPE (expression)))
26895 return true;
26896 expression = BASELINK_FUNCTIONS (expression);
26897 }
26898
26899 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
26900 {
26901 if (any_dependent_template_arguments_p
26902 (TREE_OPERAND (expression, 1)))
26903 return true;
26904 expression = TREE_OPERAND (expression, 0);
26905 if (identifier_p (expression))
26906 return true;
26907 }
26908
26909 gcc_assert (OVL_P (expression));
26910
26911 for (lkp_iterator iter (expression); iter; ++iter)
26912 if (type_dependent_expression_p (*iter))
26913 return true;
26914
26915 return false;
26916 }
26917
26918 /* The type of a non-type template parm declared with a placeholder type
26919 depends on the corresponding template argument, even though
26920 placeholders are not normally considered dependent. */
26921 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26922 && is_auto (TREE_TYPE (expression)))
26923 return true;
26924
26925 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26926
26927 /* Dependent type attributes might not have made it from the decl to
26928 the type yet. */
26929 if (DECL_P (expression)
26930 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26931 return true;
26932
26933 return (dependent_type_p (TREE_TYPE (expression)));
26934 }
26935
26936 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26937 type-dependent if the expression refers to a member of the current
26938 instantiation and the type of the referenced member is dependent, or the
26939 class member access expression refers to a member of an unknown
26940 specialization.
26941
26942 This function returns true if the OBJECT in such a class member access
26943 expression is of an unknown specialization. */
26944
26945 bool
26946 type_dependent_object_expression_p (tree object)
26947 {
26948 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26949 dependent. */
26950 if (TREE_CODE (object) == IDENTIFIER_NODE)
26951 return true;
26952 tree scope = TREE_TYPE (object);
26953 return (!scope || dependent_scope_p (scope));
26954 }
26955
26956 /* walk_tree callback function for instantiation_dependent_expression_p,
26957 below. Returns non-zero if a dependent subexpression is found. */
26958
26959 static tree
26960 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26961 void * /*data*/)
26962 {
26963 if (TYPE_P (*tp))
26964 {
26965 /* We don't have to worry about decltype currently because decltype
26966 of an instantiation-dependent expr is a dependent type. This
26967 might change depending on the resolution of DR 1172. */
26968 *walk_subtrees = false;
26969 return NULL_TREE;
26970 }
26971 enum tree_code code = TREE_CODE (*tp);
26972 switch (code)
26973 {
26974 /* Don't treat an argument list as dependent just because it has no
26975 TREE_TYPE. */
26976 case TREE_LIST:
26977 case TREE_VEC:
26978 case NONTYPE_ARGUMENT_PACK:
26979 return NULL_TREE;
26980
26981 case TEMPLATE_PARM_INDEX:
26982 if (dependent_type_p (TREE_TYPE (*tp)))
26983 return *tp;
26984 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26985 return *tp;
26986 /* We'll check value-dependence separately. */
26987 return NULL_TREE;
26988
26989 /* Handle expressions with type operands. */
26990 case SIZEOF_EXPR:
26991 case ALIGNOF_EXPR:
26992 case TYPEID_EXPR:
26993 case AT_ENCODE_EXPR:
26994 {
26995 tree op = TREE_OPERAND (*tp, 0);
26996 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26997 op = TREE_TYPE (op);
26998 if (TYPE_P (op))
26999 {
27000 if (dependent_type_p (op))
27001 return *tp;
27002 else
27003 {
27004 *walk_subtrees = false;
27005 return NULL_TREE;
27006 }
27007 }
27008 break;
27009 }
27010
27011 case COMPONENT_REF:
27012 if (identifier_p (TREE_OPERAND (*tp, 1)))
27013 /* In a template, finish_class_member_access_expr creates a
27014 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27015 type-dependent, so that we can check access control at
27016 instantiation time (PR 42277). See also Core issue 1273. */
27017 return *tp;
27018 break;
27019
27020 case SCOPE_REF:
27021 if (instantiation_dependent_scope_ref_p (*tp))
27022 return *tp;
27023 else
27024 break;
27025
27026 /* Treat statement-expressions as dependent. */
27027 case BIND_EXPR:
27028 return *tp;
27029
27030 /* Treat requires-expressions as dependent. */
27031 case REQUIRES_EXPR:
27032 return *tp;
27033
27034 case CALL_EXPR:
27035 /* Treat concept checks as dependent. */
27036 if (concept_check_p (*tp))
27037 return *tp;
27038 break;
27039
27040 case TEMPLATE_ID_EXPR:
27041 /* Treat concept checks as dependent. */
27042 if (concept_check_p (*tp))
27043 return *tp;
27044 break;
27045
27046 case CONSTRUCTOR:
27047 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27048 return *tp;
27049 break;
27050
27051 default:
27052 break;
27053 }
27054
27055 if (type_dependent_expression_p (*tp))
27056 return *tp;
27057 else
27058 return NULL_TREE;
27059 }
27060
27061 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27062 sense defined by the ABI:
27063
27064 "An expression is instantiation-dependent if it is type-dependent
27065 or value-dependent, or it has a subexpression that is type-dependent
27066 or value-dependent."
27067
27068 Except don't actually check value-dependence for unevaluated expressions,
27069 because in sizeof(i) we don't care about the value of i. Checking
27070 type-dependence will in turn check value-dependence of array bounds/template
27071 arguments as needed. */
27072
27073 bool
27074 instantiation_dependent_uneval_expression_p (tree expression)
27075 {
27076 tree result;
27077
27078 if (!processing_template_decl)
27079 return false;
27080
27081 if (expression == error_mark_node)
27082 return false;
27083
27084 result = cp_walk_tree_without_duplicates (&expression,
27085 instantiation_dependent_r, NULL);
27086 return result != NULL_TREE;
27087 }
27088
27089 /* As above, but also check value-dependence of the expression as a whole. */
27090
27091 bool
27092 instantiation_dependent_expression_p (tree expression)
27093 {
27094 return (instantiation_dependent_uneval_expression_p (expression)
27095 || value_dependent_expression_p (expression));
27096 }
27097
27098 /* Like type_dependent_expression_p, but it also works while not processing
27099 a template definition, i.e. during substitution or mangling. */
27100
27101 bool
27102 type_dependent_expression_p_push (tree expr)
27103 {
27104 bool b;
27105 ++processing_template_decl;
27106 b = type_dependent_expression_p (expr);
27107 --processing_template_decl;
27108 return b;
27109 }
27110
27111 /* Returns TRUE if ARGS contains a type-dependent expression. */
27112
27113 bool
27114 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27115 {
27116 unsigned int i;
27117 tree arg;
27118
27119 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27120 {
27121 if (type_dependent_expression_p (arg))
27122 return true;
27123 }
27124 return false;
27125 }
27126
27127 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27128 expressions) contains any type-dependent expressions. */
27129
27130 bool
27131 any_type_dependent_elements_p (const_tree list)
27132 {
27133 for (; list; list = TREE_CHAIN (list))
27134 if (type_dependent_expression_p (TREE_VALUE (list)))
27135 return true;
27136
27137 return false;
27138 }
27139
27140 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27141 expressions) contains any value-dependent expressions. */
27142
27143 bool
27144 any_value_dependent_elements_p (const_tree list)
27145 {
27146 for (; list; list = TREE_CHAIN (list))
27147 if (value_dependent_expression_p (TREE_VALUE (list)))
27148 return true;
27149
27150 return false;
27151 }
27152
27153 /* Returns TRUE if the ARG (a template argument) is dependent. */
27154
27155 bool
27156 dependent_template_arg_p (tree arg)
27157 {
27158 if (!processing_template_decl)
27159 return false;
27160
27161 /* Assume a template argument that was wrongly written by the user
27162 is dependent. This is consistent with what
27163 any_dependent_template_arguments_p [that calls this function]
27164 does. */
27165 if (!arg || arg == error_mark_node)
27166 return true;
27167
27168 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27169 arg = argument_pack_select_arg (arg);
27170
27171 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27172 return true;
27173 if (TREE_CODE (arg) == TEMPLATE_DECL)
27174 {
27175 if (DECL_TEMPLATE_PARM_P (arg))
27176 return true;
27177 /* A member template of a dependent class is not necessarily
27178 type-dependent, but it is a dependent template argument because it
27179 will be a member of an unknown specialization to that template. */
27180 tree scope = CP_DECL_CONTEXT (arg);
27181 return TYPE_P (scope) && dependent_type_p (scope);
27182 }
27183 else if (ARGUMENT_PACK_P (arg))
27184 {
27185 tree args = ARGUMENT_PACK_ARGS (arg);
27186 int i, len = TREE_VEC_LENGTH (args);
27187 for (i = 0; i < len; ++i)
27188 {
27189 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27190 return true;
27191 }
27192
27193 return false;
27194 }
27195 else if (TYPE_P (arg))
27196 return dependent_type_p (arg);
27197 else
27198 return value_dependent_expression_p (arg);
27199 }
27200
27201 /* Returns true if ARGS (a collection of template arguments) contains
27202 any types that require structural equality testing. */
27203
27204 bool
27205 any_template_arguments_need_structural_equality_p (tree args)
27206 {
27207 int i;
27208 int j;
27209
27210 if (!args)
27211 return false;
27212 if (args == error_mark_node)
27213 return true;
27214
27215 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27216 {
27217 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27218 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27219 {
27220 tree arg = TREE_VEC_ELT (level, j);
27221 tree packed_args = NULL_TREE;
27222 int k, len = 1;
27223
27224 if (ARGUMENT_PACK_P (arg))
27225 {
27226 /* Look inside the argument pack. */
27227 packed_args = ARGUMENT_PACK_ARGS (arg);
27228 len = TREE_VEC_LENGTH (packed_args);
27229 }
27230
27231 for (k = 0; k < len; ++k)
27232 {
27233 if (packed_args)
27234 arg = TREE_VEC_ELT (packed_args, k);
27235
27236 if (error_operand_p (arg))
27237 return true;
27238 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27239 continue;
27240 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27241 return true;
27242 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27243 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27244 return true;
27245 }
27246 }
27247 }
27248
27249 return false;
27250 }
27251
27252 /* Returns true if ARGS (a collection of template arguments) contains
27253 any dependent arguments. */
27254
27255 bool
27256 any_dependent_template_arguments_p (const_tree args)
27257 {
27258 int i;
27259 int j;
27260
27261 if (!args)
27262 return false;
27263 if (args == error_mark_node)
27264 return true;
27265
27266 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27267 {
27268 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27269 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27270 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27271 return true;
27272 }
27273
27274 return false;
27275 }
27276
27277 /* Returns true if ARGS contains any errors. */
27278
27279 bool
27280 any_erroneous_template_args_p (const_tree args)
27281 {
27282 int i;
27283 int j;
27284
27285 if (args == error_mark_node)
27286 return true;
27287
27288 if (args && TREE_CODE (args) != TREE_VEC)
27289 {
27290 if (tree ti = get_template_info (args))
27291 args = TI_ARGS (ti);
27292 else
27293 args = NULL_TREE;
27294 }
27295
27296 if (!args)
27297 return false;
27298
27299 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27300 {
27301 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27302 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27303 if (error_operand_p (TREE_VEC_ELT (level, j)))
27304 return true;
27305 }
27306
27307 return false;
27308 }
27309
27310 /* Returns TRUE if the template TMPL is type-dependent. */
27311
27312 bool
27313 dependent_template_p (tree tmpl)
27314 {
27315 if (TREE_CODE (tmpl) == OVERLOAD)
27316 {
27317 for (lkp_iterator iter (tmpl); iter; ++iter)
27318 if (dependent_template_p (*iter))
27319 return true;
27320 return false;
27321 }
27322
27323 /* Template template parameters are dependent. */
27324 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27325 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27326 return true;
27327 /* So are names that have not been looked up. */
27328 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27329 return true;
27330 return false;
27331 }
27332
27333 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27334
27335 bool
27336 dependent_template_id_p (tree tmpl, tree args)
27337 {
27338 return (dependent_template_p (tmpl)
27339 || any_dependent_template_arguments_p (args));
27340 }
27341
27342 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27343 are dependent. */
27344
27345 bool
27346 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27347 {
27348 int i;
27349
27350 if (!processing_template_decl)
27351 return false;
27352
27353 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27354 {
27355 tree decl = TREE_VEC_ELT (declv, i);
27356 tree init = TREE_VEC_ELT (initv, i);
27357 tree cond = TREE_VEC_ELT (condv, i);
27358 tree incr = TREE_VEC_ELT (incrv, i);
27359
27360 if (type_dependent_expression_p (decl)
27361 || TREE_CODE (decl) == SCOPE_REF)
27362 return true;
27363
27364 if (init && type_dependent_expression_p (init))
27365 return true;
27366
27367 if (cond == global_namespace)
27368 return true;
27369
27370 if (type_dependent_expression_p (cond))
27371 return true;
27372
27373 if (COMPARISON_CLASS_P (cond)
27374 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27375 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27376 return true;
27377
27378 if (TREE_CODE (incr) == MODOP_EXPR)
27379 {
27380 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27381 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27382 return true;
27383 }
27384 else if (type_dependent_expression_p (incr))
27385 return true;
27386 else if (TREE_CODE (incr) == MODIFY_EXPR)
27387 {
27388 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27389 return true;
27390 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27391 {
27392 tree t = TREE_OPERAND (incr, 1);
27393 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27394 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27395 return true;
27396
27397 /* If this loop has a class iterator with != comparison
27398 with increment other than i++/++i/i--/--i, make sure the
27399 increment is constant. */
27400 if (CLASS_TYPE_P (TREE_TYPE (decl))
27401 && TREE_CODE (cond) == NE_EXPR)
27402 {
27403 if (TREE_OPERAND (t, 0) == decl)
27404 t = TREE_OPERAND (t, 1);
27405 else
27406 t = TREE_OPERAND (t, 0);
27407 if (TREE_CODE (t) != INTEGER_CST)
27408 return true;
27409 }
27410 }
27411 }
27412 }
27413
27414 return false;
27415 }
27416
27417 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27418 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27419 no such TYPE can be found. Note that this function peers inside
27420 uninstantiated templates and therefore should be used only in
27421 extremely limited situations. ONLY_CURRENT_P restricts this
27422 peering to the currently open classes hierarchy (which is required
27423 when comparing types). */
27424
27425 tree
27426 resolve_typename_type (tree type, bool only_current_p)
27427 {
27428 tree scope;
27429 tree name;
27430 tree decl;
27431 int quals;
27432 tree pushed_scope;
27433 tree result;
27434
27435 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27436
27437 scope = TYPE_CONTEXT (type);
27438 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27439 gcc_checking_assert (uses_template_parms (scope));
27440
27441 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27442 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27443 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27444 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27445 identifier of the TYPENAME_TYPE anymore.
27446 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27447 TYPENAME_TYPE instead, we avoid messing up with a possible
27448 typedef variant case. */
27449 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27450
27451 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27452 it first before we can figure out what NAME refers to. */
27453 if (TREE_CODE (scope) == TYPENAME_TYPE)
27454 {
27455 if (TYPENAME_IS_RESOLVING_P (scope))
27456 /* Given a class template A with a dependent base with nested type C,
27457 typedef typename A::C::C C will land us here, as trying to resolve
27458 the initial A::C leads to the local C typedef, which leads back to
27459 A::C::C. So we break the recursion now. */
27460 return type;
27461 else
27462 scope = resolve_typename_type (scope, only_current_p);
27463 }
27464 /* If we don't know what SCOPE refers to, then we cannot resolve the
27465 TYPENAME_TYPE. */
27466 if (!CLASS_TYPE_P (scope))
27467 return type;
27468 /* If this is a typedef, we don't want to look inside (c++/11987). */
27469 if (typedef_variant_p (type))
27470 return type;
27471 /* If SCOPE isn't the template itself, it will not have a valid
27472 TYPE_FIELDS list. */
27473 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27474 /* scope is either the template itself or a compatible instantiation
27475 like X<T>, so look up the name in the original template. */
27476 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27477 /* If scope has no fields, it can't be a current instantiation. Check this
27478 before currently_open_class to avoid infinite recursion (71515). */
27479 if (!TYPE_FIELDS (scope))
27480 return type;
27481 /* If the SCOPE is not the current instantiation, there's no reason
27482 to look inside it. */
27483 if (only_current_p && !currently_open_class (scope))
27484 return type;
27485 /* Enter the SCOPE so that name lookup will be resolved as if we
27486 were in the class definition. In particular, SCOPE will no
27487 longer be considered a dependent type. */
27488 pushed_scope = push_scope (scope);
27489 /* Look up the declaration. */
27490 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27491 tf_warning_or_error);
27492
27493 result = NULL_TREE;
27494
27495 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27496 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27497 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27498 if (!decl)
27499 /*nop*/;
27500 else if (identifier_p (fullname)
27501 && TREE_CODE (decl) == TYPE_DECL)
27502 {
27503 result = TREE_TYPE (decl);
27504 if (result == error_mark_node)
27505 result = NULL_TREE;
27506 }
27507 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27508 && DECL_CLASS_TEMPLATE_P (decl))
27509 {
27510 /* Obtain the template and the arguments. */
27511 tree tmpl = TREE_OPERAND (fullname, 0);
27512 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27513 {
27514 /* We get here with a plain identifier because a previous tentative
27515 parse of the nested-name-specifier as part of a ptr-operator saw
27516 ::template X<A>. The use of ::template is necessary in a
27517 ptr-operator, but wrong in a declarator-id.
27518
27519 [temp.names]: In a qualified-id of a declarator-id, the keyword
27520 template shall not appear at the top level. */
27521 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27522 "keyword %<template%> not allowed in declarator-id");
27523 tmpl = decl;
27524 }
27525 tree args = TREE_OPERAND (fullname, 1);
27526 /* Instantiate the template. */
27527 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27528 /*entering_scope=*/true,
27529 tf_error | tf_user);
27530 if (result == error_mark_node)
27531 result = NULL_TREE;
27532 }
27533
27534 /* Leave the SCOPE. */
27535 if (pushed_scope)
27536 pop_scope (pushed_scope);
27537
27538 /* If we failed to resolve it, return the original typename. */
27539 if (!result)
27540 return type;
27541
27542 /* If lookup found a typename type, resolve that too. */
27543 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27544 {
27545 /* Ill-formed programs can cause infinite recursion here, so we
27546 must catch that. */
27547 TYPENAME_IS_RESOLVING_P (result) = 1;
27548 result = resolve_typename_type (result, only_current_p);
27549 TYPENAME_IS_RESOLVING_P (result) = 0;
27550 }
27551
27552 /* Qualify the resulting type. */
27553 quals = cp_type_quals (type);
27554 if (quals)
27555 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27556
27557 return result;
27558 }
27559
27560 /* EXPR is an expression which is not type-dependent. Return a proxy
27561 for EXPR that can be used to compute the types of larger
27562 expressions containing EXPR. */
27563
27564 tree
27565 build_non_dependent_expr (tree expr)
27566 {
27567 tree orig_expr = expr;
27568 tree inner_expr;
27569
27570 /* When checking, try to get a constant value for all non-dependent
27571 expressions in order to expose bugs in *_dependent_expression_p
27572 and constexpr. This can affect code generation, see PR70704, so
27573 only do this for -fchecking=2. */
27574 if (flag_checking > 1
27575 && cxx_dialect >= cxx11
27576 /* Don't do this during nsdmi parsing as it can lead to
27577 unexpected recursive instantiations. */
27578 && !parsing_nsdmi ()
27579 /* Don't do this during concept processing either and for
27580 the same reason. */
27581 && !processing_constraint_expression_p ())
27582 fold_non_dependent_expr (expr, tf_none);
27583
27584 STRIP_ANY_LOCATION_WRAPPER (expr);
27585
27586 /* Preserve OVERLOADs; the functions must be available to resolve
27587 types. */
27588 inner_expr = expr;
27589 if (TREE_CODE (inner_expr) == STMT_EXPR)
27590 inner_expr = stmt_expr_value_expr (inner_expr);
27591 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27592 inner_expr = TREE_OPERAND (inner_expr, 0);
27593 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27594 inner_expr = TREE_OPERAND (inner_expr, 1);
27595 if (is_overloaded_fn (inner_expr)
27596 || TREE_CODE (inner_expr) == OFFSET_REF)
27597 return orig_expr;
27598 /* There is no need to return a proxy for a variable or enumerator. */
27599 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27600 return orig_expr;
27601 /* Preserve string constants; conversions from string constants to
27602 "char *" are allowed, even though normally a "const char *"
27603 cannot be used to initialize a "char *". */
27604 if (TREE_CODE (expr) == STRING_CST)
27605 return orig_expr;
27606 /* Preserve void and arithmetic constants, as an optimization -- there is no
27607 reason to create a new node. */
27608 if (TREE_CODE (expr) == VOID_CST
27609 || TREE_CODE (expr) == INTEGER_CST
27610 || TREE_CODE (expr) == REAL_CST)
27611 return orig_expr;
27612 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27613 There is at least one place where we want to know that a
27614 particular expression is a throw-expression: when checking a ?:
27615 expression, there are special rules if the second or third
27616 argument is a throw-expression. */
27617 if (TREE_CODE (expr) == THROW_EXPR)
27618 return orig_expr;
27619
27620 /* Don't wrap an initializer list, we need to be able to look inside. */
27621 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27622 return orig_expr;
27623
27624 /* Don't wrap a dummy object, we need to be able to test for it. */
27625 if (is_dummy_object (expr))
27626 return orig_expr;
27627
27628 if (TREE_CODE (expr) == COND_EXPR)
27629 return build3 (COND_EXPR,
27630 TREE_TYPE (expr),
27631 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27632 (TREE_OPERAND (expr, 1)
27633 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27634 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27635 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27636 if (TREE_CODE (expr) == COMPOUND_EXPR
27637 && !COMPOUND_EXPR_OVERLOADED (expr))
27638 return build2 (COMPOUND_EXPR,
27639 TREE_TYPE (expr),
27640 TREE_OPERAND (expr, 0),
27641 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27642
27643 /* If the type is unknown, it can't really be non-dependent */
27644 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27645
27646 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27647 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27648 TREE_TYPE (expr), expr);
27649 }
27650
27651 /* ARGS is a vector of expressions as arguments to a function call.
27652 Replace the arguments with equivalent non-dependent expressions.
27653 This modifies ARGS in place. */
27654
27655 void
27656 make_args_non_dependent (vec<tree, va_gc> *args)
27657 {
27658 unsigned int ix;
27659 tree arg;
27660
27661 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27662 {
27663 tree newarg = build_non_dependent_expr (arg);
27664 if (newarg != arg)
27665 (*args)[ix] = newarg;
27666 }
27667 }
27668
27669 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27670 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27671 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27672
27673 static tree
27674 make_auto_1 (tree name, bool set_canonical)
27675 {
27676 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27677 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27678 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27679 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27680 (0, processing_template_decl + 1, processing_template_decl + 1,
27681 TYPE_NAME (au), NULL_TREE);
27682 if (set_canonical)
27683 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27684 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27685 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27686 if (name == decltype_auto_identifier)
27687 AUTO_IS_DECLTYPE (au) = true;
27688
27689 return au;
27690 }
27691
27692 tree
27693 make_decltype_auto (void)
27694 {
27695 return make_auto_1 (decltype_auto_identifier, true);
27696 }
27697
27698 tree
27699 make_auto (void)
27700 {
27701 return make_auto_1 (auto_identifier, true);
27702 }
27703
27704 /* Return a C++17 deduction placeholder for class template TMPL. */
27705
27706 tree
27707 make_template_placeholder (tree tmpl)
27708 {
27709 tree t = make_auto_1 (auto_identifier, false);
27710 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27711 /* Our canonical type depends on the placeholder. */
27712 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27713 return t;
27714 }
27715
27716 /* True iff T is a C++17 class template deduction placeholder. */
27717
27718 bool
27719 template_placeholder_p (tree t)
27720 {
27721 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27722 }
27723
27724 /* Make a "constrained auto" type-specifier. This is an auto or
27725 decltype(auto) type with constraints that must be associated after
27726 deduction. The constraint is formed from the given concept CON
27727 and its optional sequence of template arguments ARGS.
27728
27729 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27730
27731 static tree
27732 make_constrained_placeholder_type (tree type, tree con, tree args)
27733 {
27734 /* Build the constraint. */
27735 tree tmpl = DECL_TI_TEMPLATE (con);
27736 tree expr = tmpl;
27737 if (TREE_CODE (con) == FUNCTION_DECL)
27738 expr = ovl_make (tmpl);
27739 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27740
27741 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27742
27743 /* Our canonical type depends on the constraint. */
27744 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27745
27746 /* Attach the constraint to the type declaration. */
27747 return TYPE_NAME (type);
27748 }
27749
27750 /* Make a "constrained auto" type-specifier. */
27751
27752 tree
27753 make_constrained_auto (tree con, tree args)
27754 {
27755 tree type = make_auto_1 (auto_identifier, false);
27756 return make_constrained_placeholder_type (type, con, args);
27757 }
27758
27759 /* Make a "constrained decltype(auto)" type-specifier. */
27760
27761 tree
27762 make_constrained_decltype_auto (tree con, tree args)
27763 {
27764 tree type = make_auto_1 (decltype_auto_identifier, false);
27765 return make_constrained_placeholder_type (type, con, args);
27766 }
27767
27768 /* Build and return a concept definition. Like other templates, the
27769 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27770 the TEMPLATE_DECL. */
27771
27772 tree
27773 finish_concept_definition (cp_expr id, tree init)
27774 {
27775 gcc_assert (identifier_p (id));
27776 gcc_assert (processing_template_decl);
27777
27778 location_t loc = id.get_location();
27779
27780 /* A concept-definition shall not have associated constraints. */
27781 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27782 {
27783 error_at (loc, "a concept cannot be constrained");
27784 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27785 }
27786
27787 /* A concept-definition shall appear in namespace scope. Templates
27788 aren't allowed in block scope, so we only need to check for class
27789 scope. */
27790 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27791 {
27792 error_at (loc, "concept %qE not in namespace scope", *id);
27793 return error_mark_node;
27794 }
27795
27796 /* Initially build the concept declaration; it's type is bool. */
27797 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27798 DECL_CONTEXT (decl) = current_scope ();
27799 DECL_INITIAL (decl) = init;
27800
27801 /* Push the enclosing template. */
27802 return push_template_decl (decl);
27803 }
27804
27805 /* Given type ARG, return std::initializer_list<ARG>. */
27806
27807 static tree
27808 listify (tree arg)
27809 {
27810 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27811
27812 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27813 {
27814 gcc_rich_location richloc (input_location);
27815 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27816 error_at (&richloc,
27817 "deducing from brace-enclosed initializer list"
27818 " requires %<#include <initializer_list>%>");
27819
27820 return error_mark_node;
27821 }
27822 tree argvec = make_tree_vec (1);
27823 TREE_VEC_ELT (argvec, 0) = arg;
27824
27825 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27826 NULL_TREE, 0, tf_warning_or_error);
27827 }
27828
27829 /* Replace auto in TYPE with std::initializer_list<auto>. */
27830
27831 static tree
27832 listify_autos (tree type, tree auto_node)
27833 {
27834 tree init_auto = listify (strip_top_quals (auto_node));
27835 tree argvec = make_tree_vec (1);
27836 TREE_VEC_ELT (argvec, 0) = init_auto;
27837 if (processing_template_decl)
27838 argvec = add_to_template_args (current_template_args (), argvec);
27839 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27840 }
27841
27842 /* Hash traits for hashing possibly constrained 'auto'
27843 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27844
27845 struct auto_hash : default_hash_traits<tree>
27846 {
27847 static inline hashval_t hash (tree);
27848 static inline bool equal (tree, tree);
27849 };
27850
27851 /* Hash the 'auto' T. */
27852
27853 inline hashval_t
27854 auto_hash::hash (tree t)
27855 {
27856 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27857 /* Matching constrained-type-specifiers denote the same template
27858 parameter, so hash the constraint. */
27859 return hash_placeholder_constraint (c);
27860 else
27861 /* But unconstrained autos are all separate, so just hash the pointer. */
27862 return iterative_hash_object (t, 0);
27863 }
27864
27865 /* Compare two 'auto's. */
27866
27867 inline bool
27868 auto_hash::equal (tree t1, tree t2)
27869 {
27870 if (t1 == t2)
27871 return true;
27872
27873 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
27874 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
27875
27876 /* Two unconstrained autos are distinct. */
27877 if (!c1 || !c2)
27878 return false;
27879
27880 return equivalent_placeholder_constraints (c1, c2);
27881 }
27882
27883 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27884 constrained) auto, add it to the vector. */
27885
27886 static int
27887 extract_autos_r (tree t, void *data)
27888 {
27889 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
27890 if (is_auto (t))
27891 {
27892 /* All the autos were built with index 0; fix that up now. */
27893 tree *p = hash.find_slot (t, INSERT);
27894 unsigned idx;
27895 if (*p)
27896 /* If this is a repeated constrained-type-specifier, use the index we
27897 chose before. */
27898 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
27899 else
27900 {
27901 /* Otherwise this is new, so use the current count. */
27902 *p = t;
27903 idx = hash.elements () - 1;
27904 }
27905 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
27906 }
27907
27908 /* Always keep walking. */
27909 return 0;
27910 }
27911
27912 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27913 says they can appear anywhere in the type. */
27914
27915 static tree
27916 extract_autos (tree type)
27917 {
27918 hash_set<tree> visited;
27919 hash_table<auto_hash> hash (2);
27920
27921 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
27922
27923 tree tree_vec = make_tree_vec (hash.elements());
27924 for (hash_table<auto_hash>::iterator iter = hash.begin();
27925 iter != hash.end(); ++iter)
27926 {
27927 tree elt = *iter;
27928 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
27929 TREE_VEC_ELT (tree_vec, i)
27930 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
27931 }
27932
27933 return tree_vec;
27934 }
27935
27936 /* The stem for deduction guide names. */
27937 const char *const dguide_base = "__dguide_";
27938
27939 /* Return the name for a deduction guide for class template TMPL. */
27940
27941 tree
27942 dguide_name (tree tmpl)
27943 {
27944 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
27945 tree tname = TYPE_IDENTIFIER (type);
27946 char *buf = (char *) alloca (1 + strlen (dguide_base)
27947 + IDENTIFIER_LENGTH (tname));
27948 memcpy (buf, dguide_base, strlen (dguide_base));
27949 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
27950 IDENTIFIER_LENGTH (tname) + 1);
27951 tree dname = get_identifier (buf);
27952 TREE_TYPE (dname) = type;
27953 return dname;
27954 }
27955
27956 /* True if NAME is the name of a deduction guide. */
27957
27958 bool
27959 dguide_name_p (tree name)
27960 {
27961 return (TREE_CODE (name) == IDENTIFIER_NODE
27962 && TREE_TYPE (name)
27963 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
27964 strlen (dguide_base)));
27965 }
27966
27967 /* True if FN is a deduction guide. */
27968
27969 bool
27970 deduction_guide_p (const_tree fn)
27971 {
27972 if (DECL_P (fn))
27973 if (tree name = DECL_NAME (fn))
27974 return dguide_name_p (name);
27975 return false;
27976 }
27977
27978 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27979
27980 bool
27981 copy_guide_p (const_tree fn)
27982 {
27983 gcc_assert (deduction_guide_p (fn));
27984 if (!DECL_ARTIFICIAL (fn))
27985 return false;
27986 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27987 return (TREE_CHAIN (parms) == void_list_node
27988 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27989 }
27990
27991 /* True if FN is a guide generated from a constructor template. */
27992
27993 bool
27994 template_guide_p (const_tree fn)
27995 {
27996 gcc_assert (deduction_guide_p (fn));
27997 if (!DECL_ARTIFICIAL (fn))
27998 return false;
27999 tree tmpl = DECL_TI_TEMPLATE (fn);
28000 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28001 return PRIMARY_TEMPLATE_P (org);
28002 return false;
28003 }
28004
28005 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28006 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28007 template parameter types. Note that the handling of template template
28008 parameters relies on current_template_parms being set appropriately for the
28009 new template. */
28010
28011 static tree
28012 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28013 tree tsubst_args, tsubst_flags_t complain)
28014 {
28015 if (olddecl == error_mark_node)
28016 return error_mark_node;
28017
28018 tree oldidx = get_template_parm_index (olddecl);
28019
28020 tree newtype;
28021 if (TREE_CODE (olddecl) == TYPE_DECL
28022 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28023 {
28024 tree oldtype = TREE_TYPE (olddecl);
28025 newtype = cxx_make_type (TREE_CODE (oldtype));
28026 TYPE_MAIN_VARIANT (newtype) = newtype;
28027 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28028 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28029 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28030 }
28031 else
28032 {
28033 newtype = TREE_TYPE (olddecl);
28034 if (type_uses_auto (newtype))
28035 {
28036 // Substitute once to fix references to other template parameters.
28037 newtype = tsubst (newtype, tsubst_args,
28038 complain|tf_partial, NULL_TREE);
28039 // Now substitute again to reduce the level of the auto.
28040 newtype = tsubst (newtype, current_template_args (),
28041 complain, NULL_TREE);
28042 }
28043 else
28044 newtype = tsubst (newtype, tsubst_args,
28045 complain, NULL_TREE);
28046 }
28047
28048 tree newdecl
28049 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28050 DECL_NAME (olddecl), newtype);
28051 SET_DECL_TEMPLATE_PARM_P (newdecl);
28052
28053 tree newidx;
28054 if (TREE_CODE (olddecl) == TYPE_DECL
28055 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28056 {
28057 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28058 = build_template_parm_index (index, level, level,
28059 newdecl, newtype);
28060 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28061 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28062 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28063 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28064 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28065 else
28066 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28067
28068 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28069 {
28070 DECL_TEMPLATE_RESULT (newdecl)
28071 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28072 DECL_NAME (olddecl), newtype);
28073 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28074 // First create a copy (ttargs) of tsubst_args with an
28075 // additional level for the template template parameter's own
28076 // template parameters (ttparms).
28077 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28078 (DECL_TEMPLATE_PARMS (olddecl)));
28079 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28080 tree ttargs = make_tree_vec (depth + 1);
28081 for (int i = 0; i < depth; ++i)
28082 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28083 TREE_VEC_ELT (ttargs, depth)
28084 = template_parms_level_to_args (ttparms);
28085 // Substitute ttargs into ttparms to fix references to
28086 // other template parameters.
28087 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28088 complain|tf_partial);
28089 // Now substitute again with args based on tparms, to reduce
28090 // the level of the ttparms.
28091 ttargs = current_template_args ();
28092 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28093 complain);
28094 // Finally, tack the adjusted parms onto tparms.
28095 ttparms = tree_cons (size_int (depth), ttparms,
28096 current_template_parms);
28097 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28098 }
28099 }
28100 else
28101 {
28102 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28103 tree newconst
28104 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28105 TREE_CODE (oldconst),
28106 DECL_NAME (oldconst), newtype);
28107 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28108 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28109 SET_DECL_TEMPLATE_PARM_P (newconst);
28110 newidx = build_template_parm_index (index, level, level,
28111 newconst, newtype);
28112 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28113 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28114 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28115 }
28116
28117 return newdecl;
28118 }
28119
28120 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28121 template parameter. */
28122
28123 static tree
28124 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28125 tree targs, unsigned targs_index, tsubst_flags_t complain)
28126 {
28127 tree olddecl = TREE_VALUE (oldelt);
28128 tree newdecl = rewrite_template_parm (olddecl, index, level,
28129 targs, complain);
28130 if (newdecl == error_mark_node)
28131 return error_mark_node;
28132 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28133 targs, complain, NULL_TREE);
28134 tree list = build_tree_list (newdef, newdecl);
28135 TEMPLATE_PARM_CONSTRAINTS (list)
28136 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28137 targs, complain, NULL_TREE);
28138 int depth = TMPL_ARGS_DEPTH (targs);
28139 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28140 return list;
28141 }
28142
28143 /* Returns a C++17 class deduction guide template based on the constructor
28144 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28145 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28146 aggregate initialization guide. */
28147
28148 static tree
28149 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28150 {
28151 tree tparms, targs, fparms, fargs, ci;
28152 bool memtmpl = false;
28153 bool explicit_p;
28154 location_t loc;
28155 tree fn_tmpl = NULL_TREE;
28156
28157 if (outer_args)
28158 {
28159 ++processing_template_decl;
28160 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28161 --processing_template_decl;
28162 }
28163
28164 if (!DECL_DECLARES_FUNCTION_P (ctor))
28165 {
28166 if (TYPE_P (ctor))
28167 {
28168 bool copy_p = TYPE_REF_P (ctor);
28169 if (copy_p)
28170 fparms = tree_cons (NULL_TREE, type, void_list_node);
28171 else
28172 fparms = void_list_node;
28173 }
28174 else if (TREE_CODE (ctor) == TREE_LIST)
28175 fparms = ctor;
28176 else
28177 gcc_unreachable ();
28178
28179 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28180 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28181 targs = CLASSTYPE_TI_ARGS (type);
28182 ci = NULL_TREE;
28183 fargs = NULL_TREE;
28184 loc = DECL_SOURCE_LOCATION (ctmpl);
28185 explicit_p = false;
28186 }
28187 else
28188 {
28189 ++processing_template_decl;
28190 bool ok = true;
28191
28192 fn_tmpl
28193 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28194 : DECL_TI_TEMPLATE (ctor));
28195 if (outer_args)
28196 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28197 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28198
28199 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28200 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28201 fully specialized args for the enclosing class. Strip those off, as
28202 the deduction guide won't have those template parameters. */
28203 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28204 TMPL_PARMS_DEPTH (tparms));
28205 /* Discard the 'this' parameter. */
28206 fparms = FUNCTION_ARG_CHAIN (ctor);
28207 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28208 ci = get_constraints (ctor);
28209 loc = DECL_SOURCE_LOCATION (ctor);
28210 explicit_p = DECL_NONCONVERTING_P (ctor);
28211
28212 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28213 {
28214 memtmpl = true;
28215
28216 /* For a member template constructor, we need to flatten the two
28217 template parameter lists into one, and then adjust the function
28218 signature accordingly. This gets...complicated. */
28219 tree save_parms = current_template_parms;
28220
28221 /* For a member template we should have two levels of parms/args, one
28222 for the class and one for the constructor. We stripped
28223 specialized args for further enclosing classes above. */
28224 const int depth = 2;
28225 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28226
28227 /* Template args for translating references to the two-level template
28228 parameters into references to the one-level template parameters we
28229 are creating. */
28230 tree tsubst_args = copy_node (targs);
28231 TMPL_ARGS_LEVEL (tsubst_args, depth)
28232 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28233
28234 /* Template parms for the constructor template. */
28235 tree ftparms = TREE_VALUE (tparms);
28236 unsigned flen = TREE_VEC_LENGTH (ftparms);
28237 /* Template parms for the class template. */
28238 tparms = TREE_CHAIN (tparms);
28239 tree ctparms = TREE_VALUE (tparms);
28240 unsigned clen = TREE_VEC_LENGTH (ctparms);
28241 /* Template parms for the deduction guide start as a copy of the
28242 template parms for the class. We set current_template_parms for
28243 lookup_template_class_1. */
28244 current_template_parms = tparms = copy_node (tparms);
28245 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28246 for (unsigned i = 0; i < clen; ++i)
28247 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28248
28249 /* Now we need to rewrite the constructor parms to append them to the
28250 class parms. */
28251 for (unsigned i = 0; i < flen; ++i)
28252 {
28253 unsigned index = i + clen;
28254 unsigned level = 1;
28255 tree oldelt = TREE_VEC_ELT (ftparms, i);
28256 tree newelt
28257 = rewrite_tparm_list (oldelt, index, level,
28258 tsubst_args, i, complain);
28259 if (newelt == error_mark_node)
28260 ok = false;
28261 TREE_VEC_ELT (new_vec, index) = newelt;
28262 }
28263
28264 /* Now we have a final set of template parms to substitute into the
28265 function signature. */
28266 targs = template_parms_to_args (tparms);
28267 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28268 complain, ctor);
28269 if (fparms == error_mark_node)
28270 ok = false;
28271 if (ci)
28272 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28273
28274 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28275 cp_unevaluated_operand. */
28276 cp_evaluated ev;
28277 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28278 current_template_parms = save_parms;
28279 }
28280
28281 --processing_template_decl;
28282 if (!ok)
28283 return error_mark_node;
28284 }
28285
28286 if (!memtmpl)
28287 {
28288 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28289 tparms = copy_node (tparms);
28290 INNERMOST_TEMPLATE_PARMS (tparms)
28291 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28292 }
28293
28294 tree fntype = build_function_type (type, fparms);
28295 tree ded_fn = build_lang_decl_loc (loc,
28296 FUNCTION_DECL,
28297 dguide_name (type), fntype);
28298 DECL_ARGUMENTS (ded_fn) = fargs;
28299 DECL_ARTIFICIAL (ded_fn) = true;
28300 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28301 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28302 DECL_ARTIFICIAL (ded_tmpl) = true;
28303 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
28304 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
28305 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28306 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28307 if (DECL_P (ctor))
28308 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28309 if (ci)
28310 set_constraints (ded_tmpl, ci);
28311
28312 return ded_tmpl;
28313 }
28314
28315 /* Add to LIST the member types for the reshaped initializer CTOR. */
28316
28317 static tree
28318 collect_ctor_idx_types (tree ctor, tree list)
28319 {
28320 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28321 tree idx, val; unsigned i;
28322 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28323 {
28324 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28325 && CONSTRUCTOR_NELTS (val))
28326 if (tree subidx = CONSTRUCTOR_ELT (val, 0)->index)
28327 if (TREE_CODE (subidx) == FIELD_DECL)
28328 {
28329 list = collect_ctor_idx_types (val, list);
28330 continue;
28331 }
28332 tree ftype = finish_decltype_type (idx, true, tf_none);
28333 list = tree_cons (NULL_TREE, ftype, list);
28334 }
28335
28336 return list;
28337 }
28338
28339 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28340
28341 static bool
28342 is_spec_or_derived (tree etype, tree tmpl)
28343 {
28344 if (!etype || !CLASS_TYPE_P (etype))
28345 return false;
28346
28347 tree type = TREE_TYPE (tmpl);
28348 tree tparms = (INNERMOST_TEMPLATE_PARMS
28349 (DECL_TEMPLATE_PARMS (tmpl)));
28350 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28351 int err = unify (tparms, targs, type, etype,
28352 UNIFY_ALLOW_DERIVED, /*explain*/false);
28353 ggc_free (targs);
28354 return !err;
28355 }
28356
28357 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28358 INIT. */
28359
28360 static tree
28361 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28362 {
28363 if (cxx_dialect < cxx2a)
28364 return NULL_TREE;
28365
28366 if (init == NULL_TREE)
28367 return NULL_TREE;
28368
28369 tree type = TREE_TYPE (tmpl);
28370 if (!CP_AGGREGATE_TYPE_P (type))
28371 return NULL_TREE;
28372
28373 /* No aggregate candidate for copy-initialization. */
28374 if (args->length() == 1)
28375 {
28376 tree val = (*args)[0];
28377 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28378 return NULL_TREE;
28379 }
28380
28381 /* If we encounter a problem, we just won't add the candidate. */
28382 tsubst_flags_t complain = tf_none;
28383
28384 tree parms = NULL_TREE;
28385 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28386 {
28387 init = reshape_init (type, init, complain);
28388 if (init == error_mark_node)
28389 return NULL_TREE;
28390 parms = collect_ctor_idx_types (init, parms);
28391 }
28392 else if (TREE_CODE (init) == TREE_LIST)
28393 {
28394 int len = list_length (init);
28395 for (tree field = TYPE_FIELDS (type);
28396 len;
28397 --len, field = DECL_CHAIN (field))
28398 {
28399 field = next_initializable_field (field);
28400 if (!field)
28401 return NULL_TREE;
28402 tree ftype = finish_decltype_type (field, true, complain);
28403 parms = tree_cons (NULL_TREE, ftype, parms);
28404 }
28405 }
28406 else
28407 /* Aggregate initialization doesn't apply to an initializer expression. */
28408 return NULL_TREE;
28409
28410 if (parms)
28411 {
28412 tree last = parms;
28413 parms = nreverse (parms);
28414 TREE_CHAIN (last) = void_list_node;
28415 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28416 return guide;
28417 }
28418
28419 return NULL_TREE;
28420 }
28421
28422 /* UGUIDES are the deduction guides for the underlying template of alias
28423 template TMPL; adjust them to be deduction guides for TMPL. */
28424
28425 static tree
28426 alias_ctad_tweaks (tree tmpl, tree uguides)
28427 {
28428 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28429 class type (9.2.8.2) where the template-name names an alias template A,
28430 the defining-type-id of A must be of the form
28431
28432 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28433
28434 as specified in 9.2.8.2. The guides of A are the set of functions or
28435 function templates formed as follows. For each function or function
28436 template f in the guides of the template named by the simple-template-id
28437 of the defining-type-id, the template arguments of the return type of f
28438 are deduced from the defining-type-id of A according to the process in
28439 13.10.2.5 with the exception that deduction does not fail if not all
28440 template arguments are deduced. Let g denote the result of substituting
28441 these deductions into f. If substitution succeeds, form a function or
28442 function template f' with the following properties and add it to the set
28443 of guides of A:
28444
28445 * The function type of f' is the function type of g.
28446
28447 * If f is a function template, f' is a function template whose template
28448 parameter list consists of all the template parameters of A (including
28449 their default template arguments) that appear in the above deductions or
28450 (recursively) in their default template arguments, followed by the
28451 template parameters of f that were not deduced (including their default
28452 template arguments), otherwise f' is not a function template.
28453
28454 * The associated constraints (13.5.2) are the conjunction of the
28455 associated constraints of g and a constraint that is satisfied if and only
28456 if the arguments of A are deducible (see below) from the return type.
28457
28458 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28459 be so as well.
28460
28461 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28462 considered to be so as well.
28463
28464 * The explicit-specifier of f' is the explicit-specifier of g (if
28465 any). */
28466
28467 /* This implementation differs from the above in two significant ways:
28468
28469 1) We include all template parameters of A, not just some.
28470 2) The added constraint is same_type instead of deducible.
28471
28472 I believe that while it's probably possible to construct a testcase that
28473 behaves differently with this simplification, it should have the same
28474 effect for real uses. Including all template parameters means that we
28475 deduce all parameters of A when resolving the call, so when we're in the
28476 constraint we don't need to deduce them again, we can just check whether
28477 the deduction produced the desired result. */
28478
28479 tsubst_flags_t complain = tf_warning_or_error;
28480 tree atype = TREE_TYPE (tmpl);
28481 tree aguides = NULL_TREE;
28482 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28483 unsigned natparms = TREE_VEC_LENGTH (atparms);
28484 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28485 for (ovl_iterator iter (uguides); iter; ++iter)
28486 {
28487 tree f = *iter;
28488 tree in_decl = f;
28489 location_t loc = DECL_SOURCE_LOCATION (f);
28490 tree ret = TREE_TYPE (TREE_TYPE (f));
28491 tree fprime = f;
28492 if (TREE_CODE (f) == TEMPLATE_DECL)
28493 {
28494 processing_template_decl_sentinel ptds (/*reset*/false);
28495 ++processing_template_decl;
28496
28497 /* Deduce template arguments for f from the type-id of A. */
28498 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28499 unsigned len = TREE_VEC_LENGTH (ftparms);
28500 tree targs = make_tree_vec (len);
28501 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28502 gcc_assert (!err);
28503
28504 /* The number of parms for f' is the number of parms for A plus
28505 non-deduced parms of f. */
28506 unsigned ndlen = 0;
28507 unsigned j;
28508 for (unsigned i = 0; i < len; ++i)
28509 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28510 ++ndlen;
28511 tree gtparms = make_tree_vec (natparms + ndlen);
28512
28513 /* First copy over the parms of A. */
28514 for (j = 0; j < natparms; ++j)
28515 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28516 /* Now rewrite the non-deduced parms of f. */
28517 for (unsigned i = 0; ndlen && i < len; ++i)
28518 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28519 {
28520 --ndlen;
28521 unsigned index = j++;
28522 unsigned level = 1;
28523 tree oldlist = TREE_VEC_ELT (ftparms, i);
28524 tree list = rewrite_tparm_list (oldlist, index, level,
28525 targs, i, complain);
28526 TREE_VEC_ELT (gtparms, index) = list;
28527 }
28528 gtparms = build_tree_list (size_one_node, gtparms);
28529
28530 /* Substitute the deduced arguments plus the rewritten template
28531 parameters into f to get g. This covers the type, copyness,
28532 guideness, and explicit-specifier. */
28533 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28534 if (g == error_mark_node)
28535 return error_mark_node;
28536 DECL_USE_TEMPLATE (g) = 0;
28537 fprime = build_template_decl (g, gtparms, false);
28538 DECL_TEMPLATE_RESULT (fprime) = g;
28539 TREE_TYPE (fprime) = TREE_TYPE (g);
28540 tree gtargs = template_parms_to_args (gtparms);
28541 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28542 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28543
28544 /* Substitute the associated constraints. */
28545 tree ci = get_constraints (f);
28546 if (ci)
28547 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28548 if (ci == error_mark_node)
28549 return error_mark_node;
28550
28551 /* Add a constraint that the return type matches the instantiation of
28552 A with the same template arguments. */
28553 ret = TREE_TYPE (TREE_TYPE (fprime));
28554 if (!same_type_p (atype, ret)
28555 /* FIXME this should mean they don't compare as equivalent. */
28556 || dependent_alias_template_spec_p (atype, nt_opaque))
28557 {
28558 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28559 ci = append_constraint (ci, same);
28560 }
28561
28562 if (ci)
28563 set_constraints (fprime, ci);
28564 }
28565 else
28566 {
28567 /* For a non-template deduction guide, if the arguments of A aren't
28568 deducible from the return type, don't add the candidate. */
28569 tree targs = make_tree_vec (natparms);
28570 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28571 for (unsigned i = 0; !err && i < natparms; ++i)
28572 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28573 err = true;
28574 if (err)
28575 continue;
28576 }
28577
28578 aguides = lookup_add (fprime, aguides);
28579 }
28580
28581 return aguides;
28582 }
28583
28584 /* Return artificial deduction guides built from the constructors of class
28585 template TMPL. */
28586
28587 static tree
28588 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28589 {
28590 tree type = TREE_TYPE (tmpl);
28591 tree outer_args = NULL_TREE;
28592 if (DECL_CLASS_SCOPE_P (tmpl)
28593 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28594 {
28595 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28596 type = TREE_TYPE (most_general_template (tmpl));
28597 }
28598
28599 tree cands = NULL_TREE;
28600
28601 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28602 {
28603 /* Skip inherited constructors. */
28604 if (iter.using_p ())
28605 continue;
28606
28607 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28608 cands = lookup_add (guide, cands);
28609 }
28610
28611 /* Add implicit default constructor deduction guide. */
28612 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28613 {
28614 tree guide = build_deduction_guide (type, type, outer_args,
28615 complain);
28616 cands = lookup_add (guide, cands);
28617 }
28618
28619 /* Add copy guide. */
28620 {
28621 tree gtype = build_reference_type (type);
28622 tree guide = build_deduction_guide (type, gtype, outer_args,
28623 complain);
28624 cands = lookup_add (guide, cands);
28625 }
28626
28627 return cands;
28628 }
28629
28630 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28631
28632 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28633 aggregate candidate is added separately because it depends on the
28634 initializer. */
28635
28636 static tree
28637 deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28638 {
28639 tree guides = NULL_TREE;
28640 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28641 {
28642 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28643 tree tinfo = get_template_info (under);
28644 guides = deduction_guides_for (TI_TEMPLATE (tinfo), complain);
28645 }
28646 else
28647 {
28648 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28649 dguide_name (tmpl),
28650 /*type*/false, /*complain*/false,
28651 /*hidden*/false);
28652 if (guides == error_mark_node)
28653 guides = NULL_TREE;
28654 }
28655
28656 /* Cache the deduction guides for a template. We also remember the result of
28657 lookup, and rebuild everything if it changes; should be very rare. */
28658 tree_pair_p cache = NULL;
28659 if (tree_pair_p &r
28660 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28661 {
28662 cache = r;
28663 if (cache->purpose == guides)
28664 return cache->value;
28665 }
28666 else
28667 {
28668 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28669 cache->purpose = guides;
28670 }
28671
28672 tree cands = NULL_TREE;
28673 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28674 cands = alias_ctad_tweaks (tmpl, guides);
28675 else
28676 {
28677 cands = ctor_deduction_guides_for (tmpl, complain);
28678 for (ovl_iterator it (guides); it; ++it)
28679 cands = lookup_add (*it, cands);
28680 }
28681
28682 cache->value = cands;
28683 return cands;
28684 }
28685
28686 /* Return whether TMPL is a (class template argument-) deducible template. */
28687
28688 bool
28689 ctad_template_p (tree tmpl)
28690 {
28691 /* A deducible template is either a class template or is an alias template
28692 whose defining-type-id is of the form
28693
28694 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28695
28696 where the nested-name-specifier (if any) is non-dependent and the
28697 template-name of the simple-template-id names a deducible template. */
28698
28699 if (DECL_CLASS_TEMPLATE_P (tmpl)
28700 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28701 return true;
28702 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28703 return false;
28704 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28705 if (tree tinfo = get_template_info (orig))
28706 return ctad_template_p (TI_TEMPLATE (tinfo));
28707 return false;
28708 }
28709
28710 /* Deduce template arguments for the class template placeholder PTYPE for
28711 template TMPL based on the initializer INIT, and return the resulting
28712 type. */
28713
28714 static tree
28715 do_class_deduction (tree ptype, tree tmpl, tree init,
28716 int flags, tsubst_flags_t complain)
28717 {
28718 /* We should have handled this in the caller. */
28719 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28720 return ptype;
28721
28722 /* Look through alias templates that just rename another template. */
28723 tmpl = get_underlying_template (tmpl);
28724 if (!ctad_template_p (tmpl))
28725 {
28726 if (complain & tf_error)
28727 error ("non-deducible template %qT used without template arguments", tmpl);
28728 return error_mark_node;
28729 }
28730 else if (cxx_dialect < cxx2a && DECL_ALIAS_TEMPLATE_P (tmpl))
28731 {
28732 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28733 if (complain & tf_warning_or_error)
28734 pedwarn (input_location, 0, "alias template deduction only available "
28735 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
28736 }
28737
28738 if (init && TREE_TYPE (init) == ptype)
28739 /* Using the template parm as its own argument. */
28740 return ptype;
28741
28742 tree type = TREE_TYPE (tmpl);
28743
28744 bool try_list_ctor = false;
28745
28746 releasing_vec rv_args = NULL;
28747 vec<tree,va_gc> *&args = *&rv_args;
28748 if (init == NULL_TREE)
28749 args = make_tree_vector ();
28750 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28751 {
28752 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28753 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28754 {
28755 /* As an exception, the first phase in 16.3.1.7 (considering the
28756 initializer list as a single argument) is omitted if the
28757 initializer list consists of a single expression of type cv U,
28758 where U is a specialization of C or a class derived from a
28759 specialization of C. */
28760 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28761 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28762 try_list_ctor = false;
28763 }
28764 if (try_list_ctor || is_std_init_list (type))
28765 args = make_tree_vector_single (init);
28766 else
28767 args = make_tree_vector_from_ctor (init);
28768 }
28769 else if (TREE_CODE (init) == TREE_LIST)
28770 args = make_tree_vector_from_list (init);
28771 else
28772 args = make_tree_vector_single (init);
28773
28774 /* Do this now to avoid problems with erroneous args later on. */
28775 args = resolve_args (args, complain);
28776 if (args == NULL)
28777 return error_mark_node;
28778
28779 tree cands = deduction_guides_for (tmpl, complain);
28780 if (cands == error_mark_node)
28781 return error_mark_node;
28782
28783 /* Prune explicit deduction guides in copy-initialization context. */
28784 bool elided = false;
28785 if (flags & LOOKUP_ONLYCONVERTING)
28786 {
28787 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28788 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28789 elided = true;
28790
28791 if (elided)
28792 {
28793 /* Found a nonconverting guide, prune the candidates. */
28794 tree pruned = NULL_TREE;
28795 for (lkp_iterator iter (cands); iter; ++iter)
28796 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28797 pruned = lookup_add (*iter, pruned);
28798
28799 cands = pruned;
28800 }
28801 }
28802
28803 if (tree guide = maybe_aggr_guide (tmpl, init, args))
28804 cands = lookup_add (guide, cands);
28805
28806 tree call = error_mark_node;
28807
28808 /* If this is list-initialization and the class has a list constructor, first
28809 try deducing from the list as a single argument, as [over.match.list]. */
28810 tree list_cands = NULL_TREE;
28811 if (try_list_ctor && cands)
28812 for (lkp_iterator iter (cands); iter; ++iter)
28813 {
28814 tree dg = *iter;
28815 if (is_list_ctor (dg))
28816 list_cands = lookup_add (dg, list_cands);
28817 }
28818 if (list_cands)
28819 {
28820 ++cp_unevaluated_operand;
28821 call = build_new_function_call (list_cands, &args, tf_decltype);
28822 --cp_unevaluated_operand;
28823
28824 if (call == error_mark_node)
28825 {
28826 /* That didn't work, now try treating the list as a sequence of
28827 arguments. */
28828 release_tree_vector (args);
28829 args = make_tree_vector_from_ctor (init);
28830 }
28831 }
28832
28833 if (elided && !cands)
28834 {
28835 error ("cannot deduce template arguments for copy-initialization"
28836 " of %qT, as it has no non-explicit deduction guides or "
28837 "user-declared constructors", type);
28838 return error_mark_node;
28839 }
28840 else if (!cands && call == error_mark_node)
28841 {
28842 error ("cannot deduce template arguments of %qT, as it has no viable "
28843 "deduction guides", type);
28844 return error_mark_node;
28845 }
28846
28847 if (call == error_mark_node)
28848 {
28849 ++cp_unevaluated_operand;
28850 call = build_new_function_call (cands, &args, tf_decltype);
28851 --cp_unevaluated_operand;
28852 }
28853
28854 if (call == error_mark_node
28855 && (complain & tf_warning_or_error))
28856 {
28857 error ("class template argument deduction failed:");
28858
28859 ++cp_unevaluated_operand;
28860 call = build_new_function_call (cands, &args, complain | tf_decltype);
28861 --cp_unevaluated_operand;
28862
28863 if (elided)
28864 inform (input_location, "explicit deduction guides not considered "
28865 "for copy-initialization");
28866 }
28867
28868 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
28869 }
28870
28871 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28872 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28873 The CONTEXT determines the context in which auto deduction is performed
28874 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28875 OUTER_TARGS are used during template argument deduction
28876 (context == adc_unify) to properly substitute the result, and is ignored
28877 in other contexts.
28878
28879 For partial-concept-ids, extra args may be appended to the list of deduced
28880 template arguments prior to determining constraint satisfaction. */
28881
28882 tree
28883 do_auto_deduction (tree type, tree init, tree auto_node,
28884 tsubst_flags_t complain, auto_deduction_context context,
28885 tree outer_targs, int flags)
28886 {
28887 tree targs;
28888
28889 if (init == error_mark_node)
28890 return error_mark_node;
28891
28892 if (init && type_dependent_expression_p (init)
28893 && context != adc_unify)
28894 /* Defining a subset of type-dependent expressions that we can deduce
28895 from ahead of time isn't worth the trouble. */
28896 return type;
28897
28898 /* Similarly, we can't deduce from another undeduced decl. */
28899 if (init && undeduced_auto_decl (init))
28900 return type;
28901
28902 /* We may be doing a partial substitution, but we still want to replace
28903 auto_node. */
28904 complain &= ~tf_partial;
28905
28906 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
28907 /* C++17 class template argument deduction. */
28908 return do_class_deduction (type, tmpl, init, flags, complain);
28909
28910 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
28911 /* Nothing we can do with this, even in deduction context. */
28912 return type;
28913
28914 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28915 with either a new invented type template parameter U or, if the
28916 initializer is a braced-init-list (8.5.4), with
28917 std::initializer_list<U>. */
28918 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28919 {
28920 if (!DIRECT_LIST_INIT_P (init))
28921 type = listify_autos (type, auto_node);
28922 else if (CONSTRUCTOR_NELTS (init) == 1)
28923 init = CONSTRUCTOR_ELT (init, 0)->value;
28924 else
28925 {
28926 if (complain & tf_warning_or_error)
28927 {
28928 if (permerror (input_location, "direct-list-initialization of "
28929 "%<auto%> requires exactly one element"))
28930 inform (input_location,
28931 "for deduction to %<std::initializer_list%>, use copy-"
28932 "list-initialization (i.e. add %<=%> before the %<{%>)");
28933 }
28934 type = listify_autos (type, auto_node);
28935 }
28936 }
28937
28938 if (type == error_mark_node)
28939 return error_mark_node;
28940
28941 init = resolve_nondeduced_context (init, complain);
28942
28943 if (context == adc_decomp_type
28944 && auto_node == type
28945 && init != error_mark_node
28946 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
28947 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28948 and initializer has array type, deduce cv-qualified array type. */
28949 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
28950 complain);
28951 else if (AUTO_IS_DECLTYPE (auto_node))
28952 {
28953 tree stripped_init = tree_strip_any_location_wrapper (init);
28954 bool id = (DECL_P (stripped_init)
28955 || ((TREE_CODE (init) == COMPONENT_REF
28956 || TREE_CODE (init) == SCOPE_REF)
28957 && !REF_PARENTHESIZED_P (init)));
28958 targs = make_tree_vec (1);
28959 TREE_VEC_ELT (targs, 0)
28960 = finish_decltype_type (init, id, tf_warning_or_error);
28961 if (type != auto_node)
28962 {
28963 if (complain & tf_error)
28964 error ("%qT as type rather than plain %<decltype(auto)%>", type);
28965 return error_mark_node;
28966 }
28967 }
28968 else
28969 {
28970 if (error_operand_p (init))
28971 return error_mark_node;
28972
28973 tree parms = build_tree_list (NULL_TREE, type);
28974 tree tparms;
28975
28976 if (flag_concepts)
28977 tparms = extract_autos (type);
28978 else
28979 {
28980 tparms = make_tree_vec (1);
28981 TREE_VEC_ELT (tparms, 0)
28982 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
28983 }
28984
28985 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28986 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
28987 DEDUCE_CALL,
28988 NULL, /*explain_p=*/false);
28989 if (val > 0)
28990 {
28991 if (processing_template_decl)
28992 /* Try again at instantiation time. */
28993 return type;
28994 if (type && type != error_mark_node
28995 && (complain & tf_error))
28996 /* If type is error_mark_node a diagnostic must have been
28997 emitted by now. Also, having a mention to '<type error>'
28998 in the diagnostic is not really useful to the user. */
28999 {
29000 if (cfun
29001 && FNDECL_USED_AUTO (current_function_decl)
29002 && (auto_node
29003 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29004 && LAMBDA_FUNCTION_P (current_function_decl))
29005 error ("unable to deduce lambda return type from %qE", init);
29006 else
29007 error ("unable to deduce %qT from %qE", type, init);
29008 type_unification_real (tparms, targs, parms, &init, 1, 0,
29009 DEDUCE_CALL,
29010 NULL, /*explain_p=*/true);
29011 }
29012 return error_mark_node;
29013 }
29014 }
29015
29016 /* Check any placeholder constraints against the deduced type. */
29017 if (flag_concepts && !processing_template_decl)
29018 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29019 {
29020 /* Use the deduced type to check the associated constraints. If we
29021 have a partial-concept-id, rebuild the argument list so that
29022 we check using the extra arguments. */
29023 check = unpack_concept_check (check);
29024 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
29025 tree cdecl = TREE_OPERAND (check, 0);
29026 if (OVL_P (cdecl))
29027 cdecl = OVL_FIRST (cdecl);
29028 tree cargs = TREE_OPERAND (check, 1);
29029 if (TREE_VEC_LENGTH (cargs) > 1)
29030 {
29031 cargs = copy_node (cargs);
29032 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
29033 }
29034 else
29035 cargs = targs;
29036
29037 /* Rebuild the check using the deduced arguments. */
29038 check = build_concept_check (cdecl, cargs, tf_none);
29039
29040 if (!constraints_satisfied_p (check))
29041 {
29042 if (complain & tf_warning_or_error)
29043 {
29044 auto_diagnostic_group d;
29045 switch (context)
29046 {
29047 case adc_unspecified:
29048 case adc_unify:
29049 error("placeholder constraints not satisfied");
29050 break;
29051 case adc_variable_type:
29052 case adc_decomp_type:
29053 error ("deduced initializer does not satisfy "
29054 "placeholder constraints");
29055 break;
29056 case adc_return_type:
29057 error ("deduced return type does not satisfy "
29058 "placeholder constraints");
29059 break;
29060 case adc_requirement:
29061 error ("deduced expression type does not satisfy "
29062 "placeholder constraints");
29063 break;
29064 }
29065 diagnose_constraints (input_location, check, targs);
29066 }
29067 return error_mark_node;
29068 }
29069 }
29070
29071 if (processing_template_decl && context != adc_unify)
29072 outer_targs = current_template_args ();
29073 targs = add_to_template_args (outer_targs, targs);
29074 return tsubst (type, targs, complain, NULL_TREE);
29075 }
29076
29077 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29078 result. */
29079
29080 tree
29081 splice_late_return_type (tree type, tree late_return_type)
29082 {
29083 if (late_return_type)
29084 {
29085 gcc_assert (is_auto (type) || seen_error ());
29086 return late_return_type;
29087 }
29088
29089 if (tree *auto_node = find_type_usage (&type, is_auto))
29090 {
29091 tree idx = get_template_parm_index (*auto_node);
29092 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
29093 {
29094 /* In an abbreviated function template we didn't know we were dealing
29095 with a function template when we saw the auto return type, so update
29096 it to have the correct level. */
29097 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (*auto_node), false);
29098 PLACEHOLDER_TYPE_CONSTRAINTS (new_auto)
29099 = PLACEHOLDER_TYPE_CONSTRAINTS (*auto_node);
29100 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29101 new_auto = cp_build_qualified_type (new_auto, TYPE_QUALS (*auto_node));
29102 *auto_node = new_auto;
29103 }
29104 }
29105 return type;
29106 }
29107
29108 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29109 'decltype(auto)' or a deduced class template. */
29110
29111 bool
29112 is_auto (const_tree type)
29113 {
29114 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29115 && (TYPE_IDENTIFIER (type) == auto_identifier
29116 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29117 return true;
29118 else
29119 return false;
29120 }
29121
29122 /* for_each_template_parm callback for type_uses_auto. */
29123
29124 int
29125 is_auto_r (tree tp, void */*data*/)
29126 {
29127 return is_auto (tp);
29128 }
29129
29130 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29131 a use of `auto'. Returns NULL_TREE otherwise. */
29132
29133 tree
29134 type_uses_auto (tree type)
29135 {
29136 if (type == NULL_TREE)
29137 return NULL_TREE;
29138 else if (flag_concepts)
29139 {
29140 /* The Concepts TS allows multiple autos in one type-specifier; just
29141 return the first one we find, do_auto_deduction will collect all of
29142 them. */
29143 if (uses_template_parms (type))
29144 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29145 /*visited*/NULL, /*nondeduced*/false);
29146 else
29147 return NULL_TREE;
29148 }
29149 else if (tree *tp = find_type_usage (&type, is_auto))
29150 return *tp;
29151 else
29152 return NULL_TREE;
29153 }
29154
29155 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29156 concepts are enabled, auto is acceptable in template arguments, but
29157 only when TEMPL identifies a template class. Return TRUE if any
29158 such errors were reported. */
29159
29160 bool
29161 check_auto_in_tmpl_args (tree tmpl, tree args)
29162 {
29163 /* If there were previous errors, nevermind. */
29164 if (!args || TREE_CODE (args) != TREE_VEC)
29165 return false;
29166
29167 /* If TMPL is an identifier, we're parsing and we can't tell yet
29168 whether TMPL is supposed to be a type, a function or a variable.
29169 We'll only be able to tell during template substitution, so we
29170 expect to be called again then. If concepts are enabled and we
29171 know we have a type, we're ok. */
29172 if (flag_concepts
29173 && (identifier_p (tmpl)
29174 || (DECL_P (tmpl)
29175 && (DECL_TYPE_TEMPLATE_P (tmpl)
29176 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29177 return false;
29178
29179 /* Quickly search for any occurrences of auto; usually there won't
29180 be any, and then we'll avoid allocating the vector. */
29181 if (!type_uses_auto (args))
29182 return false;
29183
29184 bool errors = false;
29185
29186 tree vec = extract_autos (args);
29187 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29188 {
29189 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29190 error_at (DECL_SOURCE_LOCATION (xauto),
29191 "invalid use of %qT in template argument", xauto);
29192 errors = true;
29193 }
29194
29195 return errors;
29196 }
29197
29198 /* For a given template T, return the vector of typedefs referenced
29199 in T for which access check is needed at T instantiation time.
29200 T is either a FUNCTION_DECL or a RECORD_TYPE.
29201 Those typedefs were added to T by the function
29202 append_type_to_template_for_access_check. */
29203
29204 vec<qualified_typedef_usage_t, va_gc> *
29205 get_types_needing_access_check (tree t)
29206 {
29207 tree ti;
29208 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
29209
29210 if (!t || t == error_mark_node)
29211 return NULL;
29212
29213 if (!(ti = get_template_info (t)))
29214 return NULL;
29215
29216 if (CLASS_TYPE_P (t)
29217 || TREE_CODE (t) == FUNCTION_DECL)
29218 {
29219 if (!TI_TEMPLATE (ti))
29220 return NULL;
29221
29222 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
29223 }
29224
29225 return result;
29226 }
29227
29228 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
29229 tied to T. That list of typedefs will be access checked at
29230 T instantiation time.
29231 T is either a FUNCTION_DECL or a RECORD_TYPE.
29232 TYPE_DECL is a TYPE_DECL node representing a typedef.
29233 SCOPE is the scope through which TYPE_DECL is accessed.
29234 LOCATION is the location of the usage point of TYPE_DECL.
29235
29236 This function is a subroutine of
29237 append_type_to_template_for_access_check. */
29238
29239 static void
29240 append_type_to_template_for_access_check_1 (tree t,
29241 tree type_decl,
29242 tree scope,
29243 location_t location)
29244 {
29245 qualified_typedef_usage_t typedef_usage;
29246 tree ti;
29247
29248 if (!t || t == error_mark_node)
29249 return;
29250
29251 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
29252 || CLASS_TYPE_P (t))
29253 && type_decl
29254 && TREE_CODE (type_decl) == TYPE_DECL
29255 && scope);
29256
29257 if (!(ti = get_template_info (t)))
29258 return;
29259
29260 gcc_assert (TI_TEMPLATE (ti));
29261
29262 typedef_usage.typedef_decl = type_decl;
29263 typedef_usage.context = scope;
29264 typedef_usage.locus = location;
29265
29266 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
29267 }
29268
29269 /* Append TYPE_DECL to the template TEMPL.
29270 TEMPL is either a class type, a FUNCTION_DECL or a TEMPLATE_DECL.
29271 At TEMPL instanciation time, TYPE_DECL will be checked to see
29272 if it can be accessed through SCOPE.
29273 LOCATION is the location of the usage point of TYPE_DECL.
29274
29275 e.g. consider the following code snippet:
29276
29277 class C
29278 {
29279 typedef int myint;
29280 };
29281
29282 template<class U> struct S
29283 {
29284 C::myint mi; // <-- usage point of the typedef C::myint
29285 };
29286
29287 S<char> s;
29288
29289 At S<char> instantiation time, we need to check the access of C::myint
29290 In other words, we need to check the access of the myint typedef through
29291 the C scope. For that purpose, this function will add the myint typedef
29292 and the scope C through which its being accessed to a list of typedefs
29293 tied to the template S. That list will be walked at template instantiation
29294 time and access check performed on each typedefs it contains.
29295 Note that this particular code snippet should yield an error because
29296 myint is private to C. */
29297
29298 void
29299 append_type_to_template_for_access_check (tree templ,
29300 tree type_decl,
29301 tree scope,
29302 location_t location)
29303 {
29304 qualified_typedef_usage_t *iter;
29305 unsigned i;
29306
29307 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
29308
29309 /* Make sure we don't append the type to the template twice. */
29310 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
29311 if (iter->typedef_decl == type_decl && scope == iter->context)
29312 return;
29313
29314 append_type_to_template_for_access_check_1 (templ, type_decl,
29315 scope, location);
29316 }
29317
29318 /* Recursively walk over && expressions searching for EXPR. Return a reference
29319 to that expression. */
29320
29321 static tree *find_template_requirement (tree *t, tree key)
29322 {
29323 if (*t == key)
29324 return t;
29325 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29326 {
29327 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29328 return p;
29329 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29330 return p;
29331 }
29332 return 0;
29333 }
29334
29335 /* Convert the generic type parameters in PARM that match the types given in the
29336 range [START_IDX, END_IDX) from the current_template_parms into generic type
29337 packs. */
29338
29339 tree
29340 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29341 {
29342 tree current = current_template_parms;
29343 int depth = TMPL_PARMS_DEPTH (current);
29344 current = INNERMOST_TEMPLATE_PARMS (current);
29345 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29346
29347 for (int i = 0; i < start_idx; ++i)
29348 TREE_VEC_ELT (replacement, i)
29349 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29350
29351 for (int i = start_idx; i < end_idx; ++i)
29352 {
29353 /* Create a distinct parameter pack type from the current parm and add it
29354 to the replacement args to tsubst below into the generic function
29355 parameter. */
29356 tree node = TREE_VEC_ELT (current, i);
29357 tree o = TREE_TYPE (TREE_VALUE (node));
29358 tree t = copy_type (o);
29359 TEMPLATE_TYPE_PARM_INDEX (t)
29360 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29361 t, 0, 0, tf_none);
29362 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29363 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29364 TYPE_MAIN_VARIANT (t) = t;
29365 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29366 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29367 TREE_VEC_ELT (replacement, i) = t;
29368
29369 /* Replace the current template parameter with new pack. */
29370 TREE_VALUE (node) = TREE_CHAIN (t);
29371
29372 /* Surgically adjust the associated constraint of adjusted parameter
29373 and it's corresponding contribution to the current template
29374 requirements. */
29375 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29376 {
29377 tree id = unpack_concept_check (constr);
29378 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
29379 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29380 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29381
29382 /* If there was a constraint, we also need to replace that in
29383 the template requirements, which we've already built. */
29384 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29385 reqs = find_template_requirement (reqs, constr);
29386 *reqs = fold;
29387 }
29388 }
29389
29390 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29391 TREE_VEC_ELT (replacement, i)
29392 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29393
29394 /* If there are more levels then build up the replacement with the outer
29395 template parms. */
29396 if (depth > 1)
29397 replacement = add_to_template_args (template_parms_to_args
29398 (TREE_CHAIN (current_template_parms)),
29399 replacement);
29400
29401 return tsubst (parm, replacement, tf_none, NULL_TREE);
29402 }
29403
29404 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29405 0..N-1. */
29406
29407 void
29408 declare_integer_pack (void)
29409 {
29410 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29411 build_function_type_list (integer_type_node,
29412 integer_type_node,
29413 NULL_TREE),
29414 NULL_TREE, ECF_CONST);
29415 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29416 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29417 CP_BUILT_IN_INTEGER_PACK);
29418 }
29419
29420 /* Set up the hash tables for template instantiations. */
29421
29422 void
29423 init_template_processing (void)
29424 {
29425 /* FIXME: enable sanitization (PR87847) */
29426 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
29427 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
29428
29429 if (cxx_dialect >= cxx11)
29430 declare_integer_pack ();
29431 }
29432
29433 /* Print stats about the template hash tables for -fstats. */
29434
29435 void
29436 print_template_statistics (void)
29437 {
29438 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29439 "%f collisions\n", (long) decl_specializations->size (),
29440 (long) decl_specializations->elements (),
29441 decl_specializations->collisions ());
29442 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29443 "%f collisions\n", (long) type_specializations->size (),
29444 (long) type_specializations->elements (),
29445 type_specializations->collisions ());
29446 }
29447
29448 #if CHECKING_P
29449
29450 namespace selftest {
29451
29452 /* Verify that build_non_dependent_expr () works, for various expressions,
29453 and that location wrappers don't affect the results. */
29454
29455 static void
29456 test_build_non_dependent_expr ()
29457 {
29458 location_t loc = BUILTINS_LOCATION;
29459
29460 /* Verify constants, without and with location wrappers. */
29461 tree int_cst = build_int_cst (integer_type_node, 42);
29462 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29463
29464 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29465 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29466 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29467
29468 tree string_lit = build_string (4, "foo");
29469 TREE_TYPE (string_lit) = char_array_type_node;
29470 string_lit = fix_string_type (string_lit);
29471 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29472
29473 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29474 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29475 ASSERT_EQ (wrapped_string_lit,
29476 build_non_dependent_expr (wrapped_string_lit));
29477 }
29478
29479 /* Verify that type_dependent_expression_p () works correctly, even
29480 in the presence of location wrapper nodes. */
29481
29482 static void
29483 test_type_dependent_expression_p ()
29484 {
29485 location_t loc = BUILTINS_LOCATION;
29486
29487 tree name = get_identifier ("foo");
29488
29489 /* If no templates are involved, nothing is type-dependent. */
29490 gcc_assert (!processing_template_decl);
29491 ASSERT_FALSE (type_dependent_expression_p (name));
29492
29493 ++processing_template_decl;
29494
29495 /* Within a template, an unresolved name is always type-dependent. */
29496 ASSERT_TRUE (type_dependent_expression_p (name));
29497
29498 /* Ensure it copes with NULL_TREE and errors. */
29499 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29500 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29501
29502 /* A USING_DECL in a template should be type-dependent, even if wrapped
29503 with a location wrapper (PR c++/83799). */
29504 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29505 TREE_TYPE (using_decl) = integer_type_node;
29506 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29507 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29508 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29509 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29510
29511 --processing_template_decl;
29512 }
29513
29514 /* Run all of the selftests within this file. */
29515
29516 void
29517 cp_pt_c_tests ()
29518 {
29519 test_build_non_dependent_expr ();
29520 test_type_dependent_expression_p ();
29521 }
29522
29523 } // namespace selftest
29524
29525 #endif /* #if CHECKING_P */
29526
29527 #include "gt-cp-pt.h"